]> git.meshlink.io Git - catta/blobdiff - avahi-daemon/simple-protocol.c
* avahi-utils: replace python avahi-browse with a version written in C.
[catta] / avahi-daemon / simple-protocol.c
index c72fb86a6ee0264628be6dd643a6e822c995f1fc..a3b48cce4c3991765cb863ad94229521849ec28c 100644 (file)
 
 #include <avahi-common/llist.h>
 #include <avahi-common/malloc.h>
+#include <avahi-common/error.h>
+
 #include <avahi-core/log.h>
+#include <avahi-core/lookup.h>
+#include <avahi-core/dns-srv-rr.h>
 
 #include "simple-protocol.h"
 #include "main.h"
@@ -162,24 +166,24 @@ static void client_output_printf(Client *c, const char *format, ...) {
     avahi_free(t);
 }
 
-
 static void host_name_resolver_callback(
-    AvahiSHostNameResolver *r,
+    AVAHI_GCC_UNUSED AvahiSHostNameResolver *r,
     AvahiIfIndex iface,
     AvahiProtocol protocol,
     AvahiResolverEvent event,
     const char *hostname,
     const AvahiAddress *a,
+    AVAHI_GCC_UNUSED AvahiLookupResultFlags flags, 
     void* userdata) {
 
     Client *c = userdata;
     
     assert(c);
 
-    if (event == AVAHI_RESOLVER_TIMEOUT)
-        client_output_printf(c, "%+i Query timed out\n", AVAHI_ERR_TIMEOUT);
-    else {
-        char t[64];
+    if (event == AVAHI_RESOLVER_FAILURE)
+        client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
+    else if (event == AVAHI_RESOLVER_FOUND) {
+        char t[AVAHI_ADDRESS_STR_MAX];
         avahi_address_snprint(t, sizeof(t), a);
         client_output_printf(c, "+ %i %u %s %s\n", iface, protocol, hostname, t);
     }
@@ -188,46 +192,63 @@ static void host_name_resolver_callback(
 }
 
 static void address_resolver_callback(
-    AvahiSAddressResolver *r,
+    AVAHI_GCC_UNUSED AvahiSAddressResolver *r,
     AvahiIfIndex iface,
     AvahiProtocol protocol,
     AvahiResolverEvent event,
-    const AvahiAddress *a,
+    AVAHI_GCC_UNUSED const AvahiAddress *a,
     const char *hostname,
+    AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
     void* userdata) {
     
     Client *c = userdata;
     
     assert(c);
 
-    if (event == AVAHI_RESOLVER_TIMEOUT)
-        client_output_printf(c, "%+i Query timed out\n", AVAHI_ERR_TIMEOUT);
-    else 
+    if (event == AVAHI_RESOLVER_FAILURE)
+        client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
+    else if (event == AVAHI_RESOLVER_FOUND)
         client_output_printf(c, "+ %i %u %s\n", iface, protocol, hostname);
 
     c->state = CLIENT_DEAD;
 }
 
 static void dns_server_browser_callback(
-    AvahiSDNSServerBrowser *b,
+    AVAHI_GCC_UNUSED AvahiSDNSServerBrowser *b,
     AvahiIfIndex interface,
     AvahiProtocol protocol,
     AvahiBrowserEvent event,
-    const char *host_name,
+    AVAHI_GCC_UNUSED const char *host_name,
     const AvahiAddress *a,
     uint16_t port,
+    AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
     void* userdata) {
     
     Client *c = userdata;
-    char t[64];
+    char t[AVAHI_ADDRESS_STR_MAX];
     
     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);
+    switch (event) {
+        case AVAHI_BROWSER_FAILURE:
+            client_output_printf(c, "%+i %s\n", avahi_server_errno(avahi_server), avahi_strerror(avahi_server_errno(avahi_server)));
+            c->state = CLIENT_DEAD;
+            break;
+
+        case AVAHI_BROWSER_ALL_FOR_NOW:
+        case AVAHI_BROWSER_CACHE_EXHAUSTED:
+            break;
+
+        case AVAHI_BROWSER_NEW:
+        case AVAHI_BROWSER_REMOVE:
+    
+            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);
+            break;
+    }
 }
 
 static void handle_line(Client *c, const char *s) {
@@ -262,45 +283,65 @@ static void handle_line(Client *c, const char *s) {
         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)))
