From 79b0009ac64e25f4e6db94501a4786483d1546a4 Mon Sep 17 00:00:00 2001
From: A-star-ayush <myselfthebest@yahoo.com>
Date: Mon, 24 Apr 2017 00:22:37 +0530
Subject: [PATCH] fixed ntp.cpp
---
src/preferences/time/Jamfile | 4 ++--
src/preferences/time/ntp.cpp | 36 ++++++++++++++++++++----------------
2 files changed, 22 insertions(+), 18 deletions(-)
diff --git a/src/preferences/time/Jamfile b/src/preferences/time/Jamfile
index 291dde2..19fcca1 100644
a
|
b
|
SubDir HAIKU_TOP src preferences time ;
|
2 | 2 | |
3 | 3 | SetSubDirSupportedPlatformsBeOSCompatible ; |
4 | 4 | |
5 | | UsePrivateHeaders interface locale shared [ FDirName libroot time ] ; |
| 5 | UsePrivateHeaders interface locale net shared [ FDirName libroot time ] ; |
6 | 6 | UsePrivateSystemHeaders ; |
7 | 7 | UseBuildFeatureHeaders icu ; |
8 | 8 | |
… |
… |
Includes [ FGristFiles $(sources) ] : [ BuildFeatureAttribute icu : headers ] ;
|
32 | 32 | |
33 | 33 | Preference Time |
34 | 34 | : $(sources) |
35 | | : be shared [ TargetLibstdc++ ] localestub $(HAIKU_NETWORK_LIBS) |
| 35 | : be bnetapi shared [ TargetLibstdc++ ] localestub $(HAIKU_NETWORK_LIBS) |
36 | 36 | : Time.rdef |
37 | 37 | ; |
38 | 38 | |
diff --git a/src/preferences/time/ntp.cpp b/src/preferences/time/ntp.cpp
index 167ef2c..92c6078 100644
a
|
b
|
|
18 | 18 | #include <OS.h> |
19 | 19 | |
20 | 20 | #include <Catalog.h> |
21 | | |
| 21 | #include <NetworkAddress.h> |
| 22 | #include <NetworkAddressResolver.h> |
22 | 23 | |
23 | 24 | #undef B_TRANSLATION_CONTEXT |
24 | 25 | #define B_TRANSLATION_CONTEXT "Time" |
… |
… |
status_t
|
114 | 115 | ntp_update_time(const char* hostname, const char** errorString, |
115 | 116 | int32* errorCode) |
116 | 117 | { |
117 | | hostent *server = gethostbyname(hostname); |
| 118 | BNetworkAddressResolver resolver(hostname, NTP_PORT); |
| 119 | BNetworkAddress address; |
| 120 | uint32 cookie = 0; |
| 121 | bool success = false; |
118 | 122 | |
119 | | if (server == NULL) { |
120 | | |
121 | | *errorString = B_TRANSLATE("Could not contact server"); |
| 123 | if (resolver.InitCheck() != B_OK) { |
| 124 | *errorString = B_TRANSLATE("Could not resolve server address"); |
122 | 125 | return B_ENTRY_NOT_FOUND; |
123 | 126 | } |
124 | 127 | |
… |
… |
ntp_update_time(const char* hostname, const char** errorString,
|
144 | 147 | return B_ERROR; |
145 | 148 | } |
146 | 149 | |
147 | | struct sockaddr_in address; |
148 | | address.sin_family = AF_INET; |
149 | | address.sin_port = htons(NTP_PORT); |
150 | | address.sin_addr.s_addr = *(uint32 *)server->h_addr_list[0]; |
| 150 | while(resolver.GetNextAddress(&cookie, address) == B_OK) { |
| 151 | if (sendto(connection, reinterpret_cast<char *>(&message), sizeof(ntp_data), |
| 152 | 0, &address.SockAddr(), address.Length()) != -1) { |
| 153 | success = true; |
| 154 | break; |
| 155 | } |
| 156 | } |
151 | 157 | |
152 | | if (sendto(connection, (char *)&message, sizeof(ntp_data), |
153 | | 0, (struct sockaddr *)&address, sizeof(address)) < 0) { |
| 158 | if(!success) { |
154 | 159 | *errorString = B_TRANSLATE("Sending request failed"); |
155 | | *errorCode = errno; |
156 | 160 | close(connection); |
157 | 161 | return B_ERROR; |
158 | 162 | } |
… |
… |
ntp_update_time(const char* hostname, const char** errorString,
|
175 | 179 | |
176 | 180 | message.transmit_timestamp.SetTo(0); |
177 | 181 | |
178 | | socklen_t addressSize = sizeof(address); |
179 | | if (recvfrom(connection, (char *)&message, sizeof(ntp_data), 0, |
180 | | (sockaddr *)&address, &addressSize) < (ssize_t)sizeof(ntp_data)) { |
| 182 | socklen_t addressSize = address.Length(); |
| 183 | if (recvfrom(connection, reinterpret_cast<char *>(&message), sizeof(ntp_data), 0, |
| 184 | &address.SockAddr(), &addressSize) < (ssize_t)sizeof(ntp_data)) { |
181 | 185 | *errorString = B_TRANSLATE("Message receiving failed"); |
182 | 186 | *errorCode = errno; |
183 | 187 | close(connection); |
… |
… |
ntp_update_time(const char* hostname, const char** errorString,
|
194 | 198 | time_t now = message.transmit_timestamp.Integer() - kSecondsBetween1900And1970; |
195 | 199 | set_real_time_clock(now); |
196 | 200 | return B_OK; |
197 | | } |
| 201 | } |
| 202 | No newline at end of file |