Net stack with AutoIP enabled

Dependencies:   mbed

Committer:
darran
Date:
Fri Jul 02 17:21:58 2010 +0000
Revision:
0:ac21159e27f4

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
darran 0:ac21159e27f4 1
darran 0:ac21159e27f4 2 /*
darran 0:ac21159e27f4 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
darran 0:ac21159e27f4 4
darran 0:ac21159e27f4 5 Permission is hereby granted, free of charge, to any person obtaining a copy
darran 0:ac21159e27f4 6 of this software and associated documentation files (the "Software"), to deal
darran 0:ac21159e27f4 7 in the Software without restriction, including without limitation the rights
darran 0:ac21159e27f4 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
darran 0:ac21159e27f4 9 copies of the Software, and to permit persons to whom the Software is
darran 0:ac21159e27f4 10 furnished to do so, subject to the following conditions:
darran 0:ac21159e27f4 11
darran 0:ac21159e27f4 12 The above copyright notice and this permission notice shall be included in
darran 0:ac21159e27f4 13 all copies or substantial portions of the Software.
darran 0:ac21159e27f4 14
darran 0:ac21159e27f4 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
darran 0:ac21159e27f4 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
darran 0:ac21159e27f4 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
darran 0:ac21159e27f4 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
darran 0:ac21159e27f4 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
darran 0:ac21159e27f4 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
darran 0:ac21159e27f4 21 THE SOFTWARE.
darran 0:ac21159e27f4 22 */
darran 0:ac21159e27f4 23
darran 0:ac21159e27f4 24 #include "NTPClient.h"
darran 0:ac21159e27f4 25
darran 0:ac21159e27f4 26 #include <stdio.h>
darran 0:ac21159e27f4 27
darran 0:ac21159e27f4 28 //#define __DEBUG
darran 0:ac21159e27f4 29 #include "dbg/dbg.h"
darran 0:ac21159e27f4 30
darran 0:ac21159e27f4 31 #define NTP_PORT 123
darran 0:ac21159e27f4 32 #define NTP_CLIENT_PORT 0//50420 //Random port
darran 0:ac21159e27f4 33 #define NTP_REQUEST_TIMEOUT 15000
darran 0:ac21159e27f4 34 #define NTP_TIMESTAMP_DELTA 2208988800ull //Diff btw a UNIX timestamp (Starting Jan, 1st 1970) and a NTP timestamp (Starting Jan, 1st 1900)
darran 0:ac21159e27f4 35
darran 0:ac21159e27f4 36 #define htons( x ) ( (( x << 8 ) & 0xFF00) | (( x >> 8 ) & 0x00FF) )
darran 0:ac21159e27f4 37 #define ntohs( x ) (htons(x))
darran 0:ac21159e27f4 38
darran 0:ac21159e27f4 39 #define htonl( x ) ( (( x << 24 ) & 0xFF000000) \
darran 0:ac21159e27f4 40 | (( x << 8 ) & 0x00FF0000) \
darran 0:ac21159e27f4 41 | (( x >> 8 ) & 0x0000FF00) \
darran 0:ac21159e27f4 42 | (( x >> 24 ) & 0x000000FF) )
darran 0:ac21159e27f4 43 #define ntohl( x ) (htonl(x))
darran 0:ac21159e27f4 44
darran 0:ac21159e27f4 45 NTPClient::NTPClient() : NetService(false), m_state(NTP_PING), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL),
darran 0:ac21159e27f4 46 m_watchdog(), m_timeout(0), m_closed(true), m_host(), m_pDnsReq(NULL), m_blockingResult(NTP_PROCESSING)
darran 0:ac21159e27f4 47 {
darran 0:ac21159e27f4 48 setTimeout(NTP_REQUEST_TIMEOUT);
darran 0:ac21159e27f4 49 DBG("\r\nNew NTPClient %p\r\n",this);
darran 0:ac21159e27f4 50 }
darran 0:ac21159e27f4 51
darran 0:ac21159e27f4 52 NTPClient::~NTPClient()
darran 0:ac21159e27f4 53 {
darran 0:ac21159e27f4 54 close();
darran 0:ac21159e27f4 55 }
darran 0:ac21159e27f4 56
darran 0:ac21159e27f4 57 //High level setup functions
darran 0:ac21159e27f4 58 NTPResult NTPClient::setTime(const Host& host) //Blocking
darran 0:ac21159e27f4 59 {
darran 0:ac21159e27f4 60 doSetTime(host);
darran 0:ac21159e27f4 61 return blockingProcess();
darran 0:ac21159e27f4 62 }
darran 0:ac21159e27f4 63
darran 0:ac21159e27f4 64 NTPResult NTPClient::setTime(const Host& host, void (*pMethod)(NTPResult)) //Non blocking
darran 0:ac21159e27f4 65 {
darran 0:ac21159e27f4 66 setOnResult(pMethod);
darran 0:ac21159e27f4 67 doSetTime(host);
darran 0:ac21159e27f4 68 return NTP_PROCESSING;
darran 0:ac21159e27f4 69 }
darran 0:ac21159e27f4 70
darran 0:ac21159e27f4 71 #if 0 //For doc only
darran 0:ac21159e27f4 72 template<class T>
darran 0:ac21159e27f4 73 NTPResult NTPClient::setTime(const Host& host, T* pItem, void (T::*pMethod)(NTPResult)) //Non blocking
darran 0:ac21159e27f4 74 {
darran 0:ac21159e27f4 75 setOnResult(pItem, pMethod);
darran 0:ac21159e27f4 76 doSetTime(host);
darran 0:ac21159e27f4 77 return NTP_PROCESSING;
darran 0:ac21159e27f4 78 }
darran 0:ac21159e27f4 79 #endif
darran 0:ac21159e27f4 80
darran 0:ac21159e27f4 81 void NTPClient::doSetTime(const Host& host)
darran 0:ac21159e27f4 82 {
darran 0:ac21159e27f4 83 init();
darran 0:ac21159e27f4 84 resetTimeout();
darran 0:ac21159e27f4 85 m_host = host;
darran 0:ac21159e27f4 86 if(!m_host.getPort())
darran 0:ac21159e27f4 87 {
darran 0:ac21159e27f4 88 m_host.setPort(NTP_PORT);
darran 0:ac21159e27f4 89 }
darran 0:ac21159e27f4 90 if(m_host.getIp().isNull())
darran 0:ac21159e27f4 91 {
darran 0:ac21159e27f4 92 //DNS query required
darran 0:ac21159e27f4 93 m_pDnsReq = new DNSRequest();
darran 0:ac21159e27f4 94 DBG("\r\nNTPClient : DNSRequest %p\r\n", m_pDnsReq);
darran 0:ac21159e27f4 95 m_pDnsReq->setOnReply(this, &NTPClient::onDNSReply);
darran 0:ac21159e27f4 96 m_pDnsReq->resolve(&m_host);
darran 0:ac21159e27f4 97 return;
darran 0:ac21159e27f4 98 }
darran 0:ac21159e27f4 99 open();
darran 0:ac21159e27f4 100 }
darran 0:ac21159e27f4 101
darran 0:ac21159e27f4 102 void NTPClient::setOnResult( void (*pMethod)(NTPResult) )
darran 0:ac21159e27f4 103 {
darran 0:ac21159e27f4 104 m_pCb = pMethod;
darran 0:ac21159e27f4 105 }
darran 0:ac21159e27f4 106
darran 0:ac21159e27f4 107 void NTPClient::close()
darran 0:ac21159e27f4 108 {
darran 0:ac21159e27f4 109 if(m_closed)
darran 0:ac21159e27f4 110 return;
darran 0:ac21159e27f4 111 m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else
darran 0:ac21159e27f4 112 m_watchdog.stop();
darran 0:ac21159e27f4 113 m_pUDPSocket->resetOnEvent();
darran 0:ac21159e27f4 114 m_pUDPSocket->close();
darran 0:ac21159e27f4 115 delete m_pUDPSocket;
darran 0:ac21159e27f4 116 if( m_pDnsReq )
darran 0:ac21159e27f4 117 {
darran 0:ac21159e27f4 118 m_pDnsReq->close();
darran 0:ac21159e27f4 119 delete m_pDnsReq;
darran 0:ac21159e27f4 120 m_pDnsReq = NULL;
darran 0:ac21159e27f4 121 }
darran 0:ac21159e27f4 122 }
darran 0:ac21159e27f4 123
darran 0:ac21159e27f4 124 void NTPClient::poll() //Called by NetServices
darran 0:ac21159e27f4 125 {
darran 0:ac21159e27f4 126 if( (!m_closed) && (m_watchdog.read_ms() >= m_timeout) )
darran 0:ac21159e27f4 127 {
darran 0:ac21159e27f4 128 onTimeout();
darran 0:ac21159e27f4 129 }
darran 0:ac21159e27f4 130 }
darran 0:ac21159e27f4 131
darran 0:ac21159e27f4 132 void NTPClient::init() //Create and setup socket if needed
darran 0:ac21159e27f4 133 {
darran 0:ac21159e27f4 134 if(!m_closed) //Already opened
darran 0:ac21159e27f4 135 return;
darran 0:ac21159e27f4 136 m_state = NTP_PING;
darran 0:ac21159e27f4 137 m_pUDPSocket = new UDPSocket;
darran 0:ac21159e27f4 138 m_pUDPSocket->setOnEvent(this, &NTPClient::onUDPSocketEvent);
darran 0:ac21159e27f4 139 m_closed = false;
darran 0:ac21159e27f4 140 DBG("NTPClient: Init OK\n");
darran 0:ac21159e27f4 141 }
darran 0:ac21159e27f4 142
darran 0:ac21159e27f4 143 void NTPClient::open()
darran 0:ac21159e27f4 144 {
darran 0:ac21159e27f4 145 resetTimeout();
darran 0:ac21159e27f4 146 DBG("Opening connection\n");
darran 0:ac21159e27f4 147 m_state = NTP_PING;
darran 0:ac21159e27f4 148 Host localhost(IpAddr(127,0,0,1), NTP_CLIENT_PORT, "localhost");
darran 0:ac21159e27f4 149 m_pUDPSocket->bind(localhost);
darran 0:ac21159e27f4 150 set_time( 1280000000 ); //End of July 2010... just there to limit offset range
darran 0:ac21159e27f4 151 process();
darran 0:ac21159e27f4 152 }
darran 0:ac21159e27f4 153
darran 0:ac21159e27f4 154 #define MIN(a,b) ((a)<(b))?(a):(b)
darran 0:ac21159e27f4 155 void NTPClient::process() //Main state-machine
darran 0:ac21159e27f4 156 {
darran 0:ac21159e27f4 157 int len;
darran 0:ac21159e27f4 158 Host host;
darran 0:ac21159e27f4 159
darran 0:ac21159e27f4 160 switch(m_state)
darran 0:ac21159e27f4 161 {
darran 0:ac21159e27f4 162 case NTP_PING:
darran 0:ac21159e27f4 163 DBG("\r\nPing\r\n");
darran 0:ac21159e27f4 164 //Prepare NTP Packet:
darran 0:ac21159e27f4 165 m_pkt.li = 0; //Leap Indicator : No warning
darran 0:ac21159e27f4 166 m_pkt.vn = 4; //Version Number : 4
darran 0:ac21159e27f4 167 m_pkt.mode = 3; //Client mode
darran 0:ac21159e27f4 168 m_pkt.stratum = 0; //Not relevant here
darran 0:ac21159e27f4 169 m_pkt.poll = 0; //Not significant as well
darran 0:ac21159e27f4 170 m_pkt.precision = 0; //Neither this one is
darran 0:ac21159e27f4 171
darran 0:ac21159e27f4 172 m_pkt.rootDelay = 0; //Or this one
darran 0:ac21159e27f4 173 m_pkt.rootDispersion = 0; //Or that one
darran 0:ac21159e27f4 174 m_pkt.refId = 0; //...
darran 0:ac21159e27f4 175
darran 0:ac21159e27f4 176 m_pkt.refTm_s = 0;
darran 0:ac21159e27f4 177 m_pkt.origTm_s = 0;
darran 0:ac21159e27f4 178 m_pkt.rxTm_s = 0;
darran 0:ac21159e27f4 179 m_pkt.txTm_s = htonl( NTP_TIMESTAMP_DELTA + time(NULL) ); //WARN: We are in LE format, network byte order is BE
darran 0:ac21159e27f4 180
darran 0:ac21159e27f4 181 m_pkt.refTm_f = m_pkt.origTm_f = m_pkt.rxTm_f = m_pkt.txTm_f = 0;
darran 0:ac21159e27f4 182
darran 0:ac21159e27f4 183 //Hex Dump:
darran 0:ac21159e27f4 184 DBG("\r\nDump Tx:\r\n");
darran 0:ac21159e27f4 185 for(int i = 0; i< sizeof(NTPPacket); i++)
darran 0:ac21159e27f4 186 {
darran 0:ac21159e27f4 187 DBG("%02x ", *((char*)&m_pkt + i));
darran 0:ac21159e27f4 188 }
darran 0:ac21159e27f4 189 DBG("\r\n\r\n");
darran 0:ac21159e27f4 190
darran 0:ac21159e27f4 191 len = m_pUDPSocket->sendto( (char*)&m_pkt, sizeof(NTPPacket), &m_host );
darran 0:ac21159e27f4 192 if(len < sizeof(NTPPacket))
darran 0:ac21159e27f4 193 { onResult(NTP_PRTCL); close(); return; }
darran 0:ac21159e27f4 194
darran 0:ac21159e27f4 195 m_state = NTP_PONG;
darran 0:ac21159e27f4 196
darran 0:ac21159e27f4 197 break;
darran 0:ac21159e27f4 198
darran 0:ac21159e27f4 199 case NTP_PONG:
darran 0:ac21159e27f4 200 DBG("\r\nPong\r\n");
darran 0:ac21159e27f4 201 while( len = m_pUDPSocket->recvfrom( (char*)&m_pkt, sizeof(NTPPacket), &host ) )
darran 0:ac21159e27f4 202 {
darran 0:ac21159e27f4 203 if( len <= 0 )
darran 0:ac21159e27f4 204 break;
darran 0:ac21159e27f4 205 if( !host.getIp().isEq(m_host.getIp()) )
darran 0:ac21159e27f4 206 continue; //Not our packet
darran 0:ac21159e27f4 207 if( len > 0 )
darran 0:ac21159e27f4 208 break;
darran 0:ac21159e27f4 209 }
darran 0:ac21159e27f4 210
darran 0:ac21159e27f4 211 if(len == 0)
darran 0:ac21159e27f4 212 return; //Wait for the next packet
darran 0:ac21159e27f4 213
darran 0:ac21159e27f4 214 if(len < 0)
darran 0:ac21159e27f4 215 { onResult(NTP_PRTCL); close(); return; }
darran 0:ac21159e27f4 216
darran 0:ac21159e27f4 217 if(len < sizeof(NTPPacket)) //TODO: Accept chunks
darran 0:ac21159e27f4 218 { onResult(NTP_PRTCL); close(); return; }
darran 0:ac21159e27f4 219
darran 0:ac21159e27f4 220 //Hex Dump:
darran 0:ac21159e27f4 221 DBG("\r\nDump Rx:\r\n");
darran 0:ac21159e27f4 222 for(int i = 0; i< sizeof(NTPPacket); i++)
darran 0:ac21159e27f4 223 {
darran 0:ac21159e27f4 224 DBG("%02x ", *((char*)&m_pkt + i));
darran 0:ac21159e27f4 225 }
darran 0:ac21159e27f4 226 DBG("\r\n\r\n");
darran 0:ac21159e27f4 227
darran 0:ac21159e27f4 228 if( m_pkt.stratum == 0) //Kiss of death message : Not good !
darran 0:ac21159e27f4 229 {
darran 0:ac21159e27f4 230 onResult(NTP_PRTCL); close(); return;
darran 0:ac21159e27f4 231 }
darran 0:ac21159e27f4 232
darran 0:ac21159e27f4 233 //Correct Endianness
darran 0:ac21159e27f4 234 m_pkt.refTm_s = ntohl( m_pkt.refTm_s );
darran 0:ac21159e27f4 235 m_pkt.refTm_f = ntohl( m_pkt.refTm_f );
darran 0:ac21159e27f4 236 m_pkt.origTm_s = ntohl( m_pkt.origTm_s );
darran 0:ac21159e27f4 237 m_pkt.origTm_f = ntohl( m_pkt.origTm_f );
darran 0:ac21159e27f4 238 m_pkt.rxTm_s = ntohl( m_pkt.rxTm_s );
darran 0:ac21159e27f4 239 m_pkt.rxTm_f = ntohl( m_pkt.rxTm_f );
darran 0:ac21159e27f4 240 m_pkt.txTm_s = ntohl( m_pkt.txTm_s );
darran 0:ac21159e27f4 241 m_pkt.txTm_f = ntohl( m_pkt.txTm_f );
darran 0:ac21159e27f4 242
darran 0:ac21159e27f4 243 //Compute offset, see RFC 4330 p.13
darran 0:ac21159e27f4 244 uint32_t destTm_s = (NTP_TIMESTAMP_DELTA + time(NULL));
darran 0:ac21159e27f4 245 //int32_t origTm = (int32_t) ((uint64_t) m_pkt.origTm - NTP_TIMESTAMP_DELTA); //Convert in local 32 bits timestamps
darran 0:ac21159e27f4 246 //int32_t rxTm = (int32_t) ((uint64_t) m_pkt.rxTm - NTP_TIMESTAMP_DELTA); //Convert in local 32 bits timestamps
darran 0:ac21159e27f4 247 //int32_t txTm = (int32_t) ((uint64_t) m_pkt.txTm - NTP_TIMESTAMP_DELTA); //Convert in local 32 bits timestamps
darran 0:ac21159e27f4 248 // int64_t offset = ( ( ( m_pkt.rxTm_s - m_pkt.origTm_s ) + ( m_pkt.txTm_s - destTm_s ) ) << 32 + ( ( m_pkt.rxTm_f - m_pkt.origTm_f ) + ( m_pkt.txTm_f - 0 ) ) ) / 2;
darran 0:ac21159e27f4 249 int64_t offset = ( (int64_t)( m_pkt.rxTm_s - m_pkt.origTm_s ) + (int64_t) ( m_pkt.txTm_s - destTm_s ) ) / 2; //Avoid overflow
darran 0:ac21159e27f4 250 DBG("\r\nSent @%d\r\n", m_pkt.txTm_s);
darran 0:ac21159e27f4 251 DBG("\r\nOffset: %d\r\n", offset);
darran 0:ac21159e27f4 252 //Set time accordingly
darran 0:ac21159e27f4 253 set_time( time(NULL) + (offset /*>> 32*/) );
darran 0:ac21159e27f4 254
darran 0:ac21159e27f4 255 onResult(NTP_OK);
darran 0:ac21159e27f4 256 close();
darran 0:ac21159e27f4 257
darran 0:ac21159e27f4 258 break;
darran 0:ac21159e27f4 259 }
darran 0:ac21159e27f4 260 }
darran 0:ac21159e27f4 261
darran 0:ac21159e27f4 262 void NTPClient::setTimeout(int ms)
darran 0:ac21159e27f4 263 {
darran 0:ac21159e27f4 264 m_timeout = ms;
darran 0:ac21159e27f4 265 }
darran 0:ac21159e27f4 266
darran 0:ac21159e27f4 267 void NTPClient::resetTimeout()
darran 0:ac21159e27f4 268 {
darran 0:ac21159e27f4 269 m_watchdog.reset();
darran 0:ac21159e27f4 270 m_watchdog.start();
darran 0:ac21159e27f4 271 }
darran 0:ac21159e27f4 272
darran 0:ac21159e27f4 273 void NTPClient::onTimeout() //Connection has timed out
darran 0:ac21159e27f4 274 {
darran 0:ac21159e27f4 275 close();
darran 0:ac21159e27f4 276 onResult(NTP_TIMEOUT);
darran 0:ac21159e27f4 277 }
darran 0:ac21159e27f4 278
darran 0:ac21159e27f4 279 void NTPClient::onDNSReply(DNSReply r)
darran 0:ac21159e27f4 280 {
darran 0:ac21159e27f4 281 if(m_closed)
darran 0:ac21159e27f4 282 {
darran 0:ac21159e27f4 283 DBG("\r\nWARN: Discarded\r\n");
darran 0:ac21159e27f4 284 return;
darran 0:ac21159e27f4 285 }
darran 0:ac21159e27f4 286
darran 0:ac21159e27f4 287 if( r != DNS_FOUND )
darran 0:ac21159e27f4 288 {
darran 0:ac21159e27f4 289 DBG("\r\nCould not resolve hostname.\r\n");
darran 0:ac21159e27f4 290 onResult(NTP_DNS);
darran 0:ac21159e27f4 291 close();
darran 0:ac21159e27f4 292 return;
darran 0:ac21159e27f4 293 }
darran 0:ac21159e27f4 294 DBG("\r\nDNS resolved.\r\n");
darran 0:ac21159e27f4 295 m_pDnsReq->close();
darran 0:ac21159e27f4 296 delete m_pDnsReq;
darran 0:ac21159e27f4 297 m_pDnsReq=NULL;
darran 0:ac21159e27f4 298 open();
darran 0:ac21159e27f4 299 }
darran 0:ac21159e27f4 300
darran 0:ac21159e27f4 301 void NTPClient::onUDPSocketEvent(UDPSocketEvent e)
darran 0:ac21159e27f4 302 {
darran 0:ac21159e27f4 303 resetTimeout();
darran 0:ac21159e27f4 304 switch(e)
darran 0:ac21159e27f4 305 {
darran 0:ac21159e27f4 306 case UDPSOCKET_READABLE: //The only event for now
darran 0:ac21159e27f4 307 resetTimeout();
darran 0:ac21159e27f4 308 process();
darran 0:ac21159e27f4 309 break;
darran 0:ac21159e27f4 310 }
darran 0:ac21159e27f4 311 }
darran 0:ac21159e27f4 312
darran 0:ac21159e27f4 313 void NTPClient::onResult(NTPResult r) //Must be called by impl when the request completes
darran 0:ac21159e27f4 314 {
darran 0:ac21159e27f4 315 if(m_pCbItem && m_pCbMeth)
darran 0:ac21159e27f4 316 (m_pCbItem->*m_pCbMeth)(r);
darran 0:ac21159e27f4 317 else if(m_pCb)
darran 0:ac21159e27f4 318 m_pCb(r);
darran 0:ac21159e27f4 319 m_blockingResult = r; //Blocking mode
darran 0:ac21159e27f4 320 }
darran 0:ac21159e27f4 321
darran 0:ac21159e27f4 322 NTPResult NTPClient::blockingProcess() //Called in blocking mode, calls Net::poll() until return code is available
darran 0:ac21159e27f4 323 {
darran 0:ac21159e27f4 324 m_blockingResult = NTP_PROCESSING;
darran 0:ac21159e27f4 325 do
darran 0:ac21159e27f4 326 {
darran 0:ac21159e27f4 327 Net::poll();
darran 0:ac21159e27f4 328 } while(m_blockingResult == NTP_PROCESSING);
darran 0:ac21159e27f4 329 Net::poll(); //Necessary for cleanup
darran 0:ac21159e27f4 330 return m_blockingResult;
darran 0:ac21159e27f4 331 }