Skip to content

Commit

Permalink
MinGW Support (32bit & 64bit) (#321)
Browse files Browse the repository at this point in the history
* Added support for mingw64 (gcc 6.3.0)

* Fixed mingw32/64 issue

* be more verbose (#312)

* Improve and of ICMP messages on Windows.

When an ICMP message is received on Windows 10 for a UDP socket,
WSAECONNRESET is reported as an error. In this case, just read
again.

Thanks to nxrighthere for reporting the issue and helping to
nail it down.

This fixes #309.

* Fix broken links, Make URLs' protocols consistent (#315)

Two Links missed leading `http(s)://` and were broken.
All links to `tools.ietf.org` now consistently use https.

* Backport https://svnweb.freebsd.org/base?view=revision&revision=340783

* Improve input validation for the IPPROTO_SCTP level socket options
SCTP_CONNECT_X and SCTP_CONNECT_X_DELAYED.

* Allow sending on demand SCTP HEARTBEATS only in the ESTABLISHED state.

* Fix cross-build linux->mingw (#320)

* Fix compile error due to merge mistake.

* test commit

* Add MinGW and MinGW64 support

* MinGW support, polishing

* Sanitize non C-style comments

* Revert uint32_t casting for htonl

* Ignore wformat errors on GCC + MinGW platforms
  • Loading branch information
weinrank authored and tuexen committed Jun 28, 2019
1 parent b78601e commit 37a8a0a
Show file tree
Hide file tree
Showing 22 changed files with 89 additions and 63 deletions.
11 changes: 11 additions & 0 deletions programs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ if (CMAKE_SYSTEM_NAME MATCHES "Darwin")
add_definitions(-D__APPLE_USE_RFC_2292)
endif ()

if (MSYS)
message(STATUS "MSYS")
add_definitions(-D__USE_MINGW_ANSI_STDIO)

if (CMAKE_C_COMPILER_ID MATCHES "GNU")
message(STATUS "MYSYS + GCC")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-format")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-format")
endif ()
endif ()


#################################################
# MISC
Expand Down
24 changes: 12 additions & 12 deletions programs/client_upcall.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@
#if !defined(_WIN32)
#include <unistd.h>
#include <sys/time.h>
#endif // !defined(_WIN32)
#endif /* !defined(_WIN32) */

#include <sys/types.h>

#if !defined(_WIN32)
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#else // !defined(_WIN32)
#else /* !defined(_WIN32) */
#include <io.h>
#endif

Expand All @@ -66,17 +66,17 @@ typedef char* caddr_t;

int inputAvailable(void)
{
struct timeval tv;
fd_set fds;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&fds);
#ifndef _WIN32
FD_SET(STDIN_FILENO, &fds);
select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
struct timeval tv;
fd_set fds;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&fds);
#if defined(_WIN32) && !defined(__MINGW32__)
FD_SET(_fileno(stdin), &fds);
select(_fileno(stdin) + 1, &fds, NULL, NULL, &tv);
#else
FD_SET(_fileno(stdin), &fds);
select(_fileno(stdin) + 1, &fds, NULL, NULL, &tv);
FD_SET(STDIN_FILENO, &fds);
select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
#endif
return (FD_ISSET(0, &fds));
}
Expand Down
6 changes: 3 additions & 3 deletions programs/ekr_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ handle_packets(void *arg)
length = recv(*fdp, buf, MAX_PACKET_SIZE, 0);
if (length > 0) {
if ((dump_buf = usrsctp_dumppacket(buf, (size_t)length, SCTP_DUMP_INBOUND)) != NULL) {
//fprintf(stderr, "%s", dump_buf);
/* fprintf(stderr, "%s", dump_buf); */
usrsctp_freedumpbuffer(dump_buf);
}
usrsctp_conninput(fdp, buf, (size_t)length, 0);
Expand Down Expand Up @@ -111,7 +111,7 @@ conn_output(void *addr, void *buf, size_t length, uint8_t tos, uint8_t set_df)
fdp = (int *)addr;
#endif
if ((dump_buf = usrsctp_dumppacket(buf, length, SCTP_DUMP_OUTBOUND)) != NULL) {
//fprintf(stderr, "%s", dump_buf);
/* fprintf(stderr, "%s", dump_buf); */
usrsctp_freedumpbuffer(dump_buf);
}
#ifdef _WIN32
Expand Down Expand Up @@ -476,7 +476,7 @@ main(void)
}
memset(line, 'A', LINE_LENGTH);
sndinfo.snd_sid = 1;
sndinfo.snd_flags = 0; //SCTP_UNORDERED;
sndinfo.snd_flags = 0; /* SCTP_UNORDERED */
sndinfo.snd_ppid = htonl(DISCARD_PPID);
sndinfo.snd_context = 0;
sndinfo.snd_assoc_id = 0;
Expand Down
4 changes: 2 additions & 2 deletions programs/ekr_loop_offload.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ handle_packets(void *arg)
length = recv(*fdp, buffer, MAX_PACKET_SIZE, 0);
if (length > 0) {
if ((dump_buffer = usrsctp_dumppacket(buffer, (size_t)length, SCTP_DUMP_INBOUND)) != NULL) {
//fprintf(stderr, "%s", dump_buffer);
/* fprintf(stderr, "%s", dump_buffer); */
usrsctp_freedumpbuffer(dump_buffer);
}
if ((size_t)length >= sizeof(struct sctp_common_header)) {
Expand Down Expand Up @@ -132,7 +132,7 @@ conn_output(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df
hdr->crc32c = usrsctp_crc32c(buffer, (size_t)length);
}
if ((dump_buffer = usrsctp_dumppacket(buffer, length, SCTP_DUMP_OUTBOUND)) != NULL) {
//fprintf(stderr, "%s", dump_buffer);
/* fprintf(stderr, "%s", dump_buffer); */
usrsctp_freedumpbuffer(dump_buffer);
}
#ifdef _WIN32
Expand Down
7 changes: 3 additions & 4 deletions programs/ekr_loop_upcall.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
#define MAX_PACKET_SIZE (1<<16)
#define LINE_LENGTH (1<<20)
#define DISCARD_PPID 39
//#define DUMP_PKTS_TO_FILE
/* #define DUMP_PKTS_TO_FILE */

#ifdef _WIN32
static DWORD WINAPI
Expand Down Expand Up @@ -84,7 +84,7 @@ handle_packets(void *arg)
length = recv(*fdp, buf, MAX_PACKET_SIZE, 0);
if (length > 0) {
if ((dump_buf = usrsctp_dumppacket(buf, (size_t)length, SCTP_DUMP_INBOUND)) != NULL) {
//fprintf(stderr, "%s", dump_buf);
/* fprintf(stderr, "%s", dump_buf); */
usrsctp_freedumpbuffer(dump_buf);
}
usrsctp_conninput(fdp, buf, (size_t)length, 0);
Expand Down Expand Up @@ -113,7 +113,7 @@ conn_output(void *addr, void *buf, size_t length, uint8_t tos, uint8_t set_df)
fdp = (int *)addr;
#endif
if ((dump_buf = usrsctp_dumppacket(buf, length, SCTP_DUMP_OUTBOUND)) != NULL) {
//fprintf(stderr, "%s", dump_buf);
/* fprintf(stderr, "%s", dump_buf); */
usrsctp_freedumpbuffer(dump_buf);
}

Expand Down Expand Up @@ -150,7 +150,6 @@ handle_upcall(struct socket *sock, void *data, int flgs)
while ((events = usrsctp_get_events(sock)) && (events & SCTP_EVENT_READ)) {
struct sctp_recvv_rn rn;
ssize_t n;
//struct sockaddr_storage addr;
union sctp_sockstore addr;
int flags = 0;
socklen_t len = (socklen_t)sizeof(addr);
Expand Down
2 changes: 1 addition & 1 deletion programs/ekr_peer.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ main(int argc, char *argv[])
perror("usrsctp_connect");
}
for (;;) {
#ifdef _WIN32
#if defined(_WIN32) && !defined(__MINGW32__)
if (gets_s(line, LINE_LENGTH) == NULL) {
#else
if (fgets(line, LINE_LENGTH, stdin) == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion programs/http_client_upcall.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ main(int argc, char *argv[])
goto out;
}

//usrsctp_set_non_blocking(sock, 1);
/* usrsctp_set_non_blocking(sock, 1); */

rtoinfo.srto_assoc_id = 0;
rtoinfo.srto_initial = 1000;
Expand Down
2 changes: 1 addition & 1 deletion programs/programs_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ void handle_notification(union sctp_notification *notif, size_t n);
} while (0)
#endif

#endif // __PROGRAMS_HELPER_H__
#endif /* __PROGRAMS_HELPER_H__ */
10 changes: 5 additions & 5 deletions programs/rtcweb.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ struct peer_connection {
#define SCTP_PACKED
#endif

#if defined(_WIN32)
#if defined(_WIN32) && !defined(__MINGW32__)
#pragma warning( push )
#pragma warning( disable : 4200 )
#endif //defined(_WIN32)
#endif /* defined(_WIN32) && !defined(__MINGW32__) */
struct rtcweb_datachannel_open_request {
uint8_t msg_type; /* DATA_CHANNEL_OPEN_REQUEST */
uint8_t channel_type;
Expand All @@ -132,9 +132,9 @@ struct rtcweb_datachannel_open_request {
int16_t priority;
char label[];
} SCTP_PACKED;
#if defined(_WIN32)
#if defined(_WIN32) && !defined(__MINGW32__)
#pragma warning( pop )
#endif //defined(_WIN32)
#endif /* defined(_WIN32) && !defined(__MINGW32__) */

struct rtcweb_datachannel_open_response {
uint8_t msg_type; /* DATA_CHANNEL_OPEN_RESPONSE */
Expand Down Expand Up @@ -1432,7 +1432,7 @@ main(int argc, char *argv[])
unlock_peer_connection(&peer_connection);

for (;;) {
#ifdef _WIN32
#if defined(_WIN32) && !defined(__MINGW32__)
if (gets_s(line, LINE_LENGTH) == NULL) {
#else
if (fgets(line, LINE_LENGTH, stdin) == NULL) {
Expand Down
19 changes: 8 additions & 11 deletions programs/tsctp.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,8 @@ handle_connection(void *arg)
{
ssize_t n;
char *buf;
#ifdef _WIN32
HANDLE tid;
#else

#if !defined(_WIN32)
pthread_t tid;
#endif
struct socket *conn_sock;
Expand All @@ -151,9 +150,8 @@ handle_connection(void *arg)
unsigned long long sum = 0;

conn_sock = *(struct socket **)arg;
#ifdef _WIN32
tid = GetCurrentThread();
#else

#if !defined(_WIN32)
tid = pthread_self();
pthread_detach(tid);
#endif
Expand Down Expand Up @@ -349,7 +347,6 @@ int main(int argc, char **argv)
unsigned long messages = 0;
#ifdef _WIN32
unsigned long srcAddr;
HANDLE tid;
#else
in_addr_t srcAddr;
pthread_t tid;
Expand Down Expand Up @@ -648,19 +645,19 @@ int main(int argc, char **argv)
continue;
}
#ifdef _WIN32
tid = CreateThread(NULL, 0, &handle_connection, (void *)conn_sock, 0, NULL);
CreateThread(NULL, 0, &handle_connection, (void *)conn_sock, 0, NULL);
#else
pthread_create(&tid, NULL, &handle_connection, (void *)conn_sock);
#endif
}
if (verbose) {
// const char *inet_ntop(int af, const void *src, char *dst, socklen_t size)
//inet_ntoa(remote_addr.sin_addr)
/* const char *inet_ntop(int af, const void *src, char *dst, socklen_t size)
inet_ntoa(remote_addr.sin_addr) */
char addrbuf[INET_ADDRSTRLEN];
printf("Connection accepted from %s:%d\n", inet_ntop(AF_INET, &(remote_addr.sin_addr), addrbuf, INET_ADDRSTRLEN), ntohs(remote_addr.sin_port));
}
}
//usrsctp_close(psock); // unreachable
/* usrsctp_close(psock); unreachable */
} else {
memset(&encaps, 0, sizeof(struct sctp_udpencaps));
encaps.sue_address.ss_family = AF_INET;
Expand Down
4 changes: 2 additions & 2 deletions usrsctplib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ add_library(usrsctp-static STATIC ${usrsctp_sources} ${usrsctp_headers})

if (WIN32)
message(STATUS "link library: ws2_32")
target_link_libraries(usrsctp ws2_32)
target_link_libraries(usrsctp-static ws2_32)
target_link_libraries(usrsctp ws2_32 Iphlpapi.lib)
target_link_libraries(usrsctp-static ws2_32 Iphlpapi.lib)
endif ()

set_target_properties(usrsctp-static PROPERTIES OUTPUT_NAME "usrsctp")
Expand Down
6 changes: 3 additions & 3 deletions usrsctplib/netinet/sctp_cc_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2410,7 +2410,7 @@ sctp_htcp_cwnd_update_after_ecn_echo(struct sctp_tcb *stcb,

const struct sctp_cc_functions sctp_cc_functions[] = {
{
#if defined(__Windows__) || defined(__Userspace_os_Windows)
#if (defined(__Windows__) || defined(__Userspace_os_Windows)) && !defined(__MINGW32__)
sctp_set_initial_cc_param,
sctp_cwnd_update_after_sack,
sctp_cwnd_update_exit_pf_common,
Expand All @@ -2431,7 +2431,7 @@ const struct sctp_cc_functions sctp_cc_functions[] = {
#endif
},
{
#if defined(__Windows__) || defined(__Userspace_os_Windows)
#if (defined(__Windows__) || defined(__Userspace_os_Windows)) && !defined(__MINGW32__)
sctp_set_initial_cc_param,
sctp_hs_cwnd_update_after_sack,
sctp_cwnd_update_exit_pf_common,
Expand All @@ -2452,7 +2452,7 @@ const struct sctp_cc_functions sctp_cc_functions[] = {
#endif
},
{
#if defined(__Windows__) || defined(__Userspace_os_Windows)
#if (defined(__Windows__) || defined(__Userspace_os_Windows)) && !defined(__MINGW32__)
sctp_htcp_set_initial_cc_param,
sctp_htcp_cwnd_update_after_sack,
sctp_cwnd_update_exit_pf_common,
Expand Down
5 changes: 3 additions & 2 deletions usrsctplib/netinet/sctp_os_userspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ typedef DWORD userland_thread_id_t;
#define n_time unsigned __int32
#define sa_family_t unsigned __int8
#define ssize_t __int64
#if !defined(__MINGW32__)
#define __func__ __FUNCTION__

#endif
#ifndef EWOULDBLOCK
#define EWOULDBLOCK WSAEWOULDBLOCK
#endif
Expand Down Expand Up @@ -221,7 +222,7 @@ typedef char* caddr_t;

#define bzero(buf, len) memset(buf, 0, len)
#define bcopy(srcKey, dstKey, len) memcpy(dstKey, srcKey, len)
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#if defined(_MSC_VER) && (_MSC_VER < 1900) && !defined(__MINGW32__)
#define snprintf(data, size, format, ...) _snprintf_s(data, size, _TRUNCATE, format, __VA_ARGS__)
#endif
#define inline __inline
Expand Down
2 changes: 2 additions & 0 deletions usrsctplib/netinet/sctp_pcb.c
Original file line number Diff line number Diff line change
Expand Up @@ -6849,7 +6849,9 @@ sctp_pcb_init()
#endif
#if defined(__Userspace__)
mbuf_initialize(NULL);
#if !defined(__MINGW32__)
atomic_init();
#endif
#if defined(INET) || defined(INET6)
recv_thread_init();
#endif
Expand Down
4 changes: 2 additions & 2 deletions usrsctplib/netinet/sctp_pcb.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ struct sctp_base_info {
#endif
#endif
#ifdef INET
#if defined(__Userspace_os_Windows)
#if defined(__Userspace_os_Windows) && !defined(__MINGW32__)
SOCKET userspace_rawsctp;
SOCKET userspace_udpsctp;
#else
Expand All @@ -331,7 +331,7 @@ struct sctp_base_info {
userland_thread_t recvthreadudp;
#endif
#ifdef INET6
#if defined(__Userspace_os_Windows)
#if defined(__Userspace_os_Windows) && !defined(__MINGW32__)
SOCKET userspace_rawsctp6;
SOCKET userspace_udpsctp6;
#else
Expand Down
10 changes: 10 additions & 0 deletions usrsctplib/netinet/sctp_userspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
#include <netinet/sctp_pcb.h>
#include <sys/timeb.h>
#include <iphlpapi.h>
#if !defined(__MINGW32__)
#pragma comment(lib, "IPHLPAPI.lib")
#endif
#endif
#include <netinet/sctp_os_userspace.h>
#if defined(__Userspace_os_FreeBSD)
#include <pthread_np.h>
Expand All @@ -46,6 +48,10 @@
/* Adapter to translate Unix thread start routines to Windows thread start
* routines.
*/
#if defined(__MINGW32__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#endif
static DWORD WINAPI
sctp_create_thread_adapter(void *arg) {
start_routine_t start_routine = (start_routine_t)arg;
Expand Down Expand Up @@ -75,6 +81,10 @@ sctp_userspace_thread_equal(userland_thread_id_t t1, userland_thread_id_t t2)
return (t1 == t2);
}

#if defined(__MINGW32__)
#pragma GCC diagnostic pop
#endif

#else
int
sctp_userspace_thread_create(userland_thread_t *thread, start_routine_t start_routine)
Expand Down
2 changes: 2 additions & 0 deletions usrsctplib/user_atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@
}
#endif
#if defined(__Userspace_os_Windows)
#if !defined(__MINGW32__)
static void atomic_init(void) {} /* empty when we are not using atomic_mtx */
#endif
#else
static inline void atomic_init(void) {} /* empty when we are not using atomic_mtx */
#endif
Expand Down
2 changes: 1 addition & 1 deletion usrsctplib/user_environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ extern int ipport_firstauto, ipport_lastauto;
*/
extern int nmbclusters;

#if !defined(_MSC_VER)
#if !defined(_MSC_VER) && !defined(__MINGW32__)
#define min(a,b) ((a)>(b)?(b):(a))
#define max(a,b) ((a)>(b)?(a):(b))
#endif
Expand Down
Loading

0 comments on commit 37a8a0a

Please sign in to comment.