[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 pcapctl
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file pcapctl app
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <stdio.h>
|
---|
| 36 | #include <str.h>
|
---|
| 37 | #include <errno.h>
|
---|
[9e26790] | 38 | #include <arg_parse.h>
|
---|
| 39 | #include <getopt.h>
|
---|
[28ed2d89] | 40 | #include <vfs/vfs.h>
|
---|
[17a8fcf] | 41 |
|
---|
[f161ce1] | 42 | #include "pcapdump_client.h"
|
---|
[17a8fcf] | 43 |
|
---|
| 44 | #define NAME "pcapctl"
|
---|
[2ebbe9b] | 45 | #define DEFAULT_DEV_NUM 0
|
---|
[28ed2d89] | 46 | #define DEFAULT_OPS_NUM 0
|
---|
[e1e8f7a] | 47 | #define DECIMAL_SYSTEM 10
|
---|
[17a8fcf] | 48 |
|
---|
[e1e8f7a] | 49 | static errno_t start_dumping(int *dev_number, const char *name, int *ops_index)
|
---|
[17a8fcf] | 50 | {
|
---|
[2ebbe9b] | 51 | pcapctl_sess_t *sess = NULL;
|
---|
[9e26790] | 52 | errno_t rc = pcapctl_dump_open(dev_number, &sess);
|
---|
[91042127] | 53 | if (rc != EOK) {
|
---|
| 54 | return 1;
|
---|
| 55 | }
|
---|
[7924f82] | 56 |
|
---|
[e1e8f7a] | 57 | rc = pcapctl_is_valid_ops_number(ops_index, sess);
|
---|
| 58 | if (rc != EOK)
|
---|
| 59 | {
|
---|
| 60 | printf("Wrong number of ops: %d.\n", *ops_index);
|
---|
| 61 | pcapctl_dump_close(sess);
|
---|
| 62 | return rc;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | rc = pcapctl_dump_start(name, ops_index, sess);
|
---|
[64ea525] | 66 | if (rc != EOK) {
|
---|
[1d14090] | 67 | printf("Starting the dumping was not successful.\n");
|
---|
| 68 | }
|
---|
[8765039] | 69 | pcapctl_dump_close(sess);
|
---|
[91042127] | 70 | return EOK;
|
---|
[17a8fcf] | 71 | }
|
---|
| 72 |
|
---|
[9e26790] | 73 | static errno_t stop_dumping(int *dev_number)
|
---|
[17a8fcf] | 74 | {
|
---|
[2ebbe9b] | 75 | pcapctl_sess_t *sess = NULL;
|
---|
[9e26790] | 76 | errno_t rc = pcapctl_dump_open(dev_number, &sess);
|
---|
[91042127] | 77 | if (rc != EOK) {
|
---|
| 78 | return 1;
|
---|
| 79 | }
|
---|
[1d14090] | 80 | rc = pcapctl_dump_stop(sess);
|
---|
[64ea525] | 81 | if (rc != EOK) {
|
---|
[1d14090] | 82 | printf("Stoping the dumping was not successful.\n");
|
---|
| 83 | }
|
---|
| 84 | pcapctl_dump_close(sess);
|
---|
| 85 | return EOK;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[2ebbe9b] | 88 | static void list_devs(void)
|
---|
| 89 | {
|
---|
[91042127] | 90 | pcapctl_list();
|
---|
[17a8fcf] | 91 | }
|
---|
| 92 |
|
---|
[9e26790] | 93 | /**
|
---|
| 94 | * Array of supported commandline options
|
---|
| 95 | */
|
---|
| 96 | static const struct option opts[] = {
|
---|
| 97 | { "device", required_argument, 0, 'd' },
|
---|
| 98 | { "list", no_argument, 0, 'l' },
|
---|
| 99 | { "help", no_argument, 0, 'h' },
|
---|
[28ed2d89] | 100 | { "outfile", required_argument, 0, 'o' },
|
---|
[9e26790] | 101 | { "start", no_argument, 0, 'r' },
|
---|
| 102 | { "stop", no_argument, 0, 't' },
|
---|
[28ed2d89] | 103 | { "ops", required_argument, 0, 'p' },
|
---|
| 104 | { "force", no_argument, 0, 'f'},
|
---|
[9e26790] | 105 | { 0, 0, 0, 0 }
|
---|
| 106 | };
|
---|
| 107 |
|
---|
[28ed2d89] | 108 | static bool file_exists(const char *path)
|
---|
| 109 | {
|
---|
| 110 | vfs_stat_t stats;
|
---|
| 111 |
|
---|
| 112 | if (vfs_stat_path(path, &stats) != EOK)
|
---|
| 113 | return false;
|
---|
| 114 |
|
---|
| 115 | return true;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[7924f82] | 118 | static void usage(void)
|
---|
[17a8fcf] | 119 | {
|
---|
[7924f82] | 120 | printf("Usage:\n"
|
---|
[9e26790] | 121 | NAME " --list | -l \n"
|
---|
[2ebbe9b] | 122 | "\tList of devices\n"
|
---|
[fd6845e9] | 123 | NAME " --start | -r --device= | -d <device number from list> --outfile= | -f <outfile>\n"
|
---|
[2ebbe9b] | 124 | "\tPackets dumped from device will be written to <outfile>\n"
|
---|
[fd6845e9] | 125 | NAME " --stop | -t --device= | -d <device>\n"
|
---|
[2ebbe9b] | 126 | "\tDumping from <device> stops\n"
|
---|
[fd6845e9] | 127 | NAME " --start | -s --outfile= | -f <outfile>\n"
|
---|
| 128 | "\tPackets dumped from the 0. device from the list will be written to <outfile>\n"
|
---|
[2ebbe9b] | 129 | NAME " --help | -h\n"
|
---|
| 130 | "\tShow this application help.\n");
|
---|
[17a8fcf] | 131 | }
|
---|
| 132 |
|
---|
| 133 | int main(int argc, char *argv[])
|
---|
| 134 | {
|
---|
[9e26790] | 135 | bool start = false;
|
---|
| 136 | bool stop = false;
|
---|
[28ed2d89] | 137 | int dev_number = DEFAULT_DEV_NUM;
|
---|
| 138 | int ops_number = DEFAULT_OPS_NUM;
|
---|
| 139 | bool forced = false;
|
---|
[03cd7a9e] | 140 | const char *output_file_name = "";
|
---|
[9e26790] | 141 | int idx = 0;
|
---|
| 142 | int ret = 0;
|
---|
| 143 | if (argc == 1) {
|
---|
[7924f82] | 144 | usage();
|
---|
[9e26790] | 145 | return 0;
|
---|
| 146 | }
|
---|
| 147 | while (ret != -1) {
|
---|
[28ed2d89] | 148 | ret = getopt_long(argc, argv, "d:lho:rtp:f", opts, &idx);
|
---|
[9e26790] | 149 | switch (ret) {
|
---|
[f08447b] | 150 | case 'd':
|
---|
| 151 | char *rest;
|
---|
[e1e8f7a] | 152 | long dev_result = strtol(optarg, &rest, DECIMAL_SYSTEM);
|
---|
| 153 | dev_number = (int)dev_result;
|
---|
[f08447b] | 154 | errno_t rc = pcapctl_is_valid_device(&dev_number);
|
---|
| 155 | if (rc != EOK) {
|
---|
| 156 | printf("Device with index %d not found\n", dev_number);
|
---|
| 157 | return 1;
|
---|
[91042127] | 158 | }
|
---|
[f08447b] | 159 | break;
|
---|
| 160 | case 'l':
|
---|
| 161 | list_devs();
|
---|
| 162 | return 0;
|
---|
| 163 | case 'h':
|
---|
| 164 | usage();
|
---|
| 165 | return 0;
|
---|
[28ed2d89] | 166 | case 'o':
|
---|
[f08447b] | 167 | output_file_name = optarg;
|
---|
| 168 | break;
|
---|
| 169 | case 'r':
|
---|
| 170 | start = true;
|
---|
| 171 | break;
|
---|
| 172 | case 't':
|
---|
| 173 | stop = true;
|
---|
| 174 | break;
|
---|
[28ed2d89] | 175 | case 'p':
|
---|
[e1e8f7a] | 176 | char* ops_inval;
|
---|
| 177 | long ops_result = strtol(optarg, &ops_inval, DECIMAL_SYSTEM);
|
---|
| 178 | ops_number = (int)ops_result;
|
---|
[1d14090] | 179 | break;
|
---|
[28ed2d89] | 180 | case 'f':
|
---|
| 181 | forced = true;
|
---|
| 182 | break;
|
---|
[17a8fcf] | 183 | }
|
---|
| 184 | }
|
---|
[9e26790] | 185 |
|
---|
[03cd7a9e] | 186 | printf("%s: HelenOS Packet Dumping utility: device - %d.\n", NAME, dev_number);
|
---|
[9e26790] | 187 |
|
---|
| 188 | if (start) {
|
---|
[28ed2d89] | 189 |
|
---|
| 190 | if (file_exists(output_file_name) && !forced)
|
---|
| 191 | {
|
---|
| 192 | printf("File %s already exists. If you want to write to it, then use flag --force.\n", output_file_name);
|
---|
| 193 | return 0;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[f08447b] | 196 | /* start with dev number and name */
|
---|
[e1e8f7a] | 197 | start_dumping(&dev_number, output_file_name, &ops_number);
|
---|
[9e26790] | 198 | } else if (stop) {
|
---|
[f08447b] | 199 | /* stop with dev number */
|
---|
[9e26790] | 200 | stop_dumping(&dev_number);
|
---|
[28ed2d89] | 201 | } else {
|
---|
| 202 | usage();
|
---|
[1d14090] | 203 | }
|
---|
[17a8fcf] | 204 | return 0;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | /** @}
|
---|
| 208 | */
|
---|