aboutsummaryrefslogtreecommitdiff
path: root/src/hello
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2013-03-28 15:14:38 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2013-03-28 15:14:38 +0000
commitb3883971cd31d198dbd768fffb15960dff0352a6 (patch)
tree1803032b911774f57158013eedfc26a3c0fb5462 /src/hello
parent1fe395444d868f4fd66d44da83cb61a69acc6b66 (diff)
new friend only HELLO type GNUNET_MESSAGE_TYPE_FRIEND_HELLO
Diffstat (limited to 'src/hello')
-rw-r--r--src/hello/Makefile.am8
-rw-r--r--src/hello/gnunet-hello.c9
-rw-r--r--src/hello/hello.c81
-rw-r--r--src/hello/test_friend_hello.c186
-rw-r--r--src/hello/test_hello.c6
5 files changed, 273 insertions, 17 deletions
diff --git a/src/hello/Makefile.am b/src/hello/Makefile.am
index fd5a41beab..ef23e8a6e1 100644
--- a/src/hello/Makefile.am
+++ b/src/hello/Makefile.am
@@ -24,7 +24,8 @@ noinst_PROGRAMS = \
gnunet-hello
check_PROGRAMS = \
- test_hello
+ test_hello \
+ test_friend_hello
if ENABLE_TEST_RUN
TESTS = $(check_PROGRAMS)
@@ -36,6 +37,11 @@ test_hello_LDADD = \
$(top_builddir)/src/hello/libgnunethello.la \
$(top_builddir)/src/util/libgnunetutil.la
+test_friend_hello_SOURCES = \
+ test_friend_hello.c
+test_friend_hello_LDADD = \
+ $(top_builddir)/src/hello/libgnunethello.la \
+ $(top_builddir)/src/util/libgnunetutil.la
gnunet_hello_SOURCES = \
gnunet-hello.c
diff --git a/src/hello/gnunet-hello.c b/src/hello/gnunet-hello.c
index fa250d7a15..7df8b7b83f 100644
--- a/src/hello/gnunet-hello.c
+++ b/src/hello/gnunet-hello.c
@@ -23,6 +23,7 @@
* @author Christian Grothoff
*/
#include "platform.h"
+#include "gnunet_protocols.h"
#include "gnunet_hello_lib.h"
/**
@@ -109,6 +110,7 @@ main (int argc, char *argv[])
struct GNUNET_HELLO_Message *result;
struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pk;
uint64_t fsize;
+ int friend_only;
GNUNET_log_setup ("gnunet-hello", "INFO", NULL);
if (argc != 2)
@@ -166,7 +168,12 @@ main (int argc, char *argv[])
argv[1]);
return 1;
}
- result = GNUNET_HELLO_create (&pk, &add_from_hello, &orig);
+ friend_only = GNUNET_NO;
+ if (GNUNET_MESSAGE_TYPE_HELLO == GNUNET_HELLO_get_type ((struct GNUNET_MessageHeader *) orig))
+ friend_only = GNUNET_NO;
+ if (GNUNET_MESSAGE_TYPE_FRIEND_HELLO == GNUNET_HELLO_get_type ((struct GNUNET_MessageHeader *) orig))
+ friend_only = GNUNET_YES;
+ result = GNUNET_HELLO_create (&pk, &add_from_hello, &orig, friend_only);
GNUNET_assert (NULL != result);
fh = GNUNET_DISK_file_open (argv[1],
GNUNET_DISK_OPEN_WRITE,
diff --git a/src/hello/hello.c b/src/hello/hello.c
index a20e8992cd..066c03d87f 100644
--- a/src/hello/hello.c
+++ b/src/hello/hello.c
@@ -103,6 +103,23 @@ struct GNUNET_HELLO_ParseUriContext
GNUNET_HELLO_TransportPluginsFind plugins_find;
};
+/**
+ * Return HELLO type
+ *
+ * @param h HELLO Message to test
+ * @param GNUNET_MESSAGE_TYPE_HELLO or GNUNET_MESSAGE_TYPE_FRIEND_HELLO or 0 on error
+ */
+
+uint16_t
+GNUNET_HELLO_get_type (const struct GNUNET_MessageHeader *h)
+{
+ if (GNUNET_MESSAGE_TYPE_HELLO == ntohs(h->type))
+ return GNUNET_MESSAGE_TYPE_HELLO;
+ if (GNUNET_MESSAGE_TYPE_FRIEND_HELLO == ntohs(h->type))
+ return GNUNET_MESSAGE_TYPE_FRIEND_HELLO;
+ return 0;
+}
+
/**
* Copy the given address information into
@@ -204,7 +221,8 @@ struct GNUNET_HELLO_Message *
GNUNET_HELLO_create (const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded
*publicKey,
GNUNET_HELLO_GenerateAddressListCallback addrgen,
- void *addrgen_cls)
+ void *addrgen_cls,
+ int friend_only)
{
char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - 256 -
sizeof (struct GNUNET_HELLO_Message)];
@@ -224,7 +242,10 @@ GNUNET_HELLO_create (const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded
}
}
hello = GNUNET_malloc (sizeof (struct GNUNET_HELLO_Message) + used);
- hello->header.type = htons (GNUNET_MESSAGE_TYPE_HELLO);
+ if (GNUNET_NO == friend_only)
+ hello->header.type = htons (GNUNET_MESSAGE_TYPE_HELLO);
+ else
+ hello->header.type = htons (GNUNET_MESSAGE_TYPE_FRIEND_HELLO);
hello->header.size = htons (sizeof (struct GNUNET_HELLO_Message) + used);
memcpy (&hello->publicKey, publicKey,
sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded));
@@ -261,7 +282,8 @@ GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
msize = GNUNET_HELLO_size (msg);
if ((msize < sizeof (struct GNUNET_HELLO_Message)) ||
- (ntohs (msg->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
+ ((ntohs (msg->header.type) != GNUNET_MESSAGE_TYPE_HELLO) &&
+ (ntohs (msg->header.type) != GNUNET_MESSAGE_TYPE_FRIEND_HELLO)))
return NULL;
ret = NULL;
if (return_modified)
@@ -408,8 +430,24 @@ GNUNET_HELLO_merge (const struct GNUNET_HELLO_Message *h1,
const struct GNUNET_HELLO_Message *h2)
{
struct MergeContext mc = { h1, h2, NULL, NULL, 0, 0, 0 };
+ int friend_only;
+ if (h1->header.type != h2->header.type)
+ {
+ /* Trying to merge different HELLO types */
+ GNUNET_break (0);
+ return NULL;
+ }
+ if (GNUNET_MESSAGE_TYPE_HELLO == (ntohs(h1->header.type)))
+ friend_only = GNUNET_NO;
+ else if (GNUNET_MESSAGE_TYPE_FRIEND_HELLO == (ntohs(h1->header.type)))
+ friend_only = GNUNET_YES;
+ else
+ {
+ GNUNET_break (0);
+ return NULL;
+ }
- return GNUNET_HELLO_create (&h1->publicKey, &merge_addr, &mc);
+ return GNUNET_HELLO_create (&h1->publicKey, &merge_addr, &mc, friend_only);
}
@@ -488,7 +526,8 @@ GNUNET_HELLO_size (const struct GNUNET_HELLO_Message *hello)
uint16_t ret = ntohs (hello->header.size);
if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
- (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
+ ((ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO) &&
+ (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_FRIEND_HELLO)))
return 0;
return ret;
}
@@ -508,7 +547,8 @@ GNUNET_HELLO_get_key (const struct GNUNET_HELLO_Message *hello,
uint16_t ret = ntohs (hello->header.size);
if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
- (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
+ ((ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO) &&
+ (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_FRIEND_HELLO)))
return GNUNET_SYSERR;
*publicKey = hello->publicKey;
return GNUNET_OK;
@@ -529,7 +569,8 @@ GNUNET_HELLO_get_id (const struct GNUNET_HELLO_Message *hello,
uint16_t ret = ntohs (hello->header.size);
if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
- (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
+ ((ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO) &&
+ (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_FRIEND_HELLO)))
return GNUNET_SYSERR;
GNUNET_CRYPTO_hash (&hello->publicKey,
sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded),
@@ -552,7 +593,8 @@ GNUNET_HELLO_get_header (struct GNUNET_HELLO_Message *hello)
uint16_t ret = ntohs (hello->header.size);
if ((ret < sizeof (struct GNUNET_HELLO_Message)) ||
- (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO))
+ ((ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_HELLO) &&
+ (ntohs (hello->header.type) != GNUNET_MESSAGE_TYPE_FRIEND_HELLO)))
return NULL;
return &hello->header;
@@ -639,6 +681,9 @@ GNUNET_HELLO_equals (const struct GNUNET_HELLO_Message *h1,
{
struct EqualsContext ec;
+ if (h1->header.type != h2->header.type)
+ return GNUNET_TIME_UNIT_ZERO_ABS;
+
if (0 !=
memcmp (&h1->publicKey, &h2->publicKey,
sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded)))
@@ -1011,13 +1056,25 @@ GNUNET_HELLO_parse_uri (const char *uri,
{
const char *pks;
const char *exc;
+ int friend_only;
struct GNUNET_HELLO_ParseUriContext ctx;
- if (0 != strncmp (uri,
+ if (0 == strncmp (uri,
GNUNET_HELLO_URI_PREFIX,
strlen (GNUNET_HELLO_URI_PREFIX)))
- return GNUNET_SYSERR;
- pks = &uri[strlen (GNUNET_HELLO_URI_PREFIX)];
+ {
+ pks = &uri[strlen (GNUNET_HELLO_URI_PREFIX)];
+ friend_only = GNUNET_NO;
+ }
+ else if (0 == strncmp (uri,
+ GNUNET_FRIEND_HELLO_URI_PREFIX,
+ strlen (GNUNET_FRIEND_HELLO_URI_PREFIX)))
+ {
+ pks = &uri[strlen (GNUNET_FRIEND_HELLO_URI_PREFIX)];
+ friend_only = GNUNET_YES;
+ }
+ else
+ return GNUNET_SYSERR;
exc = strstr (pks, "!");
if (GNUNET_OK !=
@@ -1030,7 +1087,7 @@ GNUNET_HELLO_parse_uri (const char *uri,
ctx.pos = exc;
ctx.ret = GNUNET_OK;
ctx.plugins_find = plugins_find;
- *hello = GNUNET_HELLO_create (pubkey, &add_address_to_hello, &ctx);
+ *hello = GNUNET_HELLO_create (pubkey, &add_address_to_hello, &ctx, friend_only);
return ctx.ret;
}
diff --git a/src/hello/test_friend_hello.c b/src/hello/test_friend_hello.c
new file mode 100644
index 0000000000..fb14817c77
--- /dev/null
+++ b/src/hello/test_friend_hello.c
@@ -0,0 +1,186 @@
+/*
+ This file is part of GNUnet
+ (C) 2009 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 hello/test_hello.c
+ * @brief test for hello.c
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include "gnunet_hello_lib.h"
+
+static size_t
+my_addr_gen (void *cls, size_t max, void *buf)
+{
+ unsigned int *i = cls;
+ size_t ret;
+ struct GNUNET_HELLO_Address address;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "DEBUG: my_addr_gen called with i = %d\n", *i);
+ if (0 == *i)
+ return 0;
+ memset (&address.peer, 0, sizeof (struct GNUNET_PeerIdentity));
+ address.address = "address_information";
+ address.transport_name = "test";
+ address.address_length = *i;
+ ret =
+ GNUNET_HELLO_add_address (&address, GNUNET_TIME_absolute_get (), buf,
+ max);
+ (*i)--;
+ return ret;
+}
+
+
+static int
+check_addr (void *cls, const struct GNUNET_HELLO_Address *address,
+ struct GNUNET_TIME_Absolute expiration)
+{
+ unsigned int *i = cls;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "DEBUG: check_addr called with i = %d and addrlen = %u\n",
+ *i, (unsigned int) address->address_length);
+ GNUNET_assert (address->address_length > 0);
+ GNUNET_assert (*i & (1 << (address->address_length - 1)));
+ *i -= (1 << (address->address_length - 1));
+ GNUNET_assert (0 ==
+ strncmp ("address_information", address->address,
+ address->address_length));
+ GNUNET_assert (0 == strcmp ("test", address->transport_name));
+ return GNUNET_OK;
+}
+
+
+static int
+remove_some (void *cls, const struct GNUNET_HELLO_Address *address,
+ struct GNUNET_TIME_Absolute expiration)
+{
+ unsigned int *i = cls;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "DEBUG: remove_some called with i = %d and addrlen = %u\n",
+ *i, (unsigned int) address->address_length);
+ GNUNET_assert (address->address_length > 0);
+ if (*i & (1 << (address->address_length - 1)))
+ {
+ *i -= (1 << (address->address_length - 1));
+ return GNUNET_NO;
+ }
+ return GNUNET_OK;
+}
+
+
+int
+main (int argc, char *argv[])
+{
+ struct GNUNET_HELLO_Message *msg1;
+ struct GNUNET_HELLO_Message *msg2;
+ struct GNUNET_HELLO_Message *msg3;
+ struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded publicKey;
+ struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pk;
+ struct GNUNET_TIME_Absolute startup_time;
+ unsigned int i;
+
+ GNUNET_log_setup ("test-hello", "DEBUG", NULL);
+ startup_time = GNUNET_TIME_absolute_get ();
+ memset (&publicKey, 42, sizeof (publicKey));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Testing HELLO creation (without addresses)...\n");
+ i = 0;
+ msg1 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i, GNUNET_YES);
+ GNUNET_assert (msg1 != NULL);
+ GNUNET_assert (0 < GNUNET_HELLO_size (msg1));
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Testing address iteration (empty set)...\n");
+ GNUNET_assert (NULL ==
+ GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
+ &i));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Testing HELLO creation (with one address)...\n");
+ i = 1;
+ msg2 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i, GNUNET_YES);
+ GNUNET_assert (msg2 != NULL);
+ GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2));
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Testing address iteration (one address)...\n");
+ i = 1;
+ GNUNET_assert (NULL ==
+ GNUNET_HELLO_iterate_addresses (msg2, GNUNET_NO, &check_addr,
+ &i));
+ GNUNET_assert (i == 0);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Testing get_key from HELLO...\n");
+ GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_key (msg2, &pk));
+ GNUNET_assert (0 == memcmp (&publicKey, &pk, sizeof (pk)));
+ GNUNET_free (msg1);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Testing HELLO creation (with two addresses)...\n");
+ i = 2;
+ msg3 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i, GNUNET_YES);
+ GNUNET_assert (msg3 != NULL);
+ GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3));
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Testing address iteration (two addresses)...\n");
+ i = 3;
+ GNUNET_assert (NULL ==
+ GNUNET_HELLO_iterate_addresses (msg3, GNUNET_NO, &check_addr,
+ &i));
+ GNUNET_assert (i == 0);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Testing HELLO merge...\n");
+ msg1 = GNUNET_HELLO_merge (msg2, msg3);
+ GNUNET_assert (GNUNET_HELLO_size (msg1) == GNUNET_HELLO_size (msg3));
+
+ i = 3;
+ GNUNET_assert (NULL ==
+ GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
+ &i));
+ GNUNET_assert (i == 0);
+ GNUNET_free (msg1);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Testing address iteration to copy HELLO...\n");
+ i = 2;
+ msg1 = GNUNET_HELLO_iterate_addresses (msg3, GNUNET_YES, &remove_some, &i);
+ GNUNET_assert (msg1 != NULL);
+ GNUNET_assert (i == 0);
+ i = 1;
+ GNUNET_assert (NULL ==
+ GNUNET_HELLO_iterate_addresses (msg1, GNUNET_NO, &check_addr,
+ &i));
+ GNUNET_assert (i == 0);
+ GNUNET_free (msg1);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Testing delta address iteration...\n");
+ i = 2;
+ GNUNET_HELLO_iterate_new_addresses (msg3, msg2, startup_time, &check_addr,
+ &i);
+ GNUNET_assert (i == 0);
+ GNUNET_free (msg2);
+ GNUNET_free (msg3);
+ return 0; /* testcase passed */
+}
diff --git a/src/hello/test_hello.c b/src/hello/test_hello.c
index 67335ab956..64abec5e4b 100644
--- a/src/hello/test_hello.c
+++ b/src/hello/test_hello.c
@@ -104,7 +104,7 @@ main (int argc, char *argv[])
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Testing HELLO creation (without addresses)...\n");
i = 0;
- msg1 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
+ msg1 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i, GNUNET_NO);
GNUNET_assert (msg1 != NULL);
GNUNET_assert (0 < GNUNET_HELLO_size (msg1));
@@ -116,7 +116,7 @@ main (int argc, char *argv[])
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Testing HELLO creation (with one address)...\n");
i = 1;
- msg2 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
+ msg2 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i, GNUNET_NO);
GNUNET_assert (msg2 != NULL);
GNUNET_assert (GNUNET_HELLO_size (msg1) < GNUNET_HELLO_size (msg2));
@@ -137,7 +137,7 @@ main (int argc, char *argv[])
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Testing HELLO creation (with two addresses)...\n");
i = 2;
- msg3 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i);
+ msg3 = GNUNET_HELLO_create (&publicKey, &my_addr_gen, &i, GNUNET_NO);
GNUNET_assert (msg3 != NULL);
GNUNET_assert (GNUNET_HELLO_size (msg2) < GNUNET_HELLO_size (msg3));