linux下iptables能夠將網橋IP資料包交給應用程式處理,由應用程式來決定流過網橋Ip資料包是通過還是丟棄,win7下該怎么去實作這個功能呢?急急急!在線等
下面linux下的實作,win7系統該如何實作該功能
// 回呼函式定義, 基本結構是先處理包,然后回傳裁定
static int cb(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg,struct nfq_data *nfa, void *data)
{
// 資料包處理
u_int32_t id = print_pkt(nfa);
printf("entering callback\n");
// 設定裁定
return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
}
int main(int argc, char **argv)
{
struct nfq_handle *h;
struct nfq_q_handle *qh;
struct nfnl_handle *nh;
int fd;
int rv;
char buf[4096];
printf("opening library handle\n");
// 打開nfq_handle
h = nfq_open();
if (!h) {
fprintf(stderr, "error during nfq_open()\n");
exit(1);
}
printf("unbinding existing nf_queue handler for AF_INET (if any)\n");
// 先解開和AF_INET的系結
if (nfq_unbind_pf(h, AF_INET) < 0) {
fprintf(stderr, "error during nfq_unbind_pf()\n");
exit(1);
}
printf("binding nfnetlink_queue as nf_queue handler for AF_INET\n");
// 系結到AF_INET
if (nfq_bind_pf(h, AF_INET) < 0) {
fprintf(stderr, "error during nfq_bind_pf()\n");
exit(1);
}
printf("binding this socket to queue '0'\n");
// 建立nfq_q_handle, 號碼是0, 回呼函式是cb
// 可建立多個queue,用不同的號碼區分即可
qh = nfq_create_queue(h, 0, &cb, NULL);
if (!qh) {
fprintf(stderr, "error during nfq_create_queue()\n");
exit(1);
}
printf("setting copy_packet mode\n");
// 設定資料拷貝模式, 全包拷貝
if (nfq_set_mode(qh, NFQNL_COPY_PACKET, 0xffff) < 0) {
fprintf(stderr, "can't set packet_copy mode\n");
exit(1);
}
nh = nfq_nfnlh(h);
fd = nfnl_fd(nh);
// 從netlink套接字接收資料
while ((rv = recv(fd, buf, sizeof(buf), 0)) && rv >= 0) {
printf("pkt received\n");
// 處理資料,最侄訓呼叫到相應的回呼函式
nfq_handle_packet(h, buf, rv);
}
printf("unbinding from queue 0\n");
// 釋放佇列
nfq_destroy_queue(qh);
#ifdef INSANE
/* normally, applications SHOULD NOT issue this command, since
* it detaches other programs/sockets from AF_INET, too ! */
printf("unbinding from AF_INET\n");
nfq_unbind_pf(h, AF_INET);
#endif
printf("closing library handle\n");
// 關閉nfq_handle
nfq_close(h);
exit(0);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/108218.html
標籤:網絡通信
