Commit 6546b7b9 authored by Naizhi Li's avatar Naizhi Li Committed by Alecs King

Add support for creating SocketAddress directly from IPAddress object

Summary:
Today it's hard to create SocketAddress from IPAddress
without converting to strings and back. This change adds this capability.

Test Plan: build

Reviewed By: yfeldblum@fb.com

Subscribers: ps, bmatheny, folly-diffs@, yfeldblum, chalfant

FB internal diff: D2011879

Signature: t1:2011879:1429733811:c05fc2d5ef5e9fdbbbb54ad26b4d1e3c0ad06dfa
parent 75aef07c
......@@ -142,6 +142,15 @@ void SocketAddress::setFromIpPort(const char* ip, uint16_t port) {
setFromAddrInfo(results.info);
}
void SocketAddress::setFromIpAddrPort(const IPAddress& ipAddr, uint16_t port) {
if (external_) {
storage_.un.free();
external_ = false;
}
storage_.addr = ipAddr;
port_ = port;
}
void SocketAddress::setFromLocalPort(uint16_t port) {
ScopedAddrInfo results(getAddrInfo(nullptr, port, AI_ADDRCONFIG));
setFromLocalAddr(results.info);
......@@ -220,12 +229,8 @@ void SocketAddress::setFromSockaddr(const struct sockaddr* address) {
"SocketAddress::setFromSockaddr() called "
"with unsupported address type");
}
if (external_) {
storage_.un.free();
external_ = false;
}
storage_.addr = folly::IPAddress(address);
port_ = port;
setFromIpAddrPort(folly::IPAddress(address), port);
}
void SocketAddress::setFromSockaddr(const struct sockaddr* address,
......
......@@ -73,6 +73,10 @@ class SocketAddress {
}
}
SocketAddress(const IPAddress& ipAddr, uint16_t port) {
setFromIpAddrPort(ipAddr, port);
}
SocketAddress(const SocketAddress& addr) {
port_ = addr.port_;
if (addr.getFamily() == AF_UNIX) {
......@@ -190,6 +194,14 @@ class SocketAddress {
setFromIpPort(ip.c_str(), port);
}
/**
* Initialize this SocketAddress from an IPAddress struct and port.
*
* @param ip The IP address in IPAddress format
* @param port The port (in host byte order)
*/
void setFromIpAddrPort(const IPAddress& ip, uint16_t port);
/**
* Initialize this SocketAddress from a local port number.
*
......
......@@ -60,6 +60,23 @@ TEST(SocketAddress, IPv4ToStringConversion) {
}
}
TEST(SocketAddress, SetFromIpAddressPort) {
SocketAddress addr;
folly::IPAddress ipAddr("123.234.0.23");
addr.setFromIpAddrPort(ipAddr, 8888);
EXPECT_EQ(addr.getFamily(), AF_INET);
EXPECT_EQ(addr.getAddressStr(), "123.234.0.23");
EXPECT_EQ(addr.getIPAddress(), ipAddr);
EXPECT_EQ(addr.getPort(), 8888);
folly::IPAddress ip6Addr("2620:0:1cfe:face:b00c::3");
SocketAddress addr6(ip6Addr, 8888);
EXPECT_EQ(addr6.getFamily(), AF_INET6);
EXPECT_EQ(addr6.getAddressStr(), "2620:0:1cfe:face:b00c::3");
EXPECT_EQ(addr6.getIPAddress(), ip6Addr);
EXPECT_EQ(addr6.getPort(), 8888);
}
TEST(SocketAddress, SetFromIpv4) {
SocketAddress addr;
addr.setFromIpPort("255.254.253.252", 8888);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment