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
- ffmpeg: fix build failure with non-standard FFmpeg installation path
* fix build failure on Linux-PowerPC
* fix build failure on FreeBSD
ver 0.21 (2018/10/31)
* configuration

View File

@ -53,20 +53,6 @@ class IPv4Address {
uint8_t c, uint8_t d) noexcept {
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
#ifdef __BIONIC__
@ -78,11 +64,19 @@ class IPv4Address {
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
*/
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));
}
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
*/
static constexpr struct sockaddr_in Construct(struct in_addr address,
uint16_t port) noexcept {
return {
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
sizeof(struct sockaddr_in),
#endif
AF_INET,
ToBE16(port),
address,
{},
};
struct sockaddr_in sin{};
sin.sin_family = AF_INET;
sin.sin_port = ToBE16(port);
sin.sin_addr = address;
return sin;
}
/**

View File

@ -68,16 +68,12 @@ class IPv6Address {
static constexpr struct sockaddr_in6 Construct(struct in6_addr address,
uint16_t port,
uint32_t scope_id) noexcept {
return {
#if defined(__APPLE__)
sizeof(struct sockaddr_in6),
#endif
AF_INET6,
ToBE16(port),
0,
address,
scope_id,
};
struct sockaddr_in6 sin{};
sin.sin6_family = AF_INET6;
sin.sin6_port = ToBE16(port);
sin.sin6_addr = address;
sin.sin6_scope_id = scope_id;
return sin;
}
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()
error('IPv6 not supported by OS')
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
have_ipv6 = false
endif