- fixed string to address parsing
[oweals/gnunet.git] / src / ats / ats_api_scheduling.c
index 7ac53f12f245d9d783af4bf636f7507612f9da0c..123398992bed2ac0d6a511ced7737d1d02624d6a 100644 (file)
@@ -29,6 +29,8 @@
 
 #define DEBUG_ATS GNUNET_EXTRA_LOGGING
 
+#define INTERFACE_PROCESSING_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
+
 /**
  * Message in linked list we should send to the ATS service.  The
  * actual binary message follows this struct.
@@ -317,10 +319,11 @@ static struct Session *
 find_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
               const struct GNUNET_PeerIdentity *peer)
 {
-#if DEBUG_ATS
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Find session %u from peer %s in %p\n",
+
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
+              "Find session %u from peer %s in %p\n",
               (unsigned int) session_id, GNUNET_i2s (peer), sh);
-#endif
+
   if (session_id >= sh->session_array_size)
   {
     GNUNET_break (0);
@@ -364,11 +367,11 @@ get_session_id (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session,
   unsigned int i;
   unsigned int f;
 
-#if DEBUG_ATS
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
               "Get session ID for session %p from peer %s in %p\n", session,
               GNUNET_i2s (peer), sh);
-#endif
+
   if (NULL == session)
     return 0;
   f = 0;
@@ -394,11 +397,11 @@ get_session_id (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session,
   sh->session_array[f].session = session;
   sh->session_array[f].peer = *peer;
   sh->session_array[f].slot_used = GNUNET_YES;
-#if DEBUG_ATS
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
               "Assigning session ID %u for session %p of peer %s in %p\n", f,
               session, GNUNET_i2s (peer), sh);
-#endif
+
   return f;
 }
 
@@ -415,11 +418,9 @@ static void
 remove_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
                 const struct GNUNET_PeerIdentity *peer)
 {
-#if DEBUG_ATS
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Remove sessionID %u from peer %s in %p\n",
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
+              "Remove sessionID %u from peer %s in %p\n", "ats-scheduling-api",
               (unsigned int) session_id, GNUNET_i2s (peer), sh);
-#endif
   if (0 == session_id)
     return;
   GNUNET_assert (session_id < sh->session_array_size);
@@ -443,11 +444,11 @@ static void
 release_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
                  const struct GNUNET_PeerIdentity *peer)
 {
-#if DEBUG_ATS
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
               "Release sessionID %u from peer %s in %p\n",
               (unsigned int) session_id, GNUNET_i2s (peer), sh);
-#endif
+
   if (session_id >= sh->session_array_size)
   {
     GNUNET_break (0);
@@ -550,11 +551,10 @@ process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg)
     s = find_session (sh, session_id, &m->peer);
     if (s == NULL)
     {
-#if DEBUG_ATS
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+
+      GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
                   "ATS tries to use outdated session `%s'\n",
                   GNUNET_i2s (&m->peer));
-#endif
       GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
                              GNUNET_TIME_UNIT_FOREVER_REL);
       return;
@@ -676,8 +676,8 @@ interface_proc (void *cls, const char *name,
     net->netmask = (struct sockaddr *) &tmp[1];
     net->length = addrlen;
 
+    memset (&network4, 0, sizeof (network4));
     network4.sin_family = AF_INET;
-    network4.sin_port = htons (0);
 #if HAVE_SOCKADDR_IN_SIN_LEN
     network4.sin_len = sizeof (network4);
 #endif
@@ -700,8 +700,8 @@ interface_proc (void *cls, const char *name,
     net->netmask = (struct sockaddr *) &tmp[1];
     net->length = addrlen;
 
+    memset (&network6, 0, sizeof (network6));
     network6.sin6_family = AF_INET6;
-    network6.sin6_port = htons (0);
 #if HAVE_SOCKADDR_IN_SIN_LEN
     network6.sin6_len = sizeof (network6);
 #endif
@@ -719,11 +719,13 @@ interface_proc (void *cls, const char *name,
   /* Store in list */
   if (net != NULL)
   {
-    char * netmask = strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
+#if VERBOSE_ATS
+    char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding network `%s', netmask `%s'\n",
         GNUNET_a2s((struct sockaddr *) net->network, addrlen),
         netmask);
     GNUNET_free (netmask);
+# endif
     GNUNET_CONTAINER_DLL_insert(sh->net_head, sh->net_tail, net);
   }
   return GNUNET_OK;
@@ -743,24 +745,32 @@ get_addresses (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
   delete_networks (sh);
   GNUNET_OS_network_interfaces_list(interface_proc, sh);
-
-  sh->interface_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, get_addresses, sh);
+  sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
+                                                     get_addresses,
+                                                     sh);
 }
 
 /**
  * Returns where the address is located: LAN or WAN or ...
+ * @param sh the scheduling handle
  * @param addr address
  * @param addrlen address length
  * @return location as GNUNET_ATS_Information
  */
 
-struct GNUNET_ATS_Information
+const struct GNUNET_ATS_Information
 GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle * sh, const struct sockaddr * addr, socklen_t addrlen)
 {
+  GNUNET_assert (sh != NULL);
   struct GNUNET_ATS_Information ats;
   struct ATS_Network * cur = sh->net_head;
   int type = GNUNET_ATS_NET_UNSPECIFIED;
 
+  if  (addr->sa_family == AF_UNIX)
+  {
+    type = GNUNET_ATS_NET_LOOPBACK;
+  }
+
   /* IPv4 loopback check */
   if  (addr->sa_family == AF_INET)
   {
@@ -794,8 +804,9 @@ GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle * sh, const stru
 
       if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
       {
-        char * net = strdup (GNUNET_a2s ((const struct sockaddr *) net4, addrlen));
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' is in network `%s'\n",
+        char * net = GNUNET_strdup (GNUNET_a2s ((const struct sockaddr *) net4, addrlen));
+        GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
+            "`%s' is in network `%s'\n",
             GNUNET_a2s ((const struct sockaddr *)a4, addrlen),
             net);
         GNUNET_free (net);
@@ -819,7 +830,7 @@ GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle * sh, const stru
 
       if (res == GNUNET_YES)
       {
-        char * net = strdup (GNUNET_a2s ((const struct sockaddr *) net6, addrlen));
+        char * net = GNUNET_strdup (GNUNET_a2s ((const struct sockaddr *) net6, addrlen));
         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' is in network `%s'\n",
               GNUNET_a2s ((const struct sockaddr *) a6, addrlen),
               net);
@@ -830,13 +841,31 @@ GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle * sh, const stru
     cur = cur->next;
   }
 
-  /* local network found for this address, default: WAN */
+  /* no local network found for this address, default: WAN */
   if (type == GNUNET_ATS_NET_UNSPECIFIED)
     type = GNUNET_ATS_NET_WAN;
 
+#if VERBOSE
+  const char * range;
+  switch (type) {
+    case GNUNET_ATS_NET_WAN:
+        range = "WAN";
+      break;
+    case GNUNET_ATS_NET_LAN:
+        range = "LAN";
+      break;
+    case GNUNET_ATS_NET_LOOPBACK:
+        range = "LOOPBACK";
+      break;
+    default:
+
+      break;
+  }
+#endif
+
   ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
   ats.value = htonl (type);
-  return ats;
+  return (const struct GNUNET_ATS_Information) ats;
 }
 
 /**
@@ -860,7 +889,9 @@ GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
   sh->suggest_cb_cls = suggest_cb_cls;
   GNUNET_array_grow (sh->session_array, sh->session_array_size, 4);
   GNUNET_OS_network_interfaces_list(interface_proc, sh);
-  sh->interface_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, get_addresses, sh);
+  sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
+      get_addresses,
+      sh);
   reconnect (sh);
   return sh;
 }
@@ -898,9 +929,9 @@ GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
     GNUNET_SCHEDULER_cancel(sh->interface_task);
     sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
   }
-
   GNUNET_array_grow (sh->session_array, sh->session_array_size, 0);
   GNUNET_free (sh);
+  sh = NULL;
 }
 
 
@@ -986,6 +1017,17 @@ GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh,
   size_t namelen;
   size_t msize;
 
+  if (address == NULL)
+  {
+    GNUNET_break (0);
+    return;
+  }
+  if ((address == NULL) && (session == NULL))
+  {
+    GNUNET_break (0);
+    return;
+  }
+
   namelen =
       (address->transport_name ==
        NULL) ? 0 : strlen (address->transport_name) + 1;