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

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

set ops as number and with start req

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