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_zeroconf.h"
26 #include "afp_avahi.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) {
47 const struct vol *volume;
49 char name[MAXINSTANCENAMELEN+1];
50 AvahiStringList *strlist = NULL;
51 AvahiStringList *strlist2 = NULL;
57 if (!(ctx->group = avahi_entry_group_new(ctx->client, publish_reply, ctx))) {
58 LOG(log_error, logtype_afpd, "Failed to create entry group: %s",
59 avahi_strerror(avahi_client_errno(ctx->client)));
64 if (avahi_entry_group_is_empty(ctx->group)) {
65 /* Register our service */
67 /* Build AFP volumes list */
69 strlist = avahi_string_list_add_printf(strlist, "sys=waMa=0,adVF=0x100");
71 for (volume = getvolumes(); volume; volume = volume->v_next) {
73 if (convert_string(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, -1, tmpname, 255) <= 0) {
74 LOG ( log_error, logtype_afpd, "Could not set Zeroconf volume name for TimeMachine");
78 if (volume->v_flags & AFPVOL_TM) {
80 LOG(log_info, logtype_afpd, "Registering volume '%s' with UUID: '%s' for TimeMachine",
81 volume->v_localname, volume->v_uuid);
82 strlist = avahi_string_list_add_printf(strlist, "dk%u=adVN=%s,adVF=0xa1,adVU=%s",
83 i++, tmpname, volume->v_uuid);
85 LOG(log_warning, logtype_afpd, "Registering volume '%s' for TimeMachine. But UUID is invalid.",
87 strlist = avahi_string_list_add_printf(strlist, "dk%u=adVN=%s,adVF=0xa1",
94 for (dsi = ctx->obj->dsi; dsi; dsi = dsi->next) {
95 port = getip_port((struct sockaddr *)&dsi->server);
97 LOG(log_info, logtype_afpd, "hostname: %s", ctx->obj->options.hostname);
99 if (convert_string(ctx->obj->options.unixcharset,
101 ctx->obj->options.hostname,
104 MAXINSTANCENAMELEN) <= 0) {
105 LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name: %s", ctx->obj->options.hostname);
108 if ((dsi->bonjourname = strdup(name)) == NULL) {
109 LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name");
113 LOG(log_info, logtype_afpd, "Registering server '%s' with Bonjour",
116 if (avahi_entry_group_add_service(ctx->group,
121 AFP_DNS_SERVICE_TYPE,
126 LOG(log_error, logtype_afpd, "Failed to add service: %s",
127 avahi_strerror(avahi_client_errno(ctx->client)));
131 if (i && avahi_entry_group_add_service_strlst(ctx->group,
141 LOG(log_error, logtype_afpd, "Failed to add service: %s",
142 avahi_strerror(avahi_client_errno(ctx->client)));
146 if (ctx->obj->options.mimicmodel) {
147 strlist2 = avahi_string_list_add_printf(strlist2, "model=%s", ctx->obj->options.mimicmodel);
148 if (avahi_entry_group_add_service_strlst(ctx->group,
153 DEV_INFO_SERVICE_TYPE,
158 LOG(log_error, logtype_afpd, "Failed to add service: %s",
159 avahi_strerror(avahi_client_errno(ctx->client)));
162 } /* if (config->obj.options.mimicmodel) */
166 if (avahi_entry_group_commit(ctx->group) < 0) {
167 LOG(log_error, logtype_afpd, "Failed to commit entry group: %s",
168 avahi_strerror(avahi_client_errno(ctx->client)));
172 } /* if avahi_entry_group_is_empty*/
178 // avahi_threaded_poll_quit(ctx->threaded_poll);
181 /* Called when publishing of service data completes */
182 static void publish_reply(AvahiEntryGroup *g,
183 AvahiEntryGroupState state,
184 AVAHI_GCC_UNUSED void *userdata)
186 assert(ctx->group == NULL || g == ctx->group);
190 case AVAHI_ENTRY_GROUP_ESTABLISHED :
191 /* The entry group has been established successfully */
192 LOG(log_debug, logtype_afpd, "publish_reply: AVAHI_ENTRY_GROUP_ESTABLISHED");
195 case AVAHI_ENTRY_GROUP_COLLISION:
196 /* With multiple names there's no way to know which one collided */
197 LOG(log_error, logtype_afpd, "publish_reply: AVAHI_ENTRY_GROUP_COLLISION",
198 avahi_strerror(avahi_client_errno(ctx->client)));
199 avahi_threaded_poll_quit(ctx->threaded_poll);
202 case AVAHI_ENTRY_GROUP_FAILURE:
203 LOG(log_error, logtype_afpd, "Failed to register service: %s",
204 avahi_strerror(avahi_client_errno(ctx->client)));
205 avahi_threaded_poll_quit(ctx->threaded_poll);
208 case AVAHI_ENTRY_GROUP_UNCOMMITED:
210 case AVAHI_ENTRY_GROUP_REGISTERING:
215 static void client_callback(AvahiClient *client,
216 AvahiClientState state,
219 ctx->client = client;
222 case AVAHI_CLIENT_S_RUNNING:
223 /* The server has startup successfully and registered its host
224 * name on the network, so it's time to create our services */
229 case AVAHI_CLIENT_S_COLLISION:
231 avahi_entry_group_reset(ctx->group);
234 case AVAHI_CLIENT_FAILURE: {
235 if (avahi_client_errno(client) == AVAHI_ERR_DISCONNECTED) {
238 avahi_client_free(ctx->client);
242 /* Reconnect to the server */
243 if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
244 AVAHI_CLIENT_NO_FAIL,
249 LOG(log_error, logtype_afpd, "Failed to contact server: %s",
250 avahi_strerror(error));
252 avahi_threaded_poll_quit(ctx->threaded_poll);
256 LOG(log_error, logtype_afpd, "Client failure: %s",
257 avahi_strerror(avahi_client_errno(client)));
258 avahi_threaded_poll_quit(ctx->threaded_poll);
263 case AVAHI_CLIENT_S_REGISTERING:
265 case AVAHI_CLIENT_CONNECTING:
270 /************************************************************************
272 ************************************************************************/
275 * Tries to setup the Zeroconf thread and any
276 * neccessary config setting.
278 void av_zeroconf_register(const AFPObj *obj) {
281 /* initialize the struct that holds our config settings. */
283 LOG(log_debug, logtype_afpd, "Resetting zeroconf records");
284 avahi_entry_group_reset(ctx->group);
286 ctx = calloc(1, sizeof(struct context));
291 /* first of all we need to initialize our threading env */
292 if (!(ctx->threaded_poll = avahi_threaded_poll_new())) {
296 /* now we need to acquire a client */
297 if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
298 AVAHI_CLIENT_NO_FAIL,
302 LOG(log_error, logtype_afpd, "Failed to create client object: %s",
303 avahi_strerror(error));
307 if (avahi_threaded_poll_start(ctx->threaded_poll) < 0) {
308 LOG(log_error, logtype_afpd, "Failed to create thread: %s",
309 avahi_strerror(avahi_client_errno(ctx->client)));
312 LOG(log_info, logtype_afpd, "Successfully started avahi loop.");
315 ctx->thread_running = 1;
319 av_zeroconf_unregister();
325 * Tries to shutdown this loop impl.
326 * Call this function from inside this thread.
328 int av_zeroconf_unregister() {
329 LOG(log_error, logtype_afpd, "av_zeroconf_unregister");
332 LOG(log_error, logtype_afpd, "av_zeroconf_unregister: avahi_threaded_poll_stop");
333 if (ctx->threaded_poll)
334 avahi_threaded_poll_stop(ctx->threaded_poll);
335 LOG(log_error, logtype_afpd, "av_zeroconf_unregister: avahi_client_free");
337 avahi_client_free(ctx->client);
338 LOG(log_error, logtype_afpd, "av_zeroconf_unregister: avahi_threaded_poll_free");
339 if (ctx->threaded_poll)
340 avahi_threaded_poll_free(ctx->threaded_poll);
347 #endif /* USE_AVAHI */