Skip to content

Commit 9320251

Browse files
JKRhbboaks
authored andcommitted
Add Windows support
1 parent 86f4988 commit 9320251

17 files changed

Lines changed: 210 additions & 25 deletions

File tree

.github/workflows/main.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,15 @@ jobs:
6262
run: |
6363
cmake -Dmake_tests=ON .
6464
make
65+
66+
build-windows:
67+
name: Build for Windows using CMake
68+
runs-on: windows-latest
69+
70+
steps:
71+
- uses: actions/checkout@v3
72+
- name: Build tinydtls
73+
run: |
74+
cmake -G "Unix Makefiles" .
75+
make
76+

.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ dtls_config.h
5151
*.cooja
5252
*.econotag
5353
*.eval-adf7xxxmb4z
54+
*.exe
5455
*.exp5438
5556
*.iris
5657
*.mbxxx
@@ -70,3 +71,23 @@ dtls_config.h
7071
CMakeCache.txt
7172
cmake_install.cmake
7273
CMakeFiles
74+
75+
# Windows build files
76+
*.dll
77+
78+
# Visual Studio Build Files
79+
*.VC.db*
80+
.vs
81+
*.obj
82+
*.tlog
83+
*.log
84+
*.recipe
85+
*.vcxproj
86+
*.sln
87+
*.pdb
88+
*.vcxproj.user
89+
*.vcxproj.filters
90+
*.vcxproj.FileListAbsolute.txt
91+
tinydtls.dir
92+
Debug
93+
Release

CMakeLists.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ option(make_tests "Make test programs and examples" OFF)
3131
if(NOT PLATFORM)
3232
# PLATFORM seems to be not used
3333
set(PLATFORM "posix" CACHE STRING "Choose platform." FORCE)
34-
set_property(CACHE PLATFORM PROPERTY STRINGS "contiki" "espidf" "posix" "riot" "zephyr")
34+
set_property(CACHE PLATFORM PROPERTY STRINGS "contiki" "espidf" "posix" "riot" "zephyr" "windows")
3535
endif()
3636

3737
set(PACKAGE_NAME "tinydtls")
@@ -64,8 +64,11 @@ target_sources(tinydtls PRIVATE
6464
target_include_directories(tinydtls PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
6565
target_compile_definitions(tinydtls PUBLIC DTLSv12 WITH_SHA256 SHA2_USE_INTTYPES_H DTLS_CHECK_CONTENTTYPE)
6666

67-
if(NOT ZEPHYR_BASE)
68-
target_compile_options(tinydtls PRIVATE -fPIC -pedantic -std=c99 -Wall -Wextra -Wformat-security -Winline -Wmissing-declarations -Wmissing-prototypes -Wnested-externs -Wpointer-arith -Wshadow -Wstrict-prototypes -Wswitch-default -Wswitch-enum -Wunused)
67+
if(CMAKE_GENERATOR MATCHES "Visual Studio")
68+
option(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS "Export all symbols when compiling to a .dll" ON)
69+
target_compile_options(tinydtls PRIVATE -Wall)
70+
elseif(NOT ZEPHYR_BASE)
71+
target_compile_options(tinydtls PRIVATE -fPIC -pedantic -std=c99 -Wall -Wextra -Wformat-security -Winline -Wmissing-declarations -Wmissing-prototypes -Wnested-externs -Wpointer-arith -Wshadow -Wstrict-prototypes -Wswitch-default -Wswitch-enum -Wunused)
6972
endif()
7073

7174
set_target_properties(tinydtls PROPERTIES VERSION ${PACKAGE_VERSION} SOVERSION ${SOVERSION})

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ C99. The following components are available:
1818

1919
# BUILDING
2020

21+
tinydtls supports multiple platforms, including both Real-time
22+
and general-purpose Operating Systems. Below you find build instructions for
23+
all supported environments.
24+
25+
## POSIX-oriented Operating Systems
26+
2127
When using the code from the git
2228
[repository](https://github.com/eclipse/tinydtls) at GitHub, invoke
2329

@@ -68,6 +74,39 @@ Available options:
6874
| DTLS_ECC | enable/disable ECDHE_ECDSA cipher suites | ON |
6975
| DTLS_PSK | enable/disable PSK cipher suites | ON |
7076

77+
## Windows
78+
79+
Using CMake, you can also build on and for Windows using either GCC or Visual
80+
Studio.
81+
Note, however, that the `make_tests` option is currently not supported when
82+
compiling with Visual Studio, as parts of the tests rely on POSIX APIs.
83+
84+
For Visual Studio, you can apply the CMake instructions outlined above from the
85+
command line or use the CMake GUI application.
86+
87+
In order to be able to use GCC, you need to specify a different generator than
88+
the default.
89+
For instance, you can use the `Unix Makefiles` generator, which creates a
90+
Makefile for controlling the build process using GCC.
91+
The example below leads to the output of a shared library file
92+
`libtinydtls.dll`.
93+
94+
```
95+
cmake -G "Unix Makefiles" -DBUILD_SHARED_LIBS=ON .
96+
make
97+
```
98+
99+
Using MinGW64, you can also cross compile from a POSIX-oriented
100+
platform for Windows using Autotools by providing a corresponding `--host`
101+
argument:
102+
103+
```
104+
./autogen.sh
105+
./configure --host x86_64-w64-mingw32
106+
make
107+
mv libtinydtls.so libtinydtls.dll # Apply Windows file extension
108+
```
109+
71110
# License
72111

73112
Copyright (c) 2011–2022 Olaf Bergmann (TZI) and others.

dtls.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ static inline void free_context(dtls_context_t *context) {
213213
}
214214
#endif /* RIOT_VERSION */
215215

216-
#ifdef WITH_POSIX
216+
#if defined(WITH_POSIX) || defined(IS_WINDOWS)
217217

218218
static inline dtls_context_t *
219219
malloc_context(void) {
@@ -1610,9 +1610,11 @@ dtls_0_send_hello_verify_request(dtls_context_t *ctx,
16101610
dtls_ephemeral_peer_t *ephemeral_peer,
16111611
uint8 *data, size_t data_length)
16121612
{
1613-
uint8 buf[DTLS_RH_LENGTH + DTLS_HS_LENGTH + data_length];
1613+
uint8 buf[DTLS_RH_LENGTH + DTLS_HS_LENGTH + DTLS_HV_LENGTH + DTLS_COOKIE_LENGTH];
16141614
uint8 *p = dtls_set_record_header(DTLS_CT_HANDSHAKE, 0, &(ephemeral_peer->rseq), buf);
16151615

1616+
assert(data_length == DTLS_HV_LENGTH + DTLS_COOKIE_LENGTH);
1617+
16161618
/* Signal DTLS version 1.0 in the record layer of ClientHello and
16171619
* HelloVerifyRequest handshake messages according to Section 4.2.1
16181620
* of RFC 6347.

dtls.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,16 @@ void dtls_check_retransmit(dtls_context_t *context, clock_time_t *next);
341341
#define DTLS_CT_HANDSHAKE 22
342342
#define DTLS_CT_APPLICATION_DATA 23
343343

344+
#ifdef __GNUC__
345+
#define PACK( __Declaration__ ) __Declaration__ __attribute__((__packed__))
346+
#elif defined(_MSC_VER)
347+
#define PACK( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop))
348+
#else
349+
#error "Structure packing is not available for the used compiler."
350+
#endif
351+
344352
/** Generic header structure of the DTLS record layer. */
345-
typedef struct __attribute__((__packed__)) {
353+
PACK(typedef struct) {
346354
uint8 content_type; /**< content type of the included message */
347355
uint16 version; /**< Protocol version */
348356
uint16 epoch; /**< counter for cipher state changes */
@@ -371,7 +379,7 @@ typedef struct __attribute__((__packed__)) {
371379
#define DTLS_HT_NO_OPTIONAL_MESSAGE -1
372380

373381
/** Header structure for the DTLS handshake protocol. */
374-
typedef struct __attribute__((__packed__)) {
382+
PACK(typedef struct) {
375383
uint8 msg_type; /**< Type of handshake message (one of DTLS_HT_) */
376384
uint24 length; /**< length of this message */
377385
uint16 message_seq; /**< Message sequence number */
@@ -381,7 +389,7 @@ typedef struct __attribute__((__packed__)) {
381389
} dtls_handshake_header_t;
382390

383391
/** Structure of the Client Hello message. */
384-
typedef struct __attribute__((__packed__)) {
392+
PACK(typedef struct) {
385393
uint16 version; /**< Client version */
386394
uint32 gmt_random; /**< GMT time of the random byte creation */
387395
unsigned char random[28]; /**< Client random bytes */
@@ -392,7 +400,7 @@ typedef struct __attribute__((__packed__)) {
392400
} dtls_client_hello_t;
393401

394402
/** Structure of the Hello Verify Request. */
395-
typedef struct __attribute__((__packed__)) {
403+
PACK(typedef struct) {
396404
uint16 version; /**< Server version */
397405
uint8 cookie_length; /**< Length of the included cookie */
398406
uint8 cookie[]; /**< up to 32 bytes making up the cookie */

dtls_debug.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,13 @@ dsrv_print_addr(const session_t *addr, char *buf, size_t len) {
222222

223223
#endif /* WITH_CONTIKI */
224224

225-
#ifdef RIOT_VERSION
225+
#if defined(RIOT_VERSION) || defined(IS_WINDOWS)
226226
/* FIXME: Switch to RIOT own DEBUG lines */
227+
/* TODO: Check if inet_ntop can be used on Windows */
227228
(void) addr;
228229
(void) buf;
229230
(void) len;
230-
#endif /* RIOT_VERSION */
231+
#endif /* RIOT_VERSION || IS_WINDOWS */
231232

232233
#ifdef WITH_POSIX
233234
/* TODO: output addresses manually */
@@ -240,7 +241,7 @@ dsrv_print_addr(const session_t *addr, char *buf, size_t len) {
240241

241242
#endif /* NDEBUG */
242243

243-
#ifndef WITH_CONTIKI
244+
#if !defined(WITH_CONTIKI) && !defined(_MSC_VER)
244245
void
245246
dsrv_log(log_t level, const char *format, ...) {
246247
static char timebuf[32];

dtls_debug.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ void dtls_set_log_level(log_t level);
7878
* level is below or equal to the log level that set by
7979
* set_log_level(). */
8080
#ifdef HAVE_VPRINTF
81-
#if (defined(__GNUC__))
81+
#if (defined(__GNUC__) && !defined(__MINGW32__))
8282
void dsrv_log(log_t level, const char *format, ...) __attribute__ ((format(printf, 2, 3)));
83-
#else /* !__GNUC__ */
83+
#else /* !__GNUC__ && !__MINGW32__ */
8484
void dsrv_log(log_t level, const char *format, ...);
85-
#endif /* !__GNUC__ */
85+
#endif /* !__GNUC__ && !__MINGW32__ */
8686
#else
8787
#define dsrv_log(level, format, ...) PRINTF(format, ##__VA_ARGS__)
8888
#endif

dtls_mutex.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,20 @@ typedef int dtls_mutex_t;
4242
#define dtls_mutex_trylock(a) *(a) = 1
4343
#define dtls_mutex_unlock(a) *(a) = 0
4444

45-
#elif defined(WITH_ZEPHYR)
45+
#elif defined(WITH_ZEPHYR) || defined(IS_WINDOWS)
4646

4747
/* zephyr supports mutex, but this port doesn't use it */
4848

49+
// TODO: Add Windows compatible mutex definitions
50+
4951
typedef int dtls_mutex_t;
5052
#define DTLS_MUTEX_INITIALIZER 0
5153
#define dtls_mutex_lock(a) *(a) = 1
5254
#define dtls_mutex_trylock(a) *(a) = 1
5355
#define dtls_mutex_unlock(a) *(a) = 0
5456

5557

56-
#else /* ! RIOT_VERSION && ! WITH_CONTIKI && ! WITH_ZEPHYR */
58+
#else /* ! RIOT_VERSION && ! WITH_CONTIKI && ! WITH_ZEPHYR && ! IS_WINDOWS */
5759

5860
#include <pthread.h>
5961

@@ -63,6 +65,6 @@ typedef pthread_mutex_t dtls_mutex_t;
6365
#define dtls_mutex_trylock(a) pthread_mutex_trylock(a)
6466
#define dtls_mutex_unlock(a) pthread_mutex_unlock(a)
6567

66-
#endif /* ! RIOT_VERSION && ! WITH_CONTIKI */
68+
#endif /* ! RIOT_VERSION && ! WITH_CONTIKI && ! IS_WINDOWS */
6769

6870
#endif /* _DTLS_MUTEX_H_ */

dtls_prng.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#elif defined (WITH_ZEPHYR)
3131
#include "platform-specific/dtls_prng_zephyr.c"
3232

33+
#elif defined (IS_WINDOWS)
34+
#include "platform-specific/dtls_prng_win.c"
35+
3336
#elif defined (WITH_POSIX)
3437
#include "platform-specific/dtls_prng_posix.c"
3538

0 commit comments

Comments
 (0)