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