-merge
[oweals/gnunet.git] / src / hello / hello.c
index 1013fd5a3530ff24127eee60b4d00fda2a84c080..690a0961a34ffaa344277748415c8fe9a2c3db80 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2009, 2015 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009, 2015 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
 #include "gnunet_util_lib.h"
 #include "gnunet_transport_plugin.h"
 
-GNUNET_NETWORK_STRUCT_BEGIN
-
-/**
- * A HELLO message is used to exchange information about
- * transports with other peers.  This struct is always
- * followed by the actual network addresses which have
- * the format:
- *
- * 1) transport-name (0-terminated)
- * 2) address-length (uint16_t, network byte order; possibly
- *    unaligned!)
- * 3) address expiration (`struct GNUNET_TIME_AbsoluteNBO`); possibly
- *    unaligned!)
- * 4) address (address-length bytes; possibly unaligned!)
- */
-struct GNUNET_HELLO_Message
-{
-  /**
-   * Type will be #GNUNET_MESSAGE_TYPE_HELLO.
-   */
-  struct GNUNET_MessageHeader header;
-
-  /**
-   * Use in F2F mode: Do not gossip this HELLO message
-   */
-  uint32_t friend_only GNUNET_PACKED;
-
-  /**
-   * The public key of the peer.
-   */
-  struct GNUNET_CRYPTO_EddsaPublicKey publicKey;
-
-};
-GNUNET_NETWORK_STRUCT_END
-
-
 /**
  * Context used for building our own URI.
  */
@@ -157,12 +121,12 @@ GNUNET_HELLO_add_address (const struct GNUNET_HELLO_Address *address,
     return 0;
   exp = GNUNET_TIME_absolute_hton (expiration);
   alen = htons ((uint16_t) address->address_length);
-  memcpy (target, address->transport_name, slen);
-  memcpy (&target[slen], &alen, sizeof (uint16_t));
+  GNUNET_memcpy (target, address->transport_name, slen);
+  GNUNET_memcpy (&target[slen], &alen, sizeof (uint16_t));
   slen += sizeof (uint16_t);
-  memcpy (&target[slen], &exp, sizeof (struct GNUNET_TIME_AbsoluteNBO));
+  GNUNET_memcpy (&target[slen], &exp, sizeof (struct GNUNET_TIME_AbsoluteNBO));
   slen += sizeof (struct GNUNET_TIME_AbsoluteNBO);
-  memcpy (&target[slen], address->address, address->address_length);
+  GNUNET_memcpy (&target[slen], address->address, address->address_length);
   slen += address->address_length;
   return slen;
 }
@@ -208,7 +172,7 @@ get_hello_address_size (const char *buf,
     GNUNET_break_op (0);
     return 0;
   }
-  memcpy (&alen, pos, sizeof (uint16_t));
+  GNUNET_memcpy (&alen, pos, sizeof (uint16_t));
   alen = ntohs (alen);
   *ralen = alen;
   slen += alen + sizeof (uint16_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO);
