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-client/client.h>
49 struct _DNSServiceRef_t {
50 AvahiSimplePoll *simple_poll;
52 int thread_fd, main_fd;
57 pthread_mutex_t mutex;
60 DNSServiceBrowseReply service_browser_callback;
61 DNSServiceResolveReply service_resolver_callback;
64 AvahiServiceBrowser *service_browser;
65 AvahiServiceResolver *service_resolver;
68 #define ASSERT_SUCCESS(r) { int __ret = (r); assert(__ret == 0); }
70 static int read_command(int fd) {
76 if ((r = read(fd, &command, 1)) != 1) {
77 fprintf(stderr, __FILE__": read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
84 static int write_command(int fd, char reply) {
87 if (write(fd, &reply, 1) != 1) {
88 fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
95 static int poll_func(struct pollfd *ufds, unsigned int nfds, int timeout, void *userdata) {
96 DNSServiceRef sdref = userdata;
101 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
103 /* fprintf(stderr, "pre-syscall\n"); */
104 ret = poll(ufds, nfds, timeout);
105 /* fprintf(stderr, "post-syscall\n"); */
107 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
112 static void * thread_func(void *data) {
113 DNSServiceRef sdref = data;
117 pthread_sigmask(SIG_BLOCK, &mask, NULL);
119 sdref->thread = pthread_self();
120 sdref->thread_running = 1;
125 if ((command = read_command(sdref->thread_fd)) < 0)
128 /* fprintf(stderr, "Command: %c\n", command); */
134 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
137 if (avahi_simple_poll_run(sdref->simple_poll) < 0) {
138 fprintf(stderr, __FILE__": avahi_simple_poll_run() failed.\n");
139 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
143 if (write_command(sdref->thread_fd, COMMAND_POLLED) < 0) {
144 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
148 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
161 static DNSServiceRef sdref_new(void) {
162 int fd[2] = { -1, -1 };
163 DNSServiceRef sdref = NULL;
164 pthread_mutexattr_t mutex_attr;
166 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0)
169 if (!(sdref = avahi_new(struct _DNSServiceRef_t, 1)))
172 sdref->thread_fd = fd[0];
173 sdref->main_fd = fd[1];
175 sdref->client = NULL;
176 sdref->service_browser = NULL;
177 sdref->service_resolver = NULL;
179 ASSERT_SUCCESS(pthread_mutexattr_init(&mutex_attr));
180 pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
181 ASSERT_SUCCESS(pthread_mutex_init(&sdref->mutex, NULL));
183 sdref->thread_running = 0;
185 if (!(sdref->simple_poll = avahi_simple_poll_new()))
188 avahi_simple_poll_set_func(sdref->simple_poll, poll_func, sdref);
190 /* Start simple poll */
191 if (avahi_simple_poll_prepare(sdref->simple_poll, -1) < 0)
194 /* Queue a initiall POLL command for the thread */
195 if (write_command(sdref->main_fd, COMMAND_POLL) < 0)
198 if (pthread_create(&sdref->thread, NULL, thread_func, sdref) != 0)
201 sdref->thread_running = 1;
208 DNSServiceRefDeallocate(sdref);
213 int DNSSD_API DNSServiceRefSockFD(DNSServiceRef sdRef) {
218 return sdRef->main_fd;
221 DNSServiceErrorType DNSSD_API DNSServiceProcessResult(DNSServiceRef sdref) {
222 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
226 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
228 /* Cleanup notification socket */
229 if (read_command(sdref->main_fd) != COMMAND_POLLED)
232 if (avahi_simple_poll_dispatch(sdref->simple_poll) < 0)
235 if (avahi_simple_poll_prepare(sdref->simple_poll, -1) < 0)
238 /* Request the poll */
239 if (write_command(sdref->main_fd, COMMAND_POLL) < 0)
242 ret = kDNSServiceErr_NoError;
246 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
251 void DNSSD_API DNSServiceRefDeallocate(DNSServiceRef sdref) {
256 if (sdref->thread_running) {
257 write_command(sdref->main_fd, COMMAND_QUIT);
258 avahi_simple_poll_wakeup(sdref->simple_poll);
259 pthread_join(sdref->thread, NULL);
263 avahi_client_free(sdref->client);
265 if (sdref->thread_fd >= 0)
266 close(sdref->thread_fd);
268 if (sdref->main_fd >= 0)
269 close(sdref->main_fd);
271 if (sdref->simple_poll)
272 avahi_simple_poll_free(sdref->simple_poll);
274 pthread_mutex_destroy(&sdref->mutex);
279 static DNSServiceErrorType map_error(int error) {
282 return kDNSServiceErr_NoError;
284 case AVAHI_ERR_BAD_STATE :
285 return kDNSServiceErr_BadState;
287 case AVAHI_ERR_INVALID_HOST_NAME:
288 case AVAHI_ERR_INVALID_DOMAIN_NAME:
289 case AVAHI_ERR_INVALID_TTL:
290 case AVAHI_ERR_IS_PATTERN:
291 case AVAHI_ERR_INVALID_RECORD:
292 case AVAHI_ERR_INVALID_SERVICE_NAME:
293 case AVAHI_ERR_INVALID_SERVICE_TYPE:
294 case AVAHI_ERR_INVALID_PORT:
295 case AVAHI_ERR_INVALID_KEY:
296 case AVAHI_ERR_INVALID_ADDRESS:
297 return kDNSServiceErr_BadParam;
300 case AVAHI_ERR_LOCAL_COLLISION:
301 return kDNSServiceErr_NameConflict;
303 case AVAHI_ERR_TOO_MANY_CLIENTS:
304 case AVAHI_ERR_TOO_MANY_OBJECTS:
305 case AVAHI_ERR_TOO_MANY_ENTRIES:
306 case AVAHI_ERR_ACCESS_DENIED:
307 return kDNSServiceErr_Refused;
309 case AVAHI_ERR_INVALID_OPERATION:
310 case AVAHI_ERR_INVALID_OBJECT:
311 return kDNSServiceErr_Invalid;
313 case AVAHI_ERR_NO_MEMORY:
314 return kDNSServiceErr_NoMemory;
316 case AVAHI_ERR_INVALID_INTERFACE:
317 case AVAHI_ERR_INVALID_PROTOCOL:
318 return kDNSServiceErr_BadInterfaceIndex;
320 case AVAHI_ERR_INVALID_FLAGS:
321 return kDNSServiceErr_BadFlags;
323 case AVAHI_ERR_NOT_FOUND:
324 return kDNSServiceErr_NoSuchName;
326 case AVAHI_ERR_VERSION_MISMATCH:
327 return kDNSServiceErr_Incompatible;
329 case AVAHI_ERR_NO_NETWORK:
331 case AVAHI_ERR_INVALID_CONFIG:
332 case AVAHI_ERR_TIMEOUT:
333 case AVAHI_ERR_DBUS_ERROR:
334 case AVAHI_ERR_NOT_CONNECTED:
335 case AVAHI_ERR_NO_DAEMON:
340 return kDNSServiceErr_Unknown;
343 static void service_browser_callback(
344 AvahiServiceBrowser *b,
345 AvahiIfIndex interface,
346 AvahiProtocol protocol,
347 AvahiBrowserEvent event,
351 AvahiLookupResultFlags flags,
354 DNSServiceRef sdref = userdata;
360 case AVAHI_BROWSER_NEW:
361 sdref->service_browser_callback(sdref, kDNSServiceFlagsAdd, interface, kDNSServiceErr_NoError, name, type, domain, sdref->context);
364 case AVAHI_BROWSER_REMOVE:
365 sdref->service_browser_callback(sdref, 0, interface, kDNSServiceErr_NoError, name, type, domain, sdref->context);
368 case AVAHI_BROWSER_FAILURE:
369 sdref->service_browser_callback(sdref, 0, interface, kDNSServiceErr_Unknown, name, type, domain, sdref->context);
372 case AVAHI_BROWSER_NOT_FOUND:
373 sdref->service_browser_callback(sdref, 0, interface, kDNSServiceErr_NoSuchName, name, type, domain, sdref->context);
376 case AVAHI_BROWSER_CACHE_EXHAUSTED:
377 case AVAHI_BROWSER_ALL_FOR_NOW:
382 DNSServiceErrorType DNSSD_API DNSServiceBrowse(
383 DNSServiceRef *ret_sdref,
384 DNSServiceFlags flags,
388 DNSServiceBrowseReply callback,
391 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
393 DNSServiceRef sdref = NULL;
394 AvahiIfIndex ifindex;
403 if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0)
404 return kDNSServiceErr_Unsupported;
406 if (!(sdref = sdref_new()))
407 return kDNSServiceErr_Unknown;
409 sdref->context = context;
410 sdref->service_browser_callback = callback;
412 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
414 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), NULL, NULL, &error))) {
415 ret = map_error(error);
419 ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
421 if (!(sdref->service_browser = avahi_service_browser_new(sdref->client, ifindex, AVAHI_PROTO_UNSPEC, regtype, domain, 0, service_browser_callback, sdref))) {
422 ret = map_error(avahi_client_errno(sdref->client));
427 ret = kDNSServiceErr_NoError;
432 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
434 if (ret != kDNSServiceErr_NoError)
435 DNSServiceRefDeallocate(sdref);
440 static void service_resolver_callback(
441 AvahiServiceResolver *r,
442 AvahiIfIndex interface,
443 AvahiProtocol protocol,
444 AvahiResolverEvent event,
448 const char *host_name,
449 const AvahiAddress *a,
451 AvahiStringList *txt,
452 AvahiLookupResultFlags flags,
455 DNSServiceRef sdref = userdata;
461 case AVAHI_RESOLVER_FOUND: {
466 if ((p = avahi_new0(char, (l = avahi_string_list_serialize(txt, NULL, 0))+1)))
467 avahi_string_list_serialize(txt, p, l);
469 sdref->service_resolver_callback(sdref, 0, interface, kDNSServiceErr_NoError, "blaa", host_name, htons(port), l, p, sdref->context);
475 case AVAHI_RESOLVER_TIMEOUT:
476 case AVAHI_RESOLVER_NOT_FOUND:
477 sdref->service_resolver_callback(sdref, 0, interface, kDNSServiceErr_NoSuchName, NULL, NULL, 0, 0, NULL, sdref->context);
480 case AVAHI_RESOLVER_FAILURE:
481 sdref->service_resolver_callback(sdref, 0, interface, kDNSServiceErr_Unknown, NULL, NULL, 0, 0, NULL, sdref->context);
486 DNSServiceErrorType DNSSD_API DNSServiceResolve(
487 DNSServiceRef *ret_sdref,
488 DNSServiceFlags flags,
493 DNSServiceResolveReply callback,
496 DNSServiceErrorType ret = kDNSServiceErr_Unknown;
498 DNSServiceRef sdref = NULL;
499 AvahiIfIndex ifindex;
509 if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0)
510 return kDNSServiceErr_Unsupported;
512 if (!(sdref = sdref_new()))
513 return kDNSServiceErr_Unknown;
515 sdref->context = context;
516 sdref->service_resolver_callback = callback;
518 ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
520 if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), NULL, NULL, &error))) {
521 ret = map_error(error);
525 ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
527 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))) {
528 ret = map_error(avahi_client_errno(sdref->client));
533 ret = kDNSServiceErr_NoError;
538 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
540 if (ret != kDNSServiceErr_NoError)
541 DNSServiceRefDeallocate(sdref);