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