提交 7a278f81 编写于 作者: D dev@dev.com

帮助劲犇同步更新pcap的例子

上级 b228bd0c
...@@ -47,6 +47,10 @@ omp_threadpool ...@@ -47,6 +47,10 @@ omp_threadpool
C++与OMP配合的最简线程池 C++与OMP配合的最简线程池
https://goldenhawking.blog.csdn.net/article/details/51824243 https://goldenhawking.blog.csdn.net/article/details/51824243
pcapio
UDP丢包替代:用PCAP实现C/C++以太网SDR吞吐
https://goldenhawking.blog.csdn.net/article/details/126292692
primer_calc primer_calc
C++ OpenMP有限资源快速连续素数求取 C++ OpenMP有限资源快速连续素数求取
https://goldenhawking.blog.csdn.net/article/details/109043815 https://goldenhawking.blog.csdn.net/article/details/109043815
......
#include <iostream> #include <iostream>
#include <vector>
#include "pcapio.h" #include "pcapio.h"
#include <pcap.h> #include <pcap.h>
using namespace std; using namespace std;
int main() int main()
{ {
std::map<std::string,std::string > devmap; std::map<std::string,std::string > devmap;
std::string errstring = PCAPIO::pcapio_interfaces(devmap); std::vector<std::string> names;
for (auto & p : devmap) std::string errstring = PCAPIO::pcapio_interfaces(devmap);
{ if (errstring.size())
cout <<p.second; {
cout <<"\n"; cout <<"Error:\n"<<errstring<<"\n";
} return 0;
}
for (auto & p : devmap)
{
names.push_back(p.first);
cout <<"==========\n";
cout << "Device " << names.size() <<":\n";
cout <<p.second;
}
cout <<"Input device name:"; cout <<"Input src device id 1~"<<names.size()<<":";
unsigned int nID = 0;
cin >> nID;
if (!nID || nID > devmap.size())
{
cout <<"Invalid ID out of range\n";
return 0;
}
std::string strDev = names[nID-1];
std::string strDev; pcap_t *handle = NULL;
cin >> strDev; char errbuf[PCAP_ERRBUF_SIZE];
handle = pcap_open_live(strDev.c_str(), 65535, 1, 10, errbuf);
if(handle == NULL)
{
printf("pcap_open_live return err,errbuf:%s...\n", errbuf);
return -1;
}
struct bpf_program filter;
char filter_app[] = "ip";
bpf_u_int32 net = 0;
int ret32 = pcap_compile(handle, &filter, filter_app, 0, net);
if(ret32 < 0)
{
printf("pcap_compile return %d, errbuf:%s\n", ret32, errbuf);
return -1;
}
ret32 = pcap_setfilter(handle, &filter);
if(ret32 < 0)
{
printf("pcap_setfilter return %d, errbuf:%s\n", ret32, errbuf);
return -1;
}
pcap_t *handle = NULL; const u_char *packet;
char errbuf[PCAP_ERRBUF_SIZE]; struct pcap_pkthdr header;
handle = pcap_open_live(strDev.c_str(), 65535, 1, 10, errbuf); //注意,要设置较大的缓存
if(handle == NULL) pcap_set_buffer_size(handle,256*1024*1024);
{
printf("pcap_open_live return err,errbuf:%s...\n", errbuf);
return -1;
}
struct bpf_program filter;
char filter_app[] = "ip";
bpf_u_int32 net = 0;
int ret32 = pcap_compile(handle, &filter, filter_app, 0, net);
if(ret32 < 0)
{
printf("pcap_compile return %d, errbuf:%s\n", ret32, errbuf);
return -1;
}
ret32 = pcap_setfilter(handle, &filter);
if(ret32 < 0)
{
printf("pcap_setfilter return %d, errbuf:%s\n", ret32, errbuf);
return -1;
}
const u_char *packet;
struct pcap_pkthdr header;
pcap_set_buffer_size(handle,128*1024*1024); while (1)
{
packet = pcap_next(handle, &header);
while (1) if(packet)
{ {
packet = pcap_next(handle, &header); printf("LEN=%d:", header.len);
if(packet) for (unsigned int i=0;i<header.len;++i)
{ {
printf("LEN=%d:", header.len); if ((i>=14 && i<32) || i+4 >= header.len)
for (int i=0;i<header.len;++i) printf ("%02X",packet[i]);
{ else if (i==32)
if ((i>=14 && i<32) || i+4 >= header.len) printf ("...");
printf ("%02X",packet[i]); }
else if (i==32) printf ("\n");
printf ("..."); //如果需要转发,则要打开另一个handle。
} //pcap_sendpacket(handle_dst,packet,header.len);
printf ("\n"); }
} }
} pcap_close(handle);
pcap_close(handle); return 0;
return 0;
} }
...@@ -2,102 +2,105 @@ ...@@ -2,102 +2,105 @@
#include <pcap.h> #include <pcap.h>
#include <string.h> #include <string.h>
namespace PCAPIO{ namespace PCAPIO{
std::string ifaddresses(pcap_if_t *d); std::string ifaddresses(pcap_if_t *d);
std::string pcapio_interfaces(std::map<std::string,std::string> & devmap) std::string pcapio_interfaces(std::map<std::string,std::string> & devmap)
{ {
std::string res; std::string res;
pcap_if_t *alldevs; pcap_if_t *alldevs;
int count = 0; char errbuf[PCAP_ERRBUF_SIZE];
char errbuf[PCAP_ERRBUF_SIZE]; if(pcap_findalldevs(&alldevs, errbuf) == -1){
if(pcap_findalldevs(&alldevs, errbuf) == -1){ res = errbuf;
res = errbuf; return res;
return res; }
} for(auto d = alldevs; d != NULL; d = d->next){
for(auto d = alldevs; d != NULL; d = d->next){ std::string d_name = d->name;
std::string d_name = d->name; std::string d_des = ifaddresses(d);
std::string d_des = ifaddresses(d); devmap[d_name] = d_des;
devmap[d_name] = d_des; }
}
pcap_freealldevs(alldevs); pcap_freealldevs(alldevs);
return res; return res;
} }
std::string address_print(unsigned char * v) std::string address_print(unsigned char * v)
{ {
std::string res; std::string res;
char buf[1024]; char buf[1024];
int lbegin = 0, rbegin = 13; int lbegin = 0, rbegin = 13;
while (!v[lbegin] ) while (!v[lbegin] )
{ {
++lbegin; ++lbegin;
if (lbegin >=rbegin) if (lbegin >=rbegin)
break; break;
} }
while (!v[rbegin] ) while (!v[rbegin] )
{ {
--rbegin; --rbegin;
if (lbegin >=rbegin) if (lbegin >=rbegin)
break; break;
} }
res += "HEX("; res += "HEX(";
for (int i =lbegin; i<=rbegin;++i) for (int i =lbegin; i<=rbegin;++i)
{ {
snprintf(buf,1024,"%02X", (unsigned int)v[i]); if (i>lbegin)
res += buf; res += ":";
} snprintf(buf,1024,"%02X", (unsigned int)v[i]);
res += ")"; res += buf;
res += "DEC("; }
for (int i =lbegin; i<=rbegin;++i) res += ")";
{ res += "DEC(";
if (i>lbegin) for (int i =lbegin; i<=rbegin;++i)
res += "."; {
snprintf(buf,1024,"%u", (unsigned int)v[i] ); if (i>lbegin)
res += buf; res += ".";
} snprintf(buf,1024,"%u", (unsigned int)v[i] );
res += ")"; res += buf;
}
res += ")";
return res; return res;
} }
/* Print all the available information on the given interface */ /* Print all the available information on the given interface */
std::string ifaddresses(pcap_if_t *d) std::string ifaddresses(pcap_if_t *d)
{ {
char buf[1024]; char buf[1024];
pcap_addr_t *a; pcap_addr_t *a;
std::string res = d->name; std::string res = "NAME=";
res += "\n"; res+=d->name;
if(d->description) res += "\n";
{ if(d->description)
res += d->description; {
res += "\n"; res += "Description=";
} res += d->description;
res += "\n";
}
for(a=d->addresses;a;a=a->next) { for(a=d->addresses;a;a=a->next) {
snprintf(buf,1024,"\tAF_0x%02X:",(unsigned int)a->addr->sa_family); snprintf(buf,1024," AF_0x%02X:",(unsigned int)a->addr->sa_family);
res += buf; res += buf;
if (a->addr) if (a->addr)
{ {
snprintf(buf,1024,"\n\t\tAddr:%s ",address_print((unsigned char *)a->addr->sa_data).c_str()); snprintf(buf,1024,"\n Addr:%s ",address_print((unsigned char *)a->addr->sa_data).c_str());
res += buf; res += buf;
} }
if (a->netmask) if (a->netmask)
{ {
snprintf(buf,1024,"\n\t\tMask:%s ",address_print((unsigned char *)a->addr->sa_data).c_str()); snprintf(buf,1024,"\n Mask:%s ",address_print((unsigned char *)a->addr->sa_data).c_str());
res += buf; res += buf;
} }
if (a->broadaddr) if (a->broadaddr)
{ {
snprintf(buf,1024,"\n\t\tCast:%s ",address_print((unsigned char *)a->addr->sa_data).c_str()); snprintf(buf,1024,"\n Cast:%s ",address_print((unsigned char *)a->addr->sa_data).c_str());
res += buf; res += buf;
} }
if (a->dstaddr) if (a->dstaddr)
{ {
snprintf(buf,1024,"\n\t\tDest:%s ",address_print((unsigned char *)a->addr->sa_data).c_str()); snprintf(buf,1024,"\n Dest:%s ",address_print((unsigned char *)a->addr->sa_data).c_str());
res += buf; res += buf;
} }
res += "\n"; res += "\n";
} }
return res; return res;
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册