From 211fd52268a5ae7856273dd8d8b3b3ed427beadb Mon Sep 17 00:00:00 2001 From: Maximilian Szengel Date: Fri, 2 Nov 2012 16:41:25 +0000 Subject: [PATCH] Using regex for exit/vpn --- src/exit/Makefile.am | 1 + src/exit/gnunet-daemon-exit.c | 45 +++++++++++++++++++++++++++++++ src/include/gnunet_applications.h | 6 +++++ src/vpn/Makefile.am | 1 + src/vpn/gnunet-service-vpn.c | 43 ++++++++++++++++++++--------- 5 files changed, 84 insertions(+), 12 deletions(-) diff --git a/src/exit/Makefile.am b/src/exit/Makefile.am index dd030e7a4..2da59f14a 100644 --- a/src/exit/Makefile.am +++ b/src/exit/Makefile.am @@ -41,4 +41,5 @@ gnunet_daemon_exit_LDADD = \ $(top_builddir)/src/tun/libgnunettun.la \ $(top_builddir)/src/util/libgnunetutil.la \ $(top_builddir)/src/mesh/libgnunetmesh.la \ + $(top_builddir)/src/regex/libgnunetregex.la \ $(GN_LIBINTL) diff --git a/src/exit/gnunet-daemon-exit.c b/src/exit/gnunet-daemon-exit.c index 30be57845..e03a9b55c 100644 --- a/src/exit/gnunet-daemon-exit.c +++ b/src/exit/gnunet-daemon-exit.c @@ -42,8 +42,22 @@ #include "gnunet_statistics_service.h" #include "gnunet_constants.h" #include "gnunet_tun_lib.h" +#include "gnunet_regex_lib.h" #include "exit.h" +/** + * Maximum path compression length for mesh regex announcing for IPv4 address + * based regex. + */ +#define REGEX_MAX_PATH_LEN_IPV4 4 + +/** + * Maximum path compression length for mesh regex announcing for IPv6 address + * based regex. + */ +#define REGEX_MAX_PATH_LEN_IPV6 8 + + /** * Information about an address. */ @@ -3000,6 +3014,9 @@ run (void *cls, char *const *args GNUNET_UNUSED, char *ipv4addr; char *ipv4mask; char *binary; + char *regex; + char ipv4regex[GNUNET_REGEX_IPV4_REGEXLEN]; + char ipv6regex[GNUNET_REGEX_IPV6_REGEXLEN]; binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-exit"); if (GNUNET_YES != @@ -3189,6 +3206,34 @@ run (void *cls, char *const *args GNUNET_UNUSED, GNUNET_SCHEDULER_shutdown (); return; } + + /* Mesh handle acquired, now announce regular expressions matching our exit */ + if (GNUNET_YES == ipv4_enabled && GNUNET_YES == ipv4_exit) + { + GNUNET_REGEX_ipv4toregex (&exit_ipv4addr, ipv4mask, ipv4regex); + GNUNET_asprintf (®ex, "%s%s%s", + GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX, + "4", + ipv4regex); + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing exit regex: %s\n", regex); + GNUNET_MESH_announce_regex (mesh_handle, regex, REGEX_MAX_PATH_LEN_IPV4); + GNUNET_free (regex); + } + + if (GNUNET_YES == ipv6_enabled && GNUNET_YES == ipv6_exit) + { + GNUNET_REGEX_ipv6toregex (&exit_ipv6addr, ipv6prefix, ipv6regex); + GNUNET_asprintf (®ex, "%s%s%s", + GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX, + "6", + ipv6regex); + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing exit regex: %s\n", regex); + GNUNET_MESH_announce_regex (mesh_handle, regex, REGEX_MAX_PATH_LEN_IPV4); + GNUNET_free (regex); + } + helper_handle = GNUNET_HELPER_start (GNUNET_NO, "gnunet-helper-exit", exit_argv, diff --git a/src/include/gnunet_applications.h b/src/include/gnunet_applications.h index 5feaeec6b..1e9db3e72 100644 --- a/src/include/gnunet_applications.h +++ b/src/include/gnunet_applications.h @@ -61,6 +61,12 @@ extern "C" */ #define GNUNET_APPLICATION_TYPE_IPV6_GATEWAY 17 +/** + * Internet exit regex prefix. Consisting of application ID, followed by version + * and padding. + */ +#define GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX "GNEX0001PADPADPA" + #if 0 /* keep Emacsens' auto-indent happy */ { diff --git a/src/vpn/Makefile.am b/src/vpn/Makefile.am index 453b3b908..71601383e 100644 --- a/src/vpn/Makefile.am +++ b/src/vpn/Makefile.am @@ -47,6 +47,7 @@ gnunet_service_vpn_LDADD = \ $(top_builddir)/src/tun/libgnunettun.la \ $(top_builddir)/src/util/libgnunetutil.la \ $(top_builddir)/src/mesh/libgnunetmesh.la \ + $(top_builddir)/src/regex/libgnunetregex.la \ $(GN_LIBINTL) gnunet_service_vpn_CFLAGS = \ -I$(top_srcdir)/src/exit $(CFLAGS) diff --git a/src/vpn/gnunet-service-vpn.c b/src/vpn/gnunet-service-vpn.c index abdc16d79..5e1452bec 100644 --- a/src/vpn/gnunet-service-vpn.c +++ b/src/vpn/gnunet-service-vpn.c @@ -35,6 +35,7 @@ #include "gnunet_statistics_service.h" #include "gnunet_constants.h" #include "gnunet_tun_lib.h" +#include "gnunet_regex_lib.h" #include "vpn.h" #include "exit.h" @@ -807,27 +808,45 @@ create_tunnel_to_destination (struct DestinationEntry *de, } else { + char *policy; + switch (de->details.exit_destination.af) { case AF_INET: - GNUNET_MESH_peer_request_connect_by_type (ts->tunnel, - GNUNET_APPLICATION_TYPE_IPV4_GATEWAY); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Creating tunnel to exit peer for %s\n", - "IPv4"); - break; + { + char address[GNUNET_REGEX_IPV4_REGEXLEN]; + GNUNET_REGEX_ipv4toregex (&de->details.exit_destination.ip.v4, + "255.255.255.255", address); + GNUNET_asprintf (&policy, "%s%s%s", + GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX, + "4", + address); + break; + } case AF_INET6: - GNUNET_MESH_peer_request_connect_by_type (ts->tunnel, - GNUNET_APPLICATION_TYPE_IPV6_GATEWAY); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Creating tunnel to exit peer for %s\n", - "IPv6"); + { + char address[GNUNET_REGEX_IPV6_REGEXLEN]; + GNUNET_REGEX_ipv6toregex (&de->details.exit_destination.ip.v6, + 128, address); + GNUNET_asprintf (&policy, "%s%s%s", + GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX, + "6", + address); break; + } default: GNUNET_assert (0); break; } - } + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Requesting connect by string: %s\n", policy); + + GNUNET_MESH_peer_request_connect_by_string (ts->tunnel, policy); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Creating tunnel to exit peer for policy `%s'\n", + policy); + GNUNET_free (policy); + } return ts; } -- 2.25.1