]> git.saurik.com Git - apple/mdnsresponder.git/blob - Clients/dnsctl.c
mDNSResponder-522.1.11.tar.gz
[apple/mdnsresponder.git] / Clients / dnsctl.c
1 /* -*- Mode: C; tab-width: 4 -*-
2 *
3 * Copyright (c) 2012 Apple Inc. All rights reserved.
4 *
5 * dnsctl.c
6 * Command-line tool using libdns_services.dylib
7 *
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
11 *
12 */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/time.h>
18 #include <net/if.h> // if_nametoindex()
19
20 #include <dispatch/dispatch.h>
21 #include "dns_services.h"
22
23 //*************************************************************************************************************
24 // Globals:
25 //*************************************************************************************************************
26
27 static const char kFilePathSep = '/';
28 static DNSXConnRef ClientRef = NULL;
29
30 //*************************************************************************************************************
31 // Utility Funcs:
32 //*************************************************************************************************************
33
34 static void printtimestamp(void)
35 {
36 struct tm tm;
37 int ms;
38 static char date[16];
39 static char new_date[16];
40 struct timeval tv;
41 gettimeofday(&tv, NULL);
42 localtime_r((time_t*)&tv.tv_sec, &tm);
43 ms = tv.tv_usec/1000;
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)))
47 {
48 printf("DATE: ---%s---\n", new_date);
49 strncpy(date, new_date, sizeof(date));
50 }
51 printf("%2d:%02d:%02d.%03d ", tm.tm_hour, tm.tm_min, tm.tm_sec, ms);
52 }
53
54 static void print_usage(const char *arg0)
55 {
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);
59 }
60
61 //*************************************************************************************************************
62 // CallBack Funcs:
63 //*************************************************************************************************************
64
65 // DNSXEnableProxy Callback from the Daemon
66 static void dnsproxy_reply(DNSXConnRef connRef, DNSXErrorType errCode)
67 {
68 (void) connRef;
69 printtimestamp();
70 switch (errCode)
71 {
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;
81 }
82
83 }
84
85 //*************************************************************************************************************
86
87 int main(int argc, char **argv)
88 {
89 DNSXErrorType err;
90
91 // Default i/p intf is lo0 and o/p intf is primary interface
92 IfIndex Ipintfs[MaxInputIf] = {1, 0, 0, 0, 0};
93 IfIndex Opintf = kDNSIfindexAny;
94
95 // Extract program name from argv[0], which by convention contains the path to this executable
96 const char *a0 = strrchr(argv[0], kFilePathSep) + 1;
97 if (a0 == (const char *)1)
98 a0 = argv[0];
99
100 // Must run as root
101 if (0 != geteuid())
102 {
103 fprintf(stderr, "%s MUST run as root!!\n", a0);
104 exit(-1);
105 }
106 if ((sizeof(argv) == 8))
107 printf("dnsctl running in 64-bit mode\n");
108 else if ((sizeof(argv) == 4))
109 printf("dnsctl running in 32-bit mode\n");
110
111 // expects atleast one argument
112 if (argc < 2)
113 goto Usage;
114
115 if ( !strcmp(argv[1], "-DP") || !strcmp(argv[1], "-dp") )
116 {
117 if (argc == 2)
118 {
119 printtimestamp();
120 printf("Proceeding to Enable DNSProxy on mDNSResponder with Default Parameters\n");
121 dispatch_queue_t my_Q = dispatch_queue_create("com.apple.dnsctl.callback_queue", NULL);
122 err = DNSXEnableProxy(&ClientRef, kDNSProxyEnable, Ipintfs, Opintf, my_Q, dnsproxy_reply);
123 }
124 else if (argc > 2)
125 {
126 argc--;
127 argv++;
128 if (!strcmp(argv[1], "-o"))
129 {
130 Opintf = if_nametoindex(argv[2]);
131 if (!Opintf)
132 Opintf = atoi(argv[2]);
133 if (!Opintf)
134 {
135 fprintf(stderr, "Could not parse o/p interface [%s]: Passing default primary \n", argv[2]);
136 Opintf = kDNSIfindexAny;
137 }
138 argc -= 2;
139 argv += 2;
140 }
141 if (argc > 2 && !strcmp(argv[1], "-i"))
142 {
143 int i;
144 argc--;
145 argv++;
146 for (i = 0; i < MaxInputIf && argc > 1; i++)
147 {
148 Ipintfs[i] = if_nametoindex(argv[1]);
149 if (!Ipintfs[i])
150 Ipintfs[i] = atoi(argv[1]);
151 if (!Ipintfs[i])
152 {
153 fprintf(stderr, "Could not parse i/p interface [%s]: Passing default lo0 \n", argv[2]);
154 Ipintfs[i] = 1;
155 }
156 argc--;
157 argv++;
158 }
159 }
160 printtimestamp();
161 printf("Proceeding to Enable DNSProxy on mDNSResponder \n");
162 dispatch_queue_t my_Q = dispatch_queue_create("com.apple.dnsctl.callback_queue", NULL);
163 err = DNSXEnableProxy(&ClientRef, kDNSProxyEnable, Ipintfs, Opintf, my_Q, dnsproxy_reply);
164 }
165 }
166 else
167 {
168 goto Usage;
169 }
170
171 dispatch_main();
172
173 Usage:
174 print_usage(a0);
175 return 0;
176 }
177