From: Mike Brady <4265913+mikebrady@users.noreply.github.com> Date: Tue, 22 Jun 2021 09:51:37 +0000 (+0100) Subject: Add support for FreeBSD. No installer yet. X-Git-Tag: 1.2~113^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c02098d47ef2d196966dc377404219c723236234;p=thirdparty%2Fnqptp.git Add support for FreeBSD. No installer yet. --- diff --git a/Makefile.am b/Makefile.am index 61b6e27..43ff9b7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,7 +1,7 @@ bin_PROGRAMS = nqptp nqptp_SOURCES = nqptp.c nqptp-clock-sources.c nqptp-message-handlers.c nqptp-utilities.c general-utilities.c debug.c -AM_CFLAGS = -fno-common -Wno-multichar -Wall -Wextra -Wno-clobbered -Wno-psabi -pthread +AM_CFLAGS = -fno-common -Wall -Wextra -pthread --include=config.h CLEANFILES = if USE_GIT @@ -18,5 +18,9 @@ endif install-exec-hook: +if BUILD_FOR_LINUX [ -e /lib/systemd/system ] || mkdir -p /lib/systemd/system cp nqptp.service /lib/systemd/system +endif + # no installer for FreeBSD yet + diff --git a/configure.ac b/configure.ac index 2efff05..46ec73d 100644 --- a/configure.ac +++ b/configure.ac @@ -4,6 +4,34 @@ AC_PREREQ([2.68]) AC_INIT([nqptp], [1.1-dev], [4265913+mikebrady@users.noreply.github.com]) AM_INIT_AUTOMAKE +AC_CANONICAL_HOST + +build_linux=no +build_freebsd=no + +# Detect the target system +case "${host_os}" in + linux*) + build_linux=yes + ;; + freebsd*) + build_freebsd=yes + ;; + *) + AC_MSG_ERROR(["OS $host_os is not supported"]) + ;; +esac + +# Pass the conditionals to automake +AM_CONDITIONAL([BUILD_FOR_LINUX], [test "$build_linux" = "yes"]) +AM_CONDITIONAL([BUILD_FOR_FREEBSD], [test "$build_freebsd" = "yes"]) + +if test "x$build_linux" = "xyes" ; then + AC_DEFINE([CONFIG_FOR_LINUX], 1, [Build for Linux.]) +fi +if test "x$build_freebsd" = "xyes" ; then + AC_DEFINE([CONFIG_FOR_FREEBSD], 1, [Build for FreeBSD.]) +fi AC_CHECK_PROGS([GIT], [git]) if test -n "$GIT"; then diff --git a/nqptp-clock-sources.c b/nqptp-clock-sources.c index 3d05020..ae81e27 100644 --- a/nqptp-clock-sources.c +++ b/nqptp-clock-sources.c @@ -26,6 +26,11 @@ #include #include +#ifdef CONFIG_FOR_FREEBSD +#include +#include +#endif + #ifndef FIELD_SIZEOF #define FIELD_SIZEOF(t, f) (sizeof(((t *)0)->f)) #endif diff --git a/nqptp-utilities.c b/nqptp-utilities.c index 72f8e0c..07367ec 100644 --- a/nqptp-utilities.c +++ b/nqptp-utilities.c @@ -21,7 +21,19 @@ #include #include // fcntl etc. #include // getifaddrs + +#ifdef CONFIG_FOR_LINUX #include // sockaddr_ll +#endif + +#ifdef CONFIG_FOR_FREEBSD +#include +#include +#include +#include +#include +#endif + #include // getaddrinfo etc. #include // snprintf #include // malloc, free @@ -141,6 +153,7 @@ void debug_print_buffer(int level, char *buf, size_t buf_len) { } } + uint64_t get_self_clock_id() { // make up a clock ID based on an interfaces' MAC char local_clock_id[8]; @@ -153,6 +166,7 @@ uint64_t get_self_clock_id() { } else { int found = 0; for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { +#ifdef AF_PACKET if ((ifa->ifa_addr) && (ifa->ifa_addr->sa_family == AF_PACKET)) { struct sockaddr_ll *s = (struct sockaddr_ll *)ifa->ifa_addr; if ((strcmp(ifa->ifa_name, "lo") != 0) && (found == 0)) { @@ -161,6 +175,24 @@ uint64_t get_self_clock_id() { found = 1; } } +#else +// This AF_LINK stuff hasn't been tested! +#ifdef AF_LINK + struct sockaddr_dl * sdl = (struct sockaddr_dl *) ifa->ifa_addr; + if ((sdl) && (sdl->sdl_family == AF_LINK)) { + if (sdl->sdl_type == IFT_ETHER) { + char *s = LLADDR(sdl); + int i; + for (i = 0; i < sdl->sdl_alen; i++) { + debug(1,"char %d: \"%c\".", i, *s); + // *t++ = (uint8_t)*s++; + } + found = 1; + } + } + +#endif +#endif } freeifaddrs(ifaddr); } @@ -178,4 +210,4 @@ uint64_t get_self_clock_id() { uint64_t result; memcpy(&result, local_clock_id, sizeof(result)); return result; -} \ No newline at end of file +} diff --git a/nqptp.c b/nqptp.c index 09eb274..603564a 100644 --- a/nqptp.c +++ b/nqptp.c @@ -43,6 +43,11 @@ #include // SIGTERM and stuff like that +#ifdef CONFIG_FOR_FREEBSD +#include +#include +#endif + #ifndef FIELD_SIZEOF #define FIELD_SIZEOF(t, f) (sizeof(((t *)0)->f)) #endif @@ -195,9 +200,19 @@ int main(int argc, char **argv) { if (ftruncate(shm_fd, sizeof(struct shm_structure)) == -1) { die("failed to set size of shared memory \"%s\".", STORAGE_ID); } + +#ifdef CONFIG_FOR_FREEBSD + shared_memory = + (struct shm_structure *)mmap(NULL, sizeof(struct shm_structure), PROT_READ | PROT_WRITE, + MAP_SHARED, shm_fd, 0); +#endif + +#ifdef CONFIG_FOR_LINUX shared_memory = (struct shm_structure *)mmap(NULL, sizeof(struct shm_structure), PROT_READ | PROT_WRITE, MAP_LOCKED | MAP_SHARED, shm_fd, 0); +#endif + if (shared_memory == (struct shm_structure *)-1) { die("failed to mmap shared memory \"%s\".", STORAGE_ID); }