]> git.tdb.fi Git - libs/net.git/commitdiff
Add some useful factory functions to InetAddr and Inet6Addr
authorMikko Rasa <tdb@tdb.fi>
Sat, 14 Jan 2023 22:31:22 +0000 (00:31 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 14 Jan 2023 22:31:22 +0000 (00:31 +0200)
source/net/inet.cpp
source/net/inet.h
source/net/inet6.cpp
source/net/inet6.h

index f44b1c0b08f0184ed3774e4da25018934b42bdea..17546e88dfe62dc6ec2ba273d808c47653a5e1b8 100644 (file)
@@ -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;
index fb76d7e4d5edd29676d3158baa759c2922d733cf..d970559dab23fb628e61837275c1e0ba8ba807c5 100644 (file)
@@ -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;
index da584d4880ebd43dda084565fa90233b0a0792cb..1b8fc39ce2628bd793a2148723e2e33868a1ad64 100644 (file)
@@ -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;
index 38cba337e436309754c3d1e5d631b45038fcaa5f..f6b0cf4187142b32938f828efcbed6c4d3a52ce9 100644 (file)
@@ -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;