#include "stdafx.h" #include "csocket.h" #include "csocketevents.h" #include "cphidgetlist.h" #include "cphidgetmanager.h" #include "cphidgetdictionary.h" #include "cphidgetsbc.h" #include "zeroconf.h" #include "dns_sd.h" /* This is all taken from mDNSResponderPosix's nss_mdns code so we can lookup .local name manually under uClibc (PhidgetSBC) */ #ifdef ZEROCONF_LOOKUP #ifndef AF_LOCAL #define AF_LOCAL AF_UNIX #endif /* runtime linking of dns_sd functions */ #ifdef ZEROCONF_RUNTIME_LINKING typedef void (DNSSD_API * DNSServiceRefDeallocateType) (DNSServiceRef); typedef DNSServiceErrorType (DNSSD_API * DNSServiceQueryRecordType) (DNSServiceRef *, DNSServiceFlags, uint32_t, const char *, uint16_t, uint16_t, DNSServiceQueryRecordReply, void *context); typedef DNSServiceErrorType (DNSSD_API * DNSServiceProcessResultType) (DNSServiceRef); typedef int (DNSSD_API * DNSServiceRefSockFDType) (DNSServiceRef sdRef); extern DNSServiceRefDeallocateType DNSServiceRefDeallocatePtr; extern DNSServiceQueryRecordType DNSServiceQueryRecordPtr; extern DNSServiceProcessResultType DNSServiceProcessResultPtr; extern DNSServiceRefSockFDType DNSServiceRefSockFDPtr; #else #define DNSServiceRefDeallocatePtr DNSServiceRefDeallocate #define DNSServiceQueryRecordPtr DNSServiceQueryRecord #define DNSServiceProcessResultPtr DNSServiceProcessResult #define DNSServiceRefSockFDPtr DNSServiceRefSockFD #endif //---------- // Structs //---------- typedef struct { int value; const char * name; const char * comment; } table_entry_t; // Linked list of domains typedef struct domain_entry { char * domain; struct domain_entry * next; } domain_entry_t; // Config typedef struct { domain_entry_t * domains; } config_t; //---------- // Constants, Defines //---------- #define k_hostname_maxlen 255 #define k_aliases_max 15 #define k_addrs_max 15 // Maximum length of a single DNS label #define DNS_LABEL_MAXLEN 63 // Maximum length of a DNS name #define DNS_NAME_MAXLEN 255 #define CONF_LINE_SIZE 1024 // Label entries longer than this are actually pointers. static const int k_label_maxlen = DNS_LABEL_MAXLEN; // 0 seconds, 500 milliseconds static const struct timeval k_select_time = { 0, 500000 }; static const char * k_local_suffix = "local"; static const char k_dns_separator = '.'; static const table_entry_t k_table_af [] = { { AF_UNSPEC, NULL, NULL }, { AF_LOCAL, "LOCAL", NULL }, { AF_UNIX, "UNIX", NULL }, { AF_INET, "INET", NULL }, { AF_INET6, "INET6", NULL } }; static const int k_table_af_size = sizeof (k_table_af) / sizeof (* k_table_af); static const char * k_table_ns_class [] = { NULL, "IN" }; static const int k_table_ns_class_size = sizeof (k_table_ns_class) / sizeof (* k_table_ns_class); static const char * k_table_ns_type [] = { NULL, "A", "NS", "MD", "MF", "CNAME", "SOA", "MB", "MG", "MR", "NULL", "WKS", "PTR", "HINFO", "MINFO", "MX", "TXT", "RP", "AFSDB", "X25", "ISDN", "RT", "NSAP", NULL, "SIG", "KEY", "PX", "GPOS", "AAAA", "LOC", "NXT", "EID", "NIMLOC", "SRV", "ATMA", "NAPTR", "KX", "CERT", "A6", "DNAME", "SINK", "OPT" }; static const int k_table_ns_type_size = sizeof (k_table_ns_type) / sizeof (* k_table_ns_type); const char * k_conf_file = "/etc/nss_mdns.conf"; const char k_comment_char = '#'; const char * k_keyword_domain = "domain"; const char * k_default_domains [] = { "local", "254.169.in-addr.arpa", "8.e.f.ip6.int", "8.e.f.ip6.arpa", "9.e.f.ip6.int", "9.e.f.ip6.arpa", "a.e.f.ip6.int", "a.e.f.ip6.arpa", "b.e.f.ip6.int", "b.e.f.ip6.arpa", // Always null terminated NULL }; const config_t k_empty_config = { NULL }; //---------- // Enums, Structs, Typedefs //---------- /* Possible results of lookup using a nss_* function. */ enum nss_status { NSS_STATUS_TRYAGAIN = -2, NSS_STATUS_UNAVAIL, NSS_STATUS_NOTFOUND, NSS_STATUS_SUCCESS, NSS_STATUS_RETURN }; enum { // Format is broken. Usually because we ran out of data // (according to rdata) before the labels said we should. DNS_RDATA_TO_NAME_BAD_FORMAT = -1, // The converted rdata is longer than the name buffer. DNS_RDATA_TO_NAME_TOO_LONG = -2, // The rdata contains a pointer. DNS_RDATA_TO_NAME_PTR = -3, }; enum { CMP_DNS_SUFFIX_SUCCESS = 1, CMP_DNS_SUFFIX_FAILURE = 0, CMP_DNS_SUFFIX_BAD_NAME = 1, CMP_DNS_SUFFIX_BAD_DOMAIN = -2 }; typedef enum nss_status nss_status; typedef struct hostent hostent; typedef int ns_type_t; typedef int ns_class_t; typedef int errcode_t; typedef void mdns_lookup_callback_t ( DNSServiceRef sdref, DNSServiceFlags flags, uint32_t interface_index, DNSServiceErrorType error_code, const char *fullname, uint16_t rrtype, uint16_t rrclass, uint16_t rdlen, const void *rdata, uint32_t ttl, void *context ); typedef struct buf_header { char hostname [k_hostname_maxlen + 1]; char * aliases [k_aliases_max + 1]; char * addrs [k_addrs_max + 1]; } buf_header_t; typedef struct result_map { int done; nss_status status; hostent * hostent; buf_header_t * header; int aliases_count; int addrs_count; char * buffer; // Index for addresses - grow from low end // Index points to first empty space int addr_idx; // Index for aliases - grow from high end // Index points to lowest entry int alias_idx; int r_errno; int r_h_errno; } result_map_t; // Context - tracks position in config file, used for error reporting typedef struct { const char * filename; int linenum; } config_file_context_t; //---------- // Globals //---------- static config_t * g_config = NULL; pthread_mutex_t g_config_mutex = #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; #else PTHREAD_MUTEX_INITIALIZER; #endif mdns_lookup_callback_t mdns_lookup_callback; //---------- // Prototypes //---------- static int callback_body_ptr ( const char * fullname, result_map_t * result, int rdlen, const void * rdata ); static nss_status handle_events (DNSServiceRef sdref, result_map_t * result, const char * str); static nss_status mdns_lookup_name ( const char * fullname, int af, result_map_t * result ); const char * ns_class_to_str (ns_class_t in); const char * ns_type_to_str (ns_type_t in); ns_type_t af_to_rr (int af); int dns_rdata_to_name (const char * rdata, int rdlen, char * name, int name_len); int cmp_dns_suffix (const char * name, const char * domain); static char * add_hostname_or_alias (result_map_t * result, const char * data, int len); static char * add_hostname_len (result_map_t * result, const char * fullname, int len); static char * add_alias_to_buffer (result_map_t * result, const char * data, int len); static char * add_hostname_or_alias (result_map_t * result, const char * data, int len); static void * add_address_to_buffer (result_map_t * result, const void * data, int len); static errcode_t add_domain (config_t * conf, const char * domain); static nss_status set_err_success (result_map_t * result); static nss_status set_err_buf_too_small (result_map_t * result); static nss_status set_err_internal_resource_full (result_map_t * result); static nss_status set_err (result_map_t * result, nss_status status, int err, int herr); static nss_status set_err_bad_hostname (result_map_t * result); static nss_status set_err_notfound (result_map_t * result); static nss_status set_err_mdns_failed (result_map_t * result); static nss_status set_err_system (result_map_t * result); static const char * is_applicable_name ( result_map_t * result, const char * name, char * lookup_name ); int islocal (const char * name); static int contains_domain_suffix (const config_t * conf, const char * addr); static char * contains_alias (result_map_t * result, const char * alias); static void * contains_address (result_map_t * result, const void * data, int len); static int contains_domain (const config_t * conf, const char * domain); static int init_result ( result_map_t * result, hostent * result_buf, char * buf, size_t buflen ); errcode_t init_config (); static errcode_t load_config (config_t * conf); static errcode_t process_config_line ( config_t * conf, char * line, config_file_context_t * context ); static errcode_t default_config (config_t * conf); int config_is_mdns_suffix (const char * name); static char * get_next_word (char * input, char **next); //Functions /* Get next word (whitespace separated) from input string. A null character is written into the first whitespace character following the word. Parameters input Input string. This string is modified by get_next_word. next If non-NULL and the result is non-NULL, a pointer to the character following the end of the word (after the null) is written to 'next'. If no word is found, the original value is unchanged. If the word extended to the end of the string, 'next' points to the trailling NULL. It is safe to pass 'str' as 'input' and '&str' as 'next'. Returns Pointer to the first non-whitespace character (and thus word) found. if no word is found, returns NULL. */ static char * get_next_word (char * input, char **next) { char * curr = input; char * result; while (isspace (*curr)) { curr ++; } if (*curr == 0) { return NULL; } result = curr; while (*curr && ! isspace (*curr)) { curr++; } if (*curr) { *curr = 0; if (next) { *next = curr+1; } } else { if (next) { *next = curr; } } return result; } static errcode_t add_domain (config_t * conf, const char * domain) {