]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/afp_avahi.c
Merge remote-tracking branch 'origin/branch-netatalk-3-0' into develop
[netatalk.git] / etc / afpd / afp_avahi.c
index fc3627116be216f301ec713f3ce29cedb72bb3a9..5218d51758aebb0554298465c34c63436c67d541 100644 (file)
@@ -12,6 +12,7 @@
 #ifdef HAVE_AVAHI
 
 #include <unistd.h>
+#include <time.h>
 
 #include <avahi-common/strlst.h>
 
 #include <atalk/util.h>
 #include <atalk/dsi.h>
 #include <atalk/unicode.h>
+#include <atalk/netatalk_conf.h>
 
+#include "afp_zeroconf.h"
 #include "afp_avahi.h"
-#include "afp_config.h"
-#include "volume.h"
 
 /*****************************************************************
  * Global variables
@@ -43,11 +44,11 @@ static void publish_reply(AvahiEntryGroup *g,
  */
 static void register_stuff(void) {
     uint port;
-    const AFPConfig *config;
     const struct vol *volume;
     DSI *dsi;
     char name[MAXINSTANCENAMELEN+1];
     AvahiStringList *strlist = NULL;
+    AvahiStringList *strlist2 = NULL;
     char tmpname[256];
 
     assert(ctx->client);
@@ -69,7 +70,7 @@ static void register_stuff(void) {
                
         for (volume = getvolumes(); volume; volume = volume->v_next) {
 
-            if (convert_string(CH_UCS2, CH_UTF8_MAC, volume->v_name, -1, tmpname, 255) <= 0) {
+            if (convert_string(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, -1, tmpname, 255) <= 0) {
                 LOG ( log_error, logtype_afpd, "Could not set Zeroconf volume name for TimeMachine");
                 goto fail;
             }
@@ -90,23 +91,33 @@ static void register_stuff(void) {
         }
 
         /* AFP server */
-        for (config = ctx->configs; config; config = config->next) {
-
-            dsi = (DSI *)config->obj.handle;
+        for (dsi = ctx->obj->dsi; dsi; dsi = dsi->next) {
             port = getip_port((struct sockaddr *)&dsi->server);
-            
-            if (convert_string(config->obj.options.unixcharset, CH_UTF8,
-                               config->obj.options.server ? config->obj.options.server : config->obj.options.hostname, -1,
-                               name, MAXINSTANCENAMELEN) <= 0) {
-                LOG ( log_error, logtype_afpd, "Could not set Zeroconf instance name");
+
+            LOG(log_info, logtype_afpd, "hostname: %s", ctx->obj->options.hostname);
+
+            if (convert_string(ctx->obj->options.unixcharset,
+                               CH_UTF8,
+                               ctx->obj->options.hostname,
+                               -1,
+                               name,
+                               MAXINSTANCENAMELEN) <= 0) {
+                LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name: %s", ctx->obj->options.hostname);
                 goto fail;
             }
+            if ((dsi->bonjourname = strdup(name)) == NULL) {
+                LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name");
+                goto fail;
+
+            }
+            LOG(log_info, logtype_afpd, "Registering server '%s' with Bonjour",
+                dsi->bonjourname);
 
             if (avahi_entry_group_add_service(ctx->group,
                                               AVAHI_IF_UNSPEC,
                                               AVAHI_PROTO_UNSPEC,
                                               0,
-                                              name,
+                                              dsi->bonjourname,
                                               AFP_DNS_SERVICE_TYPE,
                                               NULL,
                                               NULL,
@@ -121,7 +132,7 @@ static void register_stuff(void) {
                                                           AVAHI_IF_UNSPEC,
                                                           AVAHI_PROTO_UNSPEC,
                                                           0,
-                                                          name,
+                                                          dsi->bonjourname,
                                                           ADISK_SERVICE_TYPE,
                                                           NULL,
                                                           NULL,
@@ -131,6 +142,25 @@ static void register_stuff(void) {
                     avahi_strerror(avahi_client_errno(ctx->client)));
                 goto fail;
             }  /* if */
+
+            if (ctx->obj->options.mimicmodel) {
+                strlist2 = avahi_string_list_add_printf(strlist2, "model=%s", ctx->obj->options.mimicmodel);
+                if (avahi_entry_group_add_service_strlst(ctx->group,
+                                                         AVAHI_IF_UNSPEC,
+                                                         AVAHI_PROTO_UNSPEC,
+                                                         0,
+                                                         dsi->bonjourname,
+                                                         DEV_INFO_SERVICE_TYPE,
+                                                         NULL,
+                                                         NULL,
+                                                         0,
+                                                         strlist2) < 0) {
+                    LOG(log_error, logtype_afpd, "Failed to add service: %s",
+                        avahi_strerror(avahi_client_errno(ctx->client)));
+                    goto fail;
+                }
+            } /* if (config->obj.options.mimicmodel) */
+
         }      /* for config*/
 
         if (avahi_entry_group_commit(ctx->group) < 0) {
@@ -144,8 +174,8 @@ static void register_stuff(void) {
     return;
 
 fail:
-    avahi_client_free (ctx->client);
-    avahi_threaded_poll_quit(ctx->threaded_poll);
+    time(NULL);
+//    avahi_threaded_poll_quit(ctx->threaded_poll);
 }
 
 /* Called when publishing of service data completes */
@@ -166,14 +196,12 @@ static void publish_reply(AvahiEntryGroup *g,
         /* With multiple names there's no way to know which one collided */
         LOG(log_error, logtype_afpd, "publish_reply: AVAHI_ENTRY_GROUP_COLLISION",
             avahi_strerror(avahi_client_errno(ctx->client)));
-        avahi_client_free(avahi_entry_group_get_client(g));
         avahi_threaded_poll_quit(ctx->threaded_poll);
         break;
                
     case AVAHI_ENTRY_GROUP_FAILURE:
         LOG(log_error, logtype_afpd, "Failed to register service: %s",
             avahi_strerror(avahi_client_errno(ctx->client)));
-        avahi_client_free(avahi_entry_group_get_client(g));
         avahi_threaded_poll_quit(ctx->threaded_poll);
         break;
 
@@ -221,14 +249,12 @@ static void client_callback(AvahiClient *client,
                 LOG(log_error, logtype_afpd, "Failed to contact server: %s",
                     avahi_strerror(error));
 
-                avahi_client_free (ctx->client);
                 avahi_threaded_poll_quit(ctx->threaded_poll);
             }
 
         } else {
             LOG(log_error, logtype_afpd, "Client failure: %s",
                 avahi_strerror(avahi_client_errno(client)));
-            avahi_client_free (ctx->client);
             avahi_threaded_poll_quit(ctx->threaded_poll);
         }
         break;
@@ -249,8 +275,8 @@ static void client_callback(AvahiClient *client,
  * Tries to setup the Zeroconf thread and any
  * neccessary config setting.
  */
-void av_zeroconf_setup(const AFPConfig *configs) {
-    int error, ret;
+void av_zeroconf_register(const AFPObj *obj) {
+    int error;
 
     /* initialize the struct that holds our config settings. */
     if (ctx) {
@@ -258,7 +284,7 @@ void av_zeroconf_setup(const AFPConfig *configs) {
         avahi_entry_group_reset(ctx->group);
     } else {
         ctx = calloc(1, sizeof(struct context));
-        ctx->configs = configs;
+        ctx->obj = obj;
         assert(ctx);
     }
 
@@ -274,26 +300,10 @@ void av_zeroconf_setup(const AFPConfig *configs) {
                                          NULL,
                                          &error))) {
         LOG(log_error, logtype_afpd, "Failed to create client object: %s",
-            avahi_strerror(avahi_client_errno(ctx->client)));
+            avahi_strerror(error));
         goto fail;
     }
 
-    return;
-
-fail:
-    if (ctx)
-        av_zeroconf_unregister();
-
-    return;
-}
-
-/*
- * This function finally runs the loop impl.
- */
-int av_zeroconf_run(void) {
-    int ret;
-
-    /* Finally, start the event loop thread */
     if (avahi_threaded_poll_start(ctx->threaded_poll) < 0) {
         LOG(log_error, logtype_afpd, "Failed to create thread: %s",
             avahi_strerror(avahi_client_errno(ctx->client)));
@@ -303,26 +313,12 @@ int av_zeroconf_run(void) {
     }
 
     ctx->thread_running = 1;
-    return 0;
+    return;
 
 fail:
-    if (ctx)
-        av_zeroconf_unregister();
-
-    return -1;
-}
+    av_zeroconf_unregister();
 
-/*
- * Tries to shutdown this loop impl.
- * Call this function from outside this thread.
- */
-void av_zeroconf_shutdown() {
-    /* Call this when the app shuts down */
-    avahi_threaded_poll_stop(ctx->threaded_poll);
-    avahi_client_free(ctx->client);
-    avahi_threaded_poll_free(ctx->threaded_poll);
-    free(ctx);
-    ctx = NULL;
+    return;
 }
 
 /*
@@ -330,27 +326,21 @@ void av_zeroconf_shutdown() {
  * Call this function from inside this thread.
  */
 int av_zeroconf_unregister() {
-    if (ctx->thread_running) {
-        /* First, block the event loop */
-        avahi_threaded_poll_lock(ctx->threaded_poll);
+    LOG(log_error, logtype_afpd, "av_zeroconf_unregister");
 
-        /* Than, do your stuff */
-        avahi_threaded_poll_quit(ctx->threaded_poll);
-
-        /* Finally, unblock the event loop */
-        avahi_threaded_poll_unlock(ctx->threaded_poll);
-        ctx->thread_running = 0;
+    if (ctx) {
+        LOG(log_error, logtype_afpd, "av_zeroconf_unregister: avahi_threaded_poll_stop");
+        if (ctx->threaded_poll)
+            avahi_threaded_poll_stop(ctx->threaded_poll);
+        LOG(log_error, logtype_afpd, "av_zeroconf_unregister: avahi_client_free");
+        if (ctx->client)
+            avahi_client_free(ctx->client);
+        LOG(log_error, logtype_afpd, "av_zeroconf_unregister: avahi_threaded_poll_free");
+        if (ctx->threaded_poll)
+            avahi_threaded_poll_free(ctx->threaded_poll);
+        free(ctx);
+        ctx = NULL;
     }
-
-    if (ctx->client)
-        avahi_client_free(ctx->client);
-
-    if (ctx->threaded_poll)
-        avahi_threaded_poll_free(ctx->threaded_poll);
-
-    free(ctx);
-    ctx = NULL;
-
     return 0;
 }