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 |
|
---|
46 | static 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 | rc = pcapctl_dump_start(name, sess);
|
---|
55 | if (rc != EOK) {
|
---|
56 | printf("Starting the dumping was not successful.\n");
|
---|
57 | }
|
---|
58 | pcapctl_dump_close(sess);
|
---|
59 | return EOK;
|
---|
60 | }
|
---|
61 |
|
---|
62 | static errno_t stop_dumping(int *dev_number)
|
---|
63 | {
|
---|
64 | pcapctl_sess_t *sess = NULL;
|
---|
65 | errno_t rc = pcapctl_dump_open(dev_number, &sess);
|
---|
66 | if (rc != EOK) {
|
---|
67 | return 1;
|
---|
68 | }
|
---|
69 | rc = pcapctl_dump_stop(sess);
|
---|
70 | if (rc != EOK) {
|
---|
71 | printf("Stoping the dumping was not successful.\n");
|
---|
72 | }
|
---|
73 | pcapctl_dump_close(sess);
|
---|
74 | return EOK;
|
---|
75 | }
|
---|
76 |
|
---|
77 | static 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);
|
---|
81 | if (rc != EOK) {
|
---|
82 | return rc;
|
---|
83 | }
|
---|
84 |
|
---|
85 | rc = pcapctl_dump_set_ops(name, sess);
|
---|
86 | if (rc != EOK) {
|
---|
87 | printf("Setting dumper ops was not successful.\n");
|
---|
88 | }
|
---|
89 | pcapctl_dump_close(sess);
|
---|
90 | return EOK;
|
---|
91 | }
|
---|
92 |
|
---|
93 | static void list_devs(void)
|
---|
94 | {
|
---|
95 | pcapctl_list();
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Array of supported commandline options
|
---|
100 | */
|
---|
101 | static 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' },
|
---|
108 | { "ops", required_argument, 0, 'o' },
|
---|
109 | { 0, 0, 0, 0 }
|
---|
110 | };
|
---|
111 |
|
---|
112 | static void usage(void)
|
---|
113 | {
|
---|
114 | printf("Usage:\n"
|
---|
115 | NAME " --list | -l \n"
|
---|
116 | "\tList of devices\n"
|
---|
117 | NAME " --start | -r --device= | -d <device number from list> --outfile= | -f <outfile>\n"
|
---|
118 | "\tPackets dumped from device will be written to <outfile>\n"
|
---|
119 | NAME " --stop | -t --device= | -d <device>\n"
|
---|
120 | "\tDumping from <device> stops\n"
|
---|
121 | NAME " --start | -s --outfile= | -f <outfile>\n"
|
---|
122 | "\tPackets dumped from the 0. device from the list will be written to <outfile>\n"
|
---|
123 | NAME " --help | -h\n"
|
---|
124 | "\tShow this application help.\n");
|
---|
125 | }
|
---|
126 |
|
---|
127 | int main(int argc, char *argv[])
|
---|
128 | {
|
---|
129 | bool start = false;
|
---|
130 | bool stop = false;
|
---|
131 | bool set_ops = false;
|
---|
132 | int dev_number = -1;
|
---|
133 | const char *output_file_name = "";
|
---|
134 | const char *ops_name = "";
|
---|
135 | int idx = 0;
|
---|
136 | int ret = 0;
|
---|
137 | if (argc == 1) {
|
---|
138 | usage();
|
---|
139 | return 0;
|
---|
140 | }
|
---|
141 | while (ret != -1) {
|
---|
142 | ret = getopt_long(argc, argv, "d:lhf:rt", opts, &idx);
|
---|
143 | switch (ret) {
|
---|
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;
|
---|
152 | }
|
---|
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;
|
---|
169 | case 'o':
|
---|
170 | set_ops = true;
|
---|
171 | ops_name = optarg;
|
---|
172 | break;
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | printf("%s: HelenOS Packet Dumping utility: device - %d.\n", NAME, dev_number);
|
---|
177 |
|
---|
178 | if (start) {
|
---|
179 | /* start with dev number and name */
|
---|
180 | start_dumping(&dev_number, output_file_name);
|
---|
181 | } else if (stop) {
|
---|
182 | /* stop with dev number */
|
---|
183 | stop_dumping(&dev_number);
|
---|
184 | } else if (set_ops) {
|
---|
185 | set_dumper_ops(&dev_number, ops_name);
|
---|
186 | }
|
---|
187 | return 0;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /** @}
|
---|
191 | */
|
---|