Cleanups:
[tinc] / src / net_packet.c
index 48c429a..db31b77 100644 (file)
@@ -1,7 +1,7 @@
 /*
     net_packet.c -- Handles in- and outgoing VPN packets
-    Copyright (C) 1998-2002 Ivo Timmermans <itimmermans@bigfoot.com>,
-                  2000-2002 Guus Sliepen <guus@sliepen.warande.net>
+    Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl>,
+                  2000-2002 Guus Sliepen <guus@sliepen.eu.org>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -17,7 +17,7 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: net_packet.c,v 1.1.2.8 2002/03/01 15:14:29 guus Exp $
+    $Id: net_packet.c,v 1.1.2.21 2002/09/09 19:39:58 guus Exp $
 */
 
 #include "config.h"
 #include <fcntl.h>
 #include <netdb.h>
 #include <netinet/in.h>
-#ifdef HAVE_LINUX
+#ifdef HAVE_NETINET_IN_SYSTM_H
+ #include <netinet/in_systm.h>
+#endif
+#ifdef HAVE_NETINET_IP_H
  #include <netinet/ip.h>
+#endif
+#ifdef HAVE_NETINET_TCP_H
  #include <netinet/tcp.h>
 #endif
 #include <stdio.h>
 #include <openssl/pem.h>
 #include <openssl/hmac.h>
 
-#ifndef HAVE_RAND_PSEUDO_BYTES
-#define RAND_pseudo_bytes RAND_bytes
-#endif
-
 #include <zlib.h>
 
 #include <utils.h>
@@ -94,7 +95,7 @@ void receive_udppacket(node_t *n, vpn_packet_t *inpkt)
   long int complen = MTU + 12;
   EVP_CIPHER_CTX ctx;
   char hmac[EVP_MAX_MD_SIZE];
-cp
+  cp();
   /* Check the message authentication code */
 
   if(myself->digest && myself->maclength)
@@ -103,7 +104,8 @@ cp
       HMAC(myself->digest, myself->key, myself->keylength, (char *)&inpkt->seqno, inpkt->len, hmac, NULL);
       if(memcmp(hmac, (char *)&inpkt->seqno + inpkt->len, myself->maclength))
         {
-          syslog(LOG_DEBUG, _("Got unauthenticated packet from %s (%s)"), n->name, n->hostname);
+          if(debug_lvl >= DEBUG_TRAFFIC)
+            syslog(LOG_DEBUG, _("Got unauthenticated packet from %s (%s)"), n->name, n->hostname);
           return;
         }
     }
@@ -129,7 +131,8 @@ cp
 
   if(inpkt->seqno <= n->received_seqno)
   {
-    syslog(LOG_DEBUG, _("Got late or replayed packet from %s (%s), seqno %d"), n->name, n->hostname, inpkt->seqno);
+    if(debug_lvl >= DEBUG_TRAFFIC)
+      syslog(LOG_DEBUG, _("Got late or replayed packet from %s (%s), seqno %d"), n->name, n->hostname, inpkt->seqno);
     return;
   }
   
@@ -155,28 +158,28 @@ cp
   }
 
   receive_packet(n, inpkt);
-cp
+  cp();
 }
 
 void receive_tcppacket(connection_t *c, char *buffer, int len)
 {
   vpn_packet_t outpkt;
-cp
+  cp();
   outpkt.len = len;
   memcpy(outpkt.data, buffer, len);
 
   receive_packet(c->node, &outpkt);
-cp
+  cp();
 }
 
 void receive_packet(node_t *n, vpn_packet_t *packet)
 {
-cp
+  cp();
   if(debug_lvl >= DEBUG_TRAFFIC)
     syslog(LOG_DEBUG, _("Received packet of %d bytes from %s (%s)"), packet->len, n->name, n->hostname);
 
   route_incoming(n, packet);
-cp
+  cp();
 }
 
 void send_udppacket(node_t *n, vpn_packet_t *inpkt)
@@ -192,7 +195,10 @@ void send_udppacket(node_t *n, vpn_packet_t *inpkt)
   vpn_packet_t *copy;
   static int priority = 0;
   int origpriority;
-cp
+  int sock;
+  cp();
+  /* Make sure we have a valid key */
+
   if(!n->status.validkey)
     {
       if(debug_lvl >= DEBUG_TRAFFIC)
@@ -207,9 +213,14 @@ cp
 
       list_insert_tail(n->queue, copy);
 
+      if(n->queue->count > MAXQUEUELENGTH)
+        list_delete_head(n->queue);
+
       if(!n->status.waitingforkey)
         send_req_key(n->nexthop->connection, myself, n);
 
+      n->status.waitingforkey = 1;
+
       return;
     }
 
