#include <netinet/ip.h> //ip |
#include <netinet/tcp.h> //tcp |
char srcip[]="100.100.100.100"; |
char dstip[]="192.168.11.220"; |
#if __BYTE_ORDER == __LITTLE_ENDIAN |
#if __BYTE_ORDER == __BIG_ENDIAN |
#define IP_RF 0x8000 // reserved fragment flag //标志 |
#define IP_DF 0x4000 // dont fragment flag |
#define IP_MF 0x2000 // more fragments flag |
#define IP_OFFMASK 0x1fff // mask for fragmenting bits |
struct in_addr ip_src, ip_dst; |
typedef struct _TCPHeader |
#if __BYTE_ORDER == __LITTLE_ENDIAN |
#elif __BYTE_ORDER == __BIG_ENDIAN |
#error "Adjust your <bits/endian.h> defines" |
unsigned short IPCheakSum (unsigned short *packet, int packlen) |
register unsigned long sum = 0; |
sum += *(unsigned char *)packet; |
sum = (sum & 0xffff) + (sum >> 16); |
return (unsigned short) ~sum; |
unsigned short TCPCheckSum (unsigned char *iphdr, unsigned short *packet,int packlen) |
buf = malloc(packlen+12); |
if(buf == NULL) return 0; |
*(buf+4)=htons((unsigned short)(*(iphdr+9))); |
*(buf+5)=htons((unsigned short)packlen); |
memcpy(buf+6,packet,packlen); |
res = IPCheakSum(buf,packlen+12); |
int sock, bytes_send, fromlen,n,id,s; |
unsigned char buffer[65535]; |
toaddr.sin_family =AF_INET; |
inet_aton(dstip,&toaddr.sin_addr); |
sock = socket(AF_INET, SOCK_RAW,IPPROTO_RAW); |
if (sock>0) {printf("socket ok\n");} |
else {printf ("socket error \n");} |
if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &n, sizeof(n)) < 0) { |
inet_aton(dstip,&ip->ip_dst); |
tcp = (struct tcphdr *)(buffer + (4*ip->ip_hl)); |
tcp->source=htons(srcport); |
tcp->dest=htons(dstport); |
inet_aton(srcip,&ip->ip_src); |
tcp->check=TCPCheckSum((unsigned char *)buffer, (unsigned short *)tcp,40); |
ip->ip_sum=IPCheakSum((unsigned short *)buffer,20); |
bytes_send=sendto(sock,buffer,60,0,(struct sockaddr *)&toaddr,sizeof(toaddr)); |
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)); |
作者:Gezidan
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted on 2011-08-19 14:55
日需博客 阅读(671)
评论(0) 编辑 收藏 引用 所属分类:
C C++ 、
Linux 、
技术文章 、
转载