no debug
[oweals/gnunet.git] / src / vpn / packet.c
1 #include <errno.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <sys/uio.h>
6
7 #include <linux/if_tun.h>
8
9 #include "debug.h"
10 #include "packet.h"
11 #include "arpa/inet.h"
12
13 short payload(struct ip6_hdr* hdr) {{{
14         return ntohs(hdr->paylgth);
15 }}}
16
17 void send_pkt(int fd, struct ip6_pkt* pkt) {{{
18         int sz = payload(&(pkt->hdr));
19         int w = 0;
20         char* buf = (char*)pkt;
21
22         w = 0;
23         while ( w > 0) {
24                 int t = write(fd, buf+w, (sz + 40) - w);
25                 if (t < 0)
26                         debug(1, 0, "packet: write : %s\n", strerror(errno));
27                 else
28                         w+=t;
29         }
30
31         free(buf);
32 }}}
33
34 int recv_pkt(int fd, struct pkt_tun** pkt) {{{
35         int size = 1504;
36         unsigned char data[size];
37
38         debug(1, 0, "beginning to read...\n");
39
40         int r = read(fd, data, size);
41         debug(1, 0, "read %d bytes\n", r);
42
43         *pkt = (struct pkt_tun*)malloc(r);
44
45         memcpy(*pkt, data, r);
46         struct pkt_tun *_pkt = *pkt;
47
48         debug(1, 0, "read the flags: %04x\n", ntohs(_pkt->flags));
49         debug(1, 0, "read the type: %04x\n", ntohs(_pkt->type));
50
51         switch(ntohs(_pkt->type)) {
52                 case 0x86dd:
53                         debug(1, 0, "reading an ipv6-packet\n");
54                         struct ip6_pkt * pkt6 = (struct ip6_pkt*) *pkt;
55                         size = payload(&(pkt6->hdr));
56                         // TODO: size might be greater than r!
57                         debug(1, 0, "read the size: %d\n", size);
58                         return size;
59                         break;
60                 case 0x0800:
61                         debug(1, 0, "unknown pkt-type: IPv4\n");
62                         //IPv4 TODO
63                         break;
64                 default:
65                         debug(1, 0, "unknown pkt-type: 0x%02x\n", 0x800);
66                         //Whatever TODO
67                         break;
68         }
69         return -1;
70 }}}
71
72 struct ip6_pkt* parse_ip6(struct pkt_tun* pkt) {{{
73         struct ip6_pkt* pkt6 = (struct ip6_pkt*)pkt;
74
75         return pkt6;
76 }}}
77
78 struct ip6_tcp* parse_ip6_tcp(struct ip6_pkt* pkt) {{{
79         struct ip6_tcp* res = (struct ip6_tcp*) pkt;
80
81         return res;
82 }}}
83
84 struct ip6_udp* parse_ip6_udp(struct ip6_pkt* pkt) {{{
85         struct ip6_udp* res = (struct ip6_udp*) pkt;
86
87         return res;
88 }}}