| | 1 | /* |
| | 2 | * Copyright (c) 2001, 02 Motoyuki Kasahara |
| | 3 | * |
| | 4 | * Redistribution and use in source and binary forms, with or without |
| | 5 | * modification, are permitted provided that the following conditions |
| | 6 | * are met: |
| | 7 | * 1. Redistributions of source code must retain the above copyright |
| | 8 | * notice, this list of conditions and the following disclaimer. |
| | 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| | 10 | * notice, this list of conditions and the following disclaimer in the |
| | 11 | * documentation and/or other materials provided with the distribution. |
| | 12 | * 3. Neither the name of the project nor the names of its contributors |
| | 13 | * may be used to endorse or promote products derived from this software |
| | 14 | * without specific prior written permission. |
| | 15 | * |
| | 16 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
| | 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
| | 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| | 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| | 22 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| | 23 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| | 24 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| | 25 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| | 26 | * SUCH DAMAGE. |
| | 27 | */ |
| | 28 | |
| | 29 | /* |
| | 30 | * This program provides getaddrinfo() and getnameinfo() described in |
| | 31 | * RFC2133, 2553 and 3493. These functions are mainly used for IPv6 |
| | 32 | * application to resolve hostname or address. |
| | 33 | * |
| | 34 | * This program is designed to be working on traditional IPv4 systems |
| | 35 | * which don't have those functions. Therefore, this implementation |
| | 36 | * supports IPv4 only. |
| | 37 | * |
| | 38 | * This program is useful for application which should support both IPv6 |
| | 39 | * and traditional IPv4 systems. Use genuine getaddrinfo() and getnameinfo() |
| | 40 | * provided by system if the system supports IPv6. Otherwise, use this |
| | 41 | * implementation. |
| | 42 | * |
| | 43 | * This program is intended to be used in combination with GNU Autoconf. |
| | 44 | * |
| | 45 | * This program also provides freeaddrinfo() and gai_strerror(). |
| | 46 | * |
| | 47 | * To use this program in your application, insert the following lines to |
| | 48 | * C source files after including `sys/types.h', `sys/socket.h' and |
| | 49 | * `netdb.h'. `getaddrinfo.h' defines `struct addrinfo' and AI_, NI_, |
| | 50 | * EAI_ macros. |
| | 51 | * |
| | 52 | * #ifndef HAVE_GETADDRINFO |
| | 53 | * #include "getaddrinfo.h" |
| | 54 | * #endif |
| | 55 | * |
| | 56 | * Restriction: |
| | 57 | * getaddrinfo() and getnameinfo() of this program are NOT thread |
| | 58 | * safe, unless the cpp macro ENABLE_PTHREAD is defined. |
| | 59 | */ |
| | 60 | |
| | 61 | /* |
| | 62 | * Add the following code to your configure.ac (or configure.in). |
| | 63 | * AC_C_CONST |
| | 64 | * AC_HEADER_STDC |
| | 65 | * AC_CHECK_HEADERS(string.h memory.h stdlib.h) |
| | 66 | * AC_CHECK_FUNCS(memcpy) |
| | 67 | * AC_REPLACE_FUNCS(memset) |
| | 68 | * AC_TYPE_SOCKLEN_T |
| | 69 | * AC_TYPE_IN_PORT_T |
| | 70 | * AC_DECL_H_ERRNO |
| | 71 | * |
| | 72 | * AC_CHECK_FUNCS(getaddrinfo getnameinfo) |
| | 73 | * if test "$ac_cv_func_getaddrinfo$ac_cv_func_getnameinfo" != yesyes ; then |
| | 74 | * LIBOBJS="$LIBOBJS getaddrinfo.$ac_objext" |
| | 75 | * fi |
| | 76 | */ |
| | 77 | |
| | 78 | #ifdef HAVE_CONFIG_H |
| | 79 | #include "config.h" |
| | 80 | #endif |
| | 81 | |
| | 82 | #include <sys/types.h> |
| | 83 | #include <stdio.h> |
| | 84 | #include <sys/socket.h> |
| | 85 | #include <netinet/in.h> |
| | 86 | #include <arpa/inet.h> |
| | 87 | #include <netdb.h> |
| | 88 | |
| | 89 | #if defined(STDC_HEADERS) || defined(HAVE_STRING_H) |
| | 90 | #include <string.h> |
| | 91 | #if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H) |
| | 92 | #include <memory.h> |
| | 93 | #endif /* not STDC_HEADERS and HAVE_MEMORY_H */ |
| | 94 | #else /* not STDC_HEADERS and not HAVE_STRING_H */ |
| | 95 | #include <strings.h> |
| | 96 | #endif /* not STDC_HEADERS and not HAVE_STRING_H */ |
| | 97 | |
| | 98 | #ifdef HAVE_STDLIB_H |
| | 99 | #include <stdlib.h> |
| | 100 | #endif |
| | 101 | |
| | 102 | #ifdef ENABLE_PTHREAD |
| | 103 | #include <pthread.h> |
| | 104 | #endif |
| | 105 | |
| | 106 | #ifdef ENABLE_NLS |
| | 107 | #include <libintl.h> |
| | 108 | #endif |
| | 109 | |
| | 110 | #ifndef HAVE_MEMCPY |
| | 111 | #define memcpy(d, s, n) bcopy((s), (d), (n)) |
| | 112 | #ifdef __STDC__ |
| | 113 | void *memchr(const void *, int, size_t); |
| | 114 | int memcmp(const void *, const void *, size_t); |
| | 115 | void *memmove(void *, const void *, size_t); |
| | 116 | void *memset(void *, int, size_t); |
| | 117 | #else /* not __STDC__ */ |
| | 118 | char *memchr(); |
| | 119 | int memcmp(); |
| | 120 | char *memmove(); |
| | 121 | char *memset(); |
| | 122 | #endif /* not __STDC__ */ |
| | 123 | #endif /* not HAVE_MEMCPY */ |
| | 124 | |
| | 125 | #ifndef H_ERRNO_DECLARED |
| | 126 | extern int h_errno; |
| | 127 | #endif |
| | 128 | |
| | 129 | #include "getaddrinfo.h" |
| | 130 | |
| | 131 | #ifdef ENABLE_NLS |
| | 132 | #define _(string) gettext(string) |
| | 133 | #ifdef gettext_noop |
| | 134 | #define N_(string) gettext_noop(string) |
| | 135 | #else |
| | 136 | #define N_(string) (string) |
| | 137 | #endif |
| | 138 | #else |
| | 139 | #define gettext(string) (string) |
| | 140 | #define _(string) (string) |
| | 141 | #define N_(string) (string) |
| | 142 | #endif |
| | 143 | |
| | 144 | /* |
| | 145 | * Error messages for gai_strerror(). |
| | 146 | */ |
| | 147 | static char *eai_errlist[] = { |
| | 148 | N_("Success"), |
| | 149 | |
| | 150 | /* EAI_ADDRFAMILY */ |
| | 151 | N_("Address family for hostname not supported"), |
| | 152 | |
| | 153 | /* EAI_AGAIN */ |
| | 154 | N_("Temporary failure in name resolution"), |
| | 155 | |
| | 156 | /* EAI_BADFLAGS */ |
| | 157 | N_("Invalid value for ai_flags"), |
| | 158 | |
| | 159 | /* EAI_FAIL */ |
| | 160 | N_("Non-recoverable failure in name resolution"), |
| | 161 | |
| | 162 | /* EAI_FAMILY */ |
| | 163 | N_("ai_family not supported"), |
| | 164 | |
| | 165 | /* EAI_MEMORY */ |
| | 166 | N_("Memory allocation failure"), |
| | 167 | |
| | 168 | /* EAI_NONAME */ |
| | 169 | N_("hostname nor servname provided, or not known"), |
| | 170 | |
| | 171 | /* EAI_OVERFLOW */ |
| | 172 | N_("An argument buffer overflowed"), |
| | 173 | |
| | 174 | /* EAI_SERVICE */ |
| | 175 | N_("servname not supported for ai_socktype"), |
| | 176 | |
| | 177 | /* EAI_SOCKTYPE */ |
| | 178 | N_("ai_socktype not supported"), |
| | 179 | |
| | 180 | /* EAI_SYSTEM */ |
| | 181 | N_("System error returned in errno") |
| | 182 | }; |
| | 183 | |
| | 184 | /* |
| | 185 | * Default hints for getaddrinfo(). |
| | 186 | */ |
| | 187 | static struct addrinfo default_hints = { |
| | 188 | 0, PF_UNSPEC, 0, 0, 0, NULL, NULL, NULL |
| | 189 | }; |
| | 190 | |
| | 191 | /* |
| | 192 | * Mutex. |
| | 193 | */ |
| | 194 | #ifdef ENABLE_PTHREAD |
| | 195 | static pthread_mutex_t gai_mutex = PTHREAD_MUTEX_INITIALIZER; |
| | 196 | #endif |
| | 197 | |
| | 198 | /* |
| | 199 | * Declaration of static functions. |
| | 200 | */ |
| | 201 | #ifdef __STDC__ |
| | 202 | static int is_integer(const char *); |
| | 203 | static int is_address(const char *); |
| | 204 | static int itoa_length(int); |
| | 205 | #else |
| | 206 | static int is_integer(); |
| | 207 | static int is_address(); |
| | 208 | static int itoa_length(); |
| | 209 | #endif |
| | 210 | |
| | 211 | /* |
| | 212 | * gai_strerror(). |
| | 213 | */ |
| | 214 | const char * |
| | 215 | gai_strerror(ecode) |
| | 216 | int ecode; |
| | 217 | { |
| | 218 | if (ecode < 0 || ecode > EAI_SYSTEM) |
| | 219 | return _("Unknown error"); |
| | 220 | |
| | 221 | return gettext(eai_errlist[ecode]); |
| | 222 | } |
| | 223 | |
| | 224 | /* |
| | 225 | * freeaddrinfo(). |
| | 226 | */ |
| | 227 | void |
| | 228 | freeaddrinfo(ai) |
| | 229 | struct addrinfo *ai; |
| | 230 | { |
| | 231 | struct addrinfo *next_ai; |
| | 232 | |
| | 233 | while (ai != NULL) { |
| | 234 | if (ai->ai_canonname != NULL) |
| | 235 | free(ai->ai_canonname); |
| | 236 | if (ai->ai_addr != NULL) |
| | 237 | free(ai->ai_addr); |
| | 238 | next_ai = ai->ai_next; |
| | 239 | free(ai); |
| | 240 | ai = next_ai; |
| | 241 | } |
| | 242 | } |
| | 243 | |
| | 244 | /* |
| | 245 | * Return 1 if the string `s' represents an integer. |
| | 246 | */ |
| | 247 | static int |
| | 248 | is_integer(s) |
| | 249 | const char *s; |
| | 250 | { |
| | 251 | if (*s == '-' || *s == '+') |
| | 252 | s++; |
| | 253 | if (*s < '0' || '9' < *s) |
| | 254 | return 0; |
| | 255 | |
| | 256 | s++; |
| | 257 | while ('0' <= *s && *s <= '9') |
| | 258 | s++; |
| | 259 | |
| | 260 | return (*s == '\0'); |
| | 261 | } |
| | 262 | |
| | 263 | /* |
| | 264 | * Return 1 if the string `s' represents an IPv4 address. |
| | 265 | * Unlike inet_addr(), it doesn't permit malformed nortation such |
| | 266 | * as "192.168". |
| | 267 | */ |
| | 268 | static int |
| | 269 | is_address(s) |
| | 270 | const char *s; |
| | 271 | { |
| | 272 | const static char delimiters[] = {'.', '.', '.', '\0'}; |
| | 273 | int i, j; |
| | 274 | int octet; |
| | 275 | |
| | 276 | for (i = 0; i < 4; i++) { |
| | 277 | if (*s == '0' && *(s + 1) != delimiters[i]) |
| | 278 | return 0; |
| | 279 | for (j = 0, octet = 0; '0' <= *s && *s <= '9' && j < 3; s++, j++) |
| | 280 | octet = octet * 10 + (*s - '0'); |
| | 281 | if (j == 0 || octet > 255 || *s != delimiters[i]) |
| | 282 | return 0; |
| | 283 | s++; |
| | 284 | } |
| | 285 | |
| | 286 | return 1; |
| | 287 | } |
| | 288 | |
| | 289 | /* |
| | 290 | * Calcurate length of the string `s', where `s' is set by |
| | 291 | * sprintf(s, "%d", n). |
| | 292 | */ |
| | 293 | static int |
| | 294 | itoa_length(n) |
| | 295 | int n; |
| | 296 | { |
| | 297 | int result = 1; |
| | 298 | |
| | 299 | if (n < 0) { |
| | 300 | n = -n; |
| | 301 | result++; |
| | 302 | } |
| | 303 | |
| | 304 | while (n >= 10) { |
| | 305 | result++; |
| | 306 | n /= 10; |
| | 307 | } |
| | 308 | |
| | 309 | return result; |
| | 310 | } |
| | 311 | |
| | 312 | /* |
| | 313 | * getaddrinfo(). |
| | 314 | */ |
| | 315 | int |
| | 316 | getaddrinfo(nodename, servname, hints, res) |
| | 317 | const char *nodename; |
| | 318 | const char *servname; |
| | 319 | const struct addrinfo *hints; |
| | 320 | struct addrinfo **res; |
| | 321 | { |
| | 322 | struct addrinfo *head_res = NULL; |
| | 323 | struct addrinfo *tail_res = NULL; |
| | 324 | struct addrinfo *new_res; |
| | 325 | struct sockaddr_in *sa_in; |
| | 326 | struct in_addr **addr_list; |
| | 327 | struct in_addr *addr_list_buf[2]; |
| | 328 | struct in_addr addr_buf; |
| | 329 | struct in_addr **ap; |
| | 330 | struct servent *servent; |
| | 331 | struct hostent *hostent; |
| | 332 | const char *canonname = NULL; |
| | 333 | in_port_t port; |
| | 334 | int saved_h_errno; |
| | 335 | int result = 0; |
| | 336 | |
| | 337 | #ifdef ENABLE_PTHREAD |
| | 338 | pthread_mutex_lock(&gai_mutex); |
| | 339 | #endif |
| | 340 | |
| | 341 | saved_h_errno = h_errno; |
| | 342 | |
| | 343 | if (nodename == NULL && servname == NULL) { |
| | 344 | result = EAI_NONAME; |
| | 345 | goto end; |
| | 346 | } |
| | 347 | |
| | 348 | if (hints != NULL) { |
| | 349 | if (hints->ai_family != PF_INET && hints->ai_family != PF_UNSPEC) { |
| | 350 | result = EAI_FAMILY; |
| | 351 | goto end; |
| | 352 | } |
| | 353 | if (hints->ai_socktype != SOCK_DGRAM |
| | 354 | && hints->ai_socktype != SOCK_STREAM |
| | 355 | && hints->ai_socktype != 0) { |
| | 356 | result = EAI_SOCKTYPE; |
| | 357 | goto end; |
| | 358 | } |
| | 359 | } else { |
| | 360 | hints = &default_hints; |
| | 361 | } |
| | 362 | |
| | 363 | if (servname != NULL) { |
| | 364 | if (is_integer(servname)) |
| | 365 | port = htons(atoi(servname)); |
| | 366 | else { |
| | 367 | if (hints->ai_flags & AI_NUMERICSERV) { |
| | 368 | result = EAI_NONAME; |
| | 369 | goto end; |
| | 370 | } |
| | 371 | |
| | 372 | if (hints->ai_socktype == SOCK_DGRAM) |
| | 373 | servent = getservbyname(servname, "udp"); |
| | 374 | else if (hints->ai_socktype == SOCK_STREAM) |
| | 375 | servent = getservbyname(servname, "tcp"); |
| | 376 | else if (hints->ai_socktype == 0) |
| | 377 | servent = getservbyname(servname, "tcp"); |
| | 378 | else { |
| | 379 | result = EAI_SOCKTYPE; |
| | 380 | goto end; |
| | 381 | } |
| | 382 | |
| | 383 | if (servent == NULL) { |
| | 384 | result = EAI_SERVICE; |
| | 385 | goto end; |
| | 386 | } |
| | 387 | port = servent->s_port; |
| | 388 | } |
| | 389 | } else { |
| | 390 | port = htons(0); |
| | 391 | } |
| | 392 | |
| | 393 | if (nodename != NULL) { |
| | 394 | if (is_address(nodename)) { |
| | 395 | addr_buf.s_addr = inet_addr(nodename); |
| | 396 | addr_list_buf[0] = &addr_buf; |
| | 397 | addr_list_buf[1] = NULL; |
| | 398 | addr_list = addr_list_buf; |
| | 399 | |
| | 400 | if (hints->ai_flags & AI_CANONNAME |
| | 401 | && !(hints->ai_flags & AI_NUMERICHOST)) { |
| | 402 | hostent = gethostbyaddr((char *)&addr_buf, |
| | 403 | sizeof(struct in_addr), AF_INET); |
| | 404 | if (hostent != NULL) |
| | 405 | canonname = hostent->h_name; |
| | 406 | else |
| | 407 | canonname = nodename; |
| | 408 | } |
| | 409 | } else { |
| | 410 | if (hints->ai_flags & AI_NUMERICHOST) { |
| | 411 | result = EAI_NONAME; |
| | 412 | goto end; |
| | 413 | } |
| | 414 | |
| | 415 | hostent = gethostbyname(nodename); |
| | 416 | if (hostent == NULL) { |
| | 417 | switch (h_errno) { |
| | 418 | case HOST_NOT_FOUND: |
| | 419 | case NO_DATA: |
| | 420 | result = EAI_NONAME; |
| | 421 | goto end; |
| | 422 | case TRY_AGAIN: |
| | 423 | result = EAI_AGAIN; |
| | 424 | goto end; |
| | 425 | default: |
| | 426 | result = EAI_FAIL; |
| | 427 | goto end; |
| | 428 | } |
| | 429 | } |
| | 430 | addr_list = (struct in_addr **)hostent->h_addr_list; |
| | 431 | |
| | 432 | if (hints->ai_flags & AI_CANONNAME) |
| | 433 | canonname = hostent->h_name; |
| | 434 | } |
| | 435 | } else { |
| | 436 | if (hints->ai_flags & AI_PASSIVE) |
| | 437 | addr_buf.s_addr = htonl(INADDR_ANY); |
| | 438 | else |
| | 439 | addr_buf.s_addr = htonl(0x7F000001); |
| | 440 | addr_list_buf[0] = &addr_buf; |
| | 441 | addr_list_buf[1] = NULL; |
| | 442 | addr_list = addr_list_buf; |
| | 443 | } |
| | 444 | |
| | 445 | for (ap = addr_list; *ap != NULL; ap++) { |
| | 446 | new_res = (struct addrinfo *)malloc(sizeof(struct addrinfo)); |
| | 447 | if (new_res == NULL) { |
| | 448 | if (head_res != NULL) |
| | 449 | freeaddrinfo(head_res); |
| | 450 | result = EAI_MEMORY; |
| | 451 | goto end; |
| | 452 | } |
| | 453 | |
| | 454 | new_res->ai_family = PF_INET; |
| | 455 | new_res->ai_socktype = hints->ai_socktype; |
| | 456 | new_res->ai_protocol = hints->ai_protocol; |
| | 457 | new_res->ai_addr = NULL; |
| | 458 | new_res->ai_addrlen = sizeof(struct sockaddr_in); |
| | 459 | new_res->ai_canonname = NULL; |
| | 460 | new_res->ai_next = NULL; |
| | 461 | |
| | 462 | new_res->ai_addr = (struct sockaddr *) |
| | 463 | malloc(sizeof(struct sockaddr_in)); |
| | 464 | if (new_res->ai_addr == NULL) { |
| | 465 | free(new_res); |
| | 466 | if (head_res != NULL) |
| | 467 | freeaddrinfo(head_res); |
| | 468 | result = EAI_MEMORY; |
| | 469 | goto end; |
| | 470 | } |
| | 471 | |
| | 472 | sa_in = (struct sockaddr_in *)new_res->ai_addr; |
| | 473 | memset(sa_in, 0, sizeof(struct sockaddr_in)); |
| | 474 | sa_in->sin_family = PF_INET; |
| | 475 | sa_in->sin_port = port; |
| | 476 | memcpy(&sa_in->sin_addr, *ap, sizeof(struct in_addr)); |
| | 477 | |
| | 478 | if (head_res == NULL) |
| | 479 | head_res = new_res; |
| | 480 | else |
| | 481 | tail_res->ai_next = new_res; |
| | 482 | tail_res = new_res; |
| | 483 | } |
| | 484 | |
| | 485 | if (canonname != NULL && head_res != NULL) { |
| | 486 | head_res->ai_canonname = (char *)malloc(strlen(canonname) + 1); |
| | 487 | if (head_res->ai_canonname != NULL) |
| | 488 | strcpy(head_res->ai_canonname, canonname); |
| | 489 | } |
| | 490 | |
| | 491 | *res = head_res; |
| | 492 | |
| | 493 | end: |
| | 494 | h_errno = saved_h_errno; |
| | 495 | #ifdef ENABLE_PTHREAD |
| | 496 | pthread_mutex_unlock(&gai_mutex); |
| | 497 | #endif |
| | 498 | return result; |
| | 499 | } |
| | 500 | |
| | 501 | /* |
| | 502 | * getnameinfo(). |
| | 503 | */ |
| | 504 | int |
| | 505 | getnameinfo(sa, salen, node, nodelen, serv, servlen, flags) |
| | 506 | const struct sockaddr *sa; |
| | 507 | socklen_t salen; |
| | 508 | char *node; |
| | 509 | socklen_t nodelen; |
| | 510 | char *serv; |
| | 511 | socklen_t servlen; |
| | 512 | int flags; |
| | 513 | { |
| | 514 | const struct sockaddr_in *sa_in = (const struct sockaddr_in *)sa; |
| | 515 | struct hostent *hostent; |
| | 516 | struct servent *servent; |
| | 517 | char *ntoa_address; |
| | 518 | int saved_h_errno; |
| | 519 | int result = 0; |
| | 520 | |
| | 521 | #ifdef ENABLE_PTHREAD |
| | 522 | pthread_mutex_lock(&gai_mutex); |
| | 523 | #endif |
| | 524 | |
| | 525 | saved_h_errno = h_errno; |
| | 526 | |
| | 527 | if (sa_in->sin_family != PF_INET) { |
| | 528 | result = EAI_FAMILY; |
| | 529 | goto end; |
| | 530 | } else if (node == NULL && serv == NULL) { |
| | 531 | result = EAI_NONAME; |
| | 532 | goto end; |
| | 533 | } |
| | 534 | |
| | 535 | if (serv != NULL && servlen > 0) { |
| | 536 | if (flags & NI_NUMERICSERV) |
| | 537 | servent = NULL; |
| | 538 | else if (flags & NI_DGRAM) |
| | 539 | servent = getservbyport(sa_in->sin_port, "udp"); |
| | 540 | else |
| | 541 | servent = getservbyport(sa_in->sin_port, "tcp"); |
| | 542 | |
| | 543 | if (servent != NULL) { |
| | 544 | if (servlen <= strlen(servent->s_name)) { |
| | 545 | result = EAI_OVERFLOW; |
| | 546 | goto end; |
| | 547 | } |
| | 548 | strcpy(serv, servent->s_name); |
| | 549 | } else { |
| | 550 | if (servlen <= itoa_length(ntohs(sa_in->sin_port))) { |
| | 551 | result = EAI_OVERFLOW; |
| | 552 | goto end; |
| | 553 | } |
| | 554 | sprintf(serv, "%d", ntohs(sa_in->sin_port)); |
| | 555 | } |
| | 556 | } |
| | 557 | |
| | 558 | if (node != NULL && nodelen > 0) { |
| | 559 | if (flags & NI_NUMERICHOST) |
| | 560 | hostent = NULL; |
| | 561 | else { |
| | 562 | hostent = gethostbyaddr((char *)&sa_in->sin_addr, |
| | 563 | sizeof(struct in_addr), AF_INET); |
| | 564 | } |
| | 565 | if (hostent != NULL) { |
| | 566 | if (nodelen <= strlen(hostent->h_name)) { |
| | 567 | result = EAI_OVERFLOW; |
| | 568 | goto end; |
| | 569 | } |
| | 570 | strcpy(node, hostent->h_name); |
| | 571 | } else { |
| | 572 | if (flags & NI_NAMEREQD) { |
| | 573 | result = EAI_NONAME; |
| | 574 | goto end; |
| | 575 | } |
| | 576 | ntoa_address = inet_ntoa(sa_in->sin_addr); |
| | 577 | if (nodelen <= strlen(ntoa_address)) { |
| | 578 | result = EAI_OVERFLOW; |
| | 579 | goto end; |
| | 580 | } |
| | 581 | strcpy(node, ntoa_address); |
| | 582 | } |
| | 583 | |
| | 584 | } |
| | 585 | |
| | 586 | end: |
| | 587 | h_errno = saved_h_errno; |
| | 588 | #ifdef ENABLE_PTHREAD |
| | 589 | pthread_mutex_unlock(&gai_mutex); |
| | 590 | #endif |
| | 591 | return result; |
| | 592 | } |
| | 593 | |