[haiku-commits] haiku: hrev51390 - src/preferences/time

  • From: waddlesplash@xxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Thu, 31 Aug 2017 02:04:28 +0200 (CEST)

hrev51390 adds 1 changeset to branch 'master'
old head: ed3f47fe2e7865deda80318518ae6628218d4ee0
new head: bb382cf81659d1239aeb067f53c88ee0963b6a24
overview: 
http://cgit.haiku-os.org/haiku/log/?qt=range&q=bb382cf81659+%5Eed3f47fe2e78

----------------------------------------------------------------------------

bb382cf81659: Preferences/Time: Use BNetworkAddressResolver.
  
  Signed-off-by: Augustin Cavalier <waddlesplash@xxxxxxxxx>
  Some coding style changes by me.
  Fixes #12319.

                                  [ A-star-ayush <myselfthebest@xxxxxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev51390
Commit:      bb382cf81659d1239aeb067f53c88ee0963b6a24
URL:         http://cgit.haiku-os.org/haiku/commit/?id=bb382cf81659
Author:      A-star-ayush <myselfthebest@xxxxxxxxx>
Date:        Sun Apr 23 18:52:37 2017 UTC
Committer:   Augustin Cavalier <waddlesplash@xxxxxxxxx>
Commit-Date: Thu Aug 31 00:03:18 2017 UTC

Ticket:      https://dev.haiku-os.org/ticket/12319

----------------------------------------------------------------------------

2 files changed, 21 insertions(+), 15 deletions(-)
src/preferences/time/Jamfile |  2 +-
src/preferences/time/ntp.cpp | 34 ++++++++++++++++++++--------------

----------------------------------------------------------------------------

diff --git a/src/preferences/time/Jamfile b/src/preferences/time/Jamfile
index 291dde2..e31fb1b 100644
--- a/src/preferences/time/Jamfile
+++ b/src/preferences/time/Jamfile
@@ -32,7 +32,7 @@ Includes [ FGristFiles $(sources) ] : [ BuildFeatureAttribute 
icu : headers ] ;
 
 Preference Time
        : $(sources)
-       : be shared [ TargetLibstdc++ ] localestub $(HAIKU_NETWORK_LIBS)
+       : be bnetapi network shared [ TargetLibstdc++ ] localestub
        : Time.rdef
        ;
 
diff --git a/src/preferences/time/ntp.cpp b/src/preferences/time/ntp.cpp
index 167ef2c..d07b402 100644
--- a/src/preferences/time/ntp.cpp
+++ b/src/preferences/time/ntp.cpp
@@ -18,6 +18,8 @@
 #include <OS.h>
 
 #include <Catalog.h>
+#include <NetworkAddress.h>
+#include <NetworkAddressResolver.h>
 
 
 #undef B_TRANSLATION_CONTEXT
@@ -114,11 +116,13 @@ status_t
 ntp_update_time(const char* hostname, const char** errorString,
        int32* errorCode)
 {
-       hostent *server = gethostbyname(hostname);
+       BNetworkAddressResolver resolver(hostname, NTP_PORT);
+       BNetworkAddress address;
+       uint32 cookie = 0;
+       bool success = false;
 
-       if (server == NULL) {
-       
-               *errorString = B_TRANSLATE("Could not contact server");
+       if (resolver.InitCheck() != B_OK) {
+               *errorString = B_TRANSLATE("Could not resolve server address");
                return B_ENTRY_NOT_FOUND;
        }
 
@@ -144,15 +148,17 @@ ntp_update_time(const char* hostname, const char** 
errorString,
                return B_ERROR;
        }
 
-       struct sockaddr_in address;
-       address.sin_family = AF_INET;
-       address.sin_port = htons(NTP_PORT);
-       address.sin_addr.s_addr = *(uint32 *)server->h_addr_list[0];
+       while (resolver.GetNextAddress(&cookie, address) == B_OK) {
+               if (sendto(connection, reinterpret_cast<char*>(&message),
+                               sizeof(ntp_data), 0, &address.SockAddr(),
+                               address.Length()) != -1) {
+                       success = true;
+                       break;
+               }
+       }
 
-       if (sendto(connection, (char *)&message, sizeof(ntp_data),
-                       0, (struct sockaddr *)&address, sizeof(address)) < 0) {
+       if (!success) {
                *errorString = B_TRANSLATE("Sending request failed");
-               *errorCode = errno;
                close(connection);
                return B_ERROR;
        }
@@ -175,9 +181,9 @@ ntp_update_time(const char* hostname, const char** 
errorString,
 
        message.transmit_timestamp.SetTo(0);
 
-       socklen_t addressSize = sizeof(address);
-       if (recvfrom(connection, (char *)&message, sizeof(ntp_data), 0,
-                       (sockaddr *)&address, &addressSize) < 
(ssize_t)sizeof(ntp_data)) {
+       socklen_t addressSize = address.Length();
+       if (recvfrom(connection, reinterpret_cast<char*>(&message), 
sizeof(ntp_data), 0,
+                       &address.SockAddr(), &addressSize) < 
(ssize_t)sizeof(ntp_data)) {
                *errorString = B_TRANSLATE("Message receiving failed");
                *errorCode = errno;
                close(connection);


Other related posts:

  • » [haiku-commits] haiku: hrev51390 - src/preferences/time - waddlesplash