pflogd.c (130617) | pflogd.c (134578) |
---|---|
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> | 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 130617 2004-06-16 23:39:33Z mlaier $"); | 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; | 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 |
|
328 struct pcap_pkthdr ph; | 331 struct pcap_pkthdr ph; |
332#endif |
|
329 off_t pos; 330 331 /* 332 * Must read the file, compare the header against our new 333 * options (in particular, snaplen) and adjust our options so 334 * that we generate a correct file. Furthermore, check the file 335 * for consistency so that we can append safely. 336 * --- 53 unchanged lines hidden (view full) --- 390 return (1); 391} 392 393/* dump a packet directly to the stream, which is unbuffered */ 394void 395dump_packet_nobuf(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) 396{ 397 FILE *f = (FILE *)user; | 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 |
|
398 399 if (suspended) { 400 packets_dropped++; 401 return; 402 } 403 | 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 |
|
404 if (fwrite((char *)h, sizeof(*h), 1, f) != 1) { | 419 if (fwrite((char *)h, sizeof(*h), 1, f) != 1) { |
420#endif |
|
405 /* try to undo header to prevent corruption */ 406 off_t pos = ftello(f); | 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 |
|
407 if (pos < sizeof(*h) || 408 ftruncate(fileno(f), pos - sizeof(*h))) { | 427 if (pos < sizeof(*h) || 428 ftruncate(fileno(f), pos - sizeof(*h))) { |
429#endif |
|
409 logmsg(LOG_ERR, "Write failed, corrupted logfile!"); 410 set_suspended(1); 411 gotsig_close = 1; 412 return; 413 } 414 goto error; 415 } 416 --- 52 unchanged lines hidden (view full) --- 469 bufpkt = 0; 470} 471 472/* append packet to the buffer, flushing if necessary */ 473void 474dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) 475{ 476 FILE *f = (FILE *)user; | 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 |
|
477 size_t len = sizeof(*h) + h->caplen; | 502 size_t len = sizeof(*h) + h->caplen; |
503#endif |
|
478 479 if (len < sizeof(*h) || h->caplen > (size_t)cur_snaplen) { 480 logmsg(LOG_NOTICE, "invalid size %u (%u/%u), packet dropped", 481 len, cur_snaplen, snaplen); 482 packets_dropped++; 483 return; 484 } 485 --- 11 unchanged lines hidden (view full) --- 497 } 498 499 if (len > bufleft) { 500 dump_packet_nobuf(user, h, sp); 501 return; 502 } 503 504 append: | 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 |
|
505 memcpy(bufpos, h, sizeof(*h)); 506 memcpy(bufpos + sizeof(*h), sp, h->caplen); | 540 memcpy(bufpos, h, sizeof(*h)); 541 memcpy(bufpos + sizeof(*h), sp, h->caplen); |
542#endif |
|
507 508 bufpos += len; 509 bufleft -= len; 510 bufpkt++; 511 512 return; 513} 514 --- 149 unchanged lines hidden --- | 543 544 bufpos += len; 545 bufleft -= len; 546 bufpkt++; 547 548 return; 549} 550 --- 149 unchanged lines hidden --- |