In bip.c getter and setter functions of IP address and Port number expect and return values in the network byte order. All ntoh and hton were eliminated from bip.c.

All other modules changed to be consistent.
arm7 port left unchanged.
This commit is contained in:
bacpack
2010-11-04 13:30:26 +00:00
parent 437112cc52
commit a639864cc8
9 changed files with 46 additions and 46 deletions
+2 -2
View File
@@ -919,7 +919,7 @@ int main(
* My_BIP_Port will be non-zero in this case. * My_BIP_Port will be non-zero in this case.
*/ */
if (My_BIP_Port > 0) if (My_BIP_Port > 0)
bip_set_port(My_BIP_Port); bip_set_port(htons(My_BIP_Port));
address_init(); address_init();
Init_Service_Handlers(); Init_Service_Handlers();
dlenv_init(); dlenv_init();
@@ -929,7 +929,7 @@ int main(
timeout_seconds = (apdu_timeout() / 1000) * apdu_retries(); timeout_seconds = (apdu_timeout() / 1000) * apdu_retries();
if (My_BIP_Port > 0) if (My_BIP_Port > 0)
bip_set_port(0xBAC0); /* Set back to std BACnet/IP port */ bip_set_port(htons(0xBAC0)); /* Set back to std BACnet/IP port */
/* try to bind with the target device */ /* try to bind with the target device */
found = found =
address_bind_request(Target_Device_Object_Instance, &max_apdu, address_bind_request(Target_Device_Object_Instance, &max_apdu,
+2 -2
View File
@@ -169,11 +169,11 @@ void Initialize_Device_Addresses( )
#if defined(BACDL_BIP) #if defined(BACDL_BIP)
struct in_addr *netPtr; /* Lets us cast to this type */ struct in_addr *netPtr; /* Lets us cast to this type */
uint8_t *gatewayMac = NULL; uint8_t *gatewayMac = NULL;
uint32_t myAddr = ntohl( bip_get_addr() ); uint32_t myAddr = bip_get_addr();
pDev = Get_Routed_Device_Object( i ); pDev = Get_Routed_Device_Object( i );
gatewayMac = pDev->bacDevAddr.mac; /* Keep pointer to the main MAC */ gatewayMac = pDev->bacDevAddr.mac; /* Keep pointer to the main MAC */
memcpy( pDev->bacDevAddr.mac, &myAddr, 4 ); memcpy( pDev->bacDevAddr.mac, &myAddr, 4 );
myPort = ntohs( bip_get_port() ); myPort = bip_get_port();
memcpy( &pDev->bacDevAddr.mac[4], &myPort, 2 ); memcpy( &pDev->bacDevAddr.mac[4], &myPort, 2 );
pDev->bacDevAddr.mac_len = 6; pDev->bacDevAddr.mac_len = 6;
#elif defined(BACDL_MSTP) #elif defined(BACDL_MSTP)
+3 -3
View File
@@ -167,7 +167,7 @@ void dlenv_init(
#endif #endif
pEnv = getenv("BACNET_IP_PORT"); pEnv = getenv("BACNET_IP_PORT");
if (pEnv) { if (pEnv) {
bip_set_port((uint16_t) strtol(pEnv, NULL, 0)); bip_set_port(htons((uint16_t) strtol(pEnv, NULL, 0)));
} else { } else {
/* BIP_Port is statically initialized to 0xBAC0, /* BIP_Port is statically initialized to 0xBAC0,
* so if it is different, then it was programmatically altered, * so if it is different, then it was programmatically altered,
@@ -175,8 +175,8 @@ void dlenv_init(
* Unless it is set below 1024, since: * Unless it is set below 1024, since:
* "The range for well-known ports managed by the IANA is 0-1023." * "The range for well-known ports managed by the IANA is 0-1023."
*/ */
if (bip_get_port() < 1024) if (ntohs(bip_get_port()) < 1024)
bip_set_port(0xBAC0); bip_set_port(htons(0xBAC0));
} }
#elif defined(BACDL_MSTP) #elif defined(BACDL_MSTP)
pEnv = getenv("BACNET_MAX_INFO_FRAMES"); pEnv = getenv("BACNET_MAX_INFO_FRAMES");
+4 -4
View File
@@ -89,24 +89,24 @@ extern "C" {
uint16_t max_pdu, /* amount of space available in the PDU */ uint16_t max_pdu, /* amount of space available in the PDU */
unsigned timeout); /* milliseconds to wait for a packet */ unsigned timeout); /* milliseconds to wait for a packet */
/* use host byte order for setting */ /* use network byte order for setting */
void bip_set_port( void bip_set_port(
uint16_t port); uint16_t port);
/* returns host byte order */ /* returns network byte order */
uint16_t bip_get_port( uint16_t bip_get_port(
void); void);
/* use network byte order for setting */ /* use network byte order for setting */
void bip_set_addr( void bip_set_addr(
uint32_t net_address); uint32_t net_address);
/* returns host byte order */ /* returns network byte order */
uint32_t bip_get_addr( uint32_t bip_get_addr(
void); void);
/* use network byte order for setting */ /* use network byte order for setting */
void bip_set_broadcast_addr( void bip_set_broadcast_addr(
uint32_t net_address); uint32_t net_address);
/* returns host byte order */ /* returns network byte order */
uint32_t bip_get_broadcast_addr( uint32_t bip_get_broadcast_addr(
void); void);
+3 -3
View File
@@ -128,8 +128,8 @@ static void bip_set_interface(
if (BIP_Debug) { if (BIP_Debug) {
fprintf(stderr, "IP Broadcast Address: %s\n", fprintf(stderr, "IP Broadcast Address: %s\n",
inet_ntoa(broadcast_address)); inet_ntoa(broadcast_address));
fprintf(stderr, "UDP Port: 0x%04X [%hu]\n", bip_get_port(), fprintf(stderr, "UDP Port: 0x%04X [%hu]\n", ntohs(bip_get_port()),
bip_get_port()); ntohs(bip_get_port()));
} }
} }
@@ -191,7 +191,7 @@ bool bip_init(
/* bind the socket to the local port number and IP address */ /* bind the socket to the local port number and IP address */
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(bip_get_port()); sin.sin_port = bip_get_port();
memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero)); memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero));
status = status =
bind(sock_fd, (const struct sockaddr *) &sin, sizeof(struct sockaddr)); bind(sock_fd, (const struct sockaddr *) &sin, sizeof(struct sockaddr));
+2 -2
View File
@@ -258,7 +258,7 @@ bool bip_init(
RTIP_To_Network_Address(TargetIP, &my_addr); RTIP_To_Network_Address(TargetIP, &my_addr);
bip_set_addr(my_addr.s_addr); bip_set_addr(my_addr.s_addr);
set_broadcast_address(my_addr.s_addr); set_broadcast_address(my_addr.s_addr);
bip_set_port(0xBAC0); bip_set_port(htons((0xBAC0));
/* assumes that the driver has already been initialized */ /* assumes that the driver has already been initialized */
sock_fd = socket(AF_INET, SOCK_DGRAM, IPROTO_UDP); sock_fd = socket(AF_INET, SOCK_DGRAM, IPROTO_UDP);
@@ -269,7 +269,7 @@ bool bip_init(
/* bind the socket to the local port number and IP address */ /* bind the socket to the local port number and IP address */
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(bip_get_port()); sin.sin_port = bip_get_port();
memset(&(sin.sin_zero), '\0', 8); memset(&(sin.sin_zero), '\0', 8);
rv = bind(sock_fd, (const struct sockaddr *) &sin, rv = bind(sock_fd, (const struct sockaddr *) &sin,
sizeof(struct sockaddr)); sizeof(struct sockaddr));
+8 -8
View File
@@ -191,12 +191,12 @@ void bip_set_interface(
bip_set_addr(inet_addr(ifname)); bip_set_addr(inet_addr(ifname));
} }
if (BIP_Debug) { if (BIP_Debug) {
address.s_addr = htonl(bip_get_addr()); address.s_addr = bip_get_addr();
fprintf(stderr, "Interface: %s\n", ifname); fprintf(stderr, "Interface: %s\n", ifname);
} }
/* setup local broadcast address */ /* setup local broadcast address */
if (bip_get_broadcast_addr() == 0) { if (bip_get_broadcast_addr() == 0) {
address.s_addr = htonl(bip_get_addr()); address.s_addr = bip_get_addr();
set_broadcast_address(address.s_addr); set_broadcast_address(address.s_addr);
} }
} }
@@ -339,7 +339,7 @@ bool bip_init(
if (ifname) if (ifname)
bip_set_interface(ifname); bip_set_interface(ifname);
/* has address been set? */ /* has address been set? */
address.s_addr = htonl(bip_get_addr()); address.s_addr = bip_get_addr();
if (address.s_addr == 0) { if (address.s_addr == 0) {
address.s_addr = gethostaddr(); address.s_addr = gethostaddr();
if (address.s_addr == (unsigned) -1) { if (address.s_addr == (unsigned) -1) {
@@ -358,11 +358,11 @@ bool bip_init(
set_broadcast_address(address.s_addr); set_broadcast_address(address.s_addr);
} }
if (BIP_Debug) { if (BIP_Debug) {
broadcast_address.s_addr = htonl(bip_get_broadcast_addr()); broadcast_address.s_addr = bip_get_broadcast_addr();
fprintf(stderr, "IP Broadcast Address: %s\n", fprintf(stderr, "IP Broadcast Address: %s\n",
inet_ntoa(broadcast_address)); inet_ntoa(broadcast_address));
fprintf(stderr, "UDP Port: 0x%04X [%hu]\n", bip_get_port(), fprintf(stderr, "UDP Port: 0x%04X [%hu]\n", ntohs(bip_get_port()),
bip_get_port()); ntohs(bip_get_port()));
} }
/* assumes that the driver has already been initialized */ /* assumes that the driver has already been initialized */
sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
@@ -424,13 +424,13 @@ bool bip_init(
note: already in network byte order */ note: already in network byte order */
sin.sin_addr.s_addr = address.s_addr; sin.sin_addr.s_addr = address.s_addr;
#endif #endif
sin.sin_port = htons(bip_get_port()); sin.sin_port = bip_get_port();
memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero)); memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero));
rv = bind(sock_fd, (const struct sockaddr *) &sin, rv = bind(sock_fd, (const struct sockaddr *) &sin,
sizeof(struct sockaddr)); sizeof(struct sockaddr));
if (rv < 0) { if (rv < 0) {
fprintf(stderr, "bip: failed to bind to %s port %hu\n", fprintf(stderr, "bip: failed to bind to %s port %hu\n",
inet_ntoa(sin.sin_addr), bip_get_port()); inet_ntoa(sin.sin_addr), ntohs(bip_get_port()));
close(sock_fd); close(sock_fd);
bip_set_socket(-1); bip_set_socket(-1);
return false; return false;
+8 -8
View File
@@ -97,11 +97,11 @@ void bip_set_addr(
BIP_Address.s_addr = net_address; BIP_Address.s_addr = net_address;
} }
/* returns host byte order */ /* returns network byte order */
uint32_t bip_get_addr( uint32_t bip_get_addr(
void) void)
{ {
return ntohl(BIP_Address.s_addr); return BIP_Address.s_addr;
} }
void bip_set_broadcast_addr( void bip_set_broadcast_addr(
@@ -110,25 +110,25 @@ void bip_set_broadcast_addr(
BIP_Broadcast_Address.s_addr = net_address; BIP_Broadcast_Address.s_addr = net_address;
} }
/* returns host byte order */ /* returns network byte order */
uint32_t bip_get_broadcast_addr( uint32_t bip_get_broadcast_addr(
void) void)
{ {
return ntohl(BIP_Broadcast_Address.s_addr); return BIP_Broadcast_Address.s_addr;
} }
void bip_set_port( void bip_set_port(
uint16_t port) /* in host byte order */ uint16_t port) /* in network byte order */
{ {
BIP_Port = htons(port); BIP_Port = port;
} }
/* returns host byte order */ /* returns network byte order */
uint16_t bip_get_port( uint16_t bip_get_port(
void) void)
{ {
return ntohs(BIP_Port); return BIP_Port;
} }
static int bip_decode_bip_address( static int bip_decode_bip_address(
+14 -14
View File
@@ -682,13 +682,13 @@ static void bvlc_bdt_forward_npdu(
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 = htons(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 == htonl(bip_get_broadcast_addr())) if ((bip_dest.sin_addr.s_addr == bip_get_broadcast_addr())
&& (bip_dest.sin_port == htons(bip_get_port()))) { && (bip_dest.sin_port == bip_get_port())) {
continue; continue;
} }
/* 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 == htonl(bip_get_addr())) && if ((bip_dest.sin_addr.s_addr == bip_get_addr()) &&
(bip_dest.sin_port == htons(bip_get_port()))) { (bip_dest.sin_port == bip_get_port())) {
continue; continue;
} }
bvlc_send_mpdu(&bip_dest, mtu, mtu_len); bvlc_send_mpdu(&bip_dest, mtu, mtu_len);
@@ -713,8 +713,8 @@ static void bvlc_forward_npdu(
mtu_len = mtu_len =
(uint16_t) bvlc_encode_forwarded_npdu(&mtu[0], sin, npdu, npdu_length); (uint16_t) bvlc_encode_forwarded_npdu(&mtu[0], sin, npdu, npdu_length);
bip_dest.sin_addr.s_addr = htonl(bip_get_broadcast_addr()); bip_dest.sin_addr.s_addr = bip_get_broadcast_addr();
bip_dest.sin_port = htons(bip_get_port()); bip_dest.sin_port = bip_get_port();
bvlc_send_mpdu(&bip_dest, mtu, mtu_len); bvlc_send_mpdu(&bip_dest, mtu, mtu_len);
debug_printf("BVLC: Sent Forwarded-NPDU as local broadcast.\n"); debug_printf("BVLC: Sent Forwarded-NPDU as local broadcast.\n");
} }
@@ -737,8 +737,8 @@ static void bvlc_fdt_forward_npdu(
bip_dest.sin_addr.s_addr = htonl(FD_Table[i].dest_address.s_addr); bip_dest.sin_addr.s_addr = htonl(FD_Table[i].dest_address.s_addr);
bip_dest.sin_port = htons(FD_Table[i].dest_port); bip_dest.sin_port = htons(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 == htonl(bip_get_addr())) && if ((bip_dest.sin_addr.s_addr == bip_get_addr()) &&
(bip_dest.sin_port == htons(bip_get_port()))) { (bip_dest.sin_port == bip_get_port())) {
continue; continue;
} }
/* don't send to src ip address and same port */ /* don't send to src ip address and same port */
@@ -992,8 +992,8 @@ uint16_t bvlc_receive(
npdu_len -= 6; npdu_len -= 6;
/* Broadcast locally if received via unicast from a BDT member */ /* Broadcast locally if received via unicast from a BDT member */
if (bvlc_bdt_member_mask_is_unicast(&sin)) { if (bvlc_bdt_member_mask_is_unicast(&sin)) {
dest.sin_addr.s_addr = htonl(bip_get_broadcast_addr()); dest.sin_addr.s_addr = bip_get_broadcast_addr();
dest.sin_port = htons(bip_get_port()); dest.sin_port = bip_get_port();
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 */
@@ -1102,8 +1102,8 @@ uint16_t bvlc_receive(
case BVLC_ORIGINAL_UNICAST_NPDU: case BVLC_ORIGINAL_UNICAST_NPDU:
debug_printf("BVLC: Received Original-Unicast-NPDU.\n"); debug_printf("BVLC: Received Original-Unicast-NPDU.\n");
/* ignore messages from me */ /* ignore messages from me */
if ((sin.sin_addr.s_addr == htonl(bip_get_addr())) && if ((sin.sin_addr.s_addr == bip_get_addr()) &&
(sin.sin_port == htons(bip_get_port()))) { (sin.sin_port == bip_get_port())) {
npdu_len = 0; npdu_len = 0;
} else { } else {
bvlc_internet_to_bacnet_address(src, &sin); bvlc_internet_to_bacnet_address(src, &sin);
@@ -1182,8 +1182,8 @@ int bvlc_send_pdu(
port = ntohs(Remote_BBMD.sin_port); port = ntohs(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 = bip_get_broadcast_addr(); address.s_addr = ntohl(bip_get_broadcast_addr());
port = bip_get_port(); port = ntohs(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");
} }