@@ -259,20 +270,29 @@ cp
       inpkt->len += n->maclength;
     }
 
+  /* Determine which socket we have to use */
+
+  for(sock = 0; sock < listen_sockets; sock++)
+    if(n->address.sa.sa_family == listen_socket[sock].sa.sa.sa_family)
+      break;
+
+  if(sock >= listen_sockets)
+    sock = 0;  /* If none is available, just use the first and hope for the best. */
+  
   /* Send the packet */
 
 #if defined(SOL_IP) && defined(IP_TOS)
-  if(priorityinheritance && origpriority != priority)
+  if(priorityinheritance && origpriority != priority && listen_socket[sock].sa.sa.sa_family == AF_INET)
     {
       priority = origpriority;
       if(debug_lvl >= DEBUG_TRAFFIC)
         syslog(LOG_DEBUG, _("Setting outgoing packet priority to %d"), priority);
-      if(setsockopt(udp_socket[0], SOL_IP, IP_TOS, &priority, sizeof(priority))) /* SO_PRIORITY doesn't seem to work */
+      if(setsockopt(sock, SOL_IP, IP_TOS, &priority, sizeof(priority))) /* SO_PRIORITY doesn't seem to work */
        syslog(LOG_ERR, _("System call `%s' failed: %s"), "setsockopt", strerror(errno));
     }
 #endif
 
-  if((sendto(udp_socket[0], (char *)&inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0)
+  if((sendto(listen_socket[sock].udp, (char *)&inpkt->seqno, inpkt->len, 0, &(n->address.sa), SALEN(n->address.sa))) < 0)
     {
       syslog(LOG_ERR, _("Error sending packet to %s (%s): %s"),
              n->name, n->hostname, strerror(errno));
@@ -280,7 +300,7 @@ cp
     }
   
   inpkt->len = origlen;
-cp
+  cp();
 }
 
 /*
@@ -289,7 +309,7 @@ cp
 void send_packet(node_t *n, vpn_packet_t *packet)
 {
   node_t *via;
-cp
+  cp();
   if(debug_lvl >= DEBUG_TRAFFIC)
     syslog(LOG_ERR, _("Sending packet of %d bytes to %s (%s)"),
            packet->len, n->name, n->hostname);
@@ -333,7 +353,7 @@ void broadcast_packet(node_t *from, vpn_packet_t *packet)
 {
   avl_node_t *node;
   connection_t *c;
-cp
+  cp();
   if(debug_lvl >= DEBUG_TRAFFIC)
     syslog(LOG_INFO, _("Broadcasting packet of %d bytes from %s (%s)"),
            packet->len, from->name, from->hostname);
@@ -344,13 +364,13 @@ cp
       if(c->status.active && c->status.mst && c != from->nexthop->connection)
         send_packet(c->node, packet);
     }
-cp
+  cp();
 }
 
 void flush_queue(node_t *n)
 {
   list_node_t *node, *next;
-cp
+  cp();
   if(debug_lvl >= DEBUG_TRAFFIC)
     syslog(LOG_INFO, _("Flushing queue for %s (%s)"), n->name, n->hostname);
 
@@ -360,7 +380,7 @@ cp
       send_udppacket(n, (vpn_packet_t *)node->data);
       list_delete_node(n->queue, node);
     }
-cp
+  cp();
 }
 
 void handle_incoming_vpn_data(int sock)
@@ -371,7 +391,7 @@ void handle_incoming_vpn_data(int sock)
   sockaddr_t from;
   socklen_t fromlen = sizeof(from);
   node_t *n;
-cp
+  cp();
   if(getsockopt(sock, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
     {
       syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%s"),
@@ -385,12 +405,16 @@ cp
       return;
     }
 
-  if((pkt.len = recvfrom(sock, (char *)&pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen)) <= 0)
+  pkt.len = recvfrom(sock, (char *)&pkt.seqno, MAXSIZE, 0, &from.sa, &fromlen);
+
+  if(pkt.len <= 0)
     {
       syslog(LOG_ERR, _("Receiving packet failed: %s"), strerror(errno));
       return;
     }
 
+  sockaddrunmap(&from);  /* Some braindead IPv6 implementations do stupid things. */
+
   n = lookup_node_udp(&from);
 
   if(!n)
@@ -405,6 +429,5 @@ cp
     n->connection->last_ping_time = now;
 
   receive_udppacket(n, &pkt);
-cp
+  cp();
 }
-