reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
ares__get_hostent.c
Go to the documentation of this file.
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
17#include <sys/types.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <ctype.h>
22
23#ifndef WIN32
24#include <netdb.h>
25#include <sys/socket.h>
26#include <netinet/in.h>
27#include <arpa/inet.h>
28#endif
29
30
31#include "ares.h"
32#include "ares_private.h"
33
34int 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 /* TODO - add suppport for V6 addresses in hosts file */
58#if defined(_MSC_VER) && _MSC_VER >= 1800 /* removing compilation warning in VS2013+ */
59 inet_pton(AF_INET, line, &addr.s_addr);
60#else
61 addr.s_addr = inet_addr(line);
62#endif
63 if (addr.s_addr == INADDR_NONE)
64 continue;
65
66 /* Get the canonical hostname. */
67 p++;
68 while (isspace((unsigned char)*p))
69 p++;
70 if (!*p)
71 continue;
72 q = p;
73 while (*q && !isspace((unsigned char)*q))
74 q++;
75 end_at_hostname = (*q == 0);
76 *q = 0;
77 canonical = p;
78
79 naliases = 0;
80 if (!end_at_hostname)
81 {
82 /* Count the aliases. */
83 p = q + 1;
84 while (isspace((unsigned char)*p))
85 p++;
86 while (*p)
87 {
88 while (*p && !isspace((unsigned char)*p))
89 p++;
90 while (isspace((unsigned char)*p))
91 p++;
92 naliases++;
93 }
94 }
95
96 /* Allocate memory for the host structure. */
97 hostent = malloc(sizeof(struct hostent));
98 if (!hostent)
99 break;
100 hostent->h_aliases = NULL;
101 hostent->h_addr_list = NULL;
102 hostent->h_name = strdup(canonical);
103 if (!hostent->h_name)
104 break;
105 hostent->h_addr_list = malloc(2 * sizeof(char *));
106 if (!hostent->h_addr_list)
107 break;
108 hostent->h_addr_list[0] = malloc(sizeof(struct in_addr));
109 if (!hostent->h_addr_list[0])
110 break;
111 hostent->h_aliases = malloc((naliases + 1) * sizeof(char *));
112 if (!hostent->h_aliases)
113 break;
114
115 /* Copy in aliases. */
116 naliases = 0;
117 if (!end_at_hostname)
118 {
119 p = canonical + strlen(canonical) + 1;
120 while (isspace((unsigned char)*p))
121 p++;
122 while (*p)
123 {
124 q = p;
125 while (*q && !isspace((unsigned char)*q))
126 q++;
127 hostent->h_aliases[naliases] = malloc(q - p + 1);
128 if (hostent->h_aliases[naliases] == NULL)
129 break;
130 memcpy(hostent->h_aliases[naliases], p, q - p);
131 hostent->h_aliases[naliases][q - p] = 0;
132 p = q;
133 while (isspace((unsigned char)*p))
134 p++;
135 naliases++;
136 }
137 if (*p)
138 break;
139 }
140 hostent->h_aliases[naliases] = NULL;
141
142 hostent->h_addrtype = AF_INET;
143 hostent->h_length = sizeof(struct in_addr);
144 memcpy(hostent->h_addr_list[0], &addr, sizeof(struct in_addr));
145 hostent->h_addr_list[1] = NULL;
146 *host = hostent;
147 free(line);
148 return ARES_SUCCESS;
149 }
150 free(line);
151
152 if (status == ARES_SUCCESS)
153 {
154 /* Memory allocation failure; clean up. */
155 if (hostent)
156 {
157 free((char *) hostent->h_name);
158 if (hostent->h_aliases)
159 {
160 for (alias = hostent->h_aliases; *alias; alias++)
161 free(*alias);
162 }
163 free(hostent->h_aliases);
164 if (hostent->h_addr_list)
165 free(hostent->h_addr_list[0]);
166 free(hostent->h_addr_list);
167 }
168 free(hostent);
169 return ARES_ENOMEM;
170 }
171
172 return status;
173}
#define INADDR_NONE
Definition adig.c:51
#define ARES_SUCCESS
Definition ares.h:79
#define ARES_ENOMEM
Definition ares.h:98
int ares__get_hostent(FILE *fp, struct hostent **host)
int ares__read_line(FILE *fp, char **buf, int *bufsize)
void list()
Definition userAdmin.cxx:66