source: mainline/uspace/app/pcapctl/main.c@ 28ed2d89

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

add force option for existing files

  • Property mode set to 100644
File size: 5.2 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 DEFAULT_OPS_NUM 0
47#define DECIMAL_SYSTEM 10
48
49static errno_t start_dumping(int *dev_number, const char *name, int *ops_index)
50{
51 pcapctl_sess_t *sess = NULL;
52 errno_t rc = pcapctl_dump_open(dev_number, &sess);
53 if (rc != EOK) {
54 return 1;
55 }
56
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);
66 if (rc != EOK) {
67 printf("Starting the dumping was not successful.\n");
68 }
69 pcapctl_dump_close(sess);
70 return EOK;
71}
72
73static errno_t stop_dumping(int *dev_number)
74{
75 pcapctl_sess_t *sess = NULL;
76 errno_t rc = pcapctl_dump_open(dev_number, &sess);
77 if (rc != EOK) {
78 return 1;
79 }
80 rc = pcapctl_dump_stop(sess);
81 if (rc != EOK) {
82 printf("Stoping the dumping was not successful.\n");
83 }
84 pcapctl_dump_close(sess);
85 return EOK;
86}
87
88static void list_devs(void)
89{
90 pcapctl_list();
91}
92
93/**
94 * Array of supported commandline options
95 */
96static const struct option opts[] = {
97 { "device", required_argument, 0, 'd' },
98 { "list", no_argument, 0, 'l' },
99 { "help", no_argument, 0, 'h' },
100 { "outfile", required_argument, 0, 'o' },
101 { "start", no_argument, 0, 'r' },
102 { "stop", no_argument, 0, 't' },
103 { "ops", required_argument, 0, 'p' },
104 { "force", no_argument, 0, 'f'},
105 { 0, 0, 0, 0 }
106};
107
108static 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
118static void usage(void)
119{
120 printf("Usage:\n"
121 NAME " --list | -l \n"
122 "\tList of devices\n"
123 NAME " --start | -r --device= | -d <device number from list> --outfile= | -f <outfile>\n"
124 "\tPackets dumped from device will be written to <outfile>\n"
125 NAME " --stop | -t --device= | -d <device>\n"
126 "\tDumping from <device> stops\n"
127 NAME " --start | -s --outfile= | -f <outfile>\n"
128 "\tPackets dumped from the 0. device from the list will be written to <outfile>\n"
129 NAME " --help | -h\n"
130 "\tShow this application help.\n");
131}
132
133int main(int argc, char *argv[])
134{
135 bool start = false;
136 bool stop = false;
137 int dev_number = DEFAULT_DEV_NUM;
138 int ops_number = DEFAULT_OPS_NUM;
139 bool forced = false;
140 const char *output_file_name = "";
141 int idx = 0;
142 int ret = 0;
143 if (argc == 1) {
144 usage();
145 return 0;
146 }
147 while (ret != -1) {
148 ret = getopt_long(argc, argv, "d:lho:rtp:f", opts, &idx);
149 switch (ret) {
150 case 'd':
151 char *rest;
152 long dev_result = strtol(optarg, &rest, DECIMAL_SYSTEM);
153 dev_number = (int)dev_result;
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;
158 }
159 break;
160 case 'l':
161 list_devs();
162 return 0;
163 case 'h':
164 usage();
165 return 0;
166 case 'o':
167 output_file_name = optarg;
168 break;
169 case 'r':
170 start = true;
171 break;
172 case 't':
173 stop = true;
174 break;
175 case 'p':
176 char* ops_inval;
177 long ops_result = strtol(optarg, &ops_inval, DECIMAL_SYSTEM);
178 ops_number = (int)ops_result;
179 break;
180 case 'f':
181 forced = true;
182 break;
183 }
184 }
185
186 printf("%s: HelenOS Packet Dumping utility: device - %d.\n", NAME, dev_number);
187
188 if (start) {
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
196 /* start with dev number and name */
197 start_dumping(&dev_number, output_file_name, &ops_number);
198 } else if (stop) {
199 /* stop with dev number */
200 stop_dumping(&dev_number);
201 } else {
202 usage();
203 }
204 return 0;
205}
206
207/** @}
208 */
Note: See TracBrowser for help on using the repository browser.