Commit 6699b76d authored by aligungr's avatar aligungr

IPv6 support

parent 3a9c992b
......@@ -14,13 +14,13 @@
namespace udp
{
UdpServer::UdpServer(): sockets{}
UdpServer::UdpServer() : sockets{}
{
sockets.push_back(Socket::CreateUdp6());
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}));
}
......@@ -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);
}
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();
// invalid family
if (!version)
return -1;
// send on first socket matching ip version
for(const Socket &s : sockets)
if (version != 4 && version != 6)
throw std::runtime_error{"UdpServer::Send failure: Invalid IP version on"};
for (const Socket &s : sockets)
{
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()
{
for (Socket &s : sockets)
for (auto &s : sockets)
s.close();
}
......
......@@ -27,7 +27,7 @@ class UdpServer
~UdpServer();
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
......@@ -122,10 +122,10 @@ uint16_t InetAddress::getPort() const
Socket::Socket(int domain, int type, int protocol)
{
int sd = socket(domain, type, protocol);
socketDomain = domain;
if (sd < 0)
throw LibError("Socket could not be created:", errno);
this->fd = sd;
this->domain = domain;
}
Socket Socket::CreateUdp4()
......@@ -148,7 +148,7 @@ Socket Socket::CreateTcp6()
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
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());
if (rc == -1)
......@@ -206,7 +206,6 @@ int Socket::send(const InetAddress &address, const uint8_t *buffer, size_t size)
if (err != EAGAIN)
throw LibError("sendto failed: ", errno);
}
return rc;
}
bool Socket::hasFd() const
......@@ -280,12 +279,12 @@ Socket Socket::Select(const std::vector<Socket> &readSockets, const std::vector<
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)];
}
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 {};
......@@ -323,10 +322,9 @@ InetAddress Socket::getAddress() const
int Socket::getIpVersion() const
{
if (socketDomain == AF_INET6)
if (domain == AF_INET6)
return 6;
else if (socketDomain == AF_INET)
if (domain == AF_INET)
return 4;
else
return 0;
}
......@@ -46,7 +46,7 @@ class Socket
{
private:
int fd;
int socketDomain;
int domain;
public:
Socket();
......@@ -55,7 +55,7 @@ class Socket
public:
void bind(const InetAddress &address) 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();
[[nodiscard]] bool hasFd() const;
[[nodiscard]] InetAddress getAddress() const;
......
......@@ -158,7 +158,6 @@ std::string GetIp4(const YAML::Node &node, const std::string &name)
return ipFromIf;
}
std::string GetIp(const YAML::Node &node, const std::string & name)
{
std::string s = GetString(node, name);
......@@ -173,7 +172,6 @@ std::string GetIp(const YAML::Node &node, const std::string & name)
if (!ip6FromIf.empty())
return ip6FromIf;
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)
......
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