Skip to content

Fix build for macOS where endian.h is not available #1983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions common/network/NetworkUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ typedef uint32_t in_addr_t;

#ifdef HAVE_ENDIAN_H
#include <endian.h>
#elif defined(__APPLE__)
#include <libkern/OSByteOrder.h>
Comment on lines +63 to +64
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switch this to including OSByteOrder.h if its present.

#define be64toh(x) OSSwapBigToHostInt64(x)
#define htobe64(x) OSSwapHostToBigInt64(x)
Comment on lines +65 to +66
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So remove this:

Suggested change
#define be64toh(x) OSSwapBigToHostInt64(x)
#define htobe64(x) OSSwapHostToBigInt64(x)

#endif // HAVE_ENDIAN_H
#include <errno.h>
#include <limits.h>
Expand Down Expand Up @@ -160,11 +164,11 @@ uint32_t NetworkToHost(uint32_t value) {
}

uint64_t NetworkToHost(uint64_t value) {
#ifdef HAVE_ENDIAN_H
#if defined(HAVE_ENDIAN_H) || defined(__APPLE__)
return be64toh(value);
#else
#error "No be64toh for NetworkToHost, please report this."
#endif // HAVE_ENDIAN_H
#endif // defined(HAVE_ENDIAN_H) || defined(__APPLE__)
Comment on lines -163 to +171
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then switch this, and the following cases, to something more like

#ifdef USE_SYSCTL_FOR_DEFAULT_ROUTE
return GetDefaultRouteWithSysctl(if_index, default_gateway);
#elif defined(USE_NETLINK_FOR_DEFAULT_ROUTE)
return GetDefaultRouteWithNetlink(if_index, default_gateway);
#elif defined(_WIN32)
ULONG size = 4096;
PMIB_IPFORWARDTABLE forward_table =
reinterpret_cast<PMIB_IPFORWARDTABLE>(malloc(size));
DWORD result = GetIpForwardTable(forward_table, &size, TRUE);
if (result == NO_ERROR) {
for (unsigned int i = 0; i < forward_table->dwNumEntries; ++i) {
if (forward_table->table[i].dwForwardDest == 0) {
*default_gateway =
IPV4Address(forward_table->table[i].dwForwardNextHop);
*if_index = forward_table->table[i].dwForwardIfIndex;
}
}
free(forward_table);
return true;
} else {
OLA_WARN << "GetIpForwardTable failed with " << GetLastError();
return false;
}
#else
#error "DefaultRoute not implemented for this platform, please report this."
// TODO(Peter): Do something else on machines without Netlink
// No Netlink, can't do anything
return false;
#endif // USE_SYSCTL_FOR_DEFAULT_ROUTE
(but I don't think we need the extra wrapper functions here).

}

int16_t NetworkToHost(int16_t value) {
Expand All @@ -176,11 +180,11 @@ int32_t NetworkToHost(int32_t value) {
}

int64_t NetworkToHost(int64_t value) {
#ifdef HAVE_ENDIAN_H
#if defined(HAVE_ENDIAN_H) || defined(__APPLE__)
return be64toh(value);
#else
#error "No be64toh for NetworkToHost, please report this."
#endif // HAVE_ENDIAN_H
#endif // defined(HAVE_ENDIAN_H) || defined(__APPLE__)
}

uint16_t HostToNetwork(uint16_t value) {
Expand All @@ -200,19 +204,19 @@ int32_t HostToNetwork(int32_t value) {
}

uint64_t HostToNetwork(uint64_t value) {
#ifdef HAVE_ENDIAN_H
#if defined(HAVE_ENDIAN_H) || defined(__APPLE__)
return htobe64(value);
#else
#error "No htobe64 for HostToNetwork, please report this."
#endif // HAVE_ENDIAN_H
#endif // defined(HAVE_ENDIAN_H) || defined(__APPLE__)
}

int64_t HostToNetwork(int64_t value) {
#ifdef HAVE_ENDIAN_H
#if defined(HAVE_ENDIAN_H) || defined(__APPLE__)
return htobe64(value);
#else
#error "No htobe64 for HostToNetwork, please report this."
#endif // HAVE_ENDIAN_H
#endif // defined(HAVE_ENDIAN_H) || defined(__APPLE__)
}

uint16_t HostToLittleEndian(uint16_t value) {
Expand Down