IPv4Address.hxx 6.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
/*
 * Copyright (C) 2012-2017 Max Kellermann <max.kellermann@gmail.com>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef IPV4_ADDRESS_HXX
#define IPV4_ADDRESS_HXX

#include "SocketAddress.hxx"
#include "system/ByteOrder.hxx"

#include <stdint.h>

#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <netinet/in.h>
#endif

/**
 * An OO wrapper for struct sockaddr_in.
 */
class IPv4Address {
	struct sockaddr_in address;

51
#ifdef _WIN32
52
	static constexpr struct in_addr ConstructInAddr(uint8_t a, uint8_t b,
53
							uint8_t c, uint8_t d) noexcept {
54 55 56
		return {{{ a, b, c, d }}};
	}

57 58 59 60 61 62 63
	/**
	 * @param x the 32 bit IP address in network byte order
	 */
	static constexpr struct in_addr ConstructInAddrBE(uint32_t x) noexcept {
		return (struct in_addr){{.S_addr=x}};
	}

64 65 66
	/**
	 * @param x the 32 bit IP address in host byte order
	 */
67
	static constexpr struct in_addr ConstructInAddr(uint32_t x) noexcept {
68 69 70 71 72 73 74 75 76
		return ConstructInAddr(x >> 24, x >> 16, x >> 8, x);
	}
#else

#ifdef __BIONIC__
	typedef uint32_t in_addr_t;
#endif

	static constexpr in_addr_t ConstructInAddrT(uint8_t a, uint8_t b,
77
						    uint8_t c, uint8_t d) noexcept {
78 79 80
		return ToBE32((a << 24) | (b << 16) | (c << 8) | d);
	}

81 82 83 84 85 86 87
	/**
	 * @param x the 32 bit IP address in network byte order
	 */
	static constexpr struct in_addr ConstructInAddrBE(uint32_t x) noexcept {
		return { x };
	}

88 89 90
	/**
	 * @param x the 32 bit IP address in host byte order
	 */
91
	static constexpr struct in_addr ConstructInAddr(uint32_t x) noexcept {
92
		return ConstructInAddrBE(ToBE32(x));
93 94 95
	}

	static constexpr struct in_addr ConstructInAddr(uint8_t a, uint8_t b,
96
							uint8_t c, uint8_t d) noexcept {
97 98 99 100
		return { ConstructInAddrT(a, b, c, d) };
	}
#endif

101 102 103
	/**
	 * @param port the port number in host byte order
	 */
104
	static constexpr struct sockaddr_in Construct(struct in_addr address,
105
						      uint16_t port) noexcept {
106
		return {
107
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
108 109 110 111 112 113 114 115 116
			sizeof(struct sockaddr_in),
#endif
			AF_INET,
			ToBE16(port),
			address,
			{},
		};
	}

117 118 119 120
	/**
	 * @param x the 32 bit IP address in host byte order
	 * @param port the port number in host byte order
	 */
121
	static constexpr struct sockaddr_in Construct(uint32_t address,
122
						      uint16_t port) noexcept {
123 124 125 126 127 128
		return Construct(ConstructInAddr(address), port);
	}

public:
	IPv4Address() = default;

129 130 131
	constexpr IPv4Address(const struct sockaddr_in &_address) noexcept
		:address(_address) {}

132 133 134
	/**
	 * @param port the port number in host byte order
	 */
135
	constexpr IPv4Address(struct in_addr _address, uint16_t port) noexcept
136
		:IPv4Address(Construct(_address, port)) {}
137

138 139 140
	/**
	 * @param port the port number in host byte order
	 */
141
	constexpr IPv4Address(uint8_t a, uint8_t b, uint8_t c,
142
			      uint8_t d, uint16_t port) noexcept
143 144
		:IPv4Address(ConstructInAddr(a, b, c, d), port) {}

145 146 147
	/**
	 * @param port the port number in host byte order
	 */
148
	constexpr explicit IPv4Address(uint16_t port) noexcept
149 150 151
		:IPv4Address(ConstructInAddr(INADDR_ANY), port) {}

	/**
152 153
	 * Construct with data copied from a #SocketAddress.  Its
	 * address family must be AF_INET.
154
	 */
155
	explicit IPv4Address(SocketAddress src) noexcept;
156

157
	static constexpr struct in_addr Loopback() noexcept {
158 159 160
		return ConstructInAddr(INADDR_LOOPBACK);
	}

161 162 163 164 165 166 167 168 169 170 171 172 173 174
	/**
	 * Generate a (net-)mask with the specified prefix length.
	 */
	static constexpr IPv4Address MaskFromPrefix(unsigned prefix_length) noexcept {
		return Construct(prefix_length == 0
				 ? 0
				 : (~uint32_t(0)) << (32 - prefix_length),
				 ~uint16_t(0));
	}

	/**
	 * Return a downcasted reference to the address.  This call is
	 * only legal after verifying SocketAddress::GetFamily().
	 */
175
	static constexpr const IPv4Address &Cast(const SocketAddress &src) noexcept {
176 177 178 179 180
		/* this reinterpret_cast works because this class is
		   just a wrapper for struct sockaddr_in6 */
		return *(const IPv4Address *)(const void *)src.GetAddress();
	}

181 182
	constexpr operator SocketAddress() const noexcept {
		return SocketAddress((const struct sockaddr *)&address,
183 184 185
				     sizeof(address));
	}

186
	constexpr SocketAddress::size_type GetSize() const noexcept {
187 188 189
		return sizeof(address);
	}

190
	constexpr bool IsDefined() const noexcept {
191 192 193
		return address.sin_family != AF_UNSPEC;
	}

194 195 196
	/**
	 * @return the port number in host byte order
	 */
197
	constexpr uint16_t GetPort() const noexcept {
198 199 200
		return FromBE16(address.sin_port);
	}

201 202 203
	/**
	 * @param port the port number in host byte order
	 */
204
	void SetPort(uint16_t port) noexcept {
205 206 207
		address.sin_port = ToBE16(port);
	}

208
	constexpr const struct in_addr &GetAddress() const noexcept {
209 210
		return address.sin_addr;
	}
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

	/**
	 * @return the 32 bit IP address in network byte order
	 */
	constexpr uint32_t GetNumericAddressBE() const noexcept {
		return GetAddress().s_addr;
	}

	/**
	 * @return the 32 bit IP address in host byte order
	 */
	constexpr uint32_t GetNumericAddress() const noexcept {
		return FromBE32(GetNumericAddressBE());
	}

	/**
	 * Bit-wise AND of two addresses.  This is useful for netmask
	 * calculations.
	 */
	constexpr IPv4Address operator&(const IPv4Address &other) const noexcept {
		return IPv4Address(ConstructInAddrBE(GetNumericAddressBE() & other.GetNumericAddressBE()),
				   GetPort() & other.GetPort());
	}
234 235 236
};

#endif