Вечер добрый. Пытаюсь получить данные пакеты то есть полезную нагрузку что передается а не заголовок. но встал в тупик. заголовок верный. а вот в данных получаю мусор.
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <memory.h>
#include <string.h>
#include <time.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <arpa/inet.h>
extern "C" {
#include <linux/netfilter.h> /* Defines verdicts (NF_ACCEPT, etc) */
#include <libnetfilter_queue/libnetfilter_queue.h>
#include <libnetfilter_queue/linux_nfnetlink_queue.h>
#include <libnetfilter_queue/libnetfilter_queue_tcp.h>
#include <libnetfilter_queue/libnetfilter_queue_udp.h>
}
#define ALIGN(x) (((x) + 3) & ~3)
#define iphdr(x) ((struct iphdr *)(x))
#define tcphdr(x) ((struct tcphdr *)(x))
using namespace std;
int hex_dump(unsigned char *data, int len)
{
register int i;
register int j;
for(i = 0; i < len; i += 16)
{
printf("\n");
for(j = 0; j < 16; j++)
{
if(i+j < len)
{
printf("%02X ", data[i+j]);
}
else
{
printf(" ");
}
}
printf(" | ");
for(j = 0; j < 16; j++)
{
unsigned char character = (i+j < len) ? data[i+j] : ' ';
if((character <= 0x20) || (character >= 0x7e))
{
character = '.';
}
printf("%c", character);
}
}
printf("\n");
return 0;
}
static int Callback(nfq_q_handle *myQueue, struct nfgenmsg *msg, nfq_data *pkt, void *cbData) {
uint32_t id = 0;
nfqnl_msg_packet_hdr *header;
//cout << "pkt recvd: ";
if ((header = nfq_get_msg_packet_hdr(pkt))) {
id = ntohl(header->packet_id);
unsigned char *pktData;
int len = nfq_get_payload(pkt,(unsigned char**) &pktData);
printf("payload_len=%d ", len);
struct iphdr *iph = ((struct iphdr *) pktData);
int iphlen = iphdr(pktData)->ihl * 4;
int tcphlen = tcphdr(pktData + iphlen)->doff * 4;
int hlen = iphlen + tcphlen;
cout <<"hlen:" << hlen;
char * data = (char*)malloc(hlen);
memcpy(data, pktData + iphlen, hlen);
hex_dump((unsigned char*)data,hlen);
cout << "dlen:"<< iphlen << data << endl;
//for (int i=0;i<len;i++){
//hex_dump(&pktData[i], strlen(&pktData[i]));
//}
//memset(&pktData,0,len);
return nfq_set_verdict(myQueue, id, NF_ACCEPT, 0, NULL);
}
return 0;
}
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];
if (!(nfqHandle = nfq_open())) {
cerr << "Error in nfq_open()" << endl;
return -1;
}
if (nfq_unbind_pf(nfqHandle, AF_INET) < 0) {
cerr << "Error in nfq_unbind_pf()" << endl;
exit(1);
}
if (nfq_bind_pf(nfqHandle, AF_INET) < 0) {
cerr << "Error in nfq_bind_pf()" << endl;
exit(1);
}
if (!(myQueue = nfq_create_queue(nfqHandle, 0, &Callback, NULL))) {
cerr << "Error in nfq_create_queue()" << endl;
exit(1);
}
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);
while ((res = recv(fd, buf, sizeof(buf), 0)) && res >= 0) {
nfq_handle_packet(nfqHandle, buf, res); }
nfq_destroy_queue(myQueue);
nfq_close(nfqHandle);
return 0;
}