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 |
|
---|
41 | #include "pcapctl_dump.h"
|
---|
42 | #include "pcapdump_iface.h"
|
---|
43 |
|
---|
44 | /** Finish an async exchange on the pcapctl session
|
---|
45 | *
|
---|
46 | * @param exch Exchange to be finished
|
---|
47 | */
|
---|
48 | static void pcapctl_dump_exchange_end(async_exch_t *exch)
|
---|
49 | {
|
---|
50 | async_exchange_end(exch);
|
---|
51 | }
|
---|
52 |
|
---|
53 | static errno_t pcapctl_cat_has_drv(const char *drv_name, service_id_t* svc) {
|
---|
54 | errno_t rc;
|
---|
55 | category_id_t pcap_cat;
|
---|
56 | size_t count;
|
---|
57 | service_id_t *pcap_svcs = NULL;
|
---|
58 |
|
---|
59 | rc = loc_category_get_id("pcap", &pcap_cat, 0);
|
---|
60 | if (rc != EOK) {
|
---|
61 | printf("Error resolving category 'pcap'.\n");
|
---|
62 | return rc;
|
---|
63 | }
|
---|
64 |
|
---|
65 | rc = loc_category_get_svcs(pcap_cat, &pcap_svcs, &count);
|
---|
66 | if (rc != EOK) {
|
---|
67 | printf("Error resolving list of pcap services.\n");
|
---|
68 | return rc;
|
---|
69 | }
|
---|
70 |
|
---|
71 | for (unsigned i = 0; i < count; ++i) {
|
---|
72 | char *name = NULL;
|
---|
73 | loc_service_get_name(pcap_svcs[i], &name);
|
---|
74 | if (!str_cmp(drv_name, name)) {
|
---|
75 | *svc = pcap_svcs[i];
|
---|
76 | return EOK;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | free(pcap_svcs);
|
---|
80 | return 1;
|
---|
81 | }
|
---|
82 |
|
---|
83 | extern errno_t pcapctl_list(void) {
|
---|
84 |
|
---|
85 | errno_t rc;
|
---|
86 | category_id_t pcap_cat;
|
---|
87 | size_t count;
|
---|
88 | service_id_t *pcap_svcs = NULL;
|
---|
89 |
|
---|
90 | rc = loc_category_get_id("pcap", &pcap_cat, 0);
|
---|
91 | if (rc != EOK) {
|
---|
92 | printf("Error resolving category 'pcap'.\n");
|
---|
93 | return rc;
|
---|
94 | }
|
---|
95 |
|
---|
96 | rc = loc_category_get_svcs(pcap_cat, &pcap_svcs, &count);
|
---|
97 | if (rc != EOK) {
|
---|
98 | printf("Error resolving list of pcap services.\n");
|
---|
99 | return rc;
|
---|
100 | }
|
---|
101 |
|
---|
102 | assert((count > 0) && "TODO: not implemented when no services are available\n");
|
---|
103 |
|
---|
104 | fprintf(stdout, "Drivers:\n");
|
---|
105 | for (unsigned i = 0; i < count; ++i) {
|
---|
106 | char *name = NULL;
|
---|
107 | loc_service_get_name(pcap_svcs[i], &name);
|
---|
108 |
|
---|
109 | fprintf(stdout, "driver: %s\n", name);
|
---|
110 | }
|
---|
111 | return EOK;
|
---|
112 | }
|
---|
113 |
|
---|
114 | errno_t pcapctl_dump_init(pcapctl_sess_t *sess, const char *drv_name)
|
---|
115 | {
|
---|
116 | errno_t rc;
|
---|
117 | service_id_t svc;
|
---|
118 | rc = pcapctl_cat_has_drv(drv_name, &svc);
|
---|
119 | if (rc != EOK) {
|
---|
120 | fprintf(stderr, "No such driver was found.\n");
|
---|
121 | return 1;
|
---|
122 | }
|
---|
123 | async_sess_t *new_session = loc_service_connect(svc, INTERFACE_PCAP_CONTROL, 0);
|
---|
124 | if (new_session == NULL) {
|
---|
125 | fprintf(stderr, "Error connecting to service.\n");
|
---|
126 | rc = EREFUSED;
|
---|
127 | goto error;
|
---|
128 | }
|
---|
129 | sess->sess = new_session;
|
---|
130 | rc = EOK;
|
---|
131 | error:
|
---|
132 | return rc;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /** Starting a new session for pcapctl
|
---|
136 | *
|
---|
137 | * @param name Name of the file to dump packets to
|
---|
138 | * @param sess session to start
|
---|
139 | * @return EOK on success or an error code
|
---|
140 | */
|
---|
141 | errno_t pcapctl_dump_start(const char *name, pcapctl_sess_t *sess)
|
---|
142 | {
|
---|
143 | errno_t rc;
|
---|
144 | async_exch_t *exch = async_exchange_begin(sess->sess);
|
---|
145 |
|
---|
146 | size_t size = str_size(name);
|
---|
147 | aid_t req = async_send_0(exch, PCAP_CONTROL_SET_START, NULL);
|
---|
148 |
|
---|
149 | rc = async_data_write_start(exch, (void *) name, size);
|
---|
150 |
|
---|
151 | pcapctl_dump_exchange_end(exch);
|
---|
152 |
|
---|
153 | if (rc != EOK) {
|
---|
154 | async_forget(req);
|
---|
155 | return rc;
|
---|
156 | }
|
---|
157 |
|
---|
158 | errno_t retval;
|
---|
159 | async_wait_for(req, &retval);
|
---|
160 | return retval;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /** Finish current session for pcapctl
|
---|
164 | *
|
---|
165 | * @param sess Session to finish
|
---|
166 | * @return EOK on success or an error code
|
---|
167 | */
|
---|
168 | errno_t pcapctl_dump_stop(pcapctl_sess_t *sess)
|
---|
169 | {
|
---|
170 | errno_t rc;
|
---|
171 | async_exch_t *exch = async_exchange_begin(sess->sess);
|
---|
172 | rc = async_req_0_0(exch, PCAP_CONTROL_SET_STOP);
|
---|
173 |
|
---|
174 | pcapctl_dump_exchange_end(exch);
|
---|
175 | return rc;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /** @}
|
---|
179 | */
|
---|