epan: Move time display types to field_display_e

This makes it easier to understand the code, avoids conflicts
and ugly and unnecessary casts.

The field display enum has evolved over time from integer types
to a type generic parameter.
This commit is contained in:
João Valverde 2021-12-26 16:53:23 +00:00 committed by Wireshark GitLab Utility
parent 0258ef3a04
commit a566076839
11 changed files with 53 additions and 86 deletions

View File

@ -151,7 +151,6 @@ set(LIBWIRESHARK_PUBLIC_HEADERS
timestamp.h
timestats.h
tfs.h
time_fmt.h
to_str.h
tvbparse.h
tvbuff.h

View File

@ -34,13 +34,10 @@ static int hf_time_time = -1;
static int hf_time_response = -1;
static gint ett_time = -1;
/* Instead of using absolute_time_display_e as the type for
* time_display_type, we use gint to avoid a type-punning problem
* with prefs_register_enum_preference(). This variable is also
* used with abs_time_secs_to_ep_str(), which _does_ take
* an absolute_time_display_e, but gcc doesn't complain about
* casting the gint to absolute_time_display_e */
static gint time_display_type = ABSOLUTE_TIME_LOCAL;
/* Use int instead of a field_display_type_e enum to avoid incompatible
* pointer type warnings with prefs_register_enum_preference() */
static int time_display_type = ABSOLUTE_TIME_LOCAL;
/* This dissector works for TCP and UDP time packets */
#define TIME_PORT 37
@ -66,7 +63,7 @@ dissect_time(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_
proto_tree_add_uint_format(time_tree, hf_time_time, tvb, 0, 4,
delta_seconds, "%s",
abs_time_secs_to_str(pinfo->pool, delta_seconds-EPOCH_DELTA_1900_01_01_00_00_00_UTC,
(absolute_time_display_e)time_display_type, TRUE));
time_display_type, TRUE));
}
return tvb_captured_length(tvb);
}
@ -100,7 +97,7 @@ proto_register_time(void)
"display_time_type",
"Time Display",
"Time display type",
(gint *)&time_display_type,
&time_display_type,
time_display_types,
FALSE);
}

View File

@ -284,7 +284,7 @@ absolute_val_to_repr(wmem_allocator_t *scope, const fvalue_t *fv, ftrepr_t rtype
switch (rtype) {
case FTREPR_DISPLAY:
rep = abs_time_to_str(scope, &fv->value.time,
(absolute_time_display_e)field_display, TRUE);
field_display, TRUE);
break;
case FTREPR_DFILTER:

View File

@ -22,6 +22,10 @@
#define ENUM(arg) { #arg, arg }
static ws_enum_t all_enums[] = {
ENUM(ABSOLUTE_TIME_DOY_UTC),
ENUM(ABSOLUTE_TIME_LOCAL),
ENUM(ABSOLUTE_TIME_NTP_UTC),
ENUM(ABSOLUTE_TIME_UTC),
ENUM(AT_AX25),
ENUM(AT_END_OF_LIST),
ENUM(AT_ETHER),

View File

@ -3884,7 +3884,7 @@ proto_tree_add_item_ret_time_string(proto_tree *tree, int hfindex,
switch (hfinfo->type) {
case FT_ABSOLUTE_TIME:
get_time_value(tree, tvb, start, length, encoding, &time_stamp, FALSE);
*retval = abs_time_to_str(scope, &time_stamp, (absolute_time_display_e)hfinfo->display, TRUE);
*retval = abs_time_to_str(scope, &time_stamp, hfinfo->display, TRUE);
break;
case FT_RELATIVE_TIME:
get_time_value(tree, tvb, start, length, encoding, &time_stamp, TRUE);
@ -6426,7 +6426,7 @@ proto_item_fill_display_label(field_info *finfo, gchar *display_label_str, const
break;
case FT_ABSOLUTE_TIME:
tmp_str = abs_time_to_str(NULL, (const nstime_t *)fvalue_get(&finfo->value), (absolute_time_display_e)hfinfo->display, TRUE);
tmp_str = abs_time_to_str(NULL, (const nstime_t *)fvalue_get(&finfo->value), hfinfo->display, TRUE);
label_len = protoo_strlcpy(display_label_str, tmp_str, label_str_size);
wmem_free(NULL, tmp_str);
break;
@ -9117,7 +9117,7 @@ proto_item_fill_label(field_info *fi, gchar *label_str)
}
case FT_ABSOLUTE_TIME:
tmp = abs_time_to_str(NULL, (const nstime_t *)fvalue_get(&fi->value), (absolute_time_display_e)hfinfo->display, TRUE);
tmp = abs_time_to_str(NULL, (const nstime_t *)fvalue_get(&fi->value), hfinfo->display, TRUE);
label_fill(label_str, 0, hfinfo, tmp);
wmem_free(NULL, tmp);
break;

View File

@ -31,7 +31,6 @@
#include "ipv4.h"
#include "wsutil/nstime.h"
#include "time_fmt.h"
#include "tvbuff.h"
#include "value_string.h"
#include "tfs.h"
@ -654,6 +653,7 @@ void proto_report_dissector_bug(const char *format, ...)
/*
* Note that this enum values are parsed in make-init-lua.pl so make sure
* any changes here still makes valid entries in init.lua.
* XXX The script requires the equals sign.
*/
typedef enum {
BASE_NONE = 0, /**< none */
@ -685,12 +685,20 @@ typedef enum {
BASE_PT_SCTP = 16, /**< SCTP port */
/* OUI types */
BASE_OUI = 17 /**< OUI resolution */
BASE_OUI = 17, /**< OUI resolution */
/* Time types */
ABSOLUTE_TIME_LOCAL = 18, /**< local time in our time zone, with month and day */
ABSOLUTE_TIME_UTC = 19, /**< UTC, with month and day */
ABSOLUTE_TIME_DOY_UTC = 20, /**< UTC, with 1-origin day-of-year */
ABSOLUTE_TIME_NTP_UTC = 21, /**< UTC, with "NULL" when timestamp is all zeros */
} field_display_e;
#define FIELD_DISPLAY(d) ((d) & FIELD_DISPLAY_E_MASK)
#define FIELD_DISPLAY_IS_ABSOLUTE_TIME(d) \
(FIELD_DISPLAY(d) >= ABSOLUTE_TIME_LOCAL && FIELD_DISPLAY(d) <= ABSOLUTE_TIME_NTP_UTC)
/* Following constants have to be ORed with a field_display_e when dissector
* want to use specials value-string MACROs for a header_field_info */
#define BASE_RANGE_STRING 0x00000100 /**< Use the supplied range string to convert the field to text */
@ -716,9 +724,6 @@ typedef enum {
/** BASE_PT_ values display decimal and transport port service name */
#define IS_BASE_PORT(b) (((b)==BASE_PT_UDP||(b)==BASE_PT_TCP||(b)==BASE_PT_DCCP||(b)==BASE_PT_SCTP))
/* For FT_ABSOLUTE_TIME, the display format is an absolute_time_display_e
* as per time_fmt.h. */
typedef enum {
HF_REF_TYPE_NONE, /**< Field is not referenced */
HF_REF_TYPE_INDIRECT, /**< Field is indirectly referenced (only applicable for FT_PROTOCOL) via. its child */

View File

@ -1,38 +0,0 @@
/** @file
*
* Definitions for various time display formats.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __TIME_FMT_H__
#define __TIME_FMT_H__
/*
* Resolution of a time stamp.
*/
typedef enum {
TO_STR_TIME_RES_T_SECS, /* seconds */
TO_STR_TIME_RES_T_DSECS, /* deciseconds */
TO_STR_TIME_RES_T_CSECS, /* centiseconds */
TO_STR_TIME_RES_T_MSECS, /* milliseconds */
TO_STR_TIME_RES_T_USECS, /* microseconds */
TO_STR_TIME_RES_T_NSECS /* nanoseconds */
} to_str_time_res_t;
/*
* Display format of an absolute-time time stamp.
*/
typedef enum {
/* Start at 1000 to avoid duplicating the values used in field_display_e */
ABSOLUTE_TIME_LOCAL = 1000, /* local time in our time zone, with month and day */
ABSOLUTE_TIME_UTC, /* UTC, with month and day */
ABSOLUTE_TIME_DOY_UTC, /* UTC, with 1-origin day-of-year */
ABSOLUTE_TIME_NTP_UTC /* UTC, with "NULL" when timestamp is all zeros */
} absolute_time_display_e;
#endif /* __TIME_FMT_H__ */

View File

@ -94,13 +94,14 @@ get_zonename(struct tm *tmp)
}
gchar *
abs_time_to_str(wmem_allocator_t *scope, const nstime_t *abs_time, const absolute_time_display_e fmt,
abs_time_to_str(wmem_allocator_t *scope, const nstime_t *abs_time, field_display_e fmt,
gboolean show_zone)
{
struct tm *tmp = NULL;
const char *zonename = "???";
gchar *buf = NULL;
ws_assert(FIELD_DISPLAY_IS_ABSOLUTE_TIME(fmt));
switch (fmt) {
@ -117,6 +118,8 @@ abs_time_to_str(wmem_allocator_t *scope, const nstime_t *abs_time, const absolut
zonename = get_zonename(tmp);
}
break;
default:
ws_assert_not_reached();
}
if (tmp) {
switch (fmt) {
@ -195,6 +198,8 @@ abs_time_to_str(wmem_allocator_t *scope, const nstime_t *abs_time, const absolut
(long)abs_time->nsecs);
}
break;
default:
ws_assert_not_reached();
}
} else
buf = wmem_strdup(scope, "Not representable");
@ -202,13 +207,15 @@ abs_time_to_str(wmem_allocator_t *scope, const nstime_t *abs_time, const absolut
}
gchar *
abs_time_secs_to_str(wmem_allocator_t *scope, const time_t abs_time, const absolute_time_display_e fmt,
abs_time_secs_to_str(wmem_allocator_t *scope, const time_t abs_time, field_display_e fmt,
gboolean show_zone)
{
struct tm *tmp = NULL;
const char *zonename = "???";
gchar *buf = NULL;
ws_assert(FIELD_DISPLAY_IS_ABSOLUTE_TIME(fmt));
switch (fmt) {
case ABSOLUTE_TIME_UTC:
@ -224,6 +231,8 @@ abs_time_secs_to_str(wmem_allocator_t *scope, const time_t abs_time, const absol
zonename = get_zonename(tmp);
}
break;
default:
ws_assert_not_reached();
}
if (tmp) {
switch (fmt) {
@ -278,6 +287,8 @@ abs_time_secs_to_str(wmem_allocator_t *scope, const time_t abs_time, const absol
tmp->tm_sec);
}
break;
default:
ws_assert_not_reached();
}
} else
buf = wmem_strdup(scope, "Not representable");

View File

@ -15,7 +15,7 @@
#include "wsutil/nstime.h"
#include <wsutil/inet_addr.h>
#include "time_fmt.h"
#include <epan/proto.h>
#include <epan/packet_info.h>
#include <epan/ipv6.h>
#include "ws_symbol_export.h"
@ -29,6 +29,15 @@
#define AX25_ADDR_LEN 7
#define FCWWN_ADDR_LEN 8
typedef enum {
TO_STR_TIME_RES_T_SECS, /* seconds */
TO_STR_TIME_RES_T_DSECS, /* deciseconds */
TO_STR_TIME_RES_T_CSECS, /* centiseconds */
TO_STR_TIME_RES_T_MSECS, /* milliseconds */
TO_STR_TIME_RES_T_USECS, /* microseconds */
TO_STR_TIME_RES_T_NSECS /* nanoseconds */
} to_str_time_res_t;
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
@ -121,10 +130,10 @@ WS_DLL_PUBLIC gchar* tvb_address_var_to_str(wmem_allocator_t *scope, tvbuff_t *t
************** Time
*/
WS_DLL_PUBLIC gchar *abs_time_to_str(wmem_allocator_t *scope, const nstime_t*, const absolute_time_display_e fmt,
WS_DLL_PUBLIC gchar *abs_time_to_str(wmem_allocator_t *scope, const nstime_t*, field_display_e fmt,
gboolean show_zone);
WS_DLL_PUBLIC gchar *abs_time_secs_to_str(wmem_allocator_t *scope, const time_t, const absolute_time_display_e fmt,
WS_DLL_PUBLIC gchar *abs_time_secs_to_str(wmem_allocator_t *scope, const time_t, field_display_e fmt,
gboolean show_zone);
WS_DLL_PUBLIC void display_epoch_time(gchar *, int, const time_t, gint32, const to_str_time_res_t);

View File

@ -158,7 +158,7 @@ my $skip_this = 0;
while(<PROTO_H>) {
$skip_this = 0;
if (/^\s+(?:BASE|SEP)_([A-Z_]+)[ ]*=[ ]*([0-9]+)[,\s]+(?:\/\*\*< (.*?) \*\/)?/) {
if (/^\s+(?:BASE|SEP|ABSOLUTE_TIME)_([A-Z_]+)[ ]*=[ ]*([0-9]+)[,\s]+(?:\/\*\*< (.*?) \*\/)?/) {
$bases_table .= "\t[\"$1\"] = $2, -- $3\n";
}
@ -210,26 +210,6 @@ while(<PROTO_H>) {
}
close PROTO_H;
#
# Extract values from time_fmt.h:
#
# ABSOLUTE_TIME_XXX values for absolute time bases
#
my $absolute_time_num = 0;
open TIME_FMT_H, "< $WSROOT/epan/time_fmt.h" or die "cannot open '$WSROOT/epan/time_fmt.h': $!";
while(<TIME_FMT_H>) {
if (/^\s+ABSOLUTE_TIME_([A-Z_]+)[ ]*=[ ]*([0-9]+)[,\s]+(?:\/\* (.*?) \*\/)?/) {
$bases_table .= "\t[\"$1\"] = $2, -- $3\n";
$absolute_time_num = $2 + 1;
} elsif (/^\s+ABSOLUTE_TIME_([A-Z_]+)[,\s]+(?:\/\* (.*?) \*\/)?/) {
$bases_table .= "\t[\"$1\"] = $absolute_time_num, -- $2\n";
$absolute_time_num++;
}
}
close TIME_FTM_H;
#
# Extract values from stat_groups.h:
#

View File

@ -109,7 +109,7 @@ static const struct field_display_string_t base_displays[] = {
{"16",16},
{"24",24},
{"32",32},
/* for FT_ABSOLUTE_TIME use values in absolute_time_display_e */
/* FT_ABSOLUTE_TIME */
{"base.LOCAL", ABSOLUTE_TIME_LOCAL},
{"base.UTC", ABSOLUTE_TIME_UTC},
{"base.DOY_UTC", ABSOLUTE_TIME_DOY_UTC},
@ -664,7 +664,7 @@ WSLUA_CONSTRUCTOR ProtoField_new(lua_State* L) {
case FT_ABSOLUTE_TIME:
if (base == BASE_NONE) {
base = ABSOLUTE_TIME_LOCAL; /* Default base for FT_ABSOLUTE_TIME */
} else if (base < ABSOLUTE_TIME_LOCAL || base > ABSOLUTE_TIME_DOY_UTC) {
} else if (!FIELD_DISPLAY_IS_ABSOLUTE_TIME(base)) {
WSLUA_OPTARG_ERROR(ProtoField_new,BASE,"Base must be either base.LOCAL, base.UTC, or base.DOY_UTC");
return 0;
}
@ -1131,7 +1131,7 @@ static int ProtoField_time(lua_State* L,enum ftenum type) {
}
if (type == FT_ABSOLUTE_TIME) {
if (base < ABSOLUTE_TIME_LOCAL || base > ABSOLUTE_TIME_DOY_UTC) {
if (!FIELD_DISPLAY_IS_ABSOLUTE_TIME(base)) {
luaL_argerror(L, 3, "Base must be either base.LOCAL, base.UTC, or base.DOY_UTC");
return 0;
}