]> 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 71dd63918fd3055c4fa7ddc781e16326855d4ea2..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_PATH "/tmp/avahi"
-#define UNIX_SOCKET UNIX_SOCKET_PATH"/socket"
+#define BUFFER_SIZE (20*1024)
 
 #define CLIENTS_MAX 50
 
 typedef struct Client Client;
 typedef struct Server Server;
 
+typedef enum {
+    CLIENT_IDLE,
+    CLIENT_RESOLVE_HOSTNAME,
+    CLIENT_RESOLVE_ADDRESS,
+    CLIENT_BROWSE_DNS_SERVERS,
+    CLIENT_DEAD
+} ClientState;
+
 struct Client {
     Server *server;
+
+    ClientState state;
     
     gint fd;
     GPollFD poll_fd;
 
     gchar inbuf[BUFFER_SIZE], outbuf[BUFFER_SIZE];
     guint inbuf_length, outbuf_length;
+
+    AvahiSHostNameResolver *host_name_resolver;
+    AvahiSAddressResolver *address_resolver;
+    AvahiSDNSServerBrowser *dns_server_browser;
+
+    AvahiProtocol afquery;
     
     AVAHI_LLIST_FIELDS(Client, clients);
 };
@@ -67,6 +82,7 @@ struct Server {
     AVAHI_LLIST_HEAD(Client, clients);
 
     guint n_clients;
+    gboolean bind_successful;
 };
 
 static Server *server = NULL;
@@ -76,6 +92,15 @@ static void client_free(Client *c) {
 
     g_assert(c->server->n_clients >= 1);
     c->server->n_clients--;
+
+    if (c->host_name_resolver)
+        avahi_s_host_name_resolver_free(c->host_name_resolver);
+
+    if (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);
@@ -91,9 +116,14 @@ static void client_new(Server *s, int fd) {
     c = g_new(Client, 1);
     c->server = s;
     c->fd = fd;
+    c->state = CLIENT_IDLE;
 
     c->inbuf_length = c->outbuf_length = 0;
 
+    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;
     c->poll_fd.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
@@ -117,16 +147,168 @@ static void client_output(Client *c, const guint8*data, guint size) {
 
     memcpy(c->outbuf + c->outbuf_length, data, m);
     c->outbuf_length += m;
+
+    c->poll_fd.events |= G_IO_OUT;
+}
+
+static void client_output_printf(Client *c, const gchar *format, ...) {
+    gchar *t;
+    va_list ap;
+    
+    va_start(ap, format);
+    t = g_strdup_vprintf(format, ap);
+    va_end(ap);
+
+    client_output(c, (guint8*) t, strlen(t));
+    g_free(t);
+}
+
+
+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, "%+i Query timed out\n", AVAHI_ERR_TIMEOUT);
+    else {
+        gchar t[64];
+        avahi_address_snprint(t, sizeof(t), a);
+        client_output_printf(c, "+ %i %u %s %s\n", iface, protocol, hostname, t);
+    }
+
+    c->state = CLIENT_DEAD;
+}
+
+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, "%+i Query timed out\n", AVAHI_ERR_TIMEOUT);
+    else 
+        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 t[256];
+    gchar cmd[64], arg[64];
+    gint n_args;
 
     g_assert(c);
     g_assert(s);
 
