home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR3 / KA9Q212.ZIP / NR4HDR.C < prev    next >
C/C++ Source or Header  |  1993-07-16  |  4KB  |  157 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.  
  6. /****************************************************************************
  7. *    $Id: nr4hdr.c 1.2 93/07/16 11:47:33 ROOT_DOS Exp $
  8. *    14 Jul 93    1.2        GT    Fix warnings.                                    *
  9. ****************************************************************************/
  10.  
  11. #include "global.h"
  12. #include "mbuf.h"
  13. #include "nr4.h"
  14. #include "ip.h"
  15.  
  16. #if    NETROM
  17.  
  18. /* Convert a net/rom transport header to host format structure.
  19.  * Return -1 if error, 0 if OK.
  20.  */
  21. int
  22. ntohnr4(hdr,bpp)
  23. register struct nr4hdr *hdr;
  24. struct mbuf **bpp;
  25. {
  26.     unsigned char tbuf[NR4MINHDR];
  27.     int i;
  28.  
  29.     if(pullup(bpp, (char *)tbuf, NR4MINHDR) < NR4MINHDR)
  30.         return -1;
  31.  
  32.     hdr->opcode = tbuf[4];
  33.  
  34.     switch(tbuf[4] & NR4OPCODE){
  35.         case NR4OPPID:        /* protocol ID extension */
  36.             hdr->u.pid.family = tbuf[0];
  37.             hdr->u.pid.proto = tbuf[1];
  38.             break;
  39.         case NR4OPCONRQ:    /* connect request */
  40.             hdr->u.conreq.myindex = tbuf[0];
  41.             hdr->u.conreq.myid = tbuf[1];
  42.             if((i = PULLCHAR(bpp)) == -1)
  43.                 return -1;
  44.             hdr->u.conreq.window = i;
  45.             if(pullup(bpp,hdr->u.conreq.user,AXALEN) < AXALEN)
  46.                 return -1;
  47.             if(pullup(bpp,hdr->u.conreq.node,AXALEN) < AXALEN)
  48.                 return -1;
  49.             break;
  50.         case NR4OPCONAK:    /* connect acknowledge */
  51.             hdr->yourindex = tbuf[0];
  52.             hdr->yourid = tbuf[1];
  53.             hdr->u.conack.myindex = tbuf[2];
  54.             hdr->u.conack.myid = tbuf[3];
  55.             if((i = PULLCHAR(bpp)) == -1)
  56.                 return -1;
  57.             hdr->u.conack.window = i;
  58.             break;
  59.         case NR4OPDISRQ:    /* disconnect request */
  60.             hdr->yourindex = tbuf[0];
  61.             hdr->yourid = tbuf[1];
  62.             break;
  63.         case NR4OPDISAK:    /* disconnect acknowledge */
  64.             hdr->yourindex = tbuf[0];
  65.             hdr->yourid = tbuf[1];
  66.             break;
  67.         case NR4OPINFO:        /* information frame */
  68.             hdr->yourindex = tbuf[0];
  69.             hdr->yourid = tbuf[1];
  70.             hdr->u.info.txseq = tbuf[2];
  71.             hdr->u.info.rxseq = tbuf[3];
  72.             break;
  73.         case NR4OPACK:        /* information acknowledge */
  74.             hdr->yourindex = tbuf[0];
  75.             hdr->yourid = tbuf[1];
  76.             hdr->u.ack.rxseq = tbuf[3];
  77.             break;
  78.         default:        /* what kind of frame is this? */
  79.             return -1;
  80.     }
  81.     return 0;
  82. }
  83.  
  84. /* Convert host-format level 4 header to network format */
  85. struct mbuf *
  86. htonnr4(hdr)
  87. register struct nr4hdr *hdr;
  88. {
  89.     static int16 hlen[NR4NUMOPS] = {5,20,6,5,5,5,5};
  90.     struct mbuf *rbuf;
  91.     register char *cp;
  92.     unsigned char opcode;
  93.  
  94.     opcode = hdr->opcode & NR4OPCODE;
  95.  
  96.     if(opcode >= NR4NUMOPS)
  97.         return NULLBUF;
  98.  
  99.     if(hdr == (struct nr4hdr *)NULL)
  100.         return NULLBUF;
  101.  
  102.     if((rbuf = alloc_mbuf(hlen[opcode])) == NULLBUF)
  103.         return NULLBUF;
  104.  
  105.     rbuf->cnt = hlen[opcode];
  106.     cp = rbuf->data;
  107.  
  108.     cp[4] = hdr->opcode;
  109.     
  110.     switch(opcode){
  111.         case NR4OPPID:
  112.             *cp++ = hdr->u.pid.family;
  113.             *cp = hdr->u.pid.proto;
  114.             break;
  115.         case NR4OPCONRQ:
  116.             *cp++ = hdr->u.conreq.myindex;
  117.             *cp++ = hdr->u.conreq.myid;
  118.             cp += 3; /* skip to sixth byte */
  119.             *cp++ = hdr->u.conreq.window;
  120.             memcpy(cp,hdr->u.conreq.user,AXALEN);
  121.             cp += AXALEN;
  122.             memcpy(cp,hdr->u.conreq.node,AXALEN);
  123.             cp += AXALEN;
  124.             break;
  125.         case NR4OPCONAK:
  126.             *cp++ = hdr->yourindex;
  127.             *cp++ = hdr->yourid;
  128.             *cp++ = hdr->u.conack.myindex;
  129.             *cp++ = hdr->u.conack.myid;
  130.             cp++;    /* already loaded pid */
  131.             *cp = hdr->u.conack.window;
  132.             break;
  133.         case NR4OPDISRQ:
  134.             *cp++ = hdr->yourindex;
  135.             *cp = hdr->yourid;
  136.             break;
  137.         case NR4OPDISAK:
  138.             *cp++ = hdr->yourindex;
  139.             *cp = hdr->yourid;
  140.             break;
  141.         case NR4OPINFO:
  142.             *cp++ = hdr->yourindex;
  143.             *cp++ = hdr->yourid;
  144.             *cp++ = hdr->u.info.txseq;
  145.             *cp = hdr->u.info.rxseq;
  146.             break;
  147.         case NR4OPACK:
  148.             *cp++ = hdr->yourindex;
  149.             *cp++ = hdr->yourid;
  150.             *++cp = hdr->u.ack.rxseq;    /* skip third byte (tricky yuck) */
  151.             break;
  152.     }
  153.     return rbuf;
  154. }
  155.  
  156. #endif    /* NETROM */
  157.