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