Various UDP socket fixes

This commit is contained in:
Thulinma 2017-01-19 21:05:20 +01:00
parent e8995f2457
commit dd8eed7920
2 changed files with 63 additions and 30 deletions

View file

@ -21,6 +21,17 @@
#define SOCKETSIZE 51200ul
#endif
/// Local-scope only helper function that prints address families
static const char* addrFam(int f){
switch(f){
case AF_UNSPEC: return "Unspecified";
case AF_INET: return "IPv4";
case AF_INET6: return "IPv6";
case PF_UNIX: return "Unix";
default: return "???";
}
}
/// Checks bytes (length len) containing a binary-encoded IPv4 or IPv6 IP address, and writes it in human-readable notation to target.
/// Writes "unknown" if it cannot decode to a sensible value.
void Socket::hostBytesToStr(const char *bytes, size_t len, std::string &target){
@ -940,6 +951,7 @@ Socket::UDPConnection::UDPConnection(const UDPConnection &o){
down = 0;
if (o.destAddr && o.destAddr_size){
destAddr = malloc(o.destAddr_size);
destAddr_size = o.destAddr_size;
if (destAddr){memcpy(destAddr, o.destAddr, o.destAddr_size);}
}else{
destAddr = 0;
@ -1015,6 +1027,7 @@ void Socket::UDPConnection::SetDestination(std::string destIp, uint32_t port){
close();
family = rp->ai_family;
sock = socket(family, SOCK_DGRAM, 0);
HIGH_MSG("Set UDP destination: %s:%d (%s)", destIp.c_str(), port, addrFam(family));
freeaddrinfo(result);
return;
//\todo Possibly detect and handle failure
@ -1103,15 +1116,20 @@ void Socket::UDPConnection::SendNow(const char *sdata, size_t len){
/// \arg multicastInterfaces Comma-separated list of interfaces to listen on for multicast packets. Optional, left out means automatically chosen
/// by kernel.
/// \return Actually bound port number, or zero on error.
int Socket::UDPConnection::bind(int port, std::string iface, const std::string &multicastInterfaces){
uint16_t Socket::UDPConnection::bind(int port, std::string iface, const std::string &multicastInterfaces){
close(); // we open a new socket for each attempt
int result = 0;
int addr_ret;
bool multicast = false;
struct addrinfo hints, *addr_result, *rp;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_PASSIVE;
hints.ai_family = family;
hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE | AI_V4MAPPED;
if (destAddr && destAddr_size){
hints.ai_family = ((struct sockaddr_in *)destAddr)->sin_family;
}else{
hints.ai_family = AF_UNSPEC;
}
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
@ -1135,25 +1153,11 @@ int Socket::UDPConnection::bind(int port, std::string iface, const std::string &
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sock == -1){continue;}
char human_addr[INET6_ADDRSTRLEN];
getnameinfo(rp->ai_addr, rp->ai_addrlen, human_addr, INET6_ADDRSTRLEN, 0, 0, NI_NUMERICHOST);
MEDIUM_MSG("Attempting bind to %s (%s)", human_addr, rp->ai_family == AF_INET6 ? "IPv6" : "IPv4");
if (::bind(sock, rp->ai_addr, rp->ai_addrlen) == 0){
char human_port[16];
getnameinfo(rp->ai_addr, rp->ai_addrlen, human_addr, INET6_ADDRSTRLEN, human_port, 16, NI_NUMERICHOST | NI_NUMERICSERV);
MEDIUM_MSG("Attempting bind to %s:%s (%s)", human_addr, human_port, addrFam(rp->ai_family));
family = rp->ai_family;
hints.ai_family = family;
break;
}
if (err_str.size()){err_str += ", ";}
err_str += human_addr;
err_str += ":";
err_str += strerror(errno);
close(); // we open a new socket for each attempt
}
if (sock == -1){
FAIL_MSG("Could not open %s for UDP: %s", iface.c_str(), err_str.c_str());
freeaddrinfo(addr_result);
return 0;
}
// socket is bound! Let's collect some more data...
if (family == AF_INET6){
sockaddr_in6 *addr6 = (sockaddr_in6 *)(rp->ai_addr);
result = ntohs(addr6->sin6_port);
@ -1170,7 +1174,26 @@ int Socket::UDPConnection::bind(int port, std::string iface, const std::string &
// multicast has a "1110" bit prefix
multicast = (((char *)&(addr4->sin_addr))[0] & 0xF0) == 0xE0;
}
if (multicast){
const int optval = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
WARN_MSG("Could not set multicast UDP socket re-use!");
}
}
if (::bind(sock, rp->ai_addr, rp->ai_addrlen) == 0){
break;
}
if (err_str.size()){err_str += ", ";}
err_str += human_addr;
err_str += ":";
err_str += strerror(errno);
close(); // we open a new socket for each attempt
}
freeaddrinfo(addr_result);
if (sock == -1){
FAIL_MSG("Could not open %s for UDP: %s", iface.c_str(), err_str.c_str());
return 0;
}
// handle multicast membership(s)
if (multicast){
@ -1182,8 +1205,7 @@ int Socket::UDPConnection::bind(int port, std::string iface, const std::string &
if ((addr_ret = getaddrinfo(iface.c_str(), 0, &hints, &resmulti)) != 0){
WARN_MSG("Unable to parse multicast address: %s", gai_strerror(addr_ret));
close();
result = -1;
return result;
return 0;
}
if (!multicastInterfaces.length()){
@ -1254,7 +1276,18 @@ int Socket::UDPConnection::bind(int port, std::string iface, const std::string &
}
freeaddrinfo(resmulti); // free resolved multicast addr
}
return result;
//get port number
struct sockaddr_storage fin_addr;
socklen_t alen = sizeof(fin_addr);
if (getsockname(sock, (struct sockaddr*)&fin_addr, &alen) == 0){
if (family == AF_INET6){
return ntohs(((struct sockaddr_in6*)&fin_addr)->sin6_port);
}else{
return ntohs(((struct sockaddr_in*)&fin_addr)->sin_port);
}
}else{
return 0;
}
}
/// Attempt to receive a UDP packet.

View file

@ -142,7 +142,7 @@ namespace Socket{
~UDPConnection();
void close();
int getSock();
int bind(int port, std::string iface = "", const std::string &multicastAddress = "");
uint16_t bind(int port, std::string iface = "", const std::string &multicastAddress = "");
void setBlocking(bool blocking);
void SetDestination(std::string hostname, uint32_t port);
void GetDestination(std::string &hostname, uint32_t &port);