Skip to content

Commit 5f47654

Browse files
committed
use getaddrinfo and getnameinfo where available
1 parent f7c46f0 commit 5f47654

File tree

4 files changed

+78
-13
lines changed

4 files changed

+78
-13
lines changed

CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ include(CheckStructHasMember)
88
include(CheckTypeSize)
99
check_function_exists("fcntl" HAS_FCNTL)
1010
check_function_exists("poll" HAS_POLL)
11+
check_function_exists("getaddrinfo" HAS_GETADDRINFO)
12+
check_function_exists("getnameinfo" HAS_GETNAMEINFO)
1113
check_function_exists("gethostbyname_r" HAS_GETHOSTBYNAME_R)
1214
check_function_exists("gethostbyaddr_r" HAS_GETHOSTBYADDR_R)
1315
check_function_exists("inet_pton" HAS_INET_PTON)
@@ -23,6 +25,12 @@ endif()
2325
if(HAS_POLL)
2426
add_definitions(-DHAS_POLL=1)
2527
endif()
28+
if(HAS_GETNAMEINFO)
29+
add_definitions(-DHAS_GETNAMEINFO=1)
30+
endif()
31+
if(HAS_GETADDRINFO)
32+
add_definitions(-DHAS_GETADDRINFO=1)
33+
endif()
2634
if(HAS_GETHOSTBYNAME_R)
2735
add_definitions(-DHAS_GETHOSTBYNAME_R=1)
2836
endif()

ChangeLog

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* use getaddrinfo and getnameinfo where available
2+
13
ENet 1.3.13 (April 30, 2015):
24

35
* miscellaneous bug fixes

configure.ac

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ AC_CONFIG_MACRO_DIR([m4])
77
AC_PROG_CC
88
AC_PROG_LIBTOOL
99

10+
AC_CHECK_FUNC(getaddrinfo, [AC_DEFINE(HAS_GETADDRINFO)])
11+
AC_CHECK_FUNC(getnameinfo, [AC_DEFINE(HAS_GETNAMEINFO)])
1012
AC_CHECK_FUNC(gethostbyaddr_r, [AC_DEFINE(HAS_GETHOSTBYADDR_R)])
1113
AC_CHECK_FUNC(gethostbyname_r, [AC_DEFINE(HAS_GETHOSTBYNAME_R)])
1214
AC_CHECK_FUNC(poll, [AC_DEFINE(HAS_POLL)])

unix.c

+66-13
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
#ifndef HAS_SOCKLEN_T
3939
#define HAS_SOCKLEN_T 1
4040
#endif
41+
#ifndef HAS_GETADDRINFO
42+
#define HAS_GETADDRINFO 1
43+
#endif
44+
#ifndef HAS_GETNAMEINFO
45+
#define HAS_GETNAMEINFO 1
46+
#endif
4147
#endif
4248

4349
#ifdef HAS_FCNTL
@@ -98,6 +104,32 @@ enet_time_set (enet_uint32 newTimeBase)
98104
int
99105
enet_address_set_host (ENetAddress * address, const char * name)
100106
{
107+
#ifdef HAS_GETADDRINFO
108+
struct addrinfo hints, * resultList = NULL, * result = NULL;
109+
110+
memset (& hints, 0, sizeof (hints));
111+
hints.ai_family = AF_INET;
112+
113+
if (getaddrinfo (name, NULL, NULL, & resultList) != 0)
114+
return -1;
115+
116+
for (result = resultList; result != NULL; result = result -> ai_next)
117+
{
118+
if (result -> ai_family == AF_INET && result -> ai_addr != NULL && result -> ai_addrlen >= sizeof (struct sockaddr_in))
119+
{
120+
struct sockaddr_in * sin = (struct sockaddr_in *) result -> ai_addr;
121+
122+
address -> host = sin -> sin_addr.s_addr;
123+
124+
freeaddrinfo (resultList);
125+
126+
return 0;
127+
}
128+
}
129+
130+
if (resultList != NULL)
131+
freeaddrinfo (resultList);
132+
#else
101133
struct hostent * hostEntry = NULL;
102134
#ifdef HAS_GETHOSTBYNAME_R
103135
struct hostent hostData;
@@ -113,19 +145,20 @@ enet_address_set_host (ENetAddress * address, const char * name)
113145
hostEntry = gethostbyname (name);
114146
#endif
115147

116-
if (hostEntry == NULL ||
117-
hostEntry -> h_addrtype != AF_INET)
148+
if (hostEntry != NULL && hostEntry -> h_addrtype == AF_INET)
118149
{
119-
#ifdef HAS_INET_PTON
120-
if (! inet_pton (AF_INET, name, & address -> host))
121-
#else
122-
if (! inet_aton (name, (struct in_addr *) & address -> host))
123-
#endif
124-
return -1;
150+
address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
151+
125152
return 0;
126153
}
154+
#endif
127155

128-
address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
156+
#ifdef HAS_INET_PTON
157+
if (! inet_pton (AF_INET, name, & address -> host))
158+
#else
159+
if (! inet_aton (name, (struct in_addr *) & address -> host))
160+
#endif
161+
return -1;
129162

130163
return 0;
131164
}
@@ -153,6 +186,26 @@ enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameL
153186
int
154187
enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
155188
{
189+
#ifdef HAS_GETNAMEINFO
190+
struct sockaddr_in sin;
191+
int err;
192+
193+
memset (& sin, 0, sizeof (struct sockaddr_in));
194+
195+
sin.sin_family = AF_INET;
196+
sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
197+
sin.sin_addr.s_addr = address -> host;
198+
199+
err = getnameinfo ((struct sockaddr *) & sin, sizeof (sin), name, nameLength, NULL, 0, NI_NAMEREQD);
200+
if (! err)
201+
{
202+
if (name != NULL && nameLength > 0 && ! memchr (name, '\0', nameLength))
203+
return -1;
204+
return 0;
205+
}
206+
if (err != EAI_NONAME)
207+
return 0;
208+
#else
156209
struct in_addr in;
157210
struct hostent * hostEntry = NULL;
158211
#ifdef HAS_GETHOSTBYADDR_R
@@ -173,17 +226,17 @@ enet_address_get_host (const ENetAddress * address, char * name, size_t nameLeng
173226
hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET);
174227
#endif
175228

176-
if (hostEntry == NULL)
177-
return enet_address_get_host_ip (address, name, nameLength);
178-
else
229+
if (hostEntry != NULL)
179230
{
180231
size_t hostLen = strlen (hostEntry -> h_name);
181232
if (hostLen >= nameLength)
182233
return -1;
183234
memcpy (name, hostEntry -> h_name, hostLen + 1);
235+
return 0;
184236
}
237+
#endif
185238

186-
return 0;
239+
return enet_address_get_host_ip (address, name, nameLength);
187240
}
188241

189242
int

0 commit comments

Comments
 (0)