2 NICTA Public Software Licence
5 Copyright © 2004 National ICT Australia Ltd
9 By this licence, National ICT Australia Ltd (NICTA) grants permission,
10 free of charge, to any person who obtains a copy of this software
11 and any associated documentation files ("the Software") to use and
12 deal with the Software in source code and binary forms without
13 restriction, with or without modification, and to permit persons
14 to whom the Software is furnished to do so, provided that the
15 following conditions are met:
17 - Redistributions of source code must retain the above copyright
18 notice, this list of conditions and the following disclaimers.
19 - Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimers in
21 the documentation and/or other materials provided with the
23 - The name of NICTA may not be used to endorse or promote products
24 derived from this Software without specific prior written permission.
26 EXCEPT AS EXPRESSLY STATED IN THIS LICENCE AND TO THE FULL EXTENT
27 PERMITTED BY APPLICABLE LAW, THE SOFTWARE IS PROVIDED "AS-IS" AND
28 NICTA MAKES NO REPRESENTATIONS, WARRANTIES OR CONDITIONS OF ANY
29 KIND, EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY
30 REPRESENTATIONS, WARRANTIES OR CONDITIONS REGARDING THE CONTENTS
31 OR ACCURACY OF THE SOFTWARE, OR OF TITLE, MERCHANTABILITY, FITNESS
32 FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, THE ABSENCE OF LATENT
33 OR OTHER DEFECTS, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR
36 TO THE FULL EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT WILL
37 NICTA BE LIABLE ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION,
38 NEGLIGENCE) FOR ANY LOSS OR DAMAGE WHATSOEVER, INCLUDING (WITHOUT
39 LIMITATION) LOSS OF PRODUCTION OR OPERATION TIME, LOSS, DAMAGE OR
40 CORRUPTION OF DATA OR RECORDS; OR LOSS OF ANTICIPATED SAVINGS,
41 OPPORTUNITY, REVENUE, PROFIT OR GOODWILL, OR OTHER ECONOMIC LOSS;
42 OR ANY SPECIAL, INCIDENTAL, INDIRECT, CONSEQUENTIAL, PUNITIVE OR
43 EXEMPLARY DAMAGES ARISING OUT OF OR IN CONNECTION WITH THIS LICENCE,
44 THE SOFTWARE OR THE USE OF THE SOFTWARE, EVEN IF NICTA HAS BEEN
45 ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
47 If applicable legislation implies warranties or conditions, or
48 imposes obligations or liability on NICTA in respect of the Software
49 that cannot be wholly or partly excluded, restricted or modified,
50 NICTA's liability is limited, to the full extent permitted by the
51 applicable legislation, at its option, to:
53 a. in the case of goods, any one or more of the following:
54 i. the replacement of the goods or the supply of equivalent goods;
55 ii. the repair of the goods;
56 iii. the payment of the cost of replacing the goods or of acquiring
58 iv. the payment of the cost of having the goods repaired; or
59 b. in the case of services:
60 i. the supplying of the services again; or
61 ii. the payment of the cost of having the services supplied
66 NSSwitch Implementation of mDNS interface.
68 Andrew White (Andrew.White@nicta.com.au)
80 #include <sys/types.h>
82 #include <sys/socket.h>
84 #include <netinet/in.h>
86 #include <arpa/inet.h>
87 #define BIND_8_COMPAT 1
88 #include <arpa/nameser.h>
97 Count the number of dots in a name string.
100 count_dots (const char * name
);
104 Test whether a domain name is local.
107 1 if name ends with ".local" or ".local."
111 islocal (const char * name
);
115 Format an address structure as a string appropriate for DNS reverse (PTR)
116 lookup, based on address type.
120 Prefix length, in bits. When formatting, this will be rounded up
121 to the nearest appropriate size. If -1, assume maximum.
123 Output buffer. Must be long enough to hold largest possible
126 Pointer to (first character of) output buffer,
130 format_reverse_addr (int af
, const void * addr
, int prefixlen
, char * buf
);
134 Format an address structure as a string appropriate for DNS reverse (PTR)
135 lookup for AF_INET. Output is in .in-addr.arpa domain.
139 Prefix length, in bits. When formatting, this will be rounded up
140 to the nearest byte (8). If -1, assume 32.
142 Output buffer. Must be long enough to hold largest possible
143 output. For AF_INET, this is 29 characters (including null).
145 Pointer to (first character of) output buffer,
149 format_reverse_addr_in (
150 const struct in_addr
* addr
,
154 #define DNS_PTR_AF_INET_SIZE 29
157 Format an address structure as a string appropriate for DNS reverse (PTR)
158 lookup for AF_INET6. Output is in .ip6.arpa domain.
162 Prefix length, in bits. When formatting, this will be rounded up
163 to the nearest nibble (4). If -1, assume 128.
165 Output buffer. Must be long enough to hold largest possible
166 output. For AF_INET6, this is 72 characters (including null).
168 Pointer to (first character of) output buffer,
172 format_reverse_addr_in6 (
173 const struct in6_addr
* addr
,
177 #define DNS_PTR_AF_INET6_SIZE 72
181 Compare whether the given dns name has the given domain suffix.
182 A single leading '.' on the name or leading or trailing '.' on the
183 domain is ignored for the purposes of the comparison.
184 Multiple leading or trailing '.'s are an error. Other DNS syntax
185 errors are not checked for. The comparison is case insensitive.
189 0 on failure (no match)
193 cmp_dns_suffix (const char * name
, const char * domain
);
196 CMP_DNS_SUFFIX_SUCCESS
= 1,
197 CMP_DNS_SUFFIX_FAILURE
= 0,
198 CMP_DNS_SUFFIX_BAD_NAME
= 1,
199 CMP_DNS_SUFFIX_BAD_DOMAIN
= -2
202 typedef int ns_type_t
;
203 typedef int ns_class_t
;
206 Convert a DNS resource record (RR) code to an address family (AF) code.
210 resource record type (from nameser.h)
213 Appropriate AF code (from socket.h), or AF_UNSPEC if an appropriate
214 mapping couldn't be determined
217 rr_to_af (ns_type_t rrtype
);
221 Convert an address family (AF) code to a DNS resource record (RR) code.
225 address family code (from socket.h)
227 Appropriate RR code (from nameser.h), or ns_t_invalid if an appropriate
228 mapping couldn't be determined
235 Convert a string to an address family (case insensitive).
238 Matching AF code, or AF_UNSPEC if no match found.
241 str_to_af (const char * str
);
245 Convert a string to an ns_class_t (case insensitive).
248 Matching ns_class_t, or ns_c_invalid if no match found.
251 str_to_ns_class (const char * str
);
255 Convert a string to an ns_type_t (case insensitive).
258 Matching ns_type_t, or ns_t_invalid if no match found.
261 str_to_ns_type (const char * str
);
265 Convert an address family code to a string.
268 String representation of AF,
269 or NULL if address family unrecognised or invalid.
276 Convert an ns_class_t code to a string.
279 String representation of ns_class_t,
280 or NULL if ns_class_t unrecognised or invalid.
283 ns_class_to_str (ns_class_t in
);
287 Convert an ns_type_t code to a string.
290 String representation of ns_type_t,
291 or NULL if ns_type_t unrecognised or invalid.
294 ns_type_to_str (ns_type_t in
);
298 Convert DNS rdata in label format (RFC1034, RFC1035) to a name.
300 On error, partial data is written to name (as much as was successfully
301 processed) and an error code is returned. Errors include a name too
302 long for the buffer and a pointer in the label (which cannot be
307 Rdata formatted as series of labels.
309 Length of rdata buffer.
311 Buffer to store fully qualified result in.
312 By RFC1034 section 3.1, a 255 character buffer (256 characters
313 including null) is long enough for any legal name.
315 Number of characters available in name buffer, not including
319 Length of name buffer (not including trailing null).
321 A return of 0 implies the empty domain.
324 dns_rdata_to_name (const char * rdata
, int rdlen
, char * name
, int name_len
);
327 DNS_RDATA_TO_NAME_BAD_FORMAT
= -1,
328 // Format is broken. Usually because we ran out of data
329 // (according to rdata) before the labels said we should.
330 DNS_RDATA_TO_NAME_TOO_LONG
= -2,
331 // The converted rdata is longer than the name buffer.
332 DNS_RDATA_TO_NAME_PTR
= -3,
333 // The rdata contains a pointer.
336 #define DNS_LABEL_MAXLEN 63
337 // Maximum length of a single DNS label
338 #define DNS_NAME_MAXLEN 256
339 // Maximum length of a DNS name
344 typedef int errcode_t
;
345 // Used for 0 = success, non-zero = error code functions
352 Test whether a domain name is in a domain covered by nss_mdns.
353 The name is assumed to be fully qualified (trailing dot optional);
354 unqualified names will be processed but may return unusual results
355 if the unqualified prefix happens to match a domain suffix.
360 -1 error, check errno
363 config_is_mdns_suffix (const char * name
);
367 Loads all relevant data from configuration file. Other code should
368 rarely need to call this function, since all other public configuration
369 functions do so implicitly. Once loaded, configuration info doesn't
373 0 configuration ready
374 non-zero configuration error code
379 #define ENTNAME hostent
380 #define DATABASE "hosts"
386 #include <sys/types.h>
389 typedef enum nss_status nss_status
;
390 typedef struct hostent hostent
;
393 gethostbyname implementation
402 length of auxillary buffer
409 _nss_mdns_gethostbyname_r (
411 hostent
* result_buf
,
420 gethostbyname2 implementation
431 length of auxillary buffer
438 _nss_mdns_gethostbyname2_r (
441 hostent
* result_buf
,
450 gethostbyaddr implementation
453 address structure to look up
455 length of address structure
463 length of auxillary buffer
470 _nss_mdns_gethostbyaddr_r (
474 hostent
* result_buf
,
483 // Types and Constants
485 const int MDNS_VERBOSE
= 0;
486 // This enables verbose syslog messages
487 // If zero, only "imporant" messages will appear in syslog
489 #define k_hostname_maxlen 256
490 // As per RFC1034 and RFC1035
491 #define k_aliases_max 15
492 #define k_addrs_max 15
494 typedef struct buf_header
496 char hostname
[k_hostname_maxlen
+ 1];
497 char * aliases
[k_aliases_max
+ 1];
498 char * addrs
[k_addrs_max
+ 1];
501 typedef struct result_map
506 buf_header_t
* header
;
511 // Index for addresses - grow from low end
512 // Index points to first empty space
514 // Index for aliases - grow from high end
515 // Index points to lowest entry
520 static const struct timeval
521 k_select_time
= { 0, 500000 };
522 // 0 seconds, 500 milliseconds
528 mdns_gethostbyname2 (
531 hostent
* result_buf
,
540 Lookup name using mDNS server
544 const char * fullname
,
546 result_map_t
* result
550 Lookup address using mDNS server
557 const char * addr_str
,
558 result_map_t
* result
563 Handle incoming MDNS events
566 handle_events (DNSServiceRef sdref
, result_map_t
* result
, const char * str
);
569 // Callback for mdns_lookup operations
570 //DNSServiceQueryRecordReply mdns_lookup_callback;
572 mdns_lookup_callback_t
575 DNSServiceFlags flags
,
576 uint32_t interface_index
,
577 DNSServiceErrorType error_code
,
578 const char *fullname
,
587 mdns_lookup_callback_t mdns_lookup_callback
;
592 result_map_t
* result
,
593 hostent
* result_buf
,
600 const char * fullname
,
601 result_map_t
* result
,
607 add_address_to_buffer (result_map_t
* result
, const void * data
, int len
);
609 add_alias_to_buffer (result_map_t
* result
, const char * data
, int len
);
611 add_hostname_len (result_map_t
* result
, const char * fullname
, int len
);
613 add_hostname_or_alias (result_map_t
* result
, const char * data
, int len
);
616 contains_address (result_map_t
* result
, const void * data
, int len
);
618 contains_alias (result_map_t
* result
, const char * data
);
623 result_map_t
* result
,
630 result_map_t
* result
,
637 // Error code functions
640 set_err (result_map_t
* result
, nss_status status
, int err
, int herr
);
642 static nss_status
set_err_notfound (result_map_t
* result
);
643 static nss_status
set_err_bad_hostname (result_map_t
* result
);
644 static nss_status
set_err_buf_too_small (result_map_t
* result
);
645 static nss_status
set_err_internal_resource_full (result_map_t
* result
);
646 static nss_status
set_err_system (result_map_t
* result
);
647 static nss_status
set_err_mdns_failed (result_map_t
* result
);
648 static nss_status
set_err_success (result_map_t
* result
);
659 _nss_mdns_gethostbyname_r (
661 hostent
* result_buf
,
670 "mdns: Called nss_mdns_gethostbyname with %s",
675 mdns_gethostbyname2 (
676 name
, AF_INET
, result_buf
, buf
, buflen
, errnop
, h_errnop
682 _nss_mdns_gethostbyname2_r (
685 hostent
* result_buf
,
694 "mdns: Called nss_mdns_gethostbyname2 with %s",
699 mdns_gethostbyname2 (
700 name
, af
, result_buf
, buf
, buflen
, errnop
, h_errnop
706 _nss_mdns_gethostbyaddr_r (
710 hostent
* result_buf
,
717 char addr_str
[NI_MAXHOST
+ 1];
721 if (inet_ntop (af
, addr
, addr_str
, NI_MAXHOST
) == NULL
)
723 const char * family
= af_to_str (af
);
730 "mdns: Couldn't covert address, family %d (%s) in nss_mdns_gethostbyaddr: %s",
736 // This address family never applicable to us, so return NOT_FOUND
739 *h_errnop
= HOST_NOT_FOUND
;
740 return NSS_STATUS_NOTFOUND
;
745 "mdns: Called nss_mdns_gethostbyaddr with %s",
751 err_status
= init_result (&result
, result_buf
, buf
, buflen
);
754 *errnop
= err_status
;
755 *h_errnop
= NETDB_INTERNAL
;
756 return NSS_STATUS_TRYAGAIN
;
759 if (is_applicable_addr (&result
, addr
, af
, addr_str
))
763 rv
= mdns_lookup_addr (addr
, len
, af
, addr_str
, &result
);
764 if (rv
== NSS_STATUS_SUCCESS
)
770 // Return current error status (defaults to NOT_FOUND)
772 *errnop
= result
.r_errno
;
773 *h_errnop
= result
.r_h_errno
;
774 return result
.status
;
782 mdns_gethostbyname2 (
785 hostent
* result_buf
,
792 char lookup_name
[k_hostname_maxlen
+ 1];
797 err_status
= init_result (&result
, result_buf
, buf
, buflen
);
800 *errnop
= err_status
;
801 *h_errnop
= NETDB_INTERNAL
;
802 return NSS_STATUS_TRYAGAIN
;
805 if (is_applicable_name (&result
, name
, lookup_name
))
812 "mdns: Local name: %s",
816 rv
= mdns_lookup_name (name
, af
, &result
);
817 if (rv
== NSS_STATUS_SUCCESS
)
823 // Return current error status (defaults to NOT_FOUND)
825 *errnop
= result
.r_errno
;
826 *h_errnop
= result
.r_h_errno
;
827 return result
.status
;
832 Lookup a fully qualified hostname using the default record type
833 for the specified address family.
837 Fully qualified hostname. If not fully qualified the code will
838 still 'work', but the lookup is unlikely to succeed.
840 Either AF_INET or AF_INET6. Other families are not supported.
842 Initialised 'result' data structure.
846 const char * fullname
,
848 result_map_t
* result
851 // Lookup using mDNS.
852 DNSServiceErrorType errcode
;
859 "mdns: Attempting lookup of %s",
866 rrtype
= kDNSServiceType_A
;
867 result
->hostent
->h_length
= 4;
868 // Length of an A record
872 rrtype
= kDNSServiceType_AAAA
;
873 result
->hostent
->h_length
= 16;
874 // Length of an AAAA record
879 "mdns: Unsupported address family %d",
882 return set_err_bad_hostname (result
);
884 result
->hostent
->h_addrtype
= af
;
887 DNSServiceQueryRecord (
889 kDNSServiceFlagsForceMulticast
, // force multicast query
890 kDNSServiceInterfaceIndexAny
, // all interfaces
891 fullname
, // full name to query for
892 rrtype
, // resource record type
893 kDNSServiceClass_IN
, // internet class records
894 mdns_lookup_callback
, // callback
895 result
// Context - result buffer
901 "mdns: Failed to initialise lookup, error %d",
904 return set_err_mdns_failed (result
);
907 status
= handle_events (sdref
, result
, fullname
);
908 DNSServiceRefDeallocate (sdref
);
914 Reverse (PTR) lookup for the specified address.
918 Either a struct in_addr or a struct in6_addr
922 Either AF_INET or AF_INET6. Other families are not supported.
925 Address in format suitable for PTR lookup.
926 AF_INET: a.b.c.d -> d.c.b.a.in-addr.arpa
927 AF_INET6: reverse nibble format, x.x.x...x.ip6.arpa
929 Initialised 'result' data structure.
936 const char * addr_str
,
937 result_map_t
* result
940 DNSServiceErrorType errcode
;
946 "mdns: Attempting lookup of %s",
950 result
->hostent
->h_addrtype
= af
;
951 result
->hostent
->h_length
= addr_len
;
953 // Query address becomes "address" in result.
954 if (! add_address_to_buffer (result
, addr
, addr_len
))
956 return result
->status
;
959 result
->hostent
->h_name
[0] = 0;
962 DNSServiceQueryRecord (
964 kDNSServiceFlagsForceMulticast
, // force multicast query
965 kDNSServiceInterfaceIndexAny
, // all interfaces
966 addr_str
, // address string to query for
967 kDNSServiceType_PTR
, // pointer RRs
968 kDNSServiceClass_IN
, // internet class records
969 mdns_lookup_callback
, // callback
970 result
// Context - result buffer
976 "mdns: Failed to initialise mdns lookup, error %d",
979 return set_err_mdns_failed (result
);
982 status
= handle_events (sdref
, result
, addr_str
);
983 DNSServiceRefDeallocate (sdref
);
989 Wait on result of callback, and process it when it arrives.
995 Initialised 'result' data structure.
997 lookup string, used for status/error reporting.
1000 handle_events (DNSServiceRef sdref
, result_map_t
* result
, const char * str
)
1002 int dns_sd_fd
= DNSServiceRefSockFD(sdref
);
1003 int nfds
= dns_sd_fd
+ 1;
1008 while (! result
->done
)
1011 FD_SET(dns_sd_fd
, &readfds
);
1016 select (nfds
, &readfds
, (fd_set
*)NULL
, (fd_set
*)NULL
, &tv
);
1017 if (select_result
> 0)
1019 if (FD_ISSET(dns_sd_fd
, &readfds
))
1023 "mdns: Reply received for %s",
1026 DNSServiceProcessResult(sdref
);
1030 syslog (LOG_WARNING
,
1031 "mdns: Unexpected return from select on lookup of %s",
1038 // Terminate loop due to timer expiry
1041 "mdns: %s not found - timer expired",
1044 set_err_notfound (result
);
1049 return result
->status
;
1054 Examine incoming data and add to relevant fields in result structure.
1055 This routine is called from DNSServiceProcessResult where appropriate.
1058 mdns_lookup_callback
1060 DNSServiceRef sdref
,
1061 DNSServiceFlags flags
,
1062 uint32_t interface_index
,
1063 DNSServiceErrorType error_code
,
1064 const char *fullname
,
1073 // A single record is received
1075 result_map_t
* result
= (result_map_t
*) context
;
1077 (void)sdref
; // Unused
1078 (void)interface_index
; // Unused
1079 (void)ttl
; // Unused
1081 if (! (flags
& kDNSServiceFlagsMoreComing
) )
1086 if (error_code
== kDNSServiceErr_NoError
)
1088 ns_type_t expected_rr_type
=
1089 af_to_rr (result
->hostent
->h_addrtype
);
1091 // Idiot check class
1092 if (rrclass
!= C_IN
)
1094 syslog (LOG_WARNING
,
1095 "mdns: Received bad RR class: expected %d (%s),"
1096 " got %d (%s), RR type %d (%s)",
1098 ns_class_to_str (C_IN
),
1100 ns_class_to_str (rrclass
),
1102 ns_type_to_str (rrtype
)
1108 if (rrtype
== kDNSServiceType_PTR
)
1110 if (callback_body_ptr (fullname
, result
, rdlen
, rdata
) < 0)
1113 else if (rrtype
== expected_rr_type
)
1116 add_hostname_or_alias (
1128 if (! add_address_to_buffer (result
, rdata
, rdlen
) )
1137 syslog (LOG_WARNING
,
1138 "mdns: Received bad RR type: expected %d (%s),"
1141 ns_type_to_str (expected_rr_type
),
1143 ns_type_to_str (rrtype
)
1148 if (result
->status
!= NSS_STATUS_SUCCESS
)
1149 set_err_success (result
);
1153 // For now, dump message to syslog and continue
1154 syslog (LOG_WARNING
,
1155 "mdns: callback returned error %d",
1163 const char * fullname
,
1164 result_map_t
* result
,
1169 char result_name
[k_hostname_maxlen
+ 1];
1172 // Fullname should be .in-addr.arpa or equivalent, which we're
1173 // not interested in. Ignore it.
1175 rv
= dns_rdata_to_name (rdata
, rdlen
, result_name
, k_hostname_maxlen
);
1178 const char * errmsg
;
1182 case DNS_RDATA_TO_NAME_BAD_FORMAT
:
1183 errmsg
= "mdns: PTR '%s' result badly formatted ('%s...')";
1186 case DNS_RDATA_TO_NAME_TOO_LONG
:
1187 errmsg
= "mdns: PTR '%s' result too long ('%s...')";
1190 case DNS_RDATA_TO_NAME_PTR
:
1191 errmsg
= "mdns: PTR '%s' result contained pointer ('%s...')";
1195 errmsg
= "mdns: PTR '%s' result conversion failed ('%s...')";
1198 syslog (LOG_WARNING
,
1210 "mdns: PTR '%s' resolved to '%s'",
1216 // Data should be a hostname
1218 add_hostname_or_alias (
1234 Add an address to the buffer.
1238 Result structure to write to
1240 Incoming address data buffer
1241 Must be 'int' aligned
1243 Length of data buffer (in bytes)
1244 Must match data alignment
1247 Pointer to start of newly written data,
1249 If address already exists in buffer, returns pointer to that instead.
1252 add_address_to_buffer (result_map_t
* result
, const void * data
, int len
)
1258 if ((temp
= contains_address (result
, data
, len
)))
1263 if (result
->addrs_count
>= k_addrs_max
)
1265 // Not enough addr slots
1266 set_err_internal_resource_full (result
);
1268 "mdns: Internal address buffer full; increase size"
1274 if (len
!= result
->hostent
->h_length
)
1276 syslog (LOG_WARNING
,
1277 "mdns: Unexpected rdata length for address. Expected %d, got %d",
1278 result
->hostent
->h_length
,
1281 // XXX And continue for now.
1284 new_addr
= result
->addr_idx
+ len
;
1286 if (new_addr
> result
->alias_idx
)
1289 set_err_buf_too_small (result
);
1292 "mdns: Ran out of buffer when adding address %d",
1293 result
->addrs_count
+ 1
1298 start
= result
->buffer
+ result
->addr_idx
;
1299 memcpy (start
, data
, len
);
1300 result
->addr_idx
= new_addr
;
1301 result
->header
->addrs
[result
->addrs_count
] = start
;
1302 result
->addrs_count
++;
1303 result
->header
->addrs
[result
->addrs_count
] = NULL
;
1310 contains_address (result_map_t
* result
, const void * data
, int len
)
1315 if (len
!= result
->hostent
->h_length
)
1317 syslog (LOG_WARNING
,
1318 "mdns: Unexpected rdata length for address. Expected %d, got %d",
1319 result
->hostent
->h_length
,
1322 // XXX And continue for now.
1325 for (i
= 0; result
->header
->addrs
[i
]; i
++)
1327 if (memcmp (result
->header
->addrs
[i
], data
, len
) == 0)
1329 return result
->header
->addrs
[i
];
1338 Add an alias to the buffer.
1342 Result structure to write to
1344 Incoming alias (null terminated)
1346 Length of data buffer (in bytes), including trailing null
1349 Pointer to start of newly written data,
1351 If alias already exists in buffer, returns pointer to that instead.
1354 add_alias_to_buffer (result_map_t
* result
, const char * data
, int len
)
1360 if ((temp
= contains_alias (result
, data
)))
1365 if (result
->aliases_count
>= k_aliases_max
)
1367 // Not enough alias slots
1368 set_err_internal_resource_full (result
);
1370 "mdns: Internal alias buffer full; increase size"
1375 new_alias
= result
->alias_idx
- len
;
1377 if (new_alias
< result
->addr_idx
)
1380 set_err_buf_too_small (result
);
1383 "mdns: Ran out of buffer when adding alias %d",
1384 result
->aliases_count
+ 1
1389 start
= result
->buffer
+ new_alias
;
1390 memcpy (start
, data
, len
);
1391 result
->alias_idx
= new_alias
;
1392 result
->header
->aliases
[result
->aliases_count
] = start
;
1393 result
->aliases_count
++;
1394 result
->header
->aliases
[result
->aliases_count
] = NULL
;
1401 contains_alias (result_map_t
* result
, const char * alias
)
1405 for (i
= 0; result
->header
->aliases
[i
]; i
++)
1407 if (strcmp (result
->header
->aliases
[i
], alias
) == 0)
1409 return result
->header
->aliases
[i
];
1418 Add fully qualified hostname to result.
1422 Result structure to write to
1424 Fully qualified hostname
1427 Pointer to start of hostname buffer,
1428 or NULL on error (usually hostname too long)
1432 add_hostname_len (result_map_t
* result
, const char * fullname
, int len
)
1434 if (len
>= k_hostname_maxlen
)
1436 set_err_bad_hostname (result
);
1437 syslog (LOG_WARNING
,
1438 "mdns: Hostname too long '%.*s': len %d, max %d",
1447 result
->hostent
->h_name
=
1448 strcpy (result
->header
->hostname
, fullname
);
1450 return result
->header
->hostname
;
1455 Add fully qualified name as hostname or alias.
1457 If hostname is not fully qualified this is not an error, but the data
1458 returned may be not what the application wanted.
1462 Result structure to write to
1464 Incoming alias (null terminated)
1466 Length of data buffer (in bytes), including trailing null
1469 Pointer to start of newly written data,
1471 If alias or hostname already exists, returns pointer to that instead.
1474 add_hostname_or_alias (result_map_t
* result
, const char * data
, int len
)
1476 char * hostname
= result
->hostent
->h_name
;
1480 if (strcmp (hostname
, data
) == 0)
1486 return add_alias_to_buffer (result
, data
, len
);
1491 return add_hostname_len (result
, data
, len
);
1498 result_map_t
* result
,
1499 hostent
* result_buf
,
1504 if (buflen
< sizeof (buf_header_t
))
1509 result
->hostent
= result_buf
;
1510 result
->header
= (buf_header_t
*) buf
;
1511 result
->header
->hostname
[0] = 0;
1512 result
->aliases_count
= 0;
1513 result
->header
->aliases
[0] = NULL
;
1514 result
->addrs_count
= 0;
1515 result
->header
->addrs
[0] = NULL
;
1516 result
->buffer
= buf
+ sizeof (buf_header_t
);
1517 result
->addr_idx
= 0;
1518 result
->alias_idx
= buflen
- sizeof (buf_header_t
);
1520 set_err_notfound (result
);
1522 // Point hostent to the right buffers
1523 result
->hostent
->h_name
= result
->header
->hostname
;
1524 result
->hostent
->h_aliases
= result
->header
->aliases
;
1525 result
->hostent
->h_addr_list
= result
->header
->addrs
;
1531 Set the status in the result.
1535 Result structure to update
1537 New nss_status value
1547 set_err (result_map_t
* result
, nss_status status
, int err
, int herr
)
1549 result
->status
= status
;
1550 result
->r_errno
= err
;
1551 result
->r_h_errno
= herr
;
1557 set_err_notfound (result_map_t
* result
)
1559 return set_err (result
, NSS_STATUS_NOTFOUND
, ENOENT
, HOST_NOT_FOUND
);
1563 set_err_bad_hostname (result_map_t
* result
)
1565 return set_err (result
, NSS_STATUS_TRYAGAIN
, ENOENT
, NO_RECOVERY
);
1569 set_err_buf_too_small (result_map_t
* result
)
1571 return set_err (result
, NSS_STATUS_TRYAGAIN
, ERANGE
, NETDB_INTERNAL
);
1575 set_err_internal_resource_full (result_map_t
* result
)
1577 return set_err (result
, NSS_STATUS_RETURN
, ERANGE
, NO_RECOVERY
);
1581 set_err_system (result_map_t
* result
)
1583 return set_err (result
, NSS_STATUS_UNAVAIL
, errno
, NETDB_INTERNAL
);
1587 set_err_mdns_failed (result_map_t
* result
)
1589 return set_err (result
, NSS_STATUS_TRYAGAIN
, EAGAIN
, TRY_AGAIN
);
1593 set_err_success (result_map_t
* result
)
1595 result
->status
= NSS_STATUS_SUCCESS
;
1596 return result
->status
;
1601 Test whether name is applicable for mdns to process, and if so copy into
1602 lookup_name buffer (if non-NULL).
1605 Pointer to name to lookup up, if applicable, or NULL otherwise.
1608 is_applicable_name (
1609 result_map_t
* result
,
1614 int match
= config_is_mdns_suffix (name
);
1619 strncpy (lookup_name
, name
, k_hostname_maxlen
+ 1);
1631 set_err_system (result
);
1638 Test whether address is applicable for mdns to process, and if so copy into
1639 addr_str buffer as an address suitable for ptr lookup.
1642 Pointer to name to lookup up, if applicable, or NULL otherwise.
1645 is_applicable_addr (
1646 result_map_t
* result
,
1654 if (! format_reverse_addr (af
, addr
, -1, addr_str
))
1658 "mdns: Failed to create reverse address"
1665 "mdns: Reverse address: %s",
1669 match
= config_is_mdns_suffix (addr_str
);
1678 set_err_system (result
);
1685 // Types and Constants
1687 const char * k_conf_file
= "/etc/nss_mdns.conf";
1688 #define CONF_LINE_SIZE 1024
1690 const char k_comment_char
= '#';
1692 const char * k_keyword_domain
= "domain";
1694 const char * k_default_domains
[] =
1697 "254.169.in-addr.arpa",
1707 // Always null terminated
1710 // Linked list of domains
1711 typedef struct domain_entry
1714 struct domain_entry
* next
;
1721 domain_entry_t
* domains
;
1724 const config_t k_empty_config
=
1730 // Context - tracks position in config file, used for error reporting
1733 const char * filename
;
1735 } config_file_context_t
;
1742 load_config (config_t
* conf
);
1745 process_config_line (
1748 config_file_context_t
* context
1752 get_next_word (char * input
, char **next
);
1755 default_config (config_t
* conf
);
1758 add_domain (config_t
* conf
, const char * domain
);
1761 contains_domain (const config_t
* conf
, const char * domain
);
1764 contains_domain_suffix (const config_t
* conf
, const char * addr
);
1770 static config_t
* g_config
= NULL
;
1771 // Configuration info
1773 pthread_mutex_t g_config_mutex
=
1774 #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
1775 PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
;
1777 PTHREAD_MUTEX_INITIALIZER
;
1782 // Configuration functions
1786 Initialise the configuration from the config file.
1790 non-zero error code on failure
1798 Safe to test outside mutex.
1799 If non-zero, initialisation is complete and g_config can be
1800 safely used read-only. If zero, then we do proper mutex
1801 testing before initialisation.
1809 config_t
* temp_config
;
1812 presult
= pthread_mutex_lock (&g_config_mutex
);
1816 "mdns: Fatal mutex lock error in nss_mdns:init_config, %s:%d: %d: %s",
1817 __FILE__
, __LINE__
, presult
, strerror (presult
)
1822 // Test again now we have mutex, in case initialisation occurred while
1826 temp_config
= (config_t
*) malloc (sizeof (config_t
));
1829 // Note: This code will leak memory if initialisation fails
1830 // repeatedly. This should only happen in the case of a memory
1831 // error, so I'm not sure if it's a meaningful problem. - AW
1832 *temp_config
= k_empty_config
;
1833 errcode
= load_config (temp_config
);
1837 g_config
= temp_config
;
1843 "mdns: Can't allocate memory in nss_mdns:init_config, %s:%d",
1850 presult
= pthread_mutex_unlock (&g_config_mutex
);
1854 "mdns: Fatal mutex unlock error in nss_mdns:init_config, %s:%d: %d: %s",
1855 __FILE__
, __LINE__
, presult
, strerror (presult
)
1866 config_is_mdns_suffix (const char * name
)
1868 int errcode
= init_config ();
1871 return contains_domain_suffix (g_config
, name
);
1885 load_config (config_t
* conf
)
1888 char line
[CONF_LINE_SIZE
];
1889 config_file_context_t context
;
1891 context
.filename
= k_conf_file
;
1892 context
.linenum
= 0;
1895 cf
= fopen (context
.filename
, "r");
1899 "mdns: Couldn't open nss_mdns configuration file %s, using default.",
1902 return default_config (conf
);
1905 while (fgets (line
, CONF_LINE_SIZE
, cf
))
1909 errcode
= process_config_line (conf
, line
, &context
);
1912 // Critical error, give up
1925 Parse a line of the configuration file.
1926 For each keyword recognised, perform appropriate handling.
1927 If the keyword is not recognised, print a message to syslog
1931 0 success, or recoverable config file error
1932 non-zero serious system error, processing aborted
1935 process_config_line (
1938 config_file_context_t
* context
1944 word
= get_next_word (curr
, &curr
);
1945 if (! word
|| word
[0] == k_comment_char
)
1947 // Nothing interesting on this line
1951 if (strcmp (word
, k_keyword_domain
) == 0)
1953 word
= get_next_word (curr
, &curr
);
1956 int errcode
= add_domain (conf
, word
);
1959 // something badly wrong, bail
1963 if (get_next_word (curr
, NULL
))
1965 syslog (LOG_WARNING
,
1966 "%s, line %d: ignored extra text found after domain",
1974 syslog (LOG_WARNING
,
1975 "%s, line %d: no domain specified",
1983 syslog (LOG_WARNING
,
1984 "%s, line %d: unknown keyword %s - skipping",
1996 Get next word (whitespace separated) from input string.
1997 A null character is written into the first whitespace character following
2002 Input string. This string is modified by get_next_word.
2004 If non-NULL and the result is non-NULL, a pointer to the
2005 character following the end of the word (after the null)
2006 is written to 'next'.
2007 If no word is found, the original value is unchanged.
2008 If the word extended to the end of the string, 'next' points
2009 to the trailling NULL.
2010 It is safe to pass 'str' as 'input' and '&str' as 'next'.
2012 Pointer to the first non-whitespace character (and thus word) found.
2013 if no word is found, returns NULL.
2016 get_next_word (char * input
, char **next
)
2018 char * curr
= input
;
2021 while (isspace (*curr
))
2032 while (*curr
&& ! isspace (*curr
))
2057 default_config (config_t
* conf
)
2060 for (i
= 0; k_default_domains
[i
]; i
++)
2063 add_domain (conf
, k_default_domains
[i
]);
2066 // Something has gone (badly) wrong - let's bail
2076 add_domain (config_t
* conf
, const char * domain
)
2078 if (! contains_domain (conf
, domain
))
2080 domain_entry_t
* d
=
2081 (domain_entry_t
*) malloc (sizeof (domain_entry_t
));
2085 "mdns: Can't allocate memory in nss_mdns:init_config, %s:%d",
2091 d
->domain
= strdup (domain
);
2095 "mdns: Can't allocate memory in nss_mdns:init_config, %s:%d",
2101 d
->next
= conf
->domains
;
2110 contains_domain (const config_t
* conf
, const char * domain
)
2112 const domain_entry_t
* curr
= conf
->domains
;
2114 while (curr
!= NULL
)
2116 if (strcasecmp (curr
->domain
, domain
) == 0)
2129 contains_domain_suffix (const config_t
* conf
, const char * addr
)
2131 const domain_entry_t
* curr
= conf
->domains
;
2133 while (curr
!= NULL
)
2135 if (cmp_dns_suffix (addr
, curr
->domain
) > 0)
2147 // Types and Constants
2149 static const char * k_local_suffix
= "local";
2150 static const char k_dns_separator
= '.';
2152 static const int k_label_maxlen
= DNS_LABEL_MAXLEN
;
2153 // Label entries longer than this are actually pointers.
2159 const char * comment
;
2162 static const table_entry_t k_table_af
[] =
2164 { AF_UNSPEC
, NULL
, NULL
},
2165 { AF_LOCAL
, "LOCAL", NULL
},
2166 { AF_UNIX
, "UNIX", NULL
},
2167 { AF_INET
, "INET", NULL
},
2168 { AF_INET6
, "INET6", NULL
}
2170 static const int k_table_af_size
=
2171 sizeof (k_table_af
) / sizeof (* k_table_af
);
2173 static const char * k_table_ns_class
[] =
2178 static const int k_table_ns_class_size
=
2179 sizeof (k_table_ns_class
) / sizeof (* k_table_ns_class
);
2181 static const char * k_table_ns_type
[] =
2226 static const int k_table_ns_type_size
=
2227 sizeof (k_table_ns_type
) / sizeof (* k_table_ns_type
);
2234 simple_table_index (const char * table
[], int size
, const char * str
);
2237 table_index_name (const table_entry_t table
[], int size
, const char * str
);
2240 table_index_value (const table_entry_t table
[], int size
, int n
);
2251 count_dots (const char * name
)
2255 for (i
= 0; name
[i
]; i
++)
2257 if (name
[i
] == k_dns_separator
)
2266 islocal (const char * name
)
2268 return cmp_dns_suffix (name
, k_local_suffix
) > 0;
2273 rr_to_af (ns_type_t rrtype
)
2277 case kDNSServiceType_A
:
2280 case kDNSServiceType_AAAA
:
2295 return kDNSServiceType_A
;
2298 return kDNSServiceType_AAAA
;
2301 //return ns_t_invalid;
2308 str_to_af (const char * str
)
2311 table_index_name (k_table_af
, k_table_af_size
, str
);
2315 return k_table_af
[result
].value
;
2320 str_to_ns_class (const char * str
)
2323 simple_table_index (k_table_ns_class
, k_table_ns_class_size
, str
);
2328 str_to_ns_type (const char * str
)
2331 simple_table_index (k_table_ns_type
, k_table_ns_type_size
, str
);
2339 table_index_value (k_table_af
, k_table_af_size
, in
);
2343 return k_table_af
[result
].name
;
2348 ns_class_to_str (ns_class_t in
)
2350 if (in
< k_table_ns_class_size
)
2351 return k_table_ns_class
[in
];
2358 ns_type_to_str (ns_type_t in
)
2360 if (in
< k_table_ns_type_size
)
2361 return k_table_ns_type
[in
];
2368 format_reverse_addr_in (
2369 const struct in_addr
* addr
,
2377 const uint8_t * in_addr_a
= (uint8_t *) addr
;
2384 i
= (prefixlen
+ 7) / 8;
2385 // divide prefixlen into bytes, rounding up
2390 curr
+= sprintf (curr
, "%d.", in_addr_a
[i
]);
2392 sprintf (curr
, "in-addr.arpa");
2399 format_reverse_addr_in6 (
2400 const struct in6_addr
* addr
,
2408 const uint8_t * in_addr_a
= (uint8_t *) addr
;
2410 if (prefixlen
> 128)
2415 i
= (prefixlen
+ 3) / 4;
2416 // divide prefixlen into nibbles, rounding up
2418 // Special handling for first
2421 curr
+= sprintf (curr
, "%d.", (in_addr_a
[i
/2] >> 4) & 0x0F);
2424 // Convert i to bytes (divide by 2)
2431 val
= in_addr_a
[i
];
2432 curr
+= sprintf (curr
, "%x.%x.", val
& 0x0F, (val
>> 4) & 0x0F);
2434 sprintf (curr
, "ip6.arpa");
2441 format_reverse_addr (
2452 format_reverse_addr_in (
2453 (struct in_addr
*) addr
, prefixlen
, buf
2459 format_reverse_addr_in6 (
2460 (struct in6_addr
*) addr
, prefixlen
, buf
2471 cmp_dns_suffix (const char * name
, const char * domain
)
2473 const char * nametail
;
2474 const char * domaintail
;
2477 if (*name
== 0 || *name
== k_dns_separator
)
2479 // Name can't be empty or start with separator
2480 return CMP_DNS_SUFFIX_BAD_NAME
;
2485 return CMP_DNS_SUFFIX_SUCCESS
;
2489 if (*domain
== k_dns_separator
)
2491 // drop leading separator from domain
2493 if (*domain
== k_dns_separator
)
2495 return CMP_DNS_SUFFIX_BAD_DOMAIN
;
2499 // Find ends of strings
2500 for (nametail
= name
; *nametail
; nametail
++)
2502 for (domaintail
= domain
; *domaintail
; domaintail
++)
2505 // Shuffle back to last real character, and drop any trailing '.'
2506 // while we're at it.
2508 if (*nametail
== k_dns_separator
)
2511 if (*nametail
== k_dns_separator
)
2513 return CMP_DNS_SUFFIX_BAD_NAME
;
2517 if (*domaintail
== k_dns_separator
)
2520 if (*domaintail
== k_dns_separator
)
2522 return CMP_DNS_SUFFIX_BAD_DOMAIN
;
2529 && domaintail
>= domain
2530 && tolower(*nametail
) == tolower(*domaintail
))
2536 /* A successful finish will be one of the following:
2537 (leading and trailing . ignored)
2539 name : domain2.domain1
2540 domain: domain2.domain1
2543 name : domain3.domain2.domain1
2544 domain: domain2.domain1
2549 && (nametail
< name
|| *nametail
== k_dns_separator
)
2552 return CMP_DNS_SUFFIX_SUCCESS
;
2556 return CMP_DNS_SUFFIX_FAILURE
;
2562 dns_rdata_to_name (const char * rdata
, int rdlen
, char * name
, int name_len
)
2565 // Index into 'name'
2566 const char * rdata_curr
= rdata
;
2568 if (rdlen
== 0) return DNS_RDATA_TO_NAME_BAD_FORMAT
;
2571 In RDATA, a DNS name is stored as a series of labels.
2572 Each label consists of a length octet (max value 63)
2573 followed by the data for that label.
2574 The series is terminated with a length 0 octet.
2575 A length octet beginning with bits 11 is a pointer to
2576 somewhere else in the payload, but we don't support these
2577 since we don't have access to the entire payload.
2579 See RFC1034 section 3.1 and RFC1035 section 3.1.
2583 int term_len
= *rdata_curr
;
2589 // 0 length record terminates label
2591 else if (term_len
> k_label_maxlen
)
2594 return DNS_RDATA_TO_NAME_PTR
;
2596 else if (rdata_curr
+ term_len
> rdata
+ rdlen
)
2599 return DNS_RDATA_TO_NAME_BAD_FORMAT
;
2602 if (name_len
< i
+ term_len
+ 1)
2606 return DNS_RDATA_TO_NAME_TOO_LONG
;
2609 memcpy (name
+ i
, rdata_curr
, term_len
);
2612 rdata_curr
+= term_len
;
2614 name
[i
] = k_dns_separator
;
2627 Find the index of an string entry in a table. A case insenitive match
2628 is performed. If no entry is found, 0 is returned.
2633 Table entries may be NULL. NULL entries will never match.
2635 number of entries in table
2640 index of first matching entry, or 0 if no matches
2643 simple_table_index (const char * table
[], int size
, const char * str
)
2646 for (i
= 0; i
< size
; i
++)
2650 && (strcasecmp (table
[i
], str
) == 0)
2662 Find the index of a name in a table.
2666 array of table_entry_t records. The name field is compared
2667 (ignoring case) to the input string.
2669 number of entries in table
2674 index of first matching entry, or -1 if no matches
2677 table_index_name (const table_entry_t table
[], int size
, const char * str
)
2680 for (i
= 0; i
< size
; i
++)
2684 && (strcasecmp (table
[i
].name
, str
) == 0)
2696 Find the index of a value a table.
2700 array of table_entry_t records. The value field is compared to
2703 number of entries in table
2708 index of first matching entry, or -1 if no matches
2711 table_index_value (const table_entry_t table
[], int size
, int n
)
2714 for (i
= 0; i
< size
; i
++)
2716 if (table
[i
].value
== n
)