]> git.sesse.net Git - greproxy/blob - greprotocol.cpp
Merge branch 'master' of morgental.zrh.sesse.net:greproxy
[greproxy] / greprotocol.cpp
1 #include <arpa/inet.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/types.h>
6 #include <sys/socket.h>
7
8 #include "greprotocol.h"
9
10 using namespace std;
11
12 struct gre_header {
13         uint8_t reserved0_hi : 4;
14         uint8_t has_seq : 1;
15         uint8_t has_key : 1;
16         uint8_t unused : 1;
17         uint8_t has_checksum : 1;
18
19         uint8_t version : 3;
20         uint8_t reserved0_lo: 5;
21
22         uint16_t protocol_type;
23 };
24
25
26 GREProtocol::GREProtocol(const in6_addr &src, const in6_addr &dst)
27 {
28         memset(&dstaddr, 0, sizeof(dstaddr));
29         dstaddr.sin6_family = AF_INET6;
30         dstaddr.sin6_addr = dst;
31
32         sock = socket(AF_INET6, SOCK_RAW, IPPROTO_GRE);
33         if (sock == -1) {
34                 perror("socket");
35                 exit(1);
36         }
37
38         sockaddr_in6 my_addr;
39         memset(&my_addr, 0, sizeof(my_addr));
40         my_addr.sin6_family = AF_INET6;
41         my_addr.sin6_addr = src;
42         if (::bind(sock, (sockaddr *)&my_addr, sizeof(my_addr)) == -1) {
43                 perror("bind");
44                 exit(1);
45         }
46 }
47
48 void GREProtocol::send_packet(uint16_t proto, const string &data, int incoming_seq)
49 {
50         char buf[4096];
51         gre_header *gre = (gre_header *)buf;
52
53         memset(gre, 0, sizeof(*gre));
54         gre->has_seq = 1;
55         gre->version = 0;
56         gre->protocol_type = htons(proto);
57
58         char *ptr = buf + sizeof(*gre);
59         int seq_be = htonl(incoming_seq);
60         memcpy(ptr, &seq_be, sizeof(seq_be));
61         ptr += sizeof(seq_be);
62
63         memcpy(ptr, data.data(), data.size());
64         
65         if (sendto(sock, buf, data.size() + sizeof(seq_be) + sizeof(*gre), 0, (sockaddr *)&dstaddr, sizeof(dstaddr)) == -1) {
66                 perror("sendto");
67                 return;
68         }
69 }
70
71 int GREProtocol::fd() const
72 {
73         return sock;
74 }
75
76 void GREProtocol::read_packet(Sender *sender)
77 {
78         struct sockaddr_storage addr;
79         socklen_t addrlen = sizeof(addr);
80         char buf[4096];
81         int ret = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&addr, &addrlen);
82         if (ret == -1) {
83                 perror("recvfrom");
84                 exit(1);
85         }
86         if (addr.ss_family != AF_INET6) {
87                 return;
88         }
89         struct in6_addr *addr6 = &((struct sockaddr_in6 *)&addr)->sin6_addr;
90         if (memcmp(addr6, &dstaddr.sin6_addr, sizeof(*addr6)) != 0) {
91                 // ignore
92                 return;
93         }
94         gre_header* gre = (gre_header *)buf;
95
96         char* ptr = buf + sizeof(gre_header);
97         if (gre->has_checksum) {
98                 ptr += 4;
99         }
100         if (gre->has_key) {
101                 ptr += 4;
102         }
103         uint32_t seq;
104         if (gre->has_seq) {
105                 seq = ntohl(*(uint32_t *)ptr);
106                 ptr += 4;
107         }
108
109         //printf("gre packet: proto=%x\n", ntohs(gre->protocol_type));
110
111         sender->send_packet(ntohs(gre->protocol_type), string(ptr, buf + ret), seq);
112 }
113