From 52ca1aac0adc40d34ce3638508be66649f62312e Mon Sep 17 00:00:00 2001 From: Stefan Broekman Date: Sat, 3 Apr 2021 00:18:31 +0200 Subject: [PATCH 01/11] Improve Windows (Winsock) support and resolve multiple warnings --- src/ConnectionManager.cpp | 22 +++++---- src/DiscoveryManager.cpp | 14 ++++-- src/DiscoveryManager.h | 2 +- src/IOConnection.cpp | 12 ++--- src/IdentityObject.cpp | 13 ++++-- src/ParameterObject.cpp | 25 +++++++---- src/ParameterObject.h | 2 +- src/cip/CipString.h | 4 +- src/cip/EPath.cpp | 4 +- src/cip/Types.h | 5 +-- .../connectionManager/ForwardCloseRequest.cpp | 2 +- .../NetworkConnectionParametersBuilder.cpp | 4 +- src/eip/CommonPacketItem.cpp | 4 +- src/eip/EncapsPacket.cpp | 6 +-- src/fileObject/FileObjectEmptyState.cpp | 5 ++- src/fileObject/FileObjectNonExistentState.cpp | 5 ++- src/fileObject/FileObjectState.cpp | 9 ++-- .../FileObjectUploadInProgressState.cpp | 4 +- src/sockets/BaseSocket.cpp | 17 +++---- src/sockets/EndPoint.cpp | 15 ++++--- src/sockets/EndPoint.h | 4 +- src/sockets/Platform.h | 30 +++++++++++++ src/sockets/TCPSocket.cpp | 45 +++++++++---------- src/sockets/UDPBoundSocket.cpp | 5 ++- src/sockets/UDPSocket.cpp | 29 +++++------- src/utils/Buffer.cpp | 2 +- src/utils/Logger.h | 2 +- src/vendor/ra/powerFlex525/DPIFaultObject.cpp | 2 +- .../ra/powerFlex525/DPIFaultParameter.cpp | 20 ++++----- .../yaskawa/mp3300iec/Yaskawa_EPath.cpp | 2 +- 30 files changed, 185 insertions(+), 130 deletions(-) create mode 100644 src/sockets/Platform.h diff --git a/src/ConnectionManager.cpp b/src/ConnectionManager.cpp index 0e26d92..4a8e11a 100644 --- a/src/ConnectionManager.cpp +++ b/src/ConnectionManager.cpp @@ -51,7 +51,7 @@ namespace eipScanner { IOConnection::WPtr ConnectionManager::forwardOpen(const SessionInfoIf::SPtr& si, ConnectionParameters connectionParameters, bool isLarge) { - static int serialNumberCount = 0; + static cip::CipUint serialNumberCount = 0; connectionParameters.connectionSerialNumber = ++serialNumberCount; NetworkConnectionParametersBuilder o2tNCP(connectionParameters.o2tNetworkConnectionParams, isLarge); @@ -71,7 +71,7 @@ namespace eipScanner { auto t2oSize = t2oNCP.getConnectionSize(); connectionParameters.connectionPathSize = (connectionParameters.connectionPath .size() / 2) - + (connectionParameters.connectionPath .size() % 2); + + (connectionParameters.connectionPath.size() % 2); if ((connectionParameters.transportTypeTrigger & NetworkConnectionParams::CLASS1) > 0 || (connectionParameters.transportTypeTrigger & NetworkConnectionParams::CLASS3) > 0) { @@ -87,22 +87,17 @@ namespace eipScanner { connectionParameters.t2oNetworkConnectionParams += 4; } - Buffer buffer; - buffer << sockets::EndPoint("0.0.0.0", 2222); - eip::CommonPacketItem addrItem(eip::CommonPacketItemIds::T2O_SOCKADDR_INFO, buffer.data()); - - MessageRouterResponse messageRouterResponse; if (isLarge) { LargeForwardOpenRequest request(connectionParameters); messageRouterResponse = _messageRouter->sendRequest(si, static_cast(ConnectionManagerServiceCodes::LARGE_FORWARD_OPEN), - EPath(6, 1), request.pack(), {addrItem}); + EPath(6, 1), request.pack(), {}); } else { ForwardOpenRequest request(connectionParameters); messageRouterResponse = _messageRouter->sendRequest(si, static_cast(ConnectionManagerServiceCodes::FORWARD_OPEN), - EPath(6, 1), request.pack(), {addrItem}); + EPath(6, 1), request.pack(), {}); } IOConnection::SPtr ioConnection; @@ -148,9 +143,9 @@ namespace eipScanner { } else { ioConnection->_socket = std::make_unique(endPoint); } - + } else { - ioConnection->_socket = std::make_unique(si->getRemoteEndPoint().getHost(), 2222); + ioConnection->_socket = std::make_unique(si->getRemoteEndPoint().getHost(), 2222); } Logger(LogLevel::INFO) << "Open UDP socket to send data to " @@ -202,6 +197,7 @@ namespace eipScanner { } auto rc = _connectionMap.erase(ptr->_t2oNetworkConnectionId); + (void) rc; assert(rc); } else { Logger(LogLevel::WARNING) << "Attempt to close an already closed connection"; @@ -212,6 +208,7 @@ namespace eipScanner { std::vector sockets; std::transform(_socketMap.begin(), _socketMap.end(), std::back_inserter(sockets), [](auto entry) { auto fd = entry.second->getSocketFd(); + (void) fd; return entry.second; }); @@ -235,6 +232,7 @@ namespace eipScanner { auto newSocket = std::make_shared(endPoint); _socketMap[endPoint] = newSocket; newSocket->setBeginReceiveHandler([](sockets::BaseSocket& sock) { + (void) sock; Logger(LogLevel::DEBUG) << "Received something"; }); @@ -253,7 +251,7 @@ namespace eipScanner { if (io != _connectionMap.end()) { io->second->notifyReceiveData(commonPacket.getItems().at(1).getData()); } else { - Logger(LogLevel::ERROR) << "Received data from unknow connection T2O_ID=" << connectionId; + Logger(LogLevel::ERROR) << "Received data from unknown connection T2O_ID=" << connectionId; } }); diff --git a/src/DiscoveryManager.cpp b/src/DiscoveryManager.cpp index 16cbb97..c0cf26a 100644 --- a/src/DiscoveryManager.cpp +++ b/src/DiscoveryManager.cpp @@ -2,7 +2,6 @@ // Created by Aleksey Timin on 12/17/19. // #include -#include #include "eip/EncapsPacketFactory.h" #include "eip/CommonPacket.h" @@ -11,6 +10,13 @@ #include "utils/Buffer.h" #include "DiscoveryManager.h" +#include "sockets/Platform.h" + +#if defined (__unix__) +#define DISCOVER_END (EAGAIN) +#elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) +#define DISCOVER_END (ETIMEDOUT) +#endif namespace eipScanner { using namespace cip; @@ -49,7 +55,7 @@ namespace eipScanner { CommonPacket commonPacket; commonPacket.expand(std::vector(data.begin()+EncapsPacket::HEADER_SIZE, data.end())); - for (int i=0; i < commonPacket.getItems().size(); ++i) { + for (CipUint i=0; i < commonPacket.getItems().size(); ++i) { Buffer buffer(commonPacket.getItems()[i].getData()); CipUint ignore; sockets::EndPoint socketAddr("", 0); @@ -79,7 +85,7 @@ namespace eipScanner { } } } catch (std::system_error& er) { - if (er.code().value() != EAGAIN) { + if (er.code().value() != DISCOVER_END) { throw er; } } @@ -93,7 +99,7 @@ namespace eipScanner { int broadcast = 1; if(setsockopt(socket->getSocketFd(), SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, sizeof(broadcast)) < 0) { - throw std::system_error(errno, std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), std::generic_category()); } return socket; diff --git a/src/DiscoveryManager.h b/src/DiscoveryManager.h index 2cba8cd..33d185e 100644 --- a/src/DiscoveryManager.h +++ b/src/DiscoveryManager.h @@ -37,7 +37,7 @@ namespace eipScanner { /** * @brief default destructor */ - ~DiscoveryManager(); + virtual ~DiscoveryManager(); /** * @brief Discovers the EIP network diff --git a/src/IOConnection.cpp b/src/IOConnection.cpp index 148c89c..5571496 100644 --- a/src/IOConnection.cpp +++ b/src/IOConnection.cpp @@ -31,20 +31,20 @@ namespace eipScanner { , _connectionTimeoutMultiplier{0} , _connectionTimeoutCount{0} , _o2tSequenceNumber{0} + , _t2oSequenceNumber{0} + , _serialNumber{0} , _transportTypeTrigger{0} , _o2tRealTimeFormat{0} , _t2oRealTimeFormat{0} - , _t2oSequenceNumber{0} , _sequenceValueCount{0} , _connectionPath(0) , _originatorVendorId{0} , _originatorSerialNumber{0} - , _serialNumber{0} , _outputData() - , _lastHandleTime(std::chrono::steady_clock::now()) - , _receiveDataHandle([](auto a, auto b, auto data) {}) - , _sendDataHandle([](auto data) {}) - , _closeHandle([]() {}) { + , _receiveDataHandle([](auto, auto, auto) {}) + , _closeHandle([]() {}) + , _sendDataHandle([](auto) {}) + , _lastHandleTime(std::chrono::steady_clock::now()) { } IOConnection::~IOConnection() = default; diff --git a/src/IdentityObject.cpp b/src/IdentityObject.cpp index e88596b..0e771c9 100644 --- a/src/IdentityObject.cpp +++ b/src/IdentityObject.cpp @@ -8,7 +8,7 @@ namespace eipScanner { using namespace cip; using utils::Buffer; - + IdentityObject::IdentityObject(cip::CipUint instanceId) : BaseObject(CLASS_ID, instanceId) , _vendorId(0) @@ -25,7 +25,14 @@ namespace eipScanner { } IdentityObject::IdentityObject(cip::CipUint instanceId, const SessionInfoIf::SPtr &si, const MessageRouter::SPtr &messageRouter) - : BaseObject(CLASS_ID, instanceId) { + : BaseObject(CLASS_ID, instanceId) + , _vendorId(0) + , _deviceType(0) + , _productCode(0) + , _revision(0,0) + , _status(0) + , _serialNumber(0) + , _productName("") { auto response = messageRouter->sendRequest( si, @@ -105,4 +112,4 @@ namespace eipScanner { void IdentityObject::setProductName(const std::string &productName) { _productName = CipShortString(productName); } -} \ No newline at end of file +} diff --git a/src/ParameterObject.cpp b/src/ParameterObject.cpp index b6aec85..141cecc 100644 --- a/src/ParameterObject.cpp +++ b/src/ParameterObject.cpp @@ -43,12 +43,15 @@ namespace eipScanner { ParameterObject::ParameterObject(cip::CipUint instanceId, bool fullAttributes, size_t typeSize) : BaseObject(CLASS_ID, instanceId) , _hasFullAttributes(fullAttributes) + , _isScalable(false) + , _isReadOnly(false) + , _parameter(0) , _value(typeSize) - , _maxValue(typeSize) + , _type(CipDataTypes::ANY) + , _name{""} , _minValue(typeSize) + , _maxValue(typeSize) , _defaultValue(typeSize) - , _isScalable(false) - , _isReadOnly(false) , _scalingMultiplier(1) , _scalingDivisor(1) , _scalingBase(1) @@ -60,10 +63,16 @@ namespace eipScanner { const SessionInfoIf::SPtr &si, const MessageRouter::SPtr& messageRouter) : BaseObject(CLASS_ID, instanceId) - , _name{""} - , _hasFullAttributes{fullAttributes} - , _isScalable{false} + , _hasFullAttributes(fullAttributes) + , _isScalable(false) , _isReadOnly(false) + , _parameter(0) + , _value(0) + , _type(CipDataTypes::ANY) + , _name{""} + , _minValue(0) + , _maxValue(0) + , _defaultValue(0) , _scalingMultiplier(1) , _scalingDivisor(1) , _scalingBase(1) @@ -135,7 +144,7 @@ namespace eipScanner { >> _precision; std::vector data; - for (int attrId = ParameterObjectAttributeIds::SCALING_MULTIPLIER; + for (CipUint attrId = ParameterObjectAttributeIds::SCALING_MULTIPLIER; attrId <= ParameterObjectAttributeIds::SCALING_OFFSET; ++attrId) { auto response = _messageRouter->sendRequest(si, @@ -302,4 +311,4 @@ namespace eipScanner { return engValue; } } -}; \ No newline at end of file +} diff --git a/src/ParameterObject.h b/src/ParameterObject.h index 26b92f3..c338f24 100644 --- a/src/ParameterObject.h +++ b/src/ParameterObject.h @@ -363,7 +363,7 @@ class ParameterObject : public BaseObject { bool _isScalable; bool _isReadOnly; - cip::CipUint _parameter; + cip::CipUint _parameter; std::vector _value; cip::CipDataTypes _type; std::string _name; diff --git a/src/cip/CipString.h b/src/cip/CipString.h index f32c564..0ed30d7 100644 --- a/src/cip/CipString.h +++ b/src/cip/CipString.h @@ -17,7 +17,7 @@ namespace cip { public: CipBaseString() = default; explicit CipBaseString(const std::string& string) { - _length = string.size(); + _length = static_cast(string.size()); uint8_t buffer[_length]; std::memcpy(buffer, string.data(), _length); @@ -25,7 +25,7 @@ namespace cip { } CipBaseString(const std::vector& data) { - _length = data.size(); + _length = static_cast(data.size()); _data = data; } diff --git a/src/cip/EPath.cpp b/src/cip/EPath.cpp index 7b23dd8..014f0a9 100644 --- a/src/cip/EPath.cpp +++ b/src/cip/EPath.cpp @@ -105,7 +105,7 @@ namespace cip { _attributeId = 0; _size = 0; - for (int i = 0; i < data.size() && !buffer.empty(); ++i) { + for (unsigned int i = 0; i < data.size() && !buffer.empty(); ++i) { EPathSegmentTypes segmentType; CipUsint ignore = 0; CipUsint byte; @@ -165,4 +165,4 @@ namespace cip { && _attributeId == other._attributeId; } } -} \ No newline at end of file +} diff --git a/src/cip/Types.h b/src/cip/Types.h index be38bb5..68b77ed 100644 --- a/src/cip/Types.h +++ b/src/cip/Types.h @@ -24,7 +24,7 @@ namespace cip { typedef double CipLreal; /**< 64-bit IEEE 754 floating point */ typedef uint64_t CipLword; /**< 64-bit bit unsigned integer */ typedef int64_t CipLint; /**< 64-bit bit signed integer */ - typedef uint64_t CipUlint; /**< 64-bit bit unsigned integer */ + typedef uint64_t CipUlint; /**< 64-bit bit unsigned integer */ enum class CipDataTypes : CipUsint { @@ -54,8 +54,7 @@ namespace cip { LTIME = 0xD7, /**< Duration in micro-seconds, high resolution, range of LINT */ ITIME = 0xD8, /**< Duration in milli-seconds, short; range of INT*/ STRINGN = 0xD9, /**< Character string, N byte per character */ - SHORT_STRING = 0xDA, /**< Character string, 1 byte per character, 1 byte - length indicator */ + SHORT_STRING = 0xDA, /**< Character string, 1 byte per character, 1 byte length indicator */ TIME = 0xDB, /**< Duration in milli-seconds; range of DINT */ EPATH = 0xDC, /**< CIP path segments*/ ENG_UNIT = 0xDD, /**< Engineering Units*/ diff --git a/src/cip/connectionManager/ForwardCloseRequest.cpp b/src/cip/connectionManager/ForwardCloseRequest.cpp index f1b033d..4134f80 100644 --- a/src/cip/connectionManager/ForwardCloseRequest.cpp +++ b/src/cip/connectionManager/ForwardCloseRequest.cpp @@ -11,8 +11,8 @@ namespace connectionManager { connectionManager::ForwardCloseRequest::ForwardCloseRequest() : _connectionSerialNumber{0} + , _originatorVendorID{0} , _originatorSerialNumber{0} - , _originatorVendorID{0} , _connectionPath(0) { } diff --git a/src/cip/connectionManager/NetworkConnectionParametersBuilder.cpp b/src/cip/connectionManager/NetworkConnectionParametersBuilder.cpp index bbfc28e..219a191 100644 --- a/src/cip/connectionManager/NetworkConnectionParametersBuilder.cpp +++ b/src/cip/connectionManager/NetworkConnectionParametersBuilder.cpp @@ -15,9 +15,9 @@ namespace connectionManager { using utils::Buffer; NetworkConnectionParametersBuilder::NetworkConnectionParametersBuilder(CipUdint val, bool lfo) : - _lfo{lfo}, _value{val} { + _value{val}, _lfo{lfo} { - }; + } NetworkConnectionParametersBuilder NetworkConnectionParametersBuilder::setRedundantOwner(RedundantOwner val) { if (_lfo) { diff --git a/src/eip/CommonPacketItem.cpp b/src/eip/CommonPacketItem.cpp index 7ea68d5..b12905f 100644 --- a/src/eip/CommonPacketItem.cpp +++ b/src/eip/CommonPacketItem.cpp @@ -18,7 +18,7 @@ namespace eip { CommonPacketItem::CommonPacketItem(CommonPacketItemIds typeId, const std::vector &data) : _typeId{typeId} - , _length(data.size()) + , _length((cip::CipUint)data.size()) , _data(data) { } @@ -57,4 +57,4 @@ namespace eip { return !(rhs == *this); } } -} \ No newline at end of file +} diff --git a/src/eip/EncapsPacket.cpp b/src/eip/EncapsPacket.cpp index 405b113..8a0170c 100644 --- a/src/eip/EncapsPacket.cpp +++ b/src/eip/EncapsPacket.cpp @@ -27,7 +27,7 @@ namespace eip { if (data.size() < HEADER_SIZE) { throw std::runtime_error("EncapsPacket header must be 24 bytes"); } - + Buffer buffer(data); buffer >> reinterpret_cast(_command) >> _length @@ -41,7 +41,7 @@ namespace eip { throw std::runtime_error("EncapsPacket data must be " + std::to_string(_length) + " but we have only " + std::to_string(dataSize) + " bytes"); } - + _data.resize(_length); buffer >> _data; } @@ -95,7 +95,7 @@ namespace eip { void EncapsPacket::setData(const std::vector &data) { _data = data; - _length = data.size(); + _length = (cip::CipUint)data.size(); } size_t EncapsPacket::getLengthFromHeader(const std::vector& data) { diff --git a/src/fileObject/FileObjectEmptyState.cpp b/src/fileObject/FileObjectEmptyState.cpp index 44663f6..a234485 100644 --- a/src/fileObject/FileObjectEmptyState.cpp +++ b/src/fileObject/FileObjectEmptyState.cpp @@ -13,13 +13,16 @@ namespace fileObject { } void FileObjectEmptyState::initiateUpload(SessionInfoIf::SPtr si, EndUploadHandler handle) { + (void) si; + (void) handle; logWithStateName(LogLevel::WARNING, "File cannot be uploaded"); } bool FileObjectEmptyState::transfer(SessionInfoIf::SPtr si) { + (void) si; logWithStateName(LogLevel::WARNING, "Nothing to transfer"); return false; } } -} \ No newline at end of file +} diff --git a/src/fileObject/FileObjectNonExistentState.cpp b/src/fileObject/FileObjectNonExistentState.cpp index b49245d..6215269 100644 --- a/src/fileObject/FileObjectNonExistentState.cpp +++ b/src/fileObject/FileObjectNonExistentState.cpp @@ -14,15 +14,18 @@ namespace fileObject { } void FileObjectNonExistentState::initiateUpload(SessionInfoIf::SPtr si, EndUploadHandler handle) { + (void) si; + (void) handle; logWithStateName(LogLevel::WARNING, "File cannot be uploaded"); } bool FileObjectNonExistentState::transfer(SessionInfoIf::SPtr si) { + (void) si; logWithStateName(LogLevel::WARNING, "Nothing to transfer"); return false; } } -} \ No newline at end of file +} diff --git a/src/fileObject/FileObjectState.cpp b/src/fileObject/FileObjectState.cpp index 09107fb..84d4bc2 100644 --- a/src/fileObject/FileObjectState.cpp +++ b/src/fileObject/FileObjectState.cpp @@ -18,10 +18,10 @@ namespace fileObject { FileObjectState::FileObjectState(FileObjectStateCodes state, FileObject &owner, cip::CipUint objectId, MessageRouter::SPtr messageRouter) - : _stateCode{state} - , _owner(owner) + : _messageRouter{messageRouter} , _objectId{objectId} - , _messageRouter{messageRouter} { + , _stateCode{state} + , _owner(owner) { logWithStateName(utils::LogLevel::DEBUG, "Start"); } @@ -90,10 +90,13 @@ namespace fileObject { } void FileObjectState::initiateUpload(SessionInfoIf::SPtr si, EndUploadHandler handle) { + (void) si; + (void) handle; logWithStateName(LogLevel::ERROR, "Not implemented call"); } bool FileObjectState::transfer(SessionInfoIf::SPtr si) { + (void) si; logWithStateName(LogLevel::ERROR, "Not implemented call"); return false; } diff --git a/src/fileObject/FileObjectUploadInProgressState.cpp b/src/fileObject/FileObjectUploadInProgressState.cpp index ecb6fe4..0054a26 100644 --- a/src/fileObject/FileObjectUploadInProgressState.cpp +++ b/src/fileObject/FileObjectUploadInProgressState.cpp @@ -25,6 +25,8 @@ namespace fileObject { void FileObjectUploadInProgressState::initiateUpload(SessionInfoIf::SPtr si, EndUploadHandler handle) { + (void) si; + (void) handle; logWithStateName(LogLevel::INFO, "Initiate upload of file again"); //TODO: Implement } @@ -97,4 +99,4 @@ namespace fileObject { return true; } } -} \ No newline at end of file +} diff --git a/src/sockets/BaseSocket.cpp b/src/sockets/BaseSocket.cpp index 3bc091f..a674dec 100644 --- a/src/sockets/BaseSocket.cpp +++ b/src/sockets/BaseSocket.cpp @@ -2,10 +2,10 @@ // Created by Aleksey Timin on 11/18/19. // -#if defined(__linux__) || defined(__APPLE__) +#if defined (__unix__) #include #include -#elif defined _WIN32 +#elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) #include #include #endif @@ -14,12 +14,14 @@ #include #include #include "BaseSocket.h" +#include "Platform.h" namespace eipScanner { namespace sockets { BaseSocket::BaseSocket(EndPoint endPoint) - : _remoteEndPoint(std::move(endPoint)) + : _sockedFd(0) + , _remoteEndPoint(std::move(endPoint)) , _recvTimeout(0) , _beginReceiveHandler() { @@ -42,8 +44,8 @@ namespace sockets { void BaseSocket::setRecvTimeout(const std::chrono::milliseconds &recvTimeout) { _recvTimeout = recvTimeout; -#ifdef _WIN32 - uint32_t ms = recvTimeout.count(); +#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) + uint32_t ms = (uint32_t)recvTimeout.count(); setsockopt(_sockedFd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&ms, sizeof ms); #else timeval tv = makePortableInterval(recvTimeout); @@ -62,8 +64,7 @@ namespace sockets { .tv_sec = static_cast<__time_t>(recvTimeout.count()/1000), .tv_usec = static_cast<__time_t>((recvTimeout.count()%1000)*1000) -#elif _WIN32 - // not sure what the macro is for windows +#elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) .tv_sec = static_cast(recvTimeout.count()/1000), .tv_usec = static_cast((recvTimeout.count()%1000)*1000) #endif @@ -102,7 +103,7 @@ namespace sockets { ready = ::select(socketWithMaxFd->getSocketFd() + 1, &recvSet, NULL, NULL, &tv); if (ready < 0) { - throw std::system_error(errno, std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), std::generic_category()); } for (auto& sock : sockets) { diff --git a/src/sockets/EndPoint.cpp b/src/sockets/EndPoint.cpp index d7dd0ef..1a82142 100644 --- a/src/sockets/EndPoint.cpp +++ b/src/sockets/EndPoint.cpp @@ -3,10 +3,11 @@ // #include "EndPoint.h" +#include "Platform.h" -#if defined(__linux__) || defined(__APPLE__) +#if defined(__unix__) #include -#elif defined _WIN32 +#elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) #include #include #endif @@ -26,16 +27,16 @@ namespace sockets { EndPoint::EndPoint(std::string host, int port) : _host(std::move(host)) , _port(port) - , _addr{0} { + , _addr{} { _addr.sin_family = AF_INET; _addr.sin_port = htons(_port); -#if defined(__linux__) || defined(__APPLE__) +#if defined(__unix__) if (inet_aton(_host.c_str(), &_addr.sin_addr) < 0) { -#elif defined _WIN32 - if ((_addr.sin_addr.s_addr = inet_addr(_host.c_str())) == INADDR_NONE) { +#elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) + if (inet_pton(AF_INET, _host.c_str(), &_addr.sin_addr.s_addr) < 0) { #endif - throw std::system_error(errno, std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), std::generic_category()); } } diff --git a/src/sockets/EndPoint.h b/src/sockets/EndPoint.h index bd42b11..c589127 100644 --- a/src/sockets/EndPoint.h +++ b/src/sockets/EndPoint.h @@ -5,9 +5,9 @@ #ifndef EIPSCANNER_SOCKETS_ENDPOINT_H #define EIPSCANNER_SOCKETS_ENDPOINT_H -#if defined(__linux__) || defined(__APPLE__) +#if defined(__unix__) #include -#elif defined _WIN32 +#elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) #include #endif #include diff --git a/src/sockets/Platform.h b/src/sockets/Platform.h new file mode 100644 index 0000000..19dc9a3 --- /dev/null +++ b/src/sockets/Platform.h @@ -0,0 +1,30 @@ +// +// Created by Stefan Broekman on 02/04/21. +// +// Only include in local (source) files related to (Win)Sockets, to prevent unintentional overrides in +// unwanted places. I.e. errno is not supported on Windows for WinSock only, it is for e.g. file I/O. +// + +#ifndef EIPSCANNER_SOCKETS_PLATFORM_H +#define EIPSCANNER_SOCKETS_PLATFORM_H + +#include + +#if defined (__unix__) +#define SOCKET_ERRNO() (errno) +#define SOCKET_SHUTDOWN_OPERATION (SHUT_RDWR) +#elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) +#define SOCKET_ERRNO() (WSAGetLastError()) +#define SOCKET_SHUTDOWN_OPERATION (SD_BOTH) + +//Used socket error codes - redefine to the respective WSA error code. +#undef EINTR +#define EINTR (WSAEINTR) +#undef EINPROGRESS +#define EINPROGRESS (WSAEINPROGRESS) +#undef ETIMEDOUT +#define ETIMEDOUT (WSAETIMEDOUT) + +#endif + +#endif // EIPSCANNER_SOCKETS_PLATFORM_H diff --git a/src/sockets/TCPSocket.cpp b/src/sockets/TCPSocket.cpp index 3c28a39..53f82a8 100644 --- a/src/sockets/TCPSocket.cpp +++ b/src/sockets/TCPSocket.cpp @@ -4,11 +4,11 @@ #include -#ifdef __linux__ +#if defined(__unix__) #include #include #include -#elif defined _WIN32 +#elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) #include #include #include @@ -19,11 +19,7 @@ #include "utils/Logger.h" #include "TCPSocket.h" - -#if !(defined __linux__) && !(defined SHUT_RDWR) -#define SHUT_RDWR SD_BOTH -#endif - +#include "Platform.h" namespace eipScanner { namespace sockets { @@ -38,29 +34,29 @@ namespace eipScanner { : BaseSocket(std::move(endPoint)) { _sockedFd = socket(AF_INET, SOCK_STREAM, 0); if (_sockedFd < 0) { - throw std::system_error(errno, std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), std::generic_category()); } // Set non-blocking -#ifdef __linux__ +#if defined(__unix__) auto arg = fcntl(_sockedFd, F_GETFL, NULL); if (arg < 0) { - throw std::system_error(errno, std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), std::generic_category()); } arg |= O_NONBLOCK; if (fcntl(_sockedFd, F_SETFL, arg) < 0) { - throw std::system_error(errno, std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), std::generic_category()); } #endif - Logger(LogLevel::DEBUG) << "Opened socket fd=" << _sockedFd; + Logger(LogLevel::DEBUG) << "Opened TCP socket fd=" << _sockedFd; Logger(LogLevel::DEBUG) << "Connecting to " << _remoteEndPoint.toString(); auto addr = _remoteEndPoint.getAddr(); auto res = connect(_sockedFd, (struct sockaddr *) &addr, sizeof(addr)); if (res < 0) { - if (errno == EINPROGRESS) { + if (SOCKET_ERRNO() == EINPROGRESS) { do { fd_set myset; auto tv = makePortableInterval(connTimeout); @@ -69,14 +65,14 @@ namespace eipScanner { FD_SET(_sockedFd, &myset); res = ::select(_sockedFd + 1, NULL, &myset, NULL, &tv); - if (res < 0 && errno != EINTR) { - throw std::system_error(errno, std::generic_category()); + if (res < 0 && SOCKET_ERRNO() != EINTR) { + throw std::system_error(SOCKET_ERRNO(), std::generic_category()); } else if (res > 0) { // Socket selected for write int err; socklen_t lon = sizeof(int); if (getsockopt(_sockedFd, SOL_SOCKET, SO_ERROR, (char *) (&err), &lon) < 0) { - throw std::system_error(errno, std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), std::generic_category()); } // Check the value returned... if (err) { @@ -88,18 +84,18 @@ namespace eipScanner { } } while (1); } else { - throw std::system_error(errno, std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), std::generic_category()); } } -#ifdef __linux__ +#if defined(__unix__) // Set to blocking mode again... if ((arg = fcntl(_sockedFd, F_GETFL, NULL)) < 0) { - throw std::system_error(errno, std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), std::generic_category()); } arg &= (~O_NONBLOCK); if (fcntl(_sockedFd, F_SETFL, arg) < 0) { - throw std::system_error(errno, std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), std::generic_category()); } #endif } @@ -111,8 +107,8 @@ namespace eipScanner { } TCPSocket::~TCPSocket() { - Logger(LogLevel::DEBUG) << "Close socket fd=" << _sockedFd; - shutdown(_sockedFd, SHUT_RDWR); + Logger(LogLevel::DEBUG) << "Close TCP socket fd=" << _sockedFd; + shutdown(_sockedFd, SOCKET_SHUTDOWN_OPERATION); close(_sockedFd); } @@ -121,7 +117,7 @@ namespace eipScanner { int count = send(_sockedFd, (char*)data.data(), data.size(), 0); if (count < data.size()) { - throw std::system_error(errno, std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), std::generic_category()); } } @@ -131,9 +127,10 @@ namespace eipScanner { int count = 0; while (size > count) { auto len = recv(_sockedFd, (char*)(recvBuffer.data() + count), size - count, 0); + Logger(LogLevel::TRACE) << "Received " << len << " bytes from TCP socket #" << _sockedFd << "."; count += len; if (len < 0) { - throw std::system_error(errno, std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), std::generic_category()); } if (len == 0) { diff --git a/src/sockets/UDPBoundSocket.cpp b/src/sockets/UDPBoundSocket.cpp index c8ae80c..f2b3070 100644 --- a/src/sockets/UDPBoundSocket.cpp +++ b/src/sockets/UDPBoundSocket.cpp @@ -7,6 +7,7 @@ //#include #include "UDPBoundSocket.h" +#include "Platform.h" namespace eipScanner { namespace sockets { @@ -20,13 +21,13 @@ namespace sockets { : UDPSocket(std::move(endPoint)) { int on = 1; if (setsockopt(_sockedFd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) < 0) { - throw std::system_error(errno, std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), std::generic_category()); } auto addr = _remoteEndPoint.getAddr(); addr.sin_addr.s_addr = INADDR_ANY; if (bind(_sockedFd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { - throw std::system_error(errno, std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), std::generic_category()); } } diff --git a/src/sockets/UDPSocket.cpp b/src/sockets/UDPSocket.cpp index 8ff16e1..3aa7b5a 100644 --- a/src/sockets/UDPSocket.cpp +++ b/src/sockets/UDPSocket.cpp @@ -4,33 +4,26 @@ #include -#ifdef __linux__ +#if defined(__unix__) #include #include #include -#elif defined _WIN32 +#elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) #include #include #endif -#if !(defined __linux__) && !(defined SHUT_RDWR) -#define SHUT_RDWR SD_BOTH -#endif - - #include #include "utils/Logger.h" #include "UDPSocket.h" +#include "Platform.h" namespace eipScanner { namespace sockets { using eipScanner::utils::Logger; using eipScanner::utils::LogLevel; - - - UDPSocket::UDPSocket(std::string host, int port) : UDPSocket(EndPoint(host, port)){ @@ -41,15 +34,15 @@ namespace sockets { _sockedFd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (_sockedFd < 0) { - throw std::system_error(errno, std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), std::generic_category()); } - Logger(LogLevel::DEBUG) << "Opened socket fd=" << _sockedFd; + Logger(LogLevel::DEBUG) << "Opened UDP socket fd=" << _sockedFd; } UDPSocket::~UDPSocket() { - Logger(LogLevel::DEBUG) << "Close socket fd=" << _sockedFd; - shutdown(_sockedFd, SHUT_RDWR); + Logger(LogLevel::DEBUG) << "Close UDP socket fd=" << _sockedFd; + shutdown(_sockedFd, SOCKET_SHUTDOWN_OPERATION); close(_sockedFd); } @@ -60,7 +53,7 @@ namespace sockets { int count = sendto(_sockedFd, (char*)data.data(), data.size(), 0, (struct sockaddr *)&addr, sizeof(addr)); if (count < data.size()) { - throw std::system_error(errno, std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), std::generic_category()); } } @@ -68,8 +61,9 @@ namespace sockets { std::vector recvBuffer(size); auto len = recvfrom(_sockedFd, (char*)recvBuffer.data(), recvBuffer.size(), 0, NULL, NULL); + Logger(LogLevel::TRACE) << "Received " << len << " bytes from UDP socket #" << _sockedFd << "."; if (len < 0) { - throw std::system_error(errno, std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), std::generic_category()); } return recvBuffer; @@ -80,8 +74,9 @@ namespace sockets { struct sockaddr_in addr; socklen_t addrFromLength = sizeof(addr); auto len = recvfrom(_sockedFd, (char*)recvBuffer.data(), recvBuffer.size(), 0, (struct sockaddr*)&addr, &addrFromLength); + Logger(LogLevel::TRACE) << "Received " << len << " bytes from UDP socket #" << _sockedFd << "."; if (len < 0) { - throw std::system_error(errno, std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), std::generic_category()); } endPoint = EndPoint(addr); diff --git a/src/utils/Buffer.cpp b/src/utils/Buffer.cpp index 2e32a49..0e3ed2a 100644 --- a/src/utils/Buffer.cpp +++ b/src/utils/Buffer.cpp @@ -188,7 +188,7 @@ Buffer &Buffer::operator<<(float val) { Buffer &Buffer::operator>>(sockets::EndPoint &val) { std::vector zeros(8); - sockaddr_in addr{0}; + sockaddr_in addr{}; *this >> reinterpret_cast(addr.sin_family) >> addr.sin_port >> (uint32_t&)addr.sin_addr.s_addr diff --git a/src/utils/Logger.h b/src/utils/Logger.h index d90f9e5..95d1223 100644 --- a/src/utils/Logger.h +++ b/src/utils/Logger.h @@ -8,7 +8,7 @@ #include #include -#ifdef _WIN32 +#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) #undef ERROR #endif diff --git a/src/vendor/ra/powerFlex525/DPIFaultObject.cpp b/src/vendor/ra/powerFlex525/DPIFaultObject.cpp index 430819b..e3f99cb 100644 --- a/src/vendor/ra/powerFlex525/DPIFaultObject.cpp +++ b/src/vendor/ra/powerFlex525/DPIFaultObject.cpp @@ -36,7 +36,7 @@ namespace powerFlex525 { DPIFaultObject::DPIFaultObject(CipUint instanceId, const SessionInfoIf::SPtr &si, const MessageRouter::SPtr& messageRouter) : BaseObject(CLASS_ID, instanceId) - , _fullInformation{0} { + , _fullInformation{} { auto response = messageRouter->sendRequest(si, ServiceCodes::GET_ATTRIBUTE_SINGLE, EPath(CLASS_ID, instanceId, DPIFaultObjectAttributeIds::FULL_INFORMATION)); diff --git a/src/vendor/ra/powerFlex525/DPIFaultParameter.cpp b/src/vendor/ra/powerFlex525/DPIFaultParameter.cpp index 90f164e..c0cfc0a 100644 --- a/src/vendor/ra/powerFlex525/DPIFaultParameter.cpp +++ b/src/vendor/ra/powerFlex525/DPIFaultParameter.cpp @@ -113,7 +113,7 @@ namespace powerFlex525 { } - static cip::CipLreal processVolts(uint16_t volts, int voltsParam) { + static cip::CipLreal processVolts(uint16_t volts, cip::CipUint voltsParam) { auto * parameterObject = new ParameterObject(voltsParam, true, 4); parameterObject->setScalable(true); @@ -126,7 +126,7 @@ namespace powerFlex525 { return parameterObject->actualToEngValue(volts); } - static cip::CipLreal processCurrent(uint16_t current, int currentParam) { + static cip::CipLreal processCurrent(uint16_t current, cip::CipUint currentParam) { auto * parameterObject = new ParameterObject(currentParam, true, 4); parameterObject->setScalable(true); @@ -139,7 +139,7 @@ namespace powerFlex525 { return parameterObject->actualToEngValue(current); } - static cip::CipLreal processFrequency(uint16_t frequency, int frequencyParam) { + static cip::CipLreal processFrequency(uint16_t frequency, cip::CipUint frequencyParam) { auto * parameterObject = new ParameterObject(frequencyParam, true, 4); parameterObject->setScalable(true); @@ -158,7 +158,7 @@ namespace powerFlex525 { */ static uint16_t getFaultDetail(const SessionInfoIf::SPtr &si, const MessageRouter::SPtr& messageRouter, - int parameterNumber){ + cip::CipUint parameterNumber){ auto response = messageRouter->sendRequest(si, ServiceCodes::GET_ATTRIBUTE_SINGLE, EPath(0x0F, parameterNumber, 1)); @@ -182,12 +182,12 @@ namespace powerFlex525 { */ DPIFaultParameter::DPIFaultParameter(const SessionInfoIf::SPtr &si, const MessageRouter::SPtr& messageRouter, int faultNumber, bool getFaultDetails) - : _fullInformation{0} { + : _fullInformation{} { - int faultCodeParam = 0; - int faultVoltsParam = 0; - int faultCurrentParam = 0; - int faultFrequencyParam = 0; + cip::CipUint faultCodeParam = 0; + cip::CipUint faultVoltsParam = 0; + cip::CipUint faultCurrentParam = 0; + cip::CipUint faultFrequencyParam = 0; switch(faultNumber){ case 1: @@ -304,4 +304,4 @@ namespace powerFlex525 { } } } -} \ No newline at end of file +} diff --git a/src/vendor/yaskawa/mp3300iec/Yaskawa_EPath.cpp b/src/vendor/yaskawa/mp3300iec/Yaskawa_EPath.cpp index db26946..e237eb9 100644 --- a/src/vendor/yaskawa/mp3300iec/Yaskawa_EPath.cpp +++ b/src/vendor/yaskawa/mp3300iec/Yaskawa_EPath.cpp @@ -102,7 +102,7 @@ namespace cip { _attributeId = 0; _size = 0; - for (int i = 0; i < data.size() && !buffer.empty(); ++i) { + for (unsigned int i = 0; i < data.size() && !buffer.empty(); ++i) { EPathSegmentTypes segmentType; CipUsint ignore = 0; CipUsint byte; From fd4720add6e769667b8bfb398b952bffccdfb1ff Mon Sep 17 00:00:00 2001 From: Stefan Broekman Date: Sat, 3 Apr 2021 00:34:17 +0200 Subject: [PATCH 02/11] Revert type changes --- src/ConnectionManager.cpp | 2 +- src/DiscoveryManager.cpp | 2 +- src/DiscoveryManager.h | 2 +- src/ParameterObject.cpp | 2 +- src/cip/CipString.h | 4 ++-- src/cip/EPath.cpp | 2 +- src/eip/CommonPacketItem.cpp | 2 +- src/eip/EncapsPacket.cpp | 2 +- src/sockets/BaseSocket.cpp | 2 +- src/vendor/ra/powerFlex525/DPIFaultParameter.cpp | 16 ++++++++-------- src/vendor/yaskawa/mp3300iec/Yaskawa_EPath.cpp | 2 +- 11 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/ConnectionManager.cpp b/src/ConnectionManager.cpp index 4a8e11a..2d860c8 100644 --- a/src/ConnectionManager.cpp +++ b/src/ConnectionManager.cpp @@ -51,7 +51,7 @@ namespace eipScanner { IOConnection::WPtr ConnectionManager::forwardOpen(const SessionInfoIf::SPtr& si, ConnectionParameters connectionParameters, bool isLarge) { - static cip::CipUint serialNumberCount = 0; + static int serialNumberCount = 0; connectionParameters.connectionSerialNumber = ++serialNumberCount; NetworkConnectionParametersBuilder o2tNCP(connectionParameters.o2tNetworkConnectionParams, isLarge); diff --git a/src/DiscoveryManager.cpp b/src/DiscoveryManager.cpp index c0cf26a..e9abc88 100644 --- a/src/DiscoveryManager.cpp +++ b/src/DiscoveryManager.cpp @@ -55,7 +55,7 @@ namespace eipScanner { CommonPacket commonPacket; commonPacket.expand(std::vector(data.begin()+EncapsPacket::HEADER_SIZE, data.end())); - for (CipUint i=0; i < commonPacket.getItems().size(); ++i) { + for (int i=0; i < commonPacket.getItems().size(); ++i) { Buffer buffer(commonPacket.getItems()[i].getData()); CipUint ignore; sockets::EndPoint socketAddr("", 0); diff --git a/src/DiscoveryManager.h b/src/DiscoveryManager.h index 33d185e..2cba8cd 100644 --- a/src/DiscoveryManager.h +++ b/src/DiscoveryManager.h @@ -37,7 +37,7 @@ namespace eipScanner { /** * @brief default destructor */ - virtual ~DiscoveryManager(); + ~DiscoveryManager(); /** * @brief Discovers the EIP network diff --git a/src/ParameterObject.cpp b/src/ParameterObject.cpp index 141cecc..adc702b 100644 --- a/src/ParameterObject.cpp +++ b/src/ParameterObject.cpp @@ -144,7 +144,7 @@ namespace eipScanner { >> _precision; std::vector data; - for (CipUint attrId = ParameterObjectAttributeIds::SCALING_MULTIPLIER; + for (int attrId = ParameterObjectAttributeIds::SCALING_MULTIPLIER; attrId <= ParameterObjectAttributeIds::SCALING_OFFSET; ++attrId) { auto response = _messageRouter->sendRequest(si, diff --git a/src/cip/CipString.h b/src/cip/CipString.h index 0ed30d7..f32c564 100644 --- a/src/cip/CipString.h +++ b/src/cip/CipString.h @@ -17,7 +17,7 @@ namespace cip { public: CipBaseString() = default; explicit CipBaseString(const std::string& string) { - _length = static_cast(string.size()); + _length = string.size(); uint8_t buffer[_length]; std::memcpy(buffer, string.data(), _length); @@ -25,7 +25,7 @@ namespace cip { } CipBaseString(const std::vector& data) { - _length = static_cast(data.size()); + _length = data.size(); _data = data; } diff --git a/src/cip/EPath.cpp b/src/cip/EPath.cpp index 014f0a9..626e288 100644 --- a/src/cip/EPath.cpp +++ b/src/cip/EPath.cpp @@ -105,7 +105,7 @@ namespace cip { _attributeId = 0; _size = 0; - for (unsigned int i = 0; i < data.size() && !buffer.empty(); ++i) { + for (int i = 0; i < data.size() && !buffer.empty(); ++i) { EPathSegmentTypes segmentType; CipUsint ignore = 0; CipUsint byte; diff --git a/src/eip/CommonPacketItem.cpp b/src/eip/CommonPacketItem.cpp index b12905f..b716caf 100644 --- a/src/eip/CommonPacketItem.cpp +++ b/src/eip/CommonPacketItem.cpp @@ -18,7 +18,7 @@ namespace eip { CommonPacketItem::CommonPacketItem(CommonPacketItemIds typeId, const std::vector &data) : _typeId{typeId} - , _length((cip::CipUint)data.size()) + , _length(data.size()) , _data(data) { } diff --git a/src/eip/EncapsPacket.cpp b/src/eip/EncapsPacket.cpp index 8a0170c..64d2ed3 100644 --- a/src/eip/EncapsPacket.cpp +++ b/src/eip/EncapsPacket.cpp @@ -95,7 +95,7 @@ namespace eip { void EncapsPacket::setData(const std::vector &data) { _data = data; - _length = (cip::CipUint)data.size(); + _length = data.size(); } size_t EncapsPacket::getLengthFromHeader(const std::vector& data) { diff --git a/src/sockets/BaseSocket.cpp b/src/sockets/BaseSocket.cpp index a674dec..5a1c1a4 100644 --- a/src/sockets/BaseSocket.cpp +++ b/src/sockets/BaseSocket.cpp @@ -45,7 +45,7 @@ namespace sockets { _recvTimeout = recvTimeout; #if defined(_WIN32) || defined(WIN32) || defined(_WIN64) - uint32_t ms = (uint32_t)recvTimeout.count(); + uint32_t ms = recvTimeout.count(); setsockopt(_sockedFd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&ms, sizeof ms); #else timeval tv = makePortableInterval(recvTimeout); diff --git a/src/vendor/ra/powerFlex525/DPIFaultParameter.cpp b/src/vendor/ra/powerFlex525/DPIFaultParameter.cpp index c0cfc0a..ded467c 100644 --- a/src/vendor/ra/powerFlex525/DPIFaultParameter.cpp +++ b/src/vendor/ra/powerFlex525/DPIFaultParameter.cpp @@ -113,7 +113,7 @@ namespace powerFlex525 { } - static cip::CipLreal processVolts(uint16_t volts, cip::CipUint voltsParam) { + static cip::CipLreal processVolts(uint16_t volts, int voltsParam) { auto * parameterObject = new ParameterObject(voltsParam, true, 4); parameterObject->setScalable(true); @@ -126,7 +126,7 @@ namespace powerFlex525 { return parameterObject->actualToEngValue(volts); } - static cip::CipLreal processCurrent(uint16_t current, cip::CipUint currentParam) { + static cip::CipLreal processCurrent(uint16_t current, int currentParam) { auto * parameterObject = new ParameterObject(currentParam, true, 4); parameterObject->setScalable(true); @@ -139,7 +139,7 @@ namespace powerFlex525 { return parameterObject->actualToEngValue(current); } - static cip::CipLreal processFrequency(uint16_t frequency, cip::CipUint frequencyParam) { + static cip::CipLreal processFrequency(uint16_t frequency, int frequencyParam) { auto * parameterObject = new ParameterObject(frequencyParam, true, 4); parameterObject->setScalable(true); @@ -158,7 +158,7 @@ namespace powerFlex525 { */ static uint16_t getFaultDetail(const SessionInfoIf::SPtr &si, const MessageRouter::SPtr& messageRouter, - cip::CipUint parameterNumber){ + int parameterNumber){ auto response = messageRouter->sendRequest(si, ServiceCodes::GET_ATTRIBUTE_SINGLE, EPath(0x0F, parameterNumber, 1)); @@ -184,10 +184,10 @@ namespace powerFlex525 { const MessageRouter::SPtr& messageRouter, int faultNumber, bool getFaultDetails) : _fullInformation{} { - cip::CipUint faultCodeParam = 0; - cip::CipUint faultVoltsParam = 0; - cip::CipUint faultCurrentParam = 0; - cip::CipUint faultFrequencyParam = 0; + int faultCodeParam = 0; + int faultVoltsParam = 0; + int faultCurrentParam = 0; + int faultFrequencyParam = 0; switch(faultNumber){ case 1: diff --git a/src/vendor/yaskawa/mp3300iec/Yaskawa_EPath.cpp b/src/vendor/yaskawa/mp3300iec/Yaskawa_EPath.cpp index e237eb9..db26946 100644 --- a/src/vendor/yaskawa/mp3300iec/Yaskawa_EPath.cpp +++ b/src/vendor/yaskawa/mp3300iec/Yaskawa_EPath.cpp @@ -102,7 +102,7 @@ namespace cip { _attributeId = 0; _size = 0; - for (unsigned int i = 0; i < data.size() && !buffer.empty(); ++i) { + for (int i = 0; i < data.size() && !buffer.empty(); ++i) { EPathSegmentTypes segmentType; CipUsint ignore = 0; CipUsint byte; From bfb6199c00d2de3f9493f28e4b5aeac591dbc758 Mon Sep 17 00:00:00 2001 From: Stefan Broekman Date: Sat, 3 Apr 2021 15:42:11 +0200 Subject: [PATCH 03/11] Add more common services and default port numbers --- src/ConnectionManager.cpp | 4 ++-- src/MessageRouter.cpp | 2 +- src/cip/MessageRouterResponse.cpp | 2 +- src/cip/Services.h | 19 +++++++++++++++++++ src/sockets/EndPoint.h | 3 +++ 5 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/ConnectionManager.cpp b/src/ConnectionManager.cpp index 2d860c8..96a1cf5 100644 --- a/src/ConnectionManager.cpp +++ b/src/ConnectionManager.cpp @@ -145,13 +145,13 @@ namespace eipScanner { } } else { - ioConnection->_socket = std::make_unique(si->getRemoteEndPoint().getHost(), 2222); + ioConnection->_socket = std::make_unique(si->getRemoteEndPoint().getHost(), sockets::EndPoint::defaultImplicitPort); } Logger(LogLevel::INFO) << "Open UDP socket to send data to " << ioConnection->_socket->getRemoteEndPoint().toString(); - findOrCreateSocket(sockets::EndPoint(si->getRemoteEndPoint().getHost(), 2222)); + findOrCreateSocket(sockets::EndPoint(si->getRemoteEndPoint().getHost(), sockets::EndPoint::defaultImplicitPort)); auto result = _connectionMap .insert(std::make_pair(response.getT2ONetworkConnectionId(), ioConnection)); diff --git a/src/MessageRouter.cpp b/src/MessageRouter.cpp index 657ee83..8603343 100644 --- a/src/MessageRouter.cpp +++ b/src/MessageRouter.cpp @@ -82,4 +82,4 @@ namespace eipScanner { MessageRouter::sendRequest(SessionInfoIf::SPtr si, CipUsint service, const EPath &path) const { return this->sendRequest(si, service, path, {}, {}); } -} \ No newline at end of file +} diff --git a/src/cip/MessageRouterResponse.cpp b/src/cip/MessageRouterResponse.cpp index cb0738e..5b56cc8 100644 --- a/src/cip/MessageRouterResponse.cpp +++ b/src/cip/MessageRouterResponse.cpp @@ -90,4 +90,4 @@ namespace cip { } } } -} \ No newline at end of file +} diff --git a/src/cip/Services.h b/src/cip/Services.h index eaa898c..a2c91ca 100644 --- a/src/cip/Services.h +++ b/src/cip/Services.h @@ -15,8 +15,27 @@ namespace cip { /* Start CIP common services */ GET_ATTRIBUTE_ALL = 0X01, SET_ATTRIBUTE_ALL = 0X02, + GET_ATTRIBUTE_LIST = 0x03, + SET_ATTRIBUTE_LIST = 0x04, + RESET = 0x05, + START = 0x06, + STOP = 0x07, + CREATE_OBJECT_INSTANCE = 0x08, + DELETE_OBJECT_INSTANCE = 0x09, + MULTIPLE_SERVICE_PACKET = 0x0A, + APPLY_ATTRIBUTES = 0x0D, GET_ATTRIBUTE_SINGLE = 0X0E, SET_ATTRIBUTE_SINGLE = 0X10, + FIND_NEXT_OBJECT_INSTANCE = 0x11, + ERROR_RESPONSE = 0x14, //DeviceNet only + RESTORE = 0x15, + SAVE = 0x16, + GET_MEMBER = 0x18, + NO_OPERATION = 0x17, + SET_MEMBER = 0x19, + INSERT_MEMBER = 0x1A, + REMOVE_MEMBER = 0x1B, + GROUP_SYNC = 0x1C /* End CIP common services */ }; diff --git a/src/sockets/EndPoint.h b/src/sockets/EndPoint.h index c589127..bf3e417 100644 --- a/src/sockets/EndPoint.h +++ b/src/sockets/EndPoint.h @@ -17,6 +17,9 @@ namespace sockets { class EndPoint { public: + static constexpr auto defaultExplicitPort = 44818; + static constexpr auto defaultImplicitPort = 2222; + EndPoint(std::string host, int port); EndPoint(struct sockaddr_in& addr); From 61ebf4ba50756c52aee40f1fdaa7363ac49e9e63 Mon Sep 17 00:00:00 2001 From: Stefan Broekman Date: Tue, 6 Apr 2021 15:19:23 +0200 Subject: [PATCH 04/11] Add win32 error category message format --- src/DiscoveryManager.cpp | 2 +- src/sockets/BaseSocket.cpp | 2 +- src/sockets/EndPoint.cpp | 2 +- src/sockets/Platform.cpp | 33 +++++++++++++++++++++++++++++++++ src/sockets/Platform.h | 19 ++++++++++++++++++- src/sockets/TCPSocket.cpp | 29 +++++++++++++++-------------- src/sockets/UDPBoundSocket.cpp | 4 ++-- src/sockets/UDPSocket.cpp | 13 ++++++------- 8 files changed, 77 insertions(+), 27 deletions(-) create mode 100644 src/sockets/Platform.cpp diff --git a/src/DiscoveryManager.cpp b/src/DiscoveryManager.cpp index e9abc88..86c9997 100644 --- a/src/DiscoveryManager.cpp +++ b/src/DiscoveryManager.cpp @@ -99,7 +99,7 @@ namespace eipScanner { int broadcast = 1; if(setsockopt(socket->getSocketFd(), SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, sizeof(broadcast)) < 0) { - throw std::system_error(SOCKET_ERRNO(), std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); } return socket; diff --git a/src/sockets/BaseSocket.cpp b/src/sockets/BaseSocket.cpp index 5a1c1a4..6025f8e 100644 --- a/src/sockets/BaseSocket.cpp +++ b/src/sockets/BaseSocket.cpp @@ -103,7 +103,7 @@ namespace sockets { ready = ::select(socketWithMaxFd->getSocketFd() + 1, &recvSet, NULL, NULL, &tv); if (ready < 0) { - throw std::system_error(SOCKET_ERRNO(), std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); } for (auto& sock : sockets) { diff --git a/src/sockets/EndPoint.cpp b/src/sockets/EndPoint.cpp index 1a82142..18fe224 100644 --- a/src/sockets/EndPoint.cpp +++ b/src/sockets/EndPoint.cpp @@ -36,7 +36,7 @@ namespace sockets { #elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) if (inet_pton(AF_INET, _host.c_str(), &_addr.sin_addr.s_addr) < 0) { #endif - throw std::system_error(SOCKET_ERRNO(), std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); } } diff --git a/src/sockets/Platform.cpp b/src/sockets/Platform.cpp new file mode 100644 index 0000000..e5a9602 --- /dev/null +++ b/src/sockets/Platform.cpp @@ -0,0 +1,33 @@ +// +// Created by Stefan Broekman on 02/04/21. +// + +#include "Platform.h" + +#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) + +namespace detail +{ + char const* win32_error_category::name() const noexcept { + return "Win32Error"; + } + + std::string win32_error_category::message(int c) const { + char error[UINT8_MAX]; + auto len = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, static_cast(c), 0, error, sizeof(error), nullptr); + if (len == 0) { + return "N/A"; + } + // trim trailing newline + while (len && (error[len - 1] == '\r' || error[len - 1] == '\n')) { + --len; + } + return std::string(error, len); + } +} + +detail::win32_error_category const& win32_error_category() { + static detail::win32_error_category c; + return c; +} +#endif diff --git a/src/sockets/Platform.h b/src/sockets/Platform.h index 19dc9a3..c1c074b 100644 --- a/src/sockets/Platform.h +++ b/src/sockets/Platform.h @@ -9,12 +9,30 @@ #define EIPSCANNER_SOCKETS_PLATFORM_H #include +#include #if defined (__unix__) #define SOCKET_ERRNO() (errno) +#define SOCKET_ERROR_CATEGORY() (std::generic_category()) +#define SOCKET_CLOSE(socketFd) (close(socketFd)) #define SOCKET_SHUTDOWN_OPERATION (SHUT_RDWR) + #elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) +#include + +namespace detail +{ + class win32_error_category : public std::error_category { + public: + char const* name() const noexcept override final; + std::string message(int c) const override final; + }; +} +extern detail::win32_error_category const& win32_error_category(); + #define SOCKET_ERRNO() (WSAGetLastError()) +#define SOCKET_ERROR_CATEGORY() (win32_error_category()) +#define SOCKET_CLOSE(socketFd) (closesocket(socketFd)) #define SOCKET_SHUTDOWN_OPERATION (SD_BOTH) //Used socket error codes - redefine to the respective WSA error code. @@ -24,7 +42,6 @@ #define EINPROGRESS (WSAEINPROGRESS) #undef ETIMEDOUT #define ETIMEDOUT (WSAETIMEDOUT) - #endif #endif // EIPSCANNER_SOCKETS_PLATFORM_H diff --git a/src/sockets/TCPSocket.cpp b/src/sockets/TCPSocket.cpp index 53f82a8..032ec9e 100644 --- a/src/sockets/TCPSocket.cpp +++ b/src/sockets/TCPSocket.cpp @@ -8,13 +8,13 @@ #include #include #include +#include #elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) #include #include #include #endif -#include #include #include "utils/Logger.h" @@ -32,21 +32,22 @@ namespace eipScanner { TCPSocket::TCPSocket(EndPoint endPoint, std::chrono::milliseconds connTimeout) : BaseSocket(std::move(endPoint)) { + _sockedFd = socket(AF_INET, SOCK_STREAM, 0); if (_sockedFd < 0) { - throw std::system_error(SOCKET_ERRNO(), std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); } // Set non-blocking #if defined(__unix__) auto arg = fcntl(_sockedFd, F_GETFL, NULL); if (arg < 0) { - throw std::system_error(SOCKET_ERRNO(), std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); } arg |= O_NONBLOCK; if (fcntl(_sockedFd, F_SETFL, arg) < 0) { - throw std::system_error(SOCKET_ERRNO(), std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); } #endif @@ -66,36 +67,36 @@ namespace eipScanner { res = ::select(_sockedFd + 1, NULL, &myset, NULL, &tv); if (res < 0 && SOCKET_ERRNO() != EINTR) { - throw std::system_error(SOCKET_ERRNO(), std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); } else if (res > 0) { // Socket selected for write int err; socklen_t lon = sizeof(int); if (getsockopt(_sockedFd, SOL_SOCKET, SO_ERROR, (char *) (&err), &lon) < 0) { - throw std::system_error(SOCKET_ERRNO(), std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); } // Check the value returned... if (err) { - throw std::system_error(err, std::generic_category()); + throw std::system_error(err, SOCKET_ERROR_CATEGORY()); } break; } else { - throw std::system_error(ETIMEDOUT, std::generic_category()); + throw std::system_error(ETIMEDOUT, SOCKET_ERROR_CATEGORY()); } } while (1); } else { - throw std::system_error(SOCKET_ERRNO(), std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); } } #if defined(__unix__) // Set to blocking mode again... if ((arg = fcntl(_sockedFd, F_GETFL, NULL)) < 0) { - throw std::system_error(SOCKET_ERRNO(), std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); } arg &= (~O_NONBLOCK); if (fcntl(_sockedFd, F_SETFL, arg) < 0) { - throw std::system_error(SOCKET_ERRNO(), std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); } #endif } @@ -109,7 +110,7 @@ namespace eipScanner { TCPSocket::~TCPSocket() { Logger(LogLevel::DEBUG) << "Close TCP socket fd=" << _sockedFd; shutdown(_sockedFd, SOCKET_SHUTDOWN_OPERATION); - close(_sockedFd); + SOCKET_CLOSE(_sockedFd); } void TCPSocket::Send(const std::vector &data) const { @@ -117,7 +118,7 @@ namespace eipScanner { int count = send(_sockedFd, (char*)data.data(), data.size(), 0); if (count < data.size()) { - throw std::system_error(SOCKET_ERRNO(), std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); } } @@ -130,7 +131,7 @@ namespace eipScanner { Logger(LogLevel::TRACE) << "Received " << len << " bytes from TCP socket #" << _sockedFd << "."; count += len; if (len < 0) { - throw std::system_error(SOCKET_ERRNO(), std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); } if (len == 0) { diff --git a/src/sockets/UDPBoundSocket.cpp b/src/sockets/UDPBoundSocket.cpp index f2b3070..a14ed1b 100644 --- a/src/sockets/UDPBoundSocket.cpp +++ b/src/sockets/UDPBoundSocket.cpp @@ -21,13 +21,13 @@ namespace sockets { : UDPSocket(std::move(endPoint)) { int on = 1; if (setsockopt(_sockedFd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) < 0) { - throw std::system_error(SOCKET_ERRNO(), std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); } auto addr = _remoteEndPoint.getAddr(); addr.sin_addr.s_addr = INADDR_ANY; if (bind(_sockedFd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { - throw std::system_error(SOCKET_ERRNO(), std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); } } diff --git a/src/sockets/UDPSocket.cpp b/src/sockets/UDPSocket.cpp index 3aa7b5a..8a715bd 100644 --- a/src/sockets/UDPSocket.cpp +++ b/src/sockets/UDPSocket.cpp @@ -8,13 +8,12 @@ #include #include #include +#include #elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) #include #include #endif -#include - #include "utils/Logger.h" #include "UDPSocket.h" #include "Platform.h" @@ -34,7 +33,7 @@ namespace sockets { _sockedFd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (_sockedFd < 0) { - throw std::system_error(SOCKET_ERRNO(), std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); } Logger(LogLevel::DEBUG) << "Opened UDP socket fd=" << _sockedFd; @@ -43,7 +42,7 @@ namespace sockets { UDPSocket::~UDPSocket() { Logger(LogLevel::DEBUG) << "Close UDP socket fd=" << _sockedFd; shutdown(_sockedFd, SOCKET_SHUTDOWN_OPERATION); - close(_sockedFd); + SOCKET_CLOSE(_sockedFd); } void UDPSocket::Send(const std::vector &data) const { @@ -53,7 +52,7 @@ namespace sockets { int count = sendto(_sockedFd, (char*)data.data(), data.size(), 0, (struct sockaddr *)&addr, sizeof(addr)); if (count < data.size()) { - throw std::system_error(SOCKET_ERRNO(), std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); } } @@ -63,7 +62,7 @@ namespace sockets { auto len = recvfrom(_sockedFd, (char*)recvBuffer.data(), recvBuffer.size(), 0, NULL, NULL); Logger(LogLevel::TRACE) << "Received " << len << " bytes from UDP socket #" << _sockedFd << "."; if (len < 0) { - throw std::system_error(SOCKET_ERRNO(), std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); } return recvBuffer; @@ -76,7 +75,7 @@ namespace sockets { auto len = recvfrom(_sockedFd, (char*)recvBuffer.data(), recvBuffer.size(), 0, (struct sockaddr*)&addr, &addrFromLength); Logger(LogLevel::TRACE) << "Received " << len << " bytes from UDP socket #" << _sockedFd << "."; if (len < 0) { - throw std::system_error(SOCKET_ERRNO(), std::generic_category()); + throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); } endPoint = EndPoint(addr); From 5c0cd0681ed3dce6762448d6e17ec9b17109cba2 Mon Sep 17 00:00:00 2001 From: Stefan Broekman Date: Tue, 6 Apr 2021 18:46:50 +0200 Subject: [PATCH 05/11] Replace macros with class functions --- src/DiscoveryManager.cpp | 8 +++---- src/DiscoveryManager.h | 2 +- src/sockets/BaseSocket.cpp | 40 ++++++++++++++++++++++++++++++---- src/sockets/BaseSocket.h | 6 ++++- src/sockets/EndPoint.cpp | 3 ++- src/sockets/Platform.cpp | 1 + src/sockets/Platform.h | 32 +++++---------------------- src/sockets/TCPSocket.cpp | 32 +++++++++++++-------------- src/sockets/UDPBoundSocket.cpp | 4 ++-- src/sockets/UDPSocket.cpp | 12 +++++----- 10 files changed, 78 insertions(+), 62 deletions(-) diff --git a/src/DiscoveryManager.cpp b/src/DiscoveryManager.cpp index 86c9997..e88d354 100644 --- a/src/DiscoveryManager.cpp +++ b/src/DiscoveryManager.cpp @@ -13,9 +13,9 @@ #include "sockets/Platform.h" #if defined (__unix__) -#define DISCOVER_END (EAGAIN) +#define DISCOVERY_SOCKET_RECEIVE_END_ERROR_CODE (EIPSCANNER_SOCKET_ERROR(EAGAIN)) #elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) -#define DISCOVER_END (ETIMEDOUT) +#define DISCOVERY_SOCKET_RECEIVE_END_ERROR_CODE (EIPSCANNER_SOCKET_ERROR(ETIMEDOUT)) #endif namespace eipScanner { @@ -85,7 +85,7 @@ namespace eipScanner { } } } catch (std::system_error& er) { - if (er.code().value() != DISCOVER_END) { + if (er.code().value() != DISCOVERY_SOCKET_RECEIVE_END_ERROR_CODE) { throw er; } } @@ -99,7 +99,7 @@ namespace eipScanner { int broadcast = 1; if(setsockopt(socket->getSocketFd(), SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, sizeof(broadcast)) < 0) { - throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); + throw std::system_error(sockets::BaseSocket::getLastError(), sockets::BaseSocket::getErrorCategory()); } return socket; diff --git a/src/DiscoveryManager.h b/src/DiscoveryManager.h index 2cba8cd..33d185e 100644 --- a/src/DiscoveryManager.h +++ b/src/DiscoveryManager.h @@ -37,7 +37,7 @@ namespace eipScanner { /** * @brief default destructor */ - ~DiscoveryManager(); + virtual ~DiscoveryManager(); /** * @brief Discovers the EIP network diff --git a/src/sockets/BaseSocket.cpp b/src/sockets/BaseSocket.cpp index 6025f8e..77baab1 100644 --- a/src/sockets/BaseSocket.cpp +++ b/src/sockets/BaseSocket.cpp @@ -33,6 +33,39 @@ namespace sockets { BaseSocket::~BaseSocket() = default; + void BaseSocket::Shutdown() { +#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) + shutdown(_sockedFd, SD_BOTH); +#else + shutdown(_sockedFd, SHUT_RDWR); +#endif + } + + void BaseSocket::Close() { +#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) + closesocket(_sockedFd); +#else + close(_sockedFd); +#endif + } + + /*static*/int BaseSocket::getLastError() { +#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) + return WSAGetLastError(); +#else + return errno; +#endif + } + + /*static*/const std::error_category& BaseSocket::getErrorCategory() noexcept + { +#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) + return win32_error_category(); +#else + return std::generic_category(); +#endif + } + int BaseSocket::getSocketFd() const { return _sockedFd; } @@ -60,10 +93,9 @@ namespace sockets { #ifdef __APPLE__ .tv_sec = static_cast<__darwin_suseconds_t>(recvTimeout.count()/1000), .tv_usec = static_cast<__darwin_suseconds_t>((recvTimeout.count()%1000)*1000) -#elif __linux__ +#elif __unix__ .tv_sec = static_cast<__time_t>(recvTimeout.count()/1000), .tv_usec = static_cast<__time_t>((recvTimeout.count()%1000)*1000) - #elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) .tv_sec = static_cast(recvTimeout.count()/1000), .tv_usec = static_cast((recvTimeout.count()%1000)*1000) @@ -72,7 +104,7 @@ namespace sockets { }; tv.tv_sec = std::max(tv.tv_sec, 0); - tv.tv_usec = std::max(tv.tv_usec, 0); + tv.tv_usec = std::max(tv.tv_usec, 0); return tv; } @@ -103,7 +135,7 @@ namespace sockets { ready = ::select(socketWithMaxFd->getSocketFd() + 1, &recvSet, NULL, NULL, &tv); if (ready < 0) { - throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); + throw std::system_error(BaseSocket::getLastError(), BaseSocket::getErrorCategory()); } for (auto& sock : sockets) { diff --git a/src/sockets/BaseSocket.h b/src/sockets/BaseSocket.h index 3440bd5..3dba409 100644 --- a/src/sockets/BaseSocket.h +++ b/src/sockets/BaseSocket.h @@ -30,18 +30,22 @@ namespace sockets { virtual std::vector Receive(size_t size) const = 0; void setBeginReceiveHandler(BeginReceiveHandler handler); - const std::chrono::milliseconds &getRecvTimeout() const; void setRecvTimeout(const std::chrono::milliseconds &recvTimeout); int getSocketFd() const; + static int getLastError(); + static const std::error_category& getErrorCategory() noexcept; + const EndPoint &getRemoteEndPoint() const; static void select(std::vector sockets, std::chrono::milliseconds timeout); protected: void BeginReceive(); + void Shutdown(); + void Close(); int _sockedFd; EndPoint _remoteEndPoint; diff --git a/src/sockets/EndPoint.cpp b/src/sockets/EndPoint.cpp index 18fe224..562de4d 100644 --- a/src/sockets/EndPoint.cpp +++ b/src/sockets/EndPoint.cpp @@ -3,6 +3,7 @@ // #include "EndPoint.h" +#include "BaseSocket.h" #include "Platform.h" #if defined(__unix__) @@ -36,7 +37,7 @@ namespace sockets { #elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) if (inet_pton(AF_INET, _host.c_str(), &_addr.sin_addr.s_addr) < 0) { #endif - throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); + throw std::system_error(BaseSocket::getLastError(), BaseSocket::getErrorCategory()); } } diff --git a/src/sockets/Platform.cpp b/src/sockets/Platform.cpp index e5a9602..020a6f0 100644 --- a/src/sockets/Platform.cpp +++ b/src/sockets/Platform.cpp @@ -5,6 +5,7 @@ #include "Platform.h" #if defined(_WIN32) || defined(WIN32) || defined(_WIN64) +#include namespace detail { diff --git a/src/sockets/Platform.h b/src/sockets/Platform.h index c1c074b..f72b448 100644 --- a/src/sockets/Platform.h +++ b/src/sockets/Platform.h @@ -1,27 +1,14 @@ // // Created by Stefan Broekman on 02/04/21. // -// Only include in local (source) files related to (Win)Sockets, to prevent unintentional overrides in -// unwanted places. I.e. errno is not supported on Windows for WinSock only, it is for e.g. file I/O. -// #ifndef EIPSCANNER_SOCKETS_PLATFORM_H #define EIPSCANNER_SOCKETS_PLATFORM_H -#include #include -#if defined (__unix__) -#define SOCKET_ERRNO() (errno) -#define SOCKET_ERROR_CATEGORY() (std::generic_category()) -#define SOCKET_CLOSE(socketFd) (close(socketFd)) -#define SOCKET_SHUTDOWN_OPERATION (SHUT_RDWR) - -#elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) -#include - -namespace detail -{ +#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) +namespace detail { class win32_error_category : public std::error_category { public: char const* name() const noexcept override final; @@ -30,18 +17,9 @@ namespace detail } extern detail::win32_error_category const& win32_error_category(); -#define SOCKET_ERRNO() (WSAGetLastError()) -#define SOCKET_ERROR_CATEGORY() (win32_error_category()) -#define SOCKET_CLOSE(socketFd) (closesocket(socketFd)) -#define SOCKET_SHUTDOWN_OPERATION (SD_BOTH) - -//Used socket error codes - redefine to the respective WSA error code. -#undef EINTR -#define EINTR (WSAEINTR) -#undef EINPROGRESS -#define EINPROGRESS (WSAEINPROGRESS) -#undef ETIMEDOUT -#define ETIMEDOUT (WSAETIMEDOUT) +#define EIPSCANNER_SOCKET_ERROR(err) WSA##err +#else +#define EIPSCANNER_SOCKET_ERROR(err) err #endif #endif // EIPSCANNER_SOCKETS_PLATFORM_H diff --git a/src/sockets/TCPSocket.cpp b/src/sockets/TCPSocket.cpp index 032ec9e..122ae75 100644 --- a/src/sockets/TCPSocket.cpp +++ b/src/sockets/TCPSocket.cpp @@ -35,19 +35,19 @@ namespace eipScanner { _sockedFd = socket(AF_INET, SOCK_STREAM, 0); if (_sockedFd < 0) { - throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); + throw std::system_error(BaseSocket::getLastError(), BaseSocket::getErrorCategory()); } // Set non-blocking #if defined(__unix__) auto arg = fcntl(_sockedFd, F_GETFL, NULL); if (arg < 0) { - throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); + throw std::system_error(BaseSocket::getLastError(), BaseSocket::getErrorCategory()); } arg |= O_NONBLOCK; if (fcntl(_sockedFd, F_SETFL, arg) < 0) { - throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); + throw std::system_error(BaseSocket::getLastError(), BaseSocket::getErrorCategory()); } #endif @@ -57,7 +57,7 @@ namespace eipScanner { auto addr = _remoteEndPoint.getAddr(); auto res = connect(_sockedFd, (struct sockaddr *) &addr, sizeof(addr)); if (res < 0) { - if (SOCKET_ERRNO() == EINPROGRESS) { + if (BaseSocket::getLastError() == EIPSCANNER_SOCKET_ERROR(EINPROGRESS)) { do { fd_set myset; auto tv = makePortableInterval(connTimeout); @@ -66,37 +66,37 @@ namespace eipScanner { FD_SET(_sockedFd, &myset); res = ::select(_sockedFd + 1, NULL, &myset, NULL, &tv); - if (res < 0 && SOCKET_ERRNO() != EINTR) { - throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); + if (res < 0 && BaseSocket::getLastError() != EIPSCANNER_SOCKET_ERROR(EINTR)) { + throw std::system_error(BaseSocket::getLastError(), BaseSocket::getErrorCategory()); } else if (res > 0) { // Socket selected for write int err; socklen_t lon = sizeof(int); if (getsockopt(_sockedFd, SOL_SOCKET, SO_ERROR, (char *) (&err), &lon) < 0) { - throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); + throw std::system_error(BaseSocket::getLastError(), BaseSocket::getErrorCategory()); } // Check the value returned... if (err) { - throw std::system_error(err, SOCKET_ERROR_CATEGORY()); + throw std::system_error(err, BaseSocket::getErrorCategory()); } break; } else { - throw std::system_error(ETIMEDOUT, SOCKET_ERROR_CATEGORY()); + throw std::system_error(EIPSCANNER_SOCKET_ERROR(ETIMEDOUT), BaseSocket::getErrorCategory()); } } while (1); } else { - throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); + throw std::system_error(BaseSocket::getLastError(), BaseSocket::getErrorCategory()); } } #if defined(__unix__) // Set to blocking mode again... if ((arg = fcntl(_sockedFd, F_GETFL, NULL)) < 0) { - throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); + throw std::system_error(BaseSocket::getLastError(), BaseSocket::getErrorCategory()); } arg &= (~O_NONBLOCK); if (fcntl(_sockedFd, F_SETFL, arg) < 0) { - throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); + throw std::system_error(BaseSocket::getLastError(), BaseSocket::getErrorCategory()); } #endif } @@ -109,8 +109,8 @@ namespace eipScanner { TCPSocket::~TCPSocket() { Logger(LogLevel::DEBUG) << "Close TCP socket fd=" << _sockedFd; - shutdown(_sockedFd, SOCKET_SHUTDOWN_OPERATION); - SOCKET_CLOSE(_sockedFd); + Shutdown(); + Close(); } void TCPSocket::Send(const std::vector &data) const { @@ -118,7 +118,7 @@ namespace eipScanner { int count = send(_sockedFd, (char*)data.data(), data.size(), 0); if (count < data.size()) { - throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); + throw std::system_error(BaseSocket::getLastError(), BaseSocket::getErrorCategory()); } } @@ -131,7 +131,7 @@ namespace eipScanner { Logger(LogLevel::TRACE) << "Received " << len << " bytes from TCP socket #" << _sockedFd << "."; count += len; if (len < 0) { - throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); + throw std::system_error(BaseSocket::getLastError(), BaseSocket::getErrorCategory()); } if (len == 0) { diff --git a/src/sockets/UDPBoundSocket.cpp b/src/sockets/UDPBoundSocket.cpp index a14ed1b..8b03fc9 100644 --- a/src/sockets/UDPBoundSocket.cpp +++ b/src/sockets/UDPBoundSocket.cpp @@ -21,13 +21,13 @@ namespace sockets { : UDPSocket(std::move(endPoint)) { int on = 1; if (setsockopt(_sockedFd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) < 0) { - throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); + throw std::system_error(BaseSocket::getLastError(), BaseSocket::getErrorCategory()); } auto addr = _remoteEndPoint.getAddr(); addr.sin_addr.s_addr = INADDR_ANY; if (bind(_sockedFd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { - throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); + throw std::system_error(BaseSocket::getLastError(), BaseSocket::getErrorCategory()); } } diff --git a/src/sockets/UDPSocket.cpp b/src/sockets/UDPSocket.cpp index 8a715bd..2123db1 100644 --- a/src/sockets/UDPSocket.cpp +++ b/src/sockets/UDPSocket.cpp @@ -33,7 +33,7 @@ namespace sockets { _sockedFd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (_sockedFd < 0) { - throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); + throw std::system_error(BaseSocket::getLastError(), BaseSocket::getErrorCategory()); } Logger(LogLevel::DEBUG) << "Opened UDP socket fd=" << _sockedFd; @@ -41,8 +41,8 @@ namespace sockets { UDPSocket::~UDPSocket() { Logger(LogLevel::DEBUG) << "Close UDP socket fd=" << _sockedFd; - shutdown(_sockedFd, SOCKET_SHUTDOWN_OPERATION); - SOCKET_CLOSE(_sockedFd); + Shutdown(); + Close(); } void UDPSocket::Send(const std::vector &data) const { @@ -52,7 +52,7 @@ namespace sockets { int count = sendto(_sockedFd, (char*)data.data(), data.size(), 0, (struct sockaddr *)&addr, sizeof(addr)); if (count < data.size()) { - throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); + throw std::system_error(BaseSocket::getLastError(), BaseSocket::getErrorCategory()); } } @@ -62,7 +62,7 @@ namespace sockets { auto len = recvfrom(_sockedFd, (char*)recvBuffer.data(), recvBuffer.size(), 0, NULL, NULL); Logger(LogLevel::TRACE) << "Received " << len << " bytes from UDP socket #" << _sockedFd << "."; if (len < 0) { - throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); + throw std::system_error(BaseSocket::getLastError(), BaseSocket::getErrorCategory()); } return recvBuffer; @@ -75,7 +75,7 @@ namespace sockets { auto len = recvfrom(_sockedFd, (char*)recvBuffer.data(), recvBuffer.size(), 0, (struct sockaddr*)&addr, &addrFromLength); Logger(LogLevel::TRACE) << "Received " << len << " bytes from UDP socket #" << _sockedFd << "."; if (len < 0) { - throw std::system_error(SOCKET_ERRNO(), SOCKET_ERROR_CATEGORY()); + throw std::system_error(BaseSocket::getLastError(), BaseSocket::getErrorCategory()); } endPoint = EndPoint(addr); From 4a94cbc56061cd29f8ac89e695f5826371e650be Mon Sep 17 00:00:00 2001 From: Stefan Broekman Date: Tue, 6 Apr 2021 18:56:07 +0200 Subject: [PATCH 06/11] Resolve Linux build errors --- src/sockets/BaseSocket.cpp | 3 ++- src/sockets/BaseSocket.h | 1 + src/sockets/TCPSocket.cpp | 2 -- src/sockets/UDPSocket.cpp | 2 -- 4 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/sockets/BaseSocket.cpp b/src/sockets/BaseSocket.cpp index 77baab1..2e63b55 100644 --- a/src/sockets/BaseSocket.cpp +++ b/src/sockets/BaseSocket.cpp @@ -5,6 +5,7 @@ #if defined (__unix__) #include #include +#include #elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) #include #include @@ -12,7 +13,7 @@ #include #include -#include + #include "BaseSocket.h" #include "Platform.h" diff --git a/src/sockets/BaseSocket.h b/src/sockets/BaseSocket.h index 3dba409..1019fab 100644 --- a/src/sockets/BaseSocket.h +++ b/src/sockets/BaseSocket.h @@ -11,6 +11,7 @@ #include #include #include +#include #include "EndPoint.h" diff --git a/src/sockets/TCPSocket.cpp b/src/sockets/TCPSocket.cpp index 122ae75..a8cc824 100644 --- a/src/sockets/TCPSocket.cpp +++ b/src/sockets/TCPSocket.cpp @@ -2,8 +2,6 @@ // Created by Aleksey Timin on 11/16/19. // -#include - #if defined(__unix__) #include #include diff --git a/src/sockets/UDPSocket.cpp b/src/sockets/UDPSocket.cpp index 2123db1..02f72ef 100644 --- a/src/sockets/UDPSocket.cpp +++ b/src/sockets/UDPSocket.cpp @@ -2,8 +2,6 @@ // Created by Aleksey Timin on 11/18/19. // -#include - #if defined(__unix__) #include #include From b320a4bc40c41a0a8e74952c420ac2d9340566f2 Mon Sep 17 00:00:00 2001 From: Stefan Broekman Date: Wed, 7 Apr 2021 11:27:53 +0200 Subject: [PATCH 07/11] Add Platform cpp to CMake file --- src/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9e3a993..dc2bbb9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -30,6 +30,7 @@ set(SOURCE_FILES sockets/TCPSocket.cpp sockets/UDPBoundSocket.cpp sockets/UDPSocket.cpp + sockets/Platform.cpp utils/Logger.cpp utils/Buffer.cpp From debe4ef7b22198810edf16cbbaec839f67d638a4 Mon Sep 17 00:00:00 2001 From: Stefan Broekman Date: Wed, 7 Apr 2021 11:57:55 +0200 Subject: [PATCH 08/11] Clean win32ErrorCategory class --- src/sockets/BaseSocket.cpp | 2 +- src/sockets/Platform.cpp | 17 +++++++++-------- src/sockets/Platform.h | 19 +++++++++++-------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/sockets/BaseSocket.cpp b/src/sockets/BaseSocket.cpp index 2e63b55..871ee92 100644 --- a/src/sockets/BaseSocket.cpp +++ b/src/sockets/BaseSocket.cpp @@ -61,7 +61,7 @@ namespace sockets { /*static*/const std::error_category& BaseSocket::getErrorCategory() noexcept { #if defined(_WIN32) || defined(WIN32) || defined(_WIN64) - return win32_error_category(); + return win32ErrorCategory::category(); #else return std::generic_category(); #endif diff --git a/src/sockets/Platform.cpp b/src/sockets/Platform.cpp index 020a6f0..175343f 100644 --- a/src/sockets/Platform.cpp +++ b/src/sockets/Platform.cpp @@ -7,13 +7,13 @@ #if defined(_WIN32) || defined(WIN32) || defined(_WIN64) #include -namespace detail -{ - char const* win32_error_category::name() const noexcept { +namespace eipScanner { +namespace sockets { + char const* win32ErrorCategory::name() const noexcept { return "Win32Error"; } - std::string win32_error_category::message(int c) const { + std::string win32ErrorCategory::message(int c) const { char error[UINT8_MAX]; auto len = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, static_cast(c), 0, error, sizeof(error), nullptr); if (len == 0) { @@ -25,10 +25,11 @@ namespace detail } return std::string(error, len); } -} -detail::win32_error_category const& win32_error_category() { - static detail::win32_error_category c; - return c; + /*static*/win32ErrorCategory const& win32ErrorCategory::category() { + static win32ErrorCategory c; + return c; + } +} } #endif diff --git a/src/sockets/Platform.h b/src/sockets/Platform.h index f72b448..cb94b23 100644 --- a/src/sockets/Platform.h +++ b/src/sockets/Platform.h @@ -7,19 +7,22 @@ #include +#define EIPSCANNER_SOCKET_ERROR(err) WSA##err +#else +#define EIPSCANNER_SOCKET_ERROR(err) err +#endif + #if defined(_WIN32) || defined(WIN32) || defined(_WIN64) -namespace detail { - class win32_error_category : public std::error_category { +namespace eipScanner { +namespace sockets { + class win32ErrorCategory : public std::error_category { public: char const* name() const noexcept override final; std::string message(int c) const override final; + + static win32ErrorCategory const& category(); }; } -extern detail::win32_error_category const& win32_error_category(); - -#define EIPSCANNER_SOCKET_ERROR(err) WSA##err -#else -#define EIPSCANNER_SOCKET_ERROR(err) err -#endif +} #endif // EIPSCANNER_SOCKETS_PLATFORM_H From 7dcdcc2f2c9adf52558902df1d37fa83039ae1a6 Mon Sep 17 00:00:00 2001 From: Stefan Broekman Date: Mon, 12 Apr 2021 19:21:38 +0200 Subject: [PATCH 09/11] Resolve Linux build errors and re-add Apple preprocessor --- src/sockets/EndPoint.cpp | 4 ++-- src/sockets/EndPoint.h | 2 +- src/sockets/Platform.h | 10 ++++++---- src/sockets/TCPSocket.cpp | 6 +++--- src/sockets/UDPSocket.cpp | 2 +- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/sockets/EndPoint.cpp b/src/sockets/EndPoint.cpp index 562de4d..b6de7ad 100644 --- a/src/sockets/EndPoint.cpp +++ b/src/sockets/EndPoint.cpp @@ -6,7 +6,7 @@ #include "BaseSocket.h" #include "Platform.h" -#if defined(__unix__) +#if defined(__unix__) || defined(__APPLE__) #include #elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) #include @@ -32,7 +32,7 @@ namespace sockets { _addr.sin_family = AF_INET; _addr.sin_port = htons(_port); -#if defined(__unix__) +#if defined(__unix__) || defined(__APPLE__) if (inet_aton(_host.c_str(), &_addr.sin_addr) < 0) { #elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) if (inet_pton(AF_INET, _host.c_str(), &_addr.sin_addr.s_addr) < 0) { diff --git a/src/sockets/EndPoint.h b/src/sockets/EndPoint.h index bf3e417..43c690b 100644 --- a/src/sockets/EndPoint.h +++ b/src/sockets/EndPoint.h @@ -5,7 +5,7 @@ #ifndef EIPSCANNER_SOCKETS_ENDPOINT_H #define EIPSCANNER_SOCKETS_ENDPOINT_H -#if defined(__unix__) +#if defined(__unix__) || defined(__APPLE__) #include #elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) #include diff --git a/src/sockets/Platform.h b/src/sockets/Platform.h index cb94b23..180d901 100644 --- a/src/sockets/Platform.h +++ b/src/sockets/Platform.h @@ -7,12 +7,10 @@ #include +#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) + #define EIPSCANNER_SOCKET_ERROR(err) WSA##err -#else -#define EIPSCANNER_SOCKET_ERROR(err) err -#endif -#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) namespace eipScanner { namespace sockets { class win32ErrorCategory : public std::error_category { @@ -25,4 +23,8 @@ namespace sockets { } } +#else +#define EIPSCANNER_SOCKET_ERROR(err) err +#endif + #endif // EIPSCANNER_SOCKETS_PLATFORM_H diff --git a/src/sockets/TCPSocket.cpp b/src/sockets/TCPSocket.cpp index a8cc824..a4ba7e9 100644 --- a/src/sockets/TCPSocket.cpp +++ b/src/sockets/TCPSocket.cpp @@ -2,7 +2,7 @@ // Created by Aleksey Timin on 11/16/19. // -#if defined(__unix__) +#if defined(__unix__) || defined(__APPLE__) #include #include #include @@ -37,7 +37,7 @@ namespace eipScanner { } // Set non-blocking -#if defined(__unix__) +#if defined(__unix__) || defined(__APPLE__) auto arg = fcntl(_sockedFd, F_GETFL, NULL); if (arg < 0) { throw std::system_error(BaseSocket::getLastError(), BaseSocket::getErrorCategory()); @@ -87,7 +87,7 @@ namespace eipScanner { } } -#if defined(__unix__) +#if defined(__unix__) || defined(__APPLE__) // Set to blocking mode again... if ((arg = fcntl(_sockedFd, F_GETFL, NULL)) < 0) { throw std::system_error(BaseSocket::getLastError(), BaseSocket::getErrorCategory()); diff --git a/src/sockets/UDPSocket.cpp b/src/sockets/UDPSocket.cpp index 02f72ef..eab6ea4 100644 --- a/src/sockets/UDPSocket.cpp +++ b/src/sockets/UDPSocket.cpp @@ -2,7 +2,7 @@ // Created by Aleksey Timin on 11/18/19. // -#if defined(__unix__) +#if defined(__unix__) || defined(__APPLE__) #include #include #include From 8e7bb7cae2efb55ca253c63a5f14c8e4e6ec1bd1 Mon Sep 17 00:00:00 2001 From: Stefan Broekman Date: Mon, 12 Apr 2021 19:51:10 +0200 Subject: [PATCH 10/11] Replace static constexpr (c++17) with define --- src/ConnectionManager.cpp | 4 ++-- src/sockets/EndPoint.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ConnectionManager.cpp b/src/ConnectionManager.cpp index 96a1cf5..a4c76a2 100644 --- a/src/ConnectionManager.cpp +++ b/src/ConnectionManager.cpp @@ -145,13 +145,13 @@ namespace eipScanner { } } else { - ioConnection->_socket = std::make_unique(si->getRemoteEndPoint().getHost(), sockets::EndPoint::defaultImplicitPort); + ioConnection->_socket = std::make_unique(si->getRemoteEndPoint().getHost(), EIP_DEFAULT_IMPLICIT_PORT); } Logger(LogLevel::INFO) << "Open UDP socket to send data to " << ioConnection->_socket->getRemoteEndPoint().toString(); - findOrCreateSocket(sockets::EndPoint(si->getRemoteEndPoint().getHost(), sockets::EndPoint::defaultImplicitPort)); + findOrCreateSocket(sockets::EndPoint(si->getRemoteEndPoint().getHost(), EIP_DEFAULT_IMPLICIT_PORT)); auto result = _connectionMap .insert(std::make_pair(response.getT2ONetworkConnectionId(), ioConnection)); diff --git a/src/sockets/EndPoint.h b/src/sockets/EndPoint.h index 43c690b..1bffa44 100644 --- a/src/sockets/EndPoint.h +++ b/src/sockets/EndPoint.h @@ -12,14 +12,14 @@ #endif #include +#define EIP_DEFAULT_EXPLICIT_PORT (uint16_t)(44818) +#define EIP_DEFAULT_IMPLICIT_PORT (uint16_t)(2222) + namespace eipScanner { namespace sockets { class EndPoint { public: - static constexpr auto defaultExplicitPort = 44818; - static constexpr auto defaultImplicitPort = 2222; - EndPoint(std::string host, int port); EndPoint(struct sockaddr_in& addr); From 45cf326cfc388de919adf55ab0177b10ccf7519c Mon Sep 17 00:00:00 2001 From: Stefan Broekman Date: Mon, 12 Apr 2021 20:14:42 +0200 Subject: [PATCH 11/11] Add APPLE ifdef preprocessor to remainder unix ifdefs --- src/DiscoveryManager.cpp | 2 +- src/sockets/BaseSocket.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DiscoveryManager.cpp b/src/DiscoveryManager.cpp index e88d354..e726543 100644 --- a/src/DiscoveryManager.cpp +++ b/src/DiscoveryManager.cpp @@ -12,7 +12,7 @@ #include "DiscoveryManager.h" #include "sockets/Platform.h" -#if defined (__unix__) +#if defined (__unix__) || defined(__APPLE__) #define DISCOVERY_SOCKET_RECEIVE_END_ERROR_CODE (EIPSCANNER_SOCKET_ERROR(EAGAIN)) #elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) #define DISCOVERY_SOCKET_RECEIVE_END_ERROR_CODE (EIPSCANNER_SOCKET_ERROR(ETIMEDOUT)) diff --git a/src/sockets/BaseSocket.cpp b/src/sockets/BaseSocket.cpp index 871ee92..efb71db 100644 --- a/src/sockets/BaseSocket.cpp +++ b/src/sockets/BaseSocket.cpp @@ -2,7 +2,7 @@ // Created by Aleksey Timin on 11/18/19. // -#if defined (__unix__) +#if defined (__unix__) || defined(__APPLE__) #include #include #include