modified the BACnet/IP init code for the win32 platform to make it work on my system. It seems that something else is intercepting the packets when the bind was set to INADDR_ANY, so I just bind to the host default adapter.

This commit is contained in:
skarg
2005-12-07 17:01:26 +00:00
parent b7321923af
commit ca72bf7f9f
7 changed files with 88 additions and 18 deletions
+1 -1
View File
@@ -123,7 +123,7 @@ bool bip_init(void)
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 = htons(bip_get_port());
memset(&(sin.sin_zero), '\0', 8); memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero));
status = bind(sock_fd, status = bind(sock_fd,
(const struct sockaddr*)&sin, sizeof(struct sockaddr)); (const struct sockaddr*)&sin, sizeof(struct sockaddr));
if (status < 0) if (status < 0)
Binary file not shown.
+14 -2
View File
@@ -67,7 +67,7 @@ LINK32=link.exe
# PROP Ignore_Export_Lib 0 # PROP Ignore_Export_Lib 0
# PROP Target_Dir "" # PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c # ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "c:\code\bacnet-stack\\" /I "c:\code\bacnet-stack\ports\win32\\" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "BACDL_BIP" /FD /GZ /c # ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "c:\code\bacnet-stack\\" /I "c:\code\bacnet-stack\ports\win32\\" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "BACDL_BIP" /FR /FD /GZ /c
# SUBTRACT CPP /YX # SUBTRACT CPP /YX
# ADD BASE RSC /l 0x409 /d "_DEBUG" # ADD BASE RSC /l 0x409 /d "_DEBUG"
# ADD RSC /l 0x409 /d "_DEBUG" # ADD RSC /l 0x409 /d "_DEBUG"
@@ -125,11 +125,19 @@ SOURCE=..\..\..\bacfile.c
# End Source File # End Source File
# Begin Source File # Begin Source File
SOURCE=..\..\..\bactext.c
# End Source File
# Begin Source File
SOURCE=..\..\..\bigend.c SOURCE=..\..\..\bigend.c
# End Source File # End Source File
# Begin Source File # Begin Source File
SOURCE=..\bip.c SOURCE="..\bip-init.c"
# End Source File
# Begin Source File
SOURCE=..\..\..\bip.c
# End Source File # End Source File
# Begin Source File # Begin Source File
@@ -153,6 +161,10 @@ SOURCE=..\..\..\iam.c
# End Source File # End Source File
# Begin Source File # Begin Source File
SOURCE=..\..\..\indtext.c
# End Source File
# Begin Source File
SOURCE=..\main.c SOURCE=..\main.c
# End Source File # End Source File
# Begin Source File # Begin Source File
Binary file not shown.
Binary file not shown.
+36 -11
View File
@@ -80,6 +80,11 @@ static void set_broadcast_address(uint32_t net_address)
#endif #endif
} }
static void cleanup(void)
{
WSACleanup();
}
bool bip_init(void) bool bip_init(void)
{ {
int rv = 0; // return from socket lib calls int rv = 0; // return from socket lib calls
@@ -92,7 +97,6 @@ bool bip_init(void)
struct in_addr address; struct in_addr address;
Result = WSAStartup(MAKEWORD(2,2), &wd); Result = WSAStartup(MAKEWORD(2,2), &wd);
if (Result != 0) if (Result != 0)
{ {
Code = WSAGetLastError(); Code = WSAGetLastError();
@@ -100,6 +104,8 @@ bool bip_init(void)
Code); Code);
exit(1); exit(1);
} }
atexit(cleanup);
address.s_addr = gethostaddr(); address.s_addr = gethostaddr();
if (address.s_addr == (unsigned)-1) if (address.s_addr == (unsigned)-1)
{ {
@@ -116,7 +122,7 @@ bool bip_init(void)
sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
bip_set_socket(sock_fd); bip_set_socket(sock_fd);
if (sock_fd < 0) if (sock_fd < 0)
return false; return false;
// Allow us to use the same socket for sending and receiving // Allow us to use the same socket for sending and receiving
// This makes sure that the src port is correct when sending // This makes sure that the src port is correct when sending
@@ -124,25 +130,44 @@ bool bip_init(void)
(char *)&value, sizeof(value)); (char *)&value, sizeof(value));
if (rv < 0) if (rv < 0)
{ {
close(sock_fd); close(sock_fd);
bip_set_socket(-1); bip_set_socket(-1);
return false; return false;
} }
// allow us to send a broadcast // allow us to send a broadcast
rv = setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST, rv = setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST,
(char *)&value, sizeof(value)); (char *)&value, sizeof(value));
if (rv < 0) if (rv < 0)
{ {
close(sock_fd); close(sock_fd);
bip_set_socket(-1); bip_set_socket(-1);
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
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY); /* by setting sin.sin_addr.s_addr to INADDR_ANY,
I am telling the IP stack to automatically fill
in the IP address of the machine the process
is running on.
Some server computers have multiple IP addresses.
A socket bound to one of these will not accept
connections to another address. Frequently you prefer
to allow any one of the computer's IP addresses
to be used for connections. Use INADDR_ANY (0L) to
allow clients to connect using any one of the host's
IP addresses.
Note: sometimes INADDR_ANY does not let me get
any unicast messages. Not sure why...
*/
/* sin.sin_addr.s_addr = htonl(INADDR_ANY); */
/* or we could use the specific adapter address
note: already in network byte order */
sin.sin_addr.s_addr = address.s_addr;
sin.sin_port = htons(bip_get_port()); sin.sin_port = htons(bip_get_port());
memset(&(sin.sin_zero), '\0', 8); memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero));
rv = bind(sock_fd, rv = bind(sock_fd,
(const struct sockaddr*)&sin, sizeof(struct sockaddr)); (const struct sockaddr*)&sin, sizeof(struct sockaddr));
if (rv < 0) if (rv < 0)
+37 -4
View File
@@ -29,6 +29,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <conio.h> /* for kbhit and getch */
#include "iam.h" #include "iam.h"
#include "address.h" #include "address.h"
#include "config.h" #include "config.h"
@@ -171,7 +172,7 @@ static void print_address(
BACNET_ADDRESS *dest) // destination address BACNET_ADDRESS *dest) // destination address
{ {
int i = 0; // counter int i = 0; // counter
if (dest) if (dest)
{ {
printf("%s: ",name); printf("%s: ",name);
@@ -183,6 +184,30 @@ static void print_address(
} }
} }
static void print_address_cache(void)
{
unsigned i,j;
BACNET_ADDRESS address;
uint32_t device_id = 0;
unsigned max_apdu = 0;
fprintf(stderr,"Device\tMAC\tMaxAPDU\tNet\n");
for (i = 0; i < MAX_ADDRESS_CACHE; i++)
{
if (address_get_by_index(i,&device_id, &max_apdu, &address))
{
fprintf(stderr,"%u\t",device_id);
for (j = 0; j < address.mac_len; j++)
{
fprintf(stderr,"%02X",address.mac[j]);
}
fprintf(stderr,"\t");
fprintf(stderr,"%hu\t",max_apdu);
fprintf(stderr,"%hu\n",address.net);
}
}
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
BACNET_ADDRESS src = {0}; // address where message came from BACNET_ADDRESS src = {0}; // address where message came from
@@ -200,12 +225,12 @@ int main(int argc, char *argv[])
if (!bip_init()) if (!bip_init())
return 1; return 1;
datalink_get_broadcast_address(&broadcast_address); datalink_get_broadcast_address(&broadcast_address);
print_address("Broadcast",&broadcast_address); print_address("Broadcast",&broadcast_address);
datalink_get_my_address(&my_address); datalink_get_my_address(&my_address);
print_address("Address",&my_address); print_address("Address",&my_address);
printf("BACnet stack running...\n"); printf("BACnet stack running...\n");
// loop forever // loop forever
for (;;) for (;;)
{ {
@@ -243,5 +268,13 @@ int main(int argc, char *argv[])
// output // output
// blink LEDs, Turn on or off outputs, etc // blink LEDs, Turn on or off outputs, etc
/* wait for ESC from keyboard before quitting */
if (kbhit() && (getch() == 0x1B))
break;
} }
print_address_cache();
return 0;
} }