]>
git.saurik.com Git - apple/mdnsresponder.git/blob - Clients/dnsctl.c
1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2012 Apple Inc. All rights reserved.
6 * Command-line tool using libdns_services.dylib
8 * To build only this tool, copy and paste the following on the command line:
9 * On Apple 64bit Platforms ONLY OSX/iOS:
10 * clang -Wall dnsctl.c /usr/lib/libdns_services.dylib -o dnsctl
18 #include <net/if.h> // if_nametoindex()
20 #include <dispatch/dispatch.h>
21 #include "dns_services_mdns.h"
23 //*************************************************************************************************************
25 //*************************************************************************************************************
27 static const char kFilePathSep
= '/';
28 static DNSXConnRef ClientRef
= NULL
;
30 //*************************************************************************************************************
32 //*************************************************************************************************************
34 static void printtimestamp(void)
39 static char new_date
[16];
41 gettimeofday(&tv
, NULL
);
42 localtime_r((time_t*)&tv
.tv_sec
, &tm
);
44 strftime(new_date
, sizeof(new_date
), "%a %d %b %Y", &tm
);
45 //display date only if it has changed
46 if (strncmp(date
, new_date
, sizeof(new_date
)))
48 printf("DATE: ---%s---\n", new_date
);
49 strncpy(date
, new_date
, sizeof(date
));
51 printf("%2d:%02d:%02d.%03d ", tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ms
);
54 static void print_usage(const char *arg0
)
56 fprintf(stderr
, "%s USAGE: \n", arg0
);
57 fprintf(stderr
, "%s -DP Enable DNS Proxy with Default Parameters \n", arg0
);
58 fprintf(stderr
, "%s -DP [-o <output interface>] [-i <input interface(s)>] Enable DNS Proxy \n", arg0
);
61 //*************************************************************************************************************
63 //*************************************************************************************************************
65 // DNSXEnableProxy Callback from the Daemon
66 static void dnsproxy_reply(DNSXConnRef connRef
, DNSXErrorType errCode
)
72 case kDNSX_NoError
: printf(" SUCCESS \n"); break;
73 case kDNSX_DictError
: printf(" DICT ERROR \n"); break;
74 case kDNSX_DaemonNotRunning
: printf(" NO DAEMON \n");
75 DNSXRefDeAlloc(ClientRef
); break;
76 case kDNSX_Engaged
: printf(" ENGAGED \n");
77 DNSXRefDeAlloc(ClientRef
); break;
78 case kDNSX_UnknownErr
:
79 default : printf("UNKNOWN ERR \n");
80 DNSXRefDeAlloc(ClientRef
); break;
86 //*************************************************************************************************************
88 int main(int argc
, char **argv
)
92 // Default i/p intf is lo0 and o/p intf is primary interface
93 IfIndex Ipintfs
[MaxInputIf
] = {1, 0, 0, 0, 0};
94 IfIndex Opintf
= kDNSIfindexAny
;
96 // Extract program name from argv[0], which by convention contains the path to this executable
97 const char *a0
= strrchr(argv
[0], kFilePathSep
) + 1;
98 if (a0
== (const char *)1)
104 fprintf(stderr
, "%s MUST run as root!!\n", a0
);
107 if ((sizeof(argv
) == 8))
108 printf("dnsctl running in 64-bit mode\n");
109 else if ((sizeof(argv
) == 4))
110 printf("dnsctl running in 32-bit mode\n");
112 // expects atleast one argument
116 if ( !strcmp(argv
[1], "-DP") || !strcmp(argv
[1], "-dp") )
121 printf("Proceeding to Enable DNSProxy on mDNSResponder with Default Parameters\n");
122 dispatch_queue_t my_Q
= dispatch_queue_create("com.apple.dnsctl.callback_queue", NULL
);
123 err
= DNSXEnableProxy(&ClientRef
, kDNSProxyEnable
, Ipintfs
, Opintf
, my_Q
, dnsproxy_reply
);
129 if (!strcmp(argv
[1], "-o"))
131 Opintf
= if_nametoindex(argv
[2]);
133 Opintf
= atoi(argv
[2]);
136 fprintf(stderr
, "Could not parse o/p interface [%s]: Passing default primary \n", argv
[2]);
137 Opintf
= kDNSIfindexAny
;
142 if (argc
> 2 && !strcmp(argv
[1], "-i"))
147 for (i
= 0; i
< MaxInputIf
&& argc
> 1; i
++)
149 Ipintfs
[i
] = if_nametoindex(argv
[1]);
151 Ipintfs
[i
] = atoi(argv
[1]);
154 fprintf(stderr
, "Could not parse i/p interface [%s]: Passing default lo0 \n", argv
[2]);
162 printf("Proceeding to Enable DNSProxy on mDNSResponder \n");
163 dispatch_queue_t my_Q
= dispatch_queue_create("com.apple.dnsctl.callback_queue", NULL
);
164 err
= DNSXEnableProxy(&ClientRef
, kDNSProxyEnable
, Ipintfs
, Opintf
, my_Q
, dnsproxy_reply
);