Skip to content

Commit

Permalink
support tcp protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
cofyc committed Nov 22, 2012
1 parent 723a512 commit 1e4c133
Show file tree
Hide file tree
Showing 11 changed files with 515 additions and 15 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ LIB_H = dnscrypt.h udp_request.h edns.h logger.h dnscrypt-proxy/src/libevent/inc

LIB_OBJS += dnscrypt.o
LIB_OBJS += udp_request.o
LIB_OBJS += tcp_request.o
LIB_OBJS += edns.o
LIB_OBJS += logger.o
LIB_OBJS += main.o
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ This is dnscrypt wrapper (server-side dnscrypt proxy), which helps to add dnscry
This software is modified from
[dnscrypt-proxy](https://github.com/opendns/dnscrypt-proxy).

Only udp protocol is supported now, tcp is work in progress.

INSTALLATION
============

Expand Down
2 changes: 1 addition & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
* support tcp protocol
* reconnect to resolver server?
* test framework?
2 changes: 2 additions & 0 deletions compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <sys/queue.h>
#include <netinet/tcp.h>

#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[1]))
#define COMPILER_ASSERT(X) (void) sizeof(char[(X) ? 1 : -1])
Expand Down
10 changes: 8 additions & 2 deletions dnscrypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#define DNSCRYPT_H

#include "compat.h"
#include <sys/queue.h>
#include <event2/event.h>
#include <event2/listener.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <event2/util.h>
#include <crypto_box.h>
#include <crypto_stream.h>
Expand Down Expand Up @@ -72,6 +74,7 @@

#include "edns.h"
#include "udp_request.h"
#include "tcp_request.h"
#include "rfc1035.h"
#include "logger.h"
#include "salsa20_random.h"
Expand All @@ -80,6 +83,8 @@

#define DNSCRYPT_QUERY_HEADER_SIZE \
(DNSCRYPT_MAGIC_HEADER_LEN + crypto_box_PUBLICKEYBYTES + crypto_box_HALF_NONCEBYTES + crypto_box_MACBYTES)
#define DNSCRYPT_RESPONSE_HEADER_SIZE \
(DNSCRYPT_MAGIC_HEADER_LEN + crypto_box_NONCEBYTES + crypto_box_MACBYTES)

#define DNSCRYPT_REPLY_HEADER_SIZE \
(DNSCRYPT_MAGIC_HEADER_LEN + crypto_box_HALF_NONCEBYTES * 2 + crypto_box_MACBYTES)
Expand All @@ -91,6 +96,8 @@ struct context {
ev_socklen_t resolver_sockaddr_len;
const char *resolver_address;
const char *listen_address;
struct evconnlistener *tcp_conn_listener;
struct event *tcp_accept_timer;
struct event *udp_listener_event;
struct event *udp_resolver_event;
evutil_socket_t udp_listener_handle;
Expand All @@ -107,7 +114,6 @@ struct context {

/* Process stuff. */
bool daemonize;
bool tcp_only;
char *user;
uid_t user_id;
gid_t user_group;
Expand Down
12 changes: 9 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ main(int argc, const char **argv)
OPT_STRING('r', "resolver-address", &c.resolver_address, "upstream dns resolver server (<address:port>)"),
OPT_STRING('u', "user", &c.user, "run as given user"),
OPT_BOOLEAN('d', "daemonize", &c.daemonize, "run as daemon (default: off)"),
/*OPT_BOOLEAN('t', "tcp-only", &c.tcp_only, "use tcp only (default: off)"),*/
OPT_BOOLEAN('V', "verbose", &verbose, "show verbose logs (specify more -VVV to increase verbosity)"),
OPT_STRING('l', "logfile", &c.logfile, "log file path (default: stdout)"),
OPT_BOOLEAN(0, "gen-provider-keypair", &gen_provider_keypair, "generate provider key pair"),
Expand Down Expand Up @@ -367,11 +366,13 @@ main(int argc, const char **argv)
exit(1);
}

if (udp_listern_bind(&c) != 0) {
if (udp_listener_bind(&c) != 0 ||
tcp_listener_bind(&c) != 0) {
exit(1);
}

if (udp_listener_start(&c) != 0) {
if (udp_listener_start(&c) != 0 ||
tcp_listener_start(&c) != 0) {
logger(LOG_ERR, "Unable to start udp listener.");
exit(1);
}
Expand All @@ -380,5 +381,10 @@ main(int argc, const char **argv)

event_base_dispatch(c.event_loop);

logger(LOG_INFO, "Stopping proxy");
udp_listener_stop(&c);
tcp_listener_stop(&c);
event_base_free(c.event_loop);

return 0;
}
Loading

0 comments on commit 1e4c133

Please sign in to comment.