X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FSocketAddress.h;h=a17a46238b1790a9772b5a5a2b4c6ab53ee6e37d;hb=965aa3eabc3dcf8695247c1237ab23d50eae4561;hp=ac1771c484064760d445605a3a8a7179e964b776;hpb=45c8738bf65a76678248189fc8f1f2eb2f35f628;p=folly.git diff --git a/folly/SocketAddress.h b/folly/SocketAddress.h index ac1771c4..a17a4623 100644 --- a/folly/SocketAddress.h +++ b/folly/SocketAddress.h @@ -1,5 +1,5 @@ /* - * Copyright 2014 Facebook, Inc. + * Copyright 2015 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,21 +20,19 @@ #include #include #include -#include #include #include #include #include #include +#include namespace folly { class SocketAddress { public: - SocketAddress() { - storage_.addr = folly::IPAddress(); - } + SocketAddress() {} /** * Construct a SocketAddress from a hostname and port. @@ -76,16 +74,17 @@ class SocketAddress { } SocketAddress(const SocketAddress& addr) { - storage_ = addr.storage_; port_ = addr.port_; if (addr.getFamily() == AF_UNIX) { storage_.un.init(addr.storage_.un); + } else { + storage_ = addr.storage_; } external_ = addr.external_; } SocketAddress& operator=(const SocketAddress& addr) { - if (getFamily() != AF_UNIX) { + if (!external_) { if (addr.getFamily() != AF_UNIX) { storage_ = addr.storage_; } else { @@ -105,7 +104,7 @@ class SocketAddress { return *this; } - SocketAddress(SocketAddress&& addr) { + SocketAddress(SocketAddress&& addr) noexcept { storage_ = addr.storage_; port_ = addr.port_; external_ = addr.external_; @@ -120,7 +119,7 @@ class SocketAddress { } ~SocketAddress() { - if (getFamily() == AF_UNIX) { + if (external_) { storage_.un.free(); } } @@ -349,7 +348,7 @@ class SocketAddress { * Returns the actual size of the storage used. */ socklen_t getAddress(sockaddr_storage* addr) const { - if (getFamily() != AF_UNIX) { + if (!external_) { return storage_.addr.toSockaddrStorage(addr, htons(port_)); } else { memcpy(addr, storage_.un.addr, sizeof(*storage_.un.addr)); @@ -363,6 +362,7 @@ class SocketAddress { socklen_t getActualSize() const; sa_family_t getFamily() const { + DCHECK(external_ || AF_UNIX != storage_.addr.family()); return external_ ? AF_UNIX : storage_.addr.family(); } @@ -509,17 +509,23 @@ class SocketAddress { struct sockaddr_un *addr; socklen_t len; + /* For debugging only, will be removed */ + uint64_t magic; + static constexpr uint64_t kMagic = 0x1234faceb00c; + socklen_t pathLength() const { return len - offsetof(struct sockaddr_un, sun_path); } void init() { addr = new sockaddr_un; + magic = kMagic; addr->sun_family = AF_UNIX; len = 0; } void init(const ExternalUnixAddr &other) { addr = new sockaddr_un; + magic = kMagic; len = other.len; memcpy(addr, other.addr, len); // Fill the rest with 0s, just for safety @@ -527,11 +533,14 @@ class SocketAddress { sizeof(struct sockaddr_un) - len); } void copy(const ExternalUnixAddr &other) { + CHECK(magic == kMagic); len = other.len; memcpy(addr, other.addr, len); } void free() { + CHECK(magic == kMagic); delete addr; + magic = 0; } }; @@ -547,12 +556,13 @@ class SocketAddress { void prepFamilyChange(sa_family_t newFamily) { if (newFamily != AF_UNIX) { - if (getFamily() == AF_UNIX) { + if (external_) { storage_.un.free(); + storage_.addr = folly::IPAddress(); } external_ = false; } else { - if (getFamily() != AF_UNIX) { + if (!external_) { storage_.un.init(); } external_ = true; @@ -569,7 +579,7 @@ class SocketAddress { union { folly::IPAddress addr{}; ExternalUnixAddr un; - } storage_; + } storage_{}; // IPAddress class does nto save zone or port, and must be saved here uint16_t port_;