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,32 @@ 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_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
101
133
struct hostent * hostEntry = NULL ;
102
134
#ifdef HAS_GETHOSTBYNAME_R
103
135
struct hostent hostData ;
@@ -113,19 +145,20 @@ enet_address_set_host (ENetAddress * address, const char * name)
113
145
hostEntry = gethostbyname (name );
114
146
#endif
115
147
116
- if (hostEntry == NULL ||
117
- hostEntry -> h_addrtype != AF_INET )
148
+ if (hostEntry != NULL && hostEntry -> h_addrtype == AF_INET )
118
149
{
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
+
125
152
return 0 ;
126
153
}
154
+ #endif
127
155
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 ;
129
162
130
163
return 0 ;
131
164
}
@@ -153,6 +186,26 @@ enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameL
153
186
int
154
187
enet_address_get_host (const ENetAddress * address , char * name , size_t nameLength )
155
188
{
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
156
209
struct in_addr in ;
157
210
struct hostent * hostEntry = NULL ;
158
211
#ifdef HAS_GETHOSTBYADDR_R
@@ -173,17 +226,17 @@ enet_address_get_host (const ENetAddress * address, char * name, size_t nameLeng
173
226
hostEntry = gethostbyaddr ((char * ) & in , sizeof (struct in_addr ), AF_INET );
174
227
#endif
175
228
176
- if (hostEntry == NULL )
177
- return enet_address_get_host_ip (address , name , nameLength );
178
- else
229
+ if (hostEntry != NULL )
179
230
{
180
231
size_t hostLen = strlen (hostEntry -> h_name );
181
232
if (hostLen >= nameLength )
182
233
return -1 ;
183
234
memcpy (name , hostEntry -> h_name , hostLen + 1 );
235
+ return 0 ;
184
236
}
237
+ #endif
185
238
186
- return 0 ;
239
+ return enet_address_get_host_ip ( address , name , nameLength ) ;
187
240
}
188
241
189
242
int
0 commit comments