LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-21-2010, 02:05 PM   #1
rajdip
LQ Newbie
 
Registered: Jul 2010
Posts: 3

Rep: Reputation: 0
problem using libnetfilter_queue


hi,

i got some code from net...
for testing
libnetfilter_queue

but when i run i runs but doesnt so any packet has been captured...

can help ... why it doesnt work ..


/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2007 Joe Kopena, Drexel University
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Authors: Joe Kopena <tjkopena@cs.drexel.edu>
*/

#include <iostream>
#include <iomanip>

#include <time.h>

#include <netinet/in.h>
using namespace std;
extern "C" {
#include <linux/netfilter.h> /* Defines verdicts (NF_ACCEPT, etc) */
#include <libnetfilter_queue/libnetfilter_queue.h>
}

using namespace std;

//----------------------------------------------------------------------
//------------------------------------------------------
static int callback(struct nfq_q_handle *myQueue, struct nfgenmsg *msg,
struct nfq_data *pkt, void *cbData) {
uint32_t id = 0;
nfqnl_msg_packet_hdr *header;

cout << "pkt recvd: "<<endl;
if ((header = nfq_get_msg_packet_hdr(pkt))) {
id = ntohl(header->packet_id);
cout << "id " << id << "; hw_protocol " << setfill('0') << setw(4) <<
hex << ntohs(header->hw_protocol) << "; hook " << ('0'+header->hook)
<< " ; ";
}

// The HW address is only fetchable at certain hook points
nfqnl_msg_packet_hw *macAddr = nfq_get_packet_hw(pkt);
if (macAddr) {
cout << "mac len " << ntohs(macAddr->hw_addrlen) << " addr ";
for (int i = 0; i < 8; i++) {
cout << setfill('0') << setw(2) << hex << macAddr->hw_addr;
}
// end if macAddr
} else {
cout << "no MAC addr";
}

timeval tv;
if (!nfq_get_timestamp(pkt, &tv)) {
cout << "; tstamp " << tv.tv_sec << "." << tv.tv_usec;
} else {
cout << "; no tstamp";
}


cout << "; mark " << nfq_get_nfmark(pkt);

// Note that you can also get the physical devices
cout << "; indev " << nfq_get_indev(pkt);
cout << "; outdev " << nfq_get_outdev(pkt);

cout << endl;

// Print the payload; in copy meta mode, only headers will be included;
// in copy packet mode, whole packet will be returned.
unsigned char *pktData;
int len = nfq_get_payload(pkt, &pktData);
if (len) {
cout << "data[" << len << "]: '";
for (int i = 0; i < len; i++) {
if (isprint(pktData[i]))
cout << pktData[i];
else cout << " ";
}
cout << "'" << endl;
// end data found
}

// For this program we'll always accept the packet...
return nfq_set_verdict(myQueue, id, NF_ACCEPT, 0, NULL);

// end Callback
}

//----------------------------------------------------------------------
//------------------------------------------------------
int main(int argc, char **argv) {
struct nfq_handle *nfqHandle;

struct nfq_q_handle *myQueue;
struct nfnl_handle *netlinkHandle;

int fd, res;
char buf[4096];
cout<<"1 ..... Get a queue connection handle from the module"<< endl;
// Get a queue connection handle from the module
if (!(nfqHandle = nfq_open())) {
cerr << "Error in nfq_open()" << endl;
exit(-1);
}

cout<<"2 ..... Unbind the handler from processing any IP packets"<< endl;
// Unbind the handler from processing any IP packets
// Not totally sure why this is done, or if it's necessary...
if (nfq_unbind_pf(nfqHandle, AF_UNSPEC) < 0) {
cerr << "Error in nfq_unbind_pf()" << endl;
exit(1);
}

cout<<"3 ..... Bind this handler to process IP packets..."<< endl;
// Bind this handler to process IP packets...
if (nfq_bind_pf(nfqHandle, AF_UNSPEC) < 0) {
cerr << "Error in nfq_bind_pf()" << endl;
exit(1);
}

cout<<"4 ..... Install a callback on queue 0 "<< endl;
// Install a callback on queue 0
if (!(myQueue = nfq_create_queue(nfqHandle,0,callback, NULL))) {
cerr << "Error in nfq_create_queue()" << endl;
exit(1);
}
cout<<"5 ..... Turn on packet copy mode "<< myQueue<< endl;
// Turn on packet copy mode
if (nfq_set_mode(myQueue, NFQNL_COPY_PACKET, 0xffff) < 0) {
cerr << "Could not set packet copy mode" << endl;
exit(1);
}

netlinkHandle = nfq_nfnlh(nfqHandle);
fd = nfnl_fd(netlinkHandle);
cout<<"6 ..... FD created...."<< myQueue<< endl;

/* while ((res = recv(fd, buf, sizeof(buf), 0)) && res >= 0) {
cout<<"entered into loop " << res << endl;
// I am not totally sure why a callback mechanism is used
// rather than just handling it directly here, but that
// seems to be the convention...
nfq_handle_packet(nfqHandle, buf, res);
// end while receiving traffic
cout<<buf<<endl;
}*/

while ((res = recv(fd, buf, sizeof(buf), 0)) && res >= 0)
{
cout<<" pkt received "<<endl;
nfq_handle_packet(nfqHandle, buf, res);
}


cout<<"7 ..... Exited from while loop...."<< endl;
nfq_destroy_queue(myQueue);
cout<<"8 ..... Queue is destroyed...."<< endl;
nfq_close(nfqHandle);
cout<<"9 ..... NFQ_HANDLE is closed...."<< endl;

return 0;

// end main
}
 
Old 07-27-2010, 08:19 AM   #2
rajdip
LQ Newbie
 
Registered: Jul 2010
Posts: 3

Original Poster
Rep: Reputation: 0
solution

i forgot to activate chain....

that is

iptables -A OUTPUT -p tcp -j NFQUEUE
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem using libnetfilter_queue rooky Programming 3 07-21-2010 02:01 PM
Using libnetfilter_queue reta Linux - Networking 1 02-28-2010 04:09 AM
Query regarding libnetfilter_queue usage vragukumar Linux - Networking 1 01-08-2010 06:29 PM
Error in libnetfilter_queue madhava_kulkarni1986 Linux - Newbie 2 12-21-2008 01:17 AM
perl problem? apache problem? cgi problem? WorldBuilder Linux - Software 1 09-17-2003 07:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration