2008-09-07 11:04:43 -04:00
|
|
|
/**
|
|
|
|
@file unix.c
|
|
|
|
@brief ENet Unix system specific functions
|
|
|
|
*/
|
2018-01-30 17:10:30 -05:00
|
|
|
#ifndef _WIN32
|
2013-01-16 17:18:07 -05:00
|
|
|
|
2008-09-07 11:04:43 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/time.h>
|
2018-01-30 17:10:30 -05:00
|
|
|
#include <netinet/tcp.h>
|
2008-09-07 11:04:43 -04:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#define ENET_BUILDING_LIB 1
|
|
|
|
#include "enet/enet.h"
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
2018-01-30 17:10:30 -05:00
|
|
|
#ifdef HAS_POLL
|
2008-09-07 11:04:43 -04:00
|
|
|
#undef HAS_POLL
|
|
|
|
#endif
|
2018-01-30 17:10:30 -05:00
|
|
|
#ifndef HAS_FCNTL
|
|
|
|
#define HAS_FCNTL 1
|
|
|
|
#endif
|
|
|
|
#ifndef HAS_INET_PTON
|
|
|
|
#define HAS_INET_PTON 1
|
|
|
|
#endif
|
|
|
|
#ifndef HAS_INET_NTOP
|
|
|
|
#define HAS_INET_NTOP 1
|
|
|
|
#endif
|
|
|
|
#ifndef HAS_MSGHDR_FLAGS
|
|
|
|
#define HAS_MSGHDR_FLAGS 1
|
|
|
|
#endif
|
|
|
|
#ifndef HAS_SOCKLEN_T
|
|
|
|
#define HAS_SOCKLEN_T 1
|
|
|
|
#endif
|
2020-02-27 11:55:10 -05:00
|
|
|
#ifndef HAS_GETADDRINFO
|
|
|
|
#define HAS_GETADDRINFO 1
|
|
|
|
#endif
|
|
|
|
#ifndef HAS_GETNAMEINFO
|
|
|
|
#define HAS_GETNAMEINFO 1
|
|
|
|
#endif
|
2018-01-30 17:10:30 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAS_FCNTL
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
2008-09-07 11:04:43 -04:00
|
|
|
|
|
|
|
#ifdef HAS_POLL
|
2020-02-27 11:55:10 -05:00
|
|
|
#include <poll.h>
|
2008-09-07 11:04:43 -04:00
|
|
|
#endif
|
|
|
|
|
2018-01-30 17:10:30 -05:00
|
|
|
#ifndef HAS_SOCKLEN_T
|
2008-09-07 11:04:43 -04:00
|
|
|
typedef int socklen_t;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MSG_NOSIGNAL
|
|
|
|
#define MSG_NOSIGNAL 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static enet_uint32 timeBase = 0;
|
|
|
|
|
2020-02-27 11:59:37 -05:00
|
|
|
// Global variable handled by STK
|
|
|
|
extern int isIPv6Socket(void);
|
|
|
|
|
2008-09-07 11:04:43 -04:00
|
|
|
int
|
|
|
|
enet_initialize (void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
enet_deinitialize (void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-01-30 17:10:30 -05:00
|
|
|
enet_uint32
|
|
|
|
enet_host_random_seed (void)
|
|
|
|
{
|
|
|
|
return (enet_uint32) time (NULL);
|
|
|
|
}
|
|
|
|
|
2008-09-07 11:04:43 -04:00
|
|
|
enet_uint32
|
|
|
|
enet_time_get (void)
|
|
|
|
{
|
|
|
|
struct timeval timeVal;
|
|
|
|
|
|
|
|
gettimeofday (& timeVal, NULL);
|
|
|
|
|
|
|
|
return timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - timeBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
enet_time_set (enet_uint32 newTimeBase)
|
|
|
|
{
|
|
|
|
struct timeval timeVal;
|
|
|
|
|
|
|
|
gettimeofday (& timeVal, NULL);
|
|
|
|
|
|
|
|
timeBase = timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - newTimeBase;
|
|
|
|
}
|
|
|
|
|
2020-02-27 11:55:10 -05:00
|
|
|
int
|
|
|
|
enet_address_set_host_ip (ENetAddress * address, const char * name)
|
|
|
|
{
|
|
|
|
#ifdef HAS_INET_PTON
|
2020-02-27 11:59:37 -05:00
|
|
|
if (! inet_pton (AF_INET, name, & address -> host.p0))
|
2020-02-27 11:55:10 -05:00
|
|
|
#else
|
2020-02-27 11:59:37 -05:00
|
|
|
if (! inet_aton (name, (struct in_addr *) & address -> host.p0))
|
2020-02-27 11:55:10 -05:00
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-07 11:04:43 -04:00
|
|
|
int
|
|
|
|
enet_address_set_host (ENetAddress * address, const char * name)
|
|
|
|
{
|
2020-02-27 11:55:10 -05:00
|
|
|
#ifdef HAS_GETADDRINFO
|
|
|
|
struct addrinfo hints, * resultList = NULL, * result = NULL;
|
|
|
|
|
|
|
|
memset (& hints, 0, sizeof (hints));
|
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
|
|
|
|
if (getaddrinfo (name, NULL, NULL, & resultList) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (result = resultList; result != NULL; result = result -> ai_next)
|
|
|
|
{
|
|
|
|
if (result -> ai_family == AF_INET && result -> ai_addr != NULL && result -> ai_addrlen >= sizeof (struct sockaddr_in))
|
|
|
|
{
|
|
|
|
struct sockaddr_in * sin = (struct sockaddr_in *) result -> ai_addr;
|
|
|
|
|
2020-02-27 11:59:37 -05:00
|
|
|
address -> host.p0 = sin -> sin_addr.s_addr;
|
2020-02-27 11:55:10 -05:00
|
|
|
|
|
|
|
freeaddrinfo (resultList);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resultList != NULL)
|
|
|
|
freeaddrinfo (resultList);
|
|
|
|
#else
|
2008-09-07 11:04:43 -04:00
|
|
|
struct hostent * hostEntry = NULL;
|
|
|
|
#ifdef HAS_GETHOSTBYNAME_R
|
|
|
|
struct hostent hostData;
|
|
|
|
char buffer [2048];
|
|
|
|
int errnum;
|
|
|
|
|
2020-02-27 11:55:10 -05:00
|
|
|
#if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
|
2008-09-07 11:04:43 -04:00
|
|
|
gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
|
|
|
|
#else
|
|
|
|
hostEntry = gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & errnum);
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
hostEntry = gethostbyname (name);
|
|
|
|
#endif
|
|
|
|
|
2020-02-27 11:55:10 -05:00
|
|
|
if (hostEntry != NULL && hostEntry -> h_addrtype == AF_INET)
|
2008-09-07 11:04:43 -04:00
|
|
|
{
|
2020-02-27 11:59:37 -05:00
|
|
|
address -> host.p0 = * (enet_uint32 *) hostEntry -> h_addr_list [0];
|
2020-02-27 11:55:10 -05:00
|
|
|
|
2008-09-07 11:04:43 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2020-02-27 11:55:10 -05:00
|
|
|
#endif
|
2008-09-07 11:04:43 -04:00
|
|
|
|
2020-02-27 11:55:10 -05:00
|
|
|
return enet_address_set_host_ip (address, name);
|
2008-09-07 11:04:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
|
|
|
|
{
|
|
|
|
#ifdef HAS_INET_NTOP
|
2020-02-27 11:59:37 -05:00
|
|
|
if (inet_ntop (AF_INET, & address -> host.p0, name, nameLength) == NULL)
|
2008-09-07 11:04:43 -04:00
|
|
|
#else
|
2020-02-27 11:59:37 -05:00
|
|
|
char * addr = inet_ntoa (* (struct in_addr *) & address -> host.p0);
|
2008-09-07 11:04:43 -04:00
|
|
|
if (addr != NULL)
|
2018-01-30 17:10:30 -05:00
|
|
|
{
|
|
|
|
size_t addrLen = strlen(addr);
|
|
|
|
if (addrLen >= nameLength)
|
|
|
|
return -1;
|
|
|
|
memcpy (name, addr, addrLen + 1);
|
|
|
|
}
|
2008-09-07 11:04:43 -04:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
|
|
|
|
{
|
2020-02-27 11:55:10 -05:00
|
|
|
#ifdef HAS_GETNAMEINFO
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
memset (& sin, 0, sizeof (struct sockaddr_in));
|
|
|
|
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
|
2020-02-27 11:59:37 -05:00
|
|
|
sin.sin_addr.s_addr = address -> host.p0;
|
2020-02-27 11:55:10 -05:00
|
|
|
|
|
|
|
err = getnameinfo ((struct sockaddr *) & sin, sizeof (sin), name, nameLength, NULL, 0, NI_NAMEREQD);
|
|
|
|
if (! err)
|
|
|
|
{
|
|
|
|
if (name != NULL && nameLength > 0 && ! memchr (name, '\0', nameLength))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (err != EAI_NONAME)
|
|
|
|
return -1;
|
|
|
|
#else
|
2008-09-07 11:04:43 -04:00
|
|
|
struct in_addr in;
|
|
|
|
struct hostent * hostEntry = NULL;
|
|
|
|
#ifdef HAS_GETHOSTBYADDR_R
|
|
|
|
struct hostent hostData;
|
|
|
|
char buffer [2048];
|
|
|
|
int errnum;
|
|
|
|
|
2020-02-27 11:59:37 -05:00
|
|
|
in.s_addr = address -> host.p0;
|
2008-09-07 11:04:43 -04:00
|
|
|
|
2020-02-27 11:55:10 -05:00
|
|
|
#if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
|
2008-09-07 11:04:43 -04:00
|
|
|
gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
|
|
|
|
#else
|
|
|
|
hostEntry = gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & errnum);
|
|
|
|
#endif
|
|
|
|
#else
|
2020-02-27 11:59:37 -05:00
|
|
|
in.s_addr = address -> host.p0;
|
2008-09-07 11:04:43 -04:00
|
|
|
|
|
|
|
hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET);
|
|
|
|
#endif
|
|
|
|
|
2020-02-27 11:55:10 -05:00
|
|
|
if (hostEntry != NULL)
|
2018-01-30 17:10:30 -05:00
|
|
|
{
|
|
|
|
size_t hostLen = strlen (hostEntry -> h_name);
|
|
|
|
if (hostLen >= nameLength)
|
|
|
|
return -1;
|
|
|
|
memcpy (name, hostEntry -> h_name, hostLen + 1);
|
2020-02-27 11:55:10 -05:00
|
|
|
return 0;
|
2018-01-30 17:10:30 -05:00
|
|
|
}
|
2020-02-27 11:55:10 -05:00
|
|
|
#endif
|
2008-09-07 11:04:43 -04:00
|
|
|
|
2020-02-27 11:55:10 -05:00
|
|
|
return enet_address_get_host_ip (address, name, nameLength);
|
2008-09-07 11:04:43 -04:00
|
|
|
}
|
|
|
|
|
2011-03-17 17:02:22 -04:00
|
|
|
int
|
|
|
|
enet_socket_bind (ENetSocket socket, const ENetAddress * address)
|
2008-09-07 11:04:43 -04:00
|
|
|
{
|
2020-02-27 11:59:37 -05:00
|
|
|
if (isIPv6Socket() == 1)
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 sin;
|
|
|
|
memset (& sin, 0, sizeof (sin));
|
|
|
|
sin.sin6_family = AF_INET6;
|
2020-02-27 11:55:10 -05:00
|
|
|
|
2020-02-27 11:59:37 -05:00
|
|
|
if (address != NULL)
|
|
|
|
{
|
|
|
|
sin.sin6_port = ENET_HOST_TO_NET_16 (address -> port);
|
|
|
|
memcpy (sin.sin6_addr.s6_addr, & address -> host.p0, 16);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sin.sin6_port = 0;
|
|
|
|
sin.sin6_addr = in6addr_any;
|
|
|
|
}
|
2020-02-27 11:55:10 -05:00
|
|
|
|
2020-02-27 11:59:37 -05:00
|
|
|
return bind (socket,
|
|
|
|
(struct sockaddr *) & sin,
|
|
|
|
sizeof (struct sockaddr_in6));
|
2011-03-17 17:02:22 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-27 11:59:37 -05:00
|
|
|
struct sockaddr_in sin;
|
|
|
|
|
|
|
|
memset (& sin, 0, sizeof (struct sockaddr_in));
|
|
|
|
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
|
|
|
|
if (address != NULL)
|
|
|
|
{
|
|
|
|
sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
|
|
|
|
sin.sin_addr.s_addr = address -> host.p0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sin.sin_port = 0;
|
|
|
|
sin.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
}
|
2020-02-27 11:55:10 -05:00
|
|
|
|
2020-02-27 11:59:37 -05:00
|
|
|
return bind (socket,
|
|
|
|
(struct sockaddr *) & sin,
|
|
|
|
sizeof (struct sockaddr_in));
|
|
|
|
}
|
2011-03-17 17:02:22 -04:00
|
|
|
}
|
|
|
|
|
2018-01-30 17:10:30 -05:00
|
|
|
int
|
|
|
|
enet_socket_get_address (ENetSocket socket, ENetAddress * address)
|
|
|
|
{
|
2020-02-27 11:59:37 -05:00
|
|
|
if (isIPv6Socket() == 1)
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 sin;
|
|
|
|
memset (& sin, 0, sizeof (sin));
|
|
|
|
socklen_t sinLength = sizeof (struct sockaddr_in6);
|
2018-01-30 17:10:30 -05:00
|
|
|
|
2020-02-27 11:59:37 -05:00
|
|
|
if (getsockname (socket, (struct sockaddr *) & sin, & sinLength) == -1)
|
|
|
|
return -1;
|
2018-01-30 17:10:30 -05:00
|
|
|
|
2020-02-27 11:59:37 -05:00
|
|
|
memcpy (& address -> host.p0, sin.sin6_addr.s6_addr, 16);
|
|
|
|
address -> host.p4 = sin.sin6_scope_id;
|
|
|
|
address -> port = ENET_NET_TO_HOST_16 (sin.sin6_port);
|
2018-01-30 17:10:30 -05:00
|
|
|
|
2020-02-27 11:59:37 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
socklen_t sinLength = sizeof (struct sockaddr_in);
|
|
|
|
|
|
|
|
if (getsockname (socket, (struct sockaddr *) & sin, & sinLength) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
address -> host.p0 = (enet_uint32) sin.sin_addr.s_addr;
|
|
|
|
address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2018-01-30 17:10:30 -05:00
|
|
|
}
|
|
|
|
|
2011-03-17 17:02:22 -04:00
|
|
|
int
|
|
|
|
enet_socket_listen (ENetSocket socket, int backlog)
|
|
|
|
{
|
|
|
|
return listen (socket, backlog < 0 ? SOMAXCONN : backlog);
|
|
|
|
}
|
|
|
|
|
|
|
|
ENetSocket
|
|
|
|
enet_socket_create (ENetSocketType type)
|
|
|
|
{
|
2020-02-27 11:59:37 -05:00
|
|
|
int pf_family = isIPv6Socket() == 1 ? PF_INET6 : PF_INET;
|
|
|
|
int socket_fd = socket (pf_family, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
|
|
|
|
if (isIPv6Socket() == 1 && socket_fd != -1)
|
|
|
|
{
|
|
|
|
int no = 0;
|
|
|
|
// Allow IPv6 socket listen to IPv4 connection (as long as the host has IPv4 address)
|
|
|
|
// We always use dual stack in STK
|
|
|
|
setsockopt (socket_fd, IPPROTO_IPV6, IPV6_V6ONLY, (void *) & no, sizeof (no));
|
|
|
|
}
|
|
|
|
return socket_fd;
|
2008-09-07 11:04:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value)
|
|
|
|
{
|
|
|
|
int result = -1;
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case ENET_SOCKOPT_NONBLOCK:
|
|
|
|
#ifdef HAS_FCNTL
|
2020-02-27 11:55:10 -05:00
|
|
|
result = fcntl (socket, F_SETFL, (value ? O_NONBLOCK : 0) | (fcntl (socket, F_GETFL) & ~O_NONBLOCK));
|
2008-09-07 11:04:43 -04:00
|
|
|
#else
|
|
|
|
result = ioctl (socket, FIONBIO, & value);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ENET_SOCKOPT_BROADCAST:
|
|
|
|
result = setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) & value, sizeof (int));
|
|
|
|
break;
|
|
|
|
|
2011-03-17 17:02:22 -04:00
|
|
|
case ENET_SOCKOPT_REUSEADDR:
|
|
|
|
result = setsockopt (socket, SOL_SOCKET, SO_REUSEADDR, (char *) & value, sizeof (int));
|
|
|
|
break;
|
|
|
|
|
2008-09-07 11:04:43 -04:00
|
|
|
case ENET_SOCKOPT_RCVBUF:
|
|
|
|
result = setsockopt (socket, SOL_SOCKET, SO_RCVBUF, (char *) & value, sizeof (int));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ENET_SOCKOPT_SNDBUF:
|
|
|
|
result = setsockopt (socket, SOL_SOCKET, SO_SNDBUF, (char *) & value, sizeof (int));
|
|
|
|
break;
|
|
|
|
|
2018-01-30 17:10:30 -05:00
|
|
|
case ENET_SOCKOPT_RCVTIMEO:
|
|
|
|
{
|
|
|
|
struct timeval timeVal;
|
|
|
|
timeVal.tv_sec = value / 1000;
|
|
|
|
timeVal.tv_usec = (value % 1000) * 1000;
|
|
|
|
result = setsockopt (socket, SOL_SOCKET, SO_RCVTIMEO, (char *) & timeVal, sizeof (struct timeval));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ENET_SOCKOPT_SNDTIMEO:
|
|
|
|
{
|
|
|
|
struct timeval timeVal;
|
|
|
|
timeVal.tv_sec = value / 1000;
|
|
|
|
timeVal.tv_usec = (value % 1000) * 1000;
|
|
|
|
result = setsockopt (socket, SOL_SOCKET, SO_SNDTIMEO, (char *) & timeVal, sizeof (struct timeval));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ENET_SOCKOPT_NODELAY:
|
|
|
|
result = setsockopt (socket, IPPROTO_TCP, TCP_NODELAY, (char *) & value, sizeof (int));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result == -1 ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
enet_socket_get_option (ENetSocket socket, ENetSocketOption option, int * value)
|
|
|
|
{
|
|
|
|
int result = -1;
|
|
|
|
socklen_t len;
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case ENET_SOCKOPT_ERROR:
|
|
|
|
len = sizeof (int);
|
|
|
|
result = getsockopt (socket, SOL_SOCKET, SO_ERROR, value, & len);
|
|
|
|
break;
|
|
|
|
|
2008-09-07 11:04:43 -04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result == -1 ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
enet_socket_connect (ENetSocket socket, const ENetAddress * address)
|
|
|
|
{
|
|
|
|
struct sockaddr_in sin;
|
2018-01-30 17:10:30 -05:00
|
|
|
int result;
|
2008-09-07 11:04:43 -04:00
|
|
|
|
|
|
|
memset (& sin, 0, sizeof (struct sockaddr_in));
|
|
|
|
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
|
2020-02-27 11:59:37 -05:00
|
|
|
sin.sin_addr.s_addr = address -> host.p0;
|
2008-09-07 11:04:43 -04:00
|
|
|
|
2018-01-30 17:10:30 -05:00
|
|
|
result = connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in));
|
|
|
|
if (result == -1 && errno == EINPROGRESS)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return result;
|
2008-09-07 11:04:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ENetSocket
|
|
|
|
enet_socket_accept (ENetSocket socket, ENetAddress * address)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
socklen_t sinLength = sizeof (struct sockaddr_in);
|
|
|
|
|
|
|
|
result = accept (socket,
|
|
|
|
address != NULL ? (struct sockaddr *) & sin : NULL,
|
|
|
|
address != NULL ? & sinLength : NULL);
|
|
|
|
|
|
|
|
if (result == -1)
|
|
|
|
return ENET_SOCKET_NULL;
|
|
|
|
|
|
|
|
if (address != NULL)
|
|
|
|
{
|
2020-02-27 11:59:37 -05:00
|
|
|
address -> host.p0 = (enet_uint32) sin.sin_addr.s_addr;
|
2008-09-07 11:04:43 -04:00
|
|
|
address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-01-30 17:10:30 -05:00
|
|
|
int
|
|
|
|
enet_socket_shutdown (ENetSocket socket, ENetSocketShutdown how)
|
|
|
|
{
|
|
|
|
return shutdown (socket, (int) how);
|
|
|
|
}
|
|
|
|
|
2008-09-07 11:04:43 -04:00
|
|
|
void
|
|
|
|
enet_socket_destroy (ENetSocket socket)
|
|
|
|
{
|
2018-01-30 17:10:30 -05:00
|
|
|
if (socket != -1)
|
|
|
|
close (socket);
|
2008-09-07 11:04:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
enet_socket_send (ENetSocket socket,
|
|
|
|
const ENetAddress * address,
|
|
|
|
const ENetBuffer * buffers,
|
|
|
|
size_t bufferCount)
|
|
|
|
{
|
|
|
|
struct msghdr msgHdr;
|
2020-02-27 11:59:37 -05:00
|
|
|
struct sockaddr_storage sin;
|
|
|
|
memset (& sin, 0, sizeof (sin));
|
2008-09-07 11:04:43 -04:00
|
|
|
int sentLength;
|
|
|
|
|
|
|
|
memset (& msgHdr, 0, sizeof (struct msghdr));
|
|
|
|
|
|
|
|
if (address != NULL)
|
|
|
|
{
|
2020-02-27 11:59:37 -05:00
|
|
|
if (isIPv6Socket() == 1)
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 * v6 = (struct sockaddr_in6 *) & sin;
|
|
|
|
v6 -> sin6_family = AF_INET6;
|
|
|
|
v6 -> sin6_port = ENET_HOST_TO_NET_16 (address -> port);
|
|
|
|
memcpy (v6 -> sin6_addr.s6_addr, & address -> host.p0, 16);
|
|
|
|
v6 -> sin6_scope_id = address -> host.p4;
|
|
|
|
|
|
|
|
msgHdr.msg_name = & sin;
|
|
|
|
msgHdr.msg_namelen = sizeof (struct sockaddr_in6);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct sockaddr_in * v4 = (struct sockaddr_in *) & sin;
|
|
|
|
v4 -> sin_family = AF_INET;
|
|
|
|
v4 -> sin_port = ENET_HOST_TO_NET_16 (address -> port);
|
|
|
|
v4 -> sin_addr.s_addr = address -> host.p0;
|
2008-09-07 11:04:43 -04:00
|
|
|
|
2020-02-27 11:59:37 -05:00
|
|
|
msgHdr.msg_name = & sin;
|
|
|
|
msgHdr.msg_namelen = sizeof (struct sockaddr_in);
|
|
|
|
}
|
2008-09-07 11:04:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
msgHdr.msg_iov = (struct iovec *) buffers;
|
|
|
|
msgHdr.msg_iovlen = bufferCount;
|
|
|
|
|
|
|
|
sentLength = sendmsg (socket, & msgHdr, MSG_NOSIGNAL);
|
|
|
|
|
|
|
|
if (sentLength == -1)
|
|
|
|
{
|
|
|
|
if (errno == EWOULDBLOCK)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sentLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
enet_socket_receive (ENetSocket socket,
|
|
|
|
ENetAddress * address,
|
|
|
|
ENetBuffer * buffers,
|
|
|
|
size_t bufferCount)
|
|
|
|
{
|
|
|
|
struct msghdr msgHdr;
|
2020-02-27 11:59:37 -05:00
|
|
|
struct sockaddr_storage sin;
|
|
|
|
memset (& sin, 0, sizeof (sin));
|
2008-09-07 11:04:43 -04:00
|
|
|
int recvLength;
|
|
|
|
|
|
|
|
memset (& msgHdr, 0, sizeof (struct msghdr));
|
|
|
|
|
|
|
|
if (address != NULL)
|
|
|
|
{
|
2020-02-27 11:55:10 -05:00
|
|
|
msgHdr.msg_name = & sin;
|
2020-02-27 11:59:37 -05:00
|
|
|
msgHdr.msg_namelen = sizeof (sin);
|
2008-09-07 11:04:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
msgHdr.msg_iov = (struct iovec *) buffers;
|
|
|
|
msgHdr.msg_iovlen = bufferCount;
|
|
|
|
|
|
|
|
recvLength = recvmsg (socket, & msgHdr, MSG_NOSIGNAL);
|
|
|
|
|
|
|
|
if (recvLength == -1)
|
|
|
|
{
|
|
|
|
if (errno == EWOULDBLOCK)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAS_MSGHDR_FLAGS
|
|
|
|
if (msgHdr.msg_flags & MSG_TRUNC)
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (address != NULL)
|
|
|
|
{
|
2020-02-27 11:59:37 -05:00
|
|
|
switch (sin.ss_family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
// Should not happen if dual stack is working
|
|
|
|
if (isIPv6Socket() == 1)
|
|
|
|
return -1;
|
|
|
|
struct sockaddr_in * v4 = (struct sockaddr_in *) & sin;
|
|
|
|
address -> host.p0 = (enet_uint32) v4 -> sin_addr.s_addr;
|
|
|
|
address -> port = ENET_NET_TO_HOST_16 (v4->sin_port);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
if (isIPv6Socket() != 1)
|
|
|
|
return -1;
|
|
|
|
struct sockaddr_in6 * v6 = (struct sockaddr_in6 *) & sin;
|
|
|
|
memcpy (& address -> host.p0, v6 -> sin6_addr.s6_addr, 16);
|
|
|
|
address -> host.p4 = v6 -> sin6_scope_id;
|
|
|
|
address -> port = ENET_NET_TO_HOST_16 (v6 -> sin6_port);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
2008-09-07 11:04:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return recvLength;
|
|
|
|
}
|
|
|
|
|
2011-03-17 17:02:22 -04:00
|
|
|
int
|
|
|
|
enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocketSet * writeSet, enet_uint32 timeout)
|
|
|
|
{
|
|
|
|
struct timeval timeVal;
|
|
|
|
|
|
|
|
timeVal.tv_sec = timeout / 1000;
|
|
|
|
timeVal.tv_usec = (timeout % 1000) * 1000;
|
|
|
|
|
|
|
|
return select (maxSocket + 1, readSet, writeSet, NULL, & timeVal);
|
|
|
|
}
|
|
|
|
|
2008-09-07 11:04:43 -04:00
|
|
|
int
|
|
|
|
enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
|
|
|
|
{
|
|
|
|
#ifdef HAS_POLL
|
|
|
|
struct pollfd pollSocket;
|
|
|
|
int pollCount;
|
|
|
|
|
|
|
|
pollSocket.fd = socket;
|
|
|
|
pollSocket.events = 0;
|
|
|
|
|
|
|
|
if (* condition & ENET_SOCKET_WAIT_SEND)
|
|
|
|
pollSocket.events |= POLLOUT;
|
|
|
|
|
|
|
|
if (* condition & ENET_SOCKET_WAIT_RECEIVE)
|
|
|
|
pollSocket.events |= POLLIN;
|
|
|
|
|
|
|
|
pollCount = poll (& pollSocket, 1, timeout);
|
|
|
|
|
|
|
|
if (pollCount < 0)
|
2018-01-30 17:10:30 -05:00
|
|
|
{
|
|
|
|
if (errno == EINTR && * condition & ENET_SOCKET_WAIT_INTERRUPT)
|
|
|
|
{
|
|
|
|
* condition = ENET_SOCKET_WAIT_INTERRUPT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2008-09-07 11:04:43 -04:00
|
|
|
|
|
|
|
* condition = ENET_SOCKET_WAIT_NONE;
|
|
|
|
|
|
|
|
if (pollCount == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (pollSocket.revents & POLLOUT)
|
|
|
|
* condition |= ENET_SOCKET_WAIT_SEND;
|
|
|
|
|
|
|
|
if (pollSocket.revents & POLLIN)
|
|
|
|
* condition |= ENET_SOCKET_WAIT_RECEIVE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
fd_set readSet, writeSet;
|
|
|
|
struct timeval timeVal;
|
|
|
|
int selectCount;
|
|
|
|
|
|
|
|
timeVal.tv_sec = timeout / 1000;
|
|
|
|
timeVal.tv_usec = (timeout % 1000) * 1000;
|
|
|
|
|
|
|
|
FD_ZERO (& readSet);
|
|
|
|
FD_ZERO (& writeSet);
|
|
|
|
|
|
|
|
if (* condition & ENET_SOCKET_WAIT_SEND)
|
|
|
|
FD_SET (socket, & writeSet);
|
|
|
|
|
|
|
|
if (* condition & ENET_SOCKET_WAIT_RECEIVE)
|
|
|
|
FD_SET (socket, & readSet);
|
|
|
|
|
|
|
|
selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
|
|
|
|
|
|
|
|
if (selectCount < 0)
|
2018-01-30 17:10:30 -05:00
|
|
|
{
|
|
|
|
if (errno == EINTR && * condition & ENET_SOCKET_WAIT_INTERRUPT)
|
|
|
|
{
|
|
|
|
* condition = ENET_SOCKET_WAIT_INTERRUPT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2008-09-07 11:04:43 -04:00
|
|
|
|
|
|
|
* condition = ENET_SOCKET_WAIT_NONE;
|
|
|
|
|
|
|
|
if (selectCount == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (FD_ISSET (socket, & writeSet))
|
|
|
|
* condition |= ENET_SOCKET_WAIT_SEND;
|
|
|
|
|
|
|
|
if (FD_ISSET (socket, & readSet))
|
|
|
|
* condition |= ENET_SOCKET_WAIT_RECEIVE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|