aboutsummaryrefslogtreecommitdiff
path: root/src/mesh
diff options
context:
space:
mode:
Diffstat (limited to 'src/mesh')
-rw-r--r--src/mesh/Makefile.am142
-rw-r--r--src/mesh/Makefile.in1052
-rw-r--r--src/mesh/gnunet-service-mesh.c4877
-rw-r--r--src/mesh/mesh.conf.in13
-rw-r--r--src/mesh/mesh.h263
-rw-r--r--src/mesh/mesh_api.c1713
-rw-r--r--src/mesh/mesh_protocol.h292
-rw-r--r--src/mesh/mesh_tunnel_tree.c1075
-rw-r--r--src/mesh/mesh_tunnel_tree.h345
-rw-r--r--src/mesh/test_mesh.conf69
-rw-r--r--src/mesh/test_mesh_2dtorus.c370
-rw-r--r--src/mesh/test_mesh_2dtorus.conf87
-rw-r--r--src/mesh/test_mesh_api.c192
-rw-r--r--src/mesh/test_mesh_local_1.c357
-rw-r--r--src/mesh/test_mesh_local_2.c349
-rw-r--r--src/mesh/test_mesh_path.conf69
-rw-r--r--src/mesh/test_mesh_small.c1020
-rw-r--r--src/mesh/test_mesh_small.conf95
-rw-r--r--src/mesh/test_mesh_tree_api.c378
19 files changed, 12758 insertions, 0 deletions
diff --git a/src/mesh/Makefile.am b/src/mesh/Makefile.am
new file mode 100644
index 0000000..e9c0109
--- /dev/null
+++ b/src/mesh/Makefile.am
@@ -0,0 +1,142 @@
+INCLUDES = -I$(top_srcdir)/src/include
+
+if MINGW
+ WINFLAGS = -Wl,--no-undefined -Wl,--export-all-symbols
+endif
+
+if USE_COVERAGE
+ AM_CFLAGS = --coverage -O0
+ XLIB = -lgcov
+endif
+
+pkgcfgdir= $(pkgdatadir)/config.d/
+
+pkgcfg_DATA = \
+ mesh.conf
+
+AM_CLFAGS = -g
+
+bin_PROGRAMS = \
+ gnunet-service-mesh
+
+lib_LTLIBRARIES = \
+ libgnunetmesh.la
+
+gnunet_service_mesh_SOURCES = \
+ gnunet-service-mesh.c \
+ mesh_tunnel_tree.c mesh_tunnel_tree.h
+gnunet_service_mesh_LDADD = \
+ $(top_builddir)/src/core/libgnunetcore.la\
+ $(top_builddir)/src/dht/libgnunetdht.la \
+ $(top_builddir)/src/util/libgnunetutil.la
+ gnunet_service_mesh_DEPENDENCIES = \
+ $(top_builddir)/src/core/libgnunetcore.la\
+ $(top_builddir)/src/dht/libgnunetdht.la \
+ $(top_builddir)/src/util/libgnunetutil.la
+
+libgnunetmesh_la_SOURCES = \
+ mesh_api.c mesh.h mesh_protocol.h
+libgnunetmesh_la_LIBADD = \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(XLIB)
+libgnunetmesh_la_LDFLAGS = \
+ $(GN_LIB_LDFLAGS) $(WINFLAGS) \
+ -version-info 0:0:0
+
+check_PROGRAMS = \
+ test_mesh_api \
+ test_mesh_tree_api \
+ test_mesh_local_1 \
+ test_mesh_local_2 \
+ test_mesh_2dtorus \
+ test_mesh_small_unicast \
+ test_mesh_small_multicast \
+ test_mesh_small_speed \
+ test_mesh_small_speed_ack
+
+test_mesh_api_SOURCES = \
+ test_mesh_api.c
+test_mesh_api_LDADD = \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(top_builddir)/src/mesh/libgnunetmesh.la
+test_mesh_api_DEPENDENCIES = \
+ libgnunetmesh.la \
+ $(top_builddir)/src/util/libgnunetutil.la
+
+test_mesh_tree_api_SOURCES = \
+ test_mesh_tree_api.c
+test_mesh_tree_api_LDADD = \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(top_builddir)/src/dht/libgnunetdht.la
+test_mesh_tree_api_DEPENDENCIES = \
+ libgnunetmesh.la \
+ $(top_builddir)/src/dht/libgnunetdht.la
+
+test_mesh_local_1_SOURCES = \
+ test_mesh_local_1.c
+test_mesh_local_1_LDADD = \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(top_builddir)/src/mesh/libgnunetmesh.la
+test_mesh_local_1_DEPENDENCIES = \
+ libgnunetmesh.la
+
+test_mesh_local_2_SOURCES = \
+ test_mesh_local_2.c
+test_mesh_local_2_LDADD = \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(top_builddir)/src/mesh/libgnunetmesh.la
+test_mesh_local_2_DEPENDENCIES = \
+ libgnunetmesh.la
+
+test_mesh_2dtorus_SOURCES = \
+ test_mesh_2dtorus.c
+test_mesh_2dtorus_LDADD = \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(top_builddir)/src/testing/libgnunettesting.la
+
+test_mesh_small_unicast_SOURCES = \
+ test_mesh_small.c
+test_mesh_small_unicast_LDADD = \
+ $(top_builddir)/src/mesh/libgnunetmesh.la \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(top_builddir)/src/testing/libgnunettesting.la
+test_mesh_small_unicast_DEPENDENCIES = \
+ libgnunetmesh.la
+
+test_mesh_small_multicast_SOURCES = \
+ test_mesh_small.c
+test_mesh_small_multicast_LDADD = \
+ $(top_builddir)/src/mesh/libgnunetmesh.la \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(top_builddir)/src/testing/libgnunettesting.la
+test_mesh_small_multicast_DEPENDENCIES = \
+ libgnunetmesh.la
+
+test_mesh_small_speed_SOURCES = \
+ test_mesh_small.c
+test_mesh_small_speed_LDADD = \
+ $(top_builddir)/src/mesh/libgnunetmesh.la \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(top_builddir)/src/testing/libgnunettesting.la
+test_mesh_small_speed_DEPENDENCIES = \
+ libgnunetmesh.la
+
+test_mesh_small_speed_ack_SOURCES = \
+ test_mesh_small.c
+test_mesh_small_speed_ack_LDADD = \
+ $(top_builddir)/src/mesh/libgnunetmesh.la \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(top_builddir)/src/testing/libgnunettesting.la
+test_mesh_small_speed_ack_DEPENDENCIES = \
+ libgnunetmesh.la
+
+if ENABLE_TEST_RUN
+TESTS = test_mesh_api test_mesh_tree_api test_mesh_local_1 test_mesh_local_2 \
+ test_mesh_2dtorus test_mesh_small_unicast test_mesh_small_multicast
+endif
+
+EXTRA_DIST = \
+ test_mesh.conf \
+ test_mesh_2dtorus.conf \
+ test_mesh_small.conf \
+ test_mesh_path.conf
diff --git a/src/mesh/Makefile.in b/src/mesh/Makefile.in
new file mode 100644
index 0000000..337d6ab
--- /dev/null
+++ b/src/mesh/Makefile.in
@@ -0,0 +1,1052 @@
+# Makefile.in generated by automake 1.11.1 from Makefile.am.
+# @configure_input@
+
+# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
+# 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation,
+# Inc.
+# This Makefile.in is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
+# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE.
+
+@SET_MAKE@
+
+
+
+VPATH = @srcdir@
+pkgdatadir = $(datadir)/@PACKAGE@
+pkgincludedir = $(includedir)/@PACKAGE@
+pkglibdir = $(libdir)/@PACKAGE@
+pkglibexecdir = $(libexecdir)/@PACKAGE@
+am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd
+install_sh_DATA = $(install_sh) -c -m 644
+install_sh_PROGRAM = $(install_sh) -c
+install_sh_SCRIPT = $(install_sh) -c
+INSTALL_HEADER = $(INSTALL_DATA)
+transform = $(program_transform_name)
+NORMAL_INSTALL = :
+PRE_INSTALL = :
+POST_INSTALL = :
+NORMAL_UNINSTALL = :
+PRE_UNINSTALL = :
+POST_UNINSTALL = :
+build_triplet = @build@
+host_triplet = @host@
+target_triplet = @target@
+bin_PROGRAMS = gnunet-service-mesh$(EXEEXT)
+check_PROGRAMS = test_mesh_api$(EXEEXT) test_mesh_tree_api$(EXEEXT) \
+ test_mesh_local_1$(EXEEXT) test_mesh_local_2$(EXEEXT) \
+ test_mesh_2dtorus$(EXEEXT) test_mesh_small_unicast$(EXEEXT) \
+ test_mesh_small_multicast$(EXEEXT) \
+ test_mesh_small_speed$(EXEEXT) \
+ test_mesh_small_speed_ack$(EXEEXT)
+@ENABLE_TEST_RUN_TRUE@TESTS = test_mesh_api$(EXEEXT) \
+@ENABLE_TEST_RUN_TRUE@ test_mesh_tree_api$(EXEEXT) \
+@ENABLE_TEST_RUN_TRUE@ test_mesh_local_1$(EXEEXT) \
+@ENABLE_TEST_RUN_TRUE@ test_mesh_local_2$(EXEEXT) \
+@ENABLE_TEST_RUN_TRUE@ test_mesh_2dtorus$(EXEEXT) \
+@ENABLE_TEST_RUN_TRUE@ test_mesh_small_unicast$(EXEEXT) \
+@ENABLE_TEST_RUN_TRUE@ test_mesh_small_multicast$(EXEEXT)
+subdir = src/mesh
+DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in \
+ $(srcdir)/mesh.conf.in
+ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
+am__aclocal_m4_deps = $(top_srcdir)/m4/absolute-header.m4 \
+ $(top_srcdir)/m4/align.m4 $(top_srcdir)/m4/argz.m4 \
+ $(top_srcdir)/m4/gettext.m4 $(top_srcdir)/m4/iconv.m4 \
+ $(top_srcdir)/m4/lib-ld.m4 $(top_srcdir)/m4/lib-link.m4 \
+ $(top_srcdir)/m4/lib-prefix.m4 $(top_srcdir)/m4/libcurl.m4 \
+ $(top_srcdir)/m4/libgcrypt.m4 $(top_srcdir)/m4/libtool.m4 \
+ $(top_srcdir)/m4/libunistring.m4 $(top_srcdir)/m4/ltdl.m4 \
+ $(top_srcdir)/m4/ltoptions.m4 $(top_srcdir)/m4/ltsugar.m4 \
+ $(top_srcdir)/m4/ltversion.m4 $(top_srcdir)/m4/lt~obsolete.m4 \
+ $(top_srcdir)/m4/nls.m4 $(top_srcdir)/m4/po.m4 \
+ $(top_srcdir)/m4/progtest.m4 $(top_srcdir)/acinclude.m4 \
+ $(top_srcdir)/configure.ac
+am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \
+ $(ACLOCAL_M4)
+mkinstalldirs = $(install_sh) -d
+CONFIG_HEADER = $(top_builddir)/gnunet_config.h
+CONFIG_CLEAN_FILES = mesh.conf
+CONFIG_CLEAN_VPATH_FILES =
+am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`;
+am__vpath_adj = case $$p in \
+ $(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \
+ *) f=$$p;; \
+ esac;
+am__strip_dir = f=`echo $$p | sed -e 's|^.*/||'`;
+am__install_max = 40
+am__nobase_strip_setup = \
+ srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*|]/\\\\&/g'`
+am__nobase_strip = \
+ for p in $$list; do echo "$$p"; done | sed -e "s|$$srcdirstrip/||"
+am__nobase_list = $(am__nobase_strip_setup); \
+ for p in $$list; do echo "$$p $$p"; done | \
+ sed "s| $$srcdirstrip/| |;"' / .*\//!s/ .*/ ./; s,\( .*\)/[^/]*$$,\1,' | \
+ $(AWK) 'BEGIN { files["."] = "" } { files[$$2] = files[$$2] " " $$1; \
+ if (++n[$$2] == $(am__install_max)) \
+ { print $$2, files[$$2]; n[$$2] = 0; files[$$2] = "" } } \
+ END { for (dir in files) print dir, files[dir] }'
+am__base_list = \
+ sed '$$!N;$$!N;$$!N;$$!N;$$!N;$$!N;$$!N;s/\n/ /g' | \
+ sed '$$!N;$$!N;$$!N;$$!N;s/\n/ /g'
+am__installdirs = "$(DESTDIR)$(libdir)" "$(DESTDIR)$(bindir)" \
+ "$(DESTDIR)$(pkgcfgdir)"
+LTLIBRARIES = $(lib_LTLIBRARIES)
+am__DEPENDENCIES_1 =
+libgnunetmesh_la_DEPENDENCIES = \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(am__DEPENDENCIES_1)
+am_libgnunetmesh_la_OBJECTS = mesh_api.lo
+libgnunetmesh_la_OBJECTS = $(am_libgnunetmesh_la_OBJECTS)
+AM_V_lt = $(am__v_lt_$(V))
+am__v_lt_ = $(am__v_lt_$(AM_DEFAULT_VERBOSITY))
+am__v_lt_0 = --silent
+libgnunetmesh_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC \
+ $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=link $(CCLD) \
+ $(AM_CFLAGS) $(CFLAGS) $(libgnunetmesh_la_LDFLAGS) $(LDFLAGS) \
+ -o $@
+PROGRAMS = $(bin_PROGRAMS)
+am_gnunet_service_mesh_OBJECTS = gnunet-service-mesh.$(OBJEXT) \
+ mesh_tunnel_tree.$(OBJEXT)
+gnunet_service_mesh_OBJECTS = $(am_gnunet_service_mesh_OBJECTS)
+am_test_mesh_2dtorus_OBJECTS = test_mesh_2dtorus.$(OBJEXT)
+test_mesh_2dtorus_OBJECTS = $(am_test_mesh_2dtorus_OBJECTS)
+test_mesh_2dtorus_DEPENDENCIES = \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(top_builddir)/src/testing/libgnunettesting.la
+am_test_mesh_api_OBJECTS = test_mesh_api.$(OBJEXT)
+test_mesh_api_OBJECTS = $(am_test_mesh_api_OBJECTS)
+am_test_mesh_local_1_OBJECTS = test_mesh_local_1.$(OBJEXT)
+test_mesh_local_1_OBJECTS = $(am_test_mesh_local_1_OBJECTS)
+am_test_mesh_local_2_OBJECTS = test_mesh_local_2.$(OBJEXT)
+test_mesh_local_2_OBJECTS = $(am_test_mesh_local_2_OBJECTS)
+am_test_mesh_small_multicast_OBJECTS = test_mesh_small.$(OBJEXT)
+test_mesh_small_multicast_OBJECTS = \
+ $(am_test_mesh_small_multicast_OBJECTS)
+am_test_mesh_small_speed_OBJECTS = test_mesh_small.$(OBJEXT)
+test_mesh_small_speed_OBJECTS = $(am_test_mesh_small_speed_OBJECTS)
+am_test_mesh_small_speed_ack_OBJECTS = test_mesh_small.$(OBJEXT)
+test_mesh_small_speed_ack_OBJECTS = \
+ $(am_test_mesh_small_speed_ack_OBJECTS)
+am_test_mesh_small_unicast_OBJECTS = test_mesh_small.$(OBJEXT)
+test_mesh_small_unicast_OBJECTS = \
+ $(am_test_mesh_small_unicast_OBJECTS)
+am_test_mesh_tree_api_OBJECTS = test_mesh_tree_api.$(OBJEXT)
+test_mesh_tree_api_OBJECTS = $(am_test_mesh_tree_api_OBJECTS)
+DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)
+depcomp = $(SHELL) $(top_srcdir)/depcomp
+am__depfiles_maybe = depfiles
+am__mv = mv -f
+COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
+ $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
+LTCOMPILE = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
+ $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) \
+ $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \
+ $(AM_CFLAGS) $(CFLAGS)
+AM_V_CC = $(am__v_CC_$(V))
+am__v_CC_ = $(am__v_CC_$(AM_DEFAULT_VERBOSITY))
+am__v_CC_0 = @echo " CC " $@;
+AM_V_at = $(am__v_at_$(V))
+am__v_at_ = $(am__v_at_$(AM_DEFAULT_VERBOSITY))
+am__v_at_0 = @
+CCLD = $(CC)
+LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
+ $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
+ $(AM_LDFLAGS) $(LDFLAGS) -o $@
+AM_V_CCLD = $(am__v_CCLD_$(V))
+am__v_CCLD_ = $(am__v_CCLD_$(AM_DEFAULT_VERBOSITY))
+am__v_CCLD_0 = @echo " CCLD " $@;
+AM_V_GEN = $(am__v_GEN_$(V))
+am__v_GEN_ = $(am__v_GEN_$(AM_DEFAULT_VERBOSITY))
+am__v_GEN_0 = @echo " GEN " $@;
+SOURCES = $(libgnunetmesh_la_SOURCES) $(gnunet_service_mesh_SOURCES) \
+ $(test_mesh_2dtorus_SOURCES) $(test_mesh_api_SOURCES) \
+ $(test_mesh_local_1_SOURCES) $(test_mesh_local_2_SOURCES) \
+ $(test_mesh_small_multicast_SOURCES) \
+ $(test_mesh_small_speed_SOURCES) \
+ $(test_mesh_small_speed_ack_SOURCES) \
+ $(test_mesh_small_unicast_SOURCES) \
+ $(test_mesh_tree_api_SOURCES)
+DIST_SOURCES = $(libgnunetmesh_la_SOURCES) \
+ $(gnunet_service_mesh_SOURCES) $(test_mesh_2dtorus_SOURCES) \
+ $(test_mesh_api_SOURCES) $(test_mesh_local_1_SOURCES) \
+ $(test_mesh_local_2_SOURCES) \
+ $(test_mesh_small_multicast_SOURCES) \
+ $(test_mesh_small_speed_SOURCES) \
+ $(test_mesh_small_speed_ack_SOURCES) \
+ $(test_mesh_small_unicast_SOURCES) \
+ $(test_mesh_tree_api_SOURCES)
+DATA = $(pkgcfg_DATA)
+ETAGS = etags
+CTAGS = ctags
+am__tty_colors = \
+red=; grn=; lgn=; blu=; std=
+DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
+ACLOCAL = @ACLOCAL@
+AMTAR = @AMTAR@
+AM_DEFAULT_VERBOSITY = @AM_DEFAULT_VERBOSITY@
+AR = @AR@
+ARGZ_H = @ARGZ_H@
+AS = @AS@
+AUTOCONF = @AUTOCONF@
+AUTOHEADER = @AUTOHEADER@
+AUTOMAKE = @AUTOMAKE@
+AWK = @AWK@
+CC = @CC@
+CCDEPMODE = @CCDEPMODE@
+CFLAGS = @CFLAGS@
+CPP = @CPP@
+CPPFLAGS = @CPPFLAGS@
+CXX = @CXX@
+CXXCPP = @CXXCPP@
+CXXDEPMODE = @CXXDEPMODE@
+CXXFLAGS = @CXXFLAGS@
+CYGPATH_W = @CYGPATH_W@
+DEFAULT_INTERFACE = @DEFAULT_INTERFACE@
+DEFS = @DEFS@
+DEPDIR = @DEPDIR@
+DLLDIR = @DLLDIR@
+DLLTOOL = @DLLTOOL@
+DSYMUTIL = @DSYMUTIL@
+DUMPBIN = @DUMPBIN@
+ECHO_C = @ECHO_C@
+ECHO_N = @ECHO_N@
+ECHO_T = @ECHO_T@
+EGREP = @EGREP@
+EXEEXT = @EXEEXT@
+EXT_LIBS = @EXT_LIBS@
+EXT_LIB_PATH = @EXT_LIB_PATH@
+FGREP = @FGREP@
+GMSGFMT = @GMSGFMT@
+GMSGFMT_015 = @GMSGFMT_015@
+GNUNETDNS_GROUP = @GNUNETDNS_GROUP@
+GN_DAEMON_CONFIG_DIR = @GN_DAEMON_CONFIG_DIR@
+GN_DAEMON_HOME_DIR = @GN_DAEMON_HOME_DIR@
+GN_INTLINCL = @GN_INTLINCL@
+GN_LIBINTL = @GN_LIBINTL@
+GN_LIB_LDFLAGS = @GN_LIB_LDFLAGS@
+GN_PLUGIN_LDFLAGS = @GN_PLUGIN_LDFLAGS@
+GN_USER_HOME_DIR = @GN_USER_HOME_DIR@
+GREP = @GREP@
+HAVE_LIBUNISTRING = @HAVE_LIBUNISTRING@
+INCLTDL = @INCLTDL@
+INSTALL = @INSTALL@
+INSTALL_DATA = @INSTALL_DATA@
+INSTALL_PROGRAM = @INSTALL_PROGRAM@
+INSTALL_SCRIPT = @INSTALL_SCRIPT@
+INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@
+INTLLIBS = @INTLLIBS@
+INTL_MACOSX_LIBS = @INTL_MACOSX_LIBS@
+LD = @LD@
+LDFLAGS = @LDFLAGS@
+LIBADD_DL = @LIBADD_DL@
+LIBADD_DLD_LINK = @LIBADD_DLD_LINK@
+LIBADD_DLOPEN = @LIBADD_DLOPEN@
+LIBADD_SHL_LOAD = @LIBADD_SHL_LOAD@
+LIBCURL = @LIBCURL@
+LIBCURL_CPPFLAGS = @LIBCURL_CPPFLAGS@
+LIBGCRYPT_CFLAGS = @LIBGCRYPT_CFLAGS@
+LIBGCRYPT_CONFIG = @LIBGCRYPT_CONFIG@
+LIBGCRYPT_LIBS = @LIBGCRYPT_LIBS@
+LIBICONV = @LIBICONV@
+LIBINTL = @LIBINTL@
+LIBLTDL = @LIBLTDL@
+LIBOBJS = @LIBOBJS@
+LIBPREFIX = @LIBPREFIX@
+LIBS = @LIBS@
+LIBTOOL = @LIBTOOL@
+LIBUNISTRING = @LIBUNISTRING@
+LIPO = @LIPO@
+LN_S = @LN_S@
+LTDLDEPS = @LTDLDEPS@
+LTDLINCL = @LTDLINCL@
+LTDLOPEN = @LTDLOPEN@
+LTLIBICONV = @LTLIBICONV@
+LTLIBINTL = @LTLIBINTL@
+LTLIBOBJS = @LTLIBOBJS@
+LTLIBUNISTRING = @LTLIBUNISTRING@
+LT_CONFIG_H = @LT_CONFIG_H@
+LT_DLLOADERS = @LT_DLLOADERS@
+LT_DLPREOPEN = @LT_DLPREOPEN@
+MAKEINFO = @MAKEINFO@
+MKDIR_P = @MKDIR_P@
+MSGFMT = @MSGFMT@
+MSGFMT_015 = @MSGFMT_015@
+MSGMERGE = @MSGMERGE@
+MYSQL_CPPFLAGS = @MYSQL_CPPFLAGS@
+MYSQL_LDFLAGS = @MYSQL_LDFLAGS@
+NM = @NM@
+NMEDIT = @NMEDIT@
+OBJC = @OBJC@
+OBJCDEPMODE = @OBJCDEPMODE@
+OBJCFLAGS = @OBJCFLAGS@
+OBJDUMP = @OBJDUMP@
+OBJEXT = @OBJEXT@
+OTOOL = @OTOOL@
+OTOOL64 = @OTOOL64@
+PACKAGE = @PACKAGE@
+PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@
+PACKAGE_NAME = @PACKAGE_NAME@
+PACKAGE_STRING = @PACKAGE_STRING@
+PACKAGE_TARNAME = @PACKAGE_TARNAME@
+PACKAGE_URL = @PACKAGE_URL@
+PACKAGE_VERSION = @PACKAGE_VERSION@
+PATH_SEPARATOR = @PATH_SEPARATOR@
+POSTGRES_CPPFLAGS = @POSTGRES_CPPFLAGS@
+POSTGRES_LDFLAGS = @POSTGRES_LDFLAGS@
+POSUB = @POSUB@
+PYTHON = @PYTHON@
+PYTHON_EXEC_PREFIX = @PYTHON_EXEC_PREFIX@
+PYTHON_PLATFORM = @PYTHON_PLATFORM@
+PYTHON_PREFIX = @PYTHON_PREFIX@
+PYTHON_VERSION = @PYTHON_VERSION@
+RANLIB = @RANLIB@
+SED = @SED@
+SET_MAKE = @SET_MAKE@
+SHELL = @SHELL@
+SQLITE_CPPFLAGS = @SQLITE_CPPFLAGS@
+SQLITE_LDFLAGS = @SQLITE_LDFLAGS@
+STRIP = @STRIP@
+SUDO_BINARY = @SUDO_BINARY@
+UNIXONLY = @UNIXONLY@
+USE_NLS = @USE_NLS@
+VERSION = @VERSION@
+XGETTEXT = @XGETTEXT@
+XGETTEXT_015 = @XGETTEXT_015@
+XMKMF = @XMKMF@
+X_CFLAGS = @X_CFLAGS@
+X_EXTRA_LIBS = @X_EXTRA_LIBS@
+X_LIBS = @X_LIBS@
+X_PRE_LIBS = @X_PRE_LIBS@
+_libcurl_config = @_libcurl_config@
+abs_builddir = @abs_builddir@
+abs_srcdir = @abs_srcdir@
+abs_top_builddir = @abs_top_builddir@
+abs_top_srcdir = @abs_top_srcdir@
+ac_ct_CC = @ac_ct_CC@
+ac_ct_CXX = @ac_ct_CXX@
+ac_ct_DUMPBIN = @ac_ct_DUMPBIN@
+ac_ct_OBJC = @ac_ct_OBJC@
+am__include = @am__include@
+am__leading_dot = @am__leading_dot@
+am__quote = @am__quote@
+am__tar = @am__tar@
+am__untar = @am__untar@
+bindir = @bindir@
+build = @build@
+build_alias = @build_alias@
+build_cpu = @build_cpu@
+build_os = @build_os@
+build_target = @build_target@
+build_vendor = @build_vendor@
+builddir = @builddir@
+datadir = @datadir@
+datarootdir = @datarootdir@
+docdir = @docdir@
+dvidir = @dvidir@
+exec_prefix = @exec_prefix@
+host = @host@
+host_alias = @host_alias@
+host_cpu = @host_cpu@
+host_os = @host_os@
+host_vendor = @host_vendor@
+htmldir = @htmldir@
+includedir = @includedir@
+infodir = @infodir@
+install_sh = @install_sh@
+libdir = @libdir@
+libexecdir = @libexecdir@
+localedir = @localedir@
+localstatedir = @localstatedir@
+lt_ECHO = @lt_ECHO@
+ltdl_LIBOBJS = @ltdl_LIBOBJS@
+ltdl_LTLIBOBJS = @ltdl_LTLIBOBJS@
+mandir = @mandir@
+mkdir_p = @mkdir_p@
+oldincludedir = @oldincludedir@
+pdfdir = @pdfdir@
+pkgpyexecdir = @pkgpyexecdir@
+pkgpythondir = @pkgpythondir@
+prefix = @prefix@
+program_transform_name = @program_transform_name@
+psdir = @psdir@
+pyexecdir = @pyexecdir@
+pythondir = @pythondir@
+sbindir = @sbindir@
+sharedstatedir = @sharedstatedir@
+srcdir = @srcdir@
+subdirs = @subdirs@
+sys_symbol_underscore = @sys_symbol_underscore@
+sysconfdir = @sysconfdir@
+target = @target@
+target_alias = @target_alias@
+target_cpu = @target_cpu@
+target_os = @target_os@
+target_vendor = @target_vendor@
+top_build_prefix = @top_build_prefix@
+top_builddir = @top_builddir@
+top_srcdir = @top_srcdir@
+INCLUDES = -I$(top_srcdir)/src/include
+@MINGW_TRUE@WINFLAGS = -Wl,--no-undefined -Wl,--export-all-symbols
+@USE_COVERAGE_TRUE@AM_CFLAGS = --coverage -O0
+@USE_COVERAGE_TRUE@XLIB = -lgcov
+pkgcfgdir = $(pkgdatadir)/config.d/
+pkgcfg_DATA = \
+ mesh.conf
+
+AM_CLFAGS = -g
+lib_LTLIBRARIES = \
+ libgnunetmesh.la
+
+gnunet_service_mesh_SOURCES = \
+ gnunet-service-mesh.c \
+ mesh_tunnel_tree.c mesh_tunnel_tree.h
+
+gnunet_service_mesh_LDADD = \
+ $(top_builddir)/src/core/libgnunetcore.la\
+ $(top_builddir)/src/dht/libgnunetdht.la \
+ $(top_builddir)/src/util/libgnunetutil.la
+
+gnunet_service_mesh_DEPENDENCIES = \
+ $(top_builddir)/src/core/libgnunetcore.la\
+ $(top_builddir)/src/dht/libgnunetdht.la \
+ $(top_builddir)/src/util/libgnunetutil.la
+
+libgnunetmesh_la_SOURCES = \
+ mesh_api.c mesh.h mesh_protocol.h
+
+libgnunetmesh_la_LIBADD = \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(XLIB)
+
+libgnunetmesh_la_LDFLAGS = \
+ $(GN_LIB_LDFLAGS) $(WINFLAGS) \
+ -version-info 0:0:0
+
+test_mesh_api_SOURCES = \
+ test_mesh_api.c
+
+test_mesh_api_LDADD = \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(top_builddir)/src/mesh/libgnunetmesh.la
+
+test_mesh_api_DEPENDENCIES = \
+ libgnunetmesh.la \
+ $(top_builddir)/src/util/libgnunetutil.la
+
+test_mesh_tree_api_SOURCES = \
+ test_mesh_tree_api.c
+
+test_mesh_tree_api_LDADD = \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(top_builddir)/src/dht/libgnunetdht.la
+
+test_mesh_tree_api_DEPENDENCIES = \
+ libgnunetmesh.la \
+ $(top_builddir)/src/dht/libgnunetdht.la
+
+test_mesh_local_1_SOURCES = \
+ test_mesh_local_1.c
+
+test_mesh_local_1_LDADD = \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(top_builddir)/src/mesh/libgnunetmesh.la
+
+test_mesh_local_1_DEPENDENCIES = \
+ libgnunetmesh.la
+
+test_mesh_local_2_SOURCES = \
+ test_mesh_local_2.c
+
+test_mesh_local_2_LDADD = \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(top_builddir)/src/mesh/libgnunetmesh.la
+
+test_mesh_local_2_DEPENDENCIES = \
+ libgnunetmesh.la
+
+test_mesh_2dtorus_SOURCES = \
+ test_mesh_2dtorus.c
+
+test_mesh_2dtorus_LDADD = \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(top_builddir)/src/testing/libgnunettesting.la
+
+test_mesh_small_unicast_SOURCES = \
+ test_mesh_small.c
+
+test_mesh_small_unicast_LDADD = \
+ $(top_builddir)/src/mesh/libgnunetmesh.la \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(top_builddir)/src/testing/libgnunettesting.la
+
+test_mesh_small_unicast_DEPENDENCIES = \
+ libgnunetmesh.la
+
+test_mesh_small_multicast_SOURCES = \
+ test_mesh_small.c
+
+test_mesh_small_multicast_LDADD = \
+ $(top_builddir)/src/mesh/libgnunetmesh.la \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(top_builddir)/src/testing/libgnunettesting.la
+
+test_mesh_small_multicast_DEPENDENCIES = \
+ libgnunetmesh.la
+
+test_mesh_small_speed_SOURCES = \
+ test_mesh_small.c
+
+test_mesh_small_speed_LDADD = \
+ $(top_builddir)/src/mesh/libgnunetmesh.la \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(top_builddir)/src/testing/libgnunettesting.la
+
+test_mesh_small_speed_DEPENDENCIES = \
+ libgnunetmesh.la
+
+test_mesh_small_speed_ack_SOURCES = \
+ test_mesh_small.c
+
+test_mesh_small_speed_ack_LDADD = \
+ $(top_builddir)/src/mesh/libgnunetmesh.la \
+ $(top_builddir)/src/util/libgnunetutil.la \
+ $(top_builddir)/src/testing/libgnunettesting.la
+
+test_mesh_small_speed_ack_DEPENDENCIES = \
+ libgnunetmesh.la
+
+EXTRA_DIST = \
+ test_mesh.conf \
+ test_mesh_2dtorus.conf \
+ test_mesh_small.conf \
+ test_mesh_path.conf
+
+all: all-am
+
+.SUFFIXES:
+.SUFFIXES: .c .lo .o .obj
+$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps)
+ @for dep in $?; do \
+ case '$(am__configure_deps)' in \
+ *$$dep*) \
+ ( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \
+ && { if test -f $@; then exit 0; else break; fi; }; \
+ exit 1;; \
+ esac; \
+ done; \
+ echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu src/mesh/Makefile'; \
+ $(am__cd) $(top_srcdir) && \
+ $(AUTOMAKE) --gnu src/mesh/Makefile
+.PRECIOUS: Makefile
+Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
+ @case '$?' in \
+ *config.status*) \
+ cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \
+ *) \
+ echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)'; \
+ cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \
+ esac;
+
+$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES)
+ cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
+
+$(top_srcdir)/configure: $(am__configure_deps)
+ cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
+$(ACLOCAL_M4): $(am__aclocal_m4_deps)
+ cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
+$(am__aclocal_m4_deps):
+mesh.conf: $(top_builddir)/config.status $(srcdir)/mesh.conf.in
+ cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@
+install-libLTLIBRARIES: $(lib_LTLIBRARIES)
+ @$(NORMAL_INSTALL)
+ test -z "$(libdir)" || $(MKDIR_P) "$(DESTDIR)$(libdir)"
+ @list='$(lib_LTLIBRARIES)'; test -n "$(libdir)" || list=; \
+ list2=; for p in $$list; do \
+ if test -f $$p; then \
+ list2="$$list2 $$p"; \
+ else :; fi; \
+ done; \
+ test -z "$$list2" || { \
+ echo " $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=install $(INSTALL) $(INSTALL_STRIP_FLAG) $$list2 '$(DESTDIR)$(libdir)'"; \
+ $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=install $(INSTALL) $(INSTALL_STRIP_FLAG) $$list2 "$(DESTDIR)$(libdir)"; \
+ }
+
+uninstall-libLTLIBRARIES:
+ @$(NORMAL_UNINSTALL)
+ @list='$(lib_LTLIBRARIES)'; test -n "$(libdir)" || list=; \
+ for p in $$list; do \
+ $(am__strip_dir) \
+ echo " $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=uninstall rm -f '$(DESTDIR)$(libdir)/$$f'"; \
+ $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=uninstall rm -f "$(DESTDIR)$(libdir)/$$f"; \
+ done
+
+clean-libLTLIBRARIES:
+ -test -z "$(lib_LTLIBRARIES)" || rm -f $(lib_LTLIBRARIES)
+ @list='$(lib_LTLIBRARIES)'; for p in $$list; do \
+ dir="`echo $$p | sed -e 's|/[^/]*$$||'`"; \
+ test "$$dir" != "$$p" || dir=.; \
+ echo "rm -f \"$${dir}/so_locations\""; \
+ rm -f "$${dir}/so_locations"; \
+ done
+libgnunetmesh.la: $(libgnunetmesh_la_OBJECTS) $(libgnunetmesh_la_DEPENDENCIES)
+ $(AM_V_CCLD)$(libgnunetmesh_la_LINK) -rpath $(libdir) $(libgnunetmesh_la_OBJECTS) $(libgnunetmesh_la_LIBADD) $(LIBS)
+install-binPROGRAMS: $(bin_PROGRAMS)
+ @$(NORMAL_INSTALL)
+ test -z "$(bindir)" || $(MKDIR_P) "$(DESTDIR)$(bindir)"
+ @list='$(bin_PROGRAMS)'; test -n "$(bindir)" || list=; \
+ for p in $$list; do echo "$$p $$p"; done | \
+ sed 's/$(EXEEXT)$$//' | \
+ while read p p1; do if test -f $$p || test -f $$p1; \
+ then echo "$$p"; echo "$$p"; else :; fi; \
+ done | \
+ sed -e 'p;s,.*/,,;n;h' -e 's|.*|.|' \
+ -e 'p;x;s,.*/,,;s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/' | \
+ sed 'N;N;N;s,\n, ,g' | \
+ $(AWK) 'BEGIN { files["."] = ""; dirs["."] = 1 } \
+ { d=$$3; if (dirs[d] != 1) { print "d", d; dirs[d] = 1 } \
+ if ($$2 == $$4) files[d] = files[d] " " $$1; \
+ else { print "f", $$3 "/" $$4, $$1; } } \
+ END { for (d in files) print "f", d, files[d] }' | \
+ while read type dir files; do \
+ if test "$$dir" = .; then dir=; else dir=/$$dir; fi; \
+ test -z "$$files" || { \
+ echo " $(INSTALL_PROGRAM_ENV) $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=install $(INSTALL_PROGRAM) $$files '$(DESTDIR)$(bindir)$$dir'"; \
+ $(INSTALL_PROGRAM_ENV) $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=install $(INSTALL_PROGRAM) $$files "$(DESTDIR)$(bindir)$$dir" || exit $$?; \
+ } \
+ ; done
+
+uninstall-binPROGRAMS:
+ @$(NORMAL_UNINSTALL)
+ @list='$(bin_PROGRAMS)'; test -n "$(bindir)" || list=; \
+ files=`for p in $$list; do echo "$$p"; done | \
+ sed -e 'h;s,^.*/,,;s/$(EXEEXT)$$//;$(transform)' \
+ -e 's/$$/$(EXEEXT)/' `; \
+ test -n "$$list" || exit 0; \
+ echo " ( cd '$(DESTDIR)$(bindir)' && rm -f" $$files ")"; \
+ cd "$(DESTDIR)$(bindir)" && rm -f $$files
+
+clean-binPROGRAMS:
+ @list='$(bin_PROGRAMS)'; test -n "$$list" || exit 0; \
+ echo " rm -f" $$list; \
+ rm -f $$list || exit $$?; \
+ test -n "$(EXEEXT)" || exit 0; \
+ list=`for p in $$list; do echo "$$p"; done | sed 's/$(EXEEXT)$$//'`; \
+ echo " rm -f" $$list; \
+ rm -f $$list
+
+clean-checkPROGRAMS:
+ @list='$(check_PROGRAMS)'; test -n "$$list" || exit 0; \
+ echo " rm -f" $$list; \
+ rm -f $$list || exit $$?; \
+ test -n "$(EXEEXT)" || exit 0; \
+ list=`for p in $$list; do echo "$$p"; done | sed 's/$(EXEEXT)$$//'`; \
+ echo " rm -f" $$list; \
+ rm -f $$list
+gnunet-service-mesh$(EXEEXT): $(gnunet_service_mesh_OBJECTS) $(gnunet_service_mesh_DEPENDENCIES)
+ @rm -f gnunet-service-mesh$(EXEEXT)
+ $(AM_V_CCLD)$(LINK) $(gnunet_service_mesh_OBJECTS) $(gnunet_service_mesh_LDADD) $(LIBS)
+test_mesh_2dtorus$(EXEEXT): $(test_mesh_2dtorus_OBJECTS) $(test_mesh_2dtorus_DEPENDENCIES)
+ @rm -f test_mesh_2dtorus$(EXEEXT)
+ $(AM_V_CCLD)$(LINK) $(test_mesh_2dtorus_OBJECTS) $(test_mesh_2dtorus_LDADD) $(LIBS)
+test_mesh_api$(EXEEXT): $(test_mesh_api_OBJECTS) $(test_mesh_api_DEPENDENCIES)
+ @rm -f test_mesh_api$(EXEEXT)
+ $(AM_V_CCLD)$(LINK) $(test_mesh_api_OBJECTS) $(test_mesh_api_LDADD) $(LIBS)
+test_mesh_local_1$(EXEEXT): $(test_mesh_local_1_OBJECTS) $(test_mesh_local_1_DEPENDENCIES)
+ @rm -f test_mesh_local_1$(EXEEXT)
+ $(AM_V_CCLD)$(LINK) $(test_mesh_local_1_OBJECTS) $(test_mesh_local_1_LDADD) $(LIBS)
+test_mesh_local_2$(EXEEXT): $(test_mesh_local_2_OBJECTS) $(test_mesh_local_2_DEPENDENCIES)
+ @rm -f test_mesh_local_2$(EXEEXT)
+ $(AM_V_CCLD)$(LINK) $(test_mesh_local_2_OBJECTS) $(test_mesh_local_2_LDADD) $(LIBS)
+test_mesh_small_multicast$(EXEEXT): $(test_mesh_small_multicast_OBJECTS) $(test_mesh_small_multicast_DEPENDENCIES)
+ @rm -f test_mesh_small_multicast$(EXEEXT)
+ $(AM_V_CCLD)$(LINK) $(test_mesh_small_multicast_OBJECTS) $(test_mesh_small_multicast_LDADD) $(LIBS)
+test_mesh_small_speed$(EXEEXT): $(test_mesh_small_speed_OBJECTS) $(test_mesh_small_speed_DEPENDENCIES)
+ @rm -f test_mesh_small_speed$(EXEEXT)
+ $(AM_V_CCLD)$(LINK) $(test_mesh_small_speed_OBJECTS) $(test_mesh_small_speed_LDADD) $(LIBS)
+test_mesh_small_speed_ack$(EXEEXT): $(test_mesh_small_speed_ack_OBJECTS) $(test_mesh_small_speed_ack_DEPENDENCIES)
+ @rm -f test_mesh_small_speed_ack$(EXEEXT)
+ $(AM_V_CCLD)$(LINK) $(test_mesh_small_speed_ack_OBJECTS) $(test_mesh_small_speed_ack_LDADD) $(LIBS)
+test_mesh_small_unicast$(EXEEXT): $(test_mesh_small_unicast_OBJECTS) $(test_mesh_small_unicast_DEPENDENCIES)
+ @rm -f test_mesh_small_unicast$(EXEEXT)
+ $(AM_V_CCLD)$(LINK) $(test_mesh_small_unicast_OBJECTS) $(test_mesh_small_unicast_LDADD) $(LIBS)
+test_mesh_tree_api$(EXEEXT): $(test_mesh_tree_api_OBJECTS) $(test_mesh_tree_api_DEPENDENCIES)
+ @rm -f test_mesh_tree_api$(EXEEXT)
+ $(AM_V_CCLD)$(LINK) $(test_mesh_tree_api_OBJECTS) $(test_mesh_tree_api_LDADD) $(LIBS)
+
+mostlyclean-compile:
+ -rm -f *.$(OBJEXT)
+
+distclean-compile:
+ -rm -f *.tab.c
+
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gnunet-service-mesh.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mesh_api.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mesh_tunnel_tree.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_mesh_2dtorus.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_mesh_api.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_mesh_local_1.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_mesh_local_2.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_mesh_small.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_mesh_tree_api.Po@am__quote@
+
+.c.o:
+@am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
+@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po
+@am__fastdepCC_FALSE@ $(AM_V_CC) @AM_BACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@ $(COMPILE) -c $<
+
+.c.obj:
+@am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'`
+@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po
+@am__fastdepCC_FALSE@ $(AM_V_CC) @AM_BACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@ $(COMPILE) -c `$(CYGPATH_W) '$<'`
+
+.c.lo:
+@am__fastdepCC_TRUE@ $(AM_V_CC)$(LTCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
+@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Plo
+@am__fastdepCC_FALSE@ $(AM_V_CC) @AM_BACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@ $(LTCOMPILE) -c -o $@ $<
+
+mostlyclean-libtool:
+ -rm -f *.lo
+
+clean-libtool:
+ -rm -rf .libs _libs
+install-pkgcfgDATA: $(pkgcfg_DATA)
+ @$(NORMAL_INSTALL)
+ test -z "$(pkgcfgdir)" || $(MKDIR_P) "$(DESTDIR)$(pkgcfgdir)"
+ @list='$(pkgcfg_DATA)'; test -n "$(pkgcfgdir)" || list=; \
+ for p in $$list; do \
+ if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \
+ echo "$$d$$p"; \
+ done | $(am__base_list) | \
+ while read files; do \
+ echo " $(INSTALL_DATA) $$files '$(DESTDIR)$(pkgcfgdir)'"; \
+ $(INSTALL_DATA) $$files "$(DESTDIR)$(pkgcfgdir)" || exit $$?; \
+ done
+
+uninstall-pkgcfgDATA:
+ @$(NORMAL_UNINSTALL)
+ @list='$(pkgcfg_DATA)'; test -n "$(pkgcfgdir)" || list=; \
+ files=`for p in $$list; do echo $$p; done | sed -e 's|^.*/||'`; \
+ test -n "$$files" || exit 0; \
+ echo " ( cd '$(DESTDIR)$(pkgcfgdir)' && rm -f" $$files ")"; \
+ cd "$(DESTDIR)$(pkgcfgdir)" && rm -f $$files
+
+ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES)
+ list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \
+ unique=`for i in $$list; do \
+ if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \
+ done | \
+ $(AWK) '{ files[$$0] = 1; nonempty = 1; } \
+ END { if (nonempty) { for (i in files) print i; }; }'`; \
+ mkid -fID $$unique
+tags: TAGS
+
+TAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \
+ $(TAGS_FILES) $(LISP)
+ set x; \
+ here=`pwd`; \
+ list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \
+ unique=`for i in $$list; do \
+ if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \
+ done | \
+ $(AWK) '{ files[$$0] = 1; nonempty = 1; } \
+ END { if (nonempty) { for (i in files) print i; }; }'`; \
+ shift; \
+ if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \
+ test -n "$$unique" || unique=$$empty_fix; \
+ if test $$# -gt 0; then \
+ $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \
+ "$$@" $$unique; \
+ else \
+ $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \
+ $$unique; \
+ fi; \
+ fi
+ctags: CTAGS
+CTAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \
+ $(TAGS_FILES) $(LISP)
+ list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \
+ unique=`for i in $$list; do \
+ if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \
+ done | \
+ $(AWK) '{ files[$$0] = 1; nonempty = 1; } \
+ END { if (nonempty) { for (i in files) print i; }; }'`; \
+ test -z "$(CTAGS_ARGS)$$unique" \
+ || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \
+ $$unique
+
+GTAGS:
+ here=`$(am__cd) $(top_builddir) && pwd` \
+ && $(am__cd) $(top_srcdir) \
+ && gtags -i $(GTAGS_ARGS) "$$here"
+
+distclean-tags:
+ -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags
+
+check-TESTS: $(TESTS)
+ @failed=0; all=0; xfail=0; xpass=0; skip=0; \
+ srcdir=$(srcdir); export srcdir; \
+ list=' $(TESTS) '; \
+ $(am__tty_colors); \
+ if test -n "$$list"; then \
+ for tst in $$list; do \
+ if test -f ./$$tst; then dir=./; \
+ elif test -f $$tst; then dir=; \
+ else dir="$(srcdir)/"; fi; \
+ if $(TESTS_ENVIRONMENT) $${dir}$$tst; then \
+ all=`expr $$all + 1`; \
+ case " $(XFAIL_TESTS) " in \
+ *[\ \ ]$$tst[\ \ ]*) \
+ xpass=`expr $$xpass + 1`; \
+ failed=`expr $$failed + 1`; \
+ col=$$red; res=XPASS; \
+ ;; \
+ *) \
+ col=$$grn; res=PASS; \
+ ;; \
+ esac; \
+ elif test $$? -ne 77; then \
+ all=`expr $$all + 1`; \
+ case " $(XFAIL_TESTS) " in \
+ *[\ \ ]$$tst[\ \ ]*) \
+ xfail=`expr $$xfail + 1`; \
+ col=$$lgn; res=XFAIL; \
+ ;; \
+ *) \
+ failed=`expr $$failed + 1`; \
+ col=$$red; res=FAIL; \
+ ;; \
+ esac; \
+ else \
+ skip=`expr $$skip + 1`; \
+ col=$$blu; res=SKIP; \
+ fi; \
+ echo "$${col}$$res$${std}: $$tst"; \
+ done; \
+ if test "$$all" -eq 1; then \
+ tests="test"; \
+ All=""; \
+ else \
+ tests="tests"; \
+ All="All "; \
+ fi; \
+ if test "$$failed" -eq 0; then \
+ if test "$$xfail" -eq 0; then \
+ banner="$$All$$all $$tests passed"; \
+ else \
+ if test "$$xfail" -eq 1; then failures=failure; else failures=failures; fi; \
+ banner="$$All$$all $$tests behaved as expected ($$xfail expected $$failures)"; \
+ fi; \
+ else \
+ if test "$$xpass" -eq 0; then \
+ banner="$$failed of $$all $$tests failed"; \
+ else \
+ if test "$$xpass" -eq 1; then passes=pass; else passes=passes; fi; \
+ banner="$$failed of $$all $$tests did not behave as expected ($$xpass unexpected $$passes)"; \
+ fi; \
+ fi; \
+ dashes="$$banner"; \
+ skipped=""; \
+ if test "$$skip" -ne 0; then \
+ if test "$$skip" -eq 1; then \
+ skipped="($$skip test was not run)"; \
+ else \
+ skipped="($$skip tests were not run)"; \
+ fi; \
+ test `echo "$$skipped" | wc -c` -le `echo "$$banner" | wc -c` || \
+ dashes="$$skipped"; \
+ fi; \
+ report=""; \
+ if test "$$failed" -ne 0 && test -n "$(PACKAGE_BUGREPORT)"; then \
+ report="Please report to $(PACKAGE_BUGREPORT)"; \
+ test `echo "$$report" | wc -c` -le `echo "$$banner" | wc -c` || \
+ dashes="$$report"; \
+ fi; \
+ dashes=`echo "$$dashes" | sed s/./=/g`; \
+ if test "$$failed" -eq 0; then \
+ echo "$$grn$$dashes"; \
+ else \
+ echo "$$red$$dashes"; \
+ fi; \
+ echo "$$banner"; \
+ test -z "$$skipped" || echo "$$skipped"; \
+ test -z "$$report" || echo "$$report"; \
+ echo "$$dashes$$std"; \
+ test "$$failed" -eq 0; \
+ else :; fi
+
+distdir: $(DISTFILES)
+ @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \
+ topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \
+ list='$(DISTFILES)'; \
+ dist_files=`for file in $$list; do echo $$file; done | \
+ sed -e "s|^$$srcdirstrip/||;t" \
+ -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \
+ case $$dist_files in \
+ */*) $(MKDIR_P) `echo "$$dist_files" | \
+ sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \
+ sort -u` ;; \
+ esac; \
+ for file in $$dist_files; do \
+ if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \
+ if test -d $$d/$$file; then \
+ dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \
+ if test -d "$(distdir)/$$file"; then \
+ find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \
+ fi; \
+ if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \
+ cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \
+ find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \
+ fi; \
+ cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \
+ else \
+ test -f "$(distdir)/$$file" \
+ || cp -p $$d/$$file "$(distdir)/$$file" \
+ || exit 1; \
+ fi; \
+ done
+check-am: all-am
+ $(MAKE) $(AM_MAKEFLAGS) $(check_PROGRAMS)
+ $(MAKE) $(AM_MAKEFLAGS) check-TESTS
+check: check-am
+all-am: Makefile $(LTLIBRARIES) $(PROGRAMS) $(DATA)
+install-binPROGRAMS: install-libLTLIBRARIES
+
+installdirs:
+ for dir in "$(DESTDIR)$(libdir)" "$(DESTDIR)$(bindir)" "$(DESTDIR)$(pkgcfgdir)"; do \
+ test -z "$$dir" || $(MKDIR_P) "$$dir"; \
+ done
+install: install-am
+install-exec: install-exec-am
+install-data: install-data-am
+uninstall: uninstall-am
+
+install-am: all-am
+ @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am
+
+installcheck: installcheck-am
+install-strip:
+ $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \
+ install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \
+ `test -z '$(STRIP)' || \
+ echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install
+mostlyclean-generic:
+
+clean-generic:
+
+distclean-generic:
+ -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES)
+ -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES)
+
+maintainer-clean-generic:
+ @echo "This command is intended for maintainers to use"
+ @echo "it deletes files that may require special tools to rebuild."
+clean: clean-am
+
+clean-am: clean-binPROGRAMS clean-checkPROGRAMS clean-generic \
+ clean-libLTLIBRARIES clean-libtool mostlyclean-am
+
+distclean: distclean-am
+ -rm -rf ./$(DEPDIR)
+ -rm -f Makefile
+distclean-am: clean-am distclean-compile distclean-generic \
+ distclean-tags
+
+dvi: dvi-am
+
+dvi-am:
+
+html: html-am
+
+html-am:
+
+info: info-am
+
+info-am:
+
+install-data-am: install-pkgcfgDATA
+
+install-dvi: install-dvi-am
+
+install-dvi-am:
+
+install-exec-am: install-binPROGRAMS install-libLTLIBRARIES
+
+install-html: install-html-am
+
+install-html-am:
+
+install-info: install-info-am
+
+install-info-am:
+
+install-man:
+
+install-pdf: install-pdf-am
+
+install-pdf-am:
+
+install-ps: install-ps-am
+
+install-ps-am:
+
+installcheck-am:
+
+maintainer-clean: maintainer-clean-am
+ -rm -rf ./$(DEPDIR)
+ -rm -f Makefile
+maintainer-clean-am: distclean-am maintainer-clean-generic
+
+mostlyclean: mostlyclean-am
+
+mostlyclean-am: mostlyclean-compile mostlyclean-generic \
+ mostlyclean-libtool
+
+pdf: pdf-am
+
+pdf-am:
+
+ps: ps-am
+
+ps-am:
+
+uninstall-am: uninstall-binPROGRAMS uninstall-libLTLIBRARIES \
+ uninstall-pkgcfgDATA
+
+.MAKE: check-am install-am install-strip
+
+.PHONY: CTAGS GTAGS all all-am check check-TESTS check-am clean \
+ clean-binPROGRAMS clean-checkPROGRAMS clean-generic \
+ clean-libLTLIBRARIES clean-libtool ctags distclean \
+ distclean-compile distclean-generic distclean-libtool \
+ distclean-tags distdir dvi dvi-am html html-am info info-am \
+ install install-am install-binPROGRAMS install-data \
+ install-data-am install-dvi install-dvi-am install-exec \
+ install-exec-am install-html install-html-am install-info \
+ install-info-am install-libLTLIBRARIES install-man install-pdf \
+ install-pdf-am install-pkgcfgDATA install-ps install-ps-am \
+ install-strip installcheck installcheck-am installdirs \
+ maintainer-clean maintainer-clean-generic mostlyclean \
+ mostlyclean-compile mostlyclean-generic mostlyclean-libtool \
+ pdf pdf-am ps ps-am tags uninstall uninstall-am \
+ uninstall-binPROGRAMS uninstall-libLTLIBRARIES \
+ uninstall-pkgcfgDATA
+
+
+# Tell versions [3.59,3.63) of GNU make to not export all variables.
+# Otherwise a system limit (for SysV at least) may be exceeded.
+.NOEXPORT:
diff --git a/src/mesh/gnunet-service-mesh.c b/src/mesh/gnunet-service-mesh.c
new file mode 100644
index 0000000..80848f2
--- /dev/null
+++ b/src/mesh/gnunet-service-mesh.c
@@ -0,0 +1,4877 @@
+/*
+ This file is part of GNUnet.
+ (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 3, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file mesh/gnunet-service-mesh.c
+ * @brief GNUnet MESH service
+ * @author Bartlomiej Polot
+ *
+ * STRUCTURE:
+ * - DATA STRUCTURES
+ * - GLOBAL VARIABLES
+ * - GENERAL HELPERS
+ * - PERIODIC FUNCTIONS
+ * - MESH NETWORK HANDLER HELPERS
+ * - MESH NETWORK HANDLES
+ * - MESH LOCAL HANDLER HELPERS
+ * - MESH LOCAL HANDLES
+ * - MAIN FUNCTIONS (main & run)
+ *
+ * TODO:
+ * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
+ * - partial disconnect reporting -- same as error reporting?
+ * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
+ * - speed requirement specification (change?) in mesh API -- API call
+ * - add ping message
+ * - relay corking down to core
+ * - set ttl relative to tree depth
+ * TODO END
+ */
+
+#include "platform.h"
+#include "mesh.h"
+#include "mesh_protocol.h"
+#include "gnunet_dht_service.h"
+#include "mesh_tunnel_tree.h"
+
+/* TODO: move into configuration file */
+#define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
+ GNUNET_TIME_UNIT_SECONDS,\
+ 300)
+#define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
+ GNUNET_TIME_UNIT_SECONDS,\
+ 5)
+
+#define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
+ GNUNET_TIME_UNIT_SECONDS,\
+ 5)
+
+#define UNACKNOWLEDGED_WAIT GNUNET_TIME_relative_multiply(\
+ GNUNET_TIME_UNIT_SECONDS,\
+ 2)
+#define DEFAULT_TTL 64
+
+/* TODO END */
+
+#define MESH_DEBUG_DHT GNUNET_YES
+#define MESH_DEBUG_CONNECTION GNUNET_NO
+
+#if MESH_DEBUG_CONNECTION
+#define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
+#else
+#define DEBUG_CONN(...)
+#endif
+
+#if MESH_DEBUG_DHT
+#define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
+#else
+#define DEBUG_DHT(...)
+#endif
+
+/******************************************************************************/
+/************************ DATA STRUCTURES ****************************/
+/******************************************************************************/
+
+/** FWD declaration */
+struct MeshPeerInfo;
+
+
+/**
+ * Struct representing a piece of data being sent to other peers
+ */
+struct MeshData
+{
+ /** Tunnel it belongs to. */
+ struct MeshTunnel *t;
+
+ /** In case of a multicast, task to allow a client to send more data if
+ * some neighbor is too slow. */
+ GNUNET_SCHEDULER_TaskIdentifier *task;
+
+ /** How many remaining neighbors we need to send this to. */
+ unsigned int *reference_counter;
+
+ /** Size of the data. */
+ size_t data_len;
+
+ /** Data itself */
+ void *data;
+};
+
+
+/**
+ * Struct containing all info possibly needed to build a package when called
+ * back by core.
+ */
+struct MeshTransmissionDescriptor
+{
+ /** ID of the tunnel this packet travels in */
+ struct MESH_TunnelID *origin;
+
+ /** Who was this message being sent to */
+ struct MeshPeerInfo *peer;
+
+ /** Ultimate destination of the packet */
+ GNUNET_PEER_Id destination;
+
+ /** Which handler was used to request the transmission */
+ unsigned int handler_n;
+
+ /** Data descriptor */
+ struct MeshData* mesh_data;
+};
+
+
+/**
+ * Struct containing all information regarding a given peer
+ */
+struct MeshPeerInfo
+{
+ /**
+ * ID of the peer
+ */
+ GNUNET_PEER_Id id;
+
+ /**
+ * Last time we heard from this peer
+ */
+ struct GNUNET_TIME_Absolute last_contact;
+
+ /**
+ * Number of attempts to reconnect so far
+ */
+ int n_reconnect_attempts;
+
+ /**
+ * Paths to reach the peer, ordered by ascending hop count
+ */
+ struct MeshPeerPath *path_head;
+
+ /**
+ * Paths to reach the peer, ordered by ascending hop count
+ */
+ struct MeshPeerPath *path_tail;
+
+ /**
+ * Handle to stop the DHT search for a path to this peer
+ */
+ struct GNUNET_DHT_GetHandle *dhtget;
+
+ /**
+ * Closure given to the DHT GET
+ */
+ struct MeshPathInfo *dhtgetcls;
+
+ /**
+ * Handles to stop queued transmissions for this peer
+ */
+ struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
+
+ /**
+ * Pointer to info stuctures used as cls for queued transmissions
+ */
+ void *infos[CORE_QUEUE_SIZE];
+
+ /**
+ * Type of message being in each transmission
+ */
+ uint16_t types[CORE_QUEUE_SIZE];
+
+ /**
+ * Array of tunnels this peer participates in
+ * (most probably a small amount, therefore not a hashmap)
+ * When the path to the peer changes, notify these tunnels to let them
+ * re-adjust their path trees.
+ */
+ struct MeshTunnel **tunnels;
+
+ /**
+ * Number of tunnels this peers participates in
+ */
+ unsigned int ntunnels;
+};
+
+
+/**
+ * Data scheduled to transmit (to local client or remote peer)
+ */
+struct MeshQueue
+{
+ /**
+ * Double linked list
+ */
+ struct MeshQueue *next;
+ struct MeshQueue *prev;
+
+ /**
+ * Target of the data (NULL if target is client)
+ */
+ struct MeshPeerInfo *peer;
+
+ /**
+ * Client to send the data to (NULL if target is peer)
+ */
+ struct MeshClient *client;
+
+ /**
+ * Size of the message to transmit
+ */
+ unsigned int size;
+
+ /**
+ * How old is the data?
+ */
+ struct GNUNET_TIME_Absolute timestamp;
+
+ /**
+ * Data itself
+ */
+ struct GNUNET_MessageHeader *data;
+};
+
+/**
+ * Globally unique tunnel identification (owner + number)
+ * DO NOT USE OVER THE NETWORK
+ */
+struct MESH_TunnelID
+{
+ /**
+ * Node that owns the tunnel
+ */
+ GNUNET_PEER_Id oid;
+
+ /**
+ * Tunnel number to differentiate all the tunnels owned by the node oid
+ * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
+ */
+ MESH_TunnelNumber tid;
+};
+
+
+struct MeshClient; /* FWD declaration */
+
+/**
+ * Struct containing all information regarding a tunnel
+ * For an intermediate node the improtant info used will be:
+ * - id Tunnel unique identification
+ * - paths[0] To know where to send it next
+ * - metainfo: ready, speeds, accounting
+ */
+struct MeshTunnel
+{
+ /**
+ * Tunnel ID
+ */
+ struct MESH_TunnelID id;
+
+ /**
+ * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
+ */
+ MESH_TunnelNumber local_tid;
+
+ /**
+ * Local tunnel number for local destination clients (incoming number)
+ * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
+ * number.
+ */
+ MESH_TunnelNumber local_tid_dest;
+
+ /**
+ * ID of the last multicast packet seen/sent.
+ */
+ uint32_t mid;
+
+ /**
+ * Last time the tunnel was used
+ */
+ struct GNUNET_TIME_Absolute timestamp;
+
+ /**
+ * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
+ * containing peers added by id or by type, not intermediate peers.
+ */
+ struct GNUNET_CONTAINER_MultiHashMap *peers;
+
+ /**
+ * Number of peers that are connected and potentially ready to receive data
+ */
+ unsigned int peers_ready;
+
+ /**
+ * Number of peers that have been added to the tunnel
+ */
+ unsigned int peers_total;
+
+ /**
+ * Client owner of the tunnel, if any
+ */
+ struct MeshClient *owner;
+
+ /**
+ * Clients that have been informed about the tunnel, if any
+ */
+ struct MeshClient **clients;
+
+ /**
+ * Number of elements in clients
+ */
+ unsigned int nclients;
+
+ /**
+ * Clients that have requested to leave the tunnel
+ */
+ struct MeshClient **ignore;
+
+ /**
+ * Number of elements in clients
+ */
+ unsigned int nignore;
+
+ /**
+ * Messages ready to transmit
+ */
+ struct MeshQueue *queue_head;
+ struct MeshQueue *queue_tail;
+
+ /**
+ * Tunnel paths
+ */
+ struct MeshTunnelTree *tree;
+
+ /**
+ * Application type we are looking for in this tunnel
+ */
+ GNUNET_MESH_ApplicationType type;
+
+ /**
+ * Used to search peers offering a service
+ */
+ struct GNUNET_DHT_GetHandle *dht_get_type;
+
+ /**
+ * Task to keep the used paths alive
+ */
+ GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
+
+ /**
+ * Task to destroy the tunnel after timeout
+ *
+ * FIXME: merge the two? a tunnel will have either
+ * a path refresh OR a timeout, never both!
+ */
+ GNUNET_SCHEDULER_TaskIdentifier timeout_task;
+};
+
+
+/**
+ * Info needed to work with tunnel paths and peers
+ */
+struct MeshPathInfo
+{
+ /**
+ * Tunnel
+ */
+ struct MeshTunnel *t;
+
+ /**
+ * Neighbouring peer to whom we send the packet to
+ */
+ struct MeshPeerInfo *peer;
+
+ /**
+ * Path itself
+ */
+ struct MeshPeerPath *path;
+
+ /**
+ * Position in peer's transmit queue
+ */
+ unsigned int pos;
+};
+
+
+/**
+ * Struct containing information about a client of the service
+ */
+struct MeshClient
+{
+ /**
+ * Linked list next
+ */
+ struct MeshClient *next;
+
+ /**
+ * Linked list prev
+ */
+ struct MeshClient *prev;
+
+ /**
+ * Tunnels that belong to this client, indexed by local id
+ */
+ struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
+
+ /**
+ * Tunnels this client has accepted, indexed by incoming local id
+ */
+ struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
+
+ /**
+ * Tunnels this client has rejected, indexed by incoming local id
+ */
+ struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
+ /**
+ * Handle to communicate with the client
+ */
+ struct GNUNET_SERVER_Client *handle;
+
+ /**
+ * Applications that this client has claimed to provide
+ */
+ struct GNUNET_CONTAINER_MultiHashMap *apps;
+
+ /**
+ * Messages that this client has declared interest in
+ */
+ struct GNUNET_CONTAINER_MultiHashMap *types;
+
+ /**
+ * Whether the client is active or shutting down (don't send confirmations
+ * to a client that is shutting down.
+ */
+ int shutting_down;
+
+ /**
+ * ID of the client, mainly for debug messages
+ */
+ unsigned int id;
+
+};
+
+
+
+/******************************************************************************/
+/************************ DEBUG FUNCTIONS ****************************/
+/******************************************************************************/
+
+#if MESH_DEBUG
+/**
+ * GNUNET_SCHEDULER_Task for printing a message after some operation is done
+ * @param cls string to print
+ * @param tc task context
+ */
+static void
+mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ char *s = cls;
+
+ if (NULL != tc && GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
+ {
+ return;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s\n", s);
+}
+#endif
+
+/******************************************************************************/
+/*********************** GLOBAL VARIABLES ****************************/
+/******************************************************************************/
+
+/**
+ * All the clients
+ */
+static struct MeshClient *clients;
+static struct MeshClient *clients_tail;
+
+/**
+ * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
+ */
+static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
+
+/**
+ * Tunnels incoming, indexed by MESH_TunnelNumber
+ * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
+ */
+static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
+
+/**
+ * Peers known, indexed by PeerIdentity (MeshPeerInfo)
+ */
+static struct GNUNET_CONTAINER_MultiHashMap *peers;
+
+/**
+ * Handle to communicate with core
+ */
+static struct GNUNET_CORE_Handle *core_handle;
+
+/**
+ * Handle to communicate with transport
+ */
+// static struct GNUNET_TRANSPORT_Handle *transport_handle;
+
+/**
+ * Handle to use DHT
+ */
+static struct GNUNET_DHT_Handle *dht_handle;
+
+/**
+ * Handle to server
+ */
+static struct GNUNET_SERVER_Handle *server_handle;
+
+/**
+ * Notification context, to send messages to local clients
+ */
+static struct GNUNET_SERVER_NotificationContext *nc;
+
+/**
+ * Local peer own ID (memory efficient handle)
+ */
+static GNUNET_PEER_Id myid;
+
+/**
+ * Local peer own ID (full value)
+ */
+static struct GNUNET_PeerIdentity my_full_id;
+
+/**
+ * Own private key
+ */
+static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
+
+/**
+ * Own public key.
+ */
+static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
+
+/**
+ * Tunnel ID for the next created tunnel (global tunnel number)
+ */
+static MESH_TunnelNumber next_tid;
+
+/**
+ * Tunnel ID for the next incoming tunnel (local tunnel number)
+ */
+static MESH_TunnelNumber next_local_tid;
+
+/**
+ * All application types provided by this peer
+ */
+static struct GNUNET_CONTAINER_MultiHashMap *applications;
+
+/**
+ * All message types clients of this peer are interested in
+ */
+static struct GNUNET_CONTAINER_MultiHashMap *types;
+
+/**
+ * Task to periodically announce provided applications
+ */
+GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
+
+/**
+ * Task to periodically announce itself in the network
+ */
+GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
+
+/**
+ * Next ID to assign to a client
+ */
+unsigned int next_client_id;
+
+
+
+/******************************************************************************/
+/************************ ITERATORS ****************************/
+/******************************************************************************/
+
+/* FIXME move iterators here */
+
+
+/******************************************************************************/
+/************************ PERIODIC FUNCTIONS ****************************/
+/******************************************************************************/
+
+/**
+ * Announce iterator over for each application provided by the peer
+ *
+ * @param cls closure
+ * @param key current key code
+ * @param value value in the hash map
+ * @return GNUNET_YES if we should continue to
+ * iterate,
+ * GNUNET_NO if not.
+ */
+static int
+announce_application (void *cls, const GNUNET_HashCode * key, void *value)
+{
+ /* FIXME are hashes in multihash map equal on all aquitectures? */
+ GNUNET_DHT_put (dht_handle, key, 10U,
+ GNUNET_DHT_RO_RECORD_ROUTE |
+ GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_BLOCK_TYPE_TEST,
+ sizeof (struct GNUNET_PeerIdentity),
+ (const char *) &my_full_id,
+#if MESH_DEBUG
+ GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
+ &mesh_debug, "DHT_put for app completed");
+#else
+ GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
+ APP_ANNOUNCE_TIME),
+ APP_ANNOUNCE_TIME, NULL, NULL);
+#endif
+ return GNUNET_OK;
+}
+
+
+/**
+ * Periodically announce what applications are provided by local clients
+ *
+ * @param cls closure
+ * @param tc task context
+ */
+static void
+announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
+ {
+ announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
+ return;
+ }
+
+ DEBUG_DHT ("Starting PUT for apps\n");
+
+ GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
+ NULL);
+ announce_applications_task =
+ GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
+ cls);
+ DEBUG_DHT ("Finished PUT for apps\n");
+
+ return;
+}
+
+
+/**
+ * Periodically announce self id in the DHT
+ *
+ * @param cls closure
+ * @param tc task context
+ */
+static void
+announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
+ {
+ announce_id_task = GNUNET_SCHEDULER_NO_TASK;
+ return;
+ }
+ /* TODO
+ * - Set data expiration in function of X
+ * - Adapt X to churn
+ */
+ DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
+
+ GNUNET_DHT_put (dht_handle, /* DHT handle */
+ &my_full_id.hashPubKey, /* Key to use */
+ 10U, /* Replication level */
+ GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
+ GNUNET_BLOCK_TYPE_TEST, /* Block type */
+ sizeof (my_full_id), /* Size of the data */
+ (char *) &my_full_id, /* Data itself */
+ GNUNET_TIME_absolute_get_forever (), /* Data expiration */
+ GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
+#if MESH_DEBUG_DHT
+ &mesh_debug, "DHT_put for id completed");
+#else
+ NULL, /* Continuation */
+ NULL); /* Continuation closure */
+#endif
+ announce_id_task =
+ GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
+}
+
+
+/**
+ * Function to process paths received for a new peer addition. The recorded
+ * paths form the initial tunnel, which can be optimized later.
+ * Called on each result obtained for the DHT search.
+ *
+ * @param cls closure
+ * @param exp when will this value expire
+ * @param key key of the result
+ * @param type type of the result
+ * @param size number of bytes in data
+ * @param data pointer to the result data
+ */
+static void
+dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
+ const GNUNET_HashCode * key,
+ const struct GNUNET_PeerIdentity *get_path,
+ unsigned int get_path_length,
+ const struct GNUNET_PeerIdentity *put_path,
+ unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
+ size_t size, const void *data);
+
+
+/******************************************************************************/
+/****************** GENERAL HELPER FUNCTIONS ************************/
+/******************************************************************************/
+
+/**
+ * Search for a tunnel by global ID using full PeerIdentities
+ *
+ * @param oid owner of the tunnel
+ * @param tid global tunnel number
+ *
+ * @return tunnel handler, NULL if doesn't exist
+ */
+static struct MeshTunnel *
+tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
+
+
+/**
+ * Delete an active client from the tunnel.
+ *
+ * @param t Tunnel.
+ * @param c Client.
+ */
+static void
+tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
+
+/**
+ * Notify a tunnel that a connection has broken that affects at least
+ * some of its peers.
+ *
+ * @param t Tunnel affected.
+ * @param p1 Peer that got disconnected from p2.
+ * @param p2 Peer that got disconnected from p1.
+ *
+ * @return Short ID of the peer disconnected (either p1 or p2).
+ * 0 if the tunnel remained unaffected.
+ */
+static GNUNET_PEER_Id
+tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
+ GNUNET_PEER_Id p2);
+
+
+/**
+ * Check if client has registered with the service and has not disconnected
+ *
+ * @param client the client to check
+ *
+ * @return non-NULL if client exists in the global DLL
+ */
+static struct MeshClient *
+client_get (struct GNUNET_SERVER_Client *client)
+{
+ struct MeshClient *c;
+
+ c = clients;
+ while (NULL != c)
+ {
+ if (c->handle == client)
+ return c;
+ c = c->next;
+ }
+ return NULL;
+}
+
+
+/**
+ * Checks if a given client has subscribed to certain message type
+ *
+ * @param message_type Type of message to check
+ * @param c Client to check
+ *
+ * @return GNUNET_YES or GNUNET_NO, depending on subscription status
+ *
+ * TODO inline?
+ */
+static int
+client_is_subscribed (uint16_t message_type, struct MeshClient *c)
+{
+ GNUNET_HashCode hc;
+
+ GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
+ return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
+}
+
+
+/**
+ * Allow a client to send more data after transmitting a multicast message
+ * which some neighbor has not yet accepted altough a reasonable time has
+ * passed.
+ *
+ * @param cls Closure (DataDescriptor containing the task identifier)
+ * @param tc Task Context
+ *
+ * FIXME reference counter cshould be just int
+ */
+static void
+client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct MeshData *mdata = cls;
+
+ if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
+ return;
+ GNUNET_assert (NULL != mdata->reference_counter);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
+ *(mdata->reference_counter));
+ *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
+ GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
+}
+
+
+/**
+ * Check whether client wants traffic from a tunnel.
+ *
+ * @param c Client to check.
+ * @param t Tunnel to be found.
+ *
+ * @return GNUNET_YES if client knows tunnel.
+ *
+ * TODO look in client hashmap
+ */
+static int
+client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
+{
+ unsigned int i;
+
+ for (i = 0; i < t->nclients; i++)
+ if (t->clients[i] == c)
+ return GNUNET_YES;
+ return GNUNET_NO;
+}
+
+
+/**
+ * Check whether client has been informed about a tunnel.
+ *
+ * @param c Client to check.
+ * @param t Tunnel to be found.
+ *
+ * @return GNUNET_YES if client knows tunnel.
+ *
+ * TODO look in client hashmap
+ */
+static int
+client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
+{
+ unsigned int i;
+
+ for (i = 0; i < t->nignore; i++)
+ if (t->ignore[i] == c)
+ return GNUNET_YES;
+ return client_wants_tunnel(c, t);
+}
+
+
+/**
+ * Marks a client as uninterested in traffic from the tunnel, updating both
+ * client and tunnel to reflect this.
+ *
+ * @param c Client that doesn't want traffic anymore.
+ * @param t Tunnel which should be ignored.
+ *
+ * FIXME when to delete an incoming tunnel?
+ */
+static void
+client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
+{
+ GNUNET_HashCode hash;
+
+ GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
+ GNUNET_break (GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
+ &hash, t));
+ GNUNET_break (GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
+ tunnel_delete_active_client (t, c);
+ GNUNET_array_append (t->ignore, t->nignore, c);
+}
+
+
+/**
+ * Deletes a tunnel from a client (either owner or destination). To be used on
+ * tunnel destroy, otherwise, use client_ignore_tunnel.
+ *
+ * @param c Client whose tunnel to delete.
+ * @param t Tunnel which should be deleted.
+ */
+static void
+client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
+{
+ GNUNET_HashCode hash;
+
+ if (c == t->owner)
+ {
+ GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
+ &hash,
+ t));
+ }
+ else
+ {
+ GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
+ // FIXME XOR?
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
+ &hash,
+ t) ||
+ GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
+ &hash,
+ t));
+ }
+
+}
+
+
+/**
+ * Send the message to all clients that have subscribed to its type
+ *
+ * @param msg Pointer to the message itself
+ * @param payload Pointer to the payload of the message.
+ * @return number of clients this message was sent to
+ */
+static unsigned int
+send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
+ const struct GNUNET_MessageHeader *payload)
+{
+ struct GNUNET_PeerIdentity *oid;
+ struct MeshClient *c;
+ struct MeshTunnel *t;
+ MESH_TunnelNumber *tid;
+ unsigned int count;
+ uint16_t type;
+ char cbuf[htons (msg->size)];
+
+ type = ntohs (payload->type);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
+
+ memcpy (cbuf, msg, sizeof (cbuf));
+ switch (htons (msg->type))
+ {
+ struct GNUNET_MESH_Unicast *uc;
+ struct GNUNET_MESH_Multicast *mc;
+ struct GNUNET_MESH_ToOrigin *to;
+
+ case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
+ uc = (struct GNUNET_MESH_Unicast *) cbuf;
+ tid = &uc->tid;
+ oid = &uc->oid;
+ break;
+ case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
+ mc = (struct GNUNET_MESH_Multicast *) cbuf;
+ tid = &mc->tid;
+ oid = &mc->oid;
+ break;
+ case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
+ to = (struct GNUNET_MESH_ToOrigin *) cbuf;
+ tid = &to->tid;
+ oid = &to->oid;
+ break;
+ default:
+ GNUNET_break (0);
+ return 0;
+ }
+ t = tunnel_get (oid, ntohl (*tid));
+ if (NULL == t)
+ {
+ GNUNET_break (0);
+ return 0;
+ }
+
+ for (count = 0, c = clients; c != NULL; c = c->next)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
+ if (client_is_subscribed (type, c))
+ {
+ if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
+ {
+ if (c != t->owner)
+ continue;
+ *tid = htonl (t->local_tid);
+ }
+ else
+ {
+ if (GNUNET_NO == client_knows_tunnel (c, t))
+ {
+ /* This client doesn't know the tunnel */
+ struct GNUNET_MESH_TunnelNotification tmsg;
+ GNUNET_HashCode hash;
+
+ tmsg.header.size = htons (sizeof (tmsg));
+ tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
+ GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
+ tmsg.tunnel_id = htonl (t->local_tid_dest);
+ GNUNET_SERVER_notification_context_unicast (nc, c->handle,
+ &tmsg.header, GNUNET_NO);
+ GNUNET_array_append (t->clients, t->nclients, c);
+ GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
+ &hash);
+ GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
+ c->incoming_tunnels, &hash, t,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
+ }
+ *tid = htonl (t->local_tid_dest);
+ }
+
+ /* Check if the client wants to get traffic from the tunnel */
+ if (GNUNET_NO == client_wants_tunnel(c, t))
+ continue;
+ count++;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
+ GNUNET_SERVER_notification_context_unicast (nc, c->handle,
+ (struct GNUNET_MessageHeader
+ *) cbuf, GNUNET_YES);
+ }
+ }
+ return count;
+}
+
+
+/**
+ * Notify the client that owns the tunnel that a peer has connected to it
+ * (the requested path to it has been confirmed).
+ *
+ * @param t Tunnel whose owner to notify
+ * @param id Short id of the peer that has connected
+ */
+static void
+send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
+{
+ struct GNUNET_MESH_PeerControl pc;
+
+ pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
+ pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
+ pc.tunnel_id = htonl (t->local_tid);
+ GNUNET_PEER_resolve (id, &pc.peer);
+ GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
+ GNUNET_NO);
+}
+
+
+/**
+ * Notify all clients (not depending on registration status) that the incoming
+ * tunnel is no longer valid.
+ *
+ * @param t Tunnel that was destroyed.
+ */
+static void
+send_clients_tunnel_destroy (struct MeshTunnel *t)
+{
+ struct GNUNET_MESH_TunnelMessage msg;
+
+ msg.header.size = htons (sizeof (msg));
+ msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
+ msg.tunnel_id = htonl (t->local_tid_dest);
+ GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
+}
+
+
+/**
+ * Notify clients of tunnel disconnections, if needed.
+ * In case the origin disconnects, the destination clients get a tunnel destroy
+ * notification. If the last destination disconnects (only one remaining client
+ * in tunnel), the origin gets a (local ID) peer disconnected.
+ * Note that the function must be called BEFORE removing the client from
+ * the tunnel.
+ *
+ * @param t Tunnel that was destroyed.
+ * @param c Client that disconnected.
+ */
+static void
+send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
+{
+ unsigned int i;
+
+ if (c == t->owner)
+ {
+ struct GNUNET_MESH_TunnelMessage msg;
+
+ msg.header.size = htons (sizeof (msg));
+ msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
+ msg.tunnel_id = htonl (t->local_tid_dest);
+ for (i = 0; i < t->nclients; i++)
+ GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
+ &msg.header, GNUNET_NO);
+ }
+ // FIXME when to disconnect an incoming tunnel?
+ else if (1 == t->nclients && NULL != t->owner)
+ {
+ struct GNUNET_MESH_PeerControl msg;
+
+ msg.header.size = htons (sizeof (msg));
+ msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
+ msg.tunnel_id = htonl (t->local_tid);
+ msg.peer = my_full_id;
+ GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
+ &msg.header, GNUNET_NO);
+ }
+}
+
+
+/**
+ * Function called to notify a client about the socket
+ * being ready to queue more data. "buf" will be
+ * NULL and "size" zero if the socket was closed for
+ * writing in the meantime.
+ *
+ * @param cls closure
+ * @param size number of bytes available in buf
+ * @param buf where the callee should write the message
+ * @return number of bytes written to buf
+ */
+static size_t
+send_core_create_path (void *cls, size_t size, void *buf);
+
+
+/**
+ * Function called to notify a client about the socket
+ * being ready to queue more data. "buf" will be
+ * NULL and "size" zero if the socket was closed for
+ * writing in the meantime.
+ *
+ * @param cls closure (data itself)
+ * @param size number of bytes available in buf
+ * @param buf where the callee should write the message
+ *
+ * @return number of bytes written to buf
+ */
+static size_t
+send_core_data_multicast (void *cls, size_t size, void *buf);
+
+
+/**
+ * Decrements the reference counter and frees all resources if needed
+ *
+ * @param mesh_data Data Descriptor used in a multicast message.
+ * Freed no longer needed (last message).
+ */
+static void
+data_descriptor_decrement_multicast (struct MeshData *mesh_data)
+{
+ /* Make sure it's a multicast packet */
+ GNUNET_assert (NULL != mesh_data->reference_counter);
+
+ if (0 == --(*(mesh_data->reference_counter)))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
+ if (NULL != mesh_data->task)
+ {
+ if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
+ {
+ GNUNET_SCHEDULER_cancel (*(mesh_data->task));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
+ GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
+ }
+ GNUNET_free (mesh_data->task);
+ }
+ GNUNET_free (mesh_data->reference_counter);
+ GNUNET_free (mesh_data->data);
+ GNUNET_free (mesh_data);
+ }
+}
+
+
+/**
+ * Cancel a core transmission that was already requested and free all resources
+ * associated to the request.
+ *
+ * @param peer PeeInfo of the peer whose transmission is cancelled.
+ * @param i Position of the transmission to be cancelled.
+ */
+static void
+peer_info_cancel_transmission (struct MeshPeerInfo *peer, unsigned int i)
+{
+ if (NULL != peer->core_transmit[i])
+ {
+ struct MeshTransmissionDescriptor *dd;
+ struct MeshPathInfo *path_info;
+
+#if MESH_DEBUG
+ {
+ struct GNUNET_PeerIdentity id;
+
+ GNUNET_PEER_resolve (peer->id, &id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " Cancelling data transmission at %s [%u]\n",
+ GNUNET_i2s (&id), i);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " message type %u\n",
+ peer->types[i]);
+ }
+#endif
+ /* TODO: notify that tranmission has failed */
+ switch (peer->types[i])
+ {
+ case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
+ case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
+ case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
+ dd = peer->infos[i];
+ data_descriptor_decrement_multicast (dd->mesh_data);
+ break;
+ case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
+ path_info = peer->infos[i];
+ path_destroy (path_info->path);
+ break;
+ default:
+ GNUNET_break (0);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
+ }
+ GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
+ peer->core_transmit[i] = NULL;
+ GNUNET_free (peer->infos[i]);
+ }
+}
+
+
+/**
+ * Get a unused CORE slot to transmit a message to a peer. If all the slots
+ * are used, cancel one and return it's position.
+ *
+ * @param peer PeerInfo of the neighbor we want to transmit to.
+ *
+ * @return The index of an available slot to transmit to the neighbor.
+ */
+static unsigned int
+peer_info_transmit_slot (struct MeshPeerInfo *peer)
+{
+ unsigned int i;
+
+ for (i = 0; peer->core_transmit[i]; i++)
+ {
+ if (i == (CORE_QUEUE_SIZE - 1))
+ {
+ /* All positions are taken! Overwriting! */
+ GNUNET_break (0);
+ peer_info_cancel_transmission (peer, 0);
+ return 0;
+ }
+ }
+ return i;
+}
+
+
+/**
+ * Retrieve the MeshPeerInfo stucture associated with the peer, create one
+ * and insert it in the appropiate structures if the peer is not known yet.
+ *
+ * @param peer Full identity of the peer.
+ *
+ * @return Existing or newly created peer info.
+ */
+static struct MeshPeerInfo *
+peer_info_get (const struct GNUNET_PeerIdentity *peer)
+{
+ struct MeshPeerInfo *peer_info;
+
+ peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
+ if (NULL == peer_info)
+ {
+ peer_info =
+ (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
+ GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
+ peer_info->id = GNUNET_PEER_intern (peer);
+ }
+
+ return peer_info;
+}
+
+
+/**
+ * Retrieve the MeshPeerInfo stucture associated with the peer, create one
+ * and insert it in the appropiate structures if the peer is not known yet.
+ *
+ * @param peer Short identity of the peer.
+ *
+ * @return Existing or newly created peer info.
+ */
+static struct MeshPeerInfo *
+peer_info_get_short (const GNUNET_PEER_Id peer)
+{
+ struct GNUNET_PeerIdentity id;
+
+ GNUNET_PEER_resolve (peer, &id);
+ return peer_info_get (&id);
+}
+
+
+/**
+ * Iterator to remove the tunnel from the list of tunnels a peer participates
+ * in.
+ *
+ * @param cls Closure (tunnel info)
+ * @param key GNUNET_PeerIdentity of the peer (unused)
+ * @param value PeerInfo of the peer
+ *
+ * @return always GNUNET_YES, to keep iterating
+ */
+static int
+peer_info_delete_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
+{
+ struct MeshTunnel *t = cls;
+ struct MeshPeerInfo *peer = value;
+ unsigned int i;
+
+ for (i = 0; i < peer->ntunnels; i++)
+ {
+ if (0 ==
+ memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
+ {
+ peer->ntunnels--;
+ peer->tunnels[i] = peer->tunnels[peer->ntunnels];
+ peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
+ return GNUNET_YES;
+ }
+ }
+ return GNUNET_YES;
+}
+
+
+/**
+ * Core callback to write a
+ *
+ * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
+ * @param size Number of bytes available in buf.
+ * @param buf Where the to write the message.
+ *
+ * @return number of bytes written to buf
+ */
+static size_t
+send_core_data_raw (void *cls, size_t size, void *buf)
+{
+ struct MeshTransmissionDescriptor *info = cls;
+ struct GNUNET_MessageHeader *msg;
+ size_t total_size;
+
+ GNUNET_assert (NULL != info);
+ GNUNET_assert (NULL != info->mesh_data);
+ msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
+ total_size = ntohs (msg->size);
+
+ if (total_size > size)
+ {
+ struct GNUNET_PeerIdentity id;
+
+ GNUNET_PEER_resolve (info->peer->id, &id);
+ info->peer->core_transmit[info->handler_n] =
+ GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
+ GNUNET_TIME_UNIT_FOREVER_REL, &id,
+ size, &send_core_data_raw, info);
+ return 0;
+ }
+ info->peer->core_transmit[info->handler_n] = NULL;
+ memcpy (buf, msg, total_size);
+ GNUNET_free (info->mesh_data);
+ GNUNET_free (info);
+ return total_size;
+}
+
+
+/**
+ * Sends an already built message to a peer, properly registrating
+ * all used resources.
+ *
+ * @param message Message to send. Fucntion makes a copy of it.
+ * @param peer Short ID of the neighbor whom to send the message.
+ *
+ * FIXME tunnel?
+ */
+static void
+send_message (const struct GNUNET_MessageHeader *message,
+ const struct GNUNET_PeerIdentity *peer)
+{
+ struct MeshTransmissionDescriptor *info;
+ struct MeshPeerInfo *neighbor;
+ struct MeshPeerPath *p;
+ unsigned int i;
+ size_t size;
+
+// GNUNET_TRANSPORT_try_connect();
+
+ size = ntohs (message->size);
+ info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
+ info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
+ info->mesh_data->data = GNUNET_malloc (size);
+ memcpy (info->mesh_data->data, message, size);
+ info->mesh_data->data_len = size;
+ neighbor = peer_info_get (peer);
+ for (p = neighbor->path_head; NULL != p; p = p->next)
+ {
+ if (2 == p->length)
+ {
+ break;
+ }
+ }
+ if (NULL == p)
+ {
+ GNUNET_break (0);
+ GNUNET_free (info->mesh_data->data);
+ GNUNET_free (info->mesh_data);
+ GNUNET_free (info);
+ return;
+ }
+ i = peer_info_transmit_slot (neighbor);
+ info->handler_n = i;
+ info->peer = neighbor;
+ neighbor->types[i] = GNUNET_MESSAGE_TYPE_MESH_UNICAST;
+ neighbor->infos[i] = info;
+ neighbor->core_transmit[i] =
+ GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
+ GNUNET_TIME_UNIT_FOREVER_REL, peer,
+ size, &send_core_data_raw, info);
+
+}
+
+
+/**
+ * Sends a CREATE PATH message for a path to a peer, properly registrating
+ * all used resources.
+ *
+ * @param peer PeerInfo of the final peer for whom this path is being created.
+ * @param p Path itself.
+ * @param t Tunnel for which the path is created.
+ */
+static void
+send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
+ struct MeshTunnel *t)
+{
+ struct GNUNET_PeerIdentity id;
+ struct MeshPathInfo *path_info;
+ struct MeshPeerInfo *neighbor;
+ unsigned int i;
+
+ if (NULL == p)
+ {
+ p = tree_get_path_to_peer (t->tree, peer->id);
+ if (NULL == p)
+ {
+ GNUNET_break (0);
+ return;
+ }
+ }
+ for (i = 0; i < p->length; i++)
+ {
+ if (p->peers[i] == myid)
+ break;
+ }
+ if (i >= p->length - 1)
+ {
+ path_destroy (p);
+ GNUNET_break (0);
+ return;
+ }
+ GNUNET_PEER_resolve (p->peers[i + 1], &id);
+
+ path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
+ path_info->path = p;
+ path_info->t = t;
+ neighbor = peer_info_get (&id);
+ path_info->peer = neighbor;
+ path_info->pos = peer_info_transmit_slot (neighbor);
+ neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
+ neighbor->infos[path_info->pos] = path_info;
+ neighbor->core_transmit[path_info->pos] =
+ GNUNET_CORE_notify_transmit_ready (core_handle, /* handle */
+ 0, /* cork */
+ 0, /* priority */
+ GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
+ &id, /* target */
+ sizeof (struct GNUNET_MESH_ManipulatePath) +
+ (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
+ &send_core_create_path, /* callback */
+ path_info); /* cls */
+}
+
+
+/**
+ * Sends a DESTROY PATH message to free resources for a path in a tunnel
+ *
+ * @param t Tunnel whose path to destroy.
+ * @param destination Short ID of the peer to whom the path to destroy.
+ */
+static void
+send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
+{
+ struct MeshPeerPath *p;
+ size_t size;
+
+ p = tree_get_path_to_peer (t->tree, destination);
+ if (NULL == p)
+ {
+ GNUNET_break (0);
+ return;
+ }
+ size = sizeof (struct GNUNET_MESH_ManipulatePath);
+ size += p->length * sizeof (struct GNUNET_PeerIdentity);
+ {
+ struct GNUNET_MESH_ManipulatePath *msg;
+ struct GNUNET_PeerIdentity *pi;
+ char cbuf[size];
+ unsigned int i;
+
+ msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
+ msg->header.size = htons (size);
+ msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
+ msg->tid = htonl (t->id.tid);
+ pi = (struct GNUNET_PeerIdentity *) &msg[1];
+ for (i = 0; i < p->length; i++)
+ {
+ GNUNET_PEER_resolve (p->peers[i], &pi[i]);
+ }
+ send_message (&msg->header, tree_get_first_hop (t->tree, destination));
+ }
+ path_destroy (p);
+}
+
+
+/**
+ * Try to establish a new connection to this peer.
+ * Use the best path for the given tunnel.
+ * If the peer doesn't have any path to it yet, try to get one.
+ * If the peer already has some path, send a CREATE PATH towards it.
+ *
+ * @param peer PeerInfo of the peer.
+ * @param t Tunnel for which to create the path, if possible.
+ */
+static void
+peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
+{
+ struct MeshPeerPath *p;
+ struct MeshPathInfo *path_info;
+
+ if (NULL != peer->path_head)
+ {
+ p = tree_get_path_to_peer (t->tree, peer->id);
+ if (NULL == p)
+ {
+ GNUNET_break (0);
+ return;
+ }
+
+ // FIXME always send create path to self
+ if (p->length > 1)
+ {
+ send_create_path (peer, p, t);
+ }
+ else
+ {
+ GNUNET_HashCode hash;
+
+ path_destroy (p);
+ send_client_peer_connected (t, myid);
+ t->local_tid_dest = next_local_tid++;
+ GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
+ &hash);
+ if (GNUNET_OK !=
+ GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
+ {
+ GNUNET_break (0);
+ return;
+ }
+ }
+ }
+ else if (NULL == peer->dhtget)
+ {
+ struct GNUNET_PeerIdentity id;
+
+ GNUNET_PEER_resolve (peer->id, &id);
+ path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
+ path_info->peer = peer;
+ path_info->t = t;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
+ peer->dhtgetcls = path_info;
+ peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
+ GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
+ GNUNET_BLOCK_TYPE_TEST, /* type */
+ &id.hashPubKey, /* key to search */
+ 10U, /* replication level */
+ GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
+ 0, /* xquery bits */
+ &dht_get_id_handler, path_info);
+ }
+ /* Otherwise, there is no path but the DHT get is already started. */
+}
+
+
+/**
+ * Task to delay the connection of a peer
+ *
+ * @param cls Closure (path info with tunnel and peer to connect).
+ * Will be free'd on exection.
+ * @param tc TaskContext
+ */
+static void
+peer_info_connect_task (void *cls,
+ const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct MeshPathInfo *path_info = cls;
+
+ if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
+ {
+ GNUNET_free (cls);
+ return;
+ }
+ peer_info_connect (path_info->peer, path_info->t);
+ GNUNET_free (cls);
+}
+
+
+/**
+ * Destroy the peer_info and free any allocated resources linked to it
+ *
+ * @param pi The peer_info to destroy.
+ *
+ * @return GNUNET_OK on success
+ */
+static int
+peer_info_destroy (struct MeshPeerInfo *pi)
+{
+ struct GNUNET_PeerIdentity id;
+ struct MeshPeerPath *p;
+ struct MeshPeerPath *nextp;
+ unsigned int i;
+
+ GNUNET_PEER_resolve (pi->id, &id);
+ GNUNET_PEER_change_rc (pi->id, -1);
+
+ if (GNUNET_YES !=
+ GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
+ {
+ GNUNET_break (0);
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
+ }
+ if (NULL != pi->dhtget)
+ {
+ GNUNET_DHT_get_stop (pi->dhtget);
+ GNUNET_free (pi->dhtgetcls);
+ }
+ for (i = 0; i < CORE_QUEUE_SIZE; i++)
+ {
+ peer_info_cancel_transmission (pi, i);
+ }
+ p = pi->path_head;
+ while (NULL != p)
+ {
+ nextp = p->next;
+ GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
+ path_destroy (p);
+ p = nextp;
+ }
+ GNUNET_free (pi);
+ return GNUNET_OK;
+}
+
+
+/**
+ * Remove all paths that rely on a direct connection between p1 and p2
+ * from the peer itself and notify all tunnels about it.
+ *
+ * @param peer PeerInfo of affected peer.
+ * @param p1 GNUNET_PEER_Id of one peer.
+ * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
+ * no longer is.
+ *
+ * TODO: optimize (see below)
+ */
+static void
+peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
+ GNUNET_PEER_Id p2)
+{
+ struct MeshPeerPath *p;
+ struct MeshPeerPath *aux;
+ struct MeshPeerInfo *peer_d;
+ GNUNET_PEER_Id d;
+ unsigned int destroyed;
+ unsigned int best;
+ unsigned int cost;
+ unsigned int i;
+
+ destroyed = 0;
+ p = peer->path_head;
+ while (NULL != p)
+ {
+ aux = p->next;
+ for (i = 0; i < (p->length - 1); i++)
+ {
+ if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
+ (p->peers[i] == p2 && p->peers[i + 1] == p1))
+ {
+ GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
+ path_destroy (p);
+ destroyed++;
+ break;
+ }
+ }
+ p = aux;
+ }
+ if (0 == destroyed)
+ return;
+
+ for (i = 0; i < peer->ntunnels; i++)
+ {
+ d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
+ if (0 == d)
+ continue;
+ /* TODO
+ * Problem: one or more peers have been deleted from the tunnel tree.
+ * We don't know who they are to try to add them again.
+ * We need to try to find a new path for each of the disconnected peers.
+ * Some of them might already have a path to reach them that does not
+ * involve p1 and p2. Adding all anew might render in a better tree than
+ * the trivial immediate fix.
+ *
+ * Trivial immiediate fix: try to reconnect to the disconnected node. All
+ * its children will be reachable trough him.
+ */
+ peer_d = peer_info_get_short (d);
+ best = UINT_MAX;
+ aux = NULL;
+ for (p = peer_d->path_head; NULL != p; p = p->next)
+ {
+ if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
+ {
+ best = cost;
+ aux = p;
+ }
+ }
+ if (NULL != aux)
+ {
+ /* No callback, as peer will be already disconnected and a connection
+ * scheduled by tunnel_notify_connection_broken.
+ */
+ tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
+ }
+ else
+ {
+ peer_info_connect (peer_d, peer->tunnels[i]);
+ }
+ }
+}
+
+
+/**
+ * Add the path to the peer and update the path used to reach it in case this
+ * is the shortest.
+ *
+ * @param peer_info Destination peer to add the path to.
+ * @param path New path to add. Last peer must be the peer in arg 1.
+ * Path will be either used of freed if already known.
+ * @param trusted Do we trust that this path is real?
+ */
+void
+peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
+ int trusted)
+{
+ struct MeshPeerPath *aux;
+ unsigned int l;
+ unsigned int l2;
+
+ if ((NULL == peer_info) || (NULL == path))
+ {
+ GNUNET_break (0);
+ path_destroy (path);
+ return;
+ }
+ if (path->peers[path->length - 1] != peer_info->id)
+ {
+ GNUNET_break (0);
+ path_destroy (path);
+ return;
+ }
+ if (path->length <= 2 && GNUNET_NO == trusted)
+ {
+ /* Only allow CORE to tell us about direct paths */
+ path_destroy (path);
+ return;
+ }
+ GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
+ for (l = 1; l < path->length; l++)
+ {
+ if (path->peers[l] == myid)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
+ for (l2 = 0; l2 < path->length - l; l2++)
+ {
+ path->peers[l2] = path->peers[l + l2];
+ }
+ path->length -= l;
+ l = 1;
+ path->peers =
+ GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
+ }
+ }
+#if MESH_DEBUG
+ {
+ struct GNUNET_PeerIdentity id;
+
+ GNUNET_PEER_resolve (peer_info->id, &id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
+ path->length, GNUNET_i2s (&id));
+ }
+#endif
+ l = path_get_length (path);
+ if (0 == l)
+ {
+ GNUNET_free (path);
+ return;
+ }
+
+ GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
+ for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
+ {
+ l2 = path_get_length (aux);
+ if (l2 > l)
+ {
+ GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
+ peer_info->path_tail, aux, path);
+ return;
+ }
+ else
+ {
+ if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
+ {
+ path_destroy (path);
+ return;
+ }
+ }
+ }
+ GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
+ path);
+ return;
+}
+
+
+/**
+ * Add the path to the origin peer and update the path used to reach it in case
+ * this is the shortest.
+ * The path is given in peer_info -> destination, therefore we turn the path
+ * upside down first.
+ *
+ * @param peer_info Peer to add the path to, being the origin of the path.
+ * @param path New path to add after being inversed.
+ * @param trusted Do we trust that this path is real?
+ */
+static void
+peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
+ struct MeshPeerPath *path, int trusted)
+{
+ path_invert (path);
+ peer_info_add_path (peer_info, path, trusted);
+}
+
+
+/**
+ * Build a PeerPath from the paths returned from the DHT, reversing the paths
+ * to obtain a local peer -> destination path and interning the peer ids.
+ *
+ * @return Newly allocated and created path
+ */
+static struct MeshPeerPath *
+path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
+ unsigned int get_path_length,
+ const struct GNUNET_PeerIdentity *put_path,
+ unsigned int put_path_length)
+{
+ struct MeshPeerPath *p;
+ GNUNET_PEER_Id id;
+ int i;
+
+ p = path_new (1);
+ p->peers[0] = myid;
+ GNUNET_PEER_change_rc (myid, 1);
+ i = get_path_length;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
+ for (i--; i >= 0; i--)
+ {
+ id = GNUNET_PEER_intern (&get_path[i]);
+ if (p->length > 0 && id == p->peers[p->length - 1])
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
+ GNUNET_PEER_change_rc (id, -1);
+ }
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
+ GNUNET_i2s (&get_path[i]));
+ p->length++;
+ p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
+ p->peers[p->length - 1] = id;
+ }
+ }
+ i = put_path_length;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
+ for (i--; i >= 0; i--)
+ {
+ id = GNUNET_PEER_intern (&put_path[i]);
+ if (id == myid)
+ {
+ /* PUT path went through us, so discard the path up until now and start
+ * from here to get a much shorter (and loop-free) path.
+ */
+ path_destroy (p);
+ p = path_new (0);
+ }
+ if (p->length > 0 && id == p->peers[p->length - 1])
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
+ GNUNET_PEER_change_rc (id, -1);
+ }
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
+ GNUNET_i2s (&put_path[i]));
+ p->length++;
+ p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
+ p->peers[p->length - 1] = id;
+ }
+ }
+#if MESH_DEBUG
+ if (get_path_length > 0)
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
+ GNUNET_i2s (&get_path[0]));
+ if (put_path_length > 0)
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
+ GNUNET_i2s (&put_path[0]));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
+ p->length);
+ for (i = 0; i < p->length; i++)
+ {
+ struct GNUNET_PeerIdentity peer_id;
+
+ GNUNET_PEER_resolve (p->peers[i], &peer_id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
+ GNUNET_i2s (&peer_id));
+ }
+#endif
+ return p;
+}
+
+
+/**
+ * Adds a path to the peer_infos of all the peers in the path
+ *
+ * @param p Path to process.
+ * @param confirmed Whether we know if the path works or not. FIXME use
+ */
+static void
+path_add_to_peers (struct MeshPeerPath *p, int confirmed)
+{
+ unsigned int i;
+
+ /* TODO: invert and add */
+ for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
+ for (i++; i < p->length; i++)
+ {
+ struct MeshPeerInfo *aux;
+ struct MeshPeerPath *copy;
+
+ aux = peer_info_get_short (p->peers[i]);
+ copy = path_duplicate (p);
+ copy->length = i + 1;
+ peer_info_add_path (aux, copy, GNUNET_NO);
+ }
+}
+
+
+/**
+ * Send keepalive packets for a peer
+ *
+ * @param cls Closure (tunnel for which to send the keepalive).
+ * @param tc Notification context.
+ *
+ * TODO: implement explicit multicast keepalive?
+ */
+static void
+path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+
+
+/**
+ * Search for a tunnel among the incoming tunnels
+ *
+ * @param tid the local id of the tunnel
+ *
+ * @return tunnel handler, NULL if doesn't exist
+ */
+static struct MeshTunnel *
+tunnel_get_incoming (MESH_TunnelNumber tid)
+{
+ GNUNET_HashCode hash;
+
+ GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
+ GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
+ return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
+}
+
+
+/**
+ * Search for a tunnel among the tunnels for a client
+ *
+ * @param c the client whose tunnels to search in
+ * @param tid the local id of the tunnel
+ *
+ * @return tunnel handler, NULL if doesn't exist
+ */
+static struct MeshTunnel *
+tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
+{
+ if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
+ {
+ return tunnel_get_incoming (tid);
+ }
+ else
+ {
+ GNUNET_HashCode hash;
+
+ GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
+ return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
+ }
+}
+
+
+/**
+ * Search for a tunnel by global ID using PEER_ID
+ *
+ * @param pi owner of the tunnel
+ * @param tid global tunnel number
+ *
+ * @return tunnel handler, NULL if doesn't exist
+ */
+static struct MeshTunnel *
+tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
+{
+ struct MESH_TunnelID id;
+ GNUNET_HashCode hash;
+
+ id.oid = pi;
+ id.tid = tid;
+
+ GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
+ return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
+}
+
+
+/**
+ * Search for a tunnel by global ID using full PeerIdentities
+ *
+ * @param oid owner of the tunnel
+ * @param tid global tunnel number
+ *
+ * @return tunnel handler, NULL if doesn't exist
+ */
+static struct MeshTunnel *
+tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
+{
+ return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
+}
+
+
+/**
+ * Delete an active client from the tunnel.
+ *
+ * @param t Tunnel.
+ * @param c Client.
+ */
+static void
+tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
+{
+ unsigned int i;
+
+ for (i = 0; i < t->nclients; i++)
+ {
+ if (t->clients[i] == c)
+ {
+ t->clients[i] = t->clients[t->nclients - 1];
+ GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
+ break;
+ }
+ }
+}
+
+
+/**
+ * Delete an ignored client from the tunnel.
+ *
+ * @param t Tunnel.
+ * @param c Client.
+ */
+static void
+tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
+{
+ unsigned int i;
+
+ for (i = 0; i < t->nignore; i++)
+ {
+ if (t->ignore[i] == c)
+ {
+ t->ignore[i] = t->ignore[t->nignore - 1];
+ GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
+ break;
+ }
+ }
+}
+
+
+/**
+ * Delete a client from the tunnel. It should be only done on
+ * client disconnection, otherwise use client_ignore_tunnel.
+ *
+ * @param t Tunnel.
+ * @param c Client.
+ */
+static void
+tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
+{
+ tunnel_delete_ignored_client (t, c);
+ tunnel_delete_active_client (t, c);
+}
+
+
+/**
+ * Callback used to notify a client owner of a tunnel that a peer has
+ * disconnected, most likely because of a path change.
+ *
+ * @param cls Closure (tunnel this notification is about).
+ * @param peer_id Short ID of disconnected peer.
+ */
+void
+notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
+{
+ struct MeshTunnel *t = cls;
+ struct MeshPeerInfo *peer;
+ struct MeshPathInfo *path_info;
+
+ if (NULL != t->owner && NULL != nc)
+ {
+ struct GNUNET_MESH_PeerControl msg;
+
+ msg.header.size = htons (sizeof (msg));
+ msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
+ msg.tunnel_id = htonl (t->local_tid);
+ GNUNET_PEER_resolve (peer_id, &msg.peer);
+ GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
+ &msg.header, GNUNET_NO);
+ }
+ peer = peer_info_get_short (peer_id);
+ path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
+ path_info->peer = peer;
+ path_info->t = t;
+ GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
+}
+
+
+/**
+ * Add a peer to a tunnel, accomodating paths accordingly and initializing all
+ * needed rescources.
+ * If peer already exists, reevaluate shortest path and change if different.
+ *
+ * @param t Tunnel we want to add a new peer to
+ * @param peer PeerInfo of the peer being added
+ *
+ */
+static void
+tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
+{
+ struct GNUNET_PeerIdentity id;
+ struct MeshPeerPath *best_p;
+ struct MeshPeerPath *p;
+ unsigned int best_cost;
+ unsigned int cost;
+
+ GNUNET_PEER_resolve (peer->id, &id);
+ if (GNUNET_NO ==
+ GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
+ {
+ t->peers_total++;
+ GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
+ peer,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
+ }
+
+ if (NULL != (p = peer->path_head))
+ {
+ best_p = p;
+ best_cost = tree_get_path_cost (t->tree, p);
+ while (NULL != p)
+ {
+ if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
+ {
+ best_cost = cost;
+ best_p = p;
+ }
+ p = p->next;
+ }
+ tree_add_path (t->tree, best_p, &notify_peer_disconnected, t);
+ if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
+ t->path_refresh_task =
+ GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
+ }
+ else
+ {
+ /* Start a DHT get */
+ peer_info_connect (peer, t);
+ }
+}
+
+/**
+ * Add a path to a tunnel which we don't own, just to remember the next hop.
+ * If destination node was already in the tunnel, the first hop information
+ * will be replaced with the new path.
+ *
+ * @param t Tunnel we want to add a new peer to
+ * @param p Path to add
+ * @param own_pos Position of local node in path.
+ *
+ */
+static void
+tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
+ unsigned int own_pos)
+{
+ struct GNUNET_PeerIdentity id;
+
+ GNUNET_assert (0 != own_pos);
+ tree_add_path (t->tree, p, NULL, NULL);
+ if (own_pos < p->length - 1)
+ {
+ GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
+ tree_update_first_hops (t->tree, myid, &id);
+ }
+}
+
+
+/**
+ * Notifies a tunnel that a connection has broken that affects at least
+ * some of its peers. Sends a notification towards the root of the tree.
+ * In case the peer is the owner of the tree, notifies the client that owns
+ * the tunnel and tries to reconnect.
+ *
+ * @param t Tunnel affected.
+ * @param p1 Peer that got disconnected from p2.
+ * @param p2 Peer that got disconnected from p1.
+ *
+ * @return Short ID of the peer disconnected (either p1 or p2).
+ * 0 if the tunnel remained unaffected.
+ */
+static GNUNET_PEER_Id
+tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
+ GNUNET_PEER_Id p2)
+{
+ GNUNET_PEER_Id pid;
+
+ pid =
+ tree_notify_connection_broken (t->tree, p1, p2, &notify_peer_disconnected,
+ t);
+ if (myid != p1 && myid != p2)
+ {
+ return pid;
+ }
+ if (pid != myid)
+ {
+ if (tree_get_predecessor (t->tree) != 0)
+ {
+ /* We are the peer still connected, notify owner of the disconnection. */
+ struct GNUNET_MESH_PathBroken msg;
+ struct GNUNET_PeerIdentity neighbor;
+
+ msg.header.size = htons (sizeof (msg));
+ msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
+ GNUNET_PEER_resolve (t->id.oid, &msg.oid);
+ msg.tid = htonl (t->id.tid);
+ msg.peer1 = my_full_id;
+ GNUNET_PEER_resolve (pid, &msg.peer2);
+ GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
+ send_message (&msg.header, &neighbor);
+ }
+ }
+ return pid;
+}
+
+
+/**
+ * Send a multicast packet to a neighbor.
+ *
+ * @param cls Closure (Info about the multicast packet)
+ * @param neighbor_id Short ID of the neighbor to send the packet to.
+ */
+static void
+tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
+{
+ struct MeshData *mdata = cls;
+ struct MeshTransmissionDescriptor *info;
+ struct GNUNET_PeerIdentity neighbor;
+ unsigned int i;
+
+ info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
+
+ info->mesh_data = mdata;
+ (*(mdata->reference_counter)) ++;
+ info->destination = neighbor_id;
+ GNUNET_PEER_resolve (neighbor_id, &neighbor);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
+ GNUNET_i2s (&neighbor));
+ info->peer = peer_info_get (&neighbor);
+ GNUNET_assert (NULL != info->peer);
+ i = peer_info_transmit_slot (info->peer);
+ info->handler_n = i;
+ info->peer->infos[i] = info;
+ info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
+ info->peer->core_transmit[i] =
+ GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ &neighbor, info->mesh_data->data_len,
+ &send_core_data_multicast, info);
+}
+
+/**
+ * Send a message in a tunnel in multicast, sending a copy to each child node
+ * down the local one in the tunnel tree.
+ *
+ * @param t Tunnel in which to send the data.
+ * @param msg Message to be sent.
+ * @param internal Has the service generated this message?
+ */
+static void
+tunnel_send_multicast (struct MeshTunnel *t,
+ const struct GNUNET_MessageHeader *msg,
+ int internal)
+{
+ struct MeshData *mdata;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " sending a multicast packet...\n");
+ mdata = GNUNET_malloc (sizeof (struct MeshData));
+ mdata->data_len = ntohs (msg->size);
+ mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
+ mdata->t = t;
+ mdata->data = GNUNET_malloc (mdata->data_len);
+ memcpy (mdata->data, msg, mdata->data_len);
+ if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
+ {
+ struct GNUNET_MESH_Multicast *mcast;
+
+ mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
+ mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
+ ntohl (mcast->ttl));
+ }
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
+ }
+ if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
+ && GNUNET_NO == internal)
+ {
+ mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
+ (*(mdata->task)) =
+ GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
+ mdata);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
+ *(mdata->task));
+ }
+
+ tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
+ if (*(mdata->reference_counter) == 0)
+ {
+ GNUNET_free (mdata->data);
+ GNUNET_free (mdata->reference_counter);
+ if (NULL != mdata->task)
+ {
+ GNUNET_SCHEDULER_cancel(*(mdata->task));
+ GNUNET_free (mdata->task);
+ GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
+ }
+ // FIXME change order?
+ GNUNET_free (mdata);
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " sending a multicast packet done\n");
+ return;
+}
+
+
+/**
+ * Send a message to all peers in this tunnel that the tunnel is no longer
+ * valid.
+ *
+ * @param t The tunnel whose peers to notify.
+ */
+static void
+tunnel_send_destroy (struct MeshTunnel *t)
+{
+ struct GNUNET_MESH_TunnelDestroy msg;
+
+ msg.header.size = htons (sizeof (msg));
+ msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
+ GNUNET_PEER_resolve (t->id.oid, &msg.oid);
+ msg.tid = htonl (t->id.tid);
+ tunnel_send_multicast (t, &msg.header, GNUNET_NO);
+}
+
+
+
+/**
+ * Destroy the tunnel and free any allocated resources linked to it.
+ *
+ * @param t the tunnel to destroy
+ *
+ * @return GNUNET_OK on success
+ */
+static int
+tunnel_destroy (struct MeshTunnel *t)
+{
+ struct MeshClient *c;
+ struct MeshQueue *q;
+ struct MeshQueue *qn;
+ GNUNET_HashCode hash;
+ unsigned int i;
+ int r;
+
+ if (NULL == t)
+ return GNUNET_OK;
+
+ r = GNUNET_OK;
+ c = t->owner;
+#if MESH_DEBUG
+ {
+ struct GNUNET_PeerIdentity id;
+
+ GNUNET_PEER_resolve (t->id.oid, &id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
+ GNUNET_i2s (&id), t->id.tid);
+ if (NULL != c)
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
+ }
+#endif
+
+ GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
+ if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
+ {
+ r = GNUNET_SYSERR;
+ }
+
+ GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
+ if (NULL != c &&
+ GNUNET_YES !=
+ GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
+ {
+ r = GNUNET_SYSERR;
+ }
+ GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
+ for (i = 0; i < t->nclients; i++)
+ {
+ c = t->clients[i];
+ if (GNUNET_YES !=
+ GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
+ {
+ r = GNUNET_SYSERR;
+ }
+ }
+ for (i = 0; i < t->nignore; i++)
+ {
+ c = t->ignore[i];
+ if (GNUNET_YES !=
+ GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
+ {
+ r = GNUNET_SYSERR;
+ }
+ }
+ if (t->nclients > 0)
+ {
+ if (GNUNET_YES !=
+ GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
+ {
+ r = GNUNET_SYSERR;
+ }
+ GNUNET_free (t->clients);
+ }
+ if (NULL != t->peers)
+ {
+ GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
+ t);
+ GNUNET_CONTAINER_multihashmap_destroy (t->peers);
+ }
+ q = t->queue_head;
+ while (NULL != q)
+ {
+ if (NULL != q->data)
+ GNUNET_free (q->data);
+ qn = q->next;
+ GNUNET_free (q);
+ q = qn;
+ /* TODO cancel core transmit ready in case it was active */
+ }
+ tree_destroy (t->tree);
+ if (NULL != t->dht_get_type)
+ GNUNET_DHT_get_stop (t->dht_get_type);
+ if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
+ GNUNET_SCHEDULER_cancel (t->timeout_task);
+ if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
+ GNUNET_SCHEDULER_cancel (t->path_refresh_task);
+ GNUNET_free (t);
+ return r;
+}
+
+
+/**
+ * Removes an explicit path from a tunnel, freeing all intermediate nodes
+ * that are no longer needed, as well as nodes of no longer reachable peers.
+ * The tunnel itself is also destoyed if results in a remote empty tunnel.
+ *
+ * @param t Tunnel from which to remove the path.
+ * @param peer Short id of the peer which should be removed.
+ */
+static void
+tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
+{
+ if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
+ tunnel_destroy (t);
+}
+
+
+/**
+ * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
+ * client when the client disconnects. If the client is not the owner, the
+ * owner will get notified if no more clients are in the tunnel and the client
+ * get removed from the tunnel's list.
+ *
+ * @param cls closure (client that is disconnecting)
+ * @param key the hash of the local tunnel id (used to access the hashmap)
+ * @param value the value stored at the key (tunnel to destroy)
+ *
+ * @return GNUNET_OK on success
+ */
+static int
+tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
+{
+ struct MeshTunnel *t = value;
+ struct MeshClient *c = cls;
+ int r;
+
+ send_client_tunnel_disconnect(t, c);
+ if (c != t->owner)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Client %u is destination, keeping the tunnel alive.\n", c->id);
+ tunnel_delete_client(t, c);
+ client_delete_tunnel(c, t);
+ return GNUNET_OK;
+ }
+ tunnel_send_destroy(t);
+ r = tunnel_destroy (t);
+ return r;
+}
+
+
+/**
+ * Timeout function, destroys tunnel if called
+ *
+ * @param cls Closure (tunnel to destroy).
+ * @param tc TaskContext
+ */
+static void
+tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct MeshTunnel *t = cls;
+
+ if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
+ return;
+ t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
+ tunnel_destroy (t);
+}
+
+/**
+ * Resets the tunnel timeout. Starts it if no timeout was running.
+ *
+ * @param t Tunnel whose timeout to reset.
+ */
+static void
+tunnel_reset_timeout (struct MeshTunnel *t)
+{
+ if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
+ GNUNET_SCHEDULER_cancel (t->timeout_task);
+ t->timeout_task =
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
+ (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
+}
+
+
+/******************************************************************************/
+/**************** MESH NETWORK HANDLER HELPERS ***********************/
+/******************************************************************************/
+
+/**
+ * Function called to notify a client about the socket
+ * being ready to queue more data. "buf" will be
+ * NULL and "size" zero if the socket was closed for
+ * writing in the meantime.
+ *
+ * @param cls closure
+ * @param size number of bytes available in buf
+ * @param buf where the callee should write the message
+ * @return number of bytes written to buf
+ */
+static size_t
+send_core_create_path (void *cls, size_t size, void *buf)
+{
+ struct MeshPathInfo *info = cls;
+ struct GNUNET_MESH_ManipulatePath *msg;
+ struct GNUNET_PeerIdentity *peer_ptr;
+ struct MeshPeerInfo *peer = info->peer;
+ struct MeshTunnel *t = info->t;
+ struct MeshPeerPath *p = info->path;
+ size_t size_needed;
+ int i;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
+ size_needed =
+ sizeof (struct GNUNET_MESH_ManipulatePath) +
+ p->length * sizeof (struct GNUNET_PeerIdentity);
+
+ if (size < size_needed || NULL == buf)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "create path retransmit!\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buf: %p\n", buf);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " size: (%u/%u)\n", size,
+ size_needed);
+ info->peer->core_transmit[info->pos] =
+ GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ tree_get_first_hop (t->tree,
+ peer->id),
+ size_needed, &send_core_create_path,
+ info);
+ return 0;
+ }
+ info->peer->core_transmit[info->pos] = NULL;
+#if MESH_DEBUG
+ {
+ struct GNUNET_PeerIdentity id;
+
+ GNUNET_PEER_resolve (peer->id, &id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " setting core_transmit %s [%u] to NULL\n",
+ GNUNET_i2s (&id), info->pos);
+ }
+#endif
+ msg = (struct GNUNET_MESH_ManipulatePath *) buf;
+ msg->header.size = htons (size_needed);
+ msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
+ msg->tid = ntohl (t->id.tid);
+
+ peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
+ for (i = 0; i < p->length; i++)
+ {
+ GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
+ }
+
+ path_destroy (p);
+ GNUNET_free (info);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "CREATE PATH (%u bytes long) sent!\n", size_needed);
+ return size_needed;
+}
+
+
+/**
+ * Function called to notify a client about the socket
+ * being ready to queue more data. "buf" will be
+ * NULL and "size" zero if the socket was closed for
+ * writing in the meantime.
+ *
+ * @param cls closure (data itself)
+ * @param size number of bytes available in buf
+ * @param buf where the callee should write the message
+ *
+ * @return number of bytes written to buf
+ */
+static size_t
+send_core_data_multicast (void *cls, size_t size, void *buf)
+{
+ struct MeshTransmissionDescriptor *info = cls;
+ size_t total_size;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
+ GNUNET_assert (NULL != info);
+ GNUNET_assert (NULL != info->peer);
+ total_size = info->mesh_data->data_len;
+ GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
+
+ if (total_size > size)
+ {
+ /* Retry */
+ struct GNUNET_PeerIdentity id;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Multicast: retransmitting... (%u/%u)\n", size,
+ total_size);
+ GNUNET_PEER_resolve (info->peer->id, &id);
+ info->peer->core_transmit[info->handler_n] =
+ GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
+ GNUNET_TIME_UNIT_FOREVER_REL, &id,
+ total_size,
+ &send_core_data_multicast, info);
+ return 0;
+ }
+ info->peer->core_transmit[info->handler_n] = NULL;
+ info->peer->infos[info->handler_n] = NULL;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
+ memcpy (buf, info->mesh_data->data, total_size);
+#if MESH_DEBUG
+ {
+ struct GNUNET_MESH_Multicast *mc;
+ struct GNUNET_MessageHeader *mh;
+
+ mh = buf;
+ if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
+ {
+ mc = (struct GNUNET_MESH_Multicast *) mh;
+ mh = (struct GNUNET_MessageHeader *) &mc[1];
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " multicast, payload type %u\n", ntohs (mh->type));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " multicast, payload size %u\n", ntohs (mh->size));
+ }
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
+ ntohs (mh->type));
+ }
+ }
+#endif
+ data_descriptor_decrement_multicast (info->mesh_data);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
+ GNUNET_free (info);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
+ return total_size;
+}
+
+
+/**
+ * Function called to notify a client about the socket
+ * being ready to queue more data. "buf" will be
+ * NULL and "size" zero if the socket was closed for
+ * writing in the meantime.
+ *
+ * @param cls closure (MeshTransmissionDescriptor)
+ * @param size number of bytes available in buf
+ * @param buf where the callee should write the message
+ * @return number of bytes written to buf
+ */
+static size_t
+send_core_path_ack (void *cls, size_t size, void *buf)
+{
+ struct MeshTransmissionDescriptor *info = cls;
+ struct GNUNET_MESH_PathACK *msg = buf;
+
+ GNUNET_assert (NULL != info);
+ if (info->peer)
+ {
+ info->peer->core_transmit[info->handler_n] = NULL;
+ }
+ if (sizeof (struct GNUNET_MESH_PathACK) > size)
+ {
+ GNUNET_break (0);
+ return 0;
+ }
+ msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
+ msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
+ GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
+ msg->tid = htonl (info->origin->tid);
+ msg->peer_id = my_full_id;
+ GNUNET_free (info);
+ /* TODO add signature */
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
+ return sizeof (struct GNUNET_MESH_PathACK);
+}
+
+
+/******************************************************************************/
+/******************** MESH NETWORK HANDLERS **************************/
+/******************************************************************************/
+
+
+/**
+ * Core handler for path creation
+ *
+ * @param cls closure
+ * @param message message
+ * @param peer peer identity this notification is about
+ * @param atsi performance data
+ * @param atsi_count number of records in 'atsi'
+ *
+ * @return GNUNET_OK to keep the connection open,
+ * GNUNET_SYSERR to close it (signal serious error)
+ */
+static int
+handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message,
+ const struct GNUNET_ATS_Information *atsi,
+ unsigned int atsi_count)
+{
+ unsigned int own_pos;
+ uint16_t size;
+ uint16_t i;
+ MESH_TunnelNumber tid;
+ struct GNUNET_MESH_ManipulatePath *msg;
+ struct GNUNET_PeerIdentity *pi;
+ GNUNET_HashCode hash;
+ struct MeshPeerPath *path;
+ struct MeshPeerInfo *dest_peer_info;
+ struct MeshPeerInfo *orig_peer_info;
+ struct MeshTunnel *t;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Received a path create msg [%s]\n",
+ GNUNET_i2s (&my_full_id));
+ size = ntohs (message->size);
+ if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+
+ size -= sizeof (struct GNUNET_MESH_ManipulatePath);
+ if (size % sizeof (struct GNUNET_PeerIdentity))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+ size /= sizeof (struct GNUNET_PeerIdentity);
+ if (size < 2)
+ {
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
+ msg = (struct GNUNET_MESH_ManipulatePath *) message;
+
+ tid = ntohl (msg->tid);
+ pi = (struct GNUNET_PeerIdentity *) &msg[1];
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
+ t = tunnel_get (pi, tid);
+ if (NULL == t)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
+ t = GNUNET_malloc (sizeof (struct MeshTunnel));
+ t->id.oid = GNUNET_PEER_intern (pi);
+ t->id.tid = tid;
+ while (NULL != tunnel_get_incoming (next_local_tid))
+ next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
+ t->local_tid_dest = next_local_tid++;
+ next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
+ t->tree = tree_new (t->id.oid);
+
+ GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
+ if (GNUNET_OK !=
+ GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
+ {
+ tunnel_destroy (t);
+ GNUNET_break (0);
+ return GNUNET_OK;
+ }
+ tunnel_reset_timeout (t);
+ GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
+ if (GNUNET_OK !=
+ GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
+ {
+ tunnel_destroy (t);
+ GNUNET_break (0);
+ return GNUNET_OK;
+ }
+ }
+ dest_peer_info =
+ GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
+ if (NULL == dest_peer_info)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " Creating PeerInfo for destination.\n");
+ dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
+ dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
+ GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
+ dest_peer_info,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
+ }
+ orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
+ if (NULL == orig_peer_info)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " Creating PeerInfo for origin.\n");
+ orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
+ orig_peer_info->id = GNUNET_PEER_intern (pi);
+ GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
+ path = path_new (size);
+ own_pos = 0;
+ for (i = 0; i < size; i++)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
+ GNUNET_i2s (&pi[i]));
+ path->peers[i] = GNUNET_PEER_intern (&pi[i]);
+ if (path->peers[i] == myid)
+ own_pos = i;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
+ if (own_pos == 0)
+ {
+ /* cannot be self, must be 'not found' */
+ /* create path: self not found in path through self */
+ GNUNET_break_op (0);
+ path_destroy (path);
+ /* FIXME error. destroy tunnel? leave for timeout? */
+ return 0;
+ }
+ path_add_to_peers (path, GNUNET_NO);
+ tunnel_add_path (t, path, own_pos);
+ if (own_pos == size - 1)
+ {
+ /* It is for us! Send ack. */
+ struct MeshTransmissionDescriptor *info;
+ unsigned int j;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
+ peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
+ if (NULL == t->peers)
+ {
+ /* New tunnel! Notify clients on data. */
+ t->peers = GNUNET_CONTAINER_multihashmap_create (4);
+ }
+ GNUNET_break (GNUNET_OK ==
+ GNUNET_CONTAINER_multihashmap_put (t->peers,
+ &my_full_id.hashPubKey,
+ peer_info_get
+ (&my_full_id),
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
+ /* FIXME use send_message */
+ info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
+ info->origin = &t->id;
+ info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
+ GNUNET_assert (NULL != info->peer);
+ j = peer_info_transmit_slot (info->peer);
+ info->handler_n = j;
+ info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
+ info->peer->infos[j] = info;
+ info->peer->core_transmit[j] =
+ GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
+ GNUNET_TIME_UNIT_FOREVER_REL, peer,
+ sizeof (struct GNUNET_MESH_PathACK),
+ &send_core_path_ack, info);
+ }
+ else
+ {
+ struct MeshPeerPath *path2;
+
+ /* It's for somebody else! Retransmit. */
+ path2 = path_duplicate (path);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
+ peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
+ path2 = path_duplicate (path);
+ peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
+ send_create_path (dest_peer_info, path, t);
+ }
+ return GNUNET_OK;
+}
+
+
+/**
+ * Core handler for path destruction
+ *
+ * @param cls closure
+ * @param message message
+ * @param peer peer identity this notification is about
+ * @param atsi performance data
+ * @param atsi_count number of records in 'atsi'
+ *
+ * @return GNUNET_OK to keep the connection open,
+ * GNUNET_SYSERR to close it (signal serious error)
+ */
+static int
+handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message,
+ const struct GNUNET_ATS_Information *atsi,
+ unsigned int atsi_count)
+{
+ struct GNUNET_MESH_ManipulatePath *msg;
+ struct GNUNET_PeerIdentity *pi;
+ struct MeshPeerPath *path;
+ struct MeshTunnel *t;
+ unsigned int own_pos;
+ unsigned int i;
+ size_t size;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
+ size = ntohs (message->size);
+ if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+
+ size -= sizeof (struct GNUNET_MESH_ManipulatePath);
+ if (size % sizeof (struct GNUNET_PeerIdentity))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+ size /= sizeof (struct GNUNET_PeerIdentity);
+ if (size < 2)
+ {
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
+
+ msg = (struct GNUNET_MESH_ManipulatePath *) message;
+ pi = (struct GNUNET_PeerIdentity *) &msg[1];
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
+ msg->tid);
+ t = tunnel_get (pi, ntohl (msg->tid));
+ if (NULL == t)
+ {
+ /* TODO notify back: we don't know this tunnel */
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
+ path = path_new (size);
+ own_pos = 0;
+ for (i = 0; i < size; i++)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
+ GNUNET_i2s (&pi[i]));
+ path->peers[i] = GNUNET_PEER_intern (&pi[i]);
+ if (path->peers[i] == myid)
+ own_pos = i;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
+ if (own_pos < path->length - 1)
+ send_message (message, &pi[own_pos + 1]);
+ else
+ send_client_tunnel_disconnect(t, NULL);
+
+ tunnel_delete_peer (t, path->peers[path->length - 1]);
+ path_destroy (path);
+ return GNUNET_OK;
+}
+
+
+/**
+ * Core handler for notifications of broken paths
+ *
+ * @param cls closure
+ * @param message message
+ * @param peer peer identity this notification is about
+ * @param atsi performance data
+ * @param atsi_count number of records in 'atsi'
+ *
+ * @return GNUNET_OK to keep the connection open,
+ * GNUNET_SYSERR to close it (signal serious error)
+ */
+static int
+handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message,
+ const struct GNUNET_ATS_Information *atsi,
+ unsigned int atsi_count)
+{
+ struct GNUNET_MESH_PathBroken *msg;
+ struct MeshTunnel *t;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
+ msg = (struct GNUNET_MESH_PathBroken *) message;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
+ GNUNET_i2s (&msg->peer1));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
+ GNUNET_i2s (&msg->peer2));
+ t = tunnel_get (&msg->oid, ntohl (msg->tid));
+ if (NULL == t)
+ {
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+ tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
+ GNUNET_PEER_search (&msg->peer2));
+ return GNUNET_OK;
+
+}
+
+
+/**
+ * Core handler for tunnel destruction
+ *
+ * @param cls closure
+ * @param message message
+ * @param peer peer identity this notification is about
+ * @param atsi performance data
+ * @param atsi_count number of records in 'atsi'
+ *
+ * @return GNUNET_OK to keep the connection open,
+ * GNUNET_SYSERR to close it (signal serious error)
+ */
+static int
+handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message,
+ const struct GNUNET_ATS_Information *atsi,
+ unsigned int atsi_count)
+{
+ struct GNUNET_MESH_TunnelDestroy *msg;
+ struct MeshTunnel *t;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
+ msg = (struct GNUNET_MESH_TunnelDestroy *) message;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
+ GNUNET_i2s (&msg->oid), ntohl (msg->tid));
+ t = tunnel_get (&msg->oid, ntohl (msg->tid));
+ if (NULL == t)
+ {
+ /* Probably already got the message from another path,
+ * destroyed the tunnel and retransmitted to children.
+ * Safe to ignore.
+ */
+ return GNUNET_OK;
+ }
+ if (t->id.oid == myid)
+ {
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+ if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
+ {
+ /* Tunnel was incoming, notify clients */
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
+ t->local_tid, t->local_tid_dest);
+ send_clients_tunnel_destroy (t);
+ }
+ tunnel_send_destroy (t);
+ tunnel_destroy (t);
+ return GNUNET_OK;
+}
+
+
+/**
+ * Core handler for mesh network traffic going from the origin to a peer
+ *
+ * @param cls closure
+ * @param peer peer identity this notification is about
+ * @param message message
+ * @param atsi performance data
+ * @param atsi_count number of records in 'atsi'
+ * @return GNUNET_OK to keep the connection open,
+ * GNUNET_SYSERR to close it (signal serious error)
+ */
+static int
+handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message,
+ const struct GNUNET_ATS_Information *atsi,
+ unsigned int atsi_count)
+{
+ struct GNUNET_MESH_Unicast *msg;
+ struct MeshTunnel *t;
+ GNUNET_PEER_Id pid;
+ size_t size;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
+ GNUNET_i2s (peer));
+ size = ntohs (message->size);
+ if (size <
+ sizeof (struct GNUNET_MESH_Unicast) +
+ sizeof (struct GNUNET_MessageHeader))
+ {
+ GNUNET_break (0);
+ return GNUNET_OK;
+ }
+ msg = (struct GNUNET_MESH_Unicast *) message;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
+ ntohs (msg[1].header.type));
+ t = tunnel_get (&msg->oid, ntohl (msg->tid));
+ if (NULL == t)
+ {
+ /* TODO notify back: we don't know this tunnel */
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+ tunnel_reset_timeout (t);
+ pid = GNUNET_PEER_search (&msg->destination);
+ if (pid == myid)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " it's for us! sending to clients...\n");
+ send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
+ return GNUNET_OK;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " not for us, retransmitting...\n");
+ send_message (message, tree_get_first_hop (t->tree, pid));
+ return GNUNET_OK;
+}
+
+
+/**
+ * Core handler for mesh network traffic going from the origin to all peers
+ *
+ * @param cls closure
+ * @param message message
+ * @param peer peer identity this notification is about
+ * @param atsi performance data
+ * @param atsi_count number of records in 'atsi'
+ * @return GNUNET_OK to keep the connection open,
+ * GNUNET_SYSERR to close it (signal serious error)
+ *
+ * TODO: Check who we got this from, to validate route.
+ */
+static int
+handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message,
+ const struct GNUNET_ATS_Information *atsi,
+ unsigned int atsi_count)
+{
+ struct GNUNET_MESH_Multicast *msg;
+ struct MeshTunnel *t;
+ size_t size;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
+ GNUNET_i2s (peer));
+ size = ntohs (message->size);
+ if (sizeof (struct GNUNET_MESH_Multicast) +
+ sizeof (struct GNUNET_MessageHeader) > size)
+ {
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+ msg = (struct GNUNET_MESH_Multicast *) message;
+ t = tunnel_get (&msg->oid, ntohl (msg->tid));
+
+ if (NULL == t)
+ {
+ /* TODO notify that we dont know that tunnel */
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+ if (t->mid == ntohl (msg->mid))
+ {
+ /* FIXME: already seen this packet, log dropping */
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ " Already seen mid %u, DROPPING!\n", t->mid);
+ return GNUNET_OK;
+ }
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " mid %u not seen yet, forwarding\n", ntohl (msg->mid));
+ }
+ t->mid = ntohl (msg->mid);
+ tunnel_reset_timeout (t);
+
+ /* Transmit to locally interested clients */
+ if (NULL != t->peers &&
+ GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
+ {
+ send_subscribed_clients (message, &msg[1].header);
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
+ if (ntohl (msg->ttl) == 0)
+ {
+ /* FIXME: ttl is 0, log dropping */
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
+ return GNUNET_OK;
+ }
+ tunnel_send_multicast (t, message, GNUNET_NO);
+ return GNUNET_OK;
+}
+
+
+/**
+ * Core handler for mesh network traffic toward the owner of a tunnel
+ *
+ * @param cls closure
+ * @param message message
+ * @param peer peer identity this notification is about
+ * @param atsi performance data
+ * @param atsi_count number of records in 'atsi'
+ *
+ * @return GNUNET_OK to keep the connection open,
+ * GNUNET_SYSERR to close it (signal serious error)
+ */
+static int
+handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message,
+ const struct GNUNET_ATS_Information *atsi,
+ unsigned int atsi_count)
+{
+ struct GNUNET_MESH_ToOrigin *msg;
+ struct GNUNET_PeerIdentity id;
+ struct MeshPeerInfo *peer_info;
+ struct MeshTunnel *t;
+ size_t size;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
+ GNUNET_i2s (peer));
+ size = ntohs (message->size);
+ if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
+ sizeof (struct GNUNET_MessageHeader)) /* at least a header */
+ {
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+ msg = (struct GNUNET_MESH_ToOrigin *) message;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
+ ntohs (msg[1].header.type));
+ t = tunnel_get (&msg->oid, ntohl (msg->tid));
+
+ if (NULL == t)
+ {
+ /* TODO notify that we dont know this tunnel (whom)? */
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+
+ if (t->id.oid == myid)
+ {
+ char cbuf[size];
+ struct GNUNET_MESH_ToOrigin *copy;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " it's for us! sending to clients...\n");
+ if (NULL == t->owner)
+ {
+ /* got data packet for ownerless tunnel */
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+ /* TODO signature verification */
+ memcpy (cbuf, message, size);
+ copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
+ copy->tid = htonl (t->local_tid);
+ GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
+ &copy->header, GNUNET_YES);
+ return GNUNET_OK;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " not for us, retransmitting...\n");
+
+ peer_info = peer_info_get (&msg->oid);
+ if (NULL == peer_info)
+ {
+ /* unknown origin of tunnel */
+ GNUNET_break (0);
+ return GNUNET_OK;
+ }
+ GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
+ send_message (message, &id);
+
+ return GNUNET_OK;
+}
+
+
+/**
+ * Core handler for path ACKs
+ *
+ * @param cls closure
+ * @param message message
+ * @param peer peer identity this notification is about
+ * @param atsi performance data
+ * @param atsi_count number of records in 'atsi'
+ *
+ * @return GNUNET_OK to keep the connection open,
+ * GNUNET_SYSERR to close it (signal serious error)
+ */
+static int
+handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_MessageHeader *message,
+ const struct GNUNET_ATS_Information *atsi,
+ unsigned int atsi_count)
+{
+ struct GNUNET_MESH_PathACK *msg;
+ struct GNUNET_PeerIdentity id;
+ struct MeshPeerInfo *peer_info;
+ struct MeshPeerPath *p;
+ struct MeshTunnel *t;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
+ GNUNET_i2s (&my_full_id));
+ msg = (struct GNUNET_MESH_PathACK *) message;
+ t = tunnel_get (&msg->oid, msg->tid);
+ if (NULL == t)
+ {
+ /* TODO notify that we don't know the tunnel */
+ return GNUNET_OK;
+ }
+
+ peer_info = peer_info_get (&msg->peer_id);
+
+ /* Add paths to peers? */
+ p = tree_get_path_to_peer (t->tree, peer_info->id);
+ if (NULL != p)
+ {
+ path_add_to_peers (p, GNUNET_YES);
+ path_destroy (p);
+ }
+ else
+ {
+ GNUNET_break (0);
+ }
+
+ /* Message for us? */
+ if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
+ if (NULL == t->owner)
+ {
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
+ if (NULL != t->dht_get_type)
+ {
+ GNUNET_DHT_get_stop (t->dht_get_type);
+ t->dht_get_type = NULL;
+ }
+ if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
+ {
+ tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
+ send_client_peer_connected (t, peer_info->id);
+ }
+ return GNUNET_OK;
+ }
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " not for us, retransmitting...\n");
+ GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
+ peer_info = peer_info_get (&msg->oid);
+ if (NULL == peer_info)
+ {
+ /* If we know the tunnel, we should DEFINITELY know the peer */
+ GNUNET_break (0);
+ return GNUNET_OK;
+ }
+ send_message (message, &id);
+ return GNUNET_OK;
+}
+
+
+/**
+ * Functions to handle messages from core
+ */
+static struct GNUNET_CORE_MessageHandler core_handlers[] = {
+ {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
+ {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
+ {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
+ sizeof (struct GNUNET_MESH_PathBroken)},
+ {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
+ {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
+ {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
+ {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
+ {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
+ sizeof (struct GNUNET_MESH_PathACK)},
+ {NULL, 0, 0}
+};
+
+
+
+/******************************************************************************/
+/**************** MESH LOCAL HANDLER HELPERS ***********************/
+/******************************************************************************/
+
+/**
+ * deregister_app: iterator for removing each application registered by a client
+ *
+ * @param cls closure
+ * @param key the hash of the application id (used to access the hashmap)
+ * @param value the value stored at the key (client)
+ *
+ * @return GNUNET_OK on success
+ */
+static int
+deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
+{
+ GNUNET_break (GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap_remove (applications, key,
+ value));
+ return GNUNET_OK;
+}
+
+#if LATER
+/**
+ * notify_client_connection_failure: notify a client that the connection to the
+ * requested remote peer is not possible (for instance, no route found)
+ * Function called when the socket is ready to queue more data. "buf" will be
+ * NULL and "size" zero if the socket was closed for writing in the meantime.
+ *
+ * @param cls closure
+ * @param size number of bytes available in buf
+ * @param buf where the callee should write the message
+ * @return number of bytes written to buf
+ */
+static size_t
+notify_client_connection_failure (void *cls, size_t size, void *buf)
+{
+ int size_needed;
+ struct MeshPeerInfo *peer_info;
+ struct GNUNET_MESH_PeerControl *msg;
+ struct GNUNET_PeerIdentity id;
+
+ if (0 == size && NULL == buf)
+ {
+ // TODO retry? cancel?
+ return 0;
+ }
+
+ size_needed = sizeof (struct GNUNET_MESH_PeerControl);
+ peer_info = (struct MeshPeerInfo *) cls;
+ msg = (struct GNUNET_MESH_PeerControl *) buf;
+ msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
+ msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
+// msg->tunnel_id = htonl(peer_info->t->tid);
+ GNUNET_PEER_resolve (peer_info->id, &id);
+ memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
+
+ return size_needed;
+}
+#endif
+
+
+/**
+ * Send keepalive packets for a peer
+ *
+ * @param cls Closure (tunnel for which to send the keepalive).
+ * @param tc Notification context.
+ *
+ * TODO: implement explicit multicast keepalive?
+ */
+static void
+path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct MeshTunnel *t = cls;
+ struct GNUNET_MessageHeader *payload;
+ struct GNUNET_MESH_Multicast *msg;
+ size_t size =
+ sizeof (struct GNUNET_MESH_Multicast) +
+ sizeof (struct GNUNET_MessageHeader);
+ char cbuf[size];
+
+ if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
+ {
+ return;
+ }
+ t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "sending keepalive for tunnel %d\n", t->id.tid);
+
+ msg = (struct GNUNET_MESH_Multicast *) cbuf;
+ msg->header.size = htons (size);
+ msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
+ msg->oid = my_full_id;
+ msg->tid = htonl (t->id.tid);
+ msg->ttl = htonl (DEFAULT_TTL);
+ msg->mid = htonl (t->mid + 1);
+ t->mid++;
+ payload = (struct GNUNET_MessageHeader *) &msg[1];
+ payload->size = htons (sizeof (struct GNUNET_MessageHeader));
+ payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
+ tunnel_send_multicast (t, &msg->header, GNUNET_YES);
+
+ t->path_refresh_task =
+ GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
+ return;
+}
+
+
+/**
+ * Function to process paths received for a new peer addition. The recorded
+ * paths form the initial tunnel, which can be optimized later.
+ * Called on each result obtained for the DHT search.
+ *
+ * @param cls closure
+ * @param exp when will this value expire
+ * @param key key of the result
+ * @param get_path path of the get request
+ * @param get_path_length lenght of get_path
+ * @param put_path path of the put request
+ * @param put_path_length length of the put_path
+ * @param type type of the result
+ * @param size number of bytes in data
+ * @param data pointer to the result data
+ *
+ * TODO: re-issue the request after certain time? cancel after X results?
+ */
+static void
+dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
+ const GNUNET_HashCode * key,
+ const struct GNUNET_PeerIdentity *get_path,
+ unsigned int get_path_length,
+ const struct GNUNET_PeerIdentity *put_path,
+ unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
+ size_t size, const void *data)
+{
+ struct MeshPathInfo *path_info = cls;
+ struct MeshPeerPath *p;
+ struct GNUNET_PeerIdentity pi;
+ int i;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
+ GNUNET_PEER_resolve (path_info->peer->id, &pi);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
+
+ p = path_build_from_dht (get_path, get_path_length, put_path,
+ put_path_length);
+ path_add_to_peers (p, GNUNET_NO);
+ path_destroy(p);
+ for (i = 0; i < path_info->peer->ntunnels; i++)
+ {
+ tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
+ peer_info_connect (path_info->peer, path_info->t);
+ }
+
+ return;
+}
+
+
+/**
+ * Function to process paths received for a new peer addition. The recorded
+ * paths form the initial tunnel, which can be optimized later.
+ * Called on each result obtained for the DHT search.
+ *
+ * @param cls closure
+ * @param exp when will this value expire
+ * @param key key of the result
+ * @param get_path path of the get request
+ * @param get_path_length lenght of get_path
+ * @param put_path path of the put request
+ * @param put_path_length length of the put_path
+ * @param type type of the result
+ * @param size number of bytes in data
+ * @param data pointer to the result data
+ */
+static void
+dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
+ const GNUNET_HashCode * key,
+ const struct GNUNET_PeerIdentity *get_path,
+ unsigned int get_path_length,
+ const struct GNUNET_PeerIdentity *put_path,
+ unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
+ size_t size, const void *data)
+{
+ const struct GNUNET_PeerIdentity *pi = data;
+ struct MeshTunnel *t = cls;
+ struct MeshPeerInfo *peer_info;
+ struct MeshPeerPath *p;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
+ if (size != sizeof (struct GNUNET_PeerIdentity))
+ {
+ GNUNET_break_op (0);
+ return;
+ }
+ GNUNET_assert (NULL != t->owner);
+ peer_info = peer_info_get (pi);
+ (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
+ peer_info,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
+
+ p = path_build_from_dht (get_path, get_path_length, put_path,
+ put_path_length);
+ path_add_to_peers (p, GNUNET_NO);
+ path_destroy(p);
+ tunnel_add_peer (t, peer_info);
+ peer_info_connect (peer_info, t);
+}
+
+
+/******************************************************************************/
+/********************* MESH LOCAL HANDLES **************************/
+/******************************************************************************/
+
+
+/**
+ * Handler for client disconnection
+ *
+ * @param cls closure
+ * @param client identification of the client; NULL
+ * for the last call when the server is destroyed
+ */
+static void
+handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
+{
+ struct MeshClient *c;
+ struct MeshClient *next;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
+ if (client == NULL)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
+ return;
+ }
+ c = clients;
+ while (NULL != c)
+ {
+ if (c->handle != client)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
+ c = c->next;
+ continue;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
+ c->id);
+ GNUNET_SERVER_client_drop (c->handle);
+ c->shutting_down = GNUNET_YES;
+ GNUNET_assert (NULL != c->own_tunnels);
+ GNUNET_assert (NULL != c->incoming_tunnels);
+ GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
+ &tunnel_destroy_iterator, c);
+ GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
+ &tunnel_destroy_iterator, c);
+ GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
+ &tunnel_destroy_iterator, c);
+ GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
+ GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
+ GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
+
+ /* deregister clients applications */
+ if (NULL != c->apps)
+ {
+ GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
+ GNUNET_CONTAINER_multihashmap_destroy (c->apps);
+ }
+ if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
+ GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
+ {
+ GNUNET_SCHEDULER_cancel (announce_applications_task);
+ announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+ if (NULL != c->types)
+ GNUNET_CONTAINER_multihashmap_destroy (c->types);
+ next = c->next;
+ GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
+ GNUNET_free (c);
+ c = next;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
+ return;
+}
+
+
+/**
+ * Handler for new clients
+ *
+ * @param cls closure
+ * @param client identification of the client
+ * @param message the actual message, which includes messages the client wants
+ */
+static void
+handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *message)
+{
+ struct GNUNET_MESH_ClientConnect *cc_msg;
+ struct MeshClient *c;
+ GNUNET_MESH_ApplicationType *a;
+ unsigned int size;
+ uint16_t ntypes;
+ uint16_t *t;
+ uint16_t napps;
+ uint16_t i;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
+ /* Check data sanity */
+ size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
+ cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
+ ntypes = ntohs (cc_msg->types);
+ napps = ntohs (cc_msg->applications);
+ if (size !=
+ ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ /* Create new client structure */
+ c = GNUNET_malloc (sizeof (struct MeshClient));
+ c->id = next_client_id++;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
+ c->handle = client;
+ GNUNET_SERVER_client_keep (client);
+ a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
+ if (napps > 0)
+ {
+ GNUNET_MESH_ApplicationType at;
+ GNUNET_HashCode hc;
+
+ c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
+ for (i = 0; i < napps; i++)
+ {
+ at = ntohl (a[i]);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
+ GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
+ /* store in clients hashmap */
+ GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+ /* store in global hashmap, for announcements */
+ GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+ }
+ if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
+ announce_applications_task =
+ GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
+
+ }
+ if (ntypes > 0)
+ {
+ uint16_t u16;
+ GNUNET_HashCode hc;
+
+ t = (uint16_t *) & a[napps];
+ c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
+ for (i = 0; i < ntypes; i++)
+ {
+ u16 = ntohs (t[i]);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
+ GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
+
+ /* store in clients hashmap */
+ GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+ /* store in global hashmap */
+ GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+ }
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " client has %u+%u subscriptions\n", napps, ntypes);
+
+ GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
+ c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
+ c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
+ c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
+ GNUNET_SERVER_notification_context_add (nc, client);
+
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
+}
+
+
+/**
+ * Handler for requests of new tunnels
+ *
+ * @param cls closure
+ * @param client identification of the client
+ * @param message the actual message
+ */
+static void
+handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *message)
+{
+ struct GNUNET_MESH_TunnelMessage *t_msg;
+ struct MeshTunnel *t;
+ struct MeshClient *c;
+ GNUNET_HashCode hash;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
+
+ /* Sanity check for client registration */
+ if (NULL == (c = client_get (client)))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
+
+ /* Message sanity check */
+ if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
+ /* Sanity check for tunnel numbering */
+ if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+ /* Sanity check for duplicate tunnel IDs */
+ if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ t = GNUNET_malloc (sizeof (struct MeshTunnel));
+ while (NULL != tunnel_get_by_pi (myid, next_tid))
+ next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
+ t->id.tid = next_tid++;
+ next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
+ t->id.oid = myid;
+ t->local_tid = ntohl (t_msg->tunnel_id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
+ GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
+ t->owner = c;
+ t->peers = GNUNET_CONTAINER_multihashmap_create (32);
+
+ GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
+ if (GNUNET_OK !=
+ GNUNET_CONTAINER_multihashmap_put (c->own_tunnels, &hash, t,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
+ if (GNUNET_OK !=
+ GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+ t->tree = tree_new (myid);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ return;
+}
+
+
+/**
+ * Handler for requests of deleting tunnels
+ *
+ * @param cls closure
+ * @param client identification of the client
+ * @param message the actual message
+ */
+static void
+handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *message)
+{
+ struct GNUNET_MESH_TunnelMessage *tunnel_msg;
+ struct MeshClient *c;
+ struct MeshTunnel *t;
+ MESH_TunnelNumber tid;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Got a DESTROY TUNNEL from client!\n");
+
+ /* Sanity check for client registration */
+ if (NULL == (c = client_get (client)))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+ /* Message sanity check */
+ if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
+ tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
+
+ /* Retrieve tunnel */
+ tid = ntohl (tunnel_msg->tunnel_id);
+ t = tunnel_get_by_local_id(c, tid);
+ if (NULL == t)
+ {
+ GNUNET_break (0);
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+ send_client_tunnel_disconnect(t, c);
+ if (c != t->owner)
+ {
+ client_ignore_tunnel (c, t);
+#if 0
+ // TODO: when to destroy incoming tunnel?
+ if (t->nclients == 0)
+ {
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
+ &hash, t));
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap_remove (t->peers,
+ &my_full_id.hashPubKey,
+ t));
+ }
+#endif
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ return;
+ }
+ client_delete_tunnel(c, t);
+
+ /* Don't try to ACK the client about the tunnel_destroy multicast packet */
+ t->owner = NULL;
+ tunnel_send_destroy (t);
+ tunnel_destroy (t);
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ return;
+}
+
+
+/**
+ * Handler for connection requests to new peers
+ *
+ * @param cls closure
+ * @param client identification of the client
+ * @param message the actual message (PeerControl)
+ */
+static void
+handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *message)
+{
+ struct GNUNET_MESH_PeerControl *peer_msg;
+ struct MeshPeerInfo *peer_info;
+ struct MeshClient *c;
+ struct MeshTunnel *t;
+ MESH_TunnelNumber tid;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
+ /* Sanity check for client registration */
+ if (NULL == (c = client_get (client)))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ peer_msg = (struct GNUNET_MESH_PeerControl *) message;
+ /* Sanity check for message size */
+ if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ /* Tunnel exists? */
+ tid = ntohl (peer_msg->tunnel_id);
+ t = tunnel_get_by_local_id (c, tid);
+ if (NULL == t)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ /* Does client own tunnel? */
+ if (t->owner->handle != client)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
+ GNUNET_i2s (&peer_msg->peer));
+ peer_info = peer_info_get (&peer_msg->peer);
+
+ tunnel_add_peer (t, peer_info);
+ peer_info_connect (peer_info, t);
+
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ return;
+}
+
+
+/**
+ * Handler for disconnection requests of peers in a tunnel
+ *
+ * @param cls closure
+ * @param client identification of the client
+ * @param message the actual message (PeerControl)
+ */
+static void
+handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *message)
+{
+ struct GNUNET_MESH_PeerControl *peer_msg;
+ struct MeshPeerInfo *peer_info;
+ struct MeshClient *c;
+ struct MeshTunnel *t;
+ MESH_TunnelNumber tid;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
+ /* Sanity check for client registration */
+ if (NULL == (c = client_get (client)))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+ peer_msg = (struct GNUNET_MESH_PeerControl *) message;
+ /* Sanity check for message size */
+ if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ /* Tunnel exists? */
+ tid = ntohl (peer_msg->tunnel_id);
+ t = tunnel_get_by_local_id (c, tid);
+ if (NULL == t)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
+
+ /* Does client own tunnel? */
+ if (t->owner->handle != client)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
+ GNUNET_i2s (&peer_msg->peer));
+ /* Is the peer in the tunnel? */
+ peer_info =
+ GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
+ if (NULL == peer_info)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ /* Ok, delete peer from tunnel */
+ GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
+ &peer_msg->peer.hashPubKey);
+
+ send_destroy_path (t, peer_info->id);
+ tunnel_delete_peer (t, peer_info->id);
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ return;
+}
+
+
+/**
+ * Handler for connection requests to new peers by type
+ *
+ * @param cls closure
+ * @param client identification of the client
+ * @param message the actual message (ConnectPeerByType)
+ */
+static void
+handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *message)
+{
+ struct GNUNET_MESH_ConnectPeerByType *connect_msg;
+ struct MeshClient *c;
+ struct MeshTunnel *t;
+ GNUNET_HashCode hash;
+ MESH_TunnelNumber tid;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
+ /* Sanity check for client registration */
+ if (NULL == (c = client_get (client)))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
+ /* Sanity check for message size */
+ if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
+ ntohs (connect_msg->header.size))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ /* Tunnel exists? */
+ tid = ntohl (connect_msg->tunnel_id);
+ t = tunnel_get_by_local_id (c, tid);
+ if (NULL == t)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ /* Does client own tunnel? */
+ if (t->owner->handle != client)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ /* Do WE have the service? */
+ t->type = ntohl (connect_msg->type);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
+ GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
+ if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
+ GNUNET_YES)
+ {
+ /* Yes! Fast forward, add ourselves to the tunnel and send the
+ * good news to the client, and alert the destination client of
+ * an incoming tunnel.
+ *
+ * FIXME send a path create to self, avoid code duplication
+ */
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
+ GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
+ peer_info_get (&my_full_id),
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
+ send_client_peer_connected (t, myid);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
+
+ t->local_tid_dest = next_local_tid++;
+ GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
+ GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
+
+ return;
+ }
+ /* Ok, lets find a peer offering the service */
+ if (NULL != t->dht_get_type)
+ {
+ GNUNET_DHT_get_stop (t->dht_get_type);
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
+ GNUNET_h2s (&hash));
+ t->dht_get_type =
+ GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
+ GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
+ GNUNET_DHT_RO_RECORD_ROUTE |
+ GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, 0,
+ &dht_get_type_handler, t);
+
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ return;
+}
+
+
+/**
+ * Handler for client traffic directed to one peer
+ *
+ * @param cls closure
+ * @param client identification of the client
+ * @param message the actual message
+ */
+static void
+handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *message)
+{
+ struct MeshClient *c;
+ struct MeshTunnel *t;
+ struct MeshPeerInfo *pi;
+ struct GNUNET_MESH_Unicast *data_msg;
+ MESH_TunnelNumber tid;
+ size_t size;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Got a unicast request from a client!\n");
+
+ /* Sanity check for client registration */
+ if (NULL == (c = client_get (client)))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+ data_msg = (struct GNUNET_MESH_Unicast *) message;
+ /* Sanity check for message size */
+ size = ntohs (message->size);
+ if (sizeof (struct GNUNET_MESH_Unicast) +
+ sizeof (struct GNUNET_MessageHeader) > size)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ /* Tunnel exists? */
+ tid = ntohl (data_msg->tid);
+ t = tunnel_get_by_local_id (c, tid);
+ if (NULL == t)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ /* Is it a local tunnel? Then, does client own the tunnel? */
+ if (t->owner->handle != client)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
+ &data_msg->destination.hashPubKey);
+ /* Is the selected peer in the tunnel? */
+ if (NULL == pi)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ /* Ok, everything is correct, send the message
+ * (pretend we got it from a mesh peer)
+ */
+ {
+ char buf[ntohs (message->size)];
+ struct GNUNET_MESH_Unicast *copy;
+
+ /* Work around const limitation */
+ copy = (struct GNUNET_MESH_Unicast *) buf;
+ memcpy (buf, data_msg, size);
+ copy->oid = my_full_id;
+ copy->tid = htonl (t->id.tid);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " calling generic handler...\n");
+ handle_mesh_data_unicast (NULL, &my_full_id, &copy->header, NULL, 0);
+ }
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ return;
+}
+
+
+/**
+ * Handler for client traffic directed to the origin
+ *
+ * @param cls closure
+ * @param client identification of the client
+ * @param message the actual message
+ */
+static void
+handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *message)
+{
+ struct GNUNET_MESH_ToOrigin *data_msg;
+ struct GNUNET_PeerIdentity id;
+ struct MeshClient *c;
+ struct MeshTunnel *t;
+ MESH_TunnelNumber tid;
+ size_t size;
+
+ /* Sanity check for client registration */
+ if (NULL == (c = client_get (client)))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+ data_msg = (struct GNUNET_MESH_ToOrigin *) message;
+ /* Sanity check for message size */
+ size = ntohs (message->size);
+ if (sizeof (struct GNUNET_MESH_ToOrigin) +
+ sizeof (struct GNUNET_MessageHeader) > size)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ /* Tunnel exists? */
+ tid = ntohl (data_msg->tid);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Got a ToOrigin request from a client! Tunnel %X\n", tid);
+ if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+ t = tunnel_get_by_local_id (c, tid);
+ if (NULL == t)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ /* It should be sent by someone who has this as incoming tunnel. */
+ if (-1 == client_knows_tunnel (c, t))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+ GNUNET_PEER_resolve (t->id.oid, &id);
+
+ /* Ok, everything is correct, send the message
+ * (pretend we got it from a mesh peer)
+ */
+ {
+ char buf[ntohs (message->size)];
+ struct GNUNET_MESH_ToOrigin *copy;
+
+ /* Work around const limitation */
+ copy = (struct GNUNET_MESH_ToOrigin *) buf;
+ memcpy (buf, data_msg, size);
+ copy->oid = id;
+ copy->tid = htonl (t->id.tid);
+ copy->sender = my_full_id;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " calling generic handler...\n");
+ handle_mesh_data_to_orig (NULL, &my_full_id, &copy->header, NULL, 0);
+ }
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ return;
+}
+
+
+/**
+ * Handler for client traffic directed to all peers in a tunnel
+ *
+ * @param cls closure
+ * @param client identification of the client
+ * @param message the actual message
+ */
+static void
+handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *message)
+{
+ struct MeshClient *c;
+ struct MeshTunnel *t;
+ struct GNUNET_MESH_Multicast *data_msg;
+ MESH_TunnelNumber tid;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Got a multicast request from a client!\n");
+
+ /* Sanity check for client registration */
+ if (NULL == (c = client_get (client)))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+ data_msg = (struct GNUNET_MESH_Multicast *) message;
+ /* Sanity check for message size */
+ if (sizeof (struct GNUNET_MESH_Multicast) +
+ sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ /* Tunnel exists? */
+ tid = ntohl (data_msg->tid);
+ t = tunnel_get_by_local_id (c, tid);
+ if (NULL == t)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ /* Does client own tunnel? */
+ if (t->owner->handle != client)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ {
+ char buf[ntohs (message->size)];
+ struct GNUNET_MESH_Multicast *copy;
+
+ copy = (struct GNUNET_MESH_Multicast *) buf;
+ memcpy (buf, message, ntohs (message->size));
+ copy->oid = my_full_id;
+ copy->tid = htonl (t->id.tid);
+ copy->ttl = htonl (DEFAULT_TTL);
+ copy->mid = htonl (t->mid + 1);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " calling generic handler...\n");
+ handle_mesh_data_multicast (client, &my_full_id, &copy->header, NULL, 0);
+ }
+
+ /* receive done gets called when last copy is sent to a neighbor */
+ return;
+}
+
+/**
+ * Functions to handle messages from clients
+ */
+static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
+ {&handle_local_new_client, NULL,
+ GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
+ {&handle_local_tunnel_create, NULL,
+ GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
+ sizeof (struct GNUNET_MESH_TunnelMessage)},
+ {&handle_local_tunnel_destroy, NULL,
+ GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
+ sizeof (struct GNUNET_MESH_TunnelMessage)},
+ {&handle_local_connect_add, NULL,
+ GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
+ sizeof (struct GNUNET_MESH_PeerControl)},
+ {&handle_local_connect_del, NULL,
+ GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
+ sizeof (struct GNUNET_MESH_PeerControl)},
+ {&handle_local_connect_by_type, NULL,
+ GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
+ sizeof (struct GNUNET_MESH_ConnectPeerByType)},
+ {&handle_local_unicast, NULL,
+ GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
+ {&handle_local_to_origin, NULL,
+ GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
+ {&handle_local_multicast, NULL,
+ GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
+ {NULL, NULL, 0, 0}
+};
+
+
+/**
+ * To be called on core init/fail.
+ *
+ * @param cls service closure
+ * @param server handle to the server for this service
+ * @param identity the public identity of this peer
+ */
+static void
+core_init (void *cls, struct GNUNET_CORE_Handle *server,
+ const struct GNUNET_PeerIdentity *identity)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
+ core_handle = server;
+ if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
+ NULL == server)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
+ GNUNET_SCHEDULER_shutdown ();
+ }
+ return;
+}
+
+/**
+ * Method called whenever a given peer connects.
+ *
+ * @param cls closure
+ * @param peer peer identity this notification is about
+ * @param atsi performance data for the connection
+ * @param atsi_count number of records in 'atsi'
+ */
+static void
+core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_ATS_Information *atsi,
+ unsigned int atsi_count)
+{
+ struct MeshPeerInfo *peer_info;
+ struct MeshPeerPath *path;
+
+ DEBUG_CONN ("Peer connected\n");
+ DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
+ peer_info = peer_info_get (peer);
+ if (myid == peer_info->id)
+ {
+ DEBUG_CONN (" (self)\n");
+ return;
+ }
+ else
+ {
+ DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
+ }
+ path = path_new (2);
+ path->peers[0] = myid;
+ path->peers[1] = peer_info->id;
+ GNUNET_PEER_change_rc (myid, 1);
+ GNUNET_PEER_change_rc (peer_info->id, 1);
+ peer_info_add_path (peer_info, path, GNUNET_YES);
+ return;
+}
+
+/**
+ * Method called whenever a peer disconnects.
+ *
+ * @param cls closure
+ * @param peer peer identity this notification is about
+ */
+static void
+core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
+{
+ struct MeshPeerInfo *pi;
+ unsigned int i;
+
+ DEBUG_CONN ("Peer disconnected\n");
+ pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
+ if (NULL == pi)
+ {
+ GNUNET_break (0);
+ return;
+ }
+ for (i = 0; i < CORE_QUEUE_SIZE; i++)
+ {
+ /* TODO: notify that the transmission failed */
+ peer_info_cancel_transmission (pi, i);
+ }
+ peer_info_remove_path (pi, pi->id, myid);
+ if (myid == pi->id)
+ {
+ DEBUG_CONN (" (self)\n");
+ }
+ return;
+}
+
+
+/******************************************************************************/
+/************************ MAIN FUNCTIONS ****************************/
+/******************************************************************************/
+
+/**
+ * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
+ *
+ * @param cls closure
+ * @param key current key code
+ * @param value value in the hash map
+ * @return GNUNET_YES if we should continue to iterate,
+ * GNUNET_NO if not.
+ */
+static int
+shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
+{
+ struct MeshTunnel *t = value;
+
+ tunnel_destroy (t);
+ return GNUNET_YES;
+}
+
+/**
+ * Iterator over peer hash map entries to destroy the tunnel during shutdown.
+ *
+ * @param cls closure
+ * @param key current key code
+ * @param value value in the hash map
+ * @return GNUNET_YES if we should continue to iterate,
+ * GNUNET_NO if not.
+ */
+static int
+shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
+{
+ struct MeshPeerInfo *p = value;
+
+ peer_info_destroy (p);
+ return GNUNET_YES;
+}
+
+/**
+ * Task run during shutdown.
+ *
+ * @param cls unused
+ * @param tc unused
+ */
+static void
+shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
+
+ if (core_handle != NULL)
+ {
+ GNUNET_CORE_disconnect (core_handle);
+ core_handle = NULL;
+ }
+ GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
+ GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
+ if (dht_handle != NULL)
+ {
+ GNUNET_DHT_disconnect (dht_handle);
+ dht_handle = NULL;
+ }
+ if (nc != NULL)
+ {
+ GNUNET_SERVER_notification_context_destroy (nc);
+ nc = NULL;
+ }
+ if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
+ {
+ GNUNET_SCHEDULER_cancel (announce_id_task);
+ announce_id_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
+}
+
+/**
+ * Process mesh requests.
+ *
+ * @param cls closure
+ * @param server the initialized server
+ * @param c configuration to use
+ */
+static void
+run (void *cls, struct GNUNET_SERVER_Handle *server,
+ const struct GNUNET_CONFIGURATION_Handle *c)
+{
+ struct MeshPeerInfo *peer;
+ struct MeshPeerPath *p;
+ char *keyfile;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
+ server_handle = server;
+ core_handle = GNUNET_CORE_connect (c, /* Main configuration */
+ CORE_QUEUE_SIZE, /* queue size */
+ NULL, /* Closure passed to MESH functions */
+ &core_init, /* Call core_init once connected */
+ &core_connect, /* Handle connects */
+ &core_disconnect, /* remove peers on disconnects */
+ NULL, /* Don't notify about all incoming messages */
+ GNUNET_NO, /* For header only in notification */
+ NULL, /* Don't notify about all outbound messages */
+ GNUNET_NO, /* For header-only out notification */
+ core_handlers); /* Register these handlers */
+
+ if (core_handle == NULL)
+ {
+ GNUNET_break (0);
+ GNUNET_SCHEDULER_shutdown ();
+ return;
+ }
+
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
+ &keyfile))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ _
+ ("Mesh service is lacking key configuration settings. Exiting.\n"));
+ GNUNET_SCHEDULER_shutdown ();
+ return;
+ }
+ my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
+ GNUNET_free (keyfile);
+ if (my_private_key == NULL)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ _("Mesh service could not access hostkey. Exiting.\n"));
+ GNUNET_SCHEDULER_shutdown ();
+ return;
+ }
+ GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
+ GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
+ &my_full_id.hashPubKey);
+ myid = GNUNET_PEER_intern (&my_full_id);
+
+// // transport_handle = GNUNET_TRANSPORT_connect(c,
+// // &my_full_id,
+// // NULL,
+// // NULL,
+// // NULL,
+// // NULL);
+
+ dht_handle = GNUNET_DHT_connect (c, 64);
+ if (dht_handle == NULL)
+ {
+ GNUNET_break (0);
+ }
+
+ next_tid = 0;
+ next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
+
+ tunnels = GNUNET_CONTAINER_multihashmap_create (32);
+ incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
+ peers = GNUNET_CONTAINER_multihashmap_create (32);
+ applications = GNUNET_CONTAINER_multihashmap_create (32);
+ types = GNUNET_CONTAINER_multihashmap_create (32);
+
+ GNUNET_SERVER_add_handlers (server_handle, client_handlers);
+ nc = GNUNET_SERVER_notification_context_create (server_handle,
+ LOCAL_QUEUE_SIZE);
+ GNUNET_SERVER_disconnect_notify (server_handle,
+ &handle_local_client_disconnect, NULL);
+
+
+ clients = NULL;
+ clients_tail = NULL;
+ next_client_id = 0;
+
+ announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
+ announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
+
+ /* Create a peer_info for the local peer */
+ peer = peer_info_get (&my_full_id);
+ p = path_new (1);
+ p->peers[0] = myid;
+ GNUNET_PEER_change_rc (myid, 1);
+ peer_info_add_path (peer, p, GNUNET_YES);
+
+ /* Scheduled the task to clean up when shutdown is called */
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
+ NULL);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
+}
+
+/**
+ * The main function for the mesh service.
+ *
+ * @param argc number of arguments from the command line
+ * @param argv command line arguments
+ * @return 0 ok, 1 on error
+ */
+int
+main (int argc, char *const *argv)
+{
+ int ret;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
+ ret =
+ (GNUNET_OK ==
+ GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
+ NULL)) ? 0 : 1;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
+
+ return ret;
+}
diff --git a/src/mesh/mesh.conf.in b/src/mesh/mesh.conf.in
new file mode 100644
index 0000000..83a8938
--- /dev/null
+++ b/src/mesh/mesh.conf.in
@@ -0,0 +1,13 @@
+[mesh]
+AUTOSTART = YES
+@UNIXONLY@ PORT = 2096
+HOSTNAME = localhost
+HOME = $SERVICEHOME
+CONFIG = $DEFAULTCONFIG
+BINARY = gnunet-service-mesh
+ACCEPT_FROM = 127.0.0.1;
+ACCEPT_FROM6 = ::1;
+UNIXPATH = /tmp/gnunet-service-mesh.sock
+UNIX_MATCH_UID = YES
+UNIX_MATCH_GID = YES
+
diff --git a/src/mesh/mesh.h b/src/mesh/mesh.h
new file mode 100644
index 0000000..d8fc404
--- /dev/null
+++ b/src/mesh/mesh.h
@@ -0,0 +1,263 @@
+/*
+ This file is part of GNUnet.
+ (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 3, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @author Bartlomiej Polot
+ * @file mesh/mesh.h
+ */
+
+#ifndef MESH_H_
+#define MESH_H_
+#include <stdint.h>
+
+#define MESH_DEBUG GNUNET_YES
+
+
+#include "platform.h"
+#include "gnunet_common.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_peer_lib.h"
+#include "gnunet_core_service.h"
+#include "gnunet_protocols.h"
+#include <gnunet_mesh_service.h>
+
+/******************************************************************************/
+/******************** MESH LOCAL MESSAGES *************************/
+/******************************************************************************/
+/* Any API call should be documented in the folowing table under API CALL.
+ * Also, any message type should be documented in the following table, with the
+ * associated event.
+ *
+ * API CALL (GNUNET_MESH_*) MESSAGE USED
+ * ------------------------ ------------
+ * connect GNUNET_MESH_ClientConnect
+ * disconnect None (network level disconnect)
+ *
+ * tunnel_create GNUNET_MESH_TunnelMessage
+ * tunnel_destroy GNUNET_MESH_TunnelMessage
+ *
+ * peer_request_connect_add GNUNET_MESH_PeerControl
+ * peer_request_connect_del GNUNET_MESH_PeerControl
+ * peer_request_connect_by_type GNUNET_MESH_ConnectPeerByType
+ *
+ * notify_transmit_ready *GNUNET_MESH_TransmitReady?*
+ * notify_transmit_ready_cancel None (clear of internal data structures)
+ *
+ *
+ *
+ * EVENT MESSAGE USED
+ * ----- ------------
+ * data GNUNET_MESH_Data OR
+ * GNUNET_MESH_DataBroadcast
+ * new incoming tunnel GNUNET_MESH_PeerControl
+ * peer connects to a tunnel GNUNET_MESH_PeerControl
+ * peer disconnects from a tunnel GNUNET_MESH_PeerControl
+ */
+
+/******************************************************************************/
+/************************** CONSTANTS ******************************/
+/******************************************************************************/
+
+#define GNUNET_MESH_LOCAL_TUNNEL_ID_CLI 0x80000000
+#define GNUNET_MESH_LOCAL_TUNNEL_ID_SERV 0xB0000000
+
+#define CORE_QUEUE_SIZE 10
+#define LOCAL_QUEUE_SIZE 100
+
+/******************************************************************************/
+/************************** MESSAGES ******************************/
+/******************************************************************************/
+
+GNUNET_NETWORK_STRUCT_BEGIN
+
+/**
+ * Message for a client to register to the service
+ */
+struct GNUNET_MESH_ClientConnect
+{
+ /**
+ * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT
+ *
+ * Size: sizeof(struct GNUNET_MESH_ClientConnect) +
+ * sizeof(MESH_ApplicationType) * applications +
+ * sizeof(uint16_t) * types
+ */
+ struct GNUNET_MessageHeader header;
+ uint16_t applications GNUNET_PACKED;
+ uint16_t types GNUNET_PACKED;
+ /* uint16_t list_apps[applications] */
+ /* uint16_t list_types[types] */
+};
+
+
+/**
+ * Type for tunnel numbering.
+ * - Local tunnel numbers given by the service (incoming) are >= 0xB0000000
+ * - Local tunnel numbers given by the client (created) are >= 0x80000000
+ * - Global tunnel numbers are < 0x80000000
+ */
+typedef uint32_t MESH_TunnelNumber;
+
+/**
+ * Message for a client to create and destroy tunnels.
+ */
+struct GNUNET_MESH_TunnelMessage
+{
+ /**
+ * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_[CREATE|DESTROY]
+ *
+ * Size: sizeof(struct GNUNET_MESH_TunnelMessage)
+ */
+ struct GNUNET_MessageHeader header;
+
+ /**
+ * ID of a tunnel controlled by this client.
+ */
+ MESH_TunnelNumber tunnel_id GNUNET_PACKED;
+};
+
+
+/**
+ * Message for the service to let a client know about created tunnels.
+ */
+struct GNUNET_MESH_TunnelNotification
+{
+ /**
+ * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE
+ *
+ * Size: sizeof(struct GNUNET_MESH_TunnelMessage)
+ */
+ struct GNUNET_MessageHeader header;
+
+ /**
+ * ID of a tunnel controlled by this client.
+ */
+ MESH_TunnelNumber tunnel_id GNUNET_PACKED;
+
+ /**
+ * Peer at the other end, if any
+ */
+ struct GNUNET_PeerIdentity peer;
+};
+
+/**
+ * Message for:
+ * - request adding and deleting peers from a tunnel
+ * - notify the client that peers have connected:
+ * -- requested
+ * -- unrequested (new incoming tunnels)
+ * - notify the client that peers have disconnected
+ */
+struct GNUNET_MESH_PeerControl
+{
+
+ /**
+ * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_[ADD|DEL]
+ * (client to service, client created tunnel)
+ * GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_[CONNECTED|DISCONNECTED]
+ * (service to client)
+ *
+ * Size: sizeof(struct GNUNET_MESH_PeerControl)
+ */
+ struct GNUNET_MessageHeader header;
+
+ /**
+ * ID of a tunnel controlled by this client.
+ */
+ MESH_TunnelNumber tunnel_id GNUNET_PACKED;
+
+ /**
+ * Peer to connect/disconnect.
+ */
+ struct GNUNET_PeerIdentity peer;
+};
+
+
+/**
+ * Message for connecting to peers offering a certain service.
+ */
+struct GNUNET_MESH_ConnectPeerByType
+{
+ /**
+ * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE |
+ * GNUNET_MESSAGE_TYPE_MESH_LOCAL_DISCONNECT_PEER_BY_TYPE
+ */
+ struct GNUNET_MessageHeader header;
+
+ /**
+ * ID of a tunnel controlled by this client.
+ */
+ MESH_TunnelNumber tunnel_id GNUNET_PACKED;
+
+ /**
+ * Type specification
+ */
+ GNUNET_MESH_ApplicationType type GNUNET_PACKED;
+};
+GNUNET_NETWORK_STRUCT_END
+
+/******************************************************************************/
+/************************ ENUMERATIONS ****************************/
+/******************************************************************************/
+
+/**
+ * All the states a peer participating in a tunnel can be in.
+ */
+enum MeshPeerState
+{
+ /**
+ * Uninitialized status, should never appear in operation.
+ */
+ MESH_PEER_INVALID,
+
+ /**
+ * Peer is the root and owner of the tree
+ */
+ MESH_PEER_ROOT,
+
+ /**
+ * Peer only retransmits traffic, is not a final destination
+ */
+ MESH_PEER_RELAY,
+
+ /**
+ * Path to the peer not known yet
+ */
+ MESH_PEER_SEARCHING,
+
+ /**
+ * Request sent, not yet answered.
+ */
+ MESH_PEER_WAITING,
+
+ /**
+ * Peer connected and ready to accept data
+ */
+ MESH_PEER_READY,
+
+ /**
+ * Peer connected previosly but not responding
+ */
+ MESH_PEER_RECONNECTING
+};
+
+
+
+#endif
diff --git a/src/mesh/mesh_api.c b/src/mesh/mesh_api.c
new file mode 100644
index 0000000..7b9aa06
--- /dev/null
+++ b/src/mesh/mesh_api.c
@@ -0,0 +1,1713 @@
+/*
+ This file is part of GNUnet.
+ (C) 2011 Christian Grothoff (and other contributing authors)
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 3, or (at your
+ option) any later version.
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file mesh/mesh_api.c
+ * @brief mesh api: client implementation of mesh service
+ * @author Bartlomiej Polot
+ *
+ * STRUCTURE:
+ * - CONSTANTS
+ * - DATA STRUCTURES
+ * - AUXILIARY FUNCTIONS
+ * - RECEIVE HANDLERS
+ * - SEND FUNCTIONS
+ * - API CALL DEFINITIONS
+ */
+#ifdef __cplusplus
+extern "C"
+{
+#if 0 /* keep Emacsens' auto-indent happy */
+}
+#endif
+#endif
+
+#include "platform.h"
+#include "gnunet_common.h"
+#include "gnunet_client_lib.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_peer_lib.h"
+#include "gnunet_mesh_service.h"
+#include "mesh.h"
+#include "mesh_protocol.h"
+
+#define MESH_API_DEBUG GNUNET_YES
+
+#if MESH_API_DEBUG
+#define LOG(kind,...) GNUNET_log_from (kind, "mesh-api",__VA_ARGS__)
+#else
+#define LOG(kind,...)
+#endif
+
+/******************************************************************************/
+/************************ DATA STRUCTURES ****************************/
+/******************************************************************************/
+
+/**
+ * Transmission queue to the service
+ */
+struct GNUNET_MESH_TransmitHandle
+{
+
+ /**
+ * Double Linked list
+ */
+ struct GNUNET_MESH_TransmitHandle *next;
+
+ /**
+ * Double Linked list
+ */
+ struct GNUNET_MESH_TransmitHandle *prev;
+
+ /**
+ * Tunnel this message is sent on / for (may be NULL for control messages).
+ */
+ struct GNUNET_MESH_Tunnel *tunnel;
+
+ /**
+ * Callback to obtain the message to transmit, or NULL if we
+ * got the message in 'data'. Notice that messages built
+ * by 'notify' need to be encapsulated with information about
+ * the 'target'.
+ */
+ GNUNET_CONNECTION_TransmitReadyNotify notify;
+
+ /**
+ * Closure for 'notify'
+ */
+ void *notify_cls;
+
+ /**
+ * How long is this message valid. Once the timeout has been
+ * reached, the message must no longer be sent. If this
+ * is a message with a 'notify' callback set, the 'notify'
+ * function should be called with 'buf' NULL and size 0.
+ */
+ struct GNUNET_TIME_Absolute timeout;
+
+ /**
+ * Task triggering a timeout, can be NO_TASK if the timeout is FOREVER.
+ */
+ GNUNET_SCHEDULER_TaskIdentifier timeout_task;
+
+ /**
+ * Priority of the message. The queue is sorted by priority,
+ * control messages have the maximum priority (UINT32_MAX).
+ */
+ uint32_t priority;
+
+ /**
+ * Target of the message, 0 for multicast. This field
+ * is only valid if 'notify' is non-NULL.
+ */
+ GNUNET_PEER_Id target;
+
+ /**
+ * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
+ */
+ size_t size;
+};
+
+
+/**
+ * Opaque handle to the service.
+ */
+struct GNUNET_MESH_Handle
+{
+
+ /**
+ * Handle to the server connection, to send messages later
+ */
+ struct GNUNET_CLIENT_Connection *client;
+
+ /**
+ * Set of handlers used for processing incoming messages in the tunnels
+ */
+ const struct GNUNET_MESH_MessageHandler *message_handlers;
+
+ /**
+ * Set of applications that should be claimed to be offered at this node.
+ * Note that this is just informative, the appropiate handlers must be
+ * registered independently and the mapping is up to the developer of the
+ * client application.
+ */
+ const GNUNET_MESH_ApplicationType *applications;
+
+ /**
+ * Double linked list of the tunnels this client is connected to.
+ */
+ struct GNUNET_MESH_Tunnel *tunnels_head;
+ struct GNUNET_MESH_Tunnel *tunnels_tail;
+
+ /**
+ * Callback for inbound tunnel creation
+ */
+ GNUNET_MESH_InboundTunnelNotificationHandler *new_tunnel;
+
+ /**
+ * Callback for inbound tunnel disconnection
+ */
+ GNUNET_MESH_TunnelEndHandler *cleaner;
+
+ /**
+ * Handle to cancel pending transmissions in case of disconnection
+ */
+ struct GNUNET_CLIENT_TransmitHandle *th;
+
+ /**
+ * Closure for all the handlers given by the client
+ */
+ void *cls;
+
+ /**
+ * Messages to send to the service
+ */
+ struct GNUNET_MESH_TransmitHandle *th_head;
+ struct GNUNET_MESH_TransmitHandle *th_tail;
+
+ /**
+ * tid of the next tunnel to create (to avoid reusing IDs often)
+ */
+ MESH_TunnelNumber next_tid;
+ unsigned int n_handlers;
+ unsigned int n_applications;
+ unsigned int max_queue_size;
+
+ /**
+ * Have we started the task to receive messages from the service
+ * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
+ */
+ int in_receive;
+
+ /**
+ * Number of packets queued
+ */
+ unsigned int npackets;
+
+ /**
+ * Configuration given by the client, in case of reconnection
+ */
+ const struct GNUNET_CONFIGURATION_Handle *cfg;
+
+ /**
+ * Time to the next reconnect in case one reconnect fails
+ */
+ struct GNUNET_TIME_Relative reconnect_time;
+
+ /**
+ * Task for trying to reconnect.
+ */
+ GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
+};
+
+
+/**
+ * Description of a peer
+ */
+struct GNUNET_MESH_Peer
+{
+ /**
+ * ID of the peer in short form
+ */
+ GNUNET_PEER_Id id;
+
+ /**
+ * Tunnel this peer belongs to
+ */
+ struct GNUNET_MESH_Tunnel *t;
+
+ /**
+ * Flag indicating whether service has informed about its connection
+ */
+ int connected;
+
+};
+
+
+/**
+ * Opaque handle to a tunnel.
+ */
+struct GNUNET_MESH_Tunnel
+{
+
+ /**
+ * DLL
+ */
+ struct GNUNET_MESH_Tunnel *next;
+ struct GNUNET_MESH_Tunnel *prev;
+
+ /**
+ * Callback to execute when peers connect to the tunnel
+ */
+ GNUNET_MESH_PeerConnectHandler connect_handler;
+
+ /**
+ * Callback to execute when peers disconnect from the tunnel
+ */
+ GNUNET_MESH_PeerDisconnectHandler disconnect_handler;
+
+ /**
+ * Closure for the connect/disconnect handlers
+ */
+ void *cls;
+
+ /**
+ * Handle to the mesh this tunnel belongs to
+ */
+ struct GNUNET_MESH_Handle *mesh;
+
+ /**
+ * Local ID of the tunnel
+ */
+ MESH_TunnelNumber tid;
+
+ /**
+ * Owner of the tunnel. 0 if the tunnel is the local client.
+ */
+ GNUNET_PEER_Id owner;
+
+ /**
+ * All peers added to the tunnel
+ */
+ struct GNUNET_MESH_Peer **peers;
+
+ /**
+ * List of application types that have been requested for this tunnel
+ */
+ GNUNET_MESH_ApplicationType *apps;
+
+ /**
+ * Any data the caller wants to put in here
+ */
+ void *ctx;
+
+ /**
+ * Number of peers added to the tunnel
+ */
+ unsigned int npeers;
+
+ /**
+ * Number of packets queued in this tunnel
+ */
+ unsigned int npackets;
+
+ /**
+ * Number of applications requested this tunnel
+ */
+ unsigned int napps;
+
+};
+
+
+/******************************************************************************/
+/*********************** AUXILIARY FUNCTIONS *************************/
+/******************************************************************************/
+
+/**
+ * Get the tunnel handler for the tunnel specified by id from the given handle
+ * @param h Mesh handle
+ * @param tid ID of the wanted tunnel
+ * @return handle to the required tunnel or NULL if not found
+ */
+static struct GNUNET_MESH_Tunnel *
+retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
+{
+ struct GNUNET_MESH_Tunnel *t;
+
+ t = h->tunnels_head;
+ while (t != NULL)
+ {
+ if (t->tid == tid)
+ return t;
+ t = t->next;
+ }
+ return NULL;
+}
+
+
+/**
+ * Create a new tunnel and insert it in the tunnel list of the mesh handle
+ * @param h Mesh handle
+ * @param tid desired tid of the tunnel, 0 to assign one automatically
+ * @return handle to the created tunnel
+ */
+static struct GNUNET_MESH_Tunnel *
+create_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
+{
+ struct GNUNET_MESH_Tunnel *t;
+
+ t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
+ GNUNET_CONTAINER_DLL_insert (h->tunnels_head, h->tunnels_tail, t);
+ t->mesh = h;
+ if (0 == tid)
+ {
+ t->tid = h->next_tid;
+ while (NULL != retrieve_tunnel (h, h->next_tid))
+ {
+ h->next_tid++;
+ h->next_tid &= ~GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
+ h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
+ }
+ }
+ else
+ {
+ t->tid = tid;
+ }
+ return t;
+}
+
+
+/**
+ * Destroy the specified tunnel.
+ * - Destroys all peers, calling the disconnect callback on each if needed
+ * - Cancels all outgoing traffic for that tunnel, calling respective notifys
+ * - Calls cleaner if tunnel was inbound
+ * - Frees all memory used
+ *
+ * @param t Pointer to the tunnel.
+ * @param call_cleaner Whether to call the cleaner handler.
+ *
+ * @return Handle to the required tunnel or NULL if not found.
+ */
+static void
+destroy_tunnel (struct GNUNET_MESH_Tunnel *t, int call_cleaner)
+{
+ struct GNUNET_MESH_Handle *h;
+ struct GNUNET_PeerIdentity pi;
+ struct GNUNET_MESH_TransmitHandle *th;
+ struct GNUNET_MESH_TransmitHandle *next;
+ unsigned int i;
+
+ if (NULL == t)
+ {
+ GNUNET_break (0);
+ return;
+ }
+ h = t->mesh;
+
+ /* disconnect all peers */
+ GNUNET_CONTAINER_DLL_remove (h->tunnels_head, h->tunnels_tail, t);
+ for (i = 0; i < t->npeers; i++)
+ {
+ if ( (NULL != t->disconnect_handler) && t->peers[i]->connected)
+ {
+ GNUNET_PEER_resolve (t->peers[i]->id, &pi);
+ t->disconnect_handler (t->cls, &pi);
+ }
+ GNUNET_PEER_change_rc (t->peers[i]->id, -1);
+ GNUNET_free (t->peers[i]);
+ }
+
+ /* signal tunnel destruction */
+ if ( (NULL != h->cleaner) && (0 != t->owner) && (GNUNET_YES == call_cleaner) )
+ h->cleaner (h->cls, t, t->ctx);
+
+ /* check that clients did not leave messages behind in the queue */
+ for (th = h->th_head; NULL != th; th = next)
+ {
+ next = th->next;
+ if (th->tunnel != t)
+ continue;
+ /* Clients should have aborted their requests already.
+ * Management traffic should be ok, as clients can't cancel that */
+ GNUNET_break (NULL == th->notify);
+ GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
+
+ /* clean up request */
+ if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
+ GNUNET_SCHEDULER_cancel (th->timeout_task);
+ GNUNET_free (th);
+ }
+
+ /* if there are no more pending requests with mesh service, cancel active request */
+ /* Note: this should be unnecessary... */
+ if ( (NULL == h->th_head) && (NULL != h->th))
+ {
+ GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
+ h->th = NULL;
+ }
+
+
+ if (t->npeers > 0)
+ GNUNET_free (t->peers);
+ if (0 != t->owner)
+ GNUNET_PEER_change_rc (t->owner, -1);
+ if (0 != t->napps && t->apps)
+ GNUNET_free (t->apps);
+ GNUNET_free (t);
+ return;
+}
+
+
+/**
+ * Get the peer descriptor for the peer with id from the given tunnel
+ * @param t Tunnel handle
+ * @param id Short form ID of the wanted peer
+ * @return handle to the requested peer or NULL if not found
+ */
+static struct GNUNET_MESH_Peer *
+retrieve_peer (struct GNUNET_MESH_Tunnel *t, GNUNET_PEER_Id id)
+{
+ unsigned int i;
+
+ for (i = 0; i < t->npeers; i++)
+ if (t->peers[i]->id == id)
+ return t->peers[i];
+ return NULL;
+}
+
+
+/**
+ * Add a peer into a tunnel
+ * @param t Tunnel handle
+ * @param pi Full ID of the new peer
+ * @return handle to the newly created peer
+ */
+static struct GNUNET_MESH_Peer *
+add_peer_to_tunnel (struct GNUNET_MESH_Tunnel *t,
+ const struct GNUNET_PeerIdentity *pi)
+{
+ struct GNUNET_MESH_Peer *p;
+ GNUNET_PEER_Id id;
+
+ if (0 != t->owner)
+ {
+ GNUNET_break (0);
+ return NULL;
+ }
+ id = GNUNET_PEER_intern (pi);
+
+ p = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer));
+ p->id = id;
+ p->t = t;
+ GNUNET_array_append (t->peers, t->npeers, p);
+ return p;
+}
+
+
+/**
+ * Remove a peer from a tunnel
+ * @param p Peer handle
+ */
+static void
+remove_peer_from_tunnel (struct GNUNET_MESH_Peer *p)
+{
+ unsigned int i;
+
+ for (i = 0; i < p->t->npeers; i++)
+ {
+ if (p->t->peers[i] == p)
+ break;
+ }
+ if (i == p->t->npeers)
+ {
+ GNUNET_break (0);
+ return;
+ }
+ p->t->peers[i] = p->t->peers[p->t->npeers - 1];
+ GNUNET_array_grow (p->t->peers, p->t->npeers, p->t->npeers - 1);
+}
+
+
+/**
+ * Notify client that the transmission has timed out
+ * @param cls closure
+ * @param tc task context
+ */
+static void
+timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct GNUNET_MESH_TransmitHandle *th = cls;
+ struct GNUNET_MESH_Handle *mesh;
+
+ mesh = th->tunnel->mesh;
+ GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
+ if (th->notify != NULL)
+ th->notify (th->notify_cls, 0, NULL);
+ GNUNET_free (th);
+ if ((NULL == mesh->th_head) && (NULL != mesh->th))
+ {
+ /* queue empty, no point in asking for transmission */
+ GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
+ mesh->th = NULL;
+ }
+}
+
+
+/**
+ * Add a transmit handle to the transmission queue by priority and set the
+ * timeout if needed.
+ *
+ * @param h mesh handle with the queue head and tail
+ * @param th handle to the packet to be transmitted
+ */
+static void
+add_to_queue (struct GNUNET_MESH_Handle *h,
+ struct GNUNET_MESH_TransmitHandle *th)
+{
+ struct GNUNET_MESH_TransmitHandle *p;
+
+ p = h->th_head;
+ while ((NULL != p) && (th->priority <= p->priority))
+ p = p->next;
+ if (NULL == p)
+ p = h->th_tail;
+ else
+ p = p->prev;
+ GNUNET_CONTAINER_DLL_insert_after (h->th_head, h->th_tail, p, th);
+ if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value == th->timeout.abs_value)
+ return;
+ th->timeout_task =
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
+ (th->timeout), &timeout_transmission, th);
+}
+
+
+/**
+ * Auxiliary function to send an already constructed packet to the service.
+ * Takes care of creating a new queue element, copying the message and
+ * calling the tmt_rdy function if necessary.
+ *
+ * @param h mesh handle
+ * @param msg message to transmit
+ * @param tunnel tunnel this send is related to (NULL if N/A)
+ */
+static void
+send_packet (struct GNUNET_MESH_Handle *h,
+ const struct GNUNET_MessageHeader *msg,
+ struct GNUNET_MESH_Tunnel *tunnel);
+
+
+/**
+ * Reconnect callback: tries to reconnect again after a failer previous
+ * reconnecttion
+ * @param cls closure (mesh handle)
+ * @param tc task context
+ */
+static void
+reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+
+
+/**
+ * Send a connect packet to the service with the applications and types
+ * requested by the user.
+ *
+ * @param h The mesh handle.
+ *
+ */
+static void
+send_connect (struct GNUNET_MESH_Handle *h)
+{
+ size_t size;
+
+ size = sizeof (struct GNUNET_MESH_ClientConnect);
+ size += h->n_applications * sizeof (GNUNET_MESH_ApplicationType);
+ size += h->n_handlers * sizeof (uint16_t);
+ {
+ char buf[size];
+ struct GNUNET_MESH_ClientConnect *msg;
+ GNUNET_MESH_ApplicationType *apps;
+ uint16_t napps;
+ uint16_t *types;
+ uint16_t ntypes;
+
+ /* build connection packet */
+ msg = (struct GNUNET_MESH_ClientConnect *) buf;
+ msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
+ msg->header.size = htons (size);
+ apps = (GNUNET_MESH_ApplicationType *) &msg[1];
+ for (napps = 0; napps < h->n_applications; napps++)
+ {
+ apps[napps] = htonl (h->applications[napps]);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " app %u\n", h->applications[napps]);
+ }
+ types = (uint16_t *) & apps[napps];
+ for (ntypes = 0; ntypes < h->n_handlers; ntypes++)
+ types[ntypes] = htons (h->message_handlers[ntypes].type);
+ msg->applications = htons (napps);
+ msg->types = htons (ntypes);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Sending %lu bytes long message %d types and %d apps\n",
+ ntohs (msg->header.size), ntypes, napps);
+ send_packet (h, &msg->header, NULL);
+ }
+}
+
+
+/**
+ * Reconnect to the service, retransmit all infomation to try to restore the
+ * original state.
+ *
+ * @param h handle to the mesh
+ *
+ * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
+ */
+static int
+do_reconnect (struct GNUNET_MESH_Handle *h)
+{
+ struct GNUNET_MESH_Tunnel *t;
+ unsigned int i;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
+
+ h->in_receive = GNUNET_NO;
+ /* disconnect */
+ if (NULL != h->th)
+ {
+ GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
+ h->th = NULL;
+ }
+ if (NULL != h->client)
+ {
+ GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
+ }
+
+ /* connect again */
+ h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
+ if (h->client == NULL)
+ {
+ h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
+ &reconnect_cbk, h);
+ h->reconnect_time =
+ GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
+ GNUNET_TIME_relative_multiply
+ (h->reconnect_time, 2));
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " Next retry in %sms\n",
+ GNUNET_TIME_relative_to_string (h->reconnect_time));
+ GNUNET_break (0);
+ return GNUNET_NO;
+ }
+ else
+ {
+ h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
+ }
+ send_connect (h);
+ /* Rebuild all tunnels */
+ for (t = h->tunnels_head; NULL != t; t = t->next)
+ {
+ struct GNUNET_MESH_TunnelMessage tmsg;
+ struct GNUNET_MESH_PeerControl pmsg;
+
+ if (t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
+ {
+ /* Tunnel was created by service (incoming tunnel) */
+ /* TODO: Notify service of missing tunnel, to request
+ * creator to recreate path (find a path to him via DHT?)
+ */
+ continue;
+ }
+ tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
+ tmsg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
+ tmsg.tunnel_id = htonl (t->tid);
+ send_packet (h, &tmsg.header, t);
+
+ pmsg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
+ pmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
+ pmsg.tunnel_id = htonl (t->tid);
+
+ /* Reconnect all peers */
+ for (i = 0; i < t->npeers; i++)
+ {
+ GNUNET_PEER_resolve (t->peers[i]->id, &pmsg.peer);
+ if (NULL != t->disconnect_handler && t->peers[i]->connected)
+ t->disconnect_handler (t->cls, &pmsg.peer);
+ /* If the tunnel was "by type", dont connect individual peers */
+ if (0 == t->napps)
+ send_packet (t->mesh, &pmsg.header, t);
+ }
+ /* Reconnect all types, if any */
+ for (i = 0; i < t->napps; i++)
+ {
+ struct GNUNET_MESH_ConnectPeerByType msg;
+
+ msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectPeerByType));
+ msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE);
+ msg.tunnel_id = htonl (t->tid);
+ msg.type = htonl (t->apps[i]);
+ send_packet (t->mesh, &msg.header, t);
+ }
+ }
+ return GNUNET_YES;
+}
+
+/**
+ * Reconnect callback: tries to reconnect again after a failer previous
+ * reconnecttion
+ * @param cls closure (mesh handle)
+ * @param tc task context
+ */
+static void
+reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct GNUNET_MESH_Handle *h = cls;
+
+ h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+ if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+ return;
+ do_reconnect (h);
+}
+
+
+/**
+ * Reconnect to the service, retransmit all infomation to try to restore the
+ * original state.
+ *
+ * @param h handle to the mesh
+ *
+ * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
+ */
+static void
+reconnect (struct GNUNET_MESH_Handle *h)
+{
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Requested RECONNECT\n");
+ if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
+ h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
+ &reconnect_cbk, h);
+}
+
+
+/******************************************************************************/
+/*********************** RECEIVE HANDLERS ****************************/
+/******************************************************************************/
+
+/**
+ * Process the new tunnel notification and add it to the tunnels in the handle
+ *
+ * @param h The mesh handle
+ * @param msg A message with the details of the new incoming tunnel
+ */
+static void
+process_tunnel_created (struct GNUNET_MESH_Handle *h,
+ const struct GNUNET_MESH_TunnelNotification *msg)
+{
+ struct GNUNET_MESH_Tunnel *t;
+ MESH_TunnelNumber tid;
+
+ tid = ntohl (msg->tunnel_id);
+ if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
+ {
+ GNUNET_break (0);
+ return;
+ }
+ t = create_tunnel (h, tid);
+ t->owner = GNUNET_PEER_intern (&msg->peer);
+ t->npeers = 1;
+ t->peers = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer *));
+ t->peers[0] = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer));
+ t->peers[0]->t = t;
+ t->peers[0]->connected = 1;
+ t->peers[0]->id = t->owner;
+ GNUNET_PEER_change_rc (t->owner, 1);
+ t->mesh = h;
+ t->tid = tid;
+ if (NULL != h->new_tunnel)
+ {
+ struct GNUNET_ATS_Information atsi;
+
+ atsi.type = 0;
+ atsi.value = 0;
+ t->ctx = h->new_tunnel (h->cls, t, &msg->peer, &atsi);
+ }
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "new incoming tunnel %X\n", t->tid);
+ return;
+}
+
+
+/**
+ * Process the tunnel destroy notification and free associated resources
+ *
+ * @param h The mesh handle
+ * @param msg A message with the details of the tunnel being destroyed
+ */
+static void
+process_tunnel_destroy (struct GNUNET_MESH_Handle *h,
+ const struct GNUNET_MESH_TunnelMessage *msg)
+{
+ struct GNUNET_MESH_Tunnel *t;
+ MESH_TunnelNumber tid;
+
+ tid = ntohl (msg->tunnel_id);
+ t = retrieve_tunnel (h, tid);
+
+ if (NULL == t)
+ {
+ return;
+ }
+ if (0 == t->owner)
+ {
+ GNUNET_break (0);
+ }
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %u destroyed\n", t->tid);
+ destroy_tunnel (t, GNUNET_YES);
+ return;
+}
+
+
+/**
+ * Process the new peer event and notify the upper level of it
+ *
+ * @param h The mesh handle
+ * @param msg A message with the details of the peer event
+ */
+static void
+process_peer_event (struct GNUNET_MESH_Handle *h,
+ const struct GNUNET_MESH_PeerControl *msg)
+{
+ struct GNUNET_MESH_Tunnel *t;
+ struct GNUNET_MESH_Peer *p;
+ struct GNUNET_ATS_Information atsi;
+ GNUNET_PEER_Id id;
+ uint16_t size;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "processig peer event\n");
+ size = ntohs (msg->header.size);
+ if (size != sizeof (struct GNUNET_MESH_PeerControl))
+ {
+ GNUNET_break (0);
+ return;
+ }
+ t = retrieve_tunnel (h, ntohl (msg->tunnel_id));
+ if (NULL == t)
+ {
+ GNUNET_break (0);
+ return;
+ }
+ id = GNUNET_PEER_search (&msg->peer);
+ if ((p = retrieve_peer (t, id)) == NULL)
+ p = add_peer_to_tunnel (t, &msg->peer);
+ if (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD == ntohs (msg->header.type))
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "adding peer\n");
+ if (NULL != t->connect_handler)
+ {
+ atsi.type = 0;
+ atsi.value = 0;
+ t->connect_handler (t->cls, &msg->peer, &atsi);
+ }
+ p->connected = 1;
+ }
+ else
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "removing peer\n");
+ if (NULL != t->disconnect_handler && p->connected)
+ {
+ t->disconnect_handler (t->cls, &msg->peer);
+ }
+ remove_peer_from_tunnel (p);
+ GNUNET_free (p);
+ }
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "processing peer event END\n");
+}
+
+
+/**
+ * Process the incoming data packets
+ *
+ * @param h The mesh handle
+ * @param message A message encapsulating the data
+ *
+ * @return GNUNET_YES if everything went fine
+ * GNUNET_NO if client closed connection (h no longer valid)
+ */
+static int
+process_incoming_data (struct GNUNET_MESH_Handle *h,
+ const struct GNUNET_MessageHeader *message)
+{
+ const struct GNUNET_MessageHeader *payload;
+ const struct GNUNET_MESH_MessageHandler *handler;
+ const struct GNUNET_PeerIdentity *peer;
+ struct GNUNET_MESH_Unicast *ucast;
+ struct GNUNET_MESH_Multicast *mcast;
+ struct GNUNET_MESH_ToOrigin *to_orig;
+ struct GNUNET_MESH_Tunnel *t;
+ unsigned int i;
+ uint16_t type;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
+ type = ntohs (message->type);
+ switch (type)
+ {
+ case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
+ ucast = (struct GNUNET_MESH_Unicast *) message;
+
+ t = retrieve_tunnel (h, ntohl (ucast->tid));
+ payload = (struct GNUNET_MessageHeader *) &ucast[1];
+ peer = &ucast->oid;
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " ucast on tunnel %s [%x]\n",
+ GNUNET_i2s (peer), ntohl (ucast->tid));
+ break;
+ case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
+ mcast = (struct GNUNET_MESH_Multicast *) message;
+ t = retrieve_tunnel (h, ntohl (mcast->tid));
+ payload = (struct GNUNET_MessageHeader *) &mcast[1];
+ peer = &mcast->oid;
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " mcast on tunnel %s [%x]\n",
+ GNUNET_i2s (peer), ntohl (mcast->tid));
+ break;
+ case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
+ to_orig = (struct GNUNET_MESH_ToOrigin *) message;
+ t = retrieve_tunnel (h, ntohl (to_orig->tid));
+ payload = (struct GNUNET_MessageHeader *) &to_orig[1];
+ peer = &to_orig->sender;
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " torig on tunnel %s [%x]\n",
+ GNUNET_i2s (peer), ntohl (to_orig->tid));
+ break;
+ default:
+ GNUNET_break (0);
+ return GNUNET_YES;
+ }
+ if (NULL == t)
+ {
+ GNUNET_break (0);
+ return GNUNET_YES;
+ }
+ type = ntohs (payload->type);
+ for (i = 0; i < h->n_handlers; i++)
+ {
+ handler = &h->message_handlers[i];
+ if (handler->type == type)
+ {
+ struct GNUNET_ATS_Information atsi;
+
+ atsi.type = 0;
+ atsi.value = 0;
+ if (GNUNET_OK !=
+ handler->callback (h->cls, t, &t->ctx, peer, payload, &atsi))
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH: callback caused disconnection\n");
+ GNUNET_MESH_disconnect (h);
+ return GNUNET_NO;
+ }
+ else
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "MESH: callback completed successfully\n");
+
+ }
+ }
+ }
+ return GNUNET_YES;
+}
+
+
+/**
+ * Function to process all messages received from the service
+ *
+ * @param cls closure
+ * @param msg message received, NULL on timeout or fatal error
+ */
+static void
+msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
+{
+ struct GNUNET_MESH_Handle *h = cls;
+
+ if (msg == NULL)
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Received NULL msg\n");
+ reconnect (h);
+ return;
+ }
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "received a message type %hu from MESH\n",
+ ntohs (msg->type));
+ switch (ntohs (msg->type))
+ {
+ /* Notify of a new incoming tunnel */
+ case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
+ process_tunnel_created (h, (struct GNUNET_MESH_TunnelNotification *) msg);
+ break;
+ /* Notify of a tunnel disconnection */
+ case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
+ process_tunnel_destroy (h, (struct GNUNET_MESH_TunnelMessage *) msg);
+ break;
+ /* Notify of a new peer or a peer disconnect in the tunnel */
+ case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD:
+ case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL:
+ process_peer_event (h, (struct GNUNET_MESH_PeerControl *) msg);
+ break;
+ /* Notify of a new data packet in the tunnel */
+ case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
+ case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
+ case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
+ if (GNUNET_NO == process_incoming_data (h, msg))
+ return;
+ break;
+ /* We shouldn't get any other packages, log and ignore */
+ default:
+ LOG (GNUNET_ERROR_TYPE_WARNING,
+ "MESH: unsolicited message form service (type %d)\n",
+ ntohs (msg->type));
+ }
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
+ GNUNET_CLIENT_receive (h->client, &msg_received, h,
+ GNUNET_TIME_UNIT_FOREVER_REL);
+}
+
+
+/******************************************************************************/
+/************************ SEND FUNCTIONS ****************************/
+/******************************************************************************/
+
+/**
+ * Function called to send a message to the service.
+ * "buf" will be NULL and "size" zero if the socket was closed for writing in
+ * the meantime.
+ *
+ * @param cls closure, the mesh handle
+ * @param size number of bytes available in buf
+ * @param buf where the callee should write the connect message
+ * @return number of bytes written to buf
+ */
+static size_t
+send_callback (void *cls, size_t size, void *buf)
+{
+ struct GNUNET_MESH_Handle *h = cls;
+ struct GNUNET_MESH_TransmitHandle *th;
+ char *cbuf = buf;
+ size_t tsize;
+ size_t psize;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Send packet() Buffer %u\n", size);
+ h->th = NULL;
+ if ((0 == size) || (NULL == buf))
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Received NULL callback\n");
+ reconnect (h);
+ return 0;
+ }
+ tsize = 0;
+ while ((NULL != (th = h->th_head)) && (size >= th->size))
+ {
+ if (NULL != th->notify)
+ {
+ if (th->tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
+ {
+ /* traffic to origin */
+ struct GNUNET_MESH_ToOrigin to;
+ struct GNUNET_MessageHeader *mh;
+
+ GNUNET_assert (size >= th->size);
+ mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (to)];
+ psize = th->notify (th->notify_cls, size - sizeof (to), mh);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin, type %u\n",
+ ntohs (mh->type));
+ if (psize > 0)
+ {
+ psize += sizeof (to);
+ GNUNET_assert (size >= psize);
+ to.header.size = htons (psize);
+ to.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
+ to.tid = htonl (th->tunnel->tid);
+ memset (&to.oid, 0, sizeof (struct GNUNET_PeerIdentity));
+ memset (&to.sender, 0, sizeof (struct GNUNET_PeerIdentity));
+ memcpy (cbuf, &to, sizeof (to));
+ }
+ }
+ else if (th->target == 0)
+ {
+ /* multicast */
+ struct GNUNET_MESH_Multicast mc;
+ struct GNUNET_MessageHeader *mh;
+
+ GNUNET_assert (size >= th->size);
+ mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (mc)];
+ psize = th->notify (th->notify_cls, size - sizeof (mc), mh);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " multicast, type %u\n",
+ ntohs (mh->type));
+ if (psize > 0)
+ {
+ psize += sizeof (mc);
+ GNUNET_assert (size >= psize);
+ mc.header.size = htons (psize);
+ mc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
+ mc.tid = htonl (th->tunnel->tid);
+ mc.mid = 0;
+ mc.ttl = 0;
+ memset (&mc.oid, 0, sizeof (struct GNUNET_PeerIdentity));
+ memcpy (cbuf, &mc, sizeof (mc));
+ }
+ }
+ else
+ {
+ /* unicast */
+ struct GNUNET_MESH_Unicast uc;
+ struct GNUNET_MessageHeader *mh;
+
+ GNUNET_assert (size >= th->size);
+ mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (uc)];
+ psize = th->notify (th->notify_cls, size - sizeof (uc), mh);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " unicast, type %u\n",
+ ntohs (mh->type));
+ if (psize > 0)
+ {
+ psize += sizeof (uc);
+ GNUNET_assert (size >= psize);
+ uc.header.size = htons (psize);
+ uc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
+ uc.tid = htonl (th->tunnel->tid);
+ memset (&uc.oid, 0, sizeof (struct GNUNET_PeerIdentity));
+ GNUNET_PEER_resolve (th->target, &uc.destination);
+ memcpy (cbuf, &uc, sizeof (uc));
+ }
+ }
+ }
+ else
+ {
+ memcpy (cbuf, &th[1], th->size);
+ psize = th->size;
+ }
+ if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
+ GNUNET_SCHEDULER_cancel (th->timeout_task);
+ if (NULL != th->notify)
+ {
+ th->tunnel->mesh->npackets--;
+ th->tunnel->npackets--;
+ }
+ GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
+ GNUNET_free (th);
+ cbuf += psize;
+ size -= psize;
+ tsize += psize;
+ }
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " total size: %u\n", tsize);
+ if (NULL != (th = h->th_head))
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " next size: %u\n", th->size);
+ if (NULL == h->th)
+ h->th =
+ GNUNET_CLIENT_notify_transmit_ready (h->client, th->size,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ GNUNET_YES, &send_callback, h);
+ }
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Send packet() END\n");
+ if (GNUNET_NO == h->in_receive)
+ {
+ h->in_receive = GNUNET_YES;
+ GNUNET_CLIENT_receive (h->client, &msg_received, h,
+ GNUNET_TIME_UNIT_FOREVER_REL);
+ }
+ return tsize;
+}
+
+
+/**
+ * Auxiliary function to send an already constructed packet to the service.
+ * Takes care of creating a new queue element, copying the message and
+ * calling the tmt_rdy function if necessary.
+ *
+ * @param h mesh handle
+ * @param msg message to transmit
+ * @param tunnel tunnel this send is related to (NULL if N/A)
+ */
+static void
+send_packet (struct GNUNET_MESH_Handle *h,
+ const struct GNUNET_MessageHeader *msg,
+ struct GNUNET_MESH_Tunnel *tunnel)
+{
+ struct GNUNET_MESH_TransmitHandle *th;
+ size_t msize;
+
+ msize = ntohs (msg->size);
+ th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
+ th->priority = UINT32_MAX;
+ th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
+ th->size = msize;
+ th->tunnel = tunnel;
+ memcpy (&th[1], msg, msize);
+ add_to_queue (h, th);
+ if (NULL != h->th)
+ return;
+ h->th =
+ GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ GNUNET_YES, &send_callback, h);
+}
+
+
+/******************************************************************************/
+/********************** API CALL DEFINITIONS *************************/
+/******************************************************************************/
+
+/**
+ * Connect to the mesh service.
+ *
+ * @param cfg configuration to use
+ * @param queue_size size of the data message queue, shared among all tunnels
+ * (each tunnel is guaranteed to accept at least one message,
+ * no matter what is the status of other tunnels)
+ * @param cls closure for the various callbacks that follow
+ * (including handlers in the handlers array)
+ * @param new_tunnel function called when an *inbound* tunnel is created
+ * @param cleaner function called when an *inbound* tunnel is destroyed by the
+ * remote peer, it is *not* called if GNUNET_MESH_tunnel_destroy
+ * is called on the tunnel
+ * @param handlers callbacks for messages we care about, NULL-terminated
+ * note that the mesh is allowed to drop notifications about
+ * inbound messages if the client does not process them fast
+ * enough (for this notification type, a bounded queue is used)
+ * @param stypes list of the applications that this client claims to provide
+ * @return handle to the mesh service NULL on error
+ * (in this case, init is never called)
+ */
+struct GNUNET_MESH_Handle *
+GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
+ unsigned int queue_size, void *cls,
+ GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
+ GNUNET_MESH_TunnelEndHandler cleaner,
+ const struct GNUNET_MESH_MessageHandler *handlers,
+ const GNUNET_MESH_ApplicationType *stypes)
+{
+ struct GNUNET_MESH_Handle *h;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect()\n");
+ h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
+ h->cfg = cfg;
+ h->max_queue_size = queue_size;
+ h->new_tunnel = new_tunnel;
+ h->cleaner = cleaner;
+ h->client = GNUNET_CLIENT_connect ("mesh", cfg);
+ if (h->client == NULL)
+ {
+ GNUNET_break (0);
+ GNUNET_free (h);
+ return NULL;
+ }
+ h->cls = cls;
+ /* FIXME memdup? */
+ h->applications = stypes;
+ h->message_handlers = handlers;
+ h->next_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
+ h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
+ h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+
+ /* count handlers and apps, calculate size */
+ for (h->n_applications = 0; stypes[h->n_applications]; h->n_applications++) ;
+ for (h->n_handlers = 0; handlers[h->n_handlers].type; h->n_handlers++) ;
+ send_connect (h);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
+ return h;
+}
+
+
+/**
+ * Disconnect from the mesh service. All tunnels will be destroyed. All tunnel
+ * disconnect callbacks will be called on any still connected peers, notifying
+ * about their disconnection. The registered inbound tunnel cleaner will be
+ * called should any inbound tunnels still exist.
+ *
+ * @param handle connection to mesh to disconnect
+ */
+void
+GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
+{
+ struct GNUNET_MESH_Tunnel *t;
+ struct GNUNET_MESH_Tunnel *aux;
+ struct GNUNET_MESH_TransmitHandle *th;
+
+ t = handle->tunnels_head;
+ while (NULL != t)
+ {
+ aux = t->next;
+ if (t->tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
+ {
+ GNUNET_break (0);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X not destroyed\n", t->tid);
+ }
+ destroy_tunnel (t, GNUNET_YES);
+ t = aux;
+ }
+ while ( (th = handle->th_head) != NULL)
+ {
+ struct GNUNET_MessageHeader *msg;
+
+ /* Make sure it is an allowed packet (everything else should have been
+ * already canceled).
+ */
+ GNUNET_break (UINT32_MAX == th->priority);
+ GNUNET_break (NULL == th->notify);
+ msg = (struct GNUNET_MessageHeader *) &th[1];
+ switch (ntohs(msg->type))
+ {
+ case GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT:
+ case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
+ break;
+ default:
+ GNUNET_break (0);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "unexpected msg %u\n",
+ ntohs(msg->type));
+ }
+
+ GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
+ GNUNET_free (th);
+ }
+
+ if (NULL != handle->th)
+ {
+ GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
+ handle->th = NULL;
+ }
+ if (NULL != handle->client)
+ {
+ GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
+ handle->client = NULL;
+ }
+ if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
+ {
+ GNUNET_SCHEDULER_cancel(handle->reconnect_task);
+ handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+ GNUNET_free (handle);
+}
+
+
+/**
+ * Create a new tunnel (we're initiator and will be allowed to add/remove peers
+ * and to broadcast).
+ *
+ * @param h mesh handle
+ * @param tunnel_ctx client's tunnel context to associate with the tunnel
+ * @param connect_handler function to call when peers are actually connected
+ * @param disconnect_handler function to call when peers are disconnected
+ * @param handler_cls closure for connect/disconnect handlers
+ */
+struct GNUNET_MESH_Tunnel *
+GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h, void *tunnel_ctx,
+ GNUNET_MESH_PeerConnectHandler connect_handler,
+ GNUNET_MESH_PeerDisconnectHandler disconnect_handler,
+ void *handler_cls)
+{
+ struct GNUNET_MESH_Tunnel *t;
+ struct GNUNET_MESH_TunnelMessage msg;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating new tunnel\n");
+ t = create_tunnel (h, 0);
+ t->connect_handler = connect_handler;
+ t->disconnect_handler = disconnect_handler;
+ t->cls = handler_cls;
+ t->ctx = tunnel_ctx;
+ msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
+ msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
+ msg.tunnel_id = htonl (t->tid);
+ send_packet (h, &msg.header, t);
+ return t;
+}
+
+
+/**
+ * Destroy an existing tunnel. The existing callback for the tunnel will NOT
+ * be called.
+ *
+ * @param tunnel tunnel handle
+ */
+void
+GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel)
+{
+ struct GNUNET_MESH_Handle *h;
+ struct GNUNET_MESH_TunnelMessage msg;
+ struct GNUNET_MESH_TransmitHandle *th;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying tunnel\n");
+ h = tunnel->mesh;
+
+ msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
+ msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
+ msg.tunnel_id = htonl (tunnel->tid);
+ th = h->th_head;
+ while (th != NULL)
+ {
+ struct GNUNET_MESH_TransmitHandle *aux;
+ if (th->tunnel == tunnel)
+ {
+ aux = th->next;
+ /* FIXME call the handler? */
+ if (NULL != th->notify)
+ th->notify (th->notify_cls, 0, NULL);
+ GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
+ GNUNET_free (th);
+ th = aux;
+ }
+ else
+ th = th->next;
+ }
+
+ destroy_tunnel (tunnel, GNUNET_NO);
+ send_packet (h, &msg.header, tunnel);
+}
+
+
+/**
+ * Request that a peer should be added to the tunnel. The existing
+ * connect handler will be called ONCE with either success or failure.
+ * This function should NOT be called again with the same peer before the
+ * connect handler is called.
+ *
+ * @param tunnel handle to existing tunnel
+ * @param peer peer to add
+ */
+void
+GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
+ const struct GNUNET_PeerIdentity *peer)
+{
+ struct GNUNET_MESH_PeerControl msg;
+ GNUNET_PEER_Id peer_id;
+ unsigned int i;
+
+ peer_id = GNUNET_PEER_intern (peer);
+ for (i = 0; i < tunnel->npeers; i++)
+ {
+ if (tunnel->peers[i]->id == peer_id)
+ {
+ /* Peer already exists in tunnel */
+ GNUNET_PEER_change_rc (peer_id, -1);
+ GNUNET_break (0);
+ return;
+ }
+ }
+ if (NULL == add_peer_to_tunnel (tunnel, peer))
+ return;
+
+ msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
+ msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
+ msg.tunnel_id = htonl (tunnel->tid);
+ msg.peer = *peer;
+ send_packet (tunnel->mesh, &msg.header, tunnel);
+
+ return;
+}
+
+
+/**
+ * Request that a peer should be removed from the tunnel. The existing
+ * disconnect handler will be called ONCE if we were connected.
+ *
+ * @param tunnel handle to existing tunnel
+ * @param peer peer to remove
+ */
+void
+GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
+ const struct GNUNET_PeerIdentity *peer)
+{
+ struct GNUNET_MESH_PeerControl msg;
+ GNUNET_PEER_Id peer_id;
+ unsigned int i;
+
+ peer_id = GNUNET_PEER_search (peer);
+ if (0 == peer_id)
+ {
+ GNUNET_break (0);
+ return;
+ }
+ for (i = 0; i < tunnel->npeers; i++)
+ if (tunnel->peers[i]->id == peer_id)
+ break;
+ if (i == tunnel->npeers)
+ {
+ GNUNET_break (0);
+ return;
+ }
+ if (NULL != tunnel->disconnect_handler && tunnel->peers[i]->connected == 1)
+ tunnel->disconnect_handler (tunnel->cls, peer);
+ GNUNET_PEER_change_rc (peer_id, -1);
+ GNUNET_free (tunnel->peers[i]);
+ tunnel->peers[i] = tunnel->peers[tunnel->npeers - 1];
+ GNUNET_array_grow (tunnel->peers, tunnel->npeers, tunnel->npeers - 1);
+
+ msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
+ msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
+ msg.tunnel_id = htonl (tunnel->tid);
+ memcpy (&msg.peer, peer, sizeof (struct GNUNET_PeerIdentity));
+ send_packet (tunnel->mesh, &msg.header, tunnel);
+}
+
+
+/**
+ * Request that the mesh should try to connect to a peer supporting the given
+ * message type.
+ *
+ * @param tunnel handle to existing tunnel
+ * @param app_type application type that must be supported by the peer (MESH
+ * should discover peer in proximity handling this type)
+ */
+void
+GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel,
+ GNUNET_MESH_ApplicationType app_type)
+{
+ struct GNUNET_MESH_ConnectPeerByType msg;
+
+ GNUNET_array_append (tunnel->apps, tunnel->napps, app_type);
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "* CONNECT BY TYPE *\n");
+ msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectPeerByType));
+ msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE);
+ msg.tunnel_id = htonl (tunnel->tid);
+ msg.type = htonl (app_type);
+ send_packet (tunnel->mesh, &msg.header, tunnel);
+}
+
+
+/**
+ * Ask the mesh to call "notify" once it is ready to transmit the
+ * given number of bytes to the specified "target". If we are not yet
+ * connected to the specified peer, a call to this function will cause
+ * us to try to establish a connection.
+ *
+ * @param tunnel tunnel to use for transmission
+ * @param cork is corking allowed for this transmission?
+ * @param priority how important is the message?
+ * @param maxdelay how long can the message wait?
+ * @param target destination for the message,
+ * NULL for multicast to all tunnel targets
+ * @param notify_size how many bytes of buffer space does notify want?
+ * @param notify function to call when buffer space is available;
+ * will be called with NULL on timeout or if the overall queue
+ * for this peer is larger than queue_size and this is currently
+ * the message with the lowest priority
+ * @param notify_cls closure for notify
+ * @return non-NULL if the notify callback was queued,
+ * NULL if we can not even queue the request (insufficient
+ * memory); if NULL is returned, "notify" will NOT be called.
+ */
+struct GNUNET_MESH_TransmitHandle *
+GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
+ uint32_t priority,
+ struct GNUNET_TIME_Relative maxdelay,
+ const struct GNUNET_PeerIdentity *target,
+ size_t notify_size,
+ GNUNET_CONNECTION_TransmitReadyNotify notify,
+ void *notify_cls)
+{
+ struct GNUNET_MESH_TransmitHandle *th;
+ struct GNUNET_MESH_TransmitHandle *least_priority_th;
+ uint32_t least_priority;
+ size_t overhead;
+
+ GNUNET_assert (NULL != tunnel);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "mesh notify transmit ready called\n");
+ if (NULL != target)
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " target %s\n", GNUNET_i2s (target));
+ else
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " target multicast\n");
+ GNUNET_assert (NULL != notify);
+ if (tunnel->mesh->npackets >= tunnel->mesh->max_queue_size &&
+ tunnel->npackets > 0)
+ {
+ /* queue full */
+ if (0 == priority)
+ return NULL;
+ th = tunnel->mesh->th_tail;
+ least_priority = priority;
+ least_priority_th = NULL;
+ while (NULL != th)
+ {
+ if (th->priority < least_priority && th->tunnel->npackets > 1)
+ {
+ least_priority_th = th;
+ least_priority = th->priority;
+ }
+ th = th->prev;
+ }
+ if (NULL == least_priority_th)
+ return NULL;
+ /* Can't be a control message */
+ GNUNET_assert (NULL != least_priority_th->notify);
+ least_priority_th->notify (notify_cls, 0, NULL);
+ least_priority_th->tunnel->npackets--;
+ tunnel->mesh->npackets--;
+ GNUNET_CONTAINER_DLL_remove (tunnel->mesh->th_head, tunnel->mesh->th_tail,
+ least_priority_th);
+ if (GNUNET_SCHEDULER_NO_TASK != least_priority_th->timeout_task)
+ GNUNET_SCHEDULER_cancel (least_priority_th->timeout_task);
+ GNUNET_free (least_priority_th);
+ }
+ tunnel->npackets++;
+ tunnel->mesh->npackets++;
+ th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
+ th->tunnel = tunnel;
+ th->priority = priority;
+ th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
+ th->target = GNUNET_PEER_intern (target);
+ if (tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
+ overhead = sizeof (struct GNUNET_MESH_ToOrigin);
+ else if (NULL == target)
+ overhead = sizeof (struct GNUNET_MESH_Multicast);
+ else
+ overhead = sizeof (struct GNUNET_MESH_Unicast);
+ th->size = notify_size + overhead;
+ th->notify = notify;
+ th->notify_cls = notify_cls;
+ add_to_queue (tunnel->mesh, th);
+ if (NULL != tunnel->mesh->th)
+ return th;
+ tunnel->mesh->th =
+ GNUNET_CLIENT_notify_transmit_ready (tunnel->mesh->client, th->size,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ GNUNET_YES, &send_callback,
+ tunnel->mesh);
+ return th;
+}
+
+
+/**
+ * Cancel the specified transmission-ready notification.
+ *
+ * @param th handle that was returned by "notify_transmit_ready".
+ */
+void
+GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
+{
+ struct GNUNET_MESH_Handle *mesh;
+
+ mesh = th->tunnel->mesh;
+ if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
+ GNUNET_SCHEDULER_cancel (th->timeout_task);
+ GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
+ GNUNET_free (th);
+ if ((NULL == mesh->th_head) && (NULL != mesh->th))
+ {
+ /* queue empty, no point in asking for transmission */
+ GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
+ mesh->th = NULL;
+ }
+}
+
+
+/**
+ * Transition API for tunnel ctx management
+ */
+void
+GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data)
+{
+ tunnel->ctx = data;
+}
+
+/**
+ * Transition API for tunnel ctx management
+ */
+void *
+GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel)
+{
+ return tunnel->ctx;
+}
+
+
+#if 0 /* keep Emacsens' auto-indent happy */
+{
+#endif
+#ifdef __cplusplus
+}
+#endif
diff --git a/src/mesh/mesh_protocol.h b/src/mesh/mesh_protocol.h
new file mode 100644
index 0000000..885f1f3
--- /dev/null
+++ b/src/mesh/mesh_protocol.h
@@ -0,0 +1,292 @@
+/*
+ This file is part of GNUnet.
+ (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 3, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @author Bartlomiej Polot
+ * @file mesh/mesh_protocol.h
+ */
+
+#ifndef MESH_PROTOCOL_H_
+#define MESH_PROTOCOL_H_
+
+#ifdef __cplusplus
+extern "C"
+{
+#if 0
+ /* keep Emacsens' auto-indent happy */
+}
+#endif
+#endif
+
+/******************************************************************************/
+/******************** MESH NETWORK MESSAGES **************************/
+/******************************************************************************/
+
+GNUNET_NETWORK_STRUCT_BEGIN
+
+/**
+ * Message for mesh path management
+ */
+struct GNUNET_MESH_ManipulatePath
+{
+ /**
+ * Type: GNUNET_MESSAGE_TYPE_MESH_PATH_[CREATE|CHANGE|ADD|DEL]
+ *
+ * Size: sizeof(struct GNUNET_MESH_ManipulatePath) +
+ * path_length * sizeof (struct GNUNET_PeerIdentity)
+ */
+ struct GNUNET_MessageHeader header;
+
+ /**
+ * Global id of the tunnel this path belongs to,
+ * unique in conjunction with the origin.
+ */
+ uint32_t tid GNUNET_PACKED;
+
+ /**
+ * path_length structs defining the *whole* path from the origin [0] to the
+ * final destination [path_length-1].
+ */
+ /* struct GNUNET_PeerIdentity peers[path_length]; */
+};
+
+/**
+ * Message for mesh data traffic to all tunnel targets.
+ */
+struct GNUNET_MESH_Multicast
+{
+ /**
+ * Type: GNUNET_MESSAGE_TYPE_MESH_MULTICAST
+ */
+ struct GNUNET_MessageHeader header;
+
+ /**
+ * TID of the tunnel
+ */
+ uint32_t tid GNUNET_PACKED;
+
+ /**
+ * Number of hops to live
+ */
+ uint32_t ttl GNUNET_PACKED;
+
+ /**
+ * Unique ID of the packet
+ */
+ uint32_t mid GNUNET_PACKED;
+
+ /**
+ * OID of the tunnel
+ */
+ struct GNUNET_PeerIdentity oid;
+
+ /**
+ * Payload follows
+ */
+};
+
+
+/**
+ * Message for mesh data traffic to a particular destination from origin.
+ */
+struct GNUNET_MESH_Unicast
+{
+ /**
+ * Type: GNUNET_MESSAGE_TYPE_MESH_UNICAST
+ */
+ struct GNUNET_MessageHeader header;
+
+ /**
+ * TID of the tunnel
+ */
+ uint32_t tid GNUNET_PACKED;
+
+ /**
+ * OID of the tunnel
+ */
+ struct GNUNET_PeerIdentity oid;
+
+ /**
+ * Destination.
+ */
+ struct GNUNET_PeerIdentity destination;
+
+ /**
+ * Payload follows
+ */
+};
+
+
+/**
+ * Message for mesh data traffic from a tunnel participant to origin.
+ */
+struct GNUNET_MESH_ToOrigin
+{
+ /**
+ * Type: GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN
+ */
+ struct GNUNET_MessageHeader header;
+
+ /**
+ * TID of the tunnel
+ */
+ uint32_t tid GNUNET_PACKED;
+
+ /**
+ * OID of the tunnel
+ */
+ struct GNUNET_PeerIdentity oid;
+
+ /**
+ * Sender of the message.
+ */
+ struct GNUNET_PeerIdentity sender;
+
+ /**
+ * Payload follows
+ */
+};
+
+
+/**
+ * Message for ack'ing a path
+ */
+struct GNUNET_MESH_PathACK
+{
+ /**
+ * Type: GNUNET_MESSAGE_TYPE_MESH_PATH_ACK
+ */
+ struct GNUNET_MessageHeader header;
+
+ /**
+ * TID of the tunnel
+ */
+ uint32_t tid GNUNET_PACKED;
+
+ /**
+ * OID of the tunnel
+ */
+ struct GNUNET_PeerIdentity oid;
+
+ /**
+ * ID of the endpoint
+ */
+ struct GNUNET_PeerIdentity peer_id;
+
+ /* TODO: signature */
+};
+
+
+/**
+ * Message for notifying a disconnection in a path
+ */
+struct GNUNET_MESH_PathBroken
+{
+ /**
+ * Type: GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN
+ */
+ struct GNUNET_MessageHeader header;
+
+ /**
+ * TID of the tunnel
+ */
+ uint32_t tid GNUNET_PACKED;
+
+ /**
+ * OID of the tunnel
+ */
+ struct GNUNET_PeerIdentity oid;
+
+ /**
+ * ID of the endpoint
+ */
+ struct GNUNET_PeerIdentity peer1;
+
+ /**
+ * ID of the endpoint
+ */
+ struct GNUNET_PeerIdentity peer2;
+
+ /* TODO: signature */
+};
+
+
+/**
+ * Message to destroy a tunnel
+ */
+struct GNUNET_MESH_TunnelDestroy
+{
+ /**
+ * Type: GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY
+ */
+ struct GNUNET_MessageHeader header;
+
+ /**
+ * TID of the tunnel
+ */
+ uint32_t tid GNUNET_PACKED;
+
+ /**
+ * OID of the tunnel
+ */
+ struct GNUNET_PeerIdentity oid;
+
+ /* TODO: signature */
+};
+
+
+/**
+ * Message for mesh flow control
+ */
+struct GNUNET_MESH_SpeedNotify
+{
+ /**
+ * Type: GNUNET_MESSAGE_TYPE_DATA_SPEED_NOTIFY
+ */
+ struct GNUNET_MessageHeader header;
+
+ /**
+ * TID of the tunnel
+ */
+ uint32_t tid GNUNET_PACKED;
+
+ /**
+ * OID of the tunnel
+ */
+ struct GNUNET_PeerIdentity oid;
+
+ /**
+ * Slowest link down the path (above minimum speed requirement).
+ */
+ uint32_t speed_min;
+
+};
+GNUNET_NETWORK_STRUCT_END
+
+#if 0 /* keep Emacsens' auto-indent happy */
+{
+#endif
+#ifdef __cplusplus
+}
+#endif
+
+/* ifndef MES_PROTOCOL_H */
+#endif
+/* end of mesh_protocol.h */
diff --git a/src/mesh/mesh_tunnel_tree.c b/src/mesh/mesh_tunnel_tree.c
new file mode 100644
index 0000000..445b710
--- /dev/null
+++ b/src/mesh/mesh_tunnel_tree.c
@@ -0,0 +1,1075 @@
+/*
+ This file is part of GNUnet.
+ (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 3, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file mesh/mesh_tunnel_tree.c
+ * @brief Tunnel tree handling functions
+ * @author Bartlomiej Polot
+ */
+
+#include "mesh.h"
+#include "mesh_tunnel_tree.h"
+
+#define MESH_TREE_DEBUG GNUNET_YES
+
+
+/**
+ * Node of path tree for a tunnel
+ */
+struct MeshTunnelTreeNode
+{
+ /**
+ * Peer this node describes
+ */
+ GNUNET_PEER_Id peer;
+
+ /**
+ * Parent node in the tree
+ */
+ struct MeshTunnelTreeNode *parent;
+
+ /**
+ * DLL of siblings
+ */
+ struct MeshTunnelTreeNode *next;
+
+ /**
+ * DLL of siblings
+ */
+ struct MeshTunnelTreeNode *prev;
+
+ /**
+ * DLL of children
+ */
+ struct MeshTunnelTreeNode *children_head;
+
+ /**
+ * DLL of children
+ */
+ struct MeshTunnelTreeNode *children_tail;
+
+ /**
+ * Status of the peer in the tunnel
+ */
+ enum MeshPeerState status;
+};
+
+
+/**
+ * Tree to reach all peers in the tunnel
+ */
+struct MeshTunnelTree
+{
+ /**
+ * Root node of peer tree
+ */
+ struct MeshTunnelTreeNode *root;
+
+ /**
+ * Node that represents our position in the tree (for non local tunnels)
+ */
+ struct MeshTunnelTreeNode *me;
+
+ /**
+ * DLL of disconneted nodes
+ */
+ struct MeshTunnelTreeNode *disconnected_head;
+
+ /**
+ * DLL of disconneted nodes
+ */
+ struct MeshTunnelTreeNode *disconnected_tail;
+
+ /**
+ * Cache of all peers and the first hop to them.
+ * Indexed by PeerIdentity, contains a pointer to the PeerIdentity
+ * of 1st hop.
+ */
+ struct GNUNET_CONTAINER_MultiHashMap *first_hops;
+
+};
+
+
+/**
+ * Create a new path
+ *
+ * @param length How many hops will the path have.
+ *
+ * @return A newly allocated path with a peer array of the specified length.
+ */
+struct MeshPeerPath *
+path_new (unsigned int length)
+{
+ struct MeshPeerPath *p;
+
+ p = GNUNET_malloc (sizeof (struct MeshPeerPath));
+ if (length > 0)
+ {
+ p->length = length;
+ p->peers = GNUNET_malloc (length * sizeof (GNUNET_PEER_Id));
+ }
+ return p;
+}
+
+
+/**
+ * Invert the path
+ *
+ * @param path the path to invert
+ */
+void
+path_invert (struct MeshPeerPath *path)
+{
+ GNUNET_PEER_Id aux;
+ unsigned int i;
+
+ for (i = 0; i < path->length / 2; i++)
+ {
+ aux = path->peers[i];
+ path->peers[i] = path->peers[path->length - i - 1];
+ path->peers[path->length - i - 1] = aux;
+ }
+}
+
+
+/**
+ * Duplicate a path, incrementing short peer's rc.
+ *
+ * @param path The path to duplicate.
+ */
+struct MeshPeerPath *
+path_duplicate (struct MeshPeerPath *path)
+{
+ struct MeshPeerPath *aux;
+ unsigned int i;
+
+ aux = path_new (path->length);
+ memcpy (aux->peers, path->peers, path->length * sizeof (GNUNET_PEER_Id));
+ for (i = 0; i < path->length; i++)
+ GNUNET_PEER_change_rc (path->peers[i], 1);
+ return aux;
+}
+
+
+/**
+ * Recusively update the info about what is the first hop to reach the node
+ *
+ * @param tree Tree this nodes belongs to.
+ * @param parent The node form which to start updating.
+ * @param hop If known, ID of the first hop.
+ * If not known, NULL to find out and pass on children.
+ */
+static void
+tree_node_update_first_hops (struct MeshTunnelTree *tree,
+ struct MeshTunnelTreeNode *parent,
+ struct GNUNET_PeerIdentity *hop);
+
+
+/**
+ * Get the length of a path.
+ *
+ * @param path The path to measure, with the local peer at any point of it.
+ *
+ * @return Number of hops to reach destination.
+ * UINT_MAX in case the peer is not in the path.
+ */
+unsigned int
+path_get_length (struct MeshPeerPath *path)
+{
+ if (NULL == path)
+ return UINT_MAX;
+ return path->length;
+}
+
+
+/**
+ * Destroy the path and free any allocated resources linked to it
+ *
+ * @param p the path to destroy
+ *
+ * @return GNUNET_OK on success
+ */
+int
+path_destroy (struct MeshPeerPath *p)
+{
+ if (NULL == p)
+ return GNUNET_OK;
+ GNUNET_PEER_decrement_rcs (p->peers, p->length);
+ GNUNET_free_non_null (p->peers);
+ GNUNET_free (p);
+ return GNUNET_OK;
+}
+
+
+
+/**
+ * Allocates and initializes a new node.
+ * Sets ID and parent of the new node and inserts it in the DLL of the parent
+ *
+ * @param parent Node that will be the parent from the new node, NULL for root
+ * @param peer Short Id of the new node
+ *
+ * @return Newly allocated node
+ */
+static struct MeshTunnelTreeNode *
+tree_node_new (struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer)
+{
+ struct MeshTunnelTreeNode *node;
+
+ node = GNUNET_malloc (sizeof (struct MeshTunnelTreeNode));
+ node->peer = peer;
+ GNUNET_PEER_change_rc (peer, 1);
+ node->parent = parent;
+ if (NULL != parent)
+ GNUNET_CONTAINER_DLL_insert (parent->children_head, parent->children_tail,
+ node);
+
+ return node;
+}
+
+
+/**
+ * Recursively find the given peer.
+ *
+ * @param parent Node where to start looking.
+ * @param peer_id Short ID of the peer to find.
+ *
+ * @return Pointer to the node of the peer. NULL if not found.
+ */
+static struct MeshTunnelTreeNode *
+tree_node_find_peer (struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer_id)
+{
+ struct MeshTunnelTreeNode *n;
+ struct MeshTunnelTreeNode *r;
+
+ if (parent->peer == peer_id)
+ return parent;
+ for (n = parent->children_head; NULL != n; n = n->next)
+ {
+ r = tree_node_find_peer (n, peer_id);
+ if (NULL != r)
+ return r;
+ }
+ return NULL;
+}
+
+
+/**
+ * Recusively update the info about what is the first hop to reach the node
+ *
+ * @param tree Tree this nodes belongs to.
+ * @param parent ID from node form which to start updating.
+ * @param hop If known, ID of the first hop.
+ * If not known, NULL to find out and pass on children.
+ */
+static void
+tree_node_update_first_hops (struct MeshTunnelTree *tree,
+ struct MeshTunnelTreeNode *parent,
+ struct GNUNET_PeerIdentity *hop)
+{
+ struct GNUNET_PeerIdentity pi;
+ struct GNUNET_PeerIdentity *copy;
+ struct GNUNET_PeerIdentity id;
+ struct MeshTunnelTreeNode *n;
+
+#if MESH_TREE_DEBUG
+ GNUNET_PEER_resolve (parent->peer, &id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Finding first hop for %s.\n",
+ GNUNET_i2s (&id));
+#endif
+ if (NULL == hop)
+ {
+ struct MeshTunnelTreeNode *aux;
+ struct MeshTunnelTreeNode *old;
+
+ aux = old = parent;
+ while (aux != tree->me)
+ {
+#if MESH_TREE_DEBUG
+ GNUNET_PEER_resolve (aux->peer, &id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: ... checking %s.\n",
+ GNUNET_i2s (&id));
+#endif
+ old = aux;
+ aux = aux->parent;
+ GNUNET_assert (NULL != aux);
+ }
+#if MESH_TREE_DEBUG
+ GNUNET_PEER_resolve (old->peer, &id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: It's %s!\n",
+ GNUNET_i2s (&id));
+#endif
+ hop = &pi;
+ GNUNET_PEER_resolve (old->peer, hop);
+ }
+ GNUNET_PEER_resolve (parent->peer, &id);
+ copy = GNUNET_CONTAINER_multihashmap_get (tree->first_hops, &id.hashPubKey);
+ if (NULL == copy)
+ copy = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
+ *copy = *hop;
+
+ (void) GNUNET_CONTAINER_multihashmap_put (tree->first_hops, &id.hashPubKey,
+ copy,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
+
+ for (n = parent->children_head; NULL != n; n = n->next)
+ {
+ tree_node_update_first_hops (tree, n, hop);
+ }
+}
+
+
+static void
+tree_node_debug (struct MeshTunnelTreeNode *n, uint16_t level)
+{
+ struct MeshTunnelTreeNode *c;
+ struct GNUNET_PeerIdentity id;;
+ uint16_t i;
+
+ for (i = 0; i < level; i++)
+ FPRINTF (stderr, "%s", " ");
+ if (n->status == MESH_PEER_READY)
+ FPRINTF (stderr, "%s", "#");
+ if (n->status == MESH_PEER_SEARCHING)
+ FPRINTF (stderr, "%s", "+");
+ if (n->status == MESH_PEER_RELAY)
+ FPRINTF (stderr, "%s", "-");
+ if (n->status == MESH_PEER_RECONNECTING)
+ FPRINTF (stderr, "%s", "*");
+
+ GNUNET_PEER_resolve (n->peer, &id);
+ FPRINTF (stderr, "%s, [%u, %p] ", GNUNET_i2s (&id), n->peer, n);
+ if (NULL != n->parent)
+ {
+ GNUNET_PEER_resolve (n->parent->peer, &id);
+ FPRINTF (stderr, "(-> %s [%u])\n", GNUNET_i2s (&id), n->parent->peer);
+ }
+ else
+ FPRINTF (stderr, "%s", "(root)\n");
+ for (c = n->children_head; NULL != c; c = c->next)
+ tree_node_debug (c, level + 1);
+}
+
+
+/**
+ * Destroys and frees the node and all children
+ *
+ * @param parent Parent node to be destroyed
+ */
+static void
+tree_node_destroy (struct MeshTunnelTreeNode *parent)
+{
+ struct MeshTunnelTreeNode *n;
+ struct MeshTunnelTreeNode *next;
+
+#if MESH_TREE_DEBUG
+ struct GNUNET_PeerIdentity id;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Destroying node %u\n",
+ parent->peer);
+ GNUNET_PEER_resolve (parent->peer, &id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: (%s)\n", GNUNET_i2s (&id));
+#endif
+ n = parent->children_head;
+ while (NULL != n)
+ {
+ next = n->next;
+ tree_node_destroy (n);
+ n = next;
+ }
+ GNUNET_PEER_change_rc (parent->peer, -1);
+ if (NULL != parent->parent)
+ GNUNET_CONTAINER_DLL_remove (parent->parent->children_head,
+ parent->parent->children_tail, parent);
+ GNUNET_free (parent);
+}
+
+
+
+/**
+ * Create a new tree.
+ *
+ * @param peer A short peer id of the root of the tree.
+ *
+ * @return A newly allocated and initialized tunnel tree.
+ */
+struct MeshTunnelTree *
+tree_new (GNUNET_PEER_Id peer)
+{
+ struct MeshTunnelTree *tree;
+
+ tree = GNUNET_malloc (sizeof (struct MeshTunnelTree));
+ tree->first_hops = GNUNET_CONTAINER_multihashmap_create (32);
+ tree->root = tree_node_new (NULL, peer);
+ tree->root->status = MESH_PEER_ROOT;
+
+ if (1 == peer)
+ {
+ tree->me = tree->root;
+ }
+
+ return tree;
+}
+
+
+/**
+ * Set the status of a node.
+ *
+ * @param tree Tree.
+ * @param peer A short peer id of the node.
+ * @param status New status to set.
+ */
+void
+tree_set_status (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer,
+ enum MeshPeerState status)
+{
+ struct MeshTunnelTreeNode *n;
+
+ n = tree_find_peer (tree, peer);
+ if (NULL == n)
+ return;
+ n->status = status;
+}
+
+
+/**
+ * Get the status of a node.
+ *
+ * @param tree Tree whose node's status we want to now.
+ * @param peer A short peer id of the node.
+ *
+ * @return Status of the peer.
+ */
+enum MeshPeerState
+tree_get_status (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer)
+{
+ struct MeshTunnelTreeNode *n;
+
+ n = tree_find_peer (tree, peer);
+ if (NULL == n)
+ return MESH_PEER_INVALID;
+ return n->status;
+}
+
+
+/**
+ * Get the id of the predecessor of the local node.
+ *
+ * @param tree Tree whose local id we want to now.
+ *
+ * @return Short peer id of local peer.
+ */
+GNUNET_PEER_Id
+tree_get_predecessor (struct MeshTunnelTree *tree)
+{
+ if (NULL != tree->me && NULL != tree->me->parent)
+ return tree->me->parent->peer;
+ else
+ return (GNUNET_PEER_Id) 0;
+}
+
+
+/**
+ * Find the first peer whom to send a packet to go down this path
+ *
+ * @param t The tunnel tree to use
+ * @param peer The peerinfo of the peer we are trying to reach
+ *
+ * @return peerinfo of the peer who is the first hop in the tunnel
+ * NULL on error
+ */
+struct GNUNET_PeerIdentity *
+tree_get_first_hop (struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
+{
+ struct GNUNET_PeerIdentity id;
+ struct GNUNET_PeerIdentity *r;
+
+ GNUNET_PEER_resolve (peer, &id);
+ r = GNUNET_CONTAINER_multihashmap_get (t->first_hops, &id.hashPubKey);
+ if (NULL == r)
+ {
+ struct MeshTunnelTreeNode *n;
+
+ n = tree_find_peer (t, peer);
+ if (NULL != t->me && NULL != n)
+ {
+ tree_node_update_first_hops (t, n, NULL);
+ r = GNUNET_CONTAINER_multihashmap_get (t->first_hops, &id.hashPubKey);
+ GNUNET_assert (NULL != r);
+ }
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Tree structure inconsistent! me: %p, n: %p", t->me, n);
+ GNUNET_break (0);
+ }
+ }
+
+ return r;
+}
+
+
+/**
+ * Find the given peer in the tree.
+ *
+ * @param tree Tree where to look for the peer.
+ * @param peer_id Short ID of the peer to find.
+ *
+ * @return Pointer to the node of the peer. NULL if not found.
+ */
+struct MeshTunnelTreeNode *
+tree_find_peer (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer_id)
+{
+ return tree_node_find_peer (tree->root, peer_id);
+}
+
+
+/**
+ * Recusively mark peer and children as disconnected, notify client
+ *
+ * @param tree Tree this node belongs to
+ * @param parent Node to be clean, potentially with children
+ * @param cb Callback to use to notify about disconnected peers.
+ * @param cbcls Closure for cb.
+ */
+static void
+tree_mark_peers_disconnected (struct MeshTunnelTree *tree,
+ struct MeshTunnelTreeNode *parent,
+ MeshTreeCallback cb, void *cbcls)
+{
+ struct GNUNET_PeerIdentity *pi;
+ struct GNUNET_PeerIdentity id;
+ struct MeshTunnelTreeNode *n;
+
+ for (n = parent->children_head; NULL != n; n = n->next)
+ {
+ tree_mark_peers_disconnected (tree, n, cb, cbcls);
+ }
+ if (MESH_PEER_READY == parent->status)
+ {
+ if (NULL != cb)
+ cb (cbcls, parent->peer);
+ parent->status = MESH_PEER_RECONNECTING;
+ }
+
+ /* Remove and free info about first hop */
+ GNUNET_PEER_resolve (parent->peer, &id);
+ pi = GNUNET_CONTAINER_multihashmap_get (tree->first_hops, &id.hashPubKey);
+ GNUNET_CONTAINER_multihashmap_remove_all (tree->first_hops, &id.hashPubKey);
+ if (NULL != pi)
+ GNUNET_free (pi);
+}
+
+
+/**
+ * Iterate over all children of the local node.
+ *
+ * @param tree Tree to use. Must have "me" set.
+ * @param cb Callback to call over each child.
+ * @param cls Closure.
+ */
+void
+tree_iterate_children (struct MeshTunnelTree *tree, MeshTreeCallback cb,
+ void *cls)
+{
+ struct MeshTunnelTreeNode *n;
+
+ if (NULL == tree->me)
+ {
+ GNUNET_break (0);
+ return;
+ }
+ for (n = tree->me->children_head; NULL != n; n = n->next)
+ {
+ cb (cls, n->peer);
+ }
+}
+
+
+/**
+ * Recusively update the info about what is the first hop to reach the node
+ *
+ * @param tree Tree this nodes belongs to.
+ * @param parent_id Short ID from node form which to start updating.
+ * @param hop If known, ID of the first hop.
+ * If not known, NULL to find out and pass on children.
+ */
+void
+tree_update_first_hops (struct MeshTunnelTree *tree, GNUNET_PEER_Id parent_id,
+ struct GNUNET_PeerIdentity *hop)
+{
+ tree_node_update_first_hops (tree, tree_find_peer (tree, parent_id), hop);
+}
+
+
+/**
+ * Delete the current path to the peer, including all now unused relays.
+ * The destination peer is NOT destroyed, it is returned in order to either set
+ * a new path to it or destroy it explicitly, taking care of it's child nodes.
+ *
+ * @param t Tunnel tree where to delete the path from.
+ * @param peer_id Short ID of the destination peer whose path we want to remove.
+ * @param cb Callback to use to notify about disconnected peers.
+ * @param cbcls Closure for cb.
+ *
+ * @return pointer to the pathless node.
+ * NULL when not found
+ */
+struct MeshTunnelTreeNode *
+tree_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
+ MeshTreeCallback cb, void *cbcls)
+{
+ struct MeshTunnelTreeNode *parent;
+ struct MeshTunnelTreeNode *node;
+ struct MeshTunnelTreeNode *n;
+
+#if MESH_TREE_DEBUG
+ struct GNUNET_PeerIdentity id;
+
+ GNUNET_PEER_resolve (peer_id, &id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Deleting path to %s.\n",
+ GNUNET_i2s (&id));
+#endif
+ if (peer_id == t->root->peer)
+ return NULL;
+
+ for (n = t->disconnected_head; NULL != n; n = n->next)
+ {
+ if (n->peer == peer_id)
+ {
+ /* Was already pathless, waiting for reconnection */
+ GNUNET_CONTAINER_DLL_remove (t->disconnected_head, t->disconnected_tail,
+ n);
+ return n;
+ }
+ }
+ n = tree_find_peer (t, peer_id);
+ if (NULL == n)
+ return NULL;
+ node = n;
+
+ parent = n->parent;
+ GNUNET_CONTAINER_DLL_remove (parent->children_head, parent->children_tail, n);
+ n->parent = NULL;
+
+ while (MESH_PEER_RELAY == parent->status && NULL == parent->children_head)
+ {
+#if MESH_TREE_DEBUG
+ GNUNET_PEER_resolve (parent->peer, &id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Deleting node %s.\n",
+ GNUNET_i2s (&id));
+#endif
+ n = parent->parent;
+ tree_node_destroy (parent);
+ parent = n;
+ }
+#if MESH_TREE_DEBUG
+ GNUNET_PEER_resolve (parent->peer, &id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Not deleted peer %s.\n",
+ GNUNET_i2s (&id));
+#endif
+
+ tree_mark_peers_disconnected (t, node, cb, cbcls);
+
+ return node;
+}
+
+
+/**
+ * Return a newly allocated individual path to reach a peer from the local peer,
+ * according to the path tree of some tunnel.
+ *
+ * @param t Tunnel from which to read the path tree.
+ * @param peer Short ID of the destination peer to whom we want a path.
+ *
+ * @return A newly allocated individual path to reach the destination peer.
+ * Path must be destroyed afterwards.
+ */
+struct MeshPeerPath *
+tree_get_path_to_peer (struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
+{
+ struct MeshTunnelTreeNode *n;
+ struct MeshPeerPath *p;
+
+ n = tree_find_peer (t, peer);
+ if (NULL == n)
+ {
+ GNUNET_break (0);
+ return NULL;
+ }
+ p = path_new (0);
+
+ /* Building the path (inverted!) */
+ while (n->peer != 1)
+ {
+ GNUNET_array_append (p->peers, p->length, n->peer);
+ GNUNET_PEER_change_rc (n->peer, 1);
+ n = n->parent;
+ if (NULL == n)
+ {
+ GNUNET_break (0);
+ path_destroy (p);
+ return NULL;
+ }
+ }
+ GNUNET_array_append (p->peers, p->length, 1);
+ GNUNET_PEER_change_rc (1, 1);
+
+ path_invert (p);
+
+ return p;
+}
+
+
+
+/**
+ * Integrate a stand alone path into the tunnel tree.
+ * If the peer toward which the new path is already in the tree, the peer
+ * and its children will be maked as disconnected and the callback
+ * will be called on each one of them. They will be maked as online only after
+ * receiving a PATH ACK for the new path for each one of them, so the caller
+ * should take care of sending a new CREATE PATH message for each disconnected
+ * peer.
+ *
+ * @param t Tunnel where to add the new path.
+ * @param p Path to be integrated.
+ * @param cb Callback to use to notify about peers temporarily disconnecting.
+ * @param cbcls Closure for cb.
+ *
+ * @return GNUNET_OK in case of success.
+ * GNUNET_SYSERR in case of error.
+ *
+ * TODO: optimize
+ * - go backwards on path looking for each peer in the present tree
+ * - do not disconnect peers until new path is created & connected
+ */
+int
+tree_add_path (struct MeshTunnelTree *t, const struct MeshPeerPath *p,
+ MeshTreeCallback cb, void *cbcls)
+{
+ struct MeshTunnelTreeNode *parent;
+ struct MeshTunnelTreeNode *oldnode;
+ struct MeshTunnelTreeNode *n;
+ struct MeshTunnelTreeNode *c;
+ struct GNUNET_PeerIdentity id;
+ int me;
+ unsigned int i;
+
+#if MESH_TREE_DEBUG
+ GNUNET_PEER_resolve (p->peers[p->length - 1], &id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "tree: Adding path [%u] towards peer %s.\n", p->length,
+ GNUNET_i2s (&id));
+#endif
+
+ GNUNET_assert (0 != p->length);
+ parent = n = t->root;
+ if (n->peer != p->peers[0])
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ if (1 == p->length)
+ return GNUNET_OK;
+ oldnode = tree_del_path (t, p->peers[p->length - 1], cb, cbcls);
+ /* Look for the first node that is not already present in the tree
+ *
+ * Assuming that the tree is somewhat balanced, O(log n * log N).
+ * - Length of the path is expected to be log N (size of whole network).
+ * - Each level of the tree is expected to have log n children (size of tree).
+ */
+ me = t->root->peer == 1 ? 0 : -1;
+ for (i = 1; i < p->length; i++)
+ {
+#if MESH_TREE_DEBUG
+ GNUNET_PEER_resolve (p->peers[i], &id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Looking for peer %s.\n",
+ GNUNET_i2s (&id));
+#endif
+ parent = n;
+ if (p->peers[i] == 1)
+ me = i;
+ for (c = n->children_head; NULL != c; c = c->next)
+ {
+ if (c->peer == p->peers[i])
+ {
+#if MESH_TREE_DEBUG
+ GNUNET_PEER_resolve (parent->peer, &id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "tree: Found in children of %s.\n", GNUNET_i2s (&id));
+#endif
+ n = c;
+ break;
+ }
+ }
+ /* If we couldn't find a child equal to path[i], we have reached the end
+ * of the common path. */
+ if (parent == n)
+ break;
+ }
+#if MESH_TREE_DEBUG
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: All childen visited.\n");
+#endif
+ /* Add the rest of the path as a branch from parent. */
+ while (i < p->length)
+ {
+#if MESH_TREE_DEBUG
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Adding peer %u to %u.\n",
+ p->peers[i], parent->peer);
+ GNUNET_PEER_resolve (p->peers[i], &id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Adding peer %s.\n",
+ GNUNET_i2s (&id));
+ GNUNET_PEER_resolve (parent->peer, &id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: to %s.\n",
+ GNUNET_i2s (&id));
+#endif
+
+ if (i == p->length - 1 && NULL != oldnode)
+ {
+#if MESH_TREE_DEBUG
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "tree: Putting old node into place.\n");
+#endif
+ oldnode->parent = parent;
+ GNUNET_CONTAINER_DLL_insert (parent->children_head, parent->children_tail,
+ oldnode);
+ tree_node_update_first_hops (t, oldnode, NULL);
+ n = oldnode;
+ }
+ else
+ {
+#if MESH_TREE_DEBUG
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Creating new node.\n");
+#endif
+ n = tree_node_new (parent, p->peers[i]);
+ n->status = MESH_PEER_RELAY;
+ if (n->peer == 1)
+ {
+ t->me = n;
+ me = i;
+ }
+ }
+ i++;
+ parent = n;
+ }
+ n->status = MESH_PEER_SEARCHING;
+
+ GNUNET_break (-1 != me);
+
+ /* Add info about first hop into hashmap. */
+ if (-1 != me && me < p->length - 1)
+ {
+#if MESH_TREE_DEBUG
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "MESH: finding first hop (own pos %d/%u)\n", me,
+ p->length - 1);
+#endif
+ GNUNET_PEER_resolve (p->peers[me + 1], &id);
+ tree_update_first_hops (t, p->peers[me + 1], &id);
+ }
+#if MESH_TREE_DEBUG
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "MESH: was last in path, not updating first hops (%d/%u)\n",
+ me, p->length - 1);
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: New node added.\n");
+#endif
+ if (NULL == t->me)
+ t->me = tree_find_peer (t, 1);
+ return GNUNET_OK;
+}
+
+
+/**
+ * Notifies a tree that a connection it might be using is broken.
+ * Marks all peers down the paths as disconnected and notifies the client.
+ *
+ * @param t Tree to use.
+ * @param p1 Short id of one of the peers (order unimportant)
+ * @param p2 Short id of one of the peers (order unimportant)
+ * @param cb Function to call for every peer that is marked as disconnected.
+ * @param cbcls Closure for cb.
+ *
+ * @return Short ID of the first disconnected peer in the tree.
+ */
+GNUNET_PEER_Id
+tree_notify_connection_broken (struct MeshTunnelTree *t, GNUNET_PEER_Id p1,
+ GNUNET_PEER_Id p2, MeshTreeCallback cb,
+ void *cbcls)
+{
+ struct MeshTunnelTreeNode *n;
+ struct MeshTunnelTreeNode *c;
+
+ n = tree_find_peer (t, p1);
+ if (NULL == n)
+ return 0;
+ if (NULL != n->parent && n->parent->peer == p2)
+ {
+ tree_mark_peers_disconnected (t, n, cb, cbcls);
+ GNUNET_CONTAINER_DLL_remove (n->parent->children_head,
+ n->parent->children_tail, n);
+ GNUNET_CONTAINER_DLL_insert (t->disconnected_head, t->disconnected_tail, n);
+ return p1;
+ }
+ for (c = n->children_head; NULL != c; c = c->next)
+ {
+ if (c->peer == p2)
+ {
+ tree_mark_peers_disconnected (t, c, cb, cbcls);
+ GNUNET_CONTAINER_DLL_remove (n->children_head, n->children_tail, c);
+ GNUNET_CONTAINER_DLL_insert (t->disconnected_head, t->disconnected_tail,
+ c);
+ return p2;
+ }
+ }
+ return 0;
+}
+
+
+/**
+ * Deletes a peer from a tunnel, liberating all unused resources on the path to
+ * it. It shouldn't have children, if it has they will be destroyed as well.
+ * If the tree is not local and no longer has any paths, the root node will be
+ * destroyed and marked as NULL.
+ *
+ * @param t Tunnel tree to use.
+ * @param peer Short ID of the peer to remove from the tunnel tree.
+ * @param cb Callback to notify client of disconnected peers.
+ * @param cbcls Closure for cb.
+ *
+ * @return GNUNET_OK or GNUNET_SYSERR
+ */
+int
+tree_del_peer (struct MeshTunnelTree *t, GNUNET_PEER_Id peer,
+ MeshTreeCallback cb, void *cbcls)
+{
+ struct MeshTunnelTreeNode *n;
+
+ n = tree_del_path (t, peer, cb, cbcls);
+ if (NULL == n)
+ {
+ GNUNET_break (0);
+ return GNUNET_YES;
+ }
+ GNUNET_break_op (NULL == n->children_head);
+ tree_node_destroy (n);
+ if (NULL == t->root->children_head && t->me != t->root)
+ {
+ tree_node_destroy (t->root);
+ t->root = NULL;
+ return GNUNET_NO;
+ }
+ return GNUNET_YES;
+}
+
+
+
+/**
+ * Get the cost of the path relative to the already built tunnel tree.
+ *
+ * @param t The tunnel tree to which compare.
+ * @param path The individual path to reach a peer. It has to start at the
+ * root of the tree to be comparable.
+ *
+ * @return Number of hops to reach destination, UINT_MAX in case the peer is not
+ * in the path.
+ *
+ * TODO: adapt to allow any start / root combination
+ * TODO: take in account state of the nodes
+ */
+unsigned int
+tree_get_path_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path)
+{
+ struct MeshTunnelTreeNode *n;
+ struct MeshTunnelTreeNode *p;
+ unsigned int i;
+ unsigned int l;
+
+ l = path_get_length (path);
+ p = t->root;
+ if (t->root->peer != path->peers[0])
+ {
+ GNUNET_break (0);
+ return UINT_MAX;
+ }
+ for (i = 1; i < l; i++)
+ {
+ for (n = p->children_head; NULL != n; n = n->next)
+ {
+ if (path->peers[i] == n->peer)
+ {
+ break;
+ }
+ }
+ if (NULL == n)
+ return l - i;
+ p = n;
+ }
+ return l - i;
+}
+
+
+/**
+ * Print the tree on stderr
+ *
+ * @param t The tree
+ */
+void
+tree_debug (struct MeshTunnelTree *t)
+{
+ tree_node_debug (t->root, 0);
+}
+
+
+/**
+ * Iterator over hash map peer entries and frees all data in it.
+ * Used prior to destroying a hashmap. Makes you miss anonymous functions in C.
+ *
+ * @param cls closure
+ * @param key current key code (will no longer contain valid data!!)
+ * @param value value in the hash map (treated as void *)
+ * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
+ */
+static int
+iterate_free (void *cls, const GNUNET_HashCode * key, void *value)
+{
+ GNUNET_free (value);
+ return GNUNET_YES;
+}
+
+
+/**
+ * Destroy the whole tree and free all used memory and Peer_Ids
+ *
+ * @param t Tree to be destroyed
+ */
+void
+tree_destroy (struct MeshTunnelTree *t)
+{
+#if MESH_TREE_DEBUG
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Destroying tree\n");
+#endif
+ tree_node_destroy (t->root);
+ GNUNET_CONTAINER_multihashmap_iterate (t->first_hops, &iterate_free, NULL);
+ GNUNET_CONTAINER_multihashmap_destroy (t->first_hops);
+ GNUNET_free (t);
+}
diff --git a/src/mesh/mesh_tunnel_tree.h b/src/mesh/mesh_tunnel_tree.h
new file mode 100644
index 0000000..84fd0ac
--- /dev/null
+++ b/src/mesh/mesh_tunnel_tree.h
@@ -0,0 +1,345 @@
+/*
+ This file is part of GNUnet.
+ (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 3, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file mesh/mesh_tunnel_tree.h
+ * @brief Tunnel tree handling functions
+ * @author Bartlomiej Polot
+ */
+
+#include "mesh.h"
+
+/******************************************************************************/
+/************************ DATA STRUCTURES ****************************/
+/******************************************************************************/
+
+/**
+ * Information regarding a possible path to reach a single peer
+ */
+struct MeshPeerPath
+{
+
+ /**
+ * Linked list
+ */
+ struct MeshPeerPath *next;
+ struct MeshPeerPath *prev;
+
+ /**
+ * List of all the peers that form the path from origin to target.
+ */
+ GNUNET_PEER_Id *peers;
+
+ /**
+ * Number of peers (hops) in the path
+ */
+ unsigned int length;
+
+};
+
+
+/**
+ * Node of path tree for a tunnel
+ */
+struct MeshTunnelTreeNode;
+
+
+/**
+ * Tree to reach all peers in the tunnel
+ */
+struct MeshTunnelTree;
+
+
+/******************************************************************************/
+/************************* FUNCTIONS *****************************/
+/******************************************************************************/
+
+/**
+ * Create a new path.
+ *
+ * @param length How many hops will the path have.
+ *
+ * @return A newly allocated path with a peer array of the specified length.
+ */
+struct MeshPeerPath *
+path_new (unsigned int length);
+
+
+/**
+ * Invert the path.
+ *
+ * @param path The path to invert.
+ */
+void
+path_invert (struct MeshPeerPath *path);
+
+
+/**
+ * Duplicate a path, incrementing short peer's rc.
+ *
+ * @param path The path to duplicate.
+ */
+struct MeshPeerPath *
+path_duplicate (struct MeshPeerPath *path);
+
+
+/**
+ * Get the length of a path.
+ *
+ * @param path The path to measure, with the local peer at any point of it.
+ *
+ * @return Number of hops to reach destination.
+ * UINT_MAX in case the peer is not in the path.
+ */
+unsigned int
+path_get_length (struct MeshPeerPath *path);
+
+
+/**
+ * Destroy the path and free any allocated resources linked to it
+ *
+ * @param p the path to destroy
+ *
+ * @return GNUNET_OK on success
+ */
+int
+path_destroy (struct MeshPeerPath *p);
+
+
+/******************************************************************************/
+
+/**
+ * Method called whenever a node has been marked as disconnected.
+ *
+ * @param cls Closure.
+ * @param peer_id short ID of peer that is no longer reachable.
+ */
+typedef void (*MeshTreeCallback) (void *cls, GNUNET_PEER_Id peer_id);
+
+
+/**
+ * Create a new tunnel tree associated to a tunnel
+ *
+ * @param peer A short peer id of the root of the tree
+ *
+ * @return A newly allocated and initialized tunnel tree
+ */
+struct MeshTunnelTree *
+tree_new (GNUNET_PEER_Id peer);
+
+
+/**
+ * Set the status of a node.
+ *
+ * @param tree Tree.
+ * @param peer A short peer id of the node.
+ * @param status New status to set.
+ */
+void
+tree_set_status (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer,
+ enum MeshPeerState status);
+
+
+/**
+ * Get the status of a node.
+ *
+ * @param tree Tree whose local id we want to now.
+ * @param peer A short peer id of the node.
+ *
+ * @return Short peer id of local peer.
+ */
+enum MeshPeerState
+tree_get_status (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer);
+
+
+/**
+ * Get the id of the predecessor of the local node.
+ *
+ * @param tree Tree whose local id we want to now.
+ *
+ * @return Short peer id of local peer.
+ */
+GNUNET_PEER_Id
+tree_get_predecessor (struct MeshTunnelTree *tree);
+
+
+/**
+ * Find the first peer whom to send a packet to go down this path
+ *
+ * @param t The tunnel tree to use
+ * @param peer The peerinfo of the peer we are trying to reach
+ *
+ * @return peerinfo of the peer who is the first hop in the tunnel
+ * NULL on error
+ */
+struct GNUNET_PeerIdentity *
+tree_get_first_hop (struct MeshTunnelTree *t, GNUNET_PEER_Id peer);
+
+
+/**
+ * Find the given peer in the tree.
+ *
+ * @param tree Tree where to look for the peer.
+ * @param peer_id Peer to find.
+ *
+ * @return Pointer to the node of the peer. NULL if not found.
+ */
+struct MeshTunnelTreeNode *
+tree_find_peer (struct MeshTunnelTree *tree, GNUNET_PEER_Id peer_id);
+
+
+/**
+ * Iterate over all children of the local node.
+ *
+ * @param tree Tree to use. Must have "me" set.
+ * @param cb Callback to call over each child.
+ * @param cls Closure.
+ */
+void
+tree_iterate_children (struct MeshTunnelTree *tree, MeshTreeCallback cb,
+ void *cls);
+
+
+/**
+ * Recusively update the info about what is the first hop to reach the node
+ *
+ * @param tree Tree this nodes belongs to.
+ * @param parent_id Short ID from node form which to start updating.
+ * @param hop If known, ID of the first hop.
+ * If not known, NULL to find out and pass on children.
+ */
+void
+tree_update_first_hops (struct MeshTunnelTree *tree, GNUNET_PEER_Id parent_id,
+ struct GNUNET_PeerIdentity *hop);
+
+/**
+ * Delete the current path to the peer, including all now unused relays.
+ * The destination peer is NOT destroyed, it is returned in order to either set
+ * a new path to it or destroy it explicitly, taking care of it's child nodes.
+ *
+ * @param t Tunnel tree where to delete the path from.
+ * @param peer_id Short ID of the destination peer whose path we want to remove.
+ * @param cb Callback to use to notify about which peers are going to be
+ * disconnected.
+ * @param cbcls Closure for cb.
+ *
+ * @return pointer to the pathless node.
+ * NULL when not found
+ */
+struct MeshTunnelTreeNode *
+tree_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
+ MeshTreeCallback cb, void *cbcls);
+
+
+/**
+ * Return a newly allocated individual path to reach a peer from the local peer,
+ * according to the path tree of some tunnel.
+ *
+ * @param t Tunnel from which to read the path tree
+ * @param peer Destination peer to whom we want a path
+ *
+ * @return A newly allocated individual path to reach the destination peer.
+ * Path must be destroyed afterwards.
+ */
+struct MeshPeerPath *
+tree_get_path_to_peer (struct MeshTunnelTree *t, GNUNET_PEER_Id peer);
+
+
+/**
+ * Integrate a stand alone path into the tunnel tree.
+ *
+ * @param t Tunnel where to add the new path.
+ * @param p Path to be integrated.
+ * @param cb Callback to use to notify about peers temporarily disconnecting.
+ * @param cbcls Closure for cb.
+ *
+ * @return GNUNET_OK in case of success.
+ * GNUNET_SYSERR in case of error.
+ */
+int
+tree_add_path (struct MeshTunnelTree *t, const struct MeshPeerPath *p,
+ MeshTreeCallback cb, void *cbcls);
+
+
+/**
+ * Notifies a tree that a connection it might be using is broken.
+ * Marks all peers down the paths as disconnected and notifies the client.
+ *
+ * @param t Tree to use.
+ * @param p1 Short id of one of the peers (order unimportant)
+ * @param p2 Short id of one of the peers (order unimportant)
+ * @param cb Function to call for every peer that is marked as disconnected.
+ * @param cbcls Closure for cb.
+ *
+ * @return Short ID of the first disconnected peer in the tree.
+ */
+GNUNET_PEER_Id
+tree_notify_connection_broken (struct MeshTunnelTree *t, GNUNET_PEER_Id p1,
+ GNUNET_PEER_Id p2, MeshTreeCallback cb,
+ void *cbcls);
+
+
+/**
+ * Deletes a peer from a tunnel, liberating all unused resources on the path to
+ * it. It shouldn't have children, if it has they will be destroyed as well.
+ * If the tree is not local and no longer has any paths, the root node will be
+ * destroyed and marked as NULL.
+ *
+ * @param t Tunnel tree to use.
+ * @param peer Short ID of the peer to remove from the tunnel tree.
+ * @param cb Callback to notify client of disconnected peers.
+ * @param cbcls Closure for cb.
+ *
+ * @return GNUNET_YES if the tunnel still has nodes
+ */
+int
+tree_del_peer (struct MeshTunnelTree *t, GNUNET_PEER_Id peer,
+ MeshTreeCallback cb, void *cbcls);
+
+
+/**
+ * Get the cost of the path relative to the already built tunnel tree
+ *
+ * @param t The tunnel tree to which compare
+ * @param path The individual path to reach a peer
+ *
+ * @return Number of hops to reach destination, UINT_MAX in case the peer is not
+ * in the path
+ */
+unsigned int
+tree_get_path_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path);
+
+
+/**
+ * Print the tree on stderr
+ *
+ * @param t The tree
+ */
+void
+tree_debug (struct MeshTunnelTree *t);
+
+
+/**
+ * Destroy the whole tree and free all used memory and Peer_Ids
+ *
+ * @param t Tree to be destroyed
+ */
+void
+tree_destroy (struct MeshTunnelTree *t);
diff --git a/src/mesh/test_mesh.conf b/src/mesh/test_mesh.conf
new file mode 100644
index 0000000..3d955f0
--- /dev/null
+++ b/src/mesh/test_mesh.conf
@@ -0,0 +1,69 @@
+[fs]
+AUTOSTART = NO
+
+[resolver]
+AUTOSTART = NO
+
+[mesh]
+DEBUG = YES
+AUTOSTART = YES
+ACCEPT_FROM = 127.0.0.1;
+HOSTNAME = localhost
+PORT = 10511
+# PREFIX = valgrind --leak-check=full
+# PREFIX = xterm -geometry 100x85 -T peer1 -e gdb --args
+
+[dht]
+DEBUG = NO
+AUTOSTART = YES
+ACCEPT_FROM6 = ::1;
+ACCEPT_FROM = 127.0.0.1;
+HOSTNAME = localhost
+PORT = 2100
+
+[block]
+plugins = dht test
+
+[dhtcache]
+QUOTA = 1 MB
+DATABASE = sqlite
+
+[transport]
+PLUGINS = tcp
+DEBUG = NO
+ACCEPT_FROM6 = ::1;
+ACCEPT_FROM = 127.0.0.1;
+NEIGHBOUR_LIMIT = 50
+PORT = 12365
+
+[ats]
+WAN_QUOTA_OUT = 3932160
+WAN_QUOTA_IN = 3932160
+
+[core]
+PORT = 12092
+
+[arm]
+DEFAULTSERVICES = core
+PORT = 12366
+DEBUG = NO
+
+[transport-tcp]
+TIMEOUT = 300 s
+PORT = 12368
+
+[TESTING]
+WEAKRANDOM = YES
+
+[gnunetd]
+HOSTKEY = $SERVICEHOME/.hostkey
+
+[PATHS]
+DEFAULTCONFIG = test_mesh.conf
+SERVICEHOME = /tmp/test-mesh/
+
+[dns]
+AUTOSTART = NO
+
+[nse]
+AUTOSTART = NO
diff --git a/src/mesh/test_mesh_2dtorus.c b/src/mesh/test_mesh_2dtorus.c
new file mode 100644
index 0000000..9946fe2
--- /dev/null
+++ b/src/mesh/test_mesh_2dtorus.c
@@ -0,0 +1,370 @@
+/*
+ This file is part of GNUnet.
+ (C) 2011 Christian Grothoff (and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 3, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+/**
+ * @file mesh/test_mesh_2dtorus.c
+ *
+ * @brief Test for creating a 2dtorus.
+ */
+#include "platform.h"
+#include "gnunet_testing_lib.h"
+
+#define VERBOSE GNUNET_YES
+#define REMOVE_DIR GNUNET_YES
+
+/**
+ * How long until we give up on connecting the peers?
+ */
+#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
+
+/**
+ * Time to wait for stuff that should be rather fast
+ */
+#define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
+
+
+/**
+ * How many events have happened
+ */
+static int ok;
+
+/**
+ * Be verbose
+ */
+static int verbose;
+
+/**
+ * Total number of peers in the test.
+ */
+static unsigned long long num_peers;
+
+/**
+ * Global configuration file
+ */
+static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
+
+/**
+ * Total number of currently running peers.
+ */
+static unsigned long long peers_running;
+
+/**
+ * Total number of successful connections in the whole network.
+ */
+static unsigned int total_connections;
+
+/**
+ * Total number of counted topo connections
+ */
+static unsigned int topo_connections;
+
+/**
+ * Total number of failed connections in the whole network.
+ */
+static unsigned int failed_connections;
+
+/**
+ * The currently running peer group.
+ */
+static struct GNUNET_TESTING_PeerGroup *pg;
+
+/**
+ * Task called to disconnect peers
+ */
+static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
+
+/**
+ * Task called to shutdown test.
+ */
+static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
+
+
+/**
+ * Check whether peers successfully shut down.
+ */
+static void
+shutdown_callback (void *cls, const char *emsg)
+{
+ if (emsg != NULL)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "test: Shutdown of peers failed! (%s)\n", emsg);
+ ok--;
+ }
+#if VERBOSE
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "test: All peers successfully shut down!\n");
+ }
+#endif
+ GNUNET_CONFIGURATION_destroy (testing_cfg);
+}
+
+
+static void
+shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+#if VERBOSE
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
+#endif
+
+ GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
+}
+
+
+static void
+disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: disconnecting peers\n");
+
+ if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
+ {
+ GNUNET_SCHEDULER_cancel (shutdown_handle);
+ shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
+ }
+}
+
+
+/**
+ * Prototype of a callback function indicating that two peers
+ * are currently connected.
+ *
+ * @param cls closure
+ * @param first peer id for first daemon
+ * @param second peer id for the second daemon
+ * @param distance distance between the connected peers
+ * @param emsg error message (NULL on success)
+ */
+void
+topo_cb (void *cls, const struct GNUNET_PeerIdentity *first,
+ const struct GNUNET_PeerIdentity *second, const char *emsg)
+{
+ topo_connections++;
+ if (NULL != emsg)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: Error by topo %u: %s\n",
+ topo_connections, emsg);
+ }
+ else
+ {
+ if (first == NULL || second == NULL)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Connection %u NULL\n",
+ topo_connections);
+ if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
+ {
+ GNUNET_SCHEDULER_cancel (disconnect_task);
+ GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
+ }
+ return;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Connection %u ok\n",
+ topo_connections);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: %s\n", GNUNET_i2s (first));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: %s\n", GNUNET_i2s (second));
+ }
+}
+
+
+/**
+ * peergroup_ready: start test when all peers are connected
+ * @param cls closure
+ * @param emsg error message
+ */
+static void
+peergroup_ready (void *cls, const char *emsg)
+{
+ if (emsg != NULL)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "test: Peergroup callback called with error, aborting test!\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Error from testing: `%s'\n",
+ emsg);
+ ok--;
+ GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
+ return;
+ }
+#if VERBOSE
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "************************************************************\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "test: Peer Group started successfully!\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Have %u connections\n",
+ total_connections);
+#endif
+
+ peers_running = GNUNET_TESTING_daemons_running (pg);
+ if (0 < failed_connections)
+ {
+ ok = GNUNET_SYSERR;
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: %u connections have FAILED!\n",
+ failed_connections);
+ disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
+
+ }
+ else
+ {
+ GNUNET_TESTING_get_topology (pg, &topo_cb, NULL);
+ disconnect_task =
+ GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_peers, NULL);
+ ok = GNUNET_OK;
+ }
+
+}
+
+
+/**
+ * Function that will be called whenever two daemons are connected by
+ * the testing library.
+ *
+ * @param cls closure
+ * @param first peer id for first daemon
+ * @param second peer id for the second daemon
+ * @param distance distance between the connected peers
+ * @param first_cfg config for the first daemon
+ * @param second_cfg config for the second daemon
+ * @param first_daemon handle for the first daemon
+ * @param second_daemon handle for the second daemon
+ * @param emsg error message (NULL on success)
+ */
+static void
+connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
+ const struct GNUNET_PeerIdentity *second, uint32_t distance,
+ const struct GNUNET_CONFIGURATION_Handle *first_cfg,
+ const struct GNUNET_CONFIGURATION_Handle *second_cfg,
+ struct GNUNET_TESTING_Daemon *first_daemon,
+ struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
+{
+ if (emsg == NULL)
+ {
+ total_connections++;
+ }
+ else
+ {
+ failed_connections++;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "test: Problem with new connection (%s)\n", emsg);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: (%s)\n", GNUNET_i2s (first));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: (%s)\n", GNUNET_i2s (second));
+ }
+
+}
+
+
+/**
+ * run: load configuration options and schedule test to run (start peergroup)
+ * @param cls closure
+ * @param args argv
+ * @param cfgfile configuration file name (can be NULL)
+ * @param cfg configuration handle
+ */
+static void
+run (void *cls, char *const *args, const char *cfgfile,
+ const struct GNUNET_CONFIGURATION_Handle *cfg)
+{
+ struct GNUNET_TESTING_Host *hosts;
+
+ ok = GNUNET_NO;
+ total_connections = 0;
+ failed_connections = 0;
+ testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
+
+ GNUNET_log_setup ("test_mesh_2dtorus",
+#if VERBOSE
+ "DEBUG",
+#else
+ "WARNING",
+#endif
+ NULL);
+
+#if VERBOSE
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Starting daemons.\n");
+ GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
+ "use_progressbars", "YES");
+#endif
+
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
+ "num_peers", &num_peers))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Option TESTING:NUM_PEERS is required!\n");
+ return;
+ }
+
+ hosts = GNUNET_TESTING_hosts_load (testing_cfg);
+
+ pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
+ &connect_cb, &peergroup_ready, NULL,
+ hosts);
+ GNUNET_assert (pg != NULL);
+ shutdown_handle =
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
+ &shutdown_task, NULL);
+}
+
+
+/**
+ * test_mesh_2dtorus command line options
+ */
+static struct GNUNET_GETOPT_CommandLineOption options[] = {
+ {'V', "verbose", NULL,
+ gettext_noop ("be verbose (print progress information)"),
+ 0, &GNUNET_GETOPT_set_one, &verbose},
+ GNUNET_GETOPT_OPTION_END
+};
+
+
+/**
+ * Main: start test
+ */
+int
+main (int argc, char *argv[])
+{
+ char *const argv2[] = {
+ argv[0],
+ "-c",
+ "test_mesh_2dtorus.conf",
+#if VERBOSE
+ "-L",
+ "DEBUG",
+#endif
+ NULL
+ };
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Start\n");
+
+
+ GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
+ "test_mesh_2dtorus", gettext_noop ("Test mesh 2d torus."),
+ options, &run, NULL);
+#if REMOVE_DIR
+ GNUNET_DISK_directory_remove ("/tmp/test_mesh_2dtorus");
+#endif
+ if (GNUNET_OK != ok)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: FAILED!\n");
+ return 1;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: success\n");
+ return 0;
+}
+
+/* end of test_mesh_2dtorus.c */
diff --git a/src/mesh/test_mesh_2dtorus.conf b/src/mesh/test_mesh_2dtorus.conf
new file mode 100644
index 0000000..9a2227b
--- /dev/null
+++ b/src/mesh/test_mesh_2dtorus.conf
@@ -0,0 +1,87 @@
+[PATHS]
+SERVICEHOME = /tmp/test_mesh_small/
+DEFAULTCONFIG = test_mesh_small.conf
+
+[arm]
+PORT = 10010
+DEFAULTSERVICES = core dht mesh
+#DEBUG = YES
+
+[statistics]
+AUTOSTART = YES
+PORT = 10000
+
+[dht]
+DEBUG = NO
+AUTOSTART = YES
+ACCEPT_FROM6 = ::1;
+ACCEPT_FROM = 127.0.0.1;
+HOSTNAME = localhost
+PORT = 10001
+
+[nse]
+WORKBITS = 0
+
+[dns]
+AUTOSTART = NO
+PORT = 10011
+
+[transport]
+PORT = 10002
+AUTOSTART = YES
+
+[nat]
+DISABLEV6 = YES
+BINDTO = 127.0.0.1
+ENABLE_UPNP = NO
+BEHIND_NAT = NO
+ALLOW_NAT = NO
+INTERNAL_ADDRESS = 127.0.0.1
+EXTERNAL_ADDRESS = 127.0.0.1
+
+[ats]
+WAN_QUOTA_IN = 1 GB
+WAN_QUOTA_OUT = 1 GB
+
+[core]
+AUTOSTART = YES
+PORT = 10003
+
+[peerinfo]
+AUTOSTART = YES
+PORT = 10004
+
+[mesh]
+PORT = 10005
+DEBUG=YES
+ACCEPT_FROM = 127.0.0.1;
+HOSTNAME = localhost
+# PREFIX = valgrind --leak-check=full
+# PREFIX = xterm -geometry 100x85 -T peer1 -e gdb --args
+
+[testing]
+NUM_PEERS = 16
+WEAKRANDOM = YES
+TOPOLOGY = 2D_TORUS
+CONNECT_TOPOLOGY = 2D_TORUS
+#TOPOLOGY_FILE = small.dat
+CONNECT_TOPOLOGY = 2D_TORUS
+#CONNECT_TOPOLOGY_OPTION = CONNECT_MINIMUM
+#CONNECT_TOPOLOGY_OPTION_MODIFIER = 25
+#PERCENTAGE = 3
+#PROBABILITY = .1
+F2F = NO
+CONNECT_TIMEOUT = 600 s
+CONNECT_ATTEMPTS = 2
+DEBUG = YES
+HOSTKEYSFILE = ../../contrib/testing_hostkeys.dat
+MAX_CONCURRENT_SSH = 10
+USE_PROGRESSBARS = YES
+PEERGROUP_TIMEOUT = 2400 s
+TOPOLOGY_OUTPUT_FILE = mesh_topo_initial
+MAX_OUTSTANDING_CONNECTIONS = 75
+#SINGLE_PEERINFO_PER_HOST = YES
+#NUM_PEERINFO_PER_HOST = 10
+#SINGLE_STATISTICS_PER_HOST = YES
+#NUM_STATISTICS_PER_HOST = 10
+DELETE_FILES = YES
diff --git a/src/mesh/test_mesh_api.c b/src/mesh/test_mesh_api.c
new file mode 100644
index 0000000..fbc1fba
--- /dev/null
+++ b/src/mesh/test_mesh_api.c
@@ -0,0 +1,192 @@
+/*
+ This file is part of GNUnet.
+ (C) 2011 Christian Grothoff (and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 3, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file mesh/test_mesh_api.c
+ * @brief test mesh api: dummy test of callbacks
+ * @author Bartlomiej Polot
+ */
+
+#include "platform.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_dht_service.h"
+#include "gnunet_mesh_service.h"
+
+#define VERBOSE 1
+#define VERBOSE_ARM 0
+
+static struct GNUNET_OS_Process *arm_pid;
+static struct GNUNET_MESH_Handle *mesh;
+static struct GNUNET_MESH_Tunnel *t;
+static int result;
+static GNUNET_SCHEDULER_TaskIdentifier abort_task;
+static GNUNET_SCHEDULER_TaskIdentifier test_task;
+
+/**
+ * Function is called whenever a message is received.
+ *
+ * @param cls closure (set from GNUNET_MESH_connect)
+ * @param tunnel connection to the other end
+ * @param tunnel_ctx place to store local state associated with the tunnel
+ * @param sender who sent the message
+ * @param message the actual message
+ * @param atsi performance data for the connection
+ * @return GNUNET_OK to keep the connection open,
+ * GNUNET_SYSERR to close it (signal serious error)
+ */
+static int
+callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
+ const struct GNUNET_PeerIdentity *sender,
+ const struct GNUNET_MessageHeader *message,
+ const struct GNUNET_ATS_Information *atsi)
+{
+ return GNUNET_OK;
+}
+
+static struct GNUNET_MESH_MessageHandler handlers[] = { {&callback, 1, 0},
+{NULL, 0, 0}
+};
+
+
+static void
+do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ if (NULL != t)
+ {
+ GNUNET_MESH_tunnel_destroy (t);
+ }
+ if (0 != abort_task)
+ {
+ GNUNET_SCHEDULER_cancel (abort_task);
+ }
+ if (NULL != mesh)
+ {
+ GNUNET_MESH_disconnect (mesh);
+ }
+ if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
+ {
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
+ }
+ GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
+ GNUNET_OS_process_close (arm_pid);
+}
+
+static void
+do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ if (0 != test_task)
+ {
+ GNUNET_SCHEDULER_cancel (test_task);
+ }
+ result = GNUNET_SYSERR;
+ abort_task = 0;
+ do_shutdown (cls, tc);
+}
+
+static void
+test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct GNUNET_CONFIGURATION_Handle *cfg = cls;
+ static const GNUNET_MESH_ApplicationType app[] =
+ { 1, 2, 3, 4, 5, 6, 7, 8, 0 };
+
+ test_task = (GNUNET_SCHEDULER_TaskIdentifier) 0;
+ mesh = GNUNET_MESH_connect (cfg, 10, NULL, NULL, NULL, handlers, app);
+ if (NULL == mesh)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: Couldn't connect to mesh :(\n");
+ return;
+ }
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: YAY! CONNECTED TO MESH :D\n");
+ }
+
+ t = GNUNET_MESH_tunnel_create (mesh, NULL, NULL, NULL, NULL);
+
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
+ (GNUNET_TIME_UNIT_SECONDS, 5), &do_shutdown,
+ NULL);
+}
+
+
+static void
+run (void *cls, char *const *args, const char *cfgfile,
+ const struct GNUNET_CONFIGURATION_Handle *cfg)
+{
+ GNUNET_log_setup ("test_mesh_api",
+#if VERBOSE
+ "DEBUG",
+#else
+ "WARNING",
+#endif
+ NULL);
+ arm_pid =
+ GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
+ "gnunet-service-arm",
+#if VERBOSE_ARM
+ "-L", "DEBUG",
+#endif
+ "-c", "test_mesh.conf", NULL);
+
+ abort_task =
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
+ (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
+ NULL);
+
+ test_task = GNUNET_SCHEDULER_add_now (&test, (void *) cfg);
+
+}
+
+
+int
+main (int argc, char *argv[])
+{
+ int ret;
+
+ char *const argv2[] = { "test-mesh-api",
+ "-c", "test_mesh.conf",
+#if VERBOSE
+ "-L", "DEBUG",
+#endif
+ NULL
+ };
+ struct GNUNET_GETOPT_CommandLineOption options[] = {
+ GNUNET_GETOPT_OPTION_END
+ };
+
+ ret =
+ GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
+ "test-mesh-api", "nohelp", options, &run, NULL);
+
+ if (GNUNET_OK != ret)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
+ ret);
+ return 1;
+ }
+ if (GNUNET_SYSERR == result)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
+ return 1;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");
+ return 0;
+}
diff --git a/src/mesh/test_mesh_local_1.c b/src/mesh/test_mesh_local_1.c
new file mode 100644
index 0000000..73e2bdc
--- /dev/null
+++ b/src/mesh/test_mesh_local_1.c
@@ -0,0 +1,357 @@
+/*
+ This file is part of GNUnet.
+ (C) 2011 Christian Grothoff (and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 3, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file mesh/test_mesh_local.c
+ * @brief test mesh local: test of tunnels with just one peer
+ * @author Bartlomiej Polot
+ */
+
+#include "platform.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_dht_service.h"
+#include "gnunet_mesh_service.h"
+
+#define VERBOSE 1
+#define VERBOSE_ARM 0
+
+static struct GNUNET_OS_Process *arm_pid;
+static struct GNUNET_MESH_Handle *mesh_peer_1;
+static struct GNUNET_MESH_Handle *mesh_peer_2;
+static struct GNUNET_MESH_Tunnel *t;
+static unsigned int one = 1;
+static unsigned int two = 2;
+
+static int result;
+static GNUNET_SCHEDULER_TaskIdentifier abort_task;
+static GNUNET_SCHEDULER_TaskIdentifier test_task;
+static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
+
+
+/**
+ * Shutdown nicely
+ */
+static void
+do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
+ if (0 != abort_task)
+ {
+ GNUNET_SCHEDULER_cancel (abort_task);
+ }
+ if (NULL != t)
+ {
+ GNUNET_MESH_tunnel_destroy(t);
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: D1\n");
+ if (NULL != mesh_peer_1)
+ {
+ GNUNET_MESH_disconnect (mesh_peer_1);
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: D2\n");
+ if (NULL != mesh_peer_2)
+ {
+ GNUNET_MESH_disconnect (mesh_peer_2);
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n");
+ if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
+ {
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
+ GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
+ GNUNET_OS_process_close (arm_pid);
+}
+
+
+/**
+ * Something went wrong and timed out. Kill everything and set error flag
+ */
+static void
+do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
+ if (0 != test_task)
+ {
+ GNUNET_SCHEDULER_cancel (test_task);
+ }
+ result = GNUNET_SYSERR;
+ abort_task = 0;
+ if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
+ {
+ GNUNET_SCHEDULER_cancel (shutdown_task);
+ shutdown_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+ do_shutdown (cls, tc);
+}
+
+
+/**
+ * Function is called whenever a message is received.
+ *
+ * @param cls closure (set from GNUNET_MESH_connect)
+ * @param tunnel connection to the other end
+ * @param tunnel_ctx place to store local state associated with the tunnel
+ * @param sender who sent the message
+ * @param message the actual message
+ * @param atsi performance data for the connection
+ * @return GNUNET_OK to keep the connection open,
+ * GNUNET_SYSERR to close it (signal serious error)
+ */
+static int
+data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
+ const struct GNUNET_PeerIdentity *sender,
+ const struct GNUNET_MessageHeader *message,
+ const struct GNUNET_ATS_Information *atsi)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Data callback\n");
+ if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
+ GNUNET_SCHEDULER_cancel (shutdown_task);
+ shutdown_task =
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
+ (GNUNET_TIME_UNIT_SECONDS, 2), &do_shutdown,
+ NULL);
+ return GNUNET_OK;
+}
+
+
+/**
+ * Method called whenever another peer has added us to a tunnel
+ * the other peer initiated.
+ *
+ * @param cls closure
+ * @param tunnel new handle to the tunnel
+ * @param initiator peer that started the tunnel
+ * @param atsi performance information for the tunnel
+ * @return initial tunnel context for the tunnel (can be NULL -- that's not an error)
+ */
+static void *
+inbound_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
+ const struct GNUNET_PeerIdentity *initiator,
+ const struct GNUNET_ATS_Information *atsi)
+{
+ unsigned int id = *(unsigned int *) cls;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: received incoming tunnel\n");
+ if (id != 1)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "test: received incoming tunnel on peer 2\n");
+ result = GNUNET_SYSERR;
+ }
+ return NULL;
+}
+
+
+/**
+ * Function called whenever an inbound tunnel is destroyed. Should clean up
+ * any associated state.
+ *
+ * @param cls closure (set from GNUNET_MESH_connect)
+ * @param tunnel connection to the other end (henceforth invalid)
+ * @param tunnel_ctx place where local state associated
+ * with the tunnel is stored
+ */
+static void
+inbound_end (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
+ void *tunnel_ctx)
+{
+ unsigned int id = *(unsigned int *) cls;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: incoming tunnel closed\n");
+ if (id != 1)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "test: received closing tunnel on peer 2\n");
+ result = GNUNET_SYSERR;
+ }
+}
+
+
+/**
+ * Method called whenever a peer has connected to the tunnel.
+ *
+ * @param cls closure
+ * @param peer peer identity the tunnel stopped working with
+ */
+static void
+peer_conected (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_ATS_Information *atsi)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: peer connected\n");
+ if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
+ GNUNET_SCHEDULER_cancel(shutdown_task);
+ shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
+ &do_shutdown, NULL);
+}
+
+
+/**
+ * Method called whenever a peer has connected to the tunnel.
+ *
+ * @param cls closure
+ * @param peer peer identity the tunnel was created to, NULL on timeout
+ * @param atsi performance data for the connection
+ */
+static void
+peer_disconnected (void *cls, const struct GNUNET_PeerIdentity *peer)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: peer disconnected\n");
+}
+
+
+/**
+ * Handler array for traffic received on peer1
+ */
+static struct GNUNET_MESH_MessageHandler handlers1[] = {
+ {&data_callback, 1, 0},
+ {NULL, 0, 0}
+};
+
+
+/**
+ * Handler array for traffic received on peer2 (none expected)
+ */
+static struct GNUNET_MESH_MessageHandler handlers2[] = { {NULL, 0, 0} };
+
+
+/**
+ * Start looking for a peer by type
+ */
+static void
+do_find (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: CONNECT BY TYPE\n");
+ GNUNET_MESH_peer_request_connect_by_type (t, 1);
+}
+
+
+/**
+ * Main test function
+ */
+static void
+test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct GNUNET_CONFIGURATION_Handle *cfg = cls;
+ static const GNUNET_MESH_ApplicationType app1[] = { 1, 0 };
+ static const GNUNET_MESH_ApplicationType app2[] = { 0 };
+
+ test_task = GNUNET_SCHEDULER_NO_TASK;
+ mesh_peer_1 = GNUNET_MESH_connect (cfg, /* configuration */
+ 10, /* queue size */
+ (void *) &one, /* cls */
+ &inbound_tunnel, /* inbound new hndlr */
+ &inbound_end, /* inbound end hndlr */
+ handlers1, /* traffic handlers */
+ app1); /* apps offered */
+
+ mesh_peer_2 = GNUNET_MESH_connect (cfg, /* configuration */
+ 10, /* queue size */
+ (void *) &two, /* cls */
+ &inbound_tunnel, /* inbound new hndlr */
+ &inbound_end, /* inbound end hndlr */
+ handlers2, /* traffic handlers */
+ app2); /* apps offered */
+ if (NULL == mesh_peer_1 || NULL == mesh_peer_2)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: Couldn't connect to mesh :(\n");
+ return;
+ }
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: YAY! CONNECTED TO MESH :D\n");
+ }
+
+ t = GNUNET_MESH_tunnel_create (mesh_peer_2, NULL, &peer_conected,
+ &peer_disconnected, (void *) &two);
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_find, NULL);
+}
+
+
+/**
+ * Initialize framework and start test
+ */
+static void
+run (void *cls, char *const *args, const char *cfgfile,
+ const struct GNUNET_CONFIGURATION_Handle *cfg)
+{
+ GNUNET_log_setup ("test_mesh_local",
+#if VERBOSE
+ "DEBUG",
+#else
+ "WARNING",
+#endif
+ NULL);
+ arm_pid =
+ GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
+ "gnunet-service-arm",
+#if VERBOSE_ARM
+ "-L", "DEBUG",
+#endif
+ "-c", "test_mesh.conf", NULL);
+
+ abort_task =
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
+ (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
+ NULL);
+
+ test_task = GNUNET_SCHEDULER_add_now (&test, (void *) cfg);
+
+}
+
+
+/**
+ * Main
+ */
+int
+main (int argc, char *argv[])
+{
+ int ret;
+
+ char *const argv2[] = { "test-mesh-local",
+ "-c", "test_mesh.conf",
+#if VERBOSE
+ "-L", "DEBUG",
+#endif
+ NULL
+ };
+ struct GNUNET_GETOPT_CommandLineOption options[] = {
+ GNUNET_GETOPT_OPTION_END
+ };
+
+ result = GNUNET_OK;
+ ret =
+ GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
+ "test-mesh-local", "nohelp", options, &run, NULL);
+
+ if (GNUNET_OK != ret)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
+ ret);
+ return 1;
+ }
+ if (GNUNET_SYSERR == result)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
+ return 1;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");
+ return 0;
+}
diff --git a/src/mesh/test_mesh_local_2.c b/src/mesh/test_mesh_local_2.c
new file mode 100644
index 0000000..b185f1b
--- /dev/null
+++ b/src/mesh/test_mesh_local_2.c
@@ -0,0 +1,349 @@
+/*
+ This file is part of GNUnet.
+ (C) 2011 Christian Grothoff (and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 3, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file mesh/test_mesh_local.c
+ * @brief test mesh local: test of tunnels with just one peer
+ * @author Bartlomiej Polot
+ */
+
+#include "platform.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_dht_service.h"
+#include "gnunet_mesh_service.h"
+
+#define VERBOSE 1
+#define VERBOSE_ARM 0
+
+static struct GNUNET_OS_Process *arm_pid;
+static struct GNUNET_MESH_Handle *mesh_peer_1;
+static struct GNUNET_MESH_Handle *mesh_peer_2;
+static struct GNUNET_MESH_Tunnel *t;
+static unsigned int one = 1;
+static unsigned int two = 2;
+
+static int result;
+static GNUNET_SCHEDULER_TaskIdentifier abort_task;
+static GNUNET_SCHEDULER_TaskIdentifier test_task;
+
+
+/**
+ * Shutdown nicely
+ */
+static void
+do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
+ if (0 != abort_task)
+ {
+ GNUNET_SCHEDULER_cancel (abort_task);
+ }
+ if (NULL != t)
+ {
+ GNUNET_MESH_tunnel_destroy(t);
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: D1\n");
+ if (NULL != mesh_peer_1)
+ {
+ GNUNET_MESH_disconnect (mesh_peer_1);
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: D2\n");
+ if (NULL != mesh_peer_2)
+ {
+ GNUNET_MESH_disconnect (mesh_peer_2);
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n");
+ if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
+ {
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
+ GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
+ GNUNET_OS_process_close (arm_pid);
+}
+
+
+/**
+ * Something went wrong and timed out. Kill everything and set error flag
+ */
+static void
+do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
+ if (0 != test_task)
+ {
+ GNUNET_SCHEDULER_cancel (test_task);
+ }
+ result = GNUNET_SYSERR;
+ abort_task = 0;
+ do_shutdown (cls, tc);
+}
+
+
+/**
+ * Function is called whenever a message is received.
+ *
+ * @param cls closure (set from GNUNET_MESH_connect)
+ * @param tunnel connection to the other end
+ * @param tunnel_ctx place to store local state associated with the tunnel
+ * @param sender who sent the message
+ * @param message the actual message
+ * @param atsi performance data for the connection
+ * @return GNUNET_OK to keep the connection open,
+ * GNUNET_SYSERR to close it (signal serious error)
+ */
+static int
+data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
+ const struct GNUNET_PeerIdentity *sender,
+ const struct GNUNET_MessageHeader *message,
+ const struct GNUNET_ATS_Information *atsi)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Data callback\n");
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
+ (GNUNET_TIME_UNIT_SECONDS, 2), &do_shutdown,
+ NULL);
+ return GNUNET_OK;
+}
+
+
+/**
+ * Method called whenever another peer has added us to a tunnel
+ * the other peer initiated.
+ *
+ * @param cls closure
+ * @param tunnel new handle to the tunnel
+ * @param initiator peer that started the tunnel
+ * @param atsi performance information for the tunnel
+ * @return initial tunnel context for the tunnel (can be NULL -- that's not an error)
+ */
+static void *
+inbound_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
+ const struct GNUNET_PeerIdentity *initiator,
+ const struct GNUNET_ATS_Information *atsi)
+{
+ unsigned int id = *(unsigned int *) cls;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: received incoming tunnel\n");
+ if (id != 1)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "test: received incoming tunnel on peer 2\n");
+ result = GNUNET_SYSERR;
+ }
+ return NULL;
+}
+
+
+/**
+ * Function called whenever an inbound tunnel is destroyed. Should clean up
+ * any associated state.
+ *
+ * @param cls closure (set from GNUNET_MESH_connect)
+ * @param tunnel connection to the other end (henceforth invalid)
+ * @param tunnel_ctx place where local state associated
+ * with the tunnel is stored
+ */
+static void
+inbound_end (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
+ void *tunnel_ctx)
+{
+ unsigned int id = *(unsigned int *) cls;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: incoming tunnel closed\n");
+ if (id != 1)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "test: received closing tunnel on peer 2\n");
+ result = GNUNET_SYSERR;
+ }
+}
+
+
+/**
+ * Method called whenever a peer has disconnected from the tunnel.
+ *
+ * @param cls closure
+ * @param peer peer identity the tunnel stopped working with
+ */
+static void
+peer_conected (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_ATS_Information *atsi)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: peer connected\n");
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_shutdown, NULL);
+}
+
+
+/**
+ * Method called whenever a peer has connected to the tunnel.
+ *
+ * @param cls closure
+ * @param peer peer identity the tunnel was created to, NULL on timeout
+ * @param atsi performance data for the connection
+ */
+static void
+peer_disconnected (void *cls, const struct GNUNET_PeerIdentity *peer)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: peer disconnected\n");
+}
+
+
+/**
+ * Handler array for traffic received on peer1
+ */
+static struct GNUNET_MESH_MessageHandler handlers1[] = {
+ {&data_callback, 1, 0},
+ {NULL, 0, 0}
+};
+
+
+/**
+ * Handler array for traffic received on peer2 (none expected)
+ */
+static struct GNUNET_MESH_MessageHandler handlers2[] = { {NULL, 0, 0} };
+
+
+/**
+ * Start looking for a peer by type
+ */
+static void
+do_connect_peer_1 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct GNUNET_CONFIGURATION_Handle *cfg = cls;
+ static const GNUNET_MESH_ApplicationType app1[] = { 1, 0 };
+
+ test_task = GNUNET_SCHEDULER_NO_TASK;
+ mesh_peer_1 = GNUNET_MESH_connect (cfg, /* configuration */
+ 10, /* queue size */
+ (void *) &one, /* cls */
+ &inbound_tunnel, /* inbound new hndlr */
+ &inbound_end, /* inbound end hndlr */
+ handlers1, /* traffic handlers */
+ app1); /* apps offered */
+}
+
+
+/**
+ * Main test function
+ */
+static void
+test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct GNUNET_CONFIGURATION_Handle *cfg = cls;
+ static const GNUNET_MESH_ApplicationType app2[] = { 0 };
+
+ test_task = GNUNET_SCHEDULER_NO_TASK;
+
+ mesh_peer_2 = GNUNET_MESH_connect (cfg, /* configuration */
+ 10, /* queue size */
+ (void *) &two, /* cls */
+ &inbound_tunnel, /* inbound new hndlr */
+ &inbound_end, /* inbound end hndlr */
+ handlers2, /* traffic handlers */
+ app2); /* apps offered */
+ if (NULL == mesh_peer_2)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: Couldn't connect to mesh :(\n");
+ return;
+ }
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: YAY! CONNECTED TO MESH :D\n");
+ }
+
+ t = GNUNET_MESH_tunnel_create (mesh_peer_2, NULL, &peer_conected,
+ &peer_disconnected, (void *) &two);
+ GNUNET_MESH_peer_request_connect_by_type (t, 1);
+ test_task =
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
+ (GNUNET_TIME_UNIT_SECONDS, 5),
+ &do_connect_peer_1, cfg);
+}
+
+
+/**
+ * Initialize framework and start test
+ */
+static void
+run (void *cls, char *const *args, const char *cfgfile,
+ const struct GNUNET_CONFIGURATION_Handle *cfg)
+{
+ GNUNET_log_setup ("test_mesh_local",
+#if VERBOSE
+ "DEBUG",
+#else
+ "WARNING",
+#endif
+ NULL);
+ arm_pid =
+ GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
+ "gnunet-service-arm",
+#if VERBOSE_ARM
+ "-L", "DEBUG",
+#endif
+ "-c", "test_mesh.conf", NULL);
+
+ abort_task =
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
+ (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
+ NULL);
+
+ test_task = GNUNET_SCHEDULER_add_now (&test, (void *) cfg);
+
+}
+
+
+/**
+ * Main
+ */
+int
+main (int argc, char *argv[])
+{
+ int ret;
+
+ char *const argv2[] = { "test-mesh-local",
+ "-c", "test_mesh.conf",
+#if VERBOSE
+ "-L", "DEBUG",
+#endif
+ NULL
+ };
+ struct GNUNET_GETOPT_CommandLineOption options[] = {
+ GNUNET_GETOPT_OPTION_END
+ };
+
+ ret =
+ GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
+ "test-mesh-local", "nohelp", options, &run, NULL);
+
+ if (GNUNET_OK != ret)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
+ ret);
+ return 1;
+ }
+ if (GNUNET_SYSERR == result)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
+ return 1;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");
+ return 0;
+}
diff --git a/src/mesh/test_mesh_path.conf b/src/mesh/test_mesh_path.conf
new file mode 100644
index 0000000..3d955f0
--- /dev/null
+++ b/src/mesh/test_mesh_path.conf
@@ -0,0 +1,69 @@
+[fs]
+AUTOSTART = NO
+
+[resolver]
+AUTOSTART = NO
+
+[mesh]
+DEBUG = YES
+AUTOSTART = YES
+ACCEPT_FROM = 127.0.0.1;
+HOSTNAME = localhost
+PORT = 10511
+# PREFIX = valgrind --leak-check=full
+# PREFIX = xterm -geometry 100x85 -T peer1 -e gdb --args
+
+[dht]
+DEBUG = NO
+AUTOSTART = YES
+ACCEPT_FROM6 = ::1;
+ACCEPT_FROM = 127.0.0.1;
+HOSTNAME = localhost
+PORT = 2100
+
+[block]
+plugins = dht test
+
+[dhtcache]
+QUOTA = 1 MB
+DATABASE = sqlite
+
+[transport]
+PLUGINS = tcp
+DEBUG = NO
+ACCEPT_FROM6 = ::1;
+ACCEPT_FROM = 127.0.0.1;
+NEIGHBOUR_LIMIT = 50
+PORT = 12365
+
+[ats]
+WAN_QUOTA_OUT = 3932160
+WAN_QUOTA_IN = 3932160
+
+[core]
+PORT = 12092
+
+[arm]
+DEFAULTSERVICES = core
+PORT = 12366
+DEBUG = NO
+
+[transport-tcp]
+TIMEOUT = 300 s
+PORT = 12368
+
+[TESTING]
+WEAKRANDOM = YES
+
+[gnunetd]
+HOSTKEY = $SERVICEHOME/.hostkey
+
+[PATHS]
+DEFAULTCONFIG = test_mesh.conf
+SERVICEHOME = /tmp/test-mesh/
+
+[dns]
+AUTOSTART = NO
+
+[nse]
+AUTOSTART = NO
diff --git a/src/mesh/test_mesh_small.c b/src/mesh/test_mesh_small.c
new file mode 100644
index 0000000..b06e324
--- /dev/null
+++ b/src/mesh/test_mesh_small.c
@@ -0,0 +1,1020 @@
+/*
+ This file is part of GNUnet.
+ (C) 2011 Christian Grothoff (and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 3, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+/**
+ * @file mesh/test_mesh_small.c
+ *
+ * @brief Test for the mesh service: retransmission of traffic.
+ */
+#include <stdio.h>
+#include "platform.h"
+#include "gnunet_testing_lib.h"
+#include "gnunet_mesh_service.h"
+#include <gauger.h>
+
+
+#define VERBOSE GNUNET_YES
+#define REMOVE_DIR GNUNET_YES
+
+struct MeshPeer
+{
+ struct MeshPeer *prev;
+
+ struct MeshPeer *next;
+
+ struct GNUNET_TESTING_Daemon *daemon;
+
+ struct GNUNET_MESH_Handle *mesh_handle;
+};
+
+
+/**
+ * How long until we give up on connecting the peers?
+ */
+#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
+
+/**
+ * Time to wait for stuff that should be rather fast
+ */
+#define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
+
+/**
+ * DIFFERENT TESTS TO RUN
+ */
+#define SETUP 0
+#define UNICAST 1
+#define MULTICAST 2
+#define SPEED 3
+#define SPEED_ACK 4
+
+/**
+ * Which test are we running?
+ */
+static int test;
+
+/**
+ * How many events have happened
+ */
+static int ok;
+
+static int peers_in_tunnel;
+
+static int peers_responded;
+
+static int data_sent;
+
+static int data_received;
+
+static int data_ack;
+
+/**
+ * Be verbose
+ */
+static int verbose;
+
+/**
+ * Total number of peers in the test.
+ */
+static unsigned long long num_peers;
+
+/**
+ * Global configuration file
+ */
+static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
+
+/**
+ * Total number of currently running peers.
+ */
+static unsigned long long peers_running;
+
+/**
+ * Total number of connections in the whole network.
+ */
+static unsigned int total_connections;
+
+/**
+ * The currently running peer group.
+ */
+static struct GNUNET_TESTING_PeerGroup *pg;
+
+/**
+ * File to report results to.
+ */
+static struct GNUNET_DISK_FileHandle *output_file;
+
+/**
+ * File to log connection info, statistics to.
+ */
+static struct GNUNET_DISK_FileHandle *data_file;
+
+/**
+ * How many data points to capture before triggering next round?
+ */
+static struct GNUNET_TIME_Relative wait_time;
+
+/**
+ * Task called to disconnect peers.
+ */
+static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
+
+/**
+ * Task To perform tests
+ */
+static GNUNET_SCHEDULER_TaskIdentifier test_task;
+
+/**
+ * Task called to shutdown test.
+ */
+static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
+
+static char *topology_file;
+
+static struct GNUNET_TESTING_Daemon *d1;
+
+static GNUNET_PEER_Id pid1;
+
+static struct GNUNET_TESTING_Daemon *d2;
+
+static struct GNUNET_TESTING_Daemon *d3;
+
+static struct GNUNET_MESH_Handle *h1;
+
+static struct GNUNET_MESH_Handle *h2;
+
+static struct GNUNET_MESH_Handle *h3;
+
+static struct GNUNET_MESH_Tunnel *t;
+
+static struct GNUNET_MESH_Tunnel *incoming_t;
+
+static struct GNUNET_MESH_Tunnel *incoming_t2;
+
+static struct GNUNET_TIME_Absolute start_time;
+
+static struct GNUNET_TIME_Absolute end_time;
+
+static struct GNUNET_TIME_Relative total_time;
+
+
+/**
+ * Check whether peers successfully shut down.
+ */
+static void
+shutdown_callback (void *cls, const char *emsg)
+{
+ if (emsg != NULL)
+ {
+#if VERBOSE
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Shutdown of peers failed!\n");
+#endif
+ ok--;
+ }
+ else
+ {
+#if VERBOSE
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "All peers successfully shut down!\n");
+#endif
+ }
+ GNUNET_CONFIGURATION_destroy (testing_cfg);
+}
+
+
+/**
+ * Shut down peergroup, clean up.
+ */
+static void
+shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+#if VERBOSE
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Ending test.\n");
+#endif
+
+ if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
+ {
+ GNUNET_SCHEDULER_cancel (disconnect_task);
+ disconnect_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+
+ if (NULL != h1)
+ {
+ GNUNET_MESH_disconnect (h1);
+ h1 = NULL;
+ }
+ if (NULL != h2)
+ {
+ GNUNET_MESH_disconnect (h2);
+ h2 = NULL;
+ }
+ if (test == MULTICAST && NULL != h3)
+ {
+ GNUNET_MESH_disconnect (h3);
+ h3 = NULL;
+ }
+
+ if (data_file != NULL)
+ GNUNET_DISK_file_close (data_file);
+ GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
+}
+
+
+/**
+ * Disconnect from mesh services af all peers, call shutdown.
+ */
+static void
+disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "disconnecting mesh service of peers\n");
+ disconnect_task = GNUNET_SCHEDULER_NO_TASK;
+ if (NULL != t)
+ {
+ GNUNET_MESH_tunnel_destroy(t);
+ t = NULL;
+ }
+ if (NULL != incoming_t)
+ {
+ GNUNET_MESH_tunnel_destroy(incoming_t);
+ incoming_t = NULL;
+ }
+ if (NULL != incoming_t2)
+ {
+ GNUNET_MESH_tunnel_destroy(incoming_t2);
+ incoming_t2 = NULL;
+ }
+ GNUNET_MESH_disconnect (h1);
+ GNUNET_MESH_disconnect (h2);
+ h1 = h2 = NULL;
+ if (test == MULTICAST)
+ {
+ GNUNET_MESH_disconnect (h3);
+ h3 = NULL;
+ }
+ if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
+ {
+ GNUNET_SCHEDULER_cancel (shutdown_handle);
+ shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
+ }
+}
+
+size_t
+tmt_rdy (void *cls, size_t size, void *buf);
+
+static void
+data_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct GNUNET_MESH_TransmitHandle *th;
+ if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
+ return;
+ th = GNUNET_MESH_notify_transmit_ready (t, GNUNET_NO, 0,
+ GNUNET_TIME_UNIT_FOREVER_REL, &d2->id,
+ sizeof (struct GNUNET_MessageHeader),
+ &tmt_rdy, (void *) 1L);
+ if (NULL == th)
+ {
+ unsigned long i = (unsigned long) cls;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Retransmission\n");
+ if (0 == i)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO, " in 1 ms\n");
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
+ &data_task, (void *)1UL);
+ }
+ else
+ {
+ i++;
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO, "in %u ms\n", i);
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
+ GNUNET_TIME_UNIT_MILLISECONDS,
+ i),
+ &data_task, (void *)i);
+ }
+ }
+}
+
+/**
+ * Transmit ready callback
+ *
+ * @param cls Closure.
+ * @param size Size of the buffer we have.
+ * @param buf Buffer to copy data to.
+ */
+size_t
+tmt_rdy (void *cls, size_t size, void *buf)
+{
+ struct GNUNET_MessageHeader *msg = buf;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " tmt_rdy called\n");
+ if (size < sizeof (struct GNUNET_MessageHeader) || NULL == buf)
+ return 0;
+ msg->size = htons (sizeof (struct GNUNET_MessageHeader));
+ msg->type = htons ((long) cls);
+ if (test == SPEED)
+ {
+ data_sent++;
+ if (data_sent < 1000)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " Scheduling %d packet\n", data_sent);
+ GNUNET_SCHEDULER_add_now(&data_task, NULL);
+ }
+ }
+ return sizeof (struct GNUNET_MessageHeader);
+}
+
+
+/**
+ * Function is called whenever a message is received.
+ *
+ * @param cls closure (set from GNUNET_MESH_connect)
+ * @param tunnel connection to the other end
+ * @param tunnel_ctx place to store local state associated with the tunnel
+ * @param sender who sent the message
+ * @param message the actual message
+ * @param atsi performance data for the connection
+ * @return GNUNET_OK to keep the connection open,
+ * GNUNET_SYSERR to close it (signal serious error)
+ */
+int
+data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
+ const struct GNUNET_PeerIdentity *sender,
+ const struct GNUNET_MessageHeader *message,
+ const struct GNUNET_ATS_Information *atsi)
+{
+ long client = (long) cls;
+
+ switch (client)
+ {
+ case 1L:
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Origin client got a response!\n");
+ ok++;
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
+ peers_responded++;
+ data_ack++;
+ if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
+ {
+ GNUNET_SCHEDULER_cancel (disconnect_task);
+ disconnect_task =
+ GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers,
+ NULL);
+ }
+ if (test == MULTICAST && peers_responded < 2)
+ return GNUNET_OK;
+ if (test == SPEED_ACK)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+ " received ack %u\n", data_ack);
+ GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO, 0,
+ GNUNET_TIME_UNIT_FOREVER_REL, sender,
+ sizeof (struct GNUNET_MessageHeader),
+ &tmt_rdy, (void *) 1L);
+ if (data_ack < 1000)
+ return GNUNET_OK;
+ end_time = GNUNET_TIME_absolute_get();
+ total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
+ FPRINTF (stderr, "\nTest time %llu ms\n",
+ (unsigned long long) total_time.rel_value);
+ FPRINTF (stderr, "Test bandwidth: %f kb/s\n",
+ 4000.0 / total_time.rel_value);
+ FPRINTF (stderr, "Test throughput: %f packets/s\n",
+ 1000000.0 / total_time.rel_value);
+ GAUGER ("MESH", "Tunnel 5 peers", 1000000.0 / total_time.rel_value,
+ "packets/s");
+ }
+ GNUNET_assert (tunnel == t);
+ GNUNET_MESH_tunnel_destroy (t);
+ t = NULL;
+ break;
+ case 2L:
+ case 3L:
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+ "Destination client %u got a message.\n",
+ client);
+ ok++;
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
+ if (SPEED != test)
+ {
+ GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO, 0,
+ GNUNET_TIME_UNIT_FOREVER_REL, sender,
+ sizeof (struct GNUNET_MessageHeader),
+ &tmt_rdy, (void *) 1L);
+ }
+ else
+ {
+ data_received++;
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+ " received data %u\n", data_received);
+ if (data_received < 1000)
+ return GNUNET_OK;
+ }
+ if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
+ {
+ GNUNET_SCHEDULER_cancel (disconnect_task);
+ disconnect_task =
+ GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers,
+ NULL);
+ }
+ break;
+ default:
+ break;
+ }
+ return GNUNET_OK;
+}
+
+
+/**
+ * Handlers, for diverse services
+ */
+static struct GNUNET_MESH_MessageHandler handlers[] = {
+ {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
+ {NULL, 0, 0}
+};
+
+
+/**
+ * Method called whenever another peer has added us to a tunnel
+ * the other peer initiated.
+ *
+ * @param cls closure
+ * @param tunnel new handle to the tunnel
+ * @param initiator peer that started the tunnel
+ * @param atsi performance information for the tunnel
+ * @return initial tunnel context for the tunnel
+ * (can be NULL -- that's not an error)
+ */
+static void *
+incoming_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
+ const struct GNUNET_PeerIdentity *initiator,
+ const struct GNUNET_ATS_Information *atsi)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+ "Incoming tunnel from %s to peer %d\n",
+ GNUNET_i2s (initiator), (long) cls);
+ ok++;
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
+ if ((long) cls == 2L)
+ incoming_t = tunnel;
+ else if ((long) cls == 3L)
+ incoming_t2 = tunnel;
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Incoming tunnel for unknown client %lu\n", (long) cls);
+ }
+ if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
+ {
+ GNUNET_SCHEDULER_cancel (disconnect_task);
+ disconnect_task =
+ GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers, NULL);
+ }
+ return NULL;
+}
+
+/**
+ * Function called whenever an inbound tunnel is destroyed. Should clean up
+ * any associated state.
+ *
+ * @param cls closure (set from GNUNET_MESH_connect)
+ * @param tunnel connection to the other end (henceforth invalid)
+ * @param tunnel_ctx place where local state associated
+ * with the tunnel is stored
+ */
+static void
+tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
+ void *tunnel_ctx)
+{
+ long i = (long) cls;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+ "Incoming tunnel disconnected at peer %d\n",
+ i);
+ if (2L == i)
+ {
+ ok++;
+ incoming_t = NULL;
+ }
+ else if (3L == i)
+ {
+ ok++;
+ incoming_t2 = NULL;
+ }
+ else
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Unknown peer! %d\n", i);
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
+ peers_in_tunnel--;
+ if (peers_in_tunnel > 0)
+ return;
+
+ if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
+ {
+ GNUNET_SCHEDULER_cancel (disconnect_task);
+ disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers, NULL);
+ }
+
+ return;
+}
+
+
+/**
+ * Method called whenever a tunnel falls apart.
+ *
+ * @param cls closure
+ * @param peer peer identity the tunnel stopped working with
+ */
+static void
+dh (void *cls, const struct GNUNET_PeerIdentity *peer)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "peer %s disconnected\n",
+ GNUNET_i2s (peer));
+ return;
+}
+
+
+/**
+ * Method called whenever a peer connects to a tunnel.
+ *
+ * @param cls closure
+ * @param peer peer identity the tunnel was created to, NULL on timeout
+ * @param atsi performance data for the connection
+ */
+static void
+ch (void *cls, const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_ATS_Information *atsi)
+{
+ struct GNUNET_PeerIdentity *dest;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+ "peer %s connected\n", GNUNET_i2s (peer));
+
+ if (0 == memcmp (&d2->id, peer, sizeof (d2->id)) && (long) cls == 1L)
+ {
+ ok++;
+ }
+ if (test == MULTICAST && 0 == memcmp (&d3->id, peer, sizeof (d3->id)) &&
+ (long) cls == 1L)
+ {
+ ok++;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
+ switch (test)
+ {
+ case UNICAST:
+ case SPEED:
+ case SPEED_ACK:
+ dest = &d2->id;
+ break;
+ case MULTICAST:
+ peers_in_tunnel++;
+ if (peers_in_tunnel < 2)
+ return;
+ dest = NULL;
+ break;
+ default:
+ return;
+ }
+ if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
+ {
+ GNUNET_SCHEDULER_cancel (disconnect_task);
+ disconnect_task =
+ GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers, NULL);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Sending data...\n");
+ peers_responded = 0;
+ data_ack = 0;
+ data_received = 0;
+ data_sent = 0;
+ start_time = GNUNET_TIME_absolute_get();
+ GNUNET_MESH_notify_transmit_ready (t, GNUNET_NO, 0,
+ GNUNET_TIME_UNIT_FOREVER_REL, dest,
+ sizeof (struct GNUNET_MessageHeader),
+ &tmt_rdy, (void *) 1L);
+ }
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Disconnect already run?\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Aborting...\n");
+ }
+ return;
+}
+
+
+static void
+do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test_task\n");
+ if (test == MULTICAST)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "add peer 3\n");
+ GNUNET_MESH_peer_request_connect_add (t, &d3->id);
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add peer 2\n");
+ GNUNET_MESH_peer_request_connect_add (t, &d2->id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "schedule timeout in 90s\n");
+ if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
+ {
+ GNUNET_SCHEDULER_cancel (disconnect_task);
+ disconnect_task =
+ GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers, NULL);
+ }
+}
+
+
+/**
+ * connect_mesh_service: connect to the mesh service of one of the peers
+ *
+ */
+static void
+connect_mesh_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ GNUNET_MESH_ApplicationType app;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "connect_mesh_service\n");
+
+ d2 = GNUNET_TESTING_daemon_get (pg, 4);
+ if (test == MULTICAST)
+ {
+ d3 = GNUNET_TESTING_daemon_get (pg, 3);
+ }
+ app = (GNUNET_MESH_ApplicationType) 0;
+
+#if VERBOSE
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "connecting to mesh service of peer %s\n",
+ GNUNET_i2s (&d1->id));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "connecting to mesh service of peer %s\n",
+ GNUNET_i2s (&d2->id));
+ if (test == MULTICAST)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "connecting to mesh service of peer %s\n",
+ GNUNET_i2s (&d3->id));
+ }
+#endif
+ h1 = GNUNET_MESH_connect (d1->cfg, 5, (void *) 1L, NULL, &tunnel_cleaner,
+ handlers, &app);
+ h2 = GNUNET_MESH_connect (d2->cfg, 5, (void *) 2L, &incoming_tunnel,
+ &tunnel_cleaner, handlers, &app);
+ if (test == MULTICAST)
+ {
+ h3 = GNUNET_MESH_connect (d3->cfg, 5, (void *) 3L, &incoming_tunnel,
+ &tunnel_cleaner, handlers, &app);
+ }
+ t = GNUNET_MESH_tunnel_create (h1, NULL, &ch, &dh, (void *) 1L);
+ peers_in_tunnel = 0;
+ test_task =
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
+ (GNUNET_TIME_UNIT_SECONDS, 1), &do_test,
+ NULL);
+}
+
+
+
+/**
+ * peergroup_ready: start test when all peers are connected
+ * @param cls closure
+ * @param emsg error message
+ */
+static void
+peergroup_ready (void *cls, const char *emsg)
+{
+ char *buf;
+ int buf_len;
+ unsigned int i;
+
+ if (emsg != NULL)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Peergroup callback called with error, aborting test!\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Error from testing: `%s'\n", emsg);
+ ok--;
+ GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
+ return;
+ }
+#if VERBOSE
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "************************************************************\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Peer Group started successfully!\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Have %u connections\n",
+ total_connections);
+#endif
+
+ if (data_file != NULL)
+ {
+ buf = NULL;
+ buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
+ if (buf_len > 0)
+ GNUNET_DISK_file_write (data_file, buf, buf_len);
+ GNUNET_free (buf);
+ }
+ peers_running = GNUNET_TESTING_daemons_running (pg);
+ for (i = 0; i < num_peers; i++)
+ {
+ GNUNET_PEER_Id peer_id;
+
+ d1 = GNUNET_TESTING_daemon_get (pg, i);
+ peer_id = GNUNET_PEER_intern (&d1->id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n",
+ peer_id, GNUNET_i2s (&d1->id));
+ }
+ d1 = GNUNET_TESTING_daemon_get (pg, 0);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Peer looking: %s\n",
+ GNUNET_i2s (&d1->id));
+ pid1 = GNUNET_PEER_intern (&d1->id);
+
+ GNUNET_SCHEDULER_add_now (&connect_mesh_service, NULL);
+ disconnect_task =
+ GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
+
+}
+
+
+/**
+ * Function that will be called whenever two daemons are connected by
+ * the testing library.
+ *
+ * @param cls closure
+ * @param first peer id for first daemon
+ * @param second peer id for the second daemon
+ * @param distance distance between the connected peers
+ * @param first_cfg config for the first daemon
+ * @param second_cfg config for the second daemon
+ * @param first_daemon handle for the first daemon
+ * @param second_daemon handle for the second daemon
+ * @param emsg error message (NULL on success)
+ */
+static void
+connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
+ const struct GNUNET_PeerIdentity *second, uint32_t distance,
+ const struct GNUNET_CONFIGURATION_Handle *first_cfg,
+ const struct GNUNET_CONFIGURATION_Handle *second_cfg,
+ struct GNUNET_TESTING_Daemon *first_daemon,
+ struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
+{
+ if (emsg == NULL)
+ {
+ total_connections++;
+ }
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Problem with new connection (%s)\n",
+ emsg);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (%s)\n", GNUNET_i2s (first));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (%s)\n", GNUNET_i2s (second));
+ }
+
+}
+
+
+/**
+ * run: load configuration options and schedule test to run (start peergroup)
+ * @param cls closure
+ * @param args argv
+ * @param cfgfile configuration file name (can be NULL)
+ * @param cfg configuration handle
+ */
+static void
+run (void *cls, char *const *args, const char *cfgfile,
+ const struct GNUNET_CONFIGURATION_Handle *cfg)
+{
+ char *temp_str;
+ struct GNUNET_TESTING_Host *hosts;
+ char *data_filename;
+
+ ok = 0;
+ testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
+
+ GNUNET_log_setup ("test_mesh_small",
+#if VERBOSE
+ "DEBUG",
+#else
+ "WARNING",
+#endif
+ NULL);
+
+#if VERBOSE
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Starting daemons.\n");
+ GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
+ "use_progressbars", "YES");
+#endif
+
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
+ "num_peers", &num_peers))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Option TESTING:NUM_PEERS is required!\n");
+ return;
+ }
+
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_time (testing_cfg, "test_mesh_small",
+ "WAIT_TIME", &wait_time))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Option test_mesh_small:wait_time is required!\n");
+ return;
+ }
+
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing",
+ "topology_output_file",
+ &topology_file))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Option test_mesh_small:topology_output_file is required!\n");
+ return;
+ }
+
+ if (GNUNET_OK ==
+ GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_mesh_small",
+ "data_output_file",
+ &data_filename))
+ {
+ data_file =
+ GNUNET_DISK_file_open (data_filename,
+ GNUNET_DISK_OPEN_READWRITE |
+ GNUNET_DISK_OPEN_CREATE,
+ GNUNET_DISK_PERM_USER_READ |
+ GNUNET_DISK_PERM_USER_WRITE);
+ if (data_file == NULL)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
+ data_filename);
+ GNUNET_free (data_filename);
+ }
+ }
+
+ if (GNUNET_YES ==
+ GNUNET_CONFIGURATION_get_value_string (cfg, "test_mesh_small",
+ "output_file", &temp_str))
+ {
+ output_file =
+ GNUNET_DISK_file_open (temp_str,
+ GNUNET_DISK_OPEN_READWRITE |
+ GNUNET_DISK_OPEN_CREATE,
+ GNUNET_DISK_PERM_USER_READ |
+ GNUNET_DISK_PERM_USER_WRITE);
+ if (output_file == NULL)
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
+ temp_str);
+ }
+ GNUNET_free_non_null (temp_str);
+
+ hosts = GNUNET_TESTING_hosts_load (testing_cfg);
+
+ pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
+ &connect_cb, &peergroup_ready, NULL,
+ hosts);
+ GNUNET_assert (pg != NULL);
+ shutdown_handle =
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
+ &shutdown_task, NULL);
+}
+
+
+
+/**
+ * test_mesh_small command line options
+ */
+static struct GNUNET_GETOPT_CommandLineOption options[] = {
+ {'V', "verbose", NULL,
+ gettext_noop ("be verbose (print progress information)"),
+ 0, &GNUNET_GETOPT_set_one, &verbose},
+ GNUNET_GETOPT_OPTION_END
+};
+
+
+/**
+ * Main: start test
+ */
+int
+main (int argc, char *argv[])
+{
+ char * argv2[] = {
+ argv[0],
+ "-c",
+ "test_mesh_small.conf",
+#if VERBOSE
+ "-L",
+ "DEBUG",
+#endif
+ NULL
+ };
+ int argc2 = (sizeof (argv2) / sizeof (char *)) - 1;
+
+ /* Each peer is supposed to generate the following callbacks:
+ * 1 incoming tunnel (@dest)
+ * 1 connected peer (@orig)
+ * 1 received data packet (@dest)
+ * 1 received data packet (@orig)
+ * 1 received tunnel destroy (@dest)
+ * _________________________________
+ * 5 x ok expected per peer
+ */
+ int ok_goal;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start\n");
+ if (strstr (argv[0], "test_mesh_small_unicast") != NULL)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNICAST\n");
+ test = UNICAST;
+ ok_goal = 5;
+ }
+ else if (strstr (argv[0], "test_mesh_small_multicast") != NULL)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MULTICAST\n");
+ test = MULTICAST;
+ ok_goal = 10;
+ }
+ else if (strstr (argv[0], "test_mesh_small_speed_ack") != NULL)
+ {
+ /* Each peer is supposed to generate the following callbacks:
+ * 1 incoming tunnel (@dest)
+ * 1 connected peer (@orig)
+ * 1000 received data packet (@dest)
+ * 1000 received data packet (@orig)
+ * 1 received tunnel destroy (@dest)
+ * _________________________________
+ * 5 x ok expected per peer
+ */
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n");
+ test = SPEED_ACK;
+ ok_goal = 2003;
+ argv2 [3] = NULL; // remove -L DEBUG
+#if VERBOSE
+ argc2 -= 2;
+#endif
+ }
+ else if (strstr (argv[0], "test_mesh_small_speed") != NULL)
+ {
+ /* Each peer is supposed to generate the following callbacks:
+ * 1 incoming tunnel (@dest)
+ * 1 connected peer (@orig)
+ * 1000 received data packet (@dest)
+ * 1 received tunnel destroy (@dest)
+ * _________________________________
+ * 5 x ok expected per peer
+ */
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n");
+ test = SPEED;
+ ok_goal = 1003;
+ }
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n");
+ test = SETUP;
+ ok_goal = 0;
+ }
+
+ GNUNET_PROGRAM_run (argc2, argv2,
+ "test_mesh_small",
+ gettext_noop ("Test mesh in a small network."), options,
+ &run, NULL);
+#if REMOVE_DIR
+ GNUNET_DISK_directory_remove ("/tmp/test_mesh_small");
+#endif
+ if (ok_goal > ok)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "FAILED! (%d/%d)\n", ok, ok_goal);
+ return 1;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
+ return 0;
+}
+
+/* end of test_mesh_small.c */
diff --git a/src/mesh/test_mesh_small.conf b/src/mesh/test_mesh_small.conf
new file mode 100644
index 0000000..005c490
--- /dev/null
+++ b/src/mesh/test_mesh_small.conf
@@ -0,0 +1,95 @@
+[PATHS]
+SERVICEHOME = /tmp/test_mesh_small/
+DEFAULTCONFIG = test_mesh_small.conf
+
+[arm]
+PORT = 10010
+DEFAULTSERVICES = core dht mesh
+#DEBUG = YES
+
+[statistics]
+AUTOSTART = YES
+PORT = 10000
+
+[dht]
+DEBUG = NO
+AUTOSTART = YES
+ACCEPT_FROM6 = ::1;
+ACCEPT_FROM = 127.0.0.1;
+HOSTNAME = localhost
+PORT = 10001
+
+[nse]
+WORKBITS = 0
+INTERVAL = 60 s
+WORKDELAY = 500 ms
+
+[dns]
+AUTOSTART = NO
+PORT = 10011
+
+[transport]
+PORT = 10002
+AUTOSTART = YES
+
+[nat]
+DISABLEV6 = YES
+BINDTO = 127.0.0.1
+ENABLE_UPNP = NO
+BEHIND_NAT = NO
+ALLOW_NAT = NO
+INTERNAL_ADDRESS = 127.0.0.1
+EXTERNAL_ADDRESS = 127.0.0.1
+
+[ats]
+WAN_QUOTA_IN = 1 GB
+WAN_QUOTA_OUT = 1 GB
+
+[core]
+AUTOSTART = YES
+PORT = 10003
+
+[peerinfo]
+AUTOSTART = YES
+PORT = 10004
+
+[mesh]
+PORT = 10005
+DEBUG = YES
+ACCEPT_FROM = 127.0.0.1;
+HOSTNAME = localhost
+# PREFIX = valgrind --leak-check=full --suppressions=valgrind-mesh.supp
+# PREFIX = xterm -geometry 100x85 -T peer1 -e gdb --args
+
+[testing]
+NUM_PEERS = 5
+WEAKRANDOM = YES
+TOPOLOGY = NONE
+CONNECT_TOPOLOGY = LINE
+BLACKLIST_TOPOLOGY = LINE
+BLACKLIST_TRANSPORTS = tcp udp http unix
+#TOPOLOGY_FILE = small.dat
+#CONNECT_TOPOLOGY_OPTION = CONNECT_MINIMUM
+#CONNECT_TOPOLOGY_OPTION_MODIFIER = 25
+#PERCENTAGE = 3
+#PROBABILITY = .1
+F2F = NO
+CONNECT_TIMEOUT = 660 s
+CONNECT_ATTEMPTS = 2
+DEBUG = YES
+HOSTKEYSFILE = ../../contrib/testing_hostkeys.dat
+MAX_CONCURRENT_SSH = 10
+USE_PROGRESSBARS = YES
+PEERGROUP_TIMEOUT = 2400 s
+TOPOLOGY_OUTPUT_FILE = mesh_topo_initial
+MAX_OUTSTANDING_CONNECTIONS = 75
+#SINGLE_PEERINFO_PER_HOST = YES
+#NUM_PEERINFO_PER_HOST = 10
+#SINGLE_STATISTICS_PER_HOST = YES
+#NUM_STATISTICS_PER_HOST = 10
+DELETE_FILES = YES
+
+[test_mesh_small]
+WAIT_TIME = 300 s
+CONNECTION_LIMIT = 16
+#DATA_OUTPUT_FILE=data_output
diff --git a/src/mesh/test_mesh_tree_api.c b/src/mesh/test_mesh_tree_api.c
new file mode 100644
index 0000000..1d43135
--- /dev/null
+++ b/src/mesh/test_mesh_tree_api.c
@@ -0,0 +1,378 @@
+/*
+ This file is part of GNUnet.
+ (C) 2011 Christian Grothoff (and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 3, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file mesh/test_mesh_tree_api.c
+ * @brief test mesh tree api: test of tree & path management api
+ * @author Bartlomiej Polot
+ */
+
+#include "platform.h"
+#include "gnunet_common.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_dht_service.h"
+#include "gnunet_mesh_service.h"
+#include "mesh.h"
+#ifndef MESH_TUNNEL_TREE_C
+#include "mesh_tunnel_tree.c"
+#define MESH_TUNNEL_TREE_C
+#endif
+
+#define VERBOSE 1
+
+int failed;
+int cb_call;
+struct GNUNET_PeerIdentity *pi[10];
+struct MeshTunnelTree *tree;
+
+static void
+cb (void *cls, GNUNET_PEER_Id peer_id)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: CB: Disconnected %u\n", peer_id);
+ if (0 == cb_call)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: and it shouldn't!\n");
+ failed++;
+ }
+ cb_call--;
+}
+
+
+/**
+ * Check if a node has all expected properties.
+ *
+ * @param peer_id Short ID of the peer to test.
+ * @param status Expected status of the peer.
+ * @param children Expected number of children of the peer.
+ * @param first_hop Short ID of the expected first hop towards the peer.
+ */
+static void
+test_assert (GNUNET_PEER_Id peer_id, enum MeshPeerState status,
+ unsigned int children, GNUNET_PEER_Id first_hop)
+{
+ struct MeshTunnelTreeNode *n;
+ struct MeshTunnelTreeNode *c;
+ unsigned int i;
+ int pre_failed;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Checking peer %u\n", peer_id);
+ pre_failed = failed;
+ n = tree_find_peer (tree, peer_id);
+ if (n->peer != peer_id)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Retrieved peer has wrong ID! (Got %u, expected %u)\n", n->peer,
+ peer_id);
+ failed++;
+ }
+ if (n->status != status)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Retrieved peer has wrong status! (Got %u, expected %u)\n",
+ n->status, status);
+ failed++;
+ }
+ for (c = n->children_head, i = 0; NULL != c; c = c->next, i++) ;
+ if (i != children)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Retrieved peer wrong has number of children! (Got %u, expected %u)\n",
+ i, children);
+ failed++;
+ }
+ if (0 != first_hop &&
+ GNUNET_PEER_search (tree_get_first_hop (tree, peer_id)) != first_hop)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Wrong first hop! (Got %u, expected %u)\n",
+ GNUNET_PEER_search (tree_get_first_hop (tree, peer_id)),
+ first_hop);
+ failed++;
+ }
+ if (pre_failed != failed)
+ {
+ struct GNUNET_PeerIdentity id;
+
+ GNUNET_PEER_resolve (peer_id, &id);
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "*** Peer %s (%u) has failed %d checks!\n", GNUNET_i2s (&id),
+ peer_id, failed - pre_failed);
+ }
+}
+
+
+static void
+finish (void)
+{
+ unsigned int i;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Finishing...\n");
+ for (i = 0; i < 10; i++)
+ {
+ GNUNET_free (pi[i]);
+ }
+}
+
+/**
+ * Convert an integer int to a peer identity
+ */
+static struct GNUNET_PeerIdentity *
+get_pi (uint32_t id)
+{
+ struct GNUNET_PeerIdentity *pi;
+
+ pi = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
+ pi->hashPubKey.bits[0] = id + 1;
+ return pi;
+}
+
+
+int
+main (int argc, char *argv[])
+{
+ struct MeshTunnelTreeNode *node;
+ struct MeshPeerPath *path;
+ struct MeshPeerPath *path1;
+ unsigned int i;
+
+ failed = 0;
+ cb_call = 0;
+ GNUNET_log_setup ("test_mesh_api_tree",
+#if VERBOSE
+ "DEBUG",
+#else
+ "WARNING",
+#endif
+ NULL);
+ for (i = 0; i < 10; i++)
+ {
+ pi[i] = get_pi (i);
+ GNUNET_break (i + 1 == GNUNET_PEER_intern (pi[i]));
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer %u: %s\n", i + 1,
+ GNUNET_h2s (&pi[i]->hashPubKey));
+ }
+ tree = tree_new (1);
+ tree->me = tree->root;
+ path = path_new (5);
+ path->peers[0] = 1;
+ path->peers[1] = 2;
+ path->peers[2] = 3;
+ path->peers[3] = 4;
+ path->length = 4;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 1 2 3 4\n");
+ tree_add_path (tree, path, &cb, NULL);
+ tree_debug (tree);
+ path1 = tree_get_path_to_peer (tree, 4);
+ if (NULL == path1 || path->length != path1->length ||
+ memcmp (path->peers, path1->peers, path->length) != 0)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Retrieved path != original\n");
+ failed++;
+ }
+ path_destroy (path1);
+ test_assert (4, MESH_PEER_SEARCHING, 0, 2);
+ test_assert (3, MESH_PEER_RELAY, 1, 0);
+ test_assert (2, MESH_PEER_RELAY, 1, 0);
+ test_assert (1, MESH_PEER_ROOT, 1, 0);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding second path: 1 2 3\n");
+ path->length--;
+ tree_add_path (tree, path, &cb, NULL);
+ tree_debug (tree);
+
+ test_assert (4, MESH_PEER_SEARCHING, 0, 2);
+ test_assert (3, MESH_PEER_SEARCHING, 1, 2);
+ test_assert (2, MESH_PEER_RELAY, 1, 0);
+ test_assert (1, MESH_PEER_ROOT, 1, 0);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding third path 1 2 3 5\n");
+ path->length++;
+ path->peers[3] = 5;
+ tree_add_path (tree, path, &cb, NULL);
+ tree_debug (tree);
+
+ test_assert (5, MESH_PEER_SEARCHING, 0, 2);
+ test_assert (4, MESH_PEER_SEARCHING, 0, 2);
+ test_assert (3, MESH_PEER_SEARCHING, 2, 2);
+ test_assert (2, MESH_PEER_RELAY, 1, 0);
+ test_assert (1, MESH_PEER_ROOT, 1, 0);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Calculating costs...\n");
+ for (i = 1; i < 5; i++)
+ {
+ path->length = i;
+ if (tree_get_path_cost (tree, path) != 0)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: length %u cost failed!\n",
+ i);
+ failed++;
+ }
+ }
+ path->length++;
+ path->peers[4] = 6;
+ if (tree_get_path_cost (tree, path) != 1)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: length %u cost failed!\n", i);
+ failed++;
+ }
+ path->peers[3] = 7;
+ if (tree_get_path_cost (tree, path) != 2)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: length %u cost failed!\n", i);
+ failed++;
+ }
+ path->length--;
+ if (tree_get_path_cost (tree, path) != 1)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: length %u cost failed!\n", i);
+ failed++;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Deleting third path (5)\n");
+ tree_set_status (tree, 5, MESH_PEER_READY);
+ cb_call = 1;
+ node = tree_del_path (tree, 5, &cb, NULL);
+ tree_debug (tree);
+ if (cb_call != 0)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
+ failed++;
+ }
+ if (node->peer != 5)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Retrieved peer != original\n");
+ failed++;
+ }
+
+ test_assert (4, MESH_PEER_SEARCHING, 0, 2);
+ test_assert (3, MESH_PEER_SEARCHING, 1, 2);
+ test_assert (2, MESH_PEER_RELAY, 1, 0);
+ test_assert (1, MESH_PEER_ROOT, 1, 0);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Destroying node copy...\n");
+ GNUNET_free (node);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "test: Adding new shorter first path...\n");
+ path->length = 2;
+ path->peers[1] = 4;
+ cb_call = 1;
+ tree_find_peer (tree, 4)->status = MESH_PEER_READY;
+ tree_add_path (tree, path, &cb, NULL);
+ tree_debug (tree);
+ if (cb_call != 0)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u callbacks missed!\n", cb_call);
+ failed++;
+ }
+
+ test_assert (4, MESH_PEER_SEARCHING, 0, 4);
+ test_assert (3, MESH_PEER_SEARCHING, 0, 2);
+ test_assert (2, MESH_PEER_RELAY, 1, 0);
+ test_assert (1, MESH_PEER_ROOT, 2, 0);
+
+ GNUNET_free (path->peers);
+ GNUNET_free (path);
+ tree_destroy (tree);
+
+ /****************************************************************************/
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test:\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Testing relay trees\n");
+ for (i = 0; i < 10; i++)
+ {
+ GNUNET_break (i + 1 == GNUNET_PEER_intern (pi[i]));
+ }
+ tree = tree_new (2);
+ path = path_new (8);
+ path->peers[0] = 2;
+ path->peers[1] = 1;
+ path->peers[2] = 3;
+ path->length = 3;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 2 1 3\n");
+ tree_add_path (tree, path, &cb, NULL);
+ tree_debug (tree);
+
+ test_assert (3, MESH_PEER_SEARCHING, 0, 3);
+ test_assert (1, MESH_PEER_RELAY, 1, 0);
+ test_assert (2, MESH_PEER_ROOT, 1, 0);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding long path: 2 1 4 5 3\n");
+ path->peers[2] = 4;
+ path->peers[3] = 5;
+ path->peers[4] = 3;
+ path->length = 5;
+ tree_add_path (tree, path, &cb, NULL);
+ tree_debug (tree);
+
+ test_assert (3, MESH_PEER_SEARCHING, 0, 4);
+ test_assert (5, MESH_PEER_RELAY, 1, 4);
+ test_assert (4, MESH_PEER_RELAY, 1, 4);
+ test_assert (1, MESH_PEER_RELAY, 1, 0);
+ test_assert (2, MESH_PEER_ROOT, 1, 0);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "test: Even longer path: 2 6 1 7 8 4 5 3\n");
+ path->peers[0] = 2;
+ path->peers[1] = 6;
+ path->peers[2] = 1;
+ path->peers[3] = 7;
+ path->peers[4] = 8;
+ path->peers[5] = 4;
+ path->peers[6] = 5;
+ path->peers[7] = 3;
+ path->length = 8;
+ tree_add_path (tree, path, &cb, NULL);
+ tree_debug (tree);
+
+ test_assert (3, MESH_PEER_SEARCHING, 0, 7);
+ test_assert (5, MESH_PEER_RELAY, 1, 7);
+ test_assert (4, MESH_PEER_RELAY, 1, 7);
+ test_assert (8, MESH_PEER_RELAY, 1, 7);
+ test_assert (7, MESH_PEER_RELAY, 1, 7);
+ test_assert (1, MESH_PEER_RELAY, 1, 0);
+ test_assert (6, MESH_PEER_RELAY, 1, 0);
+ test_assert (2, MESH_PEER_ROOT, 1, 0);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 2 1 3\n");
+ path->peers[1] = 1;
+ path->peers[2] = 3;
+ path->length = 3;
+ tree_add_path (tree, path, &cb, NULL);
+ tree_debug (tree);
+
+ test_assert (3, MESH_PEER_SEARCHING, 0, 3);
+ test_assert (1, MESH_PEER_RELAY, 1, 0);
+ test_assert (2, MESH_PEER_ROOT, 1, 0);
+
+ GNUNET_free (path->peers);
+ GNUNET_free (path);
+ tree_destroy (tree);
+ finish ();
+ if (failed > 0)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%u tests failed\n", failed);
+ return 1;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: OK\n");
+
+ return 0;
+}