⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcconfig.c

📁 开放源码的编译器open watcom 1.6.0版的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 *  WatTCP config file handling
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <io.h>
#include <fcntl.h>    /* open modes */
#include <ctype.h>
#include <string.h>

#include "copyrigh.h"
#include "wattcp.h"
#include "strings.h"
#include "misc.h"
#include "language.h"
#include "udp_dom.h"
#include "udp_nds.h"
#include "bsdname.h"
#include "pcqueue.h"
#include "pcsed.h"
#include "pcpkt.h"
#include "pctcp.h"
#include "pcarp.h"
#include "pcrarp.h"
#include "pcbsd.h"
#include "pcbootp.h"
#include "pcicmp.h"
#include "sock_ini.h"
#include "fragment.h"
#include "ip_out.h"
#include "profile.h"
#include "pcconfig.h"

#ifdef __BORLANDC__
#pragma warn -pro
#endif

int  debug_on          = 0;    /* tcp-state machine debug */
int  sock_delay        = 30;
int  sock_inactive     = 0;    /* defaults to forever */
int  sock_data_timeout = 120;  /* after 2 minutes we give up EE 99.08.23 */
int  multihomes        = 0;    /* We have more than 1 IP-addresses */
int  ctrace_on         = 0;    /* tracing; HighC only */
int  dynamic_host      = 0;    /* reverse resolve assigned IP to true FQDN */

DWORD cookies [MAX_COOKIES];
WORD  last_cookie;

static void dummy (const char *name, const char *value)
{
  ARGSUSED (name);
  ARGSUSED (value);
}

void (*print_hook) (const char*) = NULL;
void (*usr_init)   (const char*, const char*) = dummy;

char *_watt_config_name   = "WATTCP.CFG";  /* name of config file in */
char *_watt_environ_name  = "WATTCP.CFG";  /* path specified by this var. */
char *_watt_environ_name1 = "WATTCP_CFG";  /* path specified by this var. */

/*
 * Parse string "ether-addr, ip-addr"
 *
 * inet_atoeth - read src, dump to ethernet buffer
 *               and return pointer to "ip-addr"
 */
const char *inet_atoeth (const char *src, BYTE *eth)
{
  eth[0] = atox (src-2);      /*   xx:xx:xx:xx:xx:xx */
  eth[1] = atox (src+1);      /* ^src-2              */
  eth[2] = atox (src+4);      /*    ^src+1           */
  eth[3] = atox (src+7);
  eth[4] = atox (src+10);
  eth[5] = atox (src+13);
  src += strlen ("xx:xx:xx:xx:xx:xx");
  if (*src == ',')
     ++src;

  while (*src == ' ' || *src == '\t')
        src++;

  return (src);
}

static void EtherAddr (const char *value, int set_it)
{
  eth_address eth;
  const char *ip     = inet_atoeth (value, (BYTE*)&eth);
  BOOL        is_own = memcmp (&eth, &_eth_addr, sizeof(eth)) == 0;

  if (!isdigit(*ip) && !set_it)
     return;

  if (isdigit(*ip) && !set_it && is_own)
  {
    my_ip_addr = _inet_addr (ip);
    return;
  }

  if (set_it && !is_own)
  {
    if (!_eth_set_addr(&eth))
       outsnl (_LANG("Cannot set Ether-addr"));
#if defined(USE_RARP)
    else
    {
      WORD  save_to = _rarptimeout;
      DWORD save_ip = my_ip_addr;
      _rarptimeout = 2;           /* use only 2 sec timeout */
      my_ip_addr   = 0;
      if (!_dorarp())
         outsnl (_LANG("Warning: Ether-addr already in use"));
      my_ip_addr   = save_ip;
      _rarptimeout = save_to;
    }
#endif
  }
  else
  {
    DWORD addr = _inet_addr (ip);
    if (addr)
      _arp_add_cache (addr, &eth, FALSE); /* add ip-addr to arp-table */
  }
}

void _add_server (WORD *counter, WORD max, DWORD *array, DWORD value)
{
  int i, duplicate = 0;

  if (value && *counter < max)
  {
    for (i = 0; i < *counter; i++)
        if (array[i] == value)
           duplicate = 1;

    if (!duplicate)
       array [(*counter)++] = value;
  }
}

/*
 * Return a string with a environment variable expanded (only 1).
 *
 * E.g. if environment variable ETC is "c:\network\watt\bin",
 * "$(ETC)\hosts" becomes "c:\network\watt\bin\hosts"
 */
