home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / network / src_1218.zip / ASY.C < prev    next >
C/C++ Source or Header  |  1991-09-21  |  6KB  |  260 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.  *        'v' for Van Jacobson TCP header compression (SLIP only,
  39.  *            use ppp command for VJ compression with PPP);
  40.  */
  41. int
  42. asy_attach(argc,argv,p)
  43. int argc;
  44. char *argv[];
  45. void *p;
  46. {
  47.     register struct iface *ifp;
  48.     struct asy *asyp;
  49.     char *ifn;
  50.     int dev;
  51.     int xdev;
  52.     int trigchar = -1;
  53.     char monitor = FALSE;
  54. #if    defined(SLIP) || defined(AX25)
  55.     struct slip *sp;
  56. #endif
  57. #ifdef    NRS
  58.     struct nrs *np;
  59. #endif
  60.  
  61.     if(if_lookup(argv[4]) != NULLIF){
  62.         tprintf("Interface %s already exists\n",argv[4]);
  63.         return -1;
  64.     }
  65.     /* Find unused asy control block */
  66.     for(dev=0;dev < ASY_MAX;dev++){
  67.         asyp = &Asy[dev];
  68.         if(asyp->iface == NULLIF)
  69.             break;
  70.     }
  71.     if(dev >= ASY_MAX){
  72.         tprintf("Too many asynch controllers\n");
  73.         return -1;
  74.     }
  75.  
  76.     /* Create interface structure and fill in details */
  77.     ifp = (struct iface *)callocw(1,sizeof(struct iface));
  78.     ifp->addr = Ip_addr;
  79.     ifp->name = strdup(argv[4]);
  80.     ifp->mtu = atoi(argv[6]);
  81.     ifp->dev = dev;
  82.     ifp->stop = asy_detach;
  83.  
  84. #ifdef    SLIP
  85.     if(stricmp(argv[3],"SLIP") == 0) {
  86.         for(xdev = 0;xdev < SLIP_MAX;xdev++){
  87.             sp = &Slip[xdev];
  88.             if(sp->iface == NULLIF)
  89.                 break;
  90.         }
  91.         if(xdev >= SLIP_MAX) {
  92.             tprintf("Too many slip devices\n");
  93.             return -1;
  94.         }
  95.         setencap(ifp,"SLIP");
  96.         ifp->ioctl = asy_ioctl;
  97.         ifp->raw = slip_raw;
  98.         ifp->show = slip_status;
  99.         ifp->flags = 0;
  100.         ifp->xdev = xdev;
  101.  
  102.         sp->iface = ifp;
  103.         sp->send = asy_send;
  104.         sp->get = get_asy;
  105.         sp->type = CL_SERIAL_LINE;
  106.         trigchar = FR_END;
  107. #ifdef VJCOMPRESS
  108.         if((argc > 8) && (strchr(argv[8],'v') != NULLCHAR)) {
  109.             sp->escaped |= SLIP_VJCOMPR;
  110.             sp->slcomp = slhc_init(16,16);
  111.         }
  112. #else
  113.         sp->slcomp = NULL;
  114. #endif    /* VJCOMPRESS */
  115.         ifp->rxproc = newproc( ifn = if_name( ifp, " rx" ),
  116.             256,asy_rx,xdev,NULL,NULL,0);
  117.         free(ifn);
  118.     } else
  119. #endif
  120. #ifdef    AX25
  121.     if(stricmp(argv[3],"AX25") == 0) {
  122.         /* Set up a SLIP link to use AX.25 */
  123.         for(xdev = 0;xdev < SLIP_MAX;xdev++){
  124.             sp = &Slip[xdev];
  125.             if(sp->iface == NULLIF)
  126.                 break;
  127.         }
  128.         if(xdev >= SLIP_MAX) {
  129.             tprintf("Too many slip devices\n");
  130.             return -1;
  131.         }
  132.         setencap(ifp,"AX25");
  133.         ifp->ioctl = kiss_ioctl;
  134.         ifp->raw = kiss_raw;
  135.         ifp->show = slip_status;
  136.  
  137.         if(ifp->hwaddr == NULLCHAR)
  138.             ifp->hwaddr = mallocw(AXALEN);
  139.         memcpy(ifp->hwaddr,Mycall,AXALEN);
  140.         ifp->xdev = xdev;
  141.  
  142.         sp->iface = ifp;
  143.         sp->send = asy_send;
  144.         sp->get = get_asy;
  145.         sp->type = CL_KISS;
  146.         trigchar = FR_END;
  147.         ifp->rxproc = newproc( ifn = if_name( ifp, " rx" ),
  148.             256,asy_rx,xdev,NULL,NULL,0);
  149.         free(ifn);
  150.     } else
  151. #endif
  152. #ifdef    NRS
  153.     if(stricmp(argv[3],"NRS") == 0) {
  154.         /* Set up a net/rom serial iface */
  155.         for(xdev = 0;xdev < SLIP_MAX;xdev++){
  156.             np = &Nrs[xdev];
  157.             if(np->iface == NULLIF)
  158.                 break;
  159.         }
  160.         if(xdev >= SLIP_MAX) {
  161.             tprintf("Too many nrs devices\n");
  162.             return -1;
  163.         }
  164.         /* no call supplied? */
  165.         setencap(ifp,"AX25");
  166.         ifp->ioctl = asy_ioctl;
  167.         ifp->raw = nrs_raw;
  168.  
  169.         ifp->hwaddr = mallocw(AXALEN);
  170.         memcpy(ifp->hwaddr,Mycall,AXALEN);
  171.         ifp->xdev = xdev;
  172.         np->iface = ifp;
  173.         np->send = asy_send;
  174.         np->get = get_asy;
  175.         trigchar = ETX;
  176.         ifp->rxproc = newproc( ifn = if_name( ifp, " nrs" ),
  177.             256,nrs_recv,xdev,NULL,NULL,0);
  178.         free(ifn);
  179.     } else
  180. #endif
  181. #ifdef    PPP
  182.     if(stricmp(argv[3],"PPP") == 0) {
  183.         /* Setup for Point-to-Point Protocol */
  184.         trigchar = HDLC_FLAG;
  185.         monitor = TRUE;
  186.         setencap(ifp,"PPP");
  187.         ifp->ioctl = asy_ioctl;
  188.         ifp->flags = FALSE;
  189.  
  190.         /* Initialize parameters for various PPP phases/protocols */
  191.         if (ppp_init(ifp) != 0) {
  192.             tprintf("Cannot allocate PPP control block\n");
  193.             free(ifp->name);
  194.             free((char *)ifp);
  195.             return -1;
  196.         }
  197.     } else
  198. #endif
  199.     {
  200.         tprintf("Mode %s unknown for interface %s\n",
  201.             argv[3],argv[4]);
  202.         free(ifp->name);
  203.         free(ifp);
  204.         return -1;
  205.     }
  206.  
  207.     /* Link in the interface */
  208.     ifp->next = Ifaces;
  209.     Ifaces = ifp;
  210.  
  211.     asy_init(dev,ifp,argv[1],argv[2],(int16)atol(argv[5]),
  212.         trigchar,monitor,(int16)atol(argv[7]));
  213.     return 0;
  214. }
  215.  
  216.  
  217. static int
  218. asy_detach(ifp)
  219. struct iface *ifp;
  220. {
  221.     asy_stop(ifp);
  222.  
  223. #ifdef    SLIP
  224.     if(stricmp(ifp->iftype->name,"SLIP") == 0) {
  225.         Slip[ifp->xdev].iface = NULLIF;
  226. #ifdef VJCOMPRESS
  227.         slhc_free( Slip[ifp->xdev].slcomp );
  228.         Slip[ifp->xdev].slcomp = NULL;
  229. #endif    /* VJCOMPRESS */
  230.     } else
  231. #endif
  232. #ifdef    AX25
  233.     if(stricmp(ifp->iftype->name,"AX25") == 0
  234.      && Slip[ifp->xdev].iface == ifp ) {
  235.         Slip[ifp->xdev].iface = NULLIF;
  236.     } else
  237. #endif
  238. #ifdef    NRS
  239.     if(stricmp(ifp->iftype->name,"AX25") == 0
  240.      && Nrs[ifp->xdev].iface == ifp ) {
  241.         Nrs[ifp->xdev].iface = NULLIF;
  242.     } else
  243. #endif
  244. #ifdef    PPP
  245.     if(stricmp(ifp->iftype->name,"PPP") == 0) {
  246.         ppp_free(ifp);
  247.     } else
  248. #endif
  249.     {
  250.         tprintf("invalid type %s for interface %s\n",
  251.             ifp->iftype->name, ifp->name);
  252.         free(ifp->name);
  253.         free(ifp);
  254.         return -1;
  255.     }
  256.     return 0;
  257. }
  258.  
  259.  
  260.