/* just some testing code for the netfilter packet queuing to userspace (C) 2000 by Harald Welte This code is licensed under GPL conditions */ #include #include #include #include #include #include #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); }