Skip to content

Commit bda6c26

Browse files
committed
Added UNIX-domain sockets to Windows and made them a CMake option for all platforms.
1 parent 96711b6 commit bda6c26

File tree

12 files changed

+101
-41
lines changed

12 files changed

+101
-41
lines changed

CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,15 @@ option(SOCKPP_BUILD_TESTS "Build unit tests" OFF)
5353
option(SOCKPP_BUILD_DOCUMENTATION "Create Doxygen reference documentation" OFF)
5454
option(SOCKPP_WITH_OPENSSL "TLS Secure Sockets with OpenSSL" OFF)
5555
option(SOCKPP_WITH_MBEDTLS "TLS Secure Sockets with Mbed TLS" OFF)
56-
option(SOCKPP_WITH_CAN "Include support for Linux SocketCAN components" OFF)
56+
57+
if(NOT WIN32)
58+
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
59+
option(SOCKPP_WITH_CAN "Include support for Linux SocketCAN components" OFF)
60+
endif()
61+
option(SOCKPP_WITH_UNIX_SOCKETS "Include support for UNIX-domain sockets" ON)
62+
else()
63+
option(SOCKPP_WITH_UNIX_SOCKETS "Include support for UNIX-domain sockets" OFF)
64+
endif()
5765

5866
# ----- Find any dependencies -----
5967

examples/CMakeLists.txt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,25 @@
3636
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3737
# ---------------------------------------------------------------------------
3838

39+
# --- General TCP/IP examples ---
40+
3941
add_subdirectory(tcp)
4042
add_subdirectory(udp)
4143

42-
if(UNIX)
44+
# --- Optional UNIX-domain sockets (even on Windows!) ---
45+
46+
if(SOCKPP_WITH_UNIX_SOCKETS)
4347
add_subdirectory(unix)
44-
if(SOCKPP_BUILD_CAN)
45-
add_subdirectory(linux)
46-
endif()
4748
endif()
4849

50+
# --- Anything that is Linux-specific ---
51+
52+
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
53+
add_subdirectory(linux)
54+
endif()
55+
56+
# --- Optional secure sockets ---
57+
4958
if(SOCKPP_WITH_TLS)
5059
add_subdirectory(tls)
5160
endif()

examples/linux/CMakeLists.txt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,22 @@
3737
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3838
# ---------------------------------------------------------------------------
3939

40-
# --- Executables ---
40+
# --- Optional Linux SocketCAN support ---
4141

42-
set(EXECUTABLES
43-
cantime
44-
canrecv
45-
)
42+
if(SOCKPP_WITH_CAN)
43+
# --- Executables ---
4644

47-
foreach(EXECUTABLE ${EXECUTABLES})
48-
add_executable(${EXECUTABLE} ${EXECUTABLE}.cpp)
49-
target_link_libraries(${EXECUTABLE} Sockpp::sockpp)
50-
endforeach()
45+
set(EXECUTABLES
46+
cantime
47+
canrecv
48+
)
5149

52-
# --- Install examples ---
50+
foreach(EXECUTABLE ${EXECUTABLES})
51+
add_executable(${EXECUTABLE} ${EXECUTABLE}.cpp)
52+
target_link_libraries(${EXECUTABLE} Sockpp::sockpp)
53+
endforeach()
5354

54-
install(TARGETS ${EXECUTABLES} RUNTIME DESTINATION bin)
55+
# --- Install examples ---
5556

57+
install(TARGETS ${EXECUTABLES} RUNTIME DESTINATION bin)
58+
endif()

examples/unix/CMakeLists.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3838
# ---------------------------------------------------------------------------
3939

40+
# Remember that Windows doesn't support datagram over UNIX sockets
41+
4042
# --- For apps that use threads ---
4143

4244
set(THREADS_PREFER_PTHREAD_FLAG ON)
@@ -46,16 +48,22 @@ find_package(Threads REQUIRED)
4648

4749
set(THREADED_EXECUTABLES
4850
unechosvr
49-
undgramechosvr
5051
)
5152

53+
if(NOT WIN32)
54+
list(APPEND THREADED_EXECUTABLES undgramechosvr)
55+
endif()
56+
5257
set(EXECUTABLES
5358
unecho
5459
unechotest
55-
undgramecho
5660
${THREADED_EXECUTABLES}
5761
)
5862

63+
if(NOT WIN32)
64+
list(APPEND EXECUTABLES undgramecho)
65+
endif()
66+
5967
# --- Executables ---
6068

6169
foreach(EXECUTABLE ${EXECUTABLES})

examples/unix/unecho.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ int main(int argc, char* argv[]) {
5050
cout << "Sample Unix-domain echo client for 'sockpp' " << sockpp::SOCKPP_VERSION << '\n'
5151
<< endl;
5252

53-
string path = (argc > 1) ? argv[1] : "/tmp/unechosvr.sock";
53+
#if defined(_WIN32)
54+
const string DFLT_PATH = "C:\\TEMP\\unechosvr.sock"s;
55+
#else
56+
string DFLT_PATH = "/tmp/unechosvr.sock"s;
57+
#endif
58+
59+
const string path = (argc > 1) ? argv[1] : DFLT_PATH;
5460

5561
sockpp::initialize();
5662
sockpp::unix_connector conn;

examples/unix/unechosvr.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@
4040
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4141
// --------------------------------------------------------------------------
4242

43-
#include <iostream>
44-
#include <thread>
45-
4643
#include "sockpp/unix_acceptor.h"
4744
#include "sockpp/version.h"
4845

46+
#include <iostream>
47+
#include <thread>
48+
4949
using namespace std;
5050

5151
// --------------------------------------------------------------------------
@@ -72,11 +72,14 @@ int main(int argc, char* argv[]) {
7272
cout << "Sample Unix-domain echo server for 'sockpp' " << sockpp::SOCKPP_VERSION << '\n'
7373
<< endl;
7474

75-
string path = "/tmp/unechosvr.sock";
7675

77-
if (argc > 1) {
78-
path = argv[1];
79-
}
76+
#if defined(_WIN32)
77+
const string DFLT_PATH = "C:\\TEMP\\unechosvr.sock"s;
78+
#else
79+
string DFLT_PATH = "/tmp/unechosvr.sock"s;
80+
#endif
81+
82+
const string path = (argc > 1) ? argv[1] : DFLT_PATH;
8083

8184
sockpp::initialize();
8285
sockpp::unix_acceptor acc;

examples/unix/unechotest.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ int main(int argc, char* argv[]) {
5959
<< '\n'
6060
<< endl;
6161

62-
string path = (argc > 1) ? argv[1] : "/tmp/unechosvr.sock";
62+
#if defined(_WIN32)
63+
const string DFLT_PATH = "C:\\TEMP\\unechosvr.sock"s;
64+
#else
65+
string DFLT_PATH = "/tmp/unechosvr.sock"s;
66+
#endif
67+
68+
const string path = (argc > 1) ? argv[1] : DFLT_PATH;
6369
size_t n = (argc > 2) ? size_t(atoll(argv[2])) : DFLT_N;
6470
size_t sz = (argc > 3) ? size_t(atoll(argv[3])) : DFLT_SZ;
6571

include/sockpp/platform.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,14 @@
6161
#define _CRT_SECURE_NO_DEPRECATE
6262
#endif
6363

64+
#undef UNICODE
65+
6466
#include <winsock2.h>
6567
#include <ws2tcpip.h>
6668

69+
#undef max
70+
#undef min
71+
6772
#define SOCKPP_SOCKET_T_DEFINED
6873
using socket_t = SOCKET;
6974

include/sockpp/unix_address.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,20 @@
4646
#ifndef __sockpp_unix_addr_h
4747
#define __sockpp_unix_addr_h
4848

49-
#include <sys/un.h>
49+
#include "sockpp/platform.h"
50+
#include "sockpp/result.h"
51+
#include "sockpp/sock_address.h"
52+
#include "sockpp/types.h"
5053

5154
#include <cstring>
5255
#include <iostream>
5356
#include <string>
5457

55-
#include "sockpp/platform.h"
56-
#include "sockpp/result.h"
57-
#include "sockpp/sock_address.h"
58-
#include "sockpp/types.h"
58+
#if defined(_WIN32)
59+
#include <afunix.h>
60+
#else
61+
#include <sys/un.h>
62+
#endif
5963

6064
namespace sockpp {
6165

include/sockpp/unix_dgram_socket.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
#ifndef __sockpp_unix_dgram_socket_h
4848
#define __sockpp_unix_dgram_socket_h
4949

50+
#if defined(_WIN32)
51+
#error "UNIX datagram sockets not supported on Windows"
52+
#endif
53+
5054
#include "sockpp/datagram_socket.h"
5155
#include "sockpp/unix_address.h"
5256

src/CMakeLists.txt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,17 @@ set(SOURCES
5353

5454
# Additional sources (platform & config)
5555

56-
if(UNIX)
56+
if(SOCKPP_WITH_UNIX_SOCKETS)
5757
list(APPEND SOURCES
5858
${CMAKE_CURRENT_SOURCE_DIR}/unix/unix_address.cpp
5959
)
60-
if(SOCKPP_WITH_CAN)
61-
list(APPEND SOURCES
62-
${CMAKE_CURRENT_SOURCE_DIR}/linux/can_address.cpp
63-
${CMAKE_CURRENT_SOURCE_DIR}/linux/can_socket.cpp
64-
)
65-
endif()
60+
endif()
61+
62+
if(SOCKPP_WITH_CAN)
63+
list(APPEND SOURCES
64+
${CMAKE_CURRENT_SOURCE_DIR}/linux/can_address.cpp
65+
${CMAKE_CURRENT_SOURCE_DIR}/linux/can_socket.cpp
66+
)
6667
endif()
6768

6869
if(SOCKPP_WITH_OPENSSL)

src/unix/unix_address.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
#include "sockpp/unix_address.h"
3838

39+
#include <algorithm>
3940
#include <cstring>
4041
#include <stdexcept>
4142

@@ -56,7 +57,8 @@ unix_address::unix_address(const string& path) {
5657

5758
addr_.sun_family = ADDRESS_FAMILY;
5859
// Remember, if len==MAX, there's no NUL terminator
59-
::strncpy(addr_.sun_path, path.c_str(), MAX_PATH_NAME);
60+
const size_t n = std::min(path.length()+1, MAX_PATH_NAME);
61+
std::memcpy(addr_.sun_path, path.c_str(), n);
6062
}
6163

6264
unix_address::unix_address(const string& path, error_code& ec) noexcept {
@@ -67,7 +69,8 @@ unix_address::unix_address(const string& path, error_code& ec) noexcept {
6769
ec = error_code{};
6870
addr_.sun_family = ADDRESS_FAMILY;
6971
// Remember, if len==MAX, there's no NUL terminator
70-
::strncpy(addr_.sun_path, path.c_str(), MAX_PATH_NAME);
72+
const size_t n = std::min(path.length()+1, MAX_PATH_NAME);
73+
std::memcpy(addr_.sun_path, path.c_str(), n);
7174
}
7275
}
7376

0 commit comments

Comments
 (0)