Skip to content
Snippets Groups Projects
Commit 01d8e513 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

peerdiscovery: cleanup

parent b5921b0d
No related branches found
No related tags found
No related merge requests found
...@@ -64,10 +64,8 @@ public: ...@@ -64,10 +64,8 @@ public:
* Join the threads * Join the threads
*/ */
void join() { void join() {
if(running_listen.joinable()) running_listen.join();
if(running_listen.joinable()){ running_listen.join(); }; if(running_send.joinable()) running_send.join();
if(running_send.joinable()){ running_send.join(); };
} }
private: private:
...@@ -134,11 +132,6 @@ private: ...@@ -134,11 +132,6 @@ private:
return (uint32_t)lit_int[0] << 0 return (uint32_t)lit_int[0] << 0
| (uint32_t)lit_int[1] << 8; | (uint32_t)lit_int[1] << 8;
} }
#ifdef _WIN32
WSADATA wsaData;
#endif
}; };
} }
...@@ -91,8 +91,6 @@ public: ...@@ -91,8 +91,6 @@ public:
static std::vector<SockAddr> resolve(const std::string& host, const std::string& service = {}); static std::vector<SockAddr> resolve(const std::string& host, const std::string& service = {});
static SockAddr parse(sa_family_t family, const char* address);
bool operator<(const SockAddr& o) const { bool operator<(const SockAddr& o) const {
if (len != o.len) if (len != o.len)
return len < o.len; return len < o.len;
...@@ -193,6 +191,12 @@ public: ...@@ -193,6 +191,12 @@ public:
} }
} }
/**
* Set the address part of the socket address from a numeric IP address (string representation).
* Family must be already set. Throws in case of parse failue.
*/
void setAddress(const char* address);
/** /**
* Returns the accessible byte length at the pointer returned by #get(). * Returns the accessible byte length at the pointer returned by #get().
* If zero, #get() returns null. * If zero, #get() returns null.
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
namespace dht { namespace dht {
constexpr char MULTICAST_ADDRESS_IPV4[10] = "224.0.0.1"; constexpr char MULTICAST_ADDRESS_IPV4[10] = "224.0.0.1";
constexpr char MULTICAST_ADDRESS_IPV6[8] = "ff02::1"; constexpr char MULTICAST_ADDRESS_IPV6[8] = "ff05::2"; // Site-local multicast
#ifdef _WIN32 #ifdef _WIN32
...@@ -66,12 +66,10 @@ PeerDiscovery::PeerDiscovery(sa_family_t domain, in_port_t port) ...@@ -66,12 +66,10 @@ PeerDiscovery::PeerDiscovery(sa_family_t domain, in_port_t port)
int int
PeerDiscovery::initialize_socket(sa_family_t domain) PeerDiscovery::initialize_socket(sa_family_t domain)
{ {
#ifdef _WIN32 #ifdef _WIN32
// Initialize Windows Socket API with given VERSION. WSADATA wsaData;
if (WSAStartup(0x0101, &wsaData)) { if (WSAStartup(0x0101, &wsaData)) {
perror("WSAStartup"); throw std::runtime_error(std::string("Can't initialize Winsock2 ") + strerror(errno));
throw std::runtime_error(std::string("Socket Creation Error_initialize_socket ") + strerror(errno));
} }
#endif #endif
...@@ -108,7 +106,6 @@ PeerDiscovery::socketJoinMulticast(int sockfd, sa_family_t family) ...@@ -108,7 +106,6 @@ PeerDiscovery::socketJoinMulticast(int sockfd, sa_family_t family)
switch (family) switch (family)
{ {
case AF_INET:{ case AF_INET:{
ip_mreq config_ipv4; ip_mreq config_ipv4;
//This option can be used to set the interface for sending outbound //This option can be used to set the interface for sending outbound
...@@ -131,12 +128,9 @@ PeerDiscovery::socketJoinMulticast(int sockfd, sa_family_t family) ...@@ -131,12 +128,9 @@ PeerDiscovery::socketJoinMulticast(int sockfd, sa_family_t family)
if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&config_ipv4, sizeof(config_ipv4)) < 0){ if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&config_ipv4, sizeof(config_ipv4)) < 0){
throw std::runtime_error(std::string(" Member Addition IPV4 Error: ") + strerror(errno)); throw std::runtime_error(std::string(" Member Addition IPV4 Error: ") + strerror(errno));
} }
break; break;
} }
case AF_INET6: { case AF_INET6: {
ipv6_mreq config_ipv6; ipv6_mreq config_ipv6;
unsigned int outif = 0; unsigned int outif = 0;
...@@ -154,10 +148,8 @@ PeerDiscovery::socketJoinMulticast(int sockfd, sa_family_t family) ...@@ -154,10 +148,8 @@ PeerDiscovery::socketJoinMulticast(int sockfd, sa_family_t family)
if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &config_ipv6, sizeof(config_ipv6)) < 0){ if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &config_ipv6, sizeof(config_ipv6)) < 0){
throw std::runtime_error(std::string("Member Addition IPV6 Error: ") + strerror(errno)); throw std::runtime_error(std::string("Member Addition IPV6 Error: ") + strerror(errno));
} }
break; break;
} }
} }
} }
...@@ -205,7 +197,8 @@ PeerDiscovery::sender_setup(const dht::InfoHash& nodeId, in_port_t port_to_send) ...@@ -205,7 +197,8 @@ PeerDiscovery::sender_setup(const dht::InfoHash& nodeId, in_port_t port_to_send)
{ {
nodeId_ = nodeId; nodeId_ = nodeId;
//Set up for Sender //Set up for Sender
sockAddrSend_ = SockAddr::parse(domain_, domain_ == AF_INET ? MULTICAST_ADDRESS_IPV4 : MULTICAST_ADDRESS_IPV6); sockAddrSend_.setFamily(domain_);
sockAddrSend_.setAddress(domain_ == AF_INET ? MULTICAST_ADDRESS_IPV4 : MULTICAST_ADDRESS_IPV6);
sockAddrSend_.setPort(port_); sockAddrSend_.setPort(port_);
//Setup for send data //Setup for send data
...@@ -330,7 +323,6 @@ PeerDiscovery::stop() ...@@ -330,7 +323,6 @@ PeerDiscovery::stop()
} }
cv_.notify_one(); cv_.notify_one();
if (stop_writefd_ != -1) { if (stop_writefd_ != -1) {
if (write(stop_writefd_, "\0", 1) == -1) { if (write(stop_writefd_, "\0", 1) == -1) {
perror("write"); perror("write");
} }
......
...@@ -73,19 +73,23 @@ SockAddr::resolve(const std::string& host, const std::string& service) ...@@ -73,19 +73,23 @@ SockAddr::resolve(const std::string& host, const std::string& service)
return ips; return ips;
} }
void
SockAddr SockAddr::setAddress(const char* address)
SockAddr::parse(sa_family_t family, const char* address)
{ {
SockAddr addr; auto family = getFamily();
addr.setFamily(family); void* addr = nullptr;
if (inet_pton(family, address, family == AF_INET ? (void*)&addr.getIPv4().sin_addr.s_addr : (void*)&addr.getIPv6().sin6_addr) <= 0){ switch (family) {
throw std::runtime_error("SockAddr::parse inet_pton"); case AF_INET:
addr = &getIPv4().sin_addr;
break;
case AF_INET6:
addr = &getIPv6().sin6_addr;
break;
} }
return addr; if (not addr or inet_pton(family, address, addr) <= 0)
throw std::runtime_error("Can't parse IP address");
} }
std::string std::string
print_addr(const sockaddr* sa, socklen_t slen) print_addr(const sockaddr* sa, socklen_t slen)
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment