source: mainline/uspace/app/pcapctl/main.c@ 03cd7a9e

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

refactoring after 23.10

  • Property mode set to 100644
File size: 4.2 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
41#include "pcapctl_dump.h"
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
[8765039]54 pcapctl_dump_start(name, sess);
55 pcapctl_dump_close(sess);
[91042127]56 return EOK;
[17a8fcf]57}
58
[9e26790]59static errno_t stop_dumping(int *dev_number)
[17a8fcf]60{
[2ebbe9b]61 pcapctl_sess_t *sess = NULL;
[9e26790]62 errno_t rc = pcapctl_dump_open(dev_number, &sess);
[91042127]63 if (rc != EOK) {
64 return 1;
65 }
[8765039]66 pcapctl_dump_stop(sess);
67 pcapctl_dump_close(sess);
[91042127]68 return EOK;
69}
70
[2ebbe9b]71static void list_devs(void)
72{
[91042127]73 pcapctl_list();
[17a8fcf]74}
75
[9e26790]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
[7924f82]89static void usage(void)
[17a8fcf]90{
[7924f82]91 printf("Usage:\n"
[9e26790]92 NAME " --list | -l \n"
[2ebbe9b]93 "\tList of devices\n"
[fd6845e9]94 NAME " --start | -r --device= | -d <device number from list> --outfile= | -f <outfile>\n"
[2ebbe9b]95 "\tPackets dumped from device will be written to <outfile>\n"
[fd6845e9]96 NAME " --stop | -t --device= | -d <device>\n"
[2ebbe9b]97 "\tDumping from <device> stops\n"
[fd6845e9]98 NAME " --start | -s --outfile= | -f <outfile>\n"
99 "\tPackets dumped from the 0. device from the list will be written to <outfile>\n"
[2ebbe9b]100 NAME " --help | -h\n"
101 "\tShow this application help.\n");
[17a8fcf]102}
103
104int main(int argc, char *argv[])
105{
[9e26790]106 bool start = false;
107 bool stop = false;
[fc2d593]108 int dev_number = -1;
[03cd7a9e]109 const char *output_file_name = "";
[9e26790]110 int idx = 0;
111 int ret = 0;
112 if (argc == 1) {
[7924f82]113 usage();
[9e26790]114 return 0;
115 }
116 while (ret != -1) {
117 ret = getopt_long(argc, argv, "d:lhf:rt", opts, &idx);
118 switch (ret) {
[03cd7a9e]119 case 'd':
120 {
121 char *rest;
122 long result = strtol(optarg, &rest, 10);
123 dev_number = (int)result;
124 errno_t rc = pcapctl_is_valid_device(&dev_number);
125 if (rc != EOK) {
126 printf("Device with index %d not found\n", dev_number);
127 return 1;
128 }
129 break;
130 }
131 case 'l':
132 {
133 list_devs();
134 return 0;
135 }
136 case 'h':
137 {
138 usage();
139 return 0;
140 }
141 case 'f':
142 {
143 output_file_name = optarg;
144 break;
145 }
146 case 'r':
147 {
148 start = true;
149 break;
150 }
151 case 't':
152 { stop = true;
153 break;
[91042127]154 }
[17a8fcf]155 }
156 }
[9e26790]157
[03cd7a9e]158 printf("%s: HelenOS Packet Dumping utility: device - %d.\n", NAME, dev_number);
[9e26790]159
160 if (start) {
[03cd7a9e]161 /* start with dev number and name*/
[9e26790]162 start_dumping(&dev_number, output_file_name);
163 } else if (stop) {
[03cd7a9e]164 /* stop with dev number*/
[9e26790]165 stop_dumping(&dev_number);
166 }
[17a8fcf]167 return 0;
168}
169
170/** @}
171 */
Note: See TracBrowser for help on using the repository browser.