Skip to content

Commit

Permalink
Fix problem where NTP times with the high-bit set to 0 (which RFC 2030
Browse files Browse the repository at this point in the history
chapter 3 has redefined to mean years *after* 2036) were being represented as
times prior to 1968.

This has been broken since r35840 (apparently not many people see NTP
timestamps beyond 2036 :-)): apparently I over-optimized packet-ntp's code
while copying it into proto.c: that temporary variable is necessary for the
unsigned math to happen correctly before assigning the result to the (signed)
time_t.

Leave a comment in the code indicating why the temporary variable is needed.
Copy that comment to packet-ntp.c.

Fix the same problem in ntp_to_nstime(): it also did not use the temporary variable.

svn path=/trunk/; revision=45790
  • Loading branch information
Jeff Morriss committed Oct 25, 2012
1 parent 4518ece commit d97b4ec
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
19 changes: 16 additions & 3 deletions epan/dissectors/packet-ntp.c
Expand Up @@ -644,6 +644,10 @@ tvb_ntp_fmt_ts(tvbuff_t *tvb, gint offset)
return "NULL";
}

/* We need a temporary variable here so the unsigned math
* works correctly (for years > 2036 according to RFC 2030
* chapter 3).
*/
temptime = tempstmp - (guint32) NTP_BASETIME;
bd = gmtime(&temptime);
if(!bd){
Expand All @@ -666,9 +670,18 @@ tvb_ntp_fmt_ts(tvbuff_t *tvb, gint offset)
void
ntp_to_nstime(tvbuff_t *tvb, gint offset, nstime_t *nstime)
{
nstime->secs = tvb_get_ntohl(tvb, offset);
if (nstime->secs)
nstime->secs -= NTP_BASETIME;
guint32 tempstmp;

/* We need a temporary variable here so the unsigned math
* works correctly (for years > 2036 according to RFC 2030
* chapter 3).
*/
tempstmp = tvb_get_ntohl(tvb, offset);
if (tempstmp)
nstime->secs = tempstmp - (guint32)NTP_BASETIME;
else
nstime->secs = tempstmp; /* 0 */

nstime->nsecs = (int)(tvb_get_ntohl(tvb, offset+4)/(NTP_FLOAT_DENOM/1000000000.0));
}

Expand Down
24 changes: 17 additions & 7 deletions epan/proto.c
Expand Up @@ -618,7 +618,7 @@ void
proto_tree_reset(proto_tree *tree)
{
tree_data_t *tree_data = PTREE_DATA(tree);

proto_tree_children_foreach(tree, proto_tree_free_node, NULL);

/* reset tree */
Expand Down Expand Up @@ -1230,6 +1230,7 @@ proto_tree_new_item(field_info *new_fi, proto_tree *tree,
double doubleval;
const char *string;
nstime_t time_stamp;
guint32 tmpsecs;
GPtrArray *ptrs;
gboolean length_error;

Expand Down Expand Up @@ -1626,9 +1627,16 @@ proto_tree_new_item(field_info *new_fi, proto_tree *tree,

/* XXX - where should this go? */
#define NTP_BASETIME 2208988800ul
time_stamp.secs = tvb_get_ntohl(tvb, start);
if (time_stamp.secs)
time_stamp.secs -= NTP_BASETIME;

/* We need a temporary variable here so the unsigned math
* works correctly (for years > 2036 according to RFC 2030
* chapter 3).
*/
tmpsecs = tvb_get_ntohl(tvb, start);
if (tmpsecs)
time_stamp.secs = tmpsecs - (guint32)NTP_BASETIME;
else
time_stamp.secs = tmpsecs; /* 0 */

if (length == 8) {
/*
Expand All @@ -1648,9 +1656,11 @@ proto_tree_new_item(field_info *new_fi, proto_tree *tree,
/*
* NTP time stamp, big-endian.
*/
time_stamp.secs = tvb_get_letohl(tvb, start);
if (time_stamp.secs)
time_stamp.secs -= NTP_BASETIME;
tmpsecs = tvb_get_letohl(tvb, start);
if (tmpsecs)
time_stamp.secs = tmpsecs - (guint32)NTP_BASETIME;
else
time_stamp.secs = tmpsecs; /* 0 */

if (length == 8) {
/*
Expand Down

0 comments on commit d97b4ec

Please sign in to comment.