2 * Author: Daniel S. Haischt <me@daniel.stefan.haischt.name>
3 * Purpose: Avahi based Zeroconf support
4 * Docs: http://avahi.org/download/doxygen/
17 #include <avahi-common/strlst.h>
19 #include <atalk/logger.h>
20 #include <atalk/util.h>
21 #include <atalk/dsi.h>
22 #include <atalk/unicode.h>
23 #include <atalk/netatalk_conf.h>
25 #include "afp_avahi.h"
26 #include "afp_config.h"
29 /*****************************************************************
31 *****************************************************************/
32 struct context *ctx = NULL;
34 /*****************************************************************
36 *****************************************************************/
38 static void publish_reply(AvahiEntryGroup *g,
39 AvahiEntryGroupState state,
43 * This function tries to register the AFP DNS
46 static void register_stuff(void) {
48 const struct vol *volume;
50 char name[MAXINSTANCENAMELEN+1];
51 AvahiStringList *strlist = NULL;
52 AvahiStringList *strlist2 = NULL;
58 if (!(ctx->group = avahi_entry_group_new(ctx->client, publish_reply, ctx))) {
59 LOG(log_error, logtype_afpd, "Failed to create entry group: %s",
60 avahi_strerror(avahi_client_errno(ctx->client)));
65 if (avahi_entry_group_is_empty(ctx->group)) {
66 /* Register our service */
68 /* Build AFP volumes list */
70 strlist = avahi_string_list_add_printf(strlist, "sys=waMa=0,adVF=0x100");
72 for (volume = getvolumes(); volume; volume = volume->v_next) {
74 if (convert_string(CH_UCS2, CH_UTF8_MAC, volume->v_name, -1, tmpname, 255) <= 0) {
75 LOG ( log_error, logtype_afpd, "Could not set Zeroconf volume name for TimeMachine");
79 if (volume->v_flags & AFPVOL_TM) {
81 LOG(log_info, logtype_afpd, "Registering volume '%s' with UUID: '%s' for TimeMachine",
82 volume->v_localname, volume->v_uuid);
83 strlist = avahi_string_list_add_printf(strlist, "dk%u=adVN=%s,adVF=0xa1,adVU=%s",
84 i++, tmpname, volume->v_uuid);
86 LOG(log_warning, logtype_afpd, "Registering volume '%s' for TimeMachine. But UUID is invalid.",
88 strlist = avahi_string_list_add_printf(strlist, "dk%u=adVN=%s,adVF=0xa1",
95 for (dsi = ctx->obj->dsi; dsi; dsi = dsi->next) {
96 port = getip_port((struct sockaddr *)&dsi->server);
98 LOG(log_error, logtype_afpd, "hostname: %s", ctx->obj->options.hostname);
100 if (convert_string(ctx->obj->options.unixcharset,
102 ctx->obj->options.hostname,
105 MAXINSTANCENAMELEN) <= 0) {
106 LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name: %s", ctx->obj->options.hostname);
109 if ((dsi->bonjourname = strdup(name)) == NULL) {
110 LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name");
114 LOG(log_info, logtype_afpd, "Registering server '%s' with Bonjour",
117 if (avahi_entry_group_add_service(ctx->group,
122 AFP_DNS_SERVICE_TYPE,
127 LOG(log_error, logtype_afpd, "Failed to add service: %s",
128 avahi_strerror(avahi_client_errno(ctx->client)));
132 if (i && avahi_entry_group_add_service_strlst(ctx->group,
142 LOG(log_error, logtype_afpd, "Failed to add service: %s",
143 avahi_strerror(avahi_client_errno(ctx->client)));
147 if (ctx->obj->options.mimicmodel) {
148 strlist2 = avahi_string_list_add_printf(strlist2, "model=%s", ctx->obj->options.mimicmodel);
149 if (avahi_entry_group_add_service_strlst(ctx->group,
154 DEV_INFO_SERVICE_TYPE,
159 LOG(log_error, logtype_afpd, "Failed to add service: %s",
160 avahi_strerror(avahi_client_errno(ctx->client)));
163 } /* if (config->obj.options.mimicmodel) */
167 if (avahi_entry_group_commit(ctx->group) < 0) {
168 LOG(log_error, logtype_afpd, "Failed to commit entry group: %s",
169 avahi_strerror(avahi_client_errno(ctx->client)));
173 } /* if avahi_entry_group_is_empty*/
179 // avahi_threaded_poll_quit(ctx->threaded_poll);
182 /* Called when publishing of service data completes */
183 static void publish_reply(AvahiEntryGroup *g,
184 AvahiEntryGroupState state,
185 AVAHI_GCC_UNUSED void *userdata)
187 assert(ctx->group == NULL || g == ctx->group);
191 case AVAHI_ENTRY_GROUP_ESTABLISHED :
192 /* The entry group has been established successfully */
193 LOG(log_debug, logtype_afpd, "publish_reply: AVAHI_ENTRY_GROUP_ESTABLISHED");
196 case AVAHI_ENTRY_GROUP_COLLISION:
197 /* With multiple names there's no way to know which one collided */
198 LOG(log_error, logtype_afpd, "publish_reply: AVAHI_ENTRY_GROUP_COLLISION",
199 avahi_strerror(avahi_client_errno(ctx->client)));
200 avahi_threaded_poll_quit(ctx->threaded_poll);
203 case AVAHI_ENTRY_GROUP_FAILURE:
204 LOG(log_error, logtype_afpd, "Failed to register service: %s",
205 avahi_strerror(avahi_client_errno(ctx->client)));
206 avahi_threaded_poll_quit(ctx->threaded_poll);
209 case AVAHI_ENTRY_GROUP_UNCOMMITED:
211 case AVAHI_ENTRY_GROUP_REGISTERING:
216 static void client_callback(AvahiClient *client,
217 AvahiClientState state,
220 ctx->client = client;
223 case AVAHI_CLIENT_S_RUNNING:
224 /* The server has startup successfully and registered its host
225 * name on the network, so it's time to create our services */
230 case AVAHI_CLIENT_S_COLLISION:
232 avahi_entry_group_reset(ctx->group);
235 case AVAHI_CLIENT_FAILURE: {
236 if (avahi_client_errno(client) == AVAHI_ERR_DISCONNECTED) {
239 avahi_client_free(ctx->client);
243 /* Reconnect to the server */
244 if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
245 AVAHI_CLIENT_NO_FAIL,
250 LOG(log_error, logtype_afpd, "Failed to contact server: %s",
251 avahi_strerror(error));
253 avahi_threaded_poll_quit(ctx->threaded_poll);
257 LOG(log_error, logtype_afpd, "Client failure: %s",
258 avahi_strerror(avahi_client_errno(client)));
259 avahi_threaded_poll_quit(ctx->threaded_poll);
264 case AVAHI_CLIENT_S_REGISTERING:
266 case AVAHI_CLIENT_CONNECTING:
271 /************************************************************************
273 ************************************************************************/
276 * Tries to setup the Zeroconf thread and any
277 * neccessary config setting.
279 void av_zeroconf_register(const AFPObj *obj) {
282 /* initialize the struct that holds our config settings. */
284 LOG(log_debug, logtype_afpd, "Resetting zeroconf records");
285 avahi_entry_group_reset(ctx->group);
287 ctx = calloc(1, sizeof(struct context));
292 /* first of all we need to initialize our threading env */
293 if (!(ctx->threaded_poll = avahi_threaded_poll_new())) {
297 /* now we need to acquire a client */
298 if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
299 AVAHI_CLIENT_NO_FAIL,
303 LOG(log_error, logtype_afpd, "Failed to create client object: %s",
304 avahi_strerror(error));
308 if (avahi_threaded_poll_start(ctx->threaded_poll) < 0) {
309 LOG(log_error, logtype_afpd, "Failed to create thread: %s",
310 avahi_strerror(avahi_client_errno(ctx->client)));
313 LOG(log_info, logtype_afpd, "Successfully started avahi loop.");
316 ctx->thread_running = 1;
320 av_zeroconf_unregister();
326 * Tries to shutdown this loop impl.
327 * Call this function from inside this thread.
329 int av_zeroconf_unregister() {
330 LOG(log_error, logtype_afpd, "av_zeroconf_unregister");
333 LOG(log_error, logtype_afpd, "av_zeroconf_unregister: avahi_threaded_poll_stop");
334 if (ctx->threaded_poll)
335 avahi_threaded_poll_stop(ctx->threaded_poll);
336 LOG(log_error, logtype_afpd, "av_zeroconf_unregister: avahi_client_free");
338 avahi_client_free(ctx->client);
339 LOG(log_error, logtype_afpd, "av_zeroconf_unregister: avahi_threaded_poll_free");
340 if (ctx->threaded_poll)
341 avahi_threaded_poll_free(ctx->threaded_poll);
348 #endif /* USE_AVAHI */