Proto tree: Update left arrow navigation behavior.
[metze/wireshark/wip.git] / extcap / ciscodump.c
index 3d5676cac15756e12cfe9eee6362ae3f7d385feb..1c8f40f33ba4539788737eede5a0fabb74d13a83 100644 (file)
@@ -7,25 +7,16 @@
  * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: GPL-2.0+
  */
 
 #include "config.h"
 
 #include <extcap/extcap-base.h>
 #include <wsutil/interface.h>
+#include <wsutil/strtoi.h>
+#include <wsutil/filesystem.h>
+#include <wsutil/glib-compat.h>
 #include <extcap/ssh-base.h>
 #include <writecap/pcapio.h>
 
@@ -40,7 +31,7 @@
 /* The read timeout in msec */
 #define CISCODUMP_READ_TIMEOUT 3000
 
-#define CISCODUMP_EXTCAP_INTERFACE "cisco"
+#define CISCODUMP_EXTCAP_INTERFACE "ciscodump"
 #define SSH_READ_BLOCK_SIZE 1024
 #define SSH_READ_TIMEOUT 10000
 
@@ -153,7 +144,7 @@ static void ciscodump_cleanup(ssh_session sshs, ssh_channel channel, const char*
        ssh_cleanup(&sshs, &channel);
 }
 
-static int wait_until_data(ssh_channel channel, const long unsigned count)
+static int wait_until_data(ssh_channel channel, const guint32 count)
 {
        long unsigned got = 0;
        char output[SSH_READ_BLOCK_SIZE];
@@ -181,7 +172,7 @@ static int wait_until_data(ssh_channel channel, const long unsigned count)
        return EXIT_SUCCESS;
 }
 
-static int parse_line(char* packet _U_, unsigned* offset, char* line, int status)
+static int parse_line(char* packet, unsigned* offset, char* line, int status)
 {
        char** parts;
        char** part;
@@ -208,7 +199,7 @@ static int parse_line(char* packet _U_, unsigned* offset, char* line, int status
        /* ABCDEF01: 01020304 05060708 090A0B0C 0D0E0F10 ................                       */
        /* Note that any of the 4 groups are optional and that a group can be 1 to 4 bytes long */
        parts = g_regex_split_simple(
-               "^[\\dA-Z]{8,8}:\\s+([\\dA-Z]{2,8})\\s+([\\dA-Z]{2,8}){0,1}\\s+([\\dA-Z]{2,8}){0,1}\\s+([\\dA-Z]{2,8}){0,1}.*",
+               "^[\\dA-F]{8,8}:\\s+([\\dA-F]{2,8})\\s+([\\dA-F]{2,8}){0,1}\\s+([\\dA-F]{2,8}){0,1}\\s+([\\dA-F]{2,8}){0,1}.*",
                line, G_REGEX_CASELESS, G_REGEX_MATCH_ANCHORED);
 
        part = parts;
@@ -222,10 +213,11 @@ static int parse_line(char* packet _U_, unsigned* offset, char* line, int status
                }
                part++;
        }
+       g_strfreev(parts);
        return CISCODUMP_PARSER_IN_PACKET;
 }
 
-static void ssh_loop_read(ssh_channel channel, FILE* fp, const long unsigned count)
+static void ssh_loop_read(ssh_channel channel, FILE* fp, const guint32 count)
 {
        char line[SSH_READ_BLOCK_SIZE];
        char chr;
@@ -307,7 +299,7 @@ static int check_ios_version(ssh_channel channel)
        return FALSE;
 }
 
-static ssh_channel run_capture(ssh_session sshs, const char* iface, const char* cfilter, const unsigned long int count)
+static ssh_channel run_capture(ssh_session sshs, const char* iface, const char* cfilter, const guint32 count)
 {
        char* cmdline = NULL;
        ssh_channel channel;
@@ -338,7 +330,7 @@ static ssh_channel run_capture(ssh_session sshs, const char* iface, const char*
        if (ssh_channel_printf(channel, "monitor capture buffer %s max-size 9500\n", WIRESHARK_CAPTURE_BUFFER) == EXIT_FAILURE)
                goto error;
 
-       if (ssh_channel_printf(channel, "monitor capture buffer %s limit packet-count %lu\n", WIRESHARK_CAPTURE_BUFFER, count) == EXIT_FAILURE)
+       if (ssh_channel_printf(channel, "monitor capture buffer %s limit packet-count %u\n", WIRESHARK_CAPTURE_BUFFER, count) == EXIT_FAILURE)
                goto error;
 
        if (cfilter) {
@@ -411,7 +403,7 @@ error:
 
 static int ssh_open_remote_connection(const char* hostname, const unsigned int port, const char* username, const char* password,
        const char* sshkey, const char* sshkey_passphrase, const char* iface, const char* cfilter,
-       const unsigned long int count, const char* fifo)
+       const guint32 count, const char* fifo)
 {
        ssh_session sshs;
        ssh_channel channel;
@@ -508,6 +500,8 @@ static int list_config(char *interface, unsigned int remote_port)
                "{type=unsigned}{required=true}{tooltip=The number of remote packets to capture.}\n",
                inc++);
 
+       extcap_config_debug(&inc);
+
        g_free(ipfilter);
 
        return EXIT_SUCCESS;
@@ -517,18 +511,18 @@ int main(int argc, char **argv)
 {
        int result;
        int option_idx = 0;
-       int i;
        char* remote_host = NULL;
-       unsigned int remote_port = 22;
+       guint16 remote_port = 22;
        char* remote_username = NULL;
        char* remote_password = NULL;
        char* remote_interface = NULL;
        char* sshkey = NULL;
        char* sshkey_passphrase = NULL;
        char* remote_filter = NULL;
-       unsigned long int count = 0;
+       guint32 count = 0;
        int ret = EXIT_FAILURE;
        extcap_parameters * extcap_conf = g_new0(extcap_parameters, 1);
+       char* help_url;
        char* help_header = NULL;
 
 #ifdef _WIN32
@@ -537,23 +531,25 @@ int main(int argc, char **argv)
        attach_parent_console();
 #endif  /* _WIN32 */
 
+       help_url = data_file_url("ciscodump.html");
        extcap_base_set_util_info(extcap_conf, argv[0], CISCODUMP_VERSION_MAJOR, CISCODUMP_VERSION_MINOR,
-               CISCODUMP_VERSION_RELEASE, NULL);
+               CISCODUMP_VERSION_RELEASE, help_url);
+       g_free(help_url);
        extcap_base_register_interface(extcap_conf, CISCODUMP_EXTCAP_INTERFACE, "Cisco remote capture", 147, "Remote capture dependent DLT");
 
        help_header = g_strdup_printf(
                " %s --extcap-interfaces\n"
-               " %s --extcap-interface=INTERFACE --extcap-dlts\n"
-               " %s --extcap-interface=INTERFACE --extcap-config\n"
-               " %s --extcap-interface=INTERFACE --remote-host myhost --remote-port 22222 "
+               " %s --extcap-interface=%s --extcap-dlts\n"
+               " %s --extcap-interface=%s --extcap-config\n"
+               " %s --extcap-interface=%s --remote-host myhost --remote-port 22222 "
                "--remote-username myuser --remote-interface gigabit0/0 "
-               "--fifo=FILENAME --capture\n", argv[0], argv[0], argv[0], argv[0]);
+               "--fifo=FILENAME --capture\n", argv[0], argv[0], CISCODUMP_EXTCAP_INTERFACE, argv[0],
+               CISCODUMP_EXTCAP_INTERFACE, argv[0], CISCODUMP_EXTCAP_INTERFACE);
        extcap_help_add_header(extcap_conf, help_header);
        g_free(help_header);
 
        extcap_help_add_option(extcap_conf, "--help", "print this help");
        extcap_help_add_option(extcap_conf, "--version", "print the version");
-       extcap_help_add_option(extcap_conf, "--verbose", "print more messages");
        extcap_help_add_option(extcap_conf, "--remote-host <host>", "the remote SSH host");
        extcap_help_add_option(extcap_conf, "--remote-port <port>", "the remote SSH port (default: 22)");
        extcap_help_add_option(extcap_conf, "--remote-username <username>", "the remote SSH username (default: the current user)");
@@ -573,9 +569,6 @@ int main(int argc, char **argv)
                goto end;
        }
 
-       for (i = 0; i < argc; i++)
-               g_debug("%s ", argv[i]);
-
        while ((result = getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) {
 
                switch (result) {
@@ -595,8 +588,7 @@ int main(int argc, char **argv)
                        break;
 
                case OPT_REMOTE_PORT:
-                       remote_port = (unsigned int)strtoul(optarg, NULL, 10);
-                       if (remote_port > 65535 || remote_port == 0) {
+                       if (!ws_strtou16(optarg, NULL, &remote_port) || remote_port == 0) {
                                g_warning("Invalid port: %s", optarg);
                                goto end;
                        }
@@ -635,7 +627,10 @@ int main(int argc, char **argv)
                        break;
 
                case OPT_REMOTE_COUNT:
-                       count = strtoul(optarg, NULL, 10);
+                       if (!ws_strtou32(optarg, NULL, &count)) {
+                               g_warning("Invalid packet count: %s", optarg);
+                               goto end;
+                       }
                        break;
 
                case ':':
@@ -651,6 +646,8 @@ int main(int argc, char **argv)
                }
        }
 
+       extcap_cmdline_debug(argv, argc);
+
        if (optind != argc) {
                g_warning("Unexpected extra option: %s", argv[optind]);
                goto end;