source: mainline/uspace/lib/pcap/src/pcapctl_dump.c@ 59fe16d

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

file name debugging

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