-    snprintf(t, sizeof(t), "you said <%s>\n", s);
-    client_output(c, (guint8*) t, strlen(t));
+    if (c->state != CLIENT_IDLE)
+        return;
+
+    if ((n_args = sscanf(s, "%63s %63s", cmd, arg)) < 1 ) {
+        client_output_printf(c, "%+i Failed to parse command, try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION);
+        c->state = CLIENT_DEAD;
+        return;
+    }
+
+    if (strcmp(cmd, "HELP") == 0) {
+        client_output_printf(c,
+                             "+ Available commands are:\n"
+                             "+      RESOLVE-HOSTNAME <hostname>\n"
+                             "+      RESOLVE-HOSTNAME-IPV6 <hostname>\n"
+                             "+      RESOLVE-HOSTNAME-IPV4 <hostname>\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");
+        c->state = CLIENT_DEAD;
+    } else if (strcmp(cmd, "RESOLVE-HOSTNAME-IPV4") == 0 && n_args == 2) {
+        c->state = CLIENT_RESOLVE_HOSTNAME;
+        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;
+        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;
+        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, "%+i Failed to parse address \"%s\".\n", AVAHI_ERR_INVALID_ADDRESS, arg);
+            c->state = CLIENT_DEAD;
+        } else {
+            c->state = CLIENT_RESOLVE_ADDRESS;
+            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, "%+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) {
@@ -156,7 +338,7 @@ static void client_work(Client *c) {
         
         if ((r = read(c->fd, c->inbuf + c->inbuf_length, sizeof(c->inbuf) - c->inbuf_length)) <= 0) {
             if (r < 0)
-                g_warning("read(): %s", strerror(errno));
+                avahi_log_warn("read(): %s", strerror(errno));
             client_free(c);
             return;
         }
@@ -171,7 +353,7 @@ static void client_work(Client *c) {
         ssize_t r;
 
         if ((r = write(c->fd, c->outbuf, c->outbuf_length)) < 0) {
-            g_warning("write(): %s", strerror(errno));
+            avahi_log_warn("write(): %s", strerror(errno));
             client_free(c);
             return;
         }
@@ -181,6 +363,11 @@ static void client_work(Client *c) {
         
         if (c->outbuf_length)
             memmove(c->outbuf, c->outbuf + r, c->outbuf_length - r);
+
+        if (c->outbuf_length == 0 && c->state == CLIENT_DEAD) {
+            client_free(c);
+            return;
+        }
     }
 
     c->poll_fd.events =
@@ -224,7 +411,7 @@ static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer us
         gint fd;
 
         if ((fd = accept(s->fd, NULL, NULL)) < 0)
-            g_warning("accept(): %s", strerror(errno));
+            avahi_log_warn("accept(): %s", strerror(errno));
         else
             client_new(s, fd);
     } else if (s->poll_fd.revents)
@@ -255,37 +442,39 @@ int simple_protocol_setup(GMainContext *c) {
     g_assert(!server);
 
     server = (Server*) g_source_new(&source_funcs, sizeof(Server));
+    server->bind_successful = FALSE;
     server->fd = -1;
     AVAHI_LLIST_HEAD_INIT(Client, server->clients);
-    if (c)
-        g_main_context_ref(server->context = c);
-    else
-        server->context = g_main_context_default();
+    g_main_context_ref(server->context = (c ? c : g_main_context_default()));
     server->clients = NULL;
 
     u = umask(0000);
 
-    if (mkdir(UNIX_SOCKET_PATH, 0755) < 0 && errno != EEXIST) {
-        g_warning("mkdir(): %s", strerror(errno));
-        goto fail;
-    }
-    
     if ((server->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
-        g_warning("socket(PF_LOCAL, SOCK_STREAM, 0): %s", strerror(errno));
+        avahi_log_warn("socket(PF_LOCAL, SOCK_STREAM, 0): %s", strerror(errno));
         goto fail;
     }
 
     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) {
-        g_warning("bind(): %s", strerror(errno));
+        avahi_log_warn("bind(): %s", strerror(errno));
         goto fail;
     }
+
+    server->bind_successful = TRUE;
     
     if (listen(server->fd, 2) < 0) {
-        g_warning("listen(): %s", strerror(errno));
+        avahi_log_warn("listen(): %s", strerror(errno));
         goto fail;
     }
 
@@ -314,16 +503,34 @@ void simple_protocol_shutdown(void) {
 
         while (server->clients)
             client_free(server->clients);
+
+        if (server->bind_successful)
+            unlink(AVAHI_SOCKET);
         
-        if (server->fd >= 0) {
-            unlink(UNIX_SOCKET_PATH);
+        if (server->fd >= 0)
             close(server->fd);
-        }
 
         g_main_context_unref(server->context);
+        
         g_source_destroy(&server->source);
         g_source_unref(&server->source);
 
         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);
+        }
+}
+
+