home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / NR4HDR.C < prev    next >
C/C++ Source or Header  |  1994-04-17  |  6KB  |  190 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. #ifdef NETROM
  7. #include "mbuf.h"
  8. #include "nr4.h"
  9.   
  10. /* Convert a net/rom transport header to host format structure.
  11.  * Return -1 if error, 0 if OK.
  12.  */
  13. int
  14. ntohnr4(hdr,bpp)
  15. register struct nr4hdr *hdr;
  16. struct mbuf **bpp;
  17. {
  18.     unsigned char tbuf[NR4MINHDR];
  19.     int i;
  20.   
  21.     if(pullup(bpp, (char *)tbuf, NR4MINHDR) < NR4MINHDR)
  22.         return -1;
  23.   
  24.     hdr->opcode = tbuf[4];
  25.     hdr->flags = 0;
  26.   
  27.     switch(tbuf[4] & NR4OPCODE){
  28.         case NR4OPPID:      /* protocol ID extension */
  29.             hdr->u.pid.family = tbuf[0];
  30.             hdr->u.pid.proto = tbuf[1];
  31.             break;
  32.         case NR4OPCONRQ:    /* connect request */
  33.             hdr->u.conreq.myindex = tbuf[0];
  34.             hdr->u.conreq.myid = tbuf[1];
  35.             if((i = PULLCHAR(bpp)) == -1)
  36.                 return -1;
  37.             hdr->u.conreq.window = i;
  38.             if(pullup(bpp,hdr->u.conreq.user,AXALEN) < AXALEN)
  39.                 return -1;
  40.             if(pullup(bpp,hdr->u.conreq.node,AXALEN) < AXALEN)
  41.                 return -1;
  42. #ifdef G8BPQ
  43.             /* See if they are sending us two extra bytes - WG7J */
  44.             if(pullup(bpp,(char *)&hdr->u.conreq.t4init,2) == 2)
  45.                 hdr->flags |= NR4_G8BPQRTT;
  46.             else
  47. #endif
  48.                 hdr->u.conreq.t4init = 0;
  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. #ifdef G8BPQ
  59.             /* Is there an extra byte ? - WG7J */
  60.             if((i = PULLCHAR(bpp)) != -1) {
  61.                 hdr->flags = NR4_G8BPQTTL;
  62.                 hdr->u.conack.ttl = i;
  63.             }
  64. #endif
  65.             break;
  66.         case NR4OPDISRQ:    /* disconnect request */
  67.             hdr->yourindex = tbuf[0];
  68.             hdr->yourid = tbuf[1];
  69.             break;
  70.         case NR4OPDISAK:    /* disconnect acknowledge */
  71.             hdr->yourindex = tbuf[0];
  72.             hdr->yourid = tbuf[1];
  73.             break;
  74.         case NR4OPINFO:     /* information frame */
  75.             hdr->yourindex = tbuf[0];
  76.             hdr->yourid = tbuf[1];
  77.             hdr->u.info.txseq = tbuf[2];
  78.             hdr->u.info.rxseq = tbuf[3];
  79.             break;
  80.         case NR4OPACK:      /* information acknowledge */
  81.             hdr->yourindex = tbuf[0];
  82.             hdr->yourid = tbuf[1];
  83.             /* tbuf[2], ordinarily used for txseq,
  84.                is not significant for info ack */
  85.             hdr->u.ack.rxseq = tbuf[3];
  86.             break;
  87.         default:        /* what kind of frame is this? */
  88.             return -1;
  89.     }
  90.     return 0;
  91. }
  92.   
  93. /* Convert host-format level 4 header to network format */
  94. struct mbuf *
  95. htonnr4(hdr)
  96. struct nr4hdr *hdr;
  97. {
  98. #ifdef G8BPQ
  99.     static int16 hlen[NR4NUMOPS] = {5,22,7,5,5,5,5};
  100. #else
  101.     static int16 hlen[NR4NUMOPS] = {5,20,6,5,5,5,5};
  102. #endif
  103.     struct mbuf *rbuf;
  104.     register char *cp;
  105.     unsigned char opcode;
  106.   
  107.     opcode = hdr->opcode & NR4OPCODE;
  108.   
  109.     if(opcode >= NR4NUMOPS)
  110.         return NULLBUF;
  111.   
  112.     if(hdr == (struct nr4hdr *)NULL)
  113.         return NULLBUF;
  114.   
  115.     if((rbuf = alloc_mbuf(hlen[opcode])) == NULLBUF)
  116.         return NULLBUF;
  117.   
  118.     rbuf->cnt = hlen[opcode];
  119.     cp = rbuf->data;
  120.   
  121.     cp[4] = hdr->opcode;
  122.   
  123.     switch(opcode){
  124.         case NR4OPPID:
  125.             *cp++ = hdr->u.pid.family;
  126.             *cp = hdr->u.pid.proto;
  127.             break;
  128.         case NR4OPCONRQ:
  129.             *cp++ = hdr->u.conreq.myindex;
  130.             *cp++ = hdr->u.conreq.myid;
  131.             cp += 3; /* skip to sixth byte */
  132.             *cp++ = hdr->u.conreq.window;
  133.             memcpy(cp,hdr->u.conreq.user,AXALEN);
  134.             cp += AXALEN;
  135.             memcpy(cp,hdr->u.conreq.node,AXALEN);
  136. #ifdef G8BPQ
  137.             if(G8bpq) {
  138.                 /* Add the initial transport layer timeout in seconds,
  139.                  * as G8BPQ does it. - WG7J
  140.                  */
  141.                 cp += AXALEN;
  142.                 *(unsigned int *)cp = (unsigned int)(Nr4irtt / 1000);
  143.             } else
  144.                 rbuf->cnt -= 2;
  145. #endif
  146.             break;
  147.         case NR4OPCONAK:
  148.             *cp++ = hdr->yourindex;
  149.             *cp++ = hdr->yourid;
  150.             *cp++ = hdr->u.conack.myindex;
  151.             *cp++ = hdr->u.conack.myid;
  152.             cp++;   /* already loaded pid */
  153.             *cp = hdr->u.conack.window;
  154. #ifdef G8BPQ
  155.             if(G8bpq && (hdr->flags & NR4_G8BPQMASK)) {
  156.                 /* Ala g8bpq, add the ttl value we use - WG7J */
  157.                 *(++cp) = (unsigned char) Nr_ttl;
  158.             } else
  159.                 rbuf->cnt--;
  160. #endif
  161.             break;
  162.         case NR4OPDISRQ:
  163.             *cp++ = hdr->yourindex;
  164.             *cp = hdr->yourid;
  165.             break;
  166.         case NR4OPDISAK:
  167.             *cp++ = hdr->yourindex;
  168.             *cp = hdr->yourid;
  169.             break;
  170.         case NR4OPINFO:
  171.             *cp++ = hdr->yourindex;
  172.             *cp++ = hdr->yourid;
  173.             *cp++ = hdr->u.info.txseq;
  174.             *cp = hdr->u.info.rxseq;
  175.             break;
  176.         case NR4OPACK:
  177.             *cp++ = hdr->yourindex;
  178.             *cp++ = hdr->yourid;
  179.             *cp++ = 0;  /* txseq field is don't care, but
  180.                        emulate real netrom and G8BPQ
  181.                        behavior with a zero -- N1BEE */
  182.             *cp = hdr->u.ack.rxseq;
  183.             break;
  184.     }
  185.     return rbuf;
  186. }
  187.   
  188. #endif /* NETROM */
  189.   
  190.