summaryrefslogtreecommitdiff
path: root/drivers/unix
diff options
context:
space:
mode:
authorFabio Alessandrelli <fabio.alessandrelli@gmail.com>2017-01-18 10:24:26 +0100
committerFabio Alessandrelli <fabio.alessandrelli@gmail.com>2017-01-23 20:18:18 +0100
commit2fe4ef66991b483640f59873c22b3af671626ccc (patch)
tree2ddc072d59d6f8ceed594b669e6dfab5b1bf6c29 /drivers/unix
parentb2839343cab66880f647c77da7b2e1826761776a (diff)
Implement UDP listen bind address
Diffstat (limited to 'drivers/unix')
-rw-r--r--drivers/unix/packet_peer_udp_posix.cpp24
-rw-r--r--drivers/unix/packet_peer_udp_posix.h3
2 files changed, 21 insertions, 6 deletions
diff --git a/drivers/unix/packet_peer_udp_posix.cpp b/drivers/unix/packet_peer_udp_posix.cpp
index f8a0f556b4..15c3b7dfe9 100644
--- a/drivers/unix/packet_peer_udp_posix.cpp
+++ b/drivers/unix/packet_peer_udp_posix.cpp
@@ -98,10 +98,13 @@ Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer,int p_buffer_size){
ERR_FAIL_COND_V(!peer_addr.is_valid(), ERR_UNCONFIGURED);
+ if (sock_type==IP::TYPE_NONE)
+ sock_type = peer_addr.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
+
int sock = _get_socket();
ERR_FAIL_COND_V( sock == -1, FAILED );
struct sockaddr_storage addr;
- size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, ip_type);
+ size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, sock_type);
errno = 0;
int err;
@@ -121,16 +124,23 @@ 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, IP_Address p_bind_address, int p_recv_buffer_size) {
+
+ 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);
+
+ sock_type = ip_type;
+
+ if(p_bind_address.is_valid())
+ sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
- close();
int sock = _get_socket();
if (sock == -1 )
return ERR_CANT_CREATE;
sockaddr_storage addr = {0};
- size_t addr_size = _set_listen_sockaddr(&addr, p_port, ip_type, IP_Address());
+ size_t addr_size = _set_listen_sockaddr(&addr, p_port, sock_type, IP_Address());
if (bind(sock, (struct sockaddr*)&addr, addr_size) == -1 ) {
close();
@@ -145,6 +155,7 @@ void PacketPeerUDPPosix::close(){
if (sockfd != -1)
::close(sockfd);
sockfd=-1;
+ sock_type = IP::TYPE_NONE;
rb.resize(8);
queue_count=0;
}
@@ -225,10 +236,12 @@ int PacketPeerUDPPosix::get_packet_port() const{
int PacketPeerUDPPosix::_get_socket() {
+ ERR_FAIL_COND_V(sock_type==IP::TYPE_NONE, -1);
+
if (sockfd != -1)
return sockfd;
- sockfd = _socket_create(ip_type, SOCK_DGRAM, IPPROTO_UDP);
+ sockfd = _socket_create(sock_type, SOCK_DGRAM, IPPROTO_UDP);
return sockfd;
}
@@ -257,6 +270,7 @@ PacketPeerUDPPosix::PacketPeerUDPPosix() {
packet_port=0;
queue_count=0;
peer_port=0;
+ sock_type = IP::TYPE_NONE;
ip_type = IP::TYPE_ANY;
}
diff --git a/drivers/unix/packet_peer_udp_posix.h b/drivers/unix/packet_peer_udp_posix.h
index ead7174a9d..ac68344d78 100644
--- a/drivers/unix/packet_peer_udp_posix.h
+++ b/drivers/unix/packet_peer_udp_posix.h
@@ -48,6 +48,7 @@ class PacketPeerUDPPosix : public PacketPeerUDP {
mutable int packet_port;
mutable int queue_count;
int sockfd;
+ IP::Type sock_type;
IP_Address peer_addr;
int peer_port;
@@ -65,7 +66,7 @@ public:
virtual int get_max_packet_size() const;
- virtual Error listen(int p_port, int p_recv_buffer_size=65536);
+ virtual Error listen(int p_port, IP_Address p_bind_address=IP_Address("*"), int p_recv_buffer_size=65536);
virtual void close();
virtual Error wait();
virtual bool is_listening() const;