10 #include <sys/socket.h>
20 struct utcp_connection *c;
21 int dir = DIR_READ | DIR_WRITE;
30 void debug(const char *format, ...) {
32 gettimeofday(&now, NULL);
33 fprintf(stderr, "%lu.%lu ", now.tv_sec, now.tv_usec / 1000);
36 vfprintf(stderr, format, ap);
40 ssize_t do_recv(struct utcp_connection *c, const void *data, size_t len) {
43 debug("Error: %s\n", strerror(errno));
47 debug("Connection closed by peer\n");
51 return write(1, data, len);
54 void do_accept(struct utcp_connection *nc, uint16_t port) {
55 utcp_accept(nc, do_recv, NULL);
57 utcp_set_accept_cb(c->utcp, NULL, NULL);
60 ssize_t do_send(struct utcp *utcp, const void *data, size_t len) {
61 int s = *(int *)utcp->priv;
63 if(outpktno < dropto && outpktno >= dropfrom && drand48() < dropout)
66 ssize_t result = send(s, data, len, MSG_DONTWAIT);
68 debug("Error sending UDP packet: %s\n", strerror(errno));
72 int main(int argc, char *argv[]) {
76 if(argc < 2 || argc > 3)
79 bool server = argc == 2;
80 bool connected = false;
82 dropin = atof(getenv("DROPIN") ?: "0");
83 dropout = atof(getenv("DROPOUT") ?: "0");
84 dropfrom = atoi(getenv("DROPFROM") ?: "0");
85 dropto = atoi(getenv("DROPTO") ?: "0");
88 struct addrinfo hint = {
89 .ai_flags = server ? AI_PASSIVE : 0,
90 .ai_socktype = SOCK_DGRAM,
93 getaddrinfo(server ? NULL : argv[1], server ? argv[1] : argv[2], &hint, &ai);
97 int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
102 if(bind(s, ai->ai_addr, ai->ai_addrlen))
105 if(connect(s, ai->ai_addr, ai->ai_addrlen))
112 struct utcp *u = utcp_init(server ? do_accept : NULL, NULL, do_send, &s);
116 utcp_set_mtu(u, 1300);
117 utcp_set_user_timeout(u, 10);
120 c = utcp_connect(u, 1, do_recv, NULL);
122 struct pollfd fds[2] = {
123 {.fd = 0, .events = POLLIN | POLLERR | POLLHUP},
124 {.fd = s, .events = POLLIN | POLLERR | POLLHUP},
128 struct timeval timeout = utcp_timeout(u);
130 while(!connected || utcp_is_active(u)) {
132 size_t max = c ? utcp_get_sndbuf_free(c) : 0;
136 if((dir & DIR_READ) && max)
137 poll(fds, 2, timeout.tv_sec * 1000 + timeout.tv_usec / 1000);
139 poll(fds + 1, 1, timeout.tv_sec * 1000 + timeout.tv_usec / 1000);
144 ssize_t len = read(0, buf, max);
149 utcp_shutdown(c, SHUT_WR);
156 ssize_t sent = utcp_send(c, buf, len);
158 debug("Short send: %zd != %zd\n", sent, len);
165 struct sockaddr_storage ss;
166 socklen_t sl = sizeof ss;
167 int len = recvfrom(s, buf, sizeof buf, MSG_DONTWAIT, (struct sockaddr *)&ss, &sl);
169 debug("Error receiving UDP packet: %s\n", strerror(errno));
173 if(!connect(s, (struct sockaddr *)&ss, sl))
176 if(inpktno >= dropto || inpktno < dropfrom || drand48() >= dropin)
177 utcp_recv(u, buf, len);
180 timeout = utcp_timeout(u);