1 /* $OpenBSD: pcap.c,v 1.5 2003/07/31 21:48:05 deraadt Exp $ */ 2 /* 3 * Placed in the PUBLIC DOMAIN 4 */ 5 6 #include <pcap.h> 7 #include <stdlib.h> 8 #include <stdio.h> 9 #include <unistd.h> 10 11 #include "test.h" 12 13 #define LOOPBACK_IF "lo0" 14 #define SNAPLEN 96 15 #define NO_PROMISC 0 16 #define PKTCNT 3 17 18 volatile int packet_count = 0; 19 pthread_mutex_t dummy; 20 pthread_cond_t syncer; 21 22 static void 23 packet_ignore(u_char *tag, const struct pcap_pkthdr *hdr, const u_char *data) 24 { 25 packet_count += 1; 26 } 27 28 static void * 29 pcap_thread(void *arg) 30 { 31 char errbuf[PCAP_ERRBUF_SIZE]; 32 pcap_t *handle; 33 34 SET_NAME("pcap_thread"); 35 CHECKr(pthread_mutex_lock(&dummy)); 36 handle = pcap_open_live(LOOPBACK_IF, SNAPLEN, NO_PROMISC, 0, errbuf); 37 if (!handle) 38 PANIC("You may need to run this test as UID 0 (root)"); 39 CHECKr(pthread_mutex_unlock(&dummy)); 40 CHECKr(pthread_cond_signal(&syncer)); 41 ASSERT(pcap_loop(handle, PKTCNT, packet_ignore, 0) != -1); 42 return 0; 43 } 44 45 static void * 46 ping_thread(void *arg) 47 { 48 SET_NAME("ping_thread"); 49 CHECKr(pthread_mutex_lock(&dummy)); 50 ASSERT(system("ping -c 3 127.0.0.1") == 0); 51 CHECKr(pthread_mutex_unlock(&dummy)); 52 CHECKr(pthread_cond_signal(&syncer)); 53 return 0; 54 } 55 56 int 57 main(int argc, char **argv) 58 { 59 pthread_t pcap; 60 pthread_t ping; 61 62 CHECKr(pthread_mutex_init(&dummy, NULL)); 63 CHECKr(pthread_cond_init(&syncer, NULL)); 64 CHECKr(pthread_mutex_lock(&dummy)); 65 CHECKr(pthread_create(&pcap, NULL, pcap_thread, NULL)); 66 CHECKr(pthread_cond_wait(&syncer, &dummy)); 67 CHECKr(pthread_create(&ping, NULL, ping_thread, NULL)); 68 CHECKr(pthread_cond_wait(&syncer, &dummy)); 69 CHECKr(pthread_mutex_unlock(&dummy)); 70 ASSERT(packet_count == 3); 71 SUCCEED; 72 } 73