home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / s920603.zip / NR4HDR.C < prev    next >
C/C++ Source or Header  |  1990-08-20  |  4KB  |  146 lines

  1. /* Net/rom transport layer header conversion routines.
  2.  * Copyright 1989 by Daniel M. Frank, W9NK.  Permission granted for
  3.  * non-commercial distribution only.
  4.  */
  5. #include "global.h"
  6. #include "mbuf.h"
  7. #include "nr4.h"
  8.  
  9. /* Convert a net/rom transport header to host format structure.
  10.  * Return -1 if error, 0 if OK.
  11.  */
  12. int
  13. ntohnr4(hdr,bpp)
  14. register struct nr4hdr *hdr;
  15. struct mbuf **bpp;
  16. {
  17.     unsigned char tbuf[NR4MINHDR];
  18.     int i;
  19.  
  20.     if(pullup(bpp, (char *)tbuf, NR4MINHDR) < NR4MINHDR)
  21.         return -1;
  22.  
  23.     hdr->opcode = tbuf[4];
  24.  
  25.     switch(tbuf[4] & NR4OPCODE){
  26.         case NR4OPPID:        /* protocol ID extension */
  27.             hdr->u.pid.family = tbuf[0];
  28.             hdr->u.pid.proto = tbuf[1];
  29.             break;
  30.         case NR4OPCONRQ:    /* connect request */
  31.             hdr->u.conreq.myindex = tbuf[0];
  32.             hdr->u.conreq.myid = tbuf[1];
  33.             if((i = PULLCHAR(bpp)) == -1)
  34.                 return -1;
  35.             hdr->u.conreq.window = i;
  36.             if(pullup(bpp,hdr->u.conreq.user,AXALEN) < AXALEN)
  37.                 return -1;
  38.             if(pullup(bpp,hdr->u.conreq.node,AXALEN) < AXALEN)
  39.                 return -1;
  40.             break;
  41.         case NR4OPCONAK:    /* connect acknowledge */
  42.             hdr->yourindex = tbuf[0];
  43.             hdr->yourid = tbuf[1];
  44.             hdr->u.conack.myindex = tbuf[2];
  45.             hdr->u.conack.myid = tbuf[3];
  46.             if((i = PULLCHAR(bpp)) == -1)
  47.                 return -1;
  48.             hdr->u.conack.window = i;
  49.             break;
  50.         case NR4OPDISRQ:    /* disconnect request */
  51.             hdr->yourindex = tbuf[0];
  52.             hdr->yourid = tbuf[1];
  53.             break;
  54.         case NR4OPDISAK:    /* disconnect acknowledge */
  55.             hdr->yourindex = tbuf[0];
  56.             hdr->yourid = tbuf[1];
  57.             break;
  58.         case NR4OPINFO:        /* information frame */
  59.             hdr->yourindex = tbuf[0];
  60.             hdr->yourid = tbuf[1];
  61.             hdr->u.info.txseq = tbuf[2];
  62.             hdr->u.info.rxseq = tbuf[3];
  63.             break;
  64.         case NR4OPACK:        /* information acknowledge */
  65.             hdr->yourindex = tbuf[0];
  66.             hdr->yourid = tbuf[1];
  67.             hdr->u.ack.rxseq = tbuf[3];
  68.             break;
  69.         default:        /* what kind of frame is this? */
  70.             return -1;
  71.     }
  72.     return 0;
  73. }
  74.  
  75. /* Convert host-format level 4 header to network format */
  76. struct mbuf *
  77. htonnr4(hdr)
  78. register struct nr4hdr *hdr;
  79. {
  80.     static int16 hlen[NR4NUMOPS] = {5,20,6,5,5,5,5};
  81.     struct mbuf *rbuf;
  82.     register char *cp;
  83.     unsigned char opcode;
  84.  
  85.     opcode = hdr->opcode & NR4OPCODE;
  86.  
  87.     if(opcode >= NR4NUMOPS)
  88.         return NULLBUF;
  89.  
  90.     if(hdr == (struct nr4hdr *)NULL)
  91.         return NULLBUF;
  92.  
  93.     if((rbuf = alloc_mbuf(hlen[opcode])) == NULLBUF)
  94.         return NULLBUF;
  95.  
  96.     rbuf->cnt = hlen[opcode];
  97.     cp = rbuf->data;
  98.  
  99.     cp[4] = hdr->opcode;
  100.     
  101.     switch(opcode){
  102.         case NR4OPPID:
  103.             *cp++ = hdr->u.pid.family;
  104.             *cp = hdr->u.pid.proto;
  105.             break;
  106.         case NR4OPCONRQ:
  107.             *cp++ = hdr->u.conreq.myindex;
  108.             *cp++ = hdr->u.conreq.myid;
  109.             cp += 3; /* skip to sixth byte */
  110.             *cp++ = hdr->u.conreq.window;
  111.             memcpy(cp,hdr->u.conreq.user,AXALEN);
  112.             cp += AXALEN;
  113.             memcpy(cp,hdr->u.conreq.node,AXALEN);
  114.             cp += AXALEN;
  115.             break;
  116.         case NR4OPCONAK:
  117.             *cp++ = hdr->yourindex;
  118.             *cp++ = hdr->yourid;
  119.             *cp++ = hdr->u.conack.myindex;
  120.             *cp++ = hdr->u.conack.myid;
  121.             cp++;    /* already loaded pid */
  122.             *cp = hdr->u.conack.window;
  123.             break;
  124.         case NR4OPDISRQ:
  125.             *cp++ = hdr->yourindex;
  126.             *cp = hdr->yourid;
  127.             break;
  128.         case NR4OPDISAK:
  129.             *cp++ = hdr->yourindex;
  130.             *cp = hdr->yourid;
  131.             break;
  132.         case NR4OPINFO:
  133.             *cp++ = hdr->yourindex;
  134.             *cp++ = hdr->yourid;
  135.             *cp++ = hdr->u.info.txseq;
  136.             *cp = hdr->u.info.rxseq;
  137.             break;
  138.         case NR4OPACK:
  139.             *cp++ = hdr->yourindex;
  140.             *cp++ = hdr->yourid;
  141.             *++cp = hdr->u.ack.rxseq;    /* skip third byte (tricky yuck) */
  142.             break;
  143.     }
  144.     return rbuf;
  145. }
  146.