0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-06 05:48:54 +01:00
construct/include/ircd/net/ipaddr.h

95 lines
1.9 KiB
C
Raw Normal View History

// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2019 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
#pragma once
#define HAVE_IRCD_NET_IPADDR_H
// Forward declarations for boost because it is not included here.
namespace boost::asio::ip
{
struct address;
};
namespace ircd::net
{
union ipaddr;
const uint128_t &host6(const ipaddr &);
const uint32_t &host4(const ipaddr &);
uint128_t &host6(ipaddr &);
uint32_t &host4(ipaddr &);
bool operator!(const ipaddr &);
bool operator<(const ipaddr &, const ipaddr &);
bool operator==(const ipaddr &, const ipaddr &);
string_view string(const mutable_buffer &out, const uint32_t &);
string_view string(const mutable_buffer &out, const uint128_t &);
}
union ircd::net::ipaddr
{
uint32_t v4;
uint128_t v6 {0};
std::array<uint8_t, 16> byte;
explicit operator bool() const;
};
static_assert(std::alignment_of<ircd::net::ipaddr>() >= 16);
inline ircd::net::ipaddr::operator
bool() const
{
return bool(v6);
}
inline bool
ircd::net::operator==(const ipaddr &a, const ipaddr &b)
{
return a.v6 == b.v6;
}
inline bool
ircd::net::operator<(const ipaddr &a, const ipaddr &b)
{
return a.v6 < b.v6;
}
inline bool
ircd::net::operator!(const ipaddr &a)
{
return !a.v6;
}
inline uint32_t &
ircd::net::host4(ipaddr &ipaddr)
{
return ipaddr.v4;
}
inline ircd::uint128_t &
ircd::net::host6(ipaddr &ipaddr)
{
return ipaddr.v6;
}
inline const uint32_t &
ircd::net::host4(const ipaddr &ipaddr)
{
return ipaddr.v4;
}
inline const ircd::uint128_t &
ircd::net::host6(const ipaddr &ipaddr)
{
return ipaddr.v6;
}