- 论坛徽章:
- 0
|
仔细看了TCP/IP详解中DNS部分,了解了DNS数据报的发送和接收,粗粗的写了段源码用来获取一个域名的IP地址。源码仅仅是用来获取一个域名的IP地址,因此在处理返回数据报时,只考虑了3种类型。
#include
#include
#include
#include
#include
//#include
//#include
//#include
//#include
//#include
//#include
//#include
//#include
#define ADDRESS "192.168.0.1"
#define PORT 53
#define IPADDR 4
#define IPNUMBER 10
//定义发送和接收数据的结构体
typedef struct{
unsigned short id;
unsigned short flags;
unsigned short questNum;
unsigned short answerNum;
unsigned short authorNum;
unsigned short additionNum;
}DNSHDR,*pDNSHDR;
/*typedef struct{
unsigned short name;
unsigned short type;
unsigned short classes;
unsigned int ttl;
unsigned short length;
unsigned int addr;
}RESPONSE,*pRESPONSE;
*/
typedef struct{
// unsigned char *name;
unsigned short type;
unsigned short class;
}QUERYHDR,*pQUERYHDR;
int chagDname(char *Dname,char *name)
{
int pos[4],posnum[5];
pos[0] = -1;
if( Dname[0] == '.')
{
printf("input error!\n");
return -1;
}
int i,slen=0,num=1;
slen=strlen(Dname);
for(i=0;i='a'&&Dname='A'&&Dname
void genDNSPacket(char *Dname,char *DNSsendBuff)//返回要发送的数据报
{
memset(DNSsendBuff,' ',sizeof(DNSsendBuff));
pDNSHDR pDnsHdr = (pDNSHDR)malloc(sizeof(DNSHDR));
pQUERYHDR pQueryHdr = (pQUERYHDR)malloc(sizeof(QUERYHDR));
pDnsHdr->id = htons(0x3344);printf("pDnsHdr->id=%d\n",pDnsHdr->id);
pDnsHdr->flags = htons(0x0100);printf("pDnsHdr->flags=%d\n",pDnsHdr->flags);
pDnsHdr->questNum = htons(0x0001);printf("pDnsHdr->questNum=%d\n",pDnsHdr->questNum);
pDnsHdr->answerNum = htons(0x0000);printf("pDnsHdr->answerNum=%d\n",pDnsHdr->answerNum);
pDnsHdr->authorNum = htons(0x0000);printf("pDnsHdr->authorNum=%d\n",pDnsHdr->authorNum);
pDnsHdr->additionNum = htons(0x0000);printf("pDnsHdr->additionNum=%d\n",pDnsHdr->additionNum);
memcpy(DNSsendBuff,pDnsHdr,sizeof(DNSHDR));
int len;
len = strlen(Dname)+2;
char *name = (char *)malloc(len *sizeof(char));
chagDname(Dname,name);
// printf("%d,%d\n",sizeof(DNSsendBuff),sizeof(DNSHDR));
memcpy(DNSsendBuff + sizeof(DNSHDR),name,len);
pQueryHdr->type = htons(0x0001);
pQueryHdr->class = htons(0x0001);
memcpy(DNSsendBuff + sizeof(DNSHDR) + len,pQueryHdr,sizeof(QUERYHDR));
}
int main(int argc, char **argv)
{
unsigned char sendBuff[64];
unsigned char recvBuff[64*10];
unsigned char IpFromDname[IPNUMBER][IPADDR];
bzero(sendBuff,sizeof(sendBuff));
if (argc \n", argv[0]);
exit(1);
}
char *s;
s = (char *)malloc(20*sizeof(char));
int pos = 12;
int ipnum = 0;
int posi[IPNUMBER];
int SocketFd;
int ServerLen;
struct sockaddr_in ServerAddr;
bzero(&ServerAddr,sizeof(struct sockaddr_in));
SocketFd = socket(AF_INET,SOCK_DGRAM,0);
ServerAddr.sin_family = AF_INET;
if (inet_aton(ADDRESS,&ServerAddr.sin_addr) == 0)
return -1;
ServerAddr.sin_port = htons(PORT);
ServerLen = sizeof(ServerAddr);
genDNSPacket(argv[1],sendBuff);
int res;
res = sendto(SocketFd,sendBuff,sizeof(sendBuff),0,(struct sockaddr*)&ServerAddr,ServerLen);
printf("\nsend to %d",res);
res = recvfrom(SocketFd,recvBuff,sizeof(recvBuff),0,(struct sockaddr*)&ServerAddr,&ServerLen);
printf("recv %d\n",res);
int i;
for (i=0;i
//下面代码找到这个包中是IP的起始位子,并记下该位子
while( posval != 0 )//0表示表示域名的段结束,如
www.sina.com.cn
,在数据报中是3www4sina3com2cn0
{
pos+=posval+1;
posval=recvBuff[pos];
printf("pos=%d,posvalue=%d\n",pos,posval);
}
pos+=8;
while(pos
现在不知道怎么用定义结构体来保持接收的数据报,有达人知道指点下,万分感谢!
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/78880/showart_1901711.html |
|