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 | /** @addtogroup libpcap
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief Client side of the pcapctl. Functions are called from the app pcapctl
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <errno.h>
|
---|
37 | #include <async.h>
|
---|
38 | #include <str.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include <stdio.h>
|
---|
41 | #include <ctype.h>
|
---|
42 | #include "pcapctl_dump.h"
|
---|
43 | #include "pcapdump_iface.h"
|
---|
44 |
|
---|
45 | /** Finish an async exchange on the pcapctl session
|
---|
46 | *
|
---|
47 | * @param exch Exchange to be finished
|
---|
48 | */
|
---|
49 | static void pcapctl_dump_exchange_end(async_exch_t *exch)
|
---|
50 | {
|
---|
51 | async_exchange_end(exch);
|
---|
52 | }
|
---|
53 |
|
---|
54 | static errno_t pcapctl_cat_get_svc(int *index, service_id_t *svc)
|
---|
55 | {
|
---|
56 | errno_t rc;
|
---|
57 | category_id_t pcap_cat;
|
---|
58 | size_t count;
|
---|
59 | service_id_t *pcap_svcs = NULL;
|
---|
60 |
|
---|
61 | rc = loc_category_get_id("pcap", &pcap_cat, 0);
|
---|
62 | if (rc != EOK) {
|
---|
63 | printf("Error resolving category 'pcap'.\n");
|
---|
64 | return rc;
|
---|
65 | }
|
---|
66 |
|
---|
67 | rc = loc_category_get_svcs(pcap_cat, &pcap_svcs, &count);
|
---|
68 | if (rc != EOK) {
|
---|
69 | printf("Error resolving list of pcap services.\n");
|
---|
70 | free(pcap_svcs);
|
---|
71 | return rc;
|
---|
72 | }
|
---|
73 | if (*index < (int)count) {
|
---|
74 | *svc = pcap_svcs[*index];
|
---|
75 | free(pcap_svcs);
|
---|
76 | return EOK;
|
---|
77 | }
|
---|
78 |
|
---|
79 | return ENOENT;
|
---|
80 | }
|
---|
81 |
|
---|
82 | errno_t pcapctl_is_valid_device(int *index)
|
---|
83 | {
|
---|
84 | errno_t rc;
|
---|
85 | category_id_t pcap_cat;
|
---|
86 | size_t count;
|
---|
87 | service_id_t *pcap_svcs = NULL;
|
---|
88 |
|
---|
89 | rc = loc_category_get_id("pcap", &pcap_cat, 0);
|
---|
90 | if (rc != EOK) {
|
---|
91 | printf("Error resolving category pcap.\n");
|
---|
92 | return rc;
|
---|
93 | }
|
---|
94 |
|
---|
95 | rc = loc_category_get_svcs(pcap_cat, &pcap_svcs, &count);
|
---|
96 | if (rc != EOK) {
|
---|
97 | printf("Error resolving list of pcap services.\n");
|
---|
98 | free(pcap_svcs);
|
---|
99 | return rc;
|
---|
100 | }
|
---|
101 | if (*index + 1 > (int)count || *index < 0) {
|
---|
102 | return EINVAL;
|
---|
103 | }
|
---|
104 | return EOK;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | *
|
---|
109 | */
|
---|
110 | errno_t pcapctl_list(void)
|
---|
111 | {
|
---|
112 | errno_t rc;
|
---|
113 | category_id_t pcap_cat;
|
---|
114 | size_t count;
|
---|
115 | service_id_t *pcap_svcs = NULL;
|
---|
116 |
|
---|
117 | rc = loc_category_get_id("pcap", &pcap_cat, 0);
|
---|
118 | if (rc != EOK) {
|
---|
119 | printf("Error resolving category pcap.\n");
|
---|
120 | return rc;
|
---|
121 | }
|
---|
122 |
|
---|
123 | rc = loc_category_get_svcs(pcap_cat, &pcap_svcs, &count);
|
---|
124 | if (rc != EOK) {
|
---|
125 | printf("Error resolving list of pcap services.\n");
|
---|
126 | free(pcap_svcs);
|
---|
127 | return rc;
|
---|
128 | }
|
---|
129 |
|
---|
130 | fprintf(stdout, "Devices:\n");
|
---|
131 | for (unsigned i = 0; i < count; ++i) {
|
---|
132 | char *name = NULL;
|
---|
133 | loc_service_get_name(pcap_svcs[i], &name);
|
---|
134 | fprintf(stdout, "%d. %s\n", i, name);
|
---|
135 | }
|
---|
136 | free(pcap_svcs);
|
---|
137 | return EOK;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | *
|
---|
142 | */
|
---|
143 | errno_t pcapctl_dump_open(int *index, pcapctl_sess_t **rsess)
|
---|
144 | {
|
---|
145 | errno_t rc;
|
---|
146 | service_id_t svc;
|
---|
147 | pcapctl_sess_t *sess = calloc(1, sizeof(pcapctl_sess_t));
|
---|
148 | if (sess == NULL)
|
---|
149 | return ENOMEM;
|
---|
150 |
|
---|
151 | printf("number: %d\n", *index);
|
---|
152 | if (*index == -1) {
|
---|
153 | *index = 0;
|
---|
154 | }
|
---|
155 |
|
---|
156 | rc = pcapctl_cat_get_svc(index, &svc);
|
---|
157 | if (rc != EOK) {
|
---|
158 | printf("Error finding the device with index: %d\n", *index);
|
---|
159 | goto error;
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | async_sess_t *new_session = loc_service_connect(svc, INTERFACE_PCAP_CONTROL, 0);
|
---|
164 | if (new_session == NULL) {
|
---|
165 | fprintf(stderr, "Error connecting to service.\n");
|
---|
166 | rc = EREFUSED;
|
---|
167 | goto error;
|
---|
168 | }
|
---|
169 |
|
---|
170 | sess->sess = new_session;
|
---|
171 | *rsess = sess;
|
---|
172 | return EOK;
|
---|
173 | error:
|
---|
174 | pcapctl_dump_close(sess);
|
---|
175 | return rc;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | *
|
---|
180 | */
|
---|
181 | errno_t pcapctl_dump_close(pcapctl_sess_t *sess)
|
---|
182 | {
|
---|
183 | free(sess);
|
---|
184 | return EOK;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /** Starting a new session for pcapctl
|
---|
188 | *
|
---|
189 | * @param name Name of the file to dump packets to
|
---|
190 | * @param sess session to start
|
---|
191 | * @return EOK on success or an error code
|
---|
192 | */
|
---|
193 | errno_t pcapctl_dump_start(const char *name, pcapctl_sess_t *sess)
|
---|
194 | {
|
---|
195 | errno_t rc;
|
---|
196 | async_exch_t *exch = async_exchange_begin(sess->sess);
|
---|
197 |
|
---|
198 | size_t size = str_size(name);
|
---|
199 | aid_t req = async_send_0(exch, PCAP_CONTROL_SET_START, NULL);
|
---|
200 |
|
---|
201 | rc = async_data_write_start(exch, (const void *)name, size);
|
---|
202 |
|
---|
203 | pcapctl_dump_exchange_end(exch);
|
---|
204 |
|
---|
205 | if (rc != EOK) {
|
---|
206 | async_forget(req);
|
---|
207 | return rc;
|
---|
208 | }
|
---|
209 |
|
---|
210 | errno_t retval;
|
---|
211 | async_wait_for(req, &retval);
|
---|
212 | return retval;
|
---|
213 | }
|
---|
214 |
|
---|
215 | /** Finish current session for pcapctl
|
---|
216 | *
|
---|
217 | * @param sess Session to finish
|
---|
218 | * @return EOK on success or an error code
|
---|
219 | */
|
---|
220 | errno_t pcapctl_dump_stop(pcapctl_sess_t *sess)
|
---|
221 | {
|
---|
222 | errno_t rc;
|
---|
223 | async_exch_t *exch = async_exchange_begin(sess->sess);
|
---|
224 | rc = async_req_0_0(exch, PCAP_CONTROL_SET_STOP);
|
---|
225 |
|
---|
226 | pcapctl_dump_exchange_end(exch);
|
---|
227 | return rc;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /** @}
|
---|
231 | */
|
---|