]> git.tdb.fi Git - netmon.git/commitdiff
Change some headers and structs for BSD compatibility
authorMikko Rasa <tdb@tdb.fi>
Tue, 12 Jul 2016 10:24:59 +0000 (13:24 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 12 Jul 2016 10:24:59 +0000 (13:24 +0300)
main.c

diff --git a/main.c b/main.c
index 1b123858edba619a3b71e1f764260dfbd4535bf1..38624d03e082abcf17c6c94855b761b28d1de6c0 100644 (file)
--- a/main.c
+++ b/main.c
@@ -6,22 +6,23 @@ Distributed under the GPL
 
 #include <stdint.h>
 #include <stdio.h>
-#include <getopt.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
 #include <errno.h>
 #include <syslog.h>
 #include <sys/poll.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+#include <net/ethernet.h>
+#include <netinet/in.h>
+#include <netinet/ip.h>
 #include <netinet/ip_icmp.h>
+#include <netinet/ip6.h>
 #include <netdb.h>
-#include <sys/wait.h>
-#include <stdlib.h>
 #include <arpa/inet.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <sys/time.h>
-#include <string.h>
 #include <pcap/pcap.h>
-#include <netinet/ether.h>
-#include <netinet/ip6.h>
 
 typedef unsigned long long Time;
 
@@ -275,30 +276,30 @@ void monitor_check(Monitor *monitor, Time timestamp)
 void capture_handler(uint8_t *user, const struct pcap_pkthdr *header, const uint8_t *data)
 {
        Monitor *monitor = (Monitor *)user;
-       const struct ethhdr *eth = (const struct ethhdr *)(data);
+       const struct ether_header *eth = (const struct ether_header *)(data);
 
        int src_scope = 0;
        int dst_scope = 0;
-       int proto = ntohs(eth->h_proto);
-       if(proto==ETH_P_IP)
+       int proto = ntohs(eth->ether_type);
+       if(proto==ETHERTYPE_IP)
        {
-               const struct iphdr *ip = (const struct iphdr *)(eth+1);
-               if(ntohl(ip->daddr)>>28==14)
+               const struct ip *ip = (const struct ip *)(eth+1);
+               if(ntohl(ip->ip_dst.s_addr)>>28==14)
                        dst_scope = 3;
 
                for(unsigned i=0; i<monitor->n_addresses; ++i)
                        if(monitor->addresses[i].address.ss_family==AF_INET)
                        {
-                               int s = get_inet_scope(ip->saddr, &monitor->addresses[i]);
+                               int s = get_inet_scope(ip->ip_src.s_addr, &monitor->addresses[i]);
                                if(s>src_scope)
                                        src_scope = s;
 
-                               s = get_inet_scope(ip->daddr, &monitor->addresses[i]);
+                               s = get_inet_scope(ip->ip_dst.s_addr, &monitor->addresses[i]);
                                if(s>dst_scope)
                                        dst_scope = s;
                        }
        }
-       else if(proto==ETH_P_IPV6)
+       else if(proto==ETHERTYPE_IPV6)
        {
                const struct ip6_hdr *ip6 = (const struct ip6_hdr *)(eth+1);
                for(unsigned i=0; i<monitor->n_addresses; ++i)
@@ -415,11 +416,11 @@ void pinger_check(Pinger *pinger, Time time)
                        fprintf(stderr, "recvfrom error: %s\n", strerror(errno));
                else
                {
-                       struct iphdr *ip = (struct iphdr *)data;
-                       if(ip->protocol==IPPROTO_ICMP)
+                       struct ip *ip = (struct ip *)data;
+                       if(ip->ip_p==IPPROTO_ICMP)
                        {
-                               struct icmphdr *icmp = (struct icmphdr *)(ip+1);
-                               if(icmp->type==ICMP_ECHOREPLY && icmp->un.echo.id==pinger->id)
+                               struct icmp *icmp = (struct icmp *)(ip+1);
+                               if(icmp->icmp_type==ICMP_ECHOREPLY && icmp->icmp_id==pinger->id)
                                {
                                        /* It's an ICMP echo reply and ours, process it */
                                        if(verbose)
@@ -429,10 +430,10 @@ void pinger_check(Pinger *pinger, Time time)
                                                printf("Ping reply from %s\n", buf);
                                        }
 
-                                       if(icmp->un.echo.sequence==pinger->pending)
+                                       if(icmp->icmp_seq==pinger->pending)
                                                pinger->pending = 0;
                                        else if(verbose)
-                                               printf("Sequence %d, expected %d\n", icmp->un.echo.sequence, pinger->pending);
+                                               printf("Sequence %d, expected %d\n", icmp->icmp_seq, pinger->pending);
                                }
                        }
                }
@@ -469,13 +470,13 @@ void send_ping(Pinger *pinger)
        for(unsigned i=0; i<sizeof(data); ++i)
                data[i] = i;
 
-       struct icmphdr *hdr = (struct icmphdr *)data;
-       hdr->type = ICMP_ECHO;
-       hdr->code = 0;
-       hdr->checksum = 0;
-       hdr->un.echo.id = pinger->id;
-       hdr->un.echo.sequence = pinger->seq;
-       hdr->checksum = checksum(data, sizeof(data));
+       struct icmp *hdr = (struct icmp *)data;
+       hdr->icmp_type = ICMP_ECHO;
+       hdr->icmp_code = 0;
+       hdr->icmp_cksum = 0;
+       hdr->icmp_id = pinger->id;
+       hdr->icmp_seq = pinger->seq;
+       hdr->icmp_cksum = checksum(data, sizeof(data));
 
        if(sendto(pinger->socket, data, sizeof(data), 0, (struct sockaddr *)&pinger->target_addr, sizeof(struct sockaddr_in))==-1)
                fprintf(stderr, "sendto error: %s\n", strerror(errno));