2 netmon - a simple network connectivity monitor
3 Copyright © 2008-2016 Mikko Rasa, Mikkosoft Productions
4 Distributed under the GPL
15 #include <sys/socket.h>
18 #include <net/ethernet.h>
19 #include <netinet/in.h>
20 #include <netinet/ip.h>
21 #include <netinet/ip_icmp.h>
22 #include <netinet/ip6.h>
24 #include <arpa/inet.h>
25 #include <pcap/pcap.h>
27 typedef unsigned long long Time;
31 struct sockaddr_storage address;
32 struct sockaddr_storage mask;
43 int connection_status;
44 Time connection_down_time;
49 const char *target_name;
50 struct sockaddr_in target_addr;
62 int monitor_init(Monitor *);
63 void monitor_check(Monitor *, Time);
64 void capture_handler(uint8_t *, const struct pcap_pkthdr *, const uint8_t *);
65 int get_inet_scope(uint32_t, const Address *);
66 int get_inet6_scope(const struct in6_addr *, const Address *);
67 int pinger_init(Pinger *);
68 void pinger_check(Pinger *, Time);
69 unsigned checksum(const char *, unsigned);
70 void send_ping(Pinger *);
71 pid_t run_command(const char *);
76 int main(int argc, char **argv)
82 Time stats_interval = 1800000000ULL;
83 Time trigger_delay = 60000000ULL;
84 Time trigger_interval = 600000000ULL;
85 const char *trigger_cmd = NULL;
89 monitor.interface_name = NULL;
90 pinger.target_name = NULL;
93 while((o = getopt(argc, argv, "fvs:t:c:i:p:"))!=-1)
103 stats_interval = strtoul(optarg, NULL, 10)*1000000;
106 trigger_delay = strtoul(optarg, &endp, 10)*1000000;
108 trigger_interval = strtoul(endp+1, NULL, 10)*1000000;
110 trigger_interval = trigger_delay;
113 trigger_cmd = optarg;
116 monitor.interface_name = strdup(optarg);
119 pinger.target_name = strdup(optarg);
123 if(monitor.interface_name)
124 monitor_init(&monitor);
126 if(pinger.target_name)
127 pinger_init(&pinger);
129 if(!no_daemon && daemon(1, 0)==-1)
132 openlog("netmon", 0, LOG_LOCAL7);
134 Time next_stats = current_time()+stats_interval;
135 Time next_trigger = trigger_delay;
139 Time time = current_time();
141 if(monitor.interface_name)
143 monitor_check(&monitor, time);
145 if(monitor.connection_status)
146 next_trigger = trigger_delay;
147 else if(time>=monitor.last_receive+next_trigger)
150 printf("Running %s\n", trigger_cmd);
152 syslog(LOG_INFO, "Running %s", trigger_cmd);
153 child_pid = run_command(trigger_cmd);
154 next_trigger += trigger_interval;
158 if(pinger.target_name)
160 pinger_check(&pinger, time);
166 float loss_ratio = (float)pinger.lost/pinger.count;
168 printf("Packet loss: %.2f%%\n", loss_ratio*100);
170 syslog(LOG_INFO, "Packet loss: %.2f%%", loss_ratio*100);
175 next_stats += stats_interval;
179 /* Reap any finished child process */
182 if(waitpid(child_pid, NULL, WNOHANG)==child_pid)
195 gettimeofday(&tv, NULL);
196 return tv.tv_sec*1000000ULL+tv.tv_usec;
199 int monitor_init(Monitor *monitor)
201 char err[PCAP_ERRBUF_SIZE];
203 if(pcap_findalldevs(&devs, err)==-1)
205 fprintf(stderr, "pcap_findalldevs: %s\n", err);
209 monitor->pcap = pcap_open_live(monitor->interface_name, 64, 0, 10, err);
212 fprintf(stderr, "pcap_findalldevs: %s\n", err);
216 if(pcap_setnonblock(monitor->pcap, 1, err)==-1)
218 fprintf(stderr, "pcap_findalldevs: %s\n", err);
222 monitor->n_addresses = 0;
223 monitor->addresses = NULL;
224 for(pcap_if_t *d=devs; d; d=d->next)
225 for(pcap_addr_t *a=d->addresses; a; a=a->next)
226 if(a->addr->sa_family==AF_INET || a->addr->sa_family==AF_INET6)
228 monitor->addresses = (Address *)realloc(monitor->addresses, (monitor->n_addresses+1)*sizeof(Address));
229 unsigned size = (a->addr->sa_family==AF_INET6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in));
230 memcpy(&monitor->addresses[monitor->n_addresses].address, a->addr, size);
231 memcpy(&monitor->addresses[monitor->n_addresses].mask, a->netmask, size);
232 ++monitor->n_addresses;
235 pcap_freealldevs(devs);
237 monitor->last_receive = 0;
238 monitor->last_transmit = 0;
239 monitor->connection_status = 1;
244 void monitor_check(Monitor *monitor, Time timestamp)
246 pcap_dispatch(monitor->pcap, -1, &capture_handler, (uint8_t *)monitor);
248 if(monitor->last_transmit && monitor->last_receive)
250 /* If packets have been transmitted more recently than received, there
251 might be a problem */
252 if(monitor->last_transmit>monitor->last_receive)
254 if(monitor->connection_status && timestamp>=monitor->last_receive+10000000)
256 monitor->connection_status = 0;
257 monitor->connection_down_time = monitor->last_receive;
259 printf("Connection is down\n");
261 syslog(LOG_INFO, "Connection is down");
264 else if(!monitor->connection_status)
266 Time duration = timestamp-monitor->connection_down_time;
267 monitor->connection_status = 1;
269 printf("Connection is up (was down for %lld seconds)\n", duration/1000000);
271 syslog(LOG_INFO, "Connection is up (was down for %lld seconds)", duration/1000000);
276 void capture_handler(uint8_t *user, const struct pcap_pkthdr *header, const uint8_t *data)
278 Monitor *monitor = (Monitor *)user;
279 const struct ether_header *eth = (const struct ether_header *)(data);
283 int proto = ntohs(eth->ether_type);
284 if(proto==ETHERTYPE_IP)
286 const struct ip *ip = (const struct ip *)(eth+1);
287 if(ntohl(ip->ip_dst.s_addr)>>28==14)
290 for(unsigned i=0; i<monitor->n_addresses; ++i)
291 if(monitor->addresses[i].address.ss_family==AF_INET)
293 int s = get_inet_scope(ip->ip_src.s_addr, &monitor->addresses[i]);
297 s = get_inet_scope(ip->ip_dst.s_addr, &monitor->addresses[i]);
302 else if(proto==ETHERTYPE_IPV6)
304 const struct ip6_hdr *ip6 = (const struct ip6_hdr *)(eth+1);
305 for(unsigned i=0; i<monitor->n_addresses; ++i)
306 if(monitor->addresses[i].address.ss_family==AF_INET6)
308 int s = get_inet6_scope(&ip6->ip6_src, &monitor->addresses[i]);
312 s = get_inet6_scope(&ip6->ip6_dst, &monitor->addresses[i]);
318 Time timestamp = header->ts.tv_sec*1000000ULL+header->ts.tv_usec;
319 if(src_scope==0 && dst_scope>0)
320 monitor->last_receive = timestamp;
321 else if(src_scope>0 && dst_scope==0)
322 monitor->last_transmit = timestamp;
325 int get_inet_scope(uint32_t addr, const Address *local_addr)
327 uint32_t diff = addr^((struct sockaddr_in *)&local_addr->address)->sin_addr.s_addr;
330 else if(!(diff&((struct sockaddr_in *)&local_addr->mask)->sin_addr.s_addr))
336 int get_inet6_scope(const struct in6_addr *addr, const Address *local_addr)
339 for(unsigned i=0; i<16; ++i)
341 uint8_t diff = addr->s6_addr[i]^((struct sockaddr_in6 *)&local_addr->address)->sin6_addr.s6_addr[i];
344 diff &= ((struct sockaddr_in6 *)&local_addr->mask)->sin6_addr.s6_addr[i];
355 int pinger_init(Pinger *pinger)
357 if(!pinger->target_name)
360 struct hostent *host;
362 host = gethostbyname(pinger->target_name);
365 herror("gethostbyname");
369 if(host->h_addrtype!=AF_INET)
371 fprintf(stderr, "Got a hostent, but it doesn't have an IPv4 address");
375 pinger->target_addr.sin_family = AF_INET;
376 pinger->target_addr.sin_addr = *(struct in_addr *)host->h_addr_list[0];
381 inet_ntop(AF_INET, &pinger->target_addr.sin_addr, buf, sizeof(buf));
382 printf("Ping target is %s\n", buf);
385 pinger->socket = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
386 if(pinger->socket==-1)
393 pinger->id = getpid();
399 pinger->pfd.fd = pinger->socket;
400 pinger->pfd.events = POLLIN;
405 void pinger_check(Pinger *pinger, Time time)
407 if(poll(&pinger->pfd, 1, 10)>0)
409 struct sockaddr_in addr;
410 socklen_t alen = sizeof(addr);
412 /* Receive a packet */
414 int len = recvfrom(pinger->socket, data, sizeof(data), 0, (struct sockaddr *)&addr, &alen);
416 fprintf(stderr, "recvfrom error: %s\n", strerror(errno));
419 struct ip *ip = (struct ip *)data;
420 if(ip->ip_p==IPPROTO_ICMP)
422 struct icmp *icmp = (struct icmp *)(ip+1);
423 if(icmp->icmp_type==ICMP_ECHOREPLY && icmp->icmp_id==pinger->id)
425 /* It's an ICMP echo reply and ours, process it */
429 inet_ntop(AF_INET, &addr.sin_addr, buf, sizeof(buf));
430 printf("Ping reply from %s\n", buf);
433 if(icmp->icmp_seq==pinger->pending)
436 printf("Sequence %d, expected %d\n", icmp->icmp_seq, pinger->pending);
442 if(time>=pinger->next)
448 printf("Lost ping\n");
452 pinger->next = time+1000000;
456 unsigned checksum(const char *data, unsigned len)
459 for(unsigned i=0; i<len; i+=2)
460 sum += *(const unsigned short *)(data+i);
462 sum = (sum>>16)+(sum&0xFFFF);
467 void send_ping(Pinger *pinger)
470 for(unsigned i=0; i<sizeof(data); ++i)
473 struct icmp *hdr = (struct icmp *)data;
474 hdr->icmp_type = ICMP_ECHO;
477 hdr->icmp_id = pinger->id;
478 hdr->icmp_seq = pinger->seq;
479 hdr->icmp_cksum = checksum(data, sizeof(data));
481 if(sendto(pinger->socket, data, sizeof(data), 0, (struct sockaddr *)&pinger->target_addr, sizeof(struct sockaddr_in))==-1)
482 fprintf(stderr, "sendto error: %s\n", strerror(errno));
484 pinger->pending = pinger->seq++;
490 pid_t run_command(const char *cmd)
500 execv(cmd, (char *const *)argv);