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>
35 #include <avahi-common/simple-watch.h>
36 #include <avahi-common/malloc.h>
37 #include <avahi-common/error.h>
38 #include <avahi-common/domain.h>
39 #include <avahi-common/alternative.h>
40 #include <avahi-client/client.h>
51 struct _DNSServiceRef_t {
52 AvahiSimplePoll *simple_poll;
54 int thread_fd, main_fd;
59 pthread_mutex_t mutex;
62 DNSServiceBrowseReply service_browser_callback;
63 DNSServiceResolveReply service_resolver_callback;
64 DNSServiceDomainEnumReply domain_browser_callback;
65 DNSServiceRegisterReply service_register_callback;
68 AvahiServiceBrowser *service_browser;
69 AvahiServiceResolver *service_resolver;
70 AvahiDomainBrowser *domain_browser;
72 char *service_name, *service_name_chosen, *service_regtype, *service_domain, *service_host;
73 uint16_t service_port;
74 AvahiIfIndex service_interface;
75 AvahiStringList *service_txt;
77 AvahiEntryGroup *entry_group;
80 #define ASSERT_SUCCESS(r) { int __ret = (r); assert(__ret == 0); }
82 static int read_command(int fd) {
88 if ((r = read(fd, &command, 1)) != 1) {
89 fprintf(stderr, __FILE__": read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
96 static int write_command(int fd, char reply) {
99 if (write(fd, &reply, 1) != 1) {
100 fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
107 static int poll_func(struct pollfd *ufds, unsigned int nfds, int timeout, void *userdata) {
108 DNSServiceRef sdref = userdata;
113 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
115 /* fprintf(stderr, "pre-syscall\n"); */
116 ret = poll(ufds, nfds, timeout);
117 /* fprintf(stderr, "post-syscall\n"); */
119 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
124 static void * thread_func(void *data) {
125 DNSServiceRef sdref = data;
129 pthread_sigmask(SIG_BLOCK, &mask, NULL);
131 sdref->thread = pthread_self();
132 sdref->thread_running = 1;
137 if ((command = read_command(sdref->thread_fd)) < 0)
140 /* fprintf(stderr, "Command: %c\n", command); */
146 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
149 if (avahi_simple_poll_run(sdref->simple_poll) < 0) {
150 fprintf(stderr, __FILE__": avahi_simple_poll_run() failed.\n");
151 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
155 if (write_command(sdref->thread_fd, COMMAND_POLLED) < 0) {
156 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
160 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
173 static DNSServiceRef sdref_new(void) {
174 int fd[2] = { -1, -1 };
175 DNSServiceRef sdref = NULL;
176 pthread_mutexattr_t mutex_attr;
178 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0)
181 if (!(sdref = avahi_new(struct _DNSServiceRef_t, 1)))
184 sdref->thread_fd = fd[0];
185 sdref->main_fd = fd[1];
187 sdref->client = NULL;
188 sdref->service_browser = NULL;
189 sdref->service_resolver = NULL;
190 sdref->domain_browser = NULL;
191 sdref->entry_group = NULL;
193 sdref->service_name = sdref->service_name_chosen = sdref->service_regtype = sdref->service_domain = sdref->service_host = NULL;
194 sdref->service_txt = NULL;
196 ASSERT_SUCCESS(pthread_mutexattr_init(&mutex_attr));
197 pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
198 ASSERT_SUCCESS(pthread_mutex_init(&sdref->mutex, NULL));
200 sdref->thread_running = 0;
202 if (!(sdref->simple_poll = avahi_simple_poll_new()))
205 avahi_simple_poll_set_func(sdref->simple_poll, poll_func, sdref);
207 /* Start simple poll */
208 if (avahi_simple_poll_prepare(sdref->simple_poll, -1) < 0)
211 /* Queue a initiall POLL command for the thread */
212 if (write_command(sdref->main_fd, COMMAND_POLL) < 0)
215 if (pthread_create(&sdref->thread, NULL, thread_func, sdref) != 0)
218 sdref->thread_running = 1;
225 DNSServiceRefDeallocate(sdref);
230 int DNSSD_API DNSServiceRefSockFD(DNSServiceRef sdRef) {
235 return sdRef->main_fd;
238 DNSServiceErrorType DNSSD_API DNSServiceProcessResult(DNSServiceRef sdref) {
239 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
243 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
245 /* Cleanup notification socket */
246 if (read_command(sdref->main_fd) != COMMAND_POLLED)
249 if (avahi_simple_poll_dispatch(sdref->simple_poll) < 0)
252 if (avahi_simple_poll_prepare(sdref->simple_poll, -1) < 0)
255 /* Request the poll */
256 if (write_command(sdref->main_fd, COMMAND_POLL) < 0)
259 ret = kDNSServiceErr_NoError;
263 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
268 void DNSSD_API DNSServiceRefDeallocate(DNSServiceRef sdref) {
273 if (sdref->thread_running) {
274 write_command(sdref->main_fd, COMMAND_QUIT);
275 avahi_simple_poll_wakeup(sdref->simple_poll);
276 pthread_join(sdref->thread, NULL);
280 avahi_client_free(sdref->client);
282 if (sdref->thread_fd >= 0)
283 close(sdref->thread_fd);
285 if (sdref->main_fd >= 0)
286 close(sdref->main_fd);
288 if (sdref->simple_poll)
289 avahi_simple_poll_free(sdref->simple_poll);
291 pthread_mutex_destroy(&sdref->mutex);
293 avahi_free(sdref->service_name);
294 avahi_free(sdref->service_name_chosen);
295 avahi_free(sdref->service_regtype);
296 avahi_free(sdref->service_domain);
297 avahi_free(sdref->service_host);
299 avahi_string_list_free(sdref->service_txt);
304 static DNSServiceErrorType map_error(int error) {
307 return kDNSServiceErr_NoError;
309 case AVAHI_ERR_BAD_STATE :
310 return kDNSServiceErr_BadState;
312 case AVAHI_ERR_INVALID_HOST_NAME:
313 case AVAHI_ERR_INVALID_DOMAIN_NAME:
314 case AVAHI_ERR_INVALID_TTL:
315 case AVAHI_ERR_IS_PATTERN:
316 case AVAHI_ERR_INVALID_RECORD:
317 case AVAHI_ERR_INVALID_SERVICE_NAME:
318 case AVAHI_ERR_INVALID_SERVICE_TYPE:
319 case AVAHI_ERR_INVALID_PORT:
320 case AVAHI_ERR_INVALID_KEY:
321 case AVAHI_ERR_INVALID_ADDRESS:
322 return kDNSServiceErr_BadParam;
325 case AVAHI_ERR_LOCAL_COLLISION:
326 return kDNSServiceErr_NameConflict;
328 case AVAHI_ERR_TOO_MANY_CLIENTS:
329 case AVAHI_ERR_TOO_MANY_OBJECTS:
330 case AVAHI_ERR_TOO_MANY_ENTRIES:
331 case AVAHI_ERR_ACCESS_DENIED:
332 return kDNSServiceErr_Refused;
334 case AVAHI_ERR_INVALID_OPERATION:
335 case AVAHI_ERR_INVALID_OBJECT:
336 return kDNSServiceErr_Invalid;
338 case AVAHI_ERR_NO_MEMORY:
339 return kDNSServiceErr_NoMemory;
341 case AVAHI_ERR_INVALID_INTERFACE:
342 case AVAHI_ERR_INVALID_PROTOCOL:
343 return kDNSServiceErr_BadInterfaceIndex;
345 case AVAHI_ERR_INVALID_FLAGS:
346 return kDNSServiceErr_BadFlags;
348 case AVAHI_ERR_NOT_FOUND:
349 return kDNSServiceErr_NoSuchName;
351 case AVAHI_ERR_VERSION_MISMATCH:
352 return kDNSServiceErr_Incompatible;
354 case AVAHI_ERR_NO_NETWORK:
356 case AVAHI_ERR_INVALID_CONFIG:
357 case AVAHI_ERR_TIMEOUT:
358 case AVAHI_ERR_DBUS_ERROR:
359 case AVAHI_ERR_NOT_CONNECTED:
360 case AVAHI_ERR_NO_DAEMON:
365 return kDNSServiceErr_Unknown;
368 static void service_browser_callback(
369 AvahiServiceBrowser *b,
370 AvahiIfIndex interface,
371 AvahiProtocol protocol,
372 AvahiBrowserEvent event,
376 AvahiLookupResultFlags flags,
379 DNSServiceRef sdref = userdata;
385 case AVAHI_BROWSER_NEW:
386 sdref->service_browser_callback(sdref, kDNSServiceFlagsAdd, interface, kDNSServiceErr_NoError, name, type, domain, sdref->context);
389 case AVAHI_BROWSER_REMOVE:
390 sdref->service_browser_callback(sdref, 0, interface, kDNSServiceErr_NoError, name, type, domain, sdref->context);
393 case AVAHI_BROWSER_FAILURE:
394 sdref->service_browser_callback(sdref, 0, interface, kDNSServiceErr_Unknown, name, type, domain, sdref->context);
397 case AVAHI_BROWSER_NOT_FOUND:
398 sdref->service_browser_callback(sdref, 0, interface, kDNSServiceErr_NoSuchName, name, type, domain, sdref->context);
401 case AVAHI_BROWSER_CACHE_EXHAUSTED:
402 case AVAHI_BROWSER_ALL_FOR_NOW:
407 DNSServiceErrorType DNSSD_API DNSServiceBrowse(
408 DNSServiceRef *ret_sdref,
409 DNSServiceFlags flags,
413 DNSServiceBrowseReply callback,
416 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
418 DNSServiceRef sdref = NULL;
419 AvahiIfIndex ifindex;
428 if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0)
429 return kDNSServiceErr_Unsupported;
431 if (!(sdref = sdref_new()))
432 return kDNSServiceErr_Unknown;
434 sdref->context = context;
435 sdref->service_browser_callback = callback;
437 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
439 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), NULL, NULL, &error))) {
440 ret = map_error(error);
444 ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
446 if (!(sdref->service_browser = avahi_service_browser_new(sdref->client, ifindex, AVAHI_PROTO_UNSPEC, regtype, domain, 0, service_browser_callback, sdref))) {
447 ret = map_error(avahi_client_errno(sdref->client));
452 ret = kDNSServiceErr_NoError;
457 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
459 if (ret != kDNSServiceErr_NoError)
460 DNSServiceRefDeallocate(sdref);
465 static void service_resolver_callback(
466 AvahiServiceResolver *r,
467 AvahiIfIndex interface,
468 AvahiProtocol protocol,
469 AvahiResolverEvent event,
473 const char *host_name,
474 const AvahiAddress *a,
476 AvahiStringList *txt,
477 AvahiLookupResultFlags flags,
480 DNSServiceRef sdref = userdata;
486 case AVAHI_RESOLVER_FOUND: {
488 char full_name[kDNSServiceMaxDomainName];
493 if ((p = avahi_new0(char, (l = avahi_string_list_serialize(txt, NULL, 0))+1)))
494 avahi_string_list_serialize(txt, p, l);
496 ret = avahi_service_name_snprint(full_name, sizeof(full_name), name, type, domain);
497 assert(ret == AVAHI_OK);
499 sdref->service_resolver_callback(sdref, 0, interface, kDNSServiceErr_NoError, full_name, host_name, htons(port), l, p, sdref->context);
505 case AVAHI_RESOLVER_TIMEOUT:
506 case AVAHI_RESOLVER_NOT_FOUND:
507 sdref->service_resolver_callback(sdref, 0, interface, kDNSServiceErr_NoSuchName, NULL, NULL, 0, 0, NULL, sdref->context);
510 case AVAHI_RESOLVER_FAILURE:
511 sdref->service_resolver_callback(sdref, 0, interface, kDNSServiceErr_Unknown, NULL, NULL, 0, 0, NULL, sdref->context);
516 DNSServiceErrorType DNSSD_API DNSServiceResolve(
517 DNSServiceRef *ret_sdref,
518 DNSServiceFlags flags,
523 DNSServiceResolveReply callback,
526 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
528 DNSServiceRef sdref = NULL;
529 AvahiIfIndex ifindex;
539 if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0)
540 return kDNSServiceErr_Unsupported;
542 if (!(sdref = sdref_new()))
543 return kDNSServiceErr_Unknown;
545 sdref->context = context;
546 sdref->service_resolver_callback = callback;
548 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
550 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), NULL, NULL, &error))) {
551 ret = map_error(error);
555 ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
557 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))) {
558 ret = map_error(avahi_client_errno(sdref->client));
563 ret = kDNSServiceErr_NoError;
568 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
570 if (ret != kDNSServiceErr_NoError)
571 DNSServiceRefDeallocate(sdref);
576 int DNSSD_API DNSServiceConstructFullName (
580 const char *domain) {
588 if (avahi_service_name_snprint(fullName, kDNSServiceMaxDomainName, service, regtype, domain) < 0)
594 static void domain_browser_callback(
595 AvahiDomainBrowser *b,
596 AvahiIfIndex interface,
597 AvahiProtocol protocol,
598 AvahiBrowserEvent event,
600 AvahiLookupResultFlags flags,
603 DNSServiceRef sdref = userdata;
609 case AVAHI_BROWSER_NEW:
610 sdref->domain_browser_callback(sdref, kDNSServiceFlagsAdd, interface, kDNSServiceErr_NoError, domain, sdref->context);
613 case AVAHI_BROWSER_REMOVE:
614 sdref->domain_browser_callback(sdref, 0, interface, kDNSServiceErr_NoError, domain, sdref->context);
617 case AVAHI_BROWSER_FAILURE:
618 sdref->domain_browser_callback(sdref, 0, interface, kDNSServiceErr_Unknown, domain, sdref->context);
621 case AVAHI_BROWSER_NOT_FOUND:
622 sdref->domain_browser_callback(sdref, 0, interface, kDNSServiceErr_NoSuchName, domain, sdref->context);
625 case AVAHI_BROWSER_CACHE_EXHAUSTED:
626 case AVAHI_BROWSER_ALL_FOR_NOW:
631 DNSServiceErrorType DNSSD_API DNSServiceEnumerateDomains(
632 DNSServiceRef *ret_sdref,
633 DNSServiceFlags flags,
635 DNSServiceDomainEnumReply callback,
638 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
640 DNSServiceRef sdref = NULL;
641 AvahiIfIndex ifindex;
648 if (interface == kDNSServiceInterfaceIndexLocalOnly ||
649 (flags != kDNSServiceFlagsBrowseDomains && flags != kDNSServiceFlagsRegistrationDomains))
650 return kDNSServiceErr_Unsupported;
652 if (!(sdref = sdref_new()))
653 return kDNSServiceErr_Unknown;
655 sdref->context = context;
656 sdref->domain_browser_callback = callback;
658 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
660 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), NULL, NULL, &error))) {
661 ret = map_error(error);
665 ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
667 if (!(sdref->domain_browser = avahi_domain_browser_new(sdref->client, ifindex, AVAHI_PROTO_UNSPEC, NULL,
668 flags == kDNSServiceFlagsRegistrationDomains ? AVAHI_DOMAIN_BROWSER_REGISTER : AVAHI_DOMAIN_BROWSER_BROWSE,
669 0, domain_browser_callback, sdref))) {
670 ret = map_error(avahi_client_errno(sdref->client));
674 ret = kDNSServiceErr_NoError;
679 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
681 if (ret != kDNSServiceErr_NoError)
682 DNSServiceRefDeallocate(sdref);
687 static void reg_report_error(DNSServiceRef sdref, DNSServiceErrorType error) {
690 assert(sdref->service_register_callback);
692 sdref->service_register_callback(
694 sdref->service_name_chosen ? sdref->service_name_chosen : sdref->service_name,
695 sdref->service_regtype,
696 sdref->service_domain,
700 static int reg_create_service(DNSServiceRef sdref) {
704 if ((ret = avahi_entry_group_add_service_strlst(
706 sdref->service_interface,
709 sdref->service_name_chosen,
710 sdref->service_regtype,
711 sdref->service_domain,
714 sdref->service_txt)) < 0)
717 if ((ret = avahi_entry_group_commit(sdref->entry_group)) < 0)
723 static void reg_client_callback(AvahiClient *s, AvahiClientState state, void* userdata) {
724 DNSServiceRef sdref = userdata;
728 /* We've not been setup completely */
729 if (!sdref->entry_group)
733 case AVAHI_CLIENT_DISCONNECTED: {
735 reg_report_error(sdref, kDNSServiceErr_NoError);
739 case AVAHI_CLIENT_S_RUNNING: {
742 if (!sdref->service_name) {
744 /* If the service name is taken from the host name, copy that */
746 avahi_free(sdref->service_name_chosen);
748 if (!(n = avahi_client_get_host_name(sdref->client))) {
749 reg_report_error(sdref, map_error(avahi_client_errno(sdref->client)));
753 if (!(sdref->service_name_chosen = avahi_strdup(n))) {
754 reg_report_error(sdref, kDNSServiceErr_NoMemory);
759 /* Register the service */
761 if ((ret = reg_create_service(sdref)) < 0) {
762 reg_report_error(sdref, map_error(ret));
769 case AVAHI_CLIENT_S_COLLISION:
771 /* Remove our entry */
772 avahi_entry_group_reset(sdref->entry_group);
776 case AVAHI_CLIENT_S_INVALID:
777 case AVAHI_CLIENT_S_REGISTERING:
784 static void reg_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
785 DNSServiceRef sdref = userdata;
790 case AVAHI_ENTRY_GROUP_ESTABLISHED:
791 /* Inform the user */
792 reg_report_error(sdref, kDNSServiceErr_NoError);
796 case AVAHI_ENTRY_GROUP_COLLISION: {
800 /* Remove our entry */
801 avahi_entry_group_reset(sdref->entry_group);
803 assert(sdref->service_name_chosen);
805 /* Pick a new name */
806 if (!(n = avahi_alternative_service_name(sdref->service_name_chosen))) {
807 reg_report_error(sdref, kDNSServiceErr_NoMemory);
810 avahi_free(sdref->service_name_chosen);
811 sdref->service_name_chosen = n;
813 /* Register the service with that new name */
814 if ((ret = reg_create_service(sdref)) < 0) {
815 reg_report_error(sdref, map_error(ret));
822 case AVAHI_ENTRY_GROUP_REGISTERING:
823 case AVAHI_ENTRY_GROUP_UNCOMMITED:
829 DNSServiceErrorType DNSSD_API DNSServiceRegister (
830 DNSServiceRef *ret_sdref,
831 DNSServiceFlags flags,
839 const void *txtRecord,
840 DNSServiceRegisterReply callback,
843 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
845 DNSServiceRef sdref = NULL;
853 if (interface == kDNSServiceInterfaceIndexLocalOnly || flags)
854 return kDNSServiceErr_Unsupported;
856 if (!(sdref = sdref_new()))
857 return kDNSServiceErr_Unknown;
859 sdref->context = context;
860 sdref->service_register_callback = callback;
862 sdref->service_name = avahi_strdup(name);
863 sdref->service_regtype = avahi_strdup(regtype);
864 sdref->service_domain = avahi_strdup(domain);
865 sdref->service_host = avahi_strdup(host);
866 sdref->service_interface = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
867 sdref->service_port = ntohs(port);
868 sdref->service_txt = txtRecord ? avahi_string_list_parse(txtRecord, txtLen) : NULL;
870 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
872 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), reg_client_callback, sdref, &error))) {
873 ret = map_error(error);
877 if (!sdref->service_domain) {
880 if (!(d = avahi_client_get_domain_name(sdref->client))) {
881 ret = map_error(avahi_client_errno(sdref->client));
885 if (!(sdref->service_domain = avahi_strdup(d))) {
886 ret = kDNSServiceErr_NoMemory;
891 if (!(sdref->entry_group = avahi_entry_group_new(sdref->client, reg_entry_group_callback, sdref))) {
892 ret = map_error(avahi_client_errno(sdref->client));
896 if (avahi_client_get_state(sdref->client) == AVAHI_CLIENT_S_RUNNING) {
899 if (sdref->service_name)
900 n = sdref->service_name;
902 if (!(n = avahi_client_get_host_name(sdref->client))) {
903 ret = map_error(avahi_client_errno(sdref->client));
908 if (!(sdref->service_name_chosen = avahi_strdup(n))) {
909 ret = kDNSServiceErr_NoMemory;
914 if ((error = reg_create_service(sdref)) < 0) {
915 ret = map_error(error);
920 ret = kDNSServiceErr_NoError;
925 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
927 if (ret != kDNSServiceErr_NoError)
928 DNSServiceRefDeallocate(sdref);