@@ -61,49 +61,81 @@ enet_time_set (enet_uint32 newTimeBase)
61
61
int
62
62
enet_address_set_host (ENetAddress * address , const char * name )
63
63
{
64
- struct hostent * hostEntry ;
65
-
64
+ struct hostent * hostEntry = NULL ;
65
+ #ifdef HAS_GETHOSTBYNAME_R
66
+ struct hostent hostData ;
67
+ char buffer [2048 ];
68
+ int errnum ;
69
+
70
+ #if defined(linux ) || defined(__linux ) || defined(__linux__ ) || defined(__FreeBSD__ ) || defined(__FreeBSD_kernel__ )
71
+ gethostbyname_r (name , & hostData , buffer , sizeof (buffer ), & hostEntry , & errnum );
72
+ #else
73
+ hostEntry = gethostbyname_r (name , & hostData , buffer , sizeof (buffer ), & errnum );
74
+ #endif
75
+ #else
66
76
hostEntry = gethostbyname (name );
77
+ #endif
78
+
67
79
if (hostEntry == NULL ||
68
80
hostEntry -> h_addrtype != AF_INET )
69
81
{
70
- unsigned long host = inet_addr (name );
71
- if (host == INADDR_NONE )
82
+ #ifdef HAS_INET_PTON
83
+ if (! inet_pton (AF_INET6 , name , & address -> host ))
84
+ #else
85
+ if (! inet_aton (name , (struct in6_addr * ) & address -> host ))
86
+ #endif
72
87
return -1 ;
73
- address -> host = host ;
74
88
return 0 ;
75
89
}
76
90
77
- address -> host = * hostEntry -> h_addr_list [0 ];
91
+ // address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
78
92
79
93
return 0 ;
80
94
}
81
95
82
96
int
83
97
enet_address_get_host_ip (const ENetAddress * address , char * name , size_t nameLength )
84
98
{
99
+ #ifdef HAS_INET_NTOP
100
+ if (inet_ntop (AF_INET6 , & address -> host , name , nameLength ) == NULL )
101
+ #else
85
102
char * addr = inet_ntoa (* (struct in6_addr * ) & address -> host );
86
- if (addr == NULL )
87
- return -1 ;
88
- else
103
+ if (addr != NULL )
89
104
{
90
105
size_t addrLen = strlen (addr );
91
106
if (addrLen >= nameLength )
92
107
return -1 ;
93
108
memcpy (name , addr , addrLen + 1 );
94
- }
109
+ }
110
+ else
111
+ #endif
112
+ return -1 ;
95
113
return 0 ;
96
114
}
97
115
98
116
int
99
117
enet_address_get_host (const ENetAddress * address , char * name , size_t nameLength )
100
118
{
101
119
struct in6_addr in ;
102
- struct hostent * hostEntry ;
103
-
120
+ struct hostent * hostEntry = NULL ;
121
+ #ifdef HAS_GETHOSTBYADDR_R
122
+ struct hostent hostData ;
123
+ char buffer [2048 ];
124
+ int errnum ;
125
+
126
+ in = address -> host ;
127
+
128
+ #if defined(linux ) || defined(__linux ) || defined(__linux__ ) || defined(__FreeBSD__ ) || defined(__FreeBSD_kernel__ )
129
+ gethostbyaddr_r ((char * ) & in , sizeof (struct in6_addr ), AF_INET6 , & hostData , buffer , sizeof (buffer ), & hostEntry , & errnum );
130
+ #else
131
+ hostEntry = gethostbyaddr_r ((char * ) & in , sizeof (struct in6_addr ), AF_INET6 , & hostData , buffer , sizeof (buffer ), & errnum );
132
+ #endif
133
+ #else
104
134
in .s_addr = address -> host ;
105
-
135
+
106
136
hostEntry = gethostbyaddr ((char * ) & in , sizeof (struct in6_addr ), AF_INET6 );
137
+ #endif
138
+
107
139
if (hostEntry == NULL )
108
140
return enet_address_get_host_ip (address , name , nameLength );
109
141
else
0 commit comments