Exemplo n.º 1
0
//ht,type,domain,dlen
int
find_addr(struct htable *fwd, struct htable *ht, mbuf_type *mbuf,
          uchar * ip, int forward)
{
    int ret, xlen = 0, dbg = 100;
    int first_query, i;
    struct mvalue *mv = NULL;
    uchar *td, *buffer = mbuf->tempbuffer, *itor = NULL, *glue = NULL;
    int td_len, diff_len;
    int ori_flag = 0;
    int root_flag = 0;
    hashval_t thash, *hash;
    int label_count = 0;
    
    if (mbuf->qtimes > (MAX_TRY_TIMES - 3)) {
        fill_extra_addr(ip);
        return 0;
    }
    
    ret = pre_find(mbuf, fwd, ht, ip);
    if (ret > 0)                //find fwd
        return 0;
    else if (ret < 0)                //error
        return ret;
    else {
        if (forward) {
            fill_extra_addr(ip);
            return 0;
        }
    }
    //now we have domain or latest cname in qo->qing
    //point to qo->td or qo->qbuffer
    td = mbuf->qing;
    itor = td;
    hash = mbuf->qhash;
    td_len = mbuf->qlen;
    if (mbuf->hascname)
        ori_flag = 1;
    while (1)                   //put ns in itor(buffer), put ns'a in iitor(ip)
    {
        while (1) {
            ret = find_record_with_ttl(ht, itor, td_len, NS, buffer, IP_DATA_LEN, NULL, hash);    //ns do not
            if (ret > 0)
                break;
            if ((dbg--) == 0)   //if mess buffer
                return -1;
            if (ori_flag)
            {
                diff_len = itor[0] + 1;
                itor = itor + diff_len;  //parent, assert itor[1] < 64
                if (itor[0] == 0)   //root
                {
                    if (root_flag == 0)
                    {
                        itor[1] = '\0';
                        root_flag = 1;
                        td_len = 2;
                    }
                    else
                        return -1;
                }
                else
                {
                    td_len -= diff_len;
                }
                thash = 0;
                hash = &thash;
            }
            else
            {
                label_count++;
                if (label_count > mbuf->lowerdomain.label_count)
                    return -1;
                
                if (label_count == mbuf->lowerdomain.label_count) // root
                {
                    itor = mbuf->lowerdomain.label[label_count - 1] + mbuf->lowerdomain.label_len[label_count - 1];
                    itor[1] = '\0';
                    td_len = 2;
                    thash = 0;
                    hash = &thash;
                }
                else
                {
                    itor = mbuf->lowerdomain.label[label_count];
                    td_len = mbuf->lowerdomain.label_len[label_count];
                    hash = &(mbuf->lowerdomain.hash[label_count]);
                }
            }
        }
        mv = (struct mvalue *) buffer;  //ns record in buffer
        glue = itor;            //data in td, real domain we get ns //key
        itor = buffer + sizeof(struct mvalue);  //data //value
        ret = retrive_ip(mbuf, itor, mv->num, ip, ht, &first_query);
        if ((ret > 0)) {
            if ((ret < mv->num) && (mbuf->qns == 1)) {
                mbuf->qns = 0;
                for (i = 0; i < first_query; i++) {
                    xlen = strlen((const char *)itor) + 1;
                    itor = itor + xlen;
                }
            } else
                return 0;
        }
        if (is_glue(glue, itor) != 1) //domain and it's ns,should be use itor,not buffer + sizeof(struct mvalue)
        {
            if (!ori_flag)
                ori_flag = 1;
            {
                xlen = strlen((const char *)itor) + 1;    //ns len
                if (xlen > (QBUFFER_SIZE - 1) || itor[0] == 0 || itor[0] > xlen)
                    return -1;
                memcpy(mbuf->qbuffer, itor, xlen);
                mbuf->qbuffer_hash = 0;
                mbuf->qing = mbuf->qbuffer;
                mbuf->qhash = &(mbuf->qbuffer_hash);
                mbuf->qlen = xlen;
                hash = mbuf->qhash;
                td_len = mbuf->qlen;
                td = mbuf->qing;
            }
            itor = td;          //itor point to key now
        } else {                  //qbuffer and qing need NO change
            if (ori_flag)
            {
                diff_len = glue[0] + 1;
                itor = glue + diff_len;  //glue[0] is type,glue[1] is label length
                if (itor[0] == 0)   //root
                    return -1;
                td_len -= diff_len;
                thash = 0;
                hash = &thash;
            }
            else
            {
                label_count++;
                if (label_count >= mbuf->lowerdomain.label_count) // root
                    return -1;
                itor = mbuf->lowerdomain.label[label_count];
                td_len = mbuf->lowerdomain.label_len[label_count];
                hash = &(mbuf->lowerdomain.hash[label_count]);
            }
        }
        mbuf->qname = Q_NS;
        if ((dbg--) == 0)
            return -1;
    }
    return 0;
}
Exemplo n.º 2
0
//ht,type,domain,dlen
int find_addr(struct htable *fwd,struct htable *ht,struct qoutinfo *qo,uchar *ip)
{
 int ret,xlen = 0,dbg = 100,iplen = IP_DATA_LEN;
 int first_query,i;
 struct mvalue *mv = NULL;
 uchar td[512] = {0},buffer[IP_DATA_LEN] = {0},*itor = NULL,*glue = NULL;
 if(qo->qtimes > (MAX_TRY_TIMES - 3))
	{
	 fill_extra_addr(qo,ip);
	 return 0;
	}
 ret = pre_find(qo,fwd,ht,ip);
 if(ret > 0) //find fwd
	return 0;
 if(ret < 0) //error
	return ret;
 itor = td;
 //now we have domain or latest cname in qo->qing 
 //point to qo->td or qo->qbuffer
 memcpy(td + 1,qo->qing,strlen(qo->qing) + 1);
 td[0] = NS;
 while(1) //put ns in itor(buffer), put ns'a in iitor(ip)
	{
	 while(1)
		{
		 ret = find_record_with_ttl(ht,itor,buffer,IP_DATA_LEN,NULL); //ns do not
		 if(ret > 0)
			 break;
		 itor = itor + itor[1] + 1; //parent, assert itor[1] < 64
		 itor[0] = NS;
		 if(itor[1] == 0) //root
			return -1;
		 if((dbg --) == 0)//if mess buffer
			 return -1;
		}
	 mv = (struct mvalue*)buffer; //ns record in buffer
	 glue = itor; //data in td, real domain we get ns //key
	 itor = buffer + sizeof(struct mvalue); //data //value
	 ret = retrive_ip(itor,mv->num,ip,ht,&first_query);
	 if((ret > 0))
		{
		 if((ret < mv->num) && (qo->qns == 1))
			{
			 qo->qns = 0;
			 for(i = 0;i < first_query;i ++)
				{
				 xlen = strlen(itor) + 1;
				 itor = itor + xlen;
				}
			 xlen = strlen(itor) + 1;
			 memcpy(qo->qbuffer,itor,xlen);
			 qo->qing = qo->qbuffer;
			 memcpy(td + 1,qo->qbuffer,xlen);
			}
		 else
			return 0;
		}
	 if(is_glue(glue,buffer + sizeof(struct mvalue)) != 1) //domain and it's ns
		{
		 itor = buffer + sizeof(struct mvalue);
		 xlen = strlen(itor) + 1;//ns len
		 if(xlen > (QBUFFER_SIZE - 1))
			return -1;
		 memcpy(qo->qbuffer,itor,xlen);
		 qo->qing = qo->qbuffer;
		 memcpy(td + 1,qo->qbuffer,xlen);
		 itor = td; //itor point to key now
		}
	 else //qbuffer and qing need NO change
		itor = glue + glue[1] + 1; //glue[0] is type,glue[1] is label length
	 itor[0] = NS;
	 qo->qname = Q_NS;
	 if((dbg --) == 0)
		 return -1;
	}
 return 0;
}