diff options
| author | K. Y. Srinivasan <kys@microsoft.com> | 2012-09-05 16:50:15 -0400 |
|---|---|---|
| committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2012-09-10 19:42:33 -0400 |
| commit | 16e87100e62330cb1e58ee772cacb7d4e6d5b61b (patch) | |
| tree | fcf248e2d171816927d286b7f0541f78b1ccecdf /tools | |
| parent | 4a3b97e5799571397ade963c760b7406d8e9a8a6 (diff) | |
Tools: hv: Implement the KVP verb - KVP_OP_GET_IP_INFO
Now implement the KVP verb - KVP_OP_GET_IP_INFO. This operation retrieves IP
information for the specified interface.
Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Reviewed-by: Haiyang Zhang <haiyangz@microsoft.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/hv/hv_kvp_daemon.c | 93 |
1 files changed, 90 insertions, 3 deletions
diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c index 849c43817a2d..c8e101363735 100644 --- a/tools/hv/hv_kvp_daemon.c +++ b/tools/hv/hv_kvp_daemon.c | |||
| @@ -603,6 +603,69 @@ static char *kvp_if_name_to_mac(char *if_name) | |||
| 603 | } | 603 | } |
| 604 | 604 | ||
| 605 | 605 | ||
| 606 | /* | ||
| 607 | * Retrieve the interface name given tha MAC address. | ||
| 608 | */ | ||
| 609 | |||
| 610 | static char *kvp_mac_to_if_name(char *mac) | ||
| 611 | { | ||
| 612 | DIR *dir; | ||
| 613 | struct dirent *entry; | ||
| 614 | FILE *file; | ||
| 615 | char *p, *q, *x; | ||
| 616 | char *if_name = NULL; | ||
| 617 | char buf[256]; | ||
| 618 | char *kvp_net_dir = "/sys/class/net/"; | ||
| 619 | char dev_id[256]; | ||
| 620 | int i; | ||
| 621 | |||
| 622 | dir = opendir(kvp_net_dir); | ||
| 623 | if (dir == NULL) | ||
| 624 | return NULL; | ||
| 625 | |||
| 626 | snprintf(dev_id, sizeof(dev_id), kvp_net_dir); | ||
| 627 | q = dev_id + strlen(kvp_net_dir); | ||
| 628 | |||
| 629 | while ((entry = readdir(dir)) != NULL) { | ||
| 630 | /* | ||
| 631 | * Set the state for the next pass. | ||
| 632 | */ | ||
| 633 | *q = '\0'; | ||
| 634 | |||
| 635 | strcat(dev_id, entry->d_name); | ||
| 636 | strcat(dev_id, "/address"); | ||
| 637 | |||
| 638 | file = fopen(dev_id, "r"); | ||
| 639 | if (file == NULL) | ||
| 640 | continue; | ||
| 641 | |||
| 642 | p = fgets(buf, sizeof(buf), file); | ||
| 643 | if (p) { | ||
| 644 | x = strchr(p, '\n'); | ||
| 645 | if (x) | ||
| 646 | *x = '\0'; | ||
| 647 | |||
| 648 | for (i = 0; i < strlen(p); i++) | ||
| 649 | p[i] = toupper(p[i]); | ||
| 650 | |||
| 651 | if (!strcmp(p, mac)) { | ||
| 652 | /* | ||
| 653 | * Found the MAC match; return the interface | ||
| 654 | * name. The caller will free the memory. | ||
| 655 | */ | ||
| 656 | if_name = strdup(entry->d_name); | ||
| 657 | fclose(file); | ||
| 658 | break; | ||
| 659 | } | ||
| 660 | } | ||
| 661 | fclose(file); | ||
| 662 | } | ||
| 663 | |||
| 664 | closedir(dir); | ||
| 665 | return if_name; | ||
| 666 | } | ||
| 667 | |||
| 668 | |||
| 606 | static void kvp_process_ipconfig_file(char *cmd, | 669 | static void kvp_process_ipconfig_file(char *cmd, |
| 607 | char *config_buf, int len, | 670 | char *config_buf, int len, |
| 608 | int element_size, int offset) | 671 | int element_size, int offset) |
| @@ -749,10 +812,10 @@ static int kvp_process_ip_address(void *addrp, | |||
| 749 | } | 812 | } |
| 750 | 813 | ||
| 751 | if ((length - *offset) < addr_length + 1) | 814 | if ((length - *offset) < addr_length + 1) |
| 752 | return 1; | 815 | return HV_E_FAIL; |
| 753 | if (str == NULL) { | 816 | if (str == NULL) { |
| 754 | strcpy(buffer, "inet_ntop failed\n"); | 817 | strcpy(buffer, "inet_ntop failed\n"); |
| 755 | return 1; | 818 | return HV_E_FAIL; |
| 756 | } | 819 | } |
| 757 | if (*offset == 0) | 820 | if (*offset == 0) |
| 758 | strcpy(buffer, tmp); | 821 | strcpy(buffer, tmp); |
| @@ -796,7 +859,7 @@ kvp_get_ip_info(int family, char *if_name, int op, | |||
| 796 | 859 | ||
| 797 | if (getifaddrs(&ifap)) { | 860 | if (getifaddrs(&ifap)) { |
| 798 | strcpy(buffer, "getifaddrs failed\n"); | 861 | strcpy(buffer, "getifaddrs failed\n"); |
| 799 | return 1; | 862 | return HV_E_FAIL; |
| 800 | } | 863 | } |
| 801 | 864 | ||
| 802 | curp = ifap; | 865 | curp = ifap; |
| @@ -1386,6 +1449,30 @@ int main(void) | |||
| 1386 | } | 1449 | } |
| 1387 | 1450 | ||
| 1388 | switch (op) { | 1451 | switch (op) { |
| 1452 | case KVP_OP_GET_IP_INFO: | ||
| 1453 | kvp_ip_val = &hv_msg->body.kvp_ip_val; | ||
| 1454 | if_name = | ||
| 1455 | kvp_mac_to_if_name((char *)kvp_ip_val->adapter_id); | ||
| 1456 | |||
| 1457 | if (if_name == NULL) { | ||
| 1458 | /* | ||
| 1459 | * We could not map the mac address to an | ||
| 1460 | * interface name; return error. | ||
| 1461 | */ | ||
| 1462 | hv_msg->error = HV_E_FAIL; | ||
| 1463 | break; | ||
| 1464 | } | ||
| 1465 | error = kvp_get_ip_info( | ||
| 1466 | 0, if_name, KVP_OP_GET_IP_INFO, | ||
| 1467 | kvp_ip_val, | ||
| 1468 | (MAX_IP_ADDR_SIZE * 2)); | ||
| 1469 | |||
| 1470 | if (error) | ||
| 1471 | hv_msg->error = error; | ||
| 1472 | |||
| 1473 | free(if_name); | ||
| 1474 | break; | ||
| 1475 | |||
| 1389 | case KVP_OP_SET_IP_INFO: | 1476 | case KVP_OP_SET_IP_INFO: |
| 1390 | kvp_ip_val = &hv_msg->body.kvp_ip_val; | 1477 | kvp_ip_val = &hv_msg->body.kvp_ip_val; |
| 1391 | if_name = kvp_get_if_name( | 1478 | if_name = kvp_get_if_name( |
