]> git.sesse.net Git - greproxy/blob - tungre.cpp
Port greproxy to all the new classes and stuff.
[greproxy] / tungre.cpp
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <sys/ioctl.h>
6 #include <netinet/in.h>
7 #include <arpa/inet.h>
8
9 #include <map>
10 #include <string>
11 #include <queue>
12
13 #include "greprotocol.h"
14 #include "protocol.h"
15 #include "reorderer.h"
16 #include "tunprotocol.h"
17
18 using namespace std;
19
20 in6_addr get_addr(const char *str) {
21         in6_addr ret;
22         if (inet_pton(AF_INET6, str, &ret) != 1) {
23                 fprintf(stderr, "Could not parse %s\n", str);
24                 exit(1);
25         }
26         return ret;
27 }
28
29 int main(int argc, char **argv)
30 {
31         in6_addr myaddr = get_addr(argv[1]);
32         in6_addr remoteaddr = get_addr(argv[2]);
33         GREProtocol gre(myaddr, remoteaddr);
34         TUNProtocol tun("tungre");
35
36         Reorderer tun_reorderer(&tun);
37
38         fd_set fds;
39         FD_ZERO(&fds);
40         for ( ;; ) {
41                 FD_SET(gre.fd(), &fds);
42                 FD_SET(tun.fd(), &fds);
43                 int ret = select(1024, &fds, NULL, NULL, NULL);
44                 if (ret == -1) {
45                         perror("select");
46                         continue;
47                 }
48
49                 if (FD_ISSET(gre.fd(), &fds)) {
50                         gre.read_packet(&tun_reorderer);
51                 }
52                 if (FD_ISSET(tun.fd(), &fds)) {
53                         tun.read_packet(&gre);
54                 }
55         }
56 }