Skip to contentSkip to conversation

compiling libipq example

DISCUSSION Essien Ita Essien 4 participants 5 messages Feb 25, 2005 – Mar 3, 2005 netfilter

lore
Share & tools
lore
#0 lore raw

Hi,

I'm trying to follow the tutorial in the libipq man page, with just a little modification, and i get a compile error when i try to access the ipq_packet_msg_t structure. I've checked where ipq_packet_msg_t is defined, and found it in <linux/netfilter_ipv4/ip_queue.h>, which i've found is conditionally included in <libipq.h> already. I even tried including straight into the program.

Any ideas what i'm doing wrong?

thanks.

The error i get is:

first.c: In function `main':
first.c:47: error: dereferencing pointer to incomplete type first.c:51: error: dereferencing pointer to incomplete type

The program is:

#include <stdio.h>
#include <linux/netfilter.h>
#include <libipq.h>
#include <linux/ip.h>
#include <linux/tcp.h>

#define BUFSIZE 2048

static void die(struct ipq_handle *h)

{
	ipq_perror("passer");
	ipq_destroy_handle(h);
	exit(1);
}

int main(int argc, char **argv)

{
	int status;
	unsigned char buf[BUFSIZE];


	struct ipq_handle *h;
	h = ipq_create_handle(0,PF_INET);
	if (!h)
		die(h);


	status = ipq_set_mode(h,IPQ_COPY_PACKET,BUFSIZE);
	if (status < 0)
		die(h);

	while (1) {
		status = ipq_read(h,buf,BUFSIZE,0);
		if (status < 0)
			die(h);

		switch(ipq_message_type(buf)) {
			case NLMSG_ERROR:
				fprintf(stderr,"Recieved Error Message: %d\n",ipq_get_msgerr(buf));
				break;

			case IPQM_PACKET: {
				struct ipq_packet_msg_t *m = ipq_get_packet(buf);
				struct iphdr *ip = (struct iphdr*) m->payload; /*this line give the 
error*/
				fprintf(stdout,"Saddr: %d\nDaddr: %d\n",ip->saddr,ip->daddr);

				
				status = ipq_set_verdict(h,m->packet_id,NF_ACCEPT,0,NULL); /*this 
line also borks*/
				if (status < 0)
					die(h);

				break;

				}

			default:
				fprintf(stderr,"Unknown Message Type!\n");
		}

	}

	ipq_destroy_handle(h);
	return 0;
}

#include <stdio.h>
#include <linux/netfilter.h>
#include <libipq.h>
#include <linux/ip.h>
#include <linux/tcp.h>

#define BUFSIZE 2048

static void die(struct ipq_handle *h)

{
	ipq_perror("passer");
	ipq_destroy_handle(h);
	exit(1);
}

int main(int argc, char **argv)

{
	int status;
	unsigned char buf[BUFSIZE];


	struct ipq_handle *h;
	h = ipq_create_handle(0,PF_INET);
	if (!h)
		die(h);


	status = ipq_set_mode(h,IPQ_COPY_PACKET,BUFSIZE);
	if (status < 0)
		die(h);

	while (1) {
		status = ipq_read(h,buf,BUFSIZE,0);
		if (status < 0)
			die(h);

		switch(ipq_message_type(buf)) {
			case NLMSG_ERROR:
				fprintf(stderr,"Recieved Error Message: %d\n",ipq_get_msgerr(buf));
				break;

			case IPQM_PACKET: {
				struct ipq_packet_msg_t *m = ipq_get_packet(buf);
				struct iphdr *ip = (struct iphdr*) m->payload; /*this line give the error*/
				fprintf(stdout,"Saddr: %d\nDaddr: %d\n",ip->saddr,ip->daddr);

				
				status = ipq_set_verdict(h,m->packet_id,NF_ACCEPT,0,NULL); /*this line also borks*/
				if (status < 0)
					die(h);

				break;

				}

			default:
				fprintf(stderr,"Unknown Message Type!\n");
		}

	}

	ipq_destroy_handle(h);
	return 0;
}
#1 lore raw
Quoted 5 lines · Essien Ita Essien

Hi,

I'm trying to follow the tutorial in the libipq man page, with just a little modification, and i get a compile error when i try to access the ipq_packet_msg_t structure. I've checked where ipq_packet_msg_t is defined, and found it in <linux/netfilter_ipv4/ip_queue.h>, which i've found is conditionally included in <libipq.h> already. I even tried including straight into the program.

Any ideas what i'm doing wrong?

I've used the attached code for testing some time ago, maybe it helps.

CC = gcc
LD = ld
CFLAGS = -Wall -O2
LDFLAGS = -lipq

all: nfqtest

clean:
rm -f nfqtest nfqtest.o

nfqtest: nfqtest.o

	$(CC) nfqtest.o -o nfqtest $(LDFLAGS)

/* just some testing code for the netfilter packet queuing to userspace
(C) 2000 by Harald Welte <[email protected]>

This code is licensed under GPL conditions
*/
#include <stdio.h>
#include <stdlib.h>
#include <libipq/libipq.h>
#include <linux/netfilter.h>
#include <sys/socket.h>
#include <sys/un.h>

#define IPQ_BUF_SIZE 100000

static struct ipq_handle *qh;

static int handle_packet(ipq_packet_msg_t *pkt)

{
	int ret;

	printf("id: %lu, mark: %lu, timestamp: %ld %ld , indev: %s, datalen: %u\n",
	       pkt->packet_id, pkt->mark, pkt->timestamp_sec, pkt->timestamp_usec,
	       pkt->indev_name, (unsigned int)pkt->data_len);

	ret = ipq_set_verdict(qh, pkt->packet_id, NF_ACCEPT, pkt->data_len, pkt->payload);
	if (ret < 0) {
		printf("error setting verdict\n");
		ipq_perror(NULL);
	}
	return ret;
}

int main(int argc, char **argv)

{
	ssize_t len;
	unsigned char *buf;

	qh = ipq_create_handle(0, PF_INET);
	if (!qh) {
		printf("can't create netlink socket\n");
		ipq_perror(NULL);
		exit(1);
	}
	if (ipq_set_mode(qh, IPQ_COPY_PACKET, 0xFFFF) < 0) {
		printf("can't set netlink mode\n");
		ipq_perror(NULL);
		exit(2);
	}

	buf = malloc(IPQ_BUF_SIZE);
	while (1) {
		int ptype, error;
		ipq_packet_msg_t *packet;

		len = ipq_read(qh, buf, IPQ_BUF_SIZE, 0);
		if (len < 0) {
			printf("len < 0\n");
			break;
		} else if (len == 0) {
			printf("timeout exceeded\n");
			continue;
		}
		ptype = ipq_message_type(buf);
		packet = ipq_get_packet(buf);
		printf("received packet, length=%d, type=%d\n", (int)len, ptype);

		switch (ptype) {
		case NLMSG_ERROR:
			error = -ipq_get_msgerr(buf);
			printf("NLMSG_ERROR: %d\n", error);
			exit(3);
			break;

		case IPQM_PACKET:
			handle_packet(packet);
			break;
		}
	}
	ipq_perror(NULL);
	exit(0);
}
#2 lore raw
Quoted 2 lines

I've used the attached code for testing some time ago, maybe it helps.

Thnx Patrick. It gonna help more now that I know what I did wrong :)

Quite grateful

#3 lore raw
In reply to Essien Ita Essien

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Essien Ita Essien wrote:

| Any ideas what i'm doing wrong?

Found it! :)

| first.c:47: error: dereferencing pointer to incomplete type

The error is on line 46:

| struct ipq_packet_msg_t *m = ipq_get_packet(buf);

it should be either

struct ipq_packet_msg *m

or

ipq_packet_msg_t *m

but not

struct ipq_packet_msg_t *m

:)

- --
- - xkr47
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)

iD8DBQFCJw5lxyF48ZTvn+4RAppkAKC+vJ6OOMUWvYwibvBLbo1xWIfj+QCgjk+l Chu0AEhgZHffTyUNtC2JlDo=
=WRhi
-----END PGP SIGNATURE-----

#4 lore raw
Quoted 11 lines

The error is on line 46:

| struct ipq_packet_msg_t *m = ipq_get_packet(buf);

it should be either

struct ipq_packet_msg *m

or

Quoted 7 lines

ipq_packet_msg_t *m

but not

struct ipq_packet_msg_t *m

:)

HOLY CROWBARS!!!

My bad thnx a million...

funny thing is EVEN after looking thru the header file... I DIDN'T notice that typedef error.

Thnx again.