summaryrefslogtreecommitdiff
path: root/drivers/unix/packet_peer_udp_posix.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/unix/packet_peer_udp_posix.cpp')
-rw-r--r--drivers/unix/packet_peer_udp_posix.cpp107
1 files changed, 85 insertions, 22 deletions
diff --git a/drivers/unix/packet_peer_udp_posix.cpp b/drivers/unix/packet_peer_udp_posix.cpp
index 2111619080..6adb3eea70 100644
--- a/drivers/unix/packet_peer_udp_posix.cpp
+++ b/drivers/unix/packet_peer_udp_posix.cpp
@@ -1,3 +1,31 @@
+/*************************************************************************/
+/* packet_peer_udp_posix.cpp */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* http://www.godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
+/* */
+/* 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. */
+/*************************************************************************/
#include "packet_peer_udp_posix.h"
#ifdef UNIX_ENABLED
@@ -26,6 +54,7 @@
#include <arpa/inet.h>
#endif
+#include "drivers/unix/socket_helpers.h"
int PacketPeerUDPPosix::get_available_packet_count() const {
@@ -45,7 +74,17 @@ Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer,int &r_buffer_size
return ERR_UNAVAILABLE;
uint32_t size;
- rb.read((uint8_t*)&packet_ip.host,4,true);
+ uint8_t type;
+ rb.read(&type, 1, true);
+ if (type == IP::TYPE_IPV4) {
+ uint8_t ip[4];
+ rb.read(ip,4,true);
+ packet_ip.set_ipv4(ip);
+ } else {
+ uint8_t ipv6[16];
+ rb.read(ipv6,16,true);
+ packet_ip.set_ipv6(ipv6);
+ };
rb.read((uint8_t*)&packet_port,4,true);
rb.read((uint8_t*)&size,4,true);
rb.read(packet_buffer,size,true);
@@ -57,17 +96,17 @@ Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer,int &r_buffer_size
}
Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer,int p_buffer_size){
+ ERR_FAIL_COND_V(peer_addr == IP_Address(), ERR_UNCONFIGURED);
+
int sock = _get_socket();
ERR_FAIL_COND_V( sock == -1, FAILED );
- struct sockaddr_in addr;
- addr.sin_family = AF_INET;
- addr.sin_port = htons(peer_port);
- addr.sin_addr = *((struct in_addr*)&peer_addr.host);
+ struct sockaddr_storage addr;
+ size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, ip_type);
errno = 0;
int err;
- while ( (err = sendto(sock, p_buffer, p_buffer_size, 0, (struct sockaddr*)&addr, sizeof(addr))) != p_buffer_size) {
+ while ( (err = sendto(sock, p_buffer, p_buffer_size, 0, (struct sockaddr*)&addr, addr_size)) != p_buffer_size) {
if (errno != EAGAIN) {
return FAILED;
@@ -82,21 +121,21 @@ int PacketPeerUDPPosix::get_max_packet_size() const{
return 512; // uhm maybe not
}
-Error PacketPeerUDPPosix::listen(int p_port, int p_recv_buffer_size){
+Error PacketPeerUDPPosix::listen(int p_port, int p_recv_buffer_size) {
close();
int sock = _get_socket();
+
if (sock == -1 )
return ERR_CANT_CREATE;
- sockaddr_in addr = {0};
- addr.sin_family = AF_INET;
- addr.sin_port = htons(p_port);
- addr.sin_addr.s_addr = INADDR_ANY;
- if (bind(sock, (struct sockaddr*)&addr, sizeof(sockaddr_in)) == -1 ) {
+
+ sockaddr_storage addr = {0};
+ size_t addr_size = _set_listen_sockaddr(&addr, p_port, ip_type, NULL);
+
+ if (bind(sock, (struct sockaddr*)&addr, addr_size) == -1 ) {
close();
return ERR_UNAVAILABLE;
}
- printf("UDP Connection listening on port %i bufsize %i \n", p_port,p_recv_buffer_size);
rb.resize(nearest_shift(p_recv_buffer_size));
return OK;
}
@@ -118,16 +157,41 @@ Error PacketPeerUDPPosix::wait() {
Error PacketPeerUDPPosix::_poll(bool p_wait) {
- struct sockaddr_in from = {0};
- socklen_t len = sizeof(struct sockaddr_in);
+ struct sockaddr_storage from = {0};
+ socklen_t len = sizeof(struct sockaddr_storage);
int ret;
while ( (ret = recvfrom(sockfd, recv_buffer, MIN((int)sizeof(recv_buffer),MAX(rb.space_left()-12, 0)), p_wait?0:MSG_DONTWAIT, (struct sockaddr*)&from, &len)) > 0) {
- rb.write((uint8_t*)&from.sin_addr, 4);
- uint32_t port = ntohs(from.sin_port);
+
+ uint32_t port = 0;
+
+ if (from.ss_family == AF_INET) {
+ uint8_t type = (uint8_t)IP::TYPE_IPV4;
+ rb.write(&type, 1);
+ struct sockaddr_in* sin_from = (struct sockaddr_in*)&from;
+ rb.write((uint8_t*)&sin_from->sin_addr, 4);
+ port = ntohs(sin_from->sin_port);
+
+ } else if (from.ss_family == AF_INET6) {
+
+ uint8_t type = (uint8_t)IP::TYPE_IPV6;
+ rb.write(&type, 1);
+
+ struct sockaddr_in6* s6_from = (struct sockaddr_in6*)&from;
+ rb.write((uint8_t*)&s6_from->sin6_addr, 16);
+
+ port = ntohs(s6_from->sin6_port);
+
+ } else {
+ // WARN_PRINT("Ignoring packet with unknown address family");
+ uint8_t type = (uint8_t)IP::TYPE_NONE;
+ rb.write(&type, 1);
+ };
+
rb.write((uint8_t*)&port, 4);
rb.write((uint8_t*)&ret, 4);
rb.write(recv_buffer, ret);
- len = sizeof(struct sockaddr_in);
+
+ len = sizeof(struct sockaddr_storage);
++queue_count;
};
@@ -160,15 +224,13 @@ int PacketPeerUDPPosix::_get_socket() {
if (sockfd != -1)
return sockfd;
- sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- ERR_FAIL_COND_V( sockfd == -1, -1 );
- //fcntl(sockfd, F_SETFL, O_NONBLOCK);
+ sockfd = _socket_create(ip_type, SOCK_DGRAM, IPPROTO_UDP);
return sockfd;
}
-void PacketPeerUDPPosix::set_send_address(const IP_Address& p_address,int p_port) {
+void PacketPeerUDPPosix::set_dest_address(const IP_Address& p_address,int p_port) {
peer_addr=p_address;
peer_port=p_port;
@@ -191,6 +253,7 @@ PacketPeerUDPPosix::PacketPeerUDPPosix() {
packet_port=0;
queue_count=0;
peer_port=0;
+ ip_type = IP::TYPE_ANY;
}
PacketPeerUDPPosix::~PacketPeerUDPPosix() {