From 78fd0cf9cbc3b51d1a8a9436366a71f0b94a1a18 Mon Sep 17 00:00:00 2001 From: Patrick Grimm Date: Sun, 20 Nov 2022 21:27:30 +0100 Subject: [PATCH] FIX static timeGetTime, make it similar to linux port (#358) --- ports/bsd/bip-init.c | 32 ++++++++++++++++++++++++-------- ports/bsd/bip6.c | 3 ++- ports/bsd/mstimer-init.c | 2 +- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/ports/bsd/bip-init.c b/ports/bsd/bip-init.c index 9a41bea0..8ca0025a 100644 --- a/ports/bsd/bip-init.c +++ b/ports/bsd/bip-init.c @@ -42,10 +42,7 @@ #include "bacnet/basic/bbmd/h_bbmd.h" #include "bacport.h" -/** - * @file - * @brief Initializes BACnet/IP interface (BSD/MAC OS X). - */ +/** @file bsd/bip-init.c @brief Initializes BACnet/IP interface (BSD/MAC OS X). */ /* unix sockets */ static int BIP_Socket = -1; @@ -63,6 +60,8 @@ static struct in_addr BIP_Address; static struct in_addr BIP_Broadcast_Addr; /* enable debugging */ static bool BIP_Debug = false; +/* interface name */ +static char BIP_Interface_Name[IF_NAMESIZE] = { 0 }; /** * @brief Print the IPv4 address with debug info @@ -475,6 +474,19 @@ static void *get_addr_ptr(struct sockaddr *sockaddr_ptr) return addr_ptr; } +/** + * @brief Get the default interface name using routing info + * @return interface name, or NULL if not found or none +*/ +static char *ifname_default(void) +{ + if (BIP_Interface_Name[0] != 0) { + return BIP_Interface_Name; + } + strncpy(BIP_Interface_Name, "en0", sizeof(BIP_Interface_Name)); + return BIP_Interface_Name; +} + /** Gets the local IP address and local broadcast address from the system, * and saves it into the BACnet/IP data structures. * @@ -638,17 +650,22 @@ bool bip_init(char *ifname) int sock_fd = -1; if (ifname) { + strncpy(BIP_Interface_Name, ifname, sizeof(BIP_Interface_Name)); bip_set_interface(ifname); - printf("interface %s", ifname); } else { - bip_set_interface("en0"); + bip_set_interface(ifname_default()); + } + if (BIP_Address.s_addr == 0) { + fprintf(stderr, "BIP: Failed to get an IP address from %s!\n", + BIP_Interface_Name); + fflush(stderr); + return false; } sin.sin_family = AF_INET; sin.sin_port = BIP_Port; memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero)); - sin.sin_addr.s_addr = BIP_Address.s_addr; sock_fd = createSocket(&sin); BIP_Socket = sock_fd; @@ -682,7 +699,6 @@ bool bip_valid(void) */ void bip_cleanup(void) { - if (BIP_Socket != -1) { close(BIP_Socket); } diff --git a/ports/bsd/bip6.c b/ports/bsd/bip6.c index 60b35a77..6babb427 100644 --- a/ports/bsd/bip6.c +++ b/ports/bsd/bip6.c @@ -432,7 +432,7 @@ bool bip6_init(char *ifname) if (BIP6_Addr.port == 0) { bip6_set_port(0xBAC0U); } - PRINTF("BIP6: IPv6 UDP port: 0x%04X\n", htons(BIP6_Addr.port)); + PRINTF("BIP6: IPv6 UDP port: 0x%04X\n", BIP6_Addr.port); if (BIP6_Broadcast_Addr.address[0] == 0) { bvlc6_address_set(&BIP6_Broadcast_Addr, BIP6_MULTICAST_SITE_LOCAL, 0, 0, 0, 0, 0, 0, BIP6_MULTICAST_GROUP_ID); @@ -476,6 +476,7 @@ bool bip6_init(char *ifname) server.sin6_family = AF_INET6; server.sin6_addr = in6addr_any; server.sin6_port = htons(BIP6_Addr.port); + debug_print_ipv6("Binding->", &server.sin6_addr); status = bind(BIP6_Socket, (const void *)&server, sizeof(server)); if (status < 0) { perror("BIP: bind"); diff --git a/ports/bsd/mstimer-init.c b/ports/bsd/mstimer-init.c index 3196c94a..a6361f92 100644 --- a/ports/bsd/mstimer-init.c +++ b/ports/bsd/mstimer-init.c @@ -38,7 +38,7 @@ static struct timespec start; /* The timeGetTime function retrieves the system time, in milliseconds. The system time is the time elapsed since the OS was started. */ -unsigned long timeGetTime(void) +static unsigned long timeGetTime(void) { struct timespec now; unsigned long ticks;