[42c2e65] | 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 "pcap_iface.h"
|
---|
| 38 |
|
---|
| 39 | static size_t pcap_file_w32(pcap_writer_t *writer, uint32_t data)
|
---|
| 40 | {
|
---|
| 41 | return fwrite(&data, 1, 4, (FILE *)writer->data);
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | static size_t pcap_file_w16(pcap_writer_t *writer, uint16_t data)
|
---|
| 45 | {
|
---|
| 46 | return fwrite(&data, 1, 2, (FILE *)writer->data);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | static size_t pcap_file_wbuffer(pcap_writer_t *writer, const void *data, size_t size)
|
---|
| 50 | {
|
---|
| 51 | return fwrite(data, 1, size, (FILE *)writer->data);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | static void pcap_file_close(pcap_writer_t *writer)
|
---|
| 55 | {
|
---|
| 56 | fclose((FILE *)writer->data);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | static pcap_writer_ops_t file_ops = {
|
---|
| 60 |
|
---|
| 61 | .write_u32 = &pcap_file_w32,
|
---|
| 62 | .write_u16 = &pcap_file_w16,
|
---|
| 63 | .write_buffer = &pcap_file_wbuffer,
|
---|
| 64 | .close = &pcap_file_close
|
---|
| 65 | };
|
---|
| 66 |
|
---|
| 67 | static pcap_writer_t pcap_writer = {
|
---|
| 68 | .ops = &file_ops,
|
---|
| 69 | };
|
---|
| 70 |
|
---|
| 71 | errno_t pcap_init(const char *name)
|
---|
| 72 | {
|
---|
| 73 | errno_t rc = pcap_writer_to_file_init(&pcap_writer, name);
|
---|
| 74 | return rc;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | void pcap_add_packet(const void *data, size_t size)
|
---|
| 78 | {
|
---|
[86f862c] | 79 | if (pcap_writer.data == NULL)
|
---|
[42c2e65] | 80 | return;
|
---|
| 81 | pcap_writer_add_packet(&pcap_writer, data, size);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[6eab537d] | 84 | void pcap_close_file(void)
|
---|
[42c2e65] | 85 | {
|
---|
| 86 | pcap_writer.ops->close(&pcap_writer);
|
---|
| 87 | pcap_writer.data = NULL;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | /** Initialize interface for dumping packets
|
---|
| 91 | *
|
---|
| 92 | * @param iface Device dumping interface
|
---|
| 93 | *
|
---|
| 94 | */
|
---|
| 95 | errno_t pcap_iface_init(pcap_iface_t *iface)
|
---|
| 96 | {
|
---|
| 97 |
|
---|
| 98 | iface->to_dump = false;
|
---|
| 99 | iface->add_packet = pcap_add_packet;
|
---|
| 100 | iface->init = pcap_init;
|
---|
| 101 | iface->fini = pcap_close_file;
|
---|
| 102 |
|
---|
| 103 | return EOK;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /** @}
|
---|
| 107 | */
|
---|