Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/SuperOrxoBros_HS18/src/external/enet/unix.c @ 12177

Last change on this file since 12177 was 12177, checked in by siramesh, 5 years ago

Super Orxo Bros Final (Sidharth Ramesh, Nisa Balta, Jeff Ren)

File size: 12.2 KB
Line 
1/**
2 @file  unix.c
3 @brief ENet Unix system specific functions
4*/
5#ifndef WIN32
6
7#include <sys/types.h>
8#include <sys/socket.h>
9#include <sys/ioctl.h>
10#include <sys/time.h>
11#include <arpa/inet.h>
12#include <netdb.h>
13#include <unistd.h>
14#include <string.h>
15#include <errno.h>
16#include <time.h>
17
18#define ENET_BUILDING_LIB 1
19#include "enet/enet.h"
20
21#ifdef HAS_FCNTL
22#include <fcntl.h>
23#endif
24
25#ifdef __APPLE__
26#undef HAS_POLL
27#endif
28
29#ifdef HAS_POLL
30#include <sys/poll.h>
31#endif
32
33#ifndef HAS_SOCKLEN_T
34typedef int socklen_t;
35#endif
36
37#ifndef MSG_NOSIGNAL
38#define MSG_NOSIGNAL 0
39#endif
40
41static enet_uint32 timeBase = 0;
42
43int
44enet_initialize (void)
45{
46    return 0;
47}
48
49void
50enet_deinitialize (void)
51{
52}
53
54enet_uint32
55enet_time_get (void)
56{
57    struct timeval timeVal;
58
59    gettimeofday (& timeVal, NULL);
60
61    return timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - timeBase;
62}
63
64void
65enet_time_set (enet_uint32 newTimeBase)
66{
67    struct timeval timeVal;
68
69    gettimeofday (& timeVal, NULL);
70   
71    timeBase = timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - newTimeBase;
72}
73
74static enet_uint16
75enet_af (ENetAddressFamily family)
76{
77    if (family == ENET_IPV4)
78        return AF_INET;
79    if (family == ENET_IPV6)
80        return AF_INET6;
81    return 0;
82}
83
84static socklen_t
85enet_sa_size (ENetAddressFamily family)
86{
87    if (family == ENET_IPV4)
88        return sizeof (struct sockaddr_in);
89    if (family == ENET_IPV6)
90        return sizeof (struct sockaddr_in6);
91    return 0;
92}
93
94static ENetAddressFamily
95enet_address_set_address (ENetAddress * address, const struct sockaddr * sin)
96{
97    memset (address, 0, sizeof (ENetAddress));
98    if (sin -> sa_family == AF_INET)
99    {
100        address -> host = enet_address_map4 ((((struct sockaddr_in *) sin) -> sin_addr.s_addr));
101        /* address -> scopeID = 0; */
102        address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in *) sin) -> sin_port);
103        return ENET_IPV4;
104    }
105    if (sin -> sa_family == AF_INET6)
106    {
107        address -> host = * (ENetHostAddress *) & ((struct sockaddr_in6 *) sin) -> sin6_addr;
108        address -> scopeID = ((struct sockaddr_in6 *) sin) -> sin6_scope_id;
109        address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in6 *) sin) -> sin6_port);
110        return ENET_IPV6;
111    }
112    return ENET_NO_ADDRESS_FAMILY;
113}
114
115static int
116enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, ENetAddressFamily family)
117{
118    memset (sin, 0, enet_sa_size(family));
119    if (family == ENET_IPV4 &&
120      (enet_get_address_family (address) == ENET_IPV4 ||
121      !memcmp (& address -> host, & ENET_HOST_ANY, sizeof(ENetHostAddress))))
122    {
123        ((struct sockaddr_in *) sin) -> sin_family = AF_INET;
124        ((struct sockaddr_in *) sin) -> sin_addr = * (struct in_addr *) & address -> host.addr[12];
125        ((struct sockaddr_in *) sin) -> sin_port = ENET_HOST_TO_NET_16 (address -> port);
126        return 0;
127    }
128    else if (family == ENET_IPV6)
129    {
130        ((struct sockaddr_in6 *) sin) -> sin6_family = AF_INET6;
131        ((struct sockaddr_in6 *) sin) -> sin6_addr = * (struct in6_addr *) & address -> host;
132        ((struct sockaddr_in6 *) sin) -> sin6_scope_id = address -> scopeID;
133        ((struct sockaddr_in6 *) sin) -> sin6_port = ENET_HOST_TO_NET_16 (address -> port);
134        return 0;
135    }
136    return -1;
137}
138
139int
140enet_address_set_host (ENetAddress * address, const char * name)
141{
142    enet_uint16 port = address -> port;
143    struct addrinfo hints;
144    struct addrinfo * result;
145    struct addrinfo * res;
146
147    memset(& hints, 0, sizeof (hints));
148    hints.ai_flags = AI_ADDRCONFIG;
149    hints.ai_family = AF_UNSPEC;
150
151    if ( getaddrinfo(name, NULL, &hints, &result) )
152        return -1;
153
154    for (res = result; res != NULL; res = res -> ai_next)
155    {
156        if ( enet_address_set_address(address, res -> ai_addr) != ENET_NO_ADDRESS_FAMILY )
157            break;
158    }
159
160    address -> port = port;
161    freeaddrinfo(result);
162    if (res == NULL) return -1;
163
164    return 0;
165}
166
167static int
168enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags)
169{
170    struct sockaddr_storage sin;
171    enet_address_set_sin((struct sockaddr *) & sin, address, ENET_IPV6);
172
173    if ( getnameinfo((struct sockaddr *) & sin, enet_sa_size (ENET_IPV6), name, nameLength, NULL, 0, flags))
174        return -1;
175
176    return 0;
177}
178
179int
180enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
181{
182    return enet_address_get_host_x(address, name, nameLength, NI_NUMERICHOST);
183}
184
185int
186enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
187{
188    return enet_address_get_host_x(address, name, nameLength, 0);
189}
190
191int
192enet_socket_bind (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family)
193{
194    struct sockaddr_storage sin;
195
196    if (address != NULL)
197    {
198        enet_address_set_sin((struct sockaddr *) & sin, address, family);
199    }
200    else
201    {
202        ENetAddress address_ = { ENET_HOST_ANY_INIT, 0, 0 };
203        enet_address_set_sin((struct sockaddr *) & sin, & address_, family);
204    }
205
206    return bind (socket, (struct sockaddr *) & sin, enet_sa_size(family));
207}
208
209int
210enet_socket_listen (ENetSocket socket, int backlog)
211{
212    return listen (socket, backlog < 0 ? SOMAXCONN : backlog);
213}
214
215ENetSocket
216enet_socket_create (ENetSocketType type, ENetAddressFamily family)
217{
218    ENetSocket sock = socket (enet_af (family), type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
219
220#ifdef IPV6_V6ONLY
221    if (family == ENET_IPV6)
222    {
223        int value = 1;
224        setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, & value, sizeof (int));
225    }
226#endif /* IPV6_V6ONLY */
227
228    return sock;
229}
230
231int
232enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value)
233{
234    int result = -1;
235    switch (option)
236    {
237        case ENET_SOCKOPT_NONBLOCK:
238#ifdef HAS_FCNTL
239            result = fcntl (socket, F_SETFL, O_NONBLOCK | fcntl (socket, F_GETFL));
240#else
241            result = ioctl (socket, FIONBIO, & value);
242#endif
243            break;
244
245        case ENET_SOCKOPT_BROADCAST:
246            result = setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) & value, sizeof (int));
247            break;
248
249        case ENET_SOCKOPT_REUSEADDR:
250            result = setsockopt (socket, SOL_SOCKET, SO_REUSEADDR, (char *) & value, sizeof (int));
251            break;
252
253        case ENET_SOCKOPT_RCVBUF:
254            result = setsockopt (socket, SOL_SOCKET, SO_RCVBUF, (char *) & value, sizeof (int));
255            break;
256
257        case ENET_SOCKOPT_SNDBUF:
258            result = setsockopt (socket, SOL_SOCKET, SO_SNDBUF, (char *) & value, sizeof (int));
259            break;
260
261        default:
262            break;
263    }
264    return result == -1 ? -1 : 0;
265}
266
267int
268enet_socket_connect (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family)
269{
270    struct sockaddr_storage sin;
271    enet_address_set_sin((struct sockaddr *) & sin, address, family);
272
273    return connect (socket, (struct sockaddr *) & sin, enet_sa_size (family));
274}
275
276ENetSocket
277enet_socket_accept (ENetSocket socket, ENetAddress * address, ENetAddressFamily family)
278{
279    int result;
280    struct sockaddr_storage sin;
281    socklen_t sinLength = enet_sa_size (family);
282
283    result = accept (socket, 
284                     address != NULL ? (struct sockaddr *) & sin : NULL,
285                     address != NULL ? & sinLength : NULL);
286
287    if (result == -1)
288      return ENET_SOCKET_NULL;
289
290    if (address != NULL)
291    {
292        enet_address_set_address(address, (struct sockaddr *) & sin);
293    }
294
295    return result;
296}
297
298void
299enet_socket_destroy (ENetSocket socket)
300{
301    close (socket);
302}
303
304int
305enet_socket_send (ENetSocket socket,
306                  const ENetAddress * address,
307                  const ENetBuffer * buffers,
308                  size_t bufferCount,
309                  ENetAddressFamily family)
310{
311    struct msghdr msgHdr;
312    struct sockaddr_storage sin;
313    int sentLength;
314
315    memset (& msgHdr, 0, sizeof (struct msghdr));
316
317    if (address != NULL)
318    {
319        enet_address_set_sin((struct sockaddr *) & sin, address, family);
320        msgHdr.msg_name = & sin;
321        msgHdr.msg_namelen = enet_sa_size (family);
322    }
323
324    msgHdr.msg_iov = (struct iovec *) buffers;
325    msgHdr.msg_iovlen = bufferCount;
326
327    sentLength = sendmsg (socket, & msgHdr, MSG_NOSIGNAL);
328   
329    if (sentLength == -1)
330    {
331       if (errno == EWOULDBLOCK)
332         return 0;
333
334       return -1;
335    }
336
337    return sentLength;
338}
339
340int
341enet_socket_receive (ENetSocket socket,
342                     ENetAddress * address,
343                     ENetBuffer * buffers,
344                     size_t bufferCount,
345                     ENetAddressFamily family)
346{
347    struct msghdr msgHdr;
348    struct sockaddr_storage sin;
349    int recvLength;
350
351    memset (& msgHdr, 0, sizeof (struct msghdr));
352
353    if (address != NULL)
354    {
355        msgHdr.msg_name = & sin;
356        msgHdr.msg_namelen = enet_sa_size (family);
357    }
358
359    msgHdr.msg_iov = (struct iovec *) buffers;
360    msgHdr.msg_iovlen = bufferCount;
361
362    recvLength = recvmsg (socket, & msgHdr, MSG_NOSIGNAL);
363
364    if (recvLength == -1)
365    {
366       if (errno == EWOULDBLOCK)
367         return 0;
368
369       return -1;
370    }
371
372#ifdef HAS_MSGHDR_FLAGS
373    if (msgHdr.msg_flags & MSG_TRUNC)
374      return -1;
375#endif
376
377    if (address != NULL)
378    {
379        enet_address_set_address(address, (struct sockaddr *) & sin);
380    }
381
382    return recvLength;
383}
384
385int
386enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocketSet * writeSet, enet_uint32 timeout)
387{
388    struct timeval timeVal;
389
390    timeVal.tv_sec = timeout / 1000;
391    timeVal.tv_usec = (timeout % 1000) * 1000;
392
393    return select (maxSocket + 1, readSet, writeSet, NULL, & timeVal);
394}
395
396int
397enet_socket_wait (ENetSocket socket4, ENetSocket socket6, enet_uint32 * condition, enet_uint32 timeout)
398{
399#ifdef HAS_POLL
400    struct pollfd pollSocket[2];
401    int pollCount;
402
403    pollSocket[0].fd = socket4;
404    pollSocket[1].fd = socket6;
405    pollSocket[0].events = 0;
406    pollSocket[1].events = 0;
407    /* pollSocket[0].revents = 0; */
408    pollSocket[1].revents = 0;
409
410    if (pollSocket[0].fd == ENET_SOCKET_NULL)
411    {
412        pollSocket[0].fd = pollSocket[1].fd;
413        pollSocket[1].fd = ENET_SOCKET_NULL;
414    }
415
416    if (* condition & ENET_SOCKET_WAIT_SEND)
417    {
418        pollSocket[0].events |= POLLOUT;
419        pollSocket[1].events |= POLLOUT;
420    }
421
422    if (* condition & ENET_SOCKET_WAIT_RECEIVE)
423    {
424        pollSocket[0].events |= POLLIN;
425        pollSocket[1].events |= POLLIN;
426    }
427
428    pollCount = poll (pollSocket, pollSocket[1].fd != ENET_SOCKET_NULL ? 2 : 1, timeout);
429
430    if (pollCount < 0)
431      return -1;
432
433    * condition = ENET_SOCKET_WAIT_NONE;
434
435    if (pollCount == 0)
436      return 0;
437
438    if ((pollSocket[0].revents | pollSocket[1].revents) & POLLOUT)
439      * condition |= ENET_SOCKET_WAIT_SEND;
440   
441    if ((pollSocket[0].revents | pollSocket[1].revents) & POLLIN)
442      * condition |= ENET_SOCKET_WAIT_RECEIVE;
443
444    return 0;
445#else
446    fd_set readSet, writeSet;
447    struct timeval timeVal;
448    int selectCount;
449    ENetSocket maxSocket;
450
451    timeVal.tv_sec = timeout / 1000;
452    timeVal.tv_usec = (timeout % 1000) * 1000;
453
454    FD_ZERO (& readSet);
455    FD_ZERO (& writeSet);
456
457    if (* condition & ENET_SOCKET_WAIT_SEND)
458    {
459        if (socket4 != ENET_SOCKET_NULL)
460            FD_SET (socket4, & writeSet);
461        if (socket6 != ENET_SOCKET_NULL)
462            FD_SET (socket6, & writeSet);
463    }
464
465    if (* condition & ENET_SOCKET_WAIT_RECEIVE)
466    {
467        if (socket4 != ENET_SOCKET_NULL)
468            FD_SET (socket4, & readSet);
469        if (socket6 != ENET_SOCKET_NULL)
470            FD_SET (socket6, & readSet);
471    }
472
473    maxSocket = 0;
474    if (socket4 != ENET_SOCKET_NULL)
475        maxSocket = socket4;
476    if (socket6 != ENET_SOCKET_NULL && socket6 > maxSocket)
477        maxSocket = socket6;
478
479    selectCount = select (maxSocket + 1, & readSet, & writeSet, NULL, & timeVal);
480
481    if (selectCount < 0)
482      return -1;
483
484    * condition = ENET_SOCKET_WAIT_NONE;
485
486    if (selectCount == 0)
487      return 0;
488
489    if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & writeSet)) ||
490        (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & writeSet)) )
491        * condition |= ENET_SOCKET_WAIT_SEND;
492
493    if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & readSet)) ||
494        (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & readSet)) )
495        * condition |= ENET_SOCKET_WAIT_RECEIVE;
496
497    return 0;
498#endif
499}
500
501#endif
502
Note: See TracBrowser for help on using the repository browser.