Skip to content

Commit 71a1797

Browse files
authored
Merge pull request #39 from luntik2012/master
ported to mingw (mxe/gcc10)
2 parents 8080537 + 7ef3285 commit 71a1797

File tree

12 files changed

+86
-20
lines changed

12 files changed

+86
-20
lines changed

src/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ set(SOURCE_FILES
5555
add_library(EIPScanner SHARED ${SOURCE_FILES})
5656
add_library(EIPScannerS STATIC ${SOURCE_FILES})
5757

58+
if(WIN32)
59+
target_link_libraries(EIPScanner ws2_32)
60+
target_link_libraries(EIPScannerS ws2_32)
61+
endif()
62+
5863
set_target_properties(
5964
EIPScanner
6065
PROPERTIES

src/DiscoveryManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ namespace eipScanner {
9292
socket->setRecvTimeout(_receiveTimout);
9393

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

src/sockets/BaseSocket.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22
// Created by Aleksey Timin on 11/18/19.
33
//
44

5+
#if defined(__linux__) || defined(__APPLE__)
56
#include <sys/socket.h>
67
#include <sys/select.h>
8+
#elif defined _WIN32
9+
#include <winsock2.h>
10+
#include <time.h>
11+
#endif
12+
713
#include <utility>
814
#include <algorithm>
915
#include <system_error>
@@ -36,8 +42,14 @@ namespace sockets {
3642
void BaseSocket::setRecvTimeout(const std::chrono::milliseconds &recvTimeout) {
3743
_recvTimeout = recvTimeout;
3844

39-
timeval tv = makePortableInterval(recvTimeout);
40-
setsockopt(_sockedFd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
45+
#ifdef _WIN32
46+
uint32_t ms = recvTimeout.count();
47+
setsockopt(_sockedFd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&ms, sizeof ms);
48+
#else
49+
timeval tv = makePortableInterval(recvTimeout);
50+
setsockopt(_sockedFd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
51+
#endif
52+
4153
}
4254

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

5365
#elif _WIN32
5466
// not sure what the macro is for windows
55-
.tv_sec = static_cast<_time64>(recvTimeout.count()/1000),
56-
.tv_usec = static_cast<_time64>((recvTimeout.count()%1000)*1000)
67+
.tv_sec = static_cast<long int>(recvTimeout.count()/1000),
68+
.tv_usec = static_cast<long int>((recvTimeout.count()%1000)*1000)
5769
#endif
5870

5971
};

src/sockets/EndPoint.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
//
44

55
#include "EndPoint.h"
6+
7+
#if defined(__linux__) || defined(__APPLE__)
68
#include <arpa/inet.h>
9+
#elif defined _WIN32
10+
#include <winsock2.h>
11+
#include <ws2tcpip.h>
12+
#endif
13+
714
#include <system_error>
815
#include <utility>
916

@@ -23,7 +30,11 @@ namespace sockets {
2330

2431
_addr.sin_family = AF_INET;
2532
_addr.sin_port = htons(_port);
33+
#if defined(__linux__) || defined(__APPLE__)
2634
if (inet_aton(_host.c_str(), &_addr.sin_addr) < 0) {
35+
#elif defined _WIN32
36+
if ((_addr.sin_addr.s_addr = inet_addr(_host.c_str())) == INADDR_NONE) {
37+
#endif
2738
throw std::system_error(errno, std::generic_category());
2839
}
2940
}
@@ -61,4 +72,4 @@ namespace sockets {
6172
return _host + ":" + std::to_string(_port);
6273
}
6374
}
64-
}
75+
}

src/sockets/EndPoint.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
#ifndef EIPSCANNER_SOCKETS_ENDPOINT_H
66
#define EIPSCANNER_SOCKETS_ENDPOINT_H
77

8+
#if defined(__linux__) || defined(__APPLE__)
89
#include <netinet/in.h>
10+
#elif defined _WIN32
11+
#include <winsock2.h>
12+
#endif
913
#include <string>
1014

1115
namespace eipScanner {

src/sockets/TCPSocket.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,27 @@
33
//
44

55
#include <system_error>
6+
7+
#ifdef __linux__
68
#include <sys/socket.h>
79
#include <netinet/in.h>
810
#include <arpa/inet.h>
11+
#elif defined _WIN32
12+
#include <winsock2.h>
13+
#include <ws2tcpip.h>
14+
#include <time.h>
15+
#endif
16+
917
#include <unistd.h>
1018
#include <fcntl.h>
1119

1220
#include "utils/Logger.h"
1321
#include "TCPSocket.h"
1422

23+
#if !(defined __linux__) && !(defined SHUT_RDWR)
24+
#define SHUT_RDWR SD_BOTH
25+
#endif
26+
1527

1628
namespace eipScanner {
1729
namespace sockets {
@@ -30,6 +42,7 @@ namespace eipScanner {
3042
}
3143

3244
// Set non-blocking
45+
#ifdef __linux__
3346
auto arg = fcntl(_sockedFd, F_GETFL, NULL);
3447
if (arg < 0) {
3548
throw std::system_error(errno, std::generic_category());
@@ -39,6 +52,7 @@ namespace eipScanner {
3952
if (fcntl(_sockedFd, F_SETFL, arg) < 0) {
4053
throw std::system_error(errno, std::generic_category());
4154
}
55+
#endif
4256

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

@@ -61,7 +75,7 @@ namespace eipScanner {
6175
// Socket selected for write
6276
int err;
6377
socklen_t lon = sizeof(int);
64-
if (getsockopt(_sockedFd, SOL_SOCKET, SO_ERROR, (void *) (&err), &lon) < 0) {
78+
if (getsockopt(_sockedFd, SOL_SOCKET, SO_ERROR, (char *) (&err), &lon) < 0) {
6579
throw std::system_error(errno, std::generic_category());
6680
}
6781
// Check the value returned...
@@ -77,6 +91,8 @@ namespace eipScanner {
7791
throw std::system_error(errno, std::generic_category());
7892
}
7993
}
94+
95+
#ifdef __linux__
8096
// Set to blocking mode again...
8197
if ((arg = fcntl(_sockedFd, F_GETFL, NULL)) < 0) {
8298
throw std::system_error(errno, std::generic_category());
@@ -85,6 +101,7 @@ namespace eipScanner {
85101
if (fcntl(_sockedFd, F_SETFL, arg) < 0) {
86102
throw std::system_error(errno, std::generic_category());
87103
}
104+
#endif
88105
}
89106

90107

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

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

114131
int count = 0;
115132
while (size > count) {
116-
auto len = recv(_sockedFd, recvBuffer.data() + count, size - count, 0);
133+
auto len = recv(_sockedFd, (char*)(recvBuffer.data() + count), size - count, 0);
117134
count += len;
118135
if (len < 0) {
119136
throw std::system_error(errno, std::generic_category());
@@ -132,4 +149,4 @@ namespace eipScanner {
132149
return recvBuffer;
133150
}
134151
}
135-
}
152+
}

src/sockets/UDPBoundSocket.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// Created by Aleksey Timin on 11/21/19.
33
//
44
#include <system_error>
5-
#include <sys/socket.h>
6-
#include <netinet/in.h>
5+
6+
//#include <sys/socket.h>
7+
//#include <netinet/in.h>
8+
79
#include "UDPBoundSocket.h"
810

911
namespace eipScanner {
@@ -30,4 +32,4 @@ namespace sockets {
3032

3133
sockets::UDPBoundSocket::~UDPBoundSocket() = default;
3234
}
33-
}
35+
}

src/sockets/UDPBoundSocket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <vector>
99
#include <chrono>
1010
#include <memory>
11-
#include <netinet/in.h>
11+
//#include <netinet/in.h>
1212
#include "UDPSocket.h"
1313

1414
namespace eipScanner {

src/sockets/UDPSocket.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,21 @@
33
//
44

55
#include <system_error>
6+
7+
#ifdef __linux__
68
#include <sys/socket.h>
79
#include <netinet/in.h>
810
#include <arpa/inet.h>
11+
#elif defined _WIN32
12+
#include <winsock2.h>
13+
#include <ws2tcpip.h>
14+
#endif
15+
16+
#if !(defined __linux__) && !(defined SHUT_RDWR)
17+
#define SHUT_RDWR SD_BOTH
18+
#endif
19+
20+
921
#include <unistd.h>
1022

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

4759
auto addr = _remoteEndPoint.getAddr();
48-
int count = sendto(_sockedFd, data.data(), data.size(), 0,
60+
int count = sendto(_sockedFd, (char*)data.data(), data.size(), 0,
4961
(struct sockaddr *)&addr, sizeof(addr));
5062
if (count < data.size()) {
5163
throw std::system_error(errno, std::generic_category());
@@ -55,7 +67,7 @@ namespace sockets {
5567
std::vector<uint8_t> UDPSocket::Receive(size_t size) const {
5668
std::vector<uint8_t> recvBuffer(size);
5769

58-
auto len = recvfrom(_sockedFd, recvBuffer.data(), recvBuffer.size(), 0, NULL, NULL);
70+
auto len = recvfrom(_sockedFd, (char*)recvBuffer.data(), recvBuffer.size(), 0, NULL, NULL);
5971
if (len < 0) {
6072
throw std::system_error(errno, std::generic_category());
6173
}
@@ -67,7 +79,7 @@ namespace sockets {
6779
std::vector<uint8_t> recvBuffer(size);
6880
struct sockaddr_in addr;
6981
socklen_t addrFromLength = sizeof(addr);
70-
auto len = recvfrom(_sockedFd, recvBuffer.data(), recvBuffer.size(), 0, (struct sockaddr*)&addr, &addrFromLength);
82+
auto len = recvfrom(_sockedFd, (char*)recvBuffer.data(), recvBuffer.size(), 0, (struct sockaddr*)&addr, &addrFromLength);
7183
if (len < 0) {
7284
throw std::system_error(errno, std::generic_category());
7385
}

src/sockets/UDPSocket.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <vector>
99
#include <chrono>
1010
#include <memory>
11-
#include <netinet/in.h>
1211
#include "BaseSocket.h"
1312

1413
namespace eipScanner {

0 commit comments

Comments
 (0)