| 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 Pcap dumper. Structure is a part of every device that is in category PCAP and can dump packets.
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <errno.h>
|
|---|
| 36 | #include <str.h>
|
|---|
| 37 | #include <io/log.h>
|
|---|
| 38 | #include "pcap_dumper.h"
|
|---|
| 39 |
|
|---|
| 40 | #define SHORT_OPS_BYTE_COUNT 0x3C
|
|---|
| 41 |
|
|---|
| 42 | /** Initialize writing to .pcap file.
|
|---|
| 43 | *
|
|---|
| 44 | * @param writer Interface for writing data.
|
|---|
| 45 | * @param filename Name of the file for dumping packets.
|
|---|
| 46 | * @return EOK on success, otherwise an error code.
|
|---|
| 47 | *
|
|---|
| 48 | */
|
|---|
| 49 | static errno_t pcap_writer_to_file_init(pcap_writer_t *writer, const char *filename)
|
|---|
| 50 | {
|
|---|
| 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 |
|
|---|
| 58 | writer->data = fopen(filename, "a");
|
|---|
| 59 | if (writer->data == NULL) {
|
|---|
| 60 | return EINVAL;
|
|---|
| 61 | }
|
|---|
| 62 | pcap_writer_add_header(writer, (uint32_t)PCAP_LINKTYPE_ETHERNET);
|
|---|
| 63 |
|
|---|
| 64 | return EOK;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 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 | */
|
|---|
| 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;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 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 | */
|
|---|
| 87 | static errno_t pcap_writer_to_file_usb_init(pcap_writer_t *writer, const char *filename)
|
|---|
| 88 | {
|
|---|
| 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 |
|
|---|
| 96 | writer->data = fopen(filename, "a");
|
|---|
| 97 | if (writer->data == NULL) {
|
|---|
| 98 | return EINVAL;
|
|---|
| 99 | }
|
|---|
| 100 | pcap_writer_add_header(writer, (uint32_t)PCAP_LINKTYPE_USB_LINUX_MMAPPED);
|
|---|
| 101 |
|
|---|
| 102 | return EOK;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 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 | */
|
|---|
| 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 |
|
|---|
| 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 | */
|
|---|
| 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 |
|
|---|
| 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 | */
|
|---|
| 131 | static size_t pcap_file_wbuffer(pcap_writer_t *writer, const void *data, size_t size)
|
|---|
| 132 | {
|
|---|
| 133 | assert(writer->data);
|
|---|
| 134 | return fwrite(data, 1, size, (FILE *)writer->data);
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | /** Close file for writing.
|
|---|
| 138 | * @param writer Interaface for writing data.
|
|---|
| 139 | */
|
|---|
| 140 | static void pcap_file_close(pcap_writer_t *writer)
|
|---|
| 141 | {
|
|---|
| 142 | fclose((FILE *)writer->data);
|
|---|
| 143 | writer->data = NULL;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 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 | */
|
|---|
| 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 |
|
|---|
| 157 | /** Standard writer operations for writing data to a newly created file. */
|
|---|
| 158 | static const pcap_writer_ops_t file_ops = {
|
|---|
| 159 | .open = &pcap_writer_to_file_init,
|
|---|
| 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 |
|
|---|
| 166 | /** Truncated writer operations. Only first 60 bytes of the packet are written. */
|
|---|
| 167 | static const pcap_writer_ops_t short_file_ops = {
|
|---|
| 168 | .open = &pcap_writer_to_file_init,
|
|---|
| 169 | .write_u32 = &pcap_file_w32,
|
|---|
| 170 | .write_u16 = &pcap_file_w16,
|
|---|
| 171 | .write_buffer = &pcap_short_file_wbuffer,
|
|---|
| 172 | .close = &pcap_file_close
|
|---|
| 173 |
|
|---|
| 174 | };
|
|---|
| 175 |
|
|---|
| 176 | /** Append writer operations. Instead of creating new file open existing file and append packets. */
|
|---|
| 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
|
|---|
| 183 | };
|
|---|
| 184 |
|
|---|
| 185 | /** USB writer operations. Writing USB packets to the file. */
|
|---|
| 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 |
|
|---|
| 194 | /** Default array of operations. Must be consistens with constants in /uspace/app/pcapctl/main.c */
|
|---|
| 195 | static pcap_writer_ops_t ops[4] = { file_ops, short_file_ops, append_file_ops, usb_file_ops };
|
|---|
| 196 |
|
|---|
| 197 | /** Get number of writer operations in @ref ops */
|
|---|
| 198 | int pcap_dumper_get_ops_number(void)
|
|---|
| 199 | {
|
|---|
| 200 | return (int)(sizeof(ops) / sizeof(pcap_writer_ops_t));
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 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 | */
|
|---|
| 208 | errno_t pcap_dumper_start(pcap_dumper_t *dumper, const char *name)
|
|---|
| 209 | {
|
|---|
| 210 | fibril_mutex_lock(&dumper->mutex);
|
|---|
| 211 |
|
|---|
| 212 | errno_t rc = dumper->writer.ops->open(&dumper->writer, name);
|
|---|
| 213 | if (rc == EOK) {
|
|---|
| 214 | dumper->to_dump = true;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | fibril_mutex_unlock(&dumper->mutex);
|
|---|
| 218 | return rc;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 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 | */
|
|---|
| 226 | errno_t pcap_dumper_set_ops(pcap_dumper_t *dumper, int index)
|
|---|
| 227 | {
|
|---|
| 228 | fibril_mutex_lock(&dumper->mutex);
|
|---|
| 229 | dumper->writer.ops = &ops[index];
|
|---|
| 230 | fibril_mutex_unlock(&dumper->mutex);
|
|---|
| 231 | return EOK;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 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 | */
|
|---|
| 239 | void pcap_dumper_add_packet(pcap_dumper_t *dumper, const void *data, size_t size)
|
|---|
| 240 | {
|
|---|
| 241 | fibril_mutex_lock(&dumper->mutex);
|
|---|
| 242 |
|
|---|
| 243 | if (!dumper->to_dump) {
|
|---|
| 244 | fibril_mutex_unlock(&dumper->mutex);
|
|---|
| 245 | return;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | pcap_writer_add_packet(&dumper->writer, data, size);
|
|---|
| 249 | fibril_mutex_unlock(&dumper->mutex);
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | /** Close destination buffer for writing and unset flag for dumping.
|
|---|
| 253 | * @param dumper Structure responsible for dumping packets. Part of the driver.
|
|---|
| 254 | */
|
|---|
| 255 | void pcap_dumper_stop(pcap_dumper_t *dumper)
|
|---|
| 256 | {
|
|---|
| 257 | fibril_mutex_lock(&dumper->mutex);
|
|---|
| 258 |
|
|---|
| 259 | /** If want to stop, when already stopped, do nothing */
|
|---|
| 260 | if (!dumper->to_dump) {
|
|---|
| 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);
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | /** @}
|
|---|
| 270 | */
|
|---|