1//===-- SocketAddress.h -----------------------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef liblldb_SocketAddress_h_ 11#define liblldb_SocketAddress_h_ 12 13// C Includes 14#include <stdint.h> 15 16#ifdef _WIN32 17#include "lldb/Host/windows/windows.h" 18#include <winsock2.h> 19#include <WS2tcpip.h> 20typedef ADDRESS_FAMILY sa_family_t; 21#else 22#include <sys/socket.h> 23#include <netdb.h> 24#include <netinet/in.h> 25#endif 26 27#if defined(__FreeBSD__) 28#include <sys/types.h> 29#endif 30 31// C++ Includes 32// Other libraries and framework includes 33// Project includes 34 35namespace lldb_private { 36 37class SocketAddress 38{ 39public: 40 //------------------------------------------------------------------ 41 // Constructors and Destructors 42 //------------------------------------------------------------------ 43 SocketAddress (); 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 (const SocketAddress& rhs); 49 ~SocketAddress (); 50 51 //------------------------------------------------------------------ 52 // Operators 53 //------------------------------------------------------------------ 54 const SocketAddress& 55 operator=(const SocketAddress& rhs); 56 57 const SocketAddress& 58 operator=(const struct addrinfo *addr_info); 59 60 const SocketAddress& 61 operator=(const struct sockaddr &s); 62 63 const SocketAddress& 64 operator=(const struct sockaddr_in &s); 65 66 const SocketAddress& 67 operator=(const struct sockaddr_in6 &s); 68 69 const SocketAddress& 70 operator=(const struct sockaddr_storage &s); 71 72 //------------------------------------------------------------------ 73 // Clear the contents of this socket address 74 //------------------------------------------------------------------ 75 void 76 Clear (); 77 78 //------------------------------------------------------------------ 79 // Get the length for the current socket address family 80 //------------------------------------------------------------------ 81 socklen_t 82 GetLength () const; 83 84 //------------------------------------------------------------------ 85 // Get the mex length for the the largest socket address supported. 86 //------------------------------------------------------------------ 87 static socklen_t 88 GetMaxLength (); 89 90 //------------------------------------------------------------------ 91 // Get the socket address family 92 //------------------------------------------------------------------ 93 sa_family_t 94 GetFamily () const; 95 96 //------------------------------------------------------------------ 97 // Set the socket address family 98 //------------------------------------------------------------------ 99 void 100 SetFamily (sa_family_t family); 101 102 //------------------------------------------------------------------ 103 // Get the port if the socket address for the family has a port 104 //------------------------------------------------------------------ 105 uint16_t 106 GetPort () const; 107 108 //------------------------------------------------------------------ 109 // Set the port if the socket address for the family has a port. 110 // The family must be set correctly prior to calling this function. 111 //------------------------------------------------------------------ 112 bool 113 SetPort (uint16_t port); 114 115 //------------------------------------------------------------------ 116 // Set the socket address according to the first match from a call 117 // to getaddrinfo() (or equivalent functions for systems that don't 118 // have getaddrinfo(). If "addr_info_ptr" is not NULL, it will get 119 // filled in with the match that was used to populate this socket 120 // address. 121 //------------------------------------------------------------------ 122 bool 123 getaddrinfo (const char *host, // Hostname ("foo.bar.com" or "foo" or IP address string ("123.234.12.1" or "2001:0db8:85a3:0000:0000:8a2e:0370:7334") 124 const char *service, // Protocol name ("tcp", "http", etc) or a raw port number string ("81") 125 int ai_family = PF_UNSPEC, 126 int ai_socktype = 0, 127 int ai_protocol = 0, 128 int ai_flags = 0); 129 130 //------------------------------------------------------------------ 131 // Quick way to set the SocketAddress to localhost given the family. 132 // Returns true if successful, false if "family" doesn't support 133 // localhost or if "family" is not supported by this class. 134 //------------------------------------------------------------------ 135 bool 136 SetToLocalhost (sa_family_t family, 137 uint16_t port); 138 139 bool 140 SetToAnyAddress (sa_family_t family, 141 uint16_t port); 142 143 //------------------------------------------------------------------ 144 // Returns true if there is a valid socket address in this object. 145 //------------------------------------------------------------------ 146 bool 147 IsValid () const; 148 149 //------------------------------------------------------------------ 150 // Direct access to all of the sockaddr structures 151 //------------------------------------------------------------------ 152 struct sockaddr & 153 sockaddr () 154 { 155 return m_socket_addr.sa; 156 } 157 158 const struct sockaddr & 159 sockaddr () const 160 { 161 return m_socket_addr.sa; 162 } 163 164 struct sockaddr_in & 165 sockaddr_in () 166 { 167 return m_socket_addr.sa_ipv4; 168 } 169 170 const struct sockaddr_in & 171 sockaddr_in () const 172 { 173 return m_socket_addr.sa_ipv4; 174 } 175 176 struct sockaddr_in6 & 177 sockaddr_in6 () 178 { 179 return m_socket_addr.sa_ipv6; 180 } 181 182 const struct sockaddr_in6 & 183 sockaddr_in6 () const 184 { 185 return m_socket_addr.sa_ipv6; 186 } 187 188 struct sockaddr_storage & 189 sockaddr_storage () 190 { 191 return m_socket_addr.sa_storage; 192 } 193 194 195 const struct sockaddr_storage & 196 sockaddr_storage () const 197 { 198 return m_socket_addr.sa_storage; 199 } 200 201 202 //------------------------------------------------------------------ 203 // Conversion operators to allow getting the contents of this class 204 // as a pointer to the appropriate structure. This allows an instance 205 // of this class to be used in calls that take one of the sockaddr 206 // structure variants without having to manally use the correct 207 // accessor function. 208 //------------------------------------------------------------------ 209 210 operator struct sockaddr * () 211 { 212 return &m_socket_addr.sa; 213 } 214 215 operator const struct sockaddr * () const 216 { 217 return &m_socket_addr.sa; 218 } 219 220 operator struct sockaddr_in * () 221 { 222 return &m_socket_addr.sa_ipv4; 223 } 224 225 operator const struct sockaddr_in * () const 226 { 227 return &m_socket_addr.sa_ipv4; 228 } 229 230 operator struct sockaddr_in6 * () 231 { 232 return &m_socket_addr.sa_ipv6; 233 } 234 235 operator const struct sockaddr_in6 * () const 236 { 237 return &m_socket_addr.sa_ipv6; 238 } 239 240 operator const struct sockaddr_storage * () const 241 { 242 return &m_socket_addr.sa_storage; 243 } 244 245 operator struct sockaddr_storage * () 246 { 247 return &m_socket_addr.sa_storage; 248 } 249 250 251protected: 252 typedef union sockaddr_tag 253 { 254 struct sockaddr sa; 255 struct sockaddr_in sa_ipv4; 256 struct sockaddr_in6 sa_ipv6; 257 struct sockaddr_storage sa_storage; 258 } sockaddr_t; 259 260 //------------------------------------------------------------------ 261 // Classes that inherit from SocketAddress can see and modify these 262 //------------------------------------------------------------------ 263 sockaddr_t m_socket_addr; 264}; 265 266 267} // namespace lldb_private 268 269 270#endif // liblldb_SocketAddress_h_ 271