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