summaryrefslogtreecommitdiffstats
path: root/utils/ulog_test.c
blob: 213a2bf39db531f3ece332f3f3e3d7e559700bb2 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* ulog_test, $Revision: 1.4 $
 *
 * small testing program for libipulog, part of the netfilter ULOG target
 * for the linux 2.4 netfilter subsystem.
 *
 * (C) 2000 by Harald Welte <laforge@gnumonks.org>
 *
 * this code is released under the terms of GNU GPL
 *
 * $Id: ulog_test.c 286 2002-06-13 12:56:53Z laforge $
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libnetfilter_log/libipulog.h>

#define MYBUFSIZ 2048

/* prints some logging about a single packet */
void handle_packet(ulog_packet_msg_t *pkt)
{
	unsigned char *p;
	int i;

	printf("Hook=%u Mark=%lu len=%zu ",
	       pkt->hook, pkt->mark, pkt->data_len);
	if (strlen(pkt->prefix))
		printf("Prefix=%s ", pkt->prefix);
	if (strlen(pkt->indev_name))
		printf("Input device=%s ", pkt->indev_name);
	if (strlen(pkt->outdev_name))
		printf("Output device=%s ", pkt->outdev_name);
	if (pkt->timestamp_sec || pkt->timestamp_usec)
		printf("Timestamp=%ld.%06lds ",
		       pkt->timestamp_sec, pkt->timestamp_usec);

	if (pkt->mac_len)
	{
		printf("mac=");
		p = pkt->mac;
		for (i = 0; i < pkt->mac_len; i++, p++)
			printf("%02x%c", *p, i==pkt->mac_len-1 ? ' ':':');
	}
	printf("\n");

}

int main(int argc, char *argv[])
{
	struct ipulog_handle *h;
	unsigned char* buf;
	int len;
	ulog_packet_msg_t *upkt;
	int i;

	if (argc != 4) {
		fprintf(stderr, "Usage: %s count group timeout\n", argv[0]);
		exit(2);
	}

	/* allocate a receive buffer */
	buf = malloc(MYBUFSIZ);
	if (!buf)
		exit(1);

	/* create ipulog handle */
	h = ipulog_create_handle(ipulog_group2gmask(atoi(argv[2])), 65535);
	if (!h)
	{
		/* if some error occurrs, print it to stderr */
		ipulog_perror(NULL);
		exit(1);
	}

	alarm(atoi(argv[3]));

	/* loop receiving packets and handling them over to handle_packet */
	for (i = 0; i < atoi(argv[1]); i++) {
		len = ipulog_read(h, buf, MYBUFSIZ, 1);
		if (len <= 0) {
			ipulog_perror("ulog_test: short read");
			exit(1);
		}
		printf("%d bytes received\n", len);
		while ((upkt = ipulog_get_packet(h, buf, len)) != NULL) {
			handle_packet(upkt);
		}
	}

	/* just to give it a cleaner look */
	ipulog_destroy_handle(h);
	return 0;
}