Skip to content

Commit

Permalink
convert proxy over to evdns
Browse files Browse the repository at this point in the history
  • Loading branch information
provos committed May 27, 2007
1 parent 8dccbcb commit 7b2c6a0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
46 changes: 29 additions & 17 deletions honeyd/subsystems/proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

#include <pcre.h>
#include <event.h>
#include <dnsres.h>
#include <evdns.h>
#include <dnet.h>

#include "util.h"
Expand All @@ -45,7 +45,6 @@
#include "smtp.h"
#include "honeyd_overload.h"

extern struct dnsres dnsres;
extern int debug;

#define DFPRINTF(x, y) do { \
Expand Down Expand Up @@ -218,7 +217,7 @@ proxy_remote_readcb(struct bufferevent *bev, void *arg)
{
struct proxy_ta *ta = arg;
struct evbuffer *buffer = EVBUFFER_INPUT(bev);
char *data = EVBUFFER_DATA(buffer);
unsigned char *data = EVBUFFER_DATA(buffer);
size_t len = EVBUFFER_LENGTH(buffer);

bufferevent_write(ta->bev, data, len);
Expand Down Expand Up @@ -365,16 +364,22 @@ proxy_connect(struct proxy_ta *ta, char *host, int port)
}

void
proxy_handle_get_cb(struct dnsres_hostent *he, int error, void *arg)
proxy_handle_get_cb(int result, char type, int count, int ttl,
void *addresses, void *arg)
{
struct proxy_ta *ta = arg;
struct addr addr;
struct in_addr *in_addrs = addresses;
int port = atoi(kv_find(&ta->dictionary, "$port"));
char *response;

ta->dnsres_handle = NULL;
if (ta->dns_canceled) {
proxy_ta_free(ta);
return;
}
ta->dns_pending = 0;

if (he == NULL || he->h_length != IP_ADDR_LEN) {
if (result != DNS_ERR_NONE || type != DNS_IPv4_A || count == 0) {
response = proxy_response(ta, baddomain);
bufferevent_write(ta->bev, response, strlen(response));
ta->wantclose = 1;
Expand All @@ -384,8 +389,7 @@ proxy_handle_get_cb(struct dnsres_hostent *he, int error, void *arg)
/* Need to make a connection here */
bufferevent_disable(ta->bev, EV_READ);

addr_pack(&addr, ADDR_TYPE_IP, IP_ADDR_BITS,
he->h_addr_list[0], IP_ADDR_LEN);
addr_pack(&addr, ADDR_TYPE_IP, IP_ADDR_BITS, &in_addrs[0], IP_ADDR_LEN);
proxy_connect(ta, addr_ntoa(&addr), port);
}

Expand Down Expand Up @@ -421,24 +425,29 @@ proxy_handle_get(struct proxy_ta *ta)
}

/* Try to resolve the domain name */
ta->dnsres_handle = dnsres_gethostbyname(&dnsres,
kv_find(&ta->dictionary, "$host"),
evdns_resolve_ipv4(kv_find(&ta->dictionary, "$host"), 0,
proxy_handle_get_cb, ta);
ta->dns_pending = 1;
return (0);
}

void
proxy_handle_connect_cb(struct dnsres_hostent *he, int error, void *arg)
proxy_handle_connect_cb(int result, char type, int count, int ttl,
void *addresses, void *arg)
{
struct proxy_ta *ta = arg;
char *host = kv_find(&ta->dictionary, "$host");
int port = atoi(kv_find(&ta->dictionary, "$port"));
char *response;
fprintf(stderr, "Connecting to %s port %d\n", host, port);

ta->dnsres_handle = NULL;
if (ta->dns_canceled) {
proxy_ta_free(ta);
return;
}
ta->dns_pending = 0;

if (he == NULL) {
if (result != DNS_ERR_NONE) {
response = proxy_response(ta, baddomain);
bufferevent_write(ta->bev, response, strlen(response));
ta->wantclose = 1;
Expand Down Expand Up @@ -505,9 +514,9 @@ proxy_handle_connect(struct proxy_ta *ta)
}

/* Try to resolve the domain name */
ta->dnsres_handle = dnsres_gethostbyname(&dnsres,
kv_find(&ta->dictionary, "$host"),
evdns_resolve_ipv4(kv_find(&ta->dictionary, "$host"), 0,
proxy_handle_connect_cb, ta);
ta->dns_pending = 1;
return (0);
}

Expand Down Expand Up @@ -672,8 +681,11 @@ proxy_ta_free(struct proxy_ta *ta)
{
struct keyvalue *entry;

if (ta->dnsres_handle != NULL)
dnsres_cancel_lookup(ta->dnsres_handle);
if (ta->dns_pending && !ta->dns_canceled) {
/* if we have a pending dns lookup, tell it to cancel */
ta->dns_canceled = 1;
return;
}

while ((entry = TAILQ_FIRST(&ta->dictionary)) != NULL) {
TAILQ_REMOVE(&ta->dictionary, entry, next);
Expand Down
3 changes: 2 additions & 1 deletion honeyd/subsystems/proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ struct proxy_ta {

int (*empty_cb)(struct proxy_ta *);

void *dnsres_handle; /* used to cancel a pending callback */
int dns_pending;
int dns_canceled;
};

struct proxy_ta *proxy_ta_new(int fd, struct sockaddr *sa, socklen_t salen,
Expand Down
5 changes: 2 additions & 3 deletions honeyd/subsystems/proxy_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include <err.h>

#include <event.h>
#include <dnsres.h>
#include <evdns.h>

#include "util.h"
#include "proxy.h"
Expand All @@ -49,7 +49,6 @@ extern FILE *flog_email; /* log SMTP transactions somewhere */
extern const char *log_datadir; /* log the email transactions somewhere */

int debug;
struct dnsres dnsres; /* dns lookups */

static void
usage(char *progname)
Expand Down Expand Up @@ -123,7 +122,7 @@ main(int argc, char **argv)

event_init();

dnsres_init(&dnsres);
evdns_init();

if (ports == NULL) {
/* Just a single port to connect to */
Expand Down

0 comments on commit 7b2c6a0

Please sign in to comment.