| 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 <stdlib.h>
|
|---|
| 41 | #include <fibril_synch.h>
|
|---|
| 42 | #include <str.h>
|
|---|
| 43 | #include <io/log.h>
|
|---|
| 44 |
|
|---|
| 45 | #include "pcap_dumper.h"
|
|---|
| 46 | #include "pcapdump_srv.h"
|
|---|
| 47 | #include "pcapdump_ipc.h"
|
|---|
| 48 |
|
|---|
| 49 | /** Start dumping.
|
|---|
| 50 | * @param icall IPC call with request to start.
|
|---|
| 51 | * @param dumper Dumping interface of the driver.
|
|---|
| 52 | */
|
|---|
| 53 | static void pcapdump_start_srv(ipc_call_t *icall, pcap_dumper_t *dumper)
|
|---|
| 54 | {
|
|---|
| 55 | char *data;
|
|---|
| 56 | size_t size;
|
|---|
| 57 | int ops_index = (int)ipc_get_arg1(icall);
|
|---|
| 58 | errno_t rc = async_data_write_accept((void **) &data, true, 0, 0, 0, &size);
|
|---|
| 59 | if (rc != EOK) {
|
|---|
| 60 | async_answer_0(icall, rc);
|
|---|
| 61 | return;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | assert(str_length(data) == size && "Data were damaged during transmission.\n");
|
|---|
| 65 |
|
|---|
| 66 | // Deadlock solution when trying to start dump while dumping (to the same device)
|
|---|
| 67 | if (dumper->to_dump) {
|
|---|
| 68 | free(data);
|
|---|
| 69 | log_msg(LOG_DEFAULT, LVL_ERROR, "Trying to start dumping while dumping.\n");
|
|---|
| 70 | async_answer_0(icall, EBUSY);
|
|---|
| 71 | return;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | rc = pcap_dumper_set_ops(dumper, ops_index);
|
|---|
| 75 | if (rc != EOK) {
|
|---|
| 76 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Setting ops for dumper was not successful.\n");
|
|---|
| 77 | free(data);
|
|---|
| 78 | async_answer_0(icall, EOK);
|
|---|
| 79 | return;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | rc = pcap_dumper_start(dumper, (const char *)data);
|
|---|
| 83 | free(data);
|
|---|
| 84 | if (rc != EOK) {
|
|---|
| 85 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Starting the dumping was not successful.\n");
|
|---|
| 86 | }
|
|---|
| 87 | async_answer_0(icall, EOK);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | /** Stop dumping
|
|---|
| 91 | * @param icall IPC call with request to stop.
|
|---|
| 92 | * @param dumper Dumping interface of the driver.
|
|---|
| 93 | */
|
|---|
| 94 | static void pcapdump_stop_srv(ipc_call_t *icall, pcap_dumper_t *dumper)
|
|---|
| 95 | {
|
|---|
| 96 | pcap_dumper_stop(dumper);
|
|---|
| 97 | async_answer_0(icall, EOK);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /** Get number of accessibke writer operations.
|
|---|
| 101 | * @param icall IPC call with request to get number of accessible writer operations.
|
|---|
| 102 | */
|
|---|
| 103 | static void pcapdump_get_ops_num_srv(ipc_call_t *icall)
|
|---|
| 104 | {
|
|---|
| 105 | size_t count = pcap_dumper_get_ops_number();
|
|---|
| 106 |
|
|---|
| 107 | log_msg(LOG_DEFAULT, LVL_NOTE, "Getting number of ops.\n");
|
|---|
| 108 |
|
|---|
| 109 | async_answer_1(icall, EOK, count);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /** Callback connection function. Accepts requests and processes them.
|
|---|
| 113 | * @param icall IPC call with request.
|
|---|
| 114 | * @param arg Dumping interface of the driver.
|
|---|
| 115 | */
|
|---|
| 116 | void pcapdump_conn(ipc_call_t *icall, void *arg)
|
|---|
| 117 | {
|
|---|
| 118 | pcap_dumper_t *dumper = (pcap_dumper_t *)arg;
|
|---|
| 119 |
|
|---|
| 120 | assert((dumper != NULL) && "pcapdump requires pcap dumper\n");
|
|---|
| 121 |
|
|---|
| 122 | /* Accept connection */
|
|---|
| 123 | async_accept_0(icall);
|
|---|
| 124 |
|
|---|
| 125 | while (true) {
|
|---|
| 126 | ipc_call_t call;
|
|---|
| 127 | async_get_call(&call);
|
|---|
| 128 | sysarg_t method = ipc_get_imethod(&call);
|
|---|
| 129 | if (!method) {
|
|---|
| 130 | /* The other side has hung up */
|
|---|
| 131 | async_answer_0(&call, EOK);
|
|---|
| 132 | break;
|
|---|
| 133 | }
|
|---|
| 134 | switch (method) {
|
|---|
| 135 | case PCAP_CONTROL_SET_START:
|
|---|
| 136 | pcapdump_start_srv(&call, dumper);
|
|---|
| 137 | break;
|
|---|
| 138 | case PCAP_CONTROL_SET_STOP:
|
|---|
| 139 | pcapdump_stop_srv(&call, dumper);
|
|---|
| 140 | break;
|
|---|
| 141 | case PCAP_CONTROL_GET_OPS_NUM:
|
|---|
| 142 | pcapdump_get_ops_num_srv(&call);
|
|---|
| 143 | break;
|
|---|
| 144 | default:
|
|---|
| 145 | async_answer_0(&call, EINVAL);
|
|---|
| 146 | break;
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /** @}
|
|---|
| 152 | */
|
|---|