Commit 6699b76d authored by aligungr's avatar aligungr

IPv6 support

parent 3a9c992b
...@@ -14,13 +14,13 @@ ...@@ -14,13 +14,13 @@
namespace udp namespace udp
{ {
UdpServer::UdpServer(): sockets{} UdpServer::UdpServer() : sockets{}
{ {
sockets.push_back(Socket::CreateUdp6()); sockets.push_back(Socket::CreateUdp6());
sockets.push_back(Socket::CreateUdp4()); sockets.push_back(Socket::CreateUdp4());
} }
UdpServer::UdpServer(const std::string &address, uint16_t port): sockets{} UdpServer::UdpServer(const std::string &address, uint16_t port) : sockets{}
{ {
sockets.push_back(Socket::CreateAndBindUdp({address, port})); sockets.push_back(Socket::CreateAndBindUdp({address, port}));
} }
...@@ -32,25 +32,27 @@ int UdpServer::Receive(uint8_t *buffer, size_t bufferSize, int timeoutMs, InetAd ...@@ -32,25 +32,27 @@ int UdpServer::Receive(uint8_t *buffer, size_t bufferSize, int timeoutMs, InetAd
return Socket::Select(sockets, ws, timeoutMs).receive(buffer, bufferSize, 0, outPeerAddress); return Socket::Select(sockets, ws, timeoutMs).receive(buffer, bufferSize, 0, outPeerAddress);
} }
int UdpServer::Send(const InetAddress &address, const uint8_t *buffer, size_t bufferSize) const void UdpServer::Send(const InetAddress &address, const uint8_t *buffer, size_t bufferSize) const
{ {
int version = address.getIpVersion(); int version = address.getIpVersion();
// invalid family if (version != 4 && version != 6)
if (!version) throw std::runtime_error{"UdpServer::Send failure: Invalid IP version on"};
return -1;
// send on first socket matching ip version for (const Socket &s : sockets)
for(const Socket &s : sockets)
{ {
if (s.getIpVersion() == version) if (s.getIpVersion() == version)
return s.send(address, buffer, bufferSize); {
s.send(address, buffer, bufferSize);
return;
} }
// no socket found }
return -1;
throw std::runtime_error{"UdpServer::Send failure: No socket found"};
} }
UdpServer::~UdpServer() UdpServer::~UdpServer()
{ {
for (Socket &s : sockets) for (auto &s : sockets)
s.close(); s.close();
} }
......
...@@ -27,7 +27,7 @@ class UdpServer ...@@ -27,7 +27,7 @@ class UdpServer
~UdpServer(); ~UdpServer();
int Receive(uint8_t *buffer, size_t bufferSize, int timeoutMs, InetAddress &outPeerAddress); int Receive(uint8_t *buffer, size_t bufferSize, int timeoutMs, InetAddress &outPeerAddress);
int Send(const InetAddress &address, const uint8_t *buffer, size_t bufferSize) const; void Send(const InetAddress &address, const uint8_t *buffer, size_t bufferSize) const;
}; };
} // namespace udp } // namespace udp
...@@ -122,10 +122,10 @@ uint16_t InetAddress::getPort() const ...@@ -122,10 +122,10 @@ uint16_t InetAddress::getPort() const
Socket::Socket(int domain, int type, int protocol) Socket::Socket(int domain, int type, int protocol)
{ {
int sd = socket(domain, type, protocol); int sd = socket(domain, type, protocol);
socketDomain = domain;
if (sd < 0) if (sd < 0)
throw LibError("Socket could not be created:", errno); throw LibError("Socket could not be created:", errno);
this->fd = sd; this->fd = sd;
this->domain = domain;
} }
Socket Socket::CreateUdp4() Socket Socket::CreateUdp4()
...@@ -148,7 +148,7 @@ Socket Socket::CreateTcp6() ...@@ -148,7 +148,7 @@ Socket Socket::CreateTcp6()
return {AF_INET6, SOCK_STREAM, IPPROTO_TCP}; return {AF_INET6, SOCK_STREAM, IPPROTO_TCP};
} }
Socket::Socket() : fd(-1) Socket::Socket() : fd(-1), domain(0)
{ {
} }
...@@ -197,7 +197,7 @@ int Socket::receive(uint8_t *buffer, size_t bufferSize, int timeoutMs, InetAddre ...@@ -197,7 +197,7 @@ int Socket::receive(uint8_t *buffer, size_t bufferSize, int timeoutMs, InetAddre
return 0; return 0;
} }
int Socket::send(const InetAddress &address, const uint8_t *buffer, size_t size) const void Socket::send(const InetAddress &address, const uint8_t *buffer, size_t size) const
{ {
ssize_t rc = sendto(fd, buffer, size, MSG_DONTWAIT, address.getSockAddr(), address.getSockLen()); ssize_t rc = sendto(fd, buffer, size, MSG_DONTWAIT, address.getSockAddr(), address.getSockLen());
if (rc == -1) if (rc == -1)
...@@ -206,7 +206,6 @@ int Socket::send(const InetAddress &address, const uint8_t *buffer, size_t size) ...@@ -206,7 +206,6 @@ int Socket::send(const InetAddress &address, const uint8_t *buffer, size_t size)
if (err != EAGAIN) if (err != EAGAIN)
throw LibError("sendto failed: ", errno); throw LibError("sendto failed: ", errno);
} }
return rc;
} }
bool Socket::hasFd() const bool Socket::hasFd() const
...@@ -280,12 +279,12 @@ Socket Socket::Select(const std::vector<Socket> &readSockets, const std::vector< ...@@ -280,12 +279,12 @@ Socket Socket::Select(const std::vector<Socket> &readSockets, const std::vector<
if (!rs.empty()) if (!rs.empty())
{ {
std::uniform_int_distribution<int> drs(0, rs.size()-1); std::uniform_int_distribution<int> drs(0, rs.size() - 1);
return rs[drs(generator)]; return rs[drs(generator)];
} }
if (!ws.empty()) if (!ws.empty())
{ {
std::uniform_int_distribution<int> dws(0, ws.size()-1); std::uniform_int_distribution<int> dws(0, ws.size() - 1);
return rs[dws(generator)]; return rs[dws(generator)];
} }
return {}; return {};
...@@ -323,10 +322,9 @@ InetAddress Socket::getAddress() const ...@@ -323,10 +322,9 @@ InetAddress Socket::getAddress() const
int Socket::getIpVersion() const int Socket::getIpVersion() const
{ {
if (socketDomain == AF_INET6) if (domain == AF_INET6)
return 6; return 6;
else if (socketDomain == AF_INET) if (domain == AF_INET)
return 4; return 4;
else
return 0; return 0;
} }
...@@ -46,7 +46,7 @@ class Socket ...@@ -46,7 +46,7 @@ class Socket
{ {
private: private:
int fd; int fd;
int socketDomain; int domain;
public: public:
Socket(); Socket();
...@@ -55,7 +55,7 @@ class Socket ...@@ -55,7 +55,7 @@ class Socket
public: public:
void bind(const InetAddress &address) const; void bind(const InetAddress &address) const;
int receive(uint8_t *buffer, size_t bufferSize, int timeoutMs, InetAddress &outAddress) const; int receive(uint8_t *buffer, size_t bufferSize, int timeoutMs, InetAddress &outAddress) const;
int send(const InetAddress &address, const uint8_t *buffer, size_t size) const; void send(const InetAddress &address, const uint8_t *buffer, size_t size) const;
void close(); void close();
[[nodiscard]] bool hasFd() const; [[nodiscard]] bool hasFd() const;
[[nodiscard]] InetAddress getAddress() const; [[nodiscard]] InetAddress getAddress() const;
......
...@@ -158,7 +158,6 @@ std::string GetIp4(const YAML::Node &node, const std::string &name) ...@@ -158,7 +158,6 @@ std::string GetIp4(const YAML::Node &node, const std::string &name)
return ipFromIf; return ipFromIf;
} }
std::string GetIp(const YAML::Node &node, const std::string & name) std::string GetIp(const YAML::Node &node, const std::string & name)
{ {
std::string s = GetString(node, name); std::string s = GetString(node, name);
...@@ -173,7 +172,6 @@ std::string GetIp(const YAML::Node &node, const std::string & name) ...@@ -173,7 +172,6 @@ std::string GetIp(const YAML::Node &node, const std::string & name)
if (!ip6FromIf.empty()) if (!ip6FromIf.empty())
return ip6FromIf; return ip6FromIf;
FieldError(name, "must be a valid IP address or a valid network interface with an IP address"); FieldError(name, "must be a valid IP address or a valid network interface with an IP address");
} }
void AssertHasBool(const YAML::Node &node, const std::string &name) void AssertHasBool(const YAML::Node &node, const std::string &name)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment