curly wars / auto-indentation
[oweals/gnunet.git] / src / vpn / gnunet-daemon-vpn.c
index 3673b72c6ea463d64724d29b3108956cc5028f50..cc8001eae412119f28ab7389e5e26f443e17b2b4 100644 (file)
@@ -182,7 +182,8 @@ collect_mappings (void *cls
 
   /* FIXME! GNUNET_MESH_close_tunnel(me->tunnel); */
 
-  GNUNET_CONTAINER_multihashmap_remove (hashmap, &me->hash, me);
+  GNUNET_assert (GNUNET_YES ==
+                 GNUNET_CONTAINER_multihashmap_remove (hashmap, &me->hash, me));
 
   GNUNET_free (me);
 }
@@ -281,32 +282,33 @@ send_pkt_to_peer_notify_callback (void *cls, size_t size, void *buf)
 {
   struct GNUNET_MESH_Tunnel **tunnel = cls;
 
-  GNUNET_MESH_tunnel_set_data (*tunnel, NULL);
-  struct GNUNET_MessageHeader *hdr =
-      (struct GNUNET_MessageHeader *) (tunnel + 1);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "send_pkt_to_peer_notify_callback: buf = %x; size = %u;\n", buf,
-              size);
-  GNUNET_assert (size >= ntohs (hdr->size));
-  memcpy (buf, hdr, ntohs (hdr->size));
-  size = ntohs (hdr->size);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n");
-
-  if (NULL != GNUNET_MESH_tunnel_get_head (*tunnel))
+  struct tunnel_state *ts = GNUNET_MESH_tunnel_get_data (*tunnel);
+
+  ts->th = NULL;
+
+  if (NULL != buf)
   {
-    struct tunnel_notify_queue *element = GNUNET_MESH_tunnel_get_head (*tunnel);
-    struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (*tunnel);
-    struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (*tunnel);
+    struct GNUNET_MessageHeader *hdr =
+        (struct GNUNET_MessageHeader *) (tunnel + 1);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "send_pkt_to_peer_notify_callback: buf = %x; size = %u;\n", buf,
+                size);
+    GNUNET_assert (size >= ntohs (hdr->size));
+    memcpy (buf, hdr, ntohs (hdr->size));
+    size = ntohs (hdr->size);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n");
+  }
+  else
+    size = 0;
 
-    GNUNET_CONTAINER_DLL_remove (head, tail, element);
+  if (NULL != ts->head)
+  {
+    struct tunnel_notify_queue *element = ts->head;
 
-    GNUNET_MESH_tunnel_set_head (*tunnel, head);
-    GNUNET_MESH_tunnel_set_tail (*tunnel, tail);
+    GNUNET_CONTAINER_DLL_remove (ts->head, ts->tail, element);
 
-    struct GNUNET_MESH_TransmitHandle *th =
-        GNUNET_MESH_notify_transmit_ready (*tunnel,
-                                           GNUNET_NO,
-                                           42,
+    ts->th =
+        GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42,
                                            GNUNET_TIME_relative_divide
                                            (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
                                            (const struct GNUNET_PeerIdentity *)
@@ -315,7 +317,6 @@ send_pkt_to_peer_notify_callback (void *cls, size_t size, void *buf)
                                            element->cls);
 
     /* save the handle */
-    GNUNET_MESH_tunnel_set_data (*tunnel, th);
     GNUNET_free (element);
   }
   GNUNET_free (cls);
@@ -328,13 +329,13 @@ port_in_ports (uint64_t ports, uint16_t port)
 {
   uint16_t *ps = (uint16_t *) & ports;
 
-  return ps[0] == port || ps[1] == port || ps[2] == port || ps[3] == port;
+  return ports == 0 || ps[0] == port || ps[1] == port || ps[2] == port ||
+      ps[3] == port;
 }
 
 void
-send_pkt_to_peer (void *cls,
-                  const struct GNUNET_PeerIdentity *peer,
-                  const struct GNUNET_TRANSPORT_ATS_Information *atsi
+send_pkt_to_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
+                  const struct GNUNET_ATS_Information *atsi
                   __attribute__ ((unused)))
 {
   /* peer == NULL means that all peers in this request are connected */
@@ -347,35 +348,27 @@ send_pkt_to_peer (void *cls,
   GNUNET_assert (NULL != tunnel);
   GNUNET_assert (NULL != *tunnel);
 
-  if (NULL == GNUNET_MESH_tunnel_get_data (*tunnel))
+  struct tunnel_state *ts = GNUNET_MESH_tunnel_get_data (*tunnel);
+
+  if (NULL == ts->th)
   {
-    struct GNUNET_MESH_TransmitHandle *th =
-        GNUNET_MESH_notify_transmit_ready (*tunnel,
-                                           GNUNET_NO,
-                                           42,
+    ts->th =
+        GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42,
                                            GNUNET_TIME_relative_divide
                                            (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
                                            (const struct GNUNET_PeerIdentity *)
-                                           NULL,
-                                           ntohs (hdr->size),
+                                           NULL, ntohs (hdr->size),
                                            send_pkt_to_peer_notify_callback,
                                            cls);
-
-    GNUNET_MESH_tunnel_set_data (*tunnel, th);
   }
   else
   {
-    struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (*tunnel);
-    struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (*tunnel);
     struct tunnel_notify_queue *element = GNUNET_malloc (sizeof *element);
 
     element->cls = cls;
     element->len = ntohs (hdr->size);
 
-    GNUNET_CONTAINER_DLL_insert_tail (head, tail, element);
-
-    GNUNET_MESH_tunnel_set_head (*tunnel, head);
-    GNUNET_MESH_tunnel_set_tail (*tunnel, tail);
+    GNUNET_CONTAINER_DLL_insert_tail (ts->head, ts->tail, element);
   }
 }
 
@@ -429,8 +422,8 @@ new_ip6addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
   unsigned long long ipv6prefix;
 
   GNUNET_assert (GNUNET_OK ==
-                 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
-                                                        "IPV6ADDR", &ipv6addr));
+                 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR",
+                                                        &ipv6addr));
   GNUNET_assert (GNUNET_OK ==
                  GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
                                                         "IPV6PREFIX",
@@ -458,11 +451,11 @@ new_ip4addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
   char *ipv4mask;
 
   GNUNET_assert (GNUNET_OK ==
-                 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
-                                                        "IPV4ADDR", &ipv4addr));
+                 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4ADDR",
+                                                        &ipv4addr));
   GNUNET_assert (GNUNET_OK ==
-                 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
-                                                        "IPV4MASK", &ipv4mask));
+                 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4MASK",
+                                                        &ipv4mask));
   uint32_t mask;
 
   inet_pton (AF_INET, ipv4addr, buf);
@@ -568,9 +561,9 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
 
-      value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
-                                                       GNUNET_TIME_absolute_get
-                                                       ().abs_value);
+      value->heap_node =
+          GNUNET_CONTAINER_heap_insert (heap, value,
+                                        GNUNET_TIME_absolute_get ().abs_value);
       if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
         GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
     }
@@ -580,7 +573,8 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
     list =
         GNUNET_malloc (htons (pkt->hdr.size) +
-                       2 * sizeof (struct answer_packet_list *));
+                       sizeof (struct answer_packet_list) -
+                       sizeof (struct answer_packet));
 
     memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
 
@@ -628,7 +622,8 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     char *name = (char *) (map_entry + 1);
 
     list =
-        GNUNET_malloc (2 * sizeof (struct answer_packet_list *) + offset + 2 +
+        GNUNET_malloc (sizeof (struct answer_packet_list) -
+                       sizeof (struct answer_packet) + offset + 2 +
                        ntohs (namelen));
 
     struct answer_packet *rpkt = &list->pkt;
@@ -647,7 +642,8 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   {
     list =
         GNUNET_malloc (htons (pkt->hdr.size) +
-                       2 * sizeof (struct answer_packet_list *));
+                       sizeof (struct answer_packet_list) -
+                       sizeof (struct answer_packet));
     memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
   }
   else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA)
@@ -696,9 +692,9 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     {
       GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
-      value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
-                                                       GNUNET_TIME_absolute_get
-                                                       ().abs_value);
+      value->heap_node =
+          GNUNET_CONTAINER_heap_insert (heap, value,
+                                        GNUNET_TIME_absolute_get ().abs_value);
       if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
         GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
     }
@@ -707,7 +703,8 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
     list =
         GNUNET_malloc (htons (pkt->hdr.size) +
-                       2 * sizeof (struct answer_packet_list *));
+                       sizeof (struct answer_packet_list) -
+                       sizeof (struct answer_packet));
 
     memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
   }
@@ -722,8 +719,8 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
 
     new_ip4addr_remote (c, pkt->addr, pkt->addrsize);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New mapping to %d.%d.%d.%d\n",
-                c[0], c[1], c[2], c[3]);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New mapping to %d.%d.%d.%d\n", c[0],
+                c[1], c[2], c[3]);
     unsigned char *k = (unsigned char *) &key;
 
     /*
@@ -755,9 +752,9 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     {
       GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
-      value->heap_node = GNUNET_CONTAINER_heap_insert (heap, value,
-                                                       GNUNET_TIME_absolute_get
-                                                       ().abs_value);
+      value->heap_node =
+          GNUNET_CONTAINER_heap_insert (heap, value,
+                                        GNUNET_TIME_absolute_get ().abs_value);
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "Mapping is saved in the hashmap with key %08x.\n",
                   *((uint32_t *) (&key)));
@@ -769,7 +766,8 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
     list =
         GNUNET_malloc (htons (pkt->hdr.size) +
-                       2 * sizeof (struct answer_packet_list *));
+                       sizeof (struct answer_packet_list) -
+                       sizeof (struct answer_packet));
 
     memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
   }
@@ -860,19 +858,18 @@ add_additional_port (struct map_entry *me, uint16_t port)
 static int
 receive_udp_back (void *cls
                   __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
-                  void **tunnel_ctx __attribute__ ((unused)),
-                  const struct GNUNET_PeerIdentity *sender
-                  __attribute__ ((unused)),
+                  void **tunnel_ctx, const struct GNUNET_PeerIdentity *sender,
                   const struct GNUNET_MessageHeader *message,
-                  const struct GNUNET_TRANSPORT_ATS_Information *atsi
+                  const struct GNUNET_ATS_Information *atsi
                   __attribute__ ((unused)))
 {
   GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
   struct remote_addr *s = (struct remote_addr *) desc;
   struct udp_pkt *pkt = (struct udp_pkt *) (desc + 1);
-  const struct GNUNET_PeerIdentity *other = GNUNET_MESH_get_peer (tunnel);
+  const struct GNUNET_PeerIdentity *other = sender;
+  struct tunnel_state *ts = *tunnel_ctx;
 
-  if (16 == s->addrlen)
+  if (16 == ts->addrlen)
   {
     size_t size =
         sizeof (struct ip6_udp) + ntohs (pkt->len) - 1 -
@@ -902,7 +899,7 @@ receive_udp_back (void *cls
     pkt6->ip6_hdr.tclass_l = 0;
     pkt6->ip6_hdr.flowlbl = 0;
     pkt6->ip6_hdr.paylgth = pkt->len;
-    pkt6->ip6_hdr.nxthdr = 0x11;
+    pkt6->ip6_hdr.nxthdr = IPPROTO_UDP;
     pkt6->ip6_hdr.hoplmt = 0xff;
 
     {
@@ -994,7 +991,7 @@ receive_udp_back (void *cls
     pkt4->ip_hdr.flags = 0;
     pkt4->ip_hdr.frag_off = 0;
     pkt4->ip_hdr.ttl = 255;
-    pkt4->ip_hdr.proto = 0x11;
+    pkt4->ip_hdr.proto = IPPROTO_UDP;
     pkt4->ip_hdr.chks = 0;      /* Will be calculated later */
 
     {
@@ -1038,24 +1035,27 @@ receive_udp_back (void *cls
 static int
 receive_tcp_back (void *cls
                   __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
-                  void **tunnel_ctx
-                  __attribute__ ((unused)),
-                  const struct GNUNET_PeerIdentity *sender
+                  void **tunnel_ctx, const struct GNUNET_PeerIdentity *sender
                   __attribute__ ((unused)),
                   const struct GNUNET_MessageHeader *message,
-                  const struct GNUNET_TRANSPORT_ATS_Information *atsi
+                  const struct GNUNET_ATS_Information *atsi
                   __attribute__ ((unused)))
 {
   GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
   struct remote_addr *s = (struct remote_addr *) desc;
   struct tcp_pkt *pkt = (struct tcp_pkt *) (desc + 1);
-  const struct GNUNET_PeerIdentity *other = GNUNET_MESH_get_peer (tunnel);
+  const struct GNUNET_PeerIdentity *other = sender;
+  struct tunnel_state *ts = *tunnel_ctx;
 
   size_t pktlen =
       ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
       sizeof (GNUNET_HashCode);
 
-  if (s->addrlen == 16)
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Received TCP-Packet back, addrlen = %d\n", s->addrlen);
+
+  if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK ||
+      ts->addrlen == 16)
   {
     size_t size = pktlen + sizeof (struct ip6_tcp) - 1;
 
@@ -1081,7 +1081,7 @@ receive_tcp_back (void *cls
     pkt6->ip6_hdr.tclass_l = 0;
     pkt6->ip6_hdr.flowlbl = 0;
     pkt6->ip6_hdr.paylgth = htons (pktlen);
-    pkt6->ip6_hdr.nxthdr = 0x06;
+    pkt6->ip6_hdr.nxthdr = IPPROTO_TCP;
     pkt6->ip6_hdr.hoplmt = 0xff;
 
     {
@@ -1162,7 +1162,7 @@ receive_tcp_back (void *cls
     pkt4->ip_hdr.flags = 0;
     pkt4->ip_hdr.frag_off = 0;
     pkt4->ip_hdr.ttl = 255;
-    pkt4->ip_hdr.proto = 0x06;
+    pkt4->ip_hdr.proto = IPPROTO_TCP;
     pkt4->ip_hdr.chks = 0;      /* Will be calculated later */
 
     {
@@ -1201,7 +1201,7 @@ receive_tcp_back (void *cls
     tmp = pkt4->ip_hdr.dadr;
     sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
 
-    tmp = (0x06 << 16) | (0xffff & pktlen);
+    tmp = (0x06 << 16) | (0xffff & pktlen);     // 0x06 for TCP?
 
     tmp = htonl (tmp);
 
@@ -1219,6 +1219,23 @@ receive_tcp_back (void *cls
   return GNUNET_OK;
 }
 
+static void *
+new_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
+            const struct GNUNET_PeerIdentity *initiator,
+            const struct GNUNET_ATS_Information *atsi)
+{
+  /* Why should anyone open an inbound tunnel to vpn? */
+  GNUNET_break (0);
+  return NULL;
+}
+
+static void
+cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel, void *tunnel_ctx)
+{
+  /* Why should anyone open an inbound tunnel to vpn? */
+  GNUNET_break (0);
+}
+
 /**
  * Main function that will be run by the scheduler.
  *
@@ -1228,10 +1245,8 @@ receive_tcp_back (void *cls
  * @param cfg_ configuration
  */
 static void
-run (void *cls,
-     char *const *args __attribute__ ((unused)),
-     const char *cfgfilep __attribute__ ((unused)),
-     const struct GNUNET_CONFIGURATION_Handle *cfg_)
+run (void *cls, char *const *args __attribute__ ((unused)), const char *cfgfilep
+     __attribute__ ((unused)), const struct GNUNET_CONFIGURATION_Handle *cfg_)
 {
   static const struct GNUNET_MESH_MessageHandler handlers[] = {
     {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK, 0},
@@ -1245,7 +1260,9 @@ run (void *cls,
     GNUNET_APPLICATION_TYPE_END
   };
 
-  mesh_handle = GNUNET_MESH_connect (cfg_, NULL, NULL, handlers, types);
+  mesh_handle =
+      GNUNET_MESH_connect (cfg_, 42, NULL, new_tunnel, cleaner, handlers,
+                           types);
   cfg = cfg_;
   restart_hijack = 0;
   hashmap = GNUNET_CONTAINER_multihashmap_create (65536);
@@ -1274,10 +1291,7 @@ main (int argc, char *const *argv)
   };
 
   return (GNUNET_OK ==
-          GNUNET_PROGRAM_run (argc,
-                              argv,
-                              "vpn",
-                              gettext_noop ("help text"),
+          GNUNET_PROGRAM_run (argc, argv, "vpn", gettext_noop ("help text"),
                               options, &run, NULL)) ? ret : 1;
 }