diff options
Diffstat (limited to 'mDNSResponder/Clients/dnsctl.c')
-rw-r--r-- | mDNSResponder/Clients/dnsctl.c | 227 |
1 files changed, 215 insertions, 12 deletions
diff --git a/mDNSResponder/Clients/dnsctl.c b/mDNSResponder/Clients/dnsctl.c index f10f2259..e01c8fec 100644 --- a/mDNSResponder/Clients/dnsctl.c +++ b/mDNSResponder/Clients/dnsctl.c @@ -17,16 +17,20 @@ #include <sys/time.h> #include <net/if.h> // if_nametoindex() -#include <dispatch/dispatch.h> #include "dns_services.h" +#include <xpc/xpc.h> +#include "dns_xpc.h" //************************************************************************************************************* // Globals: //************************************************************************************************************* static const char kFilePathSep = '/'; + static DNSXConnRef ClientRef = NULL; +static xpc_connection_t dnsctl_conn = NULL; + //************************************************************************************************************* // Utility Funcs: //************************************************************************************************************* @@ -56,12 +60,31 @@ static void print_usage(const char *arg0) fprintf(stderr, "%s USAGE: \n", arg0); fprintf(stderr, "%s -DP Enable DNS Proxy with Default Parameters \n", arg0); fprintf(stderr, "%s -DP [-o <output interface>] [-i <input interface(s)>] Enable DNS Proxy \n", arg0); + fprintf(stderr, "%s -L [1/2/3/4] Change mDNSResponder Logging Level \n", arg0); + fprintf(stderr, "%s -I Print mDNSResponder STATE INFO \n", arg0); +} + + +static bool DebugEnabled() +{ + return true; // keep this true to debug the XPC msgs +} + +static void DebugLog(const char *prefix, xpc_object_t o) +{ + if (!DebugEnabled()) + return; + + char *desc = xpc_copy_description(o); + printf("%s: %s \n", prefix, desc); + free(desc); } //************************************************************************************************************* // CallBack Funcs: //************************************************************************************************************* + // DNSXEnableProxy Callback from the Daemon static void dnsproxy_reply(DNSXConnRef connRef, DNSXErrorType errCode) { @@ -75,6 +98,8 @@ static void dnsproxy_reply(DNSXConnRef connRef, DNSXErrorType errCode) DNSXRefDeAlloc(ClientRef); break; case kDNSX_BadParam : printf(" BAD PARAMETER \n"); DNSXRefDeAlloc(ClientRef); break; + case kDNSX_Busy : printf(" BUSY \n"); + DNSXRefDeAlloc(ClientRef); break; case kDNSX_UnknownErr : default : printf(" UNKNOWN ERR \n"); DNSXRefDeAlloc(ClientRef); break; @@ -84,15 +109,56 @@ static void dnsproxy_reply(DNSXConnRef connRef, DNSXErrorType errCode) } //************************************************************************************************************* +// XPC Funcs: +//************************************************************************************************************* -int main(int argc, char **argv) +static void Init_Connection(const char *servname) +{ + dnsctl_conn = xpc_connection_create_mach_service(servname, dispatch_get_main_queue(), XPC_CONNECTION_MACH_SERVICE_PRIVILEGED); + + xpc_connection_set_event_handler(dnsctl_conn, ^(xpc_object_t event) + { + printf("InitConnection: [%s] \n", xpc_dictionary_get_string(event, XPC_ERROR_KEY_DESCRIPTION)); + }); + + xpc_connection_resume(dnsctl_conn); +} + +static void SendDictToServer(xpc_object_t msg) { - DNSXErrorType err; - // Default i/p intf is lo0 and o/p intf is primary interface - IfIndex Ipintfs[MaxInputIf] = {1, 0, 0, 0, 0}; - IfIndex Opintf = kDNSIfindexAny; + DebugLog("SendDictToServer Sending msg to Daemon", msg); + xpc_connection_send_message_with_reply(dnsctl_conn, msg, dispatch_get_main_queue(), ^(xpc_object_t recv_msg) + { + xpc_type_t type = xpc_get_type(recv_msg); + + if (type == XPC_TYPE_DICTIONARY) + { + DebugLog("SendDictToServer Received reply msg from Daemon", recv_msg); + /* + // If we ever want to do something based on the reply of the daemon + switch (daemon_status) + { + default: + break; + } + */ + } + else + { + printf("SendDictToServer Received unexpected reply from daemon [%s]", + xpc_dictionary_get_string(recv_msg, XPC_ERROR_KEY_DESCRIPTION)); + DebugLog("SendDictToServer Unexpected Reply contents", recv_msg); + } + exit(1); + }); +} + +//************************************************************************************************************* + +int main(int argc, char **argv) +{ // Extract program name from argv[0], which by convention contains the path to this executable const char *a0 = strrchr(argv[0], kFilePathSep) + 1; if (a0 == (const char *)1) @@ -113,15 +179,20 @@ int main(int argc, char **argv) if (argc < 2) goto Usage; - if ( !strcmp(argv[1], "-DP") || !strcmp(argv[1], "-dp") ) + printtimestamp(); + if (!strcasecmp(argv[1], "-DP")) { + DNSXErrorType err; + // Default i/p intf is lo0 and o/p intf is primary interface + IfIndex Ipintfs[MaxInputIf] = {1, 0, 0, 0, 0}; + IfIndex Opintf = kDNSIfindexAny; + if (argc == 2) { - printtimestamp(); - printf("Enabling DNSProxy on mDNSResponder with Default Parameters\n"); dispatch_queue_t my_Q = dispatch_queue_create("com.apple.dnsctl.callback_queue", NULL); err = DNSXEnableProxy(&ClientRef, kDNSProxyEnable, Ipintfs, Opintf, my_Q, dnsproxy_reply); - if (err) fprintf(stderr, "DNSXEnableProxy returned %d\n", err); + if (err) + fprintf(stderr, "DNSXEnableProxy returned %d\n", err); } else if (argc > 2) { @@ -159,13 +230,96 @@ int main(int argc, char **argv) argv++; } } - printtimestamp(); printf("Enabling DNSProxy on mDNSResponder \n"); dispatch_queue_t my_Q = dispatch_queue_create("com.apple.dnsctl.callback_queue", NULL); err = DNSXEnableProxy(&ClientRef, kDNSProxyEnable, Ipintfs, Opintf, my_Q, dnsproxy_reply); - if (err) fprintf(stderr, "DNSXEnableProxy returned %d\n", err); + if (err) + fprintf(stderr, "DNSXEnableProxy returned %d\n", err); + } + } + else if (!strcasecmp(argv[1], "-l")) + { + printf("Changing loglevel of mDNSResponder \n"); + Init_Connection(kDNSCTLService); + + // Create Dictionary To Send + xpc_object_t dict = xpc_dictionary_create(NULL, NULL, 0); + + if (argc == 2) + { + xpc_dictionary_set_uint64(dict, kDNSLogLevel, log_level1); + + SendDictToServer(dict); + xpc_release(dict); + dict = NULL; + } + else if (argc > 2) + { + argc--; + argv++; + switch (atoi(argv[1])) + { + case log_level1: + xpc_dictionary_set_uint64(dict, kDNSLogLevel, log_level1); + break; + + case log_level2: + xpc_dictionary_set_uint64(dict, kDNSLogLevel, log_level2); + break; + + case log_level3: + xpc_dictionary_set_uint64(dict, kDNSLogLevel, log_level3); + break; + + case log_level4: + xpc_dictionary_set_uint64(dict, kDNSLogLevel, log_level4); + break; + + default: + xpc_dictionary_set_uint64(dict, kDNSLogLevel, log_level1); + break; + } + SendDictToServer(dict); + xpc_release(dict); + dict = NULL; } } + else if(!strcasecmp(argv[1], "-i")) + { + printf("Get STATE INFO of mDNSResponder \n"); + Init_Connection(kDNSCTLService); + + // Create Dictionary To Send + xpc_object_t dict = xpc_dictionary_create(NULL, NULL, 0); + xpc_dictionary_set_uint64(dict, kDNSStateInfo, full_state); + SendDictToServer(dict); + xpc_release(dict); + dict = NULL; + } + else if(!strcasecmp(argv[1], "-th")) + { + printf("Sending Test message to mDNSResponder to forward to mDNSResponderHelper\n"); + Init_Connection(kDNSCTLService); + + // Create Dictionary To Send + xpc_object_t dict = xpc_dictionary_create(NULL, NULL, 0); + xpc_dictionary_set_uint64(dict, kmDNSResponderTests, test_helper_ipc); + SendDictToServer(dict); + xpc_release(dict); + dict = NULL; + } + else if(!strcasecmp(argv[1], "-tl")) + { + printf("Testing mDNSResponder Logging\n"); + Init_Connection(kDNSCTLService); + + // Create Dictionary To Send + xpc_object_t dict = xpc_dictionary_create(NULL, NULL, 0); + xpc_dictionary_set_uint64(dict, kmDNSResponderTests, test_mDNS_log); + SendDictToServer(dict); + xpc_release(dict); + dict = NULL; + } else { goto Usage; @@ -178,3 +332,52 @@ Usage: return 0; } +/* + +#include <getopt.h> + +static int operation; + +static int getfirstoption(int argc, char **argv, const char *optstr, int *pOptInd) +{ + // Return the recognized option in optstr and the option index of the next arg. + int o = getopt(argc, (char *const *)argv, optstr); + *pOptInd = optind; + return o; +} + +int opindex; +operation = getfirstoption(argc, argv, "lLDdPp", &opindex); +if (operation == -1) + goto Usage; + + + +switch (operation) +{ + case 'L': + case 'l': + { + printtimestamp(); + printf("Change Verbosity Level of mDNSResponder\n"); + + Init_Connection(kDNSCTLService); + + // Create Dictionary To Send + xpc_object_t dict = xpc_dictionary_create(NULL, NULL, 0); + if (dict == NULL) + printf("could not create the Msg Dict To Send! \n"); + xpc_dictionary_set_uint64(dict, kDNSLogLevel, log_level2); + + SendDictToServer(dict); + + xpc_release(dict); + dict = NULL; + break; + } + // exit(1); + +} + +*/ + |