Importing all of DRL, including ulogd and all of its files.
[distributedratelimiting.git] / libipulog / ulog_test.c
1 /* ulog_test, $Revision: 5293 $
2  *
3  * small testing program for libipulog, part of the netfilter ULOG target
4  * for the linux 2.4 netfilter subsystem.
5  *
6  * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
7  *
8  * this code is released under the terms of GNU GPL
9  *
10  * $Id: ulog_test.c 5293 2005-03-11 11:47:53Z laforge $
11  */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <libipulog/libipulog.h>
16
17 #define MYBUFSIZ 2048
18
19 /* prints some logging about a single packet */
20 void handle_packet(ulog_packet_msg_t *pkt)
21 {
22         unsigned char *p;
23         int i;
24         
25         printf("Hook=%u Mark=%lu len=%d ",
26                pkt->hook, pkt->mark, pkt->data_len);
27         if (strlen(pkt->prefix))
28                 printf("Prefix=%s ", pkt->prefix);
29         
30         if (pkt->mac_len)
31         {
32                 printf("mac=");
33                 p = pkt->mac;
34                 for (i = 0; i < pkt->mac_len; i++, p++)
35                         printf("%02x%c", *p, i==pkt->mac_len-1 ? ' ':':');
36         }
37         printf("\n");
38
39 }
40
41 int main(int argc, char *argv[])
42 {
43         struct ipulog_handle *h;
44         unsigned char* buf;
45         int len;
46         ulog_packet_msg_t *upkt;
47         int i;
48
49         if (argc != 4) {
50                 fprintf(stderr, "Usage: %s count group timeout\n", argv[0]);
51                 exit(1);
52         }
53
54         /* allocate a receive buffer */
55         buf = (unsigned char *) malloc(MYBUFSIZ);
56         
57         /* create ipulog handle */
58         h = ipulog_create_handle(ipulog_group2gmask(atoi(argv[2])),150000);
59         if (!h)
60         {
61                 /* if some error occurrs, print it to stderr */
62                 ipulog_perror(NULL);
63                 exit(1);
64         }
65
66         alarm(atoi(argv[3]));
67
68         /* loop receiving packets and handling them over to handle_packet */
69         for (i = 0; i < atoi(argv[1]); i++) {
70                 len = ipulog_read(h, buf, MYBUFSIZ, 1);
71                 if (len <= 0) {
72                         ipulog_perror("ulog_test: short read");
73                         exit(1);
74                 }
75                 printf("%d bytes received\n", len);
76                 while (upkt = ipulog_get_packet(h, buf, len)) {
77                         handle_packet(upkt);
78                 }
79         }
80         
81         /* just to give it a cleaner look */
82         ipulog_destroy_handle(h);
83         return 0;
84 }