Repository navigation
Expand file tree
/
Copy pathaddrinfo.c
More file actions
executable file
·322 lines (277 loc) · 6.88 KB
/
Copy pathaddrinfo.c
File metadata and controls
executable file
·322 lines (277 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
/*
* getaddrinfo and freeaddrinfo are based on
* http://www.opengroup.org/onlinepubs/009695399/functions/getaddrinfo.html
*/
void freeaddrinfo(struct addrinfo *ai)
{
struct addrinfo *next;
while (ai)
{
/* preserve next pointer */
next = ai->ai_next;
/* free each member of the struct and the struct itself */
if (ai->ai_addr) free(ai->ai_addr);
if (ai->ai_canonname) free(ai->ai_canonname);
free(ai);
/* continue to free the next element of the linked list */
ai = next;
}
}
static int getaddrinfo_parse_hints(
const struct addrinfo *hints,
int *flags,
int *socktype,
int *protocol)
{
assert(flags);
assert(socktype);
/*
* if hints is not specified, no flags are specified and all
* socktypes must be returned
*/
if (!hints)
{
*flags = 0;
*socktype = 0;
*protocol = 0;
return 0;
}
/* check hints correctness */
if (hints->ai_addrlen || hints->ai_addr ||
hints->ai_canonname || hints->ai_next)
{
errno = EINVAL;
return EAI_SYSTEM;
}
/* check flags */
*flags = hints->ai_flags;
if (*flags & ~(AI_PASSIVE | AI_CANONNAME |
AI_NUMERICHOST | AI_NUMERICSERV))
return EAI_BADFLAGS;
/* only support IPv4 */
if (hints->ai_family != AF_UNSPEC && hints->ai_family != AF_INET)
return EAI_FAMILY;
/* only support SOCK_STREAM and SOCK_DGRAM */
*socktype = hints->ai_socktype;
switch (*socktype)
{
case 0:
case SOCK_STREAM:
case SOCK_DGRAM: break;
default: return EAI_SOCKTYPE;
}
/* get protocol */
*protocol = hints->ai_protocol;
return 0;
}
static int getaddrinfo_resolve_hostname(
const char *nodename,
int flags,
char ***addr_list,
const char **canonname)
{
static struct in_addr addr;
static char *addr_array[2];
struct hostent *hostent;
assert(addr_list);
assert(canonname);
/* if no hostname is specified, use local address */
if (!nodename)
{
if ((flags & AI_PASSIVE) == AI_PASSIVE)
addr.s_addr = htonl(INADDR_ANY);
else
addr.s_addr = htonl(INADDR_LOOPBACK);
addr_array[0] = (char *) &addr;
addr_array[1] = NULL;
*addr_list = addr_array;
*canonname = "localhost";
return 0;
}
if (!*nodename)
return EAI_NONAME;
/* convert literal IP address */
addr.s_addr = inet_addr(nodename);
if (addr.s_addr != (in_addr_t) -1)
{
addr_array[0] = (char *) &addr;
addr_array[1] = NULL;
*addr_list = addr_array;
*canonname = NULL;
return 0;
}
/* AI_NUMERICHOST avoids DNS lookup */
if ((flags & AI_NUMERICHOST) == AI_NUMERICHOST)
return EAI_NONAME;
/* attempt DNS lookup */
hostent = gethostbyname(nodename);
if (!hostent)
switch(h_errno)
{
case HOST_NOT_FOUND: return EAI_NONAME;
case NO_DATA: return EAI_FAIL;
case NO_RECOVERY: return EAI_FAIL;
case TRY_AGAIN: return EAI_AGAIN;
default: assert(0); return EAI_FAIL;
}
/* assumption: only IPv4 addresses returned */
assert(hostent->h_addrtype == AF_INET);
assert(hostent->h_length == sizeof(addr));
*addr_list = hostent->h_addr_list;
*canonname = hostent->h_name;
return 0;
}
int getaddrinfo_resolve_servname(
const char *servname,
int flags,
int socktype,
unsigned short *port,
int *protocol)
{
char *endptr;
long port_long;
struct protoent *protoent;
struct servent *servent;
assert(port);
assert(protocol);
/* if not specified, set port and protocol to zero */
if (!servname)
{
*port = 0;
*protocol = 0;
return 0;
}
if (!*servname)
return EAI_SERVICE;
/* try to parse port number */
port_long = strtol(servname, &endptr, 0);
if (!*endptr)
{
/* check whether port is within range */
if (port_long < 0 || port_long > (unsigned short) ~0)
return EAI_SERVICE;
*port = htons(port_long);
*protocol = 0;
return 0;
}
/* AI_NUMERICSERV avoids lookup */
if ((flags & AI_NUMERICSERV) == AI_NUMERICSERV)
return EAI_SERVICE;
/* look up port number */
servent = getservbyname(servname,
(socktype == SOCK_STREAM) ? "tcp" : "udp");
if (!servent)
return EAI_SERVICE;
*port = servent->s_port;
/* determine protocol number */
protoent = getprotobyname(servent->s_proto);
*protocol = protoent ? protoent->p_proto : 0;
return 0;
}
int getaddrinfo(
const char *nodename,
const char *servname,
const struct addrinfo *hints,
struct addrinfo **res)
{
struct addrinfo *addrinfo, **addrinfo_p;
char **addr_list;
const char *canonname;
int flags, i, protocol, protocol_spec, r, result;
unsigned short port;
struct sockaddr_in *sockaddr;
int socktype, socktype_spec;
/*
* The following flags are supported:
* - AI_CANONNAME
* - AI_PASSIVE
* - AI_NUMERICHOST
* - AI_NUMERICSERV
*
* The following flags not supported due to lack of IPv6 support:
* - AI_ADDRCONFIG
* - AI_ALL
* - AI_V4MAPPED
*/
/* check arguments */
if ((!nodename && !servname) || !r
7E71
es)
return EAI_NONAME;
/* parse hints */
if ((r = getaddrinfo_parse_hints(hints, &flags, &socktype_spec, &protocol_spec)))
return r;
/* resolve hostname */
if ((r = getaddrinfo_resolve_hostname(nodename, flags, &addr_list, &canonname)))
return r;
/* return a result record for each address */
addrinfo_p = res;
*addrinfo_p = NULL;
result = EAI_NONAME;
while (*addr_list)
{
/* return a result record for each socktype */
for (i = 0; i < 2; i++)
{
/* should current socktype be selected? */
socktype = (i == 0) ? SOCK_STREAM : SOCK_DGRAM;
if (socktype_spec != 0 && socktype_spec != socktype)
continue;
/* resolve port */
if ((r = getaddrinfo_resolve_servname(servname, flags, socktype, &port, &protocol)))
{
freeaddrinfo(*res);
return r;
}
/* enforce matching protocol */
if (!protocol)
protocol = protocol_spec;
else if (protocol_spec && protocol != protocol_spec)
continue;
/* allocate result */
*addrinfo_p = addrinfo = (struct addrinfo *) calloc(1, sizeof(struct addrinfo));
if (!addrinfo)
{
freeaddrinfo(*res);
return EAI_MEMORY;
}
sockaddr = (struct sockaddr_in *) calloc(1, sizeof(struct sockaddr_in));
if (!sockaddr)
{
freeaddrinfo(*res);
return EAI_MEMORY;
}
/* provide information in result */
addrinfo->ai_family = AF_INET;
addrinfo->ai_socktype = socktype;
addrinfo->ai_protocol = protocol;
addrinfo->ai_addrlen = sizeof(*sockaddr);
addrinfo->ai_addr = (struct sockaddr *) sockaddr;
sockaddr->sin_family = AF_INET;
sockaddr->sin_port = port;
memcpy(&sockaddr->sin_addr, *addr_list, sizeof(sockaddr->sin_addr));
if (((flags & AI_CANONNAME) == AI_CANONNAME) && canonname)
{
addrinfo->ai_canonname = strdup(canonname);
if (!addrinfo->ai_canonname)
{
freeaddrinfo(*res);
return EAI_MEMORY;
}
}
result = 0;
/* chain next result to the current one */
addrinfo_p = &addrinfo->ai_next;
}
/* move on to next address */
addr_list++;
}
/* found anything meaningful? */
return result;
}