- struct udp_target *ut = para_calloc(sizeof(struct udp_target));
- ut->port = port;
- ut->addr = *addr;
- ut->fd = -1; /* not yet connected */
- PARA_INFO_LOG("adding to target list (%s:%d)\n",
- TARGET_ADDR(ut), ut->port);
- para_list_add(&ut->node, &targets);
+ int ret;
+ socklen_t errlen = sizeof(ret);
+
+ if (getsockopt(ut->sc->fd, SOL_SOCKET, SO_ERROR, &ret, &errlen) < 0) {
+ PARA_ERROR_LOG("SO_ERROR failed: %s\n", strerror(ret));
+ return 0;
+ } else if (ret == 0) {
+ return 0;
+ } else if (ret == ECONNREFUSED) {
+ time_t dist = now->tv_sec - ut->last_unreachable;
+
+ if (dist <= UDP_MAX_UNREACHABLE_TIME) {
+ return 0;
+ } else if (dist > 2 * UDP_MAX_UNREACHABLE_TIME) {
+ ut->last_unreachable = now->tv_sec;
+ return 0;
+ } else {
+ /*
+ * unreachable_time < dist <= 2 * unreachable_time
+ * No errors are allowed during this time window.
+ */
+ PARA_NOTICE_LOG("Evicting %s after %d seconds "
+ "of connection errors.\n",
+ ut->sc->name, (int)dist);
+ }
+ }
+ return -ERRNO_TO_PARA_ERROR(ret);
+}
+
+static int udp_send_fec(struct sender_client *sc, char *buf, size_t len)
+{
+ struct udp_target *ut = sc->private_data;
+ int ret;
+
+ if (sender_status == SENDER_OFF)
+ return 0;
+ if (len == 0 && !cq_peek(ut->sc->cq))
+ return 0;
+ ret = udp_check_socket_state(ut);
+ if (ret < 0)
+ goto fail;
+ ret = send_queued_chunks(sc->fd, sc->cq);
+ if (ret < 0)
+ goto fail;
+ if (!ret) { /* still data left in the queue */
+ ret = cq_force_enqueue(sc->cq, buf, len);
+ assert(ret >= 0);
+ return 0;
+ }
+ ret = write_nonblock(sc->fd, buf, len);
+ if (ret == -ERRNO_TO_PARA_ERROR(ECONNREFUSED)) {
+ /*
+ * Happens if meanwhile an ICMP Destination / Port Unreachable
+ * has arrived. Ignore, persistent errors will be caught above.
+ */
+ ret = 0;
+ }
+ if (ret < 0)
+ goto fail;
+ if (ret != len) {
+ ret = cq_force_enqueue(sc->cq, buf + ret, len - ret);
+ assert(ret >= 0);
+ }
+ return 1;
+fail:
+ udp_delete_target(ut, para_strerror(-ret));
+ return ret;