net/IPv[46]Address: make the initializers more portable

Thanks to C++14, we can declare and fill variables inside `constexpr`
functions.  This means me can stop make assumptions on the `struct`
layouts without losing `constexpr`.

Closes #393
This commit is contained in:
Max Kellermann 2018-11-02 17:47:43 +01:00
parent bba22c9c8c
commit 5a5229b499
4 changed files with 21 additions and 48 deletions

1
NEWS
View File

@ -2,6 +2,7 @@ ver 0.21.1 (not yet released)
* decoder * decoder
- ffmpeg: fix build failure with non-standard FFmpeg installation path - ffmpeg: fix build failure with non-standard FFmpeg installation path
* fix build failure on Linux-PowerPC * fix build failure on Linux-PowerPC
* fix build failure on FreeBSD
ver 0.21 (2018/10/31) ver 0.21 (2018/10/31)
* configuration * configuration

View File

@ -53,20 +53,6 @@ class IPv4Address {
uint8_t c, uint8_t d) noexcept { uint8_t c, uint8_t d) noexcept {
return {{{ a, b, c, d }}}; return {{{ a, b, c, d }}};
} }
/**
* @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}};
}
/**
* @param x the 32 bit IP address in host byte order
*/
static constexpr struct in_addr ConstructInAddr(uint32_t x) noexcept {
return ConstructInAddr(x >> 24, x >> 16, x >> 8, x);
}
#else #else
#ifdef __BIONIC__ #ifdef __BIONIC__
@ -78,11 +64,19 @@ class IPv4Address {
return ToBE32((a << 24) | (b << 16) | (c << 8) | d); return ToBE32((a << 24) | (b << 16) | (c << 8) | d);
} }
static constexpr struct in_addr ConstructInAddr(uint8_t a, uint8_t b,
uint8_t c, uint8_t d) noexcept {
return { ConstructInAddrT(a, b, c, d) };
}
#endif
/** /**
* @param x the 32 bit IP address in network byte order * @param x the 32 bit IP address in network byte order
*/ */
static constexpr struct in_addr ConstructInAddrBE(uint32_t x) noexcept { static constexpr struct in_addr ConstructInAddrBE(uint32_t x) noexcept {
return { x }; struct in_addr ia{};
ia.s_addr = x;
return ia;
} }
/** /**
@ -92,26 +86,16 @@ class IPv4Address {
return ConstructInAddrBE(ToBE32(x)); return ConstructInAddrBE(ToBE32(x));
} }
static constexpr struct in_addr ConstructInAddr(uint8_t a, uint8_t b,
uint8_t c, uint8_t d) noexcept {
return { ConstructInAddrT(a, b, c, d) };
}
#endif
/** /**
* @param port the port number in host byte order * @param port the port number in host byte order
*/ */
static constexpr struct sockaddr_in Construct(struct in_addr address, static constexpr struct sockaddr_in Construct(struct in_addr address,
uint16_t port) noexcept { uint16_t port) noexcept {
return { struct sockaddr_in sin{};
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN sin.sin_family = AF_INET;
sizeof(struct sockaddr_in), sin.sin_port = ToBE16(port);
#endif sin.sin_addr = address;
AF_INET, return sin;
ToBE16(port),
address,
{},
};
} }
/** /**

View File

@ -68,16 +68,12 @@ class IPv6Address {
static constexpr struct sockaddr_in6 Construct(struct in6_addr address, static constexpr struct sockaddr_in6 Construct(struct in6_addr address,
uint16_t port, uint16_t port,
uint32_t scope_id) noexcept { uint32_t scope_id) noexcept {
return { struct sockaddr_in6 sin{};
#if defined(__APPLE__) sin.sin6_family = AF_INET6;
sizeof(struct sockaddr_in6), sin.sin6_port = ToBE16(port);
#endif sin.sin6_addr = address;
AF_INET6, sin.sin6_scope_id = scope_id;
ToBE16(port), return sin;
0,
address,
scope_id,
};
} }
public: public:

View File

@ -10,14 +10,6 @@ if have_tcp and not get_option('ipv6').disabled()
if not have_ipv6 and get_option('ipv6').enabled() if not have_ipv6 and get_option('ipv6').enabled()
error('IPv6 not supported by OS') error('IPv6 not supported by OS')
endif endif
conf.set('HAVE_STRUCT_SOCKADDR_IN_SIN_LEN', c_compiler.has_member('struct sockaddr_in', 'sin_len', prefix: '''
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <netinet/in.h>
#endif'''))
else else
have_ipv6 = false have_ipv6 = false
endif endif