| 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 pcapctl
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file pcapctl command-line utility.
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <stdio.h>
|
|---|
| 36 | #include <str.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <arg_parse.h>
|
|---|
| 39 | #include <getopt.h>
|
|---|
| 40 | #include <vfs/vfs.h>
|
|---|
| 41 |
|
|---|
| 42 | #include "pcapdump_client.h"
|
|---|
| 43 |
|
|---|
| 44 | #define NAME "pcapctl"
|
|---|
| 45 | #define DEFAULT_DEV_NUM 0
|
|---|
| 46 | #define DECIMAL_SYSTEM 10
|
|---|
| 47 |
|
|---|
| 48 | /* Default writer operations for dumper, must be consistent with array defined in /uspace/lib/pcap/pcap_dumper.c */
|
|---|
| 49 | #define DEFAULT_FILE_OPS 0
|
|---|
| 50 | #define SHORT_FILE_OPS 1
|
|---|
| 51 | #define APPEND_FILE_OPS 2
|
|---|
| 52 | #define USB_FILE_OPS 3
|
|---|
| 53 |
|
|---|
| 54 | /** Create async session and send start request.
|
|---|
| 55 | * @param dev_number index of the device that can dump packets.
|
|---|
| 56 | * @param name of the output buffer.
|
|---|
| 57 | * @param ops_index index of the writer operations for the dumper.
|
|---|
| 58 | * @return EOK if all parameters are valid and start request was sent successfully, error code otherwise.
|
|---|
| 59 | */
|
|---|
| 60 | static errno_t start_dumping(int *dev_number, const char *name, int *ops_index)
|
|---|
| 61 | {
|
|---|
| 62 | pcapctl_sess_t *sess = NULL;
|
|---|
| 63 | errno_t rc = pcapctl_dump_open(dev_number, &sess);
|
|---|
| 64 | if (rc != EOK) {
|
|---|
| 65 | return 1;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | rc = pcapctl_is_valid_ops_number(ops_index, sess);
|
|---|
| 69 | if (rc != EOK) {
|
|---|
| 70 | printf("Wrong number of ops: %d.\n", *ops_index);
|
|---|
| 71 | pcapctl_dump_close(sess);
|
|---|
| 72 | return rc;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | rc = pcapctl_dump_start(name, ops_index, sess);
|
|---|
| 76 | if (rc != EOK) {
|
|---|
| 77 | if (rc == EBUSY) {
|
|---|
| 78 | printf("Dumping for device %d is in progress, stop to start dumping to file %s.\n", *dev_number, name);
|
|---|
| 79 | }
|
|---|
| 80 | printf("Starting the dumping was not successful.\n");
|
|---|
| 81 | }
|
|---|
| 82 | pcapctl_dump_close(sess);
|
|---|
| 83 | return EOK;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | /** Create async session and send stop dumping request.
|
|---|
| 87 | * @param dev_numbe index of the device on which the dumping will be stopped.
|
|---|
| 88 | * @return EOK if request was sent successfully, error code otherwise.
|
|---|
| 89 | */
|
|---|
| 90 | static errno_t stop_dumping(int *dev_number)
|
|---|
| 91 | {
|
|---|
| 92 | pcapctl_sess_t *sess = NULL;
|
|---|
| 93 | errno_t rc = pcapctl_dump_open(dev_number, &sess);
|
|---|
| 94 | if (rc != EOK) {
|
|---|
| 95 | return 1;
|
|---|
| 96 | }
|
|---|
| 97 | rc = pcapctl_dump_stop(sess);
|
|---|
| 98 | if (rc != EOK) {
|
|---|
| 99 | printf("Stoping the dumping was not successful.\n");
|
|---|
| 100 | }
|
|---|
| 101 | pcapctl_dump_close(sess);
|
|---|
| 102 | return EOK;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /** Print devices that can dump packets. */
|
|---|
| 106 | static void list_devs(void)
|
|---|
| 107 | {
|
|---|
| 108 | pcapctl_list();
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | /**
|
|---|
| 112 | * Array of supported commandline options
|
|---|
| 113 | */
|
|---|
| 114 | static const struct option opts[] = {
|
|---|
| 115 | { "append", required_argument, 0, 'A' }, /* file as argument and ops 0 if not exist and 2 if exists */
|
|---|
| 116 | { "new", required_argument, 0, 'N' }, /* file name as argument */
|
|---|
| 117 | { "truncated", required_argument, 0, 'T' }, /* file as an argument with device 0 and dump truncated packets (for debugging purposes) */
|
|---|
| 118 | { "usb", required_argument, 0, 'U' }, /* dump usb packets (not fully implemnted) */
|
|---|
| 119 | { "device", required_argument, 0, 'd' },
|
|---|
| 120 | { "list", no_argument, 0, 'l' },
|
|---|
| 121 | { "help", no_argument, 0, 'h' },
|
|---|
| 122 | { "outfile", required_argument, 0, 'o' },
|
|---|
| 123 | { "start", no_argument, 0, 'r' },
|
|---|
| 124 | { "stop", no_argument, 0, 't' },
|
|---|
| 125 | { "ops", required_argument, 0, 'p' },
|
|---|
| 126 | { "force", no_argument, 0, 'f' },
|
|---|
| 127 | { 0, 0, 0, 0 }
|
|---|
| 128 | };
|
|---|
| 129 |
|
|---|
| 130 | /** Check if the file exists.
|
|---|
| 131 | * @param path of the file to check.
|
|---|
| 132 | * @return true if exists, false otherwise.
|
|---|
| 133 | */
|
|---|
| 134 | static bool file_exists(const char *path)
|
|---|
| 135 | {
|
|---|
| 136 | vfs_stat_t stats;
|
|---|
| 137 | if (vfs_stat_path(path, &stats) != EOK) {
|
|---|
| 138 | return false;
|
|---|
| 139 | } else {
|
|---|
| 140 | return true;
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | static void usage(void)
|
|---|
| 145 | {
|
|---|
| 146 | printf("HelenOS Packet Dumping utility.\n"
|
|---|
| 147 | "Usage:\n"
|
|---|
| 148 | NAME " --list | -l \n"
|
|---|
| 149 | "\tList of devices\n"
|
|---|
| 150 | NAME " --new= | -N <outfile>\n"
|
|---|
| 151 | "\tStart dumping with ops - 0, on device - 0\n"
|
|---|
| 152 | NAME " --append= | -A <outfile>\n"
|
|---|
| 153 | "\tContinue dumping on device - 0 to already existing file\n"
|
|---|
| 154 | NAME " --truncated= | -T <outfile>\n"
|
|---|
| 155 | "\tStart dumping truncated packets to file on device - 0\n"
|
|---|
| 156 | NAME " --start | -r --device= | -d <device number from list> --outfile= | -o <outfile> --ops= | p <ops index>\n"
|
|---|
| 157 | "\tPackets dumped from device will be written to <outfile>\n"
|
|---|
| 158 | NAME " --stop | -t --device= | -d <device number from list>\n"
|
|---|
| 159 | "\tDumping from <device> stops\n"
|
|---|
| 160 | NAME " --start | -r --outfile= | -o <outfile>\n"
|
|---|
| 161 | "\tPackets dumped from the 0. device from the list will be written to <outfile>\n"
|
|---|
| 162 | NAME " --help | -h\n"
|
|---|
| 163 | "\tShow this application help.\n"
|
|---|
| 164 | NAME " --force | -f"
|
|---|
| 165 | "\tTo open existing file and write to it.\n");
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | int main(int argc, char *argv[])
|
|---|
| 169 | {
|
|---|
| 170 | bool start = false;
|
|---|
| 171 | bool stop = false;
|
|---|
| 172 | int dev_number = DEFAULT_DEV_NUM;
|
|---|
| 173 | int ops_number = DEFAULT_FILE_OPS;
|
|---|
| 174 | bool forced = false;
|
|---|
| 175 | const char *output_file_name = "";
|
|---|
| 176 | int idx = 0;
|
|---|
| 177 | int ret = 0;
|
|---|
| 178 | if (argc == 1) {
|
|---|
| 179 | usage();
|
|---|
| 180 | return 0;
|
|---|
| 181 | }
|
|---|
| 182 | while (ret != -1) {
|
|---|
| 183 | ret = getopt_long(argc, argv, "A:N:T:U:d:lho:rtp:f", opts, &idx);
|
|---|
| 184 | switch (ret) {
|
|---|
| 185 | case 'd':
|
|---|
| 186 | char *rest;
|
|---|
| 187 | long dev_result = strtol(optarg, &rest, DECIMAL_SYSTEM);
|
|---|
| 188 | dev_number = (int)dev_result;
|
|---|
| 189 | errno_t rc = pcapctl_is_valid_device(&dev_number);
|
|---|
| 190 | if (rc != EOK) {
|
|---|
| 191 | printf("Device with index %d not found\n", dev_number);
|
|---|
| 192 | return 1;
|
|---|
| 193 | }
|
|---|
| 194 | break;
|
|---|
| 195 | case 'A':
|
|---|
| 196 | start = true;
|
|---|
| 197 | output_file_name = optarg;
|
|---|
| 198 | if (file_exists(output_file_name)) {
|
|---|
| 199 | ops_number = APPEND_FILE_OPS;
|
|---|
| 200 | }
|
|---|
| 201 | break;
|
|---|
| 202 | case 'N':
|
|---|
| 203 | start = true;
|
|---|
| 204 | output_file_name = optarg;
|
|---|
| 205 | break;
|
|---|
| 206 | case 'T':
|
|---|
| 207 | start = true;
|
|---|
| 208 | output_file_name = optarg;
|
|---|
| 209 | ops_number = SHORT_FILE_OPS;
|
|---|
| 210 | break;
|
|---|
| 211 | case 'U':
|
|---|
| 212 | start = true;
|
|---|
| 213 | output_file_name = optarg;
|
|---|
| 214 | ops_number = USB_FILE_OPS;
|
|---|
| 215 | break;
|
|---|
| 216 | case 'l':
|
|---|
| 217 | list_devs();
|
|---|
| 218 | return 0;
|
|---|
| 219 | case 'h':
|
|---|
| 220 | usage();
|
|---|
| 221 | return 0;
|
|---|
| 222 | case 'o':
|
|---|
| 223 | output_file_name = optarg;
|
|---|
| 224 | break;
|
|---|
| 225 | case 'r':
|
|---|
| 226 | start = true;
|
|---|
| 227 | break;
|
|---|
| 228 | case 't':
|
|---|
| 229 | stop = true;
|
|---|
| 230 | break;
|
|---|
| 231 | case 'p':
|
|---|
| 232 | char *ops_inval;
|
|---|
| 233 | long ops_result = strtol(optarg, &ops_inval, DECIMAL_SYSTEM);
|
|---|
| 234 | ops_number = (int)ops_result;
|
|---|
| 235 | break;
|
|---|
| 236 | case 'f':
|
|---|
| 237 | forced = true;
|
|---|
| 238 | break;
|
|---|
| 239 | }
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | if (!str_cmp(output_file_name, "") && start) {
|
|---|
| 243 | printf("Dumping destination was not specified. Specify with --outfile | -o\n");
|
|---|
| 244 | return 1;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | if (start) {
|
|---|
| 248 |
|
|---|
| 249 | if (file_exists(output_file_name) && !forced && ops_number != 2) {
|
|---|
| 250 | printf("File %s already exists. If you want to overwrite it, then use flag --force.\n", output_file_name);
|
|---|
| 251 | return 0;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | printf("Start dumping on device - %d, ops - %d.\n", dev_number, ops_number);
|
|---|
| 255 |
|
|---|
| 256 | /* start with dev number and name */
|
|---|
| 257 | start_dumping(&dev_number, output_file_name, &ops_number);
|
|---|
| 258 | } else if (stop) {
|
|---|
| 259 | /* stop with dev number */
|
|---|
| 260 | printf("Stop dumping on device - %d.\n", dev_number);
|
|---|
| 261 | stop_dumping(&dev_number);
|
|---|
| 262 | } else {
|
|---|
| 263 | usage();
|
|---|
| 264 | return 1;
|
|---|
| 265 | }
|
|---|
| 266 | return 0;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | /** @}
|
|---|
| 270 | */
|
|---|