2016-10-20 12:04:10 +02:00
|
|
|
#ifndef SOCKET_HELPERS_H
|
|
|
|
#define SOCKET_HELPERS_H
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-10-30 22:58:15 +01:00
|
|
|
#ifdef WINDOWS_ENABLED
|
|
|
|
// Workaround mingw missing flags!
|
|
|
|
#ifndef IPV6_V6ONLY
|
|
|
|
#define IPV6_V6ONLY 27
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2016-10-20 12:04:10 +02:00
|
|
|
// helpers for sockaddr -> IP_Address and back, should work for posix and winsock. All implementations should use this
|
|
|
|
|
2016-12-01 06:34:05 +01:00
|
|
|
static size_t _set_sockaddr(struct sockaddr_storage* p_addr, const IP_Address& p_ip, int p_port, IP::Type p_sock_type = IP::TYPE_ANY) {
|
2016-10-20 12:04:10 +02:00
|
|
|
|
|
|
|
memset(p_addr, 0, sizeof(struct sockaddr_storage));
|
2016-12-01 04:23:57 +01:00
|
|
|
|
|
|
|
// Dual stack (ANY) or matching ip type is required
|
2016-12-01 06:34:05 +01:00
|
|
|
ERR_FAIL_COND_V(p_sock_type != IP::TYPE_ANY && p_sock_type != p_ip.type,0);
|
2016-12-01 04:23:57 +01:00
|
|
|
|
|
|
|
// IPv6 socket
|
2016-12-01 06:34:05 +01:00
|
|
|
if (p_sock_type == IP::TYPE_IPV6 || p_sock_type == IP::TYPE_ANY) {
|
2016-10-20 12:04:10 +02:00
|
|
|
|
|
|
|
struct sockaddr_in6* addr6 = (struct sockaddr_in6*)p_addr;
|
|
|
|
addr6->sin6_family = AF_INET6;
|
|
|
|
addr6->sin6_port = htons(p_port);
|
2016-12-01 04:23:57 +01:00
|
|
|
if(p_ip.type == IP_Address::TYPE_IPV4) {
|
|
|
|
// Remapping needed
|
|
|
|
uint16_t base[8] = {0x0, 0x0, 0x0, 0x0, 0x0, 0xffff, p_ip.field16[0], p_ip.field16[1]};
|
|
|
|
copymem(&addr6->sin6_addr.s6_addr, base, 16);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
copymem(&addr6->sin6_addr.s6_addr, p_ip.field8, 16);
|
|
|
|
}
|
2016-10-22 07:23:07 +02:00
|
|
|
return sizeof(sockaddr_in6);
|
2016-10-20 12:04:10 +02:00
|
|
|
|
2016-12-01 04:23:57 +01:00
|
|
|
} else { // IPv4 socket
|
2016-10-20 12:04:10 +02:00
|
|
|
|
|
|
|
struct sockaddr_in* addr4 = (struct sockaddr_in*)p_addr;
|
|
|
|
addr4->sin_family = AF_INET; // host byte order
|
|
|
|
addr4->sin_port = htons(p_port); // short, network byte order
|
|
|
|
addr4->sin_addr = *((struct in_addr*)&p_ip.field32[0]);
|
2016-10-22 07:23:07 +02:00
|
|
|
return sizeof(sockaddr_in);
|
2016-10-20 12:04:10 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-12-01 06:34:05 +01:00
|
|
|
static size_t _set_listen_sockaddr(struct sockaddr_storage* p_addr, int p_port, IP::Type p_sock_type, const List<String> *p_accepted_hosts) {
|
2016-10-20 12:04:10 +02:00
|
|
|
|
|
|
|
memset(p_addr, 0, sizeof(struct sockaddr_storage));
|
2016-12-01 06:34:05 +01:00
|
|
|
if (p_sock_type == IP::TYPE_IPV4) {
|
2016-10-20 12:04:10 +02:00
|
|
|
struct sockaddr_in* addr4 = (struct sockaddr_in*)p_addr;
|
|
|
|
addr4->sin_family = AF_INET;
|
|
|
|
addr4->sin_port = htons(p_port);
|
|
|
|
addr4->sin_addr.s_addr = INADDR_ANY; // TODO: use accepted hosts list
|
2016-10-22 07:23:07 +02:00
|
|
|
return sizeof(sockaddr_in);
|
2016-10-20 12:04:10 +02:00
|
|
|
} else {
|
|
|
|
struct sockaddr_in6* addr6 = (struct sockaddr_in6*)p_addr;
|
|
|
|
|
|
|
|
addr6->sin6_family = AF_INET6;
|
|
|
|
addr6->sin6_port = htons(p_port);
|
|
|
|
addr6->sin6_addr = in6addr_any; // TODO: use accepted hosts list
|
2016-10-22 07:23:07 +02:00
|
|
|
return sizeof(sockaddr_in6);
|
2016-10-20 12:04:10 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-12-01 06:34:05 +01:00
|
|
|
static int _socket_create(IP::Type p_type, int type, int protocol) {
|
2016-10-28 04:18:17 +02:00
|
|
|
|
2016-12-01 06:34:05 +01:00
|
|
|
ERR_FAIL_COND_V(p_type > IP::TYPE_ANY || p_type < IP::TYPE_NONE, ERR_INVALID_PARAMETER);
|
2016-10-28 04:18:17 +02:00
|
|
|
|
2016-12-01 06:34:05 +01:00
|
|
|
int family = p_type == IP::TYPE_IPV4 ? AF_INET : AF_INET6;
|
2016-10-28 04:18:17 +02:00
|
|
|
int sockfd = socket(family, type, protocol);
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V( sockfd == -1, -1 );
|
|
|
|
|
|
|
|
if(family == AF_INET6) {
|
2016-12-01 05:05:44 +01:00
|
|
|
// Select IPv4 over IPv6 mapping
|
2016-12-01 06:34:05 +01:00
|
|
|
int opt = p_type != IP::TYPE_ANY;
|
2016-12-01 05:05:44 +01:00
|
|
|
if(setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&opt, sizeof(opt)) != 0) {
|
|
|
|
WARN_PRINT("Unable to set/unset IPv4 address mapping over IPv6");
|
2016-10-28 04:18:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sockfd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-20 12:04:10 +02:00
|
|
|
static void _set_ip_addr_port(IP_Address& r_ip, int& r_port, struct sockaddr_storage* p_addr) {
|
|
|
|
|
|
|
|
if (p_addr->ss_family == AF_INET) {
|
|
|
|
r_ip.type = IP_Address::TYPE_IPV4;
|
|
|
|
|
|
|
|
struct sockaddr_in* addr4 = (struct sockaddr_in*)p_addr;
|
|
|
|
r_ip.field32[0] = (uint32_t)addr4->sin_addr.s_addr;
|
|
|
|
|
|
|
|
r_port = ntohs(addr4->sin_port);
|
|
|
|
|
|
|
|
} else if (p_addr->ss_family == AF_INET6) {
|
|
|
|
|
|
|
|
r_ip.type = IP_Address::TYPE_IPV6;
|
|
|
|
|
|
|
|
struct sockaddr_in6* addr6 = (struct sockaddr_in6*)p_addr;
|
2016-12-02 04:10:55 +01:00
|
|
|
copymem(&r_ip.field8, addr6->sin6_addr.s6_addr, 16);
|
2016-10-20 12:04:10 +02:00
|
|
|
|
|
|
|
r_port = ntohs(addr6->sin6_port);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|