[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 | */
|
---|
| 49 | static 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] | 72 | static 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] | 87 | static 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] | 110 | static 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] | 120 | static 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] | 131 | static 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] | 140 | static 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] | 152 | static 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] | 158 | static 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] | 167 | static 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] | 177 | static 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] | 186 | static 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] | 195 | static 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] | 198 | int 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] | 208 | errno_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] | 226 | errno_t pcap_dumper_set_ops(pcap_dumper_t *dumper, int index)
|
---|
[1d14090] | 227 | {
|
---|
| 228 | fibril_mutex_lock(&dumper->mutex);
|
---|
| 229 | errno_t rc = EOK;
|
---|
[e1e8f7a] | 230 | dumper->writer.ops = &ops[index];
|
---|
[1d14090] | 231 | fibril_mutex_unlock(&dumper->mutex);
|
---|
| 232 | return rc;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
[87b490e3] | 235 | /** Write packet to destination buffer.
|
---|
| 236 | * @param dumper Structure responsible for dumping packets. Part of the driver.
|
---|
| 237 | * @param data Packet data to write.
|
---|
| 238 | * @param size Size of the packet.
|
---|
| 239 | */
|
---|
[e5b2777] | 240 | void pcap_dumper_add_packet(pcap_dumper_t *dumper, const void *data, size_t size)
|
---|
[17a8fcf] | 241 | {
|
---|
[03cd7a9e] | 242 | fibril_mutex_lock(&dumper->mutex);
|
---|
| 243 |
|
---|
[1d14090] | 244 | if (!dumper->to_dump) {
|
---|
[03cd7a9e] | 245 | fibril_mutex_unlock(&dumper->mutex);
|
---|
[17a8fcf] | 246 | return;
|
---|
[03cd7a9e] | 247 | }
|
---|
[1d14090] | 248 |
|
---|
[03cd7a9e] | 249 | pcap_writer_add_packet(&dumper->writer, data, size);
|
---|
| 250 | fibril_mutex_unlock(&dumper->mutex);
|
---|
[17a8fcf] | 251 | }
|
---|
| 252 |
|
---|
[87b490e3] | 253 | /** Close destination buffer for writing and unset flag for dumping.
|
---|
| 254 | * @param dumper Structure responsible for dumping packets. Part of the driver.
|
---|
| 255 | */
|
---|
[e5b2777] | 256 | void pcap_dumper_stop(pcap_dumper_t *dumper)
|
---|
[17a8fcf] | 257 | {
|
---|
[03cd7a9e] | 258 | fibril_mutex_lock(&dumper->mutex);
|
---|
| 259 |
|
---|
| 260 | /** If want to stop, when already stopped, do nothing */
|
---|
[1d14090] | 261 | if (!dumper->to_dump) {
|
---|
[03cd7a9e] | 262 | fibril_mutex_unlock(&dumper->mutex);
|
---|
| 263 | return;
|
---|
| 264 | }
|
---|
| 265 | dumper->to_dump = false;
|
---|
| 266 | dumper->writer.ops->close(&dumper->writer);
|
---|
| 267 | fibril_mutex_unlock(&dumper->mutex);
|
---|
[17a8fcf] | 268 | }
|
---|
| 269 |
|
---|
| 270 | /** @}
|
---|
| 271 | */
|
---|