| [7378] | 1 | From e4638515a1ade5a23f3b8eb9ca7dad249a3d6903 Mon Sep 17 00:00:00 2001 |
|---|
| 2 | From: Adrian Friedli <adi@koalatux.ch> |
|---|
| 3 | Date: Thu, 2 Sep 2010 14:26:42 +0200 |
|---|
| [7390] | 4 | Subject: [PATCH 1/5] use getaddrinfo for lookup in unix.c |
|---|
| [7378] | 5 | |
|---|
| 6 | --- |
|---|
| 7 | unix.c | 99 +++++++++++++++++++++++++-------------------------------------- |
|---|
| 8 | 1 files changed, 39 insertions(+), 60 deletions(-) |
|---|
| 9 | |
|---|
| 10 | diff --git a/unix.c b/unix.c |
|---|
| 11 | index 6971541..7329e8d 100644 |
|---|
| 12 | --- a/unix.c |
|---|
| 13 | +++ b/unix.c |
|---|
| 14 | @@ -74,82 +74,61 @@ enet_time_set (enet_uint32 newTimeBase) |
|---|
| 15 | int |
|---|
| 16 | enet_address_set_host (ENetAddress * address, const char * name) |
|---|
| 17 | { |
|---|
| 18 | - struct hostent * hostEntry = NULL; |
|---|
| 19 | -#ifdef HAS_GETHOSTBYNAME_R |
|---|
| 20 | - struct hostent hostData; |
|---|
| 21 | - char buffer [2048]; |
|---|
| 22 | - int errnum; |
|---|
| 23 | - |
|---|
| 24 | -#if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
|---|
| 25 | - gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum); |
|---|
| 26 | -#else |
|---|
| 27 | - hostEntry = gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & errnum); |
|---|
| 28 | -#endif |
|---|
| 29 | -#else |
|---|
| 30 | - hostEntry = gethostbyname (name); |
|---|
| 31 | -#endif |
|---|
| 32 | + struct addrinfo hints; |
|---|
| 33 | + struct addrinfo * result; |
|---|
| 34 | + struct addrinfo * res; |
|---|
| 35 | + |
|---|
| 36 | + memset(& hints, 0, sizeof (hints)); |
|---|
| 37 | + hints.ai_flags = AI_NUMERICSERV; |
|---|
| 38 | + hints.ai_family = AF_INET; |
|---|
| 39 | |
|---|
| 40 | - if (hostEntry == NULL || |
|---|
| 41 | - hostEntry -> h_addrtype != AF_INET) |
|---|
| 42 | + if ( getaddrinfo(name, NULL, &hints, &result) ) |
|---|
| 43 | { |
|---|
| 44 | -#ifdef HAS_INET_PTON |
|---|
| 45 | - if (! inet_pton (AF_INET, name, & address -> host)) |
|---|
| 46 | -#else |
|---|
| 47 | - if (! inet_aton (name, (struct in_addr *) & address -> host)) |
|---|
| 48 | -#endif |
|---|
| 49 | - return -1; |
|---|
| 50 | - return 0; |
|---|
| 51 | + return -1; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | - address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0]; |
|---|
| 55 | + for (res = result; res != NULL; res = res -> ai_next) |
|---|
| 56 | + { |
|---|
| 57 | + if (res -> ai_family == AF_INET) |
|---|
| 58 | + { |
|---|
| 59 | + address -> host = ((struct sockaddr_in *) res -> ai_addr ) -> sin_addr.s_addr; |
|---|
| 60 | + break; |
|---|
| 61 | + } |
|---|
| 62 | + } |
|---|
| 63 | + freeaddrinfo(result); |
|---|
| 64 | + if (res == NULL) return -1; |
|---|
| 65 | |
|---|
| 66 | return 0; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | -int |
|---|
| 70 | -enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength) |
|---|
| 71 | +static int |
|---|
| 72 | +enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags) |
|---|
| 73 | { |
|---|
| 74 | -#ifdef HAS_INET_NTOP |
|---|
| 75 | - if (inet_ntop (AF_INET, & address -> host, name, nameLength) == NULL) |
|---|
| 76 | -#else |
|---|
| 77 | - char * addr = inet_ntoa (* (struct in_addr *) & address -> host); |
|---|
| 78 | - if (addr != NULL) |
|---|
| 79 | - strncpy (name, addr, nameLength); |
|---|
| 80 | - else |
|---|
| 81 | -#endif |
|---|
| 82 | + struct sockaddr_in sin; |
|---|
| 83 | + |
|---|
| 84 | + memset (& sin, 0, sizeof (struct sockaddr_in)); |
|---|
| 85 | + |
|---|
| 86 | + sin.sin_family = AF_INET; |
|---|
| 87 | + sin.sin_addr = * (struct in_addr *) & address -> host; |
|---|
| 88 | + |
|---|
| 89 | + if ( getnameinfo((struct sockaddr *) & sin, sizeof(sin), name, nameLength, NULL, 0, flags)) |
|---|
| 90 | + { |
|---|
| 91 | return -1; |
|---|
| 92 | + } |
|---|
| 93 | + |
|---|
| 94 | return 0; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | int |
|---|
| 98 | -enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength) |
|---|
| 99 | +enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength) |
|---|
| 100 | { |
|---|
| 101 | - struct in_addr in; |
|---|
| 102 | - struct hostent * hostEntry = NULL; |
|---|
| 103 | -#ifdef HAS_GETHOSTBYADDR_R |
|---|
| 104 | - struct hostent hostData; |
|---|
| 105 | - char buffer [2048]; |
|---|
| 106 | - int errnum; |
|---|
| 107 | - |
|---|
| 108 | - in.s_addr = address -> host; |
|---|
| 109 | - |
|---|
| 110 | -#if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
|---|
| 111 | - gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum); |
|---|
| 112 | -#else |
|---|
| 113 | - hostEntry = gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & errnum); |
|---|
| 114 | -#endif |
|---|
| 115 | -#else |
|---|
| 116 | - in.s_addr = address -> host; |
|---|
| 117 | - |
|---|
| 118 | - hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET); |
|---|
| 119 | -#endif |
|---|
| 120 | - |
|---|
| 121 | - if (hostEntry == NULL) |
|---|
| 122 | - return enet_address_get_host_ip (address, name, nameLength); |
|---|
| 123 | - |
|---|
| 124 | - strncpy (name, hostEntry -> h_name, nameLength); |
|---|
| 125 | + return enet_address_get_host_x(address, name, nameLength, NI_NUMERICHOST); |
|---|
| 126 | +} |
|---|
| 127 | |
|---|
| 128 | - return 0; |
|---|
| 129 | +int |
|---|
| 130 | +enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength) |
|---|
| 131 | +{ |
|---|
| 132 | + return enet_address_get_host_x(address, name, nameLength, 0); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | int |
|---|
| 136 | -- |
|---|
| 137 | 1.7.1 |
|---|
| 138 | |
|---|