10 #include <sys/socket.h>
20 struct utcp_connection *c;
21 int dir = DIR_READ | DIR_WRITE;
28 long reorder_dist = 10;
39 int reorder_countdown;
42 static void debug(const char *format, ...) {
47 clock_gettime(CLOCK_REALTIME, &tv);
48 len = snprintf(buf, sizeof(buf), "%ld.%06lu ", (long)tv.tv_sec, tv.tv_nsec / 1000);
51 len += vsnprintf(buf + len, sizeof(buf) - len, format, ap);
54 if(len > 0 && (size_t)len < sizeof(buf)) {
55 fwrite(buf, len, 1, stderr);
59 #define debug(...) do {} while(0)
62 ssize_t do_recv(struct utcp_connection *c, const void *data, size_t len) {
67 debug("Error: %s\n", strerror(errno));
71 debug("Connection closed by peer\n");
80 if(fread(buf, len, 1, reference) != 1) {
81 debug("Error reading reference\n");
85 if(memcmp(buf, data, len)) {
86 debug("Received data differs from reference\n");
91 return write(1, data, len);
94 void do_accept(struct utcp_connection *nc, uint16_t port) {
96 utcp_accept(nc, do_recv, NULL);
100 utcp_set_sndbuf(c, bufsize);
101 utcp_set_rcvbuf(c, bufsize);
104 utcp_set_accept_cb(c->utcp, NULL, NULL);
107 ssize_t do_send(struct utcp *utcp, const void *data, size_t len) {
108 int s = *(int *)utcp->priv;
111 if(outpktno >= dropfrom && outpktno < dropto) {
112 if(drand48() < dropout) {
113 debug("Dropped outgoing packet\n");
117 if(!reorder_data && drand48() < reorder) {
118 reorder_data = malloc(len);
121 debug("Out of memory\n");
126 memcpy(reorder_data, data, len);
127 reorder_countdown = 1 + drand48() * reorder_dist;
133 if(--reorder_countdown < 0) {
134 total_out += reorder_len;
135 send(s, reorder_data, reorder_len, MSG_DONTWAIT);
142 ssize_t result = send(s, data, len, MSG_DONTWAIT);
145 debug("Error sending UDP packet: %s\n", strerror(errno));
151 static void set_mtu(struct utcp *u, int s) {
153 socklen_t optlen = sizeof(mtu);
154 getsockopt(s, IPPROTO_IP, IP_MTU, &mtu, &optlen);
157 if(!mtu || mtu == 65535) {
161 debug("Using MTU %lu\n", mtu);
163 utcp_set_mtu(u, mtu ? mtu - 48 : 1300);
166 int main(int argc, char *argv[]) {
170 if(argc < 2 || argc > 3) {
174 bool server = argc == 2;
175 bool connected = false;
176 uint32_t flags = UTCP_TCP;
177 size_t read_size = 102400;
179 if(getenv("DROPIN")) {
180 dropin = atof(getenv("DROPIN"));
183 if(getenv("DROPOUT")) {
184 dropout = atof(getenv("DROPOUT"));
187 if(getenv("DROPFROM")) {
188 dropfrom = atoi(getenv("DROPFROM"));
191 if(getenv("DROPTO")) {
192 dropto = atoi(getenv("DROPTO"));
195 if(getenv("REORDER")) {
196 reorder = atof(getenv("REORDER"));
199 if(getenv("REORDER_DIST")) {
200 reorder_dist = atoi(getenv("REORDER_DIST"));
203 if(getenv("FLAGS")) {
204 flags = atoi(getenv("FLAGS"));
207 if(getenv("READ_SIZE")) {
208 read_size = atoi(getenv("READ_SIZE"));
212 mtu = atoi(getenv("MTU"));
215 if(getenv("BUFSIZE")) {
216 bufsize = atoi(getenv("BUFSIZE"));
219 char *reference_filename = getenv("REFERENCE");
221 if(reference_filename) {
222 reference = fopen(reference_filename, "r");
225 if(dropto < dropfrom) {
231 struct addrinfo hint = {
232 .ai_flags = server ? AI_PASSIVE : 0,
233 .ai_socktype = SOCK_DGRAM,
236 getaddrinfo(server ? NULL : argv[1], server ? argv[1] : argv[2], &hint, &ai);
239 debug("Could not lookup address: %s\n", strerror(errno));
243 int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
246 debug("Could not create socket: %s\n", strerror(errno));
250 static const int one = 1;
251 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
254 if(bind(s, ai->ai_addr, ai->ai_addrlen)) {
255 debug("Could not bind: %s\n", strerror(errno));
259 if(connect(s, ai->ai_addr, ai->ai_addrlen)) {
260 debug("Could not connect: %s\n", strerror(errno));
269 struct utcp *u = utcp_init(server ? do_accept : NULL, NULL, do_send, &s);
272 debug("Could not initialize UTCP\n");
276 utcp_set_user_timeout(u, 10);
280 c = utcp_connect_ex(u, 1, do_recv, NULL, flags);
283 utcp_set_sndbuf(c, bufsize);
284 utcp_set_rcvbuf(c, bufsize);
288 struct pollfd fds[2] = {
289 {.fd = 0, .events = POLLIN | POLLERR | POLLHUP},
290 {.fd = s, .events = POLLIN | POLLERR | POLLHUP},
295 struct timeval timeout = utcp_timeout(u);
297 while(!connected || utcp_is_active(u)) {
298 size_t max = c ? utcp_get_sndbuf_free(c) : 0;
300 if(max > sizeof(buf)) {
304 if(max > read_size) {
308 int timeout_ms = timeout.tv_sec * 1000 + timeout.tv_usec / 1000 + 1;
310 debug("polling, dir = %d, timeout = %d\n", dir, timeout_ms);
312 if((dir & DIR_READ) && max) {
313 poll(fds, 2, timeout_ms);
315 poll(fds + 1, 1, timeout_ms);
320 ssize_t len = read(0, buf, max);
321 debug("stdin %zd\n", len);
328 utcp_shutdown(c, SHUT_WR);
339 ssize_t sent = utcp_send(c, buf, len);
342 debug("Short send: %zd != %zd\n", sent, len);
349 struct sockaddr_storage ss;
350 socklen_t sl = sizeof(ss);
351 int len = recvfrom(s, buf, sizeof(buf), MSG_DONTWAIT, (struct sockaddr *)&ss, &sl);
352 debug("netin %zu\n", len);
355 debug("Error receiving UDP packet: %s\n", strerror(errno));
360 if(!connect(s, (struct sockaddr *)&ss, sl)) {
368 if(inpktno >= dropto || inpktno < dropfrom || drand48() >= dropin) {
371 if(utcp_recv(u, buf, len) == -1) {
372 debug("Error receiving UTCP packet: %s\n", strerror(errno));
375 debug("Dropped incoming packet\n");
379 timeout = utcp_timeout(u);
388 debug("Total bytes in: %ld, out: %ld\n", total_in, total_out);