PACKET MMAP使用一例

2021-06-23 09:47:38 字數 1625 閱讀 6844

from:  

#include

#include

#include

#include

#include

#include

#include /* the l2 protocols */

void callbackpacket(char *data)

int main()

//packet_version和

so_bindtodevice可以省略

#if 0

const int tpacket_version = tpacket_v1;

/* set tpacket hdr version. */

ret = setsockopt(fd, sol_packet, packet_version, &tpacket_version, sizeof (int));

if(ret<0)

//#define netdev_name "wlan0"

#define netdev_name "eth0"

/* bind to device. */

ret = setsockopt(fd, sol_socket, so_bindtodevice, netdev_name, sizeof (netdev_name));

if(ret<0)

#endif

struct tpacket_req req;

#define per_packet_size 2048

const int buffer_size = 1024*1024*16; //16mb的緩衝區

req.tp_block_size = 4096;

req.tp_block_nr = buffer_size/req.tp_block_size;

req.tp_frame_size = per_packet_size;

req.tp_frame_nr = buffer_size/req.tp_frame_size;

ret = setsockopt(fd, sol_packet, packet_rx_ring, (void *)&req, sizeof(req));

if(ret<0)

buff = (char *)mmap(0, buffer_size, prot_read|prot_write, map_shared, fd, 0);

if(buff == map_failed)

int nindex=0, i=0;

while(1)

process_packet:

//盡力的去處理環形緩衝區中的資料frame,直到沒有資料frame了

for(i=0; i

} success:

close(fd);

munmap(buff, buffer_size);

return 0;

failed_1:

munmap(buff, buffer_size);

failed_2:

close(fd);

return -1; }

注:沒有加filter,感覺直接使用bpf巨麻煩,實際上可以直接使用libpcap的關於bpf的api,就像參考1中實現的那樣。

參考:

shell shift 使用一例

這段 的作用是 當輸入 export.sh 是將當前目錄進行打包,當輸入.export.sh xx1 xx2 xx3 xxn 時,如果xxn存在,則依次將存在的目錄進行打包,打包檔案都放在上一級目錄,其中使用到了shift命令 export.sh的原始碼如下 bin bash cur pwd ech...

shell shift 使用一例

這段 的作用是 當輸入 export.sh 是將當前目錄進行打包,當輸入.export.sh xx1 xx2 xx3 xxn 時,如果xxn存在,則依次將存在的目錄進行打包,打包檔案都放在上一級目錄,其中使用到了shift命令 export.sh的原始碼如下 bin bash cur pwd ech...

使用kvm的一例故障

機房中有一台伺服器,使用kvm後,一直滑鼠和鍵盤都不能使用,平時只能通過rdp來遠端控制,十分不方便。但是kvm相關的資料實在很少,查詢很久也沒有找到解決辦法。最近將該伺服器重新安裝了作業系統,再次發生類似情況。但是明明在bios時,滑鼠和鍵盤都能正常,但是啟動到桌面後,隨即滑鼠和鍵盤無法使用。隨後...