home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / wattcp.zip / PCCONFIG.C < prev    next >
C/C++ Source or Header  |  1992-03-31  |  5KB  |  205 lines

  1. #include <copyright.h>
  2. #include <wattcp.h>
  3.  
  4. #include <dos.h>    /* for _argv */
  5. #include <fcntl.h>    /* open modes */
  6. #include <string.h>
  7.  
  8. #define MY_IP        "MY_IP"
  9. #define NETMASK     "NETMASK"
  10. #define COOKIE        "COOKIE"
  11. #define NAMESERVER    "NAMESERVER"
  12. #define GATEWAY     "GATEWAY"
  13. #define DOMAINS        "DOMAINSLIST"
  14. #define HOSTNAME    "HOSTNAME"
  15. #define SOCKDELAY       "SOCKDELAY"
  16. #define ETHIP        "ETHIP"
  17. #define MSS             "MSS"
  18. #define BOOTP        "BOOTP"
  19. #define BOOTPTO        "BOOTPTO"
  20. #define DOMTO           "DOMAINTO"
  21. #define PRINT        "PRINT"
  22. #define INACTIVE        "INACTIVE"
  23. #define INCLUDE         "INCLUDE"
  24. #define MULTIHOMES      "MULTIHOMES"
  25.  
  26.  
  27. #define is_it( x ) if (!strcmp(name,x))
  28.  
  29. /*
  30.  * _inet_atoeth - read src, dump to ethernet buffer
  31.  *          and return pointer to end of text string
  32.  */
  33. char *_inet_atoeth( char *src, byte *eth )
  34. {
  35.     word count, val;
  36.     byte ch, icount;
  37.  
  38.     val = count = icount = 0;
  39.     while (ch = toupper(*src++)) {
  40.         if (ch == ':' ) continue;
  41.         if (ch == ',' ) break;
  42.  
  43.         if ((ch -= '0') > 9) ch -= 7;
  44.         val = (val << 4) + ch;
  45.  
  46.         if ( icount ) {
  47.             eth[ count++ ] = val;
  48.             if (count > 5) break;
  49.             val = icount = 0;
  50.         } else
  51.             icount = 1;
  52.     }
  53.     if ( *src == ',' ) ++src;
  54.     return( src );
  55. }
  56.  
  57. static ethip( char *s )
  58. {
  59.     eth_address temp_eth;
  60.     char *temp;
  61.  
  62.     if ( temp = _inet_atoeth( s, &temp_eth )) {
  63.     if (!memcmp( &temp_eth, &_eth_addr, sizeof( eth_address ))) {
  64.             my_ip_addr = inet_addr( temp );
  65.         }
  66.     }
  67. }
  68.  
  69. void _add_server( int *counter, int max, longword *array, longword value )
  70. {
  71.     if ( value && ( *counter < max ))
  72.     array[ (*counter)++ ] = value;
  73. }
  74.  
  75. word sock_delay = 30;
  76. word sock_inactive = 0;  /* defaults to forever */
  77. word multihomes = 0;
  78. char defaultdomain[ 80 ];
  79. longword _cookie[ MAX_COOKIES ];
  80. int _last_cookie;
  81. extern _domaintimeout;
  82. void (*usr_init)() = NULL;
  83.  
  84. static set_values(char *name, char *value )
  85. {
  86.     char *p;
  87.     longword temp;
  88.     word i;
  89.  
  90.     strupr(name);
  91.     is_it( MY_IP ) {
  92.     if ( toupper( *value ) == 'B') _bootpon = 1;
  93.     else my_ip_addr = resolve( value );
  94.     }
  95.     else is_it( NETMASK) sin_mask = resolve( value );
  96.     else is_it( GATEWAY)
  97.     /* accept gateip[,subnet[,mask]]  */
  98.     _arp_add_gateway( value , 0L );
  99.     else is_it( NAMESERVER )  _add_server( &_last_nameserver,
  100.         MAX_NAMESERVERS, def_nameservers, resolve(value));
  101.     else is_it( COOKIE ) _add_server( &_last_cookie, MAX_COOKIES,
  102.         _cookie, resolve( value ));
  103.     else is_it( DOMAINS ) def_domain = strcpy( defaultdomain, value );
  104.     else is_it( HOSTNAME ) strncpy(_hostname, value, MAX_STRING );
  105.     else is_it( SOCKDELAY ) sock_delay = atoi( value );
  106.     else is_it( ETHIP )  ethip( value );
  107.     else is_it( MSS ) _mss = atoi( value );
  108.     else is_it( BOOTP ) _bootphost = resolve( value );
  109.     else is_it( BOOTPTO) _bootptimeout = atoi( value );
  110.     else is_it( DOMTO ) _domaintimeout = atoi( value );
  111.     else is_it( INACTIVE ) sock_inactive = atoi( value );
  112.     else is_it( MULTIHOMES ) multihomes = atoi( value );
  113.     else is_it( PRINT ) {
  114.     outs( value );
  115.     outs( "\n\r" );
  116.     }
  117.     else is_it( INCLUDE ) {
  118.         if ( *(p = value) == '?') p++;
  119.         if ((i = _open( p, O_RDONLY | O_TEXT )) != -1 ) {
  120.             _close( i );
  121.             tcp_config( p );
  122.         } else if ( *value != '?' ) {
  123.             outs("\n\rUnable to open '");
  124.             outs( p );
  125.             outs("'\n\r");
  126.         }
  127.     }
  128.     else {
  129.     if ( usr_init )
  130.         (*usr_init)(name, value);
  131.     }
  132. }
  133.  
  134. static char *watname = "WATTCP.CFG";
  135.  
  136. tcp_config( char *path )
  137. {
  138.     char name[80], *temp;
  139.     char value[80], ch[2];
  140.     int  quotemode;
  141.     int f, mode;
  142.  
  143.     if (!path) {
  144.     if (path=getenv( watname )) {
  145.         path = strcpy( name, path );
  146.         strcat( name, "\\");
  147.     } else {
  148.         strcpy( name, _argv[0] );
  149.         path = ( *name && (name[1] == ':')) ? &name[2] : name;
  150.         if (!(temp = strrchr( path, '\\' ))) temp = path;
  151.         *(++temp) = 0;
  152.     }
  153. /*
  154.         strcpy( name, path );
  155.         strcat( name, "\\");
  156. */
  157.         strcat( name, watname );
  158.     } else
  159.         strcpy( name, path );
  160.  
  161.     if ( ( f = _open( name, O_RDONLY | O_TEXT )) == -1 ) {
  162.     /* try local subdirectory */
  163.     if (( f = _open( watname, O_RDONLY | O_TEXT )) == -1 ){
  164.         outs( watname );
  165.         outs(" not found\n\r");
  166.         return( -1 );
  167.     }
  168.     }
  169.     *name = *value = mode = ch[1] = quotemode = 0;
  170.     while ( _read( f, &ch, 1 ) == 1) {
  171.     switch( *ch ) {
  172.         case  '\"': quotemode ^= 1;
  173.             break;
  174.         case  ' ' :
  175.         case  '\t': if (quotemode) goto addit;
  176.             break;
  177.  
  178.         case  '=' : if (quotemode) goto addit;
  179.             if (!mode) mode = 1;
  180.             break;
  181.         case  '#' :
  182.         case  ';' : if (quotemode) goto addit;
  183.             mode = 2;
  184.             break;
  185.         case  '\n':
  186.         case  '\r': if (*name && *value)
  187.                 set_values(name, value);
  188.             *name = *value = quotemode = mode = 0;
  189.             break;
  190.         default   :
  191. addit:
  192.                   switch (mode ) {
  193.             case 0 : strcat(name, ch);
  194.                  break;
  195.             case 1 : strcat(value, ch);
  196.                  break;
  197.             }
  198.             break;
  199.     }
  200.     }
  201.     _close(f );
  202.     return( 0 );
  203. }
  204.  
  205.