diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-03-05 19:56:16 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-03-05 19:56:16 +0000 |
commit | b1780f05127f1c84c442f12fe84ae8e712032164 (patch) | |
tree | 58ecfc3fc8b92b3b5705b885260fcf456d6fe7d5 /src/transport/plugin_transport_tcp.c | |
parent | dffa6f386418b19956f23f13f3f2a32cd803bca4 (diff) | |
download | gnunet-b1780f05127f1c84c442f12fe84ae8e712032164.tar.gz gnunet-b1780f05127f1c84c442f12fe84ae8e712032164.zip |
-LRN: experimental HELLO URIs
Diffstat (limited to 'src/transport/plugin_transport_tcp.c')
-rw-r--r-- | src/transport/plugin_transport_tcp.c | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/src/transport/plugin_transport_tcp.c b/src/transport/plugin_transport_tcp.c index 52efd4d4e..628a0ff42 100644 --- a/src/transport/plugin_transport_tcp.c +++ b/src/transport/plugin_transport_tcp.c | |||
@@ -537,6 +537,109 @@ 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 | |||
617 | /** | ||
618 | * Function called to convert a string address to | ||
619 | * a binary address. | ||
620 | * | ||
621 | * @param cls closure ('struct Plugin*') | ||
622 | * @param addr string address | ||
623 | * @param addrlen length of the address | ||
624 | * @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. | ||
627 | * If the function returns GNUNET_SYSERR, its contents are undefined. | ||
628 | * @return GNUNET_OK on success, GNUNET_SYSERR on failure | ||
629 | */ | ||
630 | int | ||
631 | tcp_string_to_address (void *cls, const char *addr, uint16_t addrlen, | ||
632 | void **buf, size_t *added) | ||
633 | { | ||
634 | if (addrlen < 1) | ||
635 | return GNUNET_SYSERR; | ||
636 | |||
637 | if (addr[0] == '(') | ||
638 | return tcp_string_to_address_ipv6 (cls, addr, addrlen, buf, added); | ||
639 | else | ||
640 | return tcp_string_to_address_ipv4 (cls, addr, addrlen, buf, added); | ||
641 | } | ||
642 | |||
540 | 643 | ||
541 | struct SessionClientCtx | 644 | struct SessionClientCtx |
542 | { | 645 | { |
@@ -2084,6 +2187,7 @@ libgnunet_plugin_transport_tcp_init (void *cls) | |||
2084 | api->address_pretty_printer = &tcp_plugin_address_pretty_printer; | 2187 | api->address_pretty_printer = &tcp_plugin_address_pretty_printer; |
2085 | api->check_address = &tcp_plugin_check_address; | 2188 | api->check_address = &tcp_plugin_check_address; |
2086 | api->address_to_string = &tcp_address_to_string; | 2189 | api->address_to_string = &tcp_address_to_string; |
2190 | api->string_to_address = &tcp_string_to_address; | ||
2087 | plugin->service = service; | 2191 | plugin->service = service; |
2088 | if (service != NULL) | 2192 | if (service != NULL) |
2089 | { | 2193 | { |