+        if (!(c->host_name_resolver = avahi_s_host_name_resolver_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, arg, c->afquery = AVAHI_PROTO_INET, AVAHI_LOOKUP_USE_MULTICAST, host_name_resolver_callback, c)))
             goto fail;
+
+        avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
     } 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)))
+        if (!(c->host_name_resolver = avahi_s_host_name_resolver_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, arg, c->afquery = AVAHI_PROTO_INET6, AVAHI_LOOKUP_USE_MULTICAST, host_name_resolver_callback, c)))
             goto fail;
+
+        avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
     } 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)))
+        if (!(c->host_name_resolver = avahi_s_host_name_resolver_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, arg, c->afquery = AVAHI_PROTO_UNSPEC, AVAHI_LOOKUP_USE_MULTICAST, host_name_resolver_callback, c)))
             goto fail;
+
+        avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
     } else if (strcmp(cmd, "RESOLVE-ADDRESS") == 0 && n_args == 2) {
         AvahiAddress addr;
         
-        if (!(avahi_address_parse(arg, AF_UNSPEC, &addr))) {
+        if (!(avahi_address_parse(arg, AVAHI_PROTO_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)))
+            if (!(c->address_resolver = avahi_s_address_resolver_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, &addr, AVAHI_LOOKUP_USE_MULTICAST, address_resolver_callback, c)))
                 goto fail;
         }
+
+        avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
+
     } 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)))
+        if (!(c->dns_server_browser = avahi_s_dns_server_browser_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery = AVAHI_PROTO_INET, AVAHI_LOOKUP_USE_MULTICAST, dns_server_browser_callback, c)))
             goto fail;
         client_output_printf(c, "+ Browsing ...\n");
+
+        avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
+
     } 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)))
+        if (!(c->dns_server_browser = avahi_s_dns_server_browser_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery = AVAHI_PROTO_INET6, AVAHI_LOOKUP_USE_MULTICAST, dns_server_browser_callback, c)))
             goto fail;
         client_output_printf(c, "+ Browsing ...\n");
+
+        avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
+
     } 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)))
+        if (!(c->dns_server_browser = avahi_s_dns_server_browser_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery = AVAHI_PROTO_UNSPEC, AVAHI_LOOKUP_USE_MULTICAST, dns_server_browser_callback, c)))
             goto fail;
         client_output_printf(c, "+ Browsing ...\n");
+
+        avahi_log_debug(__FILE__": Got %s request for '%s'.", cmd, arg);
+
     } else {
         client_output_printf(c, "%+i Invalid command \"%s\", try \"HELP\".\n", AVAHI_ERR_INVALID_OPERATION, cmd);
         c->state = CLIENT_DEAD;
+
+        avahi_log_debug(__FILE__": Got invalid request '%s'.", cmd);
     }
 
     return;
@@ -329,7 +370,7 @@ static void handle_input(Client *c) {
     }
 }
 
-static void client_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata) {
+static void client_work(AvahiWatch *watch, AVAHI_GCC_UNUSED int fd, AvahiWatchEvent events, void *userdata) {
     Client *c = userdata;
 
     assert(c);
@@ -377,7 +418,7 @@ static void client_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void
         (c->inbuf_length < sizeof(c->inbuf) ? AVAHI_WATCH_IN : 0));
 }
 
-static void server_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata) {
+static void server_work(AVAHI_GCC_UNUSED AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata) {
     Server *s = userdata;
 
     assert(s);
@@ -402,6 +443,7 @@ int simple_protocol_setup(const AvahiPoll *poll_api) {
     server->poll_api = poll_api;
     server->bind_successful = 0;
     server->fd = -1;
+    server->n_clients = 0;
     AVAHI_LLIST_HEAD_INIT(Client, server->clients);
     server->watch = NULL;
     
@@ -481,7 +523,7 @@ void simple_protocol_restart_queries(void) {
     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);
+            c->dns_server_browser = avahi_s_dns_server_browser_new(avahi_server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, c->afquery, AVAHI_LOOKUP_USE_MULTICAST, dns_server_browser_callback, c);
         }
 }