Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:49

0001 //===-- SocketAddress.h -----------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_HOST_SOCKETADDRESS_H
0010 #define LLDB_HOST_SOCKETADDRESS_H
0011 
0012 #include <cstdint>
0013 
0014 #ifdef _WIN32
0015 #include "lldb/Host/windows/windows.h"
0016 #include <winsock2.h>
0017 #include <ws2tcpip.h>
0018 typedef ADDRESS_FAMILY sa_family_t;
0019 #else
0020 #include <netdb.h>
0021 #include <netinet/in.h>
0022 #include <sys/socket.h>
0023 #endif
0024 
0025 #if defined(__FreeBSD__)
0026 #include <sys/types.h>
0027 #endif
0028 
0029 #include <string>
0030 #include <vector>
0031 
0032 namespace lldb_private {
0033 
0034 class SocketAddress {
0035 public:
0036   // Static method to get all address information for a host and/or service
0037   static std::vector<SocketAddress>
0038   GetAddressInfo(const char *hostname, const char *servname, int ai_family,
0039                  int ai_socktype, int ai_protocol, int ai_flags = 0);
0040 
0041   // Constructors and Destructors
0042   SocketAddress();
0043   SocketAddress(const struct addrinfo *addr_info);
0044   SocketAddress(const struct sockaddr &s);
0045   SocketAddress(const struct sockaddr_in &s);
0046   SocketAddress(const struct sockaddr_in6 &s);
0047   SocketAddress(const struct sockaddr_storage &s);
0048   ~SocketAddress();
0049 
0050   // Operators
0051   const SocketAddress &operator=(const struct addrinfo *addr_info);
0052 
0053   const SocketAddress &operator=(const struct sockaddr &s);
0054 
0055   const SocketAddress &operator=(const struct sockaddr_in &s);
0056 
0057   const SocketAddress &operator=(const struct sockaddr_in6 &s);
0058 
0059   const SocketAddress &operator=(const struct sockaddr_storage &s);
0060 
0061   bool operator==(const SocketAddress &rhs) const;
0062   bool operator!=(const SocketAddress &rhs) const;
0063 
0064   // Clear the contents of this socket address
0065   void Clear();
0066 
0067   // Get the length for the current socket address family
0068   socklen_t GetLength() const;
0069 
0070   // Get the max length for the largest socket address supported.
0071   static socklen_t GetMaxLength();
0072 
0073   // Get the socket address family
0074   sa_family_t GetFamily() const;
0075 
0076   // Set the socket address family
0077   void SetFamily(sa_family_t family);
0078 
0079   // Get the address
0080   std::string GetIPAddress() const;
0081 
0082   // Get the port if the socket address for the family has a port
0083   uint16_t GetPort() const;
0084 
0085   // Set the port if the socket address for the family has a port. The family
0086   // must be set correctly prior to calling this function.
0087   bool SetPort(uint16_t port);
0088 
0089   // Set the socket address according to the first match from a call to
0090   // getaddrinfo() (or equivalent functions for systems that don't have
0091   // getaddrinfo(). If "addr_info_ptr" is not NULL, it will get filled in with
0092   // the match that was used to populate this socket address.
0093   bool
0094   getaddrinfo(const char *host,    // Hostname ("foo.bar.com" or "foo" or IP
0095                                    // address string ("123.234.12.1" or
0096                                    // "2001:0db8:85a3:0000:0000:8a2e:0370:7334")
0097               const char *service, // Protocol name ("tcp", "http", etc) or a
0098                                    // raw port number string ("81")
0099               int ai_family = PF_UNSPEC, int ai_socktype = 0,
0100               int ai_protocol = 0, int ai_flags = 0);
0101 
0102   // Quick way to set the SocketAddress to localhost given the family. Returns
0103   // true if successful, false if "family" doesn't support localhost or if
0104   // "family" is not supported by this class.
0105   bool SetToLocalhost(sa_family_t family, uint16_t port);
0106 
0107   bool SetToAnyAddress(sa_family_t family, uint16_t port);
0108 
0109   // Returns true if there is a valid socket address in this object.
0110   bool IsValid() const;
0111 
0112   // Returns true if the socket is INADDR_ANY
0113   bool IsAnyAddr() const;
0114 
0115   // Returns true if the socket is INADDR_LOOPBACK
0116   bool IsLocalhost() const;
0117 
0118   // Direct access to all of the sockaddr structures
0119   struct sockaddr &sockaddr() {
0120     return m_socket_addr.sa;
0121   }
0122 
0123   const struct sockaddr &sockaddr() const { return m_socket_addr.sa; }
0124 
0125   struct sockaddr_in &sockaddr_in() {
0126     return m_socket_addr.sa_ipv4;
0127   }
0128 
0129   const struct sockaddr_in &sockaddr_in() const {
0130     return m_socket_addr.sa_ipv4;
0131   }
0132 
0133   struct sockaddr_in6 &sockaddr_in6() {
0134     return m_socket_addr.sa_ipv6;
0135   }
0136 
0137   const struct sockaddr_in6 &sockaddr_in6() const {
0138     return m_socket_addr.sa_ipv6;
0139   }
0140 
0141   struct sockaddr_storage &sockaddr_storage() {
0142     return m_socket_addr.sa_storage;
0143   }
0144 
0145   const struct sockaddr_storage &sockaddr_storage() const {
0146     return m_socket_addr.sa_storage;
0147   }
0148 
0149   // Conversion operators to allow getting the contents of this class as a
0150   // pointer to the appropriate structure. This allows an instance of this
0151   // class to be used in calls that take one of the sockaddr structure variants
0152   // without having to manually use the correct accessor function.
0153 
0154   operator struct sockaddr *() { return &m_socket_addr.sa; }
0155 
0156   operator const struct sockaddr *() const { return &m_socket_addr.sa; }
0157 
0158   operator struct sockaddr_in *() { return &m_socket_addr.sa_ipv4; }
0159 
0160   operator const struct sockaddr_in *() const { return &m_socket_addr.sa_ipv4; }
0161 
0162   operator struct sockaddr_in6 *() { return &m_socket_addr.sa_ipv6; }
0163 
0164   operator const struct sockaddr_in6 *() const {
0165     return &m_socket_addr.sa_ipv6;
0166   }
0167 
0168   operator const struct sockaddr_storage *() const {
0169     return &m_socket_addr.sa_storage;
0170   }
0171 
0172   operator struct sockaddr_storage *() { return &m_socket_addr.sa_storage; }
0173 
0174 protected:
0175   typedef union sockaddr_tag {
0176     struct sockaddr sa;
0177     struct sockaddr_in sa_ipv4;
0178     struct sockaddr_in6 sa_ipv6;
0179     struct sockaddr_storage sa_storage;
0180   } sockaddr_t;
0181 
0182   // Classes that inherit from SocketAddress can see and modify these
0183   sockaddr_t m_socket_addr;
0184 };
0185 
0186 } // namespace lldb_private
0187 
0188 #endif // LLDB_HOST_SOCKETADDRESS_H