]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_avahi.c
Fix build
[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
24 #include "afp_avahi.h"
25 #include "afp_config.h"
26 #include "volume.h"
27
28 /*****************************************************************
29  * Global variables
30  *****************************************************************/
31 struct context *ctx = NULL;
32
33 /*****************************************************************
34  * Private functions
35  *****************************************************************/
36
37 static void publish_reply(AvahiEntryGroup *g,
38                           AvahiEntryGroupState state,
39                           void *userdata);
40
41 /*
42  * This function tries to register the AFP DNS
43  * SRV service type.
44  */
45 static void register_stuff(void) {
46     uint port;
47     const AFPObj *obj;
48     const struct vol *volume;
49     DSI *dsi;
50     char name[MAXINSTANCENAMELEN+1];
51     AvahiStringList *strlist = NULL;
52     AvahiStringList *strlist2 = NULL;
53     char tmpname[256];
54
55     assert(ctx->client);
56
57     if (!ctx->group) {
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)));
61             goto fail;
62         }
63     }
64
65     if (avahi_entry_group_is_empty(ctx->group)) {
66         /* Register our service */
67
68         /* Build AFP volumes list */
69         int i = 0;
70         strlist = avahi_string_list_add_printf(strlist, "sys=waMa=0,adVF=0x100");
71                 
72         for (volume = getvolumes(); volume; volume = volume->v_next) {
73
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");
76                 goto fail;
77             }
78
79             if (volume->v_flags & AFPVOL_TM) {
80                 if (volume->v_uuid) {
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);
85                 } else {
86                     LOG(log_warning, logtype_afpd, "Registering volume '%s' for TimeMachine. But UUID is invalid.",
87                         volume->v_localname);
88                     strlist = avahi_string_list_add_printf(strlist, "dk%u=adVN=%s,adVF=0xa1",
89                                                            i++, tmpname);
90                 }       
91             }
92         }
93
94         /* AFP server */
95         for (dsi = obj->dsi; dsi; dsi = dsi->next) {
96             port = getip_port((struct sockaddr *)&dsi->server);
97
98             if (convert_string(obj->options.unixcharset,
99                                CH_UTF8,
100                                obj->options.hostname,
101                                -1,
102                                name,
103                                MAXINSTANCENAMELEN) <= 0) {
104                 LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name");
105                 goto fail;
106             }
107             if ((dsi->bonjourname = strdup(name)) == NULL) {
108                 LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name");
109                 goto fail;
110
111             }
112             LOG(log_info, logtype_afpd, "Registering server '%s' with Bonjour",
113                 dsi->bonjourname);
114
115             if (avahi_entry_group_add_service(ctx->group,
116                                               AVAHI_IF_UNSPEC,
117                                               AVAHI_PROTO_UNSPEC,
118                                               0,
119                                               dsi->bonjourname,
120                                               AFP_DNS_SERVICE_TYPE,
121                                               NULL,
122                                               NULL,
123                                               port,
124                                               NULL) < 0) {
125                 LOG(log_error, logtype_afpd, "Failed to add service: %s",
126                     avahi_strerror(avahi_client_errno(ctx->client)));
127                 goto fail;
128             }
129
130             if (i && avahi_entry_group_add_service_strlst(ctx->group,
131                                                           AVAHI_IF_UNSPEC,
132                                                           AVAHI_PROTO_UNSPEC,
133                                                           0,
134                                                           dsi->bonjourname,
135                                                           ADISK_SERVICE_TYPE,
136                                                           NULL,
137                                                           NULL,
138                                                           9, /* discard */
139                                                           strlist) < 0) {
140                 LOG(log_error, logtype_afpd, "Failed to add service: %s",
141                     avahi_strerror(avahi_client_errno(ctx->client)));
142                 goto fail;
143             }   /* if */
144
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,
148                                                          AVAHI_IF_UNSPEC,
149                                                          AVAHI_PROTO_UNSPEC,
150                                                          0,
151                                                          dsi->bonjourname,
152                                                          DEV_INFO_SERVICE_TYPE,
153                                                          NULL,
154                                                          NULL,
155                                                          0,
156                                                          strlist2) < 0) {
157                     LOG(log_error, logtype_afpd, "Failed to add service: %s",
158                         avahi_strerror(avahi_client_errno(ctx->client)));
159                     goto fail;
160                 }
161             } /* if (config->obj.options.mimicmodel) */
162
163         }       /* for config*/
164
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)));
168             goto fail;
169         }
170
171     }   /* if avahi_entry_group_is_empty*/
172
173     return;
174
175 fail:
176     time(NULL);
177 //    avahi_threaded_poll_quit(ctx->threaded_poll);
178 }
179
180 /* Called when publishing of service data completes */
181 static void publish_reply(AvahiEntryGroup *g,
182                           AvahiEntryGroupState state,
183                           AVAHI_GCC_UNUSED void *userdata)
184 {
185     assert(ctx->group == NULL || g == ctx->group);
186
187     switch (state) {
188
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");
192         break;
193
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);
199         break;
200                 
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);
205         break;
206
207     case AVAHI_ENTRY_GROUP_UNCOMMITED:
208         break;
209     case AVAHI_ENTRY_GROUP_REGISTERING:
210         break;
211     }
212 }
213
214 static void client_callback(AvahiClient *client,
215                             AvahiClientState state,
216                             void *userdata)
217 {
218     ctx->client = client;
219
220     switch (state) {
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 */
224         if (!ctx->group)
225             register_stuff();
226         break;
227
228     case AVAHI_CLIENT_S_COLLISION:
229         if (ctx->group)
230             avahi_entry_group_reset(ctx->group);
231         break;
232
233     case AVAHI_CLIENT_FAILURE: {
234         if (avahi_client_errno(client) == AVAHI_ERR_DISCONNECTED) {
235             int error;
236
237             avahi_client_free(ctx->client);
238             ctx->client = NULL;
239             ctx->group = NULL;
240
241             /* Reconnect to the server */
242             if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
243                                                  AVAHI_CLIENT_NO_FAIL,
244                                                  client_callback,
245                                                  ctx,
246                                                  &error))) {
247
248                 LOG(log_error, logtype_afpd, "Failed to contact server: %s",
249                     avahi_strerror(error));
250
251                 avahi_threaded_poll_quit(ctx->threaded_poll);
252             }
253
254         } else {
255             LOG(log_error, logtype_afpd, "Client failure: %s",
256                 avahi_strerror(avahi_client_errno(client)));
257             avahi_threaded_poll_quit(ctx->threaded_poll);
258         }
259         break;
260     }
261
262     case AVAHI_CLIENT_S_REGISTERING:
263         break;
264     case AVAHI_CLIENT_CONNECTING:
265         break;
266     }
267 }
268
269 /************************************************************************
270  * Public funcions
271  ************************************************************************/
272
273 /*
274  * Tries to setup the Zeroconf thread and any
275  * neccessary config setting.
276  */
277 void av_zeroconf_register(const AFPObj *obj) {
278     int error;
279
280     /* initialize the struct that holds our config settings. */
281     if (ctx) {
282         LOG(log_debug, logtype_afpd, "Resetting zeroconf records");
283         avahi_entry_group_reset(ctx->group);
284     } else {
285         ctx = calloc(1, sizeof(struct context));
286         ctx->obj = obj;
287         assert(ctx);
288     }
289
290 /* first of all we need to initialize our threading env */
291     if (!(ctx->threaded_poll = avahi_threaded_poll_new())) {
292         goto fail;
293     }
294
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,
298                                          client_callback,
299                                          NULL,
300                                          &error))) {
301         LOG(log_error, logtype_afpd, "Failed to create client object: %s",
302             avahi_strerror(error));
303         goto fail;
304     }
305
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)));
309         goto fail;
310     } else {
311         LOG(log_info, logtype_afpd, "Successfully started avahi loop.");
312     }
313
314     ctx->thread_running = 1;
315     return;
316
317 fail:
318     av_zeroconf_unregister();
319
320     return;
321 }
322
323 /*
324  * Tries to shutdown this loop impl.
325  * Call this function from inside this thread.
326  */
327 int av_zeroconf_unregister() {
328     LOG(log_error, logtype_afpd, "av_zeroconf_unregister");
329
330     if (ctx) {
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");
335         if (ctx->client)
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);
340         free(ctx);
341         ctx = NULL;
342     }
343     return 0;
344 }
345
346 #endif /* USE_AVAHI */
347