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) {
456 assert(sdref->n_ref >= 1);
463 static void service_browser_callback(
464 AvahiServiceBrowser *b,
465 AvahiIfIndex interface,
466 AvahiProtocol protocol,
467 AvahiBrowserEvent event,
471 AvahiLookupResultFlags flags,
474 DNSServiceRef sdref = userdata;
475 char type_fixed[AVAHI_DOMAIN_NAME_MAX], domain_fixed[AVAHI_DOMAIN_NAME_MAX];
478 assert(sdref->n_ref >= 1);
480 type = add_trailing_dot(type, type_fixed, sizeof(type_fixed));
481 domain = add_trailing_dot(domain, domain_fixed, sizeof(domain_fixed));
484 case AVAHI_BROWSER_NEW:
485 sdref->service_browser_callback(sdref, kDNSServiceFlagsAdd, interface, kDNSServiceErr_NoError, name, type, domain, sdref->context);
488 case AVAHI_BROWSER_REMOVE:
489 sdref->service_browser_callback(sdref, 0, interface, kDNSServiceErr_NoError, name, type, domain, sdref->context);
492 case AVAHI_BROWSER_FAILURE:
493 sdref->service_browser_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), NULL, NULL, NULL, sdref->context);
496 case AVAHI_BROWSER_CACHE_EXHAUSTED:
497 case AVAHI_BROWSER_ALL_FOR_NOW:
502 static void generic_client_callback(AvahiClient *s, AvahiClientState state, void* userdata) {
503 DNSServiceRef sdref = userdata;
507 assert(sdref->n_ref >= 1);
510 case AVAHI_CLIENT_DISCONNECTED: {
512 if (sdref->service_browser_callback)
513 sdref->service_browser_callback(sdref, 0, 0, kDNSServiceErr_Unknown, NULL, NULL, NULL, sdref->context);
514 else if (sdref->service_resolver_callback)
515 sdref->service_resolver_callback(sdref, 0, 0, kDNSServiceErr_Unknown, NULL, NULL, 0, 0, NULL, sdref->context);
516 else if (sdref->domain_browser_callback)
517 sdref->domain_browser_callback(sdref, 0, 0, kDNSServiceErr_Unknown, NULL, sdref->context);
522 case AVAHI_CLIENT_S_RUNNING:
523 case AVAHI_CLIENT_S_COLLISION:
524 case AVAHI_CLIENT_S_INVALID:
525 case AVAHI_CLIENT_S_REGISTERING:
530 DNSServiceErrorType DNSSD_API DNSServiceBrowse(
531 DNSServiceRef *ret_sdref,
532 DNSServiceFlags flags,
536 DNSServiceBrowseReply callback,
539 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
541 DNSServiceRef sdref = NULL;
542 AvahiIfIndex ifindex;
551 if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0) {
552 AVAHI_WARN_UNSUPPORTED;
553 return kDNSServiceErr_Unsupported;
556 if (!(sdref = sdref_new()))
557 return kDNSServiceErr_Unknown;
559 sdref->context = context;
560 sdref->service_browser_callback = callback;
562 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
564 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), generic_client_callback, sdref, &error))) {
565 ret = map_error(error);
569 ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
571 if (!(sdref->service_browser = avahi_service_browser_new(sdref->client, ifindex, AVAHI_PROTO_UNSPEC, regtype, domain, 0, service_browser_callback, sdref))) {
572 ret = map_error(avahi_client_errno(sdref->client));
576 ret = kDNSServiceErr_NoError;
581 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
583 if (ret != kDNSServiceErr_NoError)
584 DNSServiceRefDeallocate(sdref);
589 static void service_resolver_callback(
590 AvahiServiceResolver *r,
591 AvahiIfIndex interface,
592 AvahiProtocol protocol,
593 AvahiResolverEvent event,
597 const char *host_name,
598 const AvahiAddress *a,
600 AvahiStringList *txt,
601 AvahiLookupResultFlags flags,
604 DNSServiceRef sdref = userdata;
608 assert(sdref->n_ref >= 1);
611 case AVAHI_RESOLVER_FOUND: {
613 char host_name_fixed[AVAHI_DOMAIN_NAME_MAX];
614 char full_name[AVAHI_DOMAIN_NAME_MAX];
619 host_name = add_trailing_dot(host_name, host_name_fixed, sizeof(host_name_fixed));
621 if ((p = avahi_new0(char, (l = avahi_string_list_serialize(txt, NULL, 0))+1)))
622 avahi_string_list_serialize(txt, p, l);
624 ret = avahi_service_name_join(full_name, sizeof(full_name), name, type, domain);
625 assert(ret == AVAHI_OK);
627 strcat(full_name, ".");
629 sdref->service_resolver_callback(sdref, 0, interface, kDNSServiceErr_NoError, full_name, host_name, htons(port), l, p, sdref->context);
635 case AVAHI_RESOLVER_FAILURE:
636 sdref->service_resolver_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), NULL, NULL, 0, 0, NULL, sdref->context);
641 DNSServiceErrorType DNSSD_API DNSServiceResolve(
642 DNSServiceRef *ret_sdref,
643 DNSServiceFlags flags,
648 DNSServiceResolveReply callback,
651 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
653 DNSServiceRef sdref = NULL;
654 AvahiIfIndex ifindex;
664 if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0) {
665 AVAHI_WARN_UNSUPPORTED;
666 return kDNSServiceErr_Unsupported;
669 if (!(sdref = sdref_new()))
670 return kDNSServiceErr_Unknown;
672 sdref->context = context;
673 sdref->service_resolver_callback = callback;
675 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
677 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), generic_client_callback, sdref, &error))) {
678 ret = map_error(error);
682 ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
684 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))) {
685 ret = map_error(avahi_client_errno(sdref->client));
690 ret = kDNSServiceErr_NoError;
695 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
697 if (ret != kDNSServiceErr_NoError)
698 DNSServiceRefDeallocate(sdref);
703 int DNSSD_API DNSServiceConstructFullName (
707 const char *domain) {
715 if (avahi_service_name_join(fullName, kDNSServiceMaxDomainName, service, regtype, domain) < 0)
721 static void domain_browser_callback(
722 AvahiDomainBrowser *b,
723 AvahiIfIndex interface,
724 AvahiProtocol protocol,
725 AvahiBrowserEvent event,
727 AvahiLookupResultFlags flags,
730 DNSServiceRef sdref = userdata;
731 static char domain_fixed[AVAHI_DOMAIN_NAME_MAX];
735 assert(sdref->n_ref >= 1);
737 domain = add_trailing_dot(domain, domain_fixed, sizeof(domain_fixed));
740 case AVAHI_BROWSER_NEW:
741 sdref->domain_browser_callback(sdref, kDNSServiceFlagsAdd, interface, kDNSServiceErr_NoError, domain, sdref->context);
744 case AVAHI_BROWSER_REMOVE:
745 sdref->domain_browser_callback(sdref, 0, interface, kDNSServiceErr_NoError, domain, sdref->context);
748 case AVAHI_BROWSER_FAILURE:
749 sdref->domain_browser_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), domain, sdref->context);
752 case AVAHI_BROWSER_CACHE_EXHAUSTED:
753 case AVAHI_BROWSER_ALL_FOR_NOW:
758 DNSServiceErrorType DNSSD_API DNSServiceEnumerateDomains(
759 DNSServiceRef *ret_sdref,
760 DNSServiceFlags flags,
762 DNSServiceDomainEnumReply callback,
765 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
767 DNSServiceRef sdref = NULL;
768 AvahiIfIndex ifindex;
775 if (interface == kDNSServiceInterfaceIndexLocalOnly ||
776 (flags != kDNSServiceFlagsBrowseDomains && flags != kDNSServiceFlagsRegistrationDomains)) {
777 AVAHI_WARN_UNSUPPORTED;
778 return kDNSServiceErr_Unsupported;
781 if (!(sdref = sdref_new()))
782 return kDNSServiceErr_Unknown;
784 sdref->context = context;
785 sdref->domain_browser_callback = callback;
787 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
789 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), generic_client_callback, sdref, &error))) {
790 ret = map_error(error);
794 ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
796 if (!(sdref->domain_browser = avahi_domain_browser_new(sdref->client, ifindex, AVAHI_PROTO_UNSPEC, "local",
797 flags == kDNSServiceFlagsRegistrationDomains ? AVAHI_DOMAIN_BROWSER_REGISTER : AVAHI_DOMAIN_BROWSER_BROWSE,
798 0, domain_browser_callback, sdref))) {
799 ret = map_error(avahi_client_errno(sdref->client));
803 ret = kDNSServiceErr_NoError;
808 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
810 if (ret != kDNSServiceErr_NoError)
811 DNSServiceRefDeallocate(sdref);
816 static void reg_report_error(DNSServiceRef sdref, DNSServiceErrorType error) {
817 char regtype_fixed[AVAHI_DOMAIN_NAME_MAX], domain_fixed[AVAHI_DOMAIN_NAME_MAX];
818 const char *regtype, *domain;
820 assert(sdref->n_ref >= 1);
822 assert(sdref->service_register_callback);
824 regtype = add_trailing_dot(sdref->service_regtype, regtype_fixed, sizeof(regtype_fixed));
825 domain = add_trailing_dot(sdref->service_domain, domain_fixed, sizeof(domain_fixed));
827 sdref->service_register_callback(
829 sdref->service_name_chosen ? sdref->service_name_chosen : sdref->service_name,
835 static int reg_create_service(DNSServiceRef sdref) {
837 const char *real_type;
840 assert(sdref->n_ref >= 1);
842 real_type = avahi_get_type_from_subtype(sdref->service_regtype);
844 if ((ret = avahi_entry_group_add_service_strlst(
846 sdref->service_interface,
849 sdref->service_name_chosen,
850 real_type ? real_type : sdref->service_regtype,
851 sdref->service_domain,
854 sdref->service_txt)) < 0)
859 /* Create a subtype entry */
861 if (avahi_entry_group_add_service_subtype(
863 sdref->service_interface,
866 sdref->service_name_chosen,
868 sdref->service_domain,
869 sdref->service_regtype) < 0)
874 if ((ret = avahi_entry_group_commit(sdref->entry_group)) < 0)
880 static void reg_client_callback(AvahiClient *s, AvahiClientState state, void* userdata) {
881 DNSServiceRef sdref = userdata;
885 assert(sdref->n_ref >= 1);
887 /* We've not been setup completely */
888 if (!sdref->entry_group)
892 case AVAHI_CLIENT_DISCONNECTED:
894 reg_report_error(sdref, kDNSServiceErr_NoError);
897 case AVAHI_CLIENT_S_RUNNING: {
900 if (!sdref->service_name) {
902 /* If the service name is taken from the host name, copy that */
904 avahi_free(sdref->service_name_chosen);
905 sdref->service_name_chosen = NULL;
907 if (!(n = avahi_client_get_host_name(sdref->client))) {
908 reg_report_error(sdref, map_error(avahi_client_errno(sdref->client)));
912 if (!(sdref->service_name_chosen = avahi_strdup(n))) {
913 reg_report_error(sdref, kDNSServiceErr_NoMemory);
918 /* Register the service */
920 if ((ret = reg_create_service(sdref)) < 0) {
921 reg_report_error(sdref, map_error(ret));
928 case AVAHI_CLIENT_S_COLLISION:
930 /* Remove our entry */
931 avahi_entry_group_reset(sdref->entry_group);
935 case AVAHI_CLIENT_S_INVALID:
936 case AVAHI_CLIENT_S_REGISTERING:
943 static void reg_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
944 DNSServiceRef sdref = userdata;
949 case AVAHI_ENTRY_GROUP_ESTABLISHED:
950 /* Inform the user */
951 reg_report_error(sdref, kDNSServiceErr_NoError);
955 case AVAHI_ENTRY_GROUP_COLLISION: {
959 /* Remove our entry */
960 avahi_entry_group_reset(sdref->entry_group);
962 assert(sdref->service_name_chosen);
964 /* Pick a new name */
965 if (!(n = avahi_alternative_service_name(sdref->service_name_chosen))) {
966 reg_report_error(sdref, kDNSServiceErr_NoMemory);
969 avahi_free(sdref->service_name_chosen);
970 sdref->service_name_chosen = n;
972 /* Register the service with that new name */
973 if ((ret = reg_create_service(sdref)) < 0) {
974 reg_report_error(sdref, map_error(ret));
981 case AVAHI_ENTRY_GROUP_REGISTERING:
982 case AVAHI_ENTRY_GROUP_UNCOMMITED:
988 DNSServiceErrorType DNSSD_API DNSServiceRegister (
989 DNSServiceRef *ret_sdref,
990 DNSServiceFlags flags,
998 const void *txtRecord,
999 DNSServiceRegisterReply callback,
1002 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
1004 DNSServiceRef sdref = NULL;
1012 if (interface == kDNSServiceInterfaceIndexLocalOnly || flags) {
1013 AVAHI_WARN_UNSUPPORTED;
1014 return kDNSServiceErr_Unsupported;
1017 if (!(sdref = sdref_new()))
1018 return kDNSServiceErr_Unknown;
1020 sdref->context = context;
1021 sdref->service_register_callback = callback;
1023 sdref->service_name = avahi_strdup(name);
1024 sdref->service_regtype = regtype ? avahi_normalize_name_strdup(regtype) : NULL;
1025 sdref->service_domain = domain ? avahi_normalize_name_strdup(domain) : NULL;
1026 sdref->service_host = host ? avahi_normalize_name_strdup(host) : NULL;
1027 sdref->service_interface = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
1028 sdref->service_port = ntohs(port);
1029 sdref->service_txt = txtRecord && txtLen > 0 ? avahi_string_list_parse(txtRecord, txtLen) : NULL;
1031 /* Some OOM checking would be cool here */
1033 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
1035 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), reg_client_callback, sdref, &error))) {
1036 ret = map_error(error);
1040 if (!sdref->service_domain) {
1043 if (!(d = avahi_client_get_domain_name(sdref->client))) {
1044 ret = map_error(avahi_client_errno(sdref->client));
1048 if (!(sdref->service_domain = avahi_strdup(d))) {
1049 ret = kDNSServiceErr_NoMemory;
1054 if (!(sdref->entry_group = avahi_entry_group_new(sdref->client, reg_entry_group_callback, sdref))) {
1055 ret = map_error(avahi_client_errno(sdref->client));
1059 if (avahi_client_get_state(sdref->client) == AVAHI_CLIENT_S_RUNNING) {
1062 if (sdref->service_name)
1063 n = sdref->service_name;
1065 if (!(n = avahi_client_get_host_name(sdref->client))) {
1066 ret = map_error(avahi_client_errno(sdref->client));
1071 if (!(sdref->service_name_chosen = avahi_strdup(n))) {
1072 ret = kDNSServiceErr_NoMemory;
1077 if ((error = reg_create_service(sdref)) < 0) {
1078 ret = map_error(error);
1083 ret = kDNSServiceErr_NoError;
1088 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
1090 if (ret != kDNSServiceErr_NoError)
1091 DNSServiceRefDeallocate(sdref);