Skip to content
Snippets Groups Projects

First unit tests

Merged Leon Dietrich requested to merge first_unit_tests into master
Compare and Show latest version
1 file
+ 2
2
Compare changes
  • Side-by-side
  • Inline
+ 73
1
#pragma once
#pragma once
 
#include <arpa/inet.h>
 
#include <sys/types.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/un.h>
@@ -9,6 +11,8 @@
@@ -9,6 +11,8 @@
#include <netinet/ip6.h>
#include <netinet/ip6.h>
#include <functional>
#include <functional>
 
#include <string>
 
#include <sstream>
#include "macros.hpp"
#include "macros.hpp"
#include "net/netio_exception.hpp"
#include "net/netio_exception.hpp"
@@ -70,6 +74,14 @@ public:
@@ -70,6 +74,14 @@ public:
len = sizeof(T);
len = sizeof(T);
}
}
 
explicit socketaddr(const sockaddr_storage *other) : addr{}, len{} {
 
*this = other;
 
}
 
 
explicit socketaddr(const sockaddr_storage &other) : addr{}, len{} {
 
*this = &other;
 
}
 
template <typename T, typename std::enable_if<has_field<T>::value, T>::type * = nullptr>
template <typename T, typename std::enable_if<has_field<T>::value, T>::type * = nullptr>
explicit socketaddr(const T& other) : addr{}, len{} {
explicit socketaddr(const T& other) : addr{}, len{} {
if (other.*(family_map<T>::sa_family_field) != family_map<T>::sa_family) {
if (other.*(family_map<T>::sa_family_field) != family_map<T>::sa_family) {
@@ -102,7 +114,7 @@ COMPILER_RESTORE("-Weffc++");
@@ -102,7 +114,7 @@ COMPILER_RESTORE("-Weffc++");
}
}
socketaddr& operator=(sockaddr *rhs) {
socketaddr& operator=(sockaddr *rhs) {
switch(rhs->sa_family) {
switch (rhs->sa_family) {
case AF_INET:
case AF_INET:
return *this = (sockaddr_in *)rhs;
return *this = (sockaddr_in *)rhs;
case AF_INET6:
case AF_INET6:
@@ -137,6 +149,66 @@ COMPILER_RESTORE("-Weffc++");
@@ -137,6 +149,66 @@ COMPILER_RESTORE("-Weffc++");
return len;
return len;
}
}
 
inline bool operator==(const socketaddr& _other) const {
 
if (!(this->family() == _other.family())) {
 
return false;
 
}
 
 
switch (_other.family()) {
 
case AF_INET:
 
return (((sockaddr_in*) &this->addr)->sin_addr.s_addr == ((sockaddr_in*) &_other.addr)->sin_addr.s_addr)
 
&& (((sockaddr_in*) &this->addr)->sin_port == ((sockaddr_in*) &_other.addr)->sin_port);
 
case AF_INET6:
 
return 0 == memcmp(&((sockaddr_in6*) &this->addr)->sin6_addr, &((sockaddr_in6*) &_other.addr)->sin6_addr, sizeof(in6_addr))
 
&& (((sockaddr_in6*) &this->addr)->sin6_port == ((sockaddr_in6*) &_other.addr)->sin6_port);
 
default:
 
return
 
this->len == _other.len &&
 
0 == memcmp(&this->addr, &_other.addr, this->len);
 
}
 
 
return true;
 
}
 
 
inline bool operator!=(const socketaddr& _other) const {
 
return !(*this == _other);
 
}
 
 
std::string str() const {
 
std::ostringstream oss;
 
oss << "SocketAddress: ";
 
const int buffer_size = 1024;
 
char buffer[buffer_size];
 
 
switch (this->family()) {
 
case AF_INET:
 
inet_ntop(AF_INET, &((sockaddr_in*)&addr)->sin_addr, buffer, buffer_size);
 
oss << "IPv4 " << buffer << ":" << ntohs(((sockaddr_in*)&addr)->sin_port);
 
break;
 
case AF_INET6:
 
inet_ntop(AF_INET6, &((sockaddr_in6*)&addr)->sin6_addr, buffer, buffer_size);
 
oss << "IPv6 ["<< buffer << "]:" << ntohs(((sockaddr_in6*)&addr)->sin6_port);
 
break;
 
case AF_UNIX:
 
oss << "FileSocket " << ((sockaddr_un*)&addr)->sun_path;
 
break;
 
case AF_NETLINK: {
 
const auto nl_sock_ptr = (sockaddr_nl*) &addr;
 
oss << "Netlink g:" << nl_sock_ptr->nl_groups << " p:" << nl_sock_ptr->nl_pad << " pid:" << nl_sock_ptr->nl_pid;
 
break;
 
}
 
default:
 
oss << "Unknown Socket Address Type";
 
break;
 
}
 
 
return oss.str();
 
}
 
};
};
 
static inline std::ostream& operator<<(std::ostream& os, const socketaddr& sockaddr) {
 
return os << sockaddr.str();
 
}
 
}
}
Loading