linux 模拟交换机,使用libnet, libpcap模拟一个交换机

/*net data packet transmit tools

* liujx

* 2007-4-1*/#include#include#include#include#include#include#include#include#include#include#defineIN_ETH "eth1"char*dev_a,*dev_b;

pcap_t*pt_a,*pt_b;

pthread_t pid_a, pid_b;

libnet_t*net_a,*net_b;staticcharerrbuf[256];voidsigproc(intsig );void*dev_a_capture(void*);voiddev_a_handle( u_char*devId,conststructpcap_pkthdr*h,constu_char*p );void*dev_b_capture(void*);voiddev_b_handle( u_char*devId,conststructpcap_pkthdr*h,constu_char*p );intmain(intargc,char*argv[] )

{/*regise signal ctrl+c stop capture*/signal( SIGINT, sigproc ) ;/*init libnet*/net_a=libnet_init( LIBNET_LINK,"eth2", errbuf );if( net_a==NULL )

{

fprintf(stderr,"libnet_init fail:%s", errbuf );return;

}

net_b=libnet_init( LIBNET_LINK,"eth1", errbuf );if( net_a==NULL )

{

fprintf(stderr,"libnet_init fail:%s", errbuf );return;

}/*create thread*/intstatus;

printf("create a");

status=pthread_create(&pid_a, NULL, dev_a_capture, NULL );if( status!=0)

{

printf("pthread_create( A ) faile.");gotoend;

}

printf("create b");

status=pthread_create(&pid_b, NULL, dev_b_capture, NULL );if( status!=0)

{

printf("pthread_create( B ) faile.");gotoend;

}

pthread_join( pid_a, NULL );

pthread_join( pid_b, NULL );

end:

pcap_close( pt_a );

pcap_close( pt_b );return0;

}/*single processing function*/voidsigproc(intsig )

{

pthread_cancel( pid_a );

pthread_cancel( pid_b );

pcap_close( pt_a );

pcap_close( pt_b );

libnet_destroy( net_a );

libnet_destroy( net_b );

printf("exit transmit.");

exit(0);

}/*receive eth1's packet and transmit to eth2*/void*dev_a_capture(void*arg)

{

dev_a=pcap_lookupdev( errbuf );if( dev_a==NULL)

{

printf("pcap_lookupdev: %s", errbuf );

exit(0);

}

printf("get dev: '%s'", dev_a );

pt_a=pcap_open_live( dev_a,8000,1,500, errbuf );if( pt_a==NULL )

{

printf("pcap_open_live:%s", errbuf );

exit(0);

}for(;;)

{intret;

ret=pcap_dispatch( pt_a,0, dev_a_handle, NULL);if( ret==-1)

{

pcap_perror( pt_a,"pcap_dispatch err:");

}

}

}voiddev_a_handle( u_char*devId,conststructpcap_pkthdr*hdr,constu_char*packet )

{//printf("%s,capture size :%d  ",devId, hdr->caplen );structether_header ehdr;

memcpy(&ehdr, packet,sizeof(structether_header ));/*labpcap can capture all packet ,include self send packet.

* only transmit distination address is 221(eth2 MAC last bytes) or broadcast address,

* 221 is eth2 link host's MAC.*/if( ehdr.ether_shost[ETH_ALEN-1]==221)

{return;

}if( ehdr.ether_dhost[ETH_ALEN-1]==221||ehdr.ether_dhost[ETH_ALEN-1]==255)

{

printf("A  src:%d, dst:%d", ehdr.ether_shost[ETH_ALEN-1], ehdr.ether_dhost[ETH_ALEN-1] );intc;

c=libnet_write_link( net_a, (u_char*)packet, hdr->caplen );//printf("A write: %d ", c );}

}/*receive eth2's packet and transmit to eth1.*/void*dev_b_capture(void*arg)

{//dev_b = pcap_lookupdev( errbuf );dev_b="eth2";

pt_b=pcap_open_live( dev_b,8000,1,500, errbuf );if( pt_b==NULL )

{

printf("pcap_open_live:%s", errbuf );

exit(0);

}for(;;)

{intret;

ret=pcap_dispatch( pt_b,0, dev_b_handle, NULL);if( ret==-1)

{

pcap_perror( pt_b,"pcap_dispatch err:");

}

}

}voiddev_b_handle( u_char*devId,conststructpcap_pkthdr*hdr,constu_char*packet )

{//printf("%s,capture size :%d  ",devId, hdr->caplen );u_int8_t eth_a[ETH_ALEN];

u_int8_t eth_b[ETH_ALEN];structether_header ehdr;

memcpy(&ehdr, packet,sizeof(structether_header ));/*Only transmit source address is 221(eth2 MAC last bytes)*/if( ehdr.ether_shost[ETH_ALEN-1]==221)

{

printf("B  src:%d, dst:%d", ehdr.ether_shost[ETH_ALEN-1], ehdr.ether_dhost[ETH_ALEN-1] );intc;

c=libnet_write_link( net_b, (u_char*)packet, hdr->caplen );//printf("B write: %d ", c );}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值