#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;
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)
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)
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);
}
}
}
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));