38
38
#ifndef HAS_SOCKLEN_T
39
39
#define HAS_SOCKLEN_T 1
40
40
#endif
41
+ #ifndef HAS_GETADDRINFO
42
+ #define HAS_GETADDRINFO 1
43
+ #endif
44
+ #ifndef HAS_GETNAMEINFO
45
+ #define HAS_GETNAMEINFO 1
46
+ #endif
41
47
#endif
42
48
43
49
#ifdef HAS_FCNTL
@@ -98,6 +104,33 @@ enet_time_set (enet_uint32 newTimeBase)
98
104
int
99
105
enet_address_set_host (ENetAddress * address , const char * name )
100
106
{
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
101
134
struct hostent * hostEntry = NULL ;
102
135
#ifdef HAS_GETHOSTBYNAME_R
103
136
struct hostent hostData ;
@@ -113,19 +146,20 @@ enet_address_set_host (ENetAddress * address, const char * name)
113
146
hostEntry = gethostbyname (name );
114
147
#endif
115
148
116
- if (hostEntry == NULL ||
117
- hostEntry -> h_addrtype != AF_INET )
149
+ if (hostEntry != NULL && hostEntry -> h_addrtype == AF_INET )
118
150
{
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
+
125
153
return 0 ;
126
154
}
155
+ #endif
127
156
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 ;
129
163
130
164
return 0 ;
131
165
}
@@ -153,6 +187,27 @@ enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameL
153
187
int
154
188
enet_address_get_host (const ENetAddress * address , char * name , size_t nameLength )
155
189
{
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
156
211
struct in6_addr in ;
157
212
struct hostent * hostEntry = NULL ;
158
213
#ifdef HAS_GETHOSTBYADDR_R
@@ -173,17 +228,17 @@ enet_address_get_host (const ENetAddress * address, char * name, size_t nameLeng
173
228
hostEntry = gethostbyaddr ((char * ) & in , sizeof (struct in6_addr ), AF_INET6 );
174
229
#endif
175
230
176
- if (hostEntry == NULL )
177
- return enet_address_get_host_ip (address , name , nameLength );
178
- else
231
+ if (hostEntry != NULL )
179
232
{
180
233
size_t hostLen = strlen (hostEntry -> h_name );
181
234
if (hostLen >= nameLength )
182
235
return -1 ;
183
236
memcpy (name , hostEntry -> h_name , hostLen + 1 );
237
+ return 0 ;
184
238
}
239
+ #endif
185
240
186
- return 0 ;
241
+ return enet_address_get_host_ip ( address , name , nameLength ) ;
187
242
}
188
243
189
244
int
0 commit comments