]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
AsyncSocket: Update AsyncSocket_GetRemoteIPAddress for IPv6 support.
authorVMware, Inc <>
Wed, 18 Sep 2013 03:43:28 +0000 (20:43 -0700)
committerDmitry Torokhov <dmitry.torokhov@gmail.com>
Mon, 23 Sep 2013 05:30:01 +0000 (22:30 -0700)
AsyncSocket_GetRemoteIPAddress offered the return of the IP in integer and
string formats, none of the callers cared for the integer return so remove it.
Update name appropriately
AsyncSocket_GetRemoteIPAddress->AsyncSocket_GetRemoteIPStr.
AsyncSocket_GetRemoteIPAddress only supported the returning of a IPv4 string,
utilize Posix_GetNameInfo that uses getnameinfo which is IPv6 compliant.

Signed-off-by: Dmitry Torokhov <dtor@vmware.com>
open-vm-tools/lib/asyncsocket/asyncsocket.c
open-vm-tools/lib/include/asyncsocket.h

index c97b6d74314f4762bcfa01f66c029410b3fc1f42..7bdb11966cffee658d61e88a7db4f17f4f4989e1 100644 (file)
@@ -376,7 +376,7 @@ AsyncSocket_GetFd(AsyncSocket *s)
 /*
  *----------------------------------------------------------------------------
  *
- * AsyncSocket_GetRemoteIPAddress --
+ * AsyncSocket_GetRemoteIPStr --
  *
  *      Given an AsyncSocket object, returns the remote IP address associated
  *      with it, or an error if the request is meaningless for the underlying
@@ -392,35 +392,32 @@ AsyncSocket_GetFd(AsyncSocket *s)
  */
 
 int
-AsyncSocket_GetRemoteIPAddress(AsyncSocket *asock,      // IN
-                               uint32 *ipRet,           // OUT
-                               const char **ipRetStr)   // OUT
+AsyncSocket_GetRemoteIPStr(AsyncSocket *asock,      // IN
+                           const char **ipRetStr)   // OUT
 {
-   uint32 ip;
-   struct in_addr ipAddr;
+   int ret = ASOCKERR_SUCCESS;
 
    ASSERT(asock);
    ASSERT(asock->asockType != ASYNCSOCKET_TYPE_NAMEDPIPE);
-   ASSERT(ipRet != NULL || ipRetStr != NULL);
+   ASSERT(ipRetStr != NULL);
 
-   if ((ipRet == NULL && ipRetStr == NULL) || asock == NULL ||
+   if (ipRetStr == NULL || asock == NULL ||
        asock->state != AsyncSocketConnected ||
-       asock->remoteAddrLen != sizeof (struct sockaddr_in)) {
-      return ASOCKERR_GENERIC;
-   }
-
-   ip = ntohl(((struct sockaddr_in *) &asock->remoteAddr)->sin_addr.s_addr);
-
-   if (ipRet != NULL) {
-      *ipRet = ip;
-   }
+       (asock->remoteAddrLen != sizeof (struct sockaddr_in) &&
+        asock->remoteAddrLen != sizeof (struct sockaddr_in6))) {
+      ret = ASOCKERR_GENERIC;
+   } else {
+      char addrBuf[NI_MAXHOST];
 
-   if (ipRetStr != NULL) {
-      ipAddr.s_addr = htonl(ip);
-      *ipRetStr = inet_ntoa(ipAddr);
+      if (Posix_GetNameInfo(&asock->remoteAddr, asock->remoteAddrLen, addrBuf,
+                            sizeof addrBuf, NULL, 0, NI_NUMERICHOST) != 0) {
+         ret = ASOCKERR_GENERIC;
+      } else {
+         *ipRetStr = Util_SafeStrdup(addrBuf);
+      }
    }
 
-   return ASOCKERR_SUCCESS;
+   return ret;
 }
 
 
index 5abcc83052ef79d75027d03a025f3974dcffdb8a..1bf7a40da35b7b463c684d4537c2e1a9b61accc7 100644 (file)
@@ -155,9 +155,8 @@ int AsyncSocket_GetFd(AsyncSocket *asock);
 /*
  * Return the remote IP address associated with this socket if applicable
  */
-int AsyncSocket_GetRemoteIPAddress(AsyncSocket *asock,
-                                   unsigned int *ip,
-                                   const char **ipStr);
+int AsyncSocket_GetRemoteIPStr(AsyncSocket *asock,
+                               const char **ipStr);
 
 int AsyncSocket_GetLocalVMCIAddress(AsyncSocket *asock,
                                     uint32 *cid, uint32 *port);