From: Mikko Rasa Date: Sat, 14 Jan 2023 22:31:22 +0000 (+0200) Subject: Add some useful factory functions to InetAddr and Inet6Addr X-Git-Url: http://git.tdb.fi/?p=libs%2Fnet.git;a=commitdiff_plain;h=d6ba60220dadabe552f9f7dc2b4fc34b51cf4d32 Add some useful factory functions to InetAddr and Inet6Addr --- diff --git a/source/net/inet.cpp b/source/net/inet.cpp index f44b1c0..17546e8 100644 --- a/source/net/inet.cpp +++ b/source/net/inet.cpp @@ -16,6 +16,22 @@ InetAddr::InetAddr(const SysAddr &sa) port = ntohs(sai.sin_port); } +InetAddr InetAddr::wildcard(unsigned port) +{ + InetAddr addr; + addr.port = port; + return addr; +} + +InetAddr InetAddr::localhost(unsigned port) +{ + InetAddr addr; + addr.addr[0] = 127; + addr.addr[3] = 1; + addr.port = port; + return addr; +} + SockAddr::SysAddr InetAddr::to_sys() const { SysAddr sa; diff --git a/source/net/inet.h b/source/net/inet.h index fb76d7e..d970559 100644 --- a/source/net/inet.h +++ b/source/net/inet.h @@ -20,6 +20,9 @@ public: InetAddr() = default; InetAddr(const SysAddr &); + static InetAddr wildcard(unsigned); + static InetAddr localhost(unsigned); + InetAddr *copy() const override { return new InetAddr(*this); } SysAddr to_sys() const override; diff --git a/source/net/inet6.cpp b/source/net/inet6.cpp index da584d4..1b8fc39 100644 --- a/source/net/inet6.cpp +++ b/source/net/inet6.cpp @@ -16,6 +16,21 @@ Inet6Addr::Inet6Addr(const SysAddr &sa) port = htons(sai6.sin6_port); } +Inet6Addr Inet6Addr::wildcard(unsigned port) +{ + Inet6Addr addr; + addr.port = port; + return addr; +} + +Inet6Addr Inet6Addr::localhost(unsigned port) +{ + Inet6Addr addr; + addr.addr[15] = 1; + addr.port = port; + return addr; +} + SockAddr::SysAddr Inet6Addr::to_sys() const { SysAddr sa; diff --git a/source/net/inet6.h b/source/net/inet6.h index 38cba33..f6b0cf4 100644 --- a/source/net/inet6.h +++ b/source/net/inet6.h @@ -17,6 +17,9 @@ public: Inet6Addr() = default; Inet6Addr(const SysAddr &); + static Inet6Addr wildcard(unsigned); + static Inet6Addr localhost(unsigned); + Inet6Addr *copy() const override { return new Inet6Addr(*this); } SysAddr to_sys() const override;