Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/ConnectionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <random>

#include "ConnectionManager.h"
#include "eip/CommonPacketItemFactory.h"
#include "eip/CommonPacket.h"
#include "cip/connectionManager/ForwardOpenRequest.h"
#include "cip/connectionManager/ForwardCloseRequest.h"
Expand Down Expand Up @@ -50,7 +51,7 @@ namespace eipScanner {
ConnectionManager::~ConnectionManager() = default;

IOConnection::WPtr
ConnectionManager::forwardOpen(const SessionInfoIf::SPtr& si, ConnectionParameters connectionParameters, bool isLarge) {
ConnectionManager::forwardOpen(const SessionInfoIf::SPtr& si, ConnectionParameters connectionParameters, bool isLarge, cip::CipUint implicit_port) {
static int serialNumberCount = 0;
connectionParameters.connectionSerialNumber = ++serialNumberCount;

Expand Down Expand Up @@ -87,17 +88,18 @@ namespace eipScanner {
connectionParameters.t2oNetworkConnectionParams += 4;
}

eip::CommonPacketItemFactory commonPacketItemFactory;
MessageRouterResponse messageRouterResponse;
if (isLarge) {
LargeForwardOpenRequest request(connectionParameters);
messageRouterResponse = _messageRouter->sendRequest(si,
static_cast<cip::CipUsint>(ConnectionManagerServiceCodes::LARGE_FORWARD_OPEN),
EPath(6, 1), request.pack(), {});
EPath(6, 1), request.pack(), {commonPacketItemFactory.createT2OSockaddrInfo(implicit_port, 0)});
} else {
ForwardOpenRequest request(connectionParameters);
messageRouterResponse = _messageRouter->sendRequest(si,
static_cast<cip::CipUsint>(ConnectionManagerServiceCodes::FORWARD_OPEN),
EPath(6, 1), request.pack(), {});
EPath(6, 1), request.pack(), {commonPacketItemFactory.createT2OSockaddrInfo(implicit_port, 0)});
}

IOConnection::SPtr ioConnection;
Expand Down Expand Up @@ -145,13 +147,13 @@ namespace eipScanner {
}

} else {
ioConnection->_socket = std::make_unique<UDPSocket>(si->getRemoteEndPoint().getHost(), EIP_DEFAULT_IMPLICIT_PORT);
ioConnection->_socket = std::make_unique<UDPSocket>(si->getRemoteEndPoint().getHost(), implicit_port);
}

Logger(LogLevel::INFO) << "Open UDP socket to send data to "
<< ioConnection->_socket->getRemoteEndPoint().toString();

findOrCreateSocket(sockets::EndPoint(si->getRemoteEndPoint().getHost(), EIP_DEFAULT_IMPLICIT_PORT));
findOrCreateSocket(sockets::EndPoint(si->getRemoteEndPoint().getHost(), implicit_port));

auto result = _connectionMap
.insert(std::make_pair(response.getT2ONetworkConnectionId(), ioConnection));
Expand Down
2 changes: 1 addition & 1 deletion src/ConnectionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace eipScanner {
* @param isLarge use large forward open if true
* @return weak pointer to the created connection or nullptr if got an error
*/
IOConnection::WPtr forwardOpen(const SessionInfoIf::SPtr& si, cip::connectionManager::ConnectionParameters connectionParameters, bool isLarge = false);
IOConnection::WPtr forwardOpen(const SessionInfoIf::SPtr& si, cip::connectionManager::ConnectionParameters connectionParameters, bool isLarge = false, cip::CipUint implicit_port=EIP_DEFAULT_IMPLICIT_PORT);

/**
* @brief Opens an EIP IO connection with the EIP adapter
Expand Down
5 changes: 3 additions & 2 deletions src/MessageRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace eipScanner {
using eip::EncapsPacket;
using eip::EncapsPacketFactory;

MessageRouter::MessageRouter(bool use_8_bit_path_segments)
MessageRouter::MessageRouter(bool use_8_bit_path_segments)
: _use_8_bit_path_segments(use_8_bit_path_segments)
{};

Expand All @@ -47,7 +47,7 @@ namespace eipScanner {

CommonPacketItemFactory commonPacketItemFactory;
CommonPacket commonPacket;
commonPacket << commonPacketItemFactory.createNullAddressItem();
commonPacket << commonPacketItemFactory.createNullAddressItem();
commonPacket << commonPacketItemFactory.createUnconnectedDataItem(request.pack());

for(auto& item : additionalPacketItems) {
Expand Down Expand Up @@ -84,4 +84,5 @@ namespace eipScanner {
MessageRouter::sendRequest(SessionInfoIf::SPtr si, CipUsint service, const EPath &path) const {
return this->sendRequest(si, service, path, {}, {});
}

}
8 changes: 7 additions & 1 deletion src/eip/CommonPacketItemFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,11 @@ namespace eip {
buffer << connectionId << seqNumber;
return CommonPacketItem(CommonPacketItemIds::SEQUENCED_ADDRESS_ITEM, buffer.data());
}
CommonPacketItem
CommonPacketItemFactory::createT2OSockaddrInfo(cip::CipUint port, cip::CipUdint address) const {
Buffer buffer;
buffer << htons(cip::CipInt(2)) << htons(port) << htonl(address) << cip::CipUdint(0) << cip::CipUdint(0);
return CommonPacketItem(CommonPacketItemIds::T2O_SOCKADDR_INFO, buffer.data());
}
}
}
}
1 change: 1 addition & 0 deletions src/eip/CommonPacketItemFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace eip {
CommonPacketItem createUnconnectedDataItem(const std::vector<uint8_t> &data) const;
CommonPacketItem createSequenceAddressItem(cip::CipUdint connectionId, cip::CipUdint seqNumber) const;
CommonPacketItem createConnectedDataItem(const std::vector<uint8_t> &data) const;
CommonPacketItem createT2OSockaddrInfo(cip::CipUint port, cip::CipUdint address) const;
};
}
}
Expand Down