Cleanups.
[tinc] / src / net.c
index 69616b4..dc632b6 100644 (file)
--- a/src/net.c
+++ b/src/net.c
@@ -1,6 +1,6 @@
 /*
     net.c -- most of the network code
-    Copyright (C) 1998,99 Ivo Timmermans <zarq@iname.com>
+    Copyright (C) 1998,1999,2000 Ivo Timmermans <zarq@iname.com>
 
     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
@@ -93,12 +93,14 @@ int xsend(conn_list_t *cl, void *packet)
   real_packet_t rp;
 cp
   do_encrypt((vpn_packet_t*)packet, &rp, cl->key);
-  rp.from = myself->vpn_ip;
+  rp.from = htonl(myself->vpn_ip);
+  rp.data.len = htons(rp.data.len);
+  rp.len = htons(rp.len);
 
   if(debug_lvl > 3)
-    syslog(LOG_ERR, "Sent %d bytes to %lx", rp.len, cl->vpn_ip);
+    syslog(LOG_ERR, "Sent %d bytes to %lx", ntohs(rp.len), cl->vpn_ip);
 
-  if((r = send(cl->socket, (char*)&rp, rp.len, 0)) < 0)
+  if((r = send(cl->socket, (char*)&rp, ntohs(rp.len), 0)) < 0)
     {
       syslog(LOG_ERR, "Error sending data: %m");
       return -1;
@@ -302,21 +304,6 @@ cp
   return xsend(cl, packet);
 }
 
-int send_broadcast(conn_list_t *cl, vpn_packet_t *packet)
-{
-  conn_list_t *p;
-cp
-  for(p = cl; p != NULL; p = p->next)
-    if(send_packet(p->real_ip, packet) < 0)
-      {
-       syslog(LOG_ERR, "Could not send a broadcast packet to %08lx (%08lx): %m",
-              p->vpn_ip, p->real_ip);
-       break; /* FIXME: should retry later, and send a ping over the metaconnection. */
-      }
-cp
-  return 0;
-}
-
 /*
   open the local ethertap device
 */
@@ -771,6 +758,11 @@ cp
       return -1;
     }
   total_socket_in += lenin;
+
+  rp.data.len = ntohs(rp.data.len);
+  rp.len = ntohs(rp.len);
+  rp.from = ntohl(rp.from);
+
   if(rp.len >= 0)
     {
       f = lookup_conn(rp.from);
@@ -923,7 +915,7 @@ cp
 int handle_incoming_meta_data(conn_list_t *cl)
 {
   int x, l = sizeof(x);
-  int request, oldlen, p, i;
+  int request, oldlen, i;
   int lenin = 0;
 cp
   if(getsockopt(cl->meta_socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
@@ -956,19 +948,19 @@ cp
 
   for(;;)
     {
-      p=0;
+      cl->reqlen = 0;
 
       for(i = oldlen; i < cl->buflen; i++)
         {
           if(cl->buffer[i] == '\n')
             {
-              p = i + 1;
-              cl->buffer[p] = 0;  /* add end-of-string so we can use sscanf */
+              cl->buffer[i] = 0;  /* replace end-of-line by end-of-string so we can use sscanf */
+              cl->reqlen = i + 1;
               break;
             }
         }
 
-      if(p)
+      if(cl->reqlen)
         {
           if(sscanf(cl->buffer, "%d", &request) == 1)
             {
@@ -988,8 +980,8 @@ cp
               syslog(LOG_ERR, "Bogus data received: %s", cl->buffer);
             }
 
-          cl->buflen -= p;
-          memmove(cl->buffer, cl->buffer + p, cl->buflen);
+          cl->buflen -= cl->reqlen;
+          memmove(cl->buffer, cl->buffer + cl->reqlen, cl->buflen);
           oldlen = 0;
         }
       else