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 file 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
|
---|
49 | #define PCAP_MAGIC_NANO 0xA1B23C4D
|
---|
50 | #define PCAP_MAJOR_VERSION 0x0002
|
---|
51 | #define PCAP_MINOR_VERSION 0x0004
|
---|
52 | #define PCAP_SNAP_LEN 0x00040000
|
---|
53 |
|
---|
54 | #define PCAP_LINKTYPE_ETHERNET 1 /* IEEE 802.3 Ethernet*/
|
---|
55 |
|
---|
56 | /** Header of the .pcap file
|
---|
57 | */
|
---|
58 | typedef struct {
|
---|
59 | uint32_t magic_number;
|
---|
60 | uint16_t major_v;
|
---|
61 | uint16_t minor_v;
|
---|
62 | uint32_t reserved1;
|
---|
63 | uint32_t reserved2;
|
---|
64 | uint32_t snaplen;
|
---|
65 | uint32_t additional; /** The LinkType and additional information field is in the form */
|
---|
66 | } __attribute__((packed, aligned(4))) pcap_file_header_t;
|
---|
67 |
|
---|
68 | typedef struct pcap_packet_header {
|
---|
69 | uint32_t seconds_stamp;
|
---|
70 | uint32_t magic_stamp;
|
---|
71 | uint32_t captured_length;
|
---|
72 | uint32_t original_length;
|
---|
73 | } pcap_packet_header_t;
|
---|
74 |
|
---|
75 | typedef struct pcap_writer pcap_writer_t;
|
---|
76 |
|
---|
77 | typedef struct {
|
---|
78 | size_t (*write_u32)(struct pcap_writer *, uint32_t);
|
---|
79 | size_t (*write_u16)(struct pcap_writer *, uint16_t);
|
---|
80 | size_t (*write_buffer)(struct pcap_writer *, const void *, size_t);
|
---|
81 | void (*close)(struct pcap_writer *);
|
---|
82 |
|
---|
83 | } pcap_writer_ops_t;
|
---|
84 |
|
---|
85 | /** Interface for working with .pcap file
|
---|
86 | */
|
---|
87 | typedef struct pcap_writer {
|
---|
88 | void *data;
|
---|
89 | pcap_writer_ops_t *ops;
|
---|
90 | } pcap_writer_t;
|
---|
91 |
|
---|
92 | errno_t pcap_writer_to_file_init(pcap_writer_t *writer, const char *filename);
|
---|
93 |
|
---|
94 | extern void pcap_writer_add_header(pcap_writer_t *);
|
---|
95 | extern void pcap_writer_add_packet(
|
---|
96 | pcap_writer_t *writer, const void *captured_packet, size_t size);
|
---|
97 |
|
---|
98 | extern void pcap_set_time(pcap_packet_header_t *header, bool nano);
|
---|
99 |
|
---|
100 | #endif
|
---|
101 |
|
---|
102 | /** @}
|
---|
103 | */
|
---|