简化这段代码unsigned int rr2buf(char *o, dns_rr* rr) { int i = 0; uint16_t temp; uint32_t temp32; temp = htons(49164); memcpy(o, &temp, sizeof(short)); o+=2; temp=htons(rr->type); memcpy(o, &temp, sizeof(short)); o+=2; temp=htons(rr->rclass); memcpy(o, &temp, sizeof(short)); o+=2; temp32=htonl(rr->ttl); memcpy(o, &temp32, (2*sizeof(short))); o+=4; temp=htons(rr->data_len); memcpy(o, &temp, sizeof(short)); o+=2; if(rr->type == MX_TYPE){ temp=htons(1); memcpy(o, &temp, sizeof(short)); o+=2; } if(rr->type == A_TYPE){ uint32_t ipAddr = inet_addr(rr->rdata); memcpy(o, &ipAddr,rr->data_len); o+=rr->data_len; return 16; } else if(rr->type == CNAME_TYPE){ char* ini = o; uint8_t count = 0; int i = 0; int j = 1; int tempts = 0; o++; while(1){ if(rr->rdata[i] == '.'){ memcpy(o-count-1, &count, sizeof(char)); count = 0; o++; i++; tempts = 1; } else if(rr->rdata[i] == '\0'){ memcpy(o, &(rr->rdata[i]), sizeof(char)); memcpy(o-count-1, &count, sizeof(char)); count = 0; break; } else{ memcpy(o, &(rr->rdata[i]), sizeof(char)); o++; i++; count++; } } return 12 + rr->data_len + 1; } else if(rr->type == MX_TYPE){ char* ini = o; uint8_t count = 0; int i = 0; int j = 1; int tempts = 0; o++; while(1){ if(rr->rdata[i] == '.'){ memcpy(o-count-1, &count, sizeof(char)); count = 0; o++; i++; tempts = 1; break; } else if(rr->rdata[i] == '\0'){ memcpy(o, &(rr->rdata[i]), sizeof(char)); memcpy(o-count-1, &count, sizeof(char)); count = 0; break; } else{ memcpy(o, &(rr->rdata[i]), sizeof(char)); o++; i++; count++; } } o--; temp = htons(49164); //这里指代1100000000001100,DNS报文中压缩指针的操作 memcpy(o, &temp, sizeof(short)); return 16+i; } }
时间: 2024-03-06 20:46:40 浏览: 184
以下是我简化后的代码,主要是将重复的代码提出来作为函数,并将一些重复操作合并在一起。
```
unsigned int rr2buf(char *o, dns_rr* rr) {
uint16_t temp = htons(49164);
memcpy(o, &temp, sizeof(short));
o += 2;
temp = htons(rr->type);
memcpy(o, &temp, sizeof(short));
o += 2;
temp = htons(rr->rclass);
memcpy(o, &temp, sizeof(short));
o += 2;
uint32_t temp32 = htonl(rr->ttl);
memcpy(o, &temp32, (2*sizeof(short)));
o += 4;
temp = htons(rr->data_len);
memcpy(o, &temp, sizeof(short));
o += 2;
if (rr->type == A_TYPE) {
return writeARecord(o, rr);
} else if (rr->type == CNAME_TYPE) {
return writeCNAMERecord(o, rr);
} else if (rr->type == MX_TYPE) {
return writeMXRecord(o, rr);
}
return 0;
}
unsigned int writeARecord(char* o, dns_rr* rr) {
uint32_t ipAddr = inet_addr(rr->rdata);
memcpy(o, &ipAddr, rr->data_len);
return 16;
}
unsigned int writeCNAMERecord(char* o, dns_rr* rr) {
char* ini = o;
uint8_t count = 0;
int i = 0;
int j = 1;
int tempts = 0;
o++;
while(1) {
if (rr->rdata[i] == '.') {
memcpy(o-count-1, &count, sizeof(char));
count = 0;
o++;
i++;
tempts = 1;
} else if (rr->rdata[i] == '\0') {
memcpy(o, &(rr->rdata[i]), sizeof(char));
memcpy(o-count-1, &count, sizeof(char));
count = 0;
break;
} else {
memcpy(o, &(rr->rdata[i]), sizeof(char));
o++;
i++;
count++;
}
}
return 12 + rr->data_len + 1;
}
unsigned int writeMXRecord(char* o, dns_rr* rr) {
char* ini = o;
uint8_t count = 0;
int i = 0;
int j = 1;
int tempts = 0;
o++;
while(1) {
if (rr->rdata[i] == '.') {
memcpy(o-count-1, &count, sizeof(char));
count = 0;
o++;
i++;
tempts = 1;
break;
} else if (rr->rdata[i] == '\0') {
memcpy(o, &(rr->rdata[i]), sizeof(char));
memcpy(o-count-1, &count, sizeof(char));
count = 0;
break;
} else {
memcpy(o, &(rr->rdata[i]), sizeof(char));
o++;
i++;
count++;
}
}
o--;
uint16_t temp = htons(49164);
memcpy(o, &temp, sizeof(short));
return 16 + i;
}
```
阅读全文