Skip to content

Commit 3d27904

Browse files
committed
ipv6 support
1 parent 54d2681 commit 3d27904

File tree

4 files changed

+51
-49
lines changed

4 files changed

+51
-49
lines changed

host.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ enet_host_create (const ENetAddress * address, size_t peerCount, size_t channelL
8787
host -> commandCount = 0;
8888
host -> bufferCount = 0;
8989
host -> checksum = NULL;
90-
host -> receivedAddress.host = ENET_HOST_ANY;
90+
host -> receivedAddress.host = in6addr_any;
9191
host -> receivedAddress.port = 0;
9292
host -> receivedData = NULL;
9393
host -> receivedDataLength = 0;

include/enet/enet.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,12 @@ typedef enum _ENetSocketShutdown
8888
*/
8989
typedef struct _ENetAddress
9090
{
91-
enet_uint32 host;
91+
struct in6_addr host;
9292
enet_uint16 port;
9393
} ENetAddress;
9494

95+
#define in6_equal(in6_addr_a, in6_addr_b) (memcmp(&in6_addr_a, &in6_addr_b, sizeof(struct in6_addr)) == 0)
96+
9597
/**
9698
* Packet flag bit constants.
9799
*

protocol.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ enet_protocol_handle_connect (ENetHost * host, ENetProtocolHeader * header, ENet
298298
}
299299
else
300300
if (currentPeer -> state != ENET_PEER_STATE_CONNECTING &&
301-
currentPeer -> address.host == host -> receivedAddress.host)
301+
in6_equal(currentPeer -> address.host , host -> receivedAddress.host))
302302
{
303303
if (currentPeer -> address.port == host -> receivedAddress.port &&
304304
currentPeer -> connectID == command -> connect.connectID)
@@ -1028,9 +1028,9 @@ enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event)
10281028

10291029
if (peer -> state == ENET_PEER_STATE_DISCONNECTED ||
10301030
peer -> state == ENET_PEER_STATE_ZOMBIE ||
1031-
((host -> receivedAddress.host != peer -> address.host ||
1031+
((!in6_equal(host -> receivedAddress.host , peer -> address.host) ||
10321032
host -> receivedAddress.port != peer -> address.port) &&
1033-
peer -> address.host != ENET_HOST_BROADCAST) ||
1033+
1 /* no broadcast in ipv6 !in6_equal(peer -> address.host , ENET_HOST_BROADCAST)*/) ||
10341034
(peer -> outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID &&
10351035
sessionID != peer -> incomingSessionID))
10361036
return 0;

unix.c

+44-44
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,18 @@ enet_address_set_host (ENetAddress * address, const char * name)
114114
#endif
115115

116116
if (hostEntry == NULL ||
117-
hostEntry -> h_addrtype != AF_INET)
117+
hostEntry -> h_addrtype != AF_INET6)
118118
{
119119
#ifdef HAS_INET_PTON
120-
if (! inet_pton (AF_INET, name, & address -> host))
120+
if (! inet_pton (AF_INET6, name, & address -> host))
121121
#else
122-
if (! inet_aton (name, (struct in_addr *) & address -> host))
122+
if (! inet_aton (name, (struct in6_addr *) & address -> host))
123123
#endif
124124
return -1;
125125
return 0;
126126
}
127127

128-
address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
128+
// address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
129129

130130
return 0;
131131
}
@@ -134,9 +134,9 @@ int
134134
enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
135135
{
136136
#ifdef HAS_INET_NTOP
137-
if (inet_ntop (AF_INET, & address -> host, name, nameLength) == NULL)
137+
if (inet_ntop (AF_INET6, & address -> host, name, nameLength) == NULL)
138138
#else
139-
char * addr = inet_ntoa (* (struct in_addr *) & address -> host);
139+
char * addr = inet_ntoa (* (struct in6_addr *) & address -> host);
140140
if (addr != NULL)
141141
{
142142
size_t addrLen = strlen(addr);
@@ -153,24 +153,24 @@ enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameL
153153
int
154154
enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
155155
{
156-
struct in_addr in;
156+
struct in6_addr in;
157157
struct hostent * hostEntry = NULL;
158158
#ifdef HAS_GETHOSTBYADDR_R
159159
struct hostent hostData;
160160
char buffer [2048];
161161
int errnum;
162162

163-
in.s_addr = address -> host;
163+
in = address -> host;
164164

165165
#if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
166-
gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
166+
gethostbyaddr_r ((char *) & in, sizeof (struct in6_addr), AF_INET6, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
167167
#else
168-
hostEntry = gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & errnum);
168+
hostEntry = gethostbyaddr_r ((char *) & in, sizeof (struct in6_addr), AF_INET6, & hostData, buffer, sizeof (buffer), & errnum);
169169
#endif
170170
#else
171171
in.s_addr = address -> host;
172172

173-
hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET);
173+
hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in6_addr), AF_INET6);
174174
#endif
175175

176176
if (hostEntry == NULL)
@@ -189,39 +189,39 @@ enet_address_get_host (const ENetAddress * address, char * name, size_t nameLeng
189189
int
190190
enet_socket_bind (ENetSocket socket, const ENetAddress * address)
191191
{
192-
struct sockaddr_in sin;
192+
struct sockaddr_in6 sin;
193193

194-
memset (& sin, 0, sizeof (struct sockaddr_in));
194+
memset (& sin, 0, sizeof (struct sockaddr_in6));
195195

196-
sin.sin_family = AF_INET;
196+
sin.sin6_family = AF_INET6;
197197

198198
if (address != NULL)
199199
{
200-
sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
201-
sin.sin_addr.s_addr = address -> host;
200+
sin.sin6_port = ENET_HOST_TO_NET_16 (address -> port);
201+
sin.sin6_addr = address -> host;
202202
}
203203
else
204204
{
205-
sin.sin_port = 0;
206-
sin.sin_addr.s_addr = INADDR_ANY;
205+
sin.sin6_port = 0;
206+
sin.sin6_addr = in6addr_any;
207207
}
208208

209209
return bind (socket,
210210
(struct sockaddr *) & sin,
211-
sizeof (struct sockaddr_in));
211+
sizeof (struct sockaddr_in6));
212212
}
213213

