source: mainline/uspace/app/pcapctl/main.c@ fb31682

Last change on this file since fb31682 was fb31682, checked in by Nataliia Korop <n.corop08@…>, 10 months ago

user friendly options, trying to start while dumping → err msg

  • Property mode set to 100644
File size: 6.4 KB
Line 
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>
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#define DEFAULT_FILE_OPS 0
49#define SHORT_FILE_OPS 1
50#define APPEND_FILE_OPS 2
51#define USB_FILE_OPS 3
52
53static errno_t start_dumping(int *dev_number, const char *name, int *ops_index)
54{
55 pcapctl_sess_t *sess = NULL;
56 errno_t rc = pcapctl_dump_open(dev_number, &sess);
57 if (rc != EOK) {
58 return 1;
59 }
60
61 rc = pcapctl_is_valid_ops_number(ops_index, sess);
62 if (rc != EOK) {
63 printf("Wrong number of ops: %d.\n", *ops_index);
64 pcapctl_dump_close(sess);
65 return rc;
66 }
67
68 rc = pcapctl_dump_start(name, ops_index, sess);
69 if (rc != EOK) {
70 if (rc == EBUSY) {
71 printf("Dumping for device %d is in process, stop to start dumping to file %s.\n", *dev_number, name);
72 }
73 printf("Starting the dumping was not successful.\n");
74 }
75 pcapctl_dump_close(sess);
76 return EOK;
77}
78
79static errno_t stop_dumping(int *dev_number)
80{
81 pcapctl_sess_t *sess = NULL;
82 errno_t rc = pcapctl_dump_open(dev_number, &sess);
83 if (rc != EOK) {
84 return 1;
85 }
86 rc = pcapctl_dump_stop(sess);
87 if (rc != EOK) {
88 printf("Stoping the dumping was not successful.\n");
89 }
90 pcapctl_dump_close(sess);
91 return EOK;
92}
93
94static void list_devs(void)
95{
96 pcapctl_list();
97}
98
99/**
100 * Array of supported commandline options
101 */
102static const struct option opts[] = {
103 { "append", required_argument, 0, 'A' }, // file as argument and ops 0 if not exist and 2 if exists
104 { "new", required_argument, 0, 'N' }, // file name as argument
105 { "truncated", required_argument, 0, 'T' }, // truncated ops
106 { "usb", required_argument, 0, 'U' }, //??
107 { "device", required_argument, 0, 'd' },
108 { "list", no_argument, 0, 'l' },
109 { "help", no_argument, 0, 'h' },
110 { "outfile", required_argument, 0, 'o' },
111 { "start", no_argument, 0, 'r' },
112 { "stop", no_argument, 0, 't' },
113 { "ops", required_argument, 0, 'p' },
114 { "force", no_argument, 0, 'f' },
115 { 0, 0, 0, 0 }
116};
117
118static bool file_exists(const char *path)
119{
120 vfs_stat_t stats;
121 if (vfs_stat_path(path, &stats) != EOK) {
122 return false;
123 } else {
124 return true;
125 }
126}
127
128static void usage(void)
129{
130 printf("Usage:\n"
131 NAME " --list | -l \n"
132 "\tList of devices\n"
133 NAME " --start | -r --device= | -d <device number from list> --outfile= | -o <outfile> --ops= | p <ops index>\n"
134 "\tPackets dumped from device will be written to <outfile>\n"
135 NAME " --stop | -t --device= | -d <device number from list>\n"
136 "\tDumping from <device> stops\n"
137 NAME " --start | -r --outfile= | -o <outfile>\n"
138 "\tPackets dumped from the 0. device from the list will be written to <outfile>\n"
139 NAME " --help | -h\n"
140 "\tShow this application help.\n"
141 NAME " --force | -f"
142 "\tTo open existing file and write to it.\n");
143}
144
145int main(int argc, char *argv[])
146{
147 bool start = false;
148 bool stop = false;
149 int dev_number = DEFAULT_DEV_NUM;
150 int ops_number = DEFAULT_FILE_OPS;
151 bool forced = false;
152 const char *output_file_name = "";
153 int idx = 0;
154 int ret = 0;
155 if (argc == 1) {
156 usage();
157 return 0;
158 }
159 while (ret != -1) {
160 ret = getopt_long(argc, argv, "A:N:T:U:d:lho:rtp:f", opts, &idx);
161 switch (ret) {
162 case 'd':
163 char *rest;
164 long dev_result = strtol(optarg, &rest, DECIMAL_SYSTEM);
165 dev_number = (int)dev_result;
166 errno_t rc = pcapctl_is_valid_device(&dev_number);
167 if (rc != EOK) {
168 printf("Device with index %d not found\n", dev_number);
169 return 1;
170 }
171 break;
172 case 'A':
173 output_file_name = optarg;
174 if (file_exists(output_file_name)) {
175 ops_number = APPEND_FILE_OPS;
176 }
177 break;
178 case 'N':
179 output_file_name = optarg;
180 break;
181 case 'T':
182 output_file_name = optarg;
183 ops_number = SHORT_FILE_OPS;
184 break;
185 case 'U':
186 output_file_name = optarg;
187 ops_number = USB_FILE_OPS;
188 break;
189 case 'l':
190 list_devs();
191 return 0;
192 case 'h':
193 usage();
194 return 0;
195 case 'o':
196 output_file_name = optarg;
197 break;
198 case 'r':
199 start = true;
200 break;
201 case 't':
202 stop = true;
203 break;
204 case 'p':
205 char *ops_inval;
206 long ops_result = strtol(optarg, &ops_inval, DECIMAL_SYSTEM);
207 ops_number = (int)ops_result;
208 break;
209 case 'f':
210 forced = true;
211 break;
212 }
213 }
214
215 if (!str_cmp(output_file_name, "") && start) {
216 printf("Dumping destination was not specified. Specify with --outfile | -o\n");
217 return 1;
218 }
219
220 printf("%s: HelenOS Packet Dumping utility: device - %d, ops - %d.\n", NAME, dev_number, ops_number);
221
222 if (start) {
223
224 if (file_exists(output_file_name) && !forced && ops_number != 2) {
225 printf("File %s already exists. If you want to overwrite to it, then use flag --force.\n", output_file_name);
226 return 0;
227 }
228
229 /* start with dev number and name */
230 start_dumping(&dev_number, output_file_name, &ops_number);
231 } else if (stop) {
232 /* stop with dev number */
233 stop_dumping(&dev_number);
234 } else {
235 usage();
236 return 1;
237 }
238 return 0;
239}
240
241/** @}
242 */
Note: See TracBrowser for help on using the repository browser.