empiredan commented on code in PR #1897:
URL:
https://github.com/apache/incubator-pegasus/pull/1897#discussion_r1497203029
##########
src/runtime/rpc/rpc_address.cpp:
##########
@@ -205,51 +246,85 @@ const char *rpc_address::ipv4_str() const
if (_addr.v4.type == HOST_TYPE_IPV4) {
net_addr.s_addr = htonl(ip());
- inet_ntop(AF_INET, &net_addr, p, sz);
+ ::inet_ntop(AF_INET, &net_addr, p, sz);
} else {
p = (char *)"invalid_ipv4";
}
return p;
}
-bool rpc_address::from_string_ipv4(const char *s)
+rpc_address rpc_address::from_ip_port(std::string_view ip_port)
{
- set_invalid();
- std::string ip_port(s);
- auto pos = ip_port.find_last_of(':');
- if (pos == std::string::npos) {
- return false;
+ std::string_view ip;
+ uint16_t port;
+ if (dsn_unlikely(!extract_host_port(ip_port, ip, port))) {
+ return {};
}
- std::string ip = ip_port.substr(0, pos);
- std::string port = ip_port.substr(pos + 1);
- // check port
- unsigned int port_num;
- if (!internal::buf2unsigned(port, port_num) || port_num > UINT16_MAX) {
- return false;
+
+ // Use std::string(ip) to add a null terminator.
+ return from_ip_port(std::string(ip), port);
+}
+
+rpc_address rpc_address::from_ip_port(std::string_view ip, uint16_t port)
+{
+ // Check is IPv4 integer.
+ uint32_t ip_num;
+ int ret = ::inet_pton(AF_INET, ip.data(), &ip_num);
+ switch (ret) {
+ case 1:
+ // ::inet_pton() returns 1 on success (network address was
successfully converted)
+ return {ntohl(ip_num), port};
+ case -1:
+ LOG_ERROR(
+ "{}: fail to convert '{}:{}' to rpc_address",
utils::safe_strerror(errno), ip, port);
+ break;
+ default:
+ LOG_ERROR("'{}' does not contain a character string representing a
valid network address",
+ ip);
+ break;
}
- // check localhost & IP
- uint32_t ip_addr;
- if (ip == "localhost" || inet_pton(AF_INET, ip.c_str(), &ip_addr)) {
- assign_ipv4(ip.c_str(), (uint16_t)port_num);
- return true;
+ return {};
+}
+
+rpc_address rpc_address::from_host_port(std::string_view host_port)
+{
+ std::string_view host;
+ uint16_t port;
+ if (dsn_unlikely(!extract_host_port(host_port, host, port))) {
+ return {};
}
- return false;
+
+ // Use std::string(host) to add a null terminator.
+ return from_host_port(std::string(host), port);
+}
+
+rpc_address rpc_address::from_host_port(std::string_view hostname, uint16_t
port)
+{
+ uint32_t ip = 0;
+ if (!ipv4_from_host(hostname, &ip)) {
+ return {};
+ }
+
+ rpc_address addr;
+ addr._addr.v4.type = HOST_TYPE_IPV4;
+ addr._addr.v4.ip = ip;
+ addr._addr.v4.port = port;
+ return addr;
}
const char *rpc_address::to_string() const
{
char *p = bf.next();
Review Comment:
```suggestion
char *p = nullptr;
```
##########
src/runtime/rpc/rpc_address.cpp:
##########
@@ -205,51 +246,85 @@ const char *rpc_address::ipv4_str() const
if (_addr.v4.type == HOST_TYPE_IPV4) {
net_addr.s_addr = htonl(ip());
- inet_ntop(AF_INET, &net_addr, p, sz);
+ ::inet_ntop(AF_INET, &net_addr, p, sz);
} else {
p = (char *)"invalid_ipv4";
}
return p;
}
-bool rpc_address::from_string_ipv4(const char *s)
+rpc_address rpc_address::from_ip_port(std::string_view ip_port)
{
- set_invalid();
- std::string ip_port(s);
- auto pos = ip_port.find_last_of(':');
- if (pos == std::string::npos) {
- return false;
+ std::string_view ip;
+ uint16_t port;
+ if (dsn_unlikely(!extract_host_port(ip_port, ip, port))) {
+ return {};
}
- std::string ip = ip_port.substr(0, pos);
- std::string port = ip_port.substr(pos + 1);
- // check port
- unsigned int port_num;
- if (!internal::buf2unsigned(port, port_num) || port_num > UINT16_MAX) {
- return false;
+
+ // Use std::string(ip) to add a null terminator.
+ return from_ip_port(std::string(ip), port);
+}
+
+rpc_address rpc_address::from_ip_port(std::string_view ip, uint16_t port)
+{
+ // Check is IPv4 integer.
+ uint32_t ip_num;
+ int ret = ::inet_pton(AF_INET, ip.data(), &ip_num);
+ switch (ret) {
+ case 1:
+ // ::inet_pton() returns 1 on success (network address was
successfully converted)
+ return {ntohl(ip_num), port};
+ case -1:
+ LOG_ERROR(
+ "{}: fail to convert '{}:{}' to rpc_address",
utils::safe_strerror(errno), ip, port);
+ break;
+ default:
+ LOG_ERROR("'{}' does not contain a character string representing a
valid network address",
+ ip);
+ break;
}
- // check localhost & IP
- uint32_t ip_addr;
- if (ip == "localhost" || inet_pton(AF_INET, ip.c_str(), &ip_addr)) {
- assign_ipv4(ip.c_str(), (uint16_t)port_num);
- return true;
+ return {};
+}
+
+rpc_address rpc_address::from_host_port(std::string_view host_port)
+{
+ std::string_view host;
+ uint16_t port;
+ if (dsn_unlikely(!extract_host_port(host_port, host, port))) {
+ return {};
}
- return false;
+
+ // Use std::string(host) to add a null terminator.
+ return from_host_port(std::string(host), port);
+}
+
+rpc_address rpc_address::from_host_port(std::string_view hostname, uint16_t
port)
+{
+ uint32_t ip = 0;
+ if (!ipv4_from_host(hostname, &ip)) {
+ return {};
+ }
+
+ rpc_address addr;
+ addr._addr.v4.type = HOST_TYPE_IPV4;
+ addr._addr.v4.ip = ip;
+ addr._addr.v4.port = port;
+ return addr;
}
const char *rpc_address::to_string() const
{
char *p = bf.next();
- auto sz = bf.get_chunk_size();
- struct in_addr net_addr;
- int ip_len;
-
switch (_addr.v4.type) {
- case HOST_TYPE_IPV4:
+ case HOST_TYPE_IPV4: {
+ const auto sz = bf.get_chunk_size();
+ struct in_addr net_addr;
net_addr.s_addr = htonl(ip());
- inet_ntop(AF_INET, &net_addr, p, sz);
- ip_len = strlen(p);
+ ::inet_ntop(AF_INET, &net_addr, p, sz);
Review Comment:
```suggestion
p = bf.next();
::inet_ntop(AF_INET, &net_addr, p, sz);
```
##########
src/runtime/rpc/rpc_address.cpp:
##########
@@ -110,50 +151,50 @@ bool rpc_address::is_docker_netcard(const char
*netcard_interface, uint32_t ip_n
/*static*/
uint32_t rpc_address::ipv4_from_network_interface(const char
*network_interface)
{
- uint32_t ret = 0;
-
+ // Get the network interface list.
struct ifaddrs *ifa = nullptr;
- if (getifaddrs(&ifa) == 0) {
- struct ifaddrs *i = ifa;
- while (i != nullptr) {
- if (i->ifa_name != nullptr && i->ifa_addr != nullptr &&
- i->ifa_addr->sa_family == AF_INET) {
- uint32_t ip_val = ((struct sockaddr_in
*)i->ifa_addr)->sin_addr.s_addr;
- if (utils::equals(i->ifa_name, network_interface) ||
- (network_interface[0] == '\0' &&
!is_docker_netcard(i->ifa_name, ip_val) &&
- is_site_local_address(ip_val))) {
- ret = (uint32_t)ntohl(ip_val);
- break;
- }
- LOG_DEBUG("skip interface({}), address({})",
- i->ifa_name,
- rpc_address(ip_val, 0).ipv4_str());
- }
- i = i->ifa_next;
- }
+ if (::getifaddrs(&ifa) != 0) {
+ LOG_ERROR("{}: fail to getifaddrs", utils::safe_strerror(errno));
+ return 0;
+ }
- if (i == nullptr) {
- LOG_ERROR("get local ip from network interfaces failed,
network_interface = {}",
- network_interface);
- } else {
- LOG_INFO("get ip address from network interface({}), addr({}),
input interface({})",
- i->ifa_name,
- rpc_address(ret, 0).ipv4_str(),
- network_interface);
+ uint32_t ret = 0;
+ struct ifaddrs *i = ifa;
+ for (; i != nullptr; i = i->ifa_next) {
+ // Skip non-IPv4 network interface.
+ if (i->ifa_name == nullptr || i->ifa_addr == nullptr ||
i->ifa_addr->sa_family != AF_INET) {
+ continue;
}
- if (ifa != nullptr) {
- // remember to free it
- freeifaddrs(ifa);
+ uint32_t ip_val = ((struct sockaddr_in *)i->ifa_addr)->sin_addr.s_addr;
+ // Get the specified network interface, or the first internal network
interface if not
+ // specified.
+ if (utils::equals(i->ifa_name, network_interface) ||
+ (network_interface[0] == '\0' && !is_docker_netcard(i->ifa_name,
ip_val) &&
Review Comment:
```suggestion
(utils::is_empty(network_interface) &&
!is_docker_netcard(i->ifa_name, ip_val) &&
```
--
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]