+static gint bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) {
+ gint yes;
+
+ g_assert(fd >= 0);
+ g_assert(sa);
+ g_assert(l > 0);
+
+ if (bind(fd, sa, l) < 0) {
+
+ if (errno != EADDRINUSE) {
+ avahi_log_warn("bind() failed: %s\n", strerror(errno));
+ return -1;
+ }
+
+ avahi_log_warn("*** WARNING: Detected another %s mDNS stack running on this host. This makes mDNS unreliable and is thus not recommended. ***",
+ sa->sa_family == AF_INET ? "IPv4" : "IPv6");
+
+ /* Try again, this time with SO_REUSEADDR set */
+ yes = 1;
+ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
+ avahi_log_warn("SO_REUSEADDR failed: %s\n", strerror(errno));
+ return -1;
+ }
+
+ if (bind(fd, sa, l) < 0) {
+ avahi_log_warn("bind() failed: %s\n", strerror(errno));
+ return -1;
+ }
+ } else {
+
+ /* We enable SO_REUSEADDR afterwards, to make sure that the
+ * user may run other mDNS implementations if he really
+ * wants. */
+
+ yes = 1;
+ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
+ avahi_log_warn("SO_REUSEADDR failed: %s\n", strerror(errno));
+ return -1;
+ }
+ }
+
+ return 0;
+}
+