]> git.ipfire.org Git - thirdparty/nqptp.git/commitdiff
Add support for FreeBSD. No installer yet.
authorMike Brady <4265913+mikebrady@users.noreply.github.com>
Tue, 22 Jun 2021 09:51:37 +0000 (10:51 +0100)
committerMike Brady <4265913+mikebrady@users.noreply.github.com>
Tue, 22 Jun 2021 09:51:37 +0000 (10:51 +0100)
Makefile.am
configure.ac
nqptp-clock-sources.c
nqptp-utilities.c
nqptp.c

index 61b6e2780a2ad6c304014c13a546f9e4f04b373e..43ff9b78dcb0c76e21217b3a0b8b2293114cfe47 100644 (file)
@@ -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
index 2efff05650bb4cf43e38a185a64c81e4060d17bf..46ec73d890b038cce4b68139d57bb122a6689b95 100644 (file)
@@ -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
index 3d05020e626dc1eac1db349ae51176f34ef3a085..ae81e27a33757d6cf8bef1a0e4592b407057ec59 100644 (file)
 #include <string.h>
 #include <sys/types.h>
 
+#ifdef CONFIG_FOR_FREEBSD
+#include <sys/socket.h>
+#include <netinet/in.h>
+#endif
+
 #ifndef FIELD_SIZEOF
 #define FIELD_SIZEOF(t, f) (sizeof(((t *)0)->f))
 #endif
index 72f8e0ccabaa773d666741aec46b9e5f4869d5a2..07367ecb466d89e7662432af9f8ffd270512beae 100644 (file)
 #include <errno.h>
 #include <fcntl.h>           // fcntl etc.
 #include <ifaddrs.h>         // getifaddrs
+
+#ifdef CONFIG_FOR_LINUX
 #include <linux/if_packet.h> // sockaddr_ll
+#endif
+
+#ifdef CONFIG_FOR_FREEBSD
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <net/if_types.h>
+#include <net/if_dl.h>
+#endif
+
 #include <netdb.h>           // getaddrinfo etc.
 #include <stdio.h>           // snprintf
 #include <stdlib.h>          // 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 09eb274bed02e492e9cbbfd268e42539e2e4fecb..603564a543c5af335a761b5cd34a2d047bd3510b 100644 (file)
--- a/nqptp.c
+++ b/nqptp.c
 
 #include <signal.h> // SIGTERM and stuff like that
 
+#ifdef CONFIG_FOR_FREEBSD
+#include <sys/socket.h>
+#include <netinet/in.h>
+#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);
   }