const char *ExpandVarStr (const char *str)
{
  static char buf[MAX_PATHLEN];
  char   env[30];
  const  char *e, *p  = strstr (str, "$(");
  const  char *dollar = p;
  int    i;

  if (!p)
     return (str);

  for (i = 0, p += 2; i < sizeof(env)-1; i++)
  {
    if (*p == ')')
       break;
    env[i] = *p++;
  }

  if (*p++ != ')')
     return (str);

  env[i] = 0;
  strupr (env);
  e = getenv (env);
  if (!e)
  {
    e = env;
    env[0] = 0;
  }

  i = dollar - str;
  strncpy (buf, str, i);
  buf[i] = 0;
  strncat (buf, e, sizeof(buf)-1-i);
  return strcat (buf, p);
}

/*
 * Convert hexstring "0x??" to hex
 */
BYTE atox (const char *value)
{
  unsigned hi = toupper (value[2]);
  unsigned lo = toupper (value[3]);

  hi -= isdigit (hi) ? '0' : 'A'-10;
  lo -= isdigit (lo) ? '0' : 'A'-10;
  return ((hi << 4) + lo);
}

/*
 * Parse the config-table and if match is found for
 * ('section'+'.'+)'name' either store variable to 'value' or
 * call function with 'value'.
 */
int parse_config_table (const struct config_table *tab,
                        const char *section,
                        const char *name,
                        const char *value)
{
  for ( ; tab && tab->keyword; tab++)
  {
    char keyword[MAX_STRING];

    if (section)
    {
      if (strlen(section) + strlen(tab->keyword) >= sizeof(keyword))
         continue;
      strcpy (keyword, section);
      strcat (keyword, tab->keyword);
    }
    else
    {
      strncpy (keyword, tab->keyword, sizeof(keyword)-1);
      keyword [sizeof(keyword)-1] = '\0';
    }

    if (!strcmp(name,keyword))
    {
      void *arg = tab->arg_func;
      char *p;

      if (!arg)     /* no storage for variable, okay */
         return (1);

      switch (tab->type)
      {
        case ARG_ATOI:
             *(int*)arg = atoi (value);
             break;

        case ARG_ATOB:
             *(BYTE*)arg = atoi (value);
             break;

        case ARG_ATOW:
             *(WORD*)arg = atoi (value);
             break;

        case ARG_ATON:
             *(DWORD*)arg = aton (value);
             break;

        case ARG_ATOX_B:
             *(BYTE*)arg = atox (value);
             break;

        case ARG_ATOX_W:
             *(WORD*)arg = (atox (value) << 8) + atox (value+2);
             break;

        case ARG_FUNC:
             (*(void(*)())arg) (value, strlen(value));
             break;

        case ARG_RESOLVE:
             {
               DWORD host = resolve (value);
               if (!host)
               {
                 outs (_LANG("Cannot resolve \""));
                 outs (value);
                 outsnl ("\"\n");
               }
               else
                 *(DWORD*)arg = host;
             }
             break;

        case ARG_STRDUP:
             p = strdup (value);
             if (!p)
             {
               outs (_LANG("No memory for \""));
               outs (name);
               outsnl ("\"\n");
             }
             else
               *(char**)arg = p;
             break;

#ifdef USE_DEBUG
        default:
             fprintf (stderr, "Something wrong in parse_config_table().\n"
                      "Section %s; %s = %s\n", section, name, value);
             exit (-1);
#endif
      }

#if 0  /* test */
      fprintf (stderr, "ARG_%s, matched `%s' = `%s'\n",
               tab->type == ARG_ATOI   ? "ATOI  "  :
               tab->type == ARG_ATON   ? "ATON  "  :
               tab->type == ARG_ATOX_B ? "ATOX_B"  :
               tab->type == ARG_ATOX_W ? "ATOX_W"  :
               tab->type == ARG_STRDUP ? "STRDUP"  :
               tab->type == ARG_RESOLVE? "RESOLVE" :
               tab->type == ARG_FUNC   ? "FUNC  "  :
               "??", keyword, value);
#endif
      return (1);
    }
  }
  return (0);
}

static void set_my_ip (const char *value)
{
  if (!stricmp(value,"bootp"))
       _bootpon = 1;
  else if (!stricmp(value,"dhcp"))
       _dhcpon = 1;
  else if (!stricmp(value,"rarp"))
       _rarpon = 1;
  else my_ip_addr = resolve (value);
}

static void set_hostname (const char *value)
{
  strncpy (hostname, value, sizeof(hostname)-1);
  hostname [sizeof(hostname)-1] = '\0';
}

static void set_gateway (const char *value)
{
  _arp_add_gateway (value, 0L);  /* accept gateip[,subnet[,mask]] */
}

static void set_nameserv (const char *value)
{
  _add_server (&last_nameserver, MAX_NAMESERVERS,
               def_nameservers, resolve(value));
}

static void set_cookie (const char *value)
{
  _add_server (&last_cookie, MAX_COOKIES, cookies, resolve(value));
}

static void set_eaddr (const char *value)
{
  EtherAddr (value, 1);
}

static void set_ethip (const char *value)
{
  EtherAddr (value, 0);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -