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 | /** @addtogroup libpcap
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief pcap inteface: Dumping interface for the device which packets we want to dump
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <errno.h>
|
---|
37 | #include <str.h>
|
---|
38 | #include <io/log.h>
|
---|
39 | #include "pcap_dumper.h"
|
---|
40 |
|
---|
41 | #define SHORT_OPS_BYTE_COUNT 60
|
---|
42 |
|
---|
43 | /** Initialize writing to .pcap file.
|
---|
44 | *
|
---|
45 | * @param writer Interface for working with .pcap file.
|
---|
46 | * @param filename Name of the file for dumping packets.
|
---|
47 | * @return EOK on success or an error code.
|
---|
48 | *
|
---|
49 | */
|
---|
50 | static errno_t pcap_writer_to_file_init(pcap_writer_t *writer, const char *filename)
|
---|
51 | {
|
---|
52 | writer->data = fopen(filename, "a");
|
---|
53 | if (writer->data == NULL) {
|
---|
54 | return EINVAL;
|
---|
55 | }
|
---|
56 | pcap_writer_add_header(writer, (uint32_t)PCAP_LINKTYPE_ETHERNET, false);
|
---|
57 |
|
---|
58 | return EOK;
|
---|
59 | }
|
---|
60 |
|
---|
61 | static errno_t pcap_writer_to_file_init_append(pcap_writer_t *writer, const char *filename)
|
---|
62 | {
|
---|
63 | writer->data = fopen(filename, "a");
|
---|
64 | if (writer->data == NULL) {
|
---|
65 | return EINVAL;
|
---|
66 | }
|
---|
67 |
|
---|
68 | return EOK;
|
---|
69 | }
|
---|
70 |
|
---|
71 | static errno_t pcap_writer_to_file_usb_init(pcap_writer_t *writer, const char *filename)
|
---|
72 | {
|
---|
73 | writer->data = fopen(filename, "a");
|
---|
74 | if (writer->data == NULL) {
|
---|
75 | return EINVAL;
|
---|
76 | }
|
---|
77 | pcap_writer_add_header(writer, (uint32_t)PCAP_LINKTYPE_USB_LINUX_MMAPPED, false);
|
---|
78 |
|
---|
79 | return EOK;
|
---|
80 | }
|
---|
81 |
|
---|
82 | static size_t pcap_file_w32(pcap_writer_t *writer, uint32_t data)
|
---|
83 | {
|
---|
84 | return fwrite(&data, 1, 4, (FILE *)writer->data);
|
---|
85 | }
|
---|
86 |
|
---|
87 | static size_t pcap_file_w16(pcap_writer_t *writer, uint16_t data)
|
---|
88 | {
|
---|
89 | return fwrite(&data, 1, 2, (FILE *)writer->data);
|
---|
90 | }
|
---|
91 |
|
---|
92 | static size_t pcap_file_wbuffer(pcap_writer_t *writer, const void *data, size_t size)
|
---|
93 | {
|
---|
94 | assert(writer->data);
|
---|
95 | return fwrite(data, 1, size, (FILE *)writer->data);
|
---|
96 | }
|
---|
97 |
|
---|
98 | static void pcap_file_close(pcap_writer_t *writer)
|
---|
99 | {
|
---|
100 | fclose((FILE *)writer->data);
|
---|
101 | writer->data = NULL;
|
---|
102 | }
|
---|
103 |
|
---|
104 | static size_t pcap_short_file_wbuffer(pcap_writer_t *writer, const void *data, size_t size)
|
---|
105 | {
|
---|
106 | return fwrite(data, 1, size < SHORT_OPS_BYTE_COUNT ? size : SHORT_OPS_BYTE_COUNT, (FILE *)writer->data);
|
---|
107 | }
|
---|
108 |
|
---|
109 | static const pcap_writer_ops_t file_ops = {
|
---|
110 | .open = &pcap_writer_to_file_init,
|
---|
111 | .write_u32 = &pcap_file_w32,
|
---|
112 | .write_u16 = &pcap_file_w16,
|
---|
113 | .write_buffer = &pcap_file_wbuffer,
|
---|
114 | .close = &pcap_file_close
|
---|
115 | };
|
---|
116 |
|
---|
117 | static const pcap_writer_ops_t short_file_ops = {
|
---|
118 | .open = &pcap_writer_to_file_init,
|
---|
119 | .write_u32 = &pcap_file_w32,
|
---|
120 | .write_u16 = &pcap_file_w16,
|
---|
121 | .write_buffer = &pcap_short_file_wbuffer,
|
---|
122 | .close = &pcap_file_close
|
---|
123 |
|
---|
124 | };
|
---|
125 |
|
---|
126 | static const pcap_writer_ops_t append_file_ops = {
|
---|
127 | .open = &pcap_writer_to_file_init_append,
|
---|
128 | .write_u32 = &pcap_file_w32,
|
---|
129 | .write_u16 = &pcap_file_w16,
|
---|
130 | .write_buffer = &pcap_file_wbuffer,
|
---|
131 | .close = &pcap_file_close
|
---|
132 | };
|
---|
133 |
|
---|
134 | static const pcap_writer_ops_t usb_file_ops = {
|
---|
135 | .open = &pcap_writer_to_file_usb_init,
|
---|
136 | .write_u32 = &pcap_file_w32,
|
---|
137 | .write_u16 = &pcap_file_w16,
|
---|
138 | .write_buffer = &pcap_file_wbuffer,
|
---|
139 | .close = &pcap_file_close
|
---|
140 | };
|
---|
141 |
|
---|
142 | static pcap_writer_ops_t ops[4] = {file_ops, short_file_ops, append_file_ops, usb_file_ops};
|
---|
143 |
|
---|
144 | int pcap_dumper_get_ops_number(void)
|
---|
145 | {
|
---|
146 | return (int)(sizeof(ops) / sizeof(pcap_writer_ops_t));
|
---|
147 | }
|
---|
148 |
|
---|
149 | errno_t pcap_dumper_start(pcap_dumper_t *dumper, const char *name)
|
---|
150 | {
|
---|
151 | fibril_mutex_lock(&dumper->mutex);
|
---|
152 |
|
---|
153 | /** When try to start when already started, close current and starts new */
|
---|
154 | if (dumper->to_dump) {
|
---|
155 | pcap_dumper_stop(dumper);
|
---|
156 | }
|
---|
157 |
|
---|
158 | errno_t rc = dumper->writer.ops->open(&dumper->writer, name);
|
---|
159 | if (rc == EOK) {
|
---|
160 | dumper->to_dump = true;
|
---|
161 | }
|
---|
162 |
|
---|
163 | fibril_mutex_unlock(&dumper->mutex);
|
---|
164 | return rc;
|
---|
165 | }
|
---|
166 |
|
---|
167 | errno_t pcap_dumper_set_ops(pcap_dumper_t *dumper, int index)
|
---|
168 | {
|
---|
169 | fibril_mutex_lock(&dumper->mutex);
|
---|
170 | errno_t rc = EOK;
|
---|
171 | dumper->writer.ops = &ops[index];
|
---|
172 | fibril_mutex_unlock(&dumper->mutex);
|
---|
173 | return rc;
|
---|
174 | }
|
---|
175 |
|
---|
176 | void pcap_dumper_add_packet(pcap_dumper_t *dumper, const void *data, size_t size)
|
---|
177 | {
|
---|
178 | fibril_mutex_lock(&dumper->mutex);
|
---|
179 |
|
---|
180 | if (!dumper->to_dump) {
|
---|
181 | fibril_mutex_unlock(&dumper->mutex);
|
---|
182 | return;
|
---|
183 | }
|
---|
184 |
|
---|
185 | pcap_writer_add_packet(&dumper->writer, data, size);
|
---|
186 | fibril_mutex_unlock(&dumper->mutex);
|
---|
187 | }
|
---|
188 |
|
---|
189 | void pcap_dumper_stop(pcap_dumper_t *dumper)
|
---|
190 | {
|
---|
191 | fibril_mutex_lock(&dumper->mutex);
|
---|
192 |
|
---|
193 | /** If want to stop, when already stopped, do nothing */
|
---|
194 | if (!dumper->to_dump) {
|
---|
195 | fibril_mutex_unlock(&dumper->mutex);
|
---|
196 | return;
|
---|
197 | }
|
---|
198 | dumper->to_dump = false;
|
---|
199 | dumper->writer.ops->close(&dumper->writer);
|
---|
200 | fibril_mutex_unlock(&dumper->mutex);
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 |
|
---|
205 | /** @}
|
---|
206 | */
|
---|