BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #79875同步于 2014/5/22
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

[求助]win7的nslookup与linux下c语言写的一个DNS服务器端交互的

swqsldz
2014/5/22镜像同步2 回复
在linux下写了一个DNS的服务器端, 用的是TCP连接, 写完之后用linux的nslookup,set vc方式连接测试没有问题,能够正常接收DNS请求包并返回,但是用win7的nslookup,set vc方式连接这个服务器端,接收的数据包会出错,只能收到2字节的内容 (TCP模式下DNS数据包长度的2字节),剩余部分就没有了~ 通过wireshark在服务器抓包发现DNS请求包是正常的,服务器已经接收到了,但是在程序里recv()的时候只能收到2字节, 并且服务器端还会发送RST包中断连接. 在linux环境下nslookup用recv()接收到的内容完全正常~~附上服务器端代码和抓包内容~ ~~~求解啊~~ void tcpSvr(){ int sock,cltsock; /* Socket */ struct sockaddr_in echoServAddr; /* Local address */ struct sockaddr_in echoClntAddr; /* Client address */ struct in_addr ipaddr; unsigned int ipAddr=0; unsigned int cliAddrLen; /* Length of client address */ char tcpBuffer[BUF_SIZE]; /* Buffer for echo string */ unsigned short echoServPort; /* Server port */ int recvMsgSize; /* Size of received message */ char name[64][128]; char *p[64]; char lvl=0; char svrType[30]; char svrAddr[40]; char splitDomain[256]; int i=0; char *ptr=tcpBuffer; DNS_HDR *dnshdr = (DNS_HDR *)(tcpBuffer+2); DNS_QER *dnsqer = NULL; DNS_RR *dnsans =NULL; DNS_SQL dnssql; SQL_INFO sqlinfo; if(GetProfileString("./server.conf","mysql","ip",sqlinfo.ip)==-1){ //读取配置文件 printf("配置文件server.conf丢失!\n"); exit(0); }else{ GetProfileString("./server.conf","mysql","username",sqlinfo.uname); GetProfileString("./server.conf","mysql","password",sqlinfo.pwd); GetProfileString("./server.conf","mysql","database",sqlinfo.database); GetProfileString("./server.conf","mysql","cache",sqlinfo.cache); GetProfileString("./server.conf","mysql","table",sqlinfo.table); GetProfileString("./server.conf","server","ip",svrAddr); GetProfileString("./server.conf","server","type",svrType); } for(i=0;i<64;i++){ p[i]=name[i]; } echoServPort = atoi("53"); /* First arg: local port */ /* Create socket for sending/receiving datagrams */ if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0){ printf("socket() failed.\n"); exit(0); } /* Construct local address structure */ memset(&echoServAddr, 0, sizeof(echoServAddr)); echoServAddr.sin_family = AF_INET; echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); echoServAddr.sin_port =htons(echoServPort); /* Bind to the local address */ if ((bind(sock, (struct sockaddr *) &echoServAddr,sizeof(echoServAddr))) < 0){ printf("bind() failed.\n"); exit(0); } listen(sock,53); //start listening while(1){ i = 0; memset(&tcpBuffer,0,sizeof(tcpBuffer)); cliAddrLen = sizeof(echoClntAddr); if((cltsock=accept(sock,(struct sockaddr *)&echoClntAddr,&cliAddrLen))<0){ printf("accept() failed.\n"); } printf("socket:%u\n",cltsock); /* Block until receive message from a client */ if ((recvMsgSize = recv(cltsock, tcpBuffer, BUF_SIZE ,0)) < 0){ /* linux下nslookup这里的recvMsgSize是正常的,win7下nslookup这里就只有2字节 */ printf("recv() failed.\n"); }else{ printf("%u\n",recvMsgSize); printf("%u,%u,%u",tcpBuffer[0],tcpBuffer[1],tcpBuffer[2]); /* 能够收取到的内容只有TCP下DNS请求包的length */ memset(name,0,sizeof(name)); /* 服务器端处理部分 */ } close(cltsock); } close(sock); } wireshark抓包的结果,可以看到DNS请求包已经发到了服务器 No. Time Source Destination Protocol Length Info 4094 35447.041449 192.168.1.111 192.168.1.104 DNS 77 Standard query A com Frame 4094: 77 bytes on wire (616 bits), 77 bytes captured (616 bits) Linux cooked capture Internet Protocol Version 4, Src: 192.168.1.111 (192.168.1.111), Dst: 192.168.1.104 (192.168.1.104) Transmission Control Protocol, Src Port: 18855 (18855), Dst Port: domain (53), Seq: 3, Ack: 1, Len: 21 Source port: 18855 (18855) Destination port: domain (53) [Stream index: 1460] Sequence number: 3 (relative sequence number) [Next sequence number: 24 (relative sequence number)] Acknowledgement number: 1 (relative ack number) Header length: 20 bytes Flags: 0x018 (PSH, ACK) Window size value: 16425 [Calculated window size: 65700] [Window size scaling factor: 4] Checksum: 0x618f [validation disabled] [SEQ/ACK analysis] [PDU Size: 23] TCP segment data (21 bytes) [2 Reassembled TCP Segments (23 bytes): #4092(2), #4094(21)] Domain Name System (query) Length: 21 Transaction ID: 0x0003 Flags: 0x0100 (Standard query) Questions: 1 Answer RRs: 0 Authority RRs: 0 Additional RRs: 0 Queries com: type A, class IN Name: com Type: A (Host address) Class: IN (0x0001) No. Time Source Destination Protocol Length Info 4095 35447.041522 192.168.1.104 192.168.1.111 TCP 56 domain > 18855 [ACK] Seq=1 Ack=24 Win=29312 Len=0 Frame 4095: 56 bytes on wire (448 bits), 56 bytes captured (448 bits) Linux cooked capture Internet Protocol Version 4, Src: 192.168.1.104 (192.168.1.104), Dst: 192.168.1.111 (192.168.1.111) Transmission Control Protocol, Src Port: domain (53), Dst Port: 18855 (18855), Seq: 1, Ack: 24, Len: 0 Source port: domain (53) Destination port: 18855 (18855) [Stream index: 1460] Sequence number: 1 (relative sequence number) Acknowledgement number: 24 (relative ack number) Header length: 20 bytes Flags: 0x010 (ACK) Window size value: 229 [Calculated window size: 29312] [Window size scaling factor: 128] Checksum: 0x8442 [validation disabled] [SEQ/ACK analysis] No. Time Source Destination Protocol Length Info 4096 35447.046200 192.168.1.104 192.168.1.111 DNS 90 Standard query response, No such name Frame 4096: 90 bytes on wire (720 bits), 90 bytes captured (720 bits) Linux cooked capture Internet Protocol Version 4, Src: 192.168.1.104 (192.168.1.104), Dst: 192.168.1.111 (192.168.1.111) Transmission Control Protocol, Src Port: domain (53), Dst Port: 18855 (18855), Seq: 1, Ack: 24, Len: 34 Source port: domain (53) Destination port: 18855 (18855) [Stream index: 1460] Sequence number: 1 (relative sequence number) [Next sequence number: 35 (relative sequence number)] Acknowledgement number: 24 (relative ack number) Header length: 20 bytes Flags: 0x018 (PSH, ACK) Window size value: 229 [Calculated window size: 29312] [Window size scaling factor: 128] Checksum: 0x8464 [validation disabled] [SEQ/ACK analysis] [PDU Size: 34] Domain Name System (response) Length: 32 Transaction ID: 0x0000 Flags: 0x8003 (Standard query response, No such name) Questions: 0 Answer RRs: 0 Authority RRs: 0 Additional RRs: 0 No. Time Source Destination Protocol Length Info 4097 35447.046512 192.168.1.104 192.168.1.111 TCP 56 domain > 18855 [RST, ACK] Seq=35 Ack=24 Win=29312 Len=0 Frame 4097: 56 bytes on wire (448 bits), 56 bytes captured (448 bits) Linux cooked capture Internet Protocol Version 4, Src: 192.168.1.104 (192.168.1.104), Dst: 192.168.1.111 (192.168.1.111) Transmission Control Protocol, Src Port: domain (53), Dst Port: 18855 (18855), Seq: 35, Ack: 24, Len: 0 Source port: domain (53) Destination port: 18855 (18855) [Stream index: 1460] Sequence number: 35 (relative sequence number) Acknowledgement number: 24 (relative ack number) Header length: 20 bytes Flags: 0x014 (RST, ACK) Window size value: 229 [Calculated window size: 29312] [Window size scaling factor: 128] Checksum: 0x8442 [validation disabled] No. Time Source Destination Protocol Length Info 4098 35447.048568 192.168.1.111 192.168.1.104 TCP 62 18855 > domain [FIN, ACK] Seq=24 Ack=35 Win=65664 Len=0 Frame 4098: 62 bytes on wire (496 bits), 62 bytes captured (496 bits) Linux cooked capture Internet Protocol Version 4, Src: 192.168.1.111 (192.168.1.111), Dst: 192.168.1.104 (192.168.1.104) Transmission Control Protocol, Src Port: 18855 (18855), Dst Port: domain (53), Seq: 24, Ack: 35, Len: 0 Source port: 18855 (18855) Destination port: domain (53) [Stream index: 1460] Sequence number: 24 (relative sequence number) Acknowledgement number: 35 (relative ack number) Header length: 20 bytes Flags: 0x011 (FIN, ACK) Window size value: 16416 [Calculated window size: 65664] [Window size scaling factor: 4] Checksum: 0xd751 [validation disabled] [SEQ/ACK analysis] No. Time Source Destination Protocol Length Info 4099 35447.048688 192.168.1.104 192.168.1.111 TCP 56 domain > 18855 [RST] Seq=35 Win=0 Len=0 Frame 4099: 56 bytes on wire (448 bits), 56 bytes captured (448 bits) Linux cooked capture Internet Protocol Version 4, Src: 192.168.1.104 (192.168.1.104), Dst: 192.168.1.111 (192.168.1.111) Transmission Control Protocol, Src Port: domain (53), Dst Port: 18855 (18855), Seq: 35, Len: 0 Source port: domain (53) Destination port: 18855 (18855) [Stream index: 1460] Sequence number: 35 (relative sequence number) Header length: 20 bytes Flags: 0x004 (RST) Window size value: 0 [Calculated window size: 0] [Window size scaling factor: 128] Checksum: 0x73de [validation disabled]
订阅后,新回复会通过你的通知中心匿名送达。
2 条回复
tonyjansan机器人#1 · 2014/5/23
用MinGW混编的?在Win下尝试显式修改下tcp option header看看。 【 在 swqsldz 的大作中提到: 】 : 在linux下写了一个DNS的服务器端, 用的是TCP连接, 写完之后用linux的nslookup,set vc方式连接测试没有问题,能够正常接收DNS请求包并返回,但是用win7的nslookup,set vc方式连接这个服务器端,接收的数据包会出错,只能收到2字节的内容 (TCP模式下DNS数据包长度的2字节),剩余部分就没有了~ 通过wireshark在服务器抓包发现DNS请求包是正常的,服务器已经接收到了,但是在程序里recv()的时候只能收到2字节, 并且服务器端还会发送RST包中断连接. 在linux环境下nslookup用recv()接收到的内容完全正常~~附上服务器端代码和抓包内容~ : ~~~求解啊~~ : [code=c] : ...................
swqsldz机器人#2 · 2014/5/29
【 在 tonyjansan 的大作中提到: 】 : 用MinGW混编的?在Win下尝试显式修改下tcp option header看看。 : ubuntu下用gcc编译