Apply GNU coding style to objc-mode as well as c-mode
[emacs.git] / lisp / net / zeroconf.el
blobd7330665a5a2404d3c858ec726461d1eced7a5bb
1 ;;; zeroconf.el --- Service browser using Avahi.
3 ;; Copyright (C) 2008-2012 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, hardware
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; This package provides an interface to the Avahi, the zeroconf
26 ;; daemon under GNU/Linux. The communication mean with Avahi is
27 ;; D-Bus.
29 ;; In order to activate this package, you must add the following code
30 ;; into your .emacs:
32 ;; (require 'zeroconf)
33 ;; (zeroconf-init "dns-sd.org")
35 ;; "dns-sd.org" is an example the domain you wish to resolve services
36 ;; for. It can also be nil or "", which means the default local
37 ;; domain "local".
39 ;; The `zeroconf-init' function installs several handlers, which are
40 ;; activated by D-Bus signals sent from the Avahi daemon.
41 ;; Immediately, when a service is added or removed in the domain, a
42 ;; corresponding handler in Emacs is called.
44 ;; Service Discovery
45 ;; -----------------
47 ;; The main purpose of zeroconf is service discovery. This means,
48 ;; that services are detected as soon as they appear or disappear in a
49 ;; given domain. A service is offered by a network device. It is
50 ;; assigned to a service type.
52 ;; In order to see all offered service types of the initialized
53 ;; domain, you can call
55 ;; (zeroconf-list-service-types)
57 ;; Service types are described at <http://www.dns-sd.org/ServiceTypes.html>.
58 ;; Detected services for a given service type, let's say "_ipp._tcp",
59 ;; are listed by
61 ;; (zeroconf-list-services "_ipp._tcp")
63 ;; It is possible to register an own handler (function) to be called
64 ;; when a service has been added or removed in the domain. The
65 ;; service type "_ipp._tcp" is used for printer services supporting
66 ;; the Internet Printing Protocol.
68 ;; (defun my-add-printer (service)
69 ;; (message "Printer `%s' detected" (zeroconf-service-name service)))
71 ;; (defun my-remove-printer (service)
72 ;; (message "Printer `%s' removed" (zeroconf-service-name service)))
74 ;; (zeroconf-service-add-hook "_ipp._tcp" :new 'my-add-printer)
75 ;; (zeroconf-service-add-hook "_ipp._tcp" :removed 'my-remove-printer)
77 ;; There are several functions returning information about a service,
78 ;; see the doc string of `zeroconf-service-add-hook'.
80 ;; Service Publishing
81 ;; ------------------
83 ;; The function `zeroconf-publish-service' publishes a new service to
84 ;; the Avahi daemon. Although the domain, where to the service is
85 ;; published, can be specified by this function, it is usually the
86 ;; default domain "local" (also written as nil or "").
88 ;; (zeroconf-publish-service
89 ;; "Example service" ;; Service name.
90 ;; "_example._tcp" ;; Service type.
91 ;; nil ;; Default domain ("local").
92 ;; nil ;; Default host (concat (getenv "HOST") ".local").
93 ;; 111 ;; Port number of the host, the service is offered.
94 ;; "1.2.3.4" ;; IPv4 address of the host.
95 ;; '("version=1.0" ;; TXT fields describing the service.
96 ;; "abc=456"))
98 ;; The lifetime of a published service is the lifetime of Emacs.
100 ;;; Code:
102 ;; Pacify byte-compiler. D-Bus support in the Emacs core can be
103 ;; disabled with configuration option "--without-dbus". Declare used
104 ;; subroutines and variables of `dbus' therefore.
105 (eval-when-compile
106 (require 'cl))
108 (defvar dbus-debug)
110 (require 'dbus)
112 (defvar zeroconf-debug nil
113 "Write messages during service discovery")
115 (defconst zeroconf-service-avahi "org.freedesktop.Avahi"
116 "The D-Bus name used to talk to Avahi.")
118 (defconst zeroconf-path-avahi "/"
119 "The D-Bus root object path used to talk to Avahi.")
121 (defvar zeroconf-path-avahi-service-type-browser nil
122 "The D-Bus object path used to talk to the Avahi service type browser.")
124 (defvar zeroconf-path-avahi-service-browser-hash (make-hash-table :test 'equal)
125 "The D-Bus object paths used to talk to the Avahi service browser.")
127 (defvar zeroconf-path-avahi-service-resolver-hash (make-hash-table :test 'equal)
128 "The D-Bus object paths used to talk to the Avahi service resolver.")
130 ;; Methods: "Free", "Commit", "Reset", "GetState", "IsEmpty",
131 ;; "AddService", "AddServiceSubtype", "UpdateServiceTxt", "AddAddress"
132 ;; and "AddRecord".
133 ;; Signals: "StateChanged".
134 (defconst zeroconf-interface-avahi-entry-group
135 (concat zeroconf-service-avahi ".EntryGroup")
136 "The D-Bus entry group interface exported by Avahi.")
138 ;; Methods: "GetVersionString", "GetAPIVersion", "GetHostName",
139 ;; "SetHostName", "GetHostNameFqdn", "GetDomainName",
140 ;; "IsNSSSupportAvailable", "GetState", "GetLocalServiceCookie",
141 ;; "GetAlternativeHostName", "GetAlternativeServiceName",
142 ;; "GetNetworkInterfaceNameByIndex", "GetNetworkInterfaceIndexByName",
143 ;; "ResolveHostName", "ResolveAddress", "ResolveService",
144 ;; "EntryGroupNew", "DomainBrowserNew", "ServiceTypeBrowserNew",
145 ;; "ServiceBrowserNew", "ServiceResolverNew", "HostNameResolverNew",
146 ;; "AddressResolverNew" and "RecordBrowserNew".
147 ;; Signals: "StateChanged".
148 (defconst zeroconf-interface-avahi-server
149 (concat zeroconf-service-avahi ".Server")
150 "The D-Bus server interface exported by Avahi.")
152 ;; Methods: "Free".
153 ;; Signals: "ItemNew", "ItemRemove", "CacheExhausted", "AllForNow" and
154 ;; "Failure".
155 (defconst zeroconf-interface-avahi-service-type-browser
156 (concat zeroconf-service-avahi ".ServiceTypeBrowser")
157 "The D-Bus service type browser interface exported by Avahi.")
159 ;; Methods: "Free".
160 ;; Signals: "ItemNew", "ItemRemove", "CacheExhausted", "AllForNow" and
161 ;; "Failure".
162 (defconst zeroconf-interface-avahi-service-browser
163 (concat zeroconf-service-avahi ".ServiceBrowser")
164 "The D-Bus service browser interface exported by Avahi.")
166 ;; Methods: "Free".
167 ;; Available signals are "Found" and "Failure".
168 (defconst zeroconf-interface-avahi-service-resolver
169 (concat zeroconf-service-avahi ".ServiceResolver")
170 "The D-Bus service resolver interface exported by Avahi.")
172 (defconst zeroconf-avahi-interface-unspec -1
173 "Wildcard Avahi interface spec.")
175 (defconst zeroconf-avahi-protocol-unspec -1
176 "Wildcard Avahi protocol spec.")
178 (defconst zeroconf-avahi-protocol-inet4 0
179 "Avahi INET4 address protocol family.")
181 (defconst zeroconf-avahi-protocol-inet6 1
182 "Avahi INET6 address protocol family.")
184 (defconst zeroconf-avahi-domain-unspec ""
185 "Empty Avahi domain.")
187 (defvar zeroconf-avahi-current-domain zeroconf-avahi-domain-unspec
188 "Domain name services are resolved for.")
190 (defconst zeroconf-avahi-flags-unspec 0
191 "No Avahi flags.")
194 ;;; Services retrieval.
196 (defvar zeroconf-services-hash (make-hash-table :test 'equal)
197 "Hash table of discovered Avahi services.
199 The key of an entry is the concatenation of the service name and
200 service type of a discovered service. The value is the service
201 itself. The format of a service is
203 \(INTERFACE PROTOCOL NAME TYPE DOMAIN FLAGS\)
205 The INTERFACE is a number, which represents the network interface
206 the service is located at. The corresponding network interface
207 name, like \"eth0\", can be retrieved with the function
208 `zeroconf-get-interface-name'.
210 PROTOCOL describes the used network protocol family the service
211 can be accessed. `zeroconf-avahi-protocol-inet4' means INET4,
212 `zeroconf-avahi-protocol-inet6' means INET6. An unspecified
213 protocol family is coded with `zeroconf-avahi-protocol-unspec'.
215 NAME is the string the service is known at Avahi. A service can
216 be known under the same name for different service types.
218 Each TYPE stands for a discovered service type of Avahi. The
219 format is described in RFC 2782. It is of the form
221 \"_APPLICATION-PROTOCOL._TRANSPORT-PROTOCOL\".
223 TRANSPORT-PROTOCOL must be either \"tcp\" or \"udp\".
224 APPLICATION-PROTOCOL must be a protocol name as specified in URL
225 `http://www.dns-sd.org/ServiceTypes.html'. Typical service types
226 are \"_workstation._tcp\" or \"_printer._tcp\".
228 DOMAIN is the domain name the service is registered in, like \"local\".
230 FLAGS, an integer, is used inside Avahi. When publishing a
231 service (see `zeroconf-publish-service', the flag 0 is used.")
233 (defvar zeroconf-resolved-services-hash (make-hash-table :test 'equal)
234 "Hash table of resolved Avahi services.
235 The key of an entry is the concatenation of the service name and
236 service type of a resolved service. The value is the service
237 itself. The format of a service is
239 \(INTERFACE PROTOCOL NAME TYPE DOMAIN HOST APROTOCOL ADDRESS PORT TXT FLAGS\)
241 INTERFACE, PROTOCOL, NAME, TYPE, DOMAIN and FLAGS have the same
242 meaning as in `zeroconf-services-hash'.
244 HOST is the host name the service is registered. It is a fully
245 qualified name, i.e., it contains DOMAIN.
247 APROTOCOL stands for the network protocol family ADDRESS is
248 encoded (`zeroconf-avahi-protocol-inet4' means INET4,
249 `zeroconf-avahi-protocol-inet6' means INET6). It can be
250 different from PROTOCOL, when an address resolution has been
251 requested for another protocol family but the default one.
253 ADDRESS is the service address, encoded according to the
254 APROTOCOL network protocol family. PORT is the corresponding
255 port the service can be reached on ADDRESS.
257 TXT is an array of strings, describing additional attributes of
258 the service. Usually, every string is a key=value pair. The
259 supported keys depend on the service type.")
261 (defun zeroconf-list-service-names ()
262 "Returns all discovered Avahi service names as list."
263 (let (result)
264 (maphash
265 (lambda (key value) (add-to-list 'result (zeroconf-service-name value)))
266 zeroconf-services-hash)
267 result))
269 (defun zeroconf-list-service-types ()
270 "Returns all discovered Avahi service types as list."
271 (let (result)
272 (maphash
273 (lambda (key value) (add-to-list 'result (zeroconf-service-type value)))
274 zeroconf-services-hash)
275 result))
277 (defun zeroconf-list-services (type)
278 "Returns all discovered Avahi services for a given service type TYPE.
279 The service type is one of the returned values of
280 `zeroconf-list-service-types'. The return value is a list
281 \(SERVICE1 SERVICE2 ...\). See `zeroconf-services-hash' for the
282 format of SERVICE."
283 (let (result)
284 (maphash
285 (lambda (key value)
286 (when (equal type (zeroconf-service-type value))
287 (add-to-list 'result value)))
288 zeroconf-services-hash)
289 result))
291 (defvar zeroconf-service-added-hooks-hash (make-hash-table :test 'equal)
292 "Hash table of hooks for newly added services.
293 The key of an entry is a service type.")
295 (defvar zeroconf-service-removed-hooks-hash (make-hash-table :test 'equal)
296 "Hash table of hooks for removed services.
297 The key of an entry is a service type.")
299 (defun zeroconf-service-add-hook (type event function)
300 "Add FUNCTION to the hook of service type TYPE.
302 EVENT must be either :new or :removed, indicating whether
303 FUNCTION shall be called when a new service has been newly
304 detected, or removed.
306 FUNCTION must accept one argument SERVICE, which identifies the
307 new service. Initially, when EVENT is :new, FUNCTION is called
308 for all already detected services of service type TYPE.
310 The attributes of SERVICE can be retrieved via the functions
312 `zeroconf-service-interface'
313 `zeroconf-service-protocol'
314 `zeroconf-service-name'
315 `zeroconf-service-type'
316 `zeroconf-service-domain'
317 `zeroconf-service-flags'
318 `zeroconf-service-host'
319 `zeroconf-service-aprotocol'
320 `zeroconf-service-address'
321 `zeroconf-service-port'
322 `zeroconf-service-txt'"
324 (cond
325 ((equal event :new)
326 (let ((l-hook (gethash type zeroconf-service-added-hooks-hash nil)))
327 (add-hook 'l-hook function)
328 (puthash type l-hook zeroconf-service-added-hooks-hash)
329 (dolist (service (zeroconf-list-services type))
330 (funcall function service))))
331 ((equal event :removed)
332 (let ((l-hook (gethash type zeroconf-service-removed-hooks-hash nil)))
333 (add-hook 'l-hook function)
334 (puthash type l-hook zeroconf-service-removed-hooks-hash)))
335 (t (error "EVENT must be either `:new' or `:removed'"))))
337 (defun zeroconf-service-remove-hook (type event function)
338 "Remove FUNCTION from the hook of service type TYPE.
340 EVENT must be either :new or :removed and has to match the event
341 type used when registering FUNCTION."
342 (let* ((table (cond
343 ((equal event :new)
344 zeroconf-service-added-hooks-hash)
345 ((equal event :removed)
346 zeroconf-service-removed-hooks-hash)
347 (t (error "EVENT must be either `:new' or `:removed'"))))
348 (l-hook (gethash type table nil)))
349 (remove-hook 'l-hook function)
350 (if l-hook
351 (puthash type l-hook table)
352 (remhash type table))))
354 (defun zeroconf-get-host ()
355 "Returns the local host name as string."
356 (dbus-call-method
357 :system zeroconf-service-avahi zeroconf-path-avahi
358 zeroconf-interface-avahi-server "GetHostName"))
360 (defun zeroconf-get-domain ()
361 "Returns the domain name as string."
362 (dbus-call-method
363 :system zeroconf-service-avahi zeroconf-path-avahi
364 zeroconf-interface-avahi-server "GetDomainName"))
366 (defun zeroconf-get-host-domain ()
367 "Returns the local host name FQDN as string."
368 (dbus-call-method
369 :system zeroconf-service-avahi zeroconf-path-avahi
370 zeroconf-interface-avahi-server "GetHostNameFqdn"))
372 (defun zeroconf-get-interface-name (number)
373 "Return the interface name of internal interface NUMBER."
374 (dbus-call-method
375 :system zeroconf-service-avahi zeroconf-path-avahi
376 zeroconf-interface-avahi-server "GetNetworkInterfaceNameByIndex"
377 :int32 number))
379 (defun zeroconf-get-interface-number (name)
380 "Return the internal interface number of interface NAME."
381 (dbus-call-method
382 :system zeroconf-service-avahi zeroconf-path-avahi
383 zeroconf-interface-avahi-server "GetNetworkInterfaceIndexByName"
384 name))
386 (defun zeroconf-get-service (name type)
387 "Return the service description of service NAME as list.
388 NAME must be a string. The service must be of service type
389 TYPE. The resulting list has the format
391 \(INTERFACE PROTOCOL NAME TYPE DOMAIN FLAGS\)."
392 ;; Due to the service browser, all known services are kept in
393 ;; `zeroconf-services-hash'.
394 (gethash (concat name "/" type) zeroconf-services-hash nil))
396 (defun zeroconf-resolve-service (service)
397 "Return all service attributes SERVICE as list.
398 NAME must be a string. The service must be of service type
399 TYPE. The resulting list has the format
401 \(INTERFACE PROTOCOL NAME TYPE DOMAIN HOST APROTOCOL ADDRESS PORT TXT FLAGS\)."
402 (let* ((name (zeroconf-service-name service))
403 (type (zeroconf-service-type service))
404 (key (concat name "/" type)))
407 ;; Check whether we know this service already.
408 (gethash key zeroconf-resolved-services-hash nil)
410 ;; Resolve the service. We don't propagate D-Bus errors.
411 (dbus-ignore-errors
412 (let* ((result
413 (dbus-call-method
414 :system zeroconf-service-avahi zeroconf-path-avahi
415 zeroconf-interface-avahi-server "ResolveService"
416 zeroconf-avahi-interface-unspec
417 zeroconf-avahi-protocol-unspec
418 name type
419 zeroconf-avahi-current-domain
420 zeroconf-avahi-protocol-unspec
421 zeroconf-avahi-flags-unspec))
422 (elt (nth 9 result))) ;; TXT.
423 ;; The TXT field has the signature "aay". Transform to "as".
424 (while elt
425 (setcar elt (dbus-byte-array-to-string (car elt)))
426 (setq elt (cdr elt)))
428 (when nil ;; We discard it, no use so far.
429 ;; Register a service resolver.
430 (let ((object-path (zeroconf-register-service-resolver name type)))
431 ;; Register the signals.
432 (dolist (member '("Found" "Failure"))
433 (dbus-register-signal
434 :system zeroconf-service-avahi object-path
435 zeroconf-interface-avahi-service-resolver member
436 'zeroconf-service-resolver-handler)))
439 ;; Return the resolved service.
440 (puthash key result zeroconf-resolved-services-hash))))))
442 (defun zeroconf-service-interface (service)
443 "Return the internal interface number of SERVICE."
444 (nth 0 service))
446 (defun zeroconf-service-protocol (service)
447 "Return the protocol number of SERVICE."
448 (nth 1 service))
450 (defun zeroconf-service-name (service)
451 "Return the service name of SERVICE."
452 (nth 2 service))
454 (defun zeroconf-service-type (service)
455 "Return the type name of SERVICE."
456 (nth 3 service))
458 (defun zeroconf-service-domain (service)
459 "Return the domain name of SERVICE."
460 (nth 4 service))
462 (defun zeroconf-service-flags (service)
463 "Return the flags of SERVICE."
464 (nth 5 service))
466 (defun zeroconf-service-host (service)
467 "Return the host name of SERVICE."
468 (nth 5 (zeroconf-resolve-service service)))
470 (defun zeroconf-service-aprotocol (service)
471 "Return the aprotocol number of SERVICE."
472 (nth 6 (zeroconf-resolve-service service)))
474 (defun zeroconf-service-address (service)
475 "Return the IP address of SERVICE."
476 (nth 7 (zeroconf-resolve-service service)))
478 (defun zeroconf-service-port (service)
479 "Return the port number of SERVICE."
480 (nth 8 (zeroconf-resolve-service service)))
482 (defun zeroconf-service-txt (service)
483 "Return the text strings of SERVICE."
484 (nth 9 (zeroconf-resolve-service service)))
487 ;;; Services signaling.
489 ;; Register for the service type browser. Service registrations will
490 ;; happen in `zeroconf-service-type-browser-handler', when there is an
491 ;; "ItemNew" signal from the service type browser.
492 (defun zeroconf-init (&optional domain)
493 "Instantiate an Avahi service type browser for domain DOMAIN.
494 DOMAIN is a string, like \"dns-sd.org\" or \"local\". When
495 DOMAIN is nil, the local domain is used."
496 (when (and (or (null domain) (stringp domain))
497 (dbus-ping :system zeroconf-service-avahi)
498 (dbus-call-method
499 :system zeroconf-service-avahi zeroconf-path-avahi
500 zeroconf-interface-avahi-server "GetVersionString"))
502 ;; Reset all stored values.
503 (setq zeroconf-path-avahi-service-type-browser nil
504 zeroconf-avahi-current-domain (or domain
505 zeroconf-avahi-domain-unspec))
506 (clrhash zeroconf-path-avahi-service-browser-hash)
507 (clrhash zeroconf-path-avahi-service-resolver-hash)
508 (clrhash zeroconf-services-hash)
509 (clrhash zeroconf-resolved-services-hash)
510 (clrhash zeroconf-service-added-hooks-hash)
511 (clrhash zeroconf-service-removed-hooks-hash)
513 ;; Register a service type browser.
514 (let ((object-path (zeroconf-register-service-type-browser)))
515 ;; Register the signals.
516 (dolist (member '("ItemNew" "ItemRemove" "Failure"))
517 (dbus-register-signal
518 :system zeroconf-service-avahi object-path
519 zeroconf-interface-avahi-service-type-browser member
520 'zeroconf-service-type-browser-handler)))
522 ;; Register state changed signal.
523 (dbus-register-signal
524 :system zeroconf-service-avahi zeroconf-path-avahi
525 zeroconf-interface-avahi-service-type-browser "StateChanged"
526 'zeroconf-service-type-browser-handler)))
528 (defun zeroconf-register-service-type-browser ()
529 "Register a service type browser at the Avahi daemon."
530 (or zeroconf-path-avahi-service-type-browser
531 (setq zeroconf-path-avahi-service-type-browser
532 (dbus-call-method
533 :system zeroconf-service-avahi zeroconf-path-avahi
534 zeroconf-interface-avahi-server "ServiceTypeBrowserNew"
535 zeroconf-avahi-interface-unspec
536 zeroconf-avahi-protocol-unspec
537 zeroconf-avahi-current-domain
538 zeroconf-avahi-flags-unspec))))
540 (defun zeroconf-service-type-browser-handler (&rest val)
541 "Registered service type browser handler at the Avahi daemon."
542 (when zeroconf-debug
543 (message "zeroconf-service-type-browser-handler: %s %S"
544 (dbus-event-member-name last-input-event) val))
545 (cond
546 ((string-equal (dbus-event-member-name last-input-event) "ItemNew")
547 ;; Parameters: (interface protocol type domain flags)
548 ;; Register a service browser.
549 (let ((object-path (zeroconf-register-service-browser (nth-value 2 val))))
550 ;; Register the signals.
551 (dolist (member '("ItemNew" "ItemRemove" "Failure"))
552 (dbus-register-signal
553 :system zeroconf-service-avahi object-path
554 zeroconf-interface-avahi-service-browser member
555 'zeroconf-service-browser-handler))))))
557 (defun zeroconf-register-service-browser (type)
558 "Register a service browser at the Avahi daemon."
559 (or (gethash type zeroconf-path-avahi-service-browser-hash nil)
560 (puthash type
561 (dbus-call-method
562 :system zeroconf-service-avahi zeroconf-path-avahi
563 zeroconf-interface-avahi-server "ServiceBrowserNew"
564 zeroconf-avahi-interface-unspec
565 zeroconf-avahi-protocol-unspec
566 type
567 zeroconf-avahi-current-domain
568 zeroconf-avahi-flags-unspec)
569 zeroconf-path-avahi-service-browser-hash)))
571 (defun zeroconf-service-browser-handler (&rest val)
572 "Registered service browser handler at the Avahi daemon."
573 ;; Parameters: (interface protocol name type domain flags)
574 (when zeroconf-debug
575 (message "zeroconf-service-browser-handler: %s %S"
576 (dbus-event-member-name last-input-event) val))
577 (let* ((name (zeroconf-service-name val))
578 (type (zeroconf-service-type val))
579 (key (concat name "/" type))
580 (ahook (gethash type zeroconf-service-added-hooks-hash nil))
581 (rhook (gethash type zeroconf-service-removed-hooks-hash nil)))
582 (cond
583 ((string-equal (dbus-event-member-name last-input-event) "ItemNew")
584 ;; Add new service.
585 (puthash key val zeroconf-services-hash)
586 (run-hook-with-args 'ahook val))
588 ((string-equal (dbus-event-member-name last-input-event) "ItemRemove")
589 ;; Remove the service.
590 (remhash key zeroconf-services-hash)
591 (remhash key zeroconf-resolved-services-hash)
592 (run-hook-with-args 'rhook val)))))
594 (defun zeroconf-register-service-resolver (name type)
595 "Register a service resolver at the Avahi daemon."
596 (let ((key (concat name "/" type)))
597 (or (gethash key zeroconf-path-avahi-service-resolver-hash nil)
598 (puthash key
599 (dbus-call-method
600 :system zeroconf-service-avahi zeroconf-path-avahi
601 zeroconf-interface-avahi-server "ServiceResolverNew"
602 zeroconf-avahi-interface-unspec
603 zeroconf-avahi-protocol-unspec
604 name type
605 zeroconf-avahi-current-domain
606 zeroconf-avahi-protocol-unspec
607 zeroconf-avahi-flags-unspec)
608 zeroconf-resolved-services-hash))))
610 (defun zeroconf-service-resolver-handler (&rest val)
611 "Registered service resolver handler at the Avahi daemon."
612 ;; Parameters: (interface protocol name type domain host aprotocol
613 ;; address port txt flags)
614 ;; The "TXT" field has the signature "aay". Transform to "as".
615 (let ((elt (nth 9 val)))
616 (while elt
617 (setcar elt (dbus-byte-array-to-string (car elt)))
618 (setq elt (cdr elt))))
619 (when zeroconf-debug
620 (message "zeroconf-service-resolver-handler: %s %S"
621 (dbus-event-member-name last-input-event) val))
622 (cond
623 ;; A new service has been detected. Add it to
624 ;; `zeroconf-resolved-services-hash'.
625 ((string-equal (dbus-event-member-name last-input-event) "Found")
626 (puthash
627 (concat (zeroconf-service-name val) "/" (zeroconf-service-type val))
628 val zeroconf-resolved-services-hash))))
631 ;;; Services publishing.
633 (defun zeroconf-publish-service (name type domain host port address txt)
634 "Publish a service at the Avahi daemon.
635 For the description of arguments, see `zeroconf-resolved-services-hash'."
636 ;; NAME and TYPE must not be empty.
637 (when (zerop (length name))
638 (error "Invalid argument NAME: %s" name))
639 (when (zerop (length type))
640 (error "Invalid argument TYPE: %s" type))
642 ;; Set default values for DOMAIN, HOST and PORT.
643 (when (zerop (length domain))
644 (setq domain (zeroconf-get-domain)))
645 (when (zerop (length host))
646 (setq host (zeroconf-get-host-domain)))
647 (when (null port)
648 (setq port 0))
650 ;; Create an entry in the daemon.
651 (let ((object-path
652 (dbus-call-method
653 :system zeroconf-service-avahi zeroconf-path-avahi
654 zeroconf-interface-avahi-server "EntryGroupNew"))
655 result)
657 ;; The TXT field has the signature "as". Transform to "aay".
658 (dolist (elt txt)
659 (add-to-list 'result (dbus-string-to-byte-array elt)))
661 ;; Add the service.
662 (dbus-call-method
663 :system zeroconf-service-avahi object-path
664 zeroconf-interface-avahi-entry-group "AddService"
665 zeroconf-avahi-interface-unspec
666 zeroconf-avahi-protocol-unspec
667 zeroconf-avahi-flags-unspec
668 name type domain host :uint16 port (append '(:array) result))
670 ;; Add the address.
671 (unless (zerop (length address))
672 (dbus-call-method
673 :system zeroconf-service-avahi object-path
674 zeroconf-interface-avahi-entry-group "AddAddress"
675 zeroconf-avahi-interface-unspec
676 zeroconf-avahi-protocol-unspec
677 zeroconf-avahi-flags-unspec
678 host address))
680 ;; Make it persistent in the daemon.
681 (dbus-call-method
682 :system zeroconf-service-avahi object-path
683 zeroconf-interface-avahi-entry-group "Commit")))
685 (provide 'zeroconf)
687 ;;; zeroconf.el ends here