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 static const char rcsid[] = "$Id: ares__get_hostent.c,v 1.3 2000/02/17 18:39:58 ghudson Exp $";
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
28 #include "ares_private.h"
30 int ares__get_hostent(FILE *fp, struct hostent **host)
32 char *line = NULL, *p, *q, *canonical, **alias;
33 int status, linesize, end_at_hostname, naliases;
35 struct hostent *hostent = NULL;
37 while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
39 /* Skip comment lines; terminate line at comment character. */
40 if (*line == '#' || !*line)
42 p = strchr(line, '#');
46 /* Get the address part. */
48 while (*p && !isspace((unsigned char)*p))
53 addr.s_addr = inet_addr(line);
54 if (addr.s_addr == INADDR_NONE)
57 /* Get the canonical hostname. */
59 while (isspace((unsigned char)*p))
64 while (*q && !isspace((unsigned char)*q))
66 end_at_hostname = (*q == 0);
73 /* Count the aliases. */
75 while (isspace((unsigned char)*p))
79 while (*p && !isspace((unsigned char)*p))
81 while (isspace((unsigned char)*p))
87 /* Allocate memory for the host structure. */
88 hostent = malloc(sizeof(struct hostent));
91 hostent->h_aliases = NULL;
92 hostent->h_addr_list = NULL;
93 hostent->h_name = strdup(canonical);
96 hostent->h_addr_list = malloc(2 * sizeof(char *));
97 if (!hostent->h_addr_list)
99 hostent->h_addr_list[0] = malloc(sizeof(struct in_addr));
100 if (!hostent->h_addr_list[0])
102 hostent->h_aliases = malloc((naliases + 1) * sizeof(char *));
103 if (!hostent->h_aliases)
106 /* Copy in aliases. */
108 if (!end_at_hostname)
110 p = canonical + strlen(canonical) + 1;
111 while (isspace((unsigned char)*p))
116 while (*q && !isspace((unsigned char)*q))
118 hostent->h_aliases[naliases] = malloc(q - p + 1);
119 if (hostent->h_aliases[naliases] == NULL)
121 memcpy(hostent->h_aliases[naliases], p, q - p);
122 hostent->h_aliases[naliases][q - p] = 0;
124 while (isspace((unsigned char)*p))
131 hostent->h_aliases[naliases] = NULL;
133 hostent->h_addrtype = AF_INET;
134 hostent->h_length = sizeof(struct in_addr);
135 memcpy(hostent->h_addr_list[0], &addr, sizeof(struct in_addr));
136 hostent->h_addr_list[1] = NULL;
143 if (status == ARES_SUCCESS)
145 /* Memory allocation failure; clean up. */
148 free((char *) hostent->h_name);
149 if (hostent->h_aliases)
151 for (alias = hostent->h_aliases; *alias; alias++)
154 free(hostent->h_aliases);
155 if (hostent->h_addr_list)
156 free(hostent->h_addr_list[0]);
157 free(hostent->h_addr_list);