libnet TCP示例
生活随笔
收集整理的這篇文章主要介紹了
libnet TCP示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
libnet TCP示例 [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\n");scanf("%s",HostAddr);printf("please enter local address:\n");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("\003\003\012\001\002\004\001\011\010\012\077\077\077\077\000\000\000\000\000\000",20,l,0);
if(t==-1){printf("cant build TCP options: %s\n",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\n",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\n",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\n",libnet_geterror(l));goto bad;
}c=libnet_write(l);
if(c==-1){printf("write error:%s\n",libnet_geterror(l));goto bad;}
else { printf("wrote %d byte TCP packet\n",c);}
libnet_destroy(l);
return(EXIT_SUCCESS);
bad:libnet_destroy(l);return(EXIT_FAILURE);}
?
posted on 2013-10-03 23:10 RodYang 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/RodYang/p/3350751.html
總結
以上是生活随笔為你收集整理的libnet TCP示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: webLogic11g部署war包问题解
- 下一篇: 女字重叠怎么读?