7 This file is part of avahi.
9 avahi is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as
11 published by the Free Software Foundation; either version 2.1 of the
12 License, or (at your option) any later version.
14 avahi is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
17 Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with avahi; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 /** \file core.h The Avahi Multicast DNS and DNS Service Discovery implmentation. */
27 /** \example core-publish-service.c Example how to register a DNS-SD
28 * service using an embedded mDNS stack. It behaves like a network
29 * printer registering both an IPP and a BSD LPR service. */
31 /** \example core-browse-services.c Example how to browse for DNS-SD
32 * services using an embedded mDNS stack. */
34 #include <avahi-common/cdecl.h>
36 #ifndef DOXYGEN_SHOULD_SKIP_THIS
40 /** An mDNS responder object */
41 typedef struct AvahiServer AvahiServer;
43 /** A group of locally registered DNS RRs */
44 typedef struct AvahiSEntryGroup AvahiSEntryGroup;
46 #ifndef DOXYGEN_SHOULD_SKIP_THIS
50 #include <avahi-core/rr.h>
51 #include <avahi-common/address.h>
52 #include <avahi-common/defs.h>
53 #include <avahi-common/watch.h>
55 #ifndef DOXYGEN_SHOULD_SKIP_THIS
59 /** Flags for server entries */
61 AVAHI_ENTRY_NULL = 0, /**< No special flags */
62 AVAHI_ENTRY_UNIQUE = 1, /**< The RRset is intended to be unique */
63 AVAHI_ENTRY_NOPROBE = 2, /**< Though the RRset is intended to be unique no probes shall be sent */
64 AVAHI_ENTRY_NOANNOUNCE = 4, /**< Do not announce this RR to other hosts */
65 AVAHI_ENTRY_ALLOWMUTIPLE = 8 /**< Allow multiple local records of this type, even if they are intended to be unique */
68 /** Prototype for callback functions which are called whenever the state of an AvahiServer object changes */
69 typedef void (*AvahiServerCallback) (AvahiServer *s, AvahiServerState state, void* userdata);
71 /** Prototype for callback functions which are called whenever the state of an AvahiSEntryGroup object changes */
72 typedef void (*AvahiSEntryGroupCallback) (AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, void* userdata);
74 /** Stores configuration options for a server instance */
75 typedef struct AvahiServerConfig {
76 char *host_name; /**< Default host name. If left empty defaults to the result of gethostname(2) of the libc */
77 char *domain_name; /**< Default domain name. If left empty defaults to .local */
78 int use_ipv4; /**< Enable IPv4 support */
79 int use_ipv6; /**< Enable IPv6 support */
80 int publish_hinfo; /**< Register a HINFO record for the host containing the local OS and CPU type */
81 int publish_addresses; /**< Register A, AAAA and PTR records for all local IP addresses */
82 int publish_workstation; /**< Register a _workstation._tcp service */
83 int publish_domain; /**< Announce the local domain for browsing */
84 int check_response_ttl; /**< If enabled the server ignores all incoming responses with IP TTL != 255. Newer versions of the RFC do no longer contain this check, so it is disabled by default. */
85 int use_iff_running; /**< Require IFF_RUNNING on local network interfaces. This is the official way to check for link beat. Unfortunately this doesn't work with all drivers. So bettere leave this off. */
86 int enable_reflector; /**< Reflect incoming mDNS traffic to all local networks. This allows mDNS based network browsing beyond ethernet borders */
87 int reflect_ipv; /**< if enable_reflector is 1, enable/disable reflecting between IPv4 and IPv6 */
90 /** Allocate a new mDNS responder object. */
91 AvahiServer *avahi_server_new(
92 const AvahiPoll *api, /**< The main loop adapter */
93 const AvahiServerConfig *sc, /**< If non-NULL a pointer to a configuration structure for the server. The server makes an internal deep copy of this structure, so you may free it using avahi_server_config_done() immediately after calling this function. */
94 AvahiServerCallback callback, /**< A callback which is called whenever the state of the server changes */
95 void* userdata, /**< An opaque pointer which is passed to the callback function */
98 /** Free an mDNS responder object */
99 void avahi_server_free(AvahiServer* s);
101 /** Fill in default values for a server configuration structure. If you
102 * make use of an AvahiServerConfig structure be sure to initialize
103 * it with this function for the sake of upwards library
104 * compatibility. This call may allocate strings on the heap. To
105 * release this memory make sure to call
106 * avahi_server_config_done(). If you want to replace any strings in
107 * the structure be sure to free the strings filled in by this
108 * function with avahi_free() first and allocate the replacements with
109 * g_malloc() (or g_strdup()).*/
110 AvahiServerConfig* avahi_server_config_init(
111 AvahiServerConfig *c /**< A structure which shall be filled in */ );
113 /** Make a deep copy of the configuration structure *c to *ret. */
114 AvahiServerConfig* avahi_server_config_copy(
115 AvahiServerConfig *ret /**< destination */,
116 const AvahiServerConfig *c /**< source */);
118 /** Free the data in a server configuration structure. */
119 void avahi_server_config_free(AvahiServerConfig *c);
121 /** Return the currently chosen domain name of the server object. The
122 * return value points to an internally allocated string. Be sure to
123 * make a copy of the string before calling any other library
125 const char* avahi_server_get_domain_name(AvahiServer *s);
127 /** Return the currently chosen host name. The return value points to a internally allocated string. */
128 const char* avahi_server_get_host_name(AvahiServer *s);
130 /** Return the currently chosen host name as a FQDN ("fully qualified
131 * domain name", i.e. the concatenation of the host and domain
132 * name). The return value points to a internally allocated string. */
133 const char* avahi_server_get_host_name_fqdn(AvahiServer *s);
135 /** Change the host name of a running mDNS responder. This will drop
136 all automicatilly generated RRs and readd them with the new
137 name. Since the responder has to probe for the new RRs this function
138 takes some time to take effect altough it returns immediately. This
139 function is intended to be called when a host name conflict is
140 reported using AvahiServerCallback. The caller should readd all user
141 defined RRs too since they otherwise continue to point to the outdated
143 int avahi_server_set_host_name(AvahiServer *s, const char *host_name);
145 /** Change the domain name of a running mDNS responder. The same rules
146 * as with avahi_server_set_host_name() apply. */
147 int avahi_server_set_domain_name(AvahiServer *s, const char *domain_name);
149 /** Return the opaque user data pointer attached to a server object */
150 void* avahi_server_get_data(AvahiServer *s);
152 /** Change the opaque user data pointer attached to a server object */
153 void avahi_server_set_data(AvahiServer *s, void* userdata);
155 /** Return the current state of the server object */
156 AvahiServerState avahi_server_get_state(AvahiServer *s);
158 /** Iterate through all local entries of the server. (when g is NULL)
159 * or of a specified entry group. At the first call state should point
160 * to a NULL initialized void pointer, That pointer is used to track
161 * the current iteration. It is not safe to call any other
162 * avahi_server_xxx() function during the iteration. If the last entry
163 * has been read, NULL is returned. */
164 const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiSEntryGroup *g, void **state);
166 /** Callback prototype for avahi_server_dump() */
167 typedef void (*AvahiDumpCallback)(const char *text, void* userdata);
169 /** Dump the current server status by calling "callback" for each line. */
170 int avahi_server_dump(AvahiServer *s, AvahiDumpCallback callback, void* userdata);
172 /** Create a new entry group. The specified callback function is
173 * called whenever the state of the group changes. Use entry group
174 * objects to keep track of you RRs. Add new RRs to a group using
175 * avahi_server_add_xxx(). Make sure to call avahi_s_entry_group_commit()
176 * to start the registration process for your RRs */
177 AvahiSEntryGroup *avahi_s_entry_group_new(AvahiServer *s, AvahiSEntryGroupCallback callback, void* userdata);
179 /** Free an entry group. All RRs assigned to the group are removed from the server */
180 void avahi_s_entry_group_free(AvahiSEntryGroup *g);
182 /** Commit an entry group. This starts the probing and registration process for all RRs in the group */
183 int avahi_s_entry_group_commit(AvahiSEntryGroup *g);
185 /** Remove all entries from the entry group and reset the state to AVAHI_ENTRY_GROUP_UNCOMMITED. */
186 void avahi_s_entry_group_reset(AvahiSEntryGroup *g);
188 /** Return 1 if the entry group is empty, i.e. has no records attached. */
189 int avahi_s_entry_group_is_empty(AvahiSEntryGroup *g);
191 /** Return the current state of the specified entry group */
192 AvahiEntryGroupState avahi_s_entry_group_get_state(AvahiSEntryGroup *g);
194 /** Change the opaque user data pointer attached to an entry group object */
195 void avahi_s_entry_group_set_data(AvahiSEntryGroup *g, void* userdata);
197 /** Return the opaque user data pointer currently set for the entry group object */
198 void* avahi_s_entry_group_get_data(AvahiSEntryGroup *g);
200 /** Add a new resource record to the server. Returns 0 on success, negative otherwise. */
201 int avahi_server_add(
202 AvahiServer *s, /**< The server object to add this record to */
203 AvahiSEntryGroup *g, /**< An entry group object if this new record shall be attached to one, or NULL. If you plan to remove the record sometime later you a required to pass an entry group object here. */
204 AvahiIfIndex interface, /**< A numeric index of a network interface to attach this record to, or AVAHI_IF_UNSPEC to attach this record to all interfaces */
205 AvahiProtocol protocol, /**< A protocol family to attach this record to. One of the AVAHI_PROTO_xxx constants. Use AVAHI_PROTO_UNSPEC to make this record available on all protocols (wich means on both IPv4 and IPv6). */
206 AvahiEntryFlags flags, /**< Special flags for this record */
207 AvahiRecord *r /**< The record to add. This function increases the reference counter of this object. */ );
209 /** Add a PTR RR to the server. See avahi_server_add() for more information. */
210 int avahi_server_add_ptr(
213 AvahiIfIndex interface,
214 AvahiProtocol protocol,
215 AvahiEntryFlags flags,
216 uint32_t ttl, /**< DNS TTL for this record */
217 const char *name, /**< PTR record name */
218 const char *dest /**< pointer destination */ );
220 /** Add a PTR RR to the server. See avahi_server_add() for more information. */
221 int avahi_server_add_txt(
224 AvahiIfIndex interface,
225 AvahiProtocol protocol,
226 AvahiEntryFlags flags,
227 uint32_t ttl, /**< DNS TTL for this record */
228 const char *name, /**< TXT record name */
229 ... /**< Text record data, terminated by NULL */) AVAHI_GCC_SENTINEL;
231 /** Add a PTR RR to the server. Mostly identical to
232 * avahi_server_add_text but takes a va_list instead of a variable
233 * number of arguments */
234 int avahi_server_add_txt_va(
237 AvahiIfIndex interface,
238 AvahiProtocol protocol,
239 AvahiEntryFlags flags,
244 /** Add a PTR RR to the server. Mostly identical to
245 * avahi_server_add_text but takes an AvahiStringList record instead of a variable
246 * number of arguments. */
247 int avahi_server_add_txt_strlst(
250 AvahiIfIndex interface,
251 AvahiProtocol protocol,
252 AvahiEntryFlags flags,
255 AvahiStringList *strlst /**< TXT decord data as a AvahiString. This routine makes a deep copy of this object. */ );
257 /** Add an IP address mapping to the server. This will add both the
258 * host-name-to-address and the reverse mapping to the server. See
259 * avahi_server_add() for more information. If adding one of the RRs
260 * fails, the function returns with an error, but it is not defined if
261 * the other RR is deleted from the server or not. Therefore, you have
262 * to free the AvahiSEntryGroup and create a new one before
264 int avahi_server_add_address(
267 AvahiIfIndex interface,
268 AvahiProtocol protocol,
269 AvahiEntryFlags flags,
273 /** Add an DNS-SD service to the Server. This will add all required
274 * RRs to the server. See avahi_server_add() for more information. If
275 * adding one of the RRs fails, the function returns with an error,
276 * but it is not defined if the other RR is deleted from the server or
277 * not. Therefore, you have to free the AvahiSEntryGroup and create a
278 * new one before proceeding. */
279 int avahi_server_add_service(
282 AvahiIfIndex interface,
283 AvahiProtocol protocol,
284 const char *name, /**< Service name, e.g. "Lennart's Files" */
285 const char *type, /**< DNS-SD type, e.g. "_http._tcp" */
287 const char *host, /**< Host name where this servcie resides, or NULL if on the local host */
288 uint16_t port, /**< Port number of the service */
289 ... /**< Text records, terminated by NULL */) AVAHI_GCC_SENTINEL;
291 /** Mostly identical to avahi_server_add_service(), but takes an va_list for the TXT records. */
292 int avahi_server_add_service_va(
295 AvahiIfIndex interface,
296 AvahiProtocol protocol,
304 /** Mostly identical to avahi_server_add_service(), but takes an AvahiStringList object for the TXT records. The AvahiStringList object is copied. */
305 int avahi_server_add_service_strlst(
308 AvahiIfIndex interface,
309 AvahiProtocol protocol,
315 AvahiStringList *strlst);
317 /** The type of DNS server */
319 AVAHI_DNS_SERVER_RESOLVE, /**< Unicast DNS servers for normal resolves (_domain._udp)*/
320 AVAHI_DNS_SERVER_UPDATE /**< Unicast DNS servers for updates (_dns-update._udp)*/
321 } AvahiDNSServerType;
323 /** Publish the specified unicast DNS server address via mDNS. You may
324 * browse for records create this way wit
325 * avahi_s_dns_server_browser_new(). */
326 int avahi_server_add_dns_server_address(
329 AvahiIfIndex interface,
330 AvahiProtocol protocol,
332 AvahiDNSServerType type,
333 const AvahiAddress *address,
334 uint16_t port /** should be 53 */);
336 /** Similar to avahi_server_add_dns_server_address(), but specify a
337 host name instead of an address. The specified host name should be
338 resolvable via mDNS */
339 int avahi_server_add_dns_server_name(
342 AvahiIfIndex interface,
343 AvahiProtocol protocol,
345 AvahiDNSServerType type,
347 uint16_t port /** should be 53 */);
349 /** A browsing object for arbitrary RRs */
350 typedef struct AvahiSRecordBrowser AvahiSRecordBrowser;
352 /** Callback prototype for AvahiSRecordBrowser events */
353 typedef void (*AvahiSRecordBrowserCallback)(
354 AvahiSRecordBrowser *b, /**< The AvahiSRecordBrowser object that is emitting this callback */
355 AvahiIfIndex interface, /**< Logical OS network interface number the record was found on */
356 AvahiProtocol protocol, /**< Protocol number the record was found. */
357 AvahiBrowserEvent event, /**< Browsing event, either AVAHI_BROWSER_NEW or AVAHI_BROWSER_REMOVE */
358 AvahiRecord *record, /**< The record that was found */
359 void* userdata /**< Arbitrary user data passed to avahi_s_record_browser_new() */ );
361 /** Create a new browsing object for arbitrary RRs */
362 AvahiSRecordBrowser *avahi_s_record_browser_new(
363 AvahiServer *server, /**< The server object to which attach this query */
364 AvahiIfIndex interface, /**< Logical OS interface number where to look for the records, or AVAHI_IF_UNSPEC to look on interfaces */
365 AvahiProtocol protocol, /**< Protocol number to use when looking for the record, or AVAHI_PROTO_UNSPEC to look on all protocols */
366 AvahiKey *key, /**< The search key */
367 AvahiSRecordBrowserCallback callback, /**< The callback to call on browsing events */
368 void* userdata /**< Arbitrary use suppliable data which is passed to the callback */);
370 /** Free an AvahiSRecordBrowser object */
371 void avahi_s_record_browser_free(AvahiSRecordBrowser *b);
373 /** A host name to IP adddress resolver object */
374 typedef struct AvahiSHostNameResolver AvahiSHostNameResolver;
376 /** Callback prototype for AvahiSHostNameResolver events */
377 typedef void (*AvahiSHostNameResolverCallback)(
378 AvahiSHostNameResolver *r,
379 AvahiIfIndex interface,
380 AvahiProtocol protocol,
381 AvahiResolverEvent event, /**< Resolving event */
382 const char *host_name, /**< Host name which should be resolved. May differ in case from the query */
383 const AvahiAddress *a, /**< The address, or NULL if the host name couldn't be resolved. */
386 /** Create an AvahiSHostNameResolver object for resolving a host name to an adddress. See AvahiSRecordBrowser for more info on the paramters. */
387 AvahiSHostNameResolver *avahi_s_host_name_resolver_new(
389 AvahiIfIndex interface,
390 AvahiProtocol protocol,
391 const char *host_name, /**< The host name to look for */
392 AvahiProtocol aprotocol, /**< The address family of the desired address or AVAHI_PROTO_UNSPEC if doesn't matter. */
393 AvahiSHostNameResolverCallback calback,
396 /** Free a AvahiSHostNameResolver object */
397 void avahi_s_host_name_resolver_free(AvahiSHostNameResolver *r);
399 /** An IP address to host name resolver object ("reverse lookup") */
400 typedef struct AvahiSAddressResolver AvahiSAddressResolver;
402 /** Callback prototype for AvahiSAddressResolver events */
403 typedef void (*AvahiSAddressResolverCallback)(
404 AvahiSAddressResolver *r,
405 AvahiIfIndex interface,
406 AvahiProtocol protocol,
407 AvahiResolverEvent event,
408 const AvahiAddress *a,
409 const char *host_name, /**< A host name for the specified address, if one was found, i.e. event == AVAHI_RESOLVER_FOUND */
412 /** Create an AvahiSAddressResolver object. See AvahiSRecordBrowser for more info on the paramters. */
413 AvahiSAddressResolver *avahi_s_address_resolver_new(
415 AvahiIfIndex interface,
416 AvahiProtocol protocol,
417 const AvahiAddress *address,
418 AvahiSAddressResolverCallback calback,
421 /** Free an AvahiSAddressResolver object */
422 void avahi_s_address_resolver_free(AvahiSAddressResolver *r);
424 /** A local domain browsing object. May be used to enumerate domains used on the local LAN */
425 typedef struct AvahiSDomainBrowser AvahiSDomainBrowser;
427 /** Callback prototype for AvahiSDomainBrowser events */
428 typedef void (*AvahiSDomainBrowserCallback)(
429 AvahiSDomainBrowser *b,
430 AvahiIfIndex interface,
431 AvahiProtocol protocol,
432 AvahiBrowserEvent event,
436 /** Create a new AvahiSDomainBrowser object */
437 AvahiSDomainBrowser *avahi_s_domain_browser_new(
439 AvahiIfIndex interface,
440 AvahiProtocol protocol,
442 AvahiDomainBrowserType type,
443 AvahiSDomainBrowserCallback callback,
446 /** Free an AvahiSDomainBrowser object */
447 void avahi_s_domain_browser_free(AvahiSDomainBrowser *b);
449 /** A DNS-SD service type browsing object. May be used to enumerate the service types of all available services on the local LAN */
450 typedef struct AvahiSServiceTypeBrowser AvahiSServiceTypeBrowser;
452 /** Callback prototype for AvahiSServiceTypeBrowser events */
453 typedef void (*AvahiSServiceTypeBrowserCallback)(
454 AvahiSServiceTypeBrowser *b,
455 AvahiIfIndex interface,
456 AvahiProtocol protocol,
457 AvahiBrowserEvent event,
462 /** Create a new AvahiSServiceTypeBrowser object. */
463 AvahiSServiceTypeBrowser *avahi_s_service_type_browser_new(
465 AvahiIfIndex interface,
466 AvahiProtocol protocol,
468 AvahiSServiceTypeBrowserCallback callback,
471 /** Free an AvahiSServiceTypeBrowser object */
472 void avahi_s_service_type_browser_free(AvahiSServiceTypeBrowser *b);
474 /** A DNS-SD service browser. Use this to enumerate available services of a certain kind on the local LAN. Use AvahiSServiceResolver to get specific service data like address and port for a service. */
475 typedef struct AvahiSServiceBrowser AvahiSServiceBrowser;
477 /** Callback prototype for AvahiSServiceBrowser events */
478 typedef void (*AvahiSServiceBrowserCallback)(
479 AvahiSServiceBrowser *b,
480 AvahiIfIndex interface,
481 AvahiProtocol protocol,
482 AvahiBrowserEvent event,
483 const char *name /**< Service name, e.g. "Lennart's Files" */,
484 const char *type /**< DNS-SD type, e.g. "_http._tcp" */,
485 const char *domain /**< Domain of this service, e.g. "local" */,
488 /** Create a new AvahiSServiceBrowser object. */
489 AvahiSServiceBrowser *avahi_s_service_browser_new(
491 AvahiIfIndex interface,
492 AvahiProtocol protocol,
493 const char *service_type /** DNS-SD service type, e.g. "_http._tcp" */,
495 AvahiSServiceBrowserCallback callback,
498 /** Free an AvahiSServiceBrowser object */
499 void avahi_s_service_browser_free(AvahiSServiceBrowser *b);
501 /** A DNS-SD service resolver. Use this to retrieve addres, port and TXT data for a DNS-SD service */
502 typedef struct AvahiSServiceResolver AvahiSServiceResolver;
504 /** Callback prototype for AvahiSServiceResolver events */
505 typedef void (*AvahiSServiceResolverCallback)(
506 AvahiSServiceResolver *r,
507 AvahiIfIndex interface,
508 AvahiProtocol protocol,
509 AvahiResolverEvent event, /**< Is AVAHI_RESOLVER_FOUND when the service was resolved successfully, and everytime it changes. Is AVAHI_RESOLVER_TIMOUT when the service failed to resolve or disappeared. */
510 const char *name, /**< Service name */
511 const char *type, /**< Service Type */
513 const char *host_name, /**< Host name of the service */
514 const AvahiAddress *a, /**< The resolved host name */
515 uint16_t port, /**< Service name */
516 AvahiStringList *txt, /**< TXT record data */
519 /** Create a new AvahiSServiceResolver object. The specified callback function will be called with the resolved service data. */
520 AvahiSServiceResolver *avahi_s_service_resolver_new(
522 AvahiIfIndex interface,
523 AvahiProtocol protocol,
527 AvahiProtocol aprotocol, /**< Address family of the desired service address. Use AVAHI_PROTO_UNSPEC if you don't care */
528 AvahiSServiceResolverCallback calback,
531 /** Free an AvahiSServiceResolver object */
532 void avahi_s_service_resolver_free(AvahiSServiceResolver *r);
534 /** A domain service browser object. Use this to browse for
535 * conventional unicast DNS servers which may be used to resolve
536 * conventional domain names */
537 typedef struct AvahiSDNSServerBrowser AvahiSDNSServerBrowser;
539 /** Callback prototype for AvahiSDNSServerBrowser events */
540 typedef void (*AvahiSDNSServerBrowserCallback)(
541 AvahiSDNSServerBrowser *b,
542 AvahiIfIndex interface,
543 AvahiProtocol protocol,
544 AvahiBrowserEvent event,
545 const char *host_name, /**< Host name of the DNS server, probably useless */
546 const AvahiAddress *a, /**< Address of the DNS server */
547 uint16_t port, /**< Port number of the DNS servers, probably 53 */
550 /** Create a new AvahiSDNSServerBrowser object */
551 AvahiSDNSServerBrowser *avahi_s_dns_server_browser_new(
553 AvahiIfIndex interface,
554 AvahiProtocol protocol,
556 AvahiDNSServerType type,
557 AvahiProtocol aprotocol, /**< Address protocol for the DNS server */
558 AvahiSDNSServerBrowserCallback callback,
561 /** Free an AvahiSDNSServerBrowser object */
562 void avahi_s_dns_server_browser_free(AvahiSDNSServerBrowser *b);
564 /** Return the last error code */
565 int avahi_server_errno(AvahiServer *s);
567 #ifndef DOXYGEN_SHOULD_SKIP_THIS