uchar * fill_all_records_in_msg(struct hlpc * h, struct hlpf * hf, int *pidx) { int step = 0; uint16_t txtlen; uchar *tmp = NULL, *to = hf->to, *from = hf->from; struct fillmsg *fm = (struct fillmsg *) (hf->to); int idx = *pidx; fm->type = htons(hf->type); fm->dclass = htons(CLASS_IN); fm->ttl = htonl(hf->ttl - global_now); if (hf->ttl == MAX_TTL + 1) fm->ttl = htonl(hf->ttl - 1); to = to + sizeof(struct fillmsg); if (hf->type == A) step = 4; if (hf->type == AAAA) step = 16; switch (hf->type) // no soa { case A: //idx not used case AAAA: fm->len = htons(step); memcpy(to, from, step); to = to + step; //data break; case CNAME: case NS: idx++; *pidx = idx; h[idx].name = from; h[idx].off = to - hf->hdr; h[idx].ref = -1; h[idx].level = get_level(h[idx].name); h[idx].mt = 0; h[idx].len = hf->len; tmp = fill_name_in_msg(h, to, idx); fm->len = htons(tmp - to); to = tmp; break; case MX: memcpy(to, from, sizeof(uint16_t)); //ref from += sizeof(uint16_t); //2 to += sizeof(uint16_t); idx++; *pidx = idx; h[idx].name = from; h[idx].off = to - hf->hdr; h[idx].ref = -1; h[idx].level = get_level(h[idx].name); h[idx].mt = 0; h[idx].len = hf->len; tmp = fill_name_in_msg(h, to, idx); fm->len = htons(tmp - to + sizeof(uint16_t)); to = tmp; break; case TXT: txtlen = *(uint16_t *) from; from += sizeof(uint16_t); //len memcpy(to, from, txtlen); fm->len = htons(txtlen); to += txtlen; break; case SRV: memcpy(to, from, sizeof(uint16_t) * 3); from += sizeof(uint16_t) * 3; to = to + sizeof(uint16_t) * 3; idx++; *pidx = idx; h[idx].name = from; h[idx].off = to - hf->hdr; h[idx].ref = -1; h[idx].level = get_level(h[idx].name); h[idx].mt = 0; h[idx].len = hf->len; tmp = fill_name_in_msg(h, to, idx); fm->len = htons(tmp - to + sizeof(uint16_t) * 3); to = tmp; break; default: break; } return to; }
//jump from author.c uchar * fill_rrset_in_msg(struct hlpc * h, uchar * from, uchar * to, int *pn, uchar * hdr) { uchar type; int i, step = 0; uint16_t txtlen = 0; struct hlpf hf; int num = 0; struct mvalue *mv = NULL; int n = *pn; type = from[0]; from++; //type mv = (struct mvalue *) from; from = from + sizeof(struct mvalue); num = mv->num; if (num > MAX_MSG_SEG) { num = MAX_MSG_SEG; } hf.hdr = hdr; hf.ttl = mv->ttl; hf.type = type; if (type == A) step = 4; if (type == AAAA) step = 16; switch (type) //7 { case A: case AAAA: for (i = 0; i < num; i++) { to = fill_name_in_msg(h, to, n); hf.from = from; hf.to = to; //jump type and dclass //then we get ttl's position //plus hdr we get it's offset //only for A record to = fill_all_records_in_msg(h, &hf, pn); from += step; } return to; break; case CNAME: // cname must has 1 record to = fill_name_in_msg(h, to, n); hf.from = from; hf.to = to; hf.len = strlen((const char *)from) + 1; to = fill_all_records_in_msg(h, &hf, pn); return to; break; case NS: for (i = 0; i < num; i++) { // h[n].len = strlen((const char *)from) + 1; to = fill_name_in_msg(h, to, n); hf.from = from; hf.to = to; hf.len = strlen((const char *)from) + 1; to = fill_all_records_in_msg(h, &hf, pn); from += hf.len;//strlen((const char *)from) + 1; } return to; break; case MX: for (i = 0; i < num; i++) { to = fill_name_in_msg(h, to, n); hf.from = from; hf.to = to; hf.len = strlen((const char *)(from + sizeof(uint16_t))) + 1; to = fill_all_records_in_msg(h, &hf, pn); from += sizeof(uint16_t); //jump ref from += hf.len;//strlen((const char *)from) + 1; //jump name and tail 0 } return to; break; case TXT: for (i = 0; i < num; i++) { txtlen = *(uint16_t *) from; h[n].len = txtlen; to = fill_name_in_msg(h, to, n); hf.from = from; hf.to = to; to = fill_all_records_in_msg(h, &hf, pn); from = from + txtlen + sizeof(uint16_t); } return to; break; case SRV: for (i = 0; i < num; i++) { to = fill_name_in_msg(h, to, n); hf.from = from; hf.to = to; hf.len = strlen((const char *)(from + sizeof(uint16_t) * 3)) + 1; to = fill_all_records_in_msg(h, &hf, pn); from += sizeof(uint16_t) * 3; //pri wei port from += hf.len;//strlen((const char *)from) + 1; //target } return to; break; default: printf("not support or error in fill msg\n"); break; } return NULL; }
//jump from author.c uchar *fill_rrset_in_msg(struct hlpc *h,uchar *from,uchar *to,int n,uchar *hdr,uint16_t *ttloff) { uchar type; type = from[0]; int i,step = 0,ttloffidx; uint16_t txtlen = 0; struct hlpf hf; struct mvalue *mv = NULL; ttloffidx = ttloff[0]; //must be 0 from ++;//type mv = (struct mvalue*)from; from = from + sizeof(struct mvalue); hf.hdr = hdr; hf.ttl = mv->ttl; hf.type = type; if(type == A) step = 4; if(type == AAAA) step = 16; switch(type) //7 { case A: case AAAA: for(i = 0;i < mv->num;i ++) { to = fill_name_in_msg(h,to,n); hf.from = from; hf.to = to; //jump type and dclass //then we get ttl's position //plus hdr we get it's offset //only for A record if(type == A) { //ttloff[0] idx //+1, jump it ttloff[ttloff[0] + 1] = to + sizeof(uint16_t) + sizeof(uint16_t) - hdr; ttloff[0] ++; } to = fill_all_records_in_msg(h,&hf,n); from += step; } return to; break; case CNAME: // cname must has 1 record to = fill_name_in_msg(h,to,n); hf.from = from; hf.to = to; to = fill_all_records_in_msg(h,&hf,n); return to; break; case NS: for(i = 0;i < mv->num;i ++) { to = fill_name_in_msg(h,to,n); hf.from = from; hf.to = to; to = fill_all_records_in_msg(h,&hf,n); from += strlen(from) + 1; } return to; break; case MX: for(i = 0;i < mv->num;i ++) { to = fill_name_in_msg(h,to,n); hf.from = from; hf.to = to; to = fill_all_records_in_msg(h,&hf,n); from += strlen(from) + 1; //ref and name } return to; break; case TXT: for(i = 0;i < mv->num;i ++) { to = fill_name_in_msg(h,to,n); hf.from = from; hf.to = to; to = fill_all_records_in_msg(h,&hf,n); txtlen = *(uint16_t*)from; from = from + txtlen + sizeof(uint16_t); } return to; break; case SRV: for(i = 0;i < mv->num;i ++) { to = fill_name_in_msg(h,to,n); hf.from = from; hf.to = to; to = fill_all_records_in_msg(h,&hf,n); from += sizeof(uint16_t) * 3; //pri wei port from += strlen(from) + 1; //target } return to; break; default: printf("not support or error in fill msg\n"); break; } return NULL; }