Skip to content

Commit

Permalink
assign a new id instead of using client query id
Browse files Browse the repository at this point in the history
  • Loading branch information
clowwindy committed Feb 4, 2015
1 parent 3770bd9 commit 7c02e76
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/chinadns.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ typedef struct {

typedef struct {
uint16_t id;
uint16_t old_id;
struct sockaddr *addr;
socklen_t addrlen;
} id_addr_t;
Expand Down Expand Up @@ -598,8 +599,23 @@ static void dns_handle_local() {
query_id = ns_msg_id(msg);
question_hostname = hostname_from_question(msg);
LOG("request %s\n", question_hostname);

// assign a new id
uint16_t new_id;
do {
struct timeval tv;
gettimeofday(&tv, 0);
int randombits = (tv.tv_sec << 8) ^ tv.tv_usec;
new_id = randombits & 0xffff;
} while (queue_lookup(new_id));

uint16_t ns_new_id = htons(new_id);
memcpy(global_buf, &ns_new_id, 2);

id_addr_t id_addr;
id_addr.id = query_id;
id_addr.id = new_id;
id_addr.old_id = query_id;

id_addr.addr = src_addr;
id_addr.addrlen = src_addrlen;
queue_add(id_addr);
Expand Down Expand Up @@ -667,7 +683,6 @@ static void dns_handle_remote() {
return;
}
// parse DNS query id
// TODO assign new id instead of using id from clients
query_id = ns_msg_id(msg);
question_hostname = hostname_from_question(msg);
if (question_hostname) {
Expand All @@ -678,6 +693,8 @@ static void dns_handle_remote() {
id_addr_t *id_addr = queue_lookup(query_id);
if (id_addr) {
id_addr->addr->sa_family = AF_INET;
uint16_t ns_old_id = htons(id_addr->old_id);
memcpy(global_buf, &ns_old_id, 2);
r = should_filter_query(msg, ((struct sockaddr_in *)src_addr)->sin_addr);
if (r == 0) {
if (verbose)
Expand Down Expand Up @@ -714,7 +731,6 @@ static void queue_add(id_addr_t id_addr) {

static id_addr_t *queue_lookup(uint16_t id) {
int i;
// TODO assign new id instead of using id from clients
for (i = 0; i < ID_ADDR_QUEUE_LEN; i++) {
if (id_addr_queue[i].id == id)
return id_addr_queue + i;
Expand Down

0 comments on commit 7c02e76

Please sign in to comment.