libnet TCP示例
生活随笔
收集整理的這篇文章主要介紹了
libnet TCP示例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
[root@TD18 tmp]#gcc -o 1 1.c -lnet
[root@TD18 tmp]#./1
please enter Host address
11.11.11.11
please enter local address:
22.22.22.22
wrote 126 byte TCP packet
[root@TD18 tmp]#cat 1.c
#include<stdio.h>
#include<string.h>
#include<pcap.h>
#include<libnet.h>
main(void)
{ int c ;
u_char *cp;
libnet_t *l;
libnet_ptag_t t;
char *payload;
u_char HostAddr[255],MyAddr[255];
u_short payload_s;
u_long src_ip,dst_ip;
u_short src_prt,dst_prt;
char errbuf[LIBNET_ERRBUF_SIZE];
printf("please enter Host address
");
scanf("%s",HostAddr);
printf("please enter local address:
");
scanf("%s",MyAddr);
l=libnet_init( LIBNET_LINK,NULL,errbuf);
if(l==NULL)
{
printf("libnet failed: %s",errbuf);
exit(EXIT_FAILURE);
}
dst_ip=inet_addr(HostAddr);
src_ip=inet_addr(MyAddr);
dst_prt=0;
src_prt=0;
payload="adafhafhasfjashfasjfasjkfhsadjkfajskfjasdhfjaksfjkaf";
payload_s=strlen(payload);
t=libnet_build_tcp_options("0303120102040111101277777777000000000000",20,l,0);
if(t==-1)
{printf("cant build TCP options: %s
",libnet_geterror(l));
goto bad;}
t=libnet_build_tcp(src_prt,dst_prt,0x01010101,0x02020202,TH_SYN,32767,0,10,
LIBNET_TCP_H+20+payload_s,payload,payload_s,l,0 );
if(t==-1)
{
printf("cant build TCP header:%s
",libnet_geterror(l));
goto bad;
}
t=libnet_build_ipv4(LIBNET_IPV4_H+LIBNET_TCP_H+20+payload_s,0,242,0,64,IPPROTO_TCP,0,src_ip,dst_ip,NULL,0,l,0);
if(t==-1)
{
printf("cant build IP header:%s
",libnet_geterror(l));
goto bad;
}
t=libnet_build_ethernet("00:0B:6A:63:5E:11","00:0B:6A:63:5E:22",ETHERTYPE_IP,NULL,0,l,0);
if(t==-1)
{printf("cant buid ethernet header:%s
",libnet_geterror(l));
goto bad;
}
c=libnet_write(l);
if(c==-1)
{printf("write error:%s
",libnet_geterror(l));
goto bad;
}
else { printf("wrote %d byte TCP packet
",c);
}
libnet_destroy(l);
return(EXIT_SUCCESS);
bad:
libnet_destroy(l);
return(EXIT_FAILURE);
}
總結(jié)
以上是生活随笔為你收集整理的libnet TCP示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: XML是什么,它能够做什么?——写给XM
- 下一篇: 火狐下的GreaseMonkey和Chr