removed tabs and trailing whitespace from source
[platform/upstream/c-ares.git] / ares__get_hostent.c
1 /* Copyright 1998 by the Massachusetts Institute of Technology.
2  *
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.
14  */
15
16 #include "setup.h"
17 #include <sys/types.h>
18
19 #if !defined(WIN32) || defined(WATT32)
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <netdb.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <ctype.h>
30
31 #include "ares.h"
32 #include "ares_private.h"
33
34 int ares__get_hostent(FILE *fp, struct hostent **host)
35 {
36   char *line = NULL, *p, *q, *canonical, **alias;
37   int status, linesize, end_at_hostname, naliases;
38   struct in_addr addr;
39   struct hostent *hostent = NULL;
40
41   while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
42     {
43       /* Skip comment lines; terminate line at comment character. */
44       if (*line == '#' || !*line)
45         continue;
46       p = strchr(line, '#');
47       if (p)
48         *p = 0;
49
50       /* Get the address part. */
51       p = line;
52       while (*p && !isspace((unsigned char)*p))
53         p++;
54       if (!*p)
55         continue;
56       *p = 0;
57       addr.s_addr = inet_addr(line);
58       if (addr.s_addr == INADDR_NONE)
59         continue;
60
61       /* Get the canonical hostname. */
62       p++;
63       while (isspace((unsigned char)*p))
64         p++;
65       if (!*p)
66         continue;
67       q = p;
68       while (*q && !isspace((unsigned char)*q))
69         q++;
70       end_at_hostname = (*q == 0);
71       *q = 0;
72       canonical = p;
73
74       naliases = 0;
75       if (!end_at_hostname)
76         {
77           /* Count the aliases. */
78           p = q + 1;
79           while (isspace((unsigned char)*p))
80             p++;
81           while (*p)
82             {
83               while (*p && !isspace((unsigned char)*p))
84                 p++;
85               while (isspace((unsigned char)*p))
86                 p++;
87               naliases++;
88             }
89         }
90
91       /* Allocate memory for the host structure. */
92       hostent = malloc(sizeof(struct hostent));
93       if (!hostent)
94         break;
95       hostent->h_aliases = NULL;
96       hostent->h_addr_list = NULL;
97       hostent->h_name = strdup(canonical);
98       if (!hostent->h_name)
99         break;
100       hostent->h_addr_list = malloc(2 * sizeof(char *));
101       if (!hostent->h_addr_list)
102         break;
103       hostent->h_addr_list[0] = malloc(sizeof(struct in_addr));
104       if (!hostent->h_addr_list[0])
105         break;
106       hostent->h_aliases = malloc((naliases + 1) * sizeof(char *));
107       if (!hostent->h_aliases)
108         break;
109
110       /* Copy in aliases. */
111       naliases = 0;
112       if (!end_at_hostname)
113         {
114           p = canonical + strlen(canonical) + 1;
115           while (isspace((unsigned char)*p))
116             p++;
117           while (*p)
118             {
119               q = p;
120               while (*q && !isspace((unsigned char)*q))
121                 q++;
122               hostent->h_aliases[naliases] = malloc(q - p + 1);
123               if (hostent->h_aliases[naliases] == NULL)
124                 break;
125               memcpy(hostent->h_aliases[naliases], p, q - p);
126               hostent->h_aliases[naliases][q - p] = 0;
127               p = q;
128               while (isspace((unsigned char)*p))
129                 p++;
130               naliases++;
131             }
132           if (*p)
133             break;
134         }
135       hostent->h_aliases[naliases] = NULL;
136
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;
141       *host = hostent;
142       free(line);
143       return ARES_SUCCESS;
144     }
145   if(line)
146     free(line);
147
148   if (status == ARES_SUCCESS)
149     {
150       /* Memory allocation failure; clean up. */
151       if (hostent)
152         {
153           if(hostent->h_name)
154             free((char *) hostent->h_name);
155           if (hostent->h_aliases)
156             {
157               for (alias = hostent->h_aliases; *alias; alias++)
158                 free(*alias);
159             }
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);
166           free(hostent);
167         }
168       return ARES_ENOMEM;
169     }
170
171   return status;
172 }