發送原始UDP封包時:socket
1 以IPPROTO_UDP 爲協議類型建立一個原始套接字,打開原始套接字上的IP_HDRINCL選項函數
2 構建UDP封包,要先設置IP頭,設置UDP頭,最後UDP淨荷數據。spa
3 初始化完整的UDP封包以後,調用sendto函數便可將他發送。code
計算UDP封包校驗和的過程以下:blog
void ComputeUdpPseudoHeaderChecksum( IPHeader *pIphdr, UDPHeader *pUdphdr, char *payload, int payloadlen ) { char buff[1024]; char *ptr = buff; int chksumlen = 0; ULONG zero = 0; // 包含源IP地址和目的IP地址 memcpy(ptr, &pIphdr->ipSource, sizeof(pIphdr->ipSource)); ptr += sizeof(pIphdr->ipSource); chksumlen += sizeof(pIphdr->ipSource); memcpy(ptr, &pIphdr->ipDestination, sizeof(pIphdr->ipDestination)); ptr += sizeof(pIphdr->ipDestination); chksumlen += sizeof(pIphdr->ipDestination); // 包含8位0域 memcpy(ptr, &zero, 1); ptr += 1; chksumlen += 1; // 協議 memcpy(ptr, &pIphdr->ipProtocol, sizeof(pIphdr->ipProtocol)); ptr += sizeof(pIphdr->ipProtocol); chksumlen += sizeof(pIphdr->ipProtocol); // UDP長度 memcpy(ptr, &pUdphdr->len, sizeof(pUdphdr->len)); ptr += sizeof(pUdphdr->len); chksumlen += sizeof(pUdphdr->len); // UDP源端口號 memcpy(ptr, &pUdphdr->sourcePort, sizeof(pUdphdr->sourcePort)); ptr += sizeof(pUdphdr->sourcePort); chksumlen += sizeof(pUdphdr->sourcePort); // UDP目的端口號 memcpy(ptr, &pUdphdr->destinationPort, sizeof(pUdphdr->destinationPort)); ptr += sizeof(pUdphdr->destinationPort); chksumlen += sizeof(pUdphdr->destinationPort); // 又是UDP長度 memcpy(ptr, &pUdphdr->len, sizeof(pUdphdr->len)); ptr += sizeof(pUdphdr->len); chksumlen += sizeof(pUdphdr->len); // 16位的UDP校驗和,置爲0 memcpy(ptr, &zero, sizeof(USHORT)); ptr += sizeof(USHORT); chksumlen += sizeof(USHORT); // 淨荷 memcpy(ptr, payload, payloadlen); ptr += payloadlen; chksumlen += payloadlen; // 補齊到下一個16位邊界 for(int i=0; i<payloadlen%2; i++) { *ptr = 0; ptr++; chksumlen++; } // 計算這個校驗和,將結果填充到UDP頭 pUdphdr->checksum = checksum((USHORT*)buff, chksumlen); }
發送原始UDP封包的過程以下:ip
int main() { // 輸入參數信息 char szDestIp[] = "10.16.115.88"; // <<== 填寫目的IP地址 char szSourceIp[] = "127.0.0.1"; // <<== 填寫您本身的IP地址 USHORT nDestPort = 4567; USHORT nSourcePort = 8888; char szMsg[] = "This is a test \r\n"; int nMsgLen = strlen(szMsg); // 建立原始套節字 SOCKET sRaw = ::socket(AF_INET, SOCK_RAW, IPPROTO_UDP); // 有效IP頭包含選項 BOOL bIncl = TRUE; ::setsockopt(sRaw, IPPROTO_IP, IP_HDRINCL, (char *)&bIncl, sizeof(bIncl)); char buff[1024] = { 0 }; // IP頭 IPHeader *pIphdr = (IPHeader *)buff; pIphdr->iphVerLen = (4<<4 | (sizeof(IPHeader)/sizeof(ULONG))); pIphdr->ipLength = ::htons(sizeof(IPHeader) + sizeof(UDPHeader) + nMsgLen); pIphdr->ipTTL = 128; pIphdr->ipProtocol = IPPROTO_UDP; pIphdr->ipSource = ::inet_addr(szSourceIp); pIphdr->ipDestination = ::inet_addr(szDestIp); pIphdr->ipChecksum = checksum((USHORT*)pIphdr, sizeof(IPHeader)); // UDP頭 UDPHeader *pUdphdr = (UDPHeader *)&buff[sizeof(IPHeader)]; pUdphdr->sourcePort = htons(8888); pUdphdr->destinationPort = htons(nDestPort); pUdphdr->len = htons(sizeof(UDPHeader) + nMsgLen); pUdphdr->checksum = 0; char *pData = &buff[sizeof(IPHeader) + sizeof(UDPHeader)]; memcpy(pData, szMsg, nMsgLen); ComputeUdpPseudoHeaderChecksum(pIphdr, pUdphdr, pData, nMsgLen); // 設置目的地址 SOCKADDR_IN destAddr = { 0 }; destAddr.sin_family = AF_INET; destAddr.sin_port = htons(nDestPort); destAddr.sin_addr.S_un.S_addr = ::inet_addr(szDestIp); // 發送原始UDP封包 int nRet; for(int i=0; i<5; i++) { nRet = ::sendto(sRaw, buff, sizeof(IPHeader) + sizeof(UDPHeader) + nMsgLen, 0, (sockaddr*)&destAddr, sizeof(destAddr)); if(nRet == SOCKET_ERROR) { printf(" sendto() failed: %d \n", ::WSAGetLastError()); break; } else { printf(" sent %d bytes \n", nRet); } } ::closesocket(sRaw); getchar(); return 0; }