home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / NETWORK / SRC_0618.ZIP / ASY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-16  |  6.0 KB  |  268 lines

  1. /* Generic serial line interface routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include <stdio.h>
  5. #include "global.h"
  6. #include "config.h"
  7. #include "proc.h"
  8. #include "iface.h"
  9. #include "netuser.h"
  10. #include "slhc.h"
  11. #include "8250.h"
  12. #include "asy.h"
  13. #include "ax25.h"
  14. #include "kiss.h"
  15. #include "nrs.h"
  16. #include "pktdrvr.h"
  17. #include "slip.h"
  18. #include "ppp.h"
  19. #include "commands.h"
  20.  
  21. static int asy_detach __ARGS((struct iface *ifp));
  22.  
  23.  
  24. /* Attach a serial interface to the system
  25.  * argv[0]: hardware type, must be "asy"
  26.  * argv[1]: I/O address, e.g., "0x3f8"
  27.  * argv[2]: vector, e.g., "4"
  28.  * argv[3]: mode, may be:
  29.  *        "slip" (point-to-point SLIP)
  30.  *        "ax25" (AX.25 frame format in SLIP for raw TNC)
  31.  *        "nrs" (NET/ROM format serial protocol)
  32.  *        "ppp" (Point-to-Point Protocol, RFC1171, RFC1172)
  33.  * argv[4]: interface label, e.g., "sl0"
  34.  * argv[5]: receiver ring buffer size in bytes
  35.  * argv[6]: maximum transmission unit, bytes
  36.  * argv[7]: interface speed, e.g, "9600"
  37.  * argv[8]: optional flags,
  38.  *        'c' for cts flow control;
  39.  *        'r' for RLSD (RS232 pin 8, CD) physical link up/down;
  40.  *        'v' for Van Jacobson TCP header compression (SLIP only,
  41.  *            use ppp command for VJ compression with PPP);
  42.  */
  43. int
  44. asy_attach(argc,argv,p)
  45. int argc;
  46. char *argv[];
  47. void *p;
  48. {
  49.     register struct iface *ifp;
  50.     struct asy *asyp;
  51.     char *ifn;
  52.     int dev;
  53.     int xdev;
  54.     int trigchar = -1;
  55.     char cts = FALSE;
  56.     char rlsd = FALSE;
  57. #if    defined(SLIP) || defined(AX25)
  58.     struct slip *sp;
  59. #endif
  60. #ifdef    NRS
  61.     struct nrs *np;
  62. #endif
  63.  
  64.     if(if_lookup(argv[4]) != NULLIF){
  65.         tprintf("Interface %s already exists\n",argv[4]);
  66.         return -1;
  67.     }
  68.     /* Find unused asy control block */
  69.     for(dev=0;dev < ASY_MAX;dev++){
  70.         asyp = &Asy[dev];
  71.         if(asyp->iface == NULLIF)
  72.             break;
  73.     }
  74.     if(dev >= ASY_MAX){
  75.         tprintf("Too many asynch controllers\n");
  76.         return -1;
  77.     }
  78.  
  79.     /* Create interface structure and fill in details */
  80.     ifp = (struct iface *)callocw(1,sizeof(struct iface));
  81.     ifp->addr = Ip_addr;
  82.     ifp->name = strdup(argv[4]);
  83.     ifp->mtu = atoi(argv[6]);
  84.     ifp->dev = dev;
  85.     ifp->stop = asy_detach;
  86.  
  87. #ifdef    SLIP
  88.     if(stricmp(argv[3],"SLIP") == 0) {
  89.         for(xdev = 0;xdev < SLIP_MAX;xdev++){
  90.             sp = &Slip[xdev];
  91.             if(sp->iface == NULLIF)
  92.                 break;
  93.         }
  94.         if(xdev >= SLIP_MAX) {
  95.             tprintf("Too many slip devices\n");
  96.             return -1;
  97.         }
  98.         setencap(ifp,"SLIP");
  99.         ifp->ioctl = asy_ioctl;
  100.         ifp->raw = slip_raw;
  101.         ifp->status = slip_status;
  102.         ifp->flags = 0;
  103.         ifp->xdev = xdev;
  104.  
  105.         sp->iface = ifp;
  106.         sp->send = asy_send;
  107.         sp->get = get_asy;
  108.         sp->type = CL_SERIAL_LINE;
  109.         trigchar = FR_END;
  110. #ifdef VJCOMPRESS
  111.         if((argc > 8) && (strchr(argv[8],'v') != NULLCHAR)) {
  112.             sp->escaped |= SLIP_VJCOMPR;
  113.             sp->slcomp = slhc_init(16,16);
  114.         }
  115. #else
  116.         sp->slcomp = NULL;
  117. #endif    /* VJCOMPRESS */
  118.         ifp->rxproc = newproc( ifn = if_name( ifp, " rx" ),
  119.             256,asy_rx,xdev,NULL,NULL,0);
  120.         free(ifn);
  121.     } else
  122. #endif
  123. #ifdef    AX25
  124.     if(stricmp(argv[3],"AX25") == 0) {
  125.         /* Set up a SLIP link to use AX.25 */
  126.         for(xdev = 0;xdev < SLIP_MAX;xdev++){
  127.             sp = &Slip[xdev];
  128.             if(sp->iface == NULLIF)
  129.                 break;
  130.         }
  131.         if(xdev >= SLIP_MAX) {
  132.             tprintf("Too many slip devices\n");
  133.             return -1;
  134.         }
  135.         setencap(ifp,"AX25");
  136.         ifp->ioctl = kiss_ioctl;
  137.         ifp->raw = kiss_raw;
  138.         ifp->status = slip_status;
  139.  
  140.         if(ifp->hwaddr == NULLCHAR)
  141.             ifp->hwaddr = mallocw(AXALEN);
  142.         memcpy(ifp->hwaddr,Mycall,AXALEN);
  143.         ifp->xdev = xdev;
  144.  
  145.         sp->iface = ifp;
  146.         sp->send = asy_send;
  147.         sp->get = get_asy;
  148.         sp->type = CL_KISS;
  149.         trigchar = FR_END;
  150.         ifp->rxproc = newproc( ifn = if_name( ifp, " rx" ),
  151.             256,asy_rx,xdev,NULL,NULL,0);
  152.         free(ifn);
  153.     } else
  154. #endif
  155. #ifdef    NRS
  156.     if(stricmp(argv[3],"NRS") == 0) {
  157.         /* Set up a net/rom serial iface */
  158.         for(xdev = 0;xdev < SLIP_MAX;xdev++){
  159.             np = &Nrs[xdev];
  160.             if(np->iface == NULLIF)
  161.                 break;
  162.         }
  163.         if(xdev >= SLIP_MAX) {
  164.             tprintf("Too many nrs devices\n");
  165.             return -1;
  166.         }
  167.         /* no call supplied? */
  168.         setencap(ifp,"AX25");
  169.         ifp->ioctl = asy_ioctl;
  170.         ifp->raw = nrs_raw;
  171.  
  172.         ifp->hwaddr = mallocw(AXALEN);
  173.         memcpy(ifp->hwaddr,Mycall,AXALEN);
  174.         ifp->xdev = xdev;
  175.         np->iface = ifp;
  176.         np->send = asy_send;
  177.         np->get = get_asy;
  178.         trigchar = ETX;
  179.         ifp->rxproc = newproc( ifn = if_name( ifp, " nrs" ),
  180.             256,nrs_recv,xdev,NULL,NULL,0);
  181.         free(ifn);
  182.     } else
  183. #endif
  184. #ifdef    PPP
  185.     if(stricmp(argv[3],"PPP") == 0) {
  186.         /* Setup for Point-to-Point Protocol */
  187.         trigchar = HDLC_FLAG;
  188.         setencap(ifp,"PPP");
  189.         ifp->ioctl = asy_ioctl;
  190.         ifp->flags = FALSE;
  191.  
  192.         /* Initialize parameters for various PPP phases/protocols */
  193.         if((argc > 8) && (strchr(argv[8],'r') != NULLCHAR))
  194.             rlsd = TRUE;
  195.  
  196.         if (ppp_init(ifp) != 0) {
  197.             tprintf("Cannot allocate PPP control block\n");
  198.             free(ifp->name);
  199.             free((char *)ifp);
  200.             return -1;
  201.         }
  202.     } else
  203. #endif
  204.     {
  205.         tprintf("Mode %s unknown for interface %s\n",
  206.             argv[3],argv[4]);
  207.         free(ifp->name);
  208.         free(ifp);
  209.         return -1;
  210.     }
  211.  
  212.     /* Link in the interface */
  213.     ifp->next = Ifaces;
  214.     Ifaces = ifp;
  215.  
  216.     if((argc > 8) && (strchr(argv[8],'c') != NULLCHAR))
  217.         cts = TRUE;
  218.  
  219.     asy_init(dev,ifp,argv[1],argv[2],(int16)atol(argv[5]),
  220.         trigchar,cts,rlsd,(int16)atol(argv[7]));
  221.     return 0;
  222. }
  223.  
  224.  
  225. static int
  226. asy_detach(ifp)
  227. struct iface *ifp;
  228. {
  229.     asy_stop(ifp);
  230.  
  231. #ifdef    SLIP
  232.     if(stricmp(ifp->iftype->name,"SLIP") == 0) {
  233.         Slip[ifp->xdev].iface = NULLIF;
  234. #ifdef VJCOMPRESS
  235.         slhc_free( Slip[ifp->xdev].slcomp );
  236.         Slip[ifp->xdev].slcomp = NULL;
  237. #endif    /* VJCOMPRESS */
  238.     } else
  239. #endif
  240. #ifdef    AX25
  241.     if(stricmp(ifp->iftype->name,"AX25") == 0
  242.      && Slip[ifp->xdev].iface == ifp ) {
  243.         Slip[ifp->xdev].iface = NULLIF;
  244.     } else
  245. #endif
  246. #ifdef    NRS
  247.     if(stricmp(ifp->iftype->name,"AX25") == 0
  248.      && Nrs[ifp->xdev].iface == ifp ) {
  249.         Nrs[ifp->xdev].iface = NULLIF;
  250.     } else
  251. #endif
  252. #ifdef    PPP
  253.     if(stricmp(ifp->iftype->name,"PPP") == 0) {
  254.         ppp_free(ifp);
  255.     } else
  256. #endif
  257.     {
  258.         tprintf("invalid type %s for interface %s\n",
  259.             ifp->iftype->name, ifp->name);
  260.         free(ifp->name);
  261.         free(ifp);
  262.         return -1;
  263.     }
  264.     return 0;
  265. }
  266.  
  267.  
  268.