You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@kvrocks.apache.org by "git-hulk (via GitHub)" <gi...@apache.org> on 2023/04/18 02:04:56 UTC

[GitHub] [incubator-kvrocks] git-hulk commented on a diff in pull request #1392: feat: implement local IP address retrieval function

git-hulk commented on code in PR #1392:
URL: https://github.com/apache/incubator-kvrocks/pull/1392#discussion_r1169414962


##########
src/cluster/cluster.cc:
##########
@@ -157,7 +159,34 @@ Status Cluster::SetSlot(int slot, const std::string &node_id, int64_t new_versio
 
   return Status::OK();
 }
+// Get all local IP addresses.
+std::vector<std::string> getLocalIPAddresses() {
+  std::vector<std::string> ipAddresses;
+
+  struct ifaddrs *ifAddrStruct = NULL;
+  struct ifaddrs *ifa = NULL;
+  void *tmpAddrPtr = NULL;
+
+  getifaddrs(&ifAddrStruct);
 
+  for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
+    if (!ifa->ifa_addr) {
+      continue;
+    }
+    if (ifa->ifa_addr->sa_family == AF_INET) {
+      // check it is IP4 and not a loopback address
+      tmpAddrPtr = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
+      char addressBuffer[INET_ADDRSTRLEN];
+      inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
+      std::string ipAddress(addressBuffer);
+      ipAddresses.push_back(move(ipAddress));

Review Comment:
   It looks unnecessary to get IP addresses, we can do it once if finds the listening address was on 0.0.0.0 when starting.



-- 
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: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org