home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-A.06 / NETKIT-A / NetKit-A-0.06 / dip337d-uri / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-15  |  5.1 KB  |  227 lines

  1. /*
  2.  * dip        A program for handling dialup IP connecions.
  3.  *        This program handles the connections needed for dialup
  4.  *        IP links, like SLIP or PPP.  It can handle both incoming
  5.  *        and outgoing connections, using password security for
  6.  *        incoming connections.  The outgoing connections use the
  7.  *        system's dial(3) library if possible.
  8.  *
  9.  * Usage:    dip -i [-v]
  10.  *        dip -k [-v]
  11.  *        dip -t [-v]
  12.  *        dip [-v] [-m mtu] [-p proto] [telno | script]
  13.  *        diplogin [-v]
  14.  *
  15.  * Version:    @(#)main.c    3.3.7d-uri    07/15/94
  16.  *
  17.  * Author:      Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  18.  *        Copyright 1988-1993 MicroWalt Corporation
  19.  *
  20.  * Modified:    Uri Blumenthal <uri@watson.ibm.com>
  21.  *              Copyright 1994 
  22.  *
  23.  *        This program is free software; you can redistribute it
  24.  *        and/or  modify it under  the terms of  the GNU General
  25.  *        Public  License as  published  by  the  Free  Software
  26.  *        Foundation;  either  version 2 of the License, or  (at
  27.  *        your option) any later version.
  28.  */
  29. #define GLOBAL
  30. #include "dip.h"
  31.  
  32.  
  33. #define VERSION    "3.3.7d-uri (15 July 94)"
  34.  
  35.  
  36. char *Version = "@(#) dip " VERSION;
  37.  
  38.  
  39. struct dip    mydip;            /* global DIP entry        */
  40. int        opt_v = 0;        /* debug flag            */
  41.  
  42.  
  43. /* Kill a running DIP process. */
  44. static void
  45. kill_dip(void)
  46. {
  47.   char buff[128];
  48.   int pid;
  49.   FILE *fp;
  50.  
  51.   fp = fopen(_PATH_DIP_PID, "r");
  52.   if (fp == NULL) {
  53.     fprintf(stderr, "DIP: cannot open %s: %s\n",
  54.             _PATH_DIP_PID, strerror(errno));
  55.     return;
  56.   }
  57.   (void) fgets(buff, 128, fp);
  58.   pid = atoi(buff);
  59.   if (kill(pid, SIGQUIT)) {
  60.     fprintf(stderr, "DIP: cannot kill process %d: %s\n",
  61.             pid, strerror(errno));
  62.     return;
  63.   } else printf("DIP: process %d killed.\n", pid);
  64.   (void) fclose(fp);
  65.   (void) unlink(_PATH_DIP_PID);
  66. }
  67.  
  68.  
  69. /* Fill in the global DIP entry. */
  70. static void
  71. dip_init(void)
  72. {
  73.   struct passwd *pw;
  74.   struct hostent *hp;
  75.  
  76.   if ((pw = getpwuid(getuid())) == (struct passwd *)NULL) {
  77.     fprintf(stderr, "You do not exist.  Go away!\n");
  78.     exit(-1);
  79.   }
  80.   memset((char *) &mydip, 0, sizeof(struct dip));
  81.   strncpy(mydip.name, pw->pw_name, 16);
  82.   strcpy(mydip.home, "/tmp");
  83.  
  84.   if (gethostname(mydip.local, 128) < 0) {
  85.     perror("gethostname");
  86.     exit(-1);
  87.   }
  88.   if ((hp = gethostbyname(mydip.local)) == (struct hostent *)NULL) {
  89.     herror(mydip.local);
  90.     strcpy(mydip.local, "");
  91.     exit(-1);
  92.   }
  93.   strncpy(mydip.local, hp->h_name, 128);
  94.   memcpy((char *) &mydip.loc_ip, (char *) hp->h_addr_list[0], hp->h_length);
  95.  
  96.   strcpy(mydip.protocol, DEF_PROT);
  97.   mydip.protonr = get_prot(mydip.protocol);
  98.   mydip.mtu = DEF_MTU;
  99. }
  100.  
  101.  
  102. static void
  103. usage(void)
  104. {
  105.   fprintf(stderr, "Usage: dip -i [-v]\n");
  106.   fprintf(stderr, "       diplogin [-v]\n");
  107.   fprintf(stderr, "       dip -k [-v]\n");
  108.   fprintf(stderr, "       dip -t [-v]\n");
  109.   fprintf(stderr, "       dip [-v] [-m mtu] [-p proto] [telno | script]\n");
  110.   exit(-1);
  111. }
  112.  
  113.  
  114. int
  115. main(int argc, char *argv[])
  116. {
  117.   char path[128];
  118.   FILE *fp;
  119.   register int s;
  120.   register char *sp;
  121.   int opt_i, opt_k, opt_t;
  122.  
  123.   /* Setup. */
  124.   dip_init();
  125.  
  126.   strcpy(path, "");
  127.   opt_i = 0;
  128.   opt_k = 0;
  129.   opt_t = 0;
  130.  
  131.   /*
  132.    * This is a kludge for bad "login" programs which cannot pass
  133.    * arguments. Needless to say, this includes the SLS login(8)
  134.    * program, and most BSD derivatives... -FvK
  135.    */
  136.   /*
  137.    * Added "login" below, so diplogin can be called directly
  138.    * from getty_ps... SJS
  139.    */
  140.   if ((sp = strrchr(argv[0], '/')) != NULL) *sp++ = '\0';
  141.     else sp = argv[0];
  142.   if ((*sp == '-') || !strcmp(sp, "diplogin") || !strcmp(sp, "login")) 
  143.       opt_i = 1;
  144.  
  145.   /* Scan command line for any arguments. */
  146.   opterr = 0;
  147.   while ((s = getopt(argc, argv, "ikm:p:vt")) != EOF) switch(s) {
  148.     case 'i':
  149.         opt_i = 1;
  150.         break;
  151.  
  152.     case 'k':
  153.         opt_k = 1;
  154.         break;
  155.  
  156.     case 'm':
  157.         mydip.mtu = atoi(optarg);
  158.         if (mydip.mtu <= 0 || mydip.mtu > 32767) usage();
  159.         break;
  160.  
  161.     case 'p':
  162.         strcpy(mydip.protocol, optarg);
  163.         mydip.protonr = get_prot(mydip.protocol);
  164.         if (mydip.protonr == 0) usage();
  165.         break;
  166.  
  167.     case 't':
  168.         opt_t = 1;
  169.         break;
  170.  
  171.     case 'v':
  172.         opt_v = 1;
  173.         break;
  174.  
  175.     default:
  176.         usage();
  177.   }
  178.  
  179.   printf("DIP: Dialup IP Protocol Driver version %s\n", VERSION);
  180.   printf("Written by Fred N. van Kempen, MicroWalt Corporation.\n\n");
  181.  
  182.   /* Are we called to kill off a DIP process? */
  183.   if (opt_k == 1) {
  184.     kill_dip();
  185.     exit(0);
  186.   }
  187.  
  188.   /* Verbose mode? -> print mydip values */
  189.   if (opt_v == 1) {
  190.     printf("DIP: name=%s home=%s\n",mydip.name,mydip.home);
  191.     printf("     host=%s IP=%s\n",mydip.local,inet_ntoa(mydip.loc_ip));
  192.     printf("     prot=%s MTU=%d\n\n",mydip.protocol,mydip.mtu);
  193.   }
  194.  
  195.   /* Are we going to be a dialIN server? */
  196.   if (opt_i == 1) {
  197.     if (optind == argc) sp = mydip.name;
  198.       else sp = argv[optind];
  199.     do_login(sp, &argv[0]);
  200.     /*NOTREACHED*/
  201.   }
  202.  
  203.   /* Are we running in TEST mode? */
  204.   if (opt_t == 1) {
  205.     if (optind != argc) usage();
  206.     do_command(stdin);
  207.     /*NOTREACHED*/
  208.   }
  209.  
  210.   /* No, so we are running in dialOUT mode. */
  211.   if (optind != (argc - 1)) usage();
  212.   strncpy(path, argv[optind], 128);
  213.   if ((sp = strrchr(path, '/')) != (char *)NULL) sp++;
  214.     else sp = path;
  215.   if (strchr(sp, '.') == (char *)NULL) strcat(path, DIP_SFX);
  216.  
  217.   if ((fp = fopen(path, "r")) == (FILE *)NULL) {
  218.     fprintf(stderr, "dip: %s: %s\n", path, strerror(errno));
  219.     exit(-1);
  220.   }
  221.   (void) setbuf(fp, (char *)NULL);
  222.   do_command(fp);
  223.  
  224.   /*NOTREACHED*/
  225.   return(-1);
  226. }
  227.