Tools: hv: Further refactor kvp_get_ip_address()
[linux-3.10.git] / tools / hv / hv_kvp_daemon.c
index 2fb9c3d..3dc989f 100644 (file)
@@ -69,15 +69,16 @@ enum key_index {
 };
 
 static char kvp_send_buffer[4096];
-static char kvp_recv_buffer[4096];
+static char kvp_recv_buffer[4096 * 2];
 static struct sockaddr_nl addr;
+static int in_hand_shake = 1;
 
 static char *os_name = "";
 static char *os_major = "";
 static char *os_minor = "";
 static char *processor_arch;
 static char *os_build;
-static char *lic_version;
+static char *lic_version = "Unknown version";
 static struct utsname uts_buf;
 
 
@@ -148,6 +149,51 @@ static void kvp_update_file(int pool)
        kvp_release_lock(pool);
 }
 
+static void kvp_update_mem_state(int pool)
+{
+       FILE *filep;
+       size_t records_read = 0;
+       struct kvp_record *record = kvp_file_info[pool].records;
+       struct kvp_record *readp;
+       int num_blocks = kvp_file_info[pool].num_blocks;
+       int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
+
+       kvp_acquire_lock(pool);
+
+       filep = fopen(kvp_file_info[pool].fname, "r");
+       if (!filep) {
+               kvp_release_lock(pool);
+               syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
+               exit(-1);
+       }
+       while (!feof(filep)) {
+               readp = &record[records_read];
+               records_read += fread(readp, sizeof(struct kvp_record),
+                                       ENTRIES_PER_BLOCK * num_blocks,
+                                       filep);
+
+               if (!feof(filep)) {
+                       /*
+                        * We have more data to read.
+                        */
+                       num_blocks++;
+                       record = realloc(record, alloc_unit * num_blocks);
+
+                       if (record == NULL) {
+                               syslog(LOG_ERR, "malloc failed");
+                               exit(-1);
+                       }
+                       continue;
+               }
+               break;
+       }
+
+       kvp_file_info[pool].num_blocks = num_blocks;
+       kvp_file_info[pool].records = record;
+       kvp_file_info[pool].num_records = records_read;
+
+       kvp_release_lock(pool);
+}
 static int kvp_file_init(void)
 {
        int ret, fd;
@@ -223,8 +269,16 @@ static int kvp_key_delete(int pool, __u8 *key, int key_size)
 {
        int i;
        int j, k;
-       int num_records = kvp_file_info[pool].num_records;
-       struct kvp_record *record = kvp_file_info[pool].records;
+       int num_records;
+       struct kvp_record *record;
+
+       /*
+        * First update the in-memory state.
+        */
+       kvp_update_mem_state(pool);
+
+       num_records = kvp_file_info[pool].num_records;
+       record = kvp_file_info[pool].records;
 
        for (i = 0; i < num_records; i++) {
                if (memcmp(key, record[i].key, key_size))
@@ -259,14 +313,23 @@ static int kvp_key_add_or_modify(int pool, __u8 *key, int key_size, __u8 *value,
 {
        int i;
        int j, k;
-       int num_records = kvp_file_info[pool].num_records;
-       struct kvp_record *record = kvp_file_info[pool].records;
-       int num_blocks = kvp_file_info[pool].num_blocks;
+       int num_records;
+       struct kvp_record *record;
+       int num_blocks;
 
        if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
                (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
                return 1;
 
+       /*
+        * First update the in-memory state.
+        */
+       kvp_update_mem_state(pool);
+
+       num_records = kvp_file_info[pool].num_records;
+       record = kvp_file_info[pool].records;
+       num_blocks = kvp_file_info[pool].num_blocks;
+
        for (i = 0; i < num_records; i++) {
                if (memcmp(key, record[i].key, key_size))
                        continue;
@@ -304,13 +367,21 @@ static int kvp_get_value(int pool, __u8 *key, int key_size, __u8 *value,
                        int value_size)
 {
        int i;
-       int num_records = kvp_file_info[pool].num_records;
-       struct kvp_record *record = kvp_file_info[pool].records;
+       int num_records;
+       struct kvp_record *record;
 
        if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
                (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
                return 1;
 
+       /*
+        * First update the in-memory state.
+        */
+       kvp_update_mem_state(pool);
+
+       num_records = kvp_file_info[pool].num_records;
+       record = kvp_file_info[pool].records;
+
        for (i = 0; i < num_records; i++) {
                if (memcmp(key, record[i].key, key_size))
                        continue;
@@ -324,6 +395,27 @@ static int kvp_get_value(int pool, __u8 *key, int key_size, __u8 *value,
        return 1;
 }
 
+static int kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size,
+                               __u8 *value, int value_size)
+{
+       struct kvp_record *record;
+
+       /*
+        * First update our in-memory database.
+        */
+       kvp_update_mem_state(pool);
+       record = kvp_file_info[pool].records;
+
+       if (index >= kvp_file_info[pool].num_records) {
+               return 1;
+       }
+
+       memcpy(key, record[index].key, key_size);
+       memcpy(value, record[index].value, value_size);
+       return 0;
+}
+
+
 void kvp_get_os_info(void)
 {
        FILE    *file;
@@ -399,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)) {
@@ -423,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;
        }
 
@@ -551,6 +672,8 @@ int main(void)
        char    *p;
        char    *key_value;
        char    *key_name;
+       int     op;
+       int     pool;
 
        daemon(1, 0);
        openlog("KVP", 0, LOG_USER);
@@ -592,7 +715,7 @@ int main(void)
        message->id.val = CN_KVP_VAL;
 
        hv_msg = (struct hv_kvp_msg *)message->data;
-       hv_msg->kvp_hdr.operation = KVP_OP_REGISTER;
+       hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
        message->ack = 0;
        message->len = sizeof(struct hv_kvp_msg);
 
@@ -606,14 +729,18 @@ int main(void)
        pfd.fd = fd;
 
        while (1) {
+               struct sockaddr *addr_p = (struct sockaddr *) &addr;
+               socklen_t addr_l = sizeof(addr);
                pfd.events = POLLIN;
                pfd.revents = 0;
                poll(&pfd, 1, -1);
 
-               len = recv(fd, kvp_recv_buffer, sizeof(kvp_recv_buffer), 0);
+               len = recvfrom(fd, kvp_recv_buffer, sizeof(kvp_recv_buffer), 0,
+                               addr_p, &addr_l);
 
-               if (len < 0) {
-                       syslog(LOG_ERR, "recv failed; error:%d", len);
+               if (len < 0 || addr.nl_pid) {
+                       syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
+                                       addr.nl_pid, errno, strerror(errno));
                        close(fd);
                        return -1;
                }
@@ -622,12 +749,21 @@ int main(void)
                incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
                hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
 
-               switch (hv_msg->kvp_hdr.operation) {
-               case KVP_OP_REGISTER:
+               /*
+                * We will use the KVP header information to pass back
+                * the error from this daemon. So, first copy the state
+                * and set the error code to success.
+                */
+               op = hv_msg->kvp_hdr.operation;
+               pool = hv_msg->kvp_hdr.pool;
+               hv_msg->error = HV_S_OK;
+
+               if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
                        /*
                         * Driver is registering with us; stash away the version
                         * information.
                         */
+                       in_hand_shake = 0;
                        p = (char *)hv_msg->body.kvp_register.version;
                        lic_version = malloc(strlen(p) + 1);
                        if (lic_version) {
@@ -638,45 +774,56 @@ int main(void)
                                syslog(LOG_ERR, "malloc failed");
                        }
                        continue;
+               }
 
-               /*
-                * The current protocol with the kernel component uses a
-                * NULL key name to pass an error condition.
-                * For the SET, GET and DELETE operations,
-                * use the existing protocol to pass back error.
-                */
-
+               switch (op) {
                case KVP_OP_SET:
-                       if (kvp_key_add_or_modify(hv_msg->kvp_hdr.pool,
+                       if (kvp_key_add_or_modify(pool,
                                        hv_msg->body.kvp_set.data.key,
                                        hv_msg->body.kvp_set.data.key_size,
                                        hv_msg->body.kvp_set.data.value,
                                        hv_msg->body.kvp_set.data.value_size))
-                               strcpy(hv_msg->body.kvp_set.data.key, "");
+                                       hv_msg->error = HV_S_CONT;
                        break;
 
                case KVP_OP_GET:
-                       if (kvp_get_value(hv_msg->kvp_hdr.pool,
+                       if (kvp_get_value(pool,
                                        hv_msg->body.kvp_set.data.key,
                                        hv_msg->body.kvp_set.data.key_size,
                                        hv_msg->body.kvp_set.data.value,
                                        hv_msg->body.kvp_set.data.value_size))
-                               strcpy(hv_msg->body.kvp_set.data.key, "");
+                                       hv_msg->error = HV_S_CONT;
                        break;
 
                case KVP_OP_DELETE:
-                       if (kvp_key_delete(hv_msg->kvp_hdr.pool,
+                       if (kvp_key_delete(pool,
                                        hv_msg->body.kvp_delete.key,
                                        hv_msg->body.kvp_delete.key_size))
-                               strcpy(hv_msg->body.kvp_delete.key, "");
+                                       hv_msg->error = HV_S_CONT;
                        break;
 
                default:
                        break;
                }
 
-               if (hv_msg->kvp_hdr.operation != KVP_OP_ENUMERATE)
+               if (op != KVP_OP_ENUMERATE)
+                       goto kvp_done;
+
+               /*
+                * If the pool is KVP_POOL_AUTO, dynamically generate
+                * both the key and the value; if not read from the
+                * appropriate pool.
+                */
+               if (pool != KVP_POOL_AUTO) {
+                       if (kvp_pool_enumerate(pool,
+                                       hv_msg->body.kvp_enum_data.index,
+                                       hv_msg->body.kvp_enum_data.data.key,
+                                       HV_KVP_EXCHANGE_MAX_KEY_SIZE,
+                                       hv_msg->body.kvp_enum_data.data.value,
+                                       HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
+                                       hv_msg->error = HV_S_CONT;
                        goto kvp_done;
+               }
 
                hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
                key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
@@ -693,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:
@@ -727,11 +874,7 @@ int main(void)
                        strcpy(key_name, "ProcessorArchitecture");
                        break;
                default:
-                       strcpy(key_value, "Unknown Key");
-                       /*
-                        * We use a null key name to terminate enumeration.
-                        */
-                       strcpy(key_name, "");
+                       hv_msg->error = HV_S_CONT;
                        break;
                }
                /*