Modified to store and use IP addresses and the IP port number in the network byte order.

This commit is contained in:
bacpack
2010-10-29 17:29:01 +00:00
parent 79b86d3a8c
commit 73415f4a5d
+47 -51
View File
@@ -45,11 +45,11 @@
/** @file bip.c Configuration and Operations for BACnet/IP */ /** @file bip.c Configuration and Operations for BACnet/IP */
static int BIP_Socket = -1; static int BIP_Socket = -1;
/* port to use - stored in host byte order */ /* port to use - stored in network byte order */
static uint16_t BIP_Port = 0xBAC0; static uint16_t BIP_Port = 0; /* this will force initialization in demos */
/* IP Address - stored in host byte order */ /* IP Address - stored in network byte order */
static struct in_addr BIP_Address; static struct in_addr BIP_Address;
/* Broadcast Address - stored in host byte order */ /* Broadcast Address - stored in network byte order */
static struct in_addr BIP_Broadcast_Address; static struct in_addr BIP_Broadcast_Address;
/** Setter for the BACnet/IP socket handle. /** Setter for the BACnet/IP socket handle.
@@ -91,60 +91,56 @@ void bip_cleanup(
return; return;
} }
/* set using network byte order */
void bip_set_addr( void bip_set_addr(
uint32_t net_address) uint32_t net_address) /* in network byte order */
{ {
BIP_Address.s_addr = ntohl(net_address); BIP_Address.s_addr = net_address;
} }
/* returns host byte order */ /* returns host byte order */
uint32_t bip_get_addr( uint32_t bip_get_addr(
void) void)
{ {
return BIP_Address.s_addr; return ntohl(BIP_Address.s_addr);
} }
/* set using network byte order */
void bip_set_broadcast_addr( void bip_set_broadcast_addr(
uint32_t net_address) uint32_t net_address) /* in network byte order */
{ {
BIP_Broadcast_Address.s_addr = ntohl(net_address); BIP_Broadcast_Address.s_addr = net_address;
} }
/* returns host byte order */ /* returns host byte order */
uint32_t bip_get_broadcast_addr( uint32_t bip_get_broadcast_addr(
void) void)
{ {
return BIP_Broadcast_Address.s_addr; return ntohl(BIP_Broadcast_Address.s_addr);
} }
/* set using host byte order */
void bip_set_port( void bip_set_port(
uint16_t port) uint16_t port) /* in host byte order */
{ {
BIP_Port = port; BIP_Port = htons(port);
} }
/* returns host byte order */ /* returns host byte order */
uint16_t bip_get_port( uint16_t bip_get_port(
void) void)
{ {
return BIP_Port; return ntohs(BIP_Port);
} }
static int bip_decode_bip_address( static int bip_decode_bip_address(
uint8_t * pdu, /* buffer to extract encoded address */ BACNET_ADDRESS * bac_addr,
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 (bac_addr) {
(void) decode_unsigned32(&pdu[0], &raw_address); memcpy (&address->s_addr, &bac_addr->mac[0], 4);
address->s_addr = raw_address; memcpy (port, &bac_addr->mac[4], 2);
(void) decode_unsigned16(&pdu[4], port);
len = 6; len = 6;
} }
@@ -188,14 +184,14 @@ int bip_send_pdu(
port = BIP_Port; port = BIP_Port;
mtu[1] = BVLC_ORIGINAL_BROADCAST_NPDU; mtu[1] = BVLC_ORIGINAL_BROADCAST_NPDU;
} else if (dest->mac_len == 6) { } else if (dest->mac_len == 6) {
bip_decode_bip_address(&dest->mac[0], &address, &port); bip_decode_bip_address(dest, &address, &port);
mtu[1] = BVLC_ORIGINAL_UNICAST_NPDU; mtu[1] = BVLC_ORIGINAL_UNICAST_NPDU;
} else { } else {
/* invalid address */ /* invalid address */
return -1; return -1;
} }
bip_dest.sin_addr.s_addr = htonl(address.s_addr); bip_dest.sin_addr.s_addr = address.s_addr;
bip_dest.sin_port = htons(port); bip_dest.sin_port = port;
memset(&(bip_dest.sin_zero), '\0', 8); memset(&(bip_dest.sin_zero), '\0', 8);
mtu_len = 2; mtu_len = 2;
mtu_len += mtu_len +=
@@ -270,17 +266,17 @@ uint16_t bip_receive(
if ((pdu[1] == BVLC_ORIGINAL_UNICAST_NPDU) || if ((pdu[1] == BVLC_ORIGINAL_UNICAST_NPDU) ||
(pdu[1] == BVLC_ORIGINAL_BROADCAST_NPDU)) { (pdu[1] == BVLC_ORIGINAL_BROADCAST_NPDU)) {
/* ignore messages from me */ /* ignore messages from me */
if ((sin.sin_addr.s_addr == htonl(BIP_Address.s_addr)) && if ((sin.sin_addr.s_addr == BIP_Address.s_addr) &&
(sin.sin_port == htons(BIP_Port))) { (sin.sin_port == BIP_Port)) {
pdu_len = 0; pdu_len = 0;
#if PRINT_ENABLED #if PRINT_ENABLED
fprintf(stderr, "BIP: src is me. Discarded!\n"); fprintf(stderr, "BIP: src is me. Discarded!\n");
#endif #endif
} else { } else {
/* copy the source address - into host format */ /* data in src->mac[] is in network format */
src->mac_len = 6; src->mac_len = 6;
(void) encode_unsigned32(&src->mac[0], htonl(sin.sin_addr.s_addr)); memcpy (&src->mac[0], &sin.sin_addr.s_addr, 4);
(void) encode_unsigned16(&src->mac[4], htons(sin.sin_port)); memcpy (&src->mac[4], &sin.sin_port, 2);
/* FIXME: check destination address */ /* FIXME: check destination address */
/* see if it is broadcast or for us */ /* see if it is broadcast or for us */
/* decode the length of the PDU - length is inclusive of BVLC */ /* decode the length of the PDU - length is inclusive of BVLC */
@@ -312,18 +308,17 @@ uint16_t bip_receive(
} }
} }
} else if (pdu[1] == BVLC_FORWARDED_NPDU) { } else if (pdu[1] == BVLC_FORWARDED_NPDU) {
// next 2 lines - moved data is always in the network format memcpy (&sin.sin_addr.s_addr, &pdu[4], 4);
*(uint32_t *)&sin.sin_addr.s_addr = *(uint32_t *)&pdu[4]; memcpy (&sin.sin_port, &pdu[8], 2);
*(uint16_t *)&sin.sin_port = *(uint16_t *)&pdu[8]; if ((sin.sin_addr.s_addr == BIP_Address.s_addr) &&
if ((sin.sin_addr.s_addr == htonl(BIP_Address.s_addr)) && (sin.sin_port == BIP_Port)) {
(sin.sin_port == htons(BIP_Port))) {
/* ignore messages from me */ /* ignore messages from me */
pdu_len = 0; pdu_len = 0;
} else { } else {
/* copy the real source address - into host format */ /* data in src->mac[] is in network format */
src->mac_len = 6; src->mac_len = 6;
(void) encode_unsigned32(&src->mac[0], htonl(sin.sin_addr.s_addr)); memcpy (&src->mac[0], &sin.sin_addr.s_addr, 4);
(void) encode_unsigned16(&src->mac[4], htons(sin.sin_port)); memcpy (&src->mac[4], &sin.sin_port, 2);
/* FIXME: check destination address */ /* FIXME: check destination address */
/* see if it is broadcast or for us */ /* see if it is broadcast or for us */
/* decode the length of the PDU - length is inclusive of BVLC */ /* decode the length of the PDU - length is inclusive of BVLC */
@@ -355,14 +350,16 @@ void bip_get_my_address(
{ {
int i = 0; int i = 0;
my_address->mac_len = 6; if (my_address) {
(void) encode_unsigned32(&my_address->mac[0], htonl(BIP_Address.s_addr)); my_address->mac_len = 6;
(void) encode_unsigned16(&my_address->mac[4], htons(BIP_Port)); memcpy (&my_address->mac[0], &BIP_Address.s_addr, 4);
my_address->net = 0; /* local only, no routing */ memcpy (&my_address->mac[4], &BIP_Port, 2);
my_address->len = 0; /* no SLEN */ my_address->net = 0; /* local only, no routing */
for (i = 0; i < MAX_MAC_LEN; i++) { my_address->len = 0; /* no SLEN */
/* no SADR */ for (i = 0; i < MAX_MAC_LEN; i++) {
my_address->adr[i] = 0; /* no SADR */
my_address->adr[i] = 0;
}
} }
return; return;
@@ -375,9 +372,8 @@ void bip_get_broadcast_address(
if (dest) { if (dest) {
dest->mac_len = 6; dest->mac_len = 6;
(void) encode_unsigned32(&dest->mac[0], memcpy (&dest->mac[0], &BIP_Broadcast_Address.s_addr, 4);
htonl(BIP_Broadcast_Address.s_addr)); memcpy (&dest->mac[4], &BIP_Port, 2);
(void) encode_unsigned16(&dest->mac[4], htons(BIP_Port));
dest->net = BACNET_BROADCAST_NETWORK; dest->net = BACNET_BROADCAST_NETWORK;
dest->len = 0; /* no SLEN */ dest->len = 0; /* no SLEN */
for (i = 0; i < MAX_MAC_LEN; i++) { for (i = 0; i < MAX_MAC_LEN; i++) {