]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_avahi.c
Merge master
[netatalk.git] / etc / afpd / afp_avahi.c
1 /*
2  * Author:  Daniel S. Haischt <me@daniel.stefan.haischt.name>
3  * Purpose: Avahi based Zeroconf support
4  * Docs:    http://avahi.org/download/doxygen/
5  *
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #ifdef HAVE_AVAHI
13
14 #include <unistd.h>
15 #include <time.h>
16
17 #include <avahi-common/strlst.h>
18
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>
24
25 #include "afp_zeroconf.h"
26 #include "afp_avahi.h"
27 #include "afp_config.h"
28 #include "volume.h"
29
30 /*****************************************************************
31  * Global variables
32  *****************************************************************/
33 struct context *ctx = NULL;
34
35 /*****************************************************************
36  * Private functions
37  *****************************************************************/
38
39 static void publish_reply(AvahiEntryGroup *g,
40                           AvahiEntryGroupState state,
41                           void *userdata);
42
43 /*
44  * This function tries to register the AFP DNS
45  * SRV service type.
46  */
47 static void register_stuff(void) {
48     uint port;
49     const struct vol *volume;
50     DSI *dsi;
51     char name[MAXINSTANCENAMELEN+1];
52     AvahiStringList *strlist = NULL;
53     AvahiStringList *strlist2 = NULL;
54     char tmpname[256];
55
56     assert(ctx->client);
57
58     if (!ctx->group) {
59         if (!(ctx->group = avahi_entry_group_new(ctx->client, publish_reply, ctx))) {
60             LOG(log_error, logtype_afpd, "Failed to create entry group: %s",
61                 avahi_strerror(avahi_client_errno(ctx->client)));
62             goto fail;
63         }
64     }
65
66     if (avahi_entry_group_is_empty(ctx->group)) {
67         /* Register our service */
68
69         /* Build AFP volumes list */
70         int i = 0;
71         strlist = avahi_string_list_add_printf(strlist, "sys=waMa=0,adVF=0x100");
72                 
73         for (volume = getvolumes(); volume; volume = volume->v_next) {
74
75             if (convert_string(CH_UCS2, CH_UTF8_MAC, volume->v_name, -1, tmpname, 255) <= 0) {
76                 LOG ( log_error, logtype_afpd, "Could not set Zeroconf volume name for TimeMachine");
77                 goto fail;
78             }
79
80             if (volume->v_flags & AFPVOL_TM) {
81                 if (volume->v_uuid) {
82                     LOG(log_info, logtype_afpd, "Registering volume '%s' with UUID: '%s' for TimeMachine",
83                         volume->v_localname, volume->v_uuid);
84                     strlist = avahi_string_list_add_printf(strlist, "dk%u=adVN=%s,adVF=0xa1,adVU=%s",
85                                                            i++, tmpname, volume->v_uuid);
86                 } else {
87                     LOG(log_warning, logtype_afpd, "Registering volume '%s' for TimeMachine. But UUID is invalid.",
88                         volume->v_localname);
89                     strlist = avahi_string_list_add_printf(strlist, "dk%u=adVN=%s,adVF=0xa1",
90                                                            i++, tmpname);
91                 }       
92             }
93         }
94
95         /* AFP server */
96         for (dsi = ctx->obj->dsi; dsi; dsi = dsi->next) {
97             port = getip_port((struct sockaddr *)&dsi->server);
98
99             LOG(log_error, logtype_afpd, "hostname: %s", ctx->obj->options.hostname);
100
101             if (convert_string(ctx->obj->options.unixcharset,
102                                CH_UTF8,
103                                ctx->obj->options.hostname,
104                                -1,
105                                name,
106                                MAXINSTANCENAMELEN) <= 0) {
107                 LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name: %s", ctx->obj->options.hostname);
108                 goto fail;
109             }
110             if ((dsi->bonjourname = strdup(name)) == NULL) {
111                 LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name");
112                 goto fail;
113
114             }
115             LOG(log_info, logtype_afpd, "Registering server '%s' with Bonjour",
116                 dsi->bonjourname);
117
118             if (avahi_entry_group_add_service(ctx->group,
119                                               AVAHI_IF_UNSPEC,
120                                               AVAHI_PROTO_UNSPEC,
121                                               0,
122                                               dsi->bonjourname,
123                                               AFP_DNS_SERVICE_TYPE,
124                                               NULL,
125                                               NULL,
126                                               port,
127                                               NULL) < 0) {
128                 LOG(log_error, logtype_afpd, "Failed to add service: %s",
129                     avahi_strerror(avahi_client_errno(ctx->client)));
130                 goto fail;
131             }
132
133             if (i && avahi_entry_group_add_service_strlst(ctx->group,
134                                                           AVAHI_IF_UNSPEC,
135                                                           AVAHI_PROTO_UNSPEC,
136                                                           0,
137                                                           dsi->bonjourname,
138                                                           ADISK_SERVICE_TYPE,
139                                                           NULL,
140                                                           NULL,
141                                                           9, /* discard */
142                                                           strlist) < 0) {
143                 LOG(log_error, logtype_afpd, "Failed to add service: %s",
144                     avahi_strerror(avahi_client_errno(ctx->client)));
145                 goto fail;
146             }   /* if */
147
148             if (ctx->obj->options.mimicmodel) {
149                 strlist2 = avahi_string_list_add_printf(strlist2, "model=%s", ctx->obj->options.mimicmodel);
150                 if (avahi_entry_group_add_service_strlst(ctx->group,
151                                                          AVAHI_IF_UNSPEC,
152                                                          AVAHI_PROTO_UNSPEC,
153                                                          0,
154                                                          dsi->bonjourname,
155                                                          DEV_INFO_SERVICE_TYPE,
156                                                          NULL,
157                                                          NULL,
158                                                          0,
159                                                          strlist2) < 0) {
160                     LOG(log_error, logtype_afpd, "Failed to add service: %s",
161                         avahi_strerror(avahi_client_errno(ctx->client)));
162                     goto fail;
163                 }
164             } /* if (config->obj.options.mimicmodel) */
165
166         }       /* for config*/
167
168         if (avahi_entry_group_commit(ctx->group) < 0) {
169             LOG(log_error, logtype_afpd, "Failed to commit entry group: %s",
170                 avahi_strerror(avahi_client_errno(ctx->client)));
171             goto fail;
172         }
173
174     }   /* if avahi_entry_group_is_empty*/
175
176     return;
177
178 fail:
179     time(NULL);
180 //    avahi_threaded_poll_quit(ctx->threaded_poll);
181 }
182
183 /* Called when publishing of service data completes */
184 static void publish_reply(AvahiEntryGroup *g,
185                           AvahiEntryGroupState state,
186                           AVAHI_GCC_UNUSED void *userdata)
187 {
188     assert(ctx->group == NULL || g == ctx->group);
189
190     switch (state) {
191
192     case AVAHI_ENTRY_GROUP_ESTABLISHED :
193         /* The entry group has been established successfully */
194         LOG(log_debug, logtype_afpd, "publish_reply: AVAHI_ENTRY_GROUP_ESTABLISHED");
195         break;
196
197     case AVAHI_ENTRY_GROUP_COLLISION:
198         /* With multiple names there's no way to know which one collided */
199         LOG(log_error, logtype_afpd, "publish_reply: AVAHI_ENTRY_GROUP_COLLISION",
200             avahi_strerror(avahi_client_errno(ctx->client)));
201         avahi_threaded_poll_quit(ctx->threaded_poll);
202         break;
203                 
204     case AVAHI_ENTRY_GROUP_FAILURE:
205         LOG(log_error, logtype_afpd, "Failed to register service: %s",
206             avahi_strerror(avahi_client_errno(ctx->client)));
207         avahi_threaded_poll_quit(ctx->threaded_poll);
208         break;
209
210     case AVAHI_ENTRY_GROUP_UNCOMMITED:
211         break;
212     case AVAHI_ENTRY_GROUP_REGISTERING:
213         break;
214     }
215 }
216
217 static void client_callback(AvahiClient *client,
218                             AvahiClientState state,
219                             void *userdata)
220 {
221     ctx->client = client;
222
223     switch (state) {
224     case AVAHI_CLIENT_S_RUNNING:
225         /* The server has startup successfully and registered its host
226          * name on the network, so it's time to create our services */
227         if (!ctx->group)
228             register_stuff();
229         break;
230
231     case AVAHI_CLIENT_S_COLLISION:
232         if (ctx->group)
233             avahi_entry_group_reset(ctx->group);
234         break;
235
236     case AVAHI_CLIENT_FAILURE: {
237         if (avahi_client_errno(client) == AVAHI_ERR_DISCONNECTED) {
238             int error;
239
240             avahi_client_free(ctx->client);
241             ctx->client = NULL;
242             ctx->group = NULL;
243
244             /* Reconnect to the server */
245             if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
246                                                  AVAHI_CLIENT_NO_FAIL,
247                                                  client_callback,
248                                                  ctx,
249                                                  &error))) {
250
251                 LOG(log_error, logtype_afpd, "Failed to contact server: %s",
252                     avahi_strerror(error));
253
254                 avahi_threaded_poll_quit(ctx->threaded_poll);
255             }
256
257         } else {
258             LOG(log_error, logtype_afpd, "Client failure: %s",
259                 avahi_strerror(avahi_client_errno(client)));
260             avahi_threaded_poll_quit(ctx->threaded_poll);
261         }
262         break;
263     }
264
265     case AVAHI_CLIENT_S_REGISTERING:
266         break;
267     case AVAHI_CLIENT_CONNECTING:
268         break;
269     }
270 }
271
272 /************************************************************************
273  * Public funcions
274  ************************************************************************/
275
276 /*
277  * Tries to setup the Zeroconf thread and any
278  * neccessary config setting.
279  */
280 void av_zeroconf_register(const AFPObj *obj) {
281     int error;
282
283     /* initialize the struct that holds our config settings. */
284     if (ctx) {
285         LOG(log_debug, logtype_afpd, "Resetting zeroconf records");
286         avahi_entry_group_reset(ctx->group);
287     } else {
288         ctx = calloc(1, sizeof(struct context));
289         ctx->obj = obj;
290         assert(ctx);
291     }
292
293 /* first of all we need to initialize our threading env */
294     if (!(ctx->threaded_poll = avahi_threaded_poll_new())) {
295         goto fail;
296     }
297
298 /* now we need to acquire a client */
299     if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
300                                          AVAHI_CLIENT_NO_FAIL,
301                                          client_callback,
302                                          NULL,
303                                          &error))) {
304         LOG(log_error, logtype_afpd, "Failed to create client object: %s",
305             avahi_strerror(error));
306         goto fail;
307     }
308
309     if (avahi_threaded_poll_start(ctx->threaded_poll) < 0) {
310         LOG(log_error, logtype_afpd, "Failed to create thread: %s",
311             avahi_strerror(avahi_client_errno(ctx->client)));
312         goto fail;
313     } else {
314         LOG(log_info, logtype_afpd, "Successfully started avahi loop.");
315     }
316
317     ctx->thread_running = 1;
318     return;
319
320 fail:
321     av_zeroconf_unregister();
322
323     return;
324 }
325
326 /*
327  * Tries to shutdown this loop impl.
328  * Call this function from inside this thread.
329  */
330 int av_zeroconf_unregister() {
331     LOG(log_error, logtype_afpd, "av_zeroconf_unregister");
332
333     if (ctx) {
334         LOG(log_error, logtype_afpd, "av_zeroconf_unregister: avahi_threaded_poll_stop");
335         if (ctx->threaded_poll)
336             avahi_threaded_poll_stop(ctx->threaded_poll);
337         LOG(log_error, logtype_afpd, "av_zeroconf_unregister: avahi_client_free");
338         if (ctx->client)
339             avahi_client_free(ctx->client);
340         LOG(log_error, logtype_afpd, "av_zeroconf_unregister: avahi_threaded_poll_free");
341         if (ctx->threaded_poll)
342             avahi_threaded_poll_free(ctx->threaded_poll);
343         free(ctx);
344         ctx = NULL;
345     }
346     return 0;
347 }
348
349 #endif /* USE_AVAHI */
350