Пример #1
0
struct protoent * getprotoent (void)
{
  static struct _protoent p;
  char   buf[100];

  if (!netdb_init() || !protoFile)
     return (NULL);

  do
  {
    if (!fgets(buf,sizeof(buf)-1,protoFile))
       return (NULL);
  }
  while (buf[0] == '#' || buf[0] == '\n');

  if (protoClose)
     endprotoent();

/*  Protocol  Name
 *  ----------------------------------------
 *  0         reserved
 *  1         icmp, internet control message
 *  2         igmp, internet group management
 *  3         ggp, gateway-gateway protocol
 */

  p.p_proto = atoi  (strtok (buf," \t\n"));
  p.p_name  = strdup(strtok (NULL," ,\t\n"));
  if (!p.p_name)
     return (NULL);

  p.p_aliases = NULL;
  return (struct protoent*) &p;
}
Пример #2
0
struct protoent * getprotobyname (const char *proto)
{
  static struct protoent udp = { "udp", NULL, UDP_PROTO };
  static struct protoent tcp = { "tcp", NULL, TCP_PROTO };
  struct _protoent *p;

  if (!stricmp(proto,"udp"))  /* no chance these are renumbered !? */
     return (&udp);

  if (!stricmp(proto,"tcp"))
     return (&tcp);

  if (!netdb_init())
     return (NULL);

  for (p = _proto0; proto && p; p = p->next)
  {
    char **alias;

    if (p->p_name && !stricmp(p->p_name,proto))
       return (struct protoent*) p;

    /* aliases not supported yet
     */
    for (alias = p->p_aliases; alias && *alias; alias++) 
        if (!stricmp(*alias,proto))
           return (struct protoent*) p;
  }
  return (NULL);
}
Пример #3
0
/*
 * Close the network file and free associated memory
 */
void endnetent (void)
{
  struct _netent *n, *next = NULL;

  if (!netdb_init() || !networkFile)
     return;

  free (networkFname);
  fclose (networkFile);
  networkFname = NULL;
  networkFile  = NULL;

  for (n = network0; n; n = next)
  {
    if (n->n_aliases)
    {
      int i;
      for (i = 0; i < MAX_NETW_ALIASES; i++)
         if (n->n_aliases[i])
           free (n->n_aliases[i]);
      free (n->n_aliases);
    }
    next = n->next;
    free (n->n_name);
    free (n);
  }
  network0 = NULL;
  networkClose = 1;
}
Пример #4
0
void W32_CALL sethostent (int stayopen)
{
  hostClose = (stayopen == 0);
  if (!netdb_init() || !hostFname)
     return;

  if (!hostFile)
       hostFile = fopen (hostFname, "rt");
  else rewind (hostFile);
}
Пример #5
0
/*
 * Open the network file.
 */
void setnetent (int stayopen)
{
  networkClose = (stayopen == 0);

  if (!netdb_init() || !networkFname)
     return;

  if (!networkFile)
       networkFile = fopen (networkFname, "rt");
  else rewind (networkFile);
}
Пример #6
0
void setprotoent (int stayopen)
{
  protoClose = (stayopen == 0);

  if (!netdb_init() || !protoFname)
     return;

  if (!protoFile)
       protoFile = fopen (protoFname,"rt");
  else rewind (protoFile);
}
Пример #7
0
/*
 * Return a 'netent' structure for network number 'net' or
 * NULL if not found.
 */
struct netent *getnetbyaddr (long net, int type)
{
  struct _netent *n;

  if (!netdb_init())
     return (NULL);

  for (n = network0; n; n = n->next)
      if ((DWORD)net == n->n_net && type == n->n_addrtype)
         return (struct netent*) n;
  return (NULL);
}
Пример #8
0
struct protoent * getprotobynumber (int proto)
{
  struct _protoent *p;

  if (!netdb_init())
     return (NULL);

  for (p = _proto0; p && proto; p = p->next)
      if (p->p_proto == proto)
         return (struct protoent*) p;
  return (NULL);
}
Пример #9
0
/*
 * Return a 'netent' structure for network number 'net' or
 * NULL if not found.
 */
struct netent * W32_CALL getnetbyaddr (long net, int type)
{
  const struct _netent *n;

  if (!netdb_init())
     return (NULL);

  for (n = network0; n; n = n->n_next)
      if ((u_long)net == n->n_net && type == n->n_addrtype && n->n_name)
         return fill_netent (n);
  return (NULL);
}
Пример #10
0
/**
 * Return the next (non-commented) line from the host-file.
 * Format is:
 *  ip-address host-name [alias..] {\n | # ..}
 */
