4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <netinet/in.h>
36 #include <avahi-common/simple-watch.h>
37 #include <avahi-common/malloc.h>
38 #include <avahi-common/error.h>
39 #include <avahi-common/domain.h>
40 #include <avahi-common/alternative.h>
42 #include <avahi-client/client.h>
43 #include <avahi-client/publish.h>
44 #include <avahi-client/lookup.h>
50 COMMAND_COME_AGAIN = 0,
53 COMMAND_POLL_DONE = 'P',
54 COMMAND_POLL_FAILED = 'F'
57 struct _DNSServiceRef_t {
60 AvahiSimplePoll *simple_poll;
62 int thread_fd, main_fd;
67 pthread_mutex_t mutex;
70 DNSServiceBrowseReply service_browser_callback;
71 DNSServiceResolveReply service_resolver_callback;
72 DNSServiceDomainEnumReply domain_browser_callback;
73 DNSServiceRegisterReply service_register_callback;
76 AvahiServiceBrowser *service_browser;
77 AvahiServiceResolver *service_resolver;
78 AvahiDomainBrowser *domain_browser;
80 char *service_name, *service_name_chosen, *service_regtype, *service_domain, *service_host;
81 uint16_t service_port;
82 AvahiIfIndex service_interface;
83 AvahiStringList *service_txt;
85 AvahiEntryGroup *entry_group;
88 #define ASSERT_SUCCESS(r) { int __ret = (r); assert(__ret == 0); }
90 static DNSServiceErrorType map_error(int error) {
93 return kDNSServiceErr_NoError;
95 case AVAHI_ERR_BAD_STATE :
96 return kDNSServiceErr_BadState;
98 case AVAHI_ERR_INVALID_HOST_NAME:
99 case AVAHI_ERR_INVALID_DOMAIN_NAME:
100 case AVAHI_ERR_INVALID_TTL:
101 case AVAHI_ERR_IS_PATTERN:
102 case AVAHI_ERR_INVALID_RECORD:
103 case AVAHI_ERR_INVALID_SERVICE_NAME:
104 case AVAHI_ERR_INVALID_SERVICE_TYPE:
105 case AVAHI_ERR_INVALID_PORT:
106 case AVAHI_ERR_INVALID_KEY:
107 case AVAHI_ERR_INVALID_ADDRESS:
108 case AVAHI_ERR_INVALID_SERVICE_SUBTYPE:
109 return kDNSServiceErr_BadParam;
112 case AVAHI_ERR_LOCAL_COLLISION:
113 return kDNSServiceErr_NameConflict;
115 case AVAHI_ERR_TOO_MANY_CLIENTS:
116 case AVAHI_ERR_TOO_MANY_OBJECTS:
117 case AVAHI_ERR_TOO_MANY_ENTRIES:
118 case AVAHI_ERR_ACCESS_DENIED:
119 return kDNSServiceErr_Refused;
121 case AVAHI_ERR_INVALID_OPERATION:
122 case AVAHI_ERR_INVALID_OBJECT:
123 return kDNSServiceErr_Invalid;
125 case AVAHI_ERR_NO_MEMORY:
126 return kDNSServiceErr_NoMemory;
128 case AVAHI_ERR_INVALID_INTERFACE:
129 case AVAHI_ERR_INVALID_PROTOCOL:
130 return kDNSServiceErr_BadInterfaceIndex;
132 case AVAHI_ERR_INVALID_FLAGS:
133 return kDNSServiceErr_BadFlags;
135 case AVAHI_ERR_NOT_FOUND:
136 return kDNSServiceErr_NoSuchName;
138 case AVAHI_ERR_VERSION_MISMATCH:
139 return kDNSServiceErr_Incompatible;
141 case AVAHI_ERR_NO_NETWORK:
143 case AVAHI_ERR_INVALID_CONFIG:
144 case AVAHI_ERR_TIMEOUT:
145 case AVAHI_ERR_DBUS_ERROR:
146 case AVAHI_ERR_NOT_CONNECTED:
147 case AVAHI_ERR_NO_DAEMON:
152 return kDNSServiceErr_Unknown;
155 static const char *add_trailing_dot(const char *s, char *buf, size_t buf_len) {
162 if (s[strlen(s)-1] == '.')
165 snprintf(buf, buf_len, "%s.", s);
169 static int read_command(int fd) {
175 if ((r = read(fd, &command, 1)) != 1) {
178 return COMMAND_COME_AGAIN;
180 fprintf(stderr, __FILE__": read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
187 static int write_command(int fd, char reply) {
190 if (write(fd, &reply, 1) != 1) {
191 fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
198 static int set_nonblock(int fd) {
203 if ((n = fcntl(fd, F_GETFL)) < 0)
209 return fcntl(fd, F_SETFL, n|O_NONBLOCK);
213 static int poll_func(struct pollfd *ufds, unsigned int nfds, int timeout, void *userdata) {
214 DNSServiceRef sdref = userdata;
219 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
221 /* fprintf(stderr, "pre-syscall\n"); */
222 ret = poll(ufds, nfds, timeout);
223 /* fprintf(stderr, "post-syscall\n"); */
225 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
230 static void * thread_func(void *data) {
231 DNSServiceRef sdref = data;
235 pthread_sigmask(SIG_BLOCK, &mask, NULL);
237 sdref->thread = pthread_self();
238 sdref->thread_running = 1;
243 if ((command = read_command(sdref->thread_fd)) < 0)
246 /* fprintf(stderr, "Command: %c\n", command); */
253 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
258 if ((ret = avahi_simple_poll_run(sdref->simple_poll)) < 0) {
263 fprintf(stderr, __FILE__": avahi_simple_poll_run() failed: %s\n", strerror(errno));
269 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
271 if (write_command(sdref->thread_fd, ret < 0 ? COMMAND_POLL_FAILED : COMMAND_POLL_DONE) < 0)
280 case COMMAND_COME_AGAIN:
289 static DNSServiceRef sdref_new(void) {
290 int fd[2] = { -1, -1 };
291 DNSServiceRef sdref = NULL;
292 pthread_mutexattr_t mutex_attr;
294 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0)
297 if (!(sdref = avahi_new(struct _DNSServiceRef_t, 1)))
301 sdref->thread_fd = fd[0];
302 sdref->main_fd = fd[1];
304 set_nonblock(sdref->main_fd);
306 sdref->client = NULL;
307 sdref->service_browser = NULL;
308 sdref->service_resolver = NULL;
309 sdref->domain_browser = NULL;
310 sdref->entry_group = NULL;
312 sdref->service_name = sdref->service_name_chosen = sdref->service_regtype = sdref->service_domain = sdref->service_host = NULL;
313 sdref->service_txt = NULL;
315 ASSERT_SUCCESS(pthread_mutexattr_init(&mutex_attr));
316 pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
317 ASSERT_SUCCESS(pthread_mutex_init(&sdref->mutex, &mutex_attr));
319 sdref->thread_running = 0;
321 if (!(sdref->simple_poll = avahi_simple_poll_new()))
324 avahi_simple_poll_set_func(sdref->simple_poll, poll_func, sdref);
326 /* Start simple poll */
327 if (avahi_simple_poll_prepare(sdref->simple_poll, -1) < 0)
330 /* Queue an initial POLL command for the thread */
331 if (write_command(sdref->main_fd, COMMAND_POLL) < 0)
334 if (pthread_create(&sdref->thread, NULL, thread_func, sdref) != 0)
337 sdref->thread_running = 1;
344 DNSServiceRefDeallocate(sdref);
349 static void sdref_free(DNSServiceRef sdref) {
352 if (sdref->thread_running) {
353 ASSERT_SUCCESS(write_command(sdref->main_fd, COMMAND_QUIT));
354 avahi_simple_poll_wakeup(sdref->simple_poll);
355 ASSERT_SUCCESS(pthread_join(sdref->thread, NULL));
359 avahi_client_free(sdref->client);
361 if (sdref->simple_poll)
362 avahi_simple_poll_free(sdref->simple_poll);
365 if (sdref->thread_fd >= 0)
366 close(sdref->thread_fd);
368 if (sdref->main_fd >= 0)
369 close(sdref->main_fd);
371 ASSERT_SUCCESS(pthread_mutex_destroy(&sdref->mutex));
373 avahi_free(sdref->service_name);
374 avahi_free(sdref->service_name_chosen);
375 avahi_free(sdref->service_regtype);
376 avahi_free(sdref->service_domain);
377 avahi_free(sdref->service_host);
379 avahi_string_list_free(sdref->service_txt);
384 static void sdref_ref(DNSServiceRef sdref) {
386 assert(sdref->n_ref >= 1);
391 static void sdref_unref(DNSServiceRef sdref) {
393 assert(sdref->n_ref >= 1);
395 if (--(sdref->n_ref) <= 0)
399 int DNSSD_API DNSServiceRefSockFD(DNSServiceRef sdref) {
401 assert(sdref->n_ref >= 1);
405 return sdref->main_fd;
408 DNSServiceErrorType DNSSD_API DNSServiceProcessResult(DNSServiceRef sdref) {
409 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
413 assert(sdref->n_ref >= 1);
419 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
421 /* Cleanup notification socket */
422 if ((t = read_command(sdref->main_fd)) != COMMAND_POLL_DONE) {
423 if (t == COMMAND_COME_AGAIN)
424 ret = kDNSServiceErr_Unknown;
428 if (avahi_simple_poll_dispatch(sdref->simple_poll) < 0)
431 if (sdref->n_ref > 1) /* Perhaps we should die */
433 /* Dispatch events */
434 if (avahi_simple_poll_prepare(sdref->simple_poll, -1) < 0)
437 if (sdref->n_ref > 1)
439 /* Request the poll */
440 if (write_command(sdref->main_fd, COMMAND_POLL) < 0)
443 ret = kDNSServiceErr_NoError;
447 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
454 void DNSSD_API DNSServiceRefDeallocate(DNSServiceRef sdref) {
461 static void service_browser_callback(
462 AvahiServiceBrowser *b,
463 AvahiIfIndex interface,
464 AvahiProtocol protocol,
465 AvahiBrowserEvent event,
469 AvahiLookupResultFlags flags,
472 DNSServiceRef sdref = userdata;
473 char type_fixed[AVAHI_DOMAIN_NAME_MAX], domain_fixed[AVAHI_DOMAIN_NAME_MAX];
476 assert(sdref->n_ref >= 1);
478 type = add_trailing_dot(type, type_fixed, sizeof(type_fixed));
479 domain = add_trailing_dot(domain, domain_fixed, sizeof(domain_fixed));
482 case AVAHI_BROWSER_NEW:
483 sdref->service_browser_callback(sdref, kDNSServiceFlagsAdd, interface, kDNSServiceErr_NoError, name, type, domain, sdref->context);
486 case AVAHI_BROWSER_REMOVE:
487 sdref->service_browser_callback(sdref, 0, interface, kDNSServiceErr_NoError, name, type, domain, sdref->context);
490 case AVAHI_BROWSER_FAILURE:
491 sdref->service_browser_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), NULL, NULL, NULL, sdref->context);
494 case AVAHI_BROWSER_CACHE_EXHAUSTED:
495 case AVAHI_BROWSER_ALL_FOR_NOW:
500 static void generic_client_callback(AvahiClient *s, AvahiClientState state, void* userdata) {
501 DNSServiceRef sdref = userdata;
505 assert(sdref->n_ref >= 1);
508 case AVAHI_CLIENT_DISCONNECTED: {
510 if (sdref->service_browser_callback)
511 sdref->service_browser_callback(sdref, 0, 0, kDNSServiceErr_Unknown, NULL, NULL, NULL, sdref->context);
512 else if (sdref->service_resolver_callback)
513 sdref->service_resolver_callback(sdref, 0, 0, kDNSServiceErr_Unknown, NULL, NULL, 0, 0, NULL, sdref->context);
514 else if (sdref->domain_browser_callback)
515 sdref->domain_browser_callback(sdref, 0, 0, kDNSServiceErr_Unknown, NULL, sdref->context);
520 case AVAHI_CLIENT_S_RUNNING:
521 case AVAHI_CLIENT_S_COLLISION:
522 case AVAHI_CLIENT_S_INVALID:
523 case AVAHI_CLIENT_S_REGISTERING:
528 DNSServiceErrorType DNSSD_API DNSServiceBrowse(
529 DNSServiceRef *ret_sdref,
530 DNSServiceFlags flags,
534 DNSServiceBrowseReply callback,
537 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
539 DNSServiceRef sdref = NULL;
540 AvahiIfIndex ifindex;
549 if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0) {
550 AVAHI_WARN_UNSUPPORTED;
551 return kDNSServiceErr_Unsupported;
554 if (!(sdref = sdref_new()))
555 return kDNSServiceErr_Unknown;
557 sdref->context = context;
558 sdref->service_browser_callback = callback;
560 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
562 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), generic_client_callback, sdref, &error))) {
563 ret = map_error(error);
567 ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
569 if (!(sdref->service_browser = avahi_service_browser_new(sdref->client, ifindex, AVAHI_PROTO_UNSPEC, regtype, domain, 0, service_browser_callback, sdref))) {
570 ret = map_error(avahi_client_errno(sdref->client));
574 ret = kDNSServiceErr_NoError;
579 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
581 if (ret != kDNSServiceErr_NoError)
582 DNSServiceRefDeallocate(sdref);
587 static void service_resolver_callback(
588 AvahiServiceResolver *r,
589 AvahiIfIndex interface,
590 AvahiProtocol protocol,
591 AvahiResolverEvent event,
595 const char *host_name,
596 const AvahiAddress *a,
598 AvahiStringList *txt,
599 AvahiLookupResultFlags flags,
602 DNSServiceRef sdref = userdata;
606 assert(sdref->n_ref >= 1);
609 case AVAHI_RESOLVER_FOUND: {
611 char host_name_fixed[AVAHI_DOMAIN_NAME_MAX];
612 char full_name[AVAHI_DOMAIN_NAME_MAX];
617 host_name = add_trailing_dot(host_name, host_name_fixed, sizeof(host_name_fixed));
619 if ((p = avahi_new0(char, (l = avahi_string_list_serialize(txt, NULL, 0))+1)))
620 avahi_string_list_serialize(txt, p, l);
622 ret = avahi_service_name_join(full_name, sizeof(full_name), name, type, domain);
623 assert(ret == AVAHI_OK);
625 strcat(full_name, ".");
627 sdref->service_resolver_callback(sdref, 0, interface, kDNSServiceErr_NoError, full_name, host_name, htons(port), l, p, sdref->context);
633 case AVAHI_RESOLVER_FAILURE:
634 sdref->service_resolver_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), NULL, NULL, 0, 0, NULL, sdref->context);
639 DNSServiceErrorType DNSSD_API DNSServiceResolve(
640 DNSServiceRef *ret_sdref,
641 DNSServiceFlags flags,
646 DNSServiceResolveReply callback,
649 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
651 DNSServiceRef sdref = NULL;
652 AvahiIfIndex ifindex;
662 if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0) {
663 AVAHI_WARN_UNSUPPORTED;
664 return kDNSServiceErr_Unsupported;
667 if (!(sdref = sdref_new()))
668 return kDNSServiceErr_Unknown;
670 sdref->context = context;
671 sdref->service_resolver_callback = callback;
673 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
675 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), generic_client_callback, sdref, &error))) {
676 ret = map_error(error);
680 ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
682 if (!(sdref->service_resolver = avahi_service_resolver_new(sdref->client, ifindex, AVAHI_PROTO_UNSPEC, name, regtype, domain, AVAHI_PROTO_UNSPEC, 0, service_resolver_callback, sdref))) {
683 ret = map_error(avahi_client_errno(sdref->client));
688 ret = kDNSServiceErr_NoError;
693 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
695 if (ret != kDNSServiceErr_NoError)
696 DNSServiceRefDeallocate(sdref);
701 int DNSSD_API DNSServiceConstructFullName (
705 const char *domain) {
713 if (avahi_service_name_join(fullName, kDNSServiceMaxDomainName, service, regtype, domain) < 0)
719 static void domain_browser_callback(
720 AvahiDomainBrowser *b,
721 AvahiIfIndex interface,
722 AvahiProtocol protocol,
723 AvahiBrowserEvent event,
725 AvahiLookupResultFlags flags,
728 DNSServiceRef sdref = userdata;
729 static char domain_fixed[AVAHI_DOMAIN_NAME_MAX];
733 assert(sdref->n_ref >= 1);
735 domain = add_trailing_dot(domain, domain_fixed, sizeof(domain_fixed));
738 case AVAHI_BROWSER_NEW:
739 sdref->domain_browser_callback(sdref, kDNSServiceFlagsAdd, interface, kDNSServiceErr_NoError, domain, sdref->context);
742 case AVAHI_BROWSER_REMOVE:
743 sdref->domain_browser_callback(sdref, 0, interface, kDNSServiceErr_NoError, domain, sdref->context);
746 case AVAHI_BROWSER_FAILURE:
747 sdref->domain_browser_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), domain, sdref->context);
750 case AVAHI_BROWSER_CACHE_EXHAUSTED:
751 case AVAHI_BROWSER_ALL_FOR_NOW:
756 DNSServiceErrorType DNSSD_API DNSServiceEnumerateDomains(
757 DNSServiceRef *ret_sdref,
758 DNSServiceFlags flags,
760 DNSServiceDomainEnumReply callback,
763 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
765 DNSServiceRef sdref = NULL;
766 AvahiIfIndex ifindex;
773 if (interface == kDNSServiceInterfaceIndexLocalOnly ||
774 (flags != kDNSServiceFlagsBrowseDomains && flags != kDNSServiceFlagsRegistrationDomains)) {
775 AVAHI_WARN_UNSUPPORTED;
776 return kDNSServiceErr_Unsupported;
779 if (!(sdref = sdref_new()))
780 return kDNSServiceErr_Unknown;
782 sdref->context = context;
783 sdref->domain_browser_callback = callback;
785 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
787 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), generic_client_callback, sdref, &error))) {
788 ret = map_error(error);
792 ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
794 if (!(sdref->domain_browser = avahi_domain_browser_new(sdref->client, ifindex, AVAHI_PROTO_UNSPEC, "local",
795 flags == kDNSServiceFlagsRegistrationDomains ? AVAHI_DOMAIN_BROWSER_REGISTER : AVAHI_DOMAIN_BROWSER_BROWSE,
796 0, domain_browser_callback, sdref))) {
797 ret = map_error(avahi_client_errno(sdref->client));
801 ret = kDNSServiceErr_NoError;
806 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
808 if (ret != kDNSServiceErr_NoError)
809 DNSServiceRefDeallocate(sdref);
814 static void reg_report_error(DNSServiceRef sdref, DNSServiceErrorType error) {
815 char regtype_fixed[AVAHI_DOMAIN_NAME_MAX], domain_fixed[AVAHI_DOMAIN_NAME_MAX];
816 const char *regtype, *domain;
818 assert(sdref->n_ref >= 1);
820 assert(sdref->service_register_callback);
822 regtype = add_trailing_dot(sdref->service_regtype, regtype_fixed, sizeof(regtype_fixed));
823 domain = add_trailing_dot(sdref->service_domain, domain_fixed, sizeof(domain_fixed));
825 sdref->service_register_callback(
827 sdref->service_name_chosen ? sdref->service_name_chosen : sdref->service_name,
833 static int reg_create_service(DNSServiceRef sdref) {
835 const char *real_type;
838 assert(sdref->n_ref >= 1);
840 real_type = avahi_get_type_from_subtype(sdref->service_regtype);
842 if ((ret = avahi_entry_group_add_service_strlst(
844 sdref->service_interface,
847 sdref->service_name_chosen,
848 real_type ? real_type : sdref->service_regtype,
849 sdref->service_domain,
852 sdref->service_txt)) < 0)
857 /* Create a subtype entry */
859 if (avahi_entry_group_add_service_subtype(
861 sdref->service_interface,
864 sdref->service_name_chosen,
866 sdref->service_domain,
867 sdref->service_regtype) < 0)
872 if ((ret = avahi_entry_group_commit(sdref->entry_group)) < 0)
878 static void reg_client_callback(AvahiClient *s, AvahiClientState state, void* userdata) {
879 DNSServiceRef sdref = userdata;
883 assert(sdref->n_ref >= 1);
885 /* We've not been setup completely */
886 if (!sdref->entry_group)
890 case AVAHI_CLIENT_DISCONNECTED:
892 reg_report_error(sdref, kDNSServiceErr_NoError);
895 case AVAHI_CLIENT_S_RUNNING: {
898 if (!sdref->service_name) {
900 /* If the service name is taken from the host name, copy that */
902 avahi_free(sdref->service_name_chosen);
903 sdref->service_name_chosen = NULL;
905 if (!(n = avahi_client_get_host_name(sdref->client))) {
906 reg_report_error(sdref, map_error(avahi_client_errno(sdref->client)));
910 if (!(sdref->service_name_chosen = avahi_strdup(n))) {
911 reg_report_error(sdref, kDNSServiceErr_NoMemory);
916 /* Register the service */
918 if ((ret = reg_create_service(sdref)) < 0) {
919 reg_report_error(sdref, map_error(ret));
926 case AVAHI_CLIENT_S_COLLISION:
928 /* Remove our entry */
929 avahi_entry_group_reset(sdref->entry_group);
933 case AVAHI_CLIENT_S_INVALID:
934 case AVAHI_CLIENT_S_REGISTERING:
941 static void reg_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
942 DNSServiceRef sdref = userdata;
947 case AVAHI_ENTRY_GROUP_ESTABLISHED:
948 /* Inform the user */
949 reg_report_error(sdref, kDNSServiceErr_NoError);
953 case AVAHI_ENTRY_GROUP_COLLISION: {
957 /* Remove our entry */
958 avahi_entry_group_reset(sdref->entry_group);
960 assert(sdref->service_name_chosen);
962 /* Pick a new name */
963 if (!(n = avahi_alternative_service_name(sdref->service_name_chosen))) {
964 reg_report_error(sdref, kDNSServiceErr_NoMemory);
967 avahi_free(sdref->service_name_chosen);
968 sdref->service_name_chosen = n;
970 /* Register the service with that new name */
971 if ((ret = reg_create_service(sdref)) < 0) {
972 reg_report_error(sdref, map_error(ret));
979 case AVAHI_ENTRY_GROUP_REGISTERING:
980 case AVAHI_ENTRY_GROUP_UNCOMMITED:
986 DNSServiceErrorType DNSSD_API DNSServiceRegister (
987 DNSServiceRef *ret_sdref,
988 DNSServiceFlags flags,
996 const void *txtRecord,
997 DNSServiceRegisterReply callback,
1000 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
1002 DNSServiceRef sdref = NULL;
1010 if (interface == kDNSServiceInterfaceIndexLocalOnly || flags) {
1011 AVAHI_WARN_UNSUPPORTED;
1012 return kDNSServiceErr_Unsupported;
1015 if (!(sdref = sdref_new()))
1016 return kDNSServiceErr_Unknown;
1018 sdref->context = context;
1019 sdref->service_register_callback = callback;
1021 sdref->service_name = avahi_strdup(name);
1022 sdref->service_regtype = regtype ? avahi_normalize_name_strdup(regtype) : NULL;
1023 sdref->service_domain = domain ? avahi_normalize_name_strdup(domain) : NULL;
1024 sdref->service_host = host ? avahi_normalize_name_strdup(host) : NULL;
1025 sdref->service_interface = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
1026 sdref->service_port = ntohs(port);
1027 sdref->service_txt = txtRecord && txtLen > 0 ? avahi_string_list_parse(txtRecord, txtLen) : NULL;
1029 /* Some OOM checking would be cool here */
1031 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
1033 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), reg_client_callback, sdref, &error))) {
1034 ret = map_error(error);
1038 if (!sdref->service_domain) {
1041 if (!(d = avahi_client_get_domain_name(sdref->client))) {
1042 ret = map_error(avahi_client_errno(sdref->client));
1046 if (!(sdref->service_domain = avahi_strdup(d))) {
1047 ret = kDNSServiceErr_NoMemory;
1052 if (!(sdref->entry_group = avahi_entry_group_new(sdref->client, reg_entry_group_callback, sdref))) {
1053 ret = map_error(avahi_client_errno(sdref->client));
1057 if (avahi_client_get_state(sdref->client) == AVAHI_CLIENT_S_RUNNING) {
1060 if (sdref->service_name)
1061 n = sdref->service_name;
1063 if (!(n = avahi_client_get_host_name(sdref->client))) {
1064 ret = map_error(avahi_client_errno(sdref->client));
1069 if (!(sdref->service_name_chosen = avahi_strdup(n))) {
1070 ret = kDNSServiceErr_NoMemory;
1075 if ((error = reg_create_service(sdref)) < 0) {
1076 ret = map_error(error);
1081 ret = kDNSServiceErr_NoError;
1086 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
1088 if (ret != kDNSServiceErr_NoError)
1089 DNSServiceRefDeallocate(sdref);