2 * Copyright (C) 2012 Max Kellermann <max@duempel.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 #ifndef XCSOAR_SOCKET_ADDRESS_HPP
31 #define XCSOAR_SOCKET_ADDRESS_HPP
39 #include <sys/socket.h>
45 * An OO wrapper for a UNIX socket address.
49 struct sockaddr_storage address
;
52 SocketAddress() = default;
55 * Creates a #SocketAddress with the specified IPv4 address and
58 * @parm ip the IPv4 address in host byte order
61 static SocketAddress
MakeIPv4Port(uint32_t ip
, unsigned port
);
64 static SocketAddress
MakeIPv4Port(uint8_t a
, uint8_t b
, uint8_t c
,
65 uint8_t d
, unsigned port
) {
66 uint32_t ip
= (a
<< 24) | (b
<< 16) | (c
<< 8) | d
;
67 return MakeIPv4Port(ip
, port
);
71 * Creates a #SocketAddress with the IPv4 a wildcard address and the
75 static SocketAddress
MakePort4(unsigned port
);
77 operator struct sockaddr
*() {
78 return reinterpret_cast<struct sockaddr
*>(&address
);
81 operator const struct sockaddr
*() const {
82 return reinterpret_cast<const struct sockaddr
*>(&address
);
85 constexpr size_t GetCapacity() const {
86 return sizeof(address
);
89 size_t GetLength() const {
93 void SetLength(size_t _length
) {
95 assert(_length
<= sizeof(address
));
100 int GetFamily() const {
101 return address
.ss_family
;
104 bool IsDefined() const {
105 return GetFamily() != AF_UNSPEC
;
109 address
.ss_family
= AF_UNSPEC
;
113 bool operator==(const SocketAddress
&other
) const;
115 bool operator!=(const SocketAddress
&other
) const {
116 return !(*this == other
);
120 bool Lookup(const char *host
, const char *service
, int socktype
);