struct hostent * W32_CALL gethostent (void)
{
  struct _hostent h;
  char  *tok, *ip, *name, *alias;
  char   buf [2*MAX_HOSTLEN];
  int    i;

  if (!netdb_init() || !hostFile)
  {
    h_errno = NO_RECOVERY;
    return (NULL);
  }

  while (1)
  {
    if (!fgets(buf,sizeof(buf),hostFile))
       return (NULL);

    tok = strltrim (buf);
    if (*tok == '#' || *tok == ';' || *tok == '\n')
       continue;

    ip   = strtok (tok, " \t");
    name = strtok (NULL, " \t\n");
    if (ip && name && isaddr(ip))
       break;
  }

  if (hostClose)
     endhostent();

  memset (&h, 0, sizeof(h));
  if (!strcmp(ip,"0.0.0.0"))   /* inet_addr() maps 0 -> INADDR_NONE */
       h.h_address[0] = INADDR_ANY;
  else h.h_address[0] = inet_addr (ip);

  h.h_num_addr = 1;
  h.h_name = name;
  alias    = strtok (NULL, " \t\n");

  for (i = 0; alias && i < MAX_HOST_ALIASES; i++)
  {
    static char aliases [MAX_NETENT_ALIASES][MAX_HOSTLEN];

    if (*alias == '#' || *alias == ';')
       break;

    h.h_aliases[i] = StrLcpy (aliases[i], alias, sizeof(aliases[i]));
    alias = strtok (NULL, " \t\n");
  }
  return fill_hostent (&h);
}
Пример #11
0
/*
 * Return the next (non-commented) line from the network-file
 * Format is:
 *   name [=] net [alias..] {\n | # ..}
 *
 * e.g.
 *   loopback     127
 *   arpanet      10   arpa
 */
struct netent * getnetent (void)
{
  static struct netent n;
  char  *name, *net, *alias;
  char   buf[100];

  if (!netdb_init())
     return (NULL);

  do
  {
    if (!fgets(buf,sizeof(buf)-1,networkFile))
       return (NULL);
  }
  while (buf[0] == '#' || buf[0] == ';' || buf[0] == '\n');

  if (networkClose)
     endnetent();

  name = strtok (buf, " \t");
  net  = strtok (NULL,"= \t\n");

  n.n_net  = inet_network (net);
  n.n_name = strdup (name);
  if (!n.n_name)
     return (NULL);

  n.n_addrtype = AF_INET;
  n.n_aliases  = NULL;
  alias        = strtok (NULL," \t\n");

  if (alias && *alias != '#' && *alias != ';')
  {
    char **alist = calloc ((1+MAX_NETW_ALIASES) * sizeof(char*), 1);
    int  i = 0;
    do
    {
      if (*alias == '#' || *alias == ';')
         break;
      if (!alist || (i == MAX_NETW_ALIASES) ||
          (alist[i++] = strdup(alias)) == NULL)
         break;
      alias = strtok (NULL," \t\n");
    }
    while (alias);
    n.n_aliases = alist;
  }
  return (&n);
}
Пример #12
0
/*
 * Return the next (non-commented) line from the network-file
 * Format is:
 *   name [=] net [alias..] {\n | # ..}
 *
 * e.g.
 *   loopback     127
 *   arpanet      10   arpa
 */
struct netent * W32_CALL getnetent (void)
{
  struct _netent n;
  char  *name, *net, *alias;
  char   buf [2*MAX_NAMELEN], *tok;
  int    i;

  if (!netdb_init())
     return (NULL);

  while (1)
  {
    if (!fgets(buf,sizeof(buf),networkFile))
       return (NULL);

    tok = strltrim (buf);
    if (*tok == '#' || *tok == ';' || *tok == '\n')
       continue;

    name = strtok (tok, " \t");
    net  = strtok (NULL, "= \t\n");
    if (name && net)
       break;
  }

  if (networkClose)
     endnetent();

  memset (&n, 0, sizeof(n));
  n.n_net  = inet_network (net);
  n.n_name = name;
  alias    = strtok (NULL, " \t\n");

