| 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 Client side of the pcapctl. Functions are called from the app pcapctl.
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <errno.h>
|
|---|
| 36 | #include <async.h>
|
|---|
| 37 | #include <str.h>
|
|---|
| 38 | #include <stdlib.h>
|
|---|
| 39 | #include <stdio.h>
|
|---|
| 40 | #include <ctype.h>
|
|---|
| 41 | #include "pcapdump_client.h"
|
|---|
| 42 | #include "pcapdump_ipc.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 | /** Get service based on the index of the device.
|
|---|
| 54 | * @param index of the device.
|
|---|
| 55 | * @param svc placeholder for service ide.
|
|---|
| 56 | * @return EOK if successful, error code otherwise.
|
|---|
| 57 | */
|
|---|
| 58 | static errno_t pcapctl_cat_get_svc(int *index, service_id_t *svc)
|
|---|
| 59 | {
|
|---|
| 60 | errno_t rc;
|
|---|
| 61 | category_id_t pcap_cat;
|
|---|
| 62 | size_t count;
|
|---|
| 63 | service_id_t *pcap_svcs = NULL;
|
|---|
| 64 |
|
|---|
| 65 | rc = loc_category_get_id("pcap", &pcap_cat, 0);
|
|---|
| 66 | if (rc != EOK) {
|
|---|
| 67 | fprintf(stderr, "Error resolving category 'pcap'.\n");
|
|---|
| 68 | return rc;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | rc = loc_category_get_svcs(pcap_cat, &pcap_svcs, &count);
|
|---|
| 72 | if (rc != EOK) {
|
|---|
| 73 | fprintf(stderr, "Error resolving list of pcap services.\n");
|
|---|
| 74 | free(pcap_svcs);
|
|---|
| 75 | return rc;
|
|---|
| 76 | }
|
|---|
| 77 | if (*index < (int)count) {
|
|---|
| 78 | *svc = pcap_svcs[*index];
|
|---|
| 79 | free(pcap_svcs);
|
|---|
| 80 | return EOK;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | return ENOENT;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | /** Check if the index is an index of valid device.
|
|---|
| 87 | * @param index to check.
|
|---|
| 88 | * @return EOK if device is valid, error code otherwise.
|
|---|
| 89 | */
|
|---|
| 90 | errno_t pcapctl_is_valid_device(int *index)
|
|---|
| 91 | {
|
|---|
| 92 | errno_t rc;
|
|---|
| 93 | category_id_t pcap_cat;
|
|---|
| 94 | size_t count;
|
|---|
| 95 | service_id_t *pcap_svcs = NULL;
|
|---|
| 96 |
|
|---|
| 97 | rc = loc_category_get_id("pcap", &pcap_cat, 0);
|
|---|
| 98 | if (rc != EOK) {
|
|---|
| 99 | fprintf(stderr, "Error resolving category pcap.\n");
|
|---|
| 100 | return rc;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | rc = loc_category_get_svcs(pcap_cat, &pcap_svcs, &count);
|
|---|
| 104 | if (rc != EOK) {
|
|---|
| 105 | fprintf(stderr, "Error resolving list of pcap services.\n");
|
|---|
| 106 | free(pcap_svcs);
|
|---|
| 107 | return rc;
|
|---|
| 108 | }
|
|---|
| 109 | if (*index + 1 > (int)count || *index < 0) {
|
|---|
| 110 | return EINVAL;
|
|---|
| 111 | }
|
|---|
| 112 | return EOK;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /** Check if the index is an index of valid writer operations.
|
|---|
| 116 | * @param index to check.
|
|---|
| 117 | * @param sess pcapctl session for IPC communictaion.
|
|---|
| 118 | */
|
|---|
| 119 | errno_t pcapctl_is_valid_ops_number(int *index, pcapctl_sess_t *sess)
|
|---|
| 120 | {
|
|---|
| 121 | async_exch_t *exch = async_exchange_begin(sess->sess);
|
|---|
| 122 | ipc_call_t answer;
|
|---|
| 123 | aid_t req = async_send_0(exch, PCAP_CONTROL_GET_OPS_NUM, &answer);
|
|---|
| 124 |
|
|---|
| 125 | async_exchange_end(exch);
|
|---|
| 126 |
|
|---|
| 127 | errno_t retval;
|
|---|
| 128 | async_wait_for(req, &retval);
|
|---|
| 129 |
|
|---|
| 130 | if (retval != EOK) {
|
|---|
| 131 | return retval;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | int ops_count = (int)ipc_get_arg1(&answer);
|
|---|
| 135 | if (*index + 1 > ops_count || *index < 0) {
|
|---|
| 136 | return EINVAL;
|
|---|
| 137 | }
|
|---|
| 138 | return EOK;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /** Get all devices that can dump packets. */
|
|---|
| 142 | errno_t pcapctl_list(void)
|
|---|
| 143 | {
|
|---|
| 144 | errno_t rc;
|
|---|
| 145 | category_id_t pcap_cat;
|
|---|
| 146 | size_t count;
|
|---|
| 147 | service_id_t *pcap_svcs = NULL;
|
|---|
| 148 |
|
|---|
| 149 | rc = loc_category_get_id("pcap", &pcap_cat, 0);
|
|---|
| 150 | if (rc != EOK) {
|
|---|
| 151 | fprintf(stderr, "Error resolving category pcap.\n");
|
|---|
| 152 | return rc;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | rc = loc_category_get_svcs(pcap_cat, &pcap_svcs, &count);
|
|---|
| 156 | if (rc != EOK) {
|
|---|
| 157 | fprintf(stderr, "Error resolving list of pcap services.\n");
|
|---|
| 158 | free(pcap_svcs);
|
|---|
| 159 | return rc;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | fprintf(stdout, "Devices:\n");
|
|---|
| 163 | for (unsigned i = 0; i < count; ++i) {
|
|---|
| 164 | char *name = NULL;
|
|---|
| 165 | loc_service_get_name(pcap_svcs[i], &name);
|
|---|
| 166 | fprintf(stdout, "%d. %s\n", i, name);
|
|---|
| 167 | }
|
|---|
| 168 | free(pcap_svcs);
|
|---|
| 169 | return EOK;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | /** Start pcapctl IPC session.
|
|---|
| 173 | * @param index index of the device which can dump packets.
|
|---|
| 174 | * @param rsess placeholder for the session.
|
|---|
| 175 | * @return EOK if successful, error code otherwise.
|
|---|
| 176 | */
|
|---|
| 177 | errno_t pcapctl_dump_open(int *index, pcapctl_sess_t **rsess)
|
|---|
| 178 | {
|
|---|
| 179 | errno_t rc;
|
|---|
| 180 | service_id_t svc;
|
|---|
| 181 | pcapctl_sess_t *sess = calloc(1, sizeof(pcapctl_sess_t));
|
|---|
| 182 | if (sess == NULL)
|
|---|
| 183 | return ENOMEM;
|
|---|
| 184 |
|
|---|
| 185 | if (*index == -1) {
|
|---|
| 186 | *index = 0;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | rc = pcapctl_cat_get_svc(index, &svc);
|
|---|
| 190 | if (rc != EOK) {
|
|---|
| 191 | fprintf(stderr, "Error finding the device with index: %d\n", *index);
|
|---|
| 192 | goto error;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | async_sess_t *new_session = loc_service_connect(svc, INTERFACE_PCAP_CONTROL, 0);
|
|---|
| 196 | if (new_session == NULL) {
|
|---|
| 197 | fprintf(stderr, "Error connecting to service.\n");
|
|---|
| 198 | rc = EREFUSED;
|
|---|
| 199 | goto error;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | sess->sess = new_session;
|
|---|
| 203 | *rsess = sess;
|
|---|
| 204 | return EOK;
|
|---|
| 205 | error:
|
|---|
| 206 | pcapctl_dump_close(sess);
|
|---|
| 207 | return rc;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | /** Close pcapctl IPC session.
|
|---|
| 211 | * @param sess Session to close.
|
|---|
| 212 | * @return EOK if successful, error code otherwise.
|
|---|
| 213 | */
|
|---|
| 214 | errno_t pcapctl_dump_close(pcapctl_sess_t *sess)
|
|---|
| 215 | {
|
|---|
| 216 | free(sess);
|
|---|
| 217 | return EOK;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | /** Send start request via IPC to start dumping.
|
|---|
| 221 | *
|
|---|
| 222 | * @param name Name of the destination buffer to dump packets to.
|
|---|
| 223 | * @param sess Session that is used for communication.
|
|---|
| 224 | * @return EOK on success or an error code.
|
|---|
| 225 | */
|
|---|
| 226 | errno_t pcapctl_dump_start(const char *name, int *ops_index, pcapctl_sess_t *sess)
|
|---|
| 227 | {
|
|---|
| 228 | errno_t rc;
|
|---|
| 229 | async_exch_t *exch = async_exchange_begin(sess->sess);
|
|---|
| 230 |
|
|---|
| 231 | size_t size = str_size(name);
|
|---|
| 232 | aid_t req = async_send_1(exch, PCAP_CONTROL_SET_START, *ops_index, NULL);
|
|---|
| 233 |
|
|---|
| 234 | rc = async_data_write_start(exch, name, size);
|
|---|
| 235 |
|
|---|
| 236 | pcapctl_dump_exchange_end(exch);
|
|---|
| 237 |
|
|---|
| 238 | if (rc != EOK) {
|
|---|
| 239 | async_forget(req);
|
|---|
| 240 | return rc;
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | errno_t retval;
|
|---|
| 244 | async_wait_for(req, &retval);
|
|---|
| 245 | return retval;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | /** Send stop request via IPC to start dumping.
|
|---|
| 249 | *
|
|---|
| 250 | * @param sess Session that is used for communication.
|
|---|
| 251 | * @return EOK on success or an error code.
|
|---|
| 252 | */
|
|---|
| 253 | errno_t pcapctl_dump_stop(pcapctl_sess_t *sess)
|
|---|
| 254 | {
|
|---|
| 255 | errno_t rc;
|
|---|
| 256 | async_exch_t *exch = async_exchange_begin(sess->sess);
|
|---|
| 257 | rc = async_req_0_0(exch, PCAP_CONTROL_SET_STOP);
|
|---|
| 258 |
|
|---|
| 259 | pcapctl_dump_exchange_end(exch);
|
|---|
| 260 | return rc;
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | /** @}
|
|---|
| 264 | */
|
|---|