Skip to content

Add the option to set the port used by the originiator for implicit communication. #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions src/ConnectionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,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(), _messageRouter->implicitPort());
}

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(), _messageRouter->implicitPort()));

auto result = _connectionMap
.insert(std::make_pair(response.getT2ONetworkConnectionId(), ioConnection));
Expand Down
11 changes: 8 additions & 3 deletions src/MessageRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ namespace eipScanner {
using eip::EncapsPacket;
using eip::EncapsPacketFactory;

MessageRouter::MessageRouter(bool use_8_bit_path_segments)
: _use_8_bit_path_segments(use_8_bit_path_segments)
MessageRouter::MessageRouter(bool use_8_bit_path_segments, cip::CipUint implicit_port)
: _use_8_bit_path_segments(use_8_bit_path_segments), _implicit_port(implicit_port)
{};

MessageRouter::~MessageRouter() = default;
Expand All @@ -47,7 +47,7 @@ namespace eipScanner {

CommonPacketItemFactory commonPacketItemFactory;
CommonPacket commonPacket;
commonPacket << commonPacketItemFactory.createNullAddressItem();
commonPacket << commonPacketItemFactory.createT2OSockaddrInfo(_implicit_port, 0);
commonPacket << commonPacketItemFactory.createUnconnectedDataItem(request.pack());

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

cip::CipUint
MessageRouter::implicitPort() const {
return _implicit_port;
}
}
7 changes: 6 additions & 1 deletion src/MessageRouter.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ namespace eipScanner {
/**
* @brief Default constructor
*/
MessageRouter(bool use_8_bit_path_segments=false);
MessageRouter(bool use_8_bit_path_segments=false, cip::CipUint implicit_port=EIP_DEFAULT_IMPLICIT_PORT);
MessageRouter(cip::CipUint implicit_port) : MessageRouter(false, implicit_port){ };
MessageRouter(bool use_8_bit_path_segments) : MessageRouter(use_8_bit_path_segments, EIP_DEFAULT_IMPLICIT_PORT) {};

/**
* @brief Default destructor
Expand Down Expand Up @@ -74,8 +76,11 @@ namespace eipScanner {
virtual cip::MessageRouterResponse sendRequest(SessionInfoIf::SPtr si, cip::CipUsint service,
const cip::EPath& path) const;

cip::CipUint implicitPort() const;

private:
bool _use_8_bit_path_segments;
cip::CipUint _implicit_port;
};
}

Expand Down
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