2 #ifndef __DNSCRYPT_PROXY_H__
3 #define __DNSCRYPT_PROXY_H__ 1
11 #include <event2/event.h>
12 #include <event2/listener.h>
17 #include "dnscrypt_client.h"
20 #ifndef DNS_QUERY_TIMEOUT
21 # define DNS_QUERY_TIMEOUT 10
24 #define DNS_MAX_PACKET_SIZE_UDP_RECV (65536U - 20U - 8U)
25 #define DNS_MAX_PACKET_SIZE_UDP_NO_EDNS_SEND 512U
27 #if DNS_MAX_PACKET_SIZE_UDP_RECV > DNS_MAX_PACKET_SIZE_UDP_NO_EDNS_SEND
28 # define DNS_MAX_PACKET_SIZE_UDP DNS_MAX_PACKET_SIZE_UDP_RECV
30 # define DNS_MAX_PACKET_SIZE_UDP DNS_MAX_PACKET_SIZE_UDP_NO_EDNS_SEND
33 #ifndef DNS_DEFAULT_STANDARD_DNS_PORT
34 # define DNS_DEFAULT_STANDARD_DNS_PORT "53"
36 #ifndef DNS_DEFAULT_LOCAL_PORT
37 # define DNS_DEFAULT_LOCAL_PORT DNS_DEFAULT_STANDARD_DNS_PORT
39 #ifndef DNS_DEFAULT_RESOLVER_PORT
40 # define DNS_DEFAULT_RESOLVER_PORT "443"
43 #ifndef DEFAULT_RESOLVERS_LIST
45 # define DEFAULT_RESOLVERS_LIST "dnscrypt-resolvers.csv"
47 # define DEFAULT_RESOLVERS_LIST PKGDATADIR "/dnscrypt-resolvers.csv"
51 #ifndef DEFAULT_RESOLVER_NAME
52 # define DEFAULT_RESOLVER_NAME NULL
55 #define DNS_HEADER_SIZE 12U
56 #define DNS_FLAGS_TC 2U
57 #define DNS_FLAGS_QR 128U
58 #define DNS_FLAGS2_RA 128U
60 #define DNS_CLASS_IN 1U
61 #define DNS_TYPE_TXT 16U
62 #define DNS_TYPE_OPT 41U
64 #define DNS_OFFSET_QUESTION DNS_HEADER_SIZE
65 #define DNS_OFFSET_FLAGS 2U
66 #define DNS_OFFSET_FLAGS2 3U
67 #define DNS_OFFSET_QDCOUNT 4U
68 #define DNS_OFFSET_ANCOUNT 6U
69 #define DNS_OFFSET_NSCOUNT 8U
70 #define DNS_OFFSET_ARCOUNT 10U
72 #define DNS_OFFSET_EDNS_TYPE 0U
73 #define DNS_OFFSET_EDNS_PAYLOAD_SIZE 2U
75 #define DNS_DEFAULT_EDNS_PAYLOAD_SIZE 1252U
77 #define DNSCRYPT_PROTOCOL_VERSIONS "2"
79 #define DNSCRYPT_EXIT_CERT_NOCERTS 2
80 #define DNSCRYPT_EXIT_CERT_TIMEOUT 3
81 #define DNSCRYPT_EXIT_CERT_MARGIN 4
83 typedef TAILQ_HEAD(TCPRequestQueue_
, TCPRequest_
) TCPRequestQueue
;
84 typedef TAILQ_HEAD(UDPRequestQueue_
, UDPRequest_
) UDPRequestQueue
;
86 typedef struct ProxyContext_
{
87 uint8_t dnscrypt_magic_query
[DNSCRYPT_MAGIC_QUERY_LEN
];
88 uint8_t provider_publickey
[crypto_sign_ed25519_PUBLICKEYBYTES
];
89 uint8_t resolver_publickey
[crypto_box_PUBLICKEYBYTES
];
90 DNSCryptClient dnscrypt_client
;
91 CertUpdater cert_updater
;
92 struct sockaddr_storage local_sockaddr
;
93 struct sockaddr_storage resolver_sockaddr
;
94 TCPRequestQueue tcp_request_queue
;
95 UDPRequestQueue udp_request_queue
;
96 AppContext
*app_context
;
97 struct event_base
*event_loop
;
99 const char *client_key_file
;
100 const char *local_ip
;
101 const char *log_file
;
102 const char *pid_file
;
103 const char *provider_name
;
104 const char *provider_publickey_s
;
105 const char *resolvers_list
;
106 const char *resolver_name
;
107 const char *resolver_ip
;
108 struct evconnlistener
*tcp_conn_listener
;
109 struct event
*tcp_accept_timer
;
110 struct event
*udp_listener_event
;
111 struct event
*udp_proxy_resolver_event
;
113 ev_socklen_t local_sockaddr_len
;
114 ev_socklen_t resolver_sockaddr_len
;
115 size_t edns_payload_size
;
116 size_t udp_current_max_size
;
118 evutil_socket_t tcp_listener_handle
;
119 evutil_socket_t udp_listener_handle
;
120 evutil_socket_t udp_proxy_resolver_handle
;
125 time_t test_cert_margin
;
126 unsigned int connections_count
;
127 unsigned int connections_count_max
;
130 _Bool ephemeral_keys
;
131 _Bool listeners_started
;
136 int dnscrypt_proxy_start_listeners(ProxyContext
* const proxy_context
);