Showing 1 changed files with 53 additions and 14 deletions
+53 -14
main.c
... ...
@@ -3,6 +3,7 @@
3 3
 #include <stdint.h>
4 4
 #include <inttypes.h>
5 5
 #include <string.h>
6
+#include <signal.h>
6 7
 #include <pcap.h>
7 8
 #include <time.h>
8 9
 
... ...
@@ -41,6 +42,31 @@ static uint64_t st_packet_num = 0;
41 42
 //~ static pcap_t *st_hdl_out_pcap = NULL;
42 43
 //~ static pcap_dumper_t *st_hdl_out_pcapdumper = NULL;
43 44
 static FILE *st_hdl_out_mypcap = NULL;
45
+static pcap_t *st_hdl_capture = NULL;
46
+
47
+static void handle_sigint(int signo)
48
+{
49
+	(void)signo;
50
+	if (st_hdl_capture != NULL) {
51
+		pcap_breakloop(st_hdl_capture);
52
+	}
53
+}
54
+
55
+static void close_output_files(void)
56
+{
57
+	for (inodelist_t *node = inodelist_get_head(); node != NULL; node = node->next) {
58
+		if (node->data.mydumper != NULL) {
59
+			mypcap_flush(node->data.mydumper);
60
+			mypcap_close(node->data.mydumper);
61
+			node->data.mydumper = NULL;
62
+		}
63
+	}
64
+	if (st_hdl_out_mypcap != NULL) {
65
+		mypcap_flush(st_hdl_out_mypcap);
66
+		mypcap_close(st_hdl_out_mypcap);
67
+		st_hdl_out_mypcap = NULL;
68
+	}
69
+}
44 70
 
45 71
 static void print_capture_progress(void)
46 72
 {
... ...
@@ -220,21 +246,31 @@ int32_t main(int argc, char **argv )
220 246
 		mask = 0;
221 247
 	}
222 248
 	
223
-	pcap_t *pcap = pcap_open_live(st_device, MAX_CAPLEN, 0, 1000, errbuff);
224
-	if (pcap == NULL) {
249
+	st_hdl_capture = pcap_open_live(st_device, MAX_CAPLEN, 0, 1000, errbuff);
250
+	if (st_hdl_capture == NULL) {
225 251
 		fprintf(stderr, "ERROR: Cannot open %s for capture\n", st_device);
226 252
 		exit(3);
227 253
 	}
254
+	struct sigaction sigint_action;
255
+	memset(&sigint_action, 0, sizeof(sigint_action));
256
+	sigint_action.sa_handler = handle_sigint;
257
+	sigemptyset(&sigint_action.sa_mask);
258
+	if (sigaction(SIGINT, &sigint_action, NULL) == -1) {
259
+		perror("ERROR: Cannot install SIGINT handler");
260
+		pcap_close(st_hdl_capture);
261
+		return 8;
262
+	}
228 263
 
229
-	if (pcap_compile(pcap, &fp, st_filter, 0, net) == -1) {
230
-		fprintf(stderr, "ERROR: Couldn't parse filter [%s]: %s\n", st_filter, pcap_geterr(pcap));
264
+	if (pcap_compile(st_hdl_capture, &fp, st_filter, 0, net) == -1) {
265
+		fprintf(stderr, "ERROR: Couldn't parse filter [%s]: %s\n", st_filter, pcap_geterr(st_hdl_capture));
231 266
 		exit(4);
232 267
 	}
233 268
 	
234
-	if (pcap_setfilter(pcap, &fp) == -1) {
235
-		fprintf(stderr, "ERROR: Couldn't install filter [%s]: %s\n", st_filter, pcap_geterr(pcap));
269
+	if (pcap_setfilter(st_hdl_capture, &fp) == -1) {
270
+		fprintf(stderr, "ERROR: Couldn't install filter [%s]: %s\n", st_filter, pcap_geterr(st_hdl_capture));
236 271
 		exit(5);
237 272
 	}
273
+	pcap_freecode(&fp);
238 274
 	
239 275
 	//Set file output prefix
240 276
 	time_t     now;
... ...
@@ -254,7 +290,7 @@ int32_t main(int argc, char **argv )
254 290
 	st_hdl_out_mypcap=mypcap_open(complete_dump_path, "w+");
255 291
 	if (st_hdl_out_mypcap == NULL) {
256 292
 		fprintf(stderr, "ERROR: Cannot create %s\n", complete_dump_path);
257
-		pcap_close(pcap);
293
+		pcap_close(st_hdl_capture);
258 294
 		return 7;
259 295
 	}
260 296
 	
... ...
@@ -267,18 +303,21 @@ int32_t main(int argc, char **argv )
267 303
 	printf("listening on %s, capture size %d bytes\n",st_device,MAX_CAPLEN);
268 304
 	printf("filtering on [%s]\n",st_filter);
269 305
 	printf("complete dump file : %s\n",complete_dump_path);
270
-	if (pcap_datalink(pcap) != DLT_EN10MB) {
306
+	if (pcap_datalink(st_hdl_capture) != DLT_EN10MB) {
271 307
 		fprintf(stderr, "ERROR: No Ethernet headers - not supported\n");
308
+		close_output_files();
309
+		pcap_close(st_hdl_capture);
272 310
 		exit(6);
273 311
 	} else {
274 312
 		printf("link-type EN10MB (Ethernet)\n");
275 313
 	}
276
-	
277
-	pcap_loop(pcap, 0, pcap_loop_cb, NULL );
278
-	pcap_loop(pcap, 100, pcap_loop_cb, NULL );
279
-
280
-	mypcap_close(st_hdl_out_mypcap);
281
-	pcap_close( pcap );
314
+	int loop_result = pcap_loop(st_hdl_capture, 0, pcap_loop_cb, NULL);
315
+	if (loop_result == -1) {
316
+		fprintf(stderr, "ERROR: Capture failed: %s\n", pcap_geterr(st_hdl_capture));
317
+	}
318
+	printf("\n");
319
+	close_output_files();
320
+	pcap_close(st_hdl_capture);
282 321
 	
283 322
 #ifdef _DEBUG
284 323
 	printf("\n");