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

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

create drv iface for drivers

  • Property mode set to 100644
File size: 3.5 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 rc = pcap_dumper_set_ops(dumper, ops_index);
63 if (rc != EOK)
64 {
65 log_msg(LOG_DEFAULT, LVL_DEBUG, "Setting ops for dumper was not successful.\n");
66 free(data);
67 async_answer_0(icall, EOK);
68 return;
69 }
70
71 rc = pcap_dumper_start(dumper, (const char *)data);
72 free(data);
73 if (rc != EOK) {
74 log_msg(LOG_DEFAULT, LVL_DEBUG, "Starting the dumping was not successful.\n");
75 }
76 async_answer_0(icall, EOK);
77}
78
79static void pcapdump_stop_srv(ipc_call_t *icall, pcap_dumper_t *dumper)
80{
81 pcap_dumper_stop(dumper);
82 async_answer_0(icall, EOK);
83}
84
85
86static void pcapdump_get_ops_num_srv(ipc_call_t *icall)
87{
88 size_t count = pcap_dumper_get_ops_number();
89
90 log_msg(LOG_DEFAULT, LVL_NOTE, "Getting number of ops.\n");
91
92 async_answer_1(icall, EOK, count);
93}
94
95void pcapdump_conn(ipc_call_t *icall, void *arg)
96{
97 pcap_dumper_t *dumper = (pcap_dumper_t *)arg;
98
99 assert((dumper != NULL) && "pcapdump requires pcap dumper\n");
100
101 /* Accept connection */
102 async_accept_0(icall);
103
104 while (true) {
105 ipc_call_t call;
106 async_get_call(&call);
107 sysarg_t method = ipc_get_imethod(&call);
108 if (!method) {
109 /* The other side has hung up */
110 async_answer_0(&call, EOK);
111 break;
112 }
113 switch (method) {
114 case PCAP_CONTROL_SET_START:
115 pcapdump_start_srv(&call, dumper);
116 break;
117 case PCAP_CONTROL_SET_STOP:
118 pcapdump_stop_srv(&call, dumper);
119 break;
120 case PCAP_CONTROL_GET_OPS_NUM:
121 pcapdump_get_ops_num_srv(&call);
122 break;
123 default:
124 async_answer_0(&call, EINVAL);
125 break;
126 }
127 }
128}
129
130
131
132/** @}
133 */
Note: See TracBrowser for help on using the repository browser.