  for (i = 0; alias && i < MAX_NETENT_ALIASES; i++)
  {
    static char aliases [MAX_NETENT_ALIASES][MAX_NAMELEN];

    if (*alias == '#' || *alias == ';')
       break;

    n.n_aliases[i] = StrLcpy (aliases[i], alias, sizeof(aliases[i]));
    alias = strtok (NULL, " \t\n");
  }
  return fill_netent (&n);
}
Пример #13
0
void db_initialize() {
	GMSTATUS status = STATUS_SUCCESS;

	if (kSupportsTierGamesman && gTierGamesman) {
		tierdb_init(db_functions);
	} else if (gBitPerfectDB) {
		if (gSymmetries)
			status = symdb_init(db_functions);
		else
			status = bpdb_init(db_functions);
		if(!GMSUCCESS(status)) {
			BPDB_TRACE("db_initialize()", "Attempt to initialize the bpdb by calling bpdb_init failed", status);
			goto _bailout;
		}
	} else if(gTwoBits) {
		twobitdb_init(db_functions);
	} else if(gCollDB) {
		colldb_init(db_functions);
	}

#ifdef HAVE_GMP
	else if(gUnivDB) {
		db_functions = univdb_init();
	}
#endif

	else if(gNetworkDB) {
		netdb_init(db_functions);
	}

	else if(gFileDB) {
		filedb_init(db_functions);
	}

	else {
		memdb_init(db_functions);
	}
	//printf("\nCalling hooking function\n");
	//db_analysis_hook();
_bailout:
	return;
}
Пример #14
0
/*
 * Return a 'netent' structure for network 'name' or
 * NULL if not found.
 */
struct netent * W32_CALL getnetbyname (const char *name)
{
  const struct _netent *n;

  if (!netdb_init() || !name)
     return (NULL);

  for (n = network0; n; n = n->n_next)
  {
    int i;

    if (n->n_name && !stricmp(n->n_name,name))
       return fill_netent (n);

    for (i = 0; n->n_aliases[i]; i++)
        if (!stricmp(name,n->n_aliases[i]))
           return fill_netent (n);
  }
  return (NULL);
}
Пример #15
0
/*
 * Return a 'netent' structure for network 'name' or
 * NULL if not found.
 */
struct netent * getnetbyname (const char *name)
{
  struct _netent *n;

  if (!netdb_init())
     return (NULL);

  for (n = network0; n; n = n->next)
  {
    char **alias;

    if (n->n_name && !stricmp(n->n_name,name))
       return (struct netent*) n;

    for (alias = n->n_aliases; alias && *alias; alias++)
        if (!stricmp(name,*alias))
           return (struct netent*) n;
  }
  return (NULL);
}
Пример #16
0
void endprotoent (void)
{
  struct _protoent *p, *next = NULL;

  if (!netdb_init() || !protoFile)
     return;

  free (protoFname);
  fclose (protoFile);
  protoFname = NULL;
  protoFile  = NULL;

  for (p = _proto0; p; p = next)
  {
    next = p->next;
    free (p->p_name);
    free (p);
  }
  _proto0 = NULL;
  protoClose = 1;
}
Пример #17
0
/**
 * BSD style: returns local IPv4-address.
 *  \retval  address on \b network order.
 */
u_long W32_CALL gethostid (void)
{
  if (!netdb_init())
     return (INADDR_NONE);
  return htonl (my_ip_addr);
}
Пример #18
0
/**
 * Core-style: Returns local IPv6-address.
 * \note   no BSD equivalent for this.
 * \retval pointer to current local IPv6 address.
 */
const void *_gethostid6 (void)
{
  if (!netdb_init())
     return (&in6addr_any);
  return (&in6addr_my_ip);
}
Пример #19
0
static BOOL gethostbyname_internal (const char *name, const char **alias,
                                    struct _hostent *ret)
{
  static char our_name [MAX_HOSTLEN];
  struct in_addr  addr;
  struct _hostent *h;
  time_t now;
  DWORD  ip;

  h_errno = HOST_NOT_FOUND;
  did_lookup = FALSE;
  from_where = NULL;

  _resolve_exit = _resolve_timeout = 0;
  memset (ret, 0, sizeof(*ret));
  *alias = NULL;

  if (!netdb_init())
  {
    h_errno = NO_RECOVERY;
    return (FALSE);
  }

  if (inet_aton(name,&addr))
  {
    /** \todo should be canonical name */
    ret->h_name       = (char*) name;
    ret->h_address[0] = addr.s_addr;
    ret->h_num_addr   = 1;
    return (TRUE);
  }

  now = time (NULL);

  for (h = host0; h; h = h->h_next)
  {
    int i;

    if (h->h_name && !stricmp(h->h_name,name))
    {
      /* if cached entry expired, do DNS lookup
       */
      if (h->h_timeout && now > h->h_timeout)
         goto expired;

      *ret = *h;
      return (h->h_address[0] != INADDR_NONE ? TRUE : FALSE);
    }
    for (i = 0; h->h_aliases[i] && i < MAX_HOST_ALIASES; i++)
        if (!stricmp(name,h->h_aliases[i]))
        {
          if (h->h_timeout && now > h->h_timeout)
             goto expired;

          *alias = h->h_aliases[i];
          *ret = *h;
          return (h->h_address[0] != INADDR_NONE ? TRUE : FALSE);
        }
  }

