+static gboolean handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord *record, gboolean unique, const AvahiAddress *a) {
+ gboolean valid = TRUE;
+ AvahiEntry *e, *n;
+ gchar *t;
+
+ g_assert(s);
+ g_assert(i);
+ g_assert(record);
+
+ t = avahi_record_to_string(record);
+
+/* g_message("CHECKING FOR CONFLICT: [%s]", t); */
+
+ for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) {
+ n = e->by_key_next;
+
+ if (e->dead)
+ continue;
+
+ if (avahi_entry_registered(s, e, i)) {
+
+ gboolean equal = avahi_record_equal_no_ttl(record, e->record);
+
+ /* Check whether there is a unique record conflict */
+ if (!equal && ((e->flags & AVAHI_ENTRY_UNIQUE) || unique)) {
+ gint cmp;
+
+ /* The lexicographically later data wins. */
+ if ((cmp = avahi_record_lexicographical_compare(record, e->record)) > 0) {
+ g_message("Recieved conflicting record [%s]. Local host lost. Withdrawing.", t);
+ withdraw_entry(s, e);
+ } else if (cmp < 0) {
+ /* Tell the other host that our entry is lexicographically later */
+
+ g_message("Recieved conflicting record [%s]. Local host won. Refreshing.", t);
+
+ valid = FALSE;
+ avahi_interface_post_response(i, a, e->record, e->flags & AVAHI_ENTRY_UNIQUE, TRUE);
+ }
+
+ /* Check wheter there is a TTL conflict */
+ } else if (equal && record->ttl <= e->record->ttl/2) {
+ /* Correct the TTL */
+ valid = FALSE;
+ avahi_interface_post_response(i, a, e->record, e->flags & AVAHI_ENTRY_UNIQUE, TRUE);
+ g_message("Recieved record with bad TTL [%s]. Refreshing.", t);
+ }
+
+ } else if (avahi_entry_registering(s, e, i)) {
+
+ if (!avahi_record_equal_no_ttl(record, e->record) && ((e->flags & AVAHI_ENTRY_UNIQUE) || unique)) {
+
+ /* We are currently registering a matching record, but
+ * someone else already claimed it, so let's
+ * withdraw */
+
+ g_message("Recieved conflicting record [%s] with local record to be. Withdrawing.", t);
+ withdraw_entry(s, e);
+ }
+ }
+ }
+
+ g_free(t);
+
+ return valid;
+}
+
+static void handle_response(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a) {