Skip to content

Commit

Permalink
feat(bacnet): add pcap based bacnet test
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Dec 2, 2021
1 parent 13d37b4 commit ffb68a6
Show file tree
Hide file tree
Showing 6 changed files with 5,282 additions and 33 deletions.
Empty file removed plc4go/README
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*/
package org.apache.plc4x.java.spi.generation;

public interface Message {
import org.apache.plc4x.java.spi.utils.Serializable;

void serialize(WriteBuffer writeBuffer) throws SerializationException;
public interface Message extends Serializable {

int getLengthInBytes();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,29 @@ protected ChannelFuture shutdownInput() {
protected void doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
this.localAddress = localAddress;

if(!(remoteAddress instanceof PcapReplayAddress)) {
if (!(remoteAddress instanceof PcapReplayAddress)) {
logger.error("Expecting remote address of type PcapSocketAddress");
pipeline().fireExceptionCaught(
new PcapException("Expecting remote address of type PcapSocketAddress"));
pipeline().fireExceptionCaught(new PcapException("Expecting remote address of type PcapSocketAddress"));
return;
}
remoteRawSocketAddress = (PcapReplayAddress) remoteAddress;

// Get a handle to the network-device and open it.
File pcapFile = remoteRawSocketAddress.getPcapFile();
if(!pcapFile.exists()) {
logger.error(String.format("Couldn't find PCAP capture file at: %s", pcapFile.getAbsolutePath()));
pipeline().fireExceptionCaught(new PcapException(String.format(
"Couldn't find PCAP capture file at: %s", pcapFile.getAbsolutePath())));
if (!pcapFile.exists()) {
logger.error("Couldn't find PCAP capture file at: {}", pcapFile.getAbsolutePath());
PcapException exception = new PcapException(String.format("Couldn't find PCAP capture file at: %s", pcapFile.getAbsolutePath()));
pipeline().fireExceptionCaught(exception);
return;
}
if(logger.isDebugEnabled()) {
logger.debug(String.format("Opening PCAP capture file at: %s", pcapFile.getAbsolutePath()));
}
logger.debug("Opening PCAP capture file at: {}", pcapFile.getAbsolutePath());

handle = Pcaps.openOffline(remoteRawSocketAddress.getPcapFile().getAbsolutePath(),
PcapHandle.TimestampPrecision.NANO);

// If the address allows fine tuning which packets to process, set a filter to reduce the load.
String filter = "";//config.getFilterString(localAddress, remoteAddress);
if(filter.length() > 0) {
if (filter.length() > 0) {
handle.setFilter(filter, BpfProgram.BpfCompileMode.OPTIMIZE);
}

Expand All @@ -120,7 +117,7 @@ public void gotPacket(Packet packet) {
Timestamp curPacketTime = handle.getTimestamp();

// Only enable the throttling if it is not disabled.
if(config.getSpeedFactor() != PcapReplayChannelConfig.SPEED_FAST_FULL) {
if (config.getSpeedFactor() != PcapReplayChannelConfig.SPEED_FAST_FULL) {
// If last-time is not null, wait for the given number of nano-seconds.
if (lastPacketTime != null) {
int numMicrosecondsSleep = (int)
Expand All @@ -131,7 +128,7 @@ public void gotPacket(Packet packet) {

// Send the bytes to the netty pipeline.
byte[] data = config.getPacketHandler().getData(packet);
if(data != null) {
if (data != null) {
buffer.writeBytes(data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,26 @@ public Map<ChannelOption<?>, Object> getOptions() {

@Override
public <T> boolean setOption(ChannelOption<T> option, T value) {
if(option == PcapChannelOption.SUPPORT_VLANS) {
if(value instanceof Boolean) {
if (option == PcapChannelOption.SUPPORT_VLANS) {
if (value instanceof Boolean) {
supportVlans = (Boolean) value;
return true;
}
return false;
} else if(option == PcapChannelOption.PORT) {
if(value instanceof Integer) {
} else if (option == PcapChannelOption.PORT) {
if (value instanceof Integer) {
port = (Integer) value;
return true;
}
return false;
} else if(option == PcapChannelOption.PROTOCOL_ID) {
if(value instanceof Integer) {
} else if (option == PcapChannelOption.PROTOCOL_ID) {
if (value instanceof Integer) {
protocolId = (Integer) value;
return true;
}
return false;
} else if(option == PcapChannelOption.PACKET_HANDLER) {
if(value instanceof PacketHandler) {
} else if (option == PcapChannelOption.PACKET_HANDLER) {
if (value instanceof PacketHandler) {
packetHandler = (PacketHandler) value;
return true;
}
Expand Down Expand Up @@ -119,22 +119,21 @@ public PacketHandler getPacketHandler() {

public String getFilterString(SocketAddress localAddress, SocketAddress remoteAddress) {
StringBuilder sb = new StringBuilder();
if(isSupportVlans()) {
final PcapChannelConfig clone = (PcapChannelConfig) this.clone();
if (isSupportVlans()) {
final PcapChannelConfig clone = this.clone();
clone.supportVlans = false;
String subFilterString = clone.getFilterString(localAddress, remoteAddress);
if(subFilterString.isEmpty()) {
if (subFilterString.isEmpty()) {
sb.append(" and (vlan)");
} else {
sb.append(" and ((vlan and ").append(subFilterString).append(") " +
"or (").append(subFilterString).append("))");
sb.append(" and ((vlan and ").append(subFilterString).append(") or (").append(subFilterString).append("))");
}
} else {
if (getProtocolId() != ALL_PROTOCOLS) {
sb.append(" and (ether proto ").append(getProtocolId()).append(")");
}
// Add a filter for TCP or UDP port.
if(getPort() != ALL_PORTS) {
if (getPort() != ALL_PORTS) {
sb.append(" and (port ").append(getPort()).append(")");
}
// Add a filter for source or target address.
Expand All @@ -146,7 +145,7 @@ public String getFilterString(SocketAddress localAddress, SocketAddress remoteAd
sb.append(" and (host ").append(localAddress.getHostAddress()).append(")");
}*/
}
return (sb.length() > 0) ? sb.toString().substring(" and ".length()) : "";
return (sb.length() > 0) ? sb.substring(" and ".length()) : "";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@
['0x00' BACnetUnconfirmedServiceRequestIAm
[simple BACnetTagApplicationObjectIdentifier deviceIdentifier ]
[simple BACnetTagApplicationUnsignedInteger maximumApduLengthAcceptedLength ]
[simple BACnetTagApplicationEnumerated segmentationSupported ]
[simple BACnetTagApplicationUnsignedInteger vendorId ]
[simple BACnetTagApplicationEnumerated segmentationSupported ] // TODO: map to enum
[simple BACnetTagApplicationUnsignedInteger vendorId ] // TODO: vendor list?
]
['0x01' BACnetUnconfirmedServiceRequestIHave
[simple BACnetTagApplicationObjectIdentifier deviceIdentifier ]
Expand Down Expand Up @@ -582,7 +582,7 @@
[virtual bit fractionalIsWildcard 'fractional == wildcard']
]
['APPLICATION_TAGS','0xC' BACnetTagApplicationObjectIdentifier
[simple uint 10 objectType]
[simple uint 10 objectType] // TODO: map to enum
[simple uint 22 instanceNumber]
]
['CONTEXT_SPECIFIC_TAGS' BACnetTagContext (uint 4 tagNumber, uint 8 extTagNumber, uint 3 lengthValueType, uint 8 extLength)
Expand Down
Loading

0 comments on commit ffb68a6

Please sign in to comment.