android/GlueIOIOPort: fix spurious errors after IOIO baud rate change
[xcsoar.git] / src / OS / SocketAddress.hpp
blob39a584bd5ffc2a3655aabb6e246ffac8f2757107
1 /*
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
6 * are met:
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
14 * distribution.
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
33 #include "Compiler.h"
35 #include <assert.h>
36 #include <stdint.h>
38 #ifdef HAVE_POSIX
39 #include <sys/socket.h>
40 #else
41 #include <winsock2.h>
42 #endif
44 /**
45 * An OO wrapper for a UNIX socket address.
47 class SocketAddress {
48 size_t length;
49 struct sockaddr_storage address;
51 public:
52 SocketAddress() = default;
54 /**
55 * Creates a #SocketAddress with the specified IPv4 address and
56 * port.
58 * @parm ip the IPv4 address in host byte order
60 gcc_const
61 static SocketAddress MakeIPv4Port(uint32_t ip, unsigned port);
63 gcc_const
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);
70 /**
71 * Creates a #SocketAddress with the IPv4 a wildcard address and the
72 * specified port.
74 gcc_const
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 {
90 return length;
93 void SetLength(size_t _length) {
94 assert(_length > 0);
95 assert(_length <= sizeof(address));
97 length = _length;
100 int GetFamily() const {
101 return address.ss_family;
104 bool IsDefined() const {
105 return GetFamily() != AF_UNSPEC;
108 void Clear() {
109 address.ss_family = AF_UNSPEC;
112 gcc_pure
113 bool operator==(const SocketAddress &other) const;
115 bool operator!=(const SocketAddress &other) const {
116 return !(*this == other);
119 #ifndef _WIN32_WCE
120 bool Lookup(const char *host, const char *service, int socktype);
121 #endif
124 #endif