@@ -242,7 +206,7 @@ GNUNET_HELLO_create (const struct GNUNET_CRYPTO_EddsaPublicKey *public_key,
                      void *addrgen_cls,
                      int friend_only)
 {
-  char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - 256 -
+  char buffer[GNUNET_MAX_MESSAGE_SIZE - 1 - 256 -
               sizeof (struct GNUNET_HELLO_Message)];
   size_t max;
   size_t used;
@@ -269,7 +233,7 @@ GNUNET_HELLO_create (const struct GNUNET_CRYPTO_EddsaPublicKey *public_key,
   hello->header.size = htons (sizeof (struct GNUNET_HELLO_Message) + used);
   hello->friend_only = htonl (friend_only);
   hello->publicKey = *public_key;
-  memcpy (&hello[1],
+  GNUNET_memcpy (&hello[1],
           buffer,
           used);
   return hello;
@@ -307,12 +271,15 @@ 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))
+  {
+    GNUNET_break_op (0);
     return NULL;
+  }
   ret = NULL;
   if (return_modified)
   {
     ret = GNUNET_malloc (msize);
-    memcpy (ret,
+    GNUNET_memcpy (ret,
             msg,
             msize);
   }
@@ -321,6 +288,10 @@ GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
   wpos = 0;
   woff = (NULL != ret) ? (char *) &ret[1] : NULL;
   address.peer.public_key = msg->publicKey;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "HELLO has %u bytes of address data\n",
+             (unsigned int) insize);
+  
   while (insize > 0)
   {
     esize = get_hello_address_size (inptr,
@@ -332,8 +303,8 @@ GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
       GNUNET_free_non_null (ret);
       return NULL;
     }
-    /* need memcpy() due to possibility of misalignment */
-    memcpy (&expire,
+    /* need GNUNET_memcpy() due to possibility of misalignment */
+    GNUNET_memcpy (&expire,
             &inptr[esize - alen - sizeof (struct GNUNET_TIME_AbsoluteNBO)],
             sizeof (struct GNUNET_TIME_AbsoluteNBO));
     address.address = &inptr[esize - alen];
@@ -349,7 +320,7 @@ GNUNET_HELLO_iterate_addresses (const struct GNUNET_HELLO_Message *msg,
          (NULL != ret) )
     {
       /* copy address over */
-      memcpy (woff,
+      GNUNET_memcpy (woff,
               inptr,
               esize);
       woff += esize;
@@ -1000,7 +971,7 @@ add_address_to_uri (void *cls,
   time_t seconds;
 
   papi = ctx->plugins_find (address->transport_name);
-  if (papi == NULL)
+  if (NULL == papi)
   {
     /* Not an error - we might just not have the right plugin. */
     return GNUNET_OK;
@@ -1012,14 +983,18 @@ add_address_to_uri (void *cls,
                address->transport_name);
     return GNUNET_OK;
   }
-  addr = papi->address_to_string (papi->cls, address->address, address->address_length);
-
-  if ( (addr == NULL) || (strlen(addr) == 0) )
+  addr = papi->address_to_string (papi->cls,
+                                  address->address,
+                                  address->address_length);
+  if ( (NULL == addr) ||
+       (0 == strlen(addr)) )
     return GNUNET_OK;
 
   addr_dup = GNUNET_strdup (addr);
   if (NULL != (pos = strstr (addr_dup, "_server")))
-       memcpy (pos, client_str, strlen(client_str)); /* Replace all server addresses with client addresses */
+    GNUNET_memcpy (pos,
+            client_str,
+            strlen (client_str)); /* Replace all server addresses with client addresses */
 
   seconds = expiration.abs_value_us / 1000LL / 1000LL;
   t = gmtime (&seconds);
@@ -1055,17 +1030,21 @@ GNUNET_HELLO_compose_uri (const struct GNUNET_HELLO_Message *hello,
                           GNUNET_HELLO_TransportPluginsFind plugins_find)
 {
   struct GNUNET_HELLO_ComposeUriContext ctx;
-  ctx.plugins_find = plugins_find;
-
-  char *pkey = GNUNET_CRYPTO_eddsa_public_key_to_string (&(hello->publicKey));
+  char *pkey;
 
-  GNUNET_asprintf (&(ctx.uri),
+  ctx.plugins_find = plugins_find;
+  pkey = GNUNET_CRYPTO_eddsa_public_key_to_string (&hello->publicKey);
+  GNUNET_asprintf (&ctx.uri,
                    "%s%s",
-                   (GNUNET_YES == GNUNET_HELLO_is_friend_only (hello)) ? GNUNET_FRIEND_HELLO_URI_PREFIX : GNUNET_HELLO_URI_PREFIX,
+                   (GNUNET_YES == GNUNET_HELLO_is_friend_only (hello))
+                   ? GNUNET_FRIEND_HELLO_URI_PREFIX
+                   : GNUNET_HELLO_URI_PREFIX,
                    pkey);
   GNUNET_free (pkey);
-
-  GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &add_address_to_uri, &ctx);
+  GNUNET_HELLO_iterate_addresses (hello,
+                                  GNUNET_NO,
+                                  &add_address_to_uri,
+                                  &ctx);
   return ctx.uri;
 }
 
@@ -1153,7 +1132,8 @@ add_address_to_hello (void *cls,
     return GNUNET_SYSERR;
   }
   tname++;
-  address = strchr (tname, (int) GNUNET_HELLO_URI_SEP);
+  address = strchr (tname,
+                    (int) GNUNET_HELLO_URI_SEP);
   if (NULL == address)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -1210,7 +1190,10 @@ add_address_to_hello (void *cls,
   haddr.address_length = addr_len;
   haddr.address = addr;
   haddr.transport_name = plugin_name;
-  ret = GNUNET_HELLO_add_address (&haddr, expire, buffer, max);
+  ret = GNUNET_HELLO_add_address (&haddr,
+                                  expire,
+                                  buffer,
+                                  max);
   ctx->counter_added ++;
   GNUNET_free (addr);
   GNUNET_free (plugin_name);