├── 3rdparty
└── libtins
│ └── README.md
├── LICENSE
├── Makefile
├── README.md
├── client.cpp
├── server.cpp
└── socketwrapper.h
/3rdparty/libtins/README.md:
--------------------------------------------------------------------------------
1 | Place libtins `include` folder and `lib` folder here.
2 |
3 | Version: [commit 78b94fa350ebd14f49b59cdb3ac59121f586bb94](https://github.com/mfontanini/libtins/tree/78b94fa350ebd14f49b59cdb3ac59121f586bb94)
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | This is free and unencumbered software released into the public domain.
2 |
3 | Anyone is free to copy, modify, publish, use, compile, sell, or
4 | distribute this software, either in source code form or as a compiled
5 | binary, for any purpose, commercial or non-commercial, and by any
6 | means.
7 |
8 | In jurisdictions that recognize copyright laws, the author or authors
9 | of this software dedicate any and all copyright interest in the
10 | software to the public domain. We make this dedication for the benefit
11 | of the public at large and to the detriment of our heirs and
12 | successors. We intend this dedication to be an overt act of
13 | relinquishment in perpetuity of all present and future rights to this
14 | software under copyright law.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | OTHER DEALINGS IN THE SOFTWARE.
23 |
24 | For more information, please refer to
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | CC = g++
2 | CXXFLAGS = -g -std=c++11 -I./3rdparty/libtins/include/ -L./3rdparty/libtins/lib -ltins
3 |
4 | all: client.elf server.elf
5 | @echo "Done"
6 |
7 | client.elf: client.o socketwrapper.h
8 | $(CC) client.o $(CXXFLAGS) -o $@
9 |
10 | server.elf: server.o socketwrapper.h
11 | $(CC) server.o $(CXXFLAGS) -o $@
12 |
13 | client.o: client.cpp
14 | $(CC) client.cpp -c $(CXXFLAGS)
15 |
16 | server.o: server.cpp
17 | $(CC) server.cpp -c $(CXXFLAGS)
18 |
19 | clean:
20 | rm ./*.elf
21 | rm ./*.o
22 |
23 | .PHONY: clean all
24 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## UDPTUN
2 |
3 | Simple UDP Tunnel program.
4 |
5 | ### Usage
6 |
7 | #### Client:
8 |
9 | ``` shell
10 | sudo ./client.elf -s
11 | ```
12 |
13 | After client up and running, route packet into the network interface called `clienttun` which created by this program. For example:
14 |
15 | ``` shell
16 | route add default dev clienttun # this will route all your traffic into clienttun
17 | route add 123.123.123.123 clienttun # or like this, route traffic of a specify ip (123.123.123.123)
18 | route add gw dev # use `route -n` to see your gateway
19 | ```
20 |
21 | #### Server:
22 |
23 | ``` shell
24 | sudo ./server.elf
25 | ```
26 |
27 | Server will create a tun named `servertun`. If you'd like to watch the traffic:
28 |
29 | ``` shell
30 | tcpdump -i servertun -vv -n
31 | ```
32 |
33 | ### FYI
34 |
35 | This is a learning purpose program. May contains bug.
36 |
37 | Associate blog about this program: [cnblogs:Make a simple udp-tunnel program](http://www.cnblogs.com/blumia/p/Make-a-simple-udp-tunnel-program.html) (Chinese).
--------------------------------------------------------------------------------
/client.cpp:
--------------------------------------------------------------------------------
1 | #include /* O_RDWR */
2 | #include /* memset(), memcpy() */
3 | #include /* perror(), printf(), fprintf() */
4 | #include /* exit(), malloc(), free() */
5 | #include /* read(), close() */
6 | #include /* select() */
7 |
8 | /* cxx */
9 | #include
10 |
11 | using namespace std;
12 |
13 | /* 3rd party libs */
14 | #include "socketwrapper.h"
15 | #include
16 |
17 | using namespace Tins;
18 |
19 | char remote_ip[16] = "";
20 |
21 | void process_arguments(int argc, char **argv) {
22 | bool argMissing = true;
23 | int opt;
24 |
25 | while (~(opt = getopt(argc, argv, "hHs:S:"))) {
26 | switch(opt) {
27 | case 's': case 'S':
28 | if (optarg) {
29 | strncpy(remote_ip,optarg,15);
30 | argMissing = false;
31 | } else {
32 | fputs("?\n", stdout);
33 | exit(0);
34 | }
35 | break;
36 | case 'h': case 'H':
37 | fputs("?\n", stdout);
38 | exit(0);
39 | //break;
40 | }
41 | }
42 |
43 | if (argMissing) {
44 | fputs("?\n", stdout);
45 | exit(0);
46 | }
47 | }
48 |
49 | int main(int argc, char *argv[])
50 | {
51 | int socketfd, tunfd, nbytes;
52 | char buf[1600];
53 |
54 | process_arguments(argc, argv);
55 |
56 | // dgram
57 | struct sockaddr_in srvaddr;
58 | socketfd = Socket(AF_INET, SOCK_DGRAM/* | SOCK_NONBLOCK*/, 0);
59 | bzero(&srvaddr, sizeof(srvaddr));
60 | srvaddr.sin_family = AF_INET;
61 | srvaddr.sin_port = htons(SERV_PORT);
62 | inet_pton(AF_INET, remote_ip, &srvaddr.sin_addr);
63 |
64 | // tun
65 | tunfd = tun_open("clienttun");
66 | system("route add 123.123.123.123 clienttun");
67 |
68 | fputs("Client now running in UDP mode.\n", stdout);
69 |
70 | int maxfd = (socketfd > tunfd) ? socketfd : tunfd;
71 |
72 | while(1) {
73 |
74 | fd_set rd_set;
75 |
76 | FD_ZERO(&rd_set);
77 | FD_SET(tunfd, &rd_set); FD_SET(socketfd, &rd_set);
78 |
79 | int ret = select(maxfd + 1, &rd_set, NULL, NULL, NULL);
80 | if (ret < 0 && errno == EINTR) continue;
81 | if (ret < 0) {
82 | perror("select()"); exit(1);
83 | }
84 |
85 | if(FD_ISSET(tunfd, &rd_set)) {
86 | nbytes = read(tunfd, buf, sizeof(buf));
87 | printf("Read %d bytes from clienttun\n", nbytes);
88 | //hex_dump(buf, nbytes);
89 | RawPDU p((uint8_t *)buf, nbytes);
90 | try {
91 | IP ip(p.to());
92 | cout << "IP Packet: " << ip.src_addr() << " -> " << ip.dst_addr() << std::endl;
93 | sendto(socketfd, buf, nbytes, 0, (sockaddr*)&srvaddr, sizeof(srvaddr));
94 | } catch (...) {
95 | continue;
96 | }
97 | }
98 |
99 | if(FD_ISSET(socketfd, &rd_set)) {
100 | int size84 = recvfrom(socketfd, buf, 1600, 0, NULL, NULL);
101 | printf("Recv %d bytes from udp socket\n", nbytes);
102 | write(tunfd, buf, size84);
103 | }
104 | }
105 | return 0;
106 | }
--------------------------------------------------------------------------------
/server.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include "socketwrapper.h"
9 |
10 | /* cxx */
11 | #include
12 | #include