10 #include <sys/socket.h>
20 static struct utcp_connection *c;
21 static int dir = DIR_READ | DIR_WRITE;
26 static double reorder;
27 static long reorder_dist = 10;
29 static double dropout;
30 static long total_out;
32 static FILE *reference;
36 static char *reorder_data;
37 static size_t reorder_len;
38 static int reorder_countdown;
41 static void debug(const char *format, ...) {
46 clock_gettime(CLOCK_REALTIME, &tv);
47 len = snprintf(buf, sizeof(buf), "%ld.%06lu ", (long)tv.tv_sec, tv.tv_nsec / 1000);
50 len += vsnprintf(buf + len, sizeof(buf) - len, format, ap);
53 if(len > 0 && (size_t)len < sizeof(buf)) {
54 fwrite(buf, len, 1, stderr);
58 #define debug(...) do {} while(0)
61 static ssize_t do_recv(struct utcp_connection *rc, const void *data, size_t len) {
66 debug("Error: %s\n", strerror(errno));
70 debug("Connection closed by peer\n");
79 if(fread(buf, len, 1, reference) != 1) {
80 debug("Error reading reference\n");
84 if(memcmp(buf, data, len)) {
85 debug("Received data differs from reference\n");
90 return write(1, data, len);
93 static void do_accept(struct utcp_connection *nc, uint16_t port) {
95 utcp_accept(nc, do_recv, NULL);
99 utcp_set_sndbuf(c, bufsize);
100 utcp_set_rcvbuf(c, bufsize);
103 utcp_set_accept_cb(c->utcp, NULL, NULL);
106 static ssize_t do_send(struct utcp *utcp, const void *data, size_t len) {
107 int s = *(int *)utcp->priv;
110 if(outpktno >= dropfrom && outpktno < dropto) {
111 if(drand48() < dropout) {
112 debug("Dropped outgoing packet\n");
116 if(!reorder_data && drand48() < reorder) {
117 reorder_data = malloc(len);
120 debug("Out of memory\n");
125 memcpy(reorder_data, data, len);
126 reorder_countdown = 1 + drand48() * reorder_dist;
132 if(--reorder_countdown < 0) {
133 total_out += reorder_len;
134 send(s, reorder_data, reorder_len, MSG_DONTWAIT);
141 ssize_t result = send(s, data, len, MSG_DONTWAIT);
144 debug("Error sending UDP packet: %s\n", strerror(errno));
150 static void set_mtu(struct utcp *u, int s) {
152 socklen_t optlen = sizeof(mtu);
153 getsockopt(s, IPPROTO_IP, IP_MTU, &mtu, &optlen);
156 if(!mtu || mtu == 65535) {
160 debug("Using MTU %lu\n", mtu);
162 utcp_set_mtu(u, mtu ? mtu - 28 : 1300);
165 int main(int argc, char *argv[]) {
169 if(argc < 2 || argc > 3) {
173 bool server = argc == 2;
174 bool connected = false;
175 uint32_t flags = UTCP_TCP;
176 size_t read_size = 102400;
178 if(getenv("DROPIN")) {
179 dropin = atof(getenv("DROPIN"));
182 if(getenv("DROPOUT")) {
183 dropout = atof(getenv("DROPOUT"));
186 if(getenv("DROPFROM")) {
187 dropfrom = atoi(getenv("DROPFROM"));
190 if(getenv("DROPTO")) {
191 dropto = atoi(getenv("DROPTO"));
194 if(getenv("REORDER")) {
195 reorder = atof(getenv("REORDER"));
198 if(getenv("REORDER_DIST")) {
199 reorder_dist = atoi(getenv("REORDER_DIST"));
202 if(getenv("FLAGS")) {
203 flags = atoi(getenv("FLAGS"));
206 if(getenv("READ_SIZE")) {
207 read_size = atoi(getenv("READ_SIZE"));
211 mtu = atoi(getenv("MTU"));
214 if(getenv("BUFSIZE")) {
215 bufsize = atoi(getenv("BUFSIZE"));
218 char *reference_filename = getenv("REFERENCE");
220 if(reference_filename) {
221 reference = fopen(reference_filename, "r");
224 if(dropto < dropfrom) {
230 struct addrinfo hint = {
231 .ai_flags = server ? AI_PASSIVE : 0,
232 .ai_socktype = SOCK_DGRAM,
235 getaddrinfo(server ? NULL : argv[1], server ? argv[1] : argv[2], &hint, &ai);
238 debug("Could not lookup address: %s\n", strerror(errno));
242 int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
245 debug("Could not create socket: %s\n", strerror(errno));
249 static const int one = 1;
250 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
253 if(bind(s, ai->ai_addr, ai->ai_addrlen)) {
254 debug("Could not bind: %s\n", strerror(errno));
258 if(connect(s, ai->ai_addr, ai->ai_addrlen)) {
259 debug("Could not connect: %s\n", strerror(errno));
268 struct utcp *u = utcp_init(server ? do_accept : NULL, NULL, do_send, &s);
271 debug("Could not initialize UTCP\n");
275 utcp_set_user_timeout(u, 10);
279 c = utcp_connect_ex(u, 1, do_recv, NULL, flags);
282 utcp_set_sndbuf(c, bufsize);
283 utcp_set_rcvbuf(c, bufsize);
287 struct pollfd fds[2] = {
288 {.fd = 0, .events = POLLIN | POLLERR | POLLHUP},
289 {.fd = s, .events = POLLIN | POLLERR | POLLHUP},
294 struct timespec timeout = utcp_timeout(u);
296 while(!connected || utcp_is_active(u)) {
297 size_t max = c ? utcp_get_sndbuf_free(c) : 0;
299 if(max > sizeof(buf)) {
303 if(max > read_size) {
307 int timeout_ms = timeout.tv_sec * 1000 + timeout.tv_nsec / 1000000 + 1;
309 debug("polling, dir = %d, timeout = %d\n", dir, timeout_ms);
311 if((dir & DIR_READ) && max) {
312 poll(fds, 2, timeout_ms);
314 poll(fds + 1, 1, timeout_ms);
319 ssize_t len = read(0, buf, max);
320 debug("stdin %zd\n", len);
327 utcp_shutdown(c, SHUT_WR);
338 ssize_t sent = utcp_send(c, buf, len);
341 debug("Short send: %zd != %zd\n", sent, len);
348 struct sockaddr_storage ss;
349 socklen_t sl = sizeof(ss);
350 int len = recvfrom(s, buf, sizeof(buf), MSG_DONTWAIT, (struct sockaddr *)&ss, &sl);
351 debug("netin %zu\n", len);
354 debug("Error receiving UDP packet: %s\n", strerror(errno));
359 if(!connect(s, (struct sockaddr *)&ss, sl)) {
367 if(inpktno >= dropto || inpktno < dropfrom || drand48() >= dropin) {
370 if(utcp_recv(u, buf, len) == -1) {
371 debug("Error receiving UTCP packet: %s\n", strerror(errno));
374 debug("Dropped incoming packet\n");
378 timeout = utcp_timeout(u);
387 debug("Total bytes in: %ld, out: %ld\n", total_in, total_out);