实验6 TCP和UDP数据包发送与接收一、实验目的TCP协议是TCP/IP协议族的核心协议之一。
熟悉TCP包结构对于理解网络层次结构,以及TCP协议与IP协议的关系有着重要意义。
根据TCP协议的基本原理,通过封装与发送一个标准的TCP数据包,了解TCP包结构中各字段的含义与用途,从而深入理解传输层与下面各层的关系。
二、实验要求(1)掌握TCP/UDP报头结构、各字段含义以及校验和计算方法;(2)使用Wincap(Lipcap)构造并发送TCP,UDP数据包;(3)使用原始套接字(Raw Socket)发送自定义的TCP,UDP数据包;(4)使用NDIS协议驱动发送自定义的TCP/UDP数据包。
三、实验内容实验一SOCKET编程实验实验内容1、通过调试、运行“UDPClient”和“UDPServer”实验程序,加强对网络通讯原理的了解。
(或“简单Client”和“简单Server”实验程序,下同)2、学习分析实验程序功能结构,了解基于SOCKET编程的网络通信软件的基本设计方法。
3、在所提供的”UDPClient”和“UDPServer”实验程序基础上,完善程序功能。
4、通过实验学习和了解SOKCET通信的实现方法。
实验结果分析与总结(1)总结运行”UDPClient”和“UDPServer”实验程序的运行情况。
UDPClient运行结UDPServer运行结果(2)设计交互程序的运行结果如下:(3)总结程序设计的情况,列出所设计或修改部分的源代码清单。
附上程序源代码。
Client端修改的代码如下://(3)开始接收或发送过程printf("\n------------- waiting for message from Seaver -------------\n");//进入一个循环while (1){//输入并发送信息给服务器buffer[0]='\0'; //先清空发送缓冲区printf("\n Input datagram send info ( quit 退出 ): "); //输入发送字符串scanf("%s",buffer);sendto(socketid,buffer,sizeof buffer,0,(struct sockaddr*)&server,server_len);//发送信息//控制循环退出if(strcmp(buffer,"quit") == 0) //输入为quit则结束{ printf("\n send info quit");return 0;}//接收服务器返回信息buffer[0]='\0'; //先清空接收缓冲区if(recvfrom(socketid,buffer,sizeof buffer,0,(struct sockaddr*)&server,&server_len)!=SOCKET_ERROR) //接收返回信息{printf("Received datagram from --%s\n",buffer);}}closesocket(socketid); //关闭SOCKET连接WSACleanup(); //退出使用wsock32.dll动态链接库return 0;}Seaver端修改的代码如下:printf("\n------------- waiting for message from client -------------\n");//进入一个循环while (1){buffer[0]='\0';if(recvfrom(socketid,buffer,sizeofbuffer,0,(structsockaddr*)&client,&client_len)!=SOCKET_ERROR){printf("Received datagram from --%s\n",buffer);//给cilent发信息// char ack[100] = "recv ok!";// sendto(socketid,ack,sizeof ack,0,(struct sockaddr*)&client,client_len);}buffer[0]='\0';printf("\n Input datagram send info ( quit 退出 ): "); //输入发送字符串scanf("%s",buffer);sendto(socketid,buffer,sizeof buffer,0,(struct sockaddr*)&client,client_len); //发if(strcmp(buffer,"quit") == 0) //输入为quit则结束{printf("\n send info quit");return 0;}//Sleep(500);}closesocket(socketid);WSACleanup();return 0;}总结:在Client端接收返回信息发送信息和Seaver接收返回信息发送信息前都要进行清空接收缓冲区。
(1)掌握TCP/UDP报头结构、各字段含义以及校验和计算方法;各字段含义:●源端口(Source Port)和目的端口(Destination Port):分别代表本次TCP通信发起主机和目的主机所使用的端口号;●序列号(Sequence Number):该字段用来标识TCP源端设备向目的端设备发送的字节流,它表示在这个报文段中的第几个数据字节。
序列号是一个32位的数。
●确认号(Acknowledge Number):TCP使用32位的确认号字段标识期望收到的下一个段的第一个字节,并声明此前的所有数据已经正确无误地收到,因此,确认号应该是上次已成功收到的数据字节序列号加1。
收到确认号的源计算机会知道特定的段已经被收到。
确认号的字段只在ACK标志被设置时才有效。
●数据偏移(Data Offset):这个4位字段包括TCP头大小。
由于首部可能含有选项内容,因此TCP首部的长度是不确定的。
首部长度的单位是32比特或4个八位组。
首部长度实际上也指示了数据区在报文段中的起始偏移值。
●保留(Reserved):6位置0的字段。
为将来定义新的用途保留。
、●控制位(Control Bits):共6位,每一位标志可以打开一个控制功能。
URG(Urgent Pointer Field Significant,紧急指针字段标志):表示TCP包的紧急指针字段有效,用来保证TCP连接不被中断,并且督促中间齐备尽快处理这些数据。
ACK(Acknowledgement field significant,确认字段标志): 取1时表示应答字段有效,也即TCP应答号将包含在TCP段中,为0则反之。
PSH(Push Function,推功能):这个标志表示Push操作。
所谓Push操作就是指在数据包到达接收端以后,立即送给应用程序,而不是在缓冲区中排队。
RST(Reset the connection,重置连接):这个标志表示感谢连接复位请求,用来复位那些产生错误的连接,也被用来拒绝错误和非法的数据包。
SYN(Synchronize sequence numbers,同步序列号):表示同步序号,用来建立连接。
FIN(No more data from sender):表示发送端已经发送到数据末尾,数据传送完成,发送FIN标志位的TCP段,连接将被断开。
●窗口(Window):目的主机使用16位的窗口字段告诉源主机它期望每次收到的数据通的字节数。
●校验和(Checksum):TCP头包括16位的校验和字段用于错误检查。
源主机基于部分IP头信息,TCP头和数据内容计算一个校验和,目的主机也要进行相同的计算,如果收到的内容没有错误过,两个计算应该完全一样,从而证明数据的有效性。
●紧急指针(Urgent Pointer):紧急指针字段是一个可选的16位指针,指向段内的最后一个字节位置,这个字段只在URG标志被设置时才有效。
●选项(Option):至少1字节的可变长字段,标识哪个选项(如果有的话)有效。
如果没有选项,这个字节等于0,说明选项的结束。
这个字节等于1表示无需再有操作;等于2表示下四个字节包括源机器的最大长度(Maximum Segment Size,MSS).●填充(Padding):这个字段中加入额外的零,以保证TCP头是32的整数倍。
(b)UDP报头结构63源端口是可选的,目的端口必须填写。
若源端口不选,则取值为0;长度字段记录UDP数据报的总长度,包括UDP首部和用户数据。
长度以八位组为单位;校验和字段的内容为整个UDP报文加上伪首部的校验和,计算方法与IP数据报首部校验和的算法相同。
校验和计算可选。
该字段全0,则表示不计算校验和,用于高效率传输。
UDP使用全1来表示校验和值为0。
(c)校验和计算方法;USHORT CheckSum(const char *buf, int size){USHORT *buffer=(USHORT *)buf;unsigned long cksum=0;while(size >1){cksum+=*buffer++;size -=sizeof(USHORT);}if(size ){cksum += *(UCHAR*)buffer;}cksum = (cksum >> 16) + (cksum & 0xffff);cksum += (cksum >>16);return (USHORT)(~cksum);}USHORT CheckSum(USHORT *buffer, int size){unsigned long cksum=0;while(size >1){cksum+=*buffer++;size -=sizeof(USHORT);}if(size ){cksum += *(UCHAR*)buffer;}cksum = (cksum >> 16) + (cksum & 0xffff);cksum += (cksum >>16);return (USHORT)(~cksum);}unsigned short TcpCheckSum(const char *pTcpData, const char *pPshData, UINT nTcpCount){unsigned short sCheckSum = ~CheckSum(pTcpData,nTcpCount);unsigned long checkSum = sCheckSum;checkSum <<= 16;sCheckSum = ~CheckSum(pPshData,12);checkSum += sCheckSum;return CheckSum((char*)&checkSum,4);}unsigned short UdpCheckSum(const char *pTcpData, const char *pPshData, UINT nTcpCount){unsigned short sCheckSum = ~CheckSum(pTcpData,nTcpCount);unsigned long checkSum = sCheckSum;checkSum <<= 16;sCheckSum = ~CheckSum(pPshData,12);checkSum += sCheckSum;return CheckSum((char*)&checkSum,4);}(2)使用Wincap(Lipcap)构造并发送TCP,UDP数据包;程序代码:pcap_t * InitWinpcap(){printf("Please Choose the Adaptor through which you send data:\r\n");pcap_if_t *alldevs;pcap_if_t *d;int inum;int i=0;pcap_t *adhandle;char errbuf[PCAP_ERRBUF_SIZE];/* Retrieve the device list */if(pcap_findalldevs(&alldevs, errbuf) == -1){fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);exit(1);}/* Print the list */for(d=alldevs; d; d=d->next){printf("%d. %s", ++i, d->name);if (d->description)printf(" (%s)\n", d->description);elseprintf(" (No description available)\n");}if(i==0){printf("\nNo interfaces found! Make sure WinPcap is installed.\n");return NULL;}printf("Enter the interface number (1-%d):",i);scanf("%d", &inum);if(inum < 1 || inum > i){printf("\nInterface number out of range.\n");/* Free the device list */pcap_freealldevs(alldevs);return NULL;}/* Jump to the selected adapter */for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);/* Open the device *//* Open the adapter */if ((adhandle= pcap_open_live(d->name, // name of the device65536, // portion of the packet to capture.// 65536 grants that the whole packet will be captured on all the MACs.1, // promiscuous mode (nonzero means promiscuous)1000, // read timeouterrbuf // error buffer)) == NULL){fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);/* Free the device list */pcap_freealldevs(alldevs);return NULL;}pcap_freealldevs(alldevs);return adhandle;}int _tmain(int argc, _TCHAR* argv[]){if(3!=argc){printf("Wrong Parament!\r\n");return 0;}//printf (argv[1]);DWORD dwDestIp= inet_addr(argv[1]);if(dwDestIp==INADDR_NONE){printf("Wrong Ip Address!\r\n");return 0;}if(strlen(argv[2])>1024){printf("Too long Parament!\r\n");return 0;}pcap_t *hWpcapHandle=InitWinpcap();UCHAR bLocalMac[6];DWORD dwDefaultGateway= 0;DWORD dwLocalIP = 0;DWORD dwNetMask= 0;char strName[64];PIP_ADAPTER_INFO pAdapterInfo = NULL;ULONG ulLen = 0;gethostname(strName,64);::GetAdaptersInfo(pAdapterInfo,&ulLen);pAdapterInfo = (PIP_ADAPTER_INFO)::GlobalAlloc(GPTR, ulLen);// 取得本地适配器结构信息if(::GetAdaptersInfo(pAdapterInfo,&ulLen) == ERROR_SUCCESS){if(pAdapterInfo != NULL){memcpy(bLocalMac, pAdapterInfo->Address, 6);dwDefaultGateway= ::inet_addr(pAdapterInfo->GatewayList.IpAddress.String);dwLocalIP= ::inet_addr(pAdapterInfo->IpAddressList.IpAddress.String);dwNetMask= ::inet_addr(pAdapterInfo->IpAddressList.IpMask.String);}else{return 0;}}else{return 0;}char bDestMac[8];memset(bDestMac,0xff,6);TcpPacket *pTcpPacket;pTcpPacket=(TcpPacket *)new char[sizeof(TcpPacket)+strlen(argv[2])+1];strcpy(((char*)pTcpPacket)+sizeof(TcpPacket),argv[2]);ulLen=6;if(SendARP(dwDestIp,0,(PULONG)bDestMac,&ulLen)!=NO_ERROR){printf("Get Mac Error!\r\n");return 0;}memcpy(pTcpPacket->theIpPacket.theEthHead.bDestMac,bDestMac,6);memcpy(pTcpPacket->theIpPacket.theEthHead.bSourceMac,bLocalMac,6);pTcpPacket->EthernetType=0x8;pTcpPacket->theIpPacket.theIpHead.ucVersionAndHeadLength=0x45;pTcpPacket->theIpPacket.theIpHead.ucTos=0;pTcpPacket->TotalLength=htons(48+strlen(argv[2]));pTcpPacket->Identification=1234;pTcpPacket->FlagsAndFragmentOffset=0;pTcpPacket->theIpPacket.theIpHead.ucTtl=119;pTcpPacket->theIpPacket.theIpHead.ucProtocol=6;//tcppTcpPacket->theIpPacket.theIpHead.dwSourceAddr=dwLocalIP;pTcpPacket->theIpPacket.theIpHead.dwDestAddr=dwDestIp;pTcpPacket->Crc=0;pTcpPacket->Crc=CheckSum((const char *)(&(pTcpPacket->theIpPacket.theIpHead)),sizeof(IpHead));pTcpPacket->DestPort=htons(1000);pTcpPacket->SourcePort=htons(3000);pTcpPacket->theTcpHead.dwSeq=ntohl(198327);pTcpPacket->theTcpHead.dwAck=0;pTcpPacket->theTcpHead.ucLength=0x70;pTcpPacket->theTcpHead.ucFlag=4;pTcpPacket->Window=0xFFFF; //16 位窗口大小pTcpPacket->Crc=0;//16 位校验和pTcpPacket->Urgent=0;//16 位紧急数据偏移量pTcpPacket->theTcpHead.unMssOpt=htonl(0x020405B4);pTcpPacket->NopOpt= 0x0101;pTcpPacket->SackOpt= 0x0204;pTcpPacket->Crc=0;TcpFakeHeader theTcpFakeHeader;theTcpFakeHeader.bZero=0;theTcpFakeHeader.bTcpLength=htons(28+strlen(argv[2]));theTcpFakeHeader.bProtocolType=6;theTcpFakeHeader.dwDestAddr=dwDestIp;theTcpFakeHeader.dwSourceAddr=dwLocalIP;pTcpPacket->Crc=TcpCheckSum((char*)(&(pTcpPacket->theTcpHead)),(char*)(&theTcpFakeHeader),sizeof(TcpHead)+strlen(argv[2]));if (pcap_sendpacket(hWpcapHandle,(u_char *)pTcpPacket,sizeof(TcpPacket)+strlen(argv[2]) ) != 0){printf("\nError Sending the TCP Packet: \n", pcap_geterr(hWpcapHandle));}else{printf("Send TCP Packet Success!\r\n");}UdpPacket *pUdpPacket=(UdpPacket *)pTcpPacket;strcpy(((char*)pUdpPacket)+sizeof(UdpPacket),argv[2]);memcpy(pUdpPacket->theEthHead.bDestMac,bDestMac,6);memcpy(pUdpPacket->theEthHead.bSourceMac,bLocalMac,6);pUdpPacket->EthernetType=0x8;pUdpPacket->theIpHead.ucVersionAndHeadLength=0x45;pUdpPacket->theIpHead.ucTos=0;pUdpPacket->TotalLength=htons(28+strlen(argv[2]));pUdpPacket->Identification=1234;pUdpPacket->FlagsAndFragmentOffset=0;pUdpPacket->theIpHead.ucTtl=119;pUdpPacket->theIpHead.ucProtocol=17;//udppUdpPacket->theIpHead.dwSourceAddr=dwLocalIP;pUdpPacket->theIpHead.dwDestAddr=dwDestIp;;pUdpPacket->Crc=0;pUdpPacket->Crc=CheckSum((USHORT*)(&(pUdpPacket->theIp Head)),sizeof(IpHead));pUdpPacket->SourcePort=ntohs(3000);pUdpPacket->DestPort=ntohs(2000);pUdpPacket->Length=ntohs(8+strlen(argv[2]));pUdpPacket->Crc=0;UdpFakeHeader theUdpFakeHeader;theUdpFakeHeader.bZero=0;theUdpFakeHeader.bUdpLength=htons(sizeof(UdpHead)+strlen(argv[2]));theUdpFakeHeader.bProtocolType=17;theUdpFakeHeader.dwSourceAddr=dwLocalIP;theUdpFakeHeader.dwDestAddr=dwDestIp;pUdpPacket->Crc=UdpCheckSum((char*)&(pUdpPacket->theUdpHead),(char*)&theUdpFakeHeader,sizeof(UdpHead)+strlen(argv[2]));if (pcap_sendpacket(hWpcapHandle,(u_char *)pUdpPacket,sizeof(UdpPacket)+strlen(argv[2]) ) != 0){printf("\nError sending the packet: \n", pcap_geterr(hWpcapHandle));return 0;}printf("Send UDP Packet Success!\r\n");delete [](char*)pTcpPacket;return 0;}程序执行在控制台界面下键入SendPacket “目的地址”“发送内容”,运行结果如下图:(3)使用原始套接字(Raw Socket)发送自定义的TCP,UDP数据包;1.创建一个原始套接字,并设置IP头选项2.构造UDP头和TCP头同以上所述。