1 /* Copyright 1998 by the Massachusetts Institute of Technology.
3 * Permission to use, copy, modify, and distribute this
4 * software and its documentation for any purpose and without
5 * fee is hereby granted, provided that the above copyright
6 * notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting
8 * documentation, and that the name of M.I.T. not be used in
9 * advertising or publicity pertaining to distribution of the
10 * software without specific, written prior permission.
11 * M.I.T. makes no representations about the suitability of
12 * this software for any purpose. It is provided "as is"
13 * without express or implied warranty.
17 #include <sys/types.h>
19 #if !defined(WIN32) || defined(WATT32)
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
32 #include "ares_private.h"
34 int ares__get_hostent(FILE *fp, struct hostent **host)
36 char *line = NULL, *p, *q, *canonical, **alias;
37 int status, linesize, end_at_hostname, naliases;
39 struct hostent *hostent = NULL;
41 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
43 /* Skip comment lines; terminate line at comment character. */
44 if (*line == '#' || !*line)
46 p = strchr(line, '#');
50 /* Get the address part. */
52 while (*p && !isspace((unsigned char)*p))
57 addr.s_addr = inet_addr(line);
58 if (addr.s_addr == INADDR_NONE)
61 /* Get the canonical hostname. */
63 while (isspace((unsigned char)*p))
68 while (*q && !isspace((unsigned char)*q))
70 end_at_hostname = (*q == 0);
77 /* Count the aliases. */
79 while (isspace((unsigned char)*p))
83 while (*p && !isspace((unsigned char)*p))
85 while (isspace((unsigned char)*p))
91 /* Allocate memory for the host structure. */
92 hostent = malloc(sizeof(struct hostent));
95 hostent->h_aliases = NULL;
96 hostent->h_addr_list = NULL;
97 hostent->h_name = strdup(canonical);
100 hostent->h_addr_list = malloc(2 * sizeof(char *));
101 if (!hostent->h_addr_list)
103 hostent->h_addr_list[0] = malloc(sizeof(struct in_addr));
104 if (!hostent->h_addr_list[0])
106 hostent->h_aliases = malloc((naliases + 1) * sizeof(char *));
107 if (!hostent->h_aliases)
110 /* Copy in aliases. */
112 if (!end_at_hostname)
114 p = canonical + strlen(canonical) + 1;
115 while (isspace((unsigned char)*p))
120 while (*q && !isspace((unsigned char)*q))
122 hostent->h_aliases[naliases] = malloc(q - p + 1);
123 if (hostent->h_aliases[naliases] == NULL)
125 memcpy(hostent->h_aliases[naliases], p, q - p);
126 hostent->h_aliases[naliases][q - p] = 0;
128 while (isspace((unsigned char)*p))
135 hostent->h_aliases[naliases] = NULL;
137 hostent->h_addrtype = AF_INET;
138 hostent->h_length = sizeof(struct in_addr);
139 memcpy(hostent->h_addr_list[0], &addr, sizeof(struct in_addr));
140 hostent->h_addr_list[1] = NULL;
148 if (status == ARES_SUCCESS)
150 /* Memory allocation failure; clean up. */
154 free((char *) hostent->h_name);
155 if (hostent->h_aliases)
157 for (alias = hostent->h_aliases; *alias; alias++)
160 if(hostent->h_aliases)
161 free(hostent->h_aliases);
162 if (hostent->h_addr_list && hostent->h_addr_list[0])
163 free(hostent->h_addr_list[0]);
164 if(hostent->h_addr_list)
165 free(hostent->h_addr_list);