A couple of dissectors have/need an NTP-to-nstime routine, so put it in

packet-ntp and export it to the other dissectors.

Move some macros (now) only used in packet-ntp.c into that file.


git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@35887 f5534014-38df-0310-8fa8-9805f1628bb7
This commit is contained in:
morriss 2011-02-09 03:24:12 +00:00
parent 2c086094ed
commit 515fcf92bc
4 changed files with 23 additions and 70 deletions

View File

@ -134,6 +134,7 @@
#include <epan/sminmpec.h>
#include <epan/dissectors/packet-tcp.h>
#include <epan/dissectors/packet-udp.h>
#include "packet-ntp.h"
#include <epan/expert.h>
@ -1466,42 +1467,6 @@ static int pen_to_type_hf_list (guint32 pen) {
}
}
/* ------------------------------------ */
/* NTP <-> nstime conversions */
/* XXX: ToDo: Put this (and ntp_fmt_ts from packet-ntp.c) in a util lib */
/* NTP_BASETIME is in fact epoch - ntp_start_time */
#define NTP_BASETIME 2208988800ul
#define FLOAT_DENOM 4294967296.0 /* (float) (2**32) */
#if 0
typedef struct _ntptime_t {
long ntp_sec; /* since 1900 */
long ntp_frac_sec; /* n/(2**32) */
} ntptime_t;
static void
nstime_to_ntptime(nstime_t *nst, ntptime_t *ntpt) {
ntpt->ntp_sec = nst->secs + NTP_BASETIME;
ntpt->ntp_frac_sec = (long) ((nst->nsecs*FLOAT_DENOM)/1000000000.0);
}
static void
ntptime_to_nstime(ntptime_t *ntpt, nstime_t *nst) {
nst->secs = ntpt->ntp_sec - NTP_BASETIME;
nst->nsecs = (int)((ntpt->ntp_frac_sec*1000000000.0)/FLOAT_DENOM);
}
#endif
static void
ntptime_to_nstime(tvbuff_t *tvb, gint offset, nstime_t *nstime) {
nstime->secs = tvb_get_ntohl(tvb, offset);
if (nstime->secs)
nstime->secs -= NTP_BASETIME;
nstime->nsecs = (int)(1000000000*tvb_get_ntohl(tvb, offset+4)/FLOAT_DENOM);
}
/* ------------------------------------ */
static int
dissect_netflow(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
@ -2641,28 +2606,28 @@ dissect_v9_v10_pdu_data(tvbuff_t *tvb, packet_info *pinfo, proto_tree *pdutree,
case 154: /* flowStartMicroseconds: 64-bit NTP format */
offset_s[rev] = offset;
ntptime_to_nstime(tvb, offset, &ts_start[rev]);
ntp_to_nstime(tvb, offset, &ts_start[rev]);
goto timestamp_common;
break;
case 155: /* flowEndMicroseconds: 64-bit NTP format */
/* XXX: Not tested ... */
offset_e[rev] = offset;
ntptime_to_nstime(tvb, offset, &ts_end[rev]);
ntp_to_nstime(tvb, offset, &ts_end[rev]);
goto timestamp_common;
break;
case 156: /* flowStartNanoseconds: 64-bit NTP format */
/* XXX: Not tested ... */
offset_s[rev] = offset;
ntptime_to_nstime(tvb, offset, &ts_start[rev]);
ntp_to_nstime(tvb, offset, &ts_start[rev]);
goto timestamp_common;
break;
case 157: /* flowEndNanoseconds: 64-bit NTP format */
/* XXX: Not tested ... */
offset_e[rev] = offset;
ntptime_to_nstime(tvb, offset, &ts_end[rev]);
ntp_to_nstime(tvb, offset, &ts_end[rev]);
goto timestamp_common;
break;

View File

@ -566,9 +566,13 @@ static const char *mon_names[12] = {
static tvbparse_wanted_t* want;
static tvbparse_wanted_t* want_ignore;
/* NTP_BASETIME is in fact epoch - ntp_start_time */
#define NTP_BASETIME 2208988800ul
#define NTP_FLOAT_DENOM 4294967296.0
#define NTP_TS_SIZE 100
/* ntp_fmt_ts - converts NTP timestamp to human readable string.
* reftime - 64bit timestamp (IN)
/* tvb_ntp_fmt_ts - converts NTP timestamp to human readable string.
* TVB and an offset (IN).
* returns pointer to filled buffer. This buffer will be freed automatically once
* dissection of the next packet occurs.
*/
@ -593,7 +597,7 @@ tvb_ntp_fmt_ts(tvbuff_t *tvb, gint offset)
return "Not representable";
}
fractime = bd->tm_sec + tempfrac / 4294967296.0;
fractime = bd->tm_sec + tempfrac / NTP_FLOAT_DENOM;
buff=ep_alloc(NTP_TS_SIZE);
g_snprintf(buff, NTP_TS_SIZE,
"%s %2d, %d %02d:%02d:%09.6f UTC",
@ -606,6 +610,15 @@ tvb_ntp_fmt_ts(tvbuff_t *tvb, gint offset)
return buff;
}
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;
nstime->nsecs = (int)(1000000000*tvb_get_ntohl(tvb, offset+4)/NTP_FLOAT_DENOM);
}
/* dissect_ntp - dissects NTP packet data
* tvb - tvbuff for packet data (IN)
* pinfo - packet info

View File

@ -28,10 +28,7 @@
#ifndef PACKET_NTP_H
#define PACKET_NTP_H
/* NTP_BASETIME is in fact epoch - ntp_start_time */
#define NTP_BASETIME 2208988800ul
#define NTP_TS_SIZE 100
extern const char *tvb_ntp_fmt_ts(tvbuff_t *tvb, gint offset);
extern void ntp_to_nstime(tvbuff_t *tvb, gint offset, nstime_t *nstime);
#endif

View File

@ -88,28 +88,6 @@ static dissector_handle_t data_handle;
static dissector_handle_t ieee802154_handle;
static dissector_handle_t ieee802154_ccfcs_handle;
/*FUNCTION:------------------------------------------------------
* NAME
* ntp_to_nstime
* DESCRIPTION
* Converts a timestamp from ntp format to nstime format.
* PARAMETERS
* guint32 ntp_secs;
* guint32 ntp_fraction;
* struct *nstime_ptr;
* RETURNS
* void
*---------------------------------------------------------------
*/
static void ntp_to_nstime(guint32 ntp_secs, guint32 ntp_fraction, nstime_t *nstime_ptr)
{
double temp;
nstime_ptr->secs = (ntp_secs >= NTP_BASETIME) ? ntp_secs - NTP_BASETIME : ntp_secs;
temp = (double)ntp_fraction / 4.294967296; /* 4.294967296 = (2<<32 / 10<<9) */
nstime_ptr->nsecs = (guint32) temp;
} /* ntp_to_nstime */
/*FUNCTION:------------------------------------------------------
* NAME
* dissect_zep
@ -174,7 +152,7 @@ static void dissect_zep(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
zep_data.device_id = tvb_get_ntohs(tvb, 5);
zep_data.lqi_mode = tvb_get_guint8(tvb, 7)?1:0;
zep_data.lqi = tvb_get_guint8(tvb, 8);
ntp_to_nstime(tvb_get_ntohl(tvb, 9), tvb_get_ntohl(tvb, 13), &(zep_data.ntp_time));
ntp_to_nstime(tvb, 9, &(zep_data.ntp_time));
zep_data.seqno = tvb_get_ntohl(tvb, 17);
ieee_packet_len = (tvb_get_guint8(tvb, ZEP_V2_HEADER_LEN - 1) & ZEP_LENGTH_MASK);
}