Skip to content

Commit

Permalink
Merge pull request #39 from luntik2012/master
Browse files Browse the repository at this point in the history
ported to mingw (mxe/gcc10)
  • Loading branch information
jadamroth authored Jan 26, 2021
2 parents 8080537 + 7ef3285 commit 71a1797
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 20 deletions.
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ set(SOURCE_FILES
add_library(EIPScanner SHARED ${SOURCE_FILES})
add_library(EIPScannerS STATIC ${SOURCE_FILES})

if(WIN32)
target_link_libraries(EIPScanner ws2_32)
target_link_libraries(EIPScannerS ws2_32)
endif()

set_target_properties(
EIPScanner
PROPERTIES
Expand Down
2 changes: 1 addition & 1 deletion src/DiscoveryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ namespace eipScanner {
socket->setRecvTimeout(_receiveTimout);

int broadcast = 1;
if(setsockopt(socket->getSocketFd(), SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) < 0) {
if(setsockopt(socket->getSocketFd(), SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, sizeof(broadcast)) < 0) {
throw std::system_error(errno, std::generic_category());
}

Expand Down
20 changes: 16 additions & 4 deletions src/sockets/BaseSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
// Created by Aleksey Timin on 11/18/19.
//

#if defined(__linux__) || defined(__APPLE__)
#include <sys/socket.h>
#include <sys/select.h>
#elif defined _WIN32
#include <winsock2.h>
#include <time.h>
#endif

#include <utility>
#include <algorithm>
#include <system_error>
Expand Down Expand Up @@ -36,8 +42,14 @@ namespace sockets {
void BaseSocket::setRecvTimeout(const std::chrono::milliseconds &recvTimeout) {
_recvTimeout = recvTimeout;

timeval tv = makePortableInterval(recvTimeout);
setsockopt(_sockedFd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
#ifdef _WIN32
uint32_t ms = recvTimeout.count();
setsockopt(_sockedFd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&ms, sizeof ms);
#else
timeval tv = makePortableInterval(recvTimeout);
setsockopt(_sockedFd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
#endif

}

timeval BaseSocket::makePortableInterval(const std::chrono::milliseconds &recvTimeout) {
Expand All @@ -52,8 +64,8 @@ namespace sockets {

#elif _WIN32
// not sure what the macro is for windows
.tv_sec = static_cast<_time64>(recvTimeout.count()/1000),
.tv_usec = static_cast<_time64>((recvTimeout.count()%1000)*1000)
.tv_sec = static_cast<long int>(recvTimeout.count()/1000),
.tv_usec = static_cast<long int>((recvTimeout.count()%1000)*1000)
#endif

};
Expand Down
13 changes: 12 additions & 1 deletion src/sockets/EndPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
//

#include "EndPoint.h"

#if defined(__linux__) || defined(__APPLE__)
#include <arpa/inet.h>
#elif defined _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#endif

#include <system_error>
#include <utility>

Expand All @@ -23,7 +30,11 @@ namespace sockets {

_addr.sin_family = AF_INET;
_addr.sin_port = htons(_port);
#if defined(__linux__) || defined(__APPLE__)
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) {
#endif
throw std::system_error(errno, std::generic_category());
}
}
Expand Down Expand Up @@ -61,4 +72,4 @@ namespace sockets {
return _host + ":" + std::to_string(_port);
}
}
}
}
4 changes: 4 additions & 0 deletions src/sockets/EndPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
#ifndef EIPSCANNER_SOCKETS_ENDPOINT_H
#define EIPSCANNER_SOCKETS_ENDPOINT_H

#if defined(__linux__) || defined(__APPLE__)
#include <netinet/in.h>
#elif defined _WIN32
#include <winsock2.h>
#endif
#include <string>

namespace eipScanner {
Expand Down
25 changes: 21 additions & 4 deletions src/sockets/TCPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,27 @@
//

#include <system_error>

#ifdef __linux__
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#elif defined _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <time.h>
#endif

#include <unistd.h>
#include <fcntl.h>

#include "utils/Logger.h"
#include "TCPSocket.h"

#if !(defined __linux__) && !(defined SHUT_RDWR)
#define SHUT_RDWR SD_BOTH
#endif


namespace eipScanner {
namespace sockets {
Expand All @@ -30,6 +42,7 @@ namespace eipScanner {
}

// Set non-blocking
#ifdef __linux__
auto arg = fcntl(_sockedFd, F_GETFL, NULL);
if (arg < 0) {
throw std::system_error(errno, std::generic_category());
Expand All @@ -39,6 +52,7 @@ namespace eipScanner {
if (fcntl(_sockedFd, F_SETFL, arg) < 0) {
throw std::system_error(errno, std::generic_category());
}
#endif

Logger(LogLevel::DEBUG) << "Opened socket fd=" << _sockedFd;

Expand All @@ -61,7 +75,7 @@ namespace eipScanner {
// Socket selected for write
int err;
socklen_t lon = sizeof(int);
if (getsockopt(_sockedFd, SOL_SOCKET, SO_ERROR, (void *) (&err), &lon) < 0) {
if (getsockopt(_sockedFd, SOL_SOCKET, SO_ERROR, (char *) (&err), &lon) < 0) {
throw std::system_error(errno, std::generic_category());
}
// Check the value returned...
Expand All @@ -77,6 +91,8 @@ namespace eipScanner {
throw std::system_error(errno, std::generic_category());
}
}

#ifdef __linux__
// Set to blocking mode again...
if ((arg = fcntl(_sockedFd, F_GETFL, NULL)) < 0) {
throw std::system_error(errno, std::generic_category());
Expand All @@ -85,6 +101,7 @@ namespace eipScanner {
if (fcntl(_sockedFd, F_SETFL, arg) < 0) {
throw std::system_error(errno, std::generic_category());
}
#endif
}


Expand All @@ -102,7 +119,7 @@ namespace eipScanner {
void TCPSocket::Send(const std::vector<uint8_t> &data) const {
Logger(LogLevel::TRACE) << "Send " << data.size() << " bytes from TCP socket #" << _sockedFd << ".";

int count = send(_sockedFd, data.data(), data.size(), 0);
int count = send(_sockedFd, (char*)data.data(), data.size(), 0);
if (count < data.size()) {
throw std::system_error(errno, std::generic_category());
}
Expand All @@ -113,7 +130,7 @@ namespace eipScanner {

int count = 0;
while (size > count) {
auto len = recv(_sockedFd, recvBuffer.data() + count, size - count, 0);
auto len = recv(_sockedFd, (char*)(recvBuffer.data() + count), size - count, 0);
count += len;
if (len < 0) {
throw std::system_error(errno, std::generic_category());
Expand All @@ -132,4 +149,4 @@ namespace eipScanner {
return recvBuffer;
}
}
}
}
8 changes: 5 additions & 3 deletions src/sockets/UDPBoundSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// Created by Aleksey Timin on 11/21/19.
//
#include <system_error>
#include <sys/socket.h>
#include <netinet/in.h>

//#include <sys/socket.h>
//#include <netinet/in.h>

#include "UDPBoundSocket.h"

namespace eipScanner {
Expand All @@ -30,4 +32,4 @@ namespace sockets {

sockets::UDPBoundSocket::~UDPBoundSocket() = default;
}
}
}
2 changes: 1 addition & 1 deletion src/sockets/UDPBoundSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <vector>
#include <chrono>
#include <memory>
#include <netinet/in.h>
//#include <netinet/in.h>
#include "UDPSocket.h"

namespace eipScanner {
Expand Down
18 changes: 15 additions & 3 deletions src/sockets/UDPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,21 @@
//

#include <system_error>

#ifdef __linux__
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#elif defined _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#endif

#if !(defined __linux__) && !(defined SHUT_RDWR)
#define SHUT_RDWR SD_BOTH
#endif


#include <unistd.h>

#include "utils/Logger.h"
Expand Down Expand Up @@ -45,7 +57,7 @@ namespace sockets {
Logger(LogLevel::TRACE) << "Send " << data.size() << " bytes from UDP socket #" << _sockedFd << ".";

auto addr = _remoteEndPoint.getAddr();
int count = sendto(_sockedFd, data.data(), data.size(), 0,
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());
Expand All @@ -55,7 +67,7 @@ namespace sockets {
std::vector<uint8_t> UDPSocket::Receive(size_t size) const {
std::vector<uint8_t> recvBuffer(size);

auto len = recvfrom(_sockedFd, recvBuffer.data(), recvBuffer.size(), 0, NULL, NULL);
auto len = recvfrom(_sockedFd, (char*)recvBuffer.data(), recvBuffer.size(), 0, NULL, NULL);
if (len < 0) {
throw std::system_error(errno, std::generic_category());
}
Expand All @@ -67,7 +79,7 @@ namespace sockets {
std::vector<uint8_t> recvBuffer(size);
struct sockaddr_in addr;
socklen_t addrFromLength = sizeof(addr);
auto len = recvfrom(_sockedFd, recvBuffer.data(), recvBuffer.size(), 0, (struct sockaddr*)&addr, &addrFromLength);
auto len = recvfrom(_sockedFd, (char*)recvBuffer.data(), recvBuffer.size(), 0, (struct sockaddr*)&addr, &addrFromLength);
if (len < 0) {
throw std::system_error(errno, std::generic_category());
}
Expand Down
1 change: 0 additions & 1 deletion src/sockets/UDPSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <vector>
#include <chrono>
#include <memory>
#include <netinet/in.h>
#include "BaseSocket.h"

namespace eipScanner {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ Buffer &Buffer::operator<<(float val) {
sockaddr_in addr = v.getAddr();
return *this << htons(static_cast<cip::CipInt>(addr.sin_family))
<< addr.sin_port
<< addr.sin_addr.s_addr
<< uint32_t(addr.sin_addr.s_addr)
<< zeros;
}

Expand All @@ -191,7 +191,7 @@ Buffer &Buffer::operator<<(float val) {
sockaddr_in addr{0};
*this >> reinterpret_cast<cip::CipInt&>(addr.sin_family)
>> addr.sin_port
>> addr.sin_addr.s_addr
>> (uint32_t&)addr.sin_addr.s_addr
>> zeros;

addr.sin_family = htons(addr.sin_family);
Expand Down
4 changes: 4 additions & 0 deletions src/utils/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include <sstream>
#include <memory>

#ifdef _WIN32
#undef ERROR
#endif

namespace eipScanner {
namespace utils {

Expand Down

0 comments on commit 71a1797

Please sign in to comment.