  /* Not found in linked list (hosts file or cache). Check name
   * against our own host-name (short-name or FQDN).
   * \todo Should return all our addresses if we're multihomed.
   */
  if (hostname[0] && !stricmp(name,hostname))
  {
    ret->h_num_addr   = 1;
    ret->h_address[0] = gethostid();
    ret->h_name       = hostname;
    from_where = "sethostname";
    return (TRUE);
  }

  if (!gethostname(our_name,sizeof(our_name)) && !stricmp(name,our_name))
  {
    ret->h_num_addr   = 1;
    ret->h_address[0] = gethostid();
    ret->h_name       = our_name;
    from_where = "sethostname";
    return (TRUE);
  }

expired:

  if (called_from_resolve) /* prevent recursion */
     return (FALSE);

  /* Do a full DNS lookup
   */
  called_from_ghbn = TRUE;
  ip = resolve (name);       /* do a normal lookup */
  called_from_ghbn = FALSE;
  did_lookup = TRUE;

  if (_resolve_exit ||   /* interrupted or other fail */
      _resolve_timeout)  /* timed out resolving */
     return (FALSE);

  if (ip)                /* successfully resolved */
  {
    h = add_hostent (h, name, dom_cname, dom_a4list, htonl(ip), dom_ttl);
    return (h ? *ret = *h, TRUE : FALSE);
  }

  /* Add the name to the list even if we got a negative DNS reply.
   * Thus the next call to gethostbyxx() will return immediately.
   */
  add_hostent (h, name, NULL, NULL, INADDR_NONE, netdbCacheLife);
  return (FALSE);
}
Пример #20
0
static BOOL gethostbyaddr_internal (const char *addr_name, int len, int type,
                                    struct _hostent *ret)
{
  static char name [MAX_HOSTLEN];
  struct _hostent *h = NULL;
  DWORD  addr;
  BOOL   rc;
  time_t now;

  h_errno = HOST_NOT_FOUND;
  did_lookup = FALSE;
  from_where = NULL;
  _resolve_exit = _resolve_timeout = 0;
  memset (ret, 0, sizeof(*ret));

  if (type != AF_INET || len < SIZEOF(addr))
  {
    h_errno = NO_RECOVERY;
    return (FALSE);
  }

  if (!netdb_init())
  {
    h_errno = NO_RECOVERY;
    return (FALSE);
  }

  addr = *(DWORD*) addr_name;

  if ((addr == INADDR_ANY ||           /* 0.0.0.0 -> my_ip_addr */
       addr == gethostid()) &&
      gethostname(name,sizeof(name)) == 0)
  {
    /** \todo Should return all our addresses if we're multihomed.
     */
    ret->h_num_addr   = 1;
    ret->h_address[0] = gethostid();
    ret->h_name       = name;
    from_where = "sethostname";
    return (TRUE);
  }

  if (addr == INADDR_BROADCAST ||      /* 255.255.255.255    */
      (~ntohl(addr) & ~sin_mask) == 0) /* directed broadcast */
  {
    ret->h_num_addr   = 1;
    ret->h_address[0] = addr;
    ret->h_name       = "broadcast";
    return (TRUE);
  }

  now = time (NULL);

  /* If called from getnameinfo() with AI_CANONNAME, we
   * should not search the host0 list. But do a full reeverse
   * lookup.
   */
  if (called_from_getai)
     goto expired;

  for (h = host0; h; h = h->h_next)
  {
    int i;

    for (i = 0; h->h_address[i] != INADDR_NONE && i < h->h_num_addr; i++)
    {
      if (addr == h->h_address[i])
      {
        /* if cached entry expired, do a new reverse lookup
         */
        if (h->h_timeout && now > h->h_timeout)
           goto expired;

        *ret = *h;
        return (TRUE);
      }
    }
  }

expired:

  /* do a reverse ip lookup
   */
  did_lookup = TRUE;
  rc = reverse_resolve_ip4 (addr, name, sizeof(name));

  /* interrupted or timedout
   */
  if (!rc && (_resolve_exit || _resolve_timeout))
     return (FALSE);

  if (rc)     /* successfully resolved */
  {
    h = add_hostent (h, name, dom_cname, NULL, addr, dom_ttl);
    /** \todo should be the new aliases */
    return (h ? *ret = *h, TRUE : FALSE);
  }

  /* Add the IP to the list even if reverse lookup failed and not
   * interrupted by _resolve_hook(). Thus the next call to gethostbyxx()
   * will return immediately.
   */
  add_hostent (h, "*unknown*", NULL, NULL, addr, 0UL);
  return (FALSE);
}