2006-07-22 17:37:10 +02:00
|
|
|
/* $Id$ */
|
2006-07-22 17:21:13 +02:00
|
|
|
|
2003-10-07 23:54:04 +02:00
|
|
|
/* Copyright 1998 by the Massachusetts Institute of Technology.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this
|
|
|
|
* software and its documentation for any purpose and without
|
|
|
|
* fee is hereby granted, provided that the above copyright
|
|
|
|
* notice appear in all copies and that both that copyright
|
|
|
|
* notice and this permission notice appear in supporting
|
|
|
|
* documentation, and that the name of M.I.T. not be used in
|
|
|
|
* advertising or publicity pertaining to distribution of the
|
|
|
|
* software without specific, written prior permission.
|
|
|
|
* M.I.T. makes no representations about the suitability of
|
|
|
|
* this software for any purpose. It is provided "as is"
|
|
|
|
* without express or implied warranty.
|
|
|
|
*/
|
|
|
|
|
2004-07-23 00:18:45 +02:00
|
|
|
#include "setup.h"
|
2003-10-07 23:54:04 +02:00
|
|
|
|
2004-08-20 15:45:26 +02:00
|
|
|
#if !defined(WIN32) || defined(WATT32)
|
2008-09-15 19:14:29 +02:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
2003-10-07 23:54:04 +02:00
|
|
|
#include <sys/socket.h>
|
2008-09-15 19:14:29 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
2003-10-07 23:54:04 +02:00
|
|
|
#include <netinet/in.h>
|
2008-09-15 19:14:29 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
2003-10-07 23:54:04 +02:00
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
2008-09-15 19:14:29 +02:00
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "ares.h"
|
2005-04-08 18:22:51 +02:00
|
|
|
#include "inet_net_pton.h"
|
2008-09-11 05:46:47 +02:00
|
|
|
#include "ares_private.h"
|
2003-10-07 23:54:04 +02:00
|
|
|
|
2005-04-08 17:41:31 +02:00
|
|
|
int ares__get_hostent(FILE *fp, int family, struct hostent **host)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
|
|
|
char *line = NULL, *p, *q, *canonical, **alias;
|
|
|
|
int status, linesize, end_at_hostname, naliases;
|
|
|
|
struct in_addr addr;
|
2005-04-08 17:41:31 +02:00
|
|
|
struct in6_addr addr6;
|
|
|
|
int addrlen = sizeof(struct in_addr);
|
2003-10-07 23:54:04 +02:00
|
|
|
struct hostent *hostent = NULL;
|
|
|
|
|
|
|
|
while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
|
|
|
|
{
|
|
|
|
/* Skip comment lines; terminate line at comment character. */
|
|
|
|
if (*line == '#' || !*line)
|
2004-10-06 09:50:18 +02:00
|
|
|
continue;
|
2003-10-07 23:54:04 +02:00
|
|
|
p = strchr(line, '#');
|
|
|
|
if (p)
|
2004-10-06 09:50:18 +02:00
|
|
|
*p = 0;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
/* Get the address part. */
|
|
|
|
p = line;
|
2006-10-18 05:42:06 +02:00
|
|
|
while (*p && !ISSPACE(*p))
|
2004-10-06 09:50:18 +02:00
|
|
|
p++;
|
2003-10-07 23:54:04 +02:00
|
|
|
if (!*p)
|
2004-10-06 09:50:18 +02:00
|
|
|
continue;
|
2003-10-07 23:54:04 +02:00
|
|
|
*p = 0;
|
|
|
|
addr.s_addr = inet_addr(line);
|
|
|
|
if (addr.s_addr == INADDR_NONE)
|
2005-04-08 17:41:31 +02:00
|
|
|
{
|
|
|
|
if (ares_inet_pton(AF_INET6, line, &addr6) > 0)
|
|
|
|
{
|
|
|
|
if (family != AF_INET6)
|
|
|
|
continue;
|
|
|
|
addrlen = sizeof(struct in6_addr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (family != AF_INET)
|
2004-10-06 09:50:18 +02:00
|
|
|
continue;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
/* Get the canonical hostname. */
|
|
|
|
p++;
|
2006-10-18 05:42:06 +02:00
|
|
|
while (ISSPACE(*p))
|
2004-10-06 09:50:18 +02:00
|
|
|
p++;
|
2003-10-07 23:54:04 +02:00
|
|
|
if (!*p)
|
2004-10-06 09:50:18 +02:00
|
|
|
continue;
|
2003-10-07 23:54:04 +02:00
|
|
|
q = p;
|
2006-10-18 05:42:06 +02:00
|
|
|
while (*q && !ISSPACE(*q))
|
2004-10-06 09:50:18 +02:00
|
|
|
q++;
|
2003-10-07 23:54:04 +02:00
|
|
|
end_at_hostname = (*q == 0);
|
|
|
|
*q = 0;
|
|
|
|
canonical = p;
|
|
|
|
|
|
|
|
naliases = 0;
|
|
|
|
if (!end_at_hostname)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
/* Count the aliases. */
|
|
|
|
p = q + 1;
|
2006-10-18 05:42:06 +02:00
|
|
|
while (ISSPACE(*p))
|
2004-10-06 09:50:18 +02:00
|
|
|
p++;
|
|
|
|
while (*p)
|
|
|
|
{
|
2006-10-18 05:42:06 +02:00
|
|
|
while (*p && !ISSPACE(*p))
|
2004-10-06 09:50:18 +02:00
|
|
|
p++;
|
2006-10-18 05:42:06 +02:00
|
|
|
while (ISSPACE(*p))
|
2004-10-06 09:50:18 +02:00
|
|
|
p++;
|
|
|
|
naliases++;
|
|
|
|
}
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
/* Allocate memory for the host structure. */
|
|
|
|
hostent = malloc(sizeof(struct hostent));
|
|
|
|
if (!hostent)
|
2004-10-06 09:50:18 +02:00
|
|
|
break;
|
2003-10-07 23:54:04 +02:00
|
|
|
hostent->h_aliases = NULL;
|
|
|
|
hostent->h_addr_list = NULL;
|
|
|
|
hostent->h_name = strdup(canonical);
|
|
|
|
if (!hostent->h_name)
|
2004-10-06 09:50:18 +02:00
|
|
|
break;
|
2003-10-07 23:54:04 +02:00
|
|
|
hostent->h_addr_list = malloc(2 * sizeof(char *));
|
|
|
|
if (!hostent->h_addr_list)
|
2004-10-06 09:50:18 +02:00
|
|
|
break;
|
2005-04-08 17:41:31 +02:00
|
|
|
hostent->h_addr_list[0] = malloc(addrlen);
|
2003-10-07 23:54:04 +02:00
|
|
|
if (!hostent->h_addr_list[0])
|
2004-10-06 09:50:18 +02:00
|
|
|
break;
|
2003-10-07 23:54:04 +02:00
|
|
|
hostent->h_aliases = malloc((naliases + 1) * sizeof(char *));
|
|
|
|
if (!hostent->h_aliases)
|
2004-10-06 09:50:18 +02:00
|
|
|
break;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
/* Copy in aliases. */
|
|
|
|
naliases = 0;
|
|
|
|
if (!end_at_hostname)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
p = canonical + strlen(canonical) + 1;
|
2006-10-18 05:42:06 +02:00
|
|
|
while (ISSPACE(*p))
|
2004-10-06 09:50:18 +02:00
|
|
|
p++;
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
q = p;
|
2006-10-18 05:42:06 +02:00
|
|
|
while (*q && !ISSPACE(*q))
|
2004-10-06 09:50:18 +02:00
|
|
|
q++;
|
|
|
|
hostent->h_aliases[naliases] = malloc(q - p + 1);
|
|
|
|
if (hostent->h_aliases[naliases] == NULL)
|
|
|
|
break;
|
|
|
|
memcpy(hostent->h_aliases[naliases], p, q - p);
|
|
|
|
hostent->h_aliases[naliases][q - p] = 0;
|
|
|
|
p = q;
|
2006-10-18 05:42:06 +02:00
|
|
|
while (ISSPACE(*p))
|
2004-10-06 09:50:18 +02:00
|
|
|
p++;
|
|
|
|
naliases++;
|
|
|
|
}
|
|
|
|
if (*p)
|
|
|
|
break;
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
hostent->h_aliases[naliases] = NULL;
|
|
|
|
|
2005-04-08 17:41:31 +02:00
|
|
|
hostent->h_addrtype = family;
|
|
|
|
hostent->h_length = addrlen;
|
|
|
|
if (family == AF_INET)
|
|
|
|
memcpy(hostent->h_addr_list[0], &addr, addrlen);
|
|
|
|
else if (family == AF_INET6)
|
|
|
|
memcpy(hostent->h_addr_list[0], &addr6, addrlen);
|
2003-10-07 23:54:04 +02:00
|
|
|
hostent->h_addr_list[1] = NULL;
|
|
|
|
*host = hostent;
|
2004-05-27 09:09:21 +02:00
|
|
|
free(line);
|
2003-10-07 23:54:04 +02:00
|
|
|
return ARES_SUCCESS;
|
|
|
|
}
|
2004-05-27 09:09:21 +02:00
|
|
|
if(line)
|
|
|
|
free(line);
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
if (status == ARES_SUCCESS)
|
|
|
|
{
|
|
|
|
/* Memory allocation failure; clean up. */
|
|
|
|
if (hostent)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
2004-05-27 08:42:48 +02:00
|
|
|
if(hostent->h_name)
|
|
|
|
free((char *) hostent->h_name);
|
2004-10-06 09:50:18 +02:00
|
|
|
if (hostent->h_aliases)
|
|
|
|
{
|
|
|
|
for (alias = hostent->h_aliases; *alias; alias++)
|
|
|
|
free(*alias);
|
|
|
|
}
|
2004-05-27 08:42:48 +02:00
|
|
|
if(hostent->h_aliases)
|
|
|
|
free(hostent->h_aliases);
|
2004-10-06 09:50:18 +02:00
|
|
|
if (hostent->h_addr_list && hostent->h_addr_list[0])
|
|
|
|
free(hostent->h_addr_list[0]);
|
2004-05-27 08:42:48 +02:00
|
|
|
if(hostent->h_addr_list)
|
|
|
|
free(hostent->h_addr_list);
|
|
|
|
free(hostent);
|
2004-10-06 09:50:18 +02:00
|
|
|
}
|
2007-06-02 21:42:25 +02:00
|
|
|
*host = NULL;
|
2003-10-07 23:54:04 +02:00
|
|
|
return ARES_ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|