Tools: hv: Further refactor kvp_get_ip_address()
[linux-3.10.git] / tools / hv / hv_kvp_daemon.c
index 069e2b3..3dc989f 100644 (file)
@@ -491,21 +491,64 @@ done:
        return;
 }
 
+static int kvp_process_ip_address(void *addrp,
+                               int family, char *buffer,
+                               int length,  int *offset)
+{
+       struct sockaddr_in *addr;
+       struct sockaddr_in6 *addr6;
+       int addr_length;
+       char tmp[50];
+       const char *str;
+
+       if (family == AF_INET) {
+               addr = (struct sockaddr_in *)addrp;
+               str = inet_ntop(family, &addr->sin_addr, tmp, 50);
+               addr_length = INET_ADDRSTRLEN;
+       } else {
+               addr6 = (struct sockaddr_in6 *)addrp;
+               str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
+               addr_length = INET6_ADDRSTRLEN;
+       }
+
+       if ((length - *offset) < addr_length + 1)
+               return 1;
+       if (str == NULL) {
+               strcpy(buffer, "inet_ntop failed\n");
+               return 1;
+       }
+       if (*offset == 0)
+               strcpy(buffer, tmp);
+       else
+               strcat(buffer, tmp);
+       strcat(buffer, ";");
+
+       *offset += strlen(str) + 1;
+       return 0;
+}
+
 static int
-kvp_get_ip_address(int family, char *buffer, int length)
+kvp_get_ip_address(int family, char *if_name, int op,
+                void  *out_buffer, int length)
 {
        struct ifaddrs *ifap;
        struct ifaddrs *curp;
-       int ipv4_len = strlen("255.255.255.255") + 1;
-       int ipv6_len = strlen("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")+1;
        int offset = 0;
        const char *str;
-       char tmp[50];
        int error = 0;
-
+       char *buffer;
+       struct hv_kvp_ipaddr_value *ip_buffer;
+
+       if (op == KVP_OP_ENUMERATE) {
+               buffer = out_buffer;
+       } else {
+               ip_buffer = out_buffer;
+               buffer = (char *)ip_buffer->ip_addr;
+               ip_buffer->addr_family = 0;
+       }
        /*
         * On entry into this function, the buffer is capable of holding the
-        * maximum key value (2048 bytes).
+        * maximum key value.
         */
 
        if (getifaddrs(&ifap)) {
@@ -515,58 +558,44 @@ kvp_get_ip_address(int family, char *buffer, int length)
 
        curp = ifap;
        while (curp != NULL) {
-               if ((curp->ifa_addr != NULL) &&
-                  (curp->ifa_addr->sa_family == family)) {
-                       if (family == AF_INET) {
-                               struct sockaddr_in *addr =
-                               (struct sockaddr_in *) curp->ifa_addr;
-
-                               str = inet_ntop(family, &addr->sin_addr,
-                                               tmp, 50);
-                               if (str == NULL) {
-                                       strcpy(buffer, "inet_ntop failed\n");
-                                       error = 1;
-                                       goto getaddr_done;
-                               }
-                               if (offset == 0)
-                                       strcpy(buffer, tmp);
-                               else
-                                       strcat(buffer, tmp);
-                               strcat(buffer, ";");
-
-                               offset += strlen(str) + 1;
-                               if ((length - offset) < (ipv4_len + 1))
-                                       goto getaddr_done;
-
-                       } else {
+               if (curp->ifa_addr == NULL) {
+                       curp = curp->ifa_next;
+                       continue;
+               }
 
+               if ((if_name != NULL) &&
+                       (strncmp(curp->ifa_name, if_name, strlen(if_name)))) {
                        /*
-                        * We only support AF_INET and AF_INET6
-                        * and the list of addresses is separated by a ";".
+                        * We want info about a specific interface;
+                        * just continue.
                         */
-                               struct sockaddr_in6 *addr =
-                               (struct sockaddr_in6 *) curp->ifa_addr;
-
-                               str = inet_ntop(family,
-                                       &addr->sin6_addr.s6_addr,
-                                       tmp, 50);
-                               if (str == NULL) {
-                                       strcpy(buffer, "inet_ntop failed\n");
-                                       error = 1;
-                                       goto getaddr_done;
-                               }
-                               if (offset == 0)
-                                       strcpy(buffer, tmp);
-                               else
-                                       strcat(buffer, tmp);
-                               strcat(buffer, ";");
-                               offset += strlen(str) + 1;
-                               if ((length - offset) < (ipv6_len + 1))
-                                       goto getaddr_done;
-
-                       }
+                       curp = curp->ifa_next;
+                       continue;
+               }
 
+               /*
+                * We only support two address families: AF_INET and AF_INET6.
+                * If a family value of 0 is specified, we collect both
+                * supported address families; if not we gather info on
+                * the specified address family.
+                */
+               if ((family != 0) && (curp->ifa_addr->sa_family != family)) {
+                       curp = curp->ifa_next;
+                       continue;
+               }
+               if ((curp->ifa_addr->sa_family != AF_INET) &&
+                       (curp->ifa_addr->sa_family != AF_INET6)) {
+                       curp = curp->ifa_next;
+                       continue;
                }
+
+               error = kvp_process_ip_address(curp->ifa_addr,
+                                               curp->ifa_addr->sa_family,
+                                               buffer,
+                                               length, &offset);
+               if (error)
+                       goto getaddr_done;
+
                curp = curp->ifa_next;
        }
 
@@ -811,13 +840,13 @@ int main(void)
                        strcpy(key_value, lic_version);
                        break;
                case NetworkAddressIPv4:
-                       kvp_get_ip_address(AF_INET, key_value,
-                                       HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
+                       kvp_get_ip_address(AF_INET, NULL, KVP_OP_ENUMERATE,
+                               key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
                        strcpy(key_name, "NetworkAddressIPv4");
                        break;
                case NetworkAddressIPv6:
-                       kvp_get_ip_address(AF_INET6, key_value,
-                                       HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
+                       kvp_get_ip_address(AF_INET6, NULL, KVP_OP_ENUMERATE,
+                               key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
                        strcpy(key_name, "NetworkAddressIPv6");
                        break;
                case OSBuildNumber: