diff options
author | K. Y. Srinivasan <kys@microsoft.com> | 2012-08-16 21:32:14 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2012-08-17 11:23:57 -0400 |
commit | af733015c9f59a0993cb9a64e6904aed8612e8b3 (patch) | |
tree | 022a9e56533c51718ca5d1a4788ef4960637c3c8 /tools/hv | |
parent | 0ecaa1985e39344ab308190063f8a60cb2a96155 (diff) |
Tools: hv: Further refactor kvp_get_ip_address()
In preparation for making kvp_get_ip_address() more generic, factor out
the code for handling IP addresses.
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/hv')
-rw-r--r-- | tools/hv/hv_kvp_daemon.c | 94 |
1 files changed, 42 insertions, 52 deletions
diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c index 3af37f0060f4..3dc989f57dc8 100644 --- a/tools/hv/hv_kvp_daemon.c +++ b/tools/hv/hv_kvp_daemon.c | |||
@@ -491,17 +491,50 @@ done: | |||
491 | return; | 491 | return; |
492 | } | 492 | } |
493 | 493 | ||
494 | static int kvp_process_ip_address(void *addrp, | ||
495 | int family, char *buffer, | ||
496 | int length, int *offset) | ||
497 | { | ||
498 | struct sockaddr_in *addr; | ||
499 | struct sockaddr_in6 *addr6; | ||
500 | int addr_length; | ||
501 | char tmp[50]; | ||
502 | const char *str; | ||
503 | |||
504 | if (family == AF_INET) { | ||
505 | addr = (struct sockaddr_in *)addrp; | ||
506 | str = inet_ntop(family, &addr->sin_addr, tmp, 50); | ||
507 | addr_length = INET_ADDRSTRLEN; | ||
508 | } else { | ||
509 | addr6 = (struct sockaddr_in6 *)addrp; | ||
510 | str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50); | ||
511 | addr_length = INET6_ADDRSTRLEN; | ||
512 | } | ||
513 | |||
514 | if ((length - *offset) < addr_length + 1) | ||
515 | return 1; | ||
516 | if (str == NULL) { | ||
517 | strcpy(buffer, "inet_ntop failed\n"); | ||
518 | return 1; | ||
519 | } | ||
520 | if (*offset == 0) | ||
521 | strcpy(buffer, tmp); | ||
522 | else | ||
523 | strcat(buffer, tmp); | ||
524 | strcat(buffer, ";"); | ||
525 | |||
526 | *offset += strlen(str) + 1; | ||
527 | return 0; | ||
528 | } | ||
529 | |||
494 | static int | 530 | static int |
495 | kvp_get_ip_address(int family, char *if_name, int op, | 531 | kvp_get_ip_address(int family, char *if_name, int op, |
496 | void *out_buffer, int length) | 532 | void *out_buffer, int length) |
497 | { | 533 | { |
498 | struct ifaddrs *ifap; | 534 | struct ifaddrs *ifap; |
499 | struct ifaddrs *curp; | 535 | struct ifaddrs *curp; |
500 | int ipv4_len = strlen("255.255.255.255") + 1; | ||
501 | int ipv6_len = strlen("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")+1; | ||
502 | int offset = 0; | 536 | int offset = 0; |
503 | const char *str; | 537 | const char *str; |
504 | char tmp[50]; | ||
505 | int error = 0; | 538 | int error = 0; |
506 | char *buffer; | 539 | char *buffer; |
507 | struct hv_kvp_ipaddr_value *ip_buffer; | 540 | struct hv_kvp_ipaddr_value *ip_buffer; |
@@ -556,55 +589,12 @@ kvp_get_ip_address(int family, char *if_name, int op, | |||
556 | continue; | 589 | continue; |
557 | } | 590 | } |
558 | 591 | ||
559 | if ((curp->ifa_addr->sa_family == AF_INET) && | 592 | error = kvp_process_ip_address(curp->ifa_addr, |
560 | ((family == AF_INET) || (family == 0))) { | 593 | curp->ifa_addr->sa_family, |
561 | struct sockaddr_in *addr = | 594 | buffer, |
562 | (struct sockaddr_in *) curp->ifa_addr; | 595 | length, &offset); |
563 | 596 | if (error) | |
564 | str = inet_ntop(AF_INET, &addr->sin_addr, tmp, 50); | 597 | goto getaddr_done; |
565 | if (str == NULL) { | ||
566 | strcpy(buffer, "inet_ntop failed\n"); | ||
567 | error = 1; | ||
568 | goto getaddr_done; | ||
569 | } | ||
570 | if (offset == 0) | ||
571 | strcpy(buffer, tmp); | ||
572 | else | ||
573 | strcat(buffer, tmp); | ||
574 | strcat(buffer, ";"); | ||
575 | |||
576 | offset += strlen(str) + 1; | ||
577 | if ((length - offset) < (ipv4_len + 1)) | ||
578 | goto getaddr_done; | ||
579 | |||
580 | } else if ((family == AF_INET6) || (family == 0)) { | ||
581 | |||
582 | /* | ||
583 | * We only support AF_INET and AF_INET6 | ||
584 | * and the list of addresses is separated by a ";". | ||
585 | */ | ||
586 | struct sockaddr_in6 *addr = | ||
587 | (struct sockaddr_in6 *) curp->ifa_addr; | ||
588 | |||
589 | str = inet_ntop(AF_INET6, | ||
590 | &addr->sin6_addr.s6_addr, | ||
591 | tmp, 50); | ||
592 | if (str == NULL) { | ||
593 | strcpy(buffer, "inet_ntop failed\n"); | ||
594 | error = 1; | ||
595 | goto getaddr_done; | ||
596 | } | ||
597 | if (offset == 0) | ||
598 | strcpy(buffer, tmp); | ||
599 | else | ||
600 | strcat(buffer, tmp); | ||
601 | strcat(buffer, ";"); | ||
602 | offset += strlen(str) + 1; | ||
603 | if ((length - offset) < (ipv6_len + 1)) | ||
604 | goto getaddr_done; | ||
605 | |||
606 | } | ||
607 | |||
608 | 598 | ||
609 | curp = curp->ifa_next; | 599 | curp = curp->ifa_next; |
610 | } | 600 | } |