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
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);
"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,
}
}
+ SocketAddress(const IPAddress& ipAddr, uint16_t port) {
+ setFromIpAddrPort(ipAddr, port);
+ }
+
SocketAddress(const SocketAddress& addr) {
port_ = addr.port_;
if (addr.getFamily() == AF_UNIX) {
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.
*
}
}
+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);