empiredan commented on code in PR #1869:
URL:
https://github.com/apache/incubator-pegasus/pull/1869#discussion_r1465914463
##########
src/runtime/rpc/rpc_address.cpp:
##########
@@ -48,23 +50,27 @@ const rpc_address rpc_address::s_invalid_address;
/*static*/
uint32_t rpc_address::ipv4_from_host(const char *name)
{
- sockaddr_in addr;
- memset(&addr, 0, sizeof(addr));
-
- addr.sin_family = AF_INET;
- if ((addr.sin_addr.s_addr = inet_addr(name)) == (unsigned int)(-1)) {
- // TODO(yingchun): use getaddrinfo instead
- hostent *hp = ::gethostbyname(name);
- if (dsn_unlikely(hp == nullptr)) {
- LOG_ERROR("gethostbyname failed, name = {}, err = {}", name,
hstrerror(h_errno));
- return 0;
- }
+ struct addrinfo hints;
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_INET;
+ hints.ai_socktype = SOCK_STREAM;
+ addrinfo *res = nullptr;
- memcpy((void *)&(addr.sin_addr.s_addr), (const void *)hp->h_addr,
(size_t)hp->h_length);
+ 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));
+ return 0;
}
+ CHECK(res->ai_family == AF_INET, "");
+ struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
Review Comment:
```suggestion
auto *ipv4 = reinterpret_cast<struct sockaddr_in *>(res->ai_addr);
```
##########
src/runtime/rpc/rpc_address.cpp:
##########
@@ -48,23 +50,27 @@ const rpc_address rpc_address::s_invalid_address;
/*static*/
uint32_t rpc_address::ipv4_from_host(const char *name)
{
- sockaddr_in addr;
- memset(&addr, 0, sizeof(addr));
-
- addr.sin_family = AF_INET;
- if ((addr.sin_addr.s_addr = inet_addr(name)) == (unsigned int)(-1)) {
- // TODO(yingchun): use getaddrinfo instead
- hostent *hp = ::gethostbyname(name);
- if (dsn_unlikely(hp == nullptr)) {
- LOG_ERROR("gethostbyname failed, name = {}, err = {}", name,
hstrerror(h_errno));
- return 0;
- }
+ struct addrinfo hints;
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_INET;
+ hints.ai_socktype = SOCK_STREAM;
+ addrinfo *res = nullptr;
- memcpy((void *)&(addr.sin_addr.s_addr), (const void *)hp->h_addr,
(size_t)hp->h_length);
+ const int rc = getaddrinfo(name, nullptr, &hints, &res);
Review Comment:
Use `freeaddrinfo()` to release memory.
##########
src/runtime/rpc/rpc_address.cpp:
##########
@@ -48,23 +50,27 @@ const rpc_address rpc_address::s_invalid_address;
/*static*/
uint32_t rpc_address::ipv4_from_host(const char *name)
{
- sockaddr_in addr;
- memset(&addr, 0, sizeof(addr));
-
- addr.sin_family = AF_INET;
- if ((addr.sin_addr.s_addr = inet_addr(name)) == (unsigned int)(-1)) {
- // TODO(yingchun): use getaddrinfo instead
- hostent *hp = ::gethostbyname(name);
- if (dsn_unlikely(hp == nullptr)) {
- LOG_ERROR("gethostbyname failed, name = {}, err = {}", name,
hstrerror(h_errno));
- return 0;
- }
+ struct addrinfo hints;
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_INET;
+ hints.ai_socktype = SOCK_STREAM;
+ addrinfo *res = nullptr;
- memcpy((void *)&(addr.sin_addr.s_addr), (const void *)hp->h_addr,
(size_t)hp->h_length);
+ 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));
+ return 0;
}
+ CHECK(res->ai_family == AF_INET, "");
+ struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
// converts from network byte order to host byte order
- return (uint32_t)ntohl(addr.sin_addr.s_addr);
+ return (uint32_t)ntohl(ipv4->sin_addr.s_addr);
Review Comment:
No need to do conversion, since the return type has been `uint32_t`.
```
#include <arpa/inet.h>
uint32_t ntohl(uint32_t netlong);
```
```suggestion
return ntohl(ipv4->sin_addr.s_addr);
```
##########
src/runtime/rpc/rpc_address.cpp:
##########
@@ -48,23 +50,27 @@ const rpc_address rpc_address::s_invalid_address;
/*static*/
uint32_t rpc_address::ipv4_from_host(const char *name)
{
- sockaddr_in addr;
- memset(&addr, 0, sizeof(addr));
-
- addr.sin_family = AF_INET;
- if ((addr.sin_addr.s_addr = inet_addr(name)) == (unsigned int)(-1)) {
- // TODO(yingchun): use getaddrinfo instead
- hostent *hp = ::gethostbyname(name);
- if (dsn_unlikely(hp == nullptr)) {
- LOG_ERROR("gethostbyname failed, name = {}, err = {}", name,
hstrerror(h_errno));
- return 0;
- }
+ struct addrinfo hints;
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_INET;
+ hints.ai_socktype = SOCK_STREAM;
+ addrinfo *res = nullptr;
- memcpy((void *)&(addr.sin_addr.s_addr), (const void *)hp->h_addr,
(size_t)hp->h_length);
+ 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));
+ return 0;
}
+ CHECK(res->ai_family == AF_INET, "");
Review Comment:
```suggestion
CHECK_EQ(res->ai_family, AF_INET);
```
##########
src/runtime/rpc/rpc_address.cpp:
##########
@@ -48,23 +50,27 @@ const rpc_address rpc_address::s_invalid_address;
/*static*/
uint32_t rpc_address::ipv4_from_host(const char *name)
{
- sockaddr_in addr;
- memset(&addr, 0, sizeof(addr));
-
- addr.sin_family = AF_INET;
- if ((addr.sin_addr.s_addr = inet_addr(name)) == (unsigned int)(-1)) {
- // TODO(yingchun): use getaddrinfo instead
- hostent *hp = ::gethostbyname(name);
- if (dsn_unlikely(hp == nullptr)) {
- LOG_ERROR("gethostbyname failed, name = {}, err = {}", name,
hstrerror(h_errno));
- return 0;
- }
+ struct addrinfo hints;
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_INET;
+ hints.ai_socktype = SOCK_STREAM;
+ addrinfo *res = nullptr;
- memcpy((void *)&(addr.sin_addr.s_addr), (const void *)hp->h_addr,
(size_t)hp->h_length);
+ const int rc = getaddrinfo(name, nullptr, &hints, &res);
+ const int err = errno; // preserving the errno from the getaddrinfo() call
+
+ if (rc != 0) {
Review Comment:
```suggestion
if (dsn_unlikely(rc != 0)) {
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]