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 locally registered DNS resource record */
44 typedef struct AvahiEntry AvahiEntry;
46 /** A group of locally registered DNS RRs */
47 typedef struct AvahiEntryGroup AvahiEntryGroup;
49 #ifndef DOXYGEN_SHOULD_SKIP_THIS
53 #include <avahi-core/rr.h>
54 #include <avahi-common/address.h>
55 #include <avahi-common/alternative.h>
56 #include <avahi-common/error.h>
57 #include <avahi-common/defs.h>
58 #include <avahi-common/watch.h>
60 #ifndef DOXYGEN_SHOULD_SKIP_THIS
64 /** Flags for server entries */
66 AVAHI_ENTRY_NULL = 0, /**< No special flags */
67 AVAHI_ENTRY_UNIQUE = 1, /**< The RRset is intended to be unique */
68 AVAHI_ENTRY_NOPROBE = 2, /**< Though the RRset is intended to be unique no probes shall be sent */
69 AVAHI_ENTRY_NOANNOUNCE = 4, /**< Do not announce this RR to other hosts */
70 AVAHI_ENTRY_ALLOWMUTIPLE = 8 /**< Allow multiple local records of this type, even if they are intended to be unique */
73 /** Prototype for callback functions which are called whenever the state of an AvahiServer object changes */
74 typedef void (*AvahiServerCallback) (AvahiServer *s, AvahiServerState state, void* userdata);
76 /** Prototype for callback functions which are called whenever the state of an AvahiEntryGroup object changes */
77 typedef void (*AvahiEntryGroupCallback) (AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, void* userdata);
79 /** Stores configuration options for a server instance */
80 typedef struct AvahiServerConfig {
81 char *host_name; /**< Default host name. If left empty defaults to the result of gethostname(2) of the libc */
82 char *domain_name; /**< Default domain name. If left empty defaults to .local */
83 int use_ipv4; /**< Enable IPv4 support */
84 int use_ipv6; /**< Enable IPv6 support */
85 int publish_hinfo; /**< Register a HINFO record for the host containing the local OS and CPU type */
86 int publish_addresses; /**< Register A, AAAA and PTR records for all local IP addresses */
87 int publish_workstation; /**< Register a _workstation._tcp service */
88 int publish_domain; /**< Announce the local domain for browsing */
89 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. */
90 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. */
91 int enable_reflector; /**< Reflect incoming mDNS traffic to all local networks. This allows mDNS based network browsing beyond ethernet borders */
92 int reflect_ipv; /**< if enable_reflector is 1, enable/disable reflecting between IPv4 and IPv6 */
95 /** Allocate a new mDNS responder object. */
96 AvahiServer *avahi_server_new(
97 AvahiPoll *api, /**< The main loop adapter */
98 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. */
99 AvahiServerCallback callback, /**< A callback which is called whenever the state of the server changes */
100 void* userdata, /**< An opaque pointer which is passed to the callback function */
103 /** Free an mDNS responder object */
104 void avahi_server_free(AvahiServer* s);
106 /** Fill in default values for a server configuration structure. If you
107 * make use of an AvahiServerConfig structure be sure to initialize
108 * it with this function for the sake of upwards library
109 * compatibility. This call may allocate strings on the heap. To
110 * release this memory make sure to call
111 * avahi_server_config_done(). If you want to replace any strings in
112 * the structure be sure to free the strings filled in by this
113 * function with avahi_free() first and allocate the replacements with
114 * g_malloc() (or g_strdup()).*/
115 AvahiServerConfig* avahi_server_config_init(
116 AvahiServerConfig *c /**< A structure which shall be filled in */ );
118 /** Make a deep copy of the configuration structure *c to *ret. */
119 AvahiServerConfig* avahi_server_config_copy(
120 AvahiServerConfig *ret /**< destination */,
121 const AvahiServerConfig *c /**< source */);
123 /** Free the data in a server configuration structure. */
124 void avahi_server_config_free(AvahiServerConfig *c);
126 /** Return the currently chosen domain name of the server object. The
127 * return value points to an internally allocated string. Be sure to
128 * make a copy of the string before calling any other library
130 const char* avahi_server_get_domain_name(AvahiServer *s);
132 /** Return the currently chosen host name. The return value points to a internally allocated string. */
133 const char* avahi_server_get_host_name(AvahiServer *s);
135 /** Return the currently chosen host name as a FQDN ("fully qualified
136 * domain name", i.e. the concatenation of the host and domain
137 * name). The return value points to a internally allocated string. */
138 const char* avahi_server_get_host_name_fqdn(AvahiServer *s);
140 /** Change the host name of a running mDNS responder. This will drop
141 all automicatilly generated RRs and readd them with the new
142 name. Since the responder has to probe for the new RRs this function
143 takes some time to take effect altough it returns immediately. This
144 function is intended to be called when a host name conflict is
145 reported using AvahiServerCallback. The caller should readd all user
146 defined RRs too since they otherwise continue to point to the outdated
148 int avahi_server_set_host_name(AvahiServer *s, const char *host_name);
150 /** Change the domain name of a running mDNS responder. The same rules
151 * as with avahi_server_set_host_name() apply. */
152 int avahi_server_set_domain_name(AvahiServer *s, const char *domain_name);
154 /** Return the opaque user data pointer attached to a server object */
155 void* avahi_server_get_data(AvahiServer *s);
157 /** Change the opaque user data pointer attached to a server object */
158 void avahi_server_set_data(AvahiServer *s, void* userdata);
160 /** Return the current state of the server object */
161 AvahiServerState avahi_server_get_state(AvahiServer *s);
163 /** Iterate through all local entries of the server. (when g is NULL)
164 * or of a specified entry group. At the first call state should point
165 * to a NULL initialized void pointer, That pointer is used to track
166 * the current iteration. It is not safe to call any other
167 * avahi_server_xxx() function during the iteration. If the last entry
168 * has been read, NULL is returned. */
169 const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiEntryGroup *g, void **state);
171 /** Callback prototype for avahi_server_dump() */
172 typedef void (*AvahiDumpCallback)(const char *text, void* userdata);
174 /** Dump the current server status by calling "callback" for each line. */
175 int avahi_server_dump(AvahiServer *s, AvahiDumpCallback callback, void* userdata);
177 /** Create a new entry group. The specified callback function is
178 * called whenever the state of the group changes. Use entry group
179 * objects to keep track of you RRs. Add new RRs to a group using
180 * avahi_server_add_xxx(). Make sure to call avahi_entry_group_commit()
181 * to start the registration process for your RRs */
182 AvahiEntryGroup *avahi_entry_group_new(AvahiServer *s, AvahiEntryGroupCallback callback, void* userdata);
184 /** Free an entry group. All RRs assigned to the group are removed from the server */
185 void avahi_entry_group_free(AvahiEntryGroup *g);
187 /** Commit an entry group. This starts the probing and registration process for all RRs in the group */
188 int avahi_entry_group_commit(AvahiEntryGroup *g);
190 /** Remove all entries from the entry group and reset the state to AVAHI_ENTRY_GROUP_UNCOMMITED. */
191 void avahi_entry_group_reset(AvahiEntryGroup *g);
193 /** Return 1 if the entry group is empty, i.e. has no records attached. */
194 int avahi_entry_group_is_empty(AvahiEntryGroup *g);
196 /** Return the current state of the specified entry group */
197 AvahiEntryGroupState avahi_entry_group_get_state(AvahiEntryGroup *g);
199 /** Change the opaque user data pointer attached to an entry group object */
200 void avahi_entry_group_set_data(AvahiEntryGroup *g, void* userdata);
202 /** Return the opaque user data pointer currently set for the entry group object */
203 void* avahi_entry_group_get_data(AvahiEntryGroup *g);
205 /** Add a new resource record to the server. Returns 0 on success, negative otherwise. */
206 int avahi_server_add(
207 AvahiServer *s, /**< The server object to add this record to */
208 AvahiEntryGroup *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. */
209 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 */
210 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). */
211 AvahiEntryFlags flags, /**< Special flags for this record */
212 AvahiRecord *r /**< The record to add. This function increases the reference counter of this object. */ );
214 /** Add a PTR RR to the server. See avahi_server_add() for more information. */
215 int avahi_server_add_ptr(
218 AvahiIfIndex interface,
219 AvahiProtocol protocol,
220 AvahiEntryFlags flags,
221 uint32_t ttl, /**< DNS TTL for this record */
222 const char *name, /**< PTR record name */
223 const char *dest /**< pointer destination */ );
225 /** Add a PTR RR to the server. See avahi_server_add() for more information. */
226 int avahi_server_add_txt(
229 AvahiIfIndex interface,
230 AvahiProtocol protocol,
231 AvahiEntryFlags flags,
232 uint32_t ttl, /**< DNS TTL for this record */
233 const char *name, /**< TXT record name */
234 ... /**< Text record data, terminated by NULL */);
236 /** Add a PTR RR to the server. Mostly identical to
237 * avahi_server_add_text but takes a va_list instead of a variable
238 * number of arguments */
239 int avahi_server_add_txt_va(
242 AvahiIfIndex interface,
243 AvahiProtocol protocol,
244 AvahiEntryFlags flags,
249 /** Add a PTR RR to the server. Mostly identical to
250 * avahi_server_add_text but takes an AvahiStringList record instead of a variable
251 * number of arguments. */
252 int avahi_server_add_txt_strlst(
255 AvahiIfIndex interface,
256 AvahiProtocol protocol,
257 AvahiEntryFlags flags,
260 AvahiStringList *strlst /**< TXT decord data as a AvahiString. This routine makes a deep copy of this object. */ );
262 /** Add an IP address mapping to the server. This will add both the
263 * host-name-to-address and the reverse mapping to the server. See
264 * avahi_server_add() for more information. If adding one of the RRs
265 * fails, the function returns with an error, but it is not defined if
266 * the other RR is deleted from the server or not. Therefore, you have
267 * to free the AvahiEntryGroup and create a new one before
269 int avahi_server_add_address(
272 AvahiIfIndex interface,
273 AvahiProtocol protocol,
274 AvahiEntryFlags flags,
278 /** Add an DNS-SD service to the Server. This will add all required
279 * RRs to the server. See avahi_server_add() for more information. If
280 * adding one of the RRs fails, the function returns with an error,
281 * but it is not defined if the other RR is deleted from the server or
282 * not. Therefore, you have to free the AvahiEntryGroup and create a
283 * new one before proceeding. */
284 int avahi_server_add_service(
287 AvahiIfIndex interface,
288 AvahiProtocol protocol,
289 const char *name, /**< Service name, e.g. "Lennart's Files" */
290 const char *type, /**< DNS-SD type, e.g. "_http._tcp" */
292 const char *host, /**< Host name where this servcie resides, or NULL if on the local host */
293 uint16_t port, /**< Port number of the service */
294 ... /**< Text records, terminated by NULL */);
296 /** Mostly identical to avahi_server_add_service(), but takes an va_list for the TXT records. */
297 int avahi_server_add_service_va(
300 AvahiIfIndex interface,
301 AvahiProtocol protocol,
309 /** Mostly identical to avahi_server_add_service(), but takes an AvahiStringList object for the TXT records. The AvahiStringList object is copied. */
310 int avahi_server_add_service_strlst(
313 AvahiIfIndex interface,
314 AvahiProtocol protocol,
320 AvahiStringList *strlst);
322 /** The type of DNS server */
324 AVAHI_DNS_SERVER_RESOLVE, /**< Unicast DNS servers for normal resolves (_domain._udp)*/
325 AVAHI_DNS_SERVER_UPDATE /**< Unicast DNS servers for updates (_dns-update._udp)*/
326 } AvahiDNSServerType;
328 /** Publish the specified unicast DNS server address via mDNS. You may
329 * browse for records create this way wit
330 * avahi_dns_server_browser_new(). */
331 int avahi_server_add_dns_server_address(
334 AvahiIfIndex interface,
335 AvahiProtocol protocol,
337 AvahiDNSServerType type,
338 const AvahiAddress *address,
339 uint16_t port /** should be 53 */);
341 /** Similar to avahi_server_add_dns_server_address(), but specify a
342 host name instead of an address. The specified host name should be
343 resolvable via mDNS */
344 int avahi_server_add_dns_server_name(
347 AvahiIfIndex interface,
348 AvahiProtocol protocol,
350 AvahiDNSServerType type,
352 uint16_t port /** should be 53 */);
354 /** A browsing object for arbitrary RRs */
355 typedef struct AvahiRecordBrowser AvahiRecordBrowser;
357 /** Callback prototype for AvahiRecordBrowser events */
358 typedef void (*AvahiRecordBrowserCallback)(
359 AvahiRecordBrowser *b, /**< The AvahiRecordBrowser object that is emitting this callback */
360 AvahiIfIndex interface, /**< Logical OS network interface number the record was found on */
361 AvahiProtocol protocol, /**< Protocol number the record was found. */
362 AvahiBrowserEvent event, /**< Browsing event, either AVAHI_BROWSER_NEW or AVAHI_BROWSER_REMOVE */
363 AvahiRecord *record, /**< The record that was found */
364 void* userdata /**< Arbitrary user data passed to avahi_record_browser_new() */ );
366 /** Create a new browsing object for arbitrary RRs */
367 AvahiRecordBrowser *avahi_record_browser_new(
368 AvahiServer *server, /**< The server object to which attach this query */
369 AvahiIfIndex interface, /**< Logical OS interface number where to look for the records, or AVAHI_IF_UNSPEC to look on interfaces */
370 AvahiProtocol protocol, /**< Protocol number to use when looking for the record, or AVAHI_PROTO_UNSPEC to look on all protocols */
371 AvahiKey *key, /**< The search key */
372 AvahiRecordBrowserCallback callback, /**< The callback to call on browsing events */
373 void* userdata /**< Arbitrary use suppliable data which is passed to the callback */);
375 /** Free an AvahiRecordBrowser object */
376 void avahi_record_browser_free(AvahiRecordBrowser *b);
378 /** A host name to IP adddress resolver object */
379 typedef struct AvahiHostNameResolver AvahiHostNameResolver;
381 /** Callback prototype for AvahiHostNameResolver events */
382 typedef void (*AvahiHostNameResolverCallback)(
383 AvahiHostNameResolver *r,
384 AvahiIfIndex interface,
385 AvahiProtocol protocol,
386 AvahiResolverEvent event, /**< Resolving event */
387 const char *host_name, /**< Host name which should be resolved. May differ in case from the query */
388 const AvahiAddress *a, /**< The address, or NULL if the host name couldn't be resolved. */
391 /** Create an AvahiHostNameResolver object for resolving a host name to an adddress. See AvahiRecordBrowser for more info on the paramters. */
392 AvahiHostNameResolver *avahi_host_name_resolver_new(
394 AvahiIfIndex interface,
395 AvahiProtocol protocol,
396 const char *host_name, /**< The host name to look for */
397 AvahiProtocol aprotocol, /**< The address family of the desired address or AVAHI_PROTO_UNSPEC if doesn't matter. */
398 AvahiHostNameResolverCallback calback,
401 /** Free a AvahiHostNameResolver object */
402 void avahi_host_name_resolver_free(AvahiHostNameResolver *r);
404 /** An IP address to host name resolver object ("reverse lookup") */
405 typedef struct AvahiAddressResolver AvahiAddressResolver;
407 /** Callback prototype for AvahiAddressResolver events */
408 typedef void (*AvahiAddressResolverCallback)(
409 AvahiAddressResolver *r,
410 AvahiIfIndex interface,
411 AvahiProtocol protocol,
412 AvahiResolverEvent event,
413 const AvahiAddress *a,
414 const char *host_name, /**< A host name for the specified address, if one was found, i.e. event == AVAHI_RESOLVER_FOUND */
417 /** Create an AvahiAddressResolver object. See AvahiRecordBrowser for more info on the paramters. */
418 AvahiAddressResolver *avahi_address_resolver_new(
420 AvahiIfIndex interface,
421 AvahiProtocol protocol,
422 const AvahiAddress *address,
423 AvahiAddressResolverCallback calback,
426 /** Free an AvahiAddressResolver object */
427 void avahi_address_resolver_free(AvahiAddressResolver *r);
429 /** A local domain browsing object. May be used to enumerate domains used on the local LAN */
430 typedef struct AvahiDomainBrowser AvahiDomainBrowser;
432 /** Callback prototype for AvahiDomainBrowser events */
433 typedef void (*AvahiDomainBrowserCallback)(
434 AvahiDomainBrowser *b,
435 AvahiIfIndex interface,
436 AvahiProtocol protocol,
437 AvahiBrowserEvent event,
441 /** Create a new AvahiDomainBrowser object */
442 AvahiDomainBrowser *avahi_domain_browser_new(
444 AvahiIfIndex interface,
445 AvahiProtocol protocol,
447 AvahiDomainBrowserType type,
448 AvahiDomainBrowserCallback callback,
451 /** Free an AvahiDomainBrowser object */
452 void avahi_domain_browser_free(AvahiDomainBrowser *b);
454 /** A DNS-SD service type browsing object. May be used to enumerate the service types of all available services on the local LAN */
455 typedef struct AvahiServiceTypeBrowser AvahiServiceTypeBrowser;
457 /** Callback prototype for AvahiServiceTypeBrowser events */
458 typedef void (*AvahiServiceTypeBrowserCallback)(
459 AvahiServiceTypeBrowser *b,
460 AvahiIfIndex interface,
461 AvahiProtocol protocol,
462 AvahiBrowserEvent event,
467 /** Create a new AvahiServiceTypeBrowser object. */
468 AvahiServiceTypeBrowser *avahi_service_type_browser_new(
470 AvahiIfIndex interface,
471 AvahiProtocol protocol,
473 AvahiServiceTypeBrowserCallback callback,
476 /** Free an AvahiServiceTypeBrowser object */
477 void avahi_service_type_browser_free(AvahiServiceTypeBrowser *b);
479 /** A DNS-SD service browser. Use this to enumerate available services of a certain kind on the local LAN. Use AvahiServiceResolver to get specific service data like address and port for a service. */
480 typedef struct AvahiServiceBrowser AvahiServiceBrowser;
482 /** Callback prototype for AvahiServiceBrowser events */
483 typedef void (*AvahiServiceBrowserCallback)(
484 AvahiServiceBrowser *b,
485 AvahiIfIndex interface,
486 AvahiProtocol protocol,
487 AvahiBrowserEvent event,
488 const char *name /**< Service name, e.g. "Lennart's Files" */,
489 const char *type /**< DNS-SD type, e.g. "_http._tcp" */,
490 const char *domain /**< Domain of this service, e.g. "local" */,
493 /** Create a new AvahiServiceBrowser object. */
494 AvahiServiceBrowser *avahi_service_browser_new(
496 AvahiIfIndex interface,
497 AvahiProtocol protocol,
498 const char *service_type /** DNS-SD service type, e.g. "_http._tcp" */,
500 AvahiServiceBrowserCallback callback,
503 /** Free an AvahiServiceBrowser object */
504 void avahi_service_browser_free(AvahiServiceBrowser *b);
506 /** A DNS-SD service resolver. Use this to retrieve addres, port and TXT data for a DNS-SD service */
507 typedef struct AvahiServiceResolver AvahiServiceResolver;
509 /** Callback prototype for AvahiServiceResolver events */
510 typedef void (*AvahiServiceResolverCallback)(
511 AvahiServiceResolver *r,
512 AvahiIfIndex interface,
513 AvahiProtocol protocol,
514 AvahiResolverEvent event,
515 const char *name, /**< Service name */
516 const char *type, /**< Service Type */
518 const char *host_name, /**< Host name of the service */
519 const AvahiAddress *a, /**< The resolved host name */
520 uint16_t port, /**< Service name */
521 AvahiStringList *txt, /**< TXT record data */
524 /** Create a new AvahiServiceResolver object */
525 AvahiServiceResolver *avahi_service_resolver_new(
527 AvahiIfIndex interface,
528 AvahiProtocol protocol,
532 AvahiProtocol aprotocol, /**< Address family of the desired service address. Use AVAHI_PROTO_UNSPEC if you don't care */
533 AvahiServiceResolverCallback calback,
536 /** Free an AvahiServiceResolver object */
537 void avahi_service_resolver_free(AvahiServiceResolver *r);
539 /** A domain service browser object. Use this to browse for
540 * conventional unicast DNS servers which may be used to resolve
541 * conventional domain names */
542 typedef struct AvahiDNSServerBrowser AvahiDNSServerBrowser;
544 /** Callback prototype for AvahiDNSServerBrowser events */
545 typedef void (*AvahiDNSServerBrowserCallback)(
546 AvahiDNSServerBrowser *b,
547 AvahiIfIndex interface,
548 AvahiProtocol protocol,
549 AvahiBrowserEvent event,
550 const char *host_name, /**< Host name of the DNS server, probably useless */
551 const AvahiAddress *a, /**< Address of the DNS server */
552 uint16_t port, /**< Port number of the DNS servers, probably 53 */
555 /** Create a new AvahiDNSServerBrowser object */
556 AvahiDNSServerBrowser *avahi_dns_server_browser_new(
558 AvahiIfIndex interface,
559 AvahiProtocol protocol,
561 AvahiDNSServerType type,
562 AvahiProtocol aprotocol, /**< Address protocol for the DNS server */
563 AvahiDNSServerBrowserCallback callback,
566 /** Free an AvahiDNSServerBrowser object */
567 void avahi_dns_server_browser_free(AvahiDNSServerBrowser *b);
569 /** Return the last error code */
570 int avahi_server_errno(AvahiServer *s);
572 #ifndef DOXYGEN_SHOULD_SKIP_THIS