diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-03-11 22:45:56 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-03-11 22:45:56 +0000 |
commit | 0746baa7c3e4924ac6a687d0d1fff3b33138eca0 (patch) | |
tree | 22fd4320a4eb7323e817b6e4ac878dec168d65d1 /src/transport/plugin_transport_tcp.c | |
parent | 494f33365cb6b1c8d290fb6ad06aa6be45b880a7 (diff) | |
download | gnunet-0746baa7c3e4924ac6a687d0d1fff3b33138eca0.tar.gz gnunet-0746baa7c3e4924ac6a687d0d1fff3b33138eca0.zip |
-LRN: Generalize ip-address str->addr conversion
Diffstat (limited to 'src/transport/plugin_transport_tcp.c')
-rw-r--r-- | src/transport/plugin_transport_tcp.c | 108 |
1 files changed, 26 insertions, 82 deletions
diff --git a/src/transport/plugin_transport_tcp.c b/src/transport/plugin_transport_tcp.c index e10dd74c1..e5d3014bc 100644 --- a/src/transport/plugin_transport_tcp.c +++ b/src/transport/plugin_transport_tcp.c | |||
@@ -537,82 +537,6 @@ tcp_address_to_string (void *cls, const void *addr, size_t addrlen) | |||
537 | return rbuf; | 537 | return rbuf; |
538 | } | 538 | } |
539 | 539 | ||
540 | #define MAX_IPV6_ADDRLEN 47 | ||
541 | |||
542 | int | ||
543 | tcp_string_to_address_ipv6 (void *cls, const char *addr, uint16_t addrlen, | ||
544 | void **buf, size_t *added) | ||
545 | { | ||
546 | char zt_addr[MAX_IPV6_ADDRLEN + 1]; | ||
547 | int ret; | ||
548 | char *port_colon; | ||
549 | unsigned int port; | ||
550 | struct IPv6TcpAddress *ipv6addr; | ||
551 | |||
552 | if (addrlen < 6) | ||
553 | return GNUNET_SYSERR; | ||
554 | |||
555 | memset (zt_addr, 0, MAX_IPV6_ADDRLEN + 1); | ||
556 | strncpy (zt_addr, addr, addrlen <= MAX_IPV6_ADDRLEN ? addrlen : MAX_IPV6_ADDRLEN); | ||
557 | |||
558 | port_colon = strrchr (zt_addr, ':'); | ||
559 | if (port_colon == NULL) | ||
560 | return GNUNET_SYSERR; | ||
561 | ret = sscanf (port_colon, ":%u", &port); | ||
562 | if (ret != 1 || port > 65535) | ||
563 | return GNUNET_SYSERR; | ||
564 | port_colon[0] = '\0'; | ||
565 | |||
566 | ipv6addr = GNUNET_malloc (sizeof (struct IPv6TcpAddress)); | ||
567 | ret = inet_pton (AF_INET6, zt_addr, &ipv6addr->ipv6_addr); | ||
568 | if (ret <= 0) | ||
569 | { | ||
570 | GNUNET_free (ipv6addr); | ||
571 | return GNUNET_SYSERR; | ||
572 | } | ||
573 | ipv6addr->t6_port = port; | ||
574 | *buf = ipv6addr; | ||
575 | *added = sizeof (struct IPv6TcpAddress); | ||
576 | return GNUNET_OK; | ||
577 | } | ||
578 | |||
579 | #define MAX_IPV4_ADDRLEN 21 | ||
580 | |||
581 | int | ||
582 | tcp_string_to_address_ipv4 (void *cls, const char *addr, uint16_t addrlen, | ||
583 | void **buf, size_t *added) | ||
584 | { | ||
585 | unsigned int temps[5]; | ||
586 | unsigned int port; | ||
587 | int cnt; | ||
588 | char zt_addr[MAX_IPV4_ADDRLEN + 1]; | ||
589 | struct IPv4TcpAddress *ipv4addr; | ||
590 | |||
591 | if (addrlen < 9) | ||
592 | return GNUNET_SYSERR; | ||
593 | |||
594 | memset (zt_addr, 0, MAX_IPV4_ADDRLEN + 1); | ||
595 | strncpy (zt_addr, addr, addrlen <= MAX_IPV4_ADDRLEN ? addrlen : MAX_IPV4_ADDRLEN); | ||
596 | |||
597 | cnt = sscanf (zt_addr, "%u.%u.%u.%u:%u", &temps[0], &temps[1], &temps[2], &temps[3], &port); | ||
598 | if (cnt != 5) | ||
599 | return GNUNET_SYSERR; | ||
600 | |||
601 | for (cnt = 0; cnt < 4; cnt++) | ||
602 | if (temps[cnt] > 0xFF) | ||
603 | return GNUNET_SYSERR; | ||
604 | if (port > 65535) | ||
605 | return GNUNET_SYSERR; | ||
606 | |||
607 | ipv4addr = GNUNET_malloc (sizeof (struct IPv4TcpAddress)); | ||
608 | ipv4addr->ipv4_addr = | ||
609 | htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + | ||
610 | temps[3]); | ||
611 | ipv4addr->t4_port = htonl (port); | ||
612 | *buf = ipv4addr; | ||
613 | *added = sizeof (struct IPv4TcpAddress); | ||
614 | return GNUNET_OK; | ||
615 | } | ||
616 | 540 | ||
617 | /** | 541 | /** |
618 | * Function called to convert a string address to | 542 | * Function called to convert a string address to |
@@ -622,7 +546,6 @@ tcp_string_to_address_ipv4 (void *cls, const char *addr, uint16_t addrlen, | |||
622 | * @param addr string address | 546 | * @param addr string address |
623 | * @param addrlen length of the address | 547 | * @param addrlen length of the address |
624 | * @param buf location to store the buffer | 548 | * @param buf location to store the buffer |
625 | * @param max size of the buffer | ||
626 | * @param added location to store the number of bytes in the buffer. | 549 | * @param added location to store the number of bytes in the buffer. |
627 | * If the function returns GNUNET_SYSERR, its contents are undefined. | 550 | * If the function returns GNUNET_SYSERR, its contents are undefined. |
628 | * @return GNUNET_OK on success, GNUNET_SYSERR on failure | 551 | * @return GNUNET_OK on success, GNUNET_SYSERR on failure |
@@ -631,13 +554,34 @@ int | |||
631 | tcp_string_to_address (void *cls, const char *addr, uint16_t addrlen, | 554 | tcp_string_to_address (void *cls, const char *addr, uint16_t addrlen, |
632 | void **buf, size_t *added) | 555 | void **buf, size_t *added) |
633 | { | 556 | { |
634 | if (addrlen < 1) | 557 | struct sockaddr_storage socket_address; |
558 | int ret = GNUNET_STRINGS_to_address_ip (addr, addrlen, | ||
559 | &socket_address); | ||
560 | |||
561 | if (ret != GNUNET_OK) | ||
635 | return GNUNET_SYSERR; | 562 | return GNUNET_SYSERR; |
636 | 563 | ||
637 | if (addr[0] == '(') | 564 | if (socket_address.ss_family == AF_INET) |
638 | return tcp_string_to_address_ipv6 (cls, addr, addrlen, buf, added); | 565 | { |
639 | else | 566 | struct IPv4TcpAddress *t4; |
640 | return tcp_string_to_address_ipv4 (cls, addr, addrlen, buf, added); | 567 | struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address; |
568 | t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress)); | ||
569 | t4->ipv4_addr = in4->sin_addr.s_addr; | ||
570 | t4->t4_port = in4->sin_port; | ||
571 | *buf = t4; | ||
572 | *added = sizeof (struct IPv4TcpAddress); | ||
573 | } | ||
574 | else if (socket_address.ss_family == AF_INET6) | ||
575 | { | ||
576 | struct IPv6TcpAddress *t6; | ||
577 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address; | ||
578 | t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress)); | ||
579 | t6->ipv6_addr = in6->sin6_addr; | ||
580 | t6->t6_port = in6->sin6_port; | ||
581 | *buf = t6; | ||
582 | *added = sizeof (struct IPv6TcpAddress); | ||
583 | } | ||
584 | return GNUNET_SYSERR; | ||
641 | } | 585 | } |
642 | 586 | ||
643 | 587 | ||