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