2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* packet_peer_udp_posix.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +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) */
|
2016-06-18 12:46:12 +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. */
|
|
|
|
/*************************************************************************/
|
2014-11-12 14:23:23 +00:00
|
|
|
#include "packet_peer_udp_posix.h"
|
|
|
|
|
|
|
|
#ifdef UNIX_ENABLED
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <sys/socket.h>
|
2017-03-05 15:44:50 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2014-11-12 14:23:23 +00:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#ifndef NO_FCNTL
|
2017-03-05 15:44:50 +00:00
|
|
|
#ifdef __HAIKU__
|
|
|
|
#include <fcntl.h>
|
|
|
|
#else
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#endif
|
2014-11-12 14:23:23 +00:00
|
|
|
#else
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef JAVASCRIPT_ENABLED
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
|
2016-10-20 10:04:10 +00:00
|
|
|
#include "drivers/unix/socket_helpers.h"
|
2014-11-12 14:23:23 +00:00
|
|
|
|
|
|
|
int PacketPeerUDPPosix::get_available_packet_count() const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Error err = const_cast<PacketPeerUDPPosix *>(this)->_poll(false);
|
|
|
|
if (err != OK)
|
2014-11-12 14:23:23 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return queue_count;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer, int &r_buffer_size) const {
|
2014-11-12 14:23:23 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Error err = const_cast<PacketPeerUDPPosix *>(this)->_poll(false);
|
|
|
|
if (err != OK)
|
2014-11-12 14:23:23 +00:00
|
|
|
return err;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (queue_count == 0)
|
2014-11-12 14:23:23 +00:00
|
|
|
return ERR_UNAVAILABLE;
|
|
|
|
|
|
|
|
uint32_t size;
|
2016-10-18 21:53:18 +00:00
|
|
|
uint8_t type;
|
|
|
|
rb.read(&type, 1, true);
|
2016-12-05 15:32:38 +00:00
|
|
|
if (type == IP::TYPE_IPV4) {
|
|
|
|
uint8_t ip[4];
|
2017-03-05 15:44:50 +00:00
|
|
|
rb.read(ip, 4, true);
|
2016-12-05 15:32:38 +00:00
|
|
|
packet_ip.set_ipv4(ip);
|
2016-10-18 21:53:18 +00:00
|
|
|
} else {
|
2016-12-05 15:32:38 +00:00
|
|
|
uint8_t ipv6[16];
|
2017-03-05 15:44:50 +00:00
|
|
|
rb.read(ipv6, 16, true);
|
2016-12-05 15:32:38 +00:00
|
|
|
packet_ip.set_ipv6(ipv6);
|
2016-10-18 21:53:18 +00:00
|
|
|
};
|
2017-03-05 15:44:50 +00:00
|
|
|
rb.read((uint8_t *)&packet_port, 4, true);
|
|
|
|
rb.read((uint8_t *)&size, 4, true);
|
|
|
|
rb.read(packet_buffer, size, true);
|
2014-11-12 14:23:23 +00:00
|
|
|
--queue_count;
|
2017-03-05 15:44:50 +00:00
|
|
|
*r_buffer = packet_buffer;
|
|
|
|
r_buffer_size = size;
|
2014-11-12 14:23:23 +00:00
|
|
|
return OK;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
|
2014-11-12 14:23:23 +00:00
|
|
|
|
2017-01-17 08:22:56 +00:00
|
|
|
ERR_FAIL_COND_V(!peer_addr.is_valid(), ERR_UNCONFIGURED);
|
2016-10-18 21:53:18 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (sock_type == IP::TYPE_NONE)
|
2017-01-18 09:24:26 +00:00
|
|
|
sock_type = peer_addr.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
|
|
|
|
|
2016-11-30 19:45:19 +00:00
|
|
|
int sock = _get_socket();
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(sock == -1, FAILED);
|
2016-10-18 21:53:18 +00:00
|
|
|
struct sockaddr_storage addr;
|
2017-01-18 09:24:26 +00:00
|
|
|
size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, sock_type);
|
2014-11-12 14:23:23 +00:00
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
int err;
|
2014-11-13 03:53:12 +00:00
|
|
|
|
2017-03-08 16:14:01 +00:00
|
|
|
_set_sock_blocking(blocking);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while ((err = sendto(sock, p_buffer, p_buffer_size, 0, (struct sockaddr *)&addr, addr_size)) != p_buffer_size) {
|
2014-11-12 14:23:23 +00:00
|
|
|
|
|
|
|
if (errno != EAGAIN) {
|
|
|
|
return FAILED;
|
2017-03-08 16:14:01 +00:00
|
|
|
} else if (!blocking) {
|
|
|
|
return ERR_UNAVAILABLE;
|
2014-11-12 14:23:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int PacketPeerUDPPosix::get_max_packet_size() const {
|
2014-11-12 14:23:23 +00:00
|
|
|
|
|
|
|
return 512; // uhm maybe not
|
|
|
|
}
|
|
|
|
|
2017-08-12 11:04:30 +00:00
|
|
|
Error PacketPeerUDPPosix::listen(int p_port, const IP_Address &p_bind_address, int p_recv_buffer_size) {
|
2017-01-18 09:24:26 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(sockfd != -1, ERR_ALREADY_IN_USE);
|
|
|
|
ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER);
|
2017-01-18 09:24:26 +00:00
|
|
|
|
2017-01-18 16:17:44 +00:00
|
|
|
#ifdef __OpenBSD__
|
|
|
|
sock_type = IP::TYPE_IPV4; // OpenBSD does not support dual stacking, fallback to IPv4 only.
|
|
|
|
#else
|
2017-01-18 11:47:12 +00:00
|
|
|
sock_type = IP::TYPE_ANY;
|
2017-01-18 16:17:44 +00:00
|
|
|
#endif
|
2017-01-18 09:24:26 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_bind_address.is_valid())
|
2017-01-18 09:24:26 +00:00
|
|
|
sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
|
2016-10-18 21:53:18 +00:00
|
|
|
|
2016-11-30 19:45:19 +00:00
|
|
|
int sock = _get_socket();
|
2016-10-28 21:11:53 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (sock == -1)
|
2014-11-12 14:23:23 +00:00
|
|
|
return ERR_CANT_CREATE;
|
2016-10-18 21:53:18 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
sockaddr_storage addr = { 0 };
|
2017-01-18 09:24:26 +00:00
|
|
|
size_t addr_size = _set_listen_sockaddr(&addr, p_port, sock_type, IP_Address());
|
2016-10-18 21:53:18 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (bind(sock, (struct sockaddr *)&addr, addr_size) == -1) {
|
2014-11-12 14:23:23 +00:00
|
|
|
close();
|
|
|
|
return ERR_UNAVAILABLE;
|
|
|
|
}
|
2014-11-13 03:53:12 +00:00
|
|
|
rb.resize(nearest_shift(p_recv_buffer_size));
|
2014-11-12 14:23:23 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void PacketPeerUDPPosix::close() {
|
2014-11-12 14:23:23 +00:00
|
|
|
|
|
|
|
if (sockfd != -1)
|
|
|
|
::close(sockfd);
|
2017-03-05 15:44:50 +00:00
|
|
|
sockfd = -1;
|
2017-01-18 09:24:26 +00:00
|
|
|
sock_type = IP::TYPE_NONE;
|
2017-01-19 12:33:10 +00:00
|
|
|
rb.resize(16);
|
2017-03-05 15:44:50 +00:00
|
|
|
queue_count = 0;
|
2014-11-12 14:23:23 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 03:53:12 +00:00
|
|
|
Error PacketPeerUDPPosix::wait() {
|
|
|
|
|
|
|
|
return _poll(true);
|
|
|
|
}
|
|
|
|
|
2017-08-11 19:10:05 +00:00
|
|
|
Error PacketPeerUDPPosix::_poll(bool p_block) {
|
2014-11-12 14:23:23 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (sockfd == -1) {
|
2017-01-18 09:48:50 +00:00
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
|
2017-08-11 19:10:05 +00:00
|
|
|
_set_sock_blocking(p_block);
|
2017-03-08 16:14:01 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
struct sockaddr_storage from = { 0 };
|
2016-10-18 21:53:18 +00:00
|
|
|
socklen_t len = sizeof(struct sockaddr_storage);
|
2014-11-12 14:23:23 +00:00
|
|
|
int ret;
|
2017-03-08 16:14:01 +00:00
|
|
|
while ((ret = recvfrom(sockfd, recv_buffer, MIN((int)sizeof(recv_buffer), MAX(rb.space_left() - 24, 0)), 0, (struct sockaddr *)&from, &len)) > 0) {
|
2016-10-18 21:53:18 +00:00
|
|
|
|
|
|
|
uint32_t port = 0;
|
|
|
|
|
|
|
|
if (from.ss_family == AF_INET) {
|
2016-12-05 15:32:38 +00:00
|
|
|
uint8_t type = (uint8_t)IP::TYPE_IPV4;
|
2016-10-18 21:53:18 +00:00
|
|
|
rb.write(&type, 1);
|
2017-03-05 15:44:50 +00:00
|
|
|
struct sockaddr_in *sin_from = (struct sockaddr_in *)&from;
|
|
|
|
rb.write((uint8_t *)&sin_from->sin_addr, 4);
|
2016-10-26 16:27:01 +00:00
|
|
|
port = ntohs(sin_from->sin_port);
|
2016-10-18 21:53:18 +00:00
|
|
|
|
|
|
|
} else if (from.ss_family == AF_INET6) {
|
|
|
|
|
2016-12-05 15:32:38 +00:00
|
|
|
uint8_t type = (uint8_t)IP::TYPE_IPV6;
|
2016-10-18 21:53:18 +00:00
|
|
|
rb.write(&type, 1);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
struct sockaddr_in6 *s6_from = (struct sockaddr_in6 *)&from;
|
|
|
|
rb.write((uint8_t *)&s6_from->sin6_addr, 16);
|
2016-10-18 21:53:18 +00:00
|
|
|
|
2016-10-26 16:27:01 +00:00
|
|
|
port = ntohs(s6_from->sin6_port);
|
2016-10-18 21:53:18 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// WARN_PRINT("Ignoring packet with unknown address family");
|
2016-12-05 15:32:38 +00:00
|
|
|
uint8_t type = (uint8_t)IP::TYPE_NONE;
|
2016-10-18 21:53:18 +00:00
|
|
|
rb.write(&type, 1);
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
rb.write((uint8_t *)&port, 4);
|
|
|
|
rb.write((uint8_t *)&ret, 4);
|
2014-11-12 14:23:23 +00:00
|
|
|
rb.write(recv_buffer, ret);
|
2016-10-18 21:53:18 +00:00
|
|
|
|
2016-10-20 10:04:10 +00:00
|
|
|
len = sizeof(struct sockaddr_storage);
|
2014-11-12 14:23:23 +00:00
|
|
|
++queue_count;
|
2017-08-11 19:10:05 +00:00
|
|
|
if (p_block)
|
2017-05-05 15:41:11 +00:00
|
|
|
break;
|
2014-11-12 14:23:23 +00:00
|
|
|
};
|
|
|
|
|
2015-11-09 00:33:16 +00:00
|
|
|
// TODO: Should ECONNRESET be handled here?
|
2017-03-05 15:44:50 +00:00
|
|
|
if (ret == 0 || (ret == -1 && errno != EAGAIN)) {
|
2014-11-12 14:23:23 +00:00
|
|
|
close();
|
|
|
|
return FAILED;
|
|
|
|
};
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
bool PacketPeerUDPPosix::is_listening() const {
|
2014-11-12 14:23:23 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return sockfd != -1;
|
2014-11-12 14:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IP_Address PacketPeerUDPPosix::get_packet_address() const {
|
|
|
|
|
|
|
|
return packet_ip;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int PacketPeerUDPPosix::get_packet_port() const {
|
2014-11-12 14:23:23 +00:00
|
|
|
|
|
|
|
return packet_port;
|
|
|
|
}
|
|
|
|
|
2016-11-30 19:45:19 +00:00
|
|
|
int PacketPeerUDPPosix::_get_socket() {
|
2014-11-12 14:23:23 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(sock_type == IP::TYPE_NONE, -1);
|
2017-01-18 09:24:26 +00:00
|
|
|
|
2014-11-12 14:23:23 +00:00
|
|
|
if (sockfd != -1)
|
|
|
|
return sockfd;
|
|
|
|
|
2017-01-18 09:24:26 +00:00
|
|
|
sockfd = _socket_create(sock_type, SOCK_DGRAM, IPPROTO_UDP);
|
2014-11-12 14:23:23 +00:00
|
|
|
|
2017-03-08 16:14:01 +00:00
|
|
|
if (sockfd != -1)
|
|
|
|
_set_sock_blocking(false);
|
|
|
|
|
2014-11-12 14:23:23 +00:00
|
|
|
return sockfd;
|
|
|
|
}
|
|
|
|
|
2017-03-08 16:14:01 +00:00
|
|
|
void PacketPeerUDPPosix::_set_sock_blocking(bool p_blocking) {
|
|
|
|
|
|
|
|
if (sock_blocking == p_blocking)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sock_blocking = p_blocking;
|
|
|
|
|
|
|
|
#ifndef NO_FCNTL
|
|
|
|
int opts = fcntl(sockfd, F_GETFL);
|
|
|
|
int ret = 0;
|
|
|
|
if (sock_blocking)
|
|
|
|
ret = fcntl(sockfd, F_SETFL, opts & ~O_NONBLOCK);
|
|
|
|
else
|
|
|
|
ret = fcntl(sockfd, F_SETFL, opts | O_NONBLOCK);
|
|
|
|
if (ret == -1)
|
|
|
|
perror("setting non-block mode");
|
|
|
|
#else
|
|
|
|
int bval = sock_blocking ? 0 : 1;
|
|
|
|
if (ioctl(sockfd, FIONBIO, &bval) == -1)
|
|
|
|
perror("setting non-block mode");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void PacketPeerUDPPosix::set_dest_address(const IP_Address &p_address, int p_port) {
|
2014-11-12 14:23:23 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
peer_addr = p_address;
|
|
|
|
peer_port = p_port;
|
2014-11-12 14:23:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
PacketPeerUDP *PacketPeerUDPPosix::_create() {
|
2014-11-12 14:23:23 +00:00
|
|
|
|
|
|
|
return memnew(PacketPeerUDPPosix);
|
|
|
|
};
|
|
|
|
|
|
|
|
void PacketPeerUDPPosix::make_default() {
|
|
|
|
|
|
|
|
PacketPeerUDP::_create = PacketPeerUDPPosix::_create;
|
|
|
|
};
|
|
|
|
|
|
|
|
PacketPeerUDPPosix::PacketPeerUDPPosix() {
|
|
|
|
|
2017-03-08 16:14:01 +00:00
|
|
|
blocking = true;
|
|
|
|
sock_blocking = true;
|
2017-03-05 15:44:50 +00:00
|
|
|
sockfd = -1;
|
|
|
|
packet_port = 0;
|
|
|
|
queue_count = 0;
|
|
|
|
peer_port = 0;
|
2017-01-18 09:24:26 +00:00
|
|
|
sock_type = IP::TYPE_NONE;
|
2017-01-19 12:33:10 +00:00
|
|
|
rb.resize(16);
|
2014-11-12 14:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PacketPeerUDPPosix::~PacketPeerUDPPosix() {
|
|
|
|
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
#endif
|