source: mainline/uspace/lib/pcap/src/pcap_dumper.c@ 46e2152

Last change on this file since 46e2152 was 46e2152, checked in by Nataliia Korop <n.corop08@…>, 10 months ago

pcapcat: typos, small fixes, pcapctl: comments, docs

  • Property mode set to 100644
File size: 8.2 KB
RevLine 
[17a8fcf]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 */
[87b490e3]32/** @file Pcap dumper. Structure is a part of every device that is in category PCAP and can dump packets.
[17a8fcf]33 */
34
35#include <errno.h>
[1d14090]36#include <str.h>
[467d2b9]37#include <io/log.h>
[1d14090]38#include "pcap_dumper.h"
[17a8fcf]39
[fb31682]40#define SHORT_OPS_BYTE_COUNT 0x3C
[e1e8f7a]41
[64ea525]42/** Initialize writing to .pcap file.
43 *
[87b490e3]44 * @param writer Interface for writing data.
[467d2b9]45 * @param filename Name of the file for dumping packets.
[87b490e3]46 * @return EOK on success, otherwise an error code.
[64ea525]47 *
48 */
49static errno_t pcap_writer_to_file_init(pcap_writer_t *writer, const char *filename)
50{
[fb31682]51 /** For overwriting file if already exists. */
52 writer->data = fopen(filename, "w");
53 if (writer->data == NULL) {
54 return EINVAL;
55 }
56 fclose(writer->data);
57
[64ea525]58 writer->data = fopen(filename, "a");
59 if (writer->data == NULL) {
[e1e8f7a]60 return EINVAL;
[64ea525]61 }
[caac052]62 pcap_writer_add_header(writer, (uint32_t)PCAP_LINKTYPE_ETHERNET);
[64ea525]63
[e1e8f7a]64 return EOK;
65}
66
[87b490e3]67/** Open file for appending to the end of it.
68 * @param writer Interface for writing data.
69 * @param filename Path to the file.
70 * @return EOK on success, error code otherwise.
71 */
[e1e8f7a]72static errno_t pcap_writer_to_file_init_append(pcap_writer_t *writer, const char *filename)
73{
74 writer->data = fopen(filename, "a");
75 if (writer->data == NULL) {
76 return EINVAL;
77 }
78
79 return EOK;
[64ea525]80}
81
[87b490e3]82/** Initialize file for dumping usb packets.
83 * @param writer Interface for writing data.
84 * @param filename Path to the file.
85 * @return EOK on success, error code otherwise.
86 */
[e5b2777]87static errno_t pcap_writer_to_file_usb_init(pcap_writer_t *writer, const char *filename)
88{
[fb31682]89 /** For overwriting file if already exists. */
90 writer->data = fopen(filename, "w");
91 if (writer->data == NULL) {
92 return EINVAL;
93 }
94 fclose(writer->data);
95
[e5b2777]96 writer->data = fopen(filename, "a");
97 if (writer->data == NULL) {
98 return EINVAL;
99 }
[caac052]100 pcap_writer_add_header(writer, (uint32_t)PCAP_LINKTYPE_USB_LINUX_MMAPPED);
[e5b2777]101
102 return EOK;
103}
104
[87b490e3]105/** Write 4B to the file.
106 * @param writer Interface for writing data.
107 * @param data Bytes to write.
108 * @return Size of successfully witten data.
109 */
[17a8fcf]110static size_t pcap_file_w32(pcap_writer_t *writer, uint32_t data)
111{
112 return fwrite(&data, 1, 4, (FILE *)writer->data);
113}
114
[87b490e3]115/** Write 2B to the file.
116 * @param writer Interface for writing data.
117 * @param data Bytes to write.
118 * @return Size of successfully witten data.
119 */
[17a8fcf]120static size_t pcap_file_w16(pcap_writer_t *writer, uint16_t data)
121{
122 return fwrite(&data, 1, 2, (FILE *)writer->data);
123}
124
[87b490e3]125/** Write block of bytes to the file.
126 * @param writer Interface for writing data.
127 * @param data Bytes to write.
128 * @param size Size of block of bytes.
129 * @return Size of successfully witten data.
130 */
[17a8fcf]131static size_t pcap_file_wbuffer(pcap_writer_t *writer, const void *data, size_t size)
132{
[1d14090]133 assert(writer->data);
[17a8fcf]134 return fwrite(data, 1, size, (FILE *)writer->data);
135}
136
[87b490e3]137/** Close file for writing.
138 * @param writer Interaface for writing data.
139 */
[17a8fcf]140static void pcap_file_close(pcap_writer_t *writer)
141{
142 fclose((FILE *)writer->data);
[1d14090]143 writer->data = NULL;
[17a8fcf]144}
145
[87b490e3]146/** Write <= 60B of block of bytes.
147 * @param writer Interface for writing data.
148 * @param data Bytes to write.
149 * @param size Size of block of bytes.
150 * @return Size of successfully witten data.
151 */
[31d2aee]152static size_t pcap_short_file_wbuffer(pcap_writer_t *writer, const void *data, size_t size)
153{
154 return fwrite(data, 1, size < SHORT_OPS_BYTE_COUNT ? size : SHORT_OPS_BYTE_COUNT, (FILE *)writer->data);
155}
156
[87b490e3]157/** Standard writer operations for writing data to a newly created file. */
[e1e8f7a]158static const pcap_writer_ops_t file_ops = {
[64ea525]159 .open = &pcap_writer_to_file_init,
[17a8fcf]160 .write_u32 = &pcap_file_w32,
161 .write_u16 = &pcap_file_w16,
162 .write_buffer = &pcap_file_wbuffer,
163 .close = &pcap_file_close
164};
165
[87b490e3]166/** Truncated writer operations. Only first 60 bytes of the packet are written. */
[e1e8f7a]167static const pcap_writer_ops_t short_file_ops = {
[64ea525]168 .open = &pcap_writer_to_file_init,
[e1e8f7a]169 .write_u32 = &pcap_file_w32,
170 .write_u16 = &pcap_file_w16,
[1d14090]171 .write_buffer = &pcap_short_file_wbuffer,
[e1e8f7a]172 .close = &pcap_file_close
173
174};
[17a8fcf]175
[87b490e3]176/** Append writer operations. Instead of creating new file open existing file and append packets. */
[e1e8f7a]177static const pcap_writer_ops_t append_file_ops = {
178 .open = &pcap_writer_to_file_init_append,
179 .write_u32 = &pcap_file_w32,
180 .write_u16 = &pcap_file_w16,
181 .write_buffer = &pcap_file_wbuffer,
182 .close = &pcap_file_close
[1d14090]183};
[03cd7a9e]184
[87b490e3]185/** USB writer operations. Writing USB packets to the file. */
[e5b2777]186static const pcap_writer_ops_t usb_file_ops = {
187 .open = &pcap_writer_to_file_usb_init,
188 .write_u32 = &pcap_file_w32,
189 .write_u16 = &pcap_file_w16,
190 .write_buffer = &pcap_file_wbuffer,
191 .close = &pcap_file_close
192};
193
[87b490e3]194/** Default array of operations. Must be consistens with constants in /uspace/app/pcapctl/main.c */
[fb31682]195static pcap_writer_ops_t ops[4] = { file_ops, short_file_ops, append_file_ops, usb_file_ops };
[e1e8f7a]196
[87b490e3]197/** Get number of writer operations in @ref ops */
[e1e8f7a]198int pcap_dumper_get_ops_number(void)
199{
200 return (int)(sizeof(ops) / sizeof(pcap_writer_ops_t));
201}
202
[87b490e3]203/** Open destination buffer for writing and set flag for dumping.
204 * @param dumper Structure responsible for dumping packets. Part of the driver.
205 * @param name Name of the destination buffer to dump packets to.
206 * @return EOK if successful, erro code otherwise.
207 */
[e5b2777]208errno_t pcap_dumper_start(pcap_dumper_t *dumper, const char *name)
[17a8fcf]209{
[03cd7a9e]210 fibril_mutex_lock(&dumper->mutex);
211
[64ea525]212 errno_t rc = dumper->writer.ops->open(&dumper->writer, name);
[f08447b]213 if (rc == EOK) {
[03cd7a9e]214 dumper->to_dump = true;
215 }
[467d2b9]216
[03cd7a9e]217 fibril_mutex_unlock(&dumper->mutex);
[17a8fcf]218 return rc;
219}
220
[87b490e3]221/** Set writer options for the writer.
222 * @param dumper Structure responsible for dumping packets. Part of the driver.
223 * @param index Index of the writer operations from array @ref ops.
224 * @return EOK if successful, erro code otherwise.
225 */
[e5b2777]226errno_t pcap_dumper_set_ops(pcap_dumper_t *dumper, int index)
[1d14090]227{
228 fibril_mutex_lock(&dumper->mutex);
[e1e8f7a]229 dumper->writer.ops = &ops[index];
[1d14090]230 fibril_mutex_unlock(&dumper->mutex);
[46e2152]231 return EOK;
[1d14090]232}
233
[87b490e3]234/** Write packet to destination buffer.
235 * @param dumper Structure responsible for dumping packets. Part of the driver.
236 * @param data Packet data to write.
237 * @param size Size of the packet.
238 */
[e5b2777]239void pcap_dumper_add_packet(pcap_dumper_t *dumper, const void *data, size_t size)
[17a8fcf]240{
[03cd7a9e]241 fibril_mutex_lock(&dumper->mutex);
242
[1d14090]243 if (!dumper->to_dump) {
[03cd7a9e]244 fibril_mutex_unlock(&dumper->mutex);
[17a8fcf]245 return;
[03cd7a9e]246 }
[1d14090]247
[03cd7a9e]248 pcap_writer_add_packet(&dumper->writer, data, size);
249 fibril_mutex_unlock(&dumper->mutex);
[17a8fcf]250}
251
[87b490e3]252/** Close destination buffer for writing and unset flag for dumping.
253 * @param dumper Structure responsible for dumping packets. Part of the driver.
254 */
[e5b2777]255void pcap_dumper_stop(pcap_dumper_t *dumper)
[17a8fcf]256{
[03cd7a9e]257 fibril_mutex_lock(&dumper->mutex);
258
259 /** If want to stop, when already stopped, do nothing */
[1d14090]260 if (!dumper->to_dump) {
[03cd7a9e]261 fibril_mutex_unlock(&dumper->mutex);
262 return;
263 }
264 dumper->to_dump = false;
265 dumper->writer.ops->close(&dumper->writer);
266 fibril_mutex_unlock(&dumper->mutex);
[17a8fcf]267}
268
269/** @}
270 */
Note: See TracBrowser for help on using the repository browser.