Merge branch 'development' into LTS_development
# Conflicts: # src/input/input.cpp
This commit is contained in:
commit
44fd455c8e
9 changed files with 341 additions and 111 deletions
|
@ -4,6 +4,82 @@
|
|||
#include "http_parser.h"
|
||||
#include "encode.h"
|
||||
#include "timing.h"
|
||||
#include "defines.h"
|
||||
|
||||
/// Helper function to check if the given c-string is numeric or not
|
||||
static bool is_numeric(const char * str){
|
||||
while (str != 0){
|
||||
if (str[0] < 48 || str[0] > 57){return false;}
|
||||
++str;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
///Constructor that does the actual parsing
|
||||
HTTP::URL::URL(const std::string & url){
|
||||
//first detect protocol at the start, if any
|
||||
size_t proto_sep = url.find("://");
|
||||
if (proto_sep != std::string::npos){
|
||||
protocol = url.substr(0, proto_sep);
|
||||
proto_sep += 3;
|
||||
}else{
|
||||
proto_sep = 0;
|
||||
}
|
||||
//proto_sep now points to the start of the host, guaranteed
|
||||
//continue by finding the path, if any
|
||||
size_t first_slash = url.find('/', proto_sep);
|
||||
if (first_slash != std::string::npos){
|
||||
path = url.substr(first_slash+1);
|
||||
}
|
||||
//host and port are now definitely between proto_sep and first_slash
|
||||
//we check for [ at the start because we may have an IPv6 address as host
|
||||
if (url[proto_sep] == '['){
|
||||
//IPv6 address - find matching brace
|
||||
size_t closing_brace = url.find(']', proto_sep);
|
||||
//check if it exists at all
|
||||
if (closing_brace == std::string::npos || closing_brace > first_slash){
|
||||
//assume host ends at first slash if there is no closing brace before it
|
||||
closing_brace = first_slash;
|
||||
}
|
||||
host = url.substr(proto_sep+1, closing_brace-(proto_sep+1));
|
||||
//continue by finding port, if any
|
||||
size_t colon = url.rfind(':', first_slash);
|
||||
if (colon == std::string::npos || colon <= closing_brace){
|
||||
//no port. Assume 80
|
||||
port = "80";
|
||||
}else{
|
||||
//we have a port number, read it
|
||||
port = url.substr(colon+1, first_slash-(colon+1));
|
||||
}
|
||||
}else{
|
||||
//"normal" host - first find port, if any
|
||||
size_t colon = url.rfind(':', first_slash);
|
||||
if (colon == std::string::npos || colon < proto_sep){
|
||||
//no port. Assume 80
|
||||
port = "80";
|
||||
host = url.substr(proto_sep, first_slash-proto_sep);
|
||||
}else{
|
||||
//we have a port number, read it
|
||||
port = url.substr(colon+1, first_slash-(colon+1));
|
||||
host = url.substr(proto_sep, colon-proto_sep);
|
||||
}
|
||||
}
|
||||
//if the host is numeric, assume it is a port, instead
|
||||
if (is_numeric(host.c_str())){
|
||||
port = host;
|
||||
host = "";
|
||||
}
|
||||
EXTREME_MSG("URL host: %s", host.c_str());
|
||||
EXTREME_MSG("URL protocol: %s", protocol.c_str());
|
||||
EXTREME_MSG("URL port: %s", port.c_str());
|
||||
EXTREME_MSG("URL path: %s", path.c_str());
|
||||
}
|
||||
|
||||
///Returns the port in numeric format
|
||||
uint32_t HTTP::URL::getPort() const{
|
||||
if (!port.size()){return 80;}
|
||||
return atoi(port.c_str());
|
||||
}
|
||||
|
||||
/// This constructor creates an empty HTTP::Parser, ready for use for either reading or writing.
|
||||
/// All this constructor does is call HTTP::Parser::Clean().
|
||||
|
@ -447,7 +523,7 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer) {
|
|||
tmpA.erase(0, f + 1);
|
||||
method = tmpA;
|
||||
if (url.find('?') != std::string::npos) {
|
||||
parseVars(url.substr(url.find('?') + 1)); //parse GET variables
|
||||
parseVars(url.substr(url.find('?') + 1), vars); //parse GET variables
|
||||
url.erase(url.find('?'));
|
||||
}
|
||||
url = Encodings::URL::decode(url);
|
||||
|
@ -463,7 +539,7 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer) {
|
|||
tmpA.erase(0, f + 1);
|
||||
protocol = tmpA;
|
||||
if (url.find('?') != std::string::npos) {
|
||||
parseVars(url.substr(url.find('?') + 1)); //parse GET variables
|
||||
parseVars(url.substr(url.find('?') + 1), vars); //parse GET variables
|
||||
url.erase(url.find('?'));
|
||||
}
|
||||
url = Encodings::URL::decode(url);
|
||||
|
@ -508,7 +584,7 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer) {
|
|||
HTTPbuffer.erase(0, toappend);
|
||||
}
|
||||
if (length == body.length()) {
|
||||
parseVars(body); //parse POST variables
|
||||
parseVars(body, vars); //parse POST variables
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
|
@ -560,12 +636,12 @@ bool HTTP::Parser::parse(std::string & HTTPbuffer) {
|
|||
return false; //empty input
|
||||
} //HTTPReader::parse
|
||||
|
||||
/// Parses GET or POST-style variable data.
|
||||
/// Saves to internal variable structure using HTTP::Parser::SetVar.
|
||||
void HTTP::Parser::parseVars(std::string data) {
|
||||
///HTTP variable parser to std::map<std::string, std::string> structure.
|
||||
///Reads variables from data, decodes and stores them to storage.
|
||||
void HTTP::parseVars(const std::string & data, std::map<std::string, std::string> & storage) {
|
||||
std::string varname;
|
||||
std::string varval;
|
||||
// position where a part start (e.g. after &)
|
||||
// position where a part starts (e.g. after &)
|
||||
size_t pos = 0;
|
||||
while (pos < data.length()) {
|
||||
size_t nextpos = data.find('&', pos);
|
||||
|
@ -582,7 +658,9 @@ void HTTP::Parser::parseVars(std::string data) {
|
|||
varname = data.substr(pos, nextpos - pos);
|
||||
varval.clear();
|
||||
}
|
||||
SetVar(Encodings::URL::decode(varname), Encodings::URL::decode(varval));
|
||||
if (varname.size()){
|
||||
storage[Encodings::URL::decode(varname)] = Encodings::URL::decode(varval);
|
||||
}
|
||||
if (nextpos == std::string::npos) {
|
||||
// in case the string is gigantic
|
||||
break;
|
||||
|
|
|
@ -10,6 +10,12 @@
|
|||
|
||||
/// Holds all HTTP processing related code.
|
||||
namespace HTTP {
|
||||
|
||||
///HTTP variable parser to std::map<std::string, std::string> structure.
|
||||
///Reads variables from data, decodes and stores them to storage.
|
||||
void parseVars(const std::string & data, std::map<std::string, std::string> & storage);
|
||||
|
||||
|
||||
/// Simple class for reading and writing HTTP 1.0 and 1.1.
|
||||
class Parser {
|
||||
public:
|
||||
|
@ -56,13 +62,22 @@ namespace HTTP {
|
|||
bool getChunks;
|
||||
unsigned int doingChunk;
|
||||
bool parse(std::string & HTTPbuffer);
|
||||
void parseVars(std::string data);
|
||||
std::string builder;
|
||||
std::string read_buffer;
|
||||
std::map<std::string, std::string> headers;
|
||||
std::map<std::string, std::string> vars;
|
||||
void Trim(std::string & s);
|
||||
};
|
||||
//HTTP::Parser class
|
||||
|
||||
///URL parsing class. Parses full URL into its subcomponents
|
||||
class URL {
|
||||
public:
|
||||
URL(const std::string & url);
|
||||
uint32_t getPort() const;
|
||||
std::string host;///< Hostname or IP address of URL
|
||||
std::string protocol;///<Protocol of URL
|
||||
std::string port;///<Port of URL
|
||||
std::string path;///<Path after the first slash, not inclusive
|
||||
};
|
||||
|
||||
}//HTTP namespace
|
||||
|
|
|
@ -721,6 +721,11 @@ namespace IPC {
|
|||
return data[172];
|
||||
}
|
||||
|
||||
///\brief Gets PID field
|
||||
uint32_t statExchange::getPID() {
|
||||
return *(uint32_t*)(data+173);
|
||||
}
|
||||
|
||||
///\brief Creates a semaphore guard, locks the semaphore on call
|
||||
semGuard::semGuard(semaphore * thisSemaphore) : mySemaphore(thisSemaphore) {
|
||||
mySemaphore->wait();
|
||||
|
|
|
@ -40,6 +40,7 @@ namespace IPC {
|
|||
char getSync();
|
||||
void setSync(char s);
|
||||
unsigned int crc();
|
||||
uint32_t getPID();
|
||||
private:
|
||||
///\brief The payload for the stat exchange
|
||||
/// - 8 byte - now (timestamp of last statistics)
|
||||
|
|
265
lib/socket.cpp
265
lib/socket.cpp
|
@ -377,10 +377,6 @@ Socket::Connection::Connection(std::string host, int port, bool nonblock) {
|
|||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_flags = AI_ADDRCONFIG;
|
||||
hints.ai_protocol = 0;
|
||||
hints.ai_canonname = NULL;
|
||||
hints.ai_addr = NULL;
|
||||
hints.ai_next = NULL;
|
||||
int s = getaddrinfo(host.c_str(), ss.str().c_str(), &hints, &result);
|
||||
if (s != 0) {
|
||||
DEBUG_MSG(DLVL_FAIL, "Could not connect to %s:%i! Error: %s", host.c_str(), port, gai_strerror(s));
|
||||
|
@ -975,14 +971,14 @@ int Socket::Server::getSocket() {
|
|||
Socket::UDPConnection::UDPConnection(bool nonblock) {
|
||||
#ifdef __CYGWIN__
|
||||
#warning UDP over IPv6 is currently disabled on windows
|
||||
isIPv6 = false;
|
||||
family = AF_INET;
|
||||
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
#else
|
||||
isIPv6 = true;
|
||||
family = AF_INET6;
|
||||
sock = socket(AF_INET6, SOCK_DGRAM, 0);
|
||||
if (sock == -1) {
|
||||
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
isIPv6 = false;
|
||||
family = AF_INET;
|
||||
}
|
||||
#endif
|
||||
if (sock == -1) {
|
||||
|
@ -1005,14 +1001,14 @@ Socket::UDPConnection::UDPConnection(bool nonblock) {
|
|||
Socket::UDPConnection::UDPConnection(const UDPConnection & o) {
|
||||
#ifdef __CYGWIN__
|
||||
#warning UDP over IPv6 is currently disabled on windows
|
||||
isIPv6 = false;
|
||||
family = AF_INET;
|
||||
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
#else
|
||||
isIPv6 = true;
|
||||
family = AF_INET6;
|
||||
sock = socket(AF_INET6, SOCK_DGRAM, 0);
|
||||
if (sock == -1) {
|
||||
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
isIPv6 = false;
|
||||
family = AF_INET;
|
||||
}
|
||||
#endif
|
||||
if (sock == -1) {
|
||||
|
@ -1038,14 +1034,19 @@ Socket::UDPConnection::UDPConnection(const UDPConnection & o) {
|
|||
data_len = 0;
|
||||
}
|
||||
|
||||
/// Closes the UDP socket, cleans up any memory allocated by the socket.
|
||||
Socket::UDPConnection::~UDPConnection() {
|
||||
/// Close the UDP socket
|
||||
void Socket::UDPConnection::close(){
|
||||
if (sock != -1) {
|
||||
errno = EINTR;
|
||||
while (::close(sock) != 0 && errno == EINTR) {
|
||||
}
|
||||
sock = -1;
|
||||
}
|
||||
}
|
||||
|
||||
/// Closes the UDP socket, cleans up any memory allocated by the socket.
|
||||
Socket::UDPConnection::~UDPConnection() {
|
||||
close();
|
||||
if (destAddr) {
|
||||
free(destAddr);
|
||||
destAddr = 0;
|
||||
|
@ -1064,21 +1065,38 @@ void Socket::UDPConnection::SetDestination(std::string destIp, uint32_t port) {
|
|||
destAddr = 0;
|
||||
}
|
||||
destAddr = malloc(sizeof(struct sockaddr_in6));
|
||||
if (destAddr) {
|
||||
destAddr_size = sizeof(struct sockaddr_in6);
|
||||
memset(destAddr, 0, destAddr_size);
|
||||
((struct sockaddr_in6 *)destAddr)->sin6_family = AF_INET6;
|
||||
((struct sockaddr_in6 *)destAddr)->sin6_port = htons(port);
|
||||
if (inet_pton(AF_INET6, destIp.c_str(), &(((struct sockaddr_in6 *)destAddr)->sin6_addr)) == 1) {
|
||||
return;
|
||||
}
|
||||
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;
|
||||
}
|
||||
if (!destAddr) {
|
||||
return;
|
||||
}
|
||||
destAddr_size = sizeof(struct sockaddr_in6);
|
||||
memset(destAddr, 0, destAddr_size);
|
||||
|
||||
struct addrinfo * result, *rp, hints;
|
||||
std::stringstream ss;
|
||||
ss << port;
|
||||
|
||||
memset(&hints, 0, sizeof(struct addrinfo));
|
||||
hints.ai_family = family;
|
||||
hints.ai_socktype = SOCK_DGRAM;
|
||||
hints.ai_flags = AI_ADDRCONFIG;
|
||||
hints.ai_protocol = 0;
|
||||
hints.ai_canonname = NULL;
|
||||
hints.ai_addr = NULL;
|
||||
hints.ai_next = NULL;
|
||||
int s = getaddrinfo(destIp.c_str(), ss.str().c_str(), &hints, &result);
|
||||
if (s != 0) {
|
||||
DEBUG_MSG(DLVL_FAIL, "Could not connect UDP socket to %s:%i! Error: %s", destIp.c_str(), port, gai_strerror(s));
|
||||
return;
|
||||
}
|
||||
|
||||
for (rp = result; rp != NULL; rp = rp->ai_next) {
|
||||
//assume success
|
||||
memcpy(destAddr, rp->ai_addr, rp->ai_addrlen);
|
||||
freeaddrinfo(result);
|
||||
return;
|
||||
//\todo Possibly detect and handle failure
|
||||
}
|
||||
freeaddrinfo(result);
|
||||
free(destAddr);
|
||||
destAddr = 0;
|
||||
DEBUG_MSG(DLVL_FAIL, "Could not set destination for UDP socket: %s:%d", destIp.c_str(), port);
|
||||
|
@ -1164,64 +1182,163 @@ void Socket::UDPConnection::SendNow(const char * sdata, size_t len) {
|
|||
}
|
||||
|
||||
/// Bind to a port number, returning the bound port.
|
||||
/// Attempts to bind over IPv6 first.
|
||||
/// If it fails, attempts to bind over IPv4.
|
||||
/// If that fails too, gives up and returns zero.
|
||||
/// Prints a debug message at DLVL_FAIL level if binding failed.
|
||||
/// If that fails, returns zero.
|
||||
/// \arg port Port to bind to, required.
|
||||
/// \arg iface Interface address to listen for packets on (may be multicast address)
|
||||
/// \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) {
|
||||
close();//we open a new socket for each attempt
|
||||
int result = 0;
|
||||
if (isIPv6) {
|
||||
struct sockaddr_in6 s6;
|
||||
memset(&s6, 0, sizeof(s6));
|
||||
s6.sin6_family = AF_INET6;
|
||||
if (iface == "0.0.0.0" || iface.length() == 0) {
|
||||
s6.sin6_addr = in6addr_any;
|
||||
} else {
|
||||
inet_pton(AF_INET6, iface.c_str(), &s6.sin6_addr);
|
||||
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 = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_DGRAM;
|
||||
hints.ai_protocol = IPPROTO_UDP;
|
||||
|
||||
std::stringstream ss;
|
||||
ss << port;
|
||||
|
||||
if (iface == "0.0.0.0" || iface.length() == 0) {
|
||||
if ((addr_ret = getaddrinfo(0, ss.str().c_str(), &hints, &addr_result)) != 0){
|
||||
FAIL_MSG("Could not resolve %s for UDP: %s", iface.c_str(), gai_strerror(addr_ret));
|
||||
return 0;
|
||||
}
|
||||
s6.sin6_port = htons(port);
|
||||
int r = ::bind(sock, (sockaddr *)&s6, sizeof(s6));
|
||||
if (r == 0) {
|
||||
result = ntohs(s6.sin6_port);
|
||||
}
|
||||
} else {
|
||||
struct sockaddr_in s4;
|
||||
memset(&s4, 0, sizeof(s4));
|
||||
s4.sin_family = AF_INET;
|
||||
if (iface == "0.0.0.0" || iface.length() == 0) {
|
||||
s4.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
} else {
|
||||
inet_pton(AF_INET, iface.c_str(), &s4.sin_addr);
|
||||
}
|
||||
s4.sin_port = htons(port);
|
||||
int r = ::bind(sock, (sockaddr *)&s4, sizeof(s4));
|
||||
if (r == 0) {
|
||||
result = ntohs(s4.sin_port);
|
||||
}else{
|
||||
if ((addr_ret = getaddrinfo(iface.c_str(), ss.str().c_str(), &hints, &addr_result)) != 0){
|
||||
FAIL_MSG("Could not resolve %s for UDP: %s", iface.c_str(), gai_strerror(addr_ret));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (!result){
|
||||
DEBUG_MSG(DLVL_FAIL, "Could not bind %s UDP socket to port %d: %s", isIPv6 ? "IPv6" : "IPv4", port, strerror(errno));
|
||||
|
||||
std::string err_str;
|
||||
for (rp = addr_result; rp != NULL; rp = rp->ai_next) {
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
//Detect multicast
|
||||
if (iface.length() && ((atoi(iface.c_str()) & 0xE0) == 0xE0)){
|
||||
if (!multicastInterfaces.length()){
|
||||
WARN_MSG("Multicast IP given without any defined interfaces");
|
||||
//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);
|
||||
if (memcmp((char*)&(addr6->sin6_addr), "\000\000\000\000\000\000\000\000\000\000\377\377", 12) == 0){
|
||||
//IPv6-mapped IPv4 address - 13th byte ([12]) holds the first IPv4 byte
|
||||
multicast = (((char*)&(addr6->sin6_addr))[12] & 0xF0) == 0xE0;
|
||||
}else{
|
||||
struct ip_mreq group;
|
||||
inet_pton(AF_INET, iface.c_str(), &group.imr_multiaddr.s_addr);
|
||||
size_t loc = 0;
|
||||
while (loc != std::string::npos){
|
||||
size_t nxtPos = multicastInterfaces.find(',', loc);
|
||||
std::string curIface = multicastInterfaces.substr(loc, (nxtPos == std::string::npos ? nxtPos : nxtPos - loc));
|
||||
inet_pton(AF_INET, curIface.c_str(), &group.imr_interface.s_addr);
|
||||
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0) {
|
||||
WARN_MSG("Unable to register for multicast on interface %s: %s", curIface.c_str() , strerror(errno));
|
||||
}
|
||||
loc = (nxtPos == std::string::npos ? nxtPos : nxtPos + 1);
|
||||
}
|
||||
//"normal" IPv6 address - prefix ff00::/8
|
||||
multicast = (((char*)&(addr6->sin6_addr))[0] == 0xFF);
|
||||
}
|
||||
}else{
|
||||
sockaddr_in * addr4 = (sockaddr_in*)(rp->ai_addr);
|
||||
result = ntohs(addr4->sin_port);
|
||||
//multicast has a "1110" bit prefix
|
||||
multicast = (((char*)&(addr4->sin_addr))[0] & 0xF0) == 0xE0;
|
||||
}
|
||||
freeaddrinfo(addr_result);
|
||||
|
||||
//handle multicast membership(s)
|
||||
if (multicast){
|
||||
struct ipv6_mreq mreq6;
|
||||
struct ip_mreq mreq4;
|
||||
memset(&mreq4, 0, sizeof(mreq4));
|
||||
memset(&mreq6, 0, sizeof(mreq6));
|
||||
struct addrinfo *reslocal, *resmulti;
|
||||
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;
|
||||
}
|
||||
|
||||
if (!multicastInterfaces.length()){
|
||||
if (family == AF_INET6){
|
||||
memcpy(&mreq6.ipv6mr_multiaddr, &((sockaddr_in6*)resmulti->ai_addr)->sin6_addr, sizeof(mreq6.ipv6mr_multiaddr));
|
||||
if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char *)&mreq6, sizeof(mreq6)) != 0) {
|
||||
FAIL_MSG("Unable to register for IPv6 multicast on all interfaces: %s", strerror(errno));
|
||||
close();
|
||||
result = -1;
|
||||
}
|
||||
}else{
|
||||
mreq4.imr_multiaddr = ((sockaddr_in*)resmulti->ai_addr)->sin_addr;
|
||||
mreq4.imr_interface.s_addr = INADDR_ANY;
|
||||
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq4, sizeof(mreq4)) != 0) {
|
||||
FAIL_MSG("Unable to register for IPv4 multicast on all interfaces: %s", strerror(errno));
|
||||
close();
|
||||
result = -1;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
size_t nxtPos = std::string::npos;
|
||||
bool atLeastOne = false;
|
||||
for (size_t loc = 0; loc != std::string::npos; loc = (nxtPos == std::string::npos ? nxtPos : nxtPos + 1)){
|
||||
nxtPos = multicastInterfaces.find(',', loc);
|
||||
std::string curIface = multicastInterfaces.substr(loc, (nxtPos == std::string::npos ? nxtPos : nxtPos - loc));
|
||||
//do a bit of filtering for IPv6, removing the []-braces, if any
|
||||
if (curIface[0] == '['){
|
||||
if (curIface[curIface.size() - 1] == ']'){
|
||||
curIface = curIface.substr(1, curIface.size()-2);
|
||||
}else{
|
||||
curIface = curIface.substr(1, curIface.size()-1);
|
||||
}
|
||||
}
|
||||
if (family == AF_INET6){
|
||||
INFO_MSG("Registering for IPv6 multicast on interface %s", curIface.c_str());
|
||||
if ((addr_ret = getaddrinfo(curIface.c_str(), 0, &hints, &reslocal)) != 0){
|
||||
WARN_MSG("Unable to resolve IPv6 interface address %s: %s", curIface.c_str(), gai_strerror(addr_ret));
|
||||
continue;
|
||||
}
|
||||
memcpy(&mreq6.ipv6mr_multiaddr, &((sockaddr_in6*)resmulti->ai_addr)->sin6_addr, sizeof(mreq6.ipv6mr_multiaddr));
|
||||
mreq6.ipv6mr_interface = ((sockaddr_in6*)reslocal->ai_addr)->sin6_scope_id;
|
||||
if (setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&mreq6, sizeof(mreq6)) != 0) {
|
||||
FAIL_MSG("Unable to register for IPv6 multicast on interface %s (%u): %s", curIface.c_str(), ((sockaddr_in6*)reslocal->ai_addr)->sin6_scope_id, strerror(errno));
|
||||
}else{
|
||||
atLeastOne = true;
|
||||
}
|
||||
}else{
|
||||
INFO_MSG("Registering for IPv4 multicast on interface %s", curIface.c_str());
|
||||
if ((addr_ret = getaddrinfo(curIface.c_str(), 0, &hints, &reslocal)) != 0){
|
||||
WARN_MSG("Unable to resolve IPv4 interface address %s: %s", curIface.c_str(), gai_strerror(addr_ret));
|
||||
continue;
|
||||
}
|
||||
mreq4.imr_multiaddr = ((sockaddr_in*)resmulti->ai_addr)->sin_addr;
|
||||
mreq4.imr_interface = ((sockaddr_in*)reslocal->ai_addr)->sin_addr;
|
||||
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq4, sizeof(mreq4)) != 0) {
|
||||
FAIL_MSG("Unable to register for IPv4 multicast on interface %s: %s", curIface.c_str() , strerror(errno));
|
||||
}else{
|
||||
atLeastOne = true;
|
||||
}
|
||||
}
|
||||
if (!atLeastOne){
|
||||
close();
|
||||
result = -1;
|
||||
}
|
||||
freeaddrinfo(reslocal);//free resolved interface addr
|
||||
}//loop over all interfaces
|
||||
}
|
||||
freeaddrinfo(resmulti);//free resolved multicast addr
|
||||
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -130,13 +130,14 @@ namespace Socket {
|
|||
unsigned int up;///< Amount of bytes transferred up.
|
||||
unsigned int down;///< Amount of bytes transferred down.
|
||||
unsigned int data_size;///< The size in bytes of the allocated space in the data pointer.
|
||||
bool isIPv6;//<<< True if IPv6 socket, false otherwise.
|
||||
int family;///<Current socket address family
|
||||
public:
|
||||
char * data;///< Holds the last received packet.
|
||||
unsigned int data_len; ///< The size in bytes of the last received packet.
|
||||
UDPConnection(const UDPConnection & o);
|
||||
UDPConnection(bool nonblock = false);
|
||||
~UDPConnection();
|
||||
void close();
|
||||
int getSock();
|
||||
int bind(int port, std::string iface = "", const std::string & multicastAddress = "");
|
||||
void setBlocking(bool blocking);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue