Made the BACnet/IP use the same socket as the receive so that the src port number would be 0XBAC0 so that the MAC address of the I-Am message would resolve to the correct port.

This commit is contained in:
skarg
2005-05-05 18:47:40 +00:00
parent 32a88cf279
commit 6470db6f66
+16 -22
View File
@@ -38,7 +38,7 @@
#include "bacdcode.h" #include "bacdcode.h"
#include "bip.h" #include "bip.h"
static int BIP_Receive_Socket = -1; static int BIP_Socket = -1;
/* port to use - stored in network byte order */ /* port to use - stored in network byte order */
static uint16_t BIP_Port = 0; static uint16_t BIP_Port = 0;
/* IP Address - stored in network byte order */ /* IP Address - stored in network byte order */
@@ -48,14 +48,14 @@ static struct in_addr BIP_Broadcast_Address = {{{-1}}};
bool bip_valid(void) bool bip_valid(void)
{ {
return (BIP_Receive_Socket != -1); return (BIP_Socket != -1);
} }
void bip_cleanup(void) void bip_cleanup(void)
{ {
if (bip_valid()) if (bip_valid())
close(BIP_Receive_Socket); close(BIP_Socket);
BIP_Receive_Socket = -1; BIP_Socket = -1;
return; return;
} }
@@ -105,8 +105,8 @@ bool bip_init(void)
bip_set_port(0xBAC0); bip_set_port(0xBAC0);
// assumes that the driver has already been initialized // assumes that the driver has already been initialized
BIP_Receive_Socket = socket(AF_INET, SOCK_DGRAM, 0); BIP_Socket = socket(AF_INET, SOCK_DGRAM, 0);
if (BIP_Receive_Socket < 0) if (BIP_Socket < 0)
return false; return false;
// bind the socket to the local port number and IP address // bind the socket to the local port number and IP address
@@ -114,12 +114,12 @@ bool bip_init(void)
sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = BIP_Port; sin.sin_port = BIP_Port;
memset(&(sin.sin_zero), '\0', 8); memset(&(sin.sin_zero), '\0', 8);
rv = bind(BIP_Receive_Socket, rv = bind(BIP_Socket,
(const struct sockaddr*)&sin, sizeof(struct sockaddr)); (const struct sockaddr*)&sin, sizeof(struct sockaddr));
if (rv < 0) if (rv < 0)
{ {
close(BIP_Receive_Socket); close(BIP_Socket);
BIP_Receive_Socket = -1; BIP_Socket = -1;
return false; return false;
} }
@@ -134,17 +134,13 @@ static int bip_send(
unsigned pdu_len) // number of bytes of data unsigned pdu_len) // number of bytes of data
{ {
int bytes = 0; int bytes = 0;
int bip_send_socket = -1;
uint8_t mtu[MAX_MPDU] = { 0 }; uint8_t mtu[MAX_MPDU] = { 0 };
int mtu_len = 0; int mtu_len = 0;
int i = 0; int i = 0;
// assumes that the driver has already been initialized // assumes that the driver has already been initialized
// FIXME: can we use the same socket over and over? if (BIP_Socket < 0)
// FIXME: can we use the same socket as receive bip? return BIP_Socket;
bip_send_socket = socket(AF_INET, SOCK_DGRAM, 0);
if (bip_send_socket < 0)
return bip_send_socket;
mtu[0] = 0x81; /* BVLL for BACnet/IP */ mtu[0] = 0x81; /* BVLL for BACnet/IP */
if (bip_dest->sin_addr.s_addr == BIP_Broadcast_Address.s_addr) if (bip_dest->sin_addr.s_addr == BIP_Broadcast_Address.s_addr)
@@ -157,12 +153,10 @@ static int bip_send(
mtu_len += pdu_len; mtu_len += pdu_len;
/* Send the packet */ /* Send the packet */
bytes = sendto(bip_send_socket, (char *)mtu, mtu_len, 0, bytes = sendto(BIP_Socket, (char *)mtu, mtu_len, 0,
(struct sockaddr *)bip_dest, (struct sockaddr *)bip_dest,
sizeof(struct sockaddr)); sizeof(struct sockaddr));
close(bip_send_socket);
return bytes; return bytes;
} }
@@ -221,7 +215,7 @@ uint16_t bip_receive(
int sin_len = sizeof(sin); int sin_len = sizeof(sin);
/* Make sure the socket is open */ /* Make sure the socket is open */
if (BIP_Receive_Socket < 0) if (BIP_Socket < 0)
return 0; return 0;
/* we could just use a non-blocking socket, but that consumes all /* we could just use a non-blocking socket, but that consumes all
@@ -239,11 +233,11 @@ uint16_t bip_receive(
select_timeout.tv_usec = 1000 * timeout; select_timeout.tv_usec = 1000 * timeout;
} }
FD_ZERO(&read_fds); FD_ZERO(&read_fds);
FD_SET(BIP_Receive_Socket, &read_fds); FD_SET(BIP_Socket, &read_fds);
max = BIP_Receive_Socket; max = BIP_Socket;
/* see if there is a packet for us */ /* see if there is a packet for us */
if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0)
received_bytes = recvfrom(BIP_Receive_Socket, received_bytes = recvfrom(BIP_Socket,
(char *)&buf[0], MAX_MPDU, 0, (char *)&buf[0], MAX_MPDU, 0,
(struct sockaddr *)&sin, &sin_len); (struct sockaddr *)&sin, &sin_len);
else else