2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010, 2011 Daniel Borkmann.
5 * Subject to the GPL, version 2.
19 static int pcap_rw_pull_file_header(int fd
)
22 struct pcap_filehdr hdr
;
24 ret
= read(fd
, &hdr
, sizeof(hdr
));
25 if (unlikely(ret
!= sizeof(hdr
)))
28 pcap_validate_header_maybe_die(&hdr
);
33 static int pcap_rw_push_file_header(int fd
)
36 struct pcap_filehdr hdr
;
38 memset(&hdr
, 0, sizeof(hdr
));
39 pcap_prepare_header(&hdr
, LINKTYPE_EN10MB
, 0,
40 PCAP_DEFAULT_SNAPSHOT_LEN
);
42 ret
= write_or_die(fd
, &hdr
, sizeof(hdr
));
43 if (unlikely(ret
!= sizeof(hdr
))) {
44 whine("Failed to write pkt file header!\n");
51 static ssize_t
pcap_rw_write_pcap_pkt(int fd
, struct pcap_pkthdr
*hdr
,
52 uint8_t *packet
, size_t len
)
54 ssize_t ret
= write_or_die(fd
, hdr
, sizeof(*hdr
));
55 if (unlikely(ret
!= sizeof(*hdr
))) {
56 whine("Failed to write pkt header!\n");
60 if (unlikely(hdr
->len
!= len
))
63 ret
= write_or_die(fd
, packet
, hdr
->len
);
64 if (unlikely(ret
!= hdr
->len
)) {
65 whine("Failed to write pkt payload!\n");
69 return sizeof(*hdr
) + hdr
->len
;
72 static ssize_t
pcap_rw_read_pcap_pkt(int fd
, struct pcap_pkthdr
*hdr
,
73 uint8_t *packet
, size_t len
)
75 ssize_t ret
= read(fd
, hdr
, sizeof(*hdr
));
76 if (unlikely(ret
!= sizeof(*hdr
)))
79 if (unlikely(hdr
->len
> len
))
82 ret
= read(fd
, packet
, hdr
->len
);
83 if (unlikely(ret
!= hdr
->len
))
86 if (unlikely(hdr
->len
== 0))
87 return -EINVAL
; /* Bogus packet */
89 return sizeof(*hdr
) + hdr
->len
;
92 static void pcap_rw_fsync_pcap(int fd
)
97 struct pcap_file_ops pcap_rw_ops __read_mostly
= {
99 .pull_file_header
= pcap_rw_pull_file_header
,
100 .push_file_header
= pcap_rw_push_file_header
,
101 .write_pcap_pkt
= pcap_rw_write_pcap_pkt
,
102 .read_pcap_pkt
= pcap_rw_read_pcap_pkt
,
103 .fsync_pcap
= pcap_rw_fsync_pcap
,
106 int init_pcap_rw(int jumbo_support
)
108 return pcap_ops_group_register(&pcap_rw_ops
, PCAP_OPS_RW
);
111 void cleanup_pcap_rw(void)
113 pcap_ops_group_unregister(PCAP_OPS_RW
);