source: mainline/uspace/app/pcapctl/main.c@ 2ebbe9b

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

nic, drvs, pcapctl

  • Property mode set to 100644
File size: 3.6 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
39#include "pcapctl_dump.h"
40
41#define NAME "pcapctl"
42#define DEFAULT_DEV_NUM 0
43
44//pcapctl_sess_t* sess = NULL;
45
46static errno_t start_dumping(const char *svc_name, const char *name)
47{
48 pcapctl_sess_t *sess = NULL;
49 errno_t rc = pcapctl_dump_open(svc_name, &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
59/** Session might */
60static errno_t stop_dumping(const char *svc_name)
61{
62 pcapctl_sess_t *sess = NULL;
63 errno_t rc = pcapctl_dump_open(svc_name, &sess);
64 if (rc != EOK) {
65 return 1;
66 }
67 pcapctl_dump_stop(sess);
68 pcapctl_dump_close(sess);
69 return EOK;
70}
71
72static void list_devs(void)
73{
74 pcapctl_list();
75}
76
77static void usage(void)
78{
79 printf("Usage:\n"
80 NAME " list \n"
81 "\tList of devices\n"
82 NAME " start --device= | -d <device number from list> <outfile>\n"
83 "\tPackets dumped from device will be written to <outfile>\n"
84 NAME " stop --device= | -d <device>\n"
85 "\tDumping from <device> stops\n"
86 NAME " start <outfile>\n"
87 "\tPackets dumped from the 1st device from the list will be written to <outfile>\n"
88 NAME " --help | -h\n"
89 "\tShow this application help.\n");
90}
91
92int main(int argc, char *argv[])
93{
94 if (argc < 2) {
95 usage();
96 return 1;
97 } else {
98 /** help */
99 if (str_cmp(argv[1], "--help") == 0 || str_cmp(argv[1], "-h") == 0) {
100 usage();
101 return 0;
102 /** list */
103 } else if (str_cmp(argv[1], "list") == 0) {
104 list_devs();
105 return 0;
106 /** start with/out devnum */
107 } else if (str_cmp(argv[1], "start") == 0) {
108 if (argc == 3) {
109 start_dumping((char *)"0", argv[2]);
110 return 0;
111 } else if (argc == 4) {
112 start_dumping(argv[2], argv[3]);
113 return 0;
114 } else {
115 usage();
116 return 1;
117 }
118 /** Stop with/out devnum */
119 } else if (str_cmp(argv[1], "stop") == 0) {
120 if (argc == 2) {
121 stop_dumping((char *)"0");
122 fprintf(stdout, "Dumping was stopped\n");
123 return 0;
124 } else if (argc == 3) {
125
126 stop_dumping(argv[2]);
127 fprintf(stdout, "Dumping was stopped\n");
128 } else {
129 usage();
130 return 1;
131 }
132 } else {
133 usage();
134 return 1;
135 }
136 }
137 return 0;
138}
139
140/** @}
141 */
Note: See TracBrowser for help on using the repository browser.