In bvlc.c, IP address and port number are always in the network byte order (including FD_Table[] and BBMD_Table[]).

The only left hton and ntoh functions are in debug_printf() and test functions.
This commit is contained in:
bacpack
2010-11-05 19:30:20 +00:00
parent a639864cc8
commit d50eb7fef7
3 changed files with 52 additions and 70 deletions
+1 -1
View File
@@ -80,7 +80,7 @@ void dlenv_register_as_foreign_device(
fprintf(stderr, fprintf(stderr,
"Registering with BBMD at %s:%ld for %ld seconds\n", "Registering with BBMD at %s:%ld for %ld seconds\n",
inet_ntoa(addr), bbmd_port, bbmd_timetolive_seconds); inet_ntoa(addr), bbmd_port, bbmd_timetolive_seconds);
bvlc_register_with_bbmd(bbmd_address, (uint16_t) bbmd_port, bvlc_register_with_bbmd(bbmd_address, htons((uint16_t) bbmd_port),
(uint16_t) bbmd_timetolive_seconds); (uint16_t) bbmd_timetolive_seconds);
BBMD_Timer_Seconds = bbmd_timetolive_seconds; BBMD_Timer_Seconds = bbmd_timetolive_seconds;
} }
+2 -2
View File
@@ -54,8 +54,8 @@ extern "C" {
#endif #endif
/* registers with a bbmd as a foreign device */ /* registers with a bbmd as a foreign device */
void bvlc_register_with_bbmd( void bvlc_register_with_bbmd(
long bbmd_address, /* in network byte order */ uint32_t bbmd_address, /* in network byte order */
uint16_t bbmd_port, uint16_t bbmd_port, /* in network byte order */
uint16_t time_to_live_seconds); uint16_t time_to_live_seconds);
uint16_t bvlc_receive( uint16_t bvlc_receive(
+49 -67
View File
@@ -55,11 +55,11 @@ typedef struct {
/* true if valid entry - false if not */ /* true if valid entry - false if not */
bool valid; bool valid;
/* BACnet/IP address */ /* BACnet/IP address */
struct in_addr dest_address; struct in_addr dest_address; /* in network format */
/* BACnet/IP port number - not always 47808=BAC0h */ /* BACnet/IP port number - not always 47808=BAC0h */
uint16_t dest_port; uint16_t dest_port; /* in network format */
/* Broadcast Distribution Mask - stored in host byte order */ /* Broadcast Distribution Mask */
struct in_addr broadcast_mask; struct in_addr broadcast_mask; /* in tework format */
} BBMD_TABLE_ENTRY; } BBMD_TABLE_ENTRY;
#define MAX_BBMD_ENTRIES 128 #define MAX_BBMD_ENTRIES 128
@@ -123,18 +123,18 @@ void bvlc_maintenance_timer(
In the case of B/IP networks, six octets consisting of the four-octet In the case of B/IP networks, six octets consisting of the four-octet
IP address followed by a two-octet UDP port number (both of IP address followed by a two-octet UDP port number (both of
which shall be transmitted most significant octet first). which shall be transmitted most significant octet first).
Note: for local storage, the storage order is host byte order. Note: for local storage, the storage order is NETWORK byte order.
Note: BACnet unsigned is encoded as most significant octet. */ Note: BACnet unsigned is encoded as most significant octet. */
static int bvlc_encode_bip_address( static int bvlc_encode_bip_address(
uint8_t * pdu, /* buffer to store encoding */ uint8_t * pdu, /* buffer to store encoding */
struct in_addr *address, /* in host format */ struct in_addr *address, /* in network format */
uint16_t port) uint16_t port) /* in network format */
{ {
int len = 0; int len = 0;
if (pdu) { if (pdu) {
encode_unsigned32(&pdu[0], address->s_addr); memcpy (&pdu[0], &address->s_addr, 4);
encode_unsigned16(&pdu[4], port); memcpy (&pdu[4], &port, 2);
len = 6; len = 6;
} }
@@ -143,16 +143,14 @@ static int bvlc_encode_bip_address(
static int bvlc_decode_bip_address( static int bvlc_decode_bip_address(
uint8_t * pdu, /* buffer to extract encoded address */ uint8_t * pdu, /* buffer to extract encoded address */
struct in_addr *address, /* in host format */ struct in_addr *address, /* in network format */
uint16_t * port) uint16_t * port) /* in network format */
{ {
int len = 0; int len = 0;
uint32_t raw_address = 0;
if (pdu) { if (pdu) {
(void) decode_unsigned32(&pdu[0], &raw_address); memcpy(&address->s_addr, &pdu[0], 4);
address->s_addr = raw_address; memcpy(port, &pdu[4], 2);
(void) decode_unsigned16(&pdu[4], port);
len = 6; len = 6;
} }
@@ -163,14 +161,15 @@ static int bvlc_decode_bip_address(
static int bvlc_encode_address_entry( static int bvlc_encode_address_entry(
uint8_t * pdu, uint8_t * pdu,
struct in_addr *address, struct in_addr *address,
uint16_t port, uint16_t port, /* in network byte order */
struct in_addr *mask) struct in_addr *mask)
{ {
int len = 0; int len = 0;
if (pdu) { if (pdu) {
len = bvlc_encode_bip_address(pdu, address, port); len = bvlc_encode_bip_address(pdu, address, port);
len += encode_unsigned32(&pdu[len], mask->s_addr); memcpy(&pdu[len], &mask->s_addr, 4);
len += 4;
} }
return len; return len;
@@ -294,8 +293,6 @@ static int bvlc_encode_forwarded_npdu(
unsigned npdu_length) unsigned npdu_length)
{ {
int len = 0; int len = 0;
struct in_addr address;
uint16_t port;
unsigned i; /* for loop counter */ unsigned i; /* for loop counter */
@@ -307,9 +304,7 @@ static int bvlc_encode_forwarded_npdu(
length field itself, most significant octet first. */ length field itself, most significant octet first. */
encode_unsigned16(&pdu[2], (uint16_t) (4 + 6 + npdu_length)); encode_unsigned16(&pdu[2], (uint16_t) (4 + 6 + npdu_length));
len = 4; len = 4;
address.s_addr = ntohl(sin->sin_addr.s_addr); len += bvlc_encode_bip_address(&pdu[len], &sin->sin_addr, sin->sin_port);
port = ntohs(sin->sin_port);
len += bvlc_encode_bip_address(&pdu[len], &address, port);
for (i = 0; i < npdu_length; i++) { for (i = 0; i < npdu_length; i++) {
pdu[len] = npdu[i]; pdu[len] = npdu[i];
len++; len++;
@@ -498,16 +493,11 @@ static void bvlc_internet_to_bacnet_address(
BACNET_ADDRESS * src, /* returns the BACnet source address */ BACNET_ADDRESS * src, /* returns the BACnet source address */
struct sockaddr_in *sin) struct sockaddr_in *sin)
{ /* source address in network order */ { /* source address in network order */
int len = 0;
uint32_t address;
uint16_t port;
if (src && sin) { if (src && sin) {
address = ntohl(sin->sin_addr.s_addr); memcpy(&src->mac[0], &sin->sin_addr.s_addr, 4);
len = encode_unsigned32(&src->mac[0], address); memcpy(&src->mac[4], &sin->sin_port, 2);
port = ntohs(sin->sin_port); src->mac_len = (uint8_t) 6;
len += encode_unsigned16(&src->mac[4], port);
src->mac_len = (uint8_t) len;
src->net = 0; src->net = 0;
src->len = 0; src->len = 0;
} }
@@ -517,20 +507,15 @@ static void bvlc_internet_to_bacnet_address(
/* copy the source internet address to the BACnet address */ /* copy the source internet address to the BACnet address */
/* FIXME: IPv6? */ /* FIXME: IPv6? */
void bvlc_bacnet_to_internet_address( static void bvlc_bacnet_to_internet_address(
struct sockaddr_in *sin, /* source address in network order */ struct sockaddr_in *sin, /* source address in network order */
BACNET_ADDRESS * src) BACNET_ADDRESS * src)
{ /* returns the BACnet source address */ { /* returns the BACnet source address */
int len = 0;
uint32_t address;
uint16_t port;
if (src && sin) { if (src && sin) {
if (src->mac_len == 6) { if (src->mac_len == 6) {
len = decode_unsigned32(&src->mac[0], &address); memcpy(&sin->sin_addr.s_addr, &src->mac[0], 4);
len += decode_unsigned16(&src->mac[4], &port); memcpy(&sin->sin_port, &src->mac[4], 2);
sin->sin_addr.s_addr = htonl(address);
sin->sin_port = htons(port);
} }
} }
@@ -548,13 +533,11 @@ static bool bvlc_create_bdt(
for (i = 0; i < MAX_BBMD_ENTRIES; i++) { for (i = 0; i < MAX_BBMD_ENTRIES; i++) {
if (npdu_length >= 10) { if (npdu_length >= 10) {
BBMD_Table[i].valid = true; BBMD_Table[i].valid = true;
BBMD_Table[i].dest_address.s_addr = BBMD_Table[i].dest_address.s_addr = *(long *) &npdu[pdu_offset]; /* FIXME: dangerous casting */
ntohl(*(long *) &npdu[pdu_offset]);
pdu_offset += 4; pdu_offset += 4;
BBMD_Table[i].dest_port = ntohs(*(short *) &npdu[pdu_offset]); BBMD_Table[i].dest_port = *(short *) &npdu[pdu_offset]; /* FIXME: dangerous casting */
pdu_offset += 2; pdu_offset += 2;
BBMD_Table[i].broadcast_mask.s_addr = BBMD_Table[i].broadcast_mask.s_addr = *(long *) &npdu[pdu_offset]; /* FIXME: dangerous casting */
ntohl(*(long *) &npdu[pdu_offset]);
pdu_offset += 4; pdu_offset += 4;
npdu_length -= 10; npdu_length -= 10;
} else { } else {
@@ -582,9 +565,8 @@ static bool bvlc_register_foreign_device(
/* am I here already? If so, update my time to live... */ /* am I here already? If so, update my time to live... */
for (i = 0; i < MAX_FD_ENTRIES; i++) { for (i = 0; i < MAX_FD_ENTRIES; i++) {
if (FD_Table[i].valid) { if (FD_Table[i].valid) {
if ((FD_Table[i].dest_address.s_addr == if ((FD_Table[i].dest_address.s_addr == sin->sin_addr.s_addr) &&
ntohl(sin->sin_addr.s_addr)) && (FD_Table[i].dest_port == sin->sin_port)) {
(FD_Table[i].dest_port == ntohs(sin->sin_port))) {
status = true; status = true;
FD_Table[i].time_to_live = time_to_live; FD_Table[i].time_to_live = time_to_live;
/* Upon receipt of a BVLL Register-Foreign-Device message, /* Upon receipt of a BVLL Register-Foreign-Device message,
@@ -599,8 +581,8 @@ static bool bvlc_register_foreign_device(
if (!status) { if (!status) {
for (i = 0; i < MAX_FD_ENTRIES; i++) { for (i = 0; i < MAX_FD_ENTRIES; i++) {
if (!FD_Table[i].valid) { if (!FD_Table[i].valid) {
FD_Table[i].dest_address.s_addr = ntohl(sin->sin_addr.s_addr); FD_Table[i].dest_address.s_addr = sin->sin_addr.s_addr;
FD_Table[i].dest_port = ntohs(sin->sin_port); FD_Table[i].dest_port = sin->sin_port;
FD_Table[i].time_to_live = time_to_live; FD_Table[i].time_to_live = time_to_live;
FD_Table[i].seconds_remaining = time_to_live + 30; FD_Table[i].seconds_remaining = time_to_live + 30;
FD_Table[i].valid = true; FD_Table[i].valid = true;
@@ -678,9 +660,9 @@ static void bvlc_bdt_forward_npdu(
mask in the BDT entry and logically ORing it with the mask in the BDT entry and logically ORing it with the
BBMD address of the same entry. */ BBMD address of the same entry. */
bip_dest.sin_addr.s_addr = bip_dest.sin_addr.s_addr =
htonl(((~BBMD_Table[i].broadcast_mask. ((~BBMD_Table[i].broadcast_mask.
s_addr) | BBMD_Table[i].dest_address.s_addr)); s_addr) | BBMD_Table[i].dest_address.s_addr);
bip_dest.sin_port = htons(BBMD_Table[i].dest_port); bip_dest.sin_port = BBMD_Table[i].dest_port;
/* don't send to my broadcast address and same port */ /* don't send to my broadcast address and same port */
if ((bip_dest.sin_addr.s_addr == bip_get_broadcast_addr()) if ((bip_dest.sin_addr.s_addr == bip_get_broadcast_addr())
&& (bip_dest.sin_port == bip_get_port())) { && (bip_dest.sin_port == bip_get_port())) {
@@ -734,8 +716,8 @@ static void bvlc_fdt_forward_npdu(
/* loop through the FDT and send one to each entry */ /* loop through the FDT and send one to each entry */
for (i = 0; i < MAX_FD_ENTRIES; i++) { for (i = 0; i < MAX_FD_ENTRIES; i++) {
if (FD_Table[i].valid && FD_Table[i].seconds_remaining) { if (FD_Table[i].valid && FD_Table[i].seconds_remaining) {
bip_dest.sin_addr.s_addr = htonl(FD_Table[i].dest_address.s_addr); bip_dest.sin_addr.s_addr = FD_Table[i].dest_address.s_addr;
bip_dest.sin_port = htons(FD_Table[i].dest_port); bip_dest.sin_port = FD_Table[i].dest_port;
/* don't send to my ip address and same port */ /* don't send to my ip address and same port */
if ((bip_dest.sin_addr.s_addr == bip_get_addr()) && if ((bip_dest.sin_addr.s_addr == bip_get_addr()) &&
(bip_dest.sin_port == bip_get_port())) { (bip_dest.sin_port == bip_get_port())) {
@@ -756,8 +738,8 @@ static void bvlc_fdt_forward_npdu(
} }
void bvlc_register_with_bbmd( void bvlc_register_with_bbmd(
long bbmd_address, /* in network byte order */ uint32_t bbmd_address, /* in network byte order */
uint16_t bbmd_port, /* in host byte order */ uint16_t bbmd_port, /* in network byte order */
uint16_t time_to_live_seconds) uint16_t time_to_live_seconds)
{ {
uint8_t mtu[MAX_MPDU] = { 0 }; uint8_t mtu[MAX_MPDU] = { 0 };
@@ -766,7 +748,7 @@ void bvlc_register_with_bbmd(
/* Store the BBMD address and port so that we /* Store the BBMD address and port so that we
won't broadcast locally. */ won't broadcast locally. */
Remote_BBMD.sin_addr.s_addr = bbmd_address; Remote_BBMD.sin_addr.s_addr = bbmd_address;
Remote_BBMD.sin_port = htons(bbmd_port); Remote_BBMD.sin_port = bbmd_port;
/* In order for their broadcasts to get here, /* In order for their broadcasts to get here,
we need to register our address with the remote BBMD using we need to register our address with the remote BBMD using
Write Broadcast Distribution Table, or Write Broadcast Distribution Table, or
@@ -828,8 +810,8 @@ static bool bvlc_bdt_member_mask_is_unicast(
if (BBMD_Table[i].valid) { if (BBMD_Table[i].valid) {
/* find the source address in the table */ /* find the source address in the table */
if ((BBMD_Table[i].dest_address.s_addr == if ((BBMD_Table[i].dest_address.s_addr ==
htonl(sin->sin_addr.s_addr)) && sin->sin_addr.s_addr) &&
(BBMD_Table[i].dest_port == htons(sin->sin_port))) { (BBMD_Table[i].dest_port == sin->sin_port)) {
/* unicast mask? */ /* unicast mask? */
if (BBMD_Table[i].broadcast_mask.s_addr == 0xFFFFFFFFL) { if (BBMD_Table[i].broadcast_mask.s_addr == 0xFFFFFFFFL) {
unicast = true; unicast = true;
@@ -997,8 +979,8 @@ uint16_t bvlc_receive(
bvlc_send_mpdu(&dest, &npdu[4 + 6], npdu_len); bvlc_send_mpdu(&dest, &npdu[4 + 6], npdu_len);
} }
/* use the original addr from the BVLC for src */ /* use the original addr from the BVLC for src */
dest.sin_addr.s_addr = htonl(original_sin.sin_addr.s_addr); dest.sin_addr.s_addr = original_sin.sin_addr.s_addr;
dest.sin_port = htons(original_sin.sin_port); dest.sin_port = original_sin.sin_port;
bvlc_fdt_forward_npdu(&dest, &npdu[4 + 6], npdu_len); bvlc_fdt_forward_npdu(&dest, &npdu[4 + 6], npdu_len);
debug_printf("BVLC: Received Forwarded-NPDU from %s:%04X.\n", debug_printf("BVLC: Received Forwarded-NPDU from %s:%04X.\n",
inet_ntoa(dest.sin_addr), ntohs(dest.sin_port)); inet_ntoa(dest.sin_addr), ntohs(dest.sin_port));
@@ -1166,7 +1148,7 @@ int bvlc_send_pdu(
struct sockaddr_in bvlc_dest = { 0 }; struct sockaddr_in bvlc_dest = { 0 };
uint8_t mtu[MAX_MPDU] = { 0 }; uint8_t mtu[MAX_MPDU] = { 0 };
uint16_t mtu_len = 0; uint16_t mtu_len = 0;
/* addr and port in host format */ /* addr and port in network format */
struct in_addr address; struct in_addr address;
uint16_t port = 0; uint16_t port = 0;
uint16_t BVLC_length = 0; uint16_t BVLC_length = 0;
@@ -1178,12 +1160,12 @@ int bvlc_send_pdu(
/* if we are a foreign device */ /* if we are a foreign device */
if (Remote_BBMD.sin_port) { if (Remote_BBMD.sin_port) {
mtu[1] = BVLC_DISTRIBUTE_BROADCAST_TO_NETWORK; mtu[1] = BVLC_DISTRIBUTE_BROADCAST_TO_NETWORK;
address.s_addr = ntohl(Remote_BBMD.sin_addr.s_addr); address.s_addr = Remote_BBMD.sin_addr.s_addr;
port = ntohs(Remote_BBMD.sin_port); port = Remote_BBMD.sin_port;
debug_printf("BVLC: Sent Distribute-Broadcast-to-Network.\n"); debug_printf("BVLC: Sent Distribute-Broadcast-to-Network.\n");
} else { } else {
address.s_addr = ntohl(bip_get_broadcast_addr()); address.s_addr = bip_get_broadcast_addr();
port = ntohs(bip_get_port()); port = bip_get_port();
mtu[1] = BVLC_ORIGINAL_BROADCAST_NPDU; mtu[1] = BVLC_ORIGINAL_BROADCAST_NPDU;
debug_printf("BVLC: Sent Original-Broadcast-NPDU.\n"); debug_printf("BVLC: Sent Original-Broadcast-NPDU.\n");
} }
@@ -1196,8 +1178,8 @@ int bvlc_send_pdu(
/* invalid address */ /* invalid address */
return -1; return -1;
} }
bvlc_dest.sin_addr.s_addr = htonl(address.s_addr); bvlc_dest.sin_addr.s_addr = address.s_addr;
bvlc_dest.sin_port = htons(port); bvlc_dest.sin_port = port;
BVLC_length = (uint16_t) pdu_len + 4 /*inclusive */ ; BVLC_length = (uint16_t) pdu_len + 4 /*inclusive */ ;
mtu_len = 2; mtu_len = 2;
mtu_len += (uint16_t) encode_unsigned16(&mtu[mtu_len], BVLC_length); mtu_len += (uint16_t) encode_unsigned16(&mtu[mtu_len], BVLC_length);