home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / MSDOS / WATTCP / MSWATTCP.ZIP / SRC / CONFIG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-09  |  6.7 KB  |  273 lines

  1. /***
  2.  *
  3.  * File: config.c
  4.  * 27-Aug-93 fr
  5.  *    removed diagnostic output
  6.  * 25-May-93 fr (Ferruccio Rosellini)
  7.  *      added hostable
  8.  * 28-Aug-92 lr
  9.  *      some cleanup
  10.  * 01-Jul-92 lr
  11.  *      added TXBUFSIZE, RXBUFSIZE, UDPBUFSIZE
  12.  *
  13.  */
  14.  
  15. #define WATTCP_KERNEL
  16. #define CONFIG
  17. #include <tcp.h>
  18.  
  19. #include <fcntl.h>      /* open modes */
  20. #include <ctype.h>
  21. #include <stdio.h>      /* for open/close/read/write -- check this */
  22.  
  23. /**** MS C Compatibility ****/
  24.  
  25. #define MY_IP           "MY_IP"
  26. #define NETMASK         "NETMASK"
  27. #define COOKIE          "COOKIE"
  28. #define NAMESERVER      "NAMESERVER"
  29. #define GATEWAY         "GATEWAY"
  30. #define DOMAINS         "DOMAINSLIST"
  31. #define HOSTNAME        "HOSTNAME"
  32. #define SOCKDELAY       "SOCKDELAY"
  33. #define ETHIP           "ETHIP"
  34. #define MSS             "MSS"
  35. #define BOOTP           "BOOTP"
  36. #define BOOTPTO         "BOOTPTO"
  37. #define DOMTO           "DOMAINTO"
  38. #define PRINT           "PRINT"
  39. #define INACTIVE        "INACTIVE"
  40. #define INCLUDE         "INCLUDE"
  41. #define TXBUFSIZE       "TXBUFSIZE"
  42. #define RXBUFSIZE       "RXBUFSIZE"
  43. #define UDPBUFSIZE      "UDPBUFSIZE"
  44. #define DEBUG_FLAG      "DEBUG"
  45.  
  46. #define is_it( x ) if (!strcmp(name,x))
  47.  
  48. /**************************** STATICS *********************/
  49. static void ethip( char *s );
  50. static void set_values(char *name, char *value );
  51.  
  52. static char *watname = "WATTCP.CFG";
  53. static char *hostsname = "HOSTS";
  54.  
  55. /*********************** END OF STATICS *******************/
  56. /*
  57.  * _inet_atoeth - read src, dump to ethernet buffer
  58.  *                and return pointer to end of text string
  59.  */
  60.  
  61. char *
  62. _inet_atoeth( char *src, eth_address eth )
  63. {
  64.     word count, val;
  65.     char ch;
  66.  
  67.     val = count = 0;
  68.     while (ch = (char)toupper(*src++)) {
  69.     if (ch == ':') {
  70.         eth[count++] = (byte)val;
  71.         if (count > 6)
  72.         break;
  73.         val = 0;
  74.     }
  75.     if (ch == ',') {
  76.         eth[count] = (byte)val;
  77.         break;
  78.     }
  79.     if ((ch -= '0') > 9) ch -= 7;
  80.     val = (val << 4) + ch;
  81.     }
  82.     return( src );
  83. }
  84.  
  85. static void
  86. ethip( char *s )
  87. {
  88.     eth_address temp_eth;
  89.     char *temp;
  90.  
  91.     if ( temp = _inet_atoeth( s, temp_eth )) {
  92.     if (!memcmp( &temp_eth, &_eth_addr, sizeof( eth_address ))) {
  93.         my_ip_addr = inet_addr( temp );
  94.     }
  95.     }
  96. }
  97.  
  98. void
  99. _add_server( int *counter, int max, longword *array, longword value )
  100. {
  101.     if ( value && ( *counter < max ))
  102.     array[ (*counter)++ ] = value;
  103. }
  104.  
  105.  
  106. static void
  107. set_values(char *name, char *value )
  108. {
  109.     char *p;
  110.     /* longword temp; */
  111.     word i;
  112.  
  113.     strupr(name);
  114.     is_it( MY_IP ) {
  115.     if ( toupper( *value ) == 'B') _bootpon = 1;
  116.     else my_ip_addr = resolve( value );
  117.     }
  118.     else is_it( NETMASK) sin_mask = resolve( value );
  119.     else is_it( GATEWAY) _arp_add_gateway( value , 0L );
  120.     /* accept gateip[,subnet[,mask]]  */
  121.     else is_it( NAMESERVER )  _add_server( &_last_nameserver,
  122.         MAX_NAMESERVERS, def_nameservers, resolve(value));
  123.     else is_it( COOKIE ) _add_server( &_last_cookie, MAX_COOKIES,
  124.         _cookie, resolve( value ));
  125.     else is_it( DOMAINS ) def_domain = strcpy( defaultdomain, value );
  126.     else is_it( HOSTNAME ) strncpy(_hostname, value, (MAX_STRING*2) );
  127.     else is_it( SOCKDELAY ) sock_delay = atoi( value );
  128.     else is_it( ETHIP )  ethip( value );
  129.     else is_it( MSS ) _mss = atoi( value );
  130.     else is_it( TXBUFSIZE ) TxMaxBufSize = atoi( value );
  131.     else is_it( RXBUFSIZE ) RxMaxBufSize = atoi( value );
  132.     else is_it( UDPBUFSIZE ) MaxBufSize = atoi( value );
  133.     else is_it( DEBUG_FLAG ) debug_on = atoi( value );
  134.     else is_it( BOOTP ) _bootphost = resolve( value );
  135.     else is_it( BOOTPTO) _bootptimeout = atoi( value );
  136.     else is_it( DOMTO ) _domaintimeout = atoi( value );
  137.     else is_it( INACTIVE ) sock_inactive = atoi( value );
  138.     else is_it( PRINT ) {
  139.     if(debug_on){
  140.         outs( value ); 
  141.         outs( "\r" );
  142.         }
  143.     }
  144.     else is_it( INCLUDE ) {
  145.     if ( *(p = value) == '?') p++;
  146.     if ((i = open( p, O_RDONLY | O_TEXT )) != -1 ) {
  147.         close( i );
  148.         tcp_config( p );
  149.     } else if ( *value != '?' ) {
  150.         outs("\n\rUnable to open '");
  151.         outs( p );
  152.         outs("'\n\r");
  153.     }
  154.     }
  155.     else {
  156.     if ( usr_init )
  157.         (*usr_init)(name, value);
  158.     }
  159. }
  160.  
  161.  
  162. static int
  163. hostable( char *path ) {
  164.     char name[80], ip[80], ipname[80], s[80];
  165.     FILE *fp;
  166.  
  167.     if (!path) {
  168.         if (path=getenv( hostsname )) {
  169.             path = strcpy( name, path );
  170.             strcat( name, "\\");  
  171.             }
  172.         strcat( name, hostsname );        
  173.         }   
  174.     
  175.     else   strcpy( name, path );           
  176.  
  177.     if ( !( fp = fopen( name, "r" ) ) ) {
  178.     /* to recover a path parameter error try local subdirectory */
  179.         if ( !( fp = fopen( hostsname, "r" ) ) ){
  180.             DB3((stderr,"%s not found\n\r\n",hostsname));
  181.             return( -1 );
  182.             }
  183.         }
  184.     while ( fgets( s , 80 , fp ) !=NULL ) {
  185.         switch( s[0] ) {
  186.             
  187.             case  '#':
  188.             case  ';': break;
  189.  
  190.             default  : sscanf( s, "%s %s",ip,ipname );
  191.                    add_hosts_table(ip,ipname);
  192.                    break;
  193.             } /* end switch */
  194.         }/* end while */
  195.     fclose(fp);
  196.     return(0);
  197.     }
  198.  
  199. int
  200. tcp_config( char *path ) {
  201.     char name[80];
  202.     char value[80], ch[2];
  203.     int  quotemode;
  204.     int f, mode;
  205.  
  206.     hostable(NULL);        
  207.  
  208.     if (!path) {
  209.         if (path=getenv( watname )) {
  210.             path = strcpy( name, path );
  211.             strcat( name, "\\");  
  212.             }
  213.          /* else {
  214.             strcpy( name, "program" !don't have argv[0]! );
  215.             path = ( *name && (name[1] == ':')) ? &name[2] : name;
  216.             if (!(temp = strrchr( path, '\\' ))) {
  217.                 temp = path;
  218.                 *(temp) = 0;    
  219.                 }                
  220.             else *(++temp) = 0;
  221.             strcpy( name , path);   
  222.                     
  223.             }      that's all rubbish !    */
  224.  
  225.         strcat( name, watname );        
  226.         }   /* end if !path */
  227.     
  228.     else   /* there is a path */
  229.         strcpy( name, path );           
  230.  
  231.     if ( ( f = open( name, O_RDONLY | O_TEXT )) == -1 ) {
  232.     /* to recover a path parameter error try local subdirectory */
  233.         if (( f = open( watname, O_RDONLY | O_TEXT )) == -1 ){
  234.             fprintf(stderr,"%s not found\n\r\n",watname);
  235.             return( -1 );
  236.             }
  237.         }
  238.     *name = *value = ch[1] = mode = quotemode = 0;
  239.     while ( read( f, &ch, 1 ) == 1) {
  240.         switch( *ch ) {
  241.             case  '\"': quotemode ^= 1;
  242.                     break;
  243.             case  ' ' :
  244.             case  '\t': if (quotemode) goto addit;
  245.                     break;
  246.  
  247.             case  '=' : if (quotemode) goto addit;
  248.                     if (!mode) mode = 1;
  249.                     break;
  250.             case  '#' :
  251.             case  ';' : if (quotemode) goto addit;
  252.                     mode = 2;
  253.                     break;
  254.             case  '\n':
  255.             case  '\r': if (*name && *value)
  256.                         set_values(name, value);
  257.                     *name = *value = quotemode = mode = 0;
  258.                     break;
  259.             default   :
  260. addit:
  261.                 switch (mode ) {
  262.                     case 0 : strcat(name, ch);
  263.                     break;
  264.                     case 1 : strcat(value, ch);
  265.                     break;
  266.                     }
  267.                 break;
  268.             } /* end switch */
  269.         }/* end while */
  270.     close(f );
  271.     return( 0 );
  272.     }
  273.