214214
int
215215
enet_socket_get_address (ENetSocket socket, ENetAddress * address)
216216
{
217-
struct sockaddr_in sin;
218-
socklen_t sinLength = sizeof (struct sockaddr_in);
217+
struct sockaddr_in6 sin;
218+
socklen_t sinLength = sizeof (struct sockaddr_in6);
219219

220220
if (getsockname (socket, (struct sockaddr *) & sin, & sinLength) == -1)
221221
return -1;
222222

223-
address -> host = (enet_uint32) sin.sin_addr.s_addr;
224-
address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
223+
address -> host = sin.sin6_addr;
224+
address -> port = ENET_NET_TO_HOST_16 (sin.sin6_port);
225225

226226
return 0;
227227
}
@@ -235,7 +235,7 @@ enet_socket_listen (ENetSocket socket, int backlog)
235235
ENetSocket
236236
enet_socket_create (ENetSocketType type)
237237
{
238-
return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
238+
return socket (PF_INET6, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
239239
}
240240

241241
int
@@ -317,16 +317,16 @@ enet_socket_get_option (ENetSocket socket, ENetSocketOption option, int * value)
317317
int
318318
enet_socket_connect (ENetSocket socket, const ENetAddress * address)
319319
{
320-
struct sockaddr_in sin;
320+
struct sockaddr_in6 sin;
321321
int result;
322322

323-
memset (& sin, 0, sizeof (struct sockaddr_in));
323+
memset (& sin, 0, sizeof (struct sockaddr_in6));
324324

325-
sin.sin_family = AF_INET;
326-
sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
327-
sin.sin_addr.s_addr = address -> host;
325+
sin.sin6_family = AF_INET6;
326+
sin.sin6_port = ENET_HOST_TO_NET_16 (address -> port);
327+
sin.sin6_addr = address -> host;
328328

329-
result = connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in));
329+
result = connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in6));
330330
if (result == -1 && errno == EINPROGRESS)
331331
return 0;
332332

@@ -337,8 +337,8 @@ ENetSocket
337337
enet_socket_accept (ENetSocket socket, ENetAddress * address)
338338
{
339339
int result;
340-
struct sockaddr_in sin;
341-
socklen_t sinLength = sizeof (struct sockaddr_in);
340+
struct sockaddr_in6 sin;
341+
socklen_t sinLength = sizeof (struct sockaddr_in6);
342342

343343
result = accept (socket,
344344
address != NULL ? (struct sockaddr *) & sin : NULL,
@@ -349,8 +349,8 @@ enet_socket_accept (ENetSocket socket, ENetAddress * address)
349349

350350
if (address != NULL)
351351
{
352-
address -> host = (enet_uint32) sin.sin_addr.s_addr;
353-
address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
352+
address -> host = sin.sin6_addr;
353+
address -> port = ENET_NET_TO_HOST_16 (sin.sin6_port);
354354
}
355355

356356
return result;
@@ -376,21 +376,21 @@ enet_socket_send (ENetSocket socket,
376376
size_t bufferCount)
377377
{
378378
struct msghdr msgHdr;
379-
struct sockaddr_in sin;
379+
struct sockaddr_in6 sin;
380380
int sentLength;
381381

382382
memset (& msgHdr, 0, sizeof (struct msghdr));
383383

384384
if (address != NULL)
385385
{
386-
memset (& sin, 0, sizeof (struct sockaddr_in));
386+
memset (& sin, 0, sizeof (struct sockaddr_in6));
387387

388-
sin.sin_family = AF_INET;
389-
sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
390-
sin.sin_addr.s_addr = address -> host;
388+
sin.sin6_family = AF_INET6;
389+
sin.sin6_port = ENET_HOST_TO_NET_16 (address -> port);
390+
sin.sin6_addr = address -> host;
391391

392392
msgHdr.msg_name = & sin;
393-
msgHdr.msg_namelen = sizeof (struct sockaddr_in);
393+
msgHdr.msg_namelen = sizeof (struct sockaddr_in6);
394394
}
395395

396396
msgHdr.msg_iov = (struct iovec *) buffers;
@@ -416,15 +416,15 @@ enet_socket_receive (ENetSocket socket,
416416
size_t bufferCount)
417417
{
418418
struct msghdr msgHdr;
419-
struct sockaddr_in sin;
419+
struct sockaddr_in6 sin;
420420
int recvLength;
421421

422422
memset (& msgHdr, 0, sizeof (struct msghdr));
423423

424424
if (address != NULL)
425425
{
426426
msgHdr.msg_name = & sin;
427-
msgHdr.msg_namelen = sizeof (struct sockaddr_in);
427+
msgHdr.msg_namelen = sizeof (struct sockaddr_in6);
428428
}
429429

430430
msgHdr.msg_iov = (struct iovec *) buffers;
@@ -447,8 +447,8 @@ enet_socket_receive (ENetSocket socket,
447447

448448
if (address != NULL)
449449
{
450-
address -> host = (enet_uint32) sin.sin_addr.s_addr;
451-
address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
450+
address -> host = sin.sin6_addr;
451+
address -> port = ENET_NET_TO_HOST_16 (sin.sin6_port);
452452
}
453453

454454
return recvLength;

0 commit comments

Comments
 (0)