From 9bc2eb5311514a53243c9e771658779524e70b9a Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 21 Jun 2005 12:58:06 +0000 Subject: [PATCH] * Update configure.ac to check for the availabilty of more functions * really return sensible error codes from avahi_server_add_xx() * make all users of avahi_server_add_xxx() check for the validity * check for local RR conflicts * only suppress local queries by remote queries if they have an empty known answer suppresion list * fix FIONREAD call git-svn-id: file:///home/lennart/svn/public/avahi/trunk@136 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-core/avahi-test.c | 25 +++++++++-- avahi-core/core.h | 3 +- avahi-core/iface.c | 16 ++++++-- avahi-core/server.c | 91 ++++++++++++++++++++++++++++++----------- avahi-core/socket.c | 4 +- configure.ac | 8 ++-- 6 files changed, 109 insertions(+), 38 deletions(-) diff --git a/avahi-core/avahi-test.c b/avahi-core/avahi-test.c index 5f4b91e..0d47941 100644 --- a/avahi-core/avahi-test.c +++ b/avahi-core/avahi-test.c @@ -114,12 +114,29 @@ static void create_entries(gboolean new_name) { service_name = n; } - avahi_server_add_service(server, group, 0, AF_UNSPEC, "_http._tcp", service_name, NULL, NULL, 80, "foo", NULL); - avahi_server_add_service(server, group, 0, AF_UNSPEC, "_ftp._tcp", service_name, NULL, NULL, 21, "foo", NULL); - avahi_server_add_service(server, group, 0, AF_UNSPEC, "_webdav._tcp", service_name, NULL, NULL, 80, "foo", NULL); + if (avahi_server_add_service(server, group, 0, AF_UNSPEC, "_http._tcp", service_name, NULL, NULL, 80, "foo", NULL) < 0) { + avahi_log_error("Failed to add HTTP service"); + goto fail; + } + + if (avahi_server_add_service(server, group, 0, AF_UNSPEC, "_ftp._tcp", service_name, NULL, NULL, 21, "foo", NULL) < 0) { + avahi_log_error("Failed to add FTP service"); + goto fail; + } + + if (avahi_server_add_service(server, group, 0, AF_UNSPEC, "_webdav._tcp", service_name, NULL, NULL, 80, "foo", NULL) < 0) { + avahi_log_error("Failed to add WEBDAV service"); + goto fail; + } - avahi_entry_group_commit(group); + avahi_entry_group_commit(group); + return; +fail: + if (group) + avahi_entry_group_free(group); + + group = NULL; } static void hnr_callback(AvahiHostNameResolver *r, gint iface, guchar protocol, AvahiBrowserEvent event, const gchar *hostname, const AvahiAddress *a, gpointer userdata) { diff --git a/avahi-core/core.h b/avahi-core/core.h index c9274c4..0565d23 100644 --- a/avahi-core/core.h +++ b/avahi-core/core.h @@ -52,7 +52,8 @@ typedef enum { AVAHI_ENTRY_NULL = 0, /**< No special flags */ AVAHI_ENTRY_UNIQUE = 1, /**< The RRset is intended to be unique */ AVAHI_ENTRY_NOPROBE = 2, /**< Though the RRset is intended to be unique no probes shall be sent */ - AVAHI_ENTRY_NOANNOUNCE = 4 /**< Do not announce this RR to other hosts */ + AVAHI_ENTRY_NOANNOUNCE = 4, /**< Do not announce this RR to other hosts */ + AVAHI_ENTRY_ALLOWMUTIPLE = 8 /**< Allow multiple local records of this type, even if they are intended to be unique */ } AvahiEntryFlags; /** States of an entry group object */ diff --git a/avahi-core/iface.c b/avahi-core/iface.c index 7972a5c..60419df 100644 --- a/avahi-core/iface.c +++ b/avahi-core/iface.c @@ -51,8 +51,12 @@ static void update_address_rr(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a if (!a->entry_group) { a->entry_group = avahi_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL); - avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, a->interface->protocol, 0, NULL, &a->address); - avahi_entry_group_commit(a->entry_group); + if (avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, a->interface->protocol, 0, NULL, &a->address) < 0) { + avahi_log_warn(__FILE__": avahi_server_add_address() failed."); + avahi_entry_group_free(a->entry_group); + a->entry_group = NULL; + } else + avahi_entry_group_commit(a->entry_group); } } else { @@ -98,8 +102,12 @@ static void update_hw_interface_rr(AvahiInterfaceMonitor *m, AvahiHwInterface *h g_free(t); hw->entry_group = avahi_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL); - avahi_server_add_service(m->server, hw->entry_group, hw->index, AF_UNSPEC, "_workstation._tcp", name, NULL, NULL, 9, NULL); - avahi_entry_group_commit(hw->entry_group); + if (avahi_server_add_service(m->server, hw->entry_group, hw->index, AF_UNSPEC, "_workstation._tcp", name, NULL, NULL, 9, NULL) < 0) { + avahi_log_warn(__FILE__": avahi_server_add_service() failed."); + avahi_entry_group_free(hw->entry_group); + hw->entry_group = NULL; + } else + avahi_entry_group_commit(hw->entry_group); g_free(name); } diff --git a/avahi-core/server.c b/avahi-core/server.c index 86f29fb..a020f64 100644 --- a/avahi-core/server.c +++ b/avahi-core/server.c @@ -561,7 +561,13 @@ static void handle_query_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterfac if (!legacy_unicast) reflect_query(s, i, key); - avahi_query_scheduler_incoming(i->query_scheduler, key); + + if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) == 0 && + !(avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_TC)) + /* Allow our own queries to be suppressed by incoming + * queries only when they do not include known answers */ + avahi_query_scheduler_incoming(i->query_scheduler, key); + avahi_server_prepare_matching_responses(s, i, key, unicast_response); avahi_key_unref(key); } @@ -1396,6 +1402,36 @@ void avahi_server_free(AvahiServer* s) { g_free(s); } +static gint check_record_conflict(AvahiServer *s, gint interface, guchar protocol, AvahiRecord *r, AvahiEntryFlags flags) { + AvahiEntry *e; + + g_assert(s); + g_assert(r); + + for (e = g_hash_table_lookup(s->entries_by_key, r->key); e; e = e->by_key_next) { + if (e->dead) + continue; + + if (!(flags & AVAHI_ENTRY_UNIQUE) && !(e->flags & AVAHI_ENTRY_UNIQUE)) + continue; + + if ((flags & AVAHI_ENTRY_ALLOWMUTIPLE) && (e->flags & AVAHI_ENTRY_ALLOWMUTIPLE) ) + continue; + + if (interface <= 0 || + e->interface <= 0 || + e->interface == interface || + protocol == AF_UNSPEC || + e->protocol == AF_UNSPEC || + e->protocol == protocol) + + return -1; + + } + + return 0; +} + gint avahi_server_add( AvahiServer *s, AvahiEntryGroup *g, @@ -1410,6 +1446,9 @@ gint avahi_server_add( g_assert(r->key->type != AVAHI_DNS_TYPE_ANY); + if (check_record_conflict(s, interface, protocol, r, flags) < 0) + return -1; + e = g_new(AvahiEntry, 1); e->server = s; e->record = avahi_record_ref(r); @@ -1485,14 +1524,15 @@ gint avahi_server_add_ptr( const gchar *dest) { AvahiRecord *r; + gint ret; g_assert(dest); r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR); r->data.ptr.name = avahi_normalize_name(dest); - avahi_server_add(s, g, interface, protocol, flags, r); + ret = avahi_server_add(s, g, interface, protocol, flags, r); avahi_record_unref(r); - return 0; + return ret; } gint avahi_server_add_address( @@ -1505,6 +1545,7 @@ gint avahi_server_add_address( AvahiAddress *a) { gchar *n = NULL; + gint ret = 0; g_assert(s); g_assert(a); @@ -1516,11 +1557,11 @@ gint avahi_server_add_address( r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A); r->data.a.address = a->data.ipv4; - avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, r); + ret = avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE | AVAHI_ENTRY_ALLOWMUTIPLE, r); avahi_record_unref(r); reverse = avahi_reverse_lookup_name_ipv4(&a->data.ipv4); - avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name); + ret |= avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name); g_free(reverse); } else { @@ -1529,21 +1570,21 @@ gint avahi_server_add_address( r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA); r->data.aaaa.address = a->data.ipv6; - avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, r); + ret = avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE | AVAHI_ENTRY_ALLOWMUTIPLE, r); avahi_record_unref(r); reverse = avahi_reverse_lookup_name_ipv6_arpa(&a->data.ipv6); - avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name); + ret |= avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name); g_free(reverse); reverse = avahi_reverse_lookup_name_ipv6_int(&a->data.ipv6); - avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name); + ret |= avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name); g_free(reverse); } g_free(n); - return 0; + return ret; } gint avahi_server_add_text_strlst( @@ -1556,15 +1597,16 @@ gint avahi_server_add_text_strlst( AvahiStringList *strlst) { AvahiRecord *r; + gint ret; g_assert(s); r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT); r->data.txt.string_list = strlst; - avahi_server_add(s, g, interface, protocol, flags, r); + ret = avahi_server_add(s, g, interface, protocol, flags, r); avahi_record_unref(r); - return 0; + return ret; } gint avahi_server_add_text_va( @@ -1578,8 +1620,7 @@ gint avahi_server_add_text_va( g_assert(s); - avahi_server_add_text_strlst(s, g, interface, protocol, flags, name, avahi_string_list_new_va(va)); - return 0; + return avahi_server_add_text_strlst(s, g, interface, protocol, flags, name, avahi_string_list_new_va(va)); } gint avahi_server_add_text( @@ -1592,14 +1633,15 @@ gint avahi_server_add_text( ...) { va_list va; + gint ret; g_assert(s); va_start(va, name); - avahi_server_add_text_va(s, g, interface, protocol, flags, name, va); + ret = avahi_server_add_text_va(s, g, interface, protocol, flags, name, va); va_end(va); - return 0; + return ret; } static void escape_service_name(gchar *d, guint size, const gchar *s) { @@ -1638,6 +1680,7 @@ gint avahi_server_add_service_strlst( gchar ptr_name[256], svc_name[256], ename[64], enum_ptr[256]; AvahiRecord *r; + gint ret = 0; g_assert(s); g_assert(type); @@ -1657,22 +1700,22 @@ gint avahi_server_add_service_strlst( snprintf(ptr_name, sizeof(ptr_name), "%s.%s", type, domain); snprintf(svc_name, sizeof(svc_name), "%s.%s.%s", ename, type, domain); - avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, ptr_name, svc_name); + ret = avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, ptr_name, svc_name); r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV); r->data.srv.priority = 0; r->data.srv.weight = 0; r->data.srv.port = port; r->data.srv.name = avahi_normalize_name(host); - avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, r); + ret |= avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, r); avahi_record_unref(r); - avahi_server_add_text_strlst(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, svc_name, strlst); + ret |= avahi_server_add_text_strlst(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, svc_name, strlst); snprintf(enum_ptr, sizeof(enum_ptr), "_services._dns-sd._udp.%s", domain); - avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, enum_ptr, ptr_name); + ret |=avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, enum_ptr, ptr_name); - return 0; + return ret; } gint avahi_server_add_service_va( @@ -1691,8 +1734,7 @@ gint avahi_server_add_service_va( g_assert(type); g_assert(name); - avahi_server_add_service_strlst(s, g, interface, protocol, type, name, domain, host, port, avahi_string_list_new_va(va)); - return 0; + return avahi_server_add_service_strlst(s, g, interface, protocol, type, name, domain, host, port, avahi_string_list_new_va(va)); } gint avahi_server_add_service( @@ -1708,15 +1750,16 @@ gint avahi_server_add_service( ... ){ va_list va; + gint ret; g_assert(s); g_assert(type); g_assert(name); va_start(va, port); - avahi_server_add_service_va(s, g, interface, protocol, type, name, domain, host, port, va); + ret = avahi_server_add_service_va(s, g, interface, protocol, type, name, domain, host, port, va); va_end(va); - return 0; + return ret; } static void post_query_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) { diff --git a/avahi-core/socket.c b/avahi-core/socket.c index 8f3d896..b635dca 100644 --- a/avahi-core/socket.c +++ b/avahi-core/socket.c @@ -452,7 +452,7 @@ AvahiDnsPacket* avahi_recv_dns_packet_ipv4(gint fd, struct sockaddr_in *ret_sa, ssize_t l; struct cmsghdr *cmsg; gboolean found_ttl = FALSE, found_iface = FALSE; - guint ms; + gint ms; g_assert(fd >= 0); g_assert(ret_sa); @@ -536,7 +536,7 @@ AvahiDnsPacket* avahi_recv_dns_packet_ipv6(gint fd, struct sockaddr_in6 *ret_sa, struct iovec io; uint8_t aux[64]; ssize_t l; - guint ms; + gint ms; struct cmsghdr *cmsg; gboolean found_ttl = FALSE, found_iface = FALSE; diff --git a/configure.ac b/configure.ac index 59a50ce..5e82327 100644 --- a/configure.ac +++ b/configure.ac @@ -55,10 +55,12 @@ AC_HEADER_TIME # Checks for library functions. AC_FUNC_MEMCMP AC_FUNC_SELECT_ARGTYPES -AC_CHECK_FUNCS([gethostname memset select socket strchr strcspn strerror uname]) +AC_CHECK_FUNCS([gethostname memchr memmove memset mkdir select socket strchr strcspn strerror strrchr strstr uname setresuid setreuid]) -AC_CHECK_FUNCS(setresuid) -AC_CHECK_FUNCS(setreuid) +AC_FUNC_CHOWN +AC_FUNC_STAT +AC_TYPE_MODE_T +AC_TYPE_PID_T # Check for GLIB 2.0 PKG_CHECK_MODULES(GLIB20, [ glib-2.0 >= 2.4.0 ]) -- 2.39.2