home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / SUN / SLIP / CSN_SLIP.TAR / slip-4.1.1 / slattach.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-05  |  5.7 KB  |  237 lines

  1. /*
  2.  *    slattach.c
  3.  *
  4.  *    slattach opens and initializes the named device at the
  5.  *    requested speed, attaches the slip streams encodeing module and 
  6.  *    and streams IP network module and initaliases them. 
  7.  *
  8.  *    Copyright CSIRO Division of Mathematics and Statistics 21 June 1990
  9.  *
  10.  *    Portions of this code may be held by, Doug Kingston,Rayan Zachariassen,
  11.  *    and Greg Earle (see below).
  12.  *
  13.  *    Permission is hereby granted for this code to be distributed
  14.  *    free of charge with this copyright intact and does not interfere
  15.  *    with any copyrights held by the above. Derived works should
  16.  *    be marked as so.
  17.  */
  18.  
  19. /*
  20.  * sliplogin.c
  21.  *
  22.  * This program initializes its own tty port to be an async TCP/IP interface.
  23.  * It merely sets up the SLIP module all by its lonesome on the STREAMS stack,
  24.  * initializes the network interface, and pauses forever waiting for hangup.
  25.  *
  26.  * It is a remote descendant of several similar programs with incestuous ties:
  27.  * - Kirk Smith's slipconf, modified by Richard Johnsson @ DEC WRL.
  28.  * - slattach, probably by Rick Adams but touched by countless hordes.
  29.  * - the original sliplogin for 4.2bsd, Doug Kingston the mover behind it.
  30.  * - a simple slattach-like program used to test the STREAMS SLIP code.
  31.  *
  32.  * Doug Kingston 8810??        - logging + first pass at adding I_STR ioctl's
  33.  * Rayan Zachariassen 881011    - version for SunOS STREAMS SLIP
  34.  * Greg Earle 890331        - added tip/UUCP style LCK lock file
  35.  */
  36.  
  37. #include <sys/types.h>
  38. #include <sys/socket.h>
  39. #include <sys/stropts.h>
  40. #include <sys/termios.h>
  41. #include <sys/ttold.h>
  42. #include <sys/sockio.h>
  43. #include <sys/file.h>
  44. #include <sys/syslog.h>
  45.  
  46. #include "slip.h"
  47.  
  48. #include <netinet/in.h>
  49. #include <net/if.h>
  50.  
  51. #include <stdio.h>
  52. #include <errno.h>
  53. #include <ctype.h>
  54. #include <netdb.h>
  55.  
  56. #include <signal.h>
  57. #include <strings.h>
  58. #include <pwd.h>
  59. #ifdef BSD >= 43
  60. #include <ttyent.h>
  61. #endif
  62.  
  63. void exit();
  64.  
  65. main(argc, argv)
  66.     int argc;
  67.     char *argv[];
  68. {
  69.     int        fd, s, unit;
  70.     int speed,i;
  71.     int        sfd = 0;
  72.     char    *device;
  73.     char    *netmask;
  74.     char    *localaddr;
  75.     char    *dstaddr;
  76.     struct termios    tios;
  77.     struct ifreq    ifr;
  78.     extern void    findid();
  79.  
  80.     if (getuid() == 0) {
  81.         if (argc != 6) {
  82.             (void) fprintf(stderr,
  83.                        "Usage: %s device speed localaddr destaddr netmask\n", argv[0]);
  84.             exit(1);
  85.         }
  86.         device = argv[1];
  87.         speed = atoi(argv[2]);
  88.         localaddr = argv[3];
  89.         dstaddr = argv[4];
  90.         netmask = argv[5];
  91.     } else
  92.         exit(1);
  93.  
  94.     switch (speed){
  95.     case 50: speed = B50; break;
  96.     case 75: speed = B75; break;
  97.     case 110: speed = B110; break;
  98.     case 134: speed = B134; break;
  99.     case 150: speed = B150; break;
  100.     case 200: speed = B200; break;
  101.     case 300: speed = B300; break;
  102.     case 600: speed = B600; break;
  103.     case 1200: speed = B1200; break;
  104.     case 1800: speed = B1800; break;
  105.     case 2400: speed = B2400; break;
  106.     case 4800: speed = B4800; break;
  107.     case 9600: speed = B9600; break;
  108.     case 19200: speed = B19200; break;
  109.     case 38400: speed = B38400; break;
  110.     default:
  111.         (void)fprintf(stderr,"bad speed %d\n",speed);
  112.         exit(1);
  113.     }
  114.  
  115.     for ( i = getdtablesize(); i--; )
  116.         (void)close(i);
  117.  
  118.     if (fork())
  119.         exit(0);
  120.  
  121.     openlog("slattach", LOG_PID, LOG_DAEMON);
  122.  
  123.     if ((fd = open("/dev/tty", O_RDONLY, 0)) >= 0) {
  124.         (void) ioctl(fd, TIOCNOTTY, 0);
  125.         (void) close(fd);
  126.     }
  127.  
  128.  
  129.     if (( sfd = open(device,O_RDWR,0)) < 0 ) {
  130.         syslog(LOG_ERR, "open: %s: %m",device);
  131.         exit(1);
  132.     }
  133.  
  134.     (void) fchmod(sfd, 0600);
  135.  
  136.     /* pop all streams modules */
  137.     while (ioctl(sfd, I_POP, 0) == 0)
  138.         continue;
  139.  
  140.     /* set up the line parameters */
  141.     if (ioctl(sfd, TCGETS, (caddr_t)&tios) < 0) {
  142.         syslog(LOG_ERR, "ioctl (TCGETS): %m");
  143.         exit(1);
  144.     }
  145.     tios.c_cflag = CS8|CREAD|HUPCL|speed;
  146.     tios.c_iflag = IGNBRK;
  147.  
  148.     if (ioctl(sfd, TCSETS, (caddr_t)&tios) < 0) {
  149.         syslog(LOG_ERR, "ioctl (TCSETS): %m");
  150.         exit(1);
  151.     }
  152.  
  153.     /* push the SLIP module */
  154.     if (ioctl(sfd, I_PUSH, "slipen") < 0) {
  155.         syslog(LOG_ERR, "ioctl (I_PUSH) slipen: %m");
  156.         exit(1);
  157.     }
  158.     if (ioctl(sfd, I_PUSH, "str_ip") < 0) {
  159.         syslog(LOG_ERR, "ioctl (I_PUSH) str_ip: %m");
  160.         exit(1);
  161.     }
  162.  
  163.     /* find out what unit number we were assigned */
  164.     if (ioctl(sfd, SLIOGUNIT, (caddr_t)&unit) < 0) {
  165.         syslog(LOG_ERR, "ioctl (SLIOGUNIT): %m");
  166.         exit(1);
  167.     }
  168.  
  169.     syslog(LOG_NOTICE, "attaching slip%d: local %s remote %s mask %s\n",
  170.         unit, localaddr, dstaddr, netmask);
  171.  
  172.     /* set the local and remote interface addresses */
  173.     s = socket(AF_INET, SOCK_DGRAM, 0);
  174.  
  175.     (void) sprintf(ifr.ifr_name, "%s%d", "slip", unit);
  176.     in_getaddr(netmask, &ifr.ifr_addr);
  177.     if (ioctl(s, SIOCSIFNETMASK, (caddr_t)&ifr) < 0) {
  178.         syslog(LOG_ERR, "ioctl (SIOCSIFNETMASK): %m");
  179.         exit(1);
  180.     }
  181.  
  182.     (void) sprintf(ifr.ifr_name, "%s%d", "slip", unit);
  183.     in_getaddr(dstaddr, &ifr.ifr_addr);
  184.     if (ioctl(s, SIOCSIFDSTADDR, (caddr_t)&ifr) < 0) {
  185.         syslog(LOG_ERR, "ioctl (SIOCSIFDSTADDR): %m");
  186.         exit(1);
  187.     }
  188.  
  189.     (void) sprintf(ifr.ifr_name, "%s%d", "slip", unit);
  190.     in_getaddr(localaddr, &ifr.ifr_addr);
  191.     /* this has the side-effect of marking the interface up */
  192.     if (ioctl(s, SIOCSIFADDR, (caddr_t)&ifr) < 0) {
  193.         syslog(LOG_ERR, "ioctl (SIOCSIFADDR): %m");
  194.         exit(1);
  195.     }
  196.     (void)pause();
  197.     exit(0);
  198.     /*NOTREACHED*/
  199. }
  200.  
  201.  
  202. in_getaddr(s, saddr)
  203.     char *s;
  204.     struct sockaddr *saddr;
  205. {
  206.     register struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
  207.     struct hostent *hp;
  208.     struct netent *np;
  209.     u_long val;
  210.     extern struct in_addr inet_makeaddr();
  211.     extern u_long inet_addr();
  212.  
  213.     bzero((caddr_t)saddr, sizeof *saddr);
  214.     sin->sin_family = AF_INET;
  215.     val = inet_addr(s);
  216.     if (val != (u_long)-1) {
  217.         sin->sin_addr.s_addr = val;
  218.         return;
  219.     }
  220.     hp = gethostbyname(s);
  221.     if (hp) {
  222.         sin->sin_family = hp->h_addrtype;
  223.         bcopy(hp->h_addr, (char *)&sin->sin_addr, hp->h_length);
  224.         return;
  225.     }
  226.     np = getnetbyname(s);
  227.     if (np) {
  228.         sin->sin_family = np->n_addrtype;
  229.         sin->sin_addr = inet_makeaddr((int)np->n_net, (int)INADDR_ANY);
  230.         return;
  231.     }
  232.     (void) fprintf(stderr, "slattach: %s: bad value\n", s);
  233.     syslog(LOG_ERR, "%s: bad value\n", s);
  234.     exit(1);
  235.     /* NOTREACHED */
  236. }
  237.