docs: fixed minor typo
[netsniff-ng.git] / src / pcap.h
blob97741f80b76edcf29a84a33c540a39c6de10541d
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Copyright 2010 Emmanuel Roullit.
6 * Subject to the GPL, version 2.
7 */
9 #ifndef PCAP_H
10 #define PCAP_H
12 #include <unistd.h>
13 #include <stdint.h>
14 #include <sys/time.h>
15 #include <linux/if_packet.h>
17 #include "compiler.h"
18 #include "die.h"
20 #define TCPDUMP_MAGIC 0xa1b2c3d4
21 #define PCAP_VERSION_MAJOR 2
22 #define PCAP_VERSION_MINOR 4
23 #define PCAP_DEFAULT_SNAPSHOT_LEN 65535
25 #define LINKTYPE_NULL 0 /* BSD loopback encapsulation */
26 #define LINKTYPE_EN10MB 1 /* Ethernet (10Mb) */
27 #define LINKTYPE_EN3MB 2 /* Experimental Ethernet (3Mb) */
28 #define LINKTYPE_AX25 3 /* Amateur Radio AX.25 */
29 #define LINKTYPE_PRONET 4 /* Proteon ProNET Token Ring */
30 #define LINKTYPE_CHAOS 5 /* Chaos */
31 #define LINKTYPE_IEEE802 6 /* 802.5 Token Ring */
32 #define LINKTYPE_ARCNET 7 /* ARCNET, with BSD-style header */
33 #define LINKTYPE_SLIP 8 /* Serial Line IP */
34 #define LINKTYPE_PPP 9 /* Point-to-point Protocol */
35 #define LINKTYPE_FDDI 10 /* FDDI */
37 struct pcap_filehdr {
38 uint32_t magic;
39 uint16_t version_major;
40 uint16_t version_minor;
41 int32_t thiszone;
42 uint32_t sigfigs;
43 uint32_t snaplen;
44 uint32_t linktype;
47 struct pcap_timeval {
48 int32_t tv_sec;
49 int32_t tv_usec;
52 struct pcap_nsf_pkthdr {
53 struct timeval ts;
54 uint32_t caplen;
55 uint32_t len;
58 struct pcap_pkthdr {
59 struct pcap_timeval ts;
60 uint32_t caplen;
61 uint32_t len;
64 static inline void tpacket_hdr_to_pcap_pkthdr(struct tpacket_hdr *thdr,
65 struct pcap_pkthdr *phdr)
67 phdr->ts.tv_sec = thdr->tp_sec;
68 phdr->ts.tv_usec = thdr->tp_usec;
69 phdr->caplen = thdr->tp_snaplen;
70 phdr->len = thdr->tp_len;
73 static inline void pcap_pkthdr_to_tpacket_hdr(struct pcap_pkthdr *phdr,
74 struct tpacket_hdr *thdr)
76 thdr->tp_sec = phdr->ts.tv_sec;
77 thdr->tp_usec = phdr->ts.tv_usec;
78 thdr->tp_snaplen = phdr->caplen;
79 thdr->tp_len = phdr->len;
82 enum pcap_ops_groups {
83 PCAP_OPS_RW = 0,
84 #define PCAP_OPS_RW PCAP_OPS_RW
85 PCAP_OPS_SG,
86 #define PCAP_OPS_SG PCAP_OPS_SG
87 PCAP_OPS_MMAP,
88 #define PCAP_OPS_MMAP PCAP_OPS_MMAP
89 __PCAP_OPS_MAX,
91 #define PCAP_OPS_MAX (__PCAP_OPS_MAX - 1)
92 #define PCAP_OPS_SIZ (__PCAP_OPS_MAX)
94 enum pcap_mode {
95 PCAP_MODE_READ = 0,
96 PCAP_MODE_WRITE,
99 struct pcap_file_ops {
100 char *name;
101 int (*pull_file_header)(int fd);
102 int (*push_file_header)(int fd);
103 int (*prepare_writing_pcap)(int fd);
104 ssize_t (*write_pcap_pkt)(int fd, struct pcap_pkthdr *hdr,
105 uint8_t *packet, size_t len);
106 void (*fsync_pcap)(int fd);
107 int (*prepare_reading_pcap)(int fd);
108 ssize_t (*read_pcap_pkt)(int fd, struct pcap_pkthdr *hdr,
109 uint8_t *packet, size_t len);
110 void (*prepare_close_pcap)(int fd, enum pcap_mode mode);
113 extern struct pcap_file_ops *pcap_ops[PCAP_OPS_SIZ];
115 extern int pcap_ops_group_register(struct pcap_file_ops *ops,
116 enum pcap_ops_groups group);
117 extern void pcap_ops_group_unregister(enum pcap_ops_groups group);
119 static inline struct pcap_file_ops *
120 pcap_ops_group_get(enum pcap_ops_groups group)
122 return pcap_ops[group];
125 static inline void pcap_prepare_header(struct pcap_filehdr *hdr,
126 uint32_t linktype,
127 int32_t thiszone, uint32_t snaplen)
129 hdr->magic = TCPDUMP_MAGIC;
130 hdr->version_major = PCAP_VERSION_MAJOR;
131 hdr->version_minor = PCAP_VERSION_MINOR;
132 hdr->thiszone = thiszone;
133 hdr->sigfigs = 0;
134 hdr->snaplen = snaplen;
135 hdr->linktype = linktype;
138 static inline void pcap_validate_header_maybe_die(struct pcap_filehdr *hdr)
140 if (unlikely(hdr->magic != TCPDUMP_MAGIC ||
141 hdr->version_major != PCAP_VERSION_MAJOR ||
142 hdr->version_minor != PCAP_VERSION_MINOR ||
143 hdr->linktype != LINKTYPE_EN10MB))
144 panic("This file has not a valid pcap header!\n");
147 extern int init_pcap_mmap(int jumbo_support);
148 extern int init_pcap_rw(int jumbo_support);
149 extern int init_pcap_sg(int jumbo_support);
151 extern void cleanup_pcap_mmap(void);
152 extern void cleanup_pcap_rw(void);
153 extern void cleanup_pcap_sg(void);
155 static inline int init_pcap(int jumbo_support)
157 init_pcap_rw(jumbo_support);
158 init_pcap_sg(jumbo_support);
159 init_pcap_mmap(jumbo_support);
160 return 0;
163 static inline void cleanup_pcap(void)
165 cleanup_pcap_rw();
166 cleanup_pcap_sg();
167 cleanup_pcap_mmap();
170 #endif /* PCAP_H */