Ticket #12319: 0001-update-ntp-to-used-BNetworkAddressResolver.patch

File 0001-update-ntp-to-used-BNetworkAddressResolver.patch, 2.3 KB (added by a-star, 7 years ago)

updated ntp to use BNetworkAddressResolver

  • src/preferences/time/ntp.cpp

    From 12356d5e2b9331bb04f13578ccd1727a39bb1f4e Mon Sep 17 00:00:00 2001
    From: A-star-ayush <myselfthebest@yahoo.com>
    Date: Tue, 21 Mar 2017 21:54:39 +0530
    Subject: [PATCH] update ntp to used BNetworkAddressResolver
    
    ---
     src/preferences/time/ntp.cpp | 24 ++++++++++++------------
     1 file changed, 12 insertions(+), 12 deletions(-)
    
    diff --git a/src/preferences/time/ntp.cpp b/src/preferences/time/ntp.cpp
    index 167ef2c..c7968f6 100644
    a b  
    55 */
    66
    77#include "ntp.h"
     8#include <NetworkAddress.h>
     9#include <NetworkAddressResolver.h>
    810
    911#include <errno.h>
    1012#include <netdb.h>
    status_t  
    114116ntp_update_time(const char* hostname, const char** errorString,
    115117    int32* errorCode)
    116118{
    117     hostent *server = gethostbyname(hostname);
     119    BNetworkAddressResolver resolver(hostname, NTP_PORT);
     120    BNetworkAddress address;
     121    uint32 cookie = 0;
     122
     123    if ( resolver.InitCheck() != B_OK ||
     124        resolver.GetNextAddress(&cookie, address) != B_OK) {
    118125
    119     if (server == NULL) {
    120    
    121126        *errorString = B_TRANSLATE("Could not contact server");
    122127        return B_ENTRY_NOT_FOUND;
    123128    }
    ntp_update_time(const char* hostname, const char** errorString,  
    144149        return B_ERROR;
    145150    }
    146151
    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];
    151 
    152     if (sendto(connection, (char *)&message, sizeof(ntp_data),
    153             0, (struct sockaddr *)&address, sizeof(address)) < 0) {
     152    if (sendto(connection, reinterpret_cast<char *>(&message), sizeof(ntp_data),
     153        0, &address.SockAddr(), address.Length()) < 0) {
    154154        *errorString = B_TRANSLATE("Sending request failed");
    155155        *errorCode = errno;
    156156        close(connection);
    ntp_update_time(const char* hostname, const char** errorString,  
    176176    message.transmit_timestamp.SetTo(0);
    177177
    178178    socklen_t addressSize = sizeof(address);
    179     if (recvfrom(connection, (char *)&message, sizeof(ntp_data), 0,
    180             (sockaddr *)&address, &addressSize) < (ssize_t)sizeof(ntp_data)) {
     179    if (recvfrom(connection, reinterpret_cast<char *>(&message), sizeof(ntp_data),
     180            0, &address.SockAddr(), address.Length()) < (ssize_t)sizeof(ntp_data)) {
    181181        *errorString = B_TRANSLATE("Message receiving failed");
    182182        *errorCode = errno;
    183183        close(connection);