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>
52 COMMAND_POLL_DONE = 'P',
53 COMMAND_POLL_FAILED = 'F'
56 struct _DNSServiceRef_t {
59 AvahiSimplePoll *simple_poll;
61 int thread_fd, main_fd;
66 pthread_mutex_t mutex;
69 DNSServiceBrowseReply service_browser_callback;
70 DNSServiceResolveReply service_resolver_callback;
71 DNSServiceDomainEnumReply domain_browser_callback;
72 DNSServiceRegisterReply service_register_callback;
75 AvahiServiceBrowser *service_browser;
76 AvahiServiceResolver *service_resolver;
77 AvahiDomainBrowser *domain_browser;
79 char *service_name, *service_name_chosen, *service_regtype, *service_domain, *service_host;
80 uint16_t service_port;
81 AvahiIfIndex service_interface;
82 AvahiStringList *service_txt;
84 AvahiEntryGroup *entry_group;
87 #define ASSERT_SUCCESS(r) { int __ret = (r); assert(__ret == 0); }
89 static DNSServiceErrorType map_error(int error) {
92 return kDNSServiceErr_NoError;
94 case AVAHI_ERR_BAD_STATE :
95 return kDNSServiceErr_BadState;
97 case AVAHI_ERR_INVALID_HOST_NAME:
98 case AVAHI_ERR_INVALID_DOMAIN_NAME:
99 case AVAHI_ERR_INVALID_TTL:
100 case AVAHI_ERR_IS_PATTERN:
101 case AVAHI_ERR_INVALID_RECORD:
102 case AVAHI_ERR_INVALID_SERVICE_NAME:
103 case AVAHI_ERR_INVALID_SERVICE_TYPE:
104 case AVAHI_ERR_INVALID_PORT:
105 case AVAHI_ERR_INVALID_KEY:
106 case AVAHI_ERR_INVALID_ADDRESS:
107 case AVAHI_ERR_INVALID_SERVICE_SUBTYPE:
108 return kDNSServiceErr_BadParam;
111 case AVAHI_ERR_COLLISION:
112 return kDNSServiceErr_NameConflict;
114 case AVAHI_ERR_TOO_MANY_CLIENTS:
115 case AVAHI_ERR_TOO_MANY_OBJECTS:
116 case AVAHI_ERR_TOO_MANY_ENTRIES:
117 case AVAHI_ERR_ACCESS_DENIED:
118 return kDNSServiceErr_Refused;
120 case AVAHI_ERR_INVALID_OPERATION:
121 case AVAHI_ERR_INVALID_OBJECT:
122 return kDNSServiceErr_Invalid;
124 case AVAHI_ERR_NO_MEMORY:
125 return kDNSServiceErr_NoMemory;
127 case AVAHI_ERR_INVALID_INTERFACE:
128 case AVAHI_ERR_INVALID_PROTOCOL:
129 return kDNSServiceErr_BadInterfaceIndex;
131 case AVAHI_ERR_INVALID_FLAGS:
132 return kDNSServiceErr_BadFlags;
134 case AVAHI_ERR_NOT_FOUND:
135 return kDNSServiceErr_NoSuchName;
137 case AVAHI_ERR_VERSION_MISMATCH:
138 return kDNSServiceErr_Incompatible;
140 case AVAHI_ERR_NO_NETWORK:
142 case AVAHI_ERR_INVALID_CONFIG:
143 case AVAHI_ERR_TIMEOUT:
144 case AVAHI_ERR_DBUS_ERROR:
145 case AVAHI_ERR_NOT_CONNECTED:
146 case AVAHI_ERR_NO_DAEMON:
151 return kDNSServiceErr_Unknown;
154 static const char *add_trailing_dot(const char *s, char *buf, size_t buf_len) {
161 if (s[strlen(s)-1] == '.')
164 snprintf(buf, buf_len, "%s.", s);
168 static int read_command(int fd) {
174 if ((r = read(fd, &command, 1)) != 1) {
175 fprintf(stderr, __FILE__": read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
182 static int write_command(int fd, char reply) {
185 if (write(fd, &reply, 1) != 1) {
186 fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
193 static int poll_func(struct pollfd *ufds, unsigned int nfds, int timeout, void *userdata) {
194 DNSServiceRef sdref = userdata;
199 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
201 /* fprintf(stderr, "pre-syscall\n"); */
202 ret = poll(ufds, nfds, timeout);
203 /* fprintf(stderr, "post-syscall\n"); */
205 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
210 static void * thread_func(void *data) {
211 DNSServiceRef sdref = data;
215 pthread_sigmask(SIG_BLOCK, &mask, NULL);
217 sdref->thread = pthread_self();
218 sdref->thread_running = 1;
223 if ((command = read_command(sdref->thread_fd)) < 0)
226 /* fprintf(stderr, "Command: %c\n", command); */
233 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
238 if ((ret = avahi_simple_poll_run(sdref->simple_poll)) < 0) {
243 fprintf(stderr, __FILE__": avahi_simple_poll_run() failed: %s\n", strerror(errno));
249 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
251 if (write_command(sdref->thread_fd, ret < 0 ? COMMAND_POLL_FAILED : COMMAND_POLL_DONE) < 0)
266 static DNSServiceRef sdref_new(void) {
267 int fd[2] = { -1, -1 };
268 DNSServiceRef sdref = NULL;
269 pthread_mutexattr_t mutex_attr;
271 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0)
274 if (!(sdref = avahi_new(struct _DNSServiceRef_t, 1)))
278 sdref->thread_fd = fd[0];
279 sdref->main_fd = fd[1];
281 sdref->client = NULL;
282 sdref->service_browser = NULL;
283 sdref->service_resolver = NULL;
284 sdref->domain_browser = NULL;
285 sdref->entry_group = NULL;
287 sdref->service_name = sdref->service_name_chosen = sdref->service_regtype = sdref->service_domain = sdref->service_host = NULL;
288 sdref->service_txt = NULL;
290 ASSERT_SUCCESS(pthread_mutexattr_init(&mutex_attr));
291 pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
292 ASSERT_SUCCESS(pthread_mutex_init(&sdref->mutex, &mutex_attr));
294 sdref->thread_running = 0;
296 if (!(sdref->simple_poll = avahi_simple_poll_new()))
299 avahi_simple_poll_set_func(sdref->simple_poll, poll_func, sdref);
301 /* Start simple poll */
302 if (avahi_simple_poll_prepare(sdref->simple_poll, -1) < 0)
305 /* Queue an initial POLL command for the thread */
306 if (write_command(sdref->main_fd, COMMAND_POLL) < 0)
309 if (pthread_create(&sdref->thread, NULL, thread_func, sdref) != 0)
312 sdref->thread_running = 1;
319 DNSServiceRefDeallocate(sdref);
324 static void sdref_free(DNSServiceRef sdref) {
327 if (sdref->thread_running) {
328 ASSERT_SUCCESS(write_command(sdref->main_fd, COMMAND_QUIT));
329 avahi_simple_poll_wakeup(sdref->simple_poll);
330 ASSERT_SUCCESS(pthread_join(sdref->thread, NULL));
334 avahi_client_free(sdref->client);
336 if (sdref->simple_poll)
337 avahi_simple_poll_free(sdref->simple_poll);
339 if (sdref->thread_fd >= 0)
340 close(sdref->thread_fd);
342 if (sdref->main_fd >= 0)
343 close(sdref->main_fd);
345 ASSERT_SUCCESS(pthread_mutex_destroy(&sdref->mutex));
347 avahi_free(sdref->service_name);
348 avahi_free(sdref->service_name_chosen);
349 avahi_free(sdref->service_regtype);
350 avahi_free(sdref->service_domain);
351 avahi_free(sdref->service_host);
353 avahi_string_list_free(sdref->service_txt);
358 static void sdref_ref(DNSServiceRef sdref) {
360 assert(sdref->n_ref >= 1);
365 static void sdref_unref(DNSServiceRef sdref) {
367 assert(sdref->n_ref >= 1);
369 if (--(sdref->n_ref) <= 0)
373 int DNSSD_API DNSServiceRefSockFD(DNSServiceRef sdref) {
375 assert(sdref->n_ref >= 1);
379 return sdref->main_fd;
382 DNSServiceErrorType DNSSD_API DNSServiceProcessResult(DNSServiceRef sdref) {
383 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
386 assert(sdref->n_ref >= 1);
390 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
394 /* Cleanup notification socket */
395 if (read_command(sdref->main_fd) != COMMAND_POLL_DONE)
398 if (avahi_simple_poll_dispatch(sdref->simple_poll) < 0)
401 if (sdref->n_ref > 1) /* Perhaps we should die */
403 /* Dispatch events */
404 if (avahi_simple_poll_prepare(sdref->simple_poll, -1) < 0)
407 if (sdref->n_ref > 1)
409 /* Request the poll */
410 if (write_command(sdref->main_fd, COMMAND_POLL) < 0)
413 ret = kDNSServiceErr_NoError;
419 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
424 void DNSSD_API DNSServiceRefDeallocate(DNSServiceRef sdref) {
431 static void service_browser_callback(
432 AvahiServiceBrowser *b,
433 AvahiIfIndex interface,
434 AVAHI_GCC_UNUSED AvahiProtocol protocol,
435 AvahiBrowserEvent event,
439 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
442 DNSServiceRef sdref = userdata;
443 char type_fixed[AVAHI_DOMAIN_NAME_MAX], domain_fixed[AVAHI_DOMAIN_NAME_MAX];
446 assert(sdref->n_ref >= 1);
448 type = add_trailing_dot(type, type_fixed, sizeof(type_fixed));
449 domain = add_trailing_dot(domain, domain_fixed, sizeof(domain_fixed));
452 case AVAHI_BROWSER_NEW:
453 sdref->service_browser_callback(sdref, kDNSServiceFlagsAdd, interface, kDNSServiceErr_NoError, name, type, domain, sdref->context);
456 case AVAHI_BROWSER_REMOVE:
457 sdref->service_browser_callback(sdref, 0, interface, kDNSServiceErr_NoError, name, type, domain, sdref->context);
460 case AVAHI_BROWSER_FAILURE:
461 sdref->service_browser_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), NULL, NULL, NULL, sdref->context);
464 case AVAHI_BROWSER_CACHE_EXHAUSTED:
465 case AVAHI_BROWSER_ALL_FOR_NOW:
470 static void generic_client_callback(AvahiClient *s, AvahiClientState state, void* userdata) {
471 DNSServiceRef sdref = userdata;
472 int error = kDNSServiceErr_Unknown;
476 assert(sdref->n_ref >= 1);
480 case AVAHI_CLIENT_DISCONNECTED:
482 if (sdref->service_browser_callback)
483 sdref->service_browser_callback(sdref, 0, 0, error, NULL, NULL, NULL, sdref->context);
484 else if (sdref->service_resolver_callback)
485 sdref->service_resolver_callback(sdref, 0, 0, error, NULL, NULL, 0, 0, NULL, sdref->context);
486 else if (sdref->domain_browser_callback)
487 sdref->domain_browser_callback(sdref, 0, 0, error, NULL, sdref->context);
491 case AVAHI_CLIENT_S_RUNNING:
492 case AVAHI_CLIENT_S_COLLISION:
493 case AVAHI_CLIENT_S_REGISTERING:
498 DNSServiceErrorType DNSSD_API DNSServiceBrowse(
499 DNSServiceRef *ret_sdref,
500 DNSServiceFlags flags,
504 DNSServiceBrowseReply callback,
507 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
509 DNSServiceRef sdref = NULL;
510 AvahiIfIndex ifindex;
519 if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0) {
520 AVAHI_WARN_UNSUPPORTED;
521 return kDNSServiceErr_Unsupported;
524 if (!(sdref = sdref_new()))
525 return kDNSServiceErr_Unknown;
527 sdref->context = context;
528 sdref->service_browser_callback = callback;
530 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
532 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), generic_client_callback, sdref, &error))) {
533 ret = map_error(error);
537 ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
539 if (!(sdref->service_browser = avahi_service_browser_new(sdref->client, ifindex, AVAHI_PROTO_UNSPEC, regtype, domain, 0, service_browser_callback, sdref))) {
540 ret = map_error(avahi_client_errno(sdref->client));
544 ret = kDNSServiceErr_NoError;
549 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
551 if (ret != kDNSServiceErr_NoError)
552 DNSServiceRefDeallocate(sdref);
557 static void service_resolver_callback(
558 AvahiServiceResolver *r,
559 AvahiIfIndex interface,
560 AVAHI_GCC_UNUSED AvahiProtocol protocol,
561 AvahiResolverEvent event,
565 const char *host_name,
566 AVAHI_GCC_UNUSED const AvahiAddress *a,
568 AvahiStringList *txt,
569 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
572 DNSServiceRef sdref = userdata;
576 assert(sdref->n_ref >= 1);
579 case AVAHI_RESOLVER_FOUND: {
581 char host_name_fixed[AVAHI_DOMAIN_NAME_MAX];
582 char full_name[AVAHI_DOMAIN_NAME_MAX];
587 host_name = add_trailing_dot(host_name, host_name_fixed, sizeof(host_name_fixed));
589 if ((p = avahi_new0(char, (l = avahi_string_list_serialize(txt, NULL, 0))+1)))
590 avahi_string_list_serialize(txt, p, l);
592 ret = avahi_service_name_join(full_name, sizeof(full_name), name, type, domain);
593 assert(ret == AVAHI_OK);
595 strcat(full_name, ".");
597 sdref->service_resolver_callback(sdref, 0, interface, kDNSServiceErr_NoError, full_name, host_name, htons(port), l, p, sdref->context);
603 case AVAHI_RESOLVER_FAILURE:
604 sdref->service_resolver_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), NULL, NULL, 0, 0, NULL, sdref->context);
609 DNSServiceErrorType DNSSD_API DNSServiceResolve(
610 DNSServiceRef *ret_sdref,
611 DNSServiceFlags flags,
616 DNSServiceResolveReply callback,
619 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
621 DNSServiceRef sdref = NULL;
622 AvahiIfIndex ifindex;
632 if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0) {
633 AVAHI_WARN_UNSUPPORTED;
634 return kDNSServiceErr_Unsupported;
637 if (!(sdref = sdref_new()))
638 return kDNSServiceErr_Unknown;
640 sdref->context = context;
641 sdref->service_resolver_callback = callback;
643 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
645 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), generic_client_callback, sdref, &error))) {
646 ret = map_error(error);
650 ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
652 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))) {
653 ret = map_error(avahi_client_errno(sdref->client));
658 ret = kDNSServiceErr_NoError;
663 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
665 if (ret != kDNSServiceErr_NoError)
666 DNSServiceRefDeallocate(sdref);
671 int DNSSD_API DNSServiceConstructFullName (
675 const char *domain) {
683 if (avahi_service_name_join(fullName, kDNSServiceMaxDomainName, service, regtype, domain) < 0)
689 static void domain_browser_callback(
690 AvahiDomainBrowser *b,
691 AvahiIfIndex interface,
692 AVAHI_GCC_UNUSED AvahiProtocol protocol,
693 AvahiBrowserEvent event,
695 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
698 DNSServiceRef sdref = userdata;
699 static char domain_fixed[AVAHI_DOMAIN_NAME_MAX];
703 assert(sdref->n_ref >= 1);
705 domain = add_trailing_dot(domain, domain_fixed, sizeof(domain_fixed));
708 case AVAHI_BROWSER_NEW:
709 sdref->domain_browser_callback(sdref, kDNSServiceFlagsAdd, interface, kDNSServiceErr_NoError, domain, sdref->context);
712 case AVAHI_BROWSER_REMOVE:
713 sdref->domain_browser_callback(sdref, 0, interface, kDNSServiceErr_NoError, domain, sdref->context);
716 case AVAHI_BROWSER_FAILURE:
717 sdref->domain_browser_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), domain, sdref->context);
720 case AVAHI_BROWSER_CACHE_EXHAUSTED:
721 case AVAHI_BROWSER_ALL_FOR_NOW:
726 DNSServiceErrorType DNSSD_API DNSServiceEnumerateDomains(
727 DNSServiceRef *ret_sdref,
728 DNSServiceFlags flags,
730 DNSServiceDomainEnumReply callback,
733 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
735 DNSServiceRef sdref = NULL;
736 AvahiIfIndex ifindex;
743 if (interface == kDNSServiceInterfaceIndexLocalOnly ||
744 (flags != kDNSServiceFlagsBrowseDomains && flags != kDNSServiceFlagsRegistrationDomains)) {
745 AVAHI_WARN_UNSUPPORTED;
746 return kDNSServiceErr_Unsupported;
749 if (!(sdref = sdref_new()))
750 return kDNSServiceErr_Unknown;
752 sdref->context = context;
753 sdref->domain_browser_callback = callback;
755 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
757 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), generic_client_callback, sdref, &error))) {
758 ret = map_error(error);
762 ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
764 if (!(sdref->domain_browser = avahi_domain_browser_new(sdref->client, ifindex, AVAHI_PROTO_UNSPEC, "local",
765 flags == kDNSServiceFlagsRegistrationDomains ? AVAHI_DOMAIN_BROWSER_REGISTER : AVAHI_DOMAIN_BROWSER_BROWSE,
766 0, domain_browser_callback, sdref))) {
767 ret = map_error(avahi_client_errno(sdref->client));
771 ret = kDNSServiceErr_NoError;
776 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
778 if (ret != kDNSServiceErr_NoError)
779 DNSServiceRefDeallocate(sdref);
784 static void reg_report_error(DNSServiceRef sdref, DNSServiceErrorType error) {
785 char regtype_fixed[AVAHI_DOMAIN_NAME_MAX], domain_fixed[AVAHI_DOMAIN_NAME_MAX];
786 const char *regtype, *domain;
788 assert(sdref->n_ref >= 1);
790 assert(sdref->service_register_callback);
792 regtype = add_trailing_dot(sdref->service_regtype, regtype_fixed, sizeof(regtype_fixed));
793 domain = add_trailing_dot(sdref->service_domain, domain_fixed, sizeof(domain_fixed));
795 sdref->service_register_callback(
797 sdref->service_name_chosen ? sdref->service_name_chosen : sdref->service_name,
803 static int reg_create_service(DNSServiceRef sdref) {
805 const char *real_type;
808 assert(sdref->n_ref >= 1);
810 real_type = avahi_get_type_from_subtype(sdref->service_regtype);
812 if ((ret = avahi_entry_group_add_service_strlst(
814 sdref->service_interface,
817 sdref->service_name_chosen,
818 real_type ? real_type : sdref->service_regtype,
819 sdref->service_domain,
822 sdref->service_txt)) < 0)
827 /* Create a subtype entry */
829 if (avahi_entry_group_add_service_subtype(
831 sdref->service_interface,
834 sdref->service_name_chosen,
836 sdref->service_domain,
837 sdref->service_regtype) < 0)
842 if ((ret = avahi_entry_group_commit(sdref->entry_group)) < 0)
848 static void reg_client_callback(AvahiClient *s, AvahiClientState state, void* userdata) {
849 DNSServiceRef sdref = userdata;
853 assert(sdref->n_ref >= 1);
855 /* We've not been setup completely */
856 if (!sdref->entry_group)
860 case AVAHI_CLIENT_DISCONNECTED:
861 reg_report_error(sdref, kDNSServiceErr_Unknown);
864 case AVAHI_CLIENT_S_RUNNING: {
867 if (!sdref->service_name) {
869 /* If the service name is taken from the host name, copy that */
871 avahi_free(sdref->service_name_chosen);
872 sdref->service_name_chosen = NULL;
874 if (!(n = avahi_client_get_host_name(sdref->client))) {
875 reg_report_error(sdref, map_error(avahi_client_errno(sdref->client)));
879 if (!(sdref->service_name_chosen = avahi_strdup(n))) {
880 reg_report_error(sdref, kDNSServiceErr_NoMemory);
885 /* Register the service */
887 if ((ret = reg_create_service(sdref)) < 0) {
888 reg_report_error(sdref, map_error(ret));
895 case AVAHI_CLIENT_S_COLLISION:
897 /* Remove our entry */
898 avahi_entry_group_reset(sdref->entry_group);
902 case AVAHI_CLIENT_S_REGISTERING:
909 static void reg_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
910 DNSServiceRef sdref = userdata;
915 case AVAHI_ENTRY_GROUP_ESTABLISHED:
916 /* Inform the user */
917 reg_report_error(sdref, kDNSServiceErr_NoError);
921 case AVAHI_ENTRY_GROUP_COLLISION: {
925 /* Remove our entry */
926 avahi_entry_group_reset(sdref->entry_group);
928 assert(sdref->service_name_chosen);
930 /* Pick a new name */
931 if (!(n = avahi_alternative_service_name(sdref->service_name_chosen))) {
932 reg_report_error(sdref, kDNSServiceErr_NoMemory);
935 avahi_free(sdref->service_name_chosen);
936 sdref->service_name_chosen = n;
938 /* Register the service with that new name */
939 if ((ret = reg_create_service(sdref)) < 0) {
940 reg_report_error(sdref, map_error(ret));
947 case AVAHI_ENTRY_GROUP_REGISTERING:
948 case AVAHI_ENTRY_GROUP_UNCOMMITED:
952 case AVAHI_ENTRY_GROUP_FAILURE:
953 /* Inform the user */
954 reg_report_error(sdref, map_error(avahi_client_errno(sdref->client)));
960 DNSServiceErrorType DNSSD_API DNSServiceRegister (
961 DNSServiceRef *ret_sdref,
962 DNSServiceFlags flags,
970 const void *txtRecord,
971 DNSServiceRegisterReply callback,
974 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
976 DNSServiceRef sdref = NULL;
977 AvahiStringList *txt = NULL;
985 if (interface == kDNSServiceInterfaceIndexLocalOnly || flags) {
986 AVAHI_WARN_UNSUPPORTED;
987 return kDNSServiceErr_Unsupported;
990 if (txtRecord && txtLen > 0)
991 if (avahi_string_list_parse(txtRecord, txtLen, &txt) < 0)
992 return kDNSServiceErr_Invalid;
994 if (!(sdref = sdref_new())) {
995 avahi_string_list_free(txt);
996 return kDNSServiceErr_Unknown;
999 sdref->context = context;
1000 sdref->service_register_callback = callback;
1002 sdref->service_name = avahi_strdup(name);
1003 sdref->service_regtype = regtype ? avahi_normalize_name_strdup(regtype) : NULL;
1004 sdref->service_domain = domain ? avahi_normalize_name_strdup(domain) : NULL;
1005 sdref->service_host = host ? avahi_normalize_name_strdup(host) : NULL;
1006 sdref->service_interface = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
1007 sdref->service_port = ntohs(port);
1008 sdref->service_txt = txt;
1010 /* Some OOM checking would be cool here */
1012 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
1014 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), reg_client_callback, sdref, &error))) {
1015 ret = map_error(error);
1019 if (!sdref->service_domain) {
1022 if (!(d = avahi_client_get_domain_name(sdref->client))) {
1023 ret = map_error(avahi_client_errno(sdref->client));
1027 if (!(sdref->service_domain = avahi_strdup(d))) {
1028 ret = kDNSServiceErr_NoMemory;
1033 if (!(sdref->entry_group = avahi_entry_group_new(sdref->client, reg_entry_group_callback, sdref))) {
1034 ret = map_error(avahi_client_errno(sdref->client));
1038 if (avahi_client_get_state(sdref->client) == AVAHI_CLIENT_S_RUNNING) {
1041 if (sdref->service_name)
1042 n = sdref->service_name;
1044 if (!(n = avahi_client_get_host_name(sdref->client))) {
1045 ret = map_error(avahi_client_errno(sdref->client));
1050 if (!(sdref->service_name_chosen = avahi_strdup(n))) {
1051 ret = kDNSServiceErr_NoMemory;
1056 if ((error = reg_create_service(sdref)) < 0) {
1057 ret = map_error(error);
1062 ret = kDNSServiceErr_NoError;
1067 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
1069 if (ret != kDNSServiceErr_NoError)
1070 DNSServiceRefDeallocate(sdref);