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 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 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/alternative.h>
53 #include <avahi-common/error.h>
54 #include <avahi-common/defs.h>
55 #include <avahi-common/watch.h>
57 #ifndef DOXYGEN_SHOULD_SKIP_THIS
61 /** Flags for server entries */
63 AVAHI_ENTRY_NULL = 0, /**< No special flags */
64 AVAHI_ENTRY_UNIQUE = 1, /**< The RRset is intended to be unique */
65 AVAHI_ENTRY_NOPROBE = 2, /**< Though the RRset is intended to be unique no probes shall be sent */
66 AVAHI_ENTRY_NOANNOUNCE = 4, /**< Do not announce this RR to other hosts */
67 AVAHI_ENTRY_ALLOWMUTIPLE = 8 /**< Allow multiple local records of this type, even if they are intended to be unique */
70 /** Prototype for callback functions which are called whenever the state of an AvahiServer object changes */
71 typedef void (*AvahiServerCallback) (AvahiServer *s, AvahiServerState state, void* userdata);
73 /** Prototype for callback functions which are called whenever the state of an AvahiSEntryGroup object changes */
74 typedef void (*AvahiSEntryGroupCallback) (AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, void* userdata);
76 /** Stores configuration options for a server instance */
77 typedef struct AvahiServerConfig {
78 char *host_name; /**< Default host name. If left empty defaults to the result of gethostname(2) of the libc */
79 char *domain_name; /**< Default domain name. If left empty defaults to .local */
80 int use_ipv4; /**< Enable IPv4 support */
81 int use_ipv6; /**< Enable IPv6 support */
82 int publish_hinfo; /**< Register a HINFO record for the host containing the local OS and CPU type */
83 int publish_addresses; /**< Register A, AAAA and PTR records for all local IP addresses */
84 int publish_workstation; /**< Register a _workstation._tcp service */
85 int publish_domain; /**< Announce the local domain for browsing */
86 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. */
87 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. */
88 int enable_reflector; /**< Reflect incoming mDNS traffic to all local networks. This allows mDNS based network browsing beyond ethernet borders */
89 int reflect_ipv; /**< if enable_reflector is 1, enable/disable reflecting between IPv4 and IPv6 */
92 /** Allocate a new mDNS responder object. */
93 AvahiServer *avahi_server_new(
94 const AvahiPoll *api, /**< The main loop adapter */
95 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. */
96 AvahiServerCallback callback, /**< A callback which is called whenever the state of the server changes */
97 void* userdata, /**< An opaque pointer which is passed to the callback function */
100 /** Free an mDNS responder object */
101 void avahi_server_free(AvahiServer* s);
103 /** Fill in default values for a server configuration structure. If you
104 * make use of an AvahiServerConfig structure be sure to initialize
105 * it with this function for the sake of upwards library
106 * compatibility. This call may allocate strings on the heap. To
107 * release this memory make sure to call
108 * avahi_server_config_done(). If you want to replace any strings in
109 * the structure be sure to free the strings filled in by this
110 * function with avahi_free() first and allocate the replacements with
111 * g_malloc() (or g_strdup()).*/
112 AvahiServerConfig* avahi_server_config_init(
113 AvahiServerConfig *c /**< A structure which shall be filled in */ );
115 /** Make a deep copy of the configuration structure *c to *ret. */
116 AvahiServerConfig* avahi_server_config_copy(
117 AvahiServerConfig *ret /**< destination */,
118 const AvahiServerConfig *c /**< source */);
120 /** Free the data in a server configuration structure. */
121 void avahi_server_config_free(AvahiServerConfig *c);
123 /** Return the currently chosen domain name of the server object. The
124 * return value points to an internally allocated string. Be sure to
125 * make a copy of the string before calling any other library
127 const char* avahi_server_get_domain_name(AvahiServer *s);
129 /** Return the currently chosen host name. The return value points to a internally allocated string. */
130 const char* avahi_server_get_host_name(AvahiServer *s);
132 /** Return the currently chosen host name as a FQDN ("fully qualified
133 * domain name", i.e. the concatenation of the host and domain
134 * name). The return value points to a internally allocated string. */
135 const char* avahi_server_get_host_name_fqdn(AvahiServer *s);
137 /** Change the host name of a running mDNS responder. This will drop
138 all automicatilly generated RRs and readd them with the new
139 name. Since the responder has to probe for the new RRs this function
140 takes some time to take effect altough it returns immediately. This
141 function is intended to be called when a host name conflict is
142 reported using AvahiServerCallback. The caller should readd all user
143 defined RRs too since they otherwise continue to point to the outdated
145 int avahi_server_set_host_name(AvahiServer *s, const char *host_name);
147 /** Change the domain name of a running mDNS responder. The same rules
148 * as with avahi_server_set_host_name() apply. */
149 int avahi_server_set_domain_name(AvahiServer *s, const char *domain_name);
151 /** Return the opaque user data pointer attached to a server object */
152 void* avahi_server_get_data(AvahiServer *s);
154 /** Change the opaque user data pointer attached to a server object */
155 void avahi_server_set_data(AvahiServer *s, void* userdata);
157 /** Return the current state of the server object */
158 AvahiServerState avahi_server_get_state(AvahiServer *s);
160 /** Iterate through all local entries of the server. (when g is NULL)
161 * or of a specified entry group. At the first call state should point
162 * to a NULL initialized void pointer, That pointer is used to track
163 * the current iteration. It is not safe to call any other
164 * avahi_server_xxx() function during the iteration. If the last entry
165 * has been read, NULL is returned. */
166 const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiSEntryGroup *g, void **state);
168 /** Callback prototype for avahi_server_dump() */
169 typedef void (*AvahiDumpCallback)(const char *text, void* userdata);
171 /** Dump the current server status by calling "callback" for each line. */
172 int avahi_server_dump(AvahiServer *s, AvahiDumpCallback callback, void* userdata);
174 /** Create a new entry group. The specified callback function is
175 * called whenever the state of the group changes. Use entry group
176 * objects to keep track of you RRs. Add new RRs to a group using
177 * avahi_server_add_xxx(). Make sure to call avahi_s_entry_group_commit()
178 * to start the registration process for your RRs */
179 AvahiSEntryGroup *avahi_s_entry_group_new(AvahiServer *s, AvahiSEntryGroupCallback callback, void* userdata);
181 /** Free an entry group. All RRs assigned to the group are removed from the server */
182 void avahi_s_entry_group_free(AvahiSEntryGroup *g);
184 /** Commit an entry group. This starts the probing and registration process for all RRs in the group */
185 int avahi_s_entry_group_commit(AvahiSEntryGroup *g);
187 /** Remove all entries from the entry group and reset the state to AVAHI_ENTRY_GROUP_UNCOMMITED. */
188 void avahi_s_entry_group_reset(AvahiSEntryGroup *g);
190 /** Return 1 if the entry group is empty, i.e. has no records attached. */
191 int avahi_s_entry_group_is_empty(AvahiSEntryGroup *g);
193 /** Return the current state of the specified entry group */
194 AvahiEntryGroupState avahi_s_entry_group_get_state(AvahiSEntryGroup *g);
196 /** Change the opaque user data pointer attached to an entry group object */
197 void avahi_s_entry_group_set_data(AvahiSEntryGroup *g, void* userdata);
199 /** Return the opaque user data pointer currently set for the entry group object */
200 void* avahi_s_entry_group_get_data(AvahiSEntryGroup *g);
202 /** Add a new resource record to the server. Returns 0 on success, negative otherwise. */
203 int avahi_server_add(
204 AvahiServer *s, /**< The server object to add this record to */
205 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. */
206 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 */
207 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). */
208 AvahiEntryFlags flags, /**< Special flags for this record */
209 AvahiRecord *r /**< The record to add. This function increases the reference counter of this object. */ );
211 /** Add a PTR RR to the server. See avahi_server_add() for more information. */
212 int avahi_server_add_ptr(
215 AvahiIfIndex interface,
216 AvahiProtocol protocol,
217 AvahiEntryFlags flags,
218 uint32_t ttl, /**< DNS TTL for this record */
219 const char *name, /**< PTR record name */
220 const char *dest /**< pointer destination */ );
222 /** Add a PTR RR to the server. See avahi_server_add() for more information. */
223 int avahi_server_add_txt(
226 AvahiIfIndex interface,
227 AvahiProtocol protocol,
228 AvahiEntryFlags flags,
229 uint32_t ttl, /**< DNS TTL for this record */
230 const char *name, /**< TXT record name */
231 ... /**< Text record data, terminated by NULL */);
233 /** Add a PTR RR to the server. Mostly identical to
234 * avahi_server_add_text but takes a va_list instead of a variable
235 * number of arguments */
236 int avahi_server_add_txt_va(
239 AvahiIfIndex interface,
240 AvahiProtocol protocol,
241 AvahiEntryFlags flags,
246 /** Add a PTR RR to the server. Mostly identical to
247 * avahi_server_add_text but takes an AvahiStringList record instead of a variable
248 * number of arguments. */
249 int avahi_server_add_txt_strlst(
252 AvahiIfIndex interface,
253 AvahiProtocol protocol,
254 AvahiEntryFlags flags,
257 AvahiStringList *strlst /**< TXT decord data as a AvahiString. This routine makes a deep copy of this object. */ );
259 /** Add an IP address mapping to the server. This will add both the
260 * host-name-to-address and the reverse mapping to the server. See
261 * avahi_server_add() for more information. If adding one of the RRs
262 * fails, the function returns with an error, but it is not defined if
263 * the other RR is deleted from the server or not. Therefore, you have
264 * to free the AvahiSEntryGroup and create a new one before
266 int avahi_server_add_address(
269 AvahiIfIndex interface,
270 AvahiProtocol protocol,
271 AvahiEntryFlags flags,
275 /** Add an DNS-SD service to the Server. This will add all required
276 * RRs to the server. See avahi_server_add() for more information. If
277 * adding one of the RRs fails, the function returns with an error,
278 * but it is not defined if the other RR is deleted from the server or
279 * not. Therefore, you have to free the AvahiSEntryGroup and create a
280 * new one before proceeding. */
281 int avahi_server_add_service(
284 AvahiIfIndex interface,
285 AvahiProtocol protocol,
286 const char *name, /**< Service name, e.g. "Lennart's Files" */
287 const char *type, /**< DNS-SD type, e.g. "_http._tcp" */
289 const char *host, /**< Host name where this servcie resides, or NULL if on the local host */
290 uint16_t port, /**< Port number of the service */
291 ... /**< Text records, terminated by NULL */);
293 /** Mostly identical to avahi_server_add_service(), but takes an va_list for the TXT records. */
294 int avahi_server_add_service_va(
297 AvahiIfIndex interface,
298 AvahiProtocol protocol,
306 /** Mostly identical to avahi_server_add_service(), but takes an AvahiStringList object for the TXT records. The AvahiStringList object is copied. */
307 int avahi_server_add_service_strlst(
310 AvahiIfIndex interface,
311 AvahiProtocol protocol,
317 AvahiStringList *strlst);
319 /** The type of DNS server */
321 AVAHI_DNS_SERVER_RESOLVE, /**< Unicast DNS servers for normal resolves (_domain._udp)*/
322 AVAHI_DNS_SERVER_UPDATE /**< Unicast DNS servers for updates (_dns-update._udp)*/
323 } AvahiDNSServerType;
325 /** Publish the specified unicast DNS server address via mDNS. You may
326 * browse for records create this way wit
327 * avahi_s_dns_server_browser_new(). */
328 int avahi_server_add_dns_server_address(
331 AvahiIfIndex interface,
332 AvahiProtocol protocol,
334 AvahiDNSServerType type,
335 const AvahiAddress *address,
336 uint16_t port /** should be 53 */);
338 /** Similar to avahi_server_add_dns_server_address(), but specify a
339 host name instead of an address. The specified host name should be
340 resolvable via mDNS */
341 int avahi_server_add_dns_server_name(
344 AvahiIfIndex interface,
345 AvahiProtocol protocol,
347 AvahiDNSServerType type,
349 uint16_t port /** should be 53 */);
351 /** A browsing object for arbitrary RRs */
352 typedef struct AvahiSRecordBrowser AvahiSRecordBrowser;
354 /** Callback prototype for AvahiSRecordBrowser events */
355 typedef void (*AvahiSRecordBrowserCallback)(
356 AvahiSRecordBrowser *b, /**< The AvahiSRecordBrowser object that is emitting this callback */
357 AvahiIfIndex interface, /**< Logical OS network interface number the record was found on */
358 AvahiProtocol protocol, /**< Protocol number the record was found. */
359 AvahiBrowserEvent event, /**< Browsing event, either AVAHI_BROWSER_NEW or AVAHI_BROWSER_REMOVE */
360 AvahiRecord *record, /**< The record that was found */
361 void* userdata /**< Arbitrary user data passed to avahi_s_record_browser_new() */ );
363 /** Create a new browsing object for arbitrary RRs */
364 AvahiSRecordBrowser *avahi_s_record_browser_new(
365 AvahiServer *server, /**< The server object to which attach this query */
366 AvahiIfIndex interface, /**< Logical OS interface number where to look for the records, or AVAHI_IF_UNSPEC to look on interfaces */
367 AvahiProtocol protocol, /**< Protocol number to use when looking for the record, or AVAHI_PROTO_UNSPEC to look on all protocols */
368 AvahiKey *key, /**< The search key */
369 AvahiSRecordBrowserCallback callback, /**< The callback to call on browsing events */
370 void* userdata /**< Arbitrary use suppliable data which is passed to the callback */);
372 /** Free an AvahiSRecordBrowser object */
373 void avahi_s_record_browser_free(AvahiSRecordBrowser *b);
375 /** A host name to IP adddress resolver object */
376 typedef struct AvahiSHostNameResolver AvahiSHostNameResolver;
378 /** Callback prototype for AvahiSHostNameResolver events */
379 typedef void (*AvahiSHostNameResolverCallback)(
380 AvahiSHostNameResolver *r,
381 AvahiIfIndex interface,
382 AvahiProtocol protocol,
383 AvahiResolverEvent event, /**< Resolving event */
384 const char *host_name, /**< Host name which should be resolved. May differ in case from the query */
385 const AvahiAddress *a, /**< The address, or NULL if the host name couldn't be resolved. */
388 /** Create an AvahiSHostNameResolver object for resolving a host name to an adddress. See AvahiSRecordBrowser for more info on the paramters. */
389 AvahiSHostNameResolver *avahi_s_host_name_resolver_new(
391 AvahiIfIndex interface,
392 AvahiProtocol protocol,
393 const char *host_name, /**< The host name to look for */
394 AvahiProtocol aprotocol, /**< The address family of the desired address or AVAHI_PROTO_UNSPEC if doesn't matter. */
395 AvahiSHostNameResolverCallback calback,
398 /** Free a AvahiSHostNameResolver object */
399 void avahi_s_host_name_resolver_free(AvahiSHostNameResolver *r);
401 /** An IP address to host name resolver object ("reverse lookup") */
402 typedef struct AvahiSAddressResolver AvahiSAddressResolver;
404 /** Callback prototype for AvahiSAddressResolver events */
405 typedef void (*AvahiSAddressResolverCallback)(
406 AvahiSAddressResolver *r,
407 AvahiIfIndex interface,
408 AvahiProtocol protocol,
409 AvahiResolverEvent event,
410 const AvahiAddress *a,
411 const char *host_name, /**< A host name for the specified address, if one was found, i.e. event == AVAHI_RESOLVER_FOUND */
414 /** Create an AvahiSAddressResolver object. See AvahiSRecordBrowser for more info on the paramters. */
415 AvahiSAddressResolver *avahi_s_address_resolver_new(
417 AvahiIfIndex interface,
418 AvahiProtocol protocol,
419 const AvahiAddress *address,
420 AvahiSAddressResolverCallback calback,
423 /** Free an AvahiSAddressResolver object */
424 void avahi_s_address_resolver_free(AvahiSAddressResolver *r);
426 /** A local domain browsing object. May be used to enumerate domains used on the local LAN */
427 typedef struct AvahiSDomainBrowser AvahiSDomainBrowser;
429 /** Callback prototype for AvahiSDomainBrowser events */
430 typedef void (*AvahiSDomainBrowserCallback)(
431 AvahiSDomainBrowser *b,
432 AvahiIfIndex interface,
433 AvahiProtocol protocol,
434 AvahiBrowserEvent event,
438 /** Create a new AvahiSDomainBrowser object */
439 AvahiSDomainBrowser *avahi_s_domain_browser_new(
441 AvahiIfIndex interface,
442 AvahiProtocol protocol,
444 AvahiDomainBrowserType type,
445 AvahiSDomainBrowserCallback callback,
448 /** Free an AvahiSDomainBrowser object */
449 void avahi_s_domain_browser_free(AvahiSDomainBrowser *b);
451 /** A DNS-SD service type browsing object. May be used to enumerate the service types of all available services on the local LAN */
452 typedef struct AvahiSServiceTypeBrowser AvahiSServiceTypeBrowser;
454 /** Callback prototype for AvahiSServiceTypeBrowser events */
455 typedef void (*AvahiSServiceTypeBrowserCallback)(
456 AvahiSServiceTypeBrowser *b,
457 AvahiIfIndex interface,
458 AvahiProtocol protocol,
459 AvahiBrowserEvent event,
464 /** Create a new AvahiSServiceTypeBrowser object. */
465 AvahiSServiceTypeBrowser *avahi_s_service_type_browser_new(
467 AvahiIfIndex interface,
468 AvahiProtocol protocol,
470 AvahiSServiceTypeBrowserCallback callback,
473 /** Free an AvahiSServiceTypeBrowser object */
474 void avahi_s_service_type_browser_free(AvahiSServiceTypeBrowser *b);
476 /** 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. */
477 typedef struct AvahiSServiceBrowser AvahiSServiceBrowser;
479 /** Callback prototype for AvahiSServiceBrowser events */
480 typedef void (*AvahiSServiceBrowserCallback)(
481 AvahiSServiceBrowser *b,
482 AvahiIfIndex interface,
483 AvahiProtocol protocol,
484 AvahiBrowserEvent event,
485 const char *name /**< Service name, e.g. "Lennart's Files" */,
486 const char *type /**< DNS-SD type, e.g. "_http._tcp" */,
487 const char *domain /**< Domain of this service, e.g. "local" */,
490 /** Create a new AvahiSServiceBrowser object. */
491 AvahiSServiceBrowser *avahi_s_service_browser_new(
493 AvahiIfIndex interface,
494 AvahiProtocol protocol,
495 const char *service_type /** DNS-SD service type, e.g. "_http._tcp" */,
497 AvahiSServiceBrowserCallback callback,
500 /** Free an AvahiSServiceBrowser object */
501 void avahi_s_service_browser_free(AvahiSServiceBrowser *b);
503 /** A DNS-SD service resolver. Use this to retrieve addres, port and TXT data for a DNS-SD service */
504 typedef struct AvahiSServiceResolver AvahiSServiceResolver;
506 /** Callback prototype for AvahiSServiceResolver events */
507 typedef void (*AvahiSServiceResolverCallback)(
508 AvahiSServiceResolver *r,
509 AvahiIfIndex interface,
510 AvahiProtocol protocol,
511 AvahiResolverEvent event,
512 const char *name, /**< Service name */
513 const char *type, /**< Service Type */
515 const char *host_name, /**< Host name of the service */
516 const AvahiAddress *a, /**< The resolved host name */
517 uint16_t port, /**< Service name */
518 AvahiStringList *txt, /**< TXT record data */
521 /** Create a new AvahiSServiceResolver object */
522 AvahiSServiceResolver *avahi_s_service_resolver_new(
524 AvahiIfIndex interface,
525 AvahiProtocol protocol,
529 AvahiProtocol aprotocol, /**< Address family of the desired service address. Use AVAHI_PROTO_UNSPEC if you don't care */
530 AvahiSServiceResolverCallback calback,
533 /** Free an AvahiSServiceResolver object */
534 void avahi_s_service_resolver_free(AvahiSServiceResolver *r);
536 /** A domain service browser object. Use this to browse for
537 * conventional unicast DNS servers which may be used to resolve
538 * conventional domain names */
539 typedef struct AvahiSDNSServerBrowser AvahiSDNSServerBrowser;
541 /** Callback prototype for AvahiSDNSServerBrowser events */
542 typedef void (*AvahiSDNSServerBrowserCallback)(
543 AvahiSDNSServerBrowser *b,
544 AvahiIfIndex interface,
545 AvahiProtocol protocol,
546 AvahiBrowserEvent event,
547 const char *host_name, /**< Host name of the DNS server, probably useless */
548 const AvahiAddress *a, /**< Address of the DNS server */
549 uint16_t port, /**< Port number of the DNS servers, probably 53 */
552 /** Create a new AvahiSDNSServerBrowser object */
553 AvahiSDNSServerBrowser *avahi_s_dns_server_browser_new(
555 AvahiIfIndex interface,
556 AvahiProtocol protocol,
558 AvahiDNSServerType type,
559 AvahiProtocol aprotocol, /**< Address protocol for the DNS server */
560 AvahiSDNSServerBrowserCallback callback,
563 /** Free an AvahiSDNSServerBrowser object */
564 void avahi_s_dns_server_browser_free(AvahiSDNSServerBrowser *b);
566 /** Return the last error code */
567 int avahi_server_errno(AvahiServer *s);
569 #ifndef DOXYGEN_SHOULD_SKIP_THIS