Added support for actual subnet mask in Windows port. Thanks to Thomas Neumann!
This commit is contained in:
@@ -10,7 +10,7 @@
|
|||||||
#define BACDL_BIP
|
#define BACDL_BIP
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* optional debug info for BACnet/IP datalink layers */
|
/* optional configuration for BACnet/IP datalink layers */
|
||||||
#if (defined(BACDL_BIP) || defined(BACDL_ALL))
|
#if (defined(BACDL_BIP) || defined(BACDL_ALL))
|
||||||
#if !defined(USE_INADDR)
|
#if !defined(USE_INADDR)
|
||||||
#define USE_INADDR 1
|
#define USE_INADDR 1
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
/*####COPYRIGHTBEGIN####
|
/*####COPYRIGHTBEGIN####
|
||||||
-------------------------------------------
|
-------------------------------------------
|
||||||
Copyright (C) 2005 Steve Karg
|
Copyright (C) 2005 Steve Karg
|
||||||
|
Contributions by Thomas Neumann in 2008.
|
||||||
|
|
||||||
This program is free software; you can redistribute it and/or
|
This program is free software; you can redistribute it and/or
|
||||||
modify it under the terms of the GNU General Public License
|
modify it under the terms of the GNU General Public License
|
||||||
@@ -37,6 +38,7 @@
|
|||||||
#include <stdint.h> /* for standard integer types uint8_t etc. */
|
#include <stdint.h> /* for standard integer types uint8_t etc. */
|
||||||
#include <stdbool.h> /* for the standard bool type. */
|
#include <stdbool.h> /* for the standard bool type. */
|
||||||
#include "bacdcode.h"
|
#include "bacdcode.h"
|
||||||
|
#include "config.h"
|
||||||
#include "bip.h"
|
#include "bip.h"
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
|
|
||||||
@@ -80,15 +82,61 @@ static long gethostaddr(
|
|||||||
return *(long *) host_ent->h_addr;
|
return *(long *) host_ent->h_addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set_broadcast_address(
|
#if !defined(USE_INADDR) && !defined(USE_CLASSADDR)
|
||||||
uint32_t net_address)
|
static uint32_t getIpMaskForIpAddress( uint32_t ipAddress )
|
||||||
|
{
|
||||||
|
/* Allocate information for up to 16 NICs */
|
||||||
|
IP_ADAPTER_INFO AdapterInfo[16];
|
||||||
|
/* Save memory size of buffer */
|
||||||
|
DWORD dwBufLen = sizeof(AdapterInfo);
|
||||||
|
uint32_t ipMask = INADDR_BROADCAST;
|
||||||
|
|
||||||
|
PIP_ADAPTER_INFO pAdapterInfo;
|
||||||
|
|
||||||
|
/* GetAdapterInfo:
|
||||||
|
[out] buffer to receive data
|
||||||
|
[in] size of receive data buffer */
|
||||||
|
DWORD dwStatus = GetAdaptersInfo(
|
||||||
|
AdapterInfo,
|
||||||
|
&dwBufLen);
|
||||||
|
if( dwStatus == ERROR_SUCCESS ) {
|
||||||
|
/* Verify return value is valid, no buffer overflow
|
||||||
|
Contains pointer to current adapter info */
|
||||||
|
pAdapterInfo = AdapterInfo;
|
||||||
|
|
||||||
|
do {
|
||||||
|
IP_ADDR_STRING* pIpAddressInfo = &pAdapterInfo->IpAddressList;
|
||||||
|
do {
|
||||||
|
unsigned long adapterAddress = inet_addr(pIpAddressInfo->IpAddress.String);
|
||||||
|
unsigned long adapterMask = inet_addr(pIpAddressInfo->IpMask.String);
|
||||||
|
if( adapterAddress == ipAddress ) {
|
||||||
|
ipMask = adapterMask;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
pIpAddressInfo = pIpAddressInfo->Next;
|
||||||
|
}
|
||||||
|
while(pIpAddressInfo);
|
||||||
|
if( ipMask != 0L ) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
/* Progress through linked list */
|
||||||
|
pAdapterInfo = pAdapterInfo->Next;
|
||||||
|
/* Terminate on last adapter */
|
||||||
|
} while(pAdapterInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ipMask;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static void set_broadcast_address(uint32_t net_address)
|
||||||
{
|
{
|
||||||
#if USE_INADDR
|
#if USE_INADDR
|
||||||
/* Note: sometimes INADDR_BROADCAST does not let me get
|
/* Note: sometimes INADDR_BROADCAST does not let me get
|
||||||
any unicast messages. Not sure why... */
|
any unicast messages. Not sure why... */
|
||||||
(void) net_address;
|
(void) net_address;
|
||||||
bip_set_broadcast_addr(INADDR_BROADCAST);
|
bip_set_broadcast_addr(INADDR_BROADCAST);
|
||||||
#else
|
#elif USE_CLASSADDR
|
||||||
long broadcast_address = 0;
|
long broadcast_address = 0;
|
||||||
|
|
||||||
if (IN_CLASSA(ntohl(net_address)))
|
if (IN_CLASSA(ntohl(net_address)))
|
||||||
@@ -106,6 +154,15 @@ static void set_broadcast_address(
|
|||||||
else
|
else
|
||||||
broadcast_address = INADDR_BROADCAST;
|
broadcast_address = INADDR_BROADCAST;
|
||||||
bip_set_broadcast_addr(htonl(broadcast_address));
|
bip_set_broadcast_addr(htonl(broadcast_address));
|
||||||
|
#else
|
||||||
|
long broadcast_address = 0;
|
||||||
|
long mask = 0;
|
||||||
|
|
||||||
|
mask = getIpMaskForIpAddress( net_address );
|
||||||
|
|
||||||
|
broadcast_address = (ntohl(net_address) & ~mask) | mask;
|
||||||
|
|
||||||
|
bip_set_broadcast_addr(htonl(broadcast_address));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,8 @@
|
|||||||
#define WIN32_LEAN_AND_MEAN
|
#define WIN32_LEAN_AND_MEAN
|
||||||
#define STRICT 1
|
#define STRICT 1
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
|
#include <Iphlpapi.h>
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
|
|
||||||
#define close closesocket
|
#define close closesocket
|
||||||
|
|||||||
Reference in New Issue
Block a user