From 40cd1f60e9d84610401db992003a3b282de5fcae Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 19 May 2005 21:49:28 +0000 Subject: [PATCH] add a new structure for configuration AvahiServerConfig git-svn-id: file:///home/lennart/svn/public/avahi/trunk@76 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-core/avahi-test.c | 4 +- avahi-core/conformance-test.c | 2 +- avahi-core/core.h | 16 ++++++- avahi-core/iface.c | 2 +- avahi-core/server.c | 82 +++++++++++++++++++++++++++-------- avahi-core/server.h | 4 +- 6 files changed, 84 insertions(+), 26 deletions(-) diff --git a/avahi-core/avahi-test.c b/avahi-core/avahi-test.c index e275649..f3ed3cc 100644 --- a/avahi-core/avahi-test.c +++ b/avahi-core/avahi-test.c @@ -65,8 +65,8 @@ int main(int argc, char *argv[]) { AvahiSubscription *s; AvahiKey *k; AvahiEntryGroup *g; - - avahi = avahi_server_new(NULL); + + avahi = avahi_server_new(NULL, NULL); g = avahi_entry_group_new(avahi, entry_group_callback, NULL); diff --git a/avahi-core/conformance-test.c b/avahi-core/conformance-test.c index 001bff1..74c3833 100644 --- a/avahi-core/conformance-test.c +++ b/avahi-core/conformance-test.c @@ -91,7 +91,7 @@ static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryG int main(int argc, char *argv[]) { GMainLoop *loop = NULL; - avahi = avahi_server_new(NULL); + avahi = avahi_server_new(NULL, NULL); create_service("gurke"); avahi_server_dump(avahi, stdout); diff --git a/avahi-core/core.h b/avahi-core/core.h index 98f36b2..49b09b5 100644 --- a/avahi-core/core.h +++ b/avahi-core/core.h @@ -48,9 +48,23 @@ typedef enum { typedef void (*AvahiEntryGroupCallback) (AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, gpointer userdata); -AvahiServer *avahi_server_new(GMainContext *c); +typedef struct AvahiServerConfig { + gboolean register_hinfo; + gboolean register_addresses; + gboolean use_ipv4; + gboolean use_ipv6; + gchar *host_name; + gchar *domain_name; + gboolean check_response_ttl; +} AvahiServerConfig; + +AvahiServer *avahi_server_new(GMainContext *c, const AvahiServerConfig *sc); void avahi_server_free(AvahiServer* s); +AvahiServerConfig* avahi_server_config_init(AvahiServerConfig *c); +AvahiServerConfig* avahi_server_config_copy(AvahiServerConfig *ret, const AvahiServerConfig *c); +void avahi_server_config_free(AvahiServerConfig *c); + const gchar* avahi_server_get_domain(AvahiServer *s); const gchar* avahi_server_get_host_name(AvahiServer *s); diff --git a/avahi-core/iface.c b/avahi-core/iface.c index 2448678..61e5a4a 100644 --- a/avahi-core/iface.c +++ b/avahi-core/iface.c @@ -47,7 +47,7 @@ static void update_address_rr(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a a->entry_group = NULL; } } else { - if (!a->entry_group) { + if (!a->entry_group && m->server->config.register_addresses) { a->entry_group = avahi_entry_group_new(m->server, NULL, NULL); avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, AF_UNSPEC, 0, NULL, &a->address); avahi_entry_group_commit(a->entry_group); diff --git a/avahi-core/server.c b/avahi-core/server.c index 31cbecc..316952b 100644 --- a/avahi-core/server.c +++ b/avahi-core/server.c @@ -626,7 +626,7 @@ static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, struct sockaddr * if (ttl != 255) { g_warning("Recieved response with invalid TTL %u on interface '%s.%i'.", ttl, i->hardware->name, i->protocol); - if (!s->ignore_bad_ttl) + if (s->config.check_response_ttl) return; } @@ -698,21 +698,22 @@ static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer us } static void add_default_entries(AvahiServer *s) { - struct utsname utsname; AvahiAddress a; - AvahiRecord *r; g_assert(s); - return ; - - /* Fill in HINFO rr */ - r = avahi_record_new_full(s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO); - uname(&utsname); - r->data.hinfo.cpu = g_strdup(g_strup(utsname.machine)); - r->data.hinfo.os = g_strdup(g_strup(utsname.sysname)); - avahi_server_add(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_UNIQUE, r); - avahi_record_unref(r); + if (s->config.register_hinfo) { + struct utsname utsname; + AvahiRecord *r; + + /* Fill in HINFO rr */ + r = avahi_record_new_full(s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO); + uname(&utsname); + r->data.hinfo.cpu = g_strdup(g_strup(utsname.machine)); + r->data.hinfo.os = g_strdup(g_strup(utsname.sysname)); + avahi_server_add(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_UNIQUE, r); + avahi_record_unref(r); + } /* Add localhost entries */ avahi_address_parse("127.0.0.1", AF_INET, &a); @@ -722,7 +723,7 @@ static void add_default_entries(AvahiServer *s) { avahi_server_add_address(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "ip6-localhost", &a); } -AvahiServer *avahi_server_new(GMainContext *c) { +AvahiServer *avahi_server_new(GMainContext *c, const AvahiServerConfig *sc) { AvahiServer *s; static GSourceFuncs source_funcs = { @@ -736,21 +737,26 @@ AvahiServer *avahi_server_new(GMainContext *c) { s = g_new(AvahiServer, 1); - s->ignore_bad_ttl = FALSE; s->need_entry_cleanup = s->need_group_cleanup = FALSE; + + if (sc) + avahi_server_config_copy(&s->config, sc); + else + avahi_server_config_init(&s->config); - s->fd_ipv4 = avahi_open_socket_ipv4(); - s->fd_ipv6 = -1 /* avahi_open_socket_ipv6() */ ; + s->fd_ipv4 = s->config.use_ipv4 ? avahi_open_socket_ipv4() : -1; + s->fd_ipv6 = s->config.use_ipv6 ? avahi_open_socket_ipv6() : -1; if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) { - g_critical("Failed to create IP sockets.\n"); + g_critical("Selected neither IPv6 nor IPv4 support, aborting.\n"); + avahi_server_config_free(&s->config); g_free(s); return NULL; } - if (s->fd_ipv4 < 0) + if (s->fd_ipv4 < 0 && s->config.use_ipv4) g_message("Failed to create IPv4 socket, proceeding in IPv6 only mode"); - else if (s->fd_ipv6 < 0) + else if (s->fd_ipv6 < 0 && s->config.use_ipv6) g_message("Failed to create IPv6 socket, proceeding in IPv4 only mode"); if (c) @@ -778,6 +784,7 @@ AvahiServer *avahi_server_new(GMainContext *c) { s->time_event_queue = avahi_time_event_queue_new(s->context, G_PRIORITY_DEFAULT+10); /* Slightly less priority than the FDs */ s->monitor = avahi_interface_monitor_new(s); avahi_interface_monitor_sync(s->monitor); + add_default_entries(s); /* Prepare IO source registration */ @@ -833,6 +840,8 @@ void avahi_server_free(AvahiServer* s) { g_source_unref(s->source); g_main_context_unref(s->context); + avahi_server_config_free(&s->config); + g_free(s); } @@ -1240,3 +1249,38 @@ const gchar* avahi_server_get_host_name(AvahiServer *s) { return s->host_name_fqdn; } + +AvahiServerConfig* avahi_server_config_init(AvahiServerConfig *c) { + g_assert(c); + + memset(c, 0, sizeof(AvahiServerConfig)); + c->register_hinfo = TRUE; + c->register_addresses = TRUE; + c->use_ipv6 = TRUE; + c->use_ipv4 = TRUE; + c->host_name = NULL; + c->domain_name = NULL; + c->check_response_ttl = TRUE; + + return c; +} + +void avahi_server_config_free(AvahiServerConfig *c) { + g_assert(c); + + g_assert(c->host_name); + g_assert(c->domain_name); + g_free(c); +} + +AvahiServerConfig* avahi_server_config_copy(AvahiServerConfig *ret, const AvahiServerConfig *c) { + g_assert(ret); + g_assert(c); + + *ret = *c; + + ret->host_name = g_strdup(c->host_name); + ret->domain_name = g_strdup(c->domain_name); + + return ret; +} diff --git a/avahi-core/server.h b/avahi-core/server.h index 7283b79..4d0e8a3 100644 --- a/avahi-core/server.h +++ b/avahi-core/server.h @@ -68,6 +68,8 @@ struct AvahiServer { GMainContext *context; AvahiInterfaceMonitor *monitor; + AvahiServerConfig config; + AVAHI_LLIST_HEAD(AvahiEntry, entries); GHashTable *entries_by_key; @@ -87,8 +89,6 @@ struct AvahiServer { GPollFD pollfd_ipv4, pollfd_ipv6; GSource *source; - gboolean ignore_bad_ttl; - /* Used for assembling responses */ AvahiRecordList *record_list; }; -- 2.39.5