7 # include <sys/socket.h>
19 #include "dnscrypt_proxy.h"
26 static struct option getopt_long_options
[] = {
27 { "local-address", 1, NULL
, 'a' },
29 { "daemonize", 0, NULL
, 'd' },
31 { "edns-payload-size", 1, NULL
, 'e' },
32 { "help", 0, NULL
, 'h' },
33 { "provider-key", 1, NULL
, 'k' },
35 { "logfile", 1, NULL
, 'l' },
37 { "max-active-requests", 1, NULL
, 'n' },
39 { "pidfile", 1, NULL
, 'p' },
41 { "resolver-address", 1, NULL
, 'r' },
42 { "resolver-port", 1, NULL
, 't' },
43 { "user", 1, NULL
, 'u' },
44 { "provider-name", 1, NULL
, 'N' },
45 { "local-port", 1, NULL
, 'P' },
46 { "tcp-only", 0, NULL
, 'T' },
47 { "version", 0, NULL
, 'V' },
51 static const char *getopt_options
= "a:de:hk:l:n:p:r:t:u:N:P:TV";
53 static const char *getopt_options
= "a:e:hk:n:r:t:u:N:P:TV";
56 #ifndef DEFAULT_CONNECTIONS_COUNT_MAX
57 # define DEFAULT_CONNECTIONS_COUNT_MAX 250U
60 #ifndef DEFAULT_PROVIDER_PUBLICKEY
61 # define DEFAULT_PROVIDER_PUBLICKEY \
62 "B735:1140:206F:225D:3E2B:D822:D7FD:691E:" \
63 "A1C3:3CC8:D666:8D0C:BE04:BFAB:CA43:FB79"
65 #ifndef DEFAULT_PROVIDER_NAME
66 # define DEFAULT_PROVIDER_NAME "2.dnscrypt-cert.opendns.com."
68 #ifndef DEFAULT_RESOLVER_IP
69 # define DEFAULT_RESOLVER_IP "208.67.220.220"
75 puts(PACKAGE_STRING
"\n"
76 "Copyright (C) 2011-2012 OpenDNS, Inc.");
82 const struct option
*options
= getopt_long_options
;
87 printf(" -%c\t--%s%s\n", options
->val
, options
->name
,
88 options
->has_arg
? "=..." : "");
90 } while (options
->name
!= NULL
);
91 puts("\nPlease consult the dnscrypt-proxy(8) man page for details.\n");
95 void options_init_with_default(AppContext
* const app_context
,
96 ProxyContext
* const proxy_context
)
98 assert(proxy_context
->event_loop
== NULL
);
99 proxy_context
->app_context
= app_context
;
100 proxy_context
->connections_count
= 0U;
101 proxy_context
->connections_count_max
= DEFAULT_CONNECTIONS_COUNT_MAX
;
102 proxy_context
->edns_payload_size
= (size_t) DNS_DEFAULT_EDNS_PAYLOAD_SIZE
;
103 proxy_context
->local_ip
= "127.0.0.1";
104 proxy_context
->local_port
= DNS_DEFAULT_LOCAL_PORT
;
105 proxy_context
->log_fd
= -1;
106 proxy_context
->log_file
= NULL
;
107 proxy_context
->pid_file
= NULL
;
108 proxy_context
->provider_name
= DEFAULT_PROVIDER_NAME
;
109 proxy_context
->provider_publickey_s
= DEFAULT_PROVIDER_PUBLICKEY
;
110 proxy_context
->resolver_ip
= DEFAULT_RESOLVER_IP
;
111 proxy_context
->resolver_port
= DNS_DEFAULT_RESOLVER_PORT
;
113 proxy_context
->user_id
= (uid_t
) 0;
114 proxy_context
->user_group
= (uid_t
) 0;
116 proxy_context
->user_dir
= NULL
;
117 proxy_context
->daemonize
= 0;
118 proxy_context
->tcp_only
= 0;
122 options_apply(ProxyContext
* const proxy_context
)
124 if (proxy_context
->resolver_ip
== NULL
) {
128 if (proxy_context
->provider_name
== NULL
||
129 *proxy_context
->provider_name
== 0) {
130 logger_noformat(proxy_context
, LOG_ERR
, "Provider name required");
133 if (proxy_context
->provider_publickey_s
== NULL
) {
134 logger_noformat(proxy_context
, LOG_ERR
, "Provider key required");
137 if (dnscrypt_fingerprint_to_key(proxy_context
->provider_publickey_s
,
138 proxy_context
->provider_publickey
) != 0) {
139 logger_noformat(proxy_context
, LOG_ERR
, "Invalid provider key");
142 if (proxy_context
->daemonize
) {
146 if (proxy_context
->pid_file
!= NULL
&&
147 pid_file_create(proxy_context
->pid_file
,
148 proxy_context
->user_id
!= (uid_t
) 0) != 0) {
149 logger_error(proxy_context
, "Unable to create pid file");
152 if (proxy_context
->log_file
!= NULL
&&
153 (proxy_context
->log_fd
= open(proxy_context
->log_file
,
154 O_WRONLY
| O_APPEND
| O_CREAT
,
155 (mode_t
) 0600)) == -1) {
156 logger_error(proxy_context
, "Unable to open log file");
159 if (proxy_context
->log_fd
== -1 && proxy_context
->daemonize
) {
160 logger_open_syslog(proxy_context
);
166 options_parse(AppContext
* const app_context
,
167 ProxyContext
* const proxy_context
, int argc
, char *argv
[])
170 int option_index
= 0;
172 options_init_with_default(app_context
, proxy_context
);
173 while ((opt_flag
= getopt_long(argc
, argv
,
174 getopt_options
, getopt_long_options
,
175 &option_index
)) != -1) {
178 proxy_context
->local_ip
= optarg
;
181 proxy_context
->daemonize
= 1;
185 const unsigned long edns_payload_size
= strtoul(optarg
, &endptr
, 10);
187 if (*optarg
== 0 || *endptr
!= 0 ||
188 edns_payload_size
> DNS_MAX_PACKET_SIZE_UDP_RECV
) {
189 logger(proxy_context
, LOG_ERR
,
190 "Invalid EDNS payload size: [%s]", optarg
);
193 if (edns_payload_size
<= DNS_MAX_PACKET_SIZE_UDP_SEND
) {
194 proxy_context
->edns_payload_size
= (size_t) 0U;
196 proxy_context
->edns_payload_size
= (size_t) edns_payload_size
;
204 proxy_context
->provider_publickey_s
= optarg
;
207 proxy_context
->log_file
= optarg
;
211 const unsigned long connections_count_max
=
212 strtoul(optarg
, &endptr
, 10);
214 if (*optarg
== 0 || *endptr
!= 0 ||
215 connections_count_max
<= 0U ||
216 connections_count_max
> UINT_MAX
) {
217 logger(proxy_context
, LOG_ERR
,
218 "Invalid max number of active request: [%s]", optarg
);
221 proxy_context
->connections_count_max
=
222 (unsigned int) connections_count_max
;
226 proxy_context
->pid_file
= optarg
;
229 proxy_context
->resolver_ip
= optarg
;
232 proxy_context
->resolver_port
= optarg
;
236 const struct passwd
* const pw
= getpwnam(optarg
);
238 logger(proxy_context
, LOG_ERR
, "Unknown user: [%s]", optarg
);
241 proxy_context
->user_id
= pw
->pw_uid
;
242 proxy_context
->user_group
= pw
->pw_gid
;
243 proxy_context
->user_dir
= strdup(pw
->pw_dir
);
248 proxy_context
->provider_name
= optarg
;
251 proxy_context
->local_port
= optarg
;
254 proxy_context
->tcp_only
= 1;
264 options_apply(proxy_context
);
270 options_free(ProxyContext
* const proxy_context
)
272 free(proxy_context
->user_dir
);
273 proxy_context
->user_dir
= NULL
;