]> git.meshlink.io Git - catta/blobdiff - avahi-daemon/simple-protocol.c
Rename some server side objects/symbols so that they do not conflict with the same...
[catta] / avahi-daemon / simple-protocol.c
index 8bd8da4403588ee27654092dda4813ed0cdcd1d3..ec43049c1005d42efbd374faa170565946a28922 100644 (file)
 
 #include <glib.h>
 
-#include <avahi-core/llist.h>
+#include <avahi-common/llist.h>
 #include <avahi-core/log.h>
 
 #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;
 
@@ -66,8 +65,11 @@ struct Client {
     gchar inbuf[BUFFER_SIZE], outbuf[BUFFER_SIZE];
     guint inbuf_length, outbuf_length;
 
-    AvahiHostNameResolver *host_name_resolver;
-    AvahiAddressResolver *address_resolver;
+    AvahiSHostNameResolver *host_name_resolver;
+    AvahiSAddressResolver *address_resolver;
+    AvahiSDNSServerBrowser *dns_server_browser;
+
+    AvahiProtocol afquery;
     
     AVAHI_LLIST_FIELDS(Client, clients);
 };
@@ -92,10 +94,13 @@ static void client_free(Client *c) {
     c->server->n_clients--;
 
     if (c->host_name_resolver)
-        avahi_host_name_resolver_free(c->host_name_resolver);
+        avahi_s_host_name_resolver_free(c->host_name_resolver);
 
     if (c->address_resolver)
-        avahi_address_resolver_free(c->address_resolver);
+        avahi_s_address_resolver_free(c->address_resolver);
+
+    if (c->dns_server_browser)
+        avahi_s_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,86 @@ 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(
+    AvahiSHostNameResolver *r,
+    AvahiIfIndex iface,
+    AvahiProtocol protocol,
+    AvahiResolverEvent event,
+    const char *hostname,
+    const AvahiAddress *a,
+    void* 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(
+    AvahiSAddressResolver *r,
+    AvahiIfIndex iface,
+    AvahiProtocol protocol,
+    AvahiResolverEvent event,
+    const AvahiAddress *a,
+    const char *hostname,
+    void* 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(
+    AvahiSDNSServerBrowser *b,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
+    AvahiBrowserEvent event,
+    const char *host_name,
+    const AvahiAddress *a,
+    uint16_t port,
+    void* 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 +242,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 +253,62 @@ static void handle_line(Client *c, const gchar *s) {
                              "+      RESOLVE-HOSTNAME <hostname>\n"
                              "+      RESOLVE-HOSTNAME-IPV6 <hostname>\n"
                              "+      RESOLVE-HOSTNAME-IPV4 <hostname>\n"
-                             "+      RESOLVE-ADDRESS <address>\n");
+                             "+      RESOLVE-ADDRESS <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_s_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_s_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_s_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_s_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_s_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_s_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_s_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,8 +457,15 @@ 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(AVAHI_SOCKET);
+    
     if (bind(server->fd, &sa, sizeof(sa)) < 0) {
         avahi_log_warn("bind(): %s", strerror(errno));
         goto fail;
@@ -425,7 +505,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);
@@ -438,3 +518,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_s_dns_server_browser_free(c->dns_server_browser);
+            c->dns_server_browser = avahi_s_dns_server_browser_new(avahi_server, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery, dns_server_browser_callback, c);
+        }
+}
+
+