Skip to content

Commit a2651c6

Browse files
committed
Merge branch 'master' into ipv6
Conflicts: unix.c
2 parents 2c1bac4 + 62755b1 commit a2651c6

File tree

4 files changed

+80
-13
lines changed

4 files changed

+80
-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

+68-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,33 @@ 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_UNSPEC;
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+
//todo: split 4 with ::ffff: and v6
119+
if (/*result -> ai_family == AF_INET && */ result -> ai_addr != NULL && result -> ai_addrlen >= sizeof (struct sockaddr_in))
120+
{
121+
struct sockaddr_in6 * sin = (struct sockaddr_in6 *) result -> ai_addr;
122+
123+
address -> host = sin -> sin6_addr;
124+
125+
freeaddrinfo (resultList);
126+
127+
return 0;
128+
}
129+
}
130+
131+
if (resultList != NULL)
132+
freeaddrinfo (resultList);
133+
#else
101134
struct hostent * hostEntry = NULL;
102135
#ifdef HAS_GETHOSTBYNAME_R
103136
struct hostent hostData;
@@ -113,19 +146,20 @@ enet_address_set_host (ENetAddress * address, const char * name)
113146
hostEntry = gethostbyname (name);
114147
#endif
115148

116-
if (hostEntry == NULL ||
117-
hostEntry -> h_addrtype != AF_INET)
149+
if (hostEntry != NULL && hostEntry -> h_addrtype == AF_INET)
118150
{
119-
#ifdef HAS_INET_PTON
120-
if (! inet_pton (AF_INET6, name, & address -> host))
121-
#else
122-
if (! inet_aton (name, (struct in_addr *) & address -> host))
123-
#endif
124-
return -1;
151+
address -> host = * hostEntry -> h_addr_list [0];
152+
125153
return 0;
126154
}
155+
#endif
127156

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

130164
return 0;
131165
}
@@ -153,6 +187,27 @@ enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameL
153187
int
154188
enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
155189
{
190+
191+
#ifdef HAS_GETNAMEINFO
192+
struct sockaddr_in6 sin;
193+
int err;
194+
195+
memset (& sin, 0, sizeof (struct sockaddr_in));
196+
197+
sin.sin6_family = AF_INET6;
198+
sin.sin6_port = ENET_HOST_TO_NET_16 (address -> port);
199+
sin.sin6_addr = address -> host;
200+
201+
err = getnameinfo ((struct sockaddr *) & sin, sizeof (sin), name, nameLength, NULL, 0, NI_NAMEREQD);
202+
if (! err)
203+
{
204+
if (name != NULL && nameLength > 0 && ! memchr (name, '\0', nameLength))
205+
return -1;
206+
return 0;
207+
}
208+
if (err != EAI_NONAME)
209+
return 0;
210+
#else
156211
struct in6_addr in;
157212
struct hostent * hostEntry = NULL;
158213
#ifdef HAS_GETHOSTBYADDR_R
@@ -173,17 +228,17 @@ enet_address_get_host (const ENetAddress * address, char * name, size_t nameLeng
173228
hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in6_addr), AF_INET6);
174229
#endif
175230

176-
if (hostEntry == NULL)
177-
return enet_address_get_host_ip (address, name, nameLength);
178-
else
231+
if (hostEntry != NULL)
179232
{
180233
size_t hostLen = strlen (hostEntry -> h_name);
181234
if (hostLen >= nameLength)
182235
return -1;
183236
memcpy (name, hostEntry -> h_name, hostLen + 1);
237+
return 0;
184238
}
239+
#endif
185240

186-
return 0;
241+
return enet_address_get_host_ip (address, name, nameLength);
187242
}
188243

189244
int

0 commit comments

Comments
 (0)