ntpquery: Use AK::convert_between_host_and_network_endian

Instead of polluting global namespace with definitions from
libkern/OSByteOrder.h and machine/endian.h on MacOS, just use AK
functions for conversions.
This commit is contained in:
Dan Klishch 2023-11-19 18:19:11 -05:00 committed by Andrew Kaster
parent 6b08b43a1e
commit 677bcea771
Notes: sideshowbarker 2024-07-17 08:45:34 +09:00
2 changed files with 9 additions and 31 deletions

View File

@ -10,30 +10,6 @@
#include <AK/Forward.h>
#include <AK/Platform.h>
#if defined(AK_OS_MACOS)
# include <libkern/OSByteOrder.h>
# include <machine/endian.h>
# define htobe16(x) OSSwapHostToBigInt16(x)
# define htole16(x) OSSwapHostToLittleInt16(x)
# define be16toh(x) OSSwapBigToHostInt16(x)
# define le16toh(x) OSSwapLittleToHostInt16(x)
# define htobe32(x) OSSwapHostToBigInt32(x)
# define htole32(x) OSSwapHostToLittleInt32(x)
# define be32toh(x) OSSwapBigToHostInt32(x)
# define le32toh(x) OSSwapLittleToHostInt32(x)
# define htobe64(x) OSSwapHostToBigInt64(x)
# define htole64(x) OSSwapHostToLittleInt64(x)
# define be64toh(x) OSSwapBigToHostInt64(x)
# define le64toh(x) OSSwapLittleToHostInt64(x)
# define __BIG_ENDIAN BIG_ENDIAN
# define __LITTLE_ENDIAN LITTLE_ENDIAN
# define __BYTE_ORDER BYTE_ORDER
#endif
namespace AK {
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
inline constexpr static bool HostIsLittleEndian = true;

View File

@ -29,6 +29,8 @@
// The fractional part in the lower 32 bits stores fractional bits times 2 ** 32.
using NtpTimestamp = uint64_t;
using AK::convert_between_host_and_network_endian;
struct [[gnu::packed]] NtpPacket {
uint8_t li_vn_mode;
uint8_t stratum;
@ -161,7 +163,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
sockaddr_in peer_address;
memset(&peer_address, 0, sizeof(peer_address));
peer_address.sin_family = AF_INET;
peer_address.sin_port = htons(123);
peer_address.sin_port = convert_between_host_and_network_endian<short>(123);
peer_address.sin_addr.s_addr = *(in_addr_t const*)hostent->h_addr_list[0];
NtpPacket packet;
@ -242,8 +244,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
}
NtpTimestamp origin_timestamp = ntp_timestamp_from_timeval(local_transmit_time);
NtpTimestamp receive_timestamp = be64toh(packet.receive_timestamp);
NtpTimestamp transmit_timestamp = be64toh(packet.transmit_timestamp);
NtpTimestamp receive_timestamp = convert_between_host_and_network_endian(packet.receive_timestamp);
NtpTimestamp transmit_timestamp = convert_between_host_and_network_endian(packet.transmit_timestamp);
#ifdef SO_TIMESTAMP
NtpTimestamp destination_timestamp = ntp_timestamp_from_timeval(kernel_receive_time);
@ -270,17 +272,17 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
outln("Stratum: {}", packet.stratum);
outln("Poll: {}", packet.stratum);
outln("Precision: {}", packet.precision);
outln("Root delay: {:x}", ntohl(packet.root_delay));
outln("Root dispersion: {:x}", ntohl(packet.root_dispersion));
outln("Root delay: {:x}", convert_between_host_and_network_endian(packet.root_delay));
outln("Root dispersion: {:x}", convert_between_host_and_network_endian(packet.root_dispersion));
u32 ref_id = ntohl(packet.reference_id);
u32 ref_id = convert_between_host_and_network_endian(packet.reference_id);
out("Reference ID: {:x}", ref_id);
if (packet.stratum == 1) {
out(" ('{:c}{:c}{:c}{:c}')", (ref_id & 0xff000000) >> 24, (ref_id & 0xff0000) >> 16, (ref_id & 0xff00) >> 8, ref_id & 0xff);
}
outln();
outln("Reference timestamp: {:#016x} ({})", be64toh(packet.reference_timestamp), format_ntp_timestamp(be64toh(packet.reference_timestamp)).characters());
outln("Reference timestamp: {:#016x} ({})", convert_between_host_and_network_endian(packet.reference_timestamp), format_ntp_timestamp(convert_between_host_and_network_endian(packet.reference_timestamp)).characters());
outln("Origin timestamp: {:#016x} ({})", origin_timestamp, format_ntp_timestamp(origin_timestamp).characters());
outln("Receive timestamp: {:#016x} ({})", receive_timestamp, format_ntp_timestamp(receive_timestamp).characters());
outln("Transmit timestamp: {:#016x} ({})", transmit_timestamp, format_ntp_timestamp(transmit_timestamp).characters());