lintian was not happy
[vde.git] / vde-2 / plugin / pdump.c
blob30424918dd58fc06d357109e1ad09f847f463fd7
1 #define _GNU_SOURCE
2 #include "vdeplugin.h"
3 #include <stdio.h>
4 #include <pcap.h>
5 #include <string.h>
7 /* usage:
9 * plugin/add pdump.so
10 * debug/add pdump/packetin
11 * debug/add pdump/packetout
12 * pdump/active 1
15 // TODO per-port dump(file?)
16 int pktevent(struct dbgcl *tag,va_list v);
18 char errbuf[PCAP_ERRBUF_SIZE];
19 pcap_t *desc = NULL;
20 pcap_dumper_t *dumper = NULL;
21 char *dumpfile = "vde_dump.cap";
23 struct plugin vde_plugin_data={
24 .name="pdump",
25 .help="dump packets to file, in pcap format",
28 // FIXME check if dumpfile exists, it will be trucated
29 static int dump(char *arg)
31 int active=atoi(arg);
32 int rv;
33 if (active){
34 // TODO configurable snaplen
35 if(!desc)
36 desc = pcap_open_dead(DLT_EN10MB, 96);
38 if(!dumper)
39 dumper = pcap_dump_open(desc, dumpfile);
41 rv=eventadd(pktevent,"packet",NULL);
42 }else{
43 rv=eventdel(pktevent,"packet",NULL);
45 if(dumper)
46 pcap_dump_flush(dumper);
49 return rv;
52 static int setfname(FILE *fd, char *arg)
54 if(strlen(arg)){
55 if(dumper)
56 pcap_dump_close(dumper);
58 dumpfile = strdup(arg);
59 if(!desc)
60 desc = pcap_open_dead(DLT_EN10MB, 96);
61 dumper = pcap_dump_open(desc, dumpfile);
64 printoutc(fd, "dumpfile=%s", dumpfile);
66 return 0;
69 static struct comlist cl[]={
70 {"pdump","============","DUMP Packets to file",NULL,NOARG},
71 {"pdump/active","0/1","start dumping data",dump,STRARG},
72 {"pdump/filename", "<file>", "set/show output filename (default: vde_dump.cap)", setfname, STRARG|WITHFILE},
76 * pcap_t *pcap_open_dead(int linktype, int snaplen)
77 * int pcap_compile(pcap_t *p, struct bpf_program *fp,
78 * char *str, int optimize, bpf_u_int32 netmask)
79 * int pcap_setfilter(pcap_t *p, struct bpf_program *fp)
80 * void pcap_freecode(struct bpf_program *)
83 // TODO activate debug as well when activated?
84 #define D_DUMP 0100
85 static struct dbgcl dl[]= {
86 {"pdump/packetin","dump incoming packet to file",D_DUMP|D_IN},
87 {"pdump/packetout","dump outgoing packet to file",D_DUMP|D_OUT},
91 int pktevent(struct dbgcl *event,va_list v)
93 // forse meglio static?
94 struct pcap_pkthdr hdr;
96 if( (desc == NULL) || (dumper == NULL) ){
97 return 0;
100 switch (event->tag) {
101 case D_PACKET|D_OUT:
102 case D_PACKET|D_IN: {
103 int port=va_arg(v,int);
104 unsigned char *buf=va_arg(v,char *);
105 int len=va_arg(v,int);
107 gettimeofday(&hdr.ts, NULL);
108 hdr.caplen = len;
109 hdr.len = len;
110 pcap_dump((u_char *)dumper, &hdr, buf);
113 return 0;
116 static void
117 __attribute__ ((constructor))
118 init (void)
120 ADDCL(cl);
121 ADDDBGCL(dl);
124 static void
125 __attribute__ ((destructor))
126 fini (void)
128 if(dumper)
129 pcap_dump_close(dumper);
131 DELCL(cl);
132 DELDBGCL(dl);