From a639864cc8429cb45c2510415509022036ee12bc Mon Sep 17 00:00:00 2001 From: bacpack Date: Thu, 4 Nov 2010 13:30:26 +0000 Subject: [PATCH] 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. --- bacnet-stack/demo/epics/main.c | 4 ++-- bacnet-stack/demo/gateway/main.c | 4 ++-- bacnet-stack/demo/handler/dlenv.c | 6 +++--- bacnet-stack/include/bip.h | 8 ++++---- bacnet-stack/ports/linux/bip-init.c | 6 +++--- bacnet-stack/ports/rtos32/bip-init.c | 4 ++-- bacnet-stack/ports/win32/bip-init.c | 16 ++++++++-------- bacnet-stack/src/bip.c | 16 ++++++++-------- bacnet-stack/src/bvlc.c | 28 ++++++++++++++-------------- 9 files changed, 46 insertions(+), 46 deletions(-) diff --git a/bacnet-stack/demo/epics/main.c b/bacnet-stack/demo/epics/main.c index 927e7eac..ee4d18f9 100644 --- a/bacnet-stack/demo/epics/main.c +++ b/bacnet-stack/demo/epics/main.c @@ -919,7 +919,7 @@ int main( * My_BIP_Port will be non-zero in this case. */ if (My_BIP_Port > 0) - bip_set_port(My_BIP_Port); + bip_set_port(htons(My_BIP_Port)); address_init(); Init_Service_Handlers(); dlenv_init(); @@ -929,7 +929,7 @@ int main( timeout_seconds = (apdu_timeout() / 1000) * apdu_retries(); 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 */ found = address_bind_request(Target_Device_Object_Instance, &max_apdu, diff --git a/bacnet-stack/demo/gateway/main.c b/bacnet-stack/demo/gateway/main.c index 39bc0436..0aa46520 100644 --- a/bacnet-stack/demo/gateway/main.c +++ b/bacnet-stack/demo/gateway/main.c @@ -169,11 +169,11 @@ void Initialize_Device_Addresses( ) #if defined(BACDL_BIP) struct in_addr *netPtr; /* Lets us cast to this type */ uint8_t *gatewayMac = NULL; - uint32_t myAddr = ntohl( bip_get_addr() ); + uint32_t myAddr = bip_get_addr(); pDev = Get_Routed_Device_Object( i ); gatewayMac = pDev->bacDevAddr.mac; /* Keep pointer to the main MAC */ memcpy( pDev->bacDevAddr.mac, &myAddr, 4 ); - myPort = ntohs( bip_get_port() ); + myPort = bip_get_port(); memcpy( &pDev->bacDevAddr.mac[4], &myPort, 2 ); pDev->bacDevAddr.mac_len = 6; #elif defined(BACDL_MSTP) diff --git a/bacnet-stack/demo/handler/dlenv.c b/bacnet-stack/demo/handler/dlenv.c index af3db6e8..b76d5af1 100644 --- a/bacnet-stack/demo/handler/dlenv.c +++ b/bacnet-stack/demo/handler/dlenv.c @@ -167,7 +167,7 @@ void dlenv_init( #endif pEnv = getenv("BACNET_IP_PORT"); if (pEnv) { - bip_set_port((uint16_t) strtol(pEnv, NULL, 0)); + bip_set_port(htons((uint16_t) strtol(pEnv, NULL, 0))); } else { /* BIP_Port is statically initialized to 0xBAC0, * so if it is different, then it was programmatically altered, @@ -175,8 +175,8 @@ void dlenv_init( * Unless it is set below 1024, since: * "The range for well-known ports managed by the IANA is 0-1023." */ - if (bip_get_port() < 1024) - bip_set_port(0xBAC0); + if (ntohs(bip_get_port()) < 1024) + bip_set_port(htons(0xBAC0)); } #elif defined(BACDL_MSTP) pEnv = getenv("BACNET_MAX_INFO_FRAMES"); diff --git a/bacnet-stack/include/bip.h b/bacnet-stack/include/bip.h index 38d4bb05..bb9e3c66 100644 --- a/bacnet-stack/include/bip.h +++ b/bacnet-stack/include/bip.h @@ -89,24 +89,24 @@ extern "C" { uint16_t max_pdu, /* amount of space available in the PDU */ unsigned timeout); /* milliseconds to wait for a packet */ - /* use host byte order for setting */ + /* use network byte order for setting */ void bip_set_port( uint16_t port); - /* returns host byte order */ + /* returns network byte order */ uint16_t bip_get_port( void); /* use network byte order for setting */ void bip_set_addr( uint32_t net_address); - /* returns host byte order */ + /* returns network byte order */ uint32_t bip_get_addr( void); /* use network byte order for setting */ void bip_set_broadcast_addr( uint32_t net_address); - /* returns host byte order */ + /* returns network byte order */ uint32_t bip_get_broadcast_addr( void); diff --git a/bacnet-stack/ports/linux/bip-init.c b/bacnet-stack/ports/linux/bip-init.c index d52031ee..6ef8765f 100644 --- a/bacnet-stack/ports/linux/bip-init.c +++ b/bacnet-stack/ports/linux/bip-init.c @@ -128,8 +128,8 @@ static void bip_set_interface( if (BIP_Debug) { fprintf(stderr, "IP Broadcast Address: %s\n", inet_ntoa(broadcast_address)); - fprintf(stderr, "UDP Port: 0x%04X [%hu]\n", bip_get_port(), - bip_get_port()); + fprintf(stderr, "UDP Port: 0x%04X [%hu]\n", ntohs(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 */ sin.sin_family = AF_INET; 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)); status = bind(sock_fd, (const struct sockaddr *) &sin, sizeof(struct sockaddr)); diff --git a/bacnet-stack/ports/rtos32/bip-init.c b/bacnet-stack/ports/rtos32/bip-init.c index 169625c6..5fb84ea4 100644 --- a/bacnet-stack/ports/rtos32/bip-init.c +++ b/bacnet-stack/ports/rtos32/bip-init.c @@ -258,7 +258,7 @@ bool bip_init( RTIP_To_Network_Address(TargetIP, &my_addr); bip_set_addr(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 */ 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 */ sin.sin_family = AF_INET; 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); rv = bind(sock_fd, (const struct sockaddr *) &sin, sizeof(struct sockaddr)); diff --git a/bacnet-stack/ports/win32/bip-init.c b/bacnet-stack/ports/win32/bip-init.c index a125ec4b..d99e8410 100644 --- a/bacnet-stack/ports/win32/bip-init.c +++ b/bacnet-stack/ports/win32/bip-init.c @@ -191,12 +191,12 @@ void bip_set_interface( bip_set_addr(inet_addr(ifname)); } if (BIP_Debug) { - address.s_addr = htonl(bip_get_addr()); + address.s_addr = bip_get_addr(); fprintf(stderr, "Interface: %s\n", ifname); } /* setup local broadcast address */ 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); } } @@ -339,7 +339,7 @@ bool bip_init( if (ifname) bip_set_interface(ifname); /* has address been set? */ - address.s_addr = htonl(bip_get_addr()); + address.s_addr = bip_get_addr(); if (address.s_addr == 0) { address.s_addr = gethostaddr(); if (address.s_addr == (unsigned) -1) { @@ -358,11 +358,11 @@ bool bip_init( set_broadcast_address(address.s_addr); } 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", inet_ntoa(broadcast_address)); - fprintf(stderr, "UDP Port: 0x%04X [%hu]\n", bip_get_port(), - bip_get_port()); + fprintf(stderr, "UDP Port: 0x%04X [%hu]\n", ntohs(bip_get_port()), + ntohs(bip_get_port())); } /* assumes that the driver has already been initialized */ sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); @@ -424,13 +424,13 @@ bool bip_init( note: already in network byte order */ sin.sin_addr.s_addr = address.s_addr; #endif - sin.sin_port = htons(bip_get_port()); + sin.sin_port = bip_get_port(); memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero)); rv = bind(sock_fd, (const struct sockaddr *) &sin, sizeof(struct sockaddr)); if (rv < 0) { 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); bip_set_socket(-1); return false; diff --git a/bacnet-stack/src/bip.c b/bacnet-stack/src/bip.c index 9093649f..0262043d 100644 --- a/bacnet-stack/src/bip.c +++ b/bacnet-stack/src/bip.c @@ -97,11 +97,11 @@ void bip_set_addr( BIP_Address.s_addr = net_address; } -/* returns host byte order */ +/* returns network byte order */ uint32_t bip_get_addr( void) { - return ntohl(BIP_Address.s_addr); + return BIP_Address.s_addr; } void bip_set_broadcast_addr( @@ -110,25 +110,25 @@ void bip_set_broadcast_addr( BIP_Broadcast_Address.s_addr = net_address; } -/* returns host byte order */ +/* returns network byte order */ uint32_t bip_get_broadcast_addr( void) { - return ntohl(BIP_Broadcast_Address.s_addr); + return BIP_Broadcast_Address.s_addr; } 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( void) { - return ntohs(BIP_Port); + return BIP_Port; } static int bip_decode_bip_address( diff --git a/bacnet-stack/src/bvlc.c b/bacnet-stack/src/bvlc.c index b47dbcf9..b3d8d90f 100644 --- a/bacnet-stack/src/bvlc.c +++ b/bacnet-stack/src/bvlc.c @@ -682,13 +682,13 @@ static void bvlc_bdt_forward_npdu( s_addr) | BBMD_Table[i].dest_address.s_addr)); bip_dest.sin_port = htons(BBMD_Table[i].dest_port); /* don't send to my broadcast address and same port */ - if ((bip_dest.sin_addr.s_addr == htonl(bip_get_broadcast_addr())) - && (bip_dest.sin_port == htons(bip_get_port()))) { + if ((bip_dest.sin_addr.s_addr == bip_get_broadcast_addr()) + && (bip_dest.sin_port == bip_get_port())) { continue; } /* don't send to my ip address and same port */ - if ((bip_dest.sin_addr.s_addr == htonl(bip_get_addr())) && - (bip_dest.sin_port == htons(bip_get_port()))) { + if ((bip_dest.sin_addr.s_addr == bip_get_addr()) && + (bip_dest.sin_port == bip_get_port())) { continue; } bvlc_send_mpdu(&bip_dest, mtu, mtu_len); @@ -713,8 +713,8 @@ static void bvlc_forward_npdu( mtu_len = (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_port = htons(bip_get_port()); + bip_dest.sin_addr.s_addr = bip_get_broadcast_addr(); + bip_dest.sin_port = bip_get_port(); bvlc_send_mpdu(&bip_dest, mtu, mtu_len); 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_port = htons(FD_Table[i].dest_port); /* don't send to my ip address and same port */ - if ((bip_dest.sin_addr.s_addr == htonl(bip_get_addr())) && - (bip_dest.sin_port == htons(bip_get_port()))) { + if ((bip_dest.sin_addr.s_addr == bip_get_addr()) && + (bip_dest.sin_port == bip_get_port())) { continue; } /* don't send to src ip address and same port */ @@ -992,8 +992,8 @@ uint16_t bvlc_receive( npdu_len -= 6; /* Broadcast locally if received via unicast from a BDT member */ if (bvlc_bdt_member_mask_is_unicast(&sin)) { - dest.sin_addr.s_addr = htonl(bip_get_broadcast_addr()); - dest.sin_port = htons(bip_get_port()); + dest.sin_addr.s_addr = bip_get_broadcast_addr(); + dest.sin_port = bip_get_port(); bvlc_send_mpdu(&dest, &npdu[4 + 6], npdu_len); } /* use the original addr from the BVLC for src */ @@ -1102,8 +1102,8 @@ uint16_t bvlc_receive( case BVLC_ORIGINAL_UNICAST_NPDU: debug_printf("BVLC: Received Original-Unicast-NPDU.\n"); /* ignore messages from me */ - if ((sin.sin_addr.s_addr == htonl(bip_get_addr())) && - (sin.sin_port == htons(bip_get_port()))) { + if ((sin.sin_addr.s_addr == bip_get_addr()) && + (sin.sin_port == bip_get_port())) { npdu_len = 0; } else { bvlc_internet_to_bacnet_address(src, &sin); @@ -1182,8 +1182,8 @@ int bvlc_send_pdu( port = ntohs(Remote_BBMD.sin_port); debug_printf("BVLC: Sent Distribute-Broadcast-to-Network.\n"); } else { - address.s_addr = bip_get_broadcast_addr(); - port = bip_get_port(); + address.s_addr = ntohl(bip_get_broadcast_addr()); + port = ntohs(bip_get_port()); mtu[1] = BVLC_ORIGINAL_BROADCAST_NPDU; debug_printf("BVLC: Sent Original-Broadcast-NPDU.\n"); }