Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <Windows.h>
- #pragma comment (lib, "ws2_32.lib" )
- unsigned char data[] = "\x45\x00\x00\x30\x00\x00\x40\x00\x3c\x06\x00\x00\xa5\xf6\x0c\xd7\xa5\xf6\x43\xd3";
- char *src_ip = "165.246.12.215";
- char *dest_ip = "165.246.67.211";
- struct ip_header
- {
- unsigned char ip_header_len:4;
- unsigned char ip_version:4;
- unsigned char ip_tos;
- unsigned short ip_total_length;
- unsigned short ip_id;
- unsigned char ip_frag_offset:5;
- unsigned char ip_more_fragment:1;
- unsigned char ip_dont_fragment:1;
- unsigned char ip_reserved_zero:1;
- unsigned char ip_frag_offset1;
- unsigned char ip_ttl;
- unsigned char ip_protocol;
- unsigned short ip_checksum;
- struct in_addr ip_srcaddr;
- struct in_addr ip_destaddr;
- };
- u_short ip_sum_calc( u_short len_ip_header, u_short * buff )
- {
- u_short word16;
- u_int sum = 0;
- u_short i;
- // make 16 bit words out of every two adjacent 8 bit words in the packet
- // and add them up
- for( i = 0; i < len_ip_header; i = i+2 )
- {
- word16 = ( ( buff[i]<<8) & 0xFF00 )+( buff[i+1] & 0xFF );
- sum = sum + (u_int) word16;
- }
- // take only 16 bits out of the 32 bit sum and add up the carries
- while( sum >> 16 )
- sum = ( sum & 0xFFFF ) + ( sum >> 16 );
- // one's complement the result
- sum = ~sum;
- return ((u_short) sum);
- }
- void main()
- {
- unsigned short chksum;
- u_short ipdata[20]; //checksum을 계산할기위한 버퍼
- ip_header *myih;
- char *ptr;
- myih = (ip_header*)malloc(sizeof(myih));
- ptr = (char*)myih;
- printf("\n");
- // IP 패킷에 대한 정보들을 저장한다.
- myih->ip_header_len=0x5;
- myih->ip_version = 0x4;
- myih->ip_tos = 0x0;
- myih->ip_total_length = ntohs(0x30);
- myih->ip_id = htons(0x00);
- myih->ip_frag_offset = 0;
- myih->ip_more_fragment = 0;
- myih->ip_dont_fragment = 1;
- myih->ip_reserved_zero =0;
- myih->ip_frag_offset1 =0;
- myih->ip_ttl = 0x3c;
- myih->ip_protocol = 0x06;
- myih->ip_checksum = 0;
- myih->ip_srcaddr.S_un.S_addr = inet_addr(src_ip);
- myih->ip_destaddr.S_un.S_addr = inet_addr(dest_ip);
- //모든 정보를 세팅하고 체크섬을 구한다.
- int i;
- // 1byte 단위 값을 2바이트 배열에 저장함
- for (i = 0; i < 20; i ++) {
- ipdata[i] = *(unsigned char*)ptr++;
- }
- myih->ip_checksum = ip_sum_calc(20, ipdata);
- printf("checksum = %x\n",myih->ip_checksum);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement