Win Socket-Raw

来源:互联网 发布:淘宝类目排名查询 编辑:程序博客网 时间:2024/06/09 18:18
// RawSock.cpp : Defines the entry point for the console application.//#define _WINSOCK_DEPRECATED_NO_WARNINGS#include "stdio.h"#include "winsock2.h"#include "ws2tcpip.h" //IP_HDRINCL is here#include "conio.h"#pragma comment(lib,"ws2_32.lib") //winsock 2.2 library#define ADDR_SRC "0.0.0.0"#define ADDR_DST "0.0.0.0"typedef unsigned char UInt8;typedef unsigned short UInt16;typedef unsigned int UInt32;typedef struct ip_hdr{unsigned char iphVerLen;unsigned char ip_tos;unsigned short ip_total_length; unsigned short ip_id;unsigned short ipFlags;unsigned char ipTTL; // Time to liveunsigned char ip_protocol; // Protocol(TCP,UDP etc)unsigned short ipCheckSum; // IP checksumunsigned int ipSrc;unsigned int ipDst;UInt8 ipRouterAlert;UInt8 ipRouterLength;UInt16 ipRouterCode;} IPHeader;// TCP headertypedef struct tcp_header{UInt8 igmpVerType;UInt8 igmpNv;//Zero is OKUInt16 igmpCheckSum;UInt32 igmpAddr;} IGMPHeader;UInt8 igmp_body[] = {0x03,0x00,0x00,0x00,0xef,0x76,0x00,0x00,0x03,0x00,0x00,0x00,0xef,0x7e,0x77,0x00,0x03,0x00,0x00,0x00,0xef,0x7e,0x63,0x73,0x03,0x00,0x00,0x00,0xef,0x7e,0x6c,0x70,};UInt16 CheckSum(UInt8 *pBuf,UInt32 uLen){UInt32 uCheckSum = 0,uLoop = 0;for(;uLoop < uLen;uLoop++){if(0 == uLoop%2){uCheckSum += pBuf[uLoop]<<8;}else{uCheckSum += pBuf[uLoop];}}uCheckSum = (uCheckSum>>16)+(uCheckSum&0x0000FFFF);return (UInt16)(~uCheckSum);}void print_log(UInt8 *p,int len){int i = 0 ;for(;i<len;i++){printf("%02X ",p[i]);if(15==i%16)printf("\n");}printf("\n");}int main(){char host[100], buf[1000], *data = NULL;SOCKET s;int k = 1;IPHeader *pIPHeader = NULL;IGMPHeader *pIgmpHeader = NULL;SOCKADDR_IN dest;//Initialise WinsockWSADATA wsock;printf("\nInitialising Winsock...");if (WSAStartup(MAKEWORD(2, 2), &wsock) != 0){fprintf(stderr, "WSAStartup() failed");exit(EXIT_FAILURE);}printf("Initialised successfully.");//////////////////////////////////////////////////Create Raw TCP Packetprintf("\nCreating Raw IGMP Socket...");if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_IP)) == SOCKET_ERROR){printf("Creation of raw socket failed.");return 0;}printf("Raw TCP Socket Created successfully.");//////////////////////////////////////////////////Put Socket in RAW Mode.printf("\nSetting the socket in RAW mode...");int optval = 1;if (setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *)&optval, sizeof(optval)) == SOCKET_ERROR){printf("failed to set socket in raw mode.");return 0;}printf("Successful.");dest.sin_family = AF_INET;dest.sin_port = htons(0x0000); //your destination portdest.sin_addr.s_addr = inet_addr(ADDR_DST);//memcpy(&dest.sin_addr.s_addr, server->h_addr, server->h_length);printf("Resolved.");/////////////////////////////////////////////////pIPHeader = (IPHeader *)buf; //lets point to the ip header portionpIPHeader->iphVerLen = 0x46;pIPHeader->ip_tos = 0xc0;pIPHeader->ip_total_length = htons(sizeof(IPHeader) + sizeof(IGMPHeader) + sizeof(igmp_body));pIPHeader->ip_id = htons(0x0023);pIPHeader->ipFlags = htons(0x4000);pIPHeader->ipTTL = 1;pIPHeader->ip_protocol = IPPROTO_IGMP;pIPHeader->ipCheckSum = 1234;pIPHeader->ipSrc = inet_addr(ADDR_SRC);pIPHeader->ipDst = inet_addr(ADDR_DST);pIPHeader->ipRouterAlert = 0x94;pIPHeader->ipRouterLength = 0x04;pIPHeader->ipRouterCode = htons(0x0000);pIgmpHeader = (IGMPHeader *)&buf[sizeof(IPHeader)]; //get the pointer to the tcp header in the packetpIgmpHeader->igmpVerType = 0x22;pIgmpHeader->igmpNv = 0x00;pIgmpHeader->igmpCheckSum = htons(0x0000);pIgmpHeader->igmpAddr = htonl(sizeof(igmp_body)/8);// Initialize the TCP payload to some rubbishdata = &buf[sizeof(IPHeader) + sizeof(IGMPHeader)];memcpy(data,igmp_body,sizeof(igmp_body));printf("\nSending packet...\n");pIgmpHeader->igmpCheckSum = htons( CheckSum((UInt8*)pIgmpHeader,sizeof(IGMPHeader) + sizeof(igmp_body)) );while (!_kbhit()){Sleep(1000);pIPHeader->ipCheckSum = htons(0x0000);pIPHeader->ipCheckSum = htons( CheckSum((UInt8*)pIPHeader,sizeof(IPHeader)) );printf(" %d packets send\n", k++);print_log((UInt8*)buf,sizeof(IPHeader) + sizeof(IGMPHeader) + sizeof(igmp_body));int err = (sendto(s, buf, sizeof(IPHeader) + sizeof(IGMPHeader) + sizeof(igmp_body), 0,(SOCKADDR *)&dest, sizeof(dest)));if ( err == SOCKET_ERROR){printf("Error sending Packet : %d", WSAGetLastError());break;}printf("Error sending Packet : %d", WSAGetLastError());}return 0;}


0 0
原创粉丝点击