当前位置:首页 > 计算机网络课程设计报告
nAccum <<= 1; nData <<= 1; } Table_CRC[i] = nAccum; } }
// 计算 32 位 CRC-32 值
unsigned long CRC_32( unsigned char * aData, unsigned long aSize ) { unsigned long i; unsigned long nAccum = 0; for ( i = 0; i < aSize; i++ ) nAccum = ( nAccum << 8 ) ^ Table_CRC[( nAccum >> 24 ) ^ *aData++]; return nAccum; }
void main() { unsigned char data[100]; unsigned long len(unsigned char *); printf(\ scanf(\ BuildTable16( nCRC_16 ); printf(\ BuildTable16( nCRC_CCITT ); printf(\ BuildTable32( nCRC_32 ); printf(\}
unsigned long len(unsigned char *str) { unsigned long len=0; while(*str++!='\\0') len++; return len; }
3、checksum
#include
#define IP_HDRINCL
typedef struct _IPHEADER {
UCHAR VerHeadLen; UCHAR TOS; USHORT Length;
USHORT ID; USHORT Flags; UCHAR TTL; UCHAR Protocol; USHORT Checksum; ULONG SourceIP; ULONG DestIP; }IPHEADER;
typedef struct _UDPHEADER {
USHORT SourcePort; USHORT DestPort; USHORT Length;
USHORT CheckSum; }UDPHEADER;
USHORT checksum(USHORT * buff, int size) {
ULONG cksum = 0; while (size > 1) {
cksum += *(buff++); size -= sizeof(USHORT); }
if(size) {
cksum += *((UCHAR*)buff); }
cksum = (cksum >> 16) + (cksum & 0xffff); cksum += (cksum >> 16); return (USHORT)(~cksum); }
void UdpHeaderChecksum(IPHEADER *IpHeader,UDPHEADER payload,int payloadlen)
*UdpHeader,char *
{
char buf[1024]; char *ptr = buf; int chksumlen = 0; ULONG zero = 0;
memcpy(ptr,&IpHeader->SourceIP,sizeof(IpHeader->SourceIP)); ptr += sizeof(IpHeader->SourceIP);
chksumlen += sizeof(IpHeader->SourceIP);
memcpy(ptr,&IpHeader->DestIP,sizeof(IpHeader->DestIP)); ptr += sizeof(IpHeader->DestIP);
chksumlen += sizeof(IpHeader->DestIP); memcpy(ptr,&zero,1); ptr += 1;
chksumlen += 1;
memcpy(ptr,&IpHeader->Protocol,sizeof(IpHeader->Protocol)); ptr += sizeof(IpHeader->Protocol);
chksumlen += sizeof(IpHeader->Protocol);
memcpy(ptr,&UdpHeader->Length,sizeof(UdpHeader->Length)); ptr += sizeof(UdpHeader->Length);
chksumlen += sizeof(UdpHeader->Length);
memcpy(ptr,&UdpHeader->SourcePort,sizeof(UdpHeader->SourcePort)); ptr += sizeof(UdpHeader->SourcePort);
chksumlen += sizeof(UdpHeader->SourcePort);
memcpy(ptr,&UdpHeader->DestPort,sizeof(UdpHeader->DestPort)); ptr += sizeof(UdpHeader->DestPort);
chksumlen += sizeof(UdpHeader->DestPort);
memcpy(ptr,&UdpHeader->Length,sizeof(UdpHeader->Length)); ptr += sizeof(UdpHeader->Length);
chksumlen += sizeof(UdpHeader->Length);
memcpy(ptr,&zero,sizeof(UdpHeader->CheckSum)); ptr += sizeof(USHORT);
chksumlen += sizeof(USHORT); memcpy(ptr,payload,payloadlen); ptr += payloadlen;
chksumlen += payloadlen;
for(int i = 0; i < payloadlen %2; i++) {
*ptr = 0; ptr++;
chksumlen++; }
UdpHeader->CheckSum = checksum((USHORT*)buf,chksumlen); }
int main() {
int i = 0;
WSADATA wsaData;
int wsaret = WSAStartup(0x101,&wsaData); if(wsaret != 0) {
printf(\失败\\n\ }
char szDestIP[] = \ char szSourceIP[] = \ USHORT nDestPort = 1234; USHORT nSourcePort = 8888; char szMsg[] = \ int nMsgLen = strlen(szMsg);
SOCKET sRaw = ::socket(AF_INET,SOCK_RAW,IPPROTO_UDP); BOOL bIncl = TRUE;
i = ::setsockopt(sRaw,IPPROTO_IP,IP_HDRINCL,(char*)&bIncl,sizeof(bIncl)); char buff[1024] = {0};
int TotalLength = sizeof(IPHEADER) + sizeof(UDPHEADER) + nMsgLen; //IP头
IPHEADER * IpHeader = (IPHEADER*)buff;
IpHeader->VerHeadLen = (4 << 4 | (sizeof(_IPHEADER)/sizeof(ULONG))); IpHeader->Length = TotalLength; IpHeader->TTL = 128;
IpHeader->Protocol = IPPROTO_UDP;
IpHeader->SourceIP = ::inet_addr(szSourceIP); IpHeader->DestIP = ::inet_addr(szDestIP);
IpHeader->Checksum = checksum((USHORT*)IpHeader,sizeof(IPHEADER)); //UDP头
UDPHEADER * UdpHeader = (UDPHEADER*)(buff + sizeof(IPHEADER)); UdpHeader->SourcePort = htons(nSourcePort); UdpHeader->DestPort = htons(nDestPort);
UdpHeader->Length = htons(sizeof(UDPHEADER) + nMsgLen); UdpHeader->CheckSum = 0;
UdpHeaderChecksum(IpHeader,UdpHeader,szMsg,nMsgLen); char* pData = buff + sizeof(IPHEADER) + sizeof(UDPHEADER); memcpy(pData,szMsg,nMsgLen); SOCKADDR_IN Addr = {0}; Addr.sin_family = AF_INET; Addr.sin_port = htons(nDestPort); memset(Addr.sin_zero,0,8);
Addr.sin_addr.S_un.S_addr = ::inet_addr(szDestIP);
共分享92篇相关文档