From 069ae2c8555e9b5723f6a8adb8c6e751cb5fda6b Mon Sep 17 00:00:00 2001 From: Thulinma Date: Tue, 18 Feb 2014 16:57:03 +0100 Subject: [PATCH] Added GetDestination UDPConnection call, changed behaviour to automatically set destination to source of last received packet. --- lib/socket.cpp | 44 ++++++++++++++++++++++++++++++++++---------- lib/socket.h | 1 + 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/lib/socket.cpp b/lib/socket.cpp index 0b4dac6e..7e0cac1a 100644 --- a/lib/socket.cpp +++ b/lib/socket.cpp @@ -966,7 +966,7 @@ Socket::UDPConnection::~UDPConnection(){ } } -/// Stores the properties of the reiving end of this UDP socket. +/// Stores the properties of the receiving end of this UDP socket. /// This will be the receiving end for all SendNow calls. void Socket::UDPConnection::SetDestination(std::string destIp, uint32_t port){ if (destAddr){ @@ -982,24 +982,47 @@ void Socket::UDPConnection::SetDestination(std::string destIp, uint32_t port){ if(inet_pton(AF_INET6, destIp.c_str(), &(((struct sockaddr_in6*)destAddr)->sin6_addr)) == 1){ return; } - free(destAddr); - destAddr = 0; - } - destAddr = malloc(sizeof(struct sockaddr_in)); - if (destAddr){ - destAddr_size = sizeof(struct sockaddr_in); memset(destAddr, 0, destAddr_size); ((struct sockaddr_in*)destAddr)->sin_family = AF_INET; ((struct sockaddr_in*)destAddr)->sin_port = htons(port); if(inet_pton(AF_INET, destIp.c_str(), &(((struct sockaddr_in*)destAddr)->sin_addr)) == 1){ return; } - free(destAddr); - destAddr = 0; } + free(destAddr); + destAddr = 0; DEBUG_MSG(DLVL_FAIL, "Could not set destination for UDP socket: %s:%d", destIp.c_str(), port); }//Socket::UDPConnection SetDestination +/// Gets the properties of the receiving end of this UDP socket. +/// This will be the receiving end for all SendNow calls. +void Socket::UDPConnection::GetDestination(std::string & destIp, uint32_t & port){ + if (!destAddr || !destAddr_size){ + destIp = ""; + port = 0; + return; + } + char addr_str[INET6_ADDRSTRLEN+1]; + addr_str[INET6_ADDRSTRLEN] = 0;//set last byte to zero, to prevent walking out of the array + if (((struct sockaddr_in*)destAddr)->sin_family == AF_INET6){ + if (inet_ntop(AF_INET6, &(((struct sockaddr_in6*)destAddr)->sin6_addr), addr_str, INET6_ADDRSTRLEN) != 0){ + destIp = addr_str; + port = ntohs(((struct sockaddr_in6*)destAddr)->sin6_port); + return; + } + } + if (((struct sockaddr_in*)destAddr)->sin_family == AF_INET){ + if (inet_ntop(AF_INET, &(((struct sockaddr_in*)destAddr)->sin_addr), addr_str, INET6_ADDRSTRLEN) != 0){ + destIp = addr_str; + port = ntohs(((struct sockaddr_in*)destAddr)->sin_port); + return; + } + } + destIp = ""; + port = 0; + DEBUG_MSG(DLVL_FAIL, "Could not get destination for UDP socket"); +}//Socket::UDPConnection GetDestination + /// Sets the socket to be blocking if the parameters is true. /// Sets the socket to be non-blocking otherwise. void Socket::UDPConnection::setBlocking(bool blocking){ @@ -1074,7 +1097,8 @@ bool Socket::UDPConnection::Receive(){ data_size = 0; } } - r = recvfrom(sock, data, data_size, 0, 0, 0); + socklen_t destsize = destAddr_size; + r = recvfrom(sock, data, data_size, 0, (sockaddr*)destAddr, &destsize); if (r > 0){ down += r; data_len = r; diff --git a/lib/socket.h b/lib/socket.h index d01175e8..fa34ed3d 100644 --- a/lib/socket.h +++ b/lib/socket.h @@ -137,6 +137,7 @@ namespace Socket { int bind(int port); void setBlocking(bool blocking); void SetDestination(std::string hostname, uint32_t port); + void GetDestination(std::string & hostname, uint32_t & port); bool Receive(); void SendNow(const std::string & data); void SendNow(const char * data);