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.
16 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
33 #include "ares_private.h"
35 int ares__get_hostent(FILE *fp, struct hostent **host)
37 char *line = NULL, *p, *q, *canonical, **alias;
38 int status, linesize, end_at_hostname, naliases;
40 struct hostent *hostent = NULL;
42 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
44 /* Skip comment lines; terminate line at comment character. */
45 if (*line == '#' || !*line)
47 p = strchr(line, '#');
51 /* Get the address part. */
53 while (*p && !isspace((unsigned char)*p))
58 addr.s_addr = inet_addr(line);
59 if (addr.s_addr == INADDR_NONE)
62 /* Get the canonical hostname. */
64 while (isspace((unsigned char)*p))
69 while (*q && !isspace((unsigned char)*q))
71 end_at_hostname = (*q == 0);
78 /* Count the aliases. */
80 while (isspace((unsigned char)*p))
84 while (*p && !isspace((unsigned char)*p))
86 while (isspace((unsigned char)*p))
92 /* Allocate memory for the host structure. */
93 hostent = malloc(sizeof(struct hostent));
96 hostent->h_aliases = NULL;
97 hostent->h_addr_list = NULL;
98 hostent->h_name = strdup(canonical);
101 hostent->h_addr_list = malloc(2 * sizeof(char *));
102 if (!hostent->h_addr_list)
104 hostent->h_addr_list[0] = malloc(sizeof(struct in_addr));
105 if (!hostent->h_addr_list[0])
107 hostent->h_aliases = malloc((naliases + 1) * sizeof(char *));
108 if (!hostent->h_aliases)
111 /* Copy in aliases. */
113 if (!end_at_hostname)
115 p = canonical + strlen(canonical) + 1;
116 while (isspace((unsigned char)*p))
121 while (*q && !isspace((unsigned char)*q))
123 hostent->h_aliases[naliases] = malloc(q - p + 1);
124 if (hostent->h_aliases[naliases] == NULL)
126 memcpy(hostent->h_aliases[naliases], p, q - p);
127 hostent->h_aliases[naliases][q - p] = 0;
129 while (isspace((unsigned char)*p))
136 hostent->h_aliases[naliases] = NULL;
138 hostent->h_addrtype = AF_INET;
139 hostent->h_length = sizeof(struct in_addr);
140 memcpy(hostent->h_addr_list[0], &addr, sizeof(struct in_addr));
141 hostent->h_addr_list[1] = NULL;
149 if (status == ARES_SUCCESS)
151 /* Memory allocation failure; clean up. */
155 free((char *) hostent->h_name);
156 if (hostent->h_aliases)
158 for (alias = hostent->h_aliases; *alias; alias++)
161 if(hostent->h_aliases)
162 free(hostent->h_aliases);
163 if (hostent->h_addr_list && hostent->h_addr_list[0])
164 free(hostent->h_addr_list[0]);
165 if(hostent->h_addr_list)
166 free(hostent->h_addr_list);