2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* socket_helpers.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:11:42 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2017-03-05 14:47:28 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2016-10-20 10:04:10 +00:00
|
|
|
#ifndef SOCKET_HELPERS_H
|
|
|
|
#define SOCKET_HELPERS_H
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
#if defined(__MINGW32__) && (!defined(__MINGW64_VERSION_MAJOR) || __MINGW64_VERSION_MAJOR < 4)
|
|
|
|
// Workaround for mingw-w64 < 4.0
|
|
|
|
#ifndef IPV6_V6ONLY
|
|
|
|
#define IPV6_V6ONLY 27
|
|
|
|
#endif
|
2016-10-31 23:39:45 +00:00
|
|
|
#endif
|
|
|
|
|
2016-10-20 10:04:10 +00:00
|
|
|
// helpers for sockaddr -> IP_Address and back, should work for posix and winsock. All implementations should use this
|
|
|
|
|
2017-03-05 15:44:50 +00: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 10:04:10 +00:00
|
|
|
|
|
|
|
memset(p_addr, 0, sizeof(struct sockaddr_storage));
|
2016-12-01 03:23:57 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!p_ip.is_valid(), 0);
|
2016-12-01 03:23:57 +00:00
|
|
|
|
|
|
|
// IPv6 socket
|
2016-12-01 05:34:05 +00:00
|
|
|
if (p_sock_type == IP::TYPE_IPV6 || p_sock_type == IP::TYPE_ANY) {
|
2016-10-20 10:04:10 +00:00
|
|
|
|
2016-12-05 15:32:38 +00:00
|
|
|
// IPv6 only socket with IPv4 address
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(p_sock_type == IP::TYPE_IPV6 && p_ip.is_ipv4(), 0);
|
2016-12-05 15:32:38 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
|
2016-10-20 10:04:10 +00:00
|
|
|
addr6->sin6_family = AF_INET6;
|
|
|
|
addr6->sin6_port = htons(p_port);
|
2016-12-05 15:32:38 +00:00
|
|
|
copymem(&addr6->sin6_addr.s6_addr, p_ip.get_ipv6(), 16);
|
2016-10-22 05:23:07 +00:00
|
|
|
return sizeof(sockaddr_in6);
|
2016-10-20 10:04:10 +00:00
|
|
|
|
2016-12-01 03:23:57 +00:00
|
|
|
} else { // IPv4 socket
|
2016-10-20 10:04:10 +00:00
|
|
|
|
2016-12-05 15:32:38 +00:00
|
|
|
// IPv4 socket with IPv6 address
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!p_ip.is_ipv4(), 0);
|
2016-12-05 15:32:38 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
|
2016-12-05 15:32:38 +00:00
|
|
|
addr4->sin_family = AF_INET;
|
2017-03-05 15:44:50 +00:00
|
|
|
addr4->sin_port = htons(p_port); // short, network byte order
|
2016-12-05 15:32:38 +00:00
|
|
|
copymem(&addr4->sin_addr.s_addr, p_ip.get_ipv4(), 16);
|
2016-10-22 05:23:07 +00:00
|
|
|
return sizeof(sockaddr_in);
|
2016-10-20 10:04:10 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
static size_t _set_listen_sockaddr(struct sockaddr_storage *p_addr, int p_port, IP::Type p_sock_type, const IP_Address p_bind_address) {
|
2016-10-20 10:04:10 +00:00
|
|
|
|
|
|
|
memset(p_addr, 0, sizeof(struct sockaddr_storage));
|
2016-12-01 05:34:05 +00:00
|
|
|
if (p_sock_type == IP::TYPE_IPV4) {
|
2017-03-05 15:44:50 +00:00
|
|
|
struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
|
2016-10-20 10:04:10 +00:00
|
|
|
addr4->sin_family = AF_INET;
|
|
|
|
addr4->sin_port = htons(p_port);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_bind_address.is_valid()) {
|
2017-01-18 07:37:24 +00:00
|
|
|
copymem(&addr4->sin_addr.s_addr, p_bind_address.get_ipv4(), 4);
|
|
|
|
} else {
|
|
|
|
addr4->sin_addr.s_addr = INADDR_ANY;
|
|
|
|
}
|
2016-10-22 05:23:07 +00:00
|
|
|
return sizeof(sockaddr_in);
|
2016-10-20 10:04:10 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
|
2016-10-20 10:04:10 +00:00
|
|
|
|
|
|
|
addr6->sin6_family = AF_INET6;
|
|
|
|
addr6->sin6_port = htons(p_port);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_bind_address.is_valid()) {
|
2017-01-18 07:37:24 +00:00
|
|
|
copymem(&addr6->sin6_addr.s6_addr, p_bind_address.get_ipv6(), 16);
|
|
|
|
} else {
|
|
|
|
addr6->sin6_addr = in6addr_any;
|
|
|
|
}
|
2016-10-22 05:23:07 +00:00
|
|
|
return sizeof(sockaddr_in6);
|
2016-10-20 10:04:10 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-05-08 11:42:43 +00:00
|
|
|
static int _socket_create(IP::Type &p_type, int type, int protocol) {
|
2016-10-28 02:18:17 +00:00
|
|
|
|
2016-12-01 05:34:05 +00:00
|
|
|
ERR_FAIL_COND_V(p_type > IP::TYPE_ANY || p_type < IP::TYPE_NONE, ERR_INVALID_PARAMETER);
|
2016-10-28 02:18:17 +00:00
|
|
|
|
2016-12-01 05:34:05 +00:00
|
|
|
int family = p_type == IP::TYPE_IPV4 ? AF_INET : AF_INET6;
|
2016-10-28 02:18:17 +00:00
|
|
|
int sockfd = socket(family, type, protocol);
|
|
|
|
|
2017-05-08 11:42:43 +00:00
|
|
|
if (sockfd == -1 && p_type == IP::TYPE_ANY) {
|
|
|
|
// Careful here, changing the referenced parameter so the caller knows that we are using an IPv4 socket
|
|
|
|
// in place of a dual stack one, and further calls to _set_sock_addr will work as expected.
|
|
|
|
p_type = IP::TYPE_IPV4;
|
|
|
|
family = AF_INET;
|
|
|
|
sockfd = socket(family, type, protocol);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(sockfd == -1, -1);
|
2016-10-28 02:18:17 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (family == AF_INET6) {
|
2016-12-01 04:05:44 +00:00
|
|
|
// Select IPv4 over IPv6 mapping
|
2016-12-01 05:34:05 +00:00
|
|
|
int opt = p_type != IP::TYPE_ANY;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&opt, sizeof(opt)) != 0) {
|
2016-12-01 04:05:44 +00:00
|
|
|
WARN_PRINT("Unable to set/unset IPv4 address mapping over IPv6");
|
2016-10-28 02:18:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sockfd;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
static void _set_ip_addr_port(IP_Address &r_ip, int &r_port, struct sockaddr_storage *p_addr) {
|
2016-10-20 10:04:10 +00:00
|
|
|
|
|
|
|
if (p_addr->ss_family == AF_INET) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
|
2016-12-05 15:32:38 +00:00
|
|
|
r_ip.set_ipv4((uint8_t *)&(addr4->sin_addr.s_addr));
|
2016-10-20 10:04:10 +00:00
|
|
|
|
|
|
|
r_port = ntohs(addr4->sin_port);
|
|
|
|
|
|
|
|
} else if (p_addr->ss_family == AF_INET6) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
|
2016-12-05 15:32:38 +00:00
|
|
|
r_ip.set_ipv6(addr6->sin6_addr.s6_addr);
|
2016-10-20 10:04:10 +00:00
|
|
|
|
|
|
|
r_port = ntohs(addr6->sin6_port);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|