proxygen
SocketAddress.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <sys/types.h>
20 #include <cstddef>
21 #include <iosfwd>
22 #include <string>
23 
24 #include <folly/IPAddress.h>
25 #include <folly/Portability.h>
26 #include <folly/Range.h>
29 
30 namespace folly {
31 
33  public:
34  SocketAddress() = default;
35 
51  SocketAddress(const char* host, uint16_t port, bool allowNameLookup = false) {
52  // Initialize the address family first,
53  // since setFromHostPort() and setFromIpPort() will check it.
54 
55  if (allowNameLookup) {
56  setFromHostPort(host, port);
57  } else {
58  setFromIpPort(host, port);
59  }
60  }
61 
63  const std::string& host,
64  uint16_t port,
65  bool allowNameLookup = false) {
66  // Initialize the address family first,
67  // since setFromHostPort() and setFromIpPort() will check it.
68 
69  if (allowNameLookup) {
70  setFromHostPort(host.c_str(), port);
71  } else {
72  setFromIpPort(host.c_str(), port);
73  }
74  }
75 
76  SocketAddress(const IPAddress& ipAddr, uint16_t port) {
77  setFromIpAddrPort(ipAddr, port);
78  }
79 
81  port_ = addr.port_;
82  if (addr.getFamily() == AF_UNIX) {
83  storage_.un.init(addr.storage_.un);
84  } else {
85  storage_ = addr.storage_;
86  }
87  external_ = addr.external_;
88  }
89 
91  if (!external_) {
92  if (addr.getFamily() != AF_UNIX) {
93  storage_ = addr.storage_;
94  } else {
95  storage_ = addr.storage_;
96  storage_.un.init(addr.storage_.un);
97  }
98  } else {
99  if (addr.getFamily() == AF_UNIX) {
100  storage_.un.copy(addr.storage_.un);
101  } else {
102  storage_.un.free();
103  storage_ = addr.storage_;
104  }
105  }
106  port_ = addr.port_;
107  external_ = addr.external_;
108  return *this;
109  }
110 
112  storage_ = addr.storage_;
113  port_ = addr.port_;
114  external_ = addr.external_;
115  addr.external_ = false;
116  }
117 
119  std::swap(storage_, addr.storage_);
120  std::swap(port_, addr.port_);
121  std::swap(external_, addr.external_);
122  return *this;
123  }
124 
126  if (external_) {
127  storage_.un.free();
128  }
129  }
130 
131  bool isInitialized() const {
132  return (getFamily() != AF_UNSPEC);
133  }
134 
148  bool isPrivateAddress() const;
149 
153  bool isLoopbackAddress() const;
154 
155  void reset() {
156  if (external_) {
157  storage_.un.free();
158  }
159  storage_.addr = folly::IPAddress();
160  external_ = false;
161  }
162 
177  void setFromHostPort(const char* host, uint16_t port);
178 
179  void setFromHostPort(const std::string& host, uint16_t port) {
180  setFromHostPort(host.c_str(), port);
181  }
182 
195  void setFromIpPort(const char* ip, uint16_t port);
196 
197  void setFromIpPort(const std::string& ip, uint16_t port) {
198  setFromIpPort(ip.c_str(), port);
199  }
200 
207  void setFromIpAddrPort(const IPAddress& ip, uint16_t port);
208 
220  void setFromLocalPort(uint16_t port);
221 
229  void setFromLocalPort(const char* port);
230  void setFromLocalPort(const std::string& port) {
231  return setFromLocalPort(port.c_str());
232  }
233 
246  void setFromLocalIpPort(const char* addressAndPort);
247  void setFromLocalIpPort(const std::string& addressAndPort) {
248  return setFromLocalIpPort(addressAndPort.c_str());
249  }
250 
260  void setFromIpPort(const char* addressAndPort);
261  void setFromIpPort(const std::string& addressAndPort) {
262  return setFromIpPort(addressAndPort.c_str());
263  }
264 
275  void setFromHostPort(const char* hostAndPort);
276  void setFromHostPort(const std::string& hostAndPort) {
277  return setFromHostPort(hostAndPort.c_str());
278  }
279 
287  static int getPortFrom(const struct sockaddr* address);
288 
295  static const char* getFamilyNameFrom(
296  const struct sockaddr* address,
297  const char* defaultResult = nullptr);
298 
304  void setFromPath(StringPiece path);
305 
306  void setFromPath(const char* path, size_t length) {
307  setFromPath(StringPiece{path, length});
308  }
309 
319  addr.setFromPath(path);
320  return addr;
321  }
322 
328  void setFromPeerAddress(int socket);
329  void setFromPeerAddress(NetworkSocket socket);
330 
336  void setFromLocalAddress(int socket);
337  void setFromLocalAddress(NetworkSocket socket);
338 
350  void setFromSockaddr(const struct sockaddr* address);
351 
362  void setFromSockaddr(const struct sockaddr* address, socklen_t addrlen);
363 
367  void setFromSockaddr(const struct sockaddr_in* address);
368 
372  void setFromSockaddr(const struct sockaddr_in6* address);
373 
386  void setFromSockaddr(const struct sockaddr_un* address, socklen_t addrlen);
387 
393  socklen_t getAddress(sockaddr_storage* addr) const {
394  if (!external_) {
395  return storage_.addr.toSockaddrStorage(addr, htons(port_));
396  } else {
397  memcpy(addr, storage_.un.addr, sizeof(*storage_.un.addr));
398  return storage_.un.len;
399  }
400  }
401 
402  const folly::IPAddress& getIPAddress() const;
403 
404  // Deprecated: getAddress() above returns the same size as getActualSize()
405  socklen_t getActualSize() const;
406 
407  sa_family_t getFamily() const {
408  DCHECK(external_ || AF_UNIX != storage_.addr.family());
409  return external_ ? sa_family_t(AF_UNIX) : storage_.addr.family();
410  }
411 
412  bool empty() const {
413  return getFamily() == AF_UNSPEC;
414  }
415 
422  std::string getAddressStr() const;
423 
430  void getAddressStr(char* buf, size_t buflen) const;
431 
435  bool isFamilyInet() const;
436 
441 
449  uint16_t getPort() const;
450 
456  void setPort(uint16_t port);
457 
461  bool isIPv4Mapped() const {
462  return (getFamily() == AF_INET6 && storage_.addr.isIPv4Mapped());
463  }
464 
470  void convertToIPv4();
471 
480  bool tryConvertToIPv4();
481 
486  bool mapToIPv6();
487 
497  std::string getHostStr() const;
498 
511  std::string getPath() const;
512 
519  std::string describe() const;
520 
521  bool operator==(const SocketAddress& other) const;
522  bool operator!=(const SocketAddress& other) const {
523  return !(*this == other);
524  }
525 
532  bool prefixMatch(const SocketAddress& other, unsigned prefixLength) const;
533 
537  bool operator<(const SocketAddress& other) const;
538 
542  size_t hash() const;
543 
544  private:
556  struct sockaddr_un* addr;
557  socklen_t len;
558 
559  socklen_t pathLength() const {
560  return socklen_t(len - offsetof(struct sockaddr_un, sun_path));
561  }
562 
563  void init() {
564  addr = new struct sockaddr_un;
565  addr->sun_family = AF_UNIX;
566  len = 0;
567  }
568  void init(const ExternalUnixAddr& other) {
569  addr = new struct sockaddr_un;
570  len = other.len;
571  memcpy(addr, other.addr, size_t(len));
572  }
573  void copy(const ExternalUnixAddr& other) {
574  len = other.len;
575  memcpy(addr, other.addr, size_t(len));
576  }
577  void free() {
578  delete addr;
579  }
580  };
581 
582  struct addrinfo* getAddrInfo(const char* host, uint16_t port, int flags);
583  struct addrinfo* getAddrInfo(const char* host, const char* port, int flags);
584  void setFromAddrInfo(const struct addrinfo* results);
585  void setFromLocalAddr(const struct addrinfo* results);
586  void setFromSocket(
587  NetworkSocket socket,
588  int (*fn)(NetworkSocket, struct sockaddr*, socklen_t*));
589  std::string getIpString(int flags) const;
590  void getIpString(char* buf, size_t buflen, int flags) const;
591 
592  void updateUnixAddressLength(socklen_t addrlen);
593 
594  /*
595  * storage_ contains room for a full IPv4 or IPv6 address, so they can be
596  * stored inline without a separate allocation on the heap.
597  *
598  * If we need to store a Unix socket address, ExternalUnixAddr is a shim to
599  * track a struct sockaddr_un allocated separately on the heap.
600  */
601  union AddrStorage {
602  folly::IPAddress addr;
604  AddrStorage() : addr() {}
605  } storage_{};
606  // IPAddress class does nto save zone or port, and must be saved here
608 
609  bool external_{false};
610 };
611 
618 size_t hash_value(const SocketAddress& address);
619 
620 std::ostream& operator<<(std::ostream& os, const SocketAddress& addr);
621 } // namespace folly
622 
623 namespace std {
624 
625 // Provide an implementation for std::hash<SocketAddress>
626 template <>
628  size_t operator()(const folly::SocketAddress& addr) const {
629  return addr.hash();
630  }
631 };
632 } // namespace std
void setFromLocalIpPort(const char *addressAndPort)
bool operator!=(const SocketAddress &other) const
void setFromPath(StringPiece path)
void setFromIpAddrPort(const IPAddress &ip, uint16_t port)
flags
Definition: http_parser.h:127
void setFromPeerAddress(int socket)
bool prefixMatch(const SocketAddress &other, unsigned prefixLength) const
void init(const ExternalUnixAddr &other)
void setFromHostPort(const char *host, uint16_t port)
bool empty() const
void setFromLocalPort(uint16_t port)
SocketAddress & operator=(const SocketAddress &addr)
Definition: SocketAddress.h:90
void setFromIpPort(const std::string &addressAndPort)
bool operator==(const SocketAddress &other) const
std::string getFullyQualified() const
bool isIPv4Mapped() const
SocketAddress(const char *host, uint16_t port, bool allowNameLookup=false)
Definition: SocketAddress.h:51
SocketAddress & operator=(SocketAddress &&addr)
void setFromLocalIpPort(const std::string &addressAndPort)
socklen_t getActualSize() const
void updateUnixAddressLength(socklen_t addrlen)
STL namespace.
void setFromSockaddr(const struct sockaddr *address)
void copy(const ExternalUnixAddr &other)
std::string getPath() const
void setFromPath(const char *path, size_t length)
void setFromLocalPort(const std::string &port)
SocketAddress(const SocketAddress &addr)
Definition: SocketAddress.h:80
uint16_t getPort() const
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
std::string describe() const
requires E e noexcept(noexcept(s.error(std::move(e))))
void setFromAddrInfo(const struct addrinfo *results)
bool operator<(const SocketAddress &other) const
void setPort(uint16_t port)
struct addrinfo * getAddrInfo(const char *host, uint16_t port, int flags)
bool isFamilyInet() const
static int getPortFrom(const struct sockaddr *address)
sa_family_t getFamily() const
void setFromHostPort(const std::string &hostAndPort)
std::string getIpString(int flags) const
bool isInitialized() const
size_t hash_value(const IPAddress &addr)
Definition: IPAddress.cpp:34
void setFromLocalAddr(const struct addrinfo *results)
const folly::IPAddress & getIPAddress() const
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
socklen_t getAddress(sockaddr_storage *addr) const
size_t operator()(const folly::SocketAddress &addr) const
void setFromIpPort(const char *ip, uint16_t port)
size_t hash() const
union folly::SocketAddress::AddrStorage storage_
void setFromLocalAddress(int socket)
const char * string
Definition: Conv.cpp:212
std::string getHostStr() const
bool isPrivateAddress() const
void setFromSocket(NetworkSocket socket, int(*fn)(NetworkSocket, struct sockaddr *, socklen_t *))
static const char * getFamilyNameFrom(const struct sockaddr *address, const char *defaultResult=nullptr)
SocketAddress(const std::string &host, uint16_t port, bool allowNameLookup=false)
Definition: SocketAddress.h:62
void swap(SwapTrackingAlloc< T > &, SwapTrackingAlloc< T > &)
Definition: F14TestUtil.h:414
static SocketAddress makeFromPath(StringPiece path)
SocketAddress(SocketAddress &&addr) noexcept
void setFromHostPort(const std::string &host, uint16_t port)
ThreadPoolListHook * addr
bool isLoopbackAddress() const
SocketAddress(const IPAddress &ipAddr, uint16_t port)
Definition: SocketAddress.h:76
std::string getAddressStr() const
void setFromIpPort(const std::string &ip, uint16_t port)
std::ostream & operator<<(std::ostream &out, dynamic const &d)
Definition: dynamic-inl.h:1158