zeroconf.el 25 KB

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