From: Naizhi Li Date: Wed, 22 Apr 2015 20:38:24 +0000 (-0700) Subject: Add support for creating SocketAddress directly from IPAddress object X-Git-Tag: v0.36.0~13 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=6546b7b9f673f3bb65fe429c2b1c8670a4502a51;p=folly.git 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 --- diff --git a/folly/SocketAddress.cpp b/folly/SocketAddress.cpp index 292ba056..dbed1747 100644 --- a/folly/SocketAddress.cpp +++ b/folly/SocketAddress.cpp @@ -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, diff --git a/folly/SocketAddress.h b/folly/SocketAddress.h index a17a4623..7915c96f 100644 --- a/folly/SocketAddress.h +++ b/folly/SocketAddress.h @@ -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. * diff --git a/folly/test/SocketAddressTest.cpp b/folly/test/SocketAddressTest.cpp index a43d2c1d..5125cc37 100644 --- a/folly/test/SocketAddressTest.cpp +++ b/folly/test/SocketAddressTest.cpp @@ -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);