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

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

renaming and cleaning

  • Property mode set to 100644
File size: 4.1 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
44#include "pcapdump_srv.h"
45
46static void pcapdump_start_srv(ipc_call_t *icall, pcap_dumper_t *dumper)
47{
48 char *data;
49 size_t size;
50 errno_t rc = async_data_write_accept((void **) &data, true, 0, 0, 0, &size);
51 if (rc != EOK) {
52 async_answer_0(icall, rc);
53 return;
54 }
55
56 assert(str_length(data) == size && "Data were damaged during transmission.\n");
57
58 rc = pcap_dumper_start(dumper, (const char *)data);
59 free(data);
60 if (rc != EOK) {
61 //TODO what?
62 }
63 async_answer_0(icall, EOK);
64}
65
66static void pcapdump_stop_srv(ipc_call_t *icall, pcap_dumper_t *dumper)
67{
68 pcap_dumper_stop(dumper);
69 async_answer_0(icall, EOK);
70}
71
72static void pcapdump_set_ops_srv(ipc_call_t *icall, pcap_dumper_t *dumper)
73{
74 char *data;
75 size_t size;
76 errno_t rc = async_data_write_accept((void **) &data, true, 0, 0, 0, &size);
77 if (rc != EOK) {
78 async_answer_0(icall, rc);
79 return;
80 }
81
82 assert(str_length(data) == size && "Data were damaged during transmission.\n");
83
84 rc = pcap_dumper_set_ops(dumper, (const char *)data);
85 free(data);
86 if (rc != EOK) {
87 //TODO what?
88 }
89 async_answer_0(icall, EOK);
90
91}
92
93void pcapdump_conn(ipc_call_t *icall, void *arg)
94{
95 pcap_dumper_t *dumper = (pcap_dumper_t *)arg;
96
97 assert((dumper != NULL) && "pcapdump requires pcap dumper\n");
98
99 /* Accept connection */
100 async_accept_0(icall);
101
102 while (true) {
103 ipc_call_t call;
104 async_get_call(&call);
105 sysarg_t method = ipc_get_imethod(&call);
106 if (!method) {
107 /* The other side has hung up */
108 async_answer_0(&call, EOK);
109 break;
110 }
111 switch (method) {
112 case PCAP_CONTROL_SET_START:
113 pcapdump_start_srv(&call, dumper);
114 break;
115 case PCAP_CONTROL_SET_STOP:
116 pcapdump_stop_srv(&call, dumper);
117 break;
118 case PCAP_CONTROL_SET_OPS:
119 pcapdump_set_ops_srv(&call, dumper);
120 break;
121 default:
122 async_answer_0(&call, EINVAL);
123 break;
124 }
125 }
126}
127
128errno_t pcapdump_init(pcap_dumper_t *dumper)
129{
130 port_id_t port;
131 errno_t rc;
132
133 rc = pcap_dumper_init(dumper);
134
135 if (rc != EOK) {
136 printf("Failed creating pcap interface: %s", str_error(rc));
137 return rc;
138 }
139
140 rc = async_create_port(INTERFACE_PCAP_CONTROL,
141 pcapdump_conn, dumper, &port);
142 if (rc != EOK) {
143 return rc;
144 }
145 return EOK;
146}
147
148/** Dumping function for driver
149 *
150 * Called every time, the packet is sent/recieved by the device
151 *
152 * @param dumper Dumping interface
153 * @param data The packet
154 * @param size Size of the packet
155 *
156 */
157void pcapdump_packet(pcap_dumper_t *dumper, const void *data, size_t size)
158{
159 if (dumper == NULL) {
160 return;
161 }
162 pcap_dumper_add_packet(dumper, data, size);
163}
164
165/** @}
166 */
Note: See TracBrowser for help on using the repository browser.