blob: 620827ff6eb101bef03589a71d14f6611bdff892 [file] [log] [blame]
Andrew Walbran3d2c1972020-04-07 12:24:26 +01001//===-- SocketAddress.h -----------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef liblldb_SocketAddress_h_
10#define liblldb_SocketAddress_h_
11
12#include <stdint.h>
13
14#ifdef _WIN32
15#include "lldb/Host/windows/windows.h"
16#include <winsock2.h>
17#include <ws2tcpip.h>
18typedef ADDRESS_FAMILY sa_family_t;
19#else
20#include <netdb.h>
21#include <netinet/in.h>
22#include <sys/socket.h>
23#endif
24
25#if defined(__FreeBSD__)
26#include <sys/types.h>
27#endif
28
29#include <string>
30#include <vector>
31
32namespace lldb_private {
33
34class SocketAddress {
35public:
36 // Static method to get all address information for a host and/or service
37 static std::vector<SocketAddress>
38 GetAddressInfo(const char *hostname, const char *servname, int ai_family,
39 int ai_socktype, int ai_protocol, int ai_flags = 0);
40
41 // Constructors and Destructors
42 SocketAddress();
43 SocketAddress(const struct addrinfo *addr_info);
44 SocketAddress(const struct sockaddr &s);
45 SocketAddress(const struct sockaddr_in &s);
46 SocketAddress(const struct sockaddr_in6 &s);
47 SocketAddress(const struct sockaddr_storage &s);
48 ~SocketAddress();
49
50 // Operators
51 const SocketAddress &operator=(const SocketAddress &rhs);
52
53 const SocketAddress &operator=(const struct addrinfo *addr_info);
54
55 const SocketAddress &operator=(const struct sockaddr &s);
56
57 const SocketAddress &operator=(const struct sockaddr_in &s);
58
59 const SocketAddress &operator=(const struct sockaddr_in6 &s);
60
61 const SocketAddress &operator=(const struct sockaddr_storage &s);
62
63 bool operator==(const SocketAddress &rhs) const;
64 bool operator!=(const SocketAddress &rhs) const;
65
66 // Clear the contents of this socket address
67 void Clear();
68
69 // Get the length for the current socket address family
70 socklen_t GetLength() const;
71
72 // Get the max length for the largest socket address supported.
73 static socklen_t GetMaxLength();
74
75 // Get the socket address family
76 sa_family_t GetFamily() const;
77
78 // Set the socket address family
79 void SetFamily(sa_family_t family);
80
81 // Get the address
82 std::string GetIPAddress() const;
83
84 // Get the port if the socket address for the family has a port
85 uint16_t GetPort() const;
86
87 // Set the port if the socket address for the family has a port. The family
88 // must be set correctly prior to calling this function.
89 bool SetPort(uint16_t port);
90
91 // Set the socket address according to the first match from a call to
92 // getaddrinfo() (or equivalent functions for systems that don't have
93 // getaddrinfo(). If "addr_info_ptr" is not NULL, it will get filled in with
94 // the match that was used to populate this socket address.
95 bool
96 getaddrinfo(const char *host, // Hostname ("foo.bar.com" or "foo" or IP
97 // address string ("123.234.12.1" or
98 // "2001:0db8:85a3:0000:0000:8a2e:0370:7334")
99 const char *service, // Protocol name ("tcp", "http", etc) or a
100 // raw port number string ("81")
101 int ai_family = PF_UNSPEC, int ai_socktype = 0,
102 int ai_protocol = 0, int ai_flags = 0);
103
104 // Quick way to set the SocketAddress to localhost given the family. Returns
105 // true if successful, false if "family" doesn't support localhost or if
106 // "family" is not supported by this class.
107 bool SetToLocalhost(sa_family_t family, uint16_t port);
108
109 bool SetToAnyAddress(sa_family_t family, uint16_t port);
110
111 // Returns true if there is a valid socket address in this object.
112 bool IsValid() const;
113
114 // Returns true if the socket is INADDR_ANY
115 bool IsAnyAddr() const;
116
117 // Returns true if the socket is INADDR_LOOPBACK
118 bool IsLocalhost() const;
119
120 // Direct access to all of the sockaddr structures
121 struct sockaddr &sockaddr() {
122 return m_socket_addr.sa;
123 }
124
125 const struct sockaddr &sockaddr() const { return m_socket_addr.sa; }
126
127 struct sockaddr_in &sockaddr_in() {
128 return m_socket_addr.sa_ipv4;
129 }
130
131 const struct sockaddr_in &sockaddr_in() const {
132 return m_socket_addr.sa_ipv4;
133 }
134
135 struct sockaddr_in6 &sockaddr_in6() {
136 return m_socket_addr.sa_ipv6;
137 }
138
139 const struct sockaddr_in6 &sockaddr_in6() const {
140 return m_socket_addr.sa_ipv6;
141 }
142
143 struct sockaddr_storage &sockaddr_storage() {
144 return m_socket_addr.sa_storage;
145 }
146
147 const struct sockaddr_storage &sockaddr_storage() const {
148 return m_socket_addr.sa_storage;
149 }
150
151 // Conversion operators to allow getting the contents of this class as a
152 // pointer to the appropriate structure. This allows an instance of this
153 // class to be used in calls that take one of the sockaddr structure variants
154 // without having to manually use the correct accessor function.
155
156 operator struct sockaddr *() { return &m_socket_addr.sa; }
157
158 operator const struct sockaddr *() const { return &m_socket_addr.sa; }
159
160 operator struct sockaddr_in *() { return &m_socket_addr.sa_ipv4; }
161
162 operator const struct sockaddr_in *() const { return &m_socket_addr.sa_ipv4; }
163
164 operator struct sockaddr_in6 *() { return &m_socket_addr.sa_ipv6; }
165
166 operator const struct sockaddr_in6 *() const {
167 return &m_socket_addr.sa_ipv6;
168 }
169
170 operator const struct sockaddr_storage *() const {
171 return &m_socket_addr.sa_storage;
172 }
173
174 operator struct sockaddr_storage *() { return &m_socket_addr.sa_storage; }
175
176protected:
177 typedef union sockaddr_tag {
178 struct sockaddr sa;
179 struct sockaddr_in sa_ipv4;
180 struct sockaddr_in6 sa_ipv6;
181 struct sockaddr_storage sa_storage;
182 } sockaddr_t;
183
184 // Classes that inherit from SocketAddress can see and modify these
185 sockaddr_t m_socket_addr;
186};
187
188} // namespace lldb_private
189
190#endif // liblldb_SocketAddress_h_