[17a8fcf] | 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>
|
---|
[7924f82] | 40 | #include <stdio.h>
|
---|
| 41 | #include <ctype.h>
|
---|
[17a8fcf] | 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 |
|
---|
[0210d42d] | 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 | }
|
---|
[91042127] | 81 |
|
---|
[9e26790] | 82 | errno_t pcapctl_is_valid_device(int *index)
|
---|
[2ebbe9b] | 83 | {
|
---|
[91042127] | 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);
|
---|
[17a8fcf] | 90 | if (rc != EOK) {
|
---|
[8765039] | 91 | printf("Error resolving category pcap.\n");
|
---|
[91042127] | 92 | return rc;
|
---|
[17a8fcf] | 93 | }
|
---|
| 94 |
|
---|
[91042127] | 95 | rc = loc_category_get_svcs(pcap_cat, &pcap_svcs, &count);
|
---|
| 96 | if (rc != EOK) {
|
---|
| 97 | printf("Error resolving list of pcap services.\n");
|
---|
[8765039] | 98 | free(pcap_svcs);
|
---|
[91042127] | 99 | return rc;
|
---|
| 100 | }
|
---|
[9e26790] | 101 | if (*index + 1 > (int)count || *index < 0) {
|
---|
| 102 | return EINVAL;
|
---|
[91042127] | 103 | }
|
---|
| 104 | return EOK;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[9e26790] | 107 | /**
|
---|
| 108 | *
|
---|
| 109 | */
|
---|
| 110 | errno_t pcapctl_list(void)
|
---|
[2ebbe9b] | 111 | {
|
---|
[7924f82] | 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 |
|
---|
[9e26790] | 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);
|
---|
[7924f82] | 135 | }
|
---|
[9e26790] | 136 | free(pcap_svcs);
|
---|
[7924f82] | 137 | return EOK;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /**
|
---|
| 141 | *
|
---|
| 142 | */
|
---|
[9e26790] | 143 | errno_t pcapctl_dump_open(int *index, pcapctl_sess_t **rsess)
|
---|
[91042127] | 144 | {
|
---|
| 145 | errno_t rc;
|
---|
| 146 | service_id_t svc;
|
---|
[8765039] | 147 | pcapctl_sess_t *sess = calloc(1, sizeof(pcapctl_sess_t));
|
---|
| 148 | if (sess == NULL)
|
---|
| 149 | return ENOMEM;
|
---|
| 150 |
|
---|
[0210d42d] | 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 | }
|
---|
[6c60a7c] | 168 |
|
---|
[9e26790] | 169 |
|
---|
[91042127] | 170 | async_sess_t *new_session = loc_service_connect(svc, INTERFACE_PCAP_CONTROL, 0);
|
---|
[17a8fcf] | 171 | if (new_session == NULL) {
|
---|
[91042127] | 172 | fprintf(stderr, "Error connecting to service.\n");
|
---|
[17a8fcf] | 173 | rc = EREFUSED;
|
---|
| 174 | goto error;
|
---|
| 175 | }
|
---|
[0210d42d] | 176 |
|
---|
[17a8fcf] | 177 | sess->sess = new_session;
|
---|
[8765039] | 178 | *rsess = sess;
|
---|
| 179 | return EOK;
|
---|
[17a8fcf] | 180 | error:
|
---|
[8765039] | 181 | pcapctl_dump_close(sess);
|
---|
[17a8fcf] | 182 | return rc;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[7924f82] | 185 | /**
|
---|
| 186 | *
|
---|
| 187 | */
|
---|
[8765039] | 188 | errno_t pcapctl_dump_close(pcapctl_sess_t *sess)
|
---|
| 189 | {
|
---|
| 190 | free(sess);
|
---|
| 191 | return EOK;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
[17a8fcf] | 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 | */
|
---|
| 200 | errno_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 |
|
---|
[59fe16d] | 208 | rc = async_data_write_start(exch, (const void *) name, size);
|
---|
[17a8fcf] | 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 | */
|
---|
| 227 | errno_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 | */
|
---|