1/* $OpenBSD: pflogd.c,v 1.27 2004/02/13 19:01:57 otto Exp $ */
2
3/*
4 * Copyright (c) 2001 Theo de Raadt
5 * Copyright (c) 2001 Can Erkin Acar
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
--- 17 unchanged lines hidden (view full) ---
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/contrib/pf/pflogd/pflogd.c 134578 2004-08-31 18:04:34Z mlaier $");
35
36#include <sys/types.h>
37#include <sys/ioctl.h>
38#include <sys/file.h>
39#include <sys/stat.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
--- 277 unchanged lines hidden (view full) ---
320
321 return (0);
322}
323
324int
325scan_dump(FILE *fp, off_t size)
326{
327 struct pcap_file_header hdr;
328#ifdef __FreeBSD__
329 struct pcap_sf_pkthdr ph;
330#else
331 struct pcap_pkthdr ph;
332#endif
333 off_t pos;
334
335 /*
336 * Must read the file, compare the header against our new
337 * options (in particular, snaplen) and adjust our options so
338 * that we generate a correct file. Furthermore, check the file
339 * for consistency so that we can append safely.
340 *
--- 53 unchanged lines hidden (view full) ---
394 return (1);
395}
396
397/* dump a packet directly to the stream, which is unbuffered */
398void
399dump_packet_nobuf(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
400{
401 FILE *f = (FILE *)user;
402#ifdef __FreeBSD__
403 struct pcap_sf_pkthdr sh;
404#endif
405
406 if (suspended) {
407 packets_dropped++;
408 return;
409 }
410
411#ifdef __FreeBSD__
412 sh.ts.tv_sec = (bpf_int32)h->ts.tv_sec;
413 sh.ts.tv_usec = (bpf_int32)h->ts.tv_usec;
414 sh.caplen = h->caplen;
415 sh.len = h->len;
416
417 if (fwrite((char *)&sh, sizeof(sh), 1, f) != 1) {
418#else
419 if (fwrite((char *)h, sizeof(*h), 1, f) != 1) {
420#endif
421 /* try to undo header to prevent corruption */
422 off_t pos = ftello(f);
423#ifdef __FreeBSD__
424 if (pos < sizeof(sh) ||
425 ftruncate(fileno(f), pos - sizeof(sh))) {
426#else
427 if (pos < sizeof(*h) ||
428 ftruncate(fileno(f), pos - sizeof(*h))) {
429#endif
430 logmsg(LOG_ERR, "Write failed, corrupted logfile!");
431 set_suspended(1);
432 gotsig_close = 1;
433 return;
434 }
435 goto error;
436 }
437
--- 52 unchanged lines hidden (view full) ---
490 bufpkt = 0;
491}
492
493/* append packet to the buffer, flushing if necessary */
494void
495dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
496{
497 FILE *f = (FILE *)user;
498#ifdef __FreeBSD__
499 struct pcap_sf_pkthdr sh;
500 size_t len = sizeof(sh) + h->caplen;
501#else
502 size_t len = sizeof(*h) + h->caplen;
503#endif
504
505 if (len < sizeof(*h) || h->caplen > (size_t)cur_snaplen) {
506 logmsg(LOG_NOTICE, "invalid size %u (%u/%u), packet dropped",
507 len, cur_snaplen, snaplen);
508 packets_dropped++;
509 return;
510 }
511
--- 11 unchanged lines hidden (view full) ---
523 }
524
525 if (len > bufleft) {
526 dump_packet_nobuf(user, h, sp);
527 return;
528 }
529
530 append:
531#ifdef __FreeBSD__
532 sh.ts.tv_sec = (bpf_int32)h->ts.tv_sec;
533 sh.ts.tv_usec = (bpf_int32)h->ts.tv_usec;
534 sh.caplen = h->caplen;
535 sh.len = h->len;
536
537 memcpy(bufpos, &sh, sizeof(sh));
538 memcpy(bufpos + sizeof(sh), sp, h->caplen);
539#else
540 memcpy(bufpos, h, sizeof(*h));
541 memcpy(bufpos + sizeof(*h), sp, h->caplen);
542#endif
543
544 bufpos += len;
545 bufleft -= len;
546 bufpkt++;
547
548 return;
549}
550
--- 149 unchanged lines hidden ---
2
3/*
4 * Copyright (c) 2001 Theo de Raadt
5 * Copyright (c) 2001 Can Erkin Acar
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
--- 17 unchanged lines hidden (view full) ---
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/contrib/pf/pflogd/pflogd.c 134578 2004-08-31 18:04:34Z mlaier $");
35
36#include <sys/types.h>
37#include <sys/ioctl.h>
38#include <sys/file.h>
39#include <sys/stat.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
--- 277 unchanged lines hidden (view full) ---
320
321 return (0);
322}
323
324int
325scan_dump(FILE *fp, off_t size)
326{
327 struct pcap_file_header hdr;
328#ifdef __FreeBSD__
329 struct pcap_sf_pkthdr ph;
330#else
331 struct pcap_pkthdr ph;
332#endif
333 off_t pos;
334
335 /*
336 * Must read the file, compare the header against our new
337 * options (in particular, snaplen) and adjust our options so
338 * that we generate a correct file. Furthermore, check the file
339 * for consistency so that we can append safely.
340 *
--- 53 unchanged lines hidden (view full) ---
394 return (1);
395}
396
397/* dump a packet directly to the stream, which is unbuffered */
398void
399dump_packet_nobuf(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
400{
401 FILE *f = (FILE *)user;
402#ifdef __FreeBSD__
403 struct pcap_sf_pkthdr sh;
404#endif
405
406 if (suspended) {
407 packets_dropped++;
408 return;
409 }
410
411#ifdef __FreeBSD__
412 sh.ts.tv_sec = (bpf_int32)h->ts.tv_sec;
413 sh.ts.tv_usec = (bpf_int32)h->ts.tv_usec;
414 sh.caplen = h->caplen;
415 sh.len = h->len;
416
417 if (fwrite((char *)&sh, sizeof(sh), 1, f) != 1) {
418#else
419 if (fwrite((char *)h, sizeof(*h), 1, f) != 1) {
420#endif
421 /* try to undo header to prevent corruption */
422 off_t pos = ftello(f);
423#ifdef __FreeBSD__
424 if (pos < sizeof(sh) ||
425 ftruncate(fileno(f), pos - sizeof(sh))) {
426#else
427 if (pos < sizeof(*h) ||
428 ftruncate(fileno(f), pos - sizeof(*h))) {
429#endif
430 logmsg(LOG_ERR, "Write failed, corrupted logfile!");
431 set_suspended(1);
432 gotsig_close = 1;
433 return;
434 }
435 goto error;
436 }
437
--- 52 unchanged lines hidden (view full) ---
490 bufpkt = 0;
491}
492
493/* append packet to the buffer, flushing if necessary */
494void
495dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
496{
497 FILE *f = (FILE *)user;
498#ifdef __FreeBSD__
499 struct pcap_sf_pkthdr sh;
500 size_t len = sizeof(sh) + h->caplen;
501#else
502 size_t len = sizeof(*h) + h->caplen;
503#endif
504
505 if (len < sizeof(*h) || h->caplen > (size_t)cur_snaplen) {
506 logmsg(LOG_NOTICE, "invalid size %u (%u/%u), packet dropped",
507 len, cur_snaplen, snaplen);
508 packets_dropped++;
509 return;
510 }
511
--- 11 unchanged lines hidden (view full) ---
523 }
524
525 if (len > bufleft) {
526 dump_packet_nobuf(user, h, sp);
527 return;
528 }
529
530 append:
531#ifdef __FreeBSD__
532 sh.ts.tv_sec = (bpf_int32)h->ts.tv_sec;
533 sh.ts.tv_usec = (bpf_int32)h->ts.tv_usec;
534 sh.caplen = h->caplen;
535 sh.len = h->len;
536
537 memcpy(bufpos, &sh, sizeof(sh));
538 memcpy(bufpos + sizeof(sh), sp, h->caplen);
539#else
540 memcpy(bufpos, h, sizeof(*h));
541 memcpy(bufpos + sizeof(*h), sp, h->caplen);
542#endif
543
544 bufpos += len;
545 bufleft -= len;
546 bufpkt++;
547
548 return;
549}
550
--- 149 unchanged lines hidden ---