]> git.meshlink.io Git - catta/blobdiff - avahi-utils/avahi-browse.c
implement command line parsing, signal handling, daemonization
[catta] / avahi-utils / avahi-browse.c
index 38460f3517d2822be84835fddd8038b5d34f48ab..248b6518b28b5af36420f2177791f858beaa2a7b 100644 (file)
@@ -43,7 +43,7 @@
 
 #include "sigint.h"
 
-#ifdef HAVE_GDBM
+#if defined(HAVE_GDBM) || defined(HAVE_DBM)
 #include "stdb.h"
 #endif
 
@@ -64,7 +64,8 @@ typedef struct Config {
     int ignore_local;
     Command command;
     int resolve;
-#ifdef HAVE_GDBM
+    int no_fail;
+#if defined(HAVE_GDBM) || defined(HAVE_DBM)
     int no_db_lookup;
 #endif
 } Config;
@@ -88,6 +89,7 @@ static int n_all_for_now = 0, n_cache_exhausted = 0, n_resolving = 0;
 static AvahiStringList *browsed_types = NULL;
 static ServiceInfo *services = NULL;
 static int n_columns = 80;
+static int browsing = 0;
 
 static void check_terminate(Config *c) {
 
@@ -125,8 +127,8 @@ static ServiceInfo *find_service(AvahiIfIndex interface, AvahiProtocol protocol,
         if (i->interface == interface &&
             i->protocol == protocol &&
             strcasecmp(i->name, name) == 0 &&
-            avahi_domain_equal(i->type, type) == 0 &&
-            avahi_domain_equal(i->domain, domain) == 0)
+            avahi_domain_equal(i->type, type) &&
+            avahi_domain_equal(i->domain, domain))
 
             return i;
 
@@ -136,7 +138,7 @@ static ServiceInfo *find_service(AvahiIfIndex interface, AvahiProtocol protocol,
 static void print_service_line(Config *config, char c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
     char ifname[IF_NAMESIZE];
 
-#ifdef HAVE_GDBM
+#if defined(HAVE_GDBM) || defined(HAVE_DBM)
     if (!config->no_db_lookup)
         type = stdb_lookup(type);
 #endif
@@ -146,6 +148,7 @@ static void print_service_line(Config *config, char c, AvahiIfIndex interface, A
            interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a",
            protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a", 
            n_columns-35, name, type, domain);
+    fflush(stdout);
 }
 
 static void service_resolver_callback(
@@ -204,6 +207,7 @@ static void service_resolver_callback(
     assert(n_resolving > 0);
     n_resolving--;
     check_terminate(i->config);
+    fflush(stdout);
 }
 
 static ServiceInfo *add_service(Config *c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
@@ -473,25 +477,114 @@ static void browse_domains(Config *c) {
     n_all_for_now++;
 }
 
+static int start(Config *config) {
+
+    assert(!browsing);
+    
+    if (config->verbose) {
+        const char *version, *hn;
+        
+        if (!(version = avahi_client_get_version_string(client))) {
+            fprintf(stderr, "Failed to query version string: %s\n", avahi_strerror(avahi_client_errno(client)));
+            return -1;
+        }
+        
+        if (!(hn = avahi_client_get_host_name_fqdn(client))) {
+            fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client)));
+            return -1;
+        }
+        
+        fprintf(stderr, "Server version: %s; Host name: %s\n", version, hn);
+        
+        if (config->command == COMMAND_BROWSE_DOMAINS)
+            fprintf(stderr, "E Ifce Prot Domain\n");
+        else
+            fprintf(stderr, "E Ifce Prot %-*s %-20s Domain\n", n_columns-35, "Name", "Type");
+    }
+    
+    if (config->command == COMMAND_BROWSE_SERVICES)
+        browse_service_type(config, config->stype, config->domain);
+    else if (config->command == COMMAND_BROWSE_ALL_SERVICES)
+        browse_all(config);
+    else {
+        assert(config->command == COMMAND_BROWSE_DOMAINS);
+        browse_domains(config);
+    }
+
+    browsing = 1;
+    return 0;
+}
+    
 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
+    Config *config = userdata;
+
+    /* This function might be called when avahi_client_new() has not
+     * returned yet.*/
+    client = c;
+    
     switch (state) {
         case AVAHI_CLIENT_FAILURE:
-            fprintf(stderr, "Client failure, exiting: %s\n", avahi_strerror(avahi_client_errno(c)));
-            avahi_simple_poll_quit(simple_poll);
+            
+            if (config->no_fail && avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
+                int error;
+
+                /* We have been disconnected, so let reconnect */
+
+                fprintf(stderr, "Disconnected, reconnecting ...\n");
+
+                avahi_client_free(client);
+                client = NULL;
+
+                avahi_string_list_free(browsed_types);
+                browsed_types = NULL;
+                
+                while (services)
+                    remove_service(config, services);
+
+                browsing = 0;
+
+                if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, config, &error))) {
+                    fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
+                    avahi_simple_poll_quit(simple_poll);
+                }
+
+            } else {
+                fprintf(stderr, "Client failure, exiting: %s\n", avahi_strerror(avahi_client_errno(c)));
+                avahi_simple_poll_quit(simple_poll);
+            }
+            
             break;
             
         case AVAHI_CLIENT_S_REGISTERING:
         case AVAHI_CLIENT_S_RUNNING:
         case AVAHI_CLIENT_S_COLLISION:
-            ;
+
+            if (!browsing)
+                if (start(config) < 0)
+                    avahi_simple_poll_quit(simple_poll);
+
+            break;
+            
+        case AVAHI_CLIENT_CONNECTING:
+            
+            if (config->verbose)
+                fprintf(stderr, "Waiting for daemon ...\n");
+
+            break;
     }
 }
 
 static void help(FILE *f, const char *argv0) {
-    fprintf(f,
-            "%s [options] <service type>\n"
-            "%s [options] -a\n"
-            "%s [options] -D\n\n"
+    if (strstr(argv0, "domain"))
+        fprintf(f, "%s [options] \n\n", argv0);
+    else
+        fprintf(f,
+                "%s [options] <service type>\n"
+                "%s [options] -a\n"
+                "%s [options] -D\n\n",
+                argv0, argv0, argv0);
+
+            fprintf(f, 
             "    -h --help            Show this help\n"
             "    -V --version         Show version\n"
             "    -D --browse-domains  Browse for browsing domains instead of services\n"
@@ -502,13 +595,14 @@ static void help(FILE *f, const char *argv0) {
             "    -c --cache           Terminate after dumping all entries from the cache\n"
             "    -l --ignore-local    Ignore local services\n"
             "    -r --resolve         Resolve services found\n"
-#ifdef HAVE_GDBM
+            "    -f --no-fail         Don't fail if the daemon is not available\n"
+#if defined(HAVE_GDBM) || defined(HAVE_DBM)
             "    -k --no-db-lookup    Don't lookup service types\n"
 #endif
-            , argv0, argv0, argv0);
+            );
 }
 
-static int parse_command_line(Config *c, int argc, char *argv[]) {
+static int parse_command_line(Config *c, const char *argv0, int argc, char *argv[]) {
     int o;
 
     static const struct option long_options[] = {
@@ -522,7 +616,8 @@ static int parse_command_line(Config *c, int argc, char *argv[]) {
         { "cache",          no_argument,       NULL, 'c' },
         { "ignore-local",   no_argument,       NULL, 'l' },
         { "resolve",        no_argument,       NULL, 'r' },
-#ifdef HAVE_GDBM
+        { "no-fail",        no_argument,       NULL, 'f' },
+#if defined(HAVE_GDBM) || defined(HAVE_DBM)
         { "no-db-lookup",   no_argument,       NULL, 'k' },
 #endif
         { NULL, 0, NULL, 0 }
@@ -530,21 +625,22 @@ static int parse_command_line(Config *c, int argc, char *argv[]) {
 
     assert(c);
 
-    c->command = COMMAND_BROWSE_SERVICES;
+    c->command = strstr(argv0, "domain") ? COMMAND_BROWSE_DOMAINS : COMMAND_BROWSE_SERVICES;
     c->verbose =
         c->terminate_on_cache_exhausted =
         c->terminate_on_all_for_now =
         c->ignore_local =
-        c->resolve = 0;
+        c->resolve =
+        c->no_fail = 0;
     c->domain = c->stype = NULL;
 
-#ifdef HAVE_GDBM
+#if defined(HAVE_GDBM) || defined(HAVE_DBM)
     c->no_db_lookup = 0;
 #endif
     
     opterr = 0;
-    while ((o = getopt_long(argc, argv, "hVd:avtclrD"
-#ifdef HAVE_GDBM
+    while ((o = getopt_long(argc, argv, "hVd:avtclrDf"
+#if defined(HAVE_GDBM) || defined(HAVE_DBM)
                             "k"
 #endif
                             , long_options, NULL)) >= 0) {
@@ -563,6 +659,7 @@ static int parse_command_line(Config *c, int argc, char *argv[]) {
                 c->command = COMMAND_BROWSE_DOMAINS;
                 break;
             case 'd':
+                avahi_free(c->domain);
                 c->domain = avahi_strdup(optarg);
                 break;
             case 'v':
@@ -580,7 +677,10 @@ static int parse_command_line(Config *c, int argc, char *argv[]) {
             case 'r':
                 c->resolve = 1;
                 break;
-#ifdef HAVE_GDBM
+            case 'f':
+                c->no_fail = 1;
+                break;
+#if defined(HAVE_GDBM) || defined(HAVE_DBM)
             case 'k':
                 c->no_db_lookup = 1;
                 break;
@@ -628,7 +728,7 @@ int main(int argc, char *argv[]) {
     if (n_columns < 40)
         n_columns = 40;
     
-    if (parse_command_line(&config, argc, argv) < 0)
+    if (parse_command_line(&config, argv0, argc, argv) < 0)
         goto fail;
 
     switch (config.command) {
@@ -654,40 +754,10 @@ int main(int argc, char *argv[]) {
             if (sigint_install(simple_poll) < 0)
                 goto fail;
             
-            if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), client_callback, NULL, &error))) {
+            if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), config.no_fail ? AVAHI_CLIENT_NO_FAIL : 0, client_callback, &config, &error))) {
                 fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
                 goto fail;
             }
-
-            if (config.verbose) {
-                const char *version, *hn;
-
-                if (!(version = avahi_client_get_version_string(client))) {
-                    fprintf(stderr, "Failed to query version string: %s\n", avahi_strerror(avahi_client_errno(client)));
-                    goto fail;
-                }
-
-                if (!(hn = avahi_client_get_host_name_fqdn(client))) {
-                    fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client)));
-                    goto fail;
-                }
-                
-                fprintf(stderr, "Server version: %s; Host name: %s\n", version, hn);
-
-                if (config.command == COMMAND_BROWSE_DOMAINS)
-                    fprintf(stderr, "E Ifce Prot Domain\n");
-                else
-                    fprintf(stderr, "E Ifce Prot %-*s %-20s Domain\n", n_columns-35, "Name", "Type");
-            }
-
-            if (config.command == COMMAND_BROWSE_SERVICES)
-                browse_service_type(&config, config.stype, config.domain);
-            else if (config.command == COMMAND_BROWSE_ALL_SERVICES)
-                browse_all(&config);
-            else {
-                assert(config.command == COMMAND_BROWSE_DOMAINS);
-                browse_domains(&config);
-            }
             
             avahi_simple_poll_loop(simple_poll);
             ret = 0;
@@ -713,7 +783,7 @@ fail:
 
     avahi_string_list_free(browsed_types);
 
-#ifdef HAVE_GDBM
+#if defined(HAVE_GDBM) || defined(HAVE_DBM)
     stdb_shutdown();
 #endif