g_fprintf() & g_vfprintf() should not be used....
[obnox/wireshark/wip.git] / dumpcap.c
index 5efab05190bf1af633b8f5486708a2ce75912f2b..d78edea1ab4aa08a7efc1e6cb5b7a04162d11983 100644 (file)
--- a/dumpcap.c
+++ b/dumpcap.c
@@ -337,6 +337,7 @@ console_log_handler(const char *log_domain, GLogLevelFlags log_level,
 static capture_options global_capture_opts;
 static gboolean quiet = FALSE;
 static gboolean use_threads = FALSE;
+static guint64 start_time;
 
 static void capture_loop_write_packet_cb(u_char *pcap_opts_p, const struct pcap_pkthdr *phdr,
                                          const u_char *pd);
@@ -355,6 +356,55 @@ static void report_cfilter_error(capture_options *capture_opts, guint i, const c
 
 #define MSG_MAX_LENGTH 4096
 
+/* Copied from pcapio.c libpcap_write_interface_statistics_block()*/
+static guint64
+create_timestamp(void) {
+    guint64 timestamp;
+#ifdef _WIN32
+    FILETIME now;
+#else
+    struct timeval now;
+#endif
+
+#ifdef _WIN32
+    /*
+     * Current time, represented as 100-nanosecond intervals since
+     * January 1, 1601, 00:00:00 UTC.
+     *
+     * I think DWORD might be signed, so cast both parts of "now"
+     * to guint32 so that the sign bit doesn't get treated specially.
+     */
+    GetSystemTimeAsFileTime(&now);
+    timestamp = (((guint64)(guint32)now.dwHighDateTime) << 32) +
+                (guint32)now.dwLowDateTime;
+
+    /*
+     * Convert to same thing but as 1-microsecond, i.e. 1000-nanosecond,
+     * intervals.
+     */
+    timestamp /= 10;
+
+    /*
+     * Subtract difference, in microseconds, between January 1, 1601
+     * 00:00:00 UTC and January 1, 1970, 00:00:00 UTC.
+     */
+    timestamp -= G_GINT64_CONSTANT(11644473600000000U);
+#else
+    /*
+     * Current time, represented as seconds and microseconds since
+     * January 1, 1970, 00:00:00 UTC.
+     */
+    gettimeofday(&now, NULL);
+
+    /*
+     * Convert to delta in microseconds.
+     */
+    timestamp = (guint64)(now.tv_sec) * 1000000 +
+                (guint64)(now.tv_usec);
+#endif
+    return timestamp;
+}
+
 static void
 print_usage(gboolean print_ver)
 {
@@ -2646,6 +2696,7 @@ capture_loop_close_output(capture_options *capture_opts, loop_data *ld, int *err
 
     unsigned int i;
     pcap_options *pcap_opts;
+    guint64 end_time = create_timestamp();
 
     g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "capture_loop_close_output");
 
@@ -2656,7 +2707,14 @@ capture_loop_close_output(capture_options *capture_opts, loop_data *ld, int *err
             for (i = 0; i < global_ld.pcaps->len; i++) {
                 pcap_opts = g_array_index(global_ld.pcaps, pcap_options *, i);
                 if (!pcap_opts->from_cap_pipe) {
-                    libpcap_write_interface_statistics_block(ld->pdh, i, pcap_opts->pcap_h, &ld->bytes_written, err_close);
+                    libpcap_write_interface_statistics_block(ld->pdh,
+                                                             i,
+                                                             pcap_opts->pcap_h,
+                                                             &ld->bytes_written,
+                                                             "Counters provided by libpcap",
+                                                             start_time,
+                                                             end_time,
+                                                             err_close);
                 }
             }
         }
@@ -3244,7 +3302,7 @@ capture_loop_start(capture_options *capture_opts, gboolean *stats_known, struct
 #else
     gettimeofday(&upd_time, NULL);
 #endif
-
+    start_time = create_timestamp();
     g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO, "Capture loop running!");
 
     /* WOW, everything is prepared! */