I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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