Custom IP Header

Source: Internet
Author: User
Tags htons

# Include # define source_port 7234 # define max_receivebyte 255 typedef struct ip_hdr // define the IP header {unsigned char h_verlen; // four-bit header length, 4-bit IP version unsigned char TOS; // 8-bit service type TOS unsigned short total_len; // 16-bit total length (bytes) unsigned short ident; // 16-bit ID unsigned
Short frag_and_flags; // three-bit unsigned char TTL; // eight-bit TTL: unsigned char proto; // the eight-bit protocol (TCP, UDP, or others) unsigned short checksum; // 16-bit IP header checksum and unsigned int sourceip; // 32-bit source IP address unsigned int destip; // 32-bit destination IP address} ipheader; typedef struct tsd_hdr // define TCP pseudo header {
Unsigned long saddr; // source address unsigned long daddr; // Destination Address char mbz; char ptcl; // protocol type unsigned short tcpl; // TCP length} psdheader; typedef struct tcp_hdr // define TCP Header {ushort th_sport; // 16-bit source port ushort th_dport; // 16-bit destination port unsigned int th_seq; // 32-bit serial number unsigned int th_ack;
// 32-bit validation No. unsigned char th_lenres; // 4-bit header length/6-bit reserved characters unsigned char th_flag; // 6-Bit Flag ushort th_win; // 16-bit window size ushort th_sum; // 16-bit checksum and ushort th_urp; // 16-bit emergency data offset} tcpheader; // checksum: calculate the checksum subfunction ushort checksum (ushort * buffer, int size) {unsigned long cksum = 0;
While (size> 1) {cksum + = * buffer ++; size-= sizeof (ushort);} If (size) {cksum + = * (uchar *) buffer ;} cksum = (cksum> 16) + (cksum & 0 xFFFF); cksum + = (cksum> 16); Return (ushort )(~ Cksum);} void useage () {printf ("************************************ * ***** \ n ");
Printf ("tcpping \ n"); printf ("\ t written by refdom \ n"); printf ("\ t Email: refdom@263.net \ n"); printf ("useage: tcpping.exe target_ip target_port \ n "); printf ("************************************* * ***** \ n ");} int main (INT argc, char * argv []) {wsadata;
Socket sock; sockaddr_in addr_in; ipheader; tcpheader; psdheader; char szsendbuf [60] = {0}; bool flag; int rect, ntimeover; useage (); if (argc! = 3) {return false;} If (wsastartup (makeword (2, 2), & wsadata )! = 0) {printf ("wsastartup
Error! \ N "); Return false;} If (sock = wsasocket (af_inet, sock_raw, ipproto_raw, null, 0, wsa_flag_overlapped) = invalid_socket) {printf ("socket setup error! \ N "); Return false;} flag = true; If (setsockopt (sock, ipproto_ip, ip_hdrincl, (char *) & flag, sizeof (FLAG) = socket_error)
{Printf ("setsockopt ip_hdrincl error! \ N "); Return false;} ntimeover = 1000; If (setsockopt (sock, sol_socket, so_sndtimeo, (char *) & ntimeover, sizeof (ntimeover) = socket_error) {printf ("setsockopt so_sndtimeo error! \ N "); Return false;} addr_in.sin_family = af_inet;
Addr_in.sin_port = htons (atoi (argv [2]); addr_in.sin_addr.s_un.s_addr = inet_addr (argv [1]); // fill in the IP header ipheader. h_verlen = (4 <4 | sizeof (ipheader)/sizeof (unsigned long); // ipheader. toS = 0; ipheader. total_len = htons (sizeof (ipheader) + sizeof (tcpheader); ipheader. ident = 1;
Ipheader. frag_and_flags = 0; ipheader. TTL = 128; ipheader. PROTO = ipproto_tcp; ipheader. checksum = 0; ipheader. sourceip = inet_addr ("local address"); ipheader. destip = inet_addr (argv [1]); // fill in the TCP Header tcpheader. th_dport = htons (atoi (argv [2]); tcpheader. th_sport = htons (source_port );
// Source port number tcpheader. th_seq = htonl (0x12345678); tcpheader. th_ack = 0; tcpheader. th_lenres = (sizeof (tcpheader)/4 <4 | 0); tcpheader. th_flag = 2; // modify here to implement different flag detection, 2 is SYN, 1 is Fin, 16 is ack detection, and so on tcpheader. th_win= htons (512); tcpheader. th_urp = 0; tcpheader. th_sum = 0; psdheader. saddr = ipheader. sourceip;
Psdheader. daddr = ipheader. destip; psdheader. mbz = 0; psdheader. ptcl = ipproto_tcp; psdheader. tcpl = htons (sizeof (tcpheader); // calculate the checksum (szsendbuf, & psdheader, sizeof (psdheader); memcpy (szsendbuf + sizeof (psdheader), & tcpheader, sizeof (tcpheader); tcpheader. th_sum = checksum (ushort
*) Szsendbuf, sizeof (psdheader) + sizeof (tcpheader); memcpy (signature, & ipheader, sizeof (ipheader); memcpy (szsendbuf + sizeof (ipheader), & tcpheader, sizeof (tcpheader); memset (szsendbuf + sizeof (ipheader) + sizeof (tcpheader), 0, 4); ipheader. checksum = checksum (ushort
*) Szsendbuf, sizeof (ipheader) + sizeof (tcpheader); memcpy (szsendbuf, & ipheader, sizeof (ipheader); rect = sendto (sock, szsendbuf, sizeof (ipheader) + sizeof (tcpheader), 0, (struct sockaddr *) & addr_in, sizeof (addr_in); If (rect = socket_error) {printf ("send error! : % D \ n ", wsagetlasterror ());
Return false;} else printf ("Send OK! \ N "); closesocket (sock); wsacleanup (); Return

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.