1 package org.apache.commons.net.ntp;
2 /*
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19
20 import java.io.IOException;
21 import java.net.DatagramPacket;
22 import java.net.InetAddress;
23
24 import org.apache.commons.net.DatagramSocketClient;
25
26 /***
27 * The NTPUDPClient class is a UDP implementation of a client for the
28 * Network Time Protocol (NTP) described in RFC 1305 as well as the
29 * Simple Network Time Protocol (SNTP) in RFC-2030. To use the class,
30 * merely open a local datagram socket with <a href="#open"> open </a>
31 * and call <a href="#getTime"> getTime </a> to retrieve the time. Then call
32 * <a href="org.apache.commons.net.DatagramSocketClient.html#close"> close </a>
33 * to close the connection properly.
34 * Successive calls to <a href="#getTime"> getTime </a> are permitted
35 * without re-establishing a connection. That is because UDP is a
36 * connectionless protocol and the Network Time Protocol is stateless.
37 *
38 * @author Jason Mathews, MITRE Corp
39 * @version $Revision: 489397 $ $Date: 2006-12-21 16:28:51 +0000 (Thu, 21 Dec 2006) $
40 ***/
41
42 public final class NTPUDPClient extends DatagramSocketClient
43 {
44 /*** The default NTP port. It is set to 123 according to RFC 1305. ***/
45 public static final int DEFAULT_PORT = 123;
46
47 private int _version = NtpV3Packet.VERSION_3;
48
49 /***
50 * Retrieves the time information from the specified server and port and
51 * returns it. The time is the number of miliiseconds since
52 * 00:00 (midnight) 1 January 1900 UTC, as specified by RFC 1305.
53 * This method reads the raw NTP packet and constructs a <i>TimeInfo</i>
54 * object that allows access to all the fields of the NTP message header.
55 * <p>
56 * @param host The address of the server.
57 * @param port The port of the service.
58 * @return The time value retrieved from the server.
59 * @exception IOException If an error occurs while retrieving the time.
60 ***/
61 public TimeInfo getTime(InetAddress host, int port) throws IOException
62 {
63 // if not connected then open to next available UDP port
64 if (!isOpen())
65 {
66 open();
67 }
68
69 NtpV3Packet message = new NtpV3Impl();
70 message.setMode(NtpV3Packet.MODE_CLIENT);
71 message.setVersion(_version);
72 DatagramPacket sendPacket = message.getDatagramPacket();
73 sendPacket.setAddress(host);
74 sendPacket.setPort(port);
75
76 NtpV3Packet recMessage = new NtpV3Impl();
77 DatagramPacket receivePacket = recMessage.getDatagramPacket();
78
79 /*
80 * Must minimize the time between getting the current time,
81 * timestamping the packet, and sending it out which
82 * introduces an error in the delay time.
83 * No extraneous logging and initializations here !!!
84 */
85 TimeStamp now = TimeStamp.getCurrentTime();
86
87 // Note that if you do not set the transmit time field then originating time
88 // in server response is all 0's which is "Thu Feb 07 01:28:16 EST 2036".
89 message.setTransmitTime(now);
90
91 _socket_.send(sendPacket);
92 _socket_.receive(receivePacket);
93
94 long returnTime = System.currentTimeMillis();
95 // create TimeInfo message container but don't pre-compute the details yet
96 TimeInfo info = new TimeInfo(recMessage, returnTime, false);
97
98 return info;
99 }
100
101 /***
102 * Retrieves the time information from the specified server on the
103 * default NTP port and returns it. The time is the number of miliiseconds
104 * since 00:00 (midnight) 1 January 1900 UTC, as specified by RFC 1305.
105 * This method reads the raw NTP packet and constructs a <i>TimeInfo</i>
106 * object that allows access to all the fields of the NTP message header.
107 * <p>
108 * @param host The address of the server.
109 * @return The time value retrieved from the server.
110 * @exception IOException If an error occurs while retrieving the time.
111 ***/
112 public TimeInfo getTime(InetAddress host) throws IOException
113 {
114 return getTime(host, NtpV3Packet.NTP_PORT);
115 }
116
117 /***
118 * Returns the NTP protocol version number that client sets on request packet
119 * that is sent to remote host (e.g. 3=NTP v3, 4=NTP v4, etc.)
120 *
121 * @return the NTP protocol version number that client sets on request packet.
122 * @see #setVersion(int)
123 ***/
124 public int getVersion()
125 {
126 return _version;
127 }
128
129 /***
130 * Sets the NTP protocol version number that client sets on request packet
131 * communicate with remote host.
132 *
133 * @param version the NTP protocol version number
134 ***/
135 public void setVersion(int version)
136 {
137 _version = version;
138 }
139
140 }