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 <sys/types.h>
37 #include <sys/socket.h>
39 #include <avahi-common/simple-watch.h>
40 #include <avahi-common/malloc.h>
41 #include <avahi-common/error.h>
42 #include <avahi-common/domain.h>
43 #include <avahi-common/alternative.h>
45 #include <avahi-client/client.h>
46 #include <avahi-client/publish.h>
47 #include <avahi-client/lookup.h>
55 COMMAND_POLL_DONE = 'P',
56 COMMAND_POLL_FAILED = 'F'
59 struct _DNSServiceRef_t {
62 AvahiSimplePoll *simple_poll;
64 int thread_fd, main_fd;
69 pthread_mutex_t mutex;
72 DNSServiceBrowseReply service_browser_callback;
73 DNSServiceResolveReply service_resolver_callback;
74 DNSServiceDomainEnumReply domain_browser_callback;
75 DNSServiceRegisterReply service_register_callback;
78 AvahiServiceBrowser *service_browser;
79 AvahiServiceResolver *service_resolver;
80 AvahiDomainBrowser *domain_browser;
82 char *service_name, *service_name_chosen, *service_regtype, *service_domain, *service_host;
83 uint16_t service_port;
84 AvahiIfIndex service_interface;
85 AvahiStringList *service_txt;
87 AvahiEntryGroup *entry_group;
90 #define ASSERT_SUCCESS(r) { int __ret = (r); assert(__ret == 0); }
92 static DNSServiceErrorType map_error(int error) {
95 return kDNSServiceErr_NoError;
97 case AVAHI_ERR_BAD_STATE :
98 return kDNSServiceErr_BadState;
100 case AVAHI_ERR_INVALID_HOST_NAME:
101 case AVAHI_ERR_INVALID_DOMAIN_NAME:
102 case AVAHI_ERR_INVALID_TTL:
103 case AVAHI_ERR_IS_PATTERN:
104 case AVAHI_ERR_INVALID_RECORD:
105 case AVAHI_ERR_INVALID_SERVICE_NAME:
106 case AVAHI_ERR_INVALID_SERVICE_TYPE:
107 case AVAHI_ERR_INVALID_PORT:
108 case AVAHI_ERR_INVALID_KEY:
109 case AVAHI_ERR_INVALID_ADDRESS:
110 case AVAHI_ERR_INVALID_SERVICE_SUBTYPE:
111 return kDNSServiceErr_BadParam;
114 case AVAHI_ERR_COLLISION:
115 return kDNSServiceErr_NameConflict;
117 case AVAHI_ERR_TOO_MANY_CLIENTS:
118 case AVAHI_ERR_TOO_MANY_OBJECTS:
119 case AVAHI_ERR_TOO_MANY_ENTRIES:
120 case AVAHI_ERR_ACCESS_DENIED:
121 return kDNSServiceErr_Refused;
123 case AVAHI_ERR_INVALID_OPERATION:
124 case AVAHI_ERR_INVALID_OBJECT:
125 return kDNSServiceErr_Invalid;
127 case AVAHI_ERR_NO_MEMORY:
128 return kDNSServiceErr_NoMemory;
130 case AVAHI_ERR_INVALID_INTERFACE:
131 case AVAHI_ERR_INVALID_PROTOCOL:
132 return kDNSServiceErr_BadInterfaceIndex;
134 case AVAHI_ERR_INVALID_FLAGS:
135 return kDNSServiceErr_BadFlags;
137 case AVAHI_ERR_NOT_FOUND:
138 return kDNSServiceErr_NoSuchName;
140 case AVAHI_ERR_VERSION_MISMATCH:
141 return kDNSServiceErr_Incompatible;
143 case AVAHI_ERR_NO_NETWORK:
145 case AVAHI_ERR_INVALID_CONFIG:
146 case AVAHI_ERR_TIMEOUT:
147 case AVAHI_ERR_DBUS_ERROR:
148 case AVAHI_ERR_DISCONNECTED:
149 case AVAHI_ERR_NO_DAEMON:
154 return kDNSServiceErr_Unknown;
157 static const char *add_trailing_dot(const char *s, char *buf, size_t buf_len) {
164 if (s[strlen(s)-1] == '.')
167 snprintf(buf, buf_len, "%s.", s);
171 static int read_command(int fd) {
177 if ((r = read(fd, &command, 1)) != 1) {
178 fprintf(stderr, __FILE__": read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
185 static int write_command(int fd, char reply) {
188 if (write(fd, &reply, 1) != 1) {
189 fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
196 static int poll_func(struct pollfd *ufds, unsigned int nfds, int timeout, void *userdata) {
197 DNSServiceRef sdref = userdata;
202 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
204 /* fprintf(stderr, "pre-syscall\n"); */
205 ret = poll(ufds, nfds, timeout);
206 /* fprintf(stderr, "post-syscall\n"); */
208 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
213 static void * thread_func(void *data) {
214 DNSServiceRef sdref = data;
218 pthread_sigmask(SIG_BLOCK, &mask, NULL);
220 sdref->thread = pthread_self();
221 sdref->thread_running = 1;
226 if ((command = read_command(sdref->thread_fd)) < 0)
229 /* fprintf(stderr, "Command: %c\n", command); */
236 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
241 if ((ret = avahi_simple_poll_run(sdref->simple_poll)) < 0) {
246 fprintf(stderr, __FILE__": avahi_simple_poll_run() failed: %s\n", strerror(errno));
252 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
254 if (write_command(sdref->thread_fd, ret < 0 ? COMMAND_POLL_FAILED : COMMAND_POLL_DONE) < 0)
269 static DNSServiceRef sdref_new(void) {
270 int fd[2] = { -1, -1 };
271 DNSServiceRef sdref = NULL;
272 pthread_mutexattr_t mutex_attr;
274 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0)
277 if (!(sdref = avahi_new(struct _DNSServiceRef_t, 1)))
281 sdref->thread_fd = fd[0];
282 sdref->main_fd = fd[1];
284 sdref->client = NULL;
285 sdref->service_browser = NULL;
286 sdref->service_resolver = NULL;
287 sdref->domain_browser = NULL;
288 sdref->entry_group = NULL;
290 sdref->service_name = sdref->service_name_chosen = sdref->service_regtype = sdref->service_domain = sdref->service_host = NULL;
291 sdref->service_txt = NULL;
293 ASSERT_SUCCESS(pthread_mutexattr_init(&mutex_attr));
294 pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
295 ASSERT_SUCCESS(pthread_mutex_init(&sdref->mutex, &mutex_attr));
297 sdref->thread_running = 0;
299 if (!(sdref->simple_poll = avahi_simple_poll_new()))
302 avahi_simple_poll_set_func(sdref->simple_poll, poll_func, sdref);
304 /* Start simple poll */
305 if (avahi_simple_poll_prepare(sdref->simple_poll, -1) < 0)
308 /* Queue an initial POLL command for the thread */
309 if (write_command(sdref->main_fd, COMMAND_POLL) < 0)
312 if (pthread_create(&sdref->thread, NULL, thread_func, sdref) != 0)
315 sdref->thread_running = 1;
322 DNSServiceRefDeallocate(sdref);
327 static void sdref_free(DNSServiceRef sdref) {
330 if (sdref->thread_running) {
331 ASSERT_SUCCESS(write_command(sdref->main_fd, COMMAND_QUIT));
332 avahi_simple_poll_wakeup(sdref->simple_poll);
333 ASSERT_SUCCESS(pthread_join(sdref->thread, NULL));
337 avahi_client_free(sdref->client);
339 if (sdref->simple_poll)
340 avahi_simple_poll_free(sdref->simple_poll);
342 if (sdref->thread_fd >= 0)
343 close(sdref->thread_fd);
345 if (sdref->main_fd >= 0)
346 close(sdref->main_fd);
348 ASSERT_SUCCESS(pthread_mutex_destroy(&sdref->mutex));
350 avahi_free(sdref->service_name);
351 avahi_free(sdref->service_name_chosen);
352 avahi_free(sdref->service_regtype);
353 avahi_free(sdref->service_domain);
354 avahi_free(sdref->service_host);
356 avahi_string_list_free(sdref->service_txt);
361 static void sdref_ref(DNSServiceRef sdref) {
363 assert(sdref->n_ref >= 1);
368 static void sdref_unref(DNSServiceRef sdref) {
370 assert(sdref->n_ref >= 1);
372 if (--(sdref->n_ref) <= 0)
376 int DNSSD_API DNSServiceRefSockFD(DNSServiceRef sdref) {
378 assert(sdref->n_ref >= 1);
382 return sdref->main_fd;
385 DNSServiceErrorType DNSSD_API DNSServiceProcessResult(DNSServiceRef sdref) {
386 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
389 assert(sdref->n_ref >= 1);
393 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
397 /* Cleanup notification socket */
398 if (read_command(sdref->main_fd) != COMMAND_POLL_DONE)
401 if (avahi_simple_poll_dispatch(sdref->simple_poll) < 0)
404 if (sdref->n_ref > 1) /* Perhaps we should die */
406 /* Dispatch events */
407 if (avahi_simple_poll_prepare(sdref->simple_poll, -1) < 0)
410 if (sdref->n_ref > 1)
412 /* Request the poll */
413 if (write_command(sdref->main_fd, COMMAND_POLL) < 0)
416 ret = kDNSServiceErr_NoError;
422 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
427 void DNSSD_API DNSServiceRefDeallocate(DNSServiceRef sdref) {
434 static void service_browser_callback(
435 AvahiServiceBrowser *b,
436 AvahiIfIndex interface,
437 AVAHI_GCC_UNUSED AvahiProtocol protocol,
438 AvahiBrowserEvent event,
442 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
445 DNSServiceRef sdref = userdata;
446 char type_fixed[AVAHI_DOMAIN_NAME_MAX], domain_fixed[AVAHI_DOMAIN_NAME_MAX];
449 assert(sdref->n_ref >= 1);
451 type = add_trailing_dot(type, type_fixed, sizeof(type_fixed));
452 domain = add_trailing_dot(domain, domain_fixed, sizeof(domain_fixed));
455 case AVAHI_BROWSER_NEW:
456 sdref->service_browser_callback(sdref, kDNSServiceFlagsAdd, interface, kDNSServiceErr_NoError, name, type, domain, sdref->context);
459 case AVAHI_BROWSER_REMOVE:
460 sdref->service_browser_callback(sdref, 0, interface, kDNSServiceErr_NoError, name, type, domain, sdref->context);
463 case AVAHI_BROWSER_FAILURE:
464 sdref->service_browser_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), NULL, NULL, NULL, sdref->context);
467 case AVAHI_BROWSER_CACHE_EXHAUSTED:
468 case AVAHI_BROWSER_ALL_FOR_NOW:
473 static void generic_client_callback(AvahiClient *s, AvahiClientState state, void* userdata) {
474 DNSServiceRef sdref = userdata;
475 int error = kDNSServiceErr_Unknown;
479 assert(sdref->n_ref >= 1);
483 case AVAHI_CLIENT_FAILURE:
485 if (sdref->service_browser_callback)
486 sdref->service_browser_callback(sdref, 0, 0, error, NULL, NULL, NULL, sdref->context);
487 else if (sdref->service_resolver_callback)
488 sdref->service_resolver_callback(sdref, 0, 0, error, NULL, NULL, 0, 0, NULL, sdref->context);
489 else if (sdref->domain_browser_callback)
490 sdref->domain_browser_callback(sdref, 0, 0, error, NULL, sdref->context);
494 case AVAHI_CLIENT_S_RUNNING:
495 case AVAHI_CLIENT_S_COLLISION:
496 case AVAHI_CLIENT_S_REGISTERING:
497 case AVAHI_CLIENT_CONNECTING:
502 DNSServiceErrorType DNSSD_API DNSServiceBrowse(
503 DNSServiceRef *ret_sdref,
504 DNSServiceFlags flags,
508 DNSServiceBrowseReply callback,
511 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
513 DNSServiceRef sdref = NULL;
514 AvahiIfIndex ifindex;
523 if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0) {
524 AVAHI_WARN_UNSUPPORTED;
525 return kDNSServiceErr_Unsupported;
528 if (!(sdref = sdref_new()))
529 return kDNSServiceErr_Unknown;
531 sdref->context = context;
532 sdref->service_browser_callback = callback;
534 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
536 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), 0, generic_client_callback, sdref, &error))) {
537 ret = map_error(error);
541 ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
543 if (!(sdref->service_browser = avahi_service_browser_new(sdref->client, ifindex, AVAHI_PROTO_UNSPEC, regtype, domain, 0, service_browser_callback, sdref))) {
544 ret = map_error(avahi_client_errno(sdref->client));
548 ret = kDNSServiceErr_NoError;
553 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
555 if (ret != kDNSServiceErr_NoError)
556 DNSServiceRefDeallocate(sdref);
561 static void service_resolver_callback(
562 AvahiServiceResolver *r,
563 AvahiIfIndex interface,
564 AVAHI_GCC_UNUSED AvahiProtocol protocol,
565 AvahiResolverEvent event,
569 const char *host_name,
570 AVAHI_GCC_UNUSED const AvahiAddress *a,
572 AvahiStringList *txt,
573 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
576 DNSServiceRef sdref = userdata;
580 assert(sdref->n_ref >= 1);
583 case AVAHI_RESOLVER_FOUND: {
585 char host_name_fixed[AVAHI_DOMAIN_NAME_MAX];
586 char full_name[AVAHI_DOMAIN_NAME_MAX];
591 host_name = add_trailing_dot(host_name, host_name_fixed, sizeof(host_name_fixed));
593 if ((p = avahi_new0(char, (l = avahi_string_list_serialize(txt, NULL, 0))+1)))
594 avahi_string_list_serialize(txt, p, l);
596 ret = avahi_service_name_join(full_name, sizeof(full_name), name, type, domain);
597 assert(ret == AVAHI_OK);
599 strcat(full_name, ".");
601 sdref->service_resolver_callback(sdref, 0, interface, kDNSServiceErr_NoError, full_name, host_name, htons(port), l, p, sdref->context);
607 case AVAHI_RESOLVER_FAILURE:
608 sdref->service_resolver_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), NULL, NULL, 0, 0, NULL, sdref->context);
613 DNSServiceErrorType DNSSD_API DNSServiceResolve(
614 DNSServiceRef *ret_sdref,
615 DNSServiceFlags flags,
620 DNSServiceResolveReply callback,
623 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
625 DNSServiceRef sdref = NULL;
626 AvahiIfIndex ifindex;
636 if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0) {
637 AVAHI_WARN_UNSUPPORTED;
638 return kDNSServiceErr_Unsupported;
641 if (!(sdref = sdref_new()))
642 return kDNSServiceErr_Unknown;
644 sdref->context = context;
645 sdref->service_resolver_callback = callback;
647 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
649 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), 0, generic_client_callback, sdref, &error))) {
650 ret = map_error(error);
654 ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
656 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))) {
657 ret = map_error(avahi_client_errno(sdref->client));
662 ret = kDNSServiceErr_NoError;
667 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
669 if (ret != kDNSServiceErr_NoError)
670 DNSServiceRefDeallocate(sdref);
675 int DNSSD_API DNSServiceConstructFullName (
679 const char *domain) {
687 if (avahi_service_name_join(fullName, kDNSServiceMaxDomainName, service, regtype, domain) < 0)
693 static void domain_browser_callback(
694 AvahiDomainBrowser *b,
695 AvahiIfIndex interface,
696 AVAHI_GCC_UNUSED AvahiProtocol protocol,
697 AvahiBrowserEvent event,
699 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
702 DNSServiceRef sdref = userdata;
703 static char domain_fixed[AVAHI_DOMAIN_NAME_MAX];
707 assert(sdref->n_ref >= 1);
709 domain = add_trailing_dot(domain, domain_fixed, sizeof(domain_fixed));
712 case AVAHI_BROWSER_NEW:
713 sdref->domain_browser_callback(sdref, kDNSServiceFlagsAdd, interface, kDNSServiceErr_NoError, domain, sdref->context);
716 case AVAHI_BROWSER_REMOVE:
717 sdref->domain_browser_callback(sdref, 0, interface, kDNSServiceErr_NoError, domain, sdref->context);
720 case AVAHI_BROWSER_FAILURE:
721 sdref->domain_browser_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), domain, sdref->context);
724 case AVAHI_BROWSER_CACHE_EXHAUSTED:
725 case AVAHI_BROWSER_ALL_FOR_NOW:
730 DNSServiceErrorType DNSSD_API DNSServiceEnumerateDomains(
731 DNSServiceRef *ret_sdref,
732 DNSServiceFlags flags,
734 DNSServiceDomainEnumReply callback,
737 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
739 DNSServiceRef sdref = NULL;
740 AvahiIfIndex ifindex;
747 if (interface == kDNSServiceInterfaceIndexLocalOnly ||
748 (flags != kDNSServiceFlagsBrowseDomains && flags != kDNSServiceFlagsRegistrationDomains)) {
749 AVAHI_WARN_UNSUPPORTED;
750 return kDNSServiceErr_Unsupported;
753 if (!(sdref = sdref_new()))
754 return kDNSServiceErr_Unknown;
756 sdref->context = context;
757 sdref->domain_browser_callback = callback;
759 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
761 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), 0, generic_client_callback, sdref, &error))) {
762 ret = map_error(error);
766 ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
768 if (!(sdref->domain_browser = avahi_domain_browser_new(sdref->client, ifindex, AVAHI_PROTO_UNSPEC, "local",
769 flags == kDNSServiceFlagsRegistrationDomains ? AVAHI_DOMAIN_BROWSER_REGISTER : AVAHI_DOMAIN_BROWSER_BROWSE,
770 0, domain_browser_callback, sdref))) {
771 ret = map_error(avahi_client_errno(sdref->client));
775 ret = kDNSServiceErr_NoError;
780 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
782 if (ret != kDNSServiceErr_NoError)
783 DNSServiceRefDeallocate(sdref);
788 static void reg_report_error(DNSServiceRef sdref, DNSServiceErrorType error) {
789 char regtype_fixed[AVAHI_DOMAIN_NAME_MAX], domain_fixed[AVAHI_DOMAIN_NAME_MAX];
790 const char *regtype, *domain;
792 assert(sdref->n_ref >= 1);
794 assert(sdref->service_register_callback);
796 regtype = add_trailing_dot(sdref->service_regtype, regtype_fixed, sizeof(regtype_fixed));
797 domain = add_trailing_dot(sdref->service_domain, domain_fixed, sizeof(domain_fixed));
799 sdref->service_register_callback(
801 sdref->service_name_chosen ? sdref->service_name_chosen : sdref->service_name,
807 static int reg_create_service(DNSServiceRef sdref) {
809 const char *real_type;
812 assert(sdref->n_ref >= 1);
814 real_type = avahi_get_type_from_subtype(sdref->service_regtype);
816 if ((ret = avahi_entry_group_add_service_strlst(
818 sdref->service_interface,
821 sdref->service_name_chosen,
822 real_type ? real_type : sdref->service_regtype,
823 sdref->service_domain,
826 sdref->service_txt)) < 0)
831 /* Create a subtype entry */
833 if (avahi_entry_group_add_service_subtype(
835 sdref->service_interface,
838 sdref->service_name_chosen,
840 sdref->service_domain,
841 sdref->service_regtype) < 0)
846 if ((ret = avahi_entry_group_commit(sdref->entry_group)) < 0)
852 static void reg_client_callback(AvahiClient *s, AvahiClientState state, void* userdata) {
853 DNSServiceRef sdref = userdata;
857 assert(sdref->n_ref >= 1);
859 /* We've not been setup completely */
860 if (!sdref->entry_group)
864 case AVAHI_CLIENT_FAILURE:
865 reg_report_error(sdref, kDNSServiceErr_Unknown);
868 case AVAHI_CLIENT_S_RUNNING: {
871 if (!sdref->service_name) {
873 /* If the service name is taken from the host name, copy that */
875 avahi_free(sdref->service_name_chosen);
876 sdref->service_name_chosen = NULL;
878 if (!(n = avahi_client_get_host_name(sdref->client))) {
879 reg_report_error(sdref, map_error(avahi_client_errno(sdref->client)));
883 if (!(sdref->service_name_chosen = avahi_strdup(n))) {
884 reg_report_error(sdref, kDNSServiceErr_NoMemory);
889 /* Register the service */
891 if ((ret = reg_create_service(sdref)) < 0) {
892 reg_report_error(sdref, map_error(ret));
899 case AVAHI_CLIENT_S_COLLISION:
901 /* Remove our entry */
902 avahi_entry_group_reset(sdref->entry_group);
906 case AVAHI_CLIENT_CONNECTING:
907 case AVAHI_CLIENT_S_REGISTERING:
914 static void reg_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
915 DNSServiceRef sdref = userdata;
920 case AVAHI_ENTRY_GROUP_ESTABLISHED:
921 /* Inform the user */
922 reg_report_error(sdref, kDNSServiceErr_NoError);
926 case AVAHI_ENTRY_GROUP_COLLISION: {
930 /* Remove our entry */
931 avahi_entry_group_reset(sdref->entry_group);
933 assert(sdref->service_name_chosen);
935 /* Pick a new name */
936 if (!(n = avahi_alternative_service_name(sdref->service_name_chosen))) {
937 reg_report_error(sdref, kDNSServiceErr_NoMemory);
940 avahi_free(sdref->service_name_chosen);
941 sdref->service_name_chosen = n;
943 /* Register the service with that new name */
944 if ((ret = reg_create_service(sdref)) < 0) {
945 reg_report_error(sdref, map_error(ret));
952 case AVAHI_ENTRY_GROUP_REGISTERING:
953 case AVAHI_ENTRY_GROUP_UNCOMMITED:
957 case AVAHI_ENTRY_GROUP_FAILURE:
958 /* Inform the user */
959 reg_report_error(sdref, map_error(avahi_client_errno(sdref->client)));
965 DNSServiceErrorType DNSSD_API DNSServiceRegister (
966 DNSServiceRef *ret_sdref,
967 DNSServiceFlags flags,
975 const void *txtRecord,
976 DNSServiceRegisterReply callback,
979 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
981 DNSServiceRef sdref = NULL;
982 AvahiStringList *txt = NULL;
990 if (interface == kDNSServiceInterfaceIndexLocalOnly || flags) {
991 AVAHI_WARN_UNSUPPORTED;
992 return kDNSServiceErr_Unsupported;
995 if (txtRecord && txtLen > 0)
996 if (avahi_string_list_parse(txtRecord, txtLen, &txt) < 0)
997 return kDNSServiceErr_Invalid;
999 if (!(sdref = sdref_new())) {
1000 avahi_string_list_free(txt);
1001 return kDNSServiceErr_Unknown;
1004 sdref->context = context;
1005 sdref->service_register_callback = callback;
1007 sdref->service_name = avahi_strdup(name);
1008 sdref->service_regtype = regtype ? avahi_normalize_name_strdup(regtype) : NULL;
1009 sdref->service_domain = domain ? avahi_normalize_name_strdup(domain) : NULL;
1010 sdref->service_host = host ? avahi_normalize_name_strdup(host) : NULL;
1011 sdref->service_interface = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
1012 sdref->service_port = ntohs(port);
1013 sdref->service_txt = txt;
1015 /* Some OOM checking would be cool here */
1017 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
1019 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), 0, reg_client_callback, sdref, &error))) {
1020 ret = map_error(error);
1024 if (!sdref->service_domain) {
1027 if (!(d = avahi_client_get_domain_name(sdref->client))) {
1028 ret = map_error(avahi_client_errno(sdref->client));
1032 if (!(sdref->service_domain = avahi_strdup(d))) {
1033 ret = kDNSServiceErr_NoMemory;
1038 if (!(sdref->entry_group = avahi_entry_group_new(sdref->client, reg_entry_group_callback, sdref))) {
1039 ret = map_error(avahi_client_errno(sdref->client));
1043 if (avahi_client_get_state(sdref->client) == AVAHI_CLIENT_S_RUNNING) {
1046 if (sdref->service_name)
1047 n = sdref->service_name;
1049 if (!(n = avahi_client_get_host_name(sdref->client))) {
1050 ret = map_error(avahi_client_errno(sdref->client));
1055 if (!(sdref->service_name_chosen = avahi_strdup(n))) {
1056 ret = kDNSServiceErr_NoMemory;
1061 if ((error = reg_create_service(sdref)) < 0) {
1062 ret = map_error(error);
1067 ret = kDNSServiceErr_NoError;
1072 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
1074 if (ret != kDNSServiceErr_NoError)
1075 DNSServiceRefDeallocate(sdref);