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
+1
View File
@@ -141,6 +141,7 @@ void bip_cleanup(void)
close_func(sock_fd);
}
bip_set_socket(MAX_SOCK_NUM);
bip_set_broadcast_port(0);
return;
}
+19 -2
View File
@@ -24,6 +24,8 @@
static uint8_t BIP_Socket = MAX_SOCK_NUM;
/* port to use - stored in network byte order */
static uint16_t BIP_Port = 0; /* this will force initialization in demos */
/* broadcast destination port to use */
static uint16_t BIP_Broadcast_Port;
/* IP Address - stored in network byte order */
// static struct in_addr BIP_Address;
static uint8_t BIP_Address[4] = { 0, 0, 0, 0 };
@@ -105,12 +107,26 @@ void bip_set_port(uint16_t port)
BIP_Port = htons(port);
}
void bip_set_broadcast_port(uint16_t port)
{ /* in network byte order */
BIP_Broadcast_Port = htons(port);
}
/* returns network byte order */
uint16_t bip_get_port(void)
{
return ntohs(BIP_Port);
}
uint16_t bip_get_broadcast_port(void)
{
if (BIP_Broadcast_Port) {
return ntohs(BIP_Broadcast_Port);
}
return ntohs(BIP_Port);
}
static int bip_decode_bip_address(
const BACNET_ADDRESS *bac_addr,
uint8_t *address, /* in network format */
@@ -166,7 +182,7 @@ int bip_send_pdu(
for (uint8_t i = 0; i < 4; i++) {
address[i] = BIP_Broadcast_Address[i];
}
port = BIP_Port;
port = htons(bip_get_broadcast_port());
mtu[1] = BVLC_ORIGINAL_BROADCAST_NPDU;
#ifdef DEBUG
fprintf(
@@ -386,11 +402,12 @@ void bip_get_my_address(BACNET_ADDRESS *my_address)
void bip_get_broadcast_address(BACNET_ADDRESS *dest)
{ /* destination address */
int i = 0; /* counter */
uint16_t port = htons(bip_get_broadcast_port());
if (dest) {
dest->mac_len = 6;
memcpy(&dest->mac[0], &BIP_Broadcast_Address, 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++) {
+2
View File
@@ -65,8 +65,10 @@ uint16_t bip_receive(
/* use host byte order for setting */
void bip_set_port(uint16_t port);
void bip_set_broadcast_port(uint16_t port);
/* returns host byte order */
uint16_t bip_get_port(void);
uint16_t bip_get_broadcast_port(void);
/* use network byte order for setting */
void bip_set_addr(const uint8_t *net_address);
+28 -2
View File
@@ -25,6 +25,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 */
@@ -103,6 +105,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
@@ -112,6 +123,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
@@ -143,11 +167,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++) {
@@ -207,7 +232,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;
@@ -814,6 +839,7 @@ void bip_cleanup(void)
/* these were set non-zero during interface configuration */
BIP_Address.s_addr = 0;
BIP_Broadcast_Addr.s_addr = 0;
BIP_Broadcast_Port = 0;
return;
}
+30 -2
View File
@@ -23,6 +23,8 @@
static uint8_t BIP_Socket = MAX_SOCK_NUM;
static uint16_t BIP_Port = 0;
/* broadcast destination port to use */
static uint16_t BIP_Broadcast_Port = 0;
static uint8_t BIP_Address[4] = { 0, 0, 0, 0 };
static uint8_t BIP_Broadcast_Address[4] = { 0, 0, 0, 0 };
@@ -130,6 +132,15 @@ void bip_set_port(uint16_t port)
BIP_Port = port;
}
/**
* @brief Store the UDP broadcast destination port used by BACnet/IP
* @param port UDP port number
*/
void bip_set_broadcast_port(uint16_t port)
{
BIP_Broadcast_Port = port;
}
/**
* @brief Get the UDP port used by BACnet/IP
* @return UDP port number
@@ -139,6 +150,19 @@ uint16_t bip_get_port(void)
return BIP_Port;
}
/**
* @brief Get the UDP broadcast destination port used by BACnet/IP
* @return UDP port number
*/
uint16_t bip_get_broadcast_port(void)
{
if (BIP_Broadcast_Port) {
return BIP_Broadcast_Port;
}
return BIP_Port;
}
/**
* @brief Decode a BACnet/IP MAC address into IPv4 address and port parts
* @param bac_addr source BACnet address
@@ -193,7 +217,7 @@ int bip_send_pdu(
for (i = 0; i < 4; i++) {
address[i] = BIP_Broadcast_Address[i];
}
port = BIP_Port;
port = bip_get_broadcast_port();
mtu[1] = BVLC_ORIGINAL_BROADCAST_NPDU;
} else if (dest->mac_len == 6) {
bip_decode_bip_address(dest, address, &port);
@@ -330,7 +354,11 @@ void bip_get_broadcast_address(BACNET_ADDRESS *dest)
if (dest) {
dest->mac_len = 6;
memcpy(&dest->mac[0], &BIP_Broadcast_Address[0], 4);
memcpy(&dest->mac[4], &BIP_Port, 2);
{
uint16_t port = bip_get_broadcast_port();
memcpy(&dest->mac[4], &port, 2);
}
dest->net = BACNET_BROADCAST_NETWORK;
dest->len = 0;
}
+12
View File
@@ -172,12 +172,24 @@ uint16_t bip_receive(
*/
void bip_set_port(uint16_t port);
/**
* @brief Store the UDP broadcast destination port used by BACnet/IP
* @param port UDP port number
*/
void bip_set_broadcast_port(uint16_t port);
/**
* @brief Get the UDP port used by BACnet/IP
* @return UDP port number
*/
uint16_t bip_get_port(void);
/**
* @brief Get the UDP broadcast destination port used by BACnet/IP
* @return UDP port number
*/
uint16_t bip_get_broadcast_port(void);
/**
* @brief Store the local IPv4 address
* @param net_address pointer to the 4-byte address
+2
View File
@@ -93,4 +93,6 @@ void bip_cleanup(void)
if (bip_valid()) {
bip_socket_cleanup();
}
bip_set_broadcast_port(0);
}
+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;
}
+43 -3
View File
@@ -24,6 +24,8 @@ static bool BIP_Port_Changed;
static BACNET_IP_ADDRESS BIP_Address;
/* Broadcast Address */
static BACNET_IP_ADDRESS BIP_Broadcast_Address;
/* broadcast destination port to use */
static uint16_t BIP_Broadcast_Port;
/* lwIP socket, of sorts */
static struct udp_pcb *Server_upcb;
/* track packets for diagnostics */
@@ -89,7 +91,14 @@ bool bip_get_addr(BACNET_IP_ADDRESS *addr)
*/
bool bip_set_broadcast_addr(const BACNET_IP_ADDRESS *addr)
{
return bvlc_address_copy(&BIP_Broadcast_Address, addr);
bool status = false;
status = bvlc_address_copy(&BIP_Broadcast_Address, addr);
if (status) {
BIP_Broadcast_Port = htons(addr->port);
}
return status;
}
/**
@@ -99,7 +108,14 @@ bool bip_set_broadcast_addr(const BACNET_IP_ADDRESS *addr)
*/
bool bip_get_broadcast_addr(BACNET_IP_ADDRESS *addr)
{
return bvlc_address_copy(addr, &BIP_Broadcast_Address);
bool status = false;
status = bvlc_address_copy(addr, &BIP_Broadcast_Address);
if (status && addr) {
addr->port = ntohs(BIP_Broadcast_Port);
}
return status;
}
/**
@@ -114,6 +130,16 @@ void bip_set_port(uint16_t 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);
BIP_Broadcast_Address.port = BIP_Broadcast_Port;
}
/**
* @brief Determine if the BACnet IPv4 UDP port number changed
* @return true of the BACnet IPv4 UDP port number changed
@@ -132,6 +158,19 @@ uint16_t bip_get_port(void)
return ntohs(BIP_Address.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_Address.port);
}
/**
* @brief Convert the BACnet IPv4 address
* @param address - IPv4 address from LwIP
@@ -321,11 +360,12 @@ void bip_get_my_address(BACNET_ADDRESS *my_address)
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_Address.address, 4);
memcpy(&dest->mac[4], &BIP_Address.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++) {
+2
View File
@@ -112,5 +112,7 @@ void bip_cleanup(void)
bip_socket_cleanup();
}
bip_set_broadcast_port(0);
return;
}
+22 -2
View File
@@ -26,6 +26,8 @@
static uint8_t BIP_Socket = MAX_SOCK_NUM;
/* port to use - stored in network byte order */
static uint16_t BIP_Port = 0;
/* broadcast destination port to use */
static uint16_t BIP_Broadcast_Port = 0;
/* IP Address - stored in network byte order */
static uint8_t BIP_Address[4] = { 0, 0, 0, 0 };
/* Broadcast Address - stored in network byte order */
@@ -106,12 +108,26 @@ void bip_set_port(uint16_t port)
BIP_Port = port;
}
void bip_set_broadcast_port(uint16_t port)
{
BIP_Broadcast_Port = port;
}
/* returns network byte order */
uint16_t bip_get_port(void)
{
return BIP_Port;
}
uint16_t bip_get_broadcast_port(void)
{
if (BIP_Broadcast_Port) {
return BIP_Broadcast_Port;
}
return BIP_Port;
}
static int bip_decode_bip_address(
const BACNET_ADDRESS *bac_addr, uint8_t *address, uint16_t *port)
{
@@ -161,7 +177,7 @@ int bip_send_pdu(
for (i = 0; i < 4; i++) {
address[i] = BIP_Broadcast_Address[i];
}
port = BIP_Port;
port = bip_get_broadcast_port();
mtu[1] = BVLC_ORIGINAL_BROADCAST_NPDU;
#ifdef DEBUG
fprintf(
@@ -349,7 +365,11 @@ void bip_get_broadcast_address(BACNET_ADDRESS *dest)
if (dest) {
dest->mac_len = 6;
memcpy(&dest->mac[0], &BIP_Broadcast_Address, 4);
memcpy(&dest->mac[4], &BIP_Port, 2);
{
uint16_t port = bip_get_broadcast_port();
memcpy(&dest->mac[4], &port, 2);
}
dest->net = BACNET_BROADCAST_NETWORK;
dest->len = 0; /* no SLEN */
for (i = 0; i < MAX_MAC_LEN; i++) {
+2
View File
@@ -108,7 +108,9 @@ uint16_t bip_receive(
/* use host byte order for setting */
void bip_set_port(uint16_t port);
void bip_set_broadcast_port(uint16_t port);
uint16_t bip_get_port(void);
uint16_t bip_get_broadcast_port(void);
/* use network byte order for setting */
void bip_set_addr(const uint8_t *net_address);
+22 -2
View File
@@ -15,6 +15,8 @@
static int BIP_Socket = -1;
/* port to use - stored in host byte order */
static uint16_t BIP_Port = 0xBAC0U;
/* broadcast destination port to use */
static uint16_t BIP_Broadcast_Port;
/* IP Address - stored in host byte order */
static struct in_addr BIP_Address;
/* Broadcast Address - stored in host byte order */
@@ -40,6 +42,7 @@ void bip_cleanup(void)
/* if (bip_valid()) */
/* close(BIP_Socket); */
BIP_Socket = -1;
BIP_Broadcast_Port = 0;
return;
}
@@ -76,12 +79,28 @@ void bip_set_port(uint16_t port)
BIP_Port = port;
}
/* set using host byte order */
void bip_set_broadcast_port(uint16_t port)
{
BIP_Broadcast_Port = port;
}
/* returns host byte order */
uint16_t bip_get_port(void)
{
return BIP_Port;
}
/* returns host byte order */
uint16_t bip_get_broadcast_port(void)
{
if (BIP_Broadcast_Port) {
return BIP_Broadcast_Port;
}
return BIP_Port;
}
/* function to send a packet out the BACnet/IP socket (Annex J) */
/* returns number of bytes sent on success, negative number on failure */
int bip_send_pdu(BACNET_ADDRESS *dest, /* destination address */
@@ -116,7 +135,7 @@ int bip_send_pdu(BACNET_ADDRESS *dest, /* destination address */
/* broadcast */
else if (dest->mac_len == 0) {
bip_dest.sin_addr.s_addr = BIP_Broadcast_Address.s_addr;
bip_dest.sin_port = htons(BIP_Port);
bip_dest.sin_port = htons(bip_get_broadcast_port());
memset(&(bip_dest.sin_zero), '\0', 8);
mtu[1] = BVLC_ORIGINAL_BROADCAST_NPDU;
} else
@@ -197,7 +216,8 @@ void bip_get_broadcast_address(BACNET_ADDRESS *dest)
dest->mac_len = 6;
(void)encode_unsigned32(
&dest->mac[0], htonl(BIP_Broadcast_Address.s_addr));
(void)encode_unsigned16(&dest->mac[4], htons(BIP_Port));
(void)encode_unsigned16(
&dest->mac[4], htons(bip_get_broadcast_port()));
dest->net = BACNET_BROADCAST_NETWORK;
dest->len = 0; /* no SLEN */
for (i = 0; i < MAX_MAC_LEN; i++) {
+29 -2
View File
@@ -29,6 +29,9 @@ static bool BIP_Initialized;
/* 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 */
/* Initialize to 0 - this will force initialization in demo apps */
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 */
@@ -259,6 +262,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
@@ -268,6 +280,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
@@ -299,11 +324,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++) {
@@ -363,7 +389,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;
@@ -940,6 +966,7 @@ void bip_cleanup(void)
/* these were set non-zero during interface configuration */
BIP_Address.s_addr = 0;
BIP_Broadcast_Addr.s_addr = 0;
BIP_Broadcast_Port = 0;
return;
}