#include <stdio.h>
#include <stdlib.h>
+#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include "utcp.h"
+#define DIR_READ 1
+#define DIR_WRITE 2
+
struct utcp_connection *c;
-int dir = 3;
+int dir = DIR_READ | DIR_WRITE;
bool running = true;
+long inpktno;
+long outpktno;
+long dropfrom;
+long dropto;
double dropin;
double dropout;
+long total_out;
+long total_in;
+
+
+void debug(const char *format, ...) {
+ struct timeval now;
+ gettimeofday(&now, NULL);
+ fprintf(stderr, "%lu.%lu ", now.tv_sec, now.tv_usec / 1000);
+ va_list ap;
+ va_start(ap, format);
+ vfprintf(stderr, format, ap);
+ va_end(ap);
+}
-int do_recv(struct utcp_connection *c, const void *data, size_t len) {
+ssize_t do_recv(struct utcp_connection *c, const void *data, size_t len) {
if(!data || !len) {
if(errno) {
- fprintf(stderr, "Error: %s\n", strerror(errno));
+ debug("Error: %s\n", strerror(errno));
dir = 0;
} else {
- dir &= ~2;
- fprintf(stderr, "Connection closed by peer\n");
+ dir &= ~DIR_WRITE;
+ debug("Connection closed by peer\n");
}
- return 0;
+ return -1;
}
return write(1, data, len);
}
void do_accept(struct utcp_connection *nc, uint16_t port) {
utcp_accept(nc, do_recv, NULL);
c = nc;
+ utcp_set_accept_cb(c->utcp, NULL, NULL);
}
-int do_send(struct utcp *utcp, const void *data, size_t len) {
+ssize_t do_send(struct utcp *utcp, const void *data, size_t len) {
int s = *(int *)utcp->priv;
- if(drand48() >= dropout)
- return send(s, data, len, MSG_DONTWAIT);
- else
- return 0;
+ outpktno++;
+ if(outpktno < dropto && outpktno >= dropfrom && drand48() < dropout)
+ return len;
+
+ total_out += len;
+ ssize_t result = send(s, data, len, MSG_DONTWAIT);
+ if(result <= 0)
+ debug("Error sending UDP packet: %s\n", strerror(errno));
+ return result;
}
int main(int argc, char *argv[]) {
dropin = atof(getenv("DROPIN") ?: "0");
dropout = atof(getenv("DROPOUT") ?: "0");
+ dropfrom = atoi(getenv("DROPFROM") ?: "0");
+ dropto = atoi(getenv("DROPTO") ?: "0");
struct addrinfo *ai;
struct addrinfo hint = {
return 1;
int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
- if(s < 0)
+ if(s == -1)
return 1;
if(server) {
connected = true;
}
+ freeaddrinfo(ai);
+
struct utcp *u = utcp_init(server ? do_accept : NULL, NULL, do_send, &s);
if(!u)
return 1;
+ utcp_set_mtu(u, 1300);
utcp_set_user_timeout(u, 10);
if(!server)
{.fd = s, .events = POLLIN | POLLERR | POLLHUP},
};
- char buf[1024];
- int timeout = utcp_timeout(u);
+ char buf[102400];
+ struct timeval timeout = utcp_timeout(u);
+
+ while(!connected || utcp_is_active(u)) {
+ size_t max = c ? utcp_get_sndbuf_free(c) : 0;
+ if(max > sizeof buf)
+ max = sizeof buf;
+
+ int timeout_ms = timeout.tv_sec * 1000 + timeout.tv_usec / 1000 + 1;
- while(dir) {
- poll(fds, 2, timeout);
+ debug("polling, dir = %d, timeout = %d\n", dir, timeout_ms);
+ if((dir & DIR_READ) && max)
+ poll(fds, 2, timeout_ms);
+ else
+ poll(fds + 1, 1, timeout_ms);
if(fds[0].revents) {
- int len = read(0, buf, sizeof buf);
+ fds[0].revents = 0;
+ debug("stdin\n");
+ ssize_t len = read(0, buf, max);
if(len <= 0) {
fds[0].fd = -1;
- dir &= ~1;
+ dir &= ~DIR_READ;
if(c)
utcp_shutdown(c, SHUT_WR);
- if(len < 0)
+ if(len == -1)
break;
else
continue;
}
- if(c)
- utcp_send(c, buf, len);
+ if(c) {
+ ssize_t sent = utcp_send(c, buf, len);
+ if(sent != len)
+ debug("Short send: %zd != %zd\n", sent, len);
+ }
}
if(fds[1].revents) {
+ fds[1].revents = 0;
+ debug("netin\n");
struct sockaddr_storage ss;
- socklen_t sl;
+ socklen_t sl = sizeof ss;
int len = recvfrom(s, buf, sizeof buf, MSG_DONTWAIT, (struct sockaddr *)&ss, &sl);
- if(len <= 0)
+ if(len <= 0) {
+ debug("Error receiving UDP packet: %s\n", strerror(errno));
break;
+ }
if(!connected)
if(!connect(s, (struct sockaddr *)&ss, sl))
connected = true;
- if(drand48() >= dropin)
+ inpktno++;
+ if(inpktno >= dropto || inpktno < dropfrom || drand48() >= dropin) {
+ total_in += len;
utcp_recv(u, buf, len);
+ }
}
timeout = utcp_timeout(u);
utcp_close(c);
utcp_exit(u);
+ debug("Total bytes in: %ld, out: %ld\n", total_in, total_out);
+
return 0;
}