From 271d2ee2c499af7fa7c38c41cda0586bffa81391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Thu, 27 Jun 2019 10:37:33 +0200 Subject: [PATCH 01/19] single thread Based on https://github.com/sctplab/usrsctp/pull/38/commits/010fcbc6f193152607dc267c7e7e573c9bffc111 from @ccaughie --- programs/CMakeLists.txt | 1 + programs/Makefile.am | 8 +- programs/Makefile.nmake | 9 +- programs/st_client.c | 355 ++++++++++++++++++++++++++++++ usrsctplib/netinet/sctp_callout.c | 2 +- usrsctplib/netinet/sctp_callout.h | 1 + usrsctplib/netinet/sctp_output.c | 2 +- usrsctplib/netinet/sctp_pcb.c | 5 +- usrsctplib/netinet/sctp_pcb.h | 2 +- usrsctplib/netinet/sctp_usrreq.c | 7 +- usrsctplib/netinet/sctp_var.h | 2 +- usrsctplib/user_socket.c | 35 ++- usrsctplib/usrsctp.h | 15 ++ 13 files changed, 425 insertions(+), 19 deletions(-) create mode 100644 programs/st_client.c diff --git a/programs/CMakeLists.txt b/programs/CMakeLists.txt index b99d18123..45c772b87 100644 --- a/programs/CMakeLists.txt +++ b/programs/CMakeLists.txt @@ -100,6 +100,7 @@ list(APPEND check_programs http_client.c http_client_upcall.c rtcweb.c + st_client.c test_libmgmt.c test_timer.c tsctp.c diff --git a/programs/Makefile.am b/programs/Makefile.am index acb216872..794c32e19 100644 --- a/programs/Makefile.am +++ b/programs/Makefile.am @@ -52,7 +52,8 @@ EXTRA_DIST = \ ekr_peer.c \ test_libmgmt.c \ http_client.c \ - http_client_upcall.c + http_client_upcall.c \ + st_client.c noinst_PROGRAMS = \ test_libmgmt \ @@ -76,7 +77,8 @@ noinst_PROGRAMS = \ ekr_peer \ test_libmgmt \ http_client \ - http_client_upcall + http_client_upcall \ + st_client test_libmgmt_SOURCES = programs_helper.c test_libmgmt.c test_libmgmt_LDADD = ../usrsctplib/libusrsctp.la @@ -120,3 +122,5 @@ http_client_SOURCES = programs_helper.c http_client.c http_client_LDADD = ../usrsctplib/libusrsctp.la http_client_upcall_SOURCES = programs_helper.c http_client_upcall.c http_client_upcall_LDADD = ../usrsctplib/libusrsctp.la +st_client_SOURCES = programs_helper.c st_client.c +st_client_LDADD = ../usrsctplib/libusrsctp.la diff --git a/programs/Makefile.nmake b/programs/Makefile.nmake index d426b2442..24e9695ec 100644 --- a/programs/Makefile.nmake +++ b/programs/Makefile.nmake @@ -57,7 +57,8 @@ all: \ ekr_loop_upcall \ test_libmgmt \ http_client \ - http_client_upcall + http_client_upcall \ + st_client programs_helper.obj : programs_helper.c programs_helper.h cl $(CVARSDLL) $(CFLAGS) -c programs_helper.c @@ -138,6 +139,10 @@ http_client_upcall: $(CC) $(CFLAGS) $(CVARSDLL) -c http_client_upcall.c link -out:http_client_upcall.exe http_client_upcall.obj programs_helper.obj $(LINKFLAGS) +st_client: + $(CC) $(CFLAGS) $(CVARSDLL) -c st_client.c + link -out:st_client.exe st_client.obj programs_helper.obj $(LINKFLAGS) + clean: del /F client.exe del /F client.obj @@ -177,3 +182,5 @@ clean: del /F http_client.obj del /F http_client_upcall.exe del /F http_client_upcall.obj + del /F st_client.exe + del /F st_client.obj diff --git a/programs/st_client.c b/programs/st_client.c new file mode 100644 index 000000000..36eed741c --- /dev/null +++ b/programs/st_client.c @@ -0,0 +1,355 @@ +/* + * Copyright (C) 2011-2013 Michael Tuexen + * Copyright (C) 2011-2015 Colin Caughie + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifdef _WIN32 +#define _CRT_SECURE_NO_WARNINGS +#endif +#include +#include +#include +#include +#include +#ifndef _WIN32 +#include +#include +#include +#include +#include +#include +#else +#include +#include +#endif +#include +#include "programs_helper.h" + +#define MAX_PACKET_SIZE (1<<16) +#define BUFFER_SIZE 80 +#define DISCARD_PPID 39 + +#define TIMER_INTERVAL_MSECS 10 + +static int connecting = 0; +static int finish = 0; + +static unsigned get_tick_count() +{ +#ifdef _WIN32 + return GetTickCount(); +#else + static const clock_t clocks_per_msec = CLOCKS_PER_SEC / 1000; + return clock() / clocks_per_msec; +#endif +} + +static void +handle_packets(int sock, struct socket* s, void* sconn_addr) +{ + char *dump_buf; + ssize_t length; + char buf[MAX_PACKET_SIZE]; + + fd_set rfds; + struct timeval tv; + int retval; + + unsigned next_fire_time = get_tick_count(); + unsigned last_fire_time = next_fire_time; + + while (!finish) { + + unsigned now = get_tick_count(); + int wait_time; + + if ((int) (now - next_fire_time) > 0) { + usrsctp_fire_timer(now - last_fire_time); + last_fire_time = now; + next_fire_time = now + TIMER_INTERVAL_MSECS; + } + + wait_time = next_fire_time - now; + tv.tv_sec = wait_time / 1000; + tv.tv_usec = (wait_time % 1000) * 1000; + + retval = select(1, &rfds, NULL, NULL, &tv); + + length = recv(sock, 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); + usrsctp_freedumpbuffer(dump_buf); + } + usrsctp_conninput(sconn_addr, buf, (size_t)length, 0); + } + } +} + +static void on_connect(struct socket* s) +{ + struct sctp_sndinfo sndinfo; + char buffer[BUFFER_SIZE]; + + memset(buffer, 'A', BUFFER_SIZE); + sndinfo.snd_sid = 1; + sndinfo.snd_flags = 0; + sndinfo.snd_ppid = htonl(DISCARD_PPID); + sndinfo.snd_context = 0; + sndinfo.snd_assoc_id = 0; + if (usrsctp_sendv(s, buffer, BUFFER_SIZE, NULL, 0, (void *)&sndinfo, + (socklen_t)sizeof(struct sctp_sndinfo), SCTP_SENDV_SNDINFO, 0) < 0) { + perror("usrsctp_sendv"); + } +} + +static void on_socket_readable(struct socket* s) { + char buffer[BUFFER_SIZE]; + union sctp_sockstore addr; + socklen_t fromlen = sizeof(addr); + struct sctp_rcvinfo rcv_info; + socklen_t infolen = sizeof(rcv_info); + unsigned int infotype = 0; + int flags = 0; + ssize_t retval; + + /* Keep reading until there is no more data */ + for (;;) { + retval = usrsctp_recvv(s, buffer, sizeof(buffer), (struct sockaddr*) &addr, + &fromlen, &rcv_info, &infolen, &infotype, &flags); + + if (retval < 0) { + if (errno != EWOULDBLOCK) { + perror("usrsctp_recvv"); + finish = 1; + } + return; + } else if (retval == 0) { + printf("socket was disconnected\n"); + finish = 1; + return; + } + + if (flags & MSG_NOTIFICATION) { + printf("Notification of length %d received.\n", (int)retval); + } else { + printf("Msg of length %d received via %p:%u on stream %d with SSN %u and TSN %u, PPID %d, context %u.\n", + (int)retval, + addr.sconn.sconn_addr, + ntohs(addr.sconn.sconn_port), + rcv_info.rcv_sid, + rcv_info.rcv_ssn, + rcv_info.rcv_tsn, + ntohl(rcv_info.rcv_ppid), + rcv_info.rcv_context); + } + } +} + +static void handle_upcall(struct socket *s, void *arg, int flags) +{ + int events = usrsctp_get_events(s); + + if (connecting) { + if (events & SCTP_EVENT_ERROR) { + connecting = 0; + finish = 1; + } else if (events & SCTP_EVENT_WRITE) { + connecting = 0; + on_connect(s); + } + + return; + } + + if (events & SCTP_EVENT_READ) { + on_socket_readable(s); + } +} + +static int +conn_output(void *addr, void *buf, size_t length, uint8_t tos, uint8_t set_df) +{ + char *dump_buf; +#ifdef _WIN32 + SOCKET *fdp; +#else + int *fdp; +#endif + +#ifdef _WIN32 + fdp = (SOCKET *)addr; +#else + fdp = (int *)addr; +#endif + if ((dump_buf = usrsctp_dumppacket(buf, length, SCTP_DUMP_OUTBOUND)) != NULL) { + fprintf(stderr, "%s", dump_buf); + usrsctp_freedumpbuffer(dump_buf); + } +#ifdef _WIN32 + if (send(*fdp, buf, length, 0) == SOCKET_ERROR) { + return (WSAGetLastError()); +#else + if (send(*fdp, buf, length, 0) < 0) { + return (errno); +#endif + } else { + return (0); + } +} + +/* Usage: st_client local_addr local_port remote_addr remote_port remote_sctp_port */ +int +main(int argc, char *argv[]) +{ + struct sockaddr_in sin; + struct sockaddr_conn sconn; +#ifdef _WIN32 + SOCKET fd; +#else + int fd; +#endif + struct socket *s; + int retval; +#ifdef _WIN32 + WSADATA wsaData; +#endif + + if (argc < 6) { + printf("Usage: st_client local_addr local_port remote_addr remote_port remote_sctp_port\n"); + exit(EXIT_FAILURE); + } + +#ifdef _WIN32 + if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { + printf("WSAStartup failed\n"); + exit (EXIT_FAILURE); + } +#endif + usrsctp_init_nothreads(0, conn_output, debug_printf); + /* set up a connected UDP socket */ +#ifdef _WIN32 + if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) { + printf("socket() failed with error: %ld\n", WSAGetLastError()); + exit(1); + } +#else + if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { + perror("socket"); + exit(1); + } +#endif + memset(&sin, 0, sizeof(struct sockaddr_in)); + sin.sin_family = AF_INET; +#ifdef HAVE_SIN_LEN + sin.sin_len = sizeof(struct sockaddr_in); +#endif + sin.sin_port = htons(atoi(argv[2])); + if (!inet_pton(AF_INET, argv[1], &sin.sin_addr.s_addr)){ + printf("error: invalid address\n"); + exit(1); + } +#ifdef _WIN32 + if (bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == SOCKET_ERROR) { + printf("bind() failed with error: %ld\n", WSAGetLastError()); + exit(1); + } +#else + if (bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) { + perror("bind"); + exit(1); + } +#endif + memset(&sin, 0, sizeof(struct sockaddr_in)); + sin.sin_family = AF_INET; +#ifdef HAVE_SIN_LEN + sin.sin_len = sizeof(struct sockaddr_in); +#endif + sin.sin_port = htons(atoi(argv[4])); + if (!inet_pton(AF_INET, argv[3], &sin.sin_addr.s_addr)){ + printf("error: invalid address\n"); + exit(1); + } +#ifdef _WIN32 + if (connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == SOCKET_ERROR) { + printf("connect() failed with error: %ld\n", WSAGetLastError()); + exit(1); + } +#else + if (connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) { + perror("connect"); + exit(1); + } +#endif +#ifdef SCTP_DEBUG + usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_NONE); +#endif + usrsctp_sysctl_set_sctp_ecn_enable(0); + usrsctp_register_address((void *)&fd); + + if ((s = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP, NULL, NULL, 0, NULL)) == NULL) { + perror("usrsctp_socket"); + exit(1); + } + + usrsctp_set_non_blocking(s, 1); + usrsctp_set_upcall(s, handle_upcall, NULL); + + memset(&sconn, 0, sizeof(struct sockaddr_conn)); + sconn.sconn_family = AF_CONN; +#ifdef HAVE_SCONN_LEN + sconn.sconn_len = sizeof(struct sockaddr_conn); +#endif + sconn.sconn_port = htons(0); + sconn.sconn_addr = NULL; + if (usrsctp_bind(s, (struct sockaddr *)&sconn, sizeof(struct sockaddr_conn)) < 0) { + perror("usrsctp_bind"); + } + + memset(&sconn, 0, sizeof(struct sockaddr_conn)); + sconn.sconn_family = AF_CONN; +#ifdef HAVE_SCONN_LEN + sconn.sconn_len = sizeof(struct sockaddr_conn); +#endif + sconn.sconn_port = htons(atoi(argv[5])); + sconn.sconn_addr = &fd; + + retval = usrsctp_connect(s, (struct sockaddr *)&sconn, sizeof(struct sockaddr_conn)); + + if (retval < 0 && errno != EWOULDBLOCK && errno != EINPROGRESS) { + perror("usrsctp_connect"); + exit(1); + } + + connecting = 1; + + handle_packets(fd, s, sconn.sconn_addr); + + return 0; +} diff --git a/usrsctplib/netinet/sctp_callout.c b/usrsctplib/netinet/sctp_callout.c index b12b4b23f..3ba527071 100755 --- a/usrsctplib/netinet/sctp_callout.c +++ b/usrsctplib/netinet/sctp_callout.c @@ -217,7 +217,7 @@ sctp_os_timer_stop(sctp_os_timer_t *c) return (1); } -static void +void sctp_handle_tick(int delta) { sctp_os_timer_t *c; diff --git a/usrsctplib/netinet/sctp_callout.h b/usrsctplib/netinet/sctp_callout.h index 814bd0698..3ac6c5bb4 100755 --- a/usrsctplib/netinet/sctp_callout.h +++ b/usrsctplib/netinet/sctp_callout.h @@ -103,6 +103,7 @@ typedef struct sctp_callout sctp_os_timer_t; void sctp_os_timer_init(sctp_os_timer_t *tmr); void sctp_os_timer_start(sctp_os_timer_t *, int, void (*)(void *), void *); int sctp_os_timer_stop(sctp_os_timer_t *); +void sctp_handle_tick(int delta); #define SCTP_OS_TIMER_INIT sctp_os_timer_init #define SCTP_OS_TIMER_START sctp_os_timer_start diff --git a/usrsctplib/netinet/sctp_output.c b/usrsctplib/netinet/sctp_output.c index 3f7c461c5..3ba64da9c 100755 --- a/usrsctplib/netinet/sctp_output.c +++ b/usrsctplib/netinet/sctp_output.c @@ -13901,7 +13901,7 @@ sctp_lower_sosend(struct socket *so, } } #if defined(__Userspace__) - if (inp->recv_callback) { + if (inp->recv_callback || SCTP_SO_IS_NBIO(so)) { non_blocking = 1; } #endif diff --git a/usrsctplib/netinet/sctp_pcb.c b/usrsctplib/netinet/sctp_pcb.c index f5c4e3a19..4ea0845cb 100755 --- a/usrsctplib/netinet/sctp_pcb.c +++ b/usrsctplib/netinet/sctp_pcb.c @@ -6635,7 +6635,7 @@ sctp_netisr_hdlr(struct mbuf *m, uintptr_t source) #endif void -sctp_pcb_init() +sctp_pcb_init(int start_threads) { /* * SCTP initialization for the PCB structures should be called by @@ -6851,7 +6851,8 @@ sctp_pcb_init() mbuf_initialize(NULL); atomic_init(); #if defined(INET) || defined(INET6) - recv_thread_init(); + if (start_threads) + recv_thread_init(); #endif #endif } diff --git a/usrsctplib/netinet/sctp_pcb.h b/usrsctplib/netinet/sctp_pcb.h index dad09e0e5..16b19c034 100755 --- a/usrsctplib/netinet/sctp_pcb.h +++ b/usrsctplib/netinet/sctp_pcb.h @@ -843,7 +843,7 @@ void sctp_remove_net(struct sctp_tcb *, struct sctp_nets *); int sctp_del_remote_addr(struct sctp_tcb *, struct sockaddr *); -void sctp_pcb_init(void); +void sctp_pcb_init(int); void sctp_pcb_finish(void); diff --git a/usrsctplib/netinet/sctp_usrreq.c b/usrsctplib/netinet/sctp_usrreq.c index 0912e3e9d..5ff7a2a8a 100755 --- a/usrsctplib/netinet/sctp_usrreq.c +++ b/usrsctplib/netinet/sctp_usrreq.c @@ -77,7 +77,7 @@ void #if defined(__Userspace__) sctp_init(uint16_t port, int (*conn_output)(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df), - void (*debug_printf)(const char *format, ...)) + void (*debug_printf)(const char *format, ...), int start_threads) #elif defined(__APPLE__) && (!defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) &&!defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)) sctp_init(struct protosw *pp SCTP_UNUSED, struct domain *dp SCTP_UNUSED) #else @@ -151,9 +151,10 @@ sctp_init(void) SCTP_BASE_VAR(debug_printf) = debug_printf; SCTP_BASE_VAR(crc32c_offloaded) = 0; #endif - sctp_pcb_init(); + sctp_pcb_init(start_threads); #if defined(__Userspace__) - sctp_start_timer(); + if (start_threads) + sctp_start_timer(); #endif #if defined(SCTP_PACKET_LOGGING) SCTP_BASE_VAR(packet_log_writers) = 0; diff --git a/usrsctplib/netinet/sctp_var.h b/usrsctplib/netinet/sctp_var.h index bc4afe95f..ead9e9cd6 100755 --- a/usrsctplib/netinet/sctp_var.h +++ b/usrsctplib/netinet/sctp_var.h @@ -454,7 +454,7 @@ void sctp_drain(void); #if defined(__Userspace__) void sctp_init(uint16_t, int (*)(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df), - void (*)(const char *, ...)); + void (*)(const char *, ...), int start_threads); #elif defined(__FreeBSD__) && __FreeBSD_version < 902000 void sctp_init __P((void)); #elif defined(__APPLE__) && (!defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) &&!defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)) diff --git a/usrsctplib/user_socket.c b/usrsctplib/user_socket.c index bec022638..653d2ee41 100755 --- a/usrsctplib/user_socket.c +++ b/usrsctplib/user_socket.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #ifdef INET6 #include @@ -77,11 +78,7 @@ extern int sctp_sosend(struct socket *so, struct sockaddr *addr, struct uio *uio extern int sctp_attach(struct socket *so, int proto, uint32_t vrf_id); extern int sctpconn_attach(struct socket *so, int proto, uint32_t vrf_id); -void -usrsctp_init(uint16_t port, - int (*conn_output)(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df), - void (*debug_printf)(const char *format, ...)) -{ +static void init_sync() { #if defined(__Userspace_os_Windows) #if defined(INET) || defined(INET6) WSADATA wsaData; @@ -104,7 +101,25 @@ usrsctp_init(uint16_t port, pthread_mutexattr_destroy(&mutex_attr); pthread_cond_init(&accept_cond, NULL); #endif - sctp_init(port, conn_output, debug_printf); +} + +void +usrsctp_init(uint16_t port, + int (*conn_output)(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df), + void (*debug_printf)(const char *format, ...)) +{ + init_sync(); + sctp_init(port, conn_output, debug_printf, 1); +} + + +void +usrsctp_init_nothreads(uint16_t port, + int (*conn_output)(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df), + void (*debug_printf)(const char *format, ...)) +{ + init_sync(); + sctp_init(port, conn_output, debug_printf, 0); } @@ -1564,8 +1579,9 @@ sowakeup(struct socket *so, struct sockbuf *sb) #endif } SOCKBUF_UNLOCK(sb); - /*__Userspace__ what todo about so_upcall?*/ + if ((sb->sb_flags & SB_UPCALL) && so->so_upcall != NULL) + (*so->so_upcall)(so, so->so_upcallarg, M_NOWAIT); } #else /* kernel version for reference */ /* @@ -3515,6 +3531,11 @@ usrsctp_conninput(void *addr, const void *buffer, size_t length, uint8_t ecn_bit return; } +void usrsctp_fire_timer(int delta) +{ + sctp_handle_tick(delta); +} + int usrsctp_get_events(struct socket *so) { diff --git a/usrsctplib/usrsctp.h b/usrsctplib/usrsctp.h index 0533c495e..6c8e9bf76 100644 --- a/usrsctplib/usrsctp.h +++ b/usrsctplib/usrsctp.h @@ -892,6 +892,11 @@ usrsctp_init(uint16_t, int (*)(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df), void (*)(const char *format, ...)); +void +usrsctp_init_nothreads(uint16_t, + int (*)(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df), + void (*)(const char *format, ...)); + struct socket * usrsctp_socket(int domain, int type, int protocol, int (*receive_cb)(struct socket *sock, union sctp_sockstore addr, void *data, @@ -1016,6 +1021,13 @@ usrsctp_set_non_blocking(struct socket *, int); int usrsctp_get_non_blocking(struct socket *); +int +usrsctp_get_events(struct socket *so); + +int +usrsctp_set_upcall(struct socket *so, + void (*upcall)(struct socket *, void *, int), void *arg); + void usrsctp_register_address(void *); @@ -1034,6 +1046,9 @@ int usrsctp_get_events(struct socket *so); +void +usrsctp_fire_timer(int delta); + #define SCTP_DUMP_OUTBOUND 1 #define SCTP_DUMP_INBOUND 0 From 483cfeb3ce4a973a03f72f1ccfd290200175ade1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Wed, 7 Aug 2019 10:35:42 +0200 Subject: [PATCH 02/19] single thread: st_client.c. remove unused variables --- programs/st_client.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/programs/st_client.c b/programs/st_client.c index 36eed741c..bc87de7c9 100644 --- a/programs/st_client.c +++ b/programs/st_client.c @@ -77,9 +77,7 @@ handle_packets(int sock, struct socket* s, void* sconn_addr) ssize_t length; char buf[MAX_PACKET_SIZE]; - fd_set rfds; struct timeval tv; - int retval; unsigned next_fire_time = get_tick_count(); unsigned last_fire_time = next_fire_time; @@ -99,8 +97,6 @@ handle_packets(int sock, struct socket* s, void* sconn_addr) tv.tv_sec = wait_time / 1000; tv.tv_usec = (wait_time % 1000) * 1000; - retval = select(1, &rfds, NULL, NULL, &tv); - length = recv(sock, buf, MAX_PACKET_SIZE, 0); if (length > 0) { if ((dump_buf = usrsctp_dumppacket(buf, (size_t)length, SCTP_DUMP_INBOUND)) != NULL) { From 709c3e0bfa6acf5551571e94645d3d90cd184be7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Wed, 7 Aug 2019 10:36:57 +0200 Subject: [PATCH 03/19] Revert "single thread: st_client.c. remove unused variables" This reverts commit 1e240a954dcadf6296b9a0f2d1299e07a9e65ca8. --- programs/st_client.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/programs/st_client.c b/programs/st_client.c index bc87de7c9..36eed741c 100644 --- a/programs/st_client.c +++ b/programs/st_client.c @@ -77,7 +77,9 @@ handle_packets(int sock, struct socket* s, void* sconn_addr) ssize_t length; char buf[MAX_PACKET_SIZE]; + fd_set rfds; struct timeval tv; + int retval; unsigned next_fire_time = get_tick_count(); unsigned last_fire_time = next_fire_time; @@ -97,6 +99,8 @@ handle_packets(int sock, struct socket* s, void* sconn_addr) tv.tv_sec = wait_time / 1000; tv.tv_usec = (wait_time % 1000) * 1000; + retval = select(1, &rfds, NULL, NULL, &tv); + length = recv(sock, buf, MAX_PACKET_SIZE, 0); if (length > 0) { if ((dump_buf = usrsctp_dumppacket(buf, (size_t)length, SCTP_DUMP_INBOUND)) != NULL) { From 32e372b29a56db2990921b12b5ed5f255014eb4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Wed, 7 Aug 2019 10:37:25 +0200 Subject: [PATCH 04/19] single thread: st_client.c. remove unused variable --- programs/st_client.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/programs/st_client.c b/programs/st_client.c index 36eed741c..1e12b8f6d 100644 --- a/programs/st_client.c +++ b/programs/st_client.c @@ -79,7 +79,6 @@ handle_packets(int sock, struct socket* s, void* sconn_addr) fd_set rfds; struct timeval tv; - int retval; unsigned next_fire_time = get_tick_count(); unsigned last_fire_time = next_fire_time; @@ -99,7 +98,7 @@ handle_packets(int sock, struct socket* s, void* sconn_addr) tv.tv_sec = wait_time / 1000; tv.tv_usec = (wait_time % 1000) * 1000; - retval = select(1, &rfds, NULL, NULL, &tv); + select(1, &rfds, NULL, NULL, &tv); length = recv(sock, buf, MAX_PACKET_SIZE, 0); if (length > 0) { From 2f8d7b6edb71eae95f1f0540fe8f9fc279efc94b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Wed, 7 Aug 2019 13:17:24 +0200 Subject: [PATCH 05/19] single thread: s/usrsctp_fire_timer/usrsctp_handle_timers --- programs/st_client.c | 2 +- usrsctplib/user_socket.c | 2 +- usrsctplib/usrsctp.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/programs/st_client.c b/programs/st_client.c index 1e12b8f6d..5ab258285 100644 --- a/programs/st_client.c +++ b/programs/st_client.c @@ -89,7 +89,7 @@ handle_packets(int sock, struct socket* s, void* sconn_addr) int wait_time; if ((int) (now - next_fire_time) > 0) { - usrsctp_fire_timer(now - last_fire_time); + usrsctp_handle_timers(now - last_fire_time); last_fire_time = now; next_fire_time = now + TIMER_INTERVAL_MSECS; } diff --git a/usrsctplib/user_socket.c b/usrsctplib/user_socket.c index 653d2ee41..c42683e30 100755 --- a/usrsctplib/user_socket.c +++ b/usrsctplib/user_socket.c @@ -3531,7 +3531,7 @@ usrsctp_conninput(void *addr, const void *buffer, size_t length, uint8_t ecn_bit return; } -void usrsctp_fire_timer(int delta) +void usrsctp_handle_timers(int delta) { sctp_handle_tick(delta); } diff --git a/usrsctplib/usrsctp.h b/usrsctplib/usrsctp.h index 6c8e9bf76..5de983282 100644 --- a/usrsctplib/usrsctp.h +++ b/usrsctplib/usrsctp.h @@ -1047,7 +1047,7 @@ usrsctp_get_events(struct socket *so); void -usrsctp_fire_timer(int delta); +usrsctp_handle_timers(int delta); #define SCTP_DUMP_OUTBOUND 1 #define SCTP_DUMP_INBOUND 0 From d9c914f7fa546c6cbad62fa87d82616bab8f1c16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Wed, 7 Aug 2019 13:18:32 +0200 Subject: [PATCH 06/19] single thread: remove duplicated functions --- usrsctplib/usrsctp.h | 7 ------- 1 file changed, 7 deletions(-) diff --git a/usrsctplib/usrsctp.h b/usrsctplib/usrsctp.h index 5de983282..d8b459382 100644 --- a/usrsctplib/usrsctp.h +++ b/usrsctplib/usrsctp.h @@ -1021,13 +1021,6 @@ usrsctp_set_non_blocking(struct socket *, int); int usrsctp_get_non_blocking(struct socket *); -int -usrsctp_get_events(struct socket *so); - -int -usrsctp_set_upcall(struct socket *so, - void (*upcall)(struct socket *, void *, int), void *arg); - void usrsctp_register_address(void *); From 2f4f431c920126f68a6cc85ae90c1da6413fcd66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Wed, 7 Aug 2019 13:22:43 +0200 Subject: [PATCH 07/19] single thread: remove duplicate check for SCTP_SO_IS_NBIO(so) --- usrsctplib/netinet/sctp_output.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usrsctplib/netinet/sctp_output.c b/usrsctplib/netinet/sctp_output.c index 3ba64da9c..3f7c461c5 100755 --- a/usrsctplib/netinet/sctp_output.c +++ b/usrsctplib/netinet/sctp_output.c @@ -13901,7 +13901,7 @@ sctp_lower_sosend(struct socket *so, } } #if defined(__Userspace__) - if (inp->recv_callback || SCTP_SO_IS_NBIO(so)) { + if (inp->recv_callback) { non_blocking = 1; } #endif From b6236a0a0d132140ab8e6e208841b659a06014ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Wed, 7 Aug 2019 15:17:20 +0200 Subject: [PATCH 08/19] single thread: s/handle_packets/handle_events --- programs/st_client.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/programs/st_client.c b/programs/st_client.c index 5ab258285..88aafdd1c 100644 --- a/programs/st_client.c +++ b/programs/st_client.c @@ -71,7 +71,7 @@ static unsigned get_tick_count() } static void -handle_packets(int sock, struct socket* s, void* sconn_addr) +handle_events(int sock, struct socket* s, void* sconn_addr) { char *dump_buf; ssize_t length; @@ -348,7 +348,7 @@ main(int argc, char *argv[]) connecting = 1; - handle_packets(fd, s, sconn.sconn_addr); + handle_events(fd, s, sconn.sconn_addr); return 0; } From 8fdb6721a053e8828c126683cfd756412b66c404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Baz=20Castillo?= Date: Wed, 7 Aug 2019 15:44:23 +0200 Subject: [PATCH 09/19] .gitignore: ignore programs/st_client binary --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index f1edda06c..2fc78e1d3 100644 --- a/.gitignore +++ b/.gitignore @@ -73,6 +73,7 @@ programs/ekr_server programs/http_client programs/http_client_upcall programs/rtcweb +programs/st_client programs/tsctp programs/test_libmgmt programs/test_timer From 3ca5f0b1047e9f72a78aa61e8ac6fb224942bc98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Baz=20Castillo?= Date: Wed, 7 Aug 2019 15:44:53 +0200 Subject: [PATCH 10/19] user_socket.c: remove unnecessary SB_UPCALL check --- usrsctplib/user_socket.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/usrsctplib/user_socket.c b/usrsctplib/user_socket.c index c42683e30..83c85e887 100755 --- a/usrsctplib/user_socket.c +++ b/usrsctplib/user_socket.c @@ -1579,9 +1579,6 @@ sowakeup(struct socket *so, struct sockbuf *sb) #endif } SOCKBUF_UNLOCK(sb); - - if ((sb->sb_flags & SB_UPCALL) && so->so_upcall != NULL) - (*so->so_upcall)(so, so->so_upcallarg, M_NOWAIT); } #else /* kernel version for reference */ /* From f77f06e01bef0fa29423fb4cc044000bfa7cb599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Baz=20Castillo?= Date: Wed, 7 Aug 2019 16:24:09 +0200 Subject: [PATCH 11/19] programs/client.c: make main() return(-1) after perror() --- programs/client.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/programs/client.c b/programs/client.c index 578a787f6..120d767a7 100644 --- a/programs/client.c +++ b/programs/client.c @@ -119,6 +119,7 @@ main(int argc, char *argv[]) if ((sock = usrsctp_socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP, receive_cb, NULL, 0, NULL)) == NULL) { perror("usrsctp_socket"); + return (-1); } memset(&event, 0, sizeof(event)); event.se_assoc_id = SCTP_ALL_ASSOC; @@ -127,6 +128,7 @@ main(int argc, char *argv[]) event.se_type = event_types[i]; if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(event)) < 0) { perror("setsockopt SCTP_EVENT"); + return (-1); } } if (argc > 3) { @@ -139,6 +141,7 @@ main(int argc, char *argv[]) addr6.sin6_addr = in6addr_any; if (usrsctp_bind(sock, (struct sockaddr *)&addr6, sizeof(struct sockaddr_in6)) < 0) { perror("bind"); + return (-1); } } if (argc > 5) { @@ -147,6 +150,7 @@ main(int argc, char *argv[]) encaps.sue_port = htons(atoi(argv[5])); if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_REMOTE_UDP_ENCAPS_PORT, (const void*)&encaps, (socklen_t)sizeof(struct sctp_udpencaps)) < 0) { perror("setsockopt"); + return (-1); } } memset((void *)&addr4, 0, sizeof(struct sockaddr_in)); @@ -164,16 +168,19 @@ main(int argc, char *argv[]) if (inet_pton(AF_INET6, argv[1], &addr6.sin6_addr) == 1) { if (usrsctp_connect(sock, (struct sockaddr *)&addr6, sizeof(struct sockaddr_in6)) < 0) { perror("usrsctp_connect"); + return (-1); } } else if (inet_pton(AF_INET, argv[1], &addr4.sin_addr) == 1) { if (usrsctp_connect(sock, (struct sockaddr *)&addr4, sizeof(struct sockaddr_in)) < 0) { perror("usrsctp_connect"); + return (-1); } } else { printf("Illegal destination address.\n"); } if ((n = usrsctp_getladdrs(sock, 0, &addrs)) < 0) { perror("usrsctp_getladdrs"); + return (-1); } else { addr = addrs; printf("Local addresses: "); @@ -222,6 +229,7 @@ main(int argc, char *argv[]) } if ((n = usrsctp_getpaddrs(sock, 0, &addrs)) < 0) { perror("usrsctp_getpaddrs"); + return (-1); } else { addr = addrs; printf("Peer addresses: "); @@ -274,6 +282,7 @@ main(int argc, char *argv[]) if (!done) { if (usrsctp_shutdown(sock, SHUT_WR) < 0) { perror("usrsctp_shutdown"); + return (-1); } } while (!done) { From 74ce07fca7cf02d5664e21ca54092d443f9520b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Baz=20Castillo?= Date: Wed, 7 Aug 2019 16:24:25 +0200 Subject: [PATCH 12/19] programs/st_client.c: trying to make it work (it does not yet) --- programs/st_client.c | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/programs/st_client.c b/programs/st_client.c index 88aafdd1c..b6660e712 100644 --- a/programs/st_client.c +++ b/programs/st_client.c @@ -29,6 +29,10 @@ * SUCH DAMAGE. */ +/* + * Usage: st_client local_addr local_port remote_addr remote_port remote_sctp_port + */ + #ifdef _WIN32 #define _CRT_SECURE_NO_WARNINGS #endif @@ -98,9 +102,22 @@ handle_events(int sock, struct socket* s, void* sconn_addr) tv.tv_sec = wait_time / 1000; tv.tv_usec = (wait_time % 1000) * 1000; - select(1, &rfds, NULL, NULL, &tv); + FD_ZERO(&rfds); + FD_SET(sock, &rfds); + + if (FD_ISSET(sock, &rfds)) + printf("--- FD_ISSET(sock, &rfds): yes\n"); + + printf("--- calling select()\n"); + + select(sock + 1, &rfds, NULL, NULL, &tv); + + printf("--- calling recv() 2\n"); length = recv(sock, buf, MAX_PACKET_SIZE, 0); + + printf("--- recv() returned, length:%zu\n", length); + if (length > 0) { if ((dump_buf = usrsctp_dumppacket(buf, (size_t)length, SCTP_DUMP_INBOUND)) != NULL) { fprintf(stderr, "%s", dump_buf); @@ -242,13 +259,13 @@ main(int argc, char *argv[]) if (argc < 6) { printf("Usage: st_client local_addr local_port remote_addr remote_port remote_sctp_port\n"); - exit(EXIT_FAILURE); + return (-1); } #ifdef _WIN32 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { printf("WSAStartup failed\n"); - exit (EXIT_FAILURE); + return (-1); } #endif usrsctp_init_nothreads(0, conn_output, debug_printf); @@ -256,12 +273,12 @@ main(int argc, char *argv[]) #ifdef _WIN32 if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) { printf("socket() failed with error: %ld\n", WSAGetLastError()); - exit(1); + return (-1); } #else if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { perror("socket"); - exit(1); + return (-1); } #endif memset(&sin, 0, sizeof(struct sockaddr_in)); @@ -272,17 +289,17 @@ main(int argc, char *argv[]) sin.sin_port = htons(atoi(argv[2])); if (!inet_pton(AF_INET, argv[1], &sin.sin_addr.s_addr)){ printf("error: invalid address\n"); - exit(1); + return (-1); } #ifdef _WIN32 if (bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == SOCKET_ERROR) { printf("bind() failed with error: %ld\n", WSAGetLastError()); - exit(1); + return (-1); } #else if (bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) { perror("bind"); - exit(1); + return (-1); } #endif memset(&sin, 0, sizeof(struct sockaddr_in)); @@ -293,17 +310,17 @@ main(int argc, char *argv[]) sin.sin_port = htons(atoi(argv[4])); if (!inet_pton(AF_INET, argv[3], &sin.sin_addr.s_addr)){ printf("error: invalid address\n"); - exit(1); + return (-1); } #ifdef _WIN32 if (connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == SOCKET_ERROR) { printf("connect() failed with error: %ld\n", WSAGetLastError()); - exit(1); + return (-1); } #else if (connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) { perror("connect"); - exit(1); + return (-1); } #endif #ifdef SCTP_DEBUG @@ -314,7 +331,7 @@ main(int argc, char *argv[]) if ((s = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP, NULL, NULL, 0, NULL)) == NULL) { perror("usrsctp_socket"); - exit(1); + return (-1); } usrsctp_set_non_blocking(s, 1); @@ -329,6 +346,7 @@ main(int argc, char *argv[]) sconn.sconn_addr = NULL; if (usrsctp_bind(s, (struct sockaddr *)&sconn, sizeof(struct sockaddr_conn)) < 0) { perror("usrsctp_bind"); + return (-1); } memset(&sconn, 0, sizeof(struct sockaddr_conn)); @@ -343,7 +361,7 @@ main(int argc, char *argv[]) if (retval < 0 && errno != EWOULDBLOCK && errno != EINPROGRESS) { perror("usrsctp_connect"); - exit(1); + return (-1); } connecting = 1; From db38dc4479a4106e151d43082088261bd0ab8aac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Baz=20Castillo?= Date: Wed, 7 Aug 2019 16:32:59 +0200 Subject: [PATCH 13/19] programs/st_client.c: replace CLOCKS_PER_SEC with gettimeofday based code --- programs/st_client.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/programs/st_client.c b/programs/st_client.c index b6660e712..adf27a479 100644 --- a/programs/st_client.c +++ b/programs/st_client.c @@ -43,6 +43,7 @@ #include #ifndef _WIN32 #include +#include #include #include #include @@ -69,8 +70,10 @@ static unsigned get_tick_count() #ifdef _WIN32 return GetTickCount(); #else - static const clock_t clocks_per_msec = CLOCKS_PER_SEC / 1000; - return clock() / clocks_per_msec; + struct timeval te; + gettimeofday(&te, NULL); // get current time + unsigned milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // calculate milliseconds + return milliseconds; #endif } From 0d3953740c90776255ed7576004d7ea7d25ce9c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Baz=20Castillo?= Date: Wed, 7 Aug 2019 16:38:38 +0200 Subject: [PATCH 14/19] programs/client.c: revert cosmetic changes --- programs/client.c | 9 --------- 1 file changed, 9 deletions(-) diff --git a/programs/client.c b/programs/client.c index 120d767a7..578a787f6 100644 --- a/programs/client.c +++ b/programs/client.c @@ -119,7 +119,6 @@ main(int argc, char *argv[]) if ((sock = usrsctp_socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP, receive_cb, NULL, 0, NULL)) == NULL) { perror("usrsctp_socket"); - return (-1); } memset(&event, 0, sizeof(event)); event.se_assoc_id = SCTP_ALL_ASSOC; @@ -128,7 +127,6 @@ main(int argc, char *argv[]) event.se_type = event_types[i]; if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(event)) < 0) { perror("setsockopt SCTP_EVENT"); - return (-1); } } if (argc > 3) { @@ -141,7 +139,6 @@ main(int argc, char *argv[]) addr6.sin6_addr = in6addr_any; if (usrsctp_bind(sock, (struct sockaddr *)&addr6, sizeof(struct sockaddr_in6)) < 0) { perror("bind"); - return (-1); } } if (argc > 5) { @@ -150,7 +147,6 @@ main(int argc, char *argv[]) encaps.sue_port = htons(atoi(argv[5])); if (usrsctp_setsockopt(sock, IPPROTO_SCTP, SCTP_REMOTE_UDP_ENCAPS_PORT, (const void*)&encaps, (socklen_t)sizeof(struct sctp_udpencaps)) < 0) { perror("setsockopt"); - return (-1); } } memset((void *)&addr4, 0, sizeof(struct sockaddr_in)); @@ -168,19 +164,16 @@ main(int argc, char *argv[]) if (inet_pton(AF_INET6, argv[1], &addr6.sin6_addr) == 1) { if (usrsctp_connect(sock, (struct sockaddr *)&addr6, sizeof(struct sockaddr_in6)) < 0) { perror("usrsctp_connect"); - return (-1); } } else if (inet_pton(AF_INET, argv[1], &addr4.sin_addr) == 1) { if (usrsctp_connect(sock, (struct sockaddr *)&addr4, sizeof(struct sockaddr_in)) < 0) { perror("usrsctp_connect"); - return (-1); } } else { printf("Illegal destination address.\n"); } if ((n = usrsctp_getladdrs(sock, 0, &addrs)) < 0) { perror("usrsctp_getladdrs"); - return (-1); } else { addr = addrs; printf("Local addresses: "); @@ -229,7 +222,6 @@ main(int argc, char *argv[]) } if ((n = usrsctp_getpaddrs(sock, 0, &addrs)) < 0) { perror("usrsctp_getpaddrs"); - return (-1); } else { addr = addrs; printf("Peer addresses: "); @@ -282,7 +274,6 @@ main(int argc, char *argv[]) if (!done) { if (usrsctp_shutdown(sock, SHUT_WR) < 0) { perror("usrsctp_shutdown"); - return (-1); } } while (!done) { From b5061e592da009ddd53d5b943278b96527fa9fc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Baz=20Castillo?= Date: Wed, 7 Aug 2019 17:27:20 +0200 Subject: [PATCH 15/19] programs/st_client.c: clean debug prints --- programs/st_client.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/programs/st_client.c b/programs/st_client.c index adf27a479..4e2edb3b9 100644 --- a/programs/st_client.c +++ b/programs/st_client.c @@ -108,25 +108,18 @@ handle_events(int sock, struct socket* s, void* sconn_addr) FD_ZERO(&rfds); FD_SET(sock, &rfds); - if (FD_ISSET(sock, &rfds)) - printf("--- FD_ISSET(sock, &rfds): yes\n"); - - printf("--- calling select()\n"); - select(sock + 1, &rfds, NULL, NULL, &tv); - printf("--- calling recv() 2\n"); - - length = recv(sock, buf, MAX_PACKET_SIZE, 0); - - printf("--- recv() returned, length:%zu\n", length); + if (FD_ISSET(sock, &rfds)) { + length = recv(sock, 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); - usrsctp_freedumpbuffer(dump_buf); + if (length > 0) { + if ((dump_buf = usrsctp_dumppacket(buf, (size_t)length, SCTP_DUMP_INBOUND)) != NULL) { + fprintf(stderr, "%s", dump_buf); + usrsctp_freedumpbuffer(dump_buf); + } + usrsctp_conninput(sconn_addr, buf, (size_t)length, 0); } - usrsctp_conninput(sconn_addr, buf, (size_t)length, 0); } } } From 31618cd5beb23814781c39466560108ab0978a09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Baz=20Castillo?= Date: Thu, 8 Aug 2019 17:27:53 +0200 Subject: [PATCH 16/19] programs/meson.build: add st_client --- programs/meson.build | 1 + 1 file changed, 1 insertion(+) diff --git a/programs/meson.build b/programs/meson.build index daed94150..9cbb0fb2e 100644 --- a/programs/meson.build +++ b/programs/meson.build @@ -5,6 +5,7 @@ programs_helper_sources = files('programs_helper.c') programs = { 'chargen_server_upcall': files('chargen_server_upcall.c'), 'client': files('client.c'), + 'st_client': files('st_client.c'), 'client_upcall': files('client_upcall.c'), 'daytime_server': files('daytime_server.c'), 'daytime_server_upcall': files('daytime_server_upcall.c'), From 9009743aee2fa752b085afbcf9e8c74dc8470b2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Baz=20Castillo?= Date: Thu, 8 Aug 2019 17:29:53 +0200 Subject: [PATCH 17/19] Fix warnings in Windows --- programs/st_client.c | 4 ++-- usrsctplib/user_socket.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/programs/st_client.c b/programs/st_client.c index 4e2edb3b9..3e1848b55 100644 --- a/programs/st_client.c +++ b/programs/st_client.c @@ -65,7 +65,7 @@ static int connecting = 0; static int finish = 0; -static unsigned get_tick_count() +static unsigned get_tick_count(void) { #ifdef _WIN32 return GetTickCount(); @@ -225,7 +225,7 @@ conn_output(void *addr, void *buf, size_t length, uint8_t tos, uint8_t set_df) usrsctp_freedumpbuffer(dump_buf); } #ifdef _WIN32 - if (send(*fdp, buf, length, 0) == SOCKET_ERROR) { + if (send(*fdp, buf, (int)length, 0) == SOCKET_ERROR) { return (WSAGetLastError()); #else if (send(*fdp, buf, length, 0) < 0) { diff --git a/usrsctplib/user_socket.c b/usrsctplib/user_socket.c index 83c85e887..bde542888 100755 --- a/usrsctplib/user_socket.c +++ b/usrsctplib/user_socket.c @@ -78,7 +78,7 @@ extern int sctp_sosend(struct socket *so, struct sockaddr *addr, struct uio *uio extern int sctp_attach(struct socket *so, int proto, uint32_t vrf_id); extern int sctpconn_attach(struct socket *so, int proto, uint32_t vrf_id); -static void init_sync() { +static void init_sync(void) { #if defined(__Userspace_os_Windows) #if defined(INET) || defined(INET6) WSADATA wsaData; From b936692e1271180d0e134c89e6ca50fc7eb56efd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Baz=20Castillo?= Date: Mon, 12 Aug 2019 09:29:58 +0200 Subject: [PATCH 18/19] programs/st_client.c: use snd 0 instead of 1. --- programs/st_client.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programs/st_client.c b/programs/st_client.c index 3e1848b55..0033eb0c4 100644 --- a/programs/st_client.c +++ b/programs/st_client.c @@ -130,7 +130,7 @@ static void on_connect(struct socket* s) char buffer[BUFFER_SIZE]; memset(buffer, 'A', BUFFER_SIZE); - sndinfo.snd_sid = 1; + sndinfo.snd_sid = 0; sndinfo.snd_flags = 0; sndinfo.snd_ppid = htonl(DISCARD_PPID); sndinfo.snd_context = 0; From a2f1f5e0e800edcfa4710dd0fc9995adedf8694d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Baz=20Castillo?= Date: Mon, 12 Aug 2019 13:47:58 +0200 Subject: [PATCH 19/19] programs/st_client.c: add usrsctp_setsockopt(s, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof(int)) --- programs/st_client.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/programs/st_client.c b/programs/st_client.c index 0033eb0c4..777e6d2af 100644 --- a/programs/st_client.c +++ b/programs/st_client.c @@ -330,6 +330,10 @@ main(int argc, char *argv[]) return (-1); } + const int on = 1; + + usrsctp_setsockopt(s, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof(int)); + usrsctp_set_non_blocking(s, 1); usrsctp_set_upcall(s, handle_upcall, NULL);