home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR3 / KA9Q212.ZIP / ASY.C < prev    next >
C/C++ Source or Header  |  1993-07-16  |  6KB  |  267 lines

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