bip: decouple broadcast destination port from bind port (#1311)

Co-authored-by: Leunar Kalludra <Leunar.Kalludra@de.bosch.com>
This commit is contained in:
Leunar Kalludra
2026-04-24 13:40:32 +02:00
committed by GitHub
parent 8607b241e9
commit f616a8cc68
22 changed files with 469 additions and 17 deletions
+28 -2
View File
@@ -41,6 +41,8 @@ static int BIP_Broadcast_Socket = -1;
/* port to use - stored here in network byte order */
/* Initialize to 0 - this will force initialization in demo apps */
static uint16_t BIP_Port;
/* broadcast destination port to use */
static uint16_t BIP_Broadcast_Port;
/* IP address - stored here in network byte order */
static struct in_addr BIP_Address;
/* IP broadcast address - stored here in network byte order */
@@ -119,6 +121,15 @@ void bip_set_port(uint16_t port)
BIP_Port = htons(port);
}
/**
* @brief Set the BACnet IPv4 UDP broadcast destination port number
* @param port - IPv4 UDP port number - in host byte order
*/
void bip_set_broadcast_port(uint16_t port)
{
BIP_Broadcast_Port = htons(port);
}
/**
* @brief Get the BACnet IPv4 UDP port number
* @return IPv4 UDP port number - in host byte order
@@ -128,6 +139,19 @@ uint16_t bip_get_port(void)
return ntohs(BIP_Port);
}
/**
* @brief Get the BACnet IPv4 UDP broadcast destination port number
* @return IPv4 UDP port number - in host byte order
*/
uint16_t bip_get_broadcast_port(void)
{
if (BIP_Broadcast_Port) {
return ntohs(BIP_Broadcast_Port);
}
return ntohs(BIP_Port);
}
/**
* @brief Get the IPv4 address for my interface. Used for sending src address.
* @param addr - BACnet datalink address
@@ -159,11 +183,12 @@ void bip_get_my_address(BACNET_ADDRESS *addr)
void bip_get_broadcast_address(BACNET_ADDRESS *dest)
{
int i = 0; /* counter */
uint16_t port = htons(bip_get_broadcast_port());
if (dest) {
dest->mac_len = 6;
memcpy(&dest->mac[0], &BIP_Broadcast_Addr.s_addr, 4);
memcpy(&dest->mac[4], &BIP_Port, 2);
memcpy(&dest->mac[4], &port, 2);
dest->net = BACNET_BROADCAST_NETWORK;
dest->len = 0; /* no SLEN */
for (i = 0; i < MAX_MAC_LEN; i++) {
@@ -223,7 +248,7 @@ bool bip_get_broadcast_addr(BACNET_IP_ADDRESS *addr)
{
if (addr) {
memcpy(&addr->address[0], &BIP_Broadcast_Addr.s_addr, 4);
addr->port = ntohs(BIP_Port);
addr->port = bip_get_broadcast_port();
}
return true;
@@ -1007,6 +1032,7 @@ void bip_cleanup(void)
BIP_Address.s_addr = 0;
BIP_Broadcast_Addr.s_addr = 0;
BIP_Netmask.s_addr = 0;
BIP_Broadcast_Port = 0;
return;
}