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

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

dumper ops can be set by user

  • Property mode set to 100644
File size: 4.7 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 "pcap_dumper.h"
39
40static size_t pcap_file_w32(pcap_writer_t *writer, uint32_t data)
41{
42 return fwrite(&data, 1, 4, (FILE *)writer->data);
43}
44
45static size_t pcap_file_w16(pcap_writer_t *writer, uint16_t data)
46{
47 return fwrite(&data, 1, 2, (FILE *)writer->data);
48}
49
50static size_t pcap_file_wbuffer(pcap_writer_t *writer, const void *data, size_t size)
51{
52 assert(writer->data);
53 return fwrite(data, 1, size, (FILE *)writer->data);
54}
55
56static void pcap_file_close(pcap_writer_t *writer)
57{
58 fclose((FILE *)writer->data);
59 writer->data = NULL;
60}
61
62static pcap_writer_ops_t file_ops = {
63
64 .write_u32 = &pcap_file_w32,
65 .write_u16 = &pcap_file_w16,
66 .write_buffer = &pcap_file_wbuffer,
67 .close = &pcap_file_close
68};
69
70static size_t pcap_short_file_w32(pcap_writer_t *writer, uint32_t data)
71{
72 return fwrite(&data, 1, 4, (FILE *)writer->data);
73}
74
75static size_t pcap_short_file_w16(pcap_writer_t *writer, uint16_t data)
76{
77 return fwrite(&data, 1, 2, (FILE *)writer->data);
78}
79
80static size_t pcap_short_file_wbuffer(pcap_writer_t *writer, const void *data, size_t size)
81{
82 return fwrite(data, 1, size<60?size:60, (FILE *)writer->data);
83}
84
85static void pcap_short_file_close(pcap_writer_t *writer)
86{
87 fclose((FILE *)writer->data);
88}
89
90static pcap_writer_ops_t short_file_ops = {
91 .write_u32 = &pcap_short_file_w32,
92 .write_u16 = &pcap_short_file_w16,
93 .write_buffer = &pcap_short_file_wbuffer,
94 .close = &pcap_short_file_close
95
96};
97
98errno_t pcap_dumper_start(struct pcap_dumper *dumper, const char *name)
99{
100 fibril_mutex_lock(&dumper->mutex);
101
102 /** When try to start when already started, close current and starts new */
103 if (dumper->to_dump) {
104 pcap_dumper_stop(dumper);
105 }
106 errno_t rc = pcap_writer_to_file_init(&dumper->writer, name);
107 if (rc == EOK) {
108 dumper->to_dump = true;
109 } else {
110 printf("Failed creating pcap dumper: %s", str_error(rc));
111 }
112 fibril_mutex_unlock(&dumper->mutex);
113 return rc;
114}
115
116errno_t pcap_dumper_set_ops(struct pcap_dumper *dumper, const char *name)
117{
118 fibril_mutex_lock(&dumper->mutex);
119 errno_t rc = EOK;
120 if (!str_cmp(name, "short_file"))
121 {
122 dumper->writer.ops = &short_file_ops;
123 }
124 else if (!str_cmp(name, "full_file"))
125 {
126 dumper->writer.ops = &file_ops;
127 }
128 else
129 {
130 rc = EINVAL;
131 }
132 fibril_mutex_unlock(&dumper->mutex);
133 return rc;
134}
135
136
137void pcap_dumper_add_packet(struct pcap_dumper *dumper, const void *data, size_t size)
138{
139 fibril_mutex_lock(&dumper->mutex);
140
141 if (!dumper->to_dump) {
142 fibril_mutex_unlock(&dumper->mutex);
143 return;
144 }
145
146 pcap_writer_add_packet(&dumper->writer, data, size);
147 fibril_mutex_unlock(&dumper->mutex);
148}
149
150void pcap_dumper_stop(struct pcap_dumper *dumper)
151{
152 fibril_mutex_lock(&dumper->mutex);
153
154 /** If want to stop, when already stopped, do nothing */
155 if (!dumper->to_dump) {
156 fibril_mutex_unlock(&dumper->mutex);
157 return;
158 }
159 dumper->to_dump = false;
160 dumper->writer.ops->close(&dumper->writer);
161 fibril_mutex_unlock(&dumper->mutex);
162}
163
164/** Initialize interface for dumping packets
165 *
166 * @param dumper Device dumping interface
167 *
168 */
169errno_t pcap_dumper_init(pcap_dumper_t *dumper)
170{
171 fibril_mutex_initialize(&dumper->mutex);
172 dumper->to_dump = false;
173 dumper->writer.ops = NULL;
174 return EOK;
175}
176
177/** @}
178 */
Note: See TracBrowser for help on using the repository browser.