summaryrefslogtreecommitdiff
path: root/regress/lib/libpthread/pcap/pcap.c
blob: faa8d38384c5ce6ab06c2076203c0fccd001f1bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*	$OpenBSD: pcap.c,v 1.5 2003/07/31 21:48:05 deraadt Exp $ */
/*
 *	Placed in the PUBLIC DOMAIN
 */

#include <pcap.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include "test.h"

#define LOOPBACK_IF	"lo0"
#define SNAPLEN		96
#define NO_PROMISC	0
#define	PKTCNT		3

volatile int packet_count = 0;
pthread_mutex_t dummy;
pthread_cond_t syncer;

static void
packet_ignore(u_char *tag, const struct pcap_pkthdr *hdr, const u_char *data)
{
	packet_count += 1;
}

static void *
pcap_thread(void *arg)
{
	char errbuf[PCAP_ERRBUF_SIZE];
	pcap_t *handle;

	SET_NAME("pcap_thread");
	CHECKr(pthread_mutex_lock(&dummy));
	handle = pcap_open_live(LOOPBACK_IF, SNAPLEN, NO_PROMISC, 0, errbuf);
	if (!handle)
		PANIC("You may need to run this test as UID 0 (root)");
	CHECKr(pthread_mutex_unlock(&dummy));
	CHECKr(pthread_cond_signal(&syncer));
	ASSERT(pcap_loop(handle, PKTCNT, packet_ignore, 0) != -1);
	return 0;
}

static void *
ping_thread(void *arg)
{
	SET_NAME("ping_thread");
	CHECKr(pthread_mutex_lock(&dummy));
	ASSERT(system("ping -c 3 127.0.0.1") == 0);
	CHECKr(pthread_mutex_unlock(&dummy));
	CHECKr(pthread_cond_signal(&syncer));
	return 0;
}

int
main(int argc, char **argv)
{
	pthread_t pcap;
	pthread_t ping;

	CHECKr(pthread_mutex_init(&dummy, NULL));
	CHECKr(pthread_cond_init(&syncer, NULL));
	CHECKr(pthread_mutex_lock(&dummy));
	CHECKr(pthread_create(&pcap, NULL, pcap_thread, NULL));
	CHECKr(pthread_cond_wait(&syncer, &dummy));
	CHECKr(pthread_create(&ping, NULL, ping_thread, NULL));
	CHECKr(pthread_cond_wait(&syncer, &dummy));
	CHECKr(pthread_mutex_unlock(&dummy));
	ASSERT(packet_count == 3);
	SUCCEED;
}