10 #include <sys/socket.h>
20 struct utcp_connection *c;
21 int dir = DIR_READ | DIR_WRITE;
28 long reorder_dist = 10;
36 int reorder_countdown;
39 void debug(const char *format, ...) {
41 gettimeofday(&now, NULL);
42 fprintf(stderr, "%lu.%lu ", now.tv_sec, now.tv_usec / 1000);
45 vfprintf(stderr, format, ap);
52 ssize_t do_recv(struct utcp_connection *c, const void *data, size_t len) {
55 debug("Error: %s\n", strerror(errno));
59 debug("Connection closed by peer\n");
63 return write(1, data, len);
66 void do_accept(struct utcp_connection *nc, uint16_t port) {
67 utcp_accept(nc, do_recv, NULL);
69 utcp_set_accept_cb(c->utcp, NULL, NULL);
72 ssize_t do_send(struct utcp *utcp, const void *data, size_t len) {
73 int s = *(int *)utcp->priv;
75 if(outpktno >= dropfrom && outpktno < dropto) {
76 if(drand48() < dropout) {
77 debug("Dropped outgoing packet\n");
80 if(!reorder_data && drand48() < reorder) {
81 reorder_data = malloc(len);
83 debug("Out of memory\n");
87 memcpy(reorder_data, data, len);
88 reorder_countdown = 1 + drand48() * reorder_dist;
94 if(--reorder_countdown < 0) {
95 total_out += reorder_len;
96 send(s, reorder_data, reorder_len, MSG_DONTWAIT);
103 ssize_t result = send(s, data, len, MSG_DONTWAIT);
105 debug("Error sending UDP packet: %s\n", strerror(errno));
109 int main(int argc, char *argv[]) {
113 if(argc < 2 || argc > 3)
116 bool server = argc == 2;
117 bool connected = false;
119 if(getenv("DROPIN")) dropin = atof(getenv("DROPIN"));
120 if(getenv("DROPOUT")) dropout = atof(getenv("DROPOUT"));
121 if(getenv("DROPFROM")) dropfrom = atoi(getenv("DROPFROM"));
122 if(getenv("DROPTO")) dropto = atoi(getenv("DROPTO"));
123 if(getenv("REORDER")) reorder = atof(getenv("REORDER"));
124 if(getenv("REORDER_DIST")) reorder_dist = atoi(getenv("REORDER_DIST"));
126 if(dropto < dropfrom)
130 struct addrinfo hint = {
131 .ai_flags = server ? AI_PASSIVE : 0,
132 .ai_socktype = SOCK_DGRAM,
135 getaddrinfo(server ? NULL : argv[1], server ? argv[1] : argv[2], &hint, &ai);
139 int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
144 if(bind(s, ai->ai_addr, ai->ai_addrlen))
147 if(connect(s, ai->ai_addr, ai->ai_addrlen))
154 struct utcp *u = utcp_init(server ? do_accept : NULL, NULL, do_send, &s);
158 utcp_set_mtu(u, 1300);
159 utcp_set_user_timeout(u, 10);
162 c = utcp_connect(u, 1, do_recv, NULL);
164 struct pollfd fds[2] = {
165 {.fd = 0, .events = POLLIN | POLLERR | POLLHUP},
166 {.fd = s, .events = POLLIN | POLLERR | POLLHUP},
170 struct timeval timeout = utcp_timeout(u);
172 while(!connected || utcp_is_active(u)) {
173 size_t max = c ? utcp_get_sndbuf_free(c) : 0;
177 int timeout_ms = timeout.tv_sec * 1000 + timeout.tv_usec / 1000 + 1;
179 debug("polling, dir = %d, timeout = %d\n", dir, timeout_ms);
180 if((dir & DIR_READ) && max)
181 poll(fds, 2, timeout_ms);
183 poll(fds + 1, 1, timeout_ms);
188 ssize_t len = read(0, buf, max);
193 utcp_shutdown(c, SHUT_WR);
200 ssize_t sent = utcp_send(c, buf, len);
202 debug("Short send: %zd != %zd\n", sent, len);
209 struct sockaddr_storage ss;
210 socklen_t sl = sizeof ss;
211 int len = recvfrom(s, buf, sizeof buf, MSG_DONTWAIT, (struct sockaddr *)&ss, &sl);
213 debug("Error receiving UDP packet: %s\n", strerror(errno));
217 if(!connect(s, (struct sockaddr *)&ss, sl))
220 if(inpktno >= dropto || inpktno < dropfrom || drand48() >= dropin) {
222 if(utcp_recv(u, buf, len) == -1)
223 debug("Error receiving UTCP packet: %s\n", strerror(errno));
225 debug("Dropped incoming packet\n");
229 timeout = utcp_timeout(u);
236 debug("Total bytes in: %ld, out: %ld\n", total_in, total_out);