summaryrefslogtreecommitdiff
path: root/src/net/ip.hpp
diff options
context:
space:
mode:
authorBen Longbons <b.r.longbons@gmail.com>2014-05-12 17:24:51 -0700
committerBen Longbons <b.r.longbons@gmail.com>2014-05-12 17:28:25 -0700
commitc87b1dc338eadc68accac02563a487d7d8e1c9a0 (patch)
tree4333dca2f664bd7170a7ff984abc8a2d1a643fb2 /src/net/ip.hpp
parent0659ef14f64e2c5d71601060717a761aded8381f (diff)
downloadtmwa-c87b1dc338eadc68accac02563a487d7d8e1c9a0.tar.gz
tmwa-c87b1dc338eadc68accac02563a487d7d8e1c9a0.tar.bz2
tmwa-c87b1dc338eadc68accac02563a487d7d8e1c9a0.tar.xz
tmwa-c87b1dc338eadc68accac02563a487d7d8e1c9a0.zip
Split net/ from mmo/
Diffstat (limited to 'src/net/ip.hpp')
-rw-r--r--src/net/ip.hpp165
1 files changed, 165 insertions, 0 deletions
diff --git a/src/net/ip.hpp b/src/net/ip.hpp
new file mode 100644
index 0000000..42a4689
--- /dev/null
+++ b/src/net/ip.hpp
@@ -0,0 +1,165 @@
+#ifndef TMWA_NET_IP_HPP
+#define TMWA_NET_IP_HPP
+// ip.hpp - classes to deal with IP addresses.
+//
+// Copyright © 2013 Ben Longbons <b.r.longbons@gmail.com>
+//
+// This file is part of The Mana World (Athena server)
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# include "fwd.hpp"
+
+# include <netinet/in.h>
+
+# include <cstddef>
+# include <cstdint>
+
+# include "../strings/fwd.hpp"
+
+// TODO - in the long run ports belong here also
+// and of course, IPv6 stuff.
+// But what about unix socket addresses?
+
+/// Helper function
+template<class T, size_t n>
+constexpr
+bool _ce_a_lt(T (&a)[n], T (&b)[n], size_t i=0)
+{
+ return (i != n
+ && (a[i] < b[i]
+ || (a[i] == b[i]
+ && _ce_a_lt(a, b, i + 1))));
+}
+
+/// A 32-bit Ipv4 address. Does not include a port.
+/// Guaranteed to be laid out like the network wants.
+class IP4Address
+{
+ uint8_t _addr[4];
+public:
+ constexpr
+ IP4Address()
+ : _addr{}
+ {}
+ constexpr explicit
+ IP4Address(const uint8_t (&a)[4])
+ : _addr{a[0], a[1], a[2], a[3]}
+ {}
+ explicit
+ IP4Address(struct in_addr addr)
+ {
+ static_assert(sizeof(addr) == sizeof(_addr), "4 bytes");
+ *this = IP4Address(reinterpret_cast<const uint8_t (&)[4]>(addr));
+ }
+ explicit
+ operator struct in_addr() const
+ {
+ return reinterpret_cast<const struct in_addr&>(_addr);
+ }
+
+ constexpr friend
+ IP4Address operator & (IP4Address l, IP4Address r)
+ {
+ return IP4Address({
+ static_cast<uint8_t>(l._addr[0] & r._addr[0]),
+ static_cast<uint8_t>(l._addr[1] & r._addr[1]),
+ static_cast<uint8_t>(l._addr[2] & r._addr[2]),
+ static_cast<uint8_t>(l._addr[3] & r._addr[3]),
+ });
+ }
+
+ IP4Address& operator &= (IP4Address m)
+ { return *this = *this & m; }
+
+ const uint8_t *bytes() const
+ { return _addr; }
+
+ constexpr friend
+ bool operator < (IP4Address l, IP4Address r)
+ {
+ return _ce_a_lt(l._addr, r._addr);
+ }
+
+ constexpr friend
+ bool operator > (IP4Address l, IP4Address r)
+ {
+ return _ce_a_lt(r._addr, l._addr);
+ }
+
+ constexpr friend
+ bool operator >= (IP4Address l, IP4Address r)
+ {
+ return !_ce_a_lt(l._addr, r._addr);
+ }
+
+ constexpr friend
+ bool operator <= (IP4Address l, IP4Address r)
+ {
+ return !_ce_a_lt(r._addr, l._addr);
+ }
+
+ constexpr friend
+ bool operator == (IP4Address l, IP4Address r)
+ {
+ return !(l < r || r < l);
+ }
+
+ constexpr friend
+ bool operator != (IP4Address l, IP4Address r)
+ {
+ return (l < r || r < l);
+ }
+};
+
+class IP4Mask
+{
+ IP4Address _addr, _mask;
+public:
+ constexpr
+ IP4Mask() : _addr(), _mask()
+ {}
+ constexpr
+ IP4Mask(IP4Address a, IP4Address m) : _addr(a & m), _mask(m)
+ {}
+
+ constexpr
+ IP4Address addr() const
+ { return _addr; }
+ constexpr
+ IP4Address mask() const
+ { return _mask; }
+
+ constexpr
+ bool covers(IP4Address a) const
+ {
+ return (a & _mask) == _addr;
+ }
+};
+
+
+constexpr
+IP4Address IP4_LOCALHOST({127, 0, 0, 1});
+constexpr
+IP4Address IP4_BROADCAST({255, 255, 255, 255});
+
+
+VString<15> convert_for_printf(IP4Address a);
+VString<31> convert_for_printf(IP4Mask m);
+
+bool extract(XString str, IP4Address *iv);
+
+bool extract(XString str, IP4Mask *iv);
+
+#endif // TMWA_NET_IP_HPP