Skip to content

Commit

Permalink
1. fix crash bug of Issues #21 and #38
Browse files Browse the repository at this point in the history
2. reduce the memory alloc
3. forward to 119.29.29.29
4. htons -> ntohs when get offset
5. enable the feature of prefetch
6. fix bug that NS RR couldn't refresh
  • Loading branch information
jfb8856606 committed Jan 29, 2016
1 parent 03dfbfb commit c4c2e14
Show file tree
Hide file tree
Showing 9 changed files with 5,488 additions and 3,486 deletions.
8,908 changes: 5,442 additions & 3,466 deletions root.z

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions src/author.c
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ cb_read_auth(struct epoll_event *ev, struct sockinfo *si)
mbuf_type *mbuf = mbuf_alloc();
if (NULL == mbuf)
return -1;
memset(mbuf, 0, sizeof(mbuf_type));
mbuf->fd = ev->data.fd;
mbuf->buf = si->buf;
mbuf->buflen = BIG_MEM_STEP;
Expand Down Expand Up @@ -658,6 +659,7 @@ launch_new_query(struct author *author/*, int idrowback*/)
for (i = start; i < end; i++) {
slotoff = 0;
typeoff = 0;
mbuf = NULL;
ret = htable_find_list_io(author->s->qlist, i, slotoff, &typeoff, (uchar **)&mbuf);
while (ret >= 0)
{
Expand Down Expand Up @@ -790,6 +792,7 @@ handle_back_event(struct author *author)
if (ret <= 0)
break;
for (i = 0; i < ret; i++) {
memset(&si, 0, sizeof(struct sockinfo));
si.buf = buf;
if (e[i].data.fd == bf) {
si.socktype = UDP;
Expand All @@ -814,7 +817,7 @@ handle_back_event(struct author *author)
author->eptcpfds[e[i].data.fd].ret = 0;
close(e[i].data.fd);
ev.data.fd = e[i].data.fd;
mbuf_free(si.mbuf);
// mbuf_free(si.mbuf);
epoll_ctl(epfd, EPOLL_CTL_DEL, ev.data.fd, &ev);
} else {
rx = passer_auth_data(author, buf, &si);
Expand Down Expand Up @@ -922,6 +925,7 @@ check_ttl_expire(struct author *author)
tn = delete_node(rbt, pn);
pthread_spin_unlock(&rbt->lock);
if (tn != NULL) {
memset(mbuf, 0, sizeof(mbuf_type));
mbuf->qname = tn->type; //type
mbuf->qtype = tn->type;
mbuf->dlen = tn->dlen;
Expand All @@ -939,7 +943,7 @@ check_ttl_expire(struct author *author)
mbuf->fd = -1;
init_qoutinfo(mbuf);
ret = htable_insert_list(author->s->qlist, tn->data, tn->dlen, tn->type, (uchar *)mbuf, 0, NULL, tn->hash); //not replace
if (0 == ret)
if (HTABLE_INSERT_RET_NORMAL == ret)
{
mbuf = mbuf_alloc();
if (NULL == mbuf)
Expand Down Expand Up @@ -1103,7 +1107,7 @@ lock_and_add_to_quizz(mbuf_type *mbuf, struct fetcher *f)
init_qoutinfo(mbuf);

ret = htable_insert_list(f->s->qlist, mbuf->lowerdomain.domain, mbuf->dlen, mbuf->qtype, (uchar *)mbuf, 0, NULL, &(mbuf->lowerdomain.hash[0])); //has same one, qeurying
if (ret != 0)
if (ret != HTABLE_INSERT_RET_NORMAL)
{
return -1;
}
Expand Down
19 changes: 12 additions & 7 deletions src/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ get_domain_from_msg(uchar * itor, uchar * hdr, uchar * to, int *tmplen)
len = itor[0];
int dlen = 0;
int hasptr = 0, infinite = 20;
offset = htons((ushort) * (ushort *) itor);
offset = ntohs((ushort) * (ushort *) itor);
*tmplen = 0;
while ((len != 0) && (infinite--)) {
if (IS_PTR(offset)) {
Expand All @@ -163,7 +163,7 @@ get_domain_from_msg(uchar * itor, uchar * hdr, uchar * to, int *tmplen)
dlen += *tmplen;
}
hasptr = 1;
offset = htons((ushort) * (ushort *) itor);
offset = ntohs((ushort) * (ushort *) itor);
continue;
}
to[0] = itor[0];
Expand All @@ -176,7 +176,7 @@ get_domain_from_msg(uchar * itor, uchar * hdr, uchar * to, int *tmplen)
to += itor[0];
itor = itor + itor[0] + 1;
len = itor[0];
offset = htons((ushort) * (ushort *) itor);
offset = ntohs((ushort) * (ushort *) itor);
}
if (infinite <= 0) //loops error
return -1;
Expand Down Expand Up @@ -291,7 +291,7 @@ process_rdata(struct hlpp * hlp, uchar * label, int n)
tx = global_now; ///
dm[0] = dm[1] = 0;
//if(hlp->section != AN_SECTION) //see header comments.
rbt = NULL;
// rbt = NULL;
for (i = 0; i < n; i++) {
dlen = get_domain_from_msg(label, hdr, tmpdomain, &tmplen);
if (dm[0] == 0 && dm[1] == 0) //first time
Expand Down Expand Up @@ -522,11 +522,11 @@ insert_kv_mem(struct rbtree *rbt, struct htable *ds, uchar * k, int klen,
memcpy(val, v, vlen);
mv = (struct mvalue *) v;
ret = htable_insert(ds + idx, k, klen, type, val, 1, &tmp, hash); //mem, replace
if (ret == 2) {
if (ret >= HTABLE_INSERT_RET_NEVER_EXPIRE) {
free(val);
}
if (rbt) {
if (ret != 0) {
if (ret == HTABLE_INSERT_RET_REPLACE) {
pthread_spin_lock(&rbt->lock);
tn.dlen = klen;
//tmp get old data
Expand Down Expand Up @@ -555,7 +555,10 @@ insert_kv_mem(struct rbtree *rbt, struct htable *ds, uchar * k, int klen,
}
//data exists in htable, delete it in ttl tree, then insert
pthread_spin_lock(&rbt->lock);
ret = insert_into_ttltree(rbt, k, klen, type, mv->ttl, lowerdomain); //ttl expired tree
if (type != NS && ret == HTABLE_INSERT_RET_REPLACE)
ret = insert_into_ttltree(rbt, k, klen, type, mv->ttl, lowerdomain); //ttl expired tree
else
ret = insert_into_ttltree(rbt, k, klen, type, tmp.ttl, lowerdomain); //ttl expired tree
pthread_spin_unlock(&rbt->lock);
return 0;
}
Expand Down Expand Up @@ -1276,6 +1279,7 @@ fill_extra_addr(uchar * ip)
mv->ttl = 0;
mv->hits = 0;
mv->len = 0;
mv->seg = 0;
for (i = 0; i < n; i++) {
if (make_bin_from_str(ip, extra[i]) == 0) {
mv->num++;
Expand All @@ -1288,6 +1292,7 @@ fill_extra_addr(uchar * ip)
mv->ttl = 0;
mv->hits = 0;
mv->len = 0;
mv->seg = 0;
return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions src/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ cb_get_tcp_msg(struct event_data *data, void *v, int idx)
if (NULL == mbuf) {
return 0;
}
memset(mbuf, 0, sizeof(mbuf_type));

mc = f[idx].mc;
pthread_spin_lock(&mc->lock);
Expand Down Expand Up @@ -202,6 +203,7 @@ cb_get_udp_msg(struct event_data *data, void *v, int idx)
if (NULL == mbuf) {
return 0;
}
memset(mbuf, 0, sizeof(mbuf_type));
mc = f[idx].mc;
pthread_spin_lock(&mc->lock);
if ((mc->tail + 8 > mc->head && mc->tail < mc->head) ||
Expand Down
6 changes: 3 additions & 3 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,14 +410,14 @@ main(int argc, char **argv)
// add default dns server 8.8.8.8, 114.114.114.114
if (g_nameservers[0] == NULL) {
assert(g_nameservers[1] == NULL);
g_nameservers[0] = strdup("8.8.8.8");
g_nameservers[0] = strdup("119.29.29.29");
g_nameservers[1] = strdup("8.8.4.4");
}
if (g_nameservers[1] == NULL) {
if (strcmp(g_nameservers[0], "8.8.8.8") == 0) {
if (strcmp(g_nameservers[0], "119.29.29.29") == 0) {
g_nameservers[1] = strdup("8.8.4.4");
} else {
g_nameservers[1] = strdup("8.8.8.8");
g_nameservers[1] = strdup("119.29.29.29");
}
}
//
Expand Down
2 changes: 1 addition & 1 deletion src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ create_transfer_point(uchar * name, struct htable *fwd, int n)
v = malloc(mv->len + sizeof(struct mvalue));
memcpy(v, vbuffer, mv->len + sizeof(struct mvalue));
ret = htable_insert(fwd, name, dlen, A, v, 0, NULL, &hash);
assert(ret >= 0);
assert(ret == HTABLE_INSERT_RET_NORMAL);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion src/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include <netinet/in.h>
#include <assert.h>

#define MEMPOOL_SIZE (262144)
#define MEMPOOL_SIZE (65536) //(262144)

#define RING_SP_ENQ 0x0001
#define RING_SC_DEQ 0x0002
Expand Down
18 changes: 13 additions & 5 deletions src/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -699,25 +699,33 @@ int append_value_to_he(struct hentry *he, uchar *val, int type, int replace,
*mv = *(struct mvalue *) (*oval);
if ((mv != NULL) && (mv->ttl != (MAX_TTL + 1)))
{
/*
* 如果替换的为NS类型,则TTL使用原有NS记录的TTL
* 否则授权与根返回的NS不一致时,会无法刷新NS
*/
if (NS == type)
{
((struct mvalue *)val)->ttl = mv->ttl;
}
free(*oval);
*oval = val;
ret = 1;
ret = HTABLE_INSERT_RET_REPLACE;
}
else
{
ret = 2;
ret = HTABLE_INSERT_RET_NEVER_EXPIRE;
}
}
else
{
ret = -1;
ret = HTABLE_INSERT_RET_NO_REPLACE;
}
}
else
{
he->count++;
*oval = val;
ret = 0;
ret = HTABLE_INSERT_RET_NORMAL;
}
return ret;
}
Expand Down Expand Up @@ -781,7 +789,7 @@ htable_insert(struct htable *ht, uchar * key, int klen, int type, uchar * val, i
pthread_spin_lock(&ht->lock);
ht->now++;
pthread_spin_unlock(&ht->lock);
return 0;
return HTABLE_INSERT_RET_NORMAL;
}

int
Expand Down
7 changes: 7 additions & 0 deletions src/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ enum {
};


enum htable_insert_ret{
HTABLE_INSERT_RET_NORMAL = 0,
HTABLE_INSERT_RET_REPLACE,
HTABLE_INSERT_RET_NEVER_EXPIRE,
HTABLE_INSERT_RET_NO_REPLACE,
};

//used by memory hash and disk db
struct mvalue {
uint16_t len;
Expand Down

0 comments on commit c4c2e14

Please sign in to comment.