Send TCP protocol using socket RAW socket

Source: Internet
Author: User
Tags socket htons

Socket use Af_inet protocol family, and create sock_raw original socket, fill in the IP header and TCP header, it is familiar with the lower head structure.

* ===================================================================================== * * FILENAME:RAW.C * *
 Description: Uses the original socket to send the TCP protocol and take out its own data.         * * version:1.0 * created:2012 September 11 10:39 00 seconds * Revision: * COMPILER:GCC * * AUTHOR:YADON.Z * * ===================================================================================== * * #in Clude <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include < netinet/in.h> #include <netinet/ip.h> #include <netinet/tcp.h> #include <arpa/inet.h> #define DATA "Hello" #define packet_size sizeof (struct IPHDR) + sizeof (struct TCPHDR) + sizeof (data)/*-------------------------- -------------------------------Function name:check_sum () Descrypthon: Checksum calculation, excerpt from UNP source------------------------------
   ------------------------------*/unsigned short check_sum (unsigned short *addr, int len) {int nleft = Len; int sum = 0;
    unsigned short *w = addr;
    Short answer = 0;
        while (Nleft > 1) {sum + = *w++;
    Nleft-=2;
        } if (nleft = = 1) {* (unsigned char *) (&answer) = * (unsigned char *) W;
    sum + = answer;
    } sum = (sum >> +) + (sum & 0xFFFF);
    sum + = (sum >> 16);
    Answer = ~sum;
return answer; }/*---------------------------------------------------------Function Name:init_socket () Descrypthon: Initialize socket, using Raw socket------------------------------------------------------------*/int init_socket (int sockfd, struct sockaddr_in *
    target,\ const char *DST_ADDR, const char *dst_port) {CONST int flag = 1;
    target->sin_family = af_inet;
    Target->sin_port = htons (atoi (Dst_port));
        if (Inet_aton (dst_addr, &target->sin_addr) = = 0) {perror ("Inet_aton fail\n");
    Exit (-1); } if ((SOCKFD = socket (af_inet, Sock_raw, ipproto_tcp)) < 0) {perror ("ErroR ");
    Exit (-1); } if (setsockopt (Sockfd,ipproto_ip, Ip_hdrincl, &flag, sizeof (flag)) < 0) {perror ("setsockopt fail \ n")
        );
    Exit (-1);
} return SOCKFD; }/*---------------------------------------------------------------Function NAME:BUILE_IPHDR () Descrypthon: Build IP Header Data, the source address uses a pseudo-random address-----------------------------------------------------------------*/void Buile_iphdr (struct sockaddr
    _in *target, char *buffer) {struct IPHDR *ip = (struct IPHDR *) (buffer);
    Ip->version = 4;
    IP-&GT;IHL = 5;
    Ip->tos = 0;
    Ip->tot_len = htons (packet_size);
    Ip->id = 0;
    Ip->frag_off = 0;
    Ip->ttl = 255;
    Ip->protocol = ipproto_tcp;
    Ip->check = 0;
    IP-&GT;SADDR = random ();
    
    IP-&GT;DADDR = target->sin_addr.s_addr;
Ip->check = Check_sum ((unsigned short *) IP, sizeof (struct IPHDR) + sizeof (DATA)); }/*---------------------------------------------------------------Function name:buile_tCPHDR () Descrypthon: Constructs TCP header information, adds some of its own data, and then verifies the calculation. -----------------------------------------------------------------*/void Buile_tcphdr (struct sockaddr_in *target,
    const Char *src_port, char *buffer) {struct TCPHDR *tcp = (struct TCPHDR *) (buffer);
    Tcp->source = htons (atoi (Src_port));
    Tcp->dest = target->sin_port;
    Tcp->seq = random ();
    Tcp->doff = 5;
    Tcp->syn = 1;
    Buffer + = sizeof (struct TCPHDR);
    memcpy (buffer, data, sizeof (data)); 
Tcp->check = Check_sum ((unsigned short *) TCP, sizeof (struct TCPHDR) + sizeof (DATA));
    } int main (int argc, const char *argv[]) {char *buffer;
    char *buffer_head = NULL;
    int sockfd = 0;
    struct sockaddr_in *target;
        if (argc! = 4) {printf ("usage:destination addresss, Destination port, source port \ n");
    Exit (-1);
    } const char *dst_addr = argv[1];
    const char *dst_port = argv[2];
    const char *src_port = argv[3]; target = Calloc (sizeof (struct sockaddr_in), 1);
    Buffer = Calloc (packet_size, 1);
    Buffer_head = buffer;
    SOCKFD = Init_socket (SOCKFD, Target, dst_addr, dst_port);
    BUILE_IPHDR (target, buffer);
    Buffer + = sizeof (struct IPHDR);
    BUILE_TCPHDR (target, src_port, buffer); SendTo (SOCKFD, Buffer_head, packet_size, 0,\ (struct sockaddr *) target, sizeof (struct SOCKAD
    dr_in));
    Free (buffer_head);

    Free (target);
return 0;
 }


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.