remove dead assignments
[oweals/gnunet.git] / src / transport / plugin_transport_unix.c
index 1b1873671f26462e3aa4d44eb582f7452ea07311..5cf9c51254d99706671aad147b78d69511c9328d 100644 (file)
@@ -107,6 +107,8 @@ struct Session
 
   size_t addrlen;
 
+  int inbound;
+
   /**
    * Session timeout task
    */
@@ -433,13 +435,17 @@ lookup_session_it (void *cls,
   struct Session *t = value;
 
   if (t->addrlen != lctx->ua_len)
+  {
+       GNUNET_break (0);
     return GNUNET_YES;
+  }
 
-  if (0 == memcmp (&t->addr, lctx->ua, lctx->ua_len))
+  if (0 == memcmp (t->addr, lctx->ua, lctx->ua_len))
   {
     lctx->s = t;
     return GNUNET_NO;
   }
+  GNUNET_break (0);
   return GNUNET_YES;
 }
 
@@ -604,7 +610,6 @@ unix_real_send (void *cls,
 
 resend:
   /* Send the data */
-  sent = 0;
   sent = GNUNET_NETWORK_socket_sendto (send_handle, msgbuf, msgbuf_size, sb, sbs);
 
   if (GNUNET_SYSERR == sent)
@@ -727,6 +732,22 @@ session_timeout (void *cls,
 }
 
 
+/**
+ * Function obtain the network type for a session
+ *
+ * @param cls closure ('struct Plugin*')
+ * @param session the session
+ * @return the network type in HBO or GNUNET_SYSERR
+ */
+static enum GNUNET_ATS_Network_Type
+unix_get_network (void *cls, 
+                 struct Session *session)
+{
+  GNUNET_assert (NULL != session);
+  return GNUNET_ATS_NET_LOOPBACK;
+}
+
+
 /**
  * Creates a new outbound session the transport service will use to send data to the
  * peer
@@ -758,7 +779,7 @@ unix_plugin_get_session (void *cls,
   }
        addrstr = (char *) &ua[1];
        addr_str_len = ntohl (ua->addrlen);
-       if (addr_str_len != address->address_length - sizeof (struct UnixAddress *))
+       if (addr_str_len != address->address_length - sizeof (struct UnixAddress))
   {
                /* This can be a legacy address */
     return NULL;
@@ -794,6 +815,7 @@ unix_plugin_get_session (void *cls,
   s->addr = (struct UnixAddress *) &s[1];
   s->addrlen = address->address_length;
   s->plugin = plugin;
+  s->inbound = GNUNET_NO;
   memcpy (s->addr, address->address, address->address_length);
   memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
@@ -926,7 +948,7 @@ unix_demultiplexer (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
 
   GNUNET_assert (ua_len >= sizeof (struct UnixAddress));
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, 
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Received message from %s\n",
        unix_address_to_string(NULL, ua, ua_len));
   GNUNET_STATISTICS_update (plugin->env->stats,
@@ -940,17 +962,23 @@ unix_demultiplexer (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
                                        ua_len);
   s = lookup_session (plugin, sender, ua, ua_len);
   if (NULL == s)
+  {
     s = unix_plugin_get_session (plugin, addr);
+    s->inbound = GNUNET_YES;
+    /* Notify transport and ATS about new inbound session */
+    plugin->env->session_start (NULL, sender,
+               PLUGIN_NAME, NULL, 0, s, &plugin->ats_network, 1);
+  }
   reschedule_session_timeout (s);
 
-  plugin->env->receive (plugin->env->cls, sender, currhdr,
-                        s, (const char *) ua, ua_len);
+  plugin->env->receive (plugin->env->cls, sender, currhdr, s,
+                        (GNUNET_YES == s->inbound) ? NULL : (const char *) ua,
+                                           (GNUNET_YES == s->inbound) ? 0 : ua_len);
 
-  plugin->env->update_address_metrics (plugin->env->cls,
-                                      sender,
-                                      (const char *) ua, ua_len,
-                                      s,
-                                      &plugin->ats_network, 1);
+  plugin->env->update_address_metrics (plugin->env->cls, sender,
+                                      (GNUNET_YES == s->inbound) ? NULL : (const char *) ua,
+                                      (GNUNET_YES == s->inbound) ? 0 : ua_len,
+                                      s, &plugin->ats_network, 1);
 
   GNUNET_free (addr);
 }
@@ -1014,6 +1042,7 @@ unix_plugin_select_read (struct Plugin *plugin)
   if ((csize < sizeof (struct UNIXMessage)) || (csize > ret))
   {
     GNUNET_break_op (0);
+    GNUNET_free (ua);
     return;
   }
   msgbuf = (char *) &msg[1];
@@ -1050,7 +1079,7 @@ unix_plugin_select_write (struct Plugin *plugin)
 
   while (NULL != (msgw = plugin->msg_tail))
   {
-    if (GNUNET_TIME_absolute_get_remaining (msgw->timeout).rel_value > 0)
+    if (GNUNET_TIME_absolute_get_remaining (msgw->timeout).rel_value_us > 0)
       break; /* Message is ready for sending */
     /* Message has a timeout */
     LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -1261,10 +1290,13 @@ unix_address_to_string (void *cls, const void *addr, size_t addrlen)
   static char rbuf[1024];
        struct UnixAddress *ua = (struct UnixAddress *) addr;
        char *addrstr;
-       char *tmp;
        size_t addr_str_len;
 
-  if ((NULL == addr) || (0 == addrlen) || (sizeof (struct UnixAddress) > addrlen))
+       if (0 == addrlen)
+       {
+               GNUNET_snprintf(rbuf, sizeof (rbuf), "%s", TRANSPORT_SESSION_INBOUND_STRING);
+       }
+  if ((NULL == addr) || (sizeof (struct UnixAddress) > addrlen))
   {
     GNUNET_break (0);
     return NULL;
@@ -1289,9 +1321,7 @@ unix_address_to_string (void *cls, const void *addr, size_t addrlen)
     return NULL;
   }
 
-  GNUNET_asprintf(&tmp, "%s.%u.%s", PLUGIN_NAME, ntohl (ua->options), addrstr);
-  memcpy (rbuf, tmp, strlen (tmp) + 1);
-  GNUNET_free (tmp);
+  GNUNET_snprintf(rbuf, sizeof (rbuf), "%s.%u.%s", PLUGIN_NAME, ntohl (ua->options), addrstr);
   return rbuf;
 }
 
@@ -1369,6 +1399,10 @@ unix_plugin_address_pretty_printer (void *cls, const char *type,
   {
     asc (asc_cls, unix_address_to_string (NULL, addr, addrlen));
   }
+  else if (0 == addrlen)
+  {
+    asc (asc_cls, TRANSPORT_SESSION_INBOUND_STRING);
+  }
   else
   {
     GNUNET_break (0);
@@ -1405,7 +1439,7 @@ unix_string_to_address (void *cls, const char *addr, uint16_t addrlen,
   address = NULL;
   plugin = NULL;
   optionstr = NULL;
-  options = 0;
+
   if ((NULL == addr) || (addrlen == 0))
   {
     GNUNET_break (0);
@@ -1599,6 +1633,7 @@ libgnunet_plugin_transport_unix_init (void *cls)
   api->address_to_string = &unix_address_to_string;
   api->check_address = &unix_check_address;
   api->string_to_address = &unix_string_to_address;
+  api->get_network = &unix_get_network;
   sockets_created = unix_transport_server_start (plugin);
   if (0 == sockets_created)
     LOG (GNUNET_ERROR_TYPE_WARNING,