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
5 #include "domain-util.h"
10 + AvahiServer *server;
11 + AvahiSEntryGroup *group;
15 + AvahiPublishFlags flags;
16 + AvahiIfIndex interface;
17 + AvahiProtocol protocol;
26 + DNSServiceRef client;
30 + AVAHI_LLIST_FIELDS(AvahiService, services);
34 static void transport_flags_from_domain(AvahiServer *s, AvahiPublishFlags *flags, const char *domain) {
38 *flags |= AVAHI_PUBLISH_USE_WIDE_AREA;
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);
53 + case kDNSServiceErr_NameConflict:
54 + avahi_s_entry_group_change_state(as->group, AVAHI_ENTRY_GROUP_COLLISION);
57 + avahi_s_entry_group_change_state(as->group, AVAHI_ENTRY_GROUP_FAILURE);
61 +static void register_service_socket_event(AvahiWatch *w, int fd, AvahiWatchEvent events, void *userdata) {
62 + AvahiService *as = userdata;
63 + DNSServiceErrorType ret;
67 + assert(events & AVAHI_WATCH_IN);
69 + assert (fd == DNSServiceRefSockFD(as->client));
70 + ret = DNSServiceProcessResult(as->client);
71 + if (ret != kDNSServiceErr_NoError) {
73 + as->server->poll_api->watch_free(as->watch);
76 + DNSServiceRefDeallocate(as->client);
78 + avahi_s_entry_group_change_state(as->group, AVAHI_ENTRY_GROUP_FAILURE);
82 +static void avahi_service_free(AvahiServer*s, AvahiService *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);
96 + avahi_free(as->name);
99 + avahi_free(as->type);
102 + avahi_free(as->domain);
105 + avahi_free(as->host);
108 + s->poll_api->watch_free(as->watch);
111 + DNSServiceRefDeallocate (as->client);
114 + avahi_free(as->txtrecord);
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 :
134 + register_service_reply,
136 + if (ret == kDNSServiceErr_NoError) {
138 + avahi_s_entry_group_change_state(as->group, AVAHI_ENTRY_GROUP_FAILURE);
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);
144 + if (ret == kDNSServiceErr_NameConflict) {
145 + avahi_s_entry_group_change_state(as->group, AVAHI_ENTRY_GROUP_COLLISION);
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:
160 + case kDNSServiceErr_NameConflict:
161 + e->recordref = NULL;
162 + avahi_server_set_errno(e->server, AVAHI_ERR_COLLISION);
165 + e->recordref = NULL;
166 + avahi_server_set_errno(e->server, AVAHI_ERR_FAILURE);
172 void avahi_entry_free(AvahiServer*s, AvahiEntry *e) {
178 +#ifndef HAVE_BONJOUR
179 avahi_goodbye_entry(s, e, 1, 1);
182 /* Remove from linked list */
183 AVAHI_LLIST_REMOVE(AvahiEntry, entries, s->entries, e);
187 avahi_entry_free(s, g->entries);
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;
198 if (g->register_time_event)
199 avahi_time_event_free(g->register_time_event);
201 s->need_entry_cleanup = 0;
205 + if (s->need_service_cleanup) {
206 + AvahiService *as, *next;
208 + for (as = s->services; as; as = next) {
209 + next = as->services_next;
212 + avahi_service_free(s, as);
215 + s->need_service_cleanup = 0;
219 if (s->need_browser_cleanup)
220 avahi_browser_cleanup(s);
224 /* Hmm, nothing found? */
228 + * Assume that we are updating a service's primary TXT record
229 + * so find the service
231 + DNSServiceErrorType ret;
233 + uint8_t rdata[AVAHI_DNS_RDATA_MAX];
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);
246 + if (!strcmp(svc_name, r->key->name)) {
253 + avahi_server_set_errno(s, AVAHI_ERR_NOT_FOUND);
256 + if ((l = avahi_rdata_serialize(r, rdata, sizeof(rdata))) == (size_t) -1) {
257 + avahi_server_set_errno(s, AVAHI_ERR_FAILURE);
260 + ret = DNSServiceUpdateRecord(as->client,
266 + if (ret != kDNSServiceErr_NoError) {
267 + avahi_server_set_errno(s, AVAHI_ERR_FAILURE);
271 avahi_server_set_errno(s, AVAHI_ERR_NOT_FOUND);
276 /* Update the entry */
279 /* Announce our changes when needed */
280 if (!avahi_record_equal_no_ttl(old_record, r) && (!g || g->state != AVAHI_ENTRY_GROUP_UNCOMMITED)) {
282 + DNSServiceErrorType ret;
284 + uint8_t rdata[AVAHI_DNS_RDATA_MAX];
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);
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);
299 + ret = DNSServiceUpdateRecord(g->record_connection,
305 + if (ret != kDNSServiceErr_NoError) {
306 + avahi_entry_free(s, e);
307 + avahi_server_set_errno(s, AVAHI_ERR_FAILURE);
312 /* Remove the old entry from all caches, if needed */
313 if (!(e->flags & AVAHI_PUBLISH_UNIQUE))
316 /* Reannounce our updated entry */
317 avahi_reannounce_entry(s, e);
321 /* If we were the first entry in the list, we need to update the key */
327 + DNSServiceErrorType ret;
328 + DNSServiceFlags bflags;
330 + uint8_t rdata[AVAHI_DNS_RDATA_MAX];
335 /* Add a new record */
339 AVAHI_LLIST_PREPEND(AvahiEntry, by_group, g->entries, e);
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);
351 + if (flags & AVAHI_PUBLISH_ALLOW_MULTIPLE)
352 + bflags |= kDNSServiceFlagsShared;
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);
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);
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);
375 + ret = DNSServiceRegisterRecord(g->record_connection,
378 + interface == AVAHI_IF_UNSPEC ?
379 + kDNSServiceInterfaceIndexAny :
387 + register_record_reply,
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);
396 + avahi_entry_free(s, e);
397 + avahi_server_set_errno(s, AVAHI_ERR_FAILURE);
400 + avahi_free(record_name);
402 avahi_announce_entry(s, e);
408 AvahiRecord *r = NULL;
410 AvahiEntry *srv_entry = NULL, *txt_entry = NULL, *ptr_entry = NULL, *enum_entry = NULL;
419 domain = s->domain_name;
422 + as = avahi_new (AvahiService, 1);
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);
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) {
441 + ret = avahi_server_set_errno(s, AVAHI_ERR_NO_MEMORY);
444 + avahi_string_list_serialize(strlst, as->txtrecord, as->txtlen);
446 + as->txtrecord = NULL;
448 + AVAHI_LLIST_PREPEND(AvahiService, services, s->services, as);
449 + AVAHI_LLIST_PREPEND(AvahiService, services, g->services, as);
452 host = s->host_name_fqdn;
455 ret = avahi_server_errno(s);
461 if (ret != AVAHI_OK && !(flags & AVAHI_PUBLISH_UPDATE)) {
462 @@ -1013,7 +1378,11 @@
463 if (g->state == state)
467 + assert(state <= AVAHI_ENTRY_GROUP_FAILURE);
469 assert(state <= AVAHI_ENTRY_GROUP_COLLISION);
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);
479 + AVAHI_LLIST_HEAD_INIT(AvahiService, g->services);
480 + g->record_connection = NULL;
483 AVAHI_LLIST_PREPEND(AvahiSEntryGroup, groups, s->groups, g);
485 @@ -1087,16 +1460,26 @@
487 void avahi_s_entry_group_free(AvahiSEntryGroup *g) {
496 for (e = g->entries; e; e = e->by_group_next) {
498 +#ifndef HAVE_BONJOUR
499 avahi_goodbye_entry(g->server, e, 1, 1);
505 + for (s = g->services; s; s = s->services_next) {
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;
517 + g->server->need_service_cleanup = 1;
520 schedule_cleanup(g->server);
523 static void entry_group_commit_real(AvahiSEntryGroup *g) {
529 gettimeofday(&g->register_time, NULL);
530 @@ -1121,8 +1510,15 @@
536 + for (s = g->services; s; s = s->services_next)
538 + avahi_register_service(g->server, s);
540 avahi_announce_group(g->server, g);
541 avahi_s_entry_group_check_probed(g, 0);
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) {
555 for (e = g->entries; e; e = e->by_group_next) {
557 +#ifndef HAVE_BONJOUR
558 avahi_goodbye_entry(g->server, e, 1, 1);
563 g->server->need_entry_cleanup = 1;
566 + for (s = g->services; s; s = s->services_next) {
569 + g->server->need_service_cleanup = 1;
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) {
586 + for (s = g->services; s; s = s->services_next)
590 /* Look for an entry that is not dead */
591 for (e = g->entries; e; e = e->by_group_next)