Pretty-Print Packets from the helper
authorPhilipp Tölke <toelke@in.tum.de>
Tue, 20 Jul 2010 09:41:26 +0000 (09:41 +0000)
committerPhilipp Tölke <toelke@in.tum.de>
Tue, 20 Jul 2010 09:41:26 +0000 (09:41 +0000)
src/vpn/gnunet-daemon-vpn.c
src/vpn/gnunet-vpn-packet.h
src/vpn/gnunet-vpn-pretty-print.c

index 5cc8321276148d4be5e080143f7ff7e38a181546..e9e944ca890cde07a7e3f8da777a0e8bc76f72b8 100644 (file)
@@ -28,6 +28,8 @@
 #include "gnunet_program_lib.h"
 #include "gnunet_os_lib.h"
 #include "gnunet-vpn-helper-p.h"
+#include "gnunet-vpn-packet.h"
+#include "gnunet-vpn-pretty-print.h"
 /* #include "gnunet_template_service.h" */
 
 /**
@@ -71,8 +73,6 @@ static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* ts
                r += t;
        }
 
-       fprintf(stderr, "Read %d bytes for the header. The 'size' is %x, that is %d\n", r, hdr.size, ntohl(hdr.size));
-
        struct suid_packet *pkt = (struct suid_packet*) GNUNET_malloc(ntohl(hdr.size));
 
        if (memcpy(pkt, &hdr, sizeof(struct suid_packet_header)) < 0) {
@@ -89,11 +89,24 @@ static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* ts
                r += t;
        }
 
-       printf("read %d bytes. The first 87 are:\n\t", r);
-
-       for (r = 0; r < 87; r++)
-               printf("%02x ", pkt->data[r]);
-       printf("\n");
+       struct ip6_pkt *pkt6 = (struct ip6_pkt*) pkt;
+       struct ip6_tcp *pkt6_tcp;
+       struct ip6_udp *pkt6_udp;
+
+       pkt_printf(pkt6);
+       switch(pkt6->ip6_hdr.nxthdr) {
+               case 0x06:
+                       pkt6_tcp = (struct ip6_tcp*)pkt6;
+                       pkt_printf_ip6tcp(pkt6_tcp);
+                       break;
+               case 0x11:
+                       pkt6_udp = (struct ip6_udp*)pkt6;
+                       pkt_printf_ip6udp(pkt6_udp);
+                       if (ntohs(pkt6_udp->udp_hdr.dpt) == 53) {
+                               pkt_printf_ip6dns((struct ip6_udp_dns*)pkt6_udp);
+                       }
+                       break;
+       }
 
        GNUNET_free(pkt);
 
index c19e7a101a2df175754ff85950206e78fe06a9cb..89b75ba35b379bd6bd7491f675d642d6fdbe5e00 100644 (file)
@@ -1,6 +1,9 @@
 #ifndef _GNTUN_PACKET_H_
 #define _GNTUN_PACKET_H_
 
+#include "gnunet-vpn-helper-p.h"
+
+// Headers
 struct pkt_tun {
        unsigned flags:16;
        unsigned type:16;
@@ -28,7 +31,6 @@ struct tcp_pkt {
        unsigned wsz:16;
        unsigned crc:16;
        unsigned urg:16;
-       unsigned char data[1];
 };
 
 struct udp_pkt {
@@ -38,6 +40,7 @@ struct udp_pkt {
        unsigned crc:16;
 };
 
+// DNS-Stuff
 struct dns_pkt {
        unsigned short id;
 
@@ -78,37 +81,36 @@ struct dns_record {
        unsigned char* data;
 };
 
+// Complete Packets
 struct ip6_pkt {
+       struct suid_packet_header shdr;
        struct pkt_tun tun;
-       struct ip6_hdr hdr;
+       struct ip6_hdr ip6_hdr;
        unsigned char data[1];
 };
 
 struct ip6_tcp {
+       struct suid_packet_header shdr;
        struct pkt_tun tun;
-       struct ip6_hdr hdr;
-       struct tcp_pkt data;
+       struct ip6_hdr ip6_hdr;
+       struct tcp_pkt tcp_hdr;
+       unsigned char data[1];
 };
 
 struct ip6_udp {
+       struct suid_packet_header shdr;
        struct pkt_tun tun;
-       struct ip6_hdr hdr;
-       struct udp_pkt data;
+       struct ip6_hdr ip6_hdr;
+       struct udp_pkt udp_hdr;
+       unsigned char data[1];
 };
 
-void send_pkt(int fd, struct ip6_pkt* pkt);
-int recv_ipv6pkt(int fd, struct pkt_tun** pkt);
-int recv_pkt(int fd, struct pkt_tun** pkt);
-struct ip6_pkt* parse_ip6(struct pkt_tun* pkt);
-
 struct ip6_udp_dns {
-       struct ip6_udp hdr;
+       struct suid_packet_header shdr;
+       struct pkt_tun tun;
+       struct ip6_hdr ip6_hdr;
+       struct udp_pkt udp_hdr;
        struct dns_pkt data;
 };
 
-struct ip6_tcp* parse_ip6_tcp(struct ip6_pkt*);
-struct ip6_udp* parse_ip6_udp(struct ip6_pkt*);
-
-short payload(struct ip6_hdr* pkt);
-
 #endif
index 90bbe7bea36b4888c47ea9ddee7330f5bd177224..d7edf0a119ad1e794b753464e8d6d35828fa082e 100644 (file)
@@ -4,7 +4,7 @@
 #include <ctype.h>
 #include <arpa/inet.h>
 
-#include "packet.h"
+#include "gnunet-vpn-packet.h"
 
 static char* pretty = /*{{{*/
 /*     0       1         2         3         4        5          6
@@ -92,7 +92,7 @@ void pp_hexdump(unsigned char* data, char* dest, int max) {{{
 }}}
 
 void pp_write_header(char* dest, struct ip6_pkt* pkt) {{{
-       switch (pkt->hdr.nxthdr) {
+       switch (pkt->ip6_hdr.nxthdr) {
                case 0x3a:
                        memcpy(dest, "ICMPv6)", 7);
                        break;
@@ -114,32 +114,32 @@ void pkt_printf(struct ip6_pkt* pkt) {{{
 
        memcpy(buf, pretty, strlen(pretty)+1);
 
-       pp_ip6adr(pkt->hdr.sadr, buf+16);
-       pp_ip6adr(pkt->hdr.dadr, buf+76);
+       pp_ip6adr(pkt->ip6_hdr.sadr, buf+16);
+       pp_ip6adr(pkt->ip6_hdr.dadr, buf+76);
 
-       int flow = (ntohl(pkt->hdr.flowlbl));
+       int flow = (ntohl(pkt->ip6_hdr.flowlbl));
        sprintf(tmp, "%03x", flow);
        memcpy(buf+138, tmp, 3);
        sprintf(tmp, "%-8d", flow);
        memcpy(buf+143, tmp, 8);
 
-       int length = ntohs(pkt->hdr.paylgth);
+       int length = ntohs(pkt->ip6_hdr.paylgth);
        sprintf(tmp, "%02x", length);
        memcpy(buf+198, tmp, 2);
        sprintf(tmp, "%-3d", length);
        memcpy(buf+203, tmp, 3);
 
-       sprintf(tmp, "%02x", pkt->hdr.nxthdr);
+       sprintf(tmp, "%02x", pkt->ip6_hdr.nxthdr);
        memcpy(buf+258, tmp, 2);
        pp_write_header(buf+263, pkt);
 
-       sprintf(tmp, "%02x", pkt->hdr.hoplmt);
+       sprintf(tmp, "%02x", pkt->ip6_hdr.hoplmt);
        memcpy(buf+318, tmp, 2);
-       sprintf(tmp, "%-3d", pkt->hdr.hoplmt);
+       sprintf(tmp, "%-3d", pkt->ip6_hdr.hoplmt);
        memcpy(buf+323, tmp, 3);
 
-       int size = payload(&pkt->hdr);
-        int i;
+       int size = ntohs(pkt->ip6_hdr.paylgth);
+       int i;
        for(i = 0; i < 8; i++) {
                if (16*i > size) break;
                pp_hexdump(pkt->data + (16*i), buf + 420 + (i*70), size - 16*i);
@@ -150,31 +150,31 @@ void pkt_printf(struct ip6_pkt* pkt) {{{
 }}}
 
 void pkt_printf_ip6tcp(struct ip6_tcp* pkt) {{{
-       printf("spt: %u\n", ntohs(pkt->data.spt));
-       printf("dpt: %u\n", ntohs(pkt->data.dpt));
-       printf("seq: %u\n", ntohs(pkt->data.seq));
-       printf("ack: %u\n", ntohs(pkt->data.ack));
-       printf("off: %u\n", ntohs(pkt->data.off));
-       printf("wsz: %u\n", ntohs(pkt->data.wsz));
-       printf("crc: 0x%x\n", ntohs(pkt->data.crc));
-       printf("urg: %u\n", ntohs(pkt->data.urg));
+       printf("spt: %u\n", ntohs(pkt->tcp_hdr.spt));
+       printf("dpt: %u\n", ntohs(pkt->tcp_hdr.dpt));
+       printf("seq: %u\n", ntohs(pkt->tcp_hdr.seq));
+       printf("ack: %u\n", ntohs(pkt->tcp_hdr.ack));
+       printf("off: %u\n", ntohs(pkt->tcp_hdr.off));
+       printf("wsz: %u\n", ntohs(pkt->tcp_hdr.wsz));
+       printf("crc: 0x%x\n", ntohs(pkt->tcp_hdr.crc));
+       printf("urg: %u\n", ntohs(pkt->tcp_hdr.urg));
        printf("flags: %c%c%c%c%c%c%c%c\n",
-                       pkt->data.flg & 0x80 ? 'C' : '.',
-                       pkt->data.flg & 0x40 ? 'E' : '.',
-                       pkt->data.flg & 0x20 ? 'U' : '.',
-                       pkt->data.flg & 0x10 ? 'A' : '.',
-                       pkt->data.flg & 0x08 ? 'P' : '.',
-                       pkt->data.flg & 0x04 ? 'R' : '.',
-                       pkt->data.flg & 0x02 ? 'S' : '.',
-                       pkt->data.flg & 0x01 ? 'F' : '.'
+                       pkt->tcp_hdr.flg & 0x80 ? 'C' : '.',
+                       pkt->tcp_hdr.flg & 0x40 ? 'E' : '.',
+                       pkt->tcp_hdr.flg & 0x20 ? 'U' : '.',
+                       pkt->tcp_hdr.flg & 0x10 ? 'A' : '.',
+                       pkt->tcp_hdr.flg & 0x08 ? 'P' : '.',
+                       pkt->tcp_hdr.flg & 0x04 ? 'R' : '.',
+                       pkt->tcp_hdr.flg & 0x02 ? 'S' : '.',
+                       pkt->tcp_hdr.flg & 0x01 ? 'F' : '.'
                        );
 }}}
 
 void pkt_printf_ip6udp(struct ip6_udp* pkt) {{{
-       printf("spt: %u\n", ntohs(pkt->data.spt));
-       printf("dpt: %u\n", ntohs(pkt->data.dpt));
-       printf("len: %u\n", ntohs(pkt->data.len));
-       printf("crc: 0x%x\n", ntohs(pkt->data.crc));
+       printf("spt: %u\n", ntohs(pkt->udp_hdr.spt));
+       printf("dpt: %u\n", ntohs(pkt->udp_hdr.dpt));
+       printf("len: %u\n", ntohs(pkt->udp_hdr.len));
+       printf("crc: 0x%x\n", ntohs(pkt->udp_hdr.crc));
 }}}
 
 static char* dns_types(unsigned short type) {{{