source: mainline/uspace/lib/pcap/src/pcapctl_dump.c@ 1333dfc

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

inetsrv dumps

  • Property mode set to 100644
File size: 5.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/** @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 */
49static 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
82errno_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 */
110errno_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 */
143errno_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 // rc = pcapctl_cat_get_svc(index, &svc);
152 // if (rc != EOK) {
153 // printf("Error finding the device with index: %d\n", *index);
154 // goto error;
155 // }
156
157 rc = loc_service_get_id("net/inet", &svc, 0);
158 if (rc != EOK)
159 return ENOENT;
160
161 async_sess_t *new_session = loc_service_connect(svc, INTERFACE_PCAP_CONTROL, 0);
162 if (new_session == NULL) {
163 fprintf(stderr, "Error connecting to service.\n");
164 rc = EREFUSED;
165 goto error;
166 }
167 printf("got new session\n");
168 sess->sess = new_session;
169 *rsess = sess;
170 return EOK;
171error:
172 pcapctl_dump_close(sess);
173 return rc;
174}
175
176/**
177 *
178 */
179errno_t pcapctl_dump_close(pcapctl_sess_t *sess)
180{
181 free(sess);
182 return EOK;
183}
184
185/** Starting a new session for pcapctl
186 *
187 * @param name Name of the file to dump packets to
188 * @param sess session to start
189 * @return EOK on success or an error code
190 */
191errno_t pcapctl_dump_start(const char *name, pcapctl_sess_t *sess)
192{
193 printf("pcapctl_dump_start\n");
194 errno_t rc;
195 async_exch_t *exch = async_exchange_begin(sess->sess);
196
197 size_t size = str_size(name);
198 aid_t req = async_send_0(exch, PCAP_CONTROL_SET_START, NULL);
199
200 rc = async_data_write_start(exch, (void *) name, size);
201
202 pcapctl_dump_exchange_end(exch);
203
204 if (rc != EOK) {
205 async_forget(req);
206 return rc;
207 }
208
209 errno_t retval;
210 async_wait_for(req, &retval);
211 return retval;
212}
213
214/** Finish current session for pcapctl
215 *
216 * @param sess Session to finish
217 * @return EOK on success or an error code
218 */
219errno_t pcapctl_dump_stop(pcapctl_sess_t *sess)
220{
221 errno_t rc;
222 async_exch_t *exch = async_exchange_begin(sess->sess);
223 rc = async_req_0_0(exch, PCAP_CONTROL_SET_STOP);
224
225 pcapctl_dump_exchange_end(exch);
226 return rc;
227}
228
229/** @}
230 */
Note: See TracBrowser for help on using the repository browser.