[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>
|
---|
| 38 |
|
---|
| 39 | #include "pcapctl_dump.h"
|
---|
| 40 |
|
---|
| 41 | #define NAME "pcapctl"
|
---|
| 42 |
|
---|
| 43 | #define LOGGER(msg, ...) \
|
---|
| 44 | fprintf(stderr, \
|
---|
| 45 | "[PCAP %s:%d]: " msg "\n", \
|
---|
| 46 | __FILE__, __LINE__, \
|
---|
| 47 | ##__VA_ARGS__\
|
---|
| 48 | )
|
---|
| 49 |
|
---|
| 50 | pcapctl_sess_t sess;
|
---|
| 51 |
|
---|
[91042127] | 52 | static errno_t start_dumping(const char *drv_name, const char *name)
|
---|
[17a8fcf] | 53 | {
|
---|
[91042127] | 54 | errno_t rc = pcapctl_dump_init(&sess, drv_name);
|
---|
| 55 | if (rc != EOK) {
|
---|
| 56 | //fprintf(stderr, "Error initializing ...\n");
|
---|
| 57 | return 1;
|
---|
| 58 | }
|
---|
[17a8fcf] | 59 | pcapctl_dump_start(name, &sess);
|
---|
[91042127] | 60 | return EOK;
|
---|
[17a8fcf] | 61 | }
|
---|
| 62 |
|
---|
[91042127] | 63 | /** Session might */
|
---|
| 64 | static errno_t stop_dumping(const char *drv_name)
|
---|
[17a8fcf] | 65 | {
|
---|
[91042127] | 66 | errno_t rc = pcapctl_dump_init(&sess, drv_name);
|
---|
| 67 | if (rc != EOK) {
|
---|
| 68 | fprintf(stderr, "Error initializing ...\n");
|
---|
| 69 | return 1;
|
---|
| 70 | }
|
---|
[17a8fcf] | 71 | pcapctl_dump_stop(&sess);
|
---|
[91042127] | 72 | return EOK;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | static void list_devs(void) {
|
---|
| 76 | pcapctl_list();
|
---|
[17a8fcf] | 77 | }
|
---|
| 78 |
|
---|
| 79 | static void usage(const char *progname)
|
---|
| 80 | {
|
---|
| 81 | fprintf(stderr, "Usage:\n");
|
---|
[91042127] | 82 | fprintf(stderr, " %s start <device> <outfile>: Packets dumped from <device> will be written to <outfile>\n", progname);
|
---|
| 83 | fprintf(stderr, " %s stop <device>: Dumping from <device> stops\n", progname);
|
---|
[17a8fcf] | 84 |
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | int main(int argc, char *argv[])
|
---|
| 88 | {
|
---|
| 89 | if (argc < 2) {
|
---|
| 90 | usage(argv[0]);
|
---|
| 91 | return 1;
|
---|
| 92 | } else {
|
---|
[91042127] | 93 | if (str_cmp(argv[1], "--help") == 0 || str_cmp(argv[1], "-h") == 0) {
|
---|
| 94 | usage(argv[0]);
|
---|
| 95 | return 0;
|
---|
| 96 | } else if (str_cmp(argv[1], "list") == 0) {
|
---|
| 97 | list_devs();
|
---|
| 98 | return 0;
|
---|
| 99 | } else if (str_cmp(argv[1], "start") == 0) {
|
---|
| 100 | if (argc != 4) {
|
---|
[17a8fcf] | 101 | usage(argv[0]);
|
---|
| 102 | return 1;
|
---|
| 103 | }
|
---|
[91042127] | 104 | start_dumping(argv[2], argv[3]);
|
---|
[17a8fcf] | 105 | } else if (str_cmp(argv[1], "stop") == 0) {
|
---|
[91042127] | 106 | if (argc != 3) {
|
---|
| 107 | usage(argv[0]);
|
---|
| 108 | return 1;
|
---|
| 109 | }
|
---|
| 110 | stop_dumping(argv[2]);
|
---|
[17a8fcf] | 111 | fprintf(stdout, "Dumping was stopped\n");
|
---|
| 112 | return EOK;
|
---|
| 113 | } else {
|
---|
| 114 | usage(argv[0]);
|
---|
| 115 | return 1;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | return 0;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | /** @}
|
---|
| 122 | */
|
---|