rust/cargo-c: update to 0.10.7+cargo-0.84.0
[oi-userland.git] / components / network / avahi / patches / 03-entry.patch
blob0ec59f7301ee0db5cd6a44371ab0f295c28a6deb
1 --- /usr/tmp/clean/avahi-0.6.28/avahi-core/entry.c 2010-08-26 01:51:38.985153000 +0100
2 +++ avahi-0.6.28/avahi-core/entry.c 2011-01-20 12:01:24.322248863 +0000
3 @@ -50,6 +50,33 @@
4 #include "rr-util.h"
5 #include "domain-util.h"
7 +#ifdef HAVE_BONJOUR
9 +struct AvahiService {
10 + AvahiServer *server;
11 + AvahiSEntryGroup *group;
13 + int dead;
15 + AvahiPublishFlags flags;
16 + AvahiIfIndex interface;
17 + AvahiProtocol protocol;
19 + char *name;
20 + char *type;
21 + char *domain;
22 + char *host;
23 + uint16_t port;
25 + AvahiWatch *watch;
26 + DNSServiceRef client;
27 + size_t txtlen;
28 + uint8_t *txtrecord;
30 + AVAHI_LLIST_FIELDS(AvahiService, services);
31 +};
32 +#endif
34 static void transport_flags_from_domain(AvahiServer *s, AvahiPublishFlags *flags, const char *domain) {
35 assert(flags);
36 assert(domain);
37 @@ -69,13 +96,146 @@
38 *flags |= AVAHI_PUBLISH_USE_WIDE_AREA;
41 +#ifdef HAVE_BONJOUR
42 +static void register_service_reply(DNSServiceRef client, const DNSServiceFlags flags, DNSServiceErrorType errorCode,
43 + const char *name, const char *regtype, const char *domain, void *context) {
44 + AvahiService *as = context;
46 + switch (errorCode) {
47 + case kDNSServiceErr_NoError:
48 + as->group->n_probing--;
49 + if (as->group->n_probing == 0) {
50 + avahi_s_entry_group_change_state(as->group, AVAHI_ENTRY_GROUP_ESTABLISHED);
51 + }
52 + break;
53 + case kDNSServiceErr_NameConflict:
54 + avahi_s_entry_group_change_state(as->group, AVAHI_ENTRY_GROUP_COLLISION);
55 + break;
56 + default:
57 + avahi_s_entry_group_change_state(as->group, AVAHI_ENTRY_GROUP_FAILURE);
58 + }
61 +static void register_service_socket_event(AvahiWatch *w, int fd, AvahiWatchEvent events, void *userdata) {
62 + AvahiService *as = userdata;
63 + DNSServiceErrorType ret;
65 + assert(w);
66 + assert(fd >= 0);
67 + assert(events & AVAHI_WATCH_IN);
69 + assert (fd == DNSServiceRefSockFD(as->client));
70 + ret = DNSServiceProcessResult(as->client);
71 + if (ret != kDNSServiceErr_NoError) {
72 + if (as->watch) {
73 + as->server->poll_api->watch_free(as->watch);
74 + as->watch = NULL;
75 + }
76 + DNSServiceRefDeallocate(as->client);
77 + as->client = NULL;
78 + avahi_s_entry_group_change_state(as->group, AVAHI_ENTRY_GROUP_FAILURE);
79 + }
82 +static void avahi_service_free(AvahiServer*s, AvahiService *as) {
83 + AvahiService *t;
85 + assert(s);
86 + assert(as);
88 + /* Remove from linked list */
89 + AVAHI_LLIST_REMOVE(AvahiService, services, s->services, as);
91 + /* Remove from associated group */
92 + if (as->group && (as->group->services != NULL))
93 + AVAHI_LLIST_REMOVE(AvahiService, services, as->group->services, as);
95 + if (as->name)
96 + avahi_free(as->name);
98 + if (as->type)
99 + avahi_free(as->type);
101 + if (as->domain)
102 + avahi_free(as->domain);
104 + if (as->host)
105 + avahi_free(as->host);
107 + if (as->watch)
108 + s->poll_api->watch_free(as->watch);
110 + if (as->client)
111 + DNSServiceRefDeallocate (as->client);
113 + if (as->txtrecord)
114 + avahi_free(as->txtrecord);
116 + avahi_free(as);
119 +static void avahi_register_service(AvahiServer *s, AvahiService *as) {
120 + DNSServiceErrorType ret;
122 + ret = DNSServiceRegister(&as->client,
123 + as->interface == AVAHI_IF_UNSPEC ?
124 + kDNSServiceInterfaceIndexAny :
125 + as->interface,
126 + 0,
127 + as->name,
128 + as->type,
129 + as->domain,
130 + as->host,
131 + htons(as->port),
132 + as->txtlen,
133 + as->txtrecord,
134 + register_service_reply,
135 + as);
136 + if (ret == kDNSServiceErr_NoError) {
137 + if (!as->client) {
138 + avahi_s_entry_group_change_state(as->group, AVAHI_ENTRY_GROUP_FAILURE);
139 + } else {
140 + as->group->n_probing++;
141 + as->watch = s->poll_api->watch_new(s->poll_api, DNSServiceRefSockFD(as->client), AVAHI_WATCH_IN, register_service_socket_event, as);
142 + }
143 + } else {
144 + if (ret == kDNSServiceErr_NameConflict) {
145 + avahi_s_entry_group_change_state(as->group, AVAHI_ENTRY_GROUP_COLLISION);
147 + else {
148 + avahi_s_entry_group_change_state(as->group, AVAHI_ENTRY_GROUP_FAILURE);
153 +static void register_record_reply(DNSServiceRef client, DNSRecordRef recordref, const DNSServiceFlags flags, DNSServiceErrorType errorCode, void *context) {
154 + AvahiEntry *e = context;
155 + DNSServiceErrorType ret;
157 + switch (errorCode) {
158 + case kDNSServiceErr_NoError:
159 + break;
160 + case kDNSServiceErr_NameConflict:
161 + e->recordref = NULL;
162 + avahi_server_set_errno(e->server, AVAHI_ERR_COLLISION);
163 + break;
164 + default:
165 + e->recordref = NULL;
166 + avahi_server_set_errno(e->server, AVAHI_ERR_FAILURE);
167 + break;
170 +#endif
172 void avahi_entry_free(AvahiServer*s, AvahiEntry *e) {
173 AvahiEntry *t;
175 assert(s);
176 assert(e);
178 +#ifndef HAVE_BONJOUR
179 avahi_goodbye_entry(s, e, 1, 1);
180 +#endif
182 /* Remove from linked list */
183 AVAHI_LLIST_REMOVE(AvahiEntry, entries, s->entries, e);
184 @@ -102,6 +262,15 @@
186 while (g->entries)
187 avahi_entry_free(s, g->entries);
188 +#ifdef HAVE_BONJOUR
189 + while (g->services)
190 + avahi_service_free(s, g->services);
192 + if (g->record_connection) {
193 + DNSServiceRefDeallocate(g->record_connection);
194 + g->record_connection = NULL;
196 +#endif
198 if (g->register_time_event)
199 avahi_time_event_free(g->register_time_event);
200 @@ -139,6 +308,21 @@
201 s->need_entry_cleanup = 0;
204 +#ifdef HAVE_BONJOUR
205 + if (s->need_service_cleanup) {
206 + AvahiService *as, *next;
208 + for (as = s->services; as; as = next) {
209 + next = as->services_next;
211 + if (as->dead)
212 + avahi_service_free(s, as);
215 + s->need_service_cleanup = 0;
217 +#endif
219 if (s->need_browser_cleanup)
220 avahi_browser_cleanup(s);
222 @@ -245,8 +429,54 @@
224 /* Hmm, nothing found? */
225 if (!e) {
226 +#ifdef HAVE_BONJOUR
227 + /*
228 + * Assume that we are updating a service's primary TXT record
229 + * so find the service
230 + */
231 + DNSServiceErrorType ret;
232 + uint16_t rlen;
233 + uint8_t rdata[AVAHI_DNS_RDATA_MAX];
234 + size_t l;
235 + AvahiService *as;
236 + int found_as = 0;
238 + for (as = g->services; as; as = as->services_next) {
239 + int a_ret = AVAHI_OK;
240 + char svc_name[AVAHI_DOMAIN_NAME_MAX];
242 + if ((a_ret = avahi_service_name_join(svc_name, sizeof(svc_name), as->name, as->type, as->domain ? as->domain : s->domain_name)) < 0) {
243 + avahi_server_set_errno(s, a_ret);
244 + return NULL;
246 + if (!strcmp(svc_name, r->key->name)) {
247 + found_as = 1;
248 + break;
252 + if (!found_as) {
253 + avahi_server_set_errno(s, AVAHI_ERR_NOT_FOUND);
254 + return NULL;
256 + if ((l = avahi_rdata_serialize(r, rdata, sizeof(rdata))) == (size_t) -1) {
257 + avahi_server_set_errno(s, AVAHI_ERR_FAILURE);
258 + return NULL;
260 + ret = DNSServiceUpdateRecord(as->client,
261 + NULL,
262 + 0,
263 + l,
264 + rdata,
265 + r->ttl);
266 + if (ret != kDNSServiceErr_NoError) {
267 + avahi_server_set_errno(s, AVAHI_ERR_FAILURE);
269 + return NULL;
270 +#else
271 avahi_server_set_errno(s, AVAHI_ERR_NOT_FOUND);
272 return NULL;
273 +#endif
276 /* Update the entry */
277 @@ -256,6 +486,36 @@
279 /* Announce our changes when needed */
280 if (!avahi_record_equal_no_ttl(old_record, r) && (!g || g->state != AVAHI_ENTRY_GROUP_UNCOMMITED)) {
281 +#ifdef HAVE_BONJOUR
282 + DNSServiceErrorType ret;
283 + uint16_t rlen;
284 + uint8_t rdata[AVAHI_DNS_RDATA_MAX];
285 + size_t l;
287 + if (!g->record_connection) {
288 + if (DNSServiceCreateConnection(&g->record_connection) != kDNSServiceErr_NoError) {
289 + avahi_entry_free(s, e);
290 + avahi_server_set_errno(s, AVAHI_ERR_FAILURE);
291 + return NULL;
294 + if ((l = avahi_rdata_serialize(r, rdata, sizeof(rdata))) == (size_t) -1) {
295 + avahi_entry_free(s, e);
296 + avahi_server_set_errno(s, AVAHI_ERR_FAILURE);
297 + return NULL;
299 + ret = DNSServiceUpdateRecord(g->record_connection,
300 + e->recordref,
301 + 0,
302 + l,
303 + rdata,
304 + r->ttl);
305 + if (ret != kDNSServiceErr_NoError) {
306 + avahi_entry_free(s, e);
307 + avahi_server_set_errno(s, AVAHI_ERR_FAILURE);
308 + return NULL;
310 +#else
312 /* Remove the old entry from all caches, if needed */
313 if (!(e->flags & AVAHI_PUBLISH_UNIQUE))
314 @@ -263,6 +523,7 @@
316 /* Reannounce our updated entry */
317 avahi_reannounce_entry(s, e);
318 +#endif
321 /* If we were the first entry in the list, we need to update the key */
322 @@ -273,6 +534,14 @@
324 } else {
325 AvahiEntry *t;
326 +#ifdef HAVE_BONJOUR
327 + DNSServiceErrorType ret;
328 + DNSServiceFlags bflags;
329 + uint16_t rlen;
330 + uint8_t rdata[AVAHI_DNS_RDATA_MAX];
331 + size_t l;
332 + char *record_name;
333 +#endif
335 /* Add a new record */
337 @@ -307,7 +576,69 @@
338 if (g)
339 AVAHI_LLIST_PREPEND(AvahiEntry, by_group, g->entries, e);
341 +#ifdef HAVE_BONJOUR
342 + e->recordref = NULL;
343 + if (!g->record_connection) {
344 + if (DNSServiceCreateConnection(&g->record_connection) != kDNSServiceErr_NoError) {
345 + avahi_entry_free(s, e);
346 + avahi_server_set_errno(s, AVAHI_ERR_FAILURE);
347 + return NULL;
350 + bflags = 0;
351 + if (flags & AVAHI_PUBLISH_ALLOW_MULTIPLE)
352 + bflags |= kDNSServiceFlagsShared;
353 + else
354 + bflags |= kDNSServiceFlagsUnique;
356 + switch (r->key->type) {
357 + case AVAHI_DNS_TYPE_A:
358 + case AVAHI_DNS_TYPE_AAAA:
359 + record_name = avahi_strdup(r->key->name);
360 + break;
361 + default:
362 + record_name = avahi_malloc(strlen(r->key->name) + strlen(s->host_name_fqdn) + 2);
363 + strcpy(record_name, r->key->name);
364 + strcat(record_name, ".");
365 + strcat(record_name, s->host_name_fqdn);
366 + break;
369 + if ((l = avahi_rdata_serialize(r, rdata, sizeof(rdata))) == (size_t) -1) {
370 + avahi_entry_free(s, e);
371 + avahi_server_set_errno(s, AVAHI_ERR_FAILURE);
372 + return NULL;
375 + ret = DNSServiceRegisterRecord(g->record_connection,
376 + &e->recordref,
377 + bflags,
378 + interface == AVAHI_IF_UNSPEC ?
379 + kDNSServiceInterfaceIndexAny :
380 + interface,
381 + record_name,
382 + r->key->type,
383 + r->key->clazz,
384 + l,
385 + rdata,
386 + r->ttl,
387 + register_record_reply,
388 + e);
389 + if (ret == kDNSServiceErr_NoError) {
390 + ret = DNSServiceProcessResult(g->record_connection);
391 + if (ret != kDNSServiceErr_NoError || e->recordref == NULL) {
392 + avahi_entry_free(s, e);
393 + return NULL;
395 + } else {
396 + avahi_entry_free(s, e);
397 + avahi_server_set_errno(s, AVAHI_ERR_FAILURE);
398 + return NULL;
400 + avahi_free(record_name);
401 +#else
402 avahi_announce_entry(s, e);
403 +#endif
406 return e;
407 @@ -583,6 +914,9 @@
408 AvahiRecord *r = NULL;
409 int ret = AVAHI_OK;
410 AvahiEntry *srv_entry = NULL, *txt_entry = NULL, *ptr_entry = NULL, *enum_entry = NULL;
411 +#ifdef HAVE_BONJOUR
412 + AvahiService *as;
413 +#endif
415 assert(s);
416 assert(type);
417 @@ -603,6 +937,36 @@
418 if (!domain)
419 domain = s->domain_name;
421 +#ifdef HAVE_BONJOUR
422 + as = avahi_new (AvahiService, 1);
423 + as->server = s;
424 + as->group = g;
425 + as->dead = 0;
426 + as->flags = flags;
427 + as->interface = interface;
428 + as->protocol = protocol;
429 + as->name = avahi_strdup(name);
430 + as->type = avahi_strdup(type);
431 + as->domain = avahi_strdup(domain);
432 + as->host = avahi_strdup(host);
433 + as->port = port;
434 + as->watch = NULL;
435 + as->client = NULL;
436 + as->txtlen = avahi_string_list_serialize(strlst, NULL, 0);
437 + if (as->txtlen > 0) {
438 + as->txtrecord = avahi_new(uint8_t, as->txtlen);
439 + if (as->txtrecord == NULL) {
440 + as->txtlen = 0;
441 + ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
442 + goto fail;
444 + avahi_string_list_serialize(strlst, as->txtrecord, as->txtlen);
445 + } else
446 + as->txtrecord = NULL;
448 + AVAHI_LLIST_PREPEND(AvahiService, services, s->services, as);
449 + AVAHI_LLIST_PREPEND(AvahiService, services, g->services, as);
450 +#else
451 if (!host)
452 host = s->host_name_fqdn;
454 @@ -667,6 +1031,7 @@
455 ret = avahi_server_errno(s);
456 goto fail;
458 +#endif
460 fail:
461 if (ret != AVAHI_OK && !(flags & AVAHI_PUBLISH_UPDATE)) {
462 @@ -1013,7 +1378,11 @@
463 if (g->state == state)
464 return;
466 +#ifdef HAVE_BONJOUR
467 + assert(state <= AVAHI_ENTRY_GROUP_FAILURE);
468 +#else
469 assert(state <= AVAHI_ENTRY_GROUP_COLLISION);
470 +#endif
472 if (g->state == AVAHI_ENTRY_GROUP_ESTABLISHED) {
474 @@ -1063,6 +1432,10 @@
475 g->register_time.tv_sec = 0;
476 g->register_time.tv_usec = 0;
477 AVAHI_LLIST_HEAD_INIT(AvahiEntry, g->entries);
478 +#ifdef HAVE_BONJOUR
479 + AVAHI_LLIST_HEAD_INIT(AvahiService, g->services);
480 + g->record_connection = NULL;
481 +#endif
483 AVAHI_LLIST_PREPEND(AvahiSEntryGroup, groups, s->groups, g);
484 return g;
485 @@ -1087,16 +1460,26 @@
487 void avahi_s_entry_group_free(AvahiSEntryGroup *g) {
488 AvahiEntry *e;
489 +#ifdef HAVE_BONJOUR
490 + AvahiService *s;
491 +#endif
493 assert(g);
494 assert(g->server);
496 for (e = g->entries; e; e = e->by_group_next) {
497 if (!e->dead) {
498 +#ifndef HAVE_BONJOUR
499 avahi_goodbye_entry(g->server, e, 1, 1);
500 +#endif
501 e->dead = 1;
504 +#ifdef HAVE_BONJOUR
505 + for (s = g->services; s; s = s->services_next) {
506 + s->dead = 1;
508 +#endif
510 if (g->register_time_event) {
511 avahi_time_event_free(g->register_time_event);
512 @@ -1107,11 +1490,17 @@
514 g->server->need_group_cleanup = 1;
515 g->server->need_entry_cleanup = 1;
516 +#ifdef HAVE_BONJOUR
517 + g->server->need_service_cleanup = 1;
518 +#endif
520 schedule_cleanup(g->server);
523 static void entry_group_commit_real(AvahiSEntryGroup *g) {
524 +#ifdef HAVE_BONJOUR
525 + AvahiService *s;
526 +#endif
527 assert(g);
529 gettimeofday(&g->register_time, NULL);
530 @@ -1121,8 +1510,15 @@
531 if (g->dead)
532 return;
534 +#ifdef HAVE_BONJOUR
535 + assert(g->server);
536 + for (s = g->services; s; s = s->services_next)
537 + if (!s->dead)
538 + avahi_register_service(g->server, s);
539 +#else
540 avahi_announce_group(g->server, g);
541 avahi_s_entry_group_check_probed(g, 0);
542 +#endif
545 static void entry_group_register_time_event_callback(AVAHI_GCC_UNUSED AvahiTimeEvent *e, void* userdata) {
546 @@ -1175,16 +1571,28 @@
548 void avahi_s_entry_group_reset(AvahiSEntryGroup *g) {
549 AvahiEntry *e;
550 +#ifdef HAVE_BONJOUR
551 + AvahiService *s;
552 +#endif
553 assert(g);
555 for (e = g->entries; e; e = e->by_group_next) {
556 if (!e->dead) {
557 +#ifndef HAVE_BONJOUR
558 avahi_goodbye_entry(g->server, e, 1, 1);
559 +#endif
560 e->dead = 1;
563 g->server->need_entry_cleanup = 1;
565 +#ifdef HAVE_BONJOUR
566 + for (s = g->services; s; s = s->services_next) {
567 + s->dead = 1;
569 + g->server->need_service_cleanup = 1;
570 +#endif
572 g->n_probing = 0;
574 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_UNCOMMITED);
575 @@ -1222,12 +1630,23 @@
577 int avahi_s_entry_group_is_empty(AvahiSEntryGroup *g) {
578 AvahiEntry *e;
579 +#ifdef HAVE_BONJOUR
580 + AvahiService *s;
581 +#endif
583 assert(g);
585 +#ifdef HAVE_BONJOUR
586 + for (s = g->services; s; s = s->services_next)
587 + if (!s->dead)
588 + return 0;
589 +#else
590 /* Look for an entry that is not dead */
591 for (e = g->entries; e; e = e->by_group_next)
592 if (!e->dead)
593 return 0;
594 +#endif
596 return 1;