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

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

user friendly options, trying to start while dumping → err msg

  • Property mode set to 100644
File size: 5.8 KB
Line 
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 0x3C
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 */
50static errno_t pcap_writer_to_file_init(pcap_writer_t *writer, const char *filename)
51{
52 /** For overwriting file if already exists. */
53 writer->data = fopen(filename, "w");
54 if (writer->data == NULL) {
55 return EINVAL;
56 }
57 fclose(writer->data);
58
59 writer->data = fopen(filename, "a");
60 if (writer->data == NULL) {
61 return EINVAL;
62 }
63 pcap_writer_add_header(writer, (uint32_t)PCAP_LINKTYPE_ETHERNET, false);
64
65 return EOK;
66}
67
68static errno_t pcap_writer_to_file_init_append(pcap_writer_t *writer, const char *filename)
69{
70 writer->data = fopen(filename, "a");
71 if (writer->data == NULL) {
72 return EINVAL;
73 }
74
75 return EOK;
76}
77
78static errno_t pcap_writer_to_file_usb_init(pcap_writer_t *writer, const char *filename)
79{
80 /** For overwriting file if already exists. */
81 writer->data = fopen(filename, "w");
82 if (writer->data == NULL) {
83 return EINVAL;
84 }
85 fclose(writer->data);
86
87 writer->data = fopen(filename, "a");
88 if (writer->data == NULL) {
89 return EINVAL;
90 }
91 pcap_writer_add_header(writer, (uint32_t)PCAP_LINKTYPE_USB_LINUX_MMAPPED, false);
92
93 return EOK;
94}
95
96static size_t pcap_file_w32(pcap_writer_t *writer, uint32_t data)
97{
98 return fwrite(&data, 1, 4, (FILE *)writer->data);
99}
100
101static size_t pcap_file_w16(pcap_writer_t *writer, uint16_t data)
102{
103 return fwrite(&data, 1, 2, (FILE *)writer->data);
104}
105
106static size_t pcap_file_wbuffer(pcap_writer_t *writer, const void *data, size_t size)
107{
108 assert(writer->data);
109 return fwrite(data, 1, size, (FILE *)writer->data);
110}
111
112static void pcap_file_close(pcap_writer_t *writer)
113{
114 fclose((FILE *)writer->data);
115 writer->data = NULL;
116}
117
118static size_t pcap_short_file_wbuffer(pcap_writer_t *writer, const void *data, size_t size)
119{
120 return fwrite(data, 1, size < SHORT_OPS_BYTE_COUNT ? size : SHORT_OPS_BYTE_COUNT, (FILE *)writer->data);
121}
122
123static const pcap_writer_ops_t file_ops = {
124 .open = &pcap_writer_to_file_init,
125 .write_u32 = &pcap_file_w32,
126 .write_u16 = &pcap_file_w16,
127 .write_buffer = &pcap_file_wbuffer,
128 .close = &pcap_file_close
129};
130
131static const pcap_writer_ops_t short_file_ops = {
132 .open = &pcap_writer_to_file_init,
133 .write_u32 = &pcap_file_w32,
134 .write_u16 = &pcap_file_w16,
135 .write_buffer = &pcap_short_file_wbuffer,
136 .close = &pcap_file_close
137
138};
139
140static const pcap_writer_ops_t append_file_ops = {
141 .open = &pcap_writer_to_file_init_append,
142 .write_u32 = &pcap_file_w32,
143 .write_u16 = &pcap_file_w16,
144 .write_buffer = &pcap_file_wbuffer,
145 .close = &pcap_file_close
146};
147
148static const pcap_writer_ops_t usb_file_ops = {
149 .open = &pcap_writer_to_file_usb_init,
150 .write_u32 = &pcap_file_w32,
151 .write_u16 = &pcap_file_w16,
152 .write_buffer = &pcap_file_wbuffer,
153 .close = &pcap_file_close
154};
155
156static pcap_writer_ops_t ops[4] = { file_ops, short_file_ops, append_file_ops, usb_file_ops };
157
158int pcap_dumper_get_ops_number(void)
159{
160 return (int)(sizeof(ops) / sizeof(pcap_writer_ops_t));
161}
162
163errno_t pcap_dumper_start(pcap_dumper_t *dumper, const char *name)
164{
165 fibril_mutex_lock(&dumper->mutex);
166
167 errno_t rc = dumper->writer.ops->open(&dumper->writer, name);
168 if (rc == EOK) {
169 dumper->to_dump = true;
170 }
171
172 fibril_mutex_unlock(&dumper->mutex);
173 return rc;
174}
175
176errno_t pcap_dumper_set_ops(pcap_dumper_t *dumper, int index)
177{
178 fibril_mutex_lock(&dumper->mutex);
179 errno_t rc = EOK;
180 dumper->writer.ops = &ops[index];
181 fibril_mutex_unlock(&dumper->mutex);
182 return rc;
183}
184
185void pcap_dumper_add_packet(pcap_dumper_t *dumper, const void *data, size_t size)
186{
187 fibril_mutex_lock(&dumper->mutex);
188
189 if (!dumper->to_dump) {
190 fibril_mutex_unlock(&dumper->mutex);
191 return;
192 }
193
194 pcap_writer_add_packet(&dumper->writer, data, size);
195 fibril_mutex_unlock(&dumper->mutex);
196}
197
198void pcap_dumper_stop(pcap_dumper_t *dumper)
199{
200 fibril_mutex_lock(&dumper->mutex);
201
202 /** If want to stop, when already stopped, do nothing */
203 if (!dumper->to_dump) {
204 fibril_mutex_unlock(&dumper->mutex);
205 return;
206 }
207 dumper->to_dump = false;
208 dumper->writer.ops->close(&dumper->writer);
209 fibril_mutex_unlock(&dumper->mutex);
210}
211
212/** @}
213 */
Note: See TracBrowser for help on using the repository browser.