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

Last change on this file since e55d2c1 was 6c1e7c0, checked in by Nataliia Korop <n.corop08@…>, 18 months ago

fix app help

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