source: mainline/uspace/lib/pcap/src/pcapdump_iface.c@ f08447b

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

ccheck

  • Property mode set to 100644
File size: 3.6 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_iface.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
72void pcapdump_conn(ipc_call_t *icall, void *arg)
73{
74 pcap_dumper_t *dumper = (pcap_dumper_t *)arg;
75
76 assert((dumper != NULL) && "pcapdump requires pcap dumper\n");
77
78 /* Accept connection */
79 async_accept_0(icall);
80
81 while (true) {
82 ipc_call_t call;
83 async_get_call(&call);
84 sysarg_t method = ipc_get_imethod(&call);
85 if (!method) {
86 /* The other side has hung up */
87 async_answer_0(&call, EOK);
88 break;
89 }
90 switch (method) {
91 case PCAP_CONTROL_SET_START:
92 pcapdump_start_srv(&call, dumper);
93 break;
94 case PCAP_CONTROL_SET_STOP:
95 pcapdump_stop_srv(&call, dumper);
96 break;
97 default:
98 async_answer_0(&call, EINVAL);
99 break;
100 }
101 }
102}
103
104errno_t pcapdump_init(pcap_dumper_t *dumper)
105{
106 port_id_t port;
107 errno_t rc;
108
109 rc = pcap_dumper_init(dumper);
110
111 if (rc != EOK) {
112 printf("Failed creating pcap interface: %s", str_error(rc));
113 return rc;
114 }
115
116 rc = async_create_port(INTERFACE_PCAP_CONTROL,
117 pcapdump_conn, dumper, &port);
118 if (rc != EOK) {
119 return rc;
120 }
121 return EOK;
122}
123
124/** Dumping function for driver
125 *
126 * Called every time, the packet is sent/recieved by the device
127 *
128 * @param dumper Dumping interface
129 * @param data The packet
130 * @param size Size of the packet
131 *
132 */
133void pcapdump_packet(pcap_dumper_t *dumper, const void *data, size_t size)
134{
135 if (dumper == NULL) {
136 return;
137 }
138 pcap_dumper_add_packet(dumper, data, size);
139}
140
141/** @}
142 */
Note: See TracBrowser for help on using the repository browser.