[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 | /**
|
---|
| 30 | * @addtogroup libpcap
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | * @brief Server side of the pcapctl
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <async.h>
|
---|
| 39 | #include <errno.h>
|
---|
| 40 | #include <fibril_synch.h>
|
---|
| 41 |
|
---|
| 42 | #include "pcapdump_iface.h"
|
---|
| 43 |
|
---|
| 44 | FIBRIL_MUTEX_INITIALIZE(to_dump_mutex);
|
---|
| 45 |
|
---|
| 46 | static void pcapdump_start_srv(ipc_call_t *icall, pcap_iface_t *iface)
|
---|
| 47 | {
|
---|
| 48 | char *data;
|
---|
| 49 | size_t size;
|
---|
| 50 | errno_t rc = async_data_write_accept((void **) &data, false, 0, 0, 0, &size);
|
---|
| 51 | if (rc != EOK) {
|
---|
| 52 | async_answer_0(icall, rc);
|
---|
| 53 | return;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | /** When try to start when already started, close current and starts new */
|
---|
| 57 | if (iface->to_dump == true) {
|
---|
| 58 | iface->fini();
|
---|
| 59 | }
|
---|
| 60 | iface->init((const char *)data);
|
---|
| 61 |
|
---|
| 62 | fibril_mutex_lock(&to_dump_mutex);
|
---|
| 63 | iface->to_dump = true;
|
---|
| 64 | fibril_mutex_unlock(&to_dump_mutex);
|
---|
| 65 |
|
---|
| 66 | async_answer_0(icall, rc);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | static void pcapdump_stop_srv(ipc_call_t *icall, pcap_iface_t *iface)
|
---|
| 70 | {
|
---|
| 71 | /** If want to stop, when already stopped, do nothing */
|
---|
| 72 | if (iface->to_dump == false) {
|
---|
| 73 | async_answer_0(icall, EOK);
|
---|
| 74 | return;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | fibril_mutex_lock(&to_dump_mutex);
|
---|
| 78 | iface->to_dump = false;
|
---|
| 79 | fibril_mutex_unlock(&to_dump_mutex);
|
---|
| 80 |
|
---|
| 81 | iface->fini();
|
---|
| 82 | async_answer_0(icall, EOK);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | static void pcapdump_conn(ipc_call_t *icall, void *arg)
|
---|
| 86 | {
|
---|
| 87 | pcap_iface_t *iface = (pcap_iface_t *)arg;
|
---|
| 88 |
|
---|
| 89 | assert((iface != NULL) && "pcapdump requires pcap interface\n");
|
---|
| 90 |
|
---|
| 91 | /* Accept connection */
|
---|
| 92 | async_accept_0(icall);
|
---|
| 93 |
|
---|
| 94 | while (true) {
|
---|
| 95 | ipc_call_t call;
|
---|
| 96 | async_get_call(&call);
|
---|
| 97 | sysarg_t method = ipc_get_imethod(&call);
|
---|
| 98 | if (!method) {
|
---|
| 99 | /* The other side has hung up */
|
---|
| 100 | async_answer_0(&call, EOK);
|
---|
| 101 | break;
|
---|
| 102 | }
|
---|
| 103 | switch (method) {
|
---|
| 104 | case PCAP_CONTROL_SET_START:
|
---|
| 105 | pcapdump_start_srv(&call, iface);
|
---|
| 106 | break;
|
---|
| 107 | case PCAP_CONTROL_SET_STOP:
|
---|
| 108 | pcapdump_stop_srv(&call, iface);
|
---|
| 109 | break;
|
---|
| 110 | default:
|
---|
| 111 | async_answer_0(&call, EINVAL);
|
---|
| 112 | break;
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | errno_t pcapdump_init(pcap_iface_t *iface)
|
---|
| 118 | {
|
---|
| 119 | port_id_t port;
|
---|
| 120 | errno_t rc;
|
---|
| 121 |
|
---|
| 122 | rc = pcap_iface_init(iface);
|
---|
| 123 |
|
---|
| 124 | if (rc != EOK) {
|
---|
| 125 | printf("Failed creating pcap interface: %s", str_error(rc));
|
---|
| 126 | return rc;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | rc = async_create_port(INTERFACE_PCAP_CONTROL,
|
---|
| 130 | pcapdump_conn, iface, &port);
|
---|
| 131 | if (rc != EOK) {
|
---|
| 132 | return rc;
|
---|
| 133 | }
|
---|
| 134 | return EOK;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /** Dumping function for driver
|
---|
| 138 | *
|
---|
| 139 | * Called every time, the packet is sent/recieved by the device
|
---|
| 140 | *
|
---|
| 141 | * @param iface Dumping interface
|
---|
| 142 | * @param data The packet
|
---|
| 143 | * @param size Size of the packet
|
---|
| 144 | *
|
---|
| 145 | */
|
---|
| 146 | void pcapdump_packet(pcap_iface_t *iface, const void *data, size_t size)
|
---|
| 147 | {
|
---|
| 148 |
|
---|
| 149 | if (iface == NULL) {
|
---|
| 150 | return;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | if (!iface->to_dump) {
|
---|
| 154 | return;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | iface->add_packet(data, size);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | /** @}
|
---|
| 161 | */
|
---|