- *int sndpkt_tcp_rst(u32 seq,u32 src_ip, u32 dst_ip, u16 src_prt, u16 dst_prt)
- {
- static int i=0;
- int one=1; /* R.Stevens says we need this variable for the setsockopt call */
- int tcphdrlen = sizeof(struct tcphdr);
- int iphdrlen = sizeof(struct iphdr);
- /* Raw socket file descriptor */
- // int rawsocket=0;
- /* Buffer for the TCP/IP SYN Packets */
- char packet[tcphdrlen+iphdrlen +1 ];
- /* It will point to start of the packet buffer */
- struct iphdr *ipheader = (struct iphdr *)packet;
- /* It will point to the end of the IP header in packet buffer */
- struct tcphdr *tcpheader = (struct tcphdr *) (packet +iphdrlen);
- /* TPC Pseudoheader (used in checksum) */
- tcp_phdr_t pseudohdr;
- /* TCP Pseudoheader + TCP actual header used for computing the checksum */
- char tcpcsumblock[ sizeof(tcp_phdr_t) + TCPSYN_LEN ];
- /* Although we are creating our own IP packet with the destination address */
- /* on it, the sendto() system call requires the sockaddr_in structure */
- struct sockaddr_in dstaddr;
- // memset(&pseudohdr,0,sizeof(tcp_phdr_t));
- // memset(&packet, 0, sizeof(packet));
- // memset(&dstaddr, 0, sizeof(dstaddr));
- dstaddr.sin_family = AF_INET; /* Address family: Internet protocols */
- dstaddr.sin_port = dst_prt; /* Leave it empty */
- dstaddr.sin_addr.s_addr = dst_ip; /* Destination IP */
- #if 0
- /* Get a raw socket to send TCP packets */
- if ((rawsocket = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) < 0)
- {
- perror("TCP_RST_send():socket()");
- exit(1);
- }
- /* We need to tell the kernel that we'll be adding our own IP header */
- /* Otherwise the kernel will create its own. The ugly "one" variable */
- /* is a bit obscure but R.Stevens says we have to do it this way ;-) */
- if(setsockopt(rawsocket, IPPROTO_IP, IP_HDRINCL, &one, sizeof(one)) < 0)
- {
- perror("TCP_RST_send():setsockopt()");
- exit(1);
- }
- #endif
- /* IP Header */
- ipheader->ihl = 5; /* Header lenght in octects */
- ipheader->version = 4; /* Ip protocol version (IPv4) */
- ipheader->tos = 0; /* Type of Service (Usually zero) */
- ipheader->tot_len = __hns(iphdrlen +tcphdrlen);
- ipheader->frag_off = 0; /* Fragment offset. We'll not use this */
- ipheader->ttl = 64; /* Time to live: 64 in Linux, 128 in Windows... */
- ipheader->protocol = 6; /* Transport layer prot. TCP=6, UDP=17, ICMP=1... */
- ipheader->check= 0; /* Checksum. It has to be zero for the moment */
- ipheader->id = __hns(1337);
- ipheader->saddr = src_ip; /* Source IP address */
- ipheader->daddr = dst_ip; /* Destination IP address */
- /* TCP Header */
- tcpheader->seq = seq; /* Sequence Number */
- tcpheader->ack_seq = __hnl(1); /* Acknowledgement Number */
- tcpheader->res1 = 0; /* Variable in 4 byte blocks. (Deprecated) */
- tcpheader->doff = 5; /* Segment offset (Lenght of the header) */
- tcpheader->fin = 0;
- tcpheader->syn = 0;
- tcpheader->rst = 1;
- tcpheader->psh = 0;
- tcpheader->ack = 0;
- tcpheader->urg = 0;
- tcpheader->ece = 0;
- tcpheader->cwr = 0; /* TCP Flags. We set the Reset Flag */
- tcpheader->window = __hns(5000);/* Window size */
- tcpheader->urg_ptr = 0; /* Urgent pointer. */
- tcpheader->source = src_prt; /* Source Port */
- tcpheader->dest = dst_prt; /* Destination Port */
- tcpheader->check=0; /* Checksum. (Zero until computed) */
- /* Fill the pseudoheader so we can compute the TCP checksum*/
- pseudohdr.src = ipheader->saddr;
- pseudohdr.dst = ipheader->daddr;
- pseudohdr.zero = 0;
- pseudohdr.protocol = ipheader->protocol;
- pseudohdr.tcplen = __hns(tcphdrlen);
- /* Copy header and pseudoheader to a buffer to compute the checksum */
- // memcpy(tcpcsumblock, &pseudohdr, sizeof(tcp_phdr_t));
- // memcpy(tcpcsumblock+sizeof(tcp_phdr_t),tcpheader, sizeof(struct tcphdr));
- tcpheader->check = CalcTCPSum((unsigned short *)&pseudohdr,(unsigned short *)tcpheader,tcphdrlen);
- /* Compute the TCP checksum as the standard says (RFC 793) */
- // tcpheader->check = in_cksum((unsigned short *)(tcpcsumblock), sizeof(tcpcsumblock));
- /* Compute the IP checksum as the standard says (RFC 791) */
- ipheader->check = in_cksum((unsigned short *)ipheader,iphdrlen);
- /* Send it through the raw socket */
- if(sendto(g_rawsocket , packet, __nhs(ipheader->tot_len), 0,(struct sockaddr *) &dstaddr, sizeof(dstaddr)) < 0)
- {
- printf("Reset Faild\\n");
- fprintf(stderr, "Recv Error %s\\n", strerror(errno));
- return -1;
- }
- // WADEBUG(D_WARNING)("Reset OK\\n");
- /*
- printf("Sent RST Packet:\\n");
- printf(" SRC: %s:%d\\n", inet_ntoa(ipheader->ip_src), ntohs(tcpheader->th_sport));
- printf(" DST: %s:%d\\n", inet_ntoa(ipheader->ip_dst), ntohs(tcpheader->th_dport));
- printf(" Seq=%u\\n", ntohl(tcpheader->th_seq));
- printf(" Ack=%d\\n", ntohl(tcpheader->th_ack));
- printf(" TCPsum: %02x\\n", tcpheader->th_sum);
- printf(" IPsum: %02x\\n", ipheader->ip_sum);
- */
- #if 0
- close(rawsocket);
- #endif
- return 1;
- } /* End of IP_Id_send() */
- //该片段来自于http://www.codesnippet.cn/detail/081120137004.html
来源: http://www.codesnippet.cn/detail/081120137004.html