提交 b228bd0c 编写于 作者: M manjaro-xfce

Add libpcap example for low level operation.

上级 138e3da3
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
.directory
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash
# qtcreator generated files
*.pro.user*
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# Binaries
# --------
*.dll
*.exe
cmake_minimum_required(VERSION 3.5)
project(pcapio LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set (PRJ_HEADERS
pcapio.h
)
set (PRJ_SOURCES
pcapio.cpp
)
add_executable(pcapio
${PRJ_HEADERS}
${PRJ_SOURCES}
main.cpp
)
target_link_libraries(pcapio pcap)
#include <iostream>
#include "pcapio.h"
#include <pcap.h>
using namespace std;
int main()
{
std::map<std::string,std::string > devmap;
std::string errstring = PCAPIO::pcapio_interfaces(devmap);
for (auto & p : devmap)
{
cout <<p.second;
cout <<"\n";
}
cout <<"Input device name:";
std::string strDev;
cin >> strDev;
pcap_t *handle = NULL;
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;
}
const u_char *packet;
struct pcap_pkthdr header;
pcap_set_buffer_size(handle,128*1024*1024);
while (1)
{
packet = pcap_next(handle, &header);
if(packet)
{
printf("LEN=%d:", header.len);
for (int i=0;i<header.len;++i)
{
if ((i>=14 && i<32) || i+4 >= header.len)
printf ("%02X",packet[i]);
else if (i==32)
printf ("...");
}
printf ("\n");
}
}
pcap_close(handle);
return 0;
}
#include "pcapio.h"
#include <pcap.h>
#include <string.h>
namespace PCAPIO{
std::string ifaddresses(pcap_if_t *d);
std::string pcapio_interfaces(std::map<std::string,std::string> & devmap)
{
std::string res;
pcap_if_t *alldevs;
int count = 0;
char errbuf[PCAP_ERRBUF_SIZE];
if(pcap_findalldevs(&alldevs, errbuf) == -1){
res = errbuf;
return res;
}
for(auto d = alldevs; d != NULL; d = d->next){
std::string d_name = d->name;
std::string d_des = ifaddresses(d);
devmap[d_name] = d_des;
}
pcap_freealldevs(alldevs);
return res;
}
std::string address_print(unsigned char * v)
{
std::string res;
char buf[1024];
int lbegin = 0, rbegin = 13;
while (!v[lbegin] )
{
++lbegin;
if (lbegin >=rbegin)
break;
}
while (!v[rbegin] )
{
--rbegin;
if (lbegin >=rbegin)
break;
}
res += "HEX(";
for (int i =lbegin; i<=rbegin;++i)
{
snprintf(buf,1024,"%02X", (unsigned int)v[i]);
res += buf;
}
res += ")";
res += "DEC(";
for (int i =lbegin; i<=rbegin;++i)
{
if (i>lbegin)
res += ".";
snprintf(buf,1024,"%u", (unsigned int)v[i] );
res += buf;
}
res += ")";
return res;
}
/* Print all the available information on the given interface */
std::string ifaddresses(pcap_if_t *d)
{
char buf[1024];
pcap_addr_t *a;
std::string res = d->name;
res += "\n";
if(d->description)
{
res += d->description;
res += "\n";
}
for(a=d->addresses;a;a=a->next) {
snprintf(buf,1024,"\tAF_0x%02X:",(unsigned int)a->addr->sa_family);
res += buf;
if (a->addr)
{
snprintf(buf,1024,"\n\t\tAddr:%s ",address_print((unsigned char *)a->addr->sa_data).c_str());
res += buf;
}
if (a->netmask)
{
snprintf(buf,1024,"\n\t\tMask:%s ",address_print((unsigned char *)a->addr->sa_data).c_str());
res += buf;
}
if (a->broadaddr)
{
snprintf(buf,1024,"\n\t\tCast:%s ",address_print((unsigned char *)a->addr->sa_data).c_str());
res += buf;
}
if (a->dstaddr)
{
snprintf(buf,1024,"\n\t\tDest:%s ",address_print((unsigned char *)a->addr->sa_data).c_str());
res += buf;
}
res += "\n";
}
return res;
}
}
#ifndef PCAPIO_H
#define PCAPIO_H
#include <string>
#include <map>
namespace PCAPIO{
std::string pcapio_interfaces(std::map<std::string,std::string> & devmap);
}
#endif // PCAPIO_H
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册