NTP client memory leak fix.
[novacoin.git] / src / ntp.cpp
index 4cc422b..5263c5a 100644 (file)
@@ -80,9 +80,9 @@ struct pkt {
   uint8_t  mac[5 * sizeof(uint32_t)]; /* mac */
 };
 
-const int nServersCount = 147;
+const int nServersCount = 162;
 
-std::string NtpServers[147] = {
+std::string NtpServers[162] = {
     // Microsoft
     "time.windows.com",
 
@@ -151,10 +151,12 @@ std::string NtpServers[147] = {
     "ntp-03.caltech.edu",
     "ntp-04.caltech.edu",
     "nist1-pa.ustiming.org",
-    "time-a.nist.gov ",
-    "time-b.nist.gov ",
-    "time-c.nist.gov ",
-    "time-d.nist.gov ",
+    "time.nist.gov",
+    "time-a.nist.gov",
+    "time-b.nist.gov",
+    "time-c.nist.gov",
+    "time-d.nist.gov",
+    "time-nw.nist.gov",
     "nist1-macon.macon.ga.us",
     "nist.netservicesgroup.com",
     "nisttime.carsoncity.k12.mi.us",
@@ -163,7 +165,6 @@ std::string NtpServers[147] = {
     "time-a.timefreq.bldrdoc.gov",
     "time-b.timefreq.bldrdoc.gov",
     "time-c.timefreq.bldrdoc.gov",
-    "time.nist.gov",
     "utcnist.colorado.edu",
     "utcnist2.colorado.edu",
     "ntp-nist.ldsbc.net",
@@ -178,6 +179,13 @@ std::string NtpServers[147] = {
     "1.us.pool.ntp.org",
     "2.us.pool.ntp.org",
     "3.us.pool.ntp.org",
+    "wwv.otc.psu.edu",
+    "otc1.psu.edu",
+    "otc2.psu.edu",
+    "now.okstate.edu",
+    "ntp.colby.edu",
+    "bonehed.lcs.mit.edu",
+    "ntp-s1.cise.ufl.edu",
 
     // South Africa
     "ntp1.meraka.csir.co.za",
@@ -197,6 +205,9 @@ std::string NtpServers[147] = {
     "3.za.pool.ntp.org",
 
     // Italy
+    "ntp0.ien.it",
+    "ntp1.ien.it",
+    "ntp2.ien.it",
     "ntp1.inrim.it",
     "ntp2.inrim.it",
     "0.it.pool.ntp.org",
@@ -257,6 +268,12 @@ std::string NtpServers[147] = {
     // Slovenia
     "time.ijs.si",
 
+    // Austria
+    "0.at.pool.ntp.org",
+    "1.at.pool.ntp.org",
+    "2.at.pool.ntp.org",
+    "3.at.pool.ntp.org",
+
     // ???
     "clepsydra.dec.com",
 
@@ -316,6 +333,8 @@ bool InitWithRandom(SOCKET &sockfd, socklen_t &servlen, struct sockaddr *pcliadd
 }
 
 int64_t DoReq(SOCKET sockfd, socklen_t servlen, struct sockaddr cliaddr) {
+
+
 #ifdef WIN32
     u_long nOne = 1;
     if (ioctlsocket(sockfd, FIONBIO, &nOne) == SOCKET_ERROR) {
@@ -327,8 +346,11 @@ int64_t DoReq(SOCKET sockfd, socklen_t servlen, struct sockaddr cliaddr) {
         return -2;
     }
 
+    struct timeval timeout = {10, 0};
     struct pkt *msg = new pkt;
     struct pkt *prt  = new pkt;
+    time_t seconds_transmit;
+    int len = 48;
 
     msg->li_vn_mode=227;
     msg->stratum=0;
@@ -346,29 +368,30 @@ int64_t DoReq(SOCKET sockfd, socklen_t servlen, struct sockaddr cliaddr) {
     msg->xmt.Ul_i.Xl_i=0;
     msg->xmt.Ul_f.Xl_f=0;
 
-    int len=48;
     int retcode = sendto(sockfd, (char *) msg, len, 0, &cliaddr, servlen);
     if (retcode < 0) {
         printf("sendto() failed: %d\n", retcode);
-        return -3;
+        seconds_transmit = -3;
+        goto _end;
     }
 
     fd_set fdset;
-    struct timeval timeout = {10, 0};
     FD_ZERO(&fdset);
     FD_SET(sockfd, &fdset);
 
     retcode = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
     if (retcode <= 0) {
         printf("recvfrom() error\n");
-        return -4;
+        seconds_transmit = -4;
+        goto _end;
     }
 
     recvfrom(sockfd, (char *) msg, len, 0, NULL, NULL);
     ntohl_fp(&msg->xmt, &prt->xmt);
-    time_t seconds_transmit;
     Ntp2Unix(prt->xmt.Ul_i.Xl_ui, seconds_transmit);
 
+    _end:
+
     delete msg;
     delete prt;