+static inline uint32_t absdiff(uint32_t a, uint32_t b) {
+ if(a > b) {
+ return a - b;
+ } else {
+ return b - a;
+ }
+}
+
+// Update RTT variables. See RFC 6298.
+static void update_rtt(struct utcp_connection *c, uint32_t rtt) {
+ if(!rtt) {
+ debug("invalid rtt\n");
+ return;
+ }
+
+ struct utcp *utcp = c->utcp;
+
+ if(!utcp->srtt) {
+ utcp->srtt = rtt;
+ utcp->rttvar = rtt / 2;
+ utcp->rto = rtt + max(2 * rtt, CLOCK_GRANULARITY);
+ } else {
+ utcp->rttvar = (utcp->rttvar * 3 + absdiff(utcp->srtt, rtt)) / 4;
+ utcp->srtt = (utcp->srtt * 7 + rtt) / 8;
+ utcp->rto = utcp->srtt + max(utcp->rttvar, CLOCK_GRANULARITY);
+ }
+
+ if(utcp->rto > MAX_RTO) {
+ utcp->rto = MAX_RTO;
+ }
+
+ debug("rtt %u srtt %u rttvar %u rto %u\n", rtt, utcp->srtt, utcp->rttvar, utcp->rto);
+}
+
+static void start_retransmit_timer(struct utcp_connection *c) {
+ gettimeofday(&c->rtrx_timeout, NULL);
+ c->rtrx_timeout.tv_usec += c->utcp->rto;
+
+ while(c->rtrx_timeout.tv_usec >= 1000000) {
+ c->rtrx_timeout.tv_usec -= 1000000;
+ c->rtrx_timeout.tv_sec++;
+ }
+
+ debug("timeout set to %lu.%06lu (%u)\n", c->rtrx_timeout.tv_sec, c->rtrx_timeout.tv_usec, c->utcp->rto);
+}
+
+static void stop_retransmit_timer(struct utcp_connection *c) {
+ timerclear(&c->rtrx_timeout);
+ debug("timeout cleared\n");
+}
+
+struct utcp_connection *utcp_connect_ex(struct utcp *utcp, uint16_t dst, utcp_recv_t recv, void *priv, uint32_t flags) {