Enable and fix many extra warnings supported by GCC and Clang.
[tinc] / src / route.c
index 2fa6175..25f2be5 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "connection.h"
 #include "control_common.h"
+#include "crypto.h"
 #include "ethernet.h"
 #include "ipv4.h"
 #include "ipv6.h"
@@ -55,15 +56,12 @@ static const size_t icmp6_size = sizeof(struct icmp6_hdr);
 static const size_t ns_size = sizeof(struct nd_neighbor_solicit);
 static const size_t opt_size = sizeof(struct nd_opt_hdr);
 
-#ifndef MAX
-#define MAX(a, b) ((a) > (b) ? (a) : (b))
-#endif
-
 static timeout_t age_subnets_timeout;
 
 /* RFC 1071 */
 
-static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) {
+static uint16_t inet_checksum(void *vdata, size_t len, uint16_t prevsum) {
+       uint8_t *data = vdata;
        uint16_t word;
        uint32_t checksum = prevsum ^ 0xFFFF;
 
@@ -75,14 +73,14 @@ static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) {
        }
 
        if(len) {
-               checksum += *(uint8_t *)data;
+               checksum += *data;
        }
 
        while(checksum >> 16) {
                checksum = (checksum & 0xFFFF) + (checksum >> 16);
        }
 
-       return ~checksum;
+       return (uint16_t) ~checksum;
 }
 
 static bool ratelimit(int frequency) {
@@ -198,7 +196,7 @@ static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, length_
        ip.ip_src = ip_dst;
        ip.ip_dst = ip_src;
 
-       ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
+       ip.ip_sum = inet_checksum(&ip, ip_size, 0xFFFF);
 
        /* Fill in ICMP header */
 
@@ -206,7 +204,7 @@ static void route_ipv4_unreachable(node_t *source, vpn_packet_t *packet, length_
        icmp.icmp_code = code;
        icmp.icmp_cksum = 0;
 
-       icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, ~0);
+       icmp.icmp_cksum = inet_checksum(&icmp, icmp_size, 0xFFFF);
        icmp.icmp_cksum = inet_checksum(DATA(packet) + ether_size + ip_size + icmp_size, oldlen, icmp.icmp_cksum);
 
        /* Copy structs on stack back to packet */
@@ -311,7 +309,7 @@ static void route_ipv6_unreachable(node_t *source, vpn_packet_t *packet, length_
 
        /* Generate checksum */
 
-       checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
+       checksum = inet_checksum(&pseudo, sizeof(pseudo), 0xFFFF);
        checksum = inet_checksum(&icmp6, icmp6_size, checksum);
        checksum = inet_checksum(DATA(packet) + ether_size + ip6_size + icmp6_size, ntohl(pseudo.length) - icmp6_size, checksum);
 
@@ -400,7 +398,7 @@ static void clamp_mss(const node_t *source, const node_t *via, vpn_packet_t *pac
        }
 
        /* Find TCP header */
-       int start = ether_size;
+       size_t start = ether_size;
        uint16_t type = DATA(packet)[12] << 8 | DATA(packet)[13];
 
        if(type == ETH_P_8021Q) {
@@ -485,7 +483,7 @@ static void clamp_mss(const node_t *source, const node_t *via, vpn_packet_t *pac
                csum += csum >> 16;
                csum ^= 0xffff;
                DATA(packet)[start + 16] = csum >> 8;
-               DATA(packet)[start + 17] = csum;
+               DATA(packet)[start + 17] = csum & 0xff;
                break;
        }
 }
@@ -494,7 +492,7 @@ static void age_subnets(void *data) {
        (void)data;
        bool left = false;
 
-       for splay_each(subnet_t, s, myself->subnet_tree) {
+       for splay_each(subnet_t, s, &myself->subnet_tree) {
                if(s->expires && s->expires < now.tv_sec) {
                        if(debug_level >= DEBUG_TRAFFIC) {
                                char netstr[MAXNETSTR];
@@ -504,7 +502,7 @@ static void age_subnets(void *data) {
                                }
                        }
 
-                       for list_each(connection_t, c, connection_list)
+                       for list_each(connection_t, c, &connection_list)
                                if(c->edge) {
                                        send_del_subnet(c, s);
                                }
@@ -519,7 +517,7 @@ static void age_subnets(void *data) {
 
        if(left)
                timeout_set(&age_subnets_timeout, &(struct timeval) {
-               10, rand() % 100000
+               10, jitter()
        });
 }
 
@@ -543,13 +541,13 @@ static void learn_mac(mac_t *address) {
 
                /* And tell all other tinc daemons it's our MAC */
 
-               for list_each(connection_t, c, connection_list)
+               for list_each(connection_t, c, &connection_list)
                        if(c->edge) {
                                send_add_subnet(c, subnet);
                        }
 
                timeout_add(&age_subnets_timeout, age_subnets, NULL, &(struct timeval) {
-                       10, rand() % 100000
+                       10, jitter()
                });
        } else {
                if(subnet->expires) {
@@ -572,7 +570,7 @@ static void route_broadcast(node_t *source, vpn_packet_t *packet) {
 static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t ether_size) {
        struct ip ip;
        vpn_packet_t fragment;
-       int maxlen, todo;
+       size_t maxlen, todo;
        uint8_t *offset;
        uint16_t ip_off, origf;
 
@@ -587,7 +585,7 @@ static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t et
        todo = ntohs(ip.ip_len) - ip_size;
 
        if(ether_size + ip_size + todo != packet->len) {
-               logger(DEBUG_TRAFFIC, LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%d)", packet->len, (int)(ether_size + ip_size + todo));
+               logger(DEBUG_TRAFFIC, LOG_WARNING, "Length of packet (%d) doesn't match length in IPv4 header (%lu)", packet->len, (unsigned long)(ether_size + ip_size + todo));
                return;
        }
 
@@ -600,7 +598,7 @@ static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t et
        ip_off &= IP_OFFMASK;
 
        while(todo) {
-               int len = todo > maxlen ? maxlen : todo;
+               size_t len = todo > maxlen ? maxlen : todo;
                memcpy(DATA(&fragment) + ether_size + ip_size, offset, len);
                todo -= len;
                offset += len;
@@ -608,7 +606,7 @@ static void fragment_ipv4_packet(node_t *dest, vpn_packet_t *packet, length_t et
                ip.ip_len = htons(ip_size + len);
                ip.ip_off = htons(ip_off | origf | (todo ? IP_MF : 0));
                ip.ip_sum = 0;
-               ip.ip_sum = inet_checksum(&ip, ip_size, ~0);
+               ip.ip_sum = inet_checksum(&ip, ip_size, 0xFFFF);
                memcpy(DATA(&fragment), DATA(packet), ether_size);
                memcpy(DATA(&fragment) + ether_size, &ip, ip_size);
                fragment.len = ether_size + ip_size + len;
@@ -856,7 +854,7 @@ static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
 
        /* Generate checksum */
 
-       checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
+       checksum = inet_checksum(&pseudo, sizeof(pseudo), 0xFFFF);
        checksum = inet_checksum(&ns, ns_size, checksum);
 
        if(has_opt) {
@@ -930,7 +928,7 @@ static void route_neighborsol(node_t *source, vpn_packet_t *packet) {
 
        /* Generate checksum */
 
-       checksum = inet_checksum(&pseudo, sizeof(pseudo), ~0);
+       checksum = inet_checksum(&pseudo, sizeof(pseudo), 0xFFFF);
        checksum = inet_checksum(&ns, ns_size, checksum);
 
        if(has_opt) {
@@ -1111,7 +1109,7 @@ static void route_mac(node_t *source, vpn_packet_t *packet) {
 static void send_pcap(vpn_packet_t *packet) {
        pcap = false;
 
-       for list_each(connection_t, c, connection_list) {
+       for list_each(connection_t, c, &connection_list) {
                if(!c->status.pcap) {
                        continue;
                }
@@ -1124,7 +1122,7 @@ static void send_pcap(vpn_packet_t *packet) {
                }
 
                if(send_request(c, "%d %d %d", CONTROL, REQ_PCAP, len)) {
-                       send_meta(c, (char *)DATA(packet), len);
+                       send_meta(c, DATA(packet), len);
                }
        }
 }