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>
24 #include "afp_avahi.h"
25 #include "afp_config.h"
28 /*****************************************************************
30 *****************************************************************/
31 struct context *ctx = NULL;
33 /*****************************************************************
35 *****************************************************************/
37 static void publish_reply(AvahiEntryGroup *g,
38 AvahiEntryGroupState state,
42 * This function tries to register the AFP DNS
45 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 = obj->dsi; dsi; dsi = dsi->next) {
96 port = getip_port((struct sockaddr *)&dsi->server);
98 if (convert_string(obj->options.unixcharset,
100 obj->options.hostname,
103 MAXINSTANCENAMELEN) <= 0) {
104 LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name");
107 if ((dsi->bonjourname = strdup(name)) == NULL) {
108 LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name");
112 LOG(log_info, logtype_afpd, "Registering server '%s' with Bonjour",
115 if (avahi_entry_group_add_service(ctx->group,
120 AFP_DNS_SERVICE_TYPE,
125 LOG(log_error, logtype_afpd, "Failed to add service: %s",
126 avahi_strerror(avahi_client_errno(ctx->client)));
130 if (i && avahi_entry_group_add_service_strlst(ctx->group,
140 LOG(log_error, logtype_afpd, "Failed to add service: %s",
141 avahi_strerror(avahi_client_errno(ctx->client)));
145 if (obj->options.mimicmodel) {
146 strlist2 = avahi_string_list_add_printf(strlist2, "model=%s", obj->options.mimicmodel);
147 if (avahi_entry_group_add_service_strlst(ctx->group,
152 DEV_INFO_SERVICE_TYPE,
157 LOG(log_error, logtype_afpd, "Failed to add service: %s",
158 avahi_strerror(avahi_client_errno(ctx->client)));
161 } /* if (config->obj.options.mimicmodel) */
165 if (avahi_entry_group_commit(ctx->group) < 0) {
166 LOG(log_error, logtype_afpd, "Failed to commit entry group: %s",
167 avahi_strerror(avahi_client_errno(ctx->client)));
171 } /* if avahi_entry_group_is_empty*/
177 // avahi_threaded_poll_quit(ctx->threaded_poll);
180 /* Called when publishing of service data completes */
181 static void publish_reply(AvahiEntryGroup *g,
182 AvahiEntryGroupState state,
183 AVAHI_GCC_UNUSED void *userdata)
185 assert(ctx->group == NULL || g == ctx->group);
189 case AVAHI_ENTRY_GROUP_ESTABLISHED :
190 /* The entry group has been established successfully */
191 LOG(log_debug, logtype_afpd, "publish_reply: AVAHI_ENTRY_GROUP_ESTABLISHED");
194 case AVAHI_ENTRY_GROUP_COLLISION:
195 /* With multiple names there's no way to know which one collided */
196 LOG(log_error, logtype_afpd, "publish_reply: AVAHI_ENTRY_GROUP_COLLISION",
197 avahi_strerror(avahi_client_errno(ctx->client)));
198 avahi_threaded_poll_quit(ctx->threaded_poll);
201 case AVAHI_ENTRY_GROUP_FAILURE:
202 LOG(log_error, logtype_afpd, "Failed to register service: %s",
203 avahi_strerror(avahi_client_errno(ctx->client)));
204 avahi_threaded_poll_quit(ctx->threaded_poll);
207 case AVAHI_ENTRY_GROUP_UNCOMMITED:
209 case AVAHI_ENTRY_GROUP_REGISTERING:
214 static void client_callback(AvahiClient *client,
215 AvahiClientState state,
218 ctx->client = client;
221 case AVAHI_CLIENT_S_RUNNING:
222 /* The server has startup successfully and registered its host
223 * name on the network, so it's time to create our services */
228 case AVAHI_CLIENT_S_COLLISION:
230 avahi_entry_group_reset(ctx->group);
233 case AVAHI_CLIENT_FAILURE: {
234 if (avahi_client_errno(client) == AVAHI_ERR_DISCONNECTED) {
237 avahi_client_free(ctx->client);
241 /* Reconnect to the server */
242 if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
243 AVAHI_CLIENT_NO_FAIL,
248 LOG(log_error, logtype_afpd, "Failed to contact server: %s",
249 avahi_strerror(error));
251 avahi_threaded_poll_quit(ctx->threaded_poll);
255 LOG(log_error, logtype_afpd, "Client failure: %s",
256 avahi_strerror(avahi_client_errno(client)));
257 avahi_threaded_poll_quit(ctx->threaded_poll);
262 case AVAHI_CLIENT_S_REGISTERING:
264 case AVAHI_CLIENT_CONNECTING:
269 /************************************************************************
271 ************************************************************************/
274 * Tries to setup the Zeroconf thread and any
275 * neccessary config setting.
277 void av_zeroconf_register(const AFPObj *obj) {
280 /* initialize the struct that holds our config settings. */
282 LOG(log_debug, logtype_afpd, "Resetting zeroconf records");
283 avahi_entry_group_reset(ctx->group);
285 ctx = calloc(1, sizeof(struct context));
290 /* first of all we need to initialize our threading env */
291 if (!(ctx->threaded_poll = avahi_threaded_poll_new())) {
295 /* now we need to acquire a client */
296 if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
297 AVAHI_CLIENT_NO_FAIL,
301 LOG(log_error, logtype_afpd, "Failed to create client object: %s",
302 avahi_strerror(error));
306 if (avahi_threaded_poll_start(ctx->threaded_poll) < 0) {
307 LOG(log_error, logtype_afpd, "Failed to create thread: %s",
308 avahi_strerror(avahi_client_errno(ctx->client)));
311 LOG(log_info, logtype_afpd, "Successfully started avahi loop.");
314 ctx->thread_running = 1;
318 av_zeroconf_unregister();
324 * Tries to shutdown this loop impl.
325 * Call this function from inside this thread.
327 int av_zeroconf_unregister() {
328 LOG(log_error, logtype_afpd, "av_zeroconf_unregister");
331 LOG(log_error, logtype_afpd, "av_zeroconf_unregister: avahi_threaded_poll_stop");
332 if (ctx->threaded_poll)
333 avahi_threaded_poll_stop(ctx->threaded_poll);
334 LOG(log_error, logtype_afpd, "av_zeroconf_unregister: avahi_client_free");
336 avahi_client_free(ctx->client);
337 LOG(log_error, logtype_afpd, "av_zeroconf_unregister: avahi_threaded_poll_free");
338 if (ctx->threaded_poll)
339 avahi_threaded_poll_free(ctx->threaded_poll);
346 #endif /* USE_AVAHI */