checkAPIs.pl: support for new-style dissectors in check_hf_entries
[metze/wireshark/wip.git] / rawshark.c
index c3bbb7660d5589d09421e14dc718fc70b70ff30d..61875c1472fb816c27cc3dd8c6714bb3e032b06c 100644 (file)
@@ -9,7 +9,7 @@
  * Based on TShark, by Gilbert Ramirez <gram@alumni.rice.edu> and Guy Harris
  * <guy@alum.mit.edu>.
  *
- * SPDX-License-Identifier: GPL-2.0+
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
 /*
@@ -44,7 +44,6 @@
 #endif
 
 #include <glib.h>
-#include <epan/epan-int.h>
 #include <epan/epan.h>
 
 #include <wsutil/cmdarg_err.h>
@@ -71,7 +70,6 @@
 #include "ui/util.h"
 #include "ui/dissect_opts.h"
 #include "ui/failure_message.h"
-#include "epan/register.h"
 #include "conditions.h"
 #include "capture_stop_conditions.h"
 #include <epan/epan_dissect.h>
@@ -91,9 +89,7 @@
 
 #include "caputils/capture-pcap-util.h"
 
-#ifdef HAVE_EXTCAP
 #include "extcap.h"
-#endif
 
 #ifdef HAVE_LIBPCAP
 #include <setjmp.h>
@@ -117,12 +113,11 @@ static const gchar decode_as_arg_template[] = "<layer_type>==<selector>,<decode_
 #define OPEN_ERROR 2
 #define FORMAT_ERROR 2
 
+capture_file cfile;
+
 static guint32 cum_bytes;
-static const frame_data *ref;
 static frame_data ref_frame;
-static frame_data *prev_dis;
 static frame_data prev_dis_frame;
-static frame_data *prev_cap;
 static frame_data prev_cap_frame;
 
 /*
@@ -142,7 +137,7 @@ static gboolean want_pcap_pkthdr;
 cf_status_t raw_cf_open(capture_file *cf, const char *fname);
 static gboolean load_cap_file(capture_file *cf);
 static gboolean process_packet(capture_file *cf, epan_dissect_t *edt, gint64 offset,
-                               struct wtap_pkthdr *whdr, const guchar *pd);
+                               wtap_rec *rec, const guchar *pd);
 static void show_print_file_io_error(int err);
 
 static void failure_warning_message(const char *msg_format, va_list ap);
@@ -167,7 +162,6 @@ typedef struct string_fmt_s {
     string_fmt_e format;    /* Valid if plain is NULL */
 } string_fmt_t;
 
-capture_file cfile;
 int n_rfilters;
 int n_rfcodes;
 dfilter_t *rfcodes[64];
@@ -491,7 +485,7 @@ main(int argc, char *argv[])
      * Attempt to get the pathname of the directory containing the
      * executable file.
      */
-    init_progfile_dir_error = init_progfile_dir(argv[0], main);
+    init_progfile_dir_error = init_progfile_dir(argv[0]);
     if (init_progfile_dir_error != NULL) {
         fprintf(stderr, "rawshark: Can't get pathname of rawshark program: %s.\n",
                 init_progfile_dir_error);
@@ -519,14 +513,13 @@ main(int argc, char *argv[])
     timestamp_set_precision(TS_PREC_AUTO);
     timestamp_set_seconds_type(TS_SECONDS_DEFAULT);
 
-    wtap_init();
+    wtap_init(FALSE);
 
     /* Register all dissectors; we must do this before checking for the
        "-G" flag, as the "-G" flag dumps information registered by the
        dissectors, and we must do it before we read the preferences, in
        case any dissectors register preferences. */
-    if (!epan_init(register_all_protocols, register_all_protocol_handoffs,
-                   NULL, NULL)) {
+    if (!epan_init(NULL, NULL, TRUE)) {
         ret = INIT_ERROR;
         goto clean_exit;
     }
@@ -702,17 +695,13 @@ main(int argc, char *argv[])
             case 'v':        /* Show version and exit */
             {
                 show_version("Rawshark (Wireshark)", comp_info_str, runtime_info_str);
-                g_string_free(comp_info_str, TRUE);
-                g_string_free(runtime_info_str, TRUE);
                 goto clean_exit;
-                break;
             }
             default:
             case '?':        /* Bad flag - print usage message */
                 print_usage(stderr);
                 ret = INVALID_OPTION;
                 goto clean_exit;
-                break;
         }
     }
 
@@ -764,11 +753,6 @@ main(int argc, char *argv[])
     }
 #endif /* _WIN32 */
 
-    /* At this point MATE will have registered its field array so we can
-       have a tap filter with one of MATE's late-registered fields as part
-       of the filter.  We can now process all the "-z" arguments. */
-    start_requested_stats();
-
     /*
      * Enabled and disabled protocols and heuristic dissectors as per
      * command-line options.
@@ -837,11 +821,12 @@ main(int argc, char *argv[])
     }
 
 clean_exit:
+    g_free(pipe_name);
+    g_string_free(comp_info_str, TRUE);
+    g_string_free(runtime_info_str, TRUE);
     epan_free(cfile.epan);
     epan_cleanup();
-#ifdef HAVE_EXTCAP
     extcap_cleanup();
-#endif
     wtap_cleanup();
     return ret;
 }
@@ -851,14 +836,13 @@ clean_exit:
  * packet header followed by the payload.
  * @param pd [IN] A POSIX file descriptor.  Because that's _exactly_ the sort
  *           of thing you want to use in Windows.
- * @param phdr [OUT] Packet header information.
  * @param err [OUT] Error indicator.  Uses wiretap values.
  * @param err_info [OUT] Error message.
  * @param data_offset [OUT] data offset in the pipe.
  * @return TRUE on success, FALSE on failure.
  */
 static gboolean
-raw_pipe_read(struct wtap_pkthdr *phdr, guchar * pd, int *err, gchar **err_info, gint64 *data_offset) {
+raw_pipe_read(wtap_rec *rec, guchar * pd, int *err, gchar **err_info, gint64 *data_offset) {
     struct pcap_pkthdr mem_hdr;
     struct pcaprec_hdr disk_hdr;
     ssize_t bytes_read = 0;
@@ -906,27 +890,29 @@ raw_pipe_read(struct wtap_pkthdr *phdr, guchar * pd, int *err, gchar **err_info,
         ptr += bytes_read;
     }
 
+    rec->rec_type = REC_TYPE_PACKET;
+    rec->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
     if (want_pcap_pkthdr) {
-        phdr->ts.secs = mem_hdr.ts.tv_sec;
-        phdr->ts.nsecs = (gint32)mem_hdr.ts.tv_usec * 1000;
-        phdr->caplen = mem_hdr.caplen;
-        phdr->len = mem_hdr.len;
+        rec->ts.secs = mem_hdr.ts.tv_sec;
+        rec->ts.nsecs = (gint32)mem_hdr.ts.tv_usec * 1000;
+        rec->rec_header.packet_header.caplen = mem_hdr.caplen;
+        rec->rec_header.packet_header.len = mem_hdr.len;
     } else {
-        phdr->ts.secs = disk_hdr.ts_sec;
-        phdr->ts.nsecs = disk_hdr.ts_usec * 1000;
-        phdr->caplen = disk_hdr.incl_len;
-        phdr->len = disk_hdr.orig_len;
+        rec->ts.secs = disk_hdr.ts_sec;
+        rec->ts.nsecs = disk_hdr.ts_usec * 1000;
+        rec->rec_header.packet_header.caplen = disk_hdr.incl_len;
+        rec->rec_header.packet_header.len = disk_hdr.orig_len;
     }
-    bytes_needed = phdr->caplen;
+    bytes_needed = rec->rec_header.packet_header.caplen;
 
-    phdr->pkt_encap = encap;
+    rec->rec_header.packet_header.pkt_encap = encap;
 
 #if 0
     printf("mem_hdr: %lu disk_hdr: %lu\n", sizeof(mem_hdr), sizeof(disk_hdr));
-    printf("tv_sec: %u (%04x)\n", (unsigned int) phdr->ts.secs, (unsigned int) phdr->ts.secs);
-    printf("tv_nsec: %d (%04x)\n", phdr->ts.nsecs, phdr->ts.nsecs);
-    printf("caplen: %d (%04x)\n", phdr->caplen, phdr->caplen);
-    printf("len: %d (%04x)\n", phdr->len, phdr->len);
+    printf("tv_sec: %u (%04x)\n", (unsigned int) rec->ts.secs, (unsigned int) rec->ts.secs);
+    printf("tv_nsec: %d (%04x)\n", rec->ts.nsecs, rec->ts.nsecs);
+    printf("caplen: %d (%04x)\n", rec->rec_header.packet_header.caplen, rec->rec_header.packet_header.caplen);
+    printf("len: %d (%04x)\n", rec->rec_header.packet_header.len, rec->rec_header.packet_header.len);
 #endif
     if (bytes_needed > WTAP_MAX_PACKET_SIZE_STANDARD) {
         *err = WTAP_ERR_BAD_FILE;
@@ -962,21 +948,21 @@ load_cap_file(capture_file *cf)
     gint64       data_offset = 0;
 
     guchar      *pd;
-    struct wtap_pkthdr phdr;
+    wtap_rec     rec;
     epan_dissect_t edt;
 
-    wtap_phdr_init(&phdr);
+    wtap_rec_init(&rec);
 
     epan_dissect_init(&edt, cf->epan, TRUE, FALSE);
 
     pd = (guchar*)g_malloc(WTAP_MAX_PACKET_SIZE_STANDARD);
-    while (raw_pipe_read(&phdr, pd, &err, &err_info, &data_offset)) {
-        process_packet(cf, &edt, data_offset, &phdr, pd);
+    while (raw_pipe_read(&rec, pd, &err, &err_info, &data_offset)) {
+        process_packet(cf, &edt, data_offset, &rec, pd);
     }
 
     epan_dissect_cleanup(&edt);
 
-    wtap_phdr_cleanup(&phdr);
+    wtap_rec_cleanup(&rec);
     g_free(pd);
     if (err != 0) {
         /* Print a message noting that the read failed somewhere along the line. */
@@ -989,20 +975,20 @@ load_cap_file(capture_file *cf)
 
 static gboolean
 process_packet(capture_file *cf, epan_dissect_t *edt, gint64 offset,
-               struct wtap_pkthdr *whdr, const guchar *pd)
+               wtap_rec *rec, const guchar *pd)
 {
     frame_data fdata;
     gboolean passed;
     int i;
 
-    if(whdr->len == 0)
+    if(rec->rec_header.packet_header.len == 0)
     {
         /* The user sends an empty packet when he wants to get output from us even if we don't currently have
            packets to process. We spit out a line with the timestamp and the text "void"
         */
         printf("%lu %lu %lu void -\n", (unsigned long int)cf->count,
-               (unsigned long int)whdr->ts.secs,
-               (unsigned long int)whdr->ts.nsecs);
+               (unsigned long int)rec->ts.secs,
+               (unsigned long int)rec->ts.nsecs);
 
         fflush(stdout);
 
@@ -1015,7 +1001,7 @@ process_packet(capture_file *cf, epan_dissect_t *edt, gint64 offset,
     /* If we're going to print packet information, or we're going to
        run a read filter, or we're going to process taps, set up to
        do a dissection and do so. */
-    frame_data_init(&fdata, cf->count, whdr, offset, cum_bytes);
+    frame_data_init(&fdata, cf->count, rec, offset, cum_bytes);
 
     passed = TRUE;
 
@@ -1030,24 +1016,26 @@ process_packet(capture_file *cf, epan_dissect_t *edt, gint64 offset,
     printf("%lu", (unsigned long int) cf->count);
 
     frame_data_set_before_dissect(&fdata, &cf->elapsed_time,
-                                  &ref, prev_dis);
+                                  &cf->provider.ref, cf->provider.prev_dis);
 
-    if (ref == &fdata) {
+    if (cf->provider.ref == &fdata) {
        ref_frame = fdata;
-       ref = &ref_frame;
+       cf->provider.ref = &ref_frame;
     }
 
     /* We only need the columns if we're printing packet info but we're
      *not* verbose; in verbose mode, we print the protocol tree, not
      the protocol summary. */
-    epan_dissect_run_with_taps(edt, cf->cd_t, whdr, frame_tvbuff_new(&fdata, pd), &fdata, &cf->cinfo);
+    epan_dissect_run_with_taps(edt, cf->cd_t, rec,
+                               frame_tvbuff_new(&cf->provider, &fdata, pd),
+                               &fdata, &cf->cinfo);
 
     frame_data_set_after_dissect(&fdata, &cum_bytes);
     prev_dis_frame = fdata;
-    prev_dis = &prev_dis_frame;
+    cf->provider.prev_dis = &prev_dis_frame;
 
     prev_cap_frame = fdata;
-    prev_cap = &prev_cap_frame;
+    cf->provider.prev_cap = &prev_cap_frame;
 
     for(i = 0; i < n_rfilters; i++) {
         /* Run the read filter if we have one. */
@@ -1208,7 +1196,7 @@ static gboolean print_field_value(field_info *finfo, int cmd_line_index)
                                 DISSECTOR_ASSERT(!hfinfo->bitmask);
                                 svalue = fvalue_get_sinteger(&finfo->value);
                                 if (hfinfo->display & BASE_RANGE_STRING) {
-                                    g_string_append(label_s, rval_to_str_const(svalue, RVALS(hfinfo->strings), "Unknown"));
+                                    g_string_append(label_s, rval_to_str_const(svalue, (const range_string *) hfinfo->strings, "Unknown"));
                                 } else if (hfinfo->display & BASE_EXT_STRING) {
                                     g_string_append(label_s, val_to_str_ext_const(svalue, (value_string_ext *) hfinfo->strings, "Unknown"));
                                 } else {
@@ -1232,7 +1220,7 @@ static gboolean print_field_value(field_info *finfo, int cmd_line_index)
                                 DISSECTOR_ASSERT(!hfinfo->bitmask);
                                 uvalue = fvalue_get_uinteger(&finfo->value);
                                 if (!hfinfo->bitmask && hfinfo->display & BASE_RANGE_STRING) {
-                                    g_string_append(label_s, rval_to_str_const(uvalue, RVALS(hfinfo->strings), "Unknown"));
+                                    g_string_append(label_s, rval_to_str_const(uvalue, (const range_string *) hfinfo->strings, "Unknown"));
                                 } else if (hfinfo->display & BASE_EXT_STRING) {
                                     g_string_append(label_s, val_to_str_ext_const(uvalue, (value_string_ext *) hfinfo->strings, "Unknown"));
                                 } else {
@@ -1332,7 +1320,7 @@ protocolinfo_init(char *field)
     rs->filter=field;
     rs->cmd_line_index = g_cmd_line_index++;
 
-    error_string=register_tap_listener("frame", rs, rs->filter, TL_REQUIRES_PROTO_TREE, NULL, protocolinfo_packet, NULL);
+    error_string=register_tap_listener("frame", rs, rs->filter, TL_REQUIRES_PROTO_TREE, NULL, protocolinfo_packet, NULL, NULL);
     if(error_string){
         /* error, we failed to attach to the tap. complain and clean up */
         fprintf(stderr, "rawshark: Couldn't register field extraction tap: %s\n",
@@ -1466,16 +1454,16 @@ open_failure_message(const char *filename, int err, gboolean for_writing)
 }
 
 static const nstime_t *
-raw_get_frame_ts(void *data _U_, guint32 frame_num)
+raw_get_frame_ts(struct packet_provider_data *prov, guint32 frame_num)
 {
-    if (ref && ref->num == frame_num)
-        return &ref->abs_ts;
+    if (prov->ref && prov->ref->num == frame_num)
+        return &prov->ref->abs_ts;
 
-    if (prev_dis && prev_dis->num == frame_num)
-        return &prev_dis->abs_ts;
+    if (prov->prev_dis && prov->prev_dis->num == frame_num)
+        return &prov->prev_dis->abs_ts;
 
-    if (prev_cap && prev_cap->num == frame_num)
-        return &prev_cap->abs_ts;
+    if (prov->prev_cap && prov->prev_cap->num == frame_num)
+        return &prov->prev_cap->abs_ts;
 
     return NULL;
 }
@@ -1483,15 +1471,14 @@ raw_get_frame_ts(void *data _U_, guint32 frame_num)
 static epan_t *
 raw_epan_new(capture_file *cf)
 {
-    epan_t *epan = epan_new();
-
-    epan->data = cf;
-    epan->get_frame_ts = raw_get_frame_ts;
-    epan->get_interface_name = cap_file_get_interface_name;
-    epan->get_interface_description = cap_file_get_interface_description;
-    epan->get_user_comment = NULL;
+    static const struct packet_provider_funcs funcs = {
+        raw_get_frame_ts,
+        cap_file_provider_get_interface_name,
+        cap_file_provider_get_interface_description,
+        NULL,
+    };
 
-    return epan;
+    return epan_new(&cf->provider, &funcs);
 }
 
 cf_status_t
@@ -1506,7 +1493,7 @@ raw_cf_open(capture_file *cf, const char *fname)
     epan_free(cf->epan);
     cf->epan = raw_epan_new(cf);
 
-    cf->wth = NULL;
+    cf->provider.wth = NULL;
     cf->f_datalen = 0; /* not used, but set it anyway */
 
     /* Set the file name because we need it to set the follow stream filter.
@@ -1527,9 +1514,9 @@ raw_cf_open(capture_file *cf, const char *fname)
     cf->drops     = 0;
     cf->snap      = 0;
     nstime_set_zero(&cf->elapsed_time);
-    ref = NULL;
-    prev_dis = NULL;
-    prev_cap = NULL;
+    cf->provider.ref = NULL;
+    cf->provider.prev_dis = NULL;
+    cf->provider.prev_cap = NULL;
 
     return CF_OK;
 }