source: mainline/uspace/lib/pcap/src/pcapdump_srv.c@ fb31682

Last change on this file since fb31682 was fb31682, checked in by Nataliia Korop <n.corop08@…>, 10 months ago

user friendly options, trying to start while dumping → err msg

  • Property mode set to 100644
File size: 3.8 KB
Line 
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
49static void pcapdump_start_srv(ipc_call_t *icall, pcap_dumper_t *dumper)
50{
51 char *data;
52 size_t size;
53 int ops_index = (int)ipc_get_arg1(icall);
54 errno_t rc = async_data_write_accept((void **) &data, true, 0, 0, 0, &size);
55 if (rc != EOK) {
56 async_answer_0(icall, rc);
57 return;
58 }
59
60 assert(str_length(data) == size && "Data were damaged during transmission.\n");
61
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
70 rc = pcap_dumper_set_ops(dumper, ops_index);
71 if (rc != EOK) {
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
78 rc = pcap_dumper_start(dumper, (const char *)data);
79 free(data);
80 if (rc != EOK) {
81 log_msg(LOG_DEFAULT, LVL_DEBUG, "Starting the dumping was not successful.\n");
82 }
83 async_answer_0(icall, EOK);
84}
85
86static void pcapdump_stop_srv(ipc_call_t *icall, pcap_dumper_t *dumper)
87{
88 pcap_dumper_stop(dumper);
89 async_answer_0(icall, EOK);
90}
91
92static void pcapdump_get_ops_num_srv(ipc_call_t *icall)
93{
94 size_t count = pcap_dumper_get_ops_number();
95
96 log_msg(LOG_DEFAULT, LVL_NOTE, "Getting number of ops.\n");
97
98 async_answer_1(icall, EOK, count);
99}
100
101void pcapdump_conn(ipc_call_t *icall, void *arg)
102{
103 pcap_dumper_t *dumper = (pcap_dumper_t *)arg;
104
105 assert((dumper != NULL) && "pcapdump requires pcap dumper\n");
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:
121 pcapdump_start_srv(&call, dumper);
122 break;
123 case PCAP_CONTROL_SET_STOP:
124 pcapdump_stop_srv(&call, dumper);
125 break;
126 case PCAP_CONTROL_GET_OPS_NUM:
127 pcapdump_get_ops_num_srv(&call);
128 break;
129 default:
130 async_answer_0(&call, EINVAL);
131 break;
132 }
133 }
134}
135
136/** @}
137 */
Note: See TracBrowser for help on using the repository browser.