Ciao a tutti, sto cercando di realizzare un'applicazione che resti in ascolto sulla rete e stampi tutto quello che passa, uno sniffer insomma.
I pacchetti vengono presi da iptables.
Attualmente riesco solo a stampare alcune cose ma non altre che mi interesserebbero di piu. Posto il codice:
codice:
/*
 * This code is GPL.
 */
#include <linux/netfilter.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <libipq.h>
#include <stdio.h>
#include <stdlib.h>

#define BUFSIZE 2048

static void die (struct ipq_handle *h) {
	ipq_perror ("there was an error");
	ipq_destroy_handle (h);
	exit (1);
}

int main (int argc, char **argv) {
	int status;
	unsigned char buf[BUFSIZE];
	struct ipq_handle *h;

	/* Creating an handle */
	h = ipq_create_handle (0, PF_INET);
	if (!h)
		die (h);

	/* Copying all the packet and not only metadata */
	status = ipq_set_mode (h, IPQ_COPY_PACKET, BUFSIZE);
	if (status < 0)
		die (h);

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

		switch (ipq_message_type (buf)) {
		
			case NLMSG_ERROR: {
			fprintf (stderr, "Received error message %d\n",
				 ipq_get_msgerr (buf));
			break;
		}
		
		case IPQM_PACKET: {
			ipq_packet_msg_t *m = ipq_get_packet (buf);
			
			struct iphdr *ip = (struct iphdr *) m->payload;
			
			//struct in_addr s_addr = ip->ip_src;
			//struct in_addr d_addr = ip->ip_dst;
			u_int32_t src_addr = ntohl(ip->saddr);
			
			fprintf(stderr, "Source address = %d", src_addr);
			
			struct tcphdr *tcp = (struct tcphdr *) (m->payload + (4 * ip->ihl));
			int src_port = ntohs (tcp->source);
			int dest_port = ntohs (tcp->dest);

			fprintf (stderr, "\nReceived a packet!!!\n");
			fprintf (stderr, "Source port = %d - ", src_port);
			fprintf (stderr, "Destination port = %d\n", dest_port);
			fprintf (stderr, "Syn = %d - ", tcp->syn);
			fprintf (stderr, "RST = %d - ", tcp->rst);
			fprintf (stderr, "Fyn = %d - ", tcp->fin);
			fprintf (stderr, "Ack = %d\n", tcp->ack);
			fprintf (stderr, "Sequence = %d\n", ntohs (tcp->seq));
			/*fprintf(stderr, "Packet ID = %d\n", m->packet_id);
			 * fprintf(stderr, "Marked = %d\n", m->mark);
			 * fprintf(stderr, "Hook = %d\n", m->hook);
			 * fprintf(stderr, "Data Len = %d\n", m->data_len);
			 * fprintf(stderr, "Payload = %c\n", m->payload[sizeof(m->payload)]);
			 * fprintf(stderr, "From = %s\n", m->hw_addr[sizeof(m->hw_addr)]);
			 * fprintf(stderr, "Buffer = %c\n", buf); */

			status = ipq_set_verdict (h, m->packet_id, NF_ACCEPT,
						  0, NULL);
			if (status < 0)
				die (h);
			break;
		}

		default: {
			fprintf (stderr, "Unknown message type!\n");
			break;
		}
		}
	}
	while (1);

	ipq_destroy_handle (h);
	return 0;
}
Ad esempio, mi piacerebbe sapere se un pacchetto sta entrando nella mia rete o sta cercando di uscire. Inoltre, cosa molto importante, l'indirizzo IP sorgente e destinazione. Quando lo provo a stampare ottengo solo sequenze di numeri, e non indirizzi. Credo che bisognera convertirlo, ma non conosco nessuna primitiva che lo permetta...
Qualche aiuto? Grazie