From 8304f69fd7f02cb5fe79010170c7550d441ad6e7 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Wed, 27 Jun 2018 20:39:15 -0700 Subject: Add macros to initialize nstime_t values and use them. This allows code to initialize them without having to know the details of the structure; the initializes should, and will, be changed if the members of the structure are changed. Change-Id: I93e6ebfcde9ceca17df696fcba4e8410c5afb175 Reviewed-on: https://code.wireshark.org/review/28501 Reviewed-by: Guy Harris --- editcap.c | 8 ++++---- epan/dissectors/packet-collectd.c | 2 +- epan/dissectors/packet-dns.c | 2 +- epan/dissectors/packet-iax2.c | 2 +- epan/dissectors/packet-rtpproxy.c | 2 +- epan/dissectors/packet-smpp.c | 8 ++++---- wiretap/merge.c | 2 +- wsutil/nstime.h | 20 ++++++++++++++++++++ 8 files changed, 33 insertions(+), 13 deletions(-) diff --git a/editcap.c b/editcap.c index 69cd055c98..2ca3ce5d61 100644 --- a/editcap.c +++ b/editcap.c @@ -159,8 +159,8 @@ static int out_file_type_subtype = WTAP_FILE_TYPE_SUBTYPE #endif static int out_frame_type = -2; /* Leave frame type alone */ static int verbose = 0; /* Not so verbose */ -static struct time_adjustment time_adj = {{0, 0}, 0}; /* no adjustment */ -static nstime_t relative_time_window = {0, 0}; /* de-dup time window */ +static struct time_adjustment time_adj = {NSTIME_INIT_ZERO, 0}; /* no adjustment */ +static nstime_t relative_time_window = NSTIME_INIT_ZERO; /* de-dup time window */ static double err_prob = 0.0; static time_t starttime = 0; static time_t stoptime = 0; @@ -170,8 +170,8 @@ static gboolean dup_detect = FALSE; static gboolean dup_detect_by_time = FALSE; static int do_strict_time_adjustment = FALSE; -static struct time_adjustment strict_time_adj = {{0, 0}, 0}; /* strict time adjustment */ -static nstime_t previous_time = {0, 0}; /* previous time */ +static struct time_adjustment strict_time_adj = {NSTIME_INIT_ZERO, 0}; /* strict time adjustment */ +static nstime_t previous_time = NSTIME_INIT_ZERO; /* previous time */ static int find_dct2000_real_data(guint8 *buf); static void handle_chopping(chop_t chop, wtap_packet_header *out_phdr, diff --git a/epan/dissectors/packet-collectd.c b/epan/dissectors/packet-collectd.c index eb54302ece..f065a76789 100644 --- a/epan/dissectors/packet-collectd.c +++ b/epan/dissectors/packet-collectd.c @@ -190,7 +190,7 @@ void proto_reg_handoff_collectd (void); static nstime_t collectd_time_to_nstime (guint64 t) { - nstime_t nstime = { 0, 0 }; + nstime_t nstime = NSTIME_INIT_ZERO;; nstime.secs = (time_t) (t / 1073741824); nstime.nsecs = (int) (((double) (t % 1073741824)) / 1.073741824); diff --git a/epan/dissectors/packet-dns.c b/epan/dissectors/packet-dns.c index 1bdd91734a..3f4e5d5443 100644 --- a/epan/dissectors/packet-dns.c +++ b/epan/dissectors/packet-dns.c @@ -3677,7 +3677,7 @@ dissect_dns_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gboolean retransmission = FALSE; const guchar *name; int name_len; - nstime_t delta = { 0, 0 }; + nstime_t delta = NSTIME_INIT_ZERO; dns_data_offset = offset; diff --git a/epan/dissectors/packet-iax2.c b/epan/dissectors/packet-iax2.c index 79cb123bea..472b2517bf 100644 --- a/epan/dissectors/packet-iax2.c +++ b/epan/dissectors/packet-iax2.c @@ -996,7 +996,7 @@ static iax_call_data *iax_new_call( packet_info *pinfo, { iax_call_data *call; guint circuit_id; - static const nstime_t millisecond = {0, 1000000}; + static const nstime_t millisecond = NSTIME_INIT_SECS_MSECS(0, 1); #ifdef DEBUG_HASHING g_debug("+ new_circuit: Handling NEW packet, frame %u", pinfo->num); diff --git a/epan/dissectors/packet-rtpproxy.c b/epan/dissectors/packet-rtpproxy.c index 78210660b6..425ff811a5 100644 --- a/epan/dissectors/packet-rtpproxy.c +++ b/epan/dissectors/packet-rtpproxy.c @@ -269,7 +269,7 @@ static gboolean rtpproxy_establish_conversation = TRUE; /* See - https://www.opensips.org/html/docs/modules/1.10.x/rtpproxy.html#id293555 */ /* See - http://www.kamailio.org/docs/modules/4.3.x/modules/rtpproxy.html#idp15794952 */ static guint rtpproxy_timeout = 1000; -static nstime_t rtpproxy_timeout_ns = {1, 0}; +static nstime_t rtpproxy_timeout_ns = NSTIME_INIT_ZERO; void proto_reg_handoff_rtpproxy(void); diff --git a/epan/dissectors/packet-smpp.c b/epan/dissectors/packet-smpp.c index 612e154bd2..04c5d9faed 100644 --- a/epan/dissectors/packet-smpp.c +++ b/epan/dissectors/packet-smpp.c @@ -1894,7 +1894,7 @@ submit_sm(proto_tree *tree, tvbuff_t *tvb, packet_info *pinfo, const char *src_str = NULL; const char *dst_str = NULL; address save_src, save_dst; - nstime_t zero_time = {0, 0}; + nstime_t zero_time = NSTIME_INIT_ZERO; smpp_handle_string_z(tree, tvb, hf_smpp_service_type, &offset, "(Default)"); smpp_handle_int1(tree, tvb, hf_smpp_source_addr_ton, &offset); @@ -1970,7 +1970,7 @@ replace_sm(proto_tree *tree, tvbuff_t *tvb) int offset = 0; guint8 flag; guint8 length; - nstime_t zero_time = {0, 0}; + nstime_t zero_time = NSTIME_INIT_ZERO; smpp_handle_string(tree, tvb, hf_smpp_message_id, &offset); smpp_handle_int1(tree, tvb, hf_smpp_source_addr_ton, &offset); @@ -2022,7 +2022,7 @@ submit_multi(proto_tree *tree, tvbuff_t *tvb) int offset = 0; guint8 flag; guint8 length; - nstime_t zero_time = {0, 0}; + nstime_t zero_time = NSTIME_INIT_ZERO; smpp_handle_string_z(tree, tvb, hf_smpp_service_type, &offset, "(Default)"); smpp_handle_int1(tree, tvb, hf_smpp_source_addr_ton, &offset); @@ -2120,7 +2120,7 @@ static void broadcast_sm(proto_tree *tree, tvbuff_t *tvb) { int offset = 0; - nstime_t zero_time = {0, 0}; + nstime_t zero_time = NSTIME_INIT_ZERO; smpp_handle_string_z(tree, tvb, hf_smpp_service_type, &offset, "(Default)"); smpp_handle_int1(tree, tvb, hf_smpp_source_addr_ton, &offset); diff --git a/wiretap/merge.c b/wiretap/merge.c index bf26d8dde6..d706f79ae8 100644 --- a/wiretap/merge.c +++ b/wiretap/merge.c @@ -245,7 +245,7 @@ merge_read_packet(int in_file_count, merge_in_file_t in_files[], { int i; int ei = -1; - nstime_t tv = { sizeof(time_t) > sizeof(int) ? LONG_MAX : INT_MAX, INT_MAX }; + nstime_t tv = NSTIME_INIT_MAX; wtap_rec *rec; /* diff --git a/wsutil/nstime.h b/wsutil/nstime.h index 0d976200b8..273569a82b 100644 --- a/wsutil/nstime.h +++ b/wsutil/nstime.h @@ -29,6 +29,26 @@ typedef struct { int nsecs; } nstime_t; +/* Macros that expand to nstime_t initializers */ + +/* Initialize to zero */ +#define NSTIME_INIT_ZERO {0, 0} + +/* Initialize to a specified number of seconds and nanoseconds */ +#define NSTIME_INIT_SECS_NSECS(secs, nsecs) {secs, nsecs} + +/* Initialize to a specified number of seconds and microseconds */ +#define NSTIME_INIT_SECS_USECS(secs, usecs) {secs, usecs*1000} + +/* Initialize to a specified number of seconds and milliseconds */ +#define NSTIME_INIT_SECS_MSECS(secs, msecs) {secs, msecs*1000000} + +/* Initialize to a specified number of seconds */ +#define NSTIME_INIT_SECS(secs) {secs, 0} + +/* Initialize to the maxximum possible value */ +#define NSTIME_INIT_MAX {sizeof(time_t) > sizeof(int) ? LONG_MAX : INT_MAX, INT_MAX} + /* functions */ /** set the given nstime_t to zero */ -- cgit v1.2.3