Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Dan Wang <[email protected]>
  • Loading branch information
GehaFearless and empiredan committed Jan 26, 2024
1 parent 97cffea commit d4e29a3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 47 deletions.
43 changes: 29 additions & 14 deletions src/runtime/rpc/rpc_address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#include "absl/strings/string_view.h"
#include "runtime/rpc/group_address.h"
#include "utils/error_code.h"
#include "utils/fixed_size_buffer_pool.h"
#include "utils/fmt_logging.h"
#include "utils/ports.h"
Expand All @@ -44,6 +45,28 @@
#include "utils/strings.h"

namespace dsn {
/*static*/
error_s rpc_address::GetAddrInfo(const std::string &hostname, const addrinfo &hints, AddrInfo *info)
{
addrinfo *res = nullptr;
const int rc = getaddrinfo(hostname.c_str(), nullptr, &hints, &res);
const int err = errno; // preserving the errno from the getaddrinfo() call
AddrInfo result(res, ::freeaddrinfo);
if (rc != 0) {
if (rc == EAI_SYSTEM) {
LOG_ERROR(
"getaddrinfo failed, name = {}, err = {}", hostname, utils::safe_strerror(rc));
return error_s::make(ERR_NETWORK_FAILURE, utils::safe_strerror(err));
}
LOG_ERROR("getaddrinfo failed, name = {}, err = {}", hostname, gai_strerror(rc));
return error_s::make(ERR_NETWORK_FAILURE, gai_strerror(rc));
}

if (info != nullptr) {
info->swap(result);
}
return error_s::ok();
}

const rpc_address rpc_address::s_invalid_address;

Expand All @@ -54,23 +77,15 @@ uint32_t rpc_address::ipv4_from_host(const char *name)
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
addrinfo *res = nullptr;

const int rc = getaddrinfo(name, nullptr, &hints, &res);
const int err = errno; // preserving the errno from the getaddrinfo() call

if (rc != 0) {
if (rc == EAI_SYSTEM) {
LOG_ERROR("getaddrinfo failed, name = {}, err = {}", name, utils::safe_strerror(err));
}
LOG_ERROR("getaddrinfo failed, name = {}, err = {}", name, gai_strerror(err));
AddrInfo result;
if (dsn_unlikely(!GetAddrInfo(name, hints, &result).is_ok())) {
return 0;
}

CHECK(res->ai_family == AF_INET, "");
struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
CHECK_EQ(result.get()->ai_family, AF_INET);
auto *ipv4 = reinterpret_cast<struct sockaddr_in *>(result.get()->ai_addr);
// converts from network byte order to host byte order
return (uint32_t)ntohl(ipv4->sin_addr.s_addr);
uint32_t iphost = ntohl(ipv4->sin_addr.s_addr);
return iphost;
}

/*static*/
Expand Down
7 changes: 7 additions & 0 deletions src/runtime/rpc/rpc_address.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@
#include <cstdint>
// IWYU pragma: no_include <experimental/string_view>
#include <functional>
#include <memory>
#include <sstream>
#include <string>
#include <string_view>

#include "utils/errors.h"
#include "utils/fmt_utils.h"

struct addrinfo;

namespace apache {
namespace thrift {
namespace protocol {
Expand All @@ -54,6 +58,8 @@ USER_DEFINED_ENUM_FORMATTER(dsn_host_type_t)

namespace dsn {

using AddrInfo = std::unique_ptr<addrinfo, std::function<void(addrinfo *)>>;

class rpc_group_address;

class rpc_address
Expand All @@ -64,6 +70,7 @@ class rpc_address
static bool is_site_local_address(uint32_t ip_net);
static uint32_t ipv4_from_host(const char *hostname);
static uint32_t ipv4_from_network_interface(const char *network_interface);
static error_s GetAddrInfo(const std::string &hostname, const addrinfo &hints, AddrInfo *info);

~rpc_address();

Expand Down
34 changes: 2 additions & 32 deletions src/runtime/rpc/rpc_host_port.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* under the License.
*/

#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
Expand All @@ -31,38 +30,13 @@
#include "runtime/rpc/rpc_host_port.h"
#include "utils/error_code.h"
#include "utils/ports.h"
#include "utils/safe_strerror_posix.h"
#include "utils/string_conv.h"
#include "utils/utils.h"

namespace dsn {

const host_port host_port::s_invalid_host_port;

namespace {

using AddrInfo = std::unique_ptr<addrinfo, std::function<void(addrinfo *)>>;

error_s GetAddrInfo(const std::string &hostname, const addrinfo &hints, AddrInfo *info)
{
addrinfo *res = nullptr;
const int rc = getaddrinfo(hostname.c_str(), nullptr, &hints, &res);
const int err = errno; // preserving the errno from the getaddrinfo() call
AddrInfo result(res, ::freeaddrinfo);
if (rc != 0) {
if (rc == EAI_SYSTEM) {
return error_s::make(ERR_NETWORK_FAILURE, utils::safe_strerror(err));
}
return error_s::make(ERR_NETWORK_FAILURE, gai_strerror(rc));
}

if (info != nullptr) {
info->swap(result);
}
return error_s::ok();
}
}

host_port::host_port(std::string host, uint16_t port)
: _host(std::move(host)), _port(port), _type(HOST_TYPE_IPV4)
{
Expand Down Expand Up @@ -100,11 +74,7 @@ bool host_port::from_string(const std::string &s)
return false;
}

struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if (dsn_unlikely(!GetAddrInfo(_host, hints, nullptr))) {
if (dsn_unlikely(rpc_address::ipv4_from_host(_host.c_str()) == 0)) {
return false;
}

Expand Down Expand Up @@ -194,7 +164,7 @@ error_s host_port::resolve_addresses(std::vector<rpc_address> &addresses) const
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
AddrInfo result;
RETURN_NOT_OK(GetAddrInfo(_host, hints, &result));
RETURN_NOT_OK(rpc_address::GetAddrInfo(_host, hints, &result));

// DNS may return the same host multiple times. We want to return only the unique
// addresses, but in the same order as DNS returned them. To do so, we keep track
Expand Down
2 changes: 1 addition & 1 deletion src/utils/test/lock.std.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <thread>

#include "gtest/gtest.h"
#include "runtime/rpc/rpc_address.h"
#include "utils/enum_helper.h"
#include "utils/lockp.std.h"

using namespace dsn;
Expand Down

0 comments on commit d4e29a3

Please sign in to comment.