Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with IP_HDRINCL?
    primarykey
    data
    text
    <p>I already asked this question on raw IP packet implementation. But I didn't get any solutions.</p> <h3>My code:</h3> <pre><code>if((s = WSASocket(AF_INET, SOCK_RAW, IPPROTO_TCP, 0, 0, 0))==SOCKET_ERROR) // Socket { printf("Creation of raw socket failed."); return 0; } if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *)&amp;optval, sizeof(optval))==SOCKET_ERROR) { printf("failed to set socket in raw mode."); return 0; } if((sendto(s ,(char *) buf , sizeof(IPV4_HDR)+sizeof(TCP_HDR) + payload, 0,(SOCKADDR *)&amp;dest, sizeof(dest)))==SOCKET_ERROR) { printf("Error sending Packet : %d",WSAGetLastError()); break; } </code></pre> <h3>Error:</h3> <p>WSAGetLastError() returns 10022: </p> <h3>Description:</h3> <p>An invalid argument (for example, an argument that specified an invalid level) was supplied to the setsockopt (Wsapiref_94aa.asp) function. Sometimes, it also refers to the current state of the sockets, for example, calling accept (Wsapiref_13aq.asp) on a socket that is not listening. </p> <h3>Commentary:</h3> <p>But I have set the correct option value and size.</p> <p>What am I doing wrong? I am using Windows XP (SP3). In setsocketopt I tried IP_OPTIONS for that program it works fine and it sends IP Packets too. But in ethereal for every IP packet it generates ICMP packets from the destination. </p> <p>How can I fix this?</p> <p><strong>Source code</strong>:</p> <p>//raw tcp packet crafter</p> <pre><code>#include "stdio.h" #include "winsock2.h" #include "ws2tcpip.h" //IP_HDRINCL is here #include "conio.h" typedef struct ip_hdr { unsigned char ip_header_len:4; // 4-bit header length (in 32-bit words) normally=5 (Means 20 Bytes may be 24 also) unsigned char ip_version :4; // 4-bit IPv4 version unsigned char ip_tos; // IP type of service unsigned short ip_total_length; // Total length unsigned short ip_id; // Unique identifier unsigned char ip_frag_offset :5; // Fragment offset field unsigned char ip_more_fragment :1; unsigned char ip_dont_fragment :1; unsigned char ip_reserved_zero :1; unsigned char ip_frag_offset1; //fragment offset unsigned char ip_ttl; // Time to live unsigned char ip_protocol; // Protocol(TCP,UDP etc) unsigned short ip_checksum; // IP checksum unsigned int ip_srcaddr; // Source address unsigned int ip_destaddr; // Source address } IPV4_HDR, *PIPV4_HDR, FAR * LPIPV4_HDR; // TCP header typedef struct tcp_header { unsigned short source_port; // source port unsigned short dest_port; // destination port unsigned int sequence; // sequence number - 32 bits unsigned int acknowledge; // acknowledgement number - 32 bits unsigned char ns :1; //Nonce Sum Flag Added in RFC 3540. unsigned char reserved_part1:3; //according to rfc unsigned char data_offset:4; /*The number of 32-bit words in the TCP header. This indicates where the data begins. The length of the TCP header is always a multiple of 32 bits.*/ unsigned char fin :1; //Finish Flag unsigned char syn :1; //Synchronise Flag unsigned char rst :1; //Reset Flag unsigned char psh :1; //Push Flag unsigned char ack :1; //Acknowledgement Flag unsigned char urg :1; //Urgent Flag unsigned char ecn :1; //ECN-Echo Flag unsigned char cwr :1; //Congestion Window Reduced Flag //////////////////////////////// unsigned short window; // window unsigned short checksum; // checksum unsigned short urgent_pointer; // urgent pointer } TCP_HDR , *PTCP_HDR , FAR * LPTCP_HDR , TCPHeader , TCP_HEADER; int main() { char host[100],buf[1000],*data=NULL,source_ip[20]; //buf is the complete packet SOCKET s; int k=1; IPV4_HDR *v4hdr=NULL; TCP_HDR *tcphdr=NULL; int payload=512 ; int optval= 1; SOCKADDR_IN dest; hostent *server; //Initialise Winsock WSADATA wsock; printf("\nInitialising Winsock..."); if (WSAStartup(MAKEWORD(2,2),&amp;wsock) != 0) { fprintf(stderr,"WSAStartup() failed"); exit(EXIT_FAILURE); } printf("Initialised successfully."); //////////////////////////////////////////////// //Create Raw TCP Packet printf("\nCreating Raw TCP Socket..."); if((s = WSASocket(AF_INET, SOCK_RAW, IPPROTO_TCP, 0, 0, 0))==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..."); if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *)&amp;optval, sizeof(optval))==SOCKET_ERROR) { printf("failed to set socket in raw mode."); return 0; } printf("Successful."); //////////////////////////////////////////////// //Target Hostname printf("\nEnter hostname : "); gets(host); printf("\nResolving Hostname..."); if((server=gethostbyname(host))==0) { printf("Unable to resolve."); return 0; } dest.sin_family = AF_INET; dest.sin_port = htons(8888); //your destination port memcpy(&amp;dest.sin_addr.s_addr,server-&gt;h_addr,server-&gt;h_length); printf("Resolved."); ///////////////////////////////////////////////// printf("\nEnter Source IP : "); gets(source_ip); v4hdr = (IPV4_HDR *)buf; //lets point to the ip header portion v4hdr-&gt;ip_version=4; v4hdr-&gt;ip_header_len=5; v4hdr-&gt;ip_tos = 0; v4hdr-&gt;ip_total_length = htons ( sizeof(IPV4_HDR) + sizeof(TCP_HDR) + payload ); v4hdr-&gt;ip_id = htons(2); v4hdr-&gt;ip_frag_offset = 0; v4hdr-&gt;ip_frag_offset1 = 0; v4hdr-&gt;ip_reserved_zero = 0; v4hdr-&gt;ip_dont_fragment = 1; v4hdr-&gt;ip_more_fragment = 0; v4hdr-&gt;ip_ttl = 8; v4hdr-&gt;ip_protocol = IPPROTO_TCP; v4hdr-&gt;ip_srcaddr = inet_addr(source_ip); v4hdr-&gt;ip_destaddr = inet_addr(inet_ntoa(dest.sin_addr)); v4hdr-&gt;ip_checksum = 0; tcphdr = (TCP_HDR *)&amp;buf[sizeof(IPV4_HDR)]; //get the pointer to the tcp header in the packet tcphdr-&gt;source_port = htons(1234); tcphdr-&gt;dest_port = htons(8888); tcphdr-&gt;cwr=0; tcphdr-&gt;ecn=1; tcphdr-&gt;urg=0; tcphdr-&gt;ack=0; tcphdr-&gt;psh=0; tcphdr-&gt;rst=1; tcphdr-&gt;syn=0; tcphdr-&gt;fin=0; tcphdr-&gt;ns=1; tcphdr-&gt;checksum = 0; // Initialize the TCP payload to some rubbish data = &amp;buf[sizeof(IPV4_HDR) + sizeof(TCP_HDR)]; memset(data, '^', payload); printf("\nSending packet...\n"); while(!_kbhit()) { printf(" %d packets send\r",k++); if((sendto(s ,(char *) buf , sizeof(IPV4_HDR)+sizeof(TCP_HDR) + payload, 0,(SOCKADDR *)&amp;dest, sizeof(dest)))==SOCKET_ERROR) { printf("Error sending Packet : %d",WSAGetLastError()); break; } } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload