| 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 | /**
|
|---|
| 30 | * @addtogroup libpcap
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /**
|
|---|
| 34 | * @file Functions that are called inside driver that can dump packets/
|
|---|
| 35 | *
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <async.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <stdlib.h>
|
|---|
| 41 | #include <fibril_synch.h>
|
|---|
| 42 | #include <str.h>
|
|---|
| 43 | #include <io/log.h>
|
|---|
| 44 |
|
|---|
| 45 | #include "pcapdump_srv.h"
|
|---|
| 46 | #include "pcapdump_drv_iface.h"
|
|---|
| 47 |
|
|---|
| 48 | #define NAME "pcap"
|
|---|
| 49 |
|
|---|
| 50 | /** Initialize interface for dumping packets.
|
|---|
| 51 | * @param dumper Device dumping interface.
|
|---|
| 52 | * @return EOK if successful, error code otherwise.
|
|---|
| 53 | */
|
|---|
| 54 | static errno_t pcapdump_drv_dumper_init(pcap_dumper_t *dumper)
|
|---|
| 55 | {
|
|---|
| 56 | fibril_mutex_initialize(&dumper->mutex);
|
|---|
| 57 | dumper->to_dump = false;
|
|---|
| 58 | dumper->writer.ops = NULL;
|
|---|
| 59 |
|
|---|
| 60 | errno_t rc = log_init(NAME);
|
|---|
| 61 | if (rc != EOK) {
|
|---|
| 62 | printf("%s : Failed to initialize log.\n", NAME);
|
|---|
| 63 | return 1;
|
|---|
| 64 | }
|
|---|
| 65 | return EOK;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | /** Initialize driver dumping functionality.
|
|---|
| 69 | * @param dumper Dumping interface of the driver.
|
|---|
| 70 | * @return EOK if successful, error code otherwise.
|
|---|
| 71 | */
|
|---|
| 72 | errno_t pcapdump_init(pcap_dumper_t *dumper)
|
|---|
| 73 | {
|
|---|
| 74 | port_id_t port;
|
|---|
| 75 | errno_t rc;
|
|---|
| 76 |
|
|---|
| 77 | rc = pcapdump_drv_dumper_init(dumper);
|
|---|
| 78 | if (rc != EOK) {
|
|---|
| 79 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Failed initializing pcap dumper: %s", str_error(rc));
|
|---|
| 80 | return rc;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | rc = async_create_port(INTERFACE_PCAP_CONTROL, pcapdump_conn, dumper, &port);
|
|---|
| 84 | if (rc != EOK) {
|
|---|
| 85 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Failed creating port for pcap dumper: %s", str_error(rc));
|
|---|
| 86 | return rc;
|
|---|
| 87 | }
|
|---|
| 88 | return EOK;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | /** Dumping function for driver.
|
|---|
| 92 | *
|
|---|
| 93 | * Called every time, the packet is sent/recieved by the device.
|
|---|
| 94 | *
|
|---|
| 95 | * @param dumper Dumping interface.
|
|---|
| 96 | * @param data The packet
|
|---|
| 97 | * @param size Size of the packet.
|
|---|
| 98 | *
|
|---|
| 99 | */
|
|---|
| 100 | void pcapdump_packet(pcap_dumper_t *dumper, const void *data, size_t size)
|
|---|
| 101 | {
|
|---|
| 102 | if (dumper == NULL) {
|
|---|
| 103 | return;
|
|---|
| 104 | }
|
|---|
| 105 | pcap_dumper_add_packet(dumper, data, size);
|
|---|
| 106 | }
|
|---|