Branch data Line data Source code
1 : : // $Id: NTP.cc 6219 2008-10-01 05:39:07Z vern $
2 : : //
3 : : // See the file "COPYING" in the main distribution directory for copyright.
4 : :
5 : : #include "config.h"
6 : :
7 : : #include "NetVar.h"
8 : : #include "NTP.h"
9 : : #include "Sessions.h"
10 : : #include "Event.h"
11 : :
12 : :
13 : 0 : NTP_Analyzer::NTP_Analyzer(Connection* conn)
14 : 0 : : Analyzer(AnalyzerTag::NTP, conn)
15 : : {
16 : 0 : ADD_ANALYZER_TIMER(&NTP_Analyzer::ExpireTimer,
17 : : network_time + ntp_session_timeout, 1,
18 : : TIMER_NTP_EXPIRE);
19 : 0 : }
20 : :
21 : 0 : void NTP_Analyzer::Done()
22 : : {
23 : 0 : Analyzer::Done();
24 : 0 : Event(udp_session_done);
25 : 0 : }
26 : :
27 : 0 : void NTP_Analyzer::DeliverPacket(int len, const u_char* data, bool is_orig, int seq, const IP_Hdr* ip, int caplen)
28 : : {
29 : 0 : Analyzer::DeliverPacket(len, data, is_orig, seq, ip, caplen);
30 : :
31 : : // Actually we could just get rid of the Request/Reply and simply use
32 : : // the code of Message(). But for now we use it as an example of how
33 : : // to convert an old-style UDP analyzer.
34 [ # # ]: 0 : if ( is_orig )
35 : 0 : Request(data, len);
36 : : else
37 : 0 : Reply(data, len);
38 : 0 : }
39 : :
40 : 0 : int NTP_Analyzer::Request(const u_char* data, int len)
41 : : {
42 : 0 : Message(data, len);
43 : 0 : return 1;
44 : : }
45 : :
46 : 0 : int NTP_Analyzer::Reply(const u_char* data, int len)
47 : : {
48 : 0 : Message(data, len);
49 : 0 : return 1;
50 : : }
51 : :
52 : 0 : void NTP_Analyzer::Message(const u_char* data, int len)
53 : : {
54 [ # # ]: 0 : if ( (unsigned) len < sizeof(struct ntpdata) )
55 : : {
56 : 0 : Weird("truncated_NTP");
57 : 0 : return;
58 : : }
59 : :
60 : 0 : struct ntpdata* ntp_data = (struct ntpdata *) data;
61 : 0 : len -= sizeof *ntp_data;
62 : 0 : data += sizeof *ntp_data;
63 : :
64 : 0 : RecordVal* msg = new RecordVal(ntp_msg);
65 : :
66 : 0 : unsigned int code = ntp_data->status & 0x7;
67 : :
68 : 0 : msg->Assign(0, new Val((unsigned int) (ntohl(ntp_data->refid)), TYPE_COUNT));
69 : 0 : msg->Assign(1, new Val(code, TYPE_COUNT));
70 : 0 : msg->Assign(2, new Val((unsigned int) ntp_data->stratum, TYPE_COUNT));
71 : 0 : msg->Assign(3, new Val((unsigned int) ntp_data->ppoll, TYPE_COUNT));
72 : 0 : msg->Assign(4, new Val((unsigned int) ntp_data->precision, TYPE_INT));
73 : 0 : msg->Assign(5, new Val(ShortFloat(ntp_data->distance), TYPE_INTERVAL));
74 : 0 : msg->Assign(6, new Val(ShortFloat(ntp_data->dispersion), TYPE_INTERVAL));
75 : 0 : msg->Assign(7, new Val(LongFloat(ntp_data->reftime), TYPE_TIME));
76 : 0 : msg->Assign(8, new Val(LongFloat(ntp_data->org), TYPE_TIME));
77 : 0 : msg->Assign(9, new Val(LongFloat(ntp_data->rec), TYPE_TIME));
78 : 0 : msg->Assign(10, new Val(LongFloat(ntp_data->xmt), TYPE_TIME));
79 : :
80 : 0 : val_list* vl = new val_list;
81 : 0 : vl->append(BuildConnVal());
82 : 0 : vl->append(msg);
83 : 0 : vl->append(new StringVal(new BroString(data, len, 0)));
84 : :
85 : 0 : ConnectionEvent(ntp_message, vl);
86 : : }
87 : :
88 : 0 : double NTP_Analyzer::ShortFloat(struct s_fixedpt fp)
89 : : {
90 : 0 : return ConvertToDouble(ntohs(fp.int_part), ntohs(fp.fraction), 65536.0);
91 : : }
92 : :
93 : 0 : double NTP_Analyzer::LongFloat(struct l_fixedpt fp)
94 : : {
95 : : double t = ConvertToDouble(ntohl(fp.int_part), ntohl(fp.fraction),
96 : 0 : 4294967296.0);
97 : :
98 [ # # ]: 0 : return t ? t - JAN_1970 : 0.0;
99 : : }
100 : :
101 : : double NTP_Analyzer::ConvertToDouble(unsigned int int_part,
102 : 0 : unsigned int fraction, double frac_base)
103 : : {
104 : 0 : return double(int_part) + double(fraction) / frac_base;
105 : : }
106 : :
107 : 0 : void NTP_Analyzer::ExpireTimer(double /* t */)
108 : : {
109 : 0 : Event(connection_timeout);
110 : 0 : sessions->Remove(Conn());
111 [ + - ][ + - ]: 6 : }
|