+ struct sender_client *sc = udp_lookup_target(scd);
+
+ if (sc) {
+ udp_delete_target(sc, "com_delete");
+ return 1;
+ }
+ PARA_NOTICE_LOG("not deleting non-existing target '%s'\n", scd->host);
+ return -E_TARGET_NOT_FOUND;
+}
+
+/** Initialize UDP session and set maximum payload size. */
+static int udp_init_fec(struct sender_client *sc)
+{
+ struct udp_target *ut = sc->private_data;
+ int mps;
+
+ PARA_NOTICE_LOG("sending to udp %s\n", sc->name);
+ ut->sent_fec_eof = false;
+ mps = generic_max_transport_msg_size(sc->fd) - sizeof(struct udphdr);
+ PARA_INFO_LOG("current MPS = %d bytes\n", mps);
+ return mps;
+}
+
+/** Check and clear socket error if any. */
+static int udp_check_socket_state(struct sender_client *sc)
+{
+ struct udp_target *ut = sc->private_data;
+ int ret;
+ socklen_t errlen = sizeof(ret);
+
+ if (getsockopt(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",
+ sc->name, (int)dist);
+ }
+ }
+ return -ERRNO_TO_PARA_ERROR(ret);
+}
+
+static void udp_send_fec(struct sender_client *sc, char *buf, size_t len)
+{
+ int ret;
+
+ if (sender_status == SENDER_off)
+ return;
+ if (len == 0)
+ return;
+ ret = udp_check_socket_state(sc);
+ if (ret < 0)
+ goto fail;
+ ret = xwrite(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;
+ return;
+fail:
+ udp_delete_target(sc, para_strerror(-ret));