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'
61 AvahiStringList *subtypes;
65 struct _DNSServiceRef_t {
68 AvahiSimplePoll *simple_poll;
70 int thread_fd, main_fd;
75 pthread_mutex_t mutex;
78 DNSServiceBrowseReply service_browser_callback;
79 DNSServiceResolveReply service_resolver_callback;
80 DNSServiceDomainEnumReply domain_browser_callback;
81 DNSServiceRegisterReply service_register_callback;
84 AvahiServiceBrowser *service_browser;
85 AvahiServiceResolver *service_resolver;
86 AvahiDomainBrowser *domain_browser;
88 struct type_info type_info;
89 char *service_name, *service_name_chosen, *service_domain, *service_host;
90 uint16_t service_port;
91 AvahiIfIndex service_interface;
92 AvahiStringList *service_txt;
94 AvahiEntryGroup *entry_group;
97 #define ASSERT_SUCCESS(r) { int __ret = (r); assert(__ret == 0); }
99 static DNSServiceErrorType map_error(int error) {
102 return kDNSServiceErr_NoError;
104 case AVAHI_ERR_BAD_STATE :
105 return kDNSServiceErr_BadState;
107 case AVAHI_ERR_INVALID_HOST_NAME:
108 case AVAHI_ERR_INVALID_DOMAIN_NAME:
109 case AVAHI_ERR_INVALID_TTL:
110 case AVAHI_ERR_IS_PATTERN:
111 case AVAHI_ERR_INVALID_RECORD:
112 case AVAHI_ERR_INVALID_SERVICE_NAME:
113 case AVAHI_ERR_INVALID_SERVICE_TYPE:
114 case AVAHI_ERR_INVALID_PORT:
115 case AVAHI_ERR_INVALID_KEY:
116 case AVAHI_ERR_INVALID_ADDRESS:
117 case AVAHI_ERR_INVALID_SERVICE_SUBTYPE:
118 return kDNSServiceErr_BadParam;
121 case AVAHI_ERR_COLLISION:
122 return kDNSServiceErr_NameConflict;
124 case AVAHI_ERR_TOO_MANY_CLIENTS:
125 case AVAHI_ERR_TOO_MANY_OBJECTS:
126 case AVAHI_ERR_TOO_MANY_ENTRIES:
127 case AVAHI_ERR_ACCESS_DENIED:
128 return kDNSServiceErr_Refused;
130 case AVAHI_ERR_INVALID_OPERATION:
131 case AVAHI_ERR_INVALID_OBJECT:
132 return kDNSServiceErr_Invalid;
134 case AVAHI_ERR_NO_MEMORY:
135 return kDNSServiceErr_NoMemory;
137 case AVAHI_ERR_INVALID_INTERFACE:
138 case AVAHI_ERR_INVALID_PROTOCOL:
139 return kDNSServiceErr_BadInterfaceIndex;
141 case AVAHI_ERR_INVALID_FLAGS:
142 return kDNSServiceErr_BadFlags;
144 case AVAHI_ERR_NOT_FOUND:
145 return kDNSServiceErr_NoSuchName;
147 case AVAHI_ERR_VERSION_MISMATCH:
148 return kDNSServiceErr_Incompatible;
150 case AVAHI_ERR_NO_NETWORK:
152 case AVAHI_ERR_INVALID_CONFIG:
153 case AVAHI_ERR_TIMEOUT:
154 case AVAHI_ERR_DBUS_ERROR:
155 case AVAHI_ERR_DISCONNECTED:
156 case AVAHI_ERR_NO_DAEMON:
161 return kDNSServiceErr_Unknown;
164 static void type_info_init(struct type_info *i) {
171 static void type_info_free(struct type_info *i) {
175 avahi_string_list_free(i->subtypes);
180 static int type_info_parse(struct type_info *i, const char *t) {
199 token = avahi_strndup(t, l);
205 /* This is the first token, hence the main type */
207 if (!avahi_is_valid_service_type_strict(token))
215 /* This is not the first token, hence a subtype */
217 if (!(fst = avahi_strdup_printf("%s._sub.%s", token, i->type)))
220 if (!avahi_is_valid_service_subtype(fst)) {
225 i->subtypes = avahi_string_list_add(i->subtypes, fst);
249 static const char *add_trailing_dot(const char *s, char *buf, size_t buf_len) {
256 if (s[strlen(s)-1] == '.')
259 snprintf(buf, buf_len, "%s.", s);
263 static int read_command(int fd) {
269 if ((r = read(fd, &command, 1)) != 1) {
270 fprintf(stderr, __FILE__": read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
277 static int write_command(int fd, char reply) {
280 if (write(fd, &reply, 1) != 1) {
281 fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
288 static int poll_func(struct pollfd *ufds, unsigned int nfds, int timeout, void *userdata) {
289 DNSServiceRef sdref = userdata;
294 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
296 /* fprintf(stderr, "pre-syscall\n"); */
297 ret = poll(ufds, nfds, timeout);
298 /* fprintf(stderr, "post-syscall\n"); */
300 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
305 static void * thread_func(void *data) {
306 DNSServiceRef sdref = data;
310 pthread_sigmask(SIG_BLOCK, &mask, NULL);
312 sdref->thread = pthread_self();
313 sdref->thread_running = 1;
318 if ((command = read_command(sdref->thread_fd)) < 0)
321 /* fprintf(stderr, "Command: %c\n", command); */
328 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
333 if ((ret = avahi_simple_poll_run(sdref->simple_poll)) < 0) {
338 fprintf(stderr, __FILE__": avahi_simple_poll_run() failed: %s\n", strerror(errno));
344 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
346 if (write_command(sdref->thread_fd, ret < 0 ? COMMAND_POLL_FAILED : COMMAND_POLL_DONE) < 0)
361 static DNSServiceRef sdref_new(void) {
362 int fd[2] = { -1, -1 };
363 DNSServiceRef sdref = NULL;
364 pthread_mutexattr_t mutex_attr;
366 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0)
369 if (!(sdref = avahi_new(struct _DNSServiceRef_t, 1)))
373 sdref->thread_fd = fd[0];
374 sdref->main_fd = fd[1];
376 sdref->client = NULL;
377 sdref->service_browser = NULL;
378 sdref->service_resolver = NULL;
379 sdref->domain_browser = NULL;
380 sdref->entry_group = NULL;
382 sdref->service_name = sdref->service_name_chosen = sdref->service_domain = sdref->service_host = NULL;
383 sdref->service_txt = NULL;
385 type_info_init(&sdref->type_info);
387 ASSERT_SUCCESS(pthread_mutexattr_init(&mutex_attr));
388 pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
389 ASSERT_SUCCESS(pthread_mutex_init(&sdref->mutex, &mutex_attr));
391 sdref->thread_running = 0;
393 if (!(sdref->simple_poll = avahi_simple_poll_new()))
396 avahi_simple_poll_set_func(sdref->simple_poll, poll_func, sdref);
398 /* Start simple poll */
399 if (avahi_simple_poll_prepare(sdref->simple_poll, -1) < 0)
402 /* Queue an initial POLL command for the thread */
403 if (write_command(sdref->main_fd, COMMAND_POLL) < 0)
406 if (pthread_create(&sdref->thread, NULL, thread_func, sdref) != 0)
409 sdref->thread_running = 1;
416 DNSServiceRefDeallocate(sdref);
421 static void sdref_free(DNSServiceRef sdref) {
424 if (sdref->thread_running) {
425 ASSERT_SUCCESS(write_command(sdref->main_fd, COMMAND_QUIT));
426 avahi_simple_poll_wakeup(sdref->simple_poll);
427 ASSERT_SUCCESS(pthread_join(sdref->thread, NULL));
431 avahi_client_free(sdref->client);
433 if (sdref->simple_poll)
434 avahi_simple_poll_free(sdref->simple_poll);
436 if (sdref->thread_fd >= 0)
437 close(sdref->thread_fd);
439 if (sdref->main_fd >= 0)
440 close(sdref->main_fd);
442 ASSERT_SUCCESS(pthread_mutex_destroy(&sdref->mutex));
444 avahi_free(sdref->service_name);
445 avahi_free(sdref->service_name_chosen);
446 avahi_free(sdref->service_domain);
447 avahi_free(sdref->service_host);
449 type_info_free(&sdref->type_info);
451 avahi_string_list_free(sdref->service_txt);
456 static void sdref_ref(DNSServiceRef sdref) {
458 assert(sdref->n_ref >= 1);
463 static void sdref_unref(DNSServiceRef sdref) {
465 assert(sdref->n_ref >= 1);
467 if (--(sdref->n_ref) <= 0)
471 int DNSSD_API DNSServiceRefSockFD(DNSServiceRef sdref) {
475 if (!sdref || sdref->n_ref <= 0)
478 return sdref->main_fd;
481 DNSServiceErrorType DNSSD_API DNSServiceProcessResult(DNSServiceRef sdref) {
482 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
486 if (!sdref || sdref->n_ref <= 0)
487 return kDNSServiceErr_BadParam;
491 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
493 /* Cleanup notification socket */
494 if (read_command(sdref->main_fd) != COMMAND_POLL_DONE)
497 if (avahi_simple_poll_dispatch(sdref->simple_poll) < 0)
500 if (sdref->n_ref > 1) /* Perhaps we should die */
502 /* Dispatch events */
503 if (avahi_simple_poll_prepare(sdref->simple_poll, -1) < 0)
506 if (sdref->n_ref > 1)
508 /* Request the poll */
509 if (write_command(sdref->main_fd, COMMAND_POLL) < 0)
512 ret = kDNSServiceErr_NoError;
516 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
523 void DNSSD_API DNSServiceRefDeallocate(DNSServiceRef sdref) {
530 static void service_browser_callback(
531 AvahiServiceBrowser *b,
532 AvahiIfIndex interface,
533 AVAHI_GCC_UNUSED AvahiProtocol protocol,
534 AvahiBrowserEvent event,
538 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
541 DNSServiceRef sdref = userdata;
542 char type_fixed[AVAHI_DOMAIN_NAME_MAX], domain_fixed[AVAHI_DOMAIN_NAME_MAX];
545 assert(sdref->n_ref >= 1);
547 type = add_trailing_dot(type, type_fixed, sizeof(type_fixed));
548 domain = add_trailing_dot(domain, domain_fixed, sizeof(domain_fixed));
551 case AVAHI_BROWSER_NEW:
552 sdref->service_browser_callback(sdref, kDNSServiceFlagsAdd, interface, kDNSServiceErr_NoError, name, type, domain, sdref->context);
555 case AVAHI_BROWSER_REMOVE:
556 sdref->service_browser_callback(sdref, 0, interface, kDNSServiceErr_NoError, name, type, domain, sdref->context);
559 case AVAHI_BROWSER_FAILURE:
560 sdref->service_browser_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), NULL, NULL, NULL, sdref->context);
563 case AVAHI_BROWSER_CACHE_EXHAUSTED:
564 case AVAHI_BROWSER_ALL_FOR_NOW:
569 static void generic_client_callback(AvahiClient *s, AvahiClientState state, void* userdata) {
570 DNSServiceRef sdref = userdata;
571 int error = kDNSServiceErr_Unknown;
575 assert(sdref->n_ref >= 1);
579 case AVAHI_CLIENT_FAILURE:
581 if (sdref->service_browser_callback)
582 sdref->service_browser_callback(sdref, 0, 0, error, NULL, NULL, NULL, sdref->context);
583 else if (sdref->service_resolver_callback)
584 sdref->service_resolver_callback(sdref, 0, 0, error, NULL, NULL, 0, 0, NULL, sdref->context);
585 else if (sdref->domain_browser_callback)
586 sdref->domain_browser_callback(sdref, 0, 0, error, NULL, sdref->context);
590 case AVAHI_CLIENT_S_RUNNING:
591 case AVAHI_CLIENT_S_COLLISION:
592 case AVAHI_CLIENT_S_REGISTERING:
593 case AVAHI_CLIENT_CONNECTING:
598 DNSServiceErrorType DNSSD_API DNSServiceBrowse(
599 DNSServiceRef *ret_sdref,
600 DNSServiceFlags flags,
604 DNSServiceBrowseReply callback,
607 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
609 DNSServiceRef sdref = NULL;
610 AvahiIfIndex ifindex;
611 struct type_info type_info;
615 if (!ret_sdref || !regtype)
616 return kDNSServiceErr_BadParam;
619 if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0) {
620 AVAHI_WARN_UNSUPPORTED;
621 return kDNSServiceErr_Unsupported;
624 type_info_init(&type_info);
626 if (type_info_parse(&type_info, regtype) < 0 || type_info.n_subtypes > 1) {
627 type_info_free(&type_info);
629 if (!avahi_is_valid_service_type_generic(regtype))
630 return kDNSServiceErr_Unsupported;
632 regtype = type_info.subtypes ? (char*) type_info.subtypes->text : type_info.type;
634 if (!(sdref = sdref_new())) {
635 type_info_free(&type_info);
636 return kDNSServiceErr_Unknown;
639 sdref->context = context;
640 sdref->service_browser_callback = callback;
642 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
644 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), 0, generic_client_callback, sdref, &error))) {
645 ret = map_error(error);
649 ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
651 if (!(sdref->service_browser = avahi_service_browser_new(sdref->client, ifindex, AVAHI_PROTO_UNSPEC, regtype, domain, 0, service_browser_callback, sdref))) {
652 ret = map_error(avahi_client_errno(sdref->client));
656 ret = kDNSServiceErr_NoError;
661 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
663 if (ret != kDNSServiceErr_NoError)
664 DNSServiceRefDeallocate(sdref);
666 type_info_free(&type_info);
671 static void service_resolver_callback(
672 AvahiServiceResolver *r,
673 AvahiIfIndex interface,
674 AVAHI_GCC_UNUSED AvahiProtocol protocol,
675 AvahiResolverEvent event,
679 const char *host_name,
680 AVAHI_GCC_UNUSED const AvahiAddress *a,
682 AvahiStringList *txt,
683 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
686 DNSServiceRef sdref = userdata;
690 assert(sdref->n_ref >= 1);
693 case AVAHI_RESOLVER_FOUND: {
695 char host_name_fixed[AVAHI_DOMAIN_NAME_MAX];
696 char full_name[AVAHI_DOMAIN_NAME_MAX];
701 host_name = add_trailing_dot(host_name, host_name_fixed, sizeof(host_name_fixed));
703 if ((p = avahi_new0(char, (l = avahi_string_list_serialize(txt, NULL, 0))+1)))
704 avahi_string_list_serialize(txt, p, l);
706 ret = avahi_service_name_join(full_name, sizeof(full_name), name, type, domain);
707 assert(ret == AVAHI_OK);
709 strcat(full_name, ".");
711 sdref->service_resolver_callback(sdref, 0, interface, kDNSServiceErr_NoError, full_name, host_name, htons(port), l, (unsigned char*) p, sdref->context);
717 case AVAHI_RESOLVER_FAILURE:
718 sdref->service_resolver_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), NULL, NULL, 0, 0, NULL, sdref->context);
723 DNSServiceErrorType DNSSD_API DNSServiceResolve(
724 DNSServiceRef *ret_sdref,
725 DNSServiceFlags flags,
730 DNSServiceResolveReply callback,
733 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
735 DNSServiceRef sdref = NULL;
736 AvahiIfIndex ifindex;
740 if (!ret_sdref || !name || !regtype || !domain || !callback)
741 return kDNSServiceErr_BadParam;
744 if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0) {
745 AVAHI_WARN_UNSUPPORTED;
746 return kDNSServiceErr_Unsupported;
749 if (!(sdref = sdref_new()))
750 return kDNSServiceErr_Unknown;
752 sdref->context = context;
753 sdref->service_resolver_callback = callback;
755 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
757 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), 0, generic_client_callback, sdref, &error))) {
758 ret = map_error(error);
762 ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
764 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))) {
765 ret = map_error(avahi_client_errno(sdref->client));
770 ret = kDNSServiceErr_NoError;
775 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
777 if (ret != kDNSServiceErr_NoError)
778 DNSServiceRefDeallocate(sdref);
783 int DNSSD_API DNSServiceConstructFullName (
787 const char *domain) {
791 if (!fullName || !regtype || !domain)
794 if (avahi_service_name_join(fullName, kDNSServiceMaxDomainName, service, regtype, domain) < 0)
800 static void domain_browser_callback(
801 AvahiDomainBrowser *b,
802 AvahiIfIndex interface,
803 AVAHI_GCC_UNUSED AvahiProtocol protocol,
804 AvahiBrowserEvent event,
806 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
809 DNSServiceRef sdref = userdata;
810 static char domain_fixed[AVAHI_DOMAIN_NAME_MAX];
814 assert(sdref->n_ref >= 1);
816 domain = add_trailing_dot(domain, domain_fixed, sizeof(domain_fixed));
819 case AVAHI_BROWSER_NEW:
820 sdref->domain_browser_callback(sdref, kDNSServiceFlagsAdd, interface, kDNSServiceErr_NoError, domain, sdref->context);
823 case AVAHI_BROWSER_REMOVE:
824 sdref->domain_browser_callback(sdref, 0, interface, kDNSServiceErr_NoError, domain, sdref->context);
827 case AVAHI_BROWSER_FAILURE:
828 sdref->domain_browser_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), domain, sdref->context);
831 case AVAHI_BROWSER_CACHE_EXHAUSTED:
832 case AVAHI_BROWSER_ALL_FOR_NOW:
837 DNSServiceErrorType DNSSD_API DNSServiceEnumerateDomains(
838 DNSServiceRef *ret_sdref,
839 DNSServiceFlags flags,
841 DNSServiceDomainEnumReply callback,
844 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
846 DNSServiceRef sdref = NULL;
847 AvahiIfIndex ifindex;
851 if (!ret_sdref || !callback)
852 return kDNSServiceErr_BadParam;
855 if (interface == kDNSServiceInterfaceIndexLocalOnly ||
856 (flags != kDNSServiceFlagsBrowseDomains && flags != kDNSServiceFlagsRegistrationDomains)) {
857 AVAHI_WARN_UNSUPPORTED;
858 return kDNSServiceErr_Unsupported;
861 if (!(sdref = sdref_new()))
862 return kDNSServiceErr_Unknown;
864 sdref->context = context;
865 sdref->domain_browser_callback = callback;
867 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
869 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), 0, generic_client_callback, sdref, &error))) {
870 ret = map_error(error);
874 ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
876 if (!(sdref->domain_browser = avahi_domain_browser_new(sdref->client, ifindex, AVAHI_PROTO_UNSPEC, "local",
877 flags == kDNSServiceFlagsRegistrationDomains ? AVAHI_DOMAIN_BROWSER_REGISTER : AVAHI_DOMAIN_BROWSER_BROWSE,
878 0, domain_browser_callback, sdref))) {
879 ret = map_error(avahi_client_errno(sdref->client));
883 ret = kDNSServiceErr_NoError;
888 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
890 if (ret != kDNSServiceErr_NoError)
891 DNSServiceRefDeallocate(sdref);
896 static void reg_report_error(DNSServiceRef sdref, DNSServiceErrorType error) {
897 char regtype_fixed[AVAHI_DOMAIN_NAME_MAX], domain_fixed[AVAHI_DOMAIN_NAME_MAX];
898 const char *regtype, *domain;
900 assert(sdref->n_ref >= 1);
902 if (!sdref->service_register_callback)
905 regtype = add_trailing_dot(sdref->type_info.type, regtype_fixed, sizeof(regtype_fixed));
906 domain = add_trailing_dot(sdref->service_domain, domain_fixed, sizeof(domain_fixed));
908 sdref->service_register_callback(
910 sdref->service_name_chosen ? sdref->service_name_chosen : sdref->service_name,
916 static int reg_create_service(DNSServiceRef sdref) {
921 assert(sdref->n_ref >= 1);
923 if ((ret = avahi_entry_group_add_service_strlst(
925 sdref->service_interface,
928 sdref->service_name_chosen,
929 sdref->type_info.type,
930 sdref->service_domain,
933 sdref->service_txt)) < 0)
936 for (l = sdref->type_info.subtypes; l; l = l->next) {
937 /* Create a subtype entry */
939 if (avahi_entry_group_add_service_subtype(
941 sdref->service_interface,
944 sdref->service_name_chosen,
945 sdref->type_info.type,
946 sdref->service_domain,
947 (const char*) l->text) < 0)
951 if ((ret = avahi_entry_group_commit(sdref->entry_group)) < 0)
957 static void reg_client_callback(AvahiClient *s, AvahiClientState state, void* userdata) {
958 DNSServiceRef sdref = userdata;
962 assert(sdref->n_ref >= 1);
964 /* We've not been setup completely */
965 if (!sdref->entry_group)
969 case AVAHI_CLIENT_FAILURE:
970 reg_report_error(sdref, kDNSServiceErr_Unknown);
973 case AVAHI_CLIENT_S_RUNNING: {
976 if (!sdref->service_name) {
978 /* If the service name is taken from the host name, copy that */
980 avahi_free(sdref->service_name_chosen);
981 sdref->service_name_chosen = NULL;
983 if (!(n = avahi_client_get_host_name(sdref->client))) {
984 reg_report_error(sdref, map_error(avahi_client_errno(sdref->client)));
988 if (!(sdref->service_name_chosen = avahi_strdup(n))) {
989 reg_report_error(sdref, kDNSServiceErr_NoMemory);
994 if (!sdref->service_name_chosen) {
996 assert(sdref->service_name);
998 if (!(sdref->service_name_chosen = avahi_strdup(sdref->service_name))) {
999 reg_report_error(sdref, kDNSServiceErr_NoMemory);
1004 /* Register the service */
1006 if ((ret = reg_create_service(sdref)) < 0) {
1007 reg_report_error(sdref, map_error(ret));
1014 case AVAHI_CLIENT_S_COLLISION:
1015 case AVAHI_CLIENT_S_REGISTERING:
1017 /* Remove our entry */
1018 avahi_entry_group_reset(sdref->entry_group);
1022 case AVAHI_CLIENT_CONNECTING:
1029 static void reg_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
1030 DNSServiceRef sdref = userdata;
1035 case AVAHI_ENTRY_GROUP_ESTABLISHED:
1037 /* Inform the user */
1038 reg_report_error(sdref, kDNSServiceErr_NoError);
1042 case AVAHI_ENTRY_GROUP_COLLISION: {
1046 /* Remove our entry */
1047 avahi_entry_group_reset(sdref->entry_group);
1049 assert(sdref->service_name_chosen);
1051 /* Pick a new name */
1052 if (!(n = avahi_alternative_service_name(sdref->service_name_chosen))) {
1053 reg_report_error(sdref, kDNSServiceErr_NoMemory);
1056 avahi_free(sdref->service_name_chosen);
1057 sdref->service_name_chosen = n;
1059 /* Register the service with that new name */
1060 if ((ret = reg_create_service(sdref)) < 0) {
1061 reg_report_error(sdref, map_error(ret));
1068 case AVAHI_ENTRY_GROUP_REGISTERING:
1069 case AVAHI_ENTRY_GROUP_UNCOMMITED:
1073 case AVAHI_ENTRY_GROUP_FAILURE:
1074 /* Inform the user */
1075 reg_report_error(sdref, map_error(avahi_client_errno(sdref->client)));
1081 DNSServiceErrorType DNSSD_API DNSServiceRegister (
1082 DNSServiceRef *ret_sdref,
1083 DNSServiceFlags flags,
1086 const char *regtype,
1091 const void *txtRecord,
1092 DNSServiceRegisterReply callback,
1095 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
1097 DNSServiceRef sdref = NULL;
1098 AvahiStringList *txt = NULL;
1099 struct type_info type_info;
1103 if (!ret_sdref || !regtype)
1104 return kDNSServiceErr_BadParam;
1112 if (interface == kDNSServiceInterfaceIndexLocalOnly || flags) {
1113 AVAHI_WARN_UNSUPPORTED;
1114 return kDNSServiceErr_Unsupported;
1118 if (avahi_string_list_parse(txtRecord, txtLen, &txt) < 0)
1119 return kDNSServiceErr_Invalid;
1121 if (type_info_parse(&type_info, regtype) < 0) {
1122 avahi_string_list_free(txt);
1123 return kDNSServiceErr_Invalid;
1126 if (!(sdref = sdref_new())) {
1127 avahi_string_list_free(txt);
1128 type_info_free(&type_info);
1129 return kDNSServiceErr_Unknown;
1132 sdref->context = context;
1133 sdref->service_register_callback = callback;
1135 sdref->type_info = type_info;
1136 sdref->service_name = avahi_strdup(name);
1137 sdref->service_domain = domain ? avahi_normalize_name_strdup(domain) : NULL;
1138 sdref->service_host = host ? avahi_normalize_name_strdup(host) : NULL;
1139 sdref->service_interface = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
1140 sdref->service_port = ntohs(port);
1141 sdref->service_txt = txt;
1143 /* Some OOM checking would be cool here */
1145 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
1147 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), 0, reg_client_callback, sdref, &error))) {
1148 ret = map_error(error);
1152 if (!sdref->service_domain) {
1155 if (!(d = avahi_client_get_domain_name(sdref->client))) {
1156 ret = map_error(avahi_client_errno(sdref->client));
1160 if (!(sdref->service_domain = avahi_strdup(d))) {
1161 ret = kDNSServiceErr_NoMemory;
1166 if (!(sdref->entry_group = avahi_entry_group_new(sdref->client, reg_entry_group_callback, sdref))) {
1167 ret = map_error(avahi_client_errno(sdref->client));
1171 if (avahi_client_get_state(sdref->client) == AVAHI_CLIENT_S_RUNNING) {
1174 if (sdref->service_name)
1175 n = sdref->service_name;
1177 if (!(n = avahi_client_get_host_name(sdref->client))) {
1178 ret = map_error(avahi_client_errno(sdref->client));
1183 if (!(sdref->service_name_chosen = avahi_strdup(n))) {
1184 ret = kDNSServiceErr_NoMemory;
1189 if ((error = reg_create_service(sdref)) < 0) {
1190 ret = map_error(error);
1195 ret = kDNSServiceErr_NoError;
1200 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
1202 if (ret != kDNSServiceErr_NoError)
1203 DNSServiceRefDeallocate(sdref);
1208 DNSServiceErrorType DNSSD_API DNSServiceUpdateRecord(
1209 DNSServiceRef sdref,
1211 DNSServiceFlags flags,
1214 AVAHI_GCC_UNUSED uint32_t ttl) {
1216 int ret = kDNSServiceErr_Unknown;
1217 AvahiStringList *txt = NULL;
1221 if (!sdref || sdref->n_ref <= 0)
1222 return kDNSServiceErr_BadParam;
1224 if (flags || rref) {
1225 AVAHI_WARN_UNSUPPORTED;
1226 return kDNSServiceErr_Unsupported;
1230 if (avahi_string_list_parse(rdata, rdlen, &txt) < 0)
1231 return kDNSServiceErr_Invalid;
1233 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
1235 if (!avahi_string_list_equal(txt, sdref->service_txt)) {
1237 avahi_string_list_free(sdref->service_txt);
1238 sdref->service_txt = txt;
1240 if (avahi_client_get_state(sdref->client) == AVAHI_CLIENT_S_RUNNING &&
1241 sdref->entry_group &&
1242 (avahi_entry_group_get_state(sdref->entry_group) == AVAHI_ENTRY_GROUP_ESTABLISHED ||
1243 avahi_entry_group_get_state(sdref->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING))
1245 if (avahi_entry_group_update_service_txt_strlst(
1247 sdref->service_interface,
1250 sdref->service_name_chosen,
1251 sdref->type_info.type,
1252 sdref->service_domain,
1253 sdref->service_txt) < 0) {
1255 ret = map_error(avahi_client_errno(sdref->client));
1260 avahi_string_list_free(txt);
1262 ret = kDNSServiceErr_NoError;
1265 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));