1 | /*
|
---|
2 | * Copyright (c) 2023 Nataliia Korop
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * @addtogroup libpcap
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | * @brief Headers and functions for PCAP format and packets to be dumped.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #ifndef PCAP_H_
|
---|
39 | #define PCAP_H_
|
---|
40 |
|
---|
41 | #include <stdint.h>
|
---|
42 | #include <stdio.h>
|
---|
43 | #include <str_error.h>
|
---|
44 | #include <time.h>
|
---|
45 | #include <stdbool.h>
|
---|
46 | #include <errno.h>
|
---|
47 |
|
---|
48 | #define PCAP_MAGIC_MICRO 0xA1B2C3D4 /** Sets time in seconds and microseconds in packet records. */
|
---|
49 | #define PCAP_MAGIC_NANO 0xA1B23C4D /** Sets time in seconds and nanoseconds in packet records. */
|
---|
50 | #define PCAP_MAJOR_VERSION 0x0002 /** Major version of the PCAP format. */
|
---|
51 | #define PCAP_MINOR_VERSION 0x0004 /** Miner version of the PCAP format. */
|
---|
52 | #define PCAP_SNAP_LEN 0x00040000 /** Maximum number of bytes that can be captured for one packet record. */
|
---|
53 |
|
---|
54 | #define PCAP_LINKTYPE_ETHERNET 1 /* IEEE 802.3 Ethernet */
|
---|
55 | #define PCAP_LINKTYPE_IP_RAW 101 /* Raw IP packet */
|
---|
56 | #define PCAP_LINKTYPE_IEEE802_11_RADIO 127
|
---|
57 | #define PCAP_LINKTYPE_USB_LINUX_MMAPPED 220
|
---|
58 |
|
---|
59 | /** Header of the .pcap file. */
|
---|
60 | typedef struct {
|
---|
61 | uint32_t magic_number;
|
---|
62 | uint16_t major_v;
|
---|
63 | uint16_t minor_v;
|
---|
64 | uint32_t reserved1;
|
---|
65 | uint32_t reserved2;
|
---|
66 | uint32_t snaplen;
|
---|
67 | uint32_t additional; /** The LinkType and additional information field is in the form */
|
---|
68 | } pcap_file_header_t;
|
---|
69 |
|
---|
70 | /** Header of the packet to be dumped to .pcap file. */
|
---|
71 | typedef struct pcap_packet_header {
|
---|
72 | uint32_t seconds_stamp;
|
---|
73 | uint32_t magic_stamp;
|
---|
74 | uint32_t captured_length;
|
---|
75 | uint32_t original_length;
|
---|
76 | } pcap_packet_header_t;
|
---|
77 |
|
---|
78 | typedef struct pcap_writer pcap_writer_t;
|
---|
79 |
|
---|
80 | /** Writing operations for destination buffer. */
|
---|
81 | typedef struct {
|
---|
82 | errno_t (*open)(pcap_writer_t *, const char *);
|
---|
83 | size_t (*write_u32)(pcap_writer_t *, uint32_t);
|
---|
84 | size_t (*write_u16)(pcap_writer_t *, uint16_t);
|
---|
85 | size_t (*write_buffer)(pcap_writer_t *, const void *, size_t);
|
---|
86 | void (*close)(pcap_writer_t *);
|
---|
87 | } pcap_writer_ops_t;
|
---|
88 |
|
---|
89 | /** Structure for writing data to the destination buffer. */
|
---|
90 | struct pcap_writer {
|
---|
91 | /** Writing buffer. */
|
---|
92 | void *data;
|
---|
93 | /** Writing operations for working with the buffer. */
|
---|
94 | pcap_writer_ops_t *ops;
|
---|
95 | };
|
---|
96 |
|
---|
97 | extern void pcap_writer_add_header(pcap_writer_t *writer, uint32_t linktype);
|
---|
98 | extern void pcap_writer_add_packet(pcap_writer_t *writer, const void *captured_packet, size_t size);
|
---|
99 | extern void pcap_set_time(pcap_packet_header_t *header);
|
---|
100 |
|
---|
101 | #endif
|
---|
102 |
|
---|
103 | /** @}
|
---|
104 | */
|
---|