File: specter_PCAP.c

package info (click to toggle)
specter 1.4-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 736 kB
  • ctags: 542
  • sloc: ansic: 5,060; sh: 320; makefile: 239; perl: 169
file content (225 lines) | stat: -rw-r--r-- 5,038 bytes parent folder | download | duplicates (3)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* specter_PCAP.c
 *
 * specter output target for writing pcap-style files (like tcpdump)
 *
 * (C) 2002 by Harald Welte <laforge@gnumonks.org>
 *
 *  11 Jun 2004, Michal Kwiatkowski <ruby@joker.linuxstuff.pl>:
 *      Fixed for specter.
 */

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 
 *  as published by the Free Software Foundation
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <pcap.h>
#include <specter/specter.h>
#include <conffile/conffile.h>
#include "lret.h"

/* we rely on arrays, not pointers, so we need different macro here */
#undef IS_VALID
#define IS_VALID(ret_arr,x)	(ret_arr[x].p->flags & SPECTER_RETF_VALID)


#ifndef PCAP_DEFAULT
#define PCAP_DEFAULT	"/var/log/specter.pcap"
#endif


static config_entry_t my_config[] = {
	{ .key = "sync", .type = CONFIG_TYPE_BOOLEAN, .options = CONFIG_OPT_NONE,
		.u = { .value = 0 } },
	{ .key = "logfile", .type = CONFIG_TYPE_STRING, .options = CONFIG_OPT_NONE,
		.u = { .string = PCAP_DEFAULT } },
};


struct my_data {
	FILE *of;
};


#define LOCAL_RET_NUM 	5
static specter_local_ret_t local_ret[LOCAL_RET_NUM] = {
	{ "raw.pkt", 0 },
	{ "raw.pktlen", 0 },
	{ "ip.totlen", 0 },
	{ "oob.time_sec", 0 },
	{ "oob.time_usec", 0 },
};


static int output_pcap(config_entry_t *ce, void *data)
{
	struct pcap_pkthdr pchdr;
	struct my_data *md = data;

	pchdr.caplen = GET_VALUE(local_ret,1).ui32;
	pchdr.len = GET_VALUE(local_ret,2).ui32;

	if (IS_VALID(local_ret,3) && IS_VALID(local_ret,4)) {
		pchdr.ts.tv_sec = GET_VALUE(local_ret,3).ui32;
		pchdr.ts.tv_usec = GET_VALUE(local_ret,4).ui32;
	} else {
		/* use current system time */
		gettimeofday(&pchdr.ts, NULL);
	}

	if (fwrite(&pchdr, sizeof(pchdr), 1, md->of) != 1) {
		specter_log(SPECTER_ERROR, "Error during write: %s.\n",
			  strerror(errno));
		return -1;
	}
	if (fwrite(GET_VALUE(local_ret,0).ptr, pchdr.caplen, 1, md->of) != 1) {
		specter_log(SPECTER_ERROR, "Error during write: %s.\n",
			  strerror(errno));
		return -1;
	}

	if (GET_CE(ce,0)->u.value)
		fflush(md->of);

	return 0;
}

/* stolen from libpcap savefile.c */
#define LINKTYPE_RAW            101
#define TCPDUMP_MAGIC	0xa1b2c3d4

static int write_pcap_header(FILE *of)
{
	struct pcap_file_header pcfh;
	int ret;

	pcfh.magic = TCPDUMP_MAGIC;
	pcfh.version_major = PCAP_VERSION_MAJOR;
	pcfh.version_minor = PCAP_VERSION_MINOR;
	pcfh.thiszone = timezone;
	pcfh.sigfigs = 0;
	pcfh.snaplen = 64 * 1024; /* we don't know the length in advance */
	pcfh.linktype = LINKTYPE_RAW;

	ret =  fwrite(&pcfh, sizeof(pcfh), 1, of);
	fflush(of);

	return ret;
}


static int append_create_outfile(char *filename, FILE **fd)
{
	struct stat st_dummy;

	if (stat(filename, &st_dummy)) {
		*fd = fopen(filename, "w");
		if (!*fd) {
			specter_log(SPECTER_FATAL, "Can't open \"%s\": %s.\n",
				  filename, strerror(errno));
			return -1;
		}
		if (!write_pcap_header(*fd)) {
			specter_log(SPECTER_FATAL, "Can't write pcap header: %s.\n",
				  strerror(errno));
			return -1;
		}
	} else {
		*fd = fopen(filename, "a");
		if (!*fd) {
			specter_log(SPECTER_FATAL, "Can't open \"%s\": %s.\n", 
				filename, strerror(errno));
			return -1;
		}		
	}

	return 0;
}


static void *init_pcap(config_entry_t *ce)
{
	struct my_data *data;

	if (fill_local_ret(local_ret, LOCAL_RET_NUM) == -1)
		return NULL;

	if ((data = malloc(sizeof(struct my_data))) == NULL) {
		specter_log(SPECTER_FATAL, "Couldn't allocate data: %s.\n",
				strerror(errno));
		return NULL;
	}

	if (append_create_outfile(GET_CE(ce,1)->u.string, &data->of) == -1)
		return NULL;

	return data;
}

static void fini_pcap(config_entry_t *ce, void *data)
{
	struct my_data *md = data;

	fclose(md->of);

	free(data);
}

static int sighup_handler(config_entry_t *ce, void *data, int signal)
{
	struct my_data *md = data;

	switch (signal) {
	case SIGHUP:
		specter_log(SPECTER_INFO, "Reopening capture file.\n");
		fclose(md->of);
		if (append_create_outfile(GET_CE(ce,1)->u.string, &md->of) == -1)
			return -1;
		break;
	default:
		break;
	}

	return 0;
}


static specter_output_t pcap_op = {
	.name = "pcap",
	.ce_base = my_config,
	.ce_num = 2,
	.init = &init_pcap,
	.fini = &fini_pcap,
	.output = &output_pcap,
	.signal = &sighup_handler
};


void _init(void)
{
	if (register_output(&pcap_op, 0) == -1) {
		specter_log(SPECTER_FATAL, "Couldn't register.\n");
		exit(EXIT_FAILURE);
	}
}