112
101
"Unknown error", /* EAI_MAX */
115
static int ipv4_aton P((const char *, struct sockaddr_storage *));
111
struct hostent **Addresses
116
* Encapsulate gethostbyname to control the error code
122
struct hostent **Addresses
125
*Addresses = gethostbyname(name);
116
130
static int do_nodename P((const char *nodename, struct addrinfo *ai,
117
131
const struct addrinfo *hints));
121
135
const struct addrinfo *hints, struct addrinfo **res)
125
struct sockaddr_in *sockin;
139
struct addrinfo *ai = NULL;
141
const char *proto = NULL;
142
int family, socktype, flags, protocol;
146
* If no name is provide just return an error
148
if (nodename == NULL && servname == NULL)
127
151
ai = calloc(sizeof(struct addrinfo), 1);
129
153
return (EAI_MEMORY);
131
if (nodename != NULL) {
132
rval = do_nodename(nodename, ai, hints);
138
if (nodename == NULL && hints != NULL) {
139
ai->ai_addr = calloc(sizeof(struct sockaddr_storage), 1);
140
if (ai->ai_addr == NULL) {
144
ai->ai_family = AF_INET;
145
ai->ai_addrlen = sizeof(struct sockaddr_storage);
146
sockin = (struct sockaddr_in *)ai->ai_addr;
147
sockin->sin_family = AF_INET;
148
sockin->sin_addr.s_addr = htonl(INADDR_ANY);
149
#ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
150
ai->ai_addr->sa_len = SOCKLEN(ai->ai_addr);
156
* Copy default values from hints, if available
159
ai->ai_flags = hints->ai_flags;
160
ai->ai_family = hints->ai_family;
161
ai->ai_socktype = hints->ai_socktype;
162
ai->ai_protocol = hints->ai_protocol;
164
family = hints->ai_family;
165
socktype = hints->ai_socktype;
166
protocol = hints->ai_protocol;
167
flags = hints->ai_flags;
171
switch (hints->ai_socktype) {
182
switch (hints->ai_socktype) {
194
return (EAI_SOCKTYPE);
199
switch (hints->ai_socktype) {
207
return (EAI_SOCKTYPE);
221
rval = do_nodename(nodename, ai, hints);
228
* First, look up the service name (port) if it was
229
* requested. If the socket type wasn't specified, then
230
* try and figure it out.
153
232
if (servname != NULL) {
154
ai->ai_family = AF_INET;
155
ai->ai_socktype = SOCK_DGRAM;
156
if (strcmp(servname, "ntp") != 0) {
158
return (EAI_SERVICE);
235
port = strtol(servname, &e, 10);
238
return (EAI_SOCKTYPE);
239
if (port < 0 || port > 65535)
240
return (EAI_SERVICE);
241
port = htons((unsigned short) port);
243
sp = getservbyname(servname, proto);
245
return (EAI_SERVICE);
248
if (strcmp(sp->s_proto, "tcp") == 0)
249
socktype = SOCK_STREAM;
250
else if (strcmp(sp->s_proto, "udp") == 0)
251
socktype = SOCK_DGRAM;
160
sockin = (struct sockaddr_in *)ai->ai_addr;
161
sockin->sin_port = htons(NTP_PORT);
259
* Set up the port number
261
if (ai->ai_family == AF_INET)
262
((struct sockaddr_in *)ai->ai_addr)->sin_port = (unsigned short) port;
263
else if (ai->ai_family == AF_INET6)
264
((struct sockaddr_in6 *)ai->ai_addr)->sin6_port = (unsigned short) port;
212
328
struct addrinfo *ai,
213
329
const struct addrinfo *hints)
331
struct hostent *hp = NULL;
216
332
struct sockaddr_in *sockin;
333
struct sockaddr_in6 *sockin6;
218
336
ai->ai_addr = calloc(sizeof(struct sockaddr_storage), 1);
219
337
if (ai->ai_addr == NULL)
220
338
return (EAI_MEMORY);
222
if (hints != NULL && hints->ai_flags & AI_NUMERICHOST) {
223
if (ipv4_aton(nodename,
224
(struct sockaddr_storage *)ai->ai_addr) == 1) {
225
ai->ai_family = AF_INET;
226
ai->ai_addrlen = sizeof(struct sockaddr_in);
341
* For an empty node name just use the wildcard.
342
* NOTE: We need to assume that the address family is
343
* set elsewhere so that we can set the appropriate wildcard
345
if (nodename == NULL) {
346
ai->ai_addrlen = sizeof(struct sockaddr_storage);
347
if (ai->ai_family == AF_INET)
349
sockin = (struct sockaddr_in *)ai->ai_addr;
350
sockin->sin_family = (short) ai->ai_family;
351
sockin->sin_addr.s_addr = htonl(INADDR_ANY);
355
sockin6 = (struct sockaddr_in6 *)ai->ai_addr;
356
sockin6->sin6_family = (short) ai->ai_family;
358
* we have already zeroed out the address
359
* so we don't actually need to do this
360
* This assignment is causing problems so
361
* we don't do what this would do.
362
sockin6->sin6_addr = in6addr_any;
365
#ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
366
ai->ai_addr->sa_len = SOCKLEN(ai->ai_addr);
373
* See if we have an IPv6 address
375
if(strchr(nodename, ':') != NULL) {
376
if (inet_pton(AF_INET6, nodename,
377
&((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr) == 1) {
378
((struct sockaddr_in6 *)ai->ai_addr)->sin6_family = AF_INET6;
379
ai->ai_family = AF_INET6;
380
ai->ai_addrlen = sizeof(struct sockaddr_in6);
386
* See if we have an IPv4 address
388
if (inet_pton(AF_INET, nodename,
389
&((struct sockaddr_in *)ai->ai_addr)->sin_addr) == 1) {
390
((struct sockaddr *)ai->ai_addr)->sa_family = AF_INET;
391
ai->ai_family = AF_INET;
392
ai->ai_addrlen = sizeof(struct sockaddr_in);
397
* If the numeric host flag is set, don't attempt resolution
399
if (hints != NULL && (hints->ai_flags & AI_NUMERICHOST))
229
400
return (EAI_NONAME);
231
hp = gethostbyname(nodename);
406
errval = DNSlookup_name(nodename, AF_INET, &hp);
232
408
if (hp == NULL) {
233
if (h_errno == TRY_AGAIN)
409
if (errval == TRY_AGAIN || errval == EAI_AGAIN)
234
410
return (EAI_AGAIN);
236
if (ipv4_aton(nodename,
237
(struct sockaddr_storage *)ai->ai_addr) == 1) {
411
else if (errval == EAI_NONAME) {
412
if (inet_pton(AF_INET, nodename,
413
&((struct sockaddr_in *)ai->ai_addr)->sin_addr) == 1) {
414
((struct sockaddr *)ai->ai_addr)->sa_family = AF_INET;
238
415
ai->ai_family = AF_INET;
239
416
ai->ai_addrlen = sizeof(struct sockaddr_in);
245
426
ai->ai_family = hp->h_addrtype;
263
* ipv4_aton - return a net number (this is crude, but careful)
268
struct sockaddr_storage *saddr
275
char buf[80]; /* will core dump on really stupid stuff */
277
struct sockaddr_in *addr;
281
for (i = 0; i < 4; i++) {
283
while (isdigit((int)*cp))
291
} else if (*cp != '\0')
302
printf("ipv4_aton %s step %d buf %s temp %d netnum %lu\n",
303
num, i, buf, temp, (u_long)netnum);
311
"ipv4_aton: \"%s\" invalid host number, line ignored\n",
318
* make up socket address. Clear it out for neatness.
320
memset((void *)saddr, 0, sizeof(struct sockaddr_storage));
321
addr = (struct sockaddr_in *)saddr;
322
addr->sin_family = AF_INET;
323
addr->sin_port = htons(NTP_PORT);
324
addr->sin_addr.s_addr = htonl(netnum);
327
printf("ipv4_aton given %s, got %s.\n", num, ntoa(saddr));
331
443
#endif /* !ISC_PLATFORM_HAVEIPV6 */