Skip to content

Commit d79f93c

Browse files
committed
Merge PR #24: Be very quiet when using stdout as pcap output file
Co-authored-by: Jordan Ritter <[email protected]>
2 parents c9ddace + 4da2fb2 commit d79f93c

File tree

2 files changed

+45
-31
lines changed

2 files changed

+45
-31
lines changed

ngrep.c

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,14 @@ int main(int argc, char **argv) {
367367

368368
/* Setup PCAP input */
369369

370-
if (setup_pcap_source())
370+
char *dev = usedev ? usedev :
371+
#if defined(_WIN32)
372+
win32_choosedevice();
373+
#else
374+
pcap_lookupdev(pc_err);
375+
#endif
376+
377+
if (setup_pcap_source(dev))
371378
clean_exit(2);
372379

373380
/* Setup BPF filter */
@@ -382,6 +389,33 @@ int main(int argc, char **argv) {
382389
}
383390
}
384391

392+
/* Setup PCAP output */
393+
394+
if (dump_file) {
395+
pd_dump = pcap_dump_open(pd, dump_file);
396+
if (!pd_dump) {
397+
fprintf(stderr, "fatal: %s\n", pcap_geterr(pd));
398+
clean_exit(2);
399+
} else if (stdout == pcap_dump_file(pd_dump)) {
400+
/* Don't output to stdout, when using it as output file */
401+
quiet = 5;
402+
}
403+
}
404+
405+
if (quiet < 2) {
406+
if (read_file)
407+
printf("input: %s\n", read_file);
408+
else {
409+
printf("interface: %s", dev);
410+
411+
if (net.s_addr && mask.s_addr) {
412+
printf(" (%s/", inet_ntoa(net));
413+
printf("%s)", inet_ntoa(mask));
414+
}
415+
printf("\n");
416+
}
417+
}
418+
385419
if (filter) {
386420
if (quiet < 2)
387421
printf("filter: %s\n", filter);
@@ -402,14 +436,8 @@ int main(int argc, char **argv) {
402436
}
403437

404438
/* Misc */
405-
406-
if (dump_file) {
407-
pd_dump = pcap_dump_open(pd, dump_file);
408-
if (!pd_dump) {
409-
fprintf(stderr, "fatal: %s\n", pcap_geterr(pd));
410-
clean_exit(2);
411-
} else printf("output: %s\n", dump_file);
412-
}
439+
if (dump_file && quiet < 2)
440+
printf("output: %s\n", dump_file);
413441

414442
update_windowsize(0);
415443

@@ -429,7 +457,7 @@ int main(int argc, char **argv) {
429457
return 0;
430458
}
431459

432-
int setup_pcap_source(void) {
460+
int setup_pcap_source(char *dev) {
433461
if (read_file) {
434462

435463
if (!(pd = pcap_open_offline(read_file, pc_err))) {
@@ -438,17 +466,9 @@ int setup_pcap_source(void) {
438466
}
439467

440468
live_read = 0;
441-
printf("input: %s\n", read_file);
442469

443470
} else {
444471

445-
char *dev = usedev ? usedev :
446-
#if defined(_WIN32)
447-
win32_choosedevice();
448-
#else
449-
pcap_lookupdev(pc_err);
450-
#endif
451-
452472
if (!dev) {
453473
perror(pc_err);
454474
return 1;
@@ -464,15 +484,6 @@ int setup_pcap_source(void) {
464484
memset(&net, 0, sizeof(net));
465485
memset(&mask, 0, sizeof(mask));
466486
}
467-
468-
if (quiet < 2) {
469-
printf("interface: %s", dev);
470-
if (net.s_addr && mask.s_addr) {
471-
printf(" (%s/", inet_ntoa(net));
472-
printf("%s)", inet_ntoa(mask));
473-
}
474-
printf("\n");
475-
}
476487
}
477488

478489
switch(pcap_datalink(pd)) {
@@ -916,6 +927,12 @@ void dump_packet(struct pcap_pkthdr *h, u_char *p, uint8_t proto, unsigned char
916927
if ((len > 0 && match_func(data, len, &match_index, &match_size) == invert_match) && !keep_matching)
917928
return;
918929

930+
if (pd_dump)
931+
pcap_dump((u_char*)pd_dump, h, p);
932+
933+
if (quiet > 3)
934+
return;
935+
919936
if (!live_read && want_delay)
920937
dump_delay(h);
921938

@@ -980,9 +997,6 @@ void dump_packet(struct pcap_pkthdr *h, u_char *p, uint8_t proto, unsigned char
980997
if (quiet < 3)
981998
dump_func(data, len, match_index, match_size);
982999

983-
if (pd_dump)
984-
pcap_dump((u_char*)pd_dump, h, p);
985-
9861000
#if USE_TCPKILL
9871001
if (tcpkill_active)
9881002
tcpkill_kill(h, p, link_offset, tcpkill_active);

ngrep.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ typedef enum {
108108
* Prototypes function signatures.
109109
*/
110110

111-
int setup_pcap_source(void);
111+
int setup_pcap_source(char *);
112112
int setup_bpf_filter(char **);
113113
int setup_matcher(void);
114114

0 commit comments

Comments
 (0)