Merged revision(s) 3044 from branches/releases/bacnet-stack-0-8-0:

Added BACnet/IPv6 datalink layer and example BACnet/IPv4 to BACnet/IPv6 router.
BVLC6 layer is working on Linux port without BBMD features yet. Win32 is implemented, untested.
Tested during BACnet North American Plugfest 2016.
........
This commit is contained in:
skarg
2016-10-01 20:23:03 +00:00
parent e9a2bfcbef
commit c9d152bf15
24 changed files with 6597 additions and 20 deletions
+4
View File
@@ -96,6 +96,9 @@ error:
router: library
$(MAKE) -s -C demo router
router-ipv6:
$(MAKE) -B -s -C demo router-ipv6
# Add "ports" to the build, if desired
ports: atmega168 bdk-atxx4-mstp at91sam7s
@echo "Built the ARM7 and AVR ports"
@@ -116,4 +119,5 @@ clean:
$(MAKE) -s -C lib clean
$(MAKE) -s -C demo clean
$(MAKE) -s -C demo/router clean
$(MAKE) -s -C demo/router-ipv6 clean
$(MAKE) -s -C demo/gateway clean
+19
View File
@@ -0,0 +1,19 @@
#!/bin/bash
echo Setting parameters for Router
BACNET_IP_PORT=47808
export BACNET_IP_PORT
#BACNET_BBMD_PORT=47809
#export BACNET_BBMD_PORT
BACNET_IFACE=${1}
export BACNET_IFACE
BACNET_IP6_PORT=47808
export BACNET_IP6_PORT
BACNET_BIP6_BROADCAST=FF05::BAC0
export BACNET_BIP6_BROADCAST
BACNET_BIP6_IFACE=${1}
export BACNET_BIP6_IFACE
# Network Numbers
BACNET_IP_NET=1
export BACNET_IP_NET
BACNET_IP6_NET=2
export BACNET_IP6_NET
+1
View File
@@ -12,6 +12,7 @@ rem make BACNET_PORT=win32 BUILD=debug clean all
rem Build for MinGW MS/TP
rem make BACNET_PORT=win32 BACDL_DEFINE=-DBACDL_MSTP=1 clean all
rem make BACNET_PORT=win32 BACDL_DEFINE=-DBACDL_BIP6=1 clean all
rem On Linux, install mingw32 and use this:
rem make BACNET_PORT=win32 CC=i586-mingw32msvc-gcc AR=i586-mingw32msvc-ar clean all
+2
View File
@@ -14,6 +14,8 @@ make BACNET_PORT=win32 BUILD=release clean all > /dev/null
# Build for MinGW MS/TP
# make BACNET_PORT=win32 BACDL_DEFINE=-DBACDL_MSTP=1 clean all
# make BACNET_PORT=win32 BACDL_DEFINE=-DBACDL_BIP6=1 clean all
# make BACDL_DEFINE=-DBACDL_BIP6=1 BUILD=debug clean all
# On Linux, install mingw32 and use this:
# make BACNET_PORT=win32 CC=i586-mingw32msvc-gcc AR=i586-mingw32msvc-ar clean all
+5 -1
View File
@@ -47,7 +47,7 @@ DEBUGGING =
OPTIMIZATION = -Os
ifeq (${BUILD},debug)
OPTIMIZATION = -O0
DEBUGGING = -g
DEBUGGING = -g -DDEBUG_ENABLED=1
endif
# put all the flags together
CFLAGS := -Wall $(DEBUGGING) $(OPTIMIZATION) $(INCLUDES) $(DEFINES)
@@ -109,3 +109,7 @@ error:
router:
$(MAKE) -s -b -C router
router-ipv6:
$(MAKE) -b -C router-ipv6
+26
View File
@@ -226,6 +226,10 @@ void dlenv_maintenance_timer(
* - BACNET_MAX_MASTER
* - BACNET_MSTP_BAUD
* - BACNET_MSTP_MAC
* - BACDL_BIP6: (BACnet/IPv6)
* - BACNET_BIP6_PORT - UDP/IP port number (0..65534) used for BACnet/IPv6
* communications. Default is 47808 (0xBAC0).
* - BACNET_BIP6_BROADCAST - FF05::BAC0 or FF02::BAC0 or ...
*/
void dlenv_init(
void)
@@ -240,6 +244,27 @@ void dlenv_init(
datalink_set(NULL);
}
#endif
#if defined(BACDL_BIP6)
BACNET_IP6_ADDRESS addr;
pEnv = getenv("BACNET_BIP6_BROADCAST");
if (pEnv) {
bvlc6_address_set(&addr,
(uint16_t) strtol(pEnv, NULL, 0), 0, 0, 0, 0, 0, 0,
BIP6_MULTICAST_GROUP_ID);
bip6_set_broadcast_addr(&addr);
} else {
bvlc6_address_set(&addr,
BIP6_MULTICAST_SITE_LOCAL, 0, 0, 0, 0, 0, 0,
BIP6_MULTICAST_GROUP_ID);
bip6_set_broadcast_addr(&addr);
}
pEnv = getenv("BACNET_BIP6_PORT");
if (pEnv) {
bip6_set_port((uint16_t) strtol(pEnv, NULL, 0));
} else {
bip6_set_port(0xBAC0);
}
#endif
#if defined(BACDL_BIP)
#if defined(BIP_DEBUG)
BIP_Debug = true;
@@ -295,6 +320,7 @@ void dlenv_init(
if (pEnv) {
apdu_retries_set((uint8_t) strtol(pEnv, NULL, 0));
}
/* === Initialize the Datalink Here === */
if (!datalink_init(getenv("BACNET_IFACE"))) {
exit(1);
}
File diff suppressed because it is too large Load Diff
+53
View File
@@ -0,0 +1,53 @@
#Makefile to build BACnet Application for the GCC port
# tools - only if you need them.
# Most platforms have this already defined
# CC = gcc
# Executable file name
TARGET = bacroute
TARGET_BIN = ${TARGET}$(TARGET_EXT)
CFLAGS += -DPRINT_ENABLED=1
BACNET_SOURCE_DIR = ../../src
BACNET_HANDLER_DIR = ../handler
BACNET_OBJECT_DIR = ../object
SRC = main.c \
$(BACNET_OBJECT_DIR)/device-client.c
PORT_BIP6_SRC = \
$(BACNET_PORT_DIR)/bip6.c \
$(BACNET_SOURCE_DIR)/bvlc6.c \
$(BACNET_HANDLER_DIR)/h_bbmd6.c \
$(BACNET_SOURCE_DIR)/vmac.c
SRCS = ${SRC} ${PORT_BIP6_SRC}
OBJS = ${SRCS:.c=.o}
all: ${BACNET_LIB_TARGET} Makefile ${TARGET_BIN}
${TARGET_BIN}: ${OBJS} Makefile ${BACNET_LIB_TARGET}
${CC} ${PFLAGS} ${OBJS} ${LFLAGS} -o $@
size $@
cp $@ ../../bin
lib: ${BACNET_LIB_TARGET}
${BACNET_LIB_TARGET}:
( cd ${BACNET_LIB_DIR} ; $(MAKE) clean ; $(MAKE) )
.c.o:
${CC} -c ${CFLAGS} $*.c -o $@
depend:
rm -f .depend
${CC} -MM ${CFLAGS} *.c >> .depend
clean:
rm -f core ${TARGET_BIN} ${OBJS} ${BACNET_LIB_TARGET} $(TARGET).map
include: .depend
File diff suppressed because it is too large Load Diff
+22
View File
@@ -0,0 +1,22 @@
BACnet Simple Router Demo
=========================
The Simple Router demo connects one BACnet/IP and one BACnet/IPv6 network.
It also includes a BBMD so that Foreign Device Registration can be used
to tunnel local command line demos to BACnet/IP and BACnet IPv6 networks.
Configuration
=============
It uses environment variables to configure
the BACnet/IP port and BACnet/IPv6 address on Linux:
export BACNET_IFACE=eth0
export BACNET_BIP6_IFACE=eth0
Also uses these configurations, but defaults to these values if not set:
export BACNET_IP_PORT=47808
export BACNET_BIP6_PORT=47808
export BACNET_BIP6_BROADCAST=FF05
export BACNET_IP_NET=1
export BACNET_IP6_NET=2
+15 -4
View File
@@ -30,11 +30,13 @@
/* declare a single physical layer using your compiler define.
see datalink.h for possible defines. */
#if !(defined(BACDL_ETHERNET) || defined(BACDL_ARCNET) || defined(BACDL_MSTP) || defined(BACDL_BIP) || defined(BACDL_TEST) || defined(BACDL_ALL))
#if !(defined(BACDL_ETHERNET) || defined(BACDL_ARCNET) || \
defined(BACDL_MSTP) || defined(BACDL_BIP) || defined(BACDL_BIP6) || \
defined(BACDL_TEST) || defined(BACDL_ALL))
#define BACDL_BIP
#endif
/* optional configuration for BACnet/IP datalink layers */
/* optional configuration for BACnet/IP datalink layer */
#if (defined(BACDL_BIP) || defined(BACDL_ALL))
/* other BIP defines (define as 1 to enable):
USE_INADDR - uses INADDR_BROADCAST for broadcast and binds using INADDR_ANY
@@ -45,6 +47,13 @@
#endif
#endif
/* optional configuration for BACnet/IPv6 datalink layer */
#if defined(BACDL_BIP6)
#if !defined(BBMD6_ENABLED)
#define BBMD6_ENABLED 0
#endif
#endif
/* Enable the Gateway (Routing) functionality here, if desired. */
#if !defined(MAX_NUM_DEVICES)
#ifdef BAC_ROUTING
@@ -80,8 +89,10 @@
/* #define MAX_APDU 1476 */
#if defined(BACDL_BIP)
#define MAX_APDU 1476
/* #define MAX_APDU 128 enable this IP for testing readrange so you get the More Follows flag set */
/* #define MAX_APDU 128 enable this IP for testing
readrange so you get the More Follows flag set */
#elif defined(BACDL_BIP6)
#define MAX_APDU 1476
#elif defined (BACDL_ETHERNET)
#if defined(BACNET_SECURITY)
#define MAX_APDU 1420
+11
View File
@@ -79,6 +79,17 @@ extern void routed_get_my_address(
#define datalink_get_my_address bip_get_my_address
#endif
#elif defined(BACDL_BIP6)
#include "bip6.h"
#include "bvlc6.h"
#define datalink_init bip6_init
#define datalink_send_pdu bip6_send_pdu
#define datalink_receive bip6_receive
#define datalink_cleanup bip6_cleanup
#define datalink_get_broadcast_address bip6_get_broadcast_address
#define datalink_get_my_address bip6_get_my_address
#else /* Ie, BACDL_ALL */
#include "npdu.h"
+52
View File
@@ -0,0 +1,52 @@
/**
* @file
* @author Steve Karg
* @date 2016
*/
#ifndef VMAC_H
#define VMAC_H
#include <stdint.h>
#include <stdbool.h>
/* define the max MAC as big as IPv6 + port number */
#define VMAC_MAC_MAX 18
/**
* VMAC data structure
*
* @{
*/
struct vmac_data {
uint8_t mac[18];
uint8_t mac_len;
};
/** @} */
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
unsigned int VMAC_Count(void);
struct vmac_data *VMAC_Find_By_Key(uint32_t device_id);
bool VMAC_Find_By_Data(struct vmac_data *vmac, uint32_t *device_id);
bool VMAC_Add(uint32_t device_id, struct vmac_data *pVMAC);
bool VMAC_Delete(uint32_t device_id);
bool VMAC_Different(
struct vmac_data *vmac1,
struct vmac_data *vmac2);
bool VMAC_Match(
struct vmac_data *vmac1,
struct vmac_data *vmac2);
void VMAC_Cleanup(void);
void VMAC_Init(void);
#ifdef TEST
#include "ctest.h"
void testVMAC(
Test * pTest);
#endif
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
+14 -12
View File
@@ -163,23 +163,25 @@ PORT_BIP_SRC = \
$(BACNET_CORE)/bvlc.c \
$(BACNET_CORE)/bip.c
PORT_BIP6_SRC = \
$(BACNET_HANDLER)/h_bbmd6.c \
$(BACNET_PORT_DIR)/bip6.c \
$(BACNET_CORE)/vmac.c \
$(BACNET_CORE)/bvlc6.c
PORT_ALL_SRC = \
$(BACNET_PORT_DIR)/arcnet.c \
$(BACNET_PORT_DIR)/dlmstp.c \
$(BACNET_PORT_DIR)/rs485.c \
$(BACNET_PORT_DIR)/timer.c \
$(BACNET_CORE)/ringbuf.c \
$(BACNET_CORE)/fifo.c \
$(BACNET_CORE)/mstp.c \
$(BACNET_CORE)/crc.c \
$(BACNET_PORT_DIR)/ethernet.c \
$(BACNET_PORT_DIR)/bip-init.c \
$(BACNET_CORE)/bvlc.c \
$(BACNET_CORE)/bip.c
$(PORT_ARCNET_SRC) \
$(PORT_MSTP_SRC) \
$(PORT_ETHERNET_SRC) \
$(PORT_BIP_SRC) \
$(PORT_BIP6_SRC)
ifeq (${BACDL_DEFINE},-DBACDL_BIP=1)
PORT_SRC = ${PORT_BIP_SRC}
endif
ifeq (${BACDL_DEFINE},-DBACDL_BIP6=1)
PORT_SRC = ${PORT_BIP6_SRC}
endif
ifeq (${BACDL_DEFINE},-DBACDL_MSTP=1)
PORT_SRC = ${PORT_MSTP_SRC}
endif
+281
View File
@@ -0,0 +1,281 @@
-- bacnet_BIPV6.lua
--
-- Dissector for BACnet/IPv6 [135-2016 Annex X]
--
-- Copyright 2011, Siemens Building Technolgies
-- Maintainer Philippe Goetz <philippe.goetz@siemens.com>
-- Updated by Steve Karg <skarg@users.sourceforge.net>
--
-- This program is free software; you can redistribute it and/or
-- modify it under the terms of the GNU General Public License
-- as published by the Free Software Foundation; either version 2
-- of the License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-- Add the following to the end of init.lua in the Wireshark install directory:
-- dofile("bacnet_ipv6.lua")
-- [in] tree,pf,buffer,offset,len
-- [out] offset, value, ti, bf
function uint_dissector(tree,pf,buffer,offset,len)
if (offset + len) <= buffer:len() then
local bf = buffer(offset,len)
local ti = nil
if tree ~= nil then
ti = tree:add(pf,bf)
end
return offset + len, bf:uint(), ti, bf
end
local ti = tree:add(pf,buffer(offset))
ti:add_expert_info(PI_MALFORMED, PI_ERROR, "Data shortage")
error("Data shortage")
end
function bytes_dissector(tree,pf,buffer,offset,len)
if (offset + len) <= buffer:len() then
local bf = buffer(offset,len)
local ti = nil
if tree ~= nil then
ti = tree:add(pf,bf)
end
return offset + len, bf:bytes(), ti, bf
end
local ti = tree:add(pf,buffer(offset))
ti:add_expert_info(PI_MALFORMED, PI_ERROR, "Data shortage")
error("Data shortage")
end
p_BACnetBIPV6 = Proto("lua_BACnetBIPV6", "B/IPv6 BACnet Virtual Link Control");
p_BACnet_Port = Proto("BACnet-Port", "BACnet UDP Port Handoff")
p_dissector_bacnet = Dissector.get("bacnet")
p_dissector_bipv4 = Dissector.get("bvlc")
do
p_BACnetBIPV6.init = function()
debug("p_BACnetBIPV6.init")
end
local t_BACnetBIPV6_Types = {
[0x82]="B/IPv6 (Annex X)"
}
local t_BACnetBIPV6_Functions = {
-- Annex X, PPR3 Draft 22
[0x00]="BVLC-Result",
[0x01]="Original-Unicast-NPDU",
[0x02]="Original-Broadcast-NPDU",
[0x03]="Address-Resolution",
[0x04]="Forwarded-Address-Resolution",
[0x05]="Address-Resolution-Ack",
[0x06]="Virtual-Address-Resolution",
[0x07]="Virtual-Address-Resolution-Ack",
[0x08]="Forwarded-NPDU",
[0x09]="Register-Foreign-Device",
[0x0A]="Delete-Foreign-Device",
[0x0B]="Secure-BVLL",
[0x0C]="Distribute-Broadcast-To-Network"
}
local pf_BIPV6Data = ProtoField.bytes ("BIPV6.data", "B/IPV6 Data")
p_BACnetBIPV6.fields = {
pf_BIPV6Data
}
local t_BACnetBIPV6_dissectors = {}
-- local dt_BIPV6_npdu = DissectorTable.new("lua_BACnetBIPV6.npdu","BIPV6 NPDU",ftypes.UINT8,base.HEX)
-- BIPV6 header fields
local pf_BIPV6 = {}
pf_BIPV6.Type = ProtoField.uint8 ("BIPV6.type","Type",base.HEX,t_BACnetBIPV6_Types)
table.insert(p_BACnetBIPV6.fields,pf_BIPV6.Type)
pf_BIPV6.Function = ProtoField.uint8 ("BIPV6.function","Function",base.HEX,t_BACnetBIPV6_Functions,0,"BIPV6 Function")
table.insert(p_BACnetBIPV6.fields,pf_BIPV6.Function)
pf_BIPV6.Length = ProtoField.uint16("BIPV6.length","BIPV6-Length",base.DEC,nil,0,"Length of BIPV6")
table.insert(p_BACnetBIPV6.fields,pf_BIPV6.Length)
pf_BIPV6.ResultCode = ProtoField.uint16("BIPV6.resultCode","Result-Code",base.DEC,nil,0,"Result Code")
table.insert(p_BACnetBIPV6.fields,pf_BIPV6.ResultCode)
pf_BIPV6.SourceVirtualAddress = ProtoField.uint24 ("BIPV6.sourceVirtualAddress","Source-Virtual-Address")
table.insert(p_BACnetBIPV6.fields,pf_BIPV6.SourceVirtualAddress)
pf_BIPV6.DestinationVirtualAddress = ProtoField.uint24 ("BIPV6.destinationVirtualAddress","Destination-Virtual-Address")
table.insert(p_BACnetBIPV6.fields,pf_BIPV6.DestinationVirtualAddress)
pf_BIPV6.OriginalSourceEffectiveAddress = ProtoField.bytes ("BIPV6.originalSourceEffectiveAddress","Original-Source-B/IPv6-Address")
table.insert(p_BACnetBIPV6.fields,pf_BIPV6.OriginalSourceEffectiveAddress)
pf_BIPV6.OriginalSourceEffectiveAddress_ip = ProtoField.ipv6 ("BIPV6.originalSourceEffectiveAddress.ip","Original-Source-B/IPv6-Address (ip)")
table.insert(p_BACnetBIPV6.fields,pf_BIPV6.OriginalSourceEffectiveAddress_ip)
pf_BIPV6.OriginalSourceEffectiveAddress_port = ProtoField.uint16 ("BIPV6.originalSourceEffectiveAddress.port","Original-Source-B/IPv6-Address (port)")
table.insert(p_BACnetBIPV6.fields,pf_BIPV6.OriginalSourceEffectiveAddress_port)
pf_BIPV6.OriginalSourceVirtualAddress = ProtoField.uint24 ("BIPV6.originalSourceVirtualAddress","Original-Source-Virtual-Address")
table.insert(p_BACnetBIPV6.fields,pf_BIPV6.OriginalSourceVirtualAddress)
pf_BIPV6.DestinationEffectiveAddress = ProtoField.bytes ("BIPV6.destinationEffectiveAddress","Original-Source-B/IPv6-Address")
table.insert(p_BACnetBIPV6.fields,pf_BIPV6.DestinationEffectiveAddress)
pf_BIPV6.DestinationEffectiveAddress_ip = ProtoField.ipv6 ("BIPV6.destinationEffectiveAddress.ip","Original-Source-B/IPv6-Address (ip)")
table.insert(p_BACnetBIPV6.fields,pf_BIPV6.DestinationEffectiveAddress_ip)
pf_BIPV6.DestinationEffectiveAddress_port = ProtoField.uint16 ("BIPV6.destinationEffectiveAddress.port","Original-Source-B/IPv6-Address (port)")
table.insert(p_BACnetBIPV6.fields,pf_BIPV6.DestinationEffectiveAddress_port)
pf_BIPV6.TimeToLive = ProtoField.uint16 ("BIPV6.timeToLive","Time-To-Live")
table.insert(p_BACnetBIPV6.fields,pf_BIPV6.TimeToLive)
function p_BACnetBIPV6.dissector(buffer,pkt,tree)
pkt.cols["protocol"] = "B/IPv6"
pkt.cols["info"] = "BACnet Building Automation and Control Network"
local offset = 0
local v_BIPV6 = {}
local ti_BIPV6 = {}
ti_BIPV6.ti = tree:add(p_BACnetBIPV6,buffer(0))
offset, v_BIPV6.Type, ti_BIPV6.Type = uint_dissector(ti_BIPV6.ti, pf_BIPV6.Type, buffer, offset, 1)
--if v_BIPV6.Type == 0x81 then
-- p_dissector_bipv4:call(buffer, pkt, tree)
-- elseif v_BIPV6 ~= 0x82 then
-- ti_BIPV6.Type:add_expert_info(PI_MALFORMED, PI_WARN, "Unknown BVLC Type")
-- return
--end
if v_BIPV6.Type ~= 0x82 then
ti_BIPV6.Type:add_expert_info(PI_MALFORMED, PI_WARN, "Unknown BVLC Type")
return
end
offset, v_BIPV6.Function, ti_BIPV6.Function = uint_dissector(ti_BIPV6.ti, pf_BIPV6.Function, buffer, offset, 1)
offset, v_BIPV6.Length, ti_BIPV6.Length = uint_dissector(ti_BIPV6.ti, pf_BIPV6.Length, buffer, offset, 2)
local BIPV6Dissector = t_BACnetBIPV6_dissectors[v_BIPV6.Function]
local npduData = nil
if BIPV6Dissector ~= nil then
pkt.cols["info"] = t_BACnetBIPV6_Functions[v_BIPV6.Function]
offset, npduData = BIPV6Dissector(buffer,pkt,ti_BIPV6.ti,offset,v_BIPV6,tree)
end
if npduData ~= nil then
if p_BACnetNPDU ~= nil then
ti_BIPV6.ti:set_len(offset)
p_BACnetNPDU.dissector:call(npduData:tvb(), pkt, tree)
offset = offset + npduData:len()
else
p_dissector_bacnet:call(npduData:tvb(), pkt, tree)
end
end
-- CLB don't show the raw data
-- if offset ~= buffer:len() then
-- ti_BIPV6.ti:add(pf_BIPV6Data,buffer(offset))
-- end
ti_BIPV6 = nil
collectgarbage("collect")
end
-- [0x00] BVLC-Result
t_BACnetBIPV6_dissectors[0x00] = function(buffer,pkt,tree,offset,npdu)
offset = bytes_dissector(tree,pf_BIPV6.SourceVirtualAddress,buffer,offset,3)
offset = bytes_dissector(tree,pf_BIPV6.ResultCode,buffer,offset,2)
return offset
end
-- [0x01] Original-Unicast-NPDU
t_BACnetBIPV6_dissectors[0x01] = function(buffer,pkt,tree,offset,npdu)
offset = bytes_dissector(tree,pf_BIPV6.SourceVirtualAddress,buffer,offset,3)
offset = bytes_dissector(tree,pf_BIPV6.DestinationVirtualAddress,buffer,offset,3)
return offset, buffer(offset)
end
-- [0x02] Original-Broadcast-NPDU
t_BACnetBIPV6_dissectors[0x02] = function(buffer,pkt,tree,offset,npdu)
offset = bytes_dissector(tree,pf_BIPV6.SourceVirtualAddress,buffer,offset,3)
return offset, buffer(offset)
end
-- [0x03] Address-Resolution
t_BACnetBIPV6_dissectors[0x03] = function(buffer,pkt,tree,offset,npdu)
offset = bytes_dissector(tree,pf_BIPV6.SourceVirtualAddress,buffer,offset,3)
offset = bytes_dissector(tree,pf_BIPV6.DestinationVirtualAddress,buffer,offset,3)
return offset
end
-- [0x04] Forwarded-Address-Resolution
t_BACnetBIPV6_dissectors[0x04] = function(buffer,pkt,tree,offset,npdu)
offset = bytes_dissector(tree,pf_BIPV6.OriginalSourceVirtualAddress,buffer,offset,3)
offset = bytes_dissector(tree,pf_BIPV6.DestinationVirtualAddress,buffer,offset,3)
local subtree = tree:add(pf_BIPV6.OriginalSourceEffectiveAddress,buffer(offset,18))
offset = bytes_dissector(subtree,pf_BIPV6.OriginalSourceEffectiveAddress_ip,buffer,offset,16)
offset = uint_dissector(subtree,pf_BIPV6.OriginalSourceEffectiveAddress_port,buffer,offset,2)
return offset
end
-- [0x05] Address-Resolution-Ack
t_BACnetBIPV6_dissectors[0x05] = function(buffer,pkt,tree,offset,npdu)
offset = bytes_dissector(tree,pf_BIPV6.SourceVirtualAddress,buffer,offset,3)
offset = bytes_dissector(tree,pf_BIPV6.DestinationVirtualAddress,buffer,offset,3)
return offset
end
-- [0x06] Virtual-Address-Resolution
t_BACnetBIPV6_dissectors[0x06] = function(buffer,pkt,tree,offset,npdu)
offset = bytes_dissector(tree,pf_BIPV6.SourceVirtualAddress,buffer,offset,3)
return offset
end
-- [0x07] Virtual-Address-Resolution-Ack
t_BACnetBIPV6_dissectors[0x07] = function(buffer,pkt,tree,offset,npdu)
offset = bytes_dissector(tree,pf_BIPV6.SourceVirtualAddress,buffer,offset,3)
return offset
end
-- [0x08] Forwarded-NPDU
t_BACnetBIPV6_dissectors[0x08] = function(buffer,pkt,tree,offset,npdu)
offset = bytes_dissector(tree,pf_BIPV6.OriginalSourceVirtualAddress,buffer,offset,3)
local subtree = tree:add(pf_BIPV6.OriginalSourceEffectiveAddress,buffer(offset,18))
offset = bytes_dissector(subtree,pf_BIPV6.OriginalSourceEffectiveAddress_ip,buffer,offset,16)
offset = uint_dissector(subtree,pf_BIPV6.OriginalSourceEffectiveAddress_port,buffer,offset,2)
return offset, buffer(offset)
end
-- [0x09] Register-Foreign-Device
t_BACnetBIPV6_dissectors[0x09] = function(buffer,pkt,tree,offset,npdu)
offset = bytes_dissector(tree,pf_BIPV6.SourceVirtualAddress,buffer,offset,3)
offset = uint_dissector(tree,pf_BIPV6.TimeToLive,buffer,offset,2)
return offset
end
-- [0x0A] Delete-Foreign-Device
t_BACnetBIPV6_dissectors[0x0A] = function(buffer,pkt,tree,offset,npdu)
offset = bytes_dissector(tree,pf_BIPV6.SourceVirtualAddress,buffer,offset,3)
-- FDT Entry decoding
return offset
end
-- [0x0B] Secure-BVLL
t_BACnetBIPV6_dissectors[0x0B] = function(buffer,pkt,tree,offset,npdu)
return offset
end
-- [0x0C] Distribute-Broadcast-To-Network
t_BACnetBIPV6_dissectors[0x0C] = function(buffer,pkt,tree,offset,npdu)
offset = bytes_dissector(tree,pf_BIPV6.OriginalSourceVirtualAddress,buffer,offset,3)
local subtree = tree:add(pf_BIPV6.OriginalSourceEffectiveAddress,buffer(offset,18))
offset = bytes_dissector(subtree,pf_BIPV6.OriginalSourceEffectiveAddress_ip,buffer,offset,16)
offset = uint_dissector(subtree,pf_BIPV6.OriginalSourceEffectiveAddress_port,buffer,offset,2)
return offset, buffer(offset)
end
function p_BACnet_Port.dissector(tvb, pinfo, tree)
local BIP_Type = tvb(0,1):uint()
if BIP_Type == 0x81 then
p_dissector_bipv4:call(tvb, pinfo, tree)
elseif BIP_Type == 0x82 then
p_BACnetBIPV6.dissector(tvb, pinfo, tree)
end
end
-- load the udp.port table
local dt_udp_port = DissectorTable.get("udp.port")
-- p_dissector_bvlc = dt_udp_port:get_dissector(47808)
-- dt_udp_port:remove(47808, p_dissector_bvlc)
-- dt_udp_port:add(47808, p_BACnetBIPV6)
dt_udp_port:add(47808, p_BACnet_Port)
end
+453
View File
@@ -0,0 +1,453 @@
/*####COPYRIGHTBEGIN####
-------------------------------------------
Copyright (C) 2016 Steve Karg
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
The Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA.
As a special exception, if other files instantiate templates or
use macros or inline functions from this file, or you compile
this file and link it with other works to produce a work based
on this file, this file does not by itself cause the resulting
work to be covered by the GNU General Public License. However
the source code for this file must still be made available in
accordance with section (3) of the GNU General Public License.
This exception does not invalidate any other reasons why a work
based on this file might be covered by the GNU General Public
License.
-------------------------------------------
####COPYRIGHTEND####*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h> /* for standard integer types uint8_t etc. */
#include <stdbool.h> /* for the standard bool type. */
#include "bacdcode.h"
#include "config.h"
#include "bip6.h"
#include "debug.h"
#include "device.h"
#include "net.h"
#include <ifaddrs.h>
static void debug_print_ipv6(const char *str, const struct in6_addr * addr) {
debug_printf( "BIP6: %s %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
str,
(int)addr->s6_addr[0], (int)addr->s6_addr[1],
(int)addr->s6_addr[2], (int)addr->s6_addr[3],
(int)addr->s6_addr[4], (int)addr->s6_addr[5],
(int)addr->s6_addr[6], (int)addr->s6_addr[7],
(int)addr->s6_addr[8], (int)addr->s6_addr[9],
(int)addr->s6_addr[10], (int)addr->s6_addr[11],
(int)addr->s6_addr[12], (int)addr->s6_addr[13],
(int)addr->s6_addr[14], (int)addr->s6_addr[15]);
}
/** @file linux/bip6.c Initializes BACnet/IPv6 interface (Linux). */
/* unix socket */
static int BIP6_Socket = -1;
/* local address - filled by init functions */
static BACNET_IP6_ADDRESS BIP6_Addr;
static BACNET_IP6_ADDRESS BIP6_Broadcast_Addr;
/**
* Set the interface name. On Linux, ifname is the /dev/ name of the interface.
*
* @param ifname - C string for name or text address
*/
void bip6_set_interface(
char *ifname)
{
struct ifaddrs *ifa, *ifa_tmp;
struct sockaddr_in6 *sin;
bool found = false;
if (getifaddrs(&ifa) == -1) {
perror("BIP6: getifaddrs failed");
exit(1);
}
ifa_tmp = ifa;
debug_printf("BIP6: seeking interface: %s\n", ifname);
while (ifa_tmp) {
if ((ifa_tmp->ifa_addr) &&
(ifa_tmp->ifa_addr->sa_family == AF_INET6)) {
debug_printf("BIP6: found interface: %s\n", ifa_tmp->ifa_name);
}
if ((ifa_tmp->ifa_addr) &&
(ifa_tmp->ifa_addr->sa_family == AF_INET6) &&
(strcasecmp(ifa_tmp->ifa_name,ifname) == 0)) {
sin = (struct sockaddr_in6*) ifa_tmp->ifa_addr;
bvlc6_address_set(&BIP6_Addr,
ntohs(sin->sin6_addr.s6_addr16[0]),
ntohs(sin->sin6_addr.s6_addr16[1]),
ntohs(sin->sin6_addr.s6_addr16[2]),
ntohs(sin->sin6_addr.s6_addr16[3]),
ntohs(sin->sin6_addr.s6_addr16[4]),
ntohs(sin->sin6_addr.s6_addr16[5]),
ntohs(sin->sin6_addr.s6_addr16[6]),
ntohs(sin->sin6_addr.s6_addr16[7]));
debug_print_ipv6(ifname, (&sin->sin6_addr));
found = true;
break;
}
ifa_tmp = ifa_tmp->ifa_next;
}
if (!found) {
debug_printf("BIP6: unable to set interface: %s\n", ifname);
exit(1);
}
}
/**
* Set the BACnet IPv6 UDP port number
*
* @param port - IPv6 UDP port number
*/
void bip6_set_port(
uint16_t port)
{
BIP6_Addr.port = port;
BIP6_Broadcast_Addr.port = port;
}
/**
* Get the BACnet IPv6 UDP port number
*
* @return IPv6 UDP port number
*/
uint16_t bip6_get_port(
void)
{
return BIP6_Addr.port;
}
/**
* Get the BACnet broadcast address for my interface.
* Used as dest address in messages sent as BROADCAST
*
* @param addr - IPv6 source address
*/
void bip6_get_broadcast_address(
BACNET_ADDRESS * addr)
{
if (addr) {
addr->net = BACNET_BROADCAST_NETWORK;
addr->mac_len = 0;
addr->len = 0;
}
}
/**
* Get the IPv6 address for my interface. Used as src address in messages sent.
*
* @param addr - IPv6 source address
*/
void bip6_get_my_address(
BACNET_ADDRESS * addr)
{
uint32_t device_id = 0;
if (addr) {
device_id = Device_Object_Instance_Number();
bvlc6_vmac_address_set(addr, device_id);
}
}
/**
* Set the BACnet/IP address
*
* @param addr - network IPv6 address
*/
bool bip6_set_addr(
BACNET_IP6_ADDRESS *addr)
{
return bvlc6_address_copy(&BIP6_Addr, addr);
}
/**
* Get the BACnet/IP address
*
* @return BACnet/IP address
*/
bool bip6_get_addr(
BACNET_IP6_ADDRESS *addr)
{
return bvlc6_address_copy(addr, &BIP6_Addr);
}
/**
* Set the BACnet/IP address
*
* @param addr - network IPv6 address
*/
bool bip6_set_broadcast_addr(
BACNET_IP6_ADDRESS *addr)
{
return bvlc6_address_copy(&BIP6_Broadcast_Addr, addr);
}
/**
* Get the BACnet/IP address
*
* @return BACnet/IP address
*/
bool bip6_get_broadcast_addr(
BACNET_IP6_ADDRESS *addr)
{
return bvlc6_address_copy(addr, &BIP6_Broadcast_Addr);
}
/**
* The send function for BACnet/IPv6 driver layer
*
* @param dest - Points to a BACNET_IP6_ADDRESS structure containing the
* destination address.
* @param mtu - the bytes of data to send
* @param mtu_len - the number of bytes of data to send
*
* @return Upon successful completion, returns the number of bytes sent.
* Otherwise, -1 shall be returned and errno set to indicate the error.
*/
int bip6_send_mpdu(
BACNET_IP6_ADDRESS *dest,
uint8_t * mtu,
uint16_t mtu_len)
{
struct sockaddr_in6 bvlc_dest = { 0 };
uint16_t addr16[8];
/* assumes that the driver has already been initialized */
if (BIP6_Socket < 0) {
return 0;
}
/* load destination IP address */
bvlc_dest.sin6_family = AF_INET6;
bvlc6_address_get(dest, &addr16[0], &addr16[1], &addr16[2], &addr16[3],
&addr16[4], &addr16[5], &addr16[6], &addr16[7]);
bvlc_dest.sin6_addr.s6_addr16[0] = htons(addr16[0]);
bvlc_dest.sin6_addr.s6_addr16[1] = htons(addr16[1]);
bvlc_dest.sin6_addr.s6_addr16[2] = htons(addr16[2]);
bvlc_dest.sin6_addr.s6_addr16[3] = htons(addr16[3]);
bvlc_dest.sin6_addr.s6_addr16[4] = htons(addr16[4]);
bvlc_dest.sin6_addr.s6_addr16[5] = htons(addr16[5]);
bvlc_dest.sin6_addr.s6_addr16[6] = htons(addr16[6]);
bvlc_dest.sin6_addr.s6_addr16[7] = htons(addr16[7]);
bvlc_dest.sin6_port = htons(dest->port);
debug_print_ipv6("Sending MPDU->", &bvlc_dest.sin6_addr);
/* Send the packet */
return sendto(BIP6_Socket, (char *) mtu, mtu_len, 0,
(struct sockaddr *) &bvlc_dest, sizeof(bvlc_dest));
}
/**
* BACnet/IP Datalink Receive handler.
*
* @param src - returns the source address
* @param npdu - returns the NPDU buffer
* @param max_npdu -maximum size of the NPDU buffer
* @param timeout - number of milliseconds to wait for a packet
*
* @return Number of bytes received, or 0 if none or timeout.
*/
uint16_t bip6_receive(
BACNET_ADDRESS * src,
uint8_t * npdu,
uint16_t max_npdu,
unsigned timeout)
{
uint16_t npdu_len = 0; /* return value */
fd_set read_fds;
int max = 0;
struct timeval select_timeout;
struct sockaddr_in6 sin = { 0 };
BACNET_IP6_ADDRESS addr = {{ 0 }};
socklen_t sin_len = sizeof(sin);
int received_bytes = 0;
int offset = 0;
uint16_t i = 0;
/* Make sure the socket is open */
if (BIP6_Socket < 0) {
return 0;
}
/* we could just use a non-blocking socket, but that consumes all
the CPU time. We can use a timeout; it is only supported as
a select. */
if (timeout >= 1000) {
select_timeout.tv_sec = timeout / 1000;
select_timeout.tv_usec =
1000 * (timeout - select_timeout.tv_sec * 1000);
} else {
select_timeout.tv_sec = 0;
select_timeout.tv_usec = 1000 * timeout;
}
FD_ZERO(&read_fds);
FD_SET(BIP6_Socket, &read_fds);
max = BIP6_Socket;
/* see if there is a packet for us */
if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) {
received_bytes =
recvfrom(BIP6_Socket, (char *) &npdu[0], max_npdu, 0,
(struct sockaddr *) &sin, &sin_len);
} else {
return 0;
}
/* See if there is a problem */
if (received_bytes < 0) {
return 0;
}
/* no problem, just no bytes */
if (received_bytes == 0) {
return 0;
}
/* the signature of a BACnet/IPv6 packet */
if (npdu[0] != BVLL_TYPE_BACNET_IP6) {
return 0;
}
/* pass the packet into the BBMD handler */
debug_print_ipv6("Received MPDU->", &sin.sin6_addr);
bvlc6_address_set(&addr,
ntohs(sin.sin6_addr.s6_addr16[0]),
ntohs(sin.sin6_addr.s6_addr16[1]),
ntohs(sin.sin6_addr.s6_addr16[2]),
ntohs(sin.sin6_addr.s6_addr16[3]),
ntohs(sin.sin6_addr.s6_addr16[4]),
ntohs(sin.sin6_addr.s6_addr16[5]),
ntohs(sin.sin6_addr.s6_addr16[6]),
ntohs(sin.sin6_addr.s6_addr16[7]));
addr.port = ntohs(sin.sin6_port);
offset = bvlc6_handler(&addr, src, npdu, received_bytes);
if (offset > 0) {
npdu_len = received_bytes - offset;
if (npdu_len <= max_npdu) {
/* shift the buffer to return a valid NPDU */
for (i = 0; i < npdu_len; i++) {
npdu[i] = npdu[offset + i];
}
} else {
npdu_len = 0;
}
}
return npdu_len;
}
/** Cleanup and close out the BACnet/IP services by closing the socket.
* @ingroup DLBIP6
*/
void bip6_cleanup(
void)
{
if (BIP6_Socket != -1) {
close(BIP6_Socket);
}
BIP6_Socket = -1;
return;
}
/** Initialize the BACnet/IP services at the given interface.
* @ingroup DLBIP6
* -# Gets the local IP address and local broadcast address from the system,
* and saves it into the BACnet/IPv6 data structures.
* -# Opens a UDP socket
* -# Configures the socket for sending and receiving
* -# Configures the socket so it can send multicasts
* -# Binds the socket to the local IP address at the specified port for
* BACnet/IPv6 (by default, 0xBAC0 = 47808).
*
* @note For Linux, ifname is eth0, ath0, arc0, and others.
*
* @param ifname [in] The named interface to use for the network layer.
* If NULL, the "eth0" interface is assigned.
* @return True if the socket is successfully opened for BACnet/IP,
* else False if the socket functions fail.
*/
bool bip6_init(
char *ifname)
{
int status = 0; /* return from socket lib calls */
struct sockaddr_in6 server = {0};
struct in6_addr broadcast_address;
struct ipv6_mreq join_request;
int sockopt = 0;
if (ifname) {
bip6_set_interface(ifname);
} else {
bip6_set_interface("eth0");
}
if (BIP6_Addr.port == 0) {
bip6_set_port(0xBAC0);
}
debug_printf("BIP6: IPv6 UDP port: 0x%04X\n", htons(BIP6_Addr.port));
if (BIP6_Broadcast_Addr.address[0] == 0) {
bvlc6_address_set(&BIP6_Broadcast_Addr,
BIP6_MULTICAST_SITE_LOCAL, 0, 0, 0, 0, 0, 0,
BIP6_MULTICAST_GROUP_ID);
}
/* assumes that the driver has already been initialized */
BIP6_Socket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (BIP6_Socket < 0)
return false;
/* Allow us to use the same socket for sending and receiving */
/* This makes sure that the src port is correct when sending */
sockopt = 1;
status =
setsockopt(BIP6_Socket, SOL_SOCKET, SO_REUSEADDR, &sockopt,
sizeof(sockopt));
if (status < 0) {
close(BIP6_Socket);
BIP6_Socket = -1;
return status;
}
/* allow us to send a broadcast */
status =
setsockopt(BIP6_Socket, SOL_SOCKET, SO_BROADCAST, &sockopt,
sizeof(sockopt));
if (status < 0) {
close(BIP6_Socket);
BIP6_Socket = -1;
return false;
}
/* subscribe to a multicast address */
memcpy(&broadcast_address.s6_addr[0], &BIP6_Broadcast_Addr.address[0], IP6_ADDRESS_MAX);
memcpy(&join_request.ipv6mr_multiaddr, &broadcast_address, sizeof(struct in6_addr));
/* Let system choose the interface */
join_request.ipv6mr_interface = 0;
status = setsockopt(BIP6_Socket, IPPROTO_IPV6, IPV6_JOIN_GROUP,
&join_request, sizeof(join_request));
if (status < 0) {
perror("BIP: setsockopt(IPV6_JOIN_GROUP)");
}
/* bind the socket to the local port number and IP address */
server.sin6_family = AF_INET6;
server.sin6_addr = in6addr_any;
server.sin6_port = htons(BIP6_Addr.port);
status = bind(BIP6_Socket, (const void*)&server, sizeof(server));
if (status < 0) {
perror("BIP: bind");
close(BIP6_Socket);
BIP6_Socket = -1;
return false;
}
bvlc6_init();
return true;
}
+407
View File
@@ -0,0 +1,407 @@
/*####COPYRIGHTBEGIN####
-------------------------------------------
Copyright (C) 2016 Steve Karg
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
The Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA.
As a special exception, if other files instantiate templates or
use macros or inline functions from this file, or you compile
this file and link it with other works to produce a work based
on this file, this file does not by itself cause the resulting
work to be covered by the GNU General Public License. However
the source code for this file must still be made available in
accordance with section (3) of the GNU General Public License.
This exception does not invalidate any other reasons why a work
based on this file might be covered by the GNU General Public
License.
-------------------------------------------
####COPYRIGHTEND####*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h> /* for standard integer types uint8_t etc. */
#include <stdbool.h> /* for the standard bool type. */
#include "bacdcode.h"
#include "config.h"
#include "bip6.h"
#include "net.h"
/* Win32 Socket */
static SOCKET BIP6_Socket = INVALID_SOCKET;
/* local address - filled by init functions */
static BACNET_IP6_ADDRESS BIP6_Addr;
static BACNET_IP6_ADDRESS BIP6_Broadcast_Addr;
/* on Windows, ifname is the IPv6 address of the interface */
void bip6_set_interface(
char *ifname)
{
}
/**
* Get the BACnet broadcast address for my interface.
* Used as dest address in messages sent as BROADCAST
*
* @param addr - IPv6 source address
*/
void bip6_get_broadcast_address(
BACNET_ADDRESS * addr)
{
if (addr) {
addr->net = BACNET_BROADCAST_NETWORK;
addr->mac_len = 0;
addr->len = 0;
}
}
/**
* Get the IPv6 address for my interface. Used as src address in messages sent.
*
* @param addr - IPv6 source address
*/
void bip6_get_my_address(
BACNET_ADDRESS * addr)
{
uint32_t device_id = 0;
if (addr) {
device_id = Device_Object_Instance_Number();
bvlc6_vmac_address_set(addr, device_id);
}
}
/**
* Set the BACnet/IP address
*
* @param addr - network IPv6 address
*/
bool bip6_set_addr(
BACNET_IP6_ADDRESS *addr)
{
return bvlc6_address_copy(&BIP6_Addr, addr);
}
/**
* Get the BACnet/IP address
*
* @return BACnet/IP address
*/
bool bip6_get_addr(
BACNET_IP6_ADDRESS *addr)
{
return bvlc6_address_copy(addr, &BIP6_Addr);
}
/**
* Set the BACnet/IP address
*
* @param addr - network IPv6 address
*/
bool bip6_set_broadcast_addr(
BACNET_IP6_ADDRESS *addr)
{
return bvlc6_address_copy(&BIP6_Broadcast_Addr, addr);
}
/**
* Get the BACnet/IP address
*
* @return BACnet/IP address
*/
bool bip6_get_broadcast_addr(
BACNET_IP6_ADDRESS *addr)
{
return bvlc6_address_copy(addr, &BIP6_Broadcast_Addr);
}
/**
* The send function for BACnet/IPv6 driver layer
*
* @param dest - Points to a BACNET_IP6_ADDRESS structure containing the
* destination address.
* @param mtu - the bytes of data to send
* @param mtu_len - the number of bytes of data to send
*
* @return Upon successful completion, returns the number of bytes sent.
* Otherwise, -1 shall be returned and errno set to indicate the error.
*/
int bip6_send_mpdu(
BACNET_IP6_ADDRESS *dest,
uint8_t * mtu,
uint16_t mtu_len)
{
struct sockaddr_in6 bvlc_dest = { 0 };
/* assumes that the driver has already been initialized */
if (BIP6_Socket < 0) {
return 0;
}
/* load destination IP address */
bvlc_dest.sin6_family = AF_INET6;
memcpy(&bvlc_dest.sin6_addr, &dest->address[0], IP6_ADDRESS_MAX);
bvlc_dest.sin6_port = dest->port;
/* Send the packet */
return sendto(BIP6_Socket, (char *) mtu, mtu_len, 0,
(struct sockaddr *) &bvlc_dest, sizeof(bvlc_dest));
}
/**
* BACnet/IP Datalink Receive handler.
*
* @param src - returns the source address
* @param npdu - returns the NPDU buffer
* @param max_npdu -maximum size of the NPDU buffer
* @param timeout - number of milliseconds to wait for a packet
*
* @return Number of bytes received, or 0 if none or timeout.
*/
uint16_t bip6_receive(
BACNET_ADDRESS * src,
uint8_t * npdu,
uint16_t max_npdu,
unsigned timeout)
{
uint16_t npdu_len = 0; /* return value */
fd_set read_fds;
int max = 0;
struct timeval select_timeout;
struct sockaddr_in6 sin = { 0 };
BACNET_IP6_ADDRESS addr = {{ 0 }};
socklen_t sin_len = sizeof(sin);
int received_bytes = 0;
int offset = 0;
uint16_t i = 0;
/* Make sure the socket is open */
if (BIP6_Socket < 0) {
return 0;
}
/* we could just use a non-blocking socket, but that consumes all
the CPU time. We can use a timeout; it is only supported as
a select. */
if (timeout >= 1000) {
select_timeout.tv_sec = timeout / 1000;
select_timeout.tv_usec =
1000 * (timeout - select_timeout.tv_sec * 1000);
} else {
select_timeout.tv_sec = 0;
select_timeout.tv_usec = 1000 * timeout;
}
FD_ZERO(&read_fds);
FD_SET(BIP6_Socket, &read_fds);
max = BIP6_Socket;
/* see if there is a packet for us */
if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) {
received_bytes =
recvfrom(BIP6_Socket, (char *) &npdu[0], max_npdu, 0,
(struct sockaddr *) &sin, &sin_len);
} else {
return 0;
}
/* See if there is a problem */
if (received_bytes < 0) {
return 0;
}
/* no problem, just no bytes */
if (received_bytes == 0) {
return 0;
}
/* the signature of a BACnet/IPv6 packet */
if (npdu[0] != BVLL_TYPE_BACNET_IP6) {
return 0;
}
/* pass the packet into the BBMD handler */
memcpy(&addr.address[0], &(sin.sin6_addr), 16);
memcpy(&addr.port, &sin.sin6_port, 2);
offset = bvlc6_handler(&addr, src, npdu, received_bytes);
if (offset > 0) {
npdu_len = received_bytes - offset;
if (npdu_len <= max_npdu) {
/* shift the buffer to return a valid NPDU */
for (i = 0; i < npdu_len; i++) {
npdu[i] = npdu[offset + i];
}
} else {
npdu_len = 0;
}
}
return npdu_len;
}
/** Cleanup and close out the BACnet/IP services by closing the socket.
* @ingroup DLBIP6
*/
void bip6_cleanup(
void)
{
if (BIP6_Socket != -1) {
close(BIP6_Socket);
}
BIP6_Socket = -1;
WSACleanup();
return;
}
static LPSTR PrintError(int ErrorCode)
{
static char Message[1024];
// If this program was multithreaded, we'd want to use
// FORMAT_MESSAGE_ALLOCATE_BUFFER instead of a static buffer here.
// (And of course, free the buffer when we were done with it)
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, ErrorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR) Message, 1024, NULL);
return Message;
}
/** Initialize the BACnet/IP services at the given interface.
* @ingroup DLBIP6
* -# Gets the local IP address and local broadcast address from the system,
* and saves it into the BACnet/IPv6 data structures.
* -# Opens a UDP socket
* -# Configures the socket for sending and receiving
* -# Configures the socket so it can send multicasts
* -# Binds the socket to the local IP address at the specified port for
* BACnet/IPv6 (by default, 0xBAC0 = 47808).
*
* @note For Windows, ifname is the IPv6 address of the interface.
*
* https://msdn.microsoft.com/en-us/library/windows/desktop/ms738639(v=vs.85).aspx
*
* @param ifname [in] The named interface to use for the network layer.
*
* @return True if the socket is successfully opened for BACnet/IPv6,
* else False if the socket functions fail.
*/
bool bip6_init(
char *ifname)
{
WSADATA wd;
int i, NumSocks, RetVal, FromLen, AmountRead;
SOCKADDR_STORAGE From;
ADDRINFO Hints, *AddrInfo, *AI;
SOCKET ServSock[FD_SETSIZE];
fd_set SockSet;
// Ask for Winsock version 2.2.
if ((RetVal = WSAStartup(MAKEWORD(2, 2), &wd)) != 0) {
fprintf(stderr, "WSAStartup failed with error %d: %s\n",
RetVal, PrintError(RetVal));
WSACleanup();
return -1;
}
// By setting the AI_PASSIVE flag in the hints to getaddrinfo, we're
// indicating that we intend to use the resulting address(es) to bind
// to a socket(s) for accepting incoming connections. This means that
// when the Address parameter is NULL, getaddrinfo will return one
// entry per allowed protocol family containing the unspecified address
// for that family.
//
memset(&Hints, 0, sizeof (Hints));
Hints.ai_family = PF_INET6;
Hints.ai_socktype = SOCK_DGRAM;
Hints.ai_flags = AI_NUMERICHOST | AI_PASSIVE;
RetVal = getaddrinfo(ifname, BIP6_Addr.port, &Hints, &AddrInfo);
if (RetVal != 0) {
fprintf(stderr, "getaddrinfo failed with error %d: %s\n",
RetVal, gai_strerror(RetVal));
WSACleanup();
return -1;
}
//
// Find the first matchin address getaddrinfo returned so that
// we can create a new socket and bind that address to it,
// and create a queue to listen on.
//
for (i = 0, AI = AddrInfo; AI != NULL; AI = AI->ai_next) {
// Highly unlikely, but check anyway.
if (i == FD_SETSIZE) {
printf("getaddrinfo returned more addresses than we could use.\n");
break;
}
// only support PF_INET6.
if (AI->ai_family != PF_INET6) {
continue;
}
// only support SOCK_DGRAM.
if (AI->ai_socktype != SOCK_DGRAM) {
continue;
}
// only support IPPROTO_UDP.
if (AI->ai_protocol != IPPROTO_UDP) {
continue;
}
BIP6_Socket = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (BIP6_Socket == INVALID_SOCKET) {
fprintf(stderr, "socket() failed with error %d: %s\n",
WSAGetLastError(), PrintError(WSAGetLastError()));
continue;
}
if ((AI->ai_family == PF_INET6) &&
IN6_IS_ADDR_LINKLOCAL((IN6_ADDR *) INETADDR_ADDRESS(AI->ai_addr)) &&
(((SOCKADDR_IN6 *) (AI->ai_addr))->sin6_scope_id == 0)
) {
fprintf(stderr,
"IPv6 link local addresses should specify a scope ID!\n");
}
/* Allow us to use the same socket for sending and receiving */
/* This makes sure that the src port is correct when sending */
sockopt = 1;
RetVal =
setsockopt(BIP6_Socket, SOL_SOCKET, SO_REUSEADDR, &sockopt,
sizeof(sockopt));
if (RetVal < 0) {
closesocket(BIP6_Socket);
BIP6_Socket = INVALID_SOCKET;
continue;
}
//
// bind() associates a local address and port combination
// with the socket just created. This is most useful when
// the application is a server that has a well-known port
// that clients know about in advance.
//
if (bind(BIP6_Socket, AI->ai_addr, (int) AI->ai_addrlen) == SOCKET_ERROR) {
fprintf(stderr, "bind() failed with error %d: %s\n",
WSAGetLastError(), PrintError(WSAGetLastError()));
closesocket(ServSock[i]);
continue;
} else {
/* FIXME: store the address */
/* https://msdn.microsoft.com/en-us/library/windows/desktop/ms740496(v=vs.85).aspx */
}
i++;
if (BIP6_Socket != INVALID_SOCKET) {
break;
}
}
freeaddrinfo(AddrInfo);
if (BIP6_Socket == INVALID_SOCKET) {
fprintf(stderr, "Fatal error: unable to serve on any address.\n");
WSACleanup();
return -1;
}
return true;
}
+20
View File
@@ -28,13 +28,33 @@
#define WIN32_LEAN_AND_MEAN
#define STRICT 1
/* WindowsXP - minimum */
#define _NTDDI_VERSION_FROM_WIN32_WINNT2(ver) ver##0000
#define _NTDDI_VERSION_FROM_WIN32_WINNT(ver) _NTDDI_VERSION_FROM_WIN32_WINNT2(ver)
#ifndef _WIN32_WINNT
# define _WIN32_WINNT 0x501
#endif
#ifndef NTDDI_VERSION
# define NTDDI_VERSION _NTDDI_VERSION_FROM_WIN32_WINNT(_WIN32_WINNT)
#endif
#include <windows.h>
#if (!defined(USE_INADDR) || (USE_INADDR == 0)) && \
(!defined(USE_CLASSADDR) || (USE_CLASSADDR == 0))
#include <iphlpapi.h>
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#ifdef __MINGW32__
#include <ws2spi.h>
#else
#include <wspiapi.h>
#endif
#include <sys/timeb.h>
#ifdef _MSC_VER
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -325,7 +325,7 @@ int Keylist_Index(
}
/* returns the data specified by key */
/* returns the data specified by index */
void *Keylist_Data_Index(
OS_Keylist list,
int index)
+326
View File
@@ -0,0 +1,326 @@
/*####COPYRIGHTBEGIN####
-------------------------------------------
Copyright (C) 2015 Steve Karg
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
The Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA.
As a special exception, if other files instantiate templates or
use macros or inline functions from this file, or you compile
this file and link it with other works to produce a work based
on this file, this file does not by itself cause the resulting
work to be covered by the GNU General Public License. However
the source code for this file must still be made available in
accordance with section (3) of the GNU General Public License.
This exception does not invalidate any other reasons why a work
based on this file might be covered by the GNU General Public
License.
-------------------------------------------
####COPYRIGHTEND####*/
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
#include "bacdef.h"
#include "keylist.h"
/* me! */
#include "vmac.h"
/** @file
Handle VMAC address binding */
/* This module is used to handle the virtual MAC address binding that */
/* occurs in BACnet for ZigBee or IPv6. */
/* Key List for storing the object data sorted by instance number */
static OS_Keylist VMAC_List;
/**
* Returns the number of VMAC in the list
*/
unsigned int VMAC_Count(void)
{
return Keylist_Count(VMAC_List);
}
/**
* Adds a VMAC to the list
*
* @param device_id - BACnet device object instance number
* @param src - BACnet/IPv6 address
*
* @return true if the device ID and MAC are added
*/
bool VMAC_Add(uint32_t device_id, struct vmac_data *src)
{
bool status = false;
struct vmac_data *pVMAC = NULL;
int index = 0;
unsigned int i = 0;
pVMAC = Keylist_Data(VMAC_List, device_id);
if (!pVMAC) {
pVMAC = calloc(1, sizeof(struct vmac_data));
if (pVMAC) {
/* copy the MAC into the data store */
for (i = 0; i < sizeof(pVMAC->mac); i++) {
if (i < src->mac_len) {
pVMAC->mac[i] = src->mac[i];
} else {
break;
}
}
pVMAC->mac_len = src->mac_len;
index = Keylist_Data_Add(VMAC_List, device_id, pVMAC);
if (index >= 0) {
status = true;
printf("VMAC %u added.\n", device_id);
}
}
}
return status;
}
/**
* Finds a VMAC in the list by seeking the Device ID, and deletes it.
*
* @param device_id - BACnet device object instance number
*
* @return pointer to the VMAC data from the list - be sure to free() it!
*/
bool VMAC_Delete(uint32_t device_id)
{
bool status = false;
struct vmac_data *pVMAC;
pVMAC = Keylist_Data_Delete(VMAC_List, device_id);
if (pVMAC) {
free(pVMAC);
status = true;
}
return status;
}
/**
* Finds a VMAC in the list by seeking the Device ID.
*
* @param device_id - BACnet device object instance number
*
* @return pointer to the VMAC data from the list
*/
struct vmac_data *VMAC_Find_By_Key(uint32_t device_id)
{
return Keylist_Data(VMAC_List, device_id);
}
/** Compare the VMAC address
*
* @param vmac1 - VMAC address that will be compared to vmac2
* @param vmac2 - VMAC address that will be compared to vmac1
*
* @return true if the addresses are different
*/
bool VMAC_Different(
struct vmac_data *vmac1,
struct vmac_data *vmac2)
{
bool status = false;
unsigned int i = 0;
unsigned int mac_len = VMAC_MAC_MAX;
if (vmac1 && vmac2) {
if (vmac1->mac_len != vmac2->mac_len) {
status = true;
} else {
if (vmac1->mac_len < mac_len) {
mac_len = vmac1->mac_len;
}
for (i = 0; i < mac_len; i++) {
if (vmac1->mac[i] != vmac1->mac[i]) {
status = true;
}
}
}
}
return status;
}
/** Compare the VMAC address
*
* @param vmac1 - VMAC address that will be compared to vmac2
* @param vmac2 - VMAC address that will be compared to vmac1
*
* @return true if the addresses are the same
*/
bool VMAC_Match(
struct vmac_data *vmac1,
struct vmac_data *vmac2)
{
bool status = false;
unsigned int i = 0;
unsigned int mac_len = VMAC_MAC_MAX;
if (vmac1 && vmac2 && vmac1->mac_len) {
status = true;
if (vmac1->mac_len != vmac2->mac_len) {
status = false;
} else {
if (vmac1->mac_len < mac_len) {
mac_len = vmac1->mac_len;
}
for (i = 0; i < mac_len; i++) {
if (vmac1->mac[i] != vmac1->mac[i]) {
status = false;
}
}
}
}
return status;
}
/**
* Finds a VMAC in the list by seeking a matching VMAC address
*
* @param vmac - VMAC address that will be sought
* @param device_id - BACnet device object instance number
*
* @return true if the VMAC address was found
*/
bool VMAC_Find_By_Data(struct vmac_data *vmac, uint32_t *device_id)
{
bool status = false;
struct vmac_data *list_vmac;
int count = 0;
int index = 0;
count = Keylist_Count(VMAC_List);
while (count) {
index = count - 1;
list_vmac = Keylist_Data_Index(VMAC_List, index);
if (list_vmac) {
if (VMAC_Match(vmac, list_vmac)) {
if (device_id) {
*device_id = Keylist_Key(VMAC_List, index);
}
status = true;
break;
}
}
count--;
}
return status;
}
/**
* Cleans up the memory used by the VMAC list data
*/
void VMAC_Cleanup(void)
{
struct vmac_data *pVMAC;
if (VMAC_List) {
do {
pVMAC = Keylist_Data_Pop(VMAC_List);
if (pVMAC) {
free(pVMAC);
}
} while (pVMAC);
Keylist_Delete(VMAC_List);
VMAC_List = NULL;
}
}
/**
* Initializes the VMAC list data
*/
void VMAC_Init(void)
{
VMAC_List = Keylist_Create();
if (VMAC_List) {
atexit(VMAC_Cleanup);
printf("VMAC List initialized.\n");
}
}
#ifdef TEST
#include <assert.h>
#include <string.h>
#include "ctest.h"
void testVMAC(
Test * pTest)
{
uint32_t device_id = 123;
uint32_t test_device_id = 0;
struct vmac_data test_vmac_data;
struct vmac_data *pVMAC;
unsigned int i = 0;
bool status = false;
VMAC_Init();
for (i = 0; i < VMAC_MAC_MAX; i++) {
test_vmac_data.mac[i] = 1 + i;
}
test_vmac_data.mac_len = VMAC_MAC_MAX;
status = VMAC_Add(device_id, &test_vmac_data);
ct_test(pTest, status);
pVMAC = VMAC_Find_By_Key(0);
ct_test(pTest, pVMAC == NULL);
pVMAC = VMAC_Find_By_Key(device_id);
ct_test(pTest, pVMAC);
status = VMAC_Different(pVMAC, &test_vmac_data);
ct_test(pTest, !status);
status = VMAC_Match(pVMAC, &test_vmac_data);
ct_test(pTest, status);
status = VMAC_Find_By_Data(&test_vmac_data, &test_device_id);
ct_test(pTest, status);
ct_test(pTest, test_device_id == device_id);
status = VMAC_Delete(device_id);
ct_test(pTest, status);
pVMAC = VMAC_Find_By_Key(device_id);
ct_test(pTest, pVMAC == NULL);
VMAC_Cleanup();
}
#ifdef TEST_VMAC
int main(
void)
{
Test *pTest;
bool rc;
pTest = ct_create("BACnet VMAC", NULL);
/* individual tests */
rc = ct_addTestFunction(pTest, testVMAC);
assert(rc);
ct_setStream(pTest, stdout);
ct_run(pTest);
(void) ct_report(pTest);
ct_destroy(pTest);
return 0;
}
#endif
#endif
+12 -2
View File
@@ -9,10 +9,10 @@
LOGFILE = test.log
all: abort address arf awf bacapp bacdcode bacerror bacint bacstr \
all: abort address arf awf bvlc6 bacapp bacdcode bacerror bacint bacstr \
cov crc datetime dcc event filename fifo getevent iam ihave \
indtext keylist key memcopy npdu proplist ptransfer \
rd reject ringbuf rp rpm sbuf timesync \
rd reject ringbuf rp rpm sbuf timesync vmac \
whohas whois wp objects lighting
clean: logfile
@@ -66,6 +66,11 @@ bacstr: logfile test/bacstr.mak
( ./test/bacstr >> ${LOGFILE} )
$(MAKE) -s -C test -f bacstr.mak clean
bvlc6: logfile test/bvlc6.mak
$(MAKE) -s -C test -f bvlc6.mak clean all
( ./test/bvlc6 >> ${LOGFILE} )
$(MAKE) -s -C test -f bvlc6.mak clean
cov: logfile test/cov.mak
$(MAKE) -s -C test -f cov.mak clean all
( ./test/cov >> ${LOGFILE} )
@@ -191,6 +196,11 @@ timesync: logfile test/timesync.mak
( ./test/timesync >> ${LOGFILE} )
$(MAKE) -s -C test -f timesync.mak clean
vmac: logfile test/vmac.mak
$(MAKE) -s -C test -f vmac.mak clean all
( ./test/vmac >> ${LOGFILE} )
$(MAKE) -s -C test -f vmac.mak clean
whohas: logfile test/whohas.mak
$(MAKE) -s -C test -f whohas.mak clean all
( ./test/whohas >> ${LOGFILE} )
+42
View File
@@ -0,0 +1,42 @@
#Makefile to build test case
CC = gcc
SRC_DIR = ../src
SRC_INC = ../include
DEMO_DIR = ../demo/handler
DEMO_INC = ../demo/object
INCLUDES = -I. -I$(SRC_INC) -I$(DEMO_INC)
DEFINES = -DBIG_ENDIAN=0 -DTEST -DTEST_BBMD6
CFLAGS = -Wall -Wmissing-prototypes $(INCLUDES) $(DEFINES) -g
SRCS = $(SRC_DIR)/bacdcode.c \
$(SRC_DIR)/bacint.c \
$(SRC_DIR)/bacstr.c \
$(SRC_DIR)/bacreal.c \
$(SRC_DIR)/bvlc6.c \
$(SRC_DIR)/debug.c \
$(SRC_DIR)/keylist.c \
$(SRC_DIR)/vmac.c \
$(DEMO_DIR)/h_bbmd6.c \
ctest.c
TARGET = bbmd6
all: ${TARGET}
OBJS = ${SRCS:.c=.o}
${TARGET}: ${OBJS}
${CC} -o $@ ${OBJS}
.c.o:
${CC} -c ${CFLAGS} $*.c -o $@
depend:
rm -f .depend
${CC} -MM ${CFLAGS} *.c >> .depend
clean:
rm -rf ${TARGET} $(OBJS)
include: .depend
+32
View File
@@ -0,0 +1,32 @@
#Makefile to build test case
CC = gcc
SRC_DIR = ../src
INCLUDES = -I../include -I.
DEFINES = -DBIG_ENDIAN=0 -DTEST -DTEST_VMAC
CFLAGS = -Wall -Wmissing-prototypes $(INCLUDES) $(DEFINES) -g
SRCS = $(SRC_DIR)/keylist.c \
$(SRC_DIR)/vmac.c \
ctest.c
TARGET = vmac
all: ${TARGET}
OBJS = ${SRCS:.c=.o}
${TARGET}: ${OBJS}
${CC} -o $@ ${OBJS}
.c.o:
${CC} -c ${CFLAGS} $*.c -o $@
depend:
rm -f .depend
${CC} -MM ${CFLAGS} *.c >> .depend
clean:
rm -rf ${TARGET} $(OBJS)
include: .depend