home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / network / src_1218.zip / NRS.C < prev    next >
C/C++ Source or Header  |  1991-01-27  |  5KB  |  224 lines

  1. /* This module implements the serial line framing method used by
  2.  * net/rom nodes.  This allows the net/rom software to talk to
  3.  * an actual net/rom over its serial interface, which is useful
  4.  * if we want to do packet switching for multi-line wormholes.
  5.  *
  6.  * Copyright 1989 Dan Frank, W9NK
  7.  */
  8. #include <stdio.h>
  9. #include "global.h"
  10. #include "mbuf.h"
  11. #include "iface.h"
  12. #include "pktdrvr.h"
  13. #include "ax25.h"
  14. #include "nrs.h"
  15. #include "asy.h"
  16. #include "trace.h"
  17. #include "commands.h"
  18.  
  19. static struct mbuf *nrs_encode __ARGS((struct mbuf *bp));
  20. static struct mbuf *nrs_decode __ARGS((int dev,char c));
  21.  
  22. /* control structures, sort of overlayed on async control blocks */
  23. struct nrs Nrs[ASY_MAX];
  24.  
  25. /* Send a raw net/rom serial frame */
  26. int
  27. nrs_raw(iface,bp)
  28. struct iface *iface;
  29. struct mbuf *bp;
  30. {
  31.     struct mbuf *bp1;
  32.  
  33.     dump(iface,IF_TRACE_OUT,CL_AX25,bp);
  34.     iface->rawsndcnt++;
  35.     iface->lastsent = secclock();
  36.  
  37.     if((bp1 = nrs_encode(bp)) == NULLBUF){
  38.         free_p(bp);
  39.         return -1;
  40.     }
  41.     return Nrs[iface->xdev].send(iface->dev,bp1);
  42. }
  43.  
  44. /* Encode a packet in net/rom serial format */
  45. static struct mbuf *
  46. nrs_encode(bp)
  47. struct mbuf *bp;
  48. {
  49.     struct mbuf *lbp;    /* Mbuf containing line-ready packet */
  50.     register char *cp;
  51.     int c;
  52.     unsigned char csum = 0;
  53.  
  54.     /* Allocate output mbuf that's twice as long as the packet.
  55.      * This is a worst-case guess (consider a packet full of STX's!)
  56.      * Add five bytes for STX, ETX, checksum, and two nulls.
  57.      */
  58.     lbp = alloc_mbuf((int16)(2*len_p(bp) + 5));
  59.     if(lbp == NULLBUF){
  60.         /* No space; drop */
  61.         free_p(bp);
  62.         return NULLBUF;
  63.     }
  64.     cp = lbp->data;
  65.  
  66.     *cp++ = STX;
  67.  
  68.     /* Copy input to output, escaping special characters */
  69.     while((c = PULLCHAR(&bp)) != -1){
  70.         switch(c){
  71.         case STX:
  72.         case ETX:
  73.         case DLE:
  74.             *cp++ = DLE;
  75.             /* notice drop through to default */
  76.         default:
  77.             *cp++ = c;
  78.         }
  79.         csum += c;
  80.     }
  81.     *cp++ = ETX;
  82.     *cp++ = csum;
  83.     *cp++ = NUL;
  84.     *cp++ = NUL;
  85.     
  86.     lbp->cnt = cp - lbp->data;
  87.     return lbp;
  88. }
  89. /* Process incoming bytes in net/rom serial format
  90.  * When a buffer is complete, return it; otherwise NULLBUF
  91.  */
  92. static struct mbuf *
  93. nrs_decode(dev,c)
  94. int dev;    /* net/rom unit number */
  95. char c;        /* Incoming character */
  96. {
  97.     struct mbuf *bp;
  98.     register struct nrs *sp;
  99.  
  100.     sp = &Nrs[dev];
  101.     switch(sp->state) {
  102.         case NRS_INTER:
  103.             if(uchar(c) == STX) {    /* look for start of frame */
  104.                 sp->state = NRS_INPACK;    /* we're in a packet */
  105.                 sp->csum = 0;                /* reset checksum */
  106.             }
  107.             return NULLBUF;
  108.         case NRS_CSUM:
  109.             bp = sp->rbp;
  110.             sp->rbp = NULLBUF;
  111.             sp->rcnt = 0;
  112.             sp->state = NRS_INTER;    /* go back to inter-packet state */
  113.             if(sp->csum == uchar(c)) {
  114.                 sp->packets++;
  115.             } else {
  116.                 free_p(bp);    /* drop packet with bad checksum */
  117.                 bp = NULLBUF;
  118.                 sp->errors++;    /* increment error count */
  119.             }
  120.             return bp;
  121.         case NRS_ESCAPE:
  122.             sp->state = NRS_INPACK;    /* end of escape */
  123.             break;            /* this will drop through to char processing */
  124.         case NRS_INPACK:
  125.             switch (uchar(c)) {
  126.             /* If we see an STX in a packet, assume that previous */
  127.             /* packet was trashed, and start a new packet */
  128.             case STX:
  129.                 free_p(sp->rbp);
  130.                 sp->rbp = NULLBUF;
  131.                 sp->rcnt = 0;
  132.                 sp->csum = 0;
  133.                 sp->errors++;
  134.                 return NULLBUF;
  135.             case ETX:
  136.                 sp->state = NRS_CSUM;    /* look for checksum */
  137.                 return NULLBUF;
  138.             case DLE:
  139.                 sp->state = NRS_ESCAPE;
  140.                 return NULLBUF;
  141.             }
  142.     }
  143.     /* If we get to here, it's with a character that's part of the packet.
  144.      * Make sure there's space for it.
  145.      */
  146.     if(sp->rbp == NULLBUF){
  147.         /* Allocate first mbuf for new packet */
  148.         if((sp->rbp1 = sp->rbp = alloc_mbuf(NRS_ALLOC)) == NULLBUF) {
  149.             sp->state = NRS_INTER;
  150.             return NULLBUF; /* No memory, drop */
  151.         }
  152.         sp->rcp = sp->rbp->data;
  153.     } else if(sp->rbp1->cnt == NRS_ALLOC){
  154.         /* Current mbuf is full; link in another */
  155.         if((sp->rbp1->next = alloc_mbuf(NRS_ALLOC)) == NULLBUF){
  156.             /* No memory, drop whole thing */
  157.             free_p(sp->rbp);
  158.             sp->rbp = NULLBUF;
  159.             sp->rcnt = 0;
  160.             sp->state = NRS_INTER;
  161.             return NULLBUF;
  162.         }
  163.         sp->rbp1 = sp->rbp1->next;
  164.         sp->rcp = sp->rbp1->data;
  165.     }
  166.     /* Store the character, increment fragment and total
  167.      * byte counts
  168.      */
  169.     *sp->rcp++ = c;
  170.     sp->rbp1->cnt++;
  171.     sp->rcnt++;
  172.     sp->csum += uchar(c);    /* add to checksum */
  173.     return NULLBUF;
  174. }
  175.  
  176. /* Process net/rom serial line I/O */
  177. void
  178. nrs_recv(dev,v1,v2)
  179. int dev;
  180. void *v1;
  181. void *v2;
  182. {
  183.     char c;
  184.     struct mbuf *bp,*nbp;
  185.     struct phdr phdr;
  186.  
  187.     /* Process any pending input */
  188.     for(;;){
  189.         c = Nrs[dev].get(Nrs[dev].iface->dev);
  190.         if((bp = nrs_decode(dev,c)) == NULLBUF)
  191.             continue;
  192.         if((nbp = pushdown(bp,sizeof(phdr))) == NULLBUF){
  193.             free_p(bp);
  194.             continue;
  195.         }
  196.         phdr.iface = Nrs[dev].iface;
  197.         phdr.type = CL_AX25;
  198.         memcpy(&nbp->data[0],(char *)&phdr,sizeof(phdr));
  199.         enqueue(&Hopper,nbp);
  200.     }
  201.  
  202. }
  203. /* donrstat:  display status of active net/rom serial interfaces */
  204. int
  205. donrstat(argc,argv,p)
  206. int argc;
  207. char *argv[];
  208. void *p;
  209. {
  210.     register struct nrs *np;
  211.     register int i;
  212.  
  213.     tprintf("Interface   RcvB  NumReceived  CSumErrors\n");
  214.  
  215.     for(i = 0, np = Nrs; i < ASY_MAX; i++, np++)
  216.         if(np->iface != NULLIF)
  217.             if(tprintf(" %8s   %4d   %10lu  %10lu\n",
  218.              np->iface->name, np->rcnt,
  219.              np->packets, np->errors) == EOF)
  220.                 break;
  221.  
  222.     return 0;
  223. }
  224.