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
27 /** \file core.h The Avahi Multicast DNS and DNS Service Discovery implmentation. */
29 /** \example publish-service.c Example how to register a DNS-SD
30 * service using an embedded mDNS stack. It behaves like a network
31 * printer registering both an IPP and a BSD LPR service. */
33 /** \example browse-services.c Example how to browse for DNS-SD
34 * services using an embedded mDNS stack. */
36 #include <avahi-common/cdecl.h>
38 #ifndef DOXYGEN_SHOULD_SKIP_THIS
42 /** An mDNS responder object */
43 typedef struct AvahiServer AvahiServer;
45 /** A locally registered DNS resource record */
46 typedef struct AvahiEntry AvahiEntry;
48 /** A group of locally registered DNS RRs */
49 typedef struct AvahiEntryGroup AvahiEntryGroup;
51 #ifndef DOXYGEN_SHOULD_SKIP_THIS
55 #include <avahi-core/rr.h>
56 #include <avahi-common/address.h>
57 #include <avahi-common/alternative.h>
58 #include <avahi-common/error.h>
59 #include <avahi-common/defs.h>
61 #ifndef DOXYGEN_SHOULD_SKIP_THIS
65 /** Flags for server entries */
67 AVAHI_ENTRY_NULL = 0, /**< No special flags */
68 AVAHI_ENTRY_UNIQUE = 1, /**< The RRset is intended to be unique */
69 AVAHI_ENTRY_NOPROBE = 2, /**< Though the RRset is intended to be unique no probes shall be sent */
70 AVAHI_ENTRY_NOANNOUNCE = 4, /**< Do not announce this RR to other hosts */
71 AVAHI_ENTRY_ALLOWMUTIPLE = 8 /**< Allow multiple local records of this type, even if they are intended to be unique */
74 /** Prototype for callback functions which are called whenever the state of an AvahiServer object changes */
75 typedef void (*AvahiServerCallback) (AvahiServer *s, AvahiServerState state, gpointer userdata);
77 /** Prototype for callback functions which are called whenever the state of an AvahiEntryGroup object changes */
78 typedef void (*AvahiEntryGroupCallback) (AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, gpointer userdata);
80 /** Stores configuration options for a server instance */
81 typedef struct AvahiServerConfig {
82 gchar *host_name; /**< Default host name. If left empty defaults to the result of gethostname(2) of the libc */
83 gchar *domain_name; /**< Default domain name. If left empty defaults to .local */
84 gboolean use_ipv4; /**< Enable IPv4 support */
85 gboolean use_ipv6; /**< Enable IPv6 support */
86 gboolean publish_hinfo; /**< Register a HINFO record for the host containing the local OS and CPU type */
87 gboolean publish_addresses; /**< Register A, AAAA and PTR records for all local IP addresses */
88 gboolean publish_workstation; /**< Register a _workstation._tcp service */
89 gboolean publish_domain; /**< Announce the local domain for browsing */
90 gboolean 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. */
91 gboolean 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. */
92 gboolean enable_reflector; /**< Reflect incoming mDNS traffic to all local networks. This allows mDNS based network browsing beyond ethernet borders */
93 gboolean reflect_ipv; /**< if enable_reflector is TRUE, enable/disable reflecting between IPv4 and IPv6 */
96 /** Allocate a new mDNS responder object. */
97 AvahiServer *avahi_server_new(
98 GMainContext *c, /**< The GLIB main loop context to attach to */
99 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. */
100 AvahiServerCallback callback, /**< A callback which is called whenever the state of the server changes */
101 gpointer userdata, /**< An opaque pointer which is passed to the callback function */
104 /** Free an mDNS responder object */
105 void avahi_server_free(AvahiServer* s);
107 /** Fill in default values for a server configuration structure. If you
108 * make use of an AvahiServerConfig structure be sure to initialize
109 * it with this function for the sake of upwards library
110 * compatibility. This call may allocate strings on the heap. To
111 * release this memory make sure to call
112 * avahi_server_config_done(). If you want to replace any strings in
113 * the structure be sure to free the strings filled in by this
114 * function with g_free() first and allocate the replacements with
115 * g_malloc() (or g_strdup()).*/
116 AvahiServerConfig* avahi_server_config_init(
117 AvahiServerConfig *c /**< A structure which shall be filled in */ );
119 /** Make a deep copy of the configuration structure *c to *ret. */
120 AvahiServerConfig* avahi_server_config_copy(
121 AvahiServerConfig *ret /**< destination */,
122 const AvahiServerConfig *c /**< source */);
124 /** Free the data in a server configuration structure. */
125 void avahi_server_config_free(AvahiServerConfig *c);
127 /** Return the currently chosen domain name of the server object. The
128 * return value points to an internally allocated string. Be sure to
129 * make a copy of the string before calling any other library
131 const gchar* avahi_server_get_domain_name(AvahiServer *s);
133 /** Return the currently chosen host name. The return value points to a internally allocated string. */
134 const gchar* avahi_server_get_host_name(AvahiServer *s);
136 /** Return the currently chosen host name as a FQDN ("fully qualified
137 * domain name", i.e. the concatenation of the host and domain
138 * name). The return value points to a internally allocated string. */
139 const gchar* avahi_server_get_host_name_fqdn(AvahiServer *s);
141 /** Change the host name of a running mDNS responder. This will drop
142 all automicatilly generated RRs and readd them with the new
143 name. Since the responder has to probe for the new RRs this function
144 takes some time to take effect altough it returns immediately. This
145 function is intended to be called when a host name conflict is
146 reported using AvahiServerCallback. The caller should readd all user
147 defined RRs too since they otherwise continue to point to the outdated
149 gint avahi_server_set_host_name(AvahiServer *s, const gchar *host_name);
151 /** Change the domain name of a running mDNS responder. The same rules
152 * as with avahi_server_set_host_name() apply. */
153 gint avahi_server_set_domain_name(AvahiServer *s, const gchar *domain_name);
155 /** Return the opaque user data pointer attached to a server object */
156 gpointer avahi_server_get_data(AvahiServer *s);
158 /** Change the opaque user data pointer attached to a server object */
159 void avahi_server_set_data(AvahiServer *s, gpointer userdata);
161 /** Return the current state of the server object */
162 AvahiServerState avahi_server_get_state(AvahiServer *s);
164 /** Iterate through all local entries of the server. (when g is NULL)
165 * or of a specified entry group. At the first call state should point
166 * to a NULL initialized void pointer, That pointer is used to track
167 * the current iteration. It is not safe to call any other
168 * avahi_server_xxx() function during the iteration. If the last entry
169 * has been read, NULL is returned. */
170 const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiEntryGroup *g, void **state);
172 /** Callback prototype for avahi_server_dump() */
173 typedef void (*AvahiDumpCallback)(const gchar *text, gpointer userdata);
175 /** Dump the current server status by calling "callback" for each line. */
176 void avahi_server_dump(AvahiServer *s, AvahiDumpCallback callback, gpointer userdata);
178 /** Create a new entry group. The specified callback function is
179 * called whenever the state of the group changes. Use entry group
180 * objects to keep track of you RRs. Add new RRs to a group using
181 * avahi_server_add_xxx(). Make sure to call avahi_entry_group_commit()
182 * to start the registration process for your RRs */
183 AvahiEntryGroup *avahi_entry_group_new(AvahiServer *s, AvahiEntryGroupCallback callback, gpointer userdata);
185 /** Free an entry group. All RRs assigned to the group are removed from the server */
186 void avahi_entry_group_free(AvahiEntryGroup *g);
188 /** Commit an entry group. This starts the probing and registration process for all RRs in the group */
189 gint avahi_entry_group_commit(AvahiEntryGroup *g);
191 /** Remove all entries from the entry group and reset the state to AVAHI_ENTRY_GROUP_UNCOMMITED. */
192 void avahi_entry_group_reset(AvahiEntryGroup *g);
194 /** Return TRUE if the entry group is empty, i.e. has no records attached. */
195 gboolean avahi_entry_group_is_empty(AvahiEntryGroup *g);
197 /** Return the current state of the specified entry group */
198 AvahiEntryGroupState avahi_entry_group_get_state(AvahiEntryGroup *g);
200 /** Change the opaque user data pointer attached to an entry group object */
201 void avahi_entry_group_set_data(AvahiEntryGroup *g, gpointer userdata);
203 /** Return the opaque user data pointer currently set for the entry group object */
204 gpointer avahi_entry_group_get_data(AvahiEntryGroup *g);
206 /** Add a new resource record to the server. Returns 0 on success, negative otherwise. */
207 gint avahi_server_add(
208 AvahiServer *s, /**< The server object to add this record to */
209 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. */
210 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 */
211 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). */
212 AvahiEntryFlags flags, /**< Special flags for this record */
213 AvahiRecord *r /**< The record to add. This function increases the reference counter of this object. */ );
215 /** Add a PTR RR to the server. See avahi_server_add() for more information. */
216 gint avahi_server_add_ptr(
219 AvahiIfIndex interface,
220 AvahiProtocol protocol,
221 AvahiEntryFlags flags,
222 guint32 ttl, /**< DNS TTL for this record */
223 const gchar *name, /**< PTR record name */
224 const gchar *dest /**< pointer destination */ );
226 /** Add a PTR RR to the server. See avahi_server_add() for more information. */
227 gint avahi_server_add_txt(
230 AvahiIfIndex interface,
231 AvahiProtocol protocol,
232 AvahiEntryFlags flags,
233 guint32 ttl, /**< DNS TTL for this record */
234 const gchar *name, /**< TXT record name */
235 ... /**< Text record data, terminated by NULL */);
237 /** Add a PTR RR to the server. Mostly identical to
238 * avahi_server_add_text but takes a va_list instead of a variable
239 * number of arguments */
240 gint avahi_server_add_txt_va(
243 AvahiIfIndex interface,
244 AvahiProtocol protocol,
245 AvahiEntryFlags flags,
250 /** Add a PTR RR to the server. Mostly identical to
251 * avahi_server_add_text but takes an AvahiStringList record instead of a variable
252 * number of arguments. */
253 gint avahi_server_add_txt_strlst(
256 AvahiIfIndex interface,
257 AvahiProtocol protocol,
258 AvahiEntryFlags flags,
261 AvahiStringList *strlst /**< TXT decord data as a AvahiString. This routine makes a deep copy of this object. */ );
263 /** Add an IP address mapping to the server. This will add both the
264 * host-name-to-address and the reverse mapping to the server. See
265 * avahi_server_add() for more information. If adding one of the RRs
266 * fails, the function returns with an error, but it is not defined if
267 * the other RR is deleted from the server or not. Therefore, you have
268 * to free the AvahiEntryGroup and create a new one before
270 gint avahi_server_add_address(
273 AvahiIfIndex interface,
274 AvahiProtocol protocol,
275 AvahiEntryFlags flags,
279 /** Add an DNS-SD service to the Server. This will add all required
280 * RRs to the server. See avahi_server_add() for more information. If
281 * adding one of the RRs fails, the function returns with an error,
282 * but it is not defined if the other RR is deleted from the server or
283 * not. Therefore, you have to free the AvahiEntryGroup and create a
284 * new one before proceeding. */
285 gint avahi_server_add_service(
288 AvahiIfIndex interface,
289 AvahiProtocol protocol,
290 const gchar *name, /**< Service name, e.g. "Lennart's Files" */
291 const gchar *type, /**< DNS-SD type, e.g. "_http._tcp" */
293 const gchar *host, /**< Host name where this servcie resides, or NULL if on the local host */
294 guint16 port, /**< Port number of the service */
295 ... /**< Text records, terminated by NULL */);
297 /** Mostly identical to avahi_server_add_service(), but takes an va_list for the TXT records. */
298 gint avahi_server_add_service_va(
301 AvahiIfIndex interface,
302 AvahiProtocol protocol,
310 /** Mostly identical to avahi_server_add_service(), but takes an AvahiStringList object for the TXT records. The AvahiStringList object is copied. */
311 gint avahi_server_add_service_strlst(
314 AvahiIfIndex interface,
315 AvahiProtocol protocol,
321 AvahiStringList *strlst);
323 /** The type of DNS server */
325 AVAHI_DNS_SERVER_RESOLVE, /**< Unicast DNS servers for normal resolves (_domain._udp)*/
326 AVAHI_DNS_SERVER_UPDATE /**< Unicast DNS servers for updates (_dns-update._udp)*/
327 } AvahiDNSServerType;
329 /** Publish the specified unicast DNS server address via mDNS. You may
330 * browse for records create this way wit
331 * avahi_dns_server_browser_new(). */
332 gint avahi_server_add_dns_server_address(
335 AvahiIfIndex interface,
336 AvahiProtocol protocol,
338 AvahiDNSServerType type,
339 const AvahiAddress *address,
340 guint16 port /** should be 53 */);
342 /** Similar to avahi_server_add_dns_server_address(), but specify a
343 host name instead of an address. The specified host name should be
344 resolvable via mDNS */
345 gint avahi_server_add_dns_server_name(
348 AvahiIfIndex interface,
349 AvahiProtocol protocol,
351 AvahiDNSServerType type,
353 guint16 port /** should be 53 */);
355 /** A browsing object for arbitrary RRs */
356 typedef struct AvahiRecordBrowser AvahiRecordBrowser;
358 /** Callback prototype for AvahiRecordBrowser events */
359 typedef void (*AvahiRecordBrowserCallback)(
360 AvahiRecordBrowser *b, /**< The AvahiRecordBrowser object that is emitting this callback */
361 AvahiIfIndex interface, /**< Logical OS network interface number the record was found on */
362 AvahiProtocol protocol, /**< Protocol number the record was found. */
363 AvahiBrowserEvent event, /**< Browsing event, either AVAHI_BROWSER_NEW or AVAHI_BROWSER_REMOVE */
364 AvahiRecord *record, /**< The record that was found */
365 gpointer userdata /**< Arbitrary user data passed to avahi_record_browser_new() */ );
367 /** Create a new browsing object for arbitrary RRs */
368 AvahiRecordBrowser *avahi_record_browser_new(
369 AvahiServer *server, /**< The server object to which attach this query */
370 AvahiIfIndex interface, /**< Logical OS interface number where to look for the records, or AVAHI_IF_UNSPEC to look on interfaces */
371 AvahiProtocol protocol, /**< Protocol number to use when looking for the record, or AVAHI_PROTO_UNSPEC to look on all protocols */
372 AvahiKey *key, /**< The search key */
373 AvahiRecordBrowserCallback callback, /**< The callback to call on browsing events */
374 gpointer userdata /**< Arbitrary use suppliable data which is passed to the callback */);
376 /** Free an AvahiRecordBrowser object */
377 void avahi_record_browser_free(AvahiRecordBrowser *b);
379 /** A host name to IP adddress resolver object */
380 typedef struct AvahiHostNameResolver AvahiHostNameResolver;
382 /** Callback prototype for AvahiHostNameResolver events */
383 typedef void (*AvahiHostNameResolverCallback)(
384 AvahiHostNameResolver *r,
385 AvahiIfIndex interface,
386 AvahiProtocol protocol,
387 AvahiResolverEvent event, /**< Resolving event */
388 const gchar *host_name, /**< Host name which should be resolved. May differ in case from the query */
389 const AvahiAddress *a, /**< The address, or NULL if the host name couldn't be resolved. */
392 /** Create an AvahiHostNameResolver object for resolving a host name to an adddress. See AvahiRecordBrowser for more info on the paramters. */
393 AvahiHostNameResolver *avahi_host_name_resolver_new(
395 AvahiIfIndex interface,
396 AvahiProtocol protocol,
397 const gchar *host_name, /**< The host name to look for */
398 AvahiProtocol aprotocol, /**< The address family of the desired address or AVAHI_PROTO_UNSPEC if doesn't matter. */
399 AvahiHostNameResolverCallback calback,
402 /** Free a AvahiHostNameResolver object */
403 void avahi_host_name_resolver_free(AvahiHostNameResolver *r);
405 /** An IP address to host name resolver object ("reverse lookup") */
406 typedef struct AvahiAddressResolver AvahiAddressResolver;
408 /** Callback prototype for AvahiAddressResolver events */
409 typedef void (*AvahiAddressResolverCallback)(
410 AvahiAddressResolver *r,
411 AvahiIfIndex interface,
412 AvahiProtocol protocol,
413 AvahiResolverEvent event,
414 const AvahiAddress *a,
415 const gchar *host_name, /**< A host name for the specified address, if one was found, i.e. event == AVAHI_RESOLVER_FOUND */
418 /** Create an AvahiAddressResolver object. See AvahiRecordBrowser for more info on the paramters. */
419 AvahiAddressResolver *avahi_address_resolver_new(
421 AvahiIfIndex interface,
422 AvahiProtocol protocol,
423 const AvahiAddress *address,
424 AvahiAddressResolverCallback calback,
427 /** Free an AvahiAddressResolver object */
428 void avahi_address_resolver_free(AvahiAddressResolver *r);
430 /** A local domain browsing object. May be used to enumerate domains used on the local LAN */
431 typedef struct AvahiDomainBrowser AvahiDomainBrowser;
433 /** Callback prototype for AvahiDomainBrowser events */
434 typedef void (*AvahiDomainBrowserCallback)(
435 AvahiDomainBrowser *b,
436 AvahiIfIndex interface,
437 AvahiProtocol protocol,
438 AvahiBrowserEvent event,
442 /** Create a new AvahiDomainBrowser object */
443 AvahiDomainBrowser *avahi_domain_browser_new(
445 AvahiIfIndex interface,
446 AvahiProtocol protocol,
448 AvahiDomainBrowserType type,
449 AvahiDomainBrowserCallback callback,
452 /** Free an AvahiDomainBrowser object */
453 void avahi_domain_browser_free(AvahiDomainBrowser *b);
455 /** A DNS-SD service type browsing object. May be used to enumerate the service types of all available services on the local LAN */
456 typedef struct AvahiServiceTypeBrowser AvahiServiceTypeBrowser;
458 /** Callback prototype for AvahiServiceTypeBrowser events */
459 typedef void (*AvahiServiceTypeBrowserCallback)(
460 AvahiServiceTypeBrowser *b,
461 AvahiIfIndex interface,
462 AvahiProtocol protocol,
463 AvahiBrowserEvent event,
468 /** Create a new AvahiServiceTypeBrowser object. */
469 AvahiServiceTypeBrowser *avahi_service_type_browser_new(
471 AvahiIfIndex interface,
472 AvahiProtocol protocol,
474 AvahiServiceTypeBrowserCallback callback,
477 /** Free an AvahiServiceTypeBrowser object */
478 void avahi_service_type_browser_free(AvahiServiceTypeBrowser *b);
480 /** 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. */
481 typedef struct AvahiServiceBrowser AvahiServiceBrowser;
483 /** Callback prototype for AvahiServiceBrowser events */
484 typedef void (*AvahiServiceBrowserCallback)(
485 AvahiServiceBrowser *b,
486 AvahiIfIndex interface,
487 AvahiProtocol protocol,
488 AvahiBrowserEvent event,
489 const gchar *name /**< Service name, e.g. "Lennart's Files" */,
490 const gchar *type /**< DNS-SD type, e.g. "_http._tcp" */,
491 const gchar *domain /**< Domain of this service, e.g. "local" */,
494 /** Create a new AvahiServiceBrowser object. */
495 AvahiServiceBrowser *avahi_service_browser_new(
497 AvahiIfIndex interface,
498 AvahiProtocol protocol,
499 const gchar *service_type /** DNS-SD service type, e.g. "_http._tcp" */,
501 AvahiServiceBrowserCallback callback,
504 /** Free an AvahiServiceBrowser object */
505 void avahi_service_browser_free(AvahiServiceBrowser *b);
507 /** A DNS-SD service resolver. Use this to retrieve addres, port and TXT data for a DNS-SD service */
508 typedef struct AvahiServiceResolver AvahiServiceResolver;
510 /** Callback prototype for AvahiServiceResolver events */
511 typedef void (*AvahiServiceResolverCallback)(
512 AvahiServiceResolver *r,
513 AvahiIfIndex interface,
514 AvahiProtocol protocol,
515 AvahiResolverEvent event,
516 const gchar *name, /**< Service name */
517 const gchar *type, /**< Service Type */
519 const gchar *host_name, /**< Host name of the service */
520 const AvahiAddress *a, /**< The resolved host name */
521 guint16 port, /**< Service name */
522 AvahiStringList *txt, /**< TXT record data */
525 /** Create a new AvahiServiceResolver object */
526 AvahiServiceResolver *avahi_service_resolver_new(
528 AvahiIfIndex interface,
529 AvahiProtocol protocol,
533 AvahiProtocol aprotocol, /**< Address family of the desired service address. Use AVAHI_PROTO_UNSPEC if you don't care */
534 AvahiServiceResolverCallback calback,
537 /** Free an AvahiServiceResolver object */
538 void avahi_service_resolver_free(AvahiServiceResolver *r);
540 /** A domain service browser object. Use this to browse for
541 * conventional unicast DNS servers which may be used to resolve
542 * conventional domain names */
543 typedef struct AvahiDNSServerBrowser AvahiDNSServerBrowser;
545 /** Callback prototype for AvahiDNSServerBrowser events */
546 typedef void (*AvahiDNSServerBrowserCallback)(
547 AvahiDNSServerBrowser *b,
548 AvahiIfIndex interface,
549 AvahiProtocol protocol,
550 AvahiBrowserEvent event,
551 const gchar *host_name, /**< Host name of the DNS server, probably useless */
552 const AvahiAddress *a, /**< Address of the DNS server */
553 guint16 port, /**< Port number of the DNS servers, probably 53 */
556 /** Create a new AvahiDNSServerBrowser object */
557 AvahiDNSServerBrowser *avahi_dns_server_browser_new(
559 AvahiIfIndex interface,
560 AvahiProtocol protocol,
562 AvahiDNSServerType type,
563 AvahiProtocol aprotocol, /**< Address protocol for the DNS server */
564 AvahiDNSServerBrowserCallback callback,
567 /** Free an AvahiDNSServerBrowser object */
568 void avahi_dns_server_browser_free(AvahiDNSServerBrowser *b);
570 /** Return the last error code */
571 gint avahi_server_errno(AvahiServer *s);
573 #ifndef DOXYGEN_SHOULD_SKIP_THIS