X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-daemon%2Fsimple-protocol.c;h=39af043ca01a09686787fddedbb9bcac71a6dadd;hb=e63a65b3955b173a3e8d6b78c6377a518a9922d6;hp=c41e07cd224a502862feb115f72e790db653d573;hpb=0612e247a644046a01b018534cfc27e4af1d13e6;p=catta diff --git a/avahi-daemon/simple-protocol.c b/avahi-daemon/simple-protocol.c index c41e07c..39af043 100644 --- a/avahi-daemon/simple-protocol.c +++ b/avahi-daemon/simple-protocol.c @@ -33,15 +33,13 @@ #include -#include +#include #include #include "simple-protocol.h" #include "main.h" -#define BUFFER_SIZE (10*1024) - -#define UNIX_SOCKET AVAHI_RUNTIME_DIR "/socket" +#define BUFFER_SIZE (20*1024) #define CLIENTS_MAX 50 @@ -52,6 +50,7 @@ typedef enum { CLIENT_IDLE, CLIENT_RESOLVE_HOSTNAME, CLIENT_RESOLVE_ADDRESS, + CLIENT_BROWSE_DNS_SERVERS, CLIENT_DEAD } ClientState; @@ -68,6 +67,9 @@ struct Client { AvahiHostNameResolver *host_name_resolver; AvahiAddressResolver *address_resolver; + AvahiDNSServerBrowser *dns_server_browser; + + AvahiProtocol afquery; AVAHI_LLIST_FIELDS(Client, clients); }; @@ -96,6 +98,9 @@ static void client_free(Client *c) { if (c->address_resolver) avahi_address_resolver_free(c->address_resolver); + + if (c->dns_server_browser) + avahi_dns_server_browser_free(c->dns_server_browser); g_source_remove_poll(&c->server->source, &c->poll_fd); close(c->fd); @@ -117,6 +122,7 @@ static void client_new(Server *s, int fd) { c->host_name_resolver = NULL; c->address_resolver = NULL; + c->dns_server_browser = NULL; memset(&c->poll_fd, 0, sizeof(GPollFD)); c->poll_fd.fd = fd; @@ -145,47 +151,63 @@ static void client_output(Client *c, const guint8*data, guint size) { c->poll_fd.events |= G_IO_OUT; } -static void client_output_printf(Client *c, gchar *format, ...) { - gchar txt[256]; +static void client_output_printf(Client *c, const gchar *format, ...) { + gchar *t; va_list ap; va_start(ap, format); - vsnprintf(txt, sizeof(txt), format, ap); + t = g_strdup_vprintf(format, ap); va_end(ap); - client_output(c, (guint8*) txt, strlen(txt)); + client_output(c, (guint8*) t, strlen(t)); + g_free(t); } -static void host_name_resolver_callback(AvahiHostNameResolver *r, gint iface, guchar protocol, AvahiBrowserEvent event, const gchar *hostname, const AvahiAddress *a, gpointer userdata) { + +static void host_name_resolver_callback(AvahiHostNameResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *hostname, const AvahiAddress *a, gpointer userdata) { Client *c = userdata; g_assert(c); if (event == AVAHI_RESOLVER_TIMEOUT) - client_output_printf(c, "- Query timed out\n"); + client_output_printf(c, "%+i Query timed out\n", AVAHI_ERR_TIMEOUT); else { gchar t[64]; avahi_address_snprint(t, sizeof(t), a); - client_output_printf(c, "+ %s\n", t); + client_output_printf(c, "+ %i %u %s %s\n", iface, protocol, hostname, t); } c->state = CLIENT_DEAD; } -static void address_resolver_callback(AvahiAddressResolver *r, gint iface, guchar protocol, AvahiBrowserEvent event, const AvahiAddress *a, const gchar *hostname, gpointer userdata) { +static void address_resolver_callback(AvahiAddressResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const AvahiAddress *a, const gchar *hostname, gpointer userdata) { Client *c = userdata; + g_assert(c); if (event == AVAHI_RESOLVER_TIMEOUT) - client_output_printf(c, "- Query timed out\n"); + client_output_printf(c, "%+i Query timed out\n", AVAHI_ERR_TIMEOUT); else - client_output_printf(c, "+ %s\n", hostname); + client_output_printf(c, "+ %i %u %s\n", iface, protocol, hostname); c->state = CLIENT_DEAD; } +static void dns_server_browser_callback(AvahiDNSServerBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *host_name, const AvahiAddress *a, guint16 port, gpointer userdata) { + Client *c = userdata; + gchar t[64]; + + g_assert(c); + + if (!a) + return; + + avahi_address_snprint(t, sizeof(t), a); + client_output_printf(c, "%c %i %u %s %u\n", event == AVAHI_BROWSER_NEW ? '>' : '<', interface, protocol, t, port); +} + static void handle_line(Client *c, const gchar *s) { gchar cmd[64], arg[64]; gint n_args; @@ -197,7 +219,7 @@ static void handle_line(Client *c, const gchar *s) { return; if ((n_args = sscanf(s, "%63s %63s", cmd, arg)) < 1 ) { - client_output_printf(c, "- Failed to parse command, try \"HELP\".\n"); + client_output_printf(c, "%+i Failed to parse command, try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION); c->state = CLIENT_DEAD; return; } @@ -208,34 +230,62 @@ static void handle_line(Client *c, const gchar *s) { "+ RESOLVE-HOSTNAME \n" "+ RESOLVE-HOSTNAME-IPV6 \n" "+ RESOLVE-HOSTNAME-IPV4 \n" - "+ RESOLVE-ADDRESS
\n"); + "+ RESOLVE-ADDRESS
\n" + "+ BROWSE-DNS-SERVERS\n" + "+ BROWSE-DNS-SERVERS-IPV4\n" + "+ BROWSE-DNS-SERVERS-IPV6\n"); c->state = CLIENT_DEAD; } else if (strcmp(cmd, "FUCK") == 0 && n_args == 1) { - client_output_printf(c, "FUCK: Go fuck yourself!\n"); + client_output_printf(c, "+ FUCK: Go fuck yourself!\n"); c->state = CLIENT_DEAD; } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV4") == 0 && n_args == 2) { c->state = CLIENT_RESOLVE_HOSTNAME; - c->host_name_resolver = avahi_host_name_resolver_new(avahi_server, -1, AF_UNSPEC, arg, AF_INET, host_name_resolver_callback, c); + if (!(c->host_name_resolver = avahi_host_name_resolver_new(avahi_server, -1, AF_UNSPEC, arg, c->afquery = AF_INET, host_name_resolver_callback, c))) + goto fail; } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV6") == 0 && n_args == 2) { c->state = CLIENT_RESOLVE_HOSTNAME; - c->host_name_resolver = avahi_host_name_resolver_new(avahi_server, -1, AF_UNSPEC, arg, AF_INET6, host_name_resolver_callback, c); + if (!(c->host_name_resolver = avahi_host_name_resolver_new(avahi_server, -1, AF_UNSPEC, arg, c->afquery = AF_INET6, host_name_resolver_callback, c))) + goto fail; } else if (strcmp(cmd, "RESOLVE-HOSTNAME") == 0 && n_args == 2) { c->state = CLIENT_RESOLVE_HOSTNAME; - c->host_name_resolver = avahi_host_name_resolver_new(avahi_server, -1, AF_UNSPEC, arg, AF_UNSPEC, host_name_resolver_callback, c); + if (!(c->host_name_resolver = avahi_host_name_resolver_new(avahi_server, -1, AF_UNSPEC, arg, c->afquery = AF_UNSPEC, host_name_resolver_callback, c))) + goto fail; } else if (strcmp(cmd, "RESOLVE-ADDRESS") == 0 && n_args == 2) { AvahiAddress addr; if (!(avahi_address_parse(arg, AF_UNSPEC, &addr))) { - client_output_printf(c, "- Failed to parse address \"%s\".\n", arg); + client_output_printf(c, "%+i Failed to parse address \"%s\".\n", AVAHI_ERR_INVALID_ADDRESS, arg); c->state = CLIENT_DEAD; } else { c->state = CLIENT_RESOLVE_ADDRESS; - c->address_resolver = avahi_address_resolver_new(avahi_server, -1, AF_UNSPEC, &addr, address_resolver_callback, c); + if (!(c->address_resolver = avahi_address_resolver_new(avahi_server, -1, AF_UNSPEC, &addr, address_resolver_callback, c))) + goto fail; } + } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV4") == 0 && n_args == 1) { + c->state = CLIENT_BROWSE_DNS_SERVERS; + if (!(c->dns_server_browser = avahi_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery = AF_INET, dns_server_browser_callback, c))) + goto fail; + client_output_printf(c, "+ Browsing ...\n"); + } else if (strcmp(cmd, "BROWSE-DNS-SERVERS-IPV6") == 0 && n_args == 1) { + c->state = CLIENT_BROWSE_DNS_SERVERS; + if (!(c->dns_server_browser = avahi_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery = AF_INET6, dns_server_browser_callback, c))) + goto fail; + client_output_printf(c, "+ Browsing ...\n"); + } else if (strcmp(cmd, "BROWSE-DNS-SERVERS") == 0 && n_args == 1) { + c->state = CLIENT_BROWSE_DNS_SERVERS; + if (!(c->dns_server_browser = avahi_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery = AF_UNSPEC, dns_server_browser_callback, c))) + goto fail; + client_output_printf(c, "+ Browsing ...\n"); } else { - client_output_printf(c, "- Invalid command \"%s\", try \"HELP\".\n", cmd); + client_output_printf(c, "%+i Invalid command \"%s\", try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION, cmd); c->state = CLIENT_DEAD; } + + return; + +fail: + client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server))); + c->state = CLIENT_DEAD; } static void handle_input(Client *c) { @@ -384,14 +434,14 @@ int simple_protocol_setup(GMainContext *c) { memset(&sa, 0, sizeof(sa)); sa.sun_family = AF_LOCAL; - strncpy(sa.sun_path, UNIX_SOCKET, sizeof(sa.sun_path)-1); + strncpy(sa.sun_path, AVAHI_SOCKET, sizeof(sa.sun_path)-1); /* We simply remove existing UNIX sockets under this name. The Avahi daemons makes sure that it runs only once on a host, therefore sockets that already exist are stale and may be removed without any ill effects */ - unlink(UNIX_SOCKET); + unlink(AVAHI_SOCKET); if (bind(server->fd, &sa, sizeof(sa)) < 0) { avahi_log_warn("bind(): %s", strerror(errno)); @@ -432,7 +482,7 @@ void simple_protocol_shutdown(void) { client_free(server->clients); if (server->bind_successful) - unlink(UNIX_SOCKET); + unlink(AVAHI_SOCKET); if (server->fd >= 0) close(server->fd); @@ -445,3 +495,19 @@ void simple_protocol_shutdown(void) { server = NULL; } } + +void simple_protocol_restart_queries(void) { + Client *c; + + /* Restart queries in case of local domain name changes */ + + g_assert(server); + + for (c = server->clients; c; c = c->clients_next) + if (c->state == CLIENT_BROWSE_DNS_SERVERS && c->dns_server_browser) { + avahi_dns_server_browser_free(c->dns_server_browser); + c->dns_server_browser = avahi_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery, dns_server_browser_callback, c); + } +} + +