10 #include <sys/socket.h>
20 struct utcp_connection *c;
21 int dir = DIR_READ | DIR_WRITE;
28 long reorder_dist = 10;
38 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 ssize_t do_recv(struct utcp_connection *c, 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 void do_accept(struct utcp_connection *nc, uint16_t port) {
95 utcp_accept(nc, do_recv, NULL);
97 utcp_set_accept_cb(c->utcp, NULL, NULL);
100 ssize_t do_send(struct utcp *utcp, const void *data, size_t len) {
101 int s = *(int *)utcp->priv;
104 if(outpktno >= dropfrom && outpktno < dropto) {
105 if(drand48() < dropout) {
106 debug("Dropped outgoing packet\n");
110 if(!reorder_data && drand48() < reorder) {
111 reorder_data = malloc(len);
114 debug("Out of memory\n");
119 memcpy(reorder_data, data, len);
120 reorder_countdown = 1 + drand48() * reorder_dist;
126 if(--reorder_countdown < 0) {
127 total_out += reorder_len;
128 send(s, reorder_data, reorder_len, MSG_DONTWAIT);
135 ssize_t result = send(s, data, len, MSG_DONTWAIT);
138 debug("Error sending UDP packet: %s\n", strerror(errno));
144 static void set_mtu(struct utcp *u, int s) {
146 socklen_t optlen = sizeof(mtu);
147 getsockopt(s, IPPROTO_IP, IP_MTU, &mtu, &optlen);
150 if(!mtu || mtu == 65535) {
154 debug("Using MTU %lu\n", mtu);
156 utcp_set_mtu(u, mtu ? mtu - 48 : 1300);
159 int main(int argc, char *argv[]) {
163 if(argc < 2 || argc > 3) {
167 bool server = argc == 2;
168 bool connected = false;
169 uint32_t flags = UTCP_TCP;
170 size_t read_size = 102400;
172 if(getenv("DROPIN")) {
173 dropin = atof(getenv("DROPIN"));
176 if(getenv("DROPOUT")) {
177 dropout = atof(getenv("DROPOUT"));
180 if(getenv("DROPFROM")) {
181 dropfrom = atoi(getenv("DROPFROM"));
184 if(getenv("DROPTO")) {
185 dropto = atoi(getenv("DROPTO"));
188 if(getenv("REORDER")) {
189 reorder = atof(getenv("REORDER"));
192 if(getenv("REORDER_DIST")) {
193 reorder_dist = atoi(getenv("REORDER_DIST"));
196 if(getenv("FLAGS")) {
197 flags = atoi(getenv("FLAGS"));
200 if(getenv("READ_SIZE")) {
201 read_size = atoi(getenv("READ_SIZE"));
205 mtu = atoi(getenv("MTU"));
208 char *reference_filename = getenv("REFERENCE");
210 if(reference_filename) {
211 reference = fopen(reference_filename, "r");
214 if(dropto < dropfrom) {
220 struct addrinfo hint = {
221 .ai_flags = server ? AI_PASSIVE : 0,
222 .ai_socktype = SOCK_DGRAM,
225 getaddrinfo(server ? NULL : argv[1], server ? argv[1] : argv[2], &hint, &ai);
228 debug("Could not lookup address: %s\n", strerror(errno));
232 int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
235 debug("Could not create socket: %s\n", strerror(errno));
239 static const int one = 1;
240 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
243 if(bind(s, ai->ai_addr, ai->ai_addrlen)) {
244 debug("Could not bind: %s\n", strerror(errno));
248 if(connect(s, ai->ai_addr, ai->ai_addrlen)) {
249 debug("Could not connect: %s\n", strerror(errno));
258 struct utcp *u = utcp_init(server ? do_accept : NULL, NULL, do_send, &s);
261 debug("Could not initialize UTCP\n");
265 utcp_set_user_timeout(u, 10);
269 c = utcp_connect_ex(u, 1, do_recv, NULL, flags);
272 struct pollfd fds[2] = {
273 {.fd = 0, .events = POLLIN | POLLERR | POLLHUP},
274 {.fd = s, .events = POLLIN | POLLERR | POLLHUP},
279 struct timeval timeout = utcp_timeout(u);
281 while(!connected || utcp_is_active(u)) {
282 size_t max = c ? utcp_get_sndbuf_free(c) : 0;
284 if(max > sizeof(buf)) {
288 if(max > read_size) {
292 int timeout_ms = timeout.tv_sec * 1000 + timeout.tv_usec / 1000 + 1;
294 debug("polling, dir = %d, timeout = %d\n", dir, timeout_ms);
296 if((dir & DIR_READ) && max) {
297 poll(fds, 2, timeout_ms);
299 poll(fds + 1, 1, timeout_ms);
304 ssize_t len = read(0, buf, max);
305 debug("stdin %zd\n", len);
312 utcp_shutdown(c, SHUT_WR);
323 ssize_t sent = utcp_send(c, buf, len);
326 debug("Short send: %zd != %zd\n", sent, len);
333 struct sockaddr_storage ss;
334 socklen_t sl = sizeof(ss);
335 int len = recvfrom(s, buf, sizeof(buf), MSG_DONTWAIT, (struct sockaddr *)&ss, &sl);
336 debug("netin %zu\n", len);
339 debug("Error receiving UDP packet: %s\n", strerror(errno));
344 if(!connect(s, (struct sockaddr *)&ss, sl)) {
352 if(inpktno >= dropto || inpktno < dropfrom || drand48() >= dropin) {
355 if(utcp_recv(u, buf, len) == -1) {
356 debug("Error receiving UTCP packet: %s\n", strerror(errno));
359 debug("Dropped incoming packet\n");
363 timeout = utcp_timeout(u);
372 debug("Total bytes in: %ld, out: %ld\n", total_in, total_out);