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

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

renaming and cleaning

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