aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--programs/CMakeLists.txt1
-rw-r--r--programs/chargen_server_upcall.c36
-rw-r--r--programs/http_client_upcall.c4
-rw-r--r--programs/tsctp.c72
-rw-r--r--programs/tsctp_upcall.c732
5 files changed, 793 insertions, 52 deletions
diff --git a/programs/CMakeLists.txt b/programs/CMakeLists.txt
index e81bc8e8..4f473905 100644
--- a/programs/CMakeLists.txt
+++ b/programs/CMakeLists.txt
@@ -90,6 +90,7 @@ set(check_programs
test_libmgmt.c
test_timer.c
tsctp.c
+ tsctp_upcall.c
)
foreach (source_file ${check_programs})
diff --git a/programs/chargen_server_upcall.c b/programs/chargen_server_upcall.c
index 0b01ed88..06056bc0 100644
--- a/programs/chargen_server_upcall.c
+++ b/programs/chargen_server_upcall.c
@@ -67,14 +67,14 @@ initBuffer(void) {
unsigned int signCounter = 0;
static void
-handle_upcall(struct socket *sock, void *data, int flgs);
+handle_upcall(struct socket *upcall_socket, void *upcall_data, int upcall_flags);
static void
-handle_accept(struct socket *sock, void *data, int flags)
+handle_accept(struct socket *upcall_socket, void *upcall_data, int upcall_flags)
{
struct socket *conn_sock;
- if (((conn_sock = usrsctp_accept(sock, NULL, NULL)) == NULL)
+ if (((conn_sock = usrsctp_accept(upcall_socket, NULL, NULL)) == NULL)
&& (errno != EINPROGRESS)) {
perror("usrsctp_accept");
return;
@@ -85,9 +85,9 @@ handle_accept(struct socket *sock, void *data, int flags)
}
static void
-handle_upcall(struct socket *sock, void *data, int flgs)
+handle_upcall(struct socket *upcall_socket, void *upcall_data, int upcall_flags)
{
- int events = usrsctp_get_events(sock);
+ int events = usrsctp_get_events(upcall_socket);
if (events & SCTP_EVENT_READ && !send_done) {
char *buf;
@@ -95,31 +95,31 @@ handle_upcall(struct socket *sock, void *data, int flgs)
ssize_t n;
struct sockaddr_storage addr;
buf = malloc(BUFFERSIZE);
- int flags = 0;
+ int recv_flags = 0;
socklen_t len = (socklen_t)sizeof(struct sockaddr_storage);
unsigned int infotype = 0;
socklen_t infolen = sizeof(struct sctp_recvv_rn);
memset(&rn, 0, sizeof(struct sctp_recvv_rn));
- n = usrsctp_recvv(sock, buf, BUFFERSIZE, (struct sockaddr *) &addr, &len, (void *)&rn,
- &infolen, &infotype, &flags);
+ n = usrsctp_recvv(upcall_socket, buf, BUFFERSIZE, (struct sockaddr *) &addr, &len, (void *)&rn,
+ &infolen, &infotype, &recv_flags);
if (n < 0) {
perror("usrsctp_recvv");
done = 1;
- usrsctp_close(sock);
- printf("client socket %p closed\n", (void *)sock);
- sock = NULL;
+ usrsctp_close(upcall_socket);
+ printf("client socket %p closed\n", (void *)upcall_socket);
+ upcall_socket = NULL;
return;
}
if (n == 0) {
done = 1;
- usrsctp_close(sock);
- printf("client socket %p closed\n", (void *)sock);
- sock = NULL;
+ usrsctp_close(upcall_socket);
+ printf("client socket %p closed\n", (void *)upcall_socket);
+ upcall_socket = NULL;
return;
}
if (n > 0) {
- if (flags & MSG_NOTIFICATION) {
+ if (recv_flags & MSG_NOTIFICATION) {
printf("Notification of length %d received.\n", (int)n);
} else {
printf("data of size %d received\n", (int)n);
@@ -135,11 +135,11 @@ handle_upcall(struct socket *sock, void *data, int flgs)
snd_info.snd_ppid = 0;
snd_info.snd_context = 0;
snd_info.snd_assoc_id = 0;
- if (usrsctp_sendv(sock, buffer, strlen(buffer), NULL, 0, &snd_info, (socklen_t)sizeof(struct sctp_sndinfo), SCTP_SENDV_SNDINFO, 0) < 0) {
+ if (usrsctp_sendv(upcall_socket, buffer, strlen(buffer), NULL, 0, &snd_info, (socklen_t)sizeof(struct sctp_sndinfo), SCTP_SENDV_SNDINFO, 0) < 0) {
if (errno != EAGAIN) {
send_done = 1;
- usrsctp_close(sock);
- printf("client socket %p closed\n", (void *)sock);
+ usrsctp_close(upcall_socket);
+ printf("client socket %p closed\n", (void *)upcall_socket);
return;
}
}
diff --git a/programs/http_client_upcall.c b/programs/http_client_upcall.c
index e21a68cc..41633278 100644
--- a/programs/http_client_upcall.c
+++ b/programs/http_client_upcall.c
@@ -103,7 +103,9 @@ static void handle_upcall(struct socket *sock, void *arg, int flgs)
#ifdef _WIN32
_write(_fileno(stdout), buf, (unsigned int)n);
#else
- write(1, buf, n);
+ if (write(fileno(stdout), buf, n) < 0) {
+ perror("write");
+ }
#endif
done = 1;
usrsctp_close(sock);
diff --git a/programs/tsctp.c b/programs/tsctp.c
index c85da22b..1d4f7de3 100644
--- a/programs/tsctp.c
+++ b/programs/tsctp.c
@@ -66,9 +66,9 @@ struct socket *psock = NULL;
static struct timeval start_time;
unsigned int runtime = 0;
-static unsigned long messages = 0;
-static unsigned long long first_length = 0;
-static unsigned long long sum = 0;
+static unsigned long cb_messages = 0;
+static unsigned long long cb_first_length = 0;
+static unsigned long long cb_sum = 0;
static unsigned int use_cb = 0;
#ifndef timersub
@@ -91,7 +91,7 @@ char Usage[] =
" -c use callback API\n"
" -E local UDP encapsulation port (default 9899)\n"
" -f fragmentation point\n"
-" -l size of send/receive buffer\n"
+" -l message length\n"
" -L bind to local IP (default INADDR_ANY)\n"
" -n number of messages sent (0 means infinite)/received\n"
" -D turns Nagle off\n"
@@ -157,6 +157,9 @@ handle_connection(void *arg)
unsigned int infotype;
struct sctp_recvv_rn rn;
socklen_t infolen = sizeof(struct sctp_recvv_rn);
+ unsigned long messages = 0;
+ unsigned long long first_length = 0;
+ unsigned long long sum = 0;
conn_sock = *(struct socket **)arg;
#ifdef _WIN32
@@ -182,8 +185,7 @@ handle_connection(void *arg)
gettimeofday(&note_time, NULL);
printf("notification arrived at %f\n", note_time.tv_sec+(double)note_time.tv_usec/1000000.0);
snp = (union sctp_notification *)buf;
- if (snp->sn_header.sn_type==SCTP_PEER_ADDR_CHANGE)
- {
+ if (snp->sn_header.sn_type == SCTP_PEER_ADDR_CHANGE) {
spc = &snp->sn_paddr_change;
printf("SCTP_PEER_ADDR_CHANGE: state=%d, error=%d\n",spc->spc_state, spc->spc_error);
}
@@ -194,8 +196,9 @@ handle_connection(void *arg)
sum += n;
if (flags & MSG_EOR) {
messages++;
- if (first_length == 0)
+ if (first_length == 0) {
first_length = sum;
+ }
}
}
flags = 0;
@@ -206,13 +209,14 @@ handle_connection(void *arg)
n = usrsctp_recvv(conn_sock, (void *) buf, BUFFERSIZE, (struct sockaddr *) &addr, &len, (void *)&rn,
&infolen, &infotype, &flags);
}
- if (n < 0)
+ if (n < 0) {
perror("sctp_recvv");
+ }
gettimeofday(&time_now, NULL);
timersub(&time_now, &time_start, &time_diff);
seconds = time_diff.tv_sec + (double)time_diff.tv_usec/1000000.0;
- printf("%llu, %lu, %lu, %lu, %llu, %f, %f\n",
- first_length, messages, recv_calls, notifications, sum, seconds, (double)first_length * (double)messages / seconds);
+ printf("%llu, %lu, %lu, %llu, %f, %f, %lu\n",
+ first_length, messages, recv_calls, sum, seconds, (double)first_length * (double)messages / seconds, notifications);
fflush(stdout);
usrsctp_close(conn_sock);
free(buf);
@@ -227,7 +231,7 @@ static int
send_cb(struct socket *sock, uint32_t sb_free) {
struct sctp_sndinfo sndinfo;
- if ((messages == 0) & verbose) {
+ if ((cb_messages == 0) & verbose) {
printf("Start sending ");
if (number_of_messages > 0) {
printf("%ld messages ", (long)number_of_messages);
@@ -248,9 +252,9 @@ send_cb(struct socket *sock, uint32_t sb_free) {
sndinfo.snd_context = 0;
sndinfo.snd_assoc_id = 0;
- while (!done && ((number_of_messages == 0) || (messages < (number_of_messages - 1)))) {
+ while (!done && ((number_of_messages == 0) || (cb_messages < (number_of_messages - 1)))) {
if (very_verbose) {
- printf("Sending message number %lu.\n", messages + 1);
+ printf("Sending message number %lu.\n", cb_messages + 1);
}
if (usrsctp_sendv(psock, buffer, length,
@@ -262,17 +266,18 @@ send_cb(struct socket *sock, uint32_t sb_free) {
exit(1);
} else {
if (very_verbose){
- printf("EWOULDBLOCK or EAGAIN for message number %lu - will retry\n", messages + 1);
+ printf("EWOULDBLOCK or EAGAIN for message number %lu - will retry\n", cb_messages + 1);
}
/* send until EWOULDBLOCK then exit callback. */
return (1);
}
}
- messages++;
+ cb_messages++;
}
- if ((done == 1) || (messages == (number_of_messages - 1))) {
- if (very_verbose)
- printf("Sending final message number %lu.\n", messages + 1);
+ if ((done == 1) || (cb_messages == (number_of_messages - 1))) {
+ if (very_verbose) {
+ printf("Sending final message number %lu.\n", cb_messages + 1);
+ }
sndinfo.snd_flags |= SCTP_EOF;
if (usrsctp_sendv(psock, buffer, length, (struct sockaddr *) &remote_addr, 1,
@@ -283,13 +288,13 @@ send_cb(struct socket *sock, uint32_t sb_free) {
exit(1);
} else {
if (very_verbose){
- printf("EWOULDBLOCK or EAGAIN for final message number %lu - will retry\n", messages + 1);
+ printf("EWOULDBLOCK or EAGAIN for final message number %lu - will retry\n", cb_messages + 1);
}
/* send until EWOULDBLOCK then exit callback. */
return (1);
}
}
- messages++;
+ cb_messages++;
done = 2;
}
@@ -306,21 +311,21 @@ server_receive_cb(struct socket *sock, union sctp_sockstore addr, void *data,
if (data == NULL) {
gettimeofday(&now, NULL);
timersub(&now, &start_time, &diff_time);
- seconds = diff_time.tv_sec + (double)diff_time.tv_usec/1000000.0;
+ seconds = diff_time.tv_sec + (double)diff_time.tv_usec / 1000000.0;
printf("%llu, %lu, %llu, %f, %f\n",
- first_length, messages, sum, seconds, (double)first_length * (double)messages / seconds);
+ cb_first_length, cb_messages, cb_sum, seconds, (double)cb_first_length * (double)cb_messages / seconds);
usrsctp_close(sock);
- first_length = 0;
- sum = 0;
- messages = 0;
+ cb_first_length = 0;
+ cb_sum = 0;
+ cb_messages = 0;
return (1);
}
- if (first_length == 0) {
- first_length = (unsigned int)datalen;
+ if (cb_first_length == 0) {
+ cb_first_length = (unsigned int)datalen;
gettimeofday(&start_time, NULL);
}
- sum += datalen;
- messages++;
+ cb_sum += datalen;
+ cb_messages++;
free(data);
return (1);
@@ -362,6 +367,7 @@ int main(int argc, char **argv)
struct sctp_assoc_value av;
struct sctp_udpencaps encaps;
struct sctp_sndinfo sndinfo;
+ unsigned long messages = 0;
#ifdef _WIN32
unsigned long srcAddr;
HANDLE tid;
@@ -650,15 +656,15 @@ int main(int argc, char **argv)
if (use_cb) {
struct socket *conn_sock;
- if ((conn_sock = usrsctp_accept(psock, (struct sockaddr *) &remote_addr, &addr_len))== NULL) {
+ if ((conn_sock = usrsctp_accept(psock, (struct sockaddr *) &remote_addr, &addr_len)) == NULL) {
perror("usrsctp_accept");
continue;
}
} else {
struct socket **conn_sock;
- conn_sock = (struct socket **)malloc(sizeof(struct socket *));
- if ((*conn_sock = usrsctp_accept(psock, (struct sockaddr *) &remote_addr, &addr_len))== NULL) {
+ conn_sock = (struct socket **) malloc(sizeof(struct socket *));
+ if ((*conn_sock = usrsctp_accept(psock, (struct sockaddr *) &remote_addr, &addr_len)) == NULL) {
perror("usrsctp_accept");
continue;
}
@@ -747,7 +753,7 @@ int main(int argc, char **argv)
}
if (use_cb) {
- while (done < 2 && (messages < (number_of_messages - 1))) {
+ while (done < 2 && (cb_messages < (number_of_messages - 1))) {
#ifdef _WIN32
Sleep(1000);
#else
diff --git a/programs/tsctp_upcall.c b/programs/tsctp_upcall.c
new file mode 100644
index 00000000..7830628a
--- /dev/null
+++ b/programs/tsctp_upcall.c
@@ -0,0 +1,732 @@
+/*
+ * Copyright (C) 2005-2013 Michael Tuexen
+ * Copyright (C) 2011-2013 Irene Ruengeler
+ * Copyright (C) 2014-2019 Felix Weinrank
+ *
+ * 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.
+ */
+
+#include <sys/types.h>
+#ifdef _WIN32
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#include <stdlib.h>
+#include <crtdbg.h>
+#include <sys/timeb.h>
+#else
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <sys/time.h>
+#include <unistd.h>
+#include <pthread.h>
+#endif
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <signal.h>
+#include <errno.h>
+#include <inttypes.h>
+#ifdef LINUX
+#include <getopt.h>
+#endif
+#include <usrsctp.h>
+
+#define TSCTP_CLIENT 1
+#define TSCTP_SERVER 2
+
+#define DEFAULT_LENGTH 1024
+#define DEFAULT_NUMBER_OF_MESSAGES 1024
+#define DEFAULT_PORT 5001
+#define BUFFERSIZE (1<<16)
+
+static int par_verbose = 0;
+static int par_very_verbose = 0;
+static unsigned int done = 0;
+
+struct tsctp_meta {
+ uint8_t par_role;
+ uint8_t par_stats_human;
+ uint8_t par_ordered;
+ uint64_t par_messages;
+ uint64_t par_message_length;
+ uint64_t par_runtime;
+
+ uint64_t stat_messages;
+ uint64_t stat_message_length;
+ uint64_t stat_notifications;
+ uint64_t stat_recv_calls;
+ struct timeval stat_start;
+
+ uint64_t stat_fragment_sum;
+
+ char *buffer;
+};
+
+#ifndef timersub
+#define timersub(tvp, uvp, vvp) \
+ do { \
+ (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
+ (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
+ if ((vvp)->tv_usec < 0) { \
+ (vvp)->tv_sec--; \
+ (vvp)->tv_usec += 1000000; \
+ } \
+ } while (0)
+#endif
+
+
+char Usage[] =
+"Usage: tsctp [options] [address]\n"
+"Options:\n"
+" -a set adaptation layer indication\n"
+" -E local UDP encapsulation port (default 9899)\n"
+" -f fragmentation point\n"
+" -H human readable statistics"
+" -l message length\n"
+" -L bind to local IP (default INADDR_ANY)\n"
+" -n number of messages sent (0 means infinite)/received\n"
+" -D turns Nagle off\n"
+" -R socket recv buffer\n"
+" -S socket send buffer\n"
+" -T time to send messages\n"
+" -u use unordered user messages\n"
+" -U remote UDP encapsulation port\n"
+" -v verbose\n"
+" -V very verbose\n"
+;
+
+static void handle_upcall(struct socket *upcall_socket, void *upcall_data, int upcall_flags);
+
+static const char *bytes2human(uint64_t bytes)
+{
+ char *suffix[] = {"", "K", "M", "G", "T"};
+ char suffix_length = sizeof(suffix) / sizeof(suffix[0]);
+ int i = 0;
+ double human_size = bytes;
+ static char output[200];
+
+ if (bytes > 1024) {
+ for (i = 0; (bytes / 1024) > 0 && i < suffix_length - 1; i++) {
+ human_size = bytes / 1024.0;
+ bytes /= 1024;
+ }
+ }
+
+ snprintf(output, sizeof(output), "%.02lf %s", human_size, suffix[i]);
+ return output;
+}
+
+
+static void
+handle_accept(struct socket *upcall_socket, void *upcall_data, int upcall_flags)
+{
+ struct socket *conn_sock;
+ struct sockaddr_in remote_addr;
+ socklen_t addr_len = sizeof(struct sockaddr_in);
+ struct tsctp_meta *meta_listening, *meta_accepted;
+ char addrbuf[INET_ADDRSTRLEN];
+
+ meta_listening = (struct tsctp_meta *) upcall_data;
+
+ memset(&remote_addr, 0, sizeof(struct sockaddr_in));
+ if (((conn_sock = usrsctp_accept(upcall_socket, (struct sockaddr *) &remote_addr, &addr_len)) == NULL) && (errno != EINPROGRESS)) {
+ perror("usrsctp_accept");
+ exit(EXIT_FAILURE);
+ }
+
+ if (par_verbose) {
+ printf("Connection accepted from %s:%d\n", inet_ntop(AF_INET, &(remote_addr.sin_addr), addrbuf, INET_ADDRSTRLEN), ntohs(remote_addr.sin_port));
+ }
+
+ meta_accepted = malloc(sizeof(struct tsctp_meta));
+ memset(meta_accepted, 0, sizeof(struct tsctp_meta));
+
+ meta_accepted->par_role = meta_listening->par_role;
+ meta_accepted->par_stats_human = meta_listening->par_stats_human;
+ meta_accepted->buffer = malloc(BUFFERSIZE);
+
+ usrsctp_set_upcall(conn_sock, handle_upcall, meta_accepted);
+}
+
+static void
+handle_upcall(struct socket *upcall_socket, void *upcall_data, int upcall_flags)
+{
+ int events = usrsctp_get_events(upcall_socket);
+ struct tsctp_meta* tsctp_meta = (struct tsctp_meta*) upcall_data;
+
+ struct sctp_recvv_rn rn;
+ ssize_t n;
+ struct sockaddr_storage addr;
+ int recv_flags = 0;
+ socklen_t len = (socklen_t)sizeof(struct sockaddr_storage);
+ unsigned int infotype = 0;
+ socklen_t infolen = sizeof(struct sctp_recvv_rn);
+ struct sctp_rcvinfo *rcvinfo = (struct sctp_rcvinfo *) &rn;
+ memset(&rn, 0, sizeof(struct sctp_recvv_rn));
+ struct timeval note_time;
+ union sctp_notification *snp;
+ struct sctp_paddr_change *spc;
+ struct timeval time_now;
+ struct timeval time_diff;
+ float seconds;
+ struct sctp_sndinfo snd_info;
+
+ if (events & SCTP_EVENT_READ) {
+ while ((n = usrsctp_recvv(upcall_socket, tsctp_meta->buffer, BUFFERSIZE, (struct sockaddr *) &addr, &len, (void *)&rn, &infolen, &infotype, &recv_flags)) > 0) {
+
+ if (!tsctp_meta->stat_recv_calls) {
+ gettimeofday(&tsctp_meta->stat_start, NULL);
+ }
+ tsctp_meta->stat_recv_calls++;
+
+ if (recv_flags & MSG_NOTIFICATION) {
+ tsctp_meta->stat_notifications++;
+ gettimeofday(&note_time, NULL);
+ if (par_verbose) {
+ printf("notification arrived at %f\n", note_time.tv_sec + (double)note_time.tv_usec / 1000000.0);
+
+ snp = (union sctp_notification *)tsctp_meta->buffer;
+ if (snp->sn_header.sn_type == SCTP_PEER_ADDR_CHANGE) {
+ spc = &snp->sn_paddr_change;
+ printf("SCTP_PEER_ADDR_CHANGE: state=%d, error=%d\n",spc->spc_state, spc->spc_error);
+ }
+ }
+ } else {
+ if (par_very_verbose) {
+ if (infotype == SCTP_RECVV_RCVINFO) {
+ printf("Message received - %zd bytes - %s - sid %u - tsn %u %s\n",
+ n,
+ (rcvinfo->rcv_flags & SCTP_UNORDERED) ? "unordered" : "ordered",
+ rcvinfo->rcv_sid,
+ rcvinfo->rcv_tsn,
+ (recv_flags & MSG_EOR) ? "- EOR" : ""
+ );
+
+ } else {
+ printf("Message received - %zd bytes %s\n", n, (recv_flags & MSG_EOR) ? "- EOR" : "");
+ }
+ }
+ tsctp_meta->stat_fragment_sum += n;
+ if (recv_flags & MSG_EOR) {
+ tsctp_meta->stat_messages++;
+ if (tsctp_meta->stat_message_length == 0) {
+ tsctp_meta->stat_message_length = tsctp_meta->stat_fragment_sum;
+ }
+ }
+ }
+ }
+
+ if (n < 0 && errno != EAGAIN) {
+ perror("usrsctp_recvv");
+ exit(EXIT_FAILURE);
+ }
+
+ if (n == 0) {
+ done = 1;
+
+ gettimeofday(&time_now, NULL);
+ timersub(&time_now, &tsctp_meta->stat_start, &time_diff);
+ seconds = time_diff.tv_sec + (double)time_diff.tv_usec / 1000000.0;
+
+ if (tsctp_meta->par_stats_human) {
+ printf("Connection closed - statistics\n");
+
+ printf("\tmessage size : %" PRIu64 "\n", tsctp_meta->stat_message_length);
+ printf("\tmessages : %" PRIu64 "\n", tsctp_meta->stat_messages);
+ printf("\trecv() calls : %" PRIu64 "\n", tsctp_meta->stat_recv_calls);
+ printf("\tnotifications : %" PRIu64 "\n", tsctp_meta->stat_notifications);
+ printf("\ttransferred : %sByte\n", bytes2human(tsctp_meta->stat_message_length * tsctp_meta->stat_messages));
+ printf("\truntime : %.2f s\n", seconds);
+ printf("\tgoodput : %sBit/s\n", bytes2human((double) tsctp_meta->stat_message_length * (double) tsctp_meta->stat_messages / seconds * 8));
+
+ } else {
+ printf("%" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %f, %f, %" PRIu64 "\n",
+ tsctp_meta->stat_message_length,
+ tsctp_meta->stat_messages,
+ tsctp_meta->stat_recv_calls,
+ tsctp_meta->stat_message_length * tsctp_meta->stat_messages,
+ seconds,
+ (double) tsctp_meta->stat_message_length * (double) tsctp_meta->stat_messages / seconds,
+ tsctp_meta->stat_notifications);
+ }
+ fflush(stdout);
+ usrsctp_close(upcall_socket);
+ }
+ }
+
+ if ((events & SCTP_EVENT_WRITE) && tsctp_meta->par_role == TSCTP_CLIENT && !done) {
+
+ memset(&snd_info, 0, sizeof(struct sctp_sndinfo));
+ if (tsctp_meta->par_ordered == 0) {
+ snd_info.snd_flags |= SCTP_UNORDERED;
+ }
+
+ while ((n = usrsctp_sendv(upcall_socket, tsctp_meta->buffer, tsctp_meta->par_message_length, NULL, 0, &snd_info, (socklen_t)sizeof(struct sctp_sndinfo), SCTP_SENDV_SNDINFO, 0)) > 0) {
+ if (tsctp_meta->stat_messages == 0) {
+ gettimeofday(&tsctp_meta->stat_start, NULL);
+ }
+ tsctp_meta->stat_messages++;
+
+ if (par_very_verbose) {
+ printf("Message #%" PRIu64 " sent\n", tsctp_meta->stat_messages);
+ }
+
+ if (tsctp_meta->par_messages && tsctp_meta->par_messages == tsctp_meta->stat_messages) {
+ break;
+ }
+ }
+
+ if (errno != EAGAIN) {
+ done = 1;
+ usrsctp_close(upcall_socket);
+ printf("client socket %p closed\n", (void *)upcall_socket);
+ return;
+ }
+
+ gettimeofday(&time_now, NULL);
+ timersub(&time_now, &tsctp_meta->stat_start, &time_diff);
+ seconds = time_diff.tv_sec + (double)time_diff.tv_usec / 1000000.0;
+
+ if ((tsctp_meta->par_messages && tsctp_meta->par_messages == tsctp_meta->stat_messages) ||
+ (tsctp_meta->par_runtime && tsctp_meta->par_runtime <= seconds)) {
+
+ if (par_verbose) {
+ printf("Runtime or max messages reached - finishing...\n");
+ }
+
+ done = 1;
+ usrsctp_close(upcall_socket);
+ }
+ }
+
+ return;
+}
+
+#ifdef _WIN32
+static void
+gettimeofday(struct timeval *tv, void *ignore)
+{
+ struct timeb tb;
+
+ ftime(&tb);
+ tv->tv_sec = (long)tb.time;
+ tv->tv_usec = tb.millitm * 1000;
+}
+#endif
+
+void
+debug_printf(const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ vprintf(format, ap);
+ va_end(ap);
+}
+
+int main(int argc, char **argv)
+{
+#ifndef _WIN32
+ int c;
+#endif
+ struct socket *psock = NULL;
+ struct sockaddr_in local_addr;
+ struct sockaddr_in remote_addr;
+ int optval;
+ uint16_t local_port;
+ uint16_t remote_port;
+ uint16_t local_udp_port;
+ uint16_t remote_udp_port;
+ int rcvbufsize = 0;
+ int sndbufsize = 0;
+ socklen_t intlen;
+ int nodelay = 0;
+ struct sctp_assoc_value av;
+ struct sctp_udpencaps encaps;
+ struct tsctp_meta *meta;
+
+ uint16_t par_port = DEFAULT_PORT;
+ uint8_t par_stats_human = 0;
+ int par_ordered = 1;
+ int par_message_length = DEFAULT_LENGTH;
+ int par_messages = DEFAULT_NUMBER_OF_MESSAGES;
+ int par_runtime = 0;
+
+#ifdef _WIN32
+ unsigned long src_addr;
+ HANDLE tid;
+#else
+ in_addr_t src_addr;
+#endif
+ int fragpoint = 0;
+ struct sctp_setadaptation ind = {0};
+#ifdef _WIN32
+ char *opt;
+ int optind;
+#endif
+
+ remote_udp_port = 0;
+ local_udp_port = 9899;
+ src_addr = htonl(INADDR_ANY);
+
+ memset((void *) &remote_addr, 0, sizeof(struct sockaddr_in));
+ memset((void *) &local_addr, 0, sizeof(struct sockaddr_in));
+
+#ifndef _WIN32
+ while ((c = getopt(argc, argv, "a:cp:l:E:f:HL:n:R:S:T:uU:vVD")) != -1)
+ switch(c) {
+ case 'a':
+ ind.ssb_adaptation_ind = atoi(optarg);
+ break;
+ case 'l':
+ par_message_length = atoi(optarg);
+ break;
+ case 'n':
+ par_messages = atoi(optarg);
+ break;
+ case 'p':
+ par_port = atoi(optarg);
+ break;
+ case 'E':
+ local_udp_port = atoi(optarg);
+ break;
+ case 'f':
+ fragpoint = atoi(optarg);
+ break;
+ case 'H':
+ par_stats_human = 1;
+ break;
+ case 'L':
+ if (inet_pton(AF_INET, optarg, &src_addr) != 1) {
+ printf("Can't parse %s\n", optarg);
+ }
+ break;
+ case 'R':
+ rcvbufsize = atoi(optarg);
+ break;
+ case 'S':
+ sndbufsize = atoi(optarg);
+ break;
+ case 'T':
+ par_runtime = atoi(optarg);
+ par_messages = 0;
+ break;
+ case 'u':
+ par_ordered = 0;
+ break;
+ case 'U':
+ remote_udp_port = atoi(optarg);
+ break;
+ case 'v':
+ par_verbose = 1;
+ break;
+ case 'V':
+ par_verbose = 1;
+ par_very_verbose = 1;
+ break;
+ case 'D':
+ nodelay = 1;
+ break;
+ default:
+ fprintf(stderr, "%s", Usage);
+ exit(1);
+ }
+#else
+ for (optind = 1; optind < argc; optind++) {
+ if (argv[optind][0] == '-') {
+ switch (argv[optind][1]) {
+ case 'a':
+ if (++optind >= argc) {
+ printf("%s", Usage);
+ exit(1);
+ }
+ opt = argv[optind];
+ ind.ssb_adaptation_ind = atoi(opt);
+ break;
+ case 'l':
+ if (++optind >= argc) {
+ printf("%s", Usage);
+ exit(1);
+ }
+ opt = argv[optind];
+ length = atoi(opt);
+ break;
+ case 'p':
+ if (++optind >= argc) {
+ printf("%s", Usage);
+ exit(1);
+ }
+ opt = argv[optind];
+ par_port = atoi(opt);
+ break;
+ case 'n':
+ if (++optind >= argc) {
+ printf("%s", Usage);
+ exit(1);
+ }
+ opt = argv[optind];
+ number_of_messages = atoi(opt);
+ break;
+ case 'f':
+ if (++optind >= argc) {
+ printf("%s", Usage);
+ exit(1);
+ }
+ opt = argv[optind];
+ fragpoint = atoi(opt);
+ break;
+ case 'L':
+ if (++optind >= argc) {
+ printf("%s", Usage);
+ exit(1);
+ }
+ opt = argv[optind];
+ inet_pton(AF_INET, opt, &src_addr);
+ break;
+ case 'U':
+ if (++optind >= argc) {
+ printf("%s", Usage);
+ exit(1);
+ }
+ opt = argv[optind];
+ remote_udp_port = atoi(opt);
+ break;
+ case 'E':
+ if (++optind >= argc) {
+ printf("%s", Usage);
+ exit(1);
+ }
+ opt = argv[optind];
+ local_udp_port = atoi(opt);
+ break;
+ case 'R':
+ if (++optind >= argc) {
+ printf("%s", Usage);
+ exit(1);
+ }
+ opt = argv[optind];
+ rcvbufsize = atoi(opt);
+ break;
+ case 'S':
+ if (++optind >= argc) {
+ printf("%s", Usage);
+ exit(1);
+ }
+ opt = argv[optind];
+ sndbufsize = atoi(opt);
+ break;
+ case 'T':
+ if (++optind >= argc) {
+ printf("%s", Usage);
+ exit(1);
+ }
+ opt = argv[optind];
+ runtime = atoi(opt);
+ number_of_messages = 0;
+ break;
+ case 'u':
+ unordered = 1;
+ break;
+ case 'v':
+ par_verbose = 1;
+ break;
+ case 'V':
+ par_verbose = 1;
+ par_very_verbose = 1;
+ break;
+ case 'D':
+ nodelay = 1;
+ break;
+ default:
+ printf("%s", Usage);
+ exit(1);
+ }
+ } else {
+ break;
+ }
+ }
+#endif
+
+ meta = malloc(sizeof(struct tsctp_meta));
+ memset(meta, 0, sizeof(struct tsctp_meta));
+
+ meta->buffer = malloc(BUFFERSIZE);
+
+ meta->par_stats_human = par_stats_human;
+ meta->par_message_length = par_message_length;
+ meta->par_messages = par_messages;
+ meta->par_ordered = par_ordered;
+ meta->par_runtime = par_runtime;
+
+ if (optind == argc) {
+ meta->par_role = TSCTP_SERVER;
+ local_port = par_port;
+ remote_port = 0;
+ } else {
+ meta->par_role = TSCTP_CLIENT;
+ local_port = 0;
+ remote_port = par_port;
+ }
+ local_addr.sin_family = AF_INET;
+#ifdef HAVE_SIN_LEN
+ local_addr.sin_len = sizeof(struct sockaddr_in);
+#endif
+ local_addr.sin_port = htons(local_port);
+ local_addr.sin_addr.s_addr = src_addr;
+
+ usrsctp_init(local_udp_port, NULL, debug_printf);
+#ifdef SCTP_DEBUG
+ usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL);
+#endif
+ usrsctp_sysctl_set_sctp_blackhole(2);
+ usrsctp_sysctl_set_sctp_enable_sack_immediately(1);
+
+ if (!(psock = usrsctp_socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP, NULL, NULL, 0, NULL))) {
+ perror("user_socket");
+ exit(EXIT_FAILURE);
+ }
+
+ optval = 1;
+ if (usrsctp_setsockopt(psock, IPPROTO_SCTP, SCTP_RECVRCVINFO, &optval, sizeof(optval)) < 0) {
+ perror("usrsctp_setsockopt SCTP_RECVRCVINFO");
+ }
+
+ usrsctp_set_non_blocking(psock, 1);
+
+ if (usrsctp_bind(psock, (struct sockaddr *) &local_addr, sizeof(struct sockaddr_in)) == -1) {
+ perror("usrsctp_bind");
+ exit(1);
+ }
+
+ if (usrsctp_setsockopt(psock, IPPROTO_SCTP, SCTP_ADAPTATION_LAYER, (const void*)&ind, (socklen_t)sizeof(struct sctp_setadaptation)) < 0) {
+ perror("setsockopt");
+ }
+
+ if (meta->par_role == TSCTP_SERVER) {
+ if (rcvbufsize) {
+ if (usrsctp_setsockopt(psock, SOL_SOCKET, SO_RCVBUF, &rcvbufsize, sizeof(int)) < 0) {
+ perror("setsockopt: rcvbuf");
+ }
+ }
+ if (par_verbose) {
+ intlen = sizeof(int);
+ if (usrsctp_getsockopt(psock, SOL_SOCKET, SO_RCVBUF, &rcvbufsize, (socklen_t *)&intlen) < 0) {
+ perror("getsockopt: rcvbuf");
+ } else {
+ fprintf(stdout, "Receive buffer size: %d.\n", rcvbufsize);
+ }
+ }
+
+ if (usrsctp_listen(psock, 1) < 0) {
+ perror("usrsctp_listen");
+ exit(EXIT_FAILURE);
+ }
+
+ usrsctp_set_upcall(psock, handle_accept, meta);
+
+ while (1) {
+ sleep(1);
+ }
+
+ } else {
+ memset(&encaps, 0, sizeof(struct sctp_udpencaps));
+ encaps.sue_address.ss_family = AF_INET;
+ encaps.sue_port = htons(remote_udp_port);
+ if (usrsctp_setsockopt(psock, IPPROTO_SCTP, SCTP_REMOTE_UDP_ENCAPS_PORT, (const void*)&encaps, (socklen_t)sizeof(struct sctp_udpencaps)) < 0) {
+ perror("setsockopt");
+ }
+
+ remote_addr.sin_family = AF_INET;
+#ifdef HAVE_SIN_LEN
+ remote_addr.sin_len = sizeof(struct sockaddr_in);
+#endif
+ if (!inet_pton(AF_INET, argv[optind], &remote_addr.sin_addr.s_addr)){
+ printf("error: invalid destination address\n");
+ exit(EXIT_FAILURE);
+ }
+ remote_addr.sin_port = htons(remote_port);
+
+ memset(meta->buffer, 'X', BUFFERSIZE);
+
+ usrsctp_set_upcall(psock, handle_upcall, meta);
+
+ usrsctp_setsockopt(psock, IPPROTO_SCTP, SCTP_NODELAY, &nodelay, sizeof(nodelay));
+
+ if (fragpoint) {
+ av.assoc_id = 0;
+ av.assoc_value = fragpoint;
+ if (usrsctp_setsockopt(psock, IPPROTO_SCTP, SCTP_MAXSEG, &av, sizeof(struct sctp_assoc_value)) < 0) {
+ perror("setsockopt: SCTP_MAXSEG");
+ }
+ }
+
+ if (sndbufsize) {
+ if (usrsctp_setsockopt(psock, SOL_SOCKET, SO_SNDBUF, &sndbufsize, sizeof(int)) < 0) {
+ perror("setsockopt: sndbuf");
+ }
+ }
+
+ if (par_verbose) {
+ intlen = sizeof(int);
+ if (usrsctp_getsockopt(psock, SOL_SOCKET, SO_SNDBUF, &sndbufsize, (socklen_t *)&intlen) < 0) {
+ perror("setsockopt: SO_SNDBUF");
+ } else {
+ fprintf(stdout,"Send buffer size: %d.\n", sndbufsize);
+ }
+ }
+
+ if (usrsctp_connect(psock, (struct sockaddr *) &remote_addr, sizeof(struct sockaddr_in)) == -1 ) {
+ if (errno != EINPROGRESS) {
+ perror("usrsctp_connect");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ while (!done) {
+#ifdef _WIN32
+ Sleep(1000);
+#else
+ sleep(1);
+#endif
+ }
+
+ if (par_verbose) {
+ printf("Finished... \n");
+ }
+ }
+
+ while (usrsctp_finish() != 0) {
+#ifdef _WIN32
+ Sleep(1000);
+#else
+ sleep(1);
+#endif
+ }
+ return 0;
+}