seqWindow test
[anytun.git] / anytun.cpp
blob8924697111b034c312dea48015f705bda86f5597
1 /*
2 * anytun
4 * The secure anycast tunneling protocol (satp) defines a protocol used
5 * for communication between any combination of unicast and anycast
6 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
7 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
8 * ethernet, ip, arp ...). satp directly includes cryptography and
9 * message authentication based on the methodes used by SRTP. It is
10 * intended to deliver a generic, scaleable and secure solution for
11 * tunneling and relaying of packets of any protocol.
14 * Copyright (C) 2007 anytun.org <satp@wirdorange.org>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program (see the file COPYING included with this
27 * distribution); if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <iostream>
32 #include <poll.h>
34 #include "datatypes.h"
36 #include "log.h"
37 #include "buffer.h"
38 #include "packet.h"
39 #include "cypher.h"
40 #include "authAlgo.h"
41 #include "signalController.h"
42 #include "packetSource.h"
43 #include "tunDevice.h"
44 #include "options.h"
45 #include "seqWindow.h"
47 #define PAYLOAD_TYPE_TAP 0x6558
48 #define PAYLOAD_TYPE_TUN 0x0800
50 struct Param
52 Options& opt;
53 TunDevice& dev;
54 Cypher& c;
55 AuthAlgo& a;
56 PacketSource& src;
57 SeqWindow& seq;
60 void* sender(void* p)
62 Param* param = reinterpret_cast<Param*>(p);
64 seq_nr_t seq = 0;
65 while(1)
67 Packet pack(1600); // fix me... mtu size
69 // read packet from device
70 int len = param->dev.read(pack);
71 pack.resizeBack(len);
73 if(param->opt.getRemoteAddr() == "")
74 continue;
76 // add payload type
77 if(param->dev.getType() == TunDevice::TYPE_TUN)
78 pack.addPayloadType(PAYLOAD_TYPE_TUN);
79 else if(param->dev.getType() == TunDevice::TYPE_TAP)
80 pack.addPayloadType(PAYLOAD_TYPE_TAP);
81 else
82 pack.addPayloadType(0);
84 // cypher the packet
85 param->c.cypher(pack);
87 // add header to packet
88 pack.addHeader(param->opt.getSenderId(), seq);
90 // calc auth_tag and add it to the packet
91 auth_tag_t at = param->a.calc(pack);
92 pack.addAuthTag(at);
94 // send it out to remote host
95 param->src.send(pack, param->opt.getRemoteAddr(), param->opt.getRemotePort());
97 pthread_exit(NULL);
100 void* receiver(void* p)
102 Param* param = reinterpret_cast<Param*>(p);
104 while(1)
106 string remote_host;
107 u_int16_t remote_port;
108 Packet pack(1600); // fix me... mtu size
110 // read packet from socket
111 u_int32_t len = param->src.recv(pack, remote_host, remote_port);
112 pack.resizeBack(len);
113 pack.withPayloadType(true).withHeader(true).withAuthTag(true);
115 // check auth_tag and remove it
116 auth_tag_t at = pack.getAuthTag();
117 pack.removeAuthTag();
118 if(at != param->a.calc(pack))
119 continue;
121 // autodetect peer
122 if(param->opt.getRemoteAddr() == "")
124 param->opt.setRemoteAddrPort(remote_host, remote_port);
125 cLog.msg(Log::PRIO_NOTICE) << "autodetected remote host " << remote_host << ":" << remote_port;
127 // compare sender_id and seq with window
128 if(param->seq.hasSeqNr(pack.getSenderId(), pack.getSeqNr()))
129 continue;
130 param->seq.addSeqNr(pack.getSenderId(), pack.getSeqNr());
131 pack.removeHeader();
133 // decypher the packet
134 param->c.cypher(pack);
136 // check payload_type and remove it
137 if((param->dev.getType() == TunDevice::TYPE_TUN && pack.getPayloadType() != PAYLOAD_TYPE_TUN) ||
138 (param->dev.getType() == TunDevice::TYPE_TAP && pack.getPayloadType() != PAYLOAD_TYPE_TAP))
139 continue;
140 pack.removePayloadType();
142 // write it on the device
143 param->dev.write(pack);
145 pthread_exit(NULL);
148 int main(int argc, char* argv[])
150 std::cout << "anytun - secure anycast tunneling protocol" << std::endl;
151 Options opt;
152 if(!opt.parse(argc, argv))
154 opt.printUsage();
155 exit(-1);
157 cLog.msg(Log::PRIO_NOTICE) << "anytun started...";
159 SignalController sig;
160 sig.init();
162 TunDevice dev(opt.getDevName().c_str(), opt.getIfconfigParamLocal().c_str(), opt.getIfconfigParamRemoteNetmask().c_str());
163 SeqWindow seq(opt.getSeqWindowSize());
164 NullCypher c;
165 NullAuthAlgo a;
166 PacketSource* src;
167 if(opt.getLocalAddr() == "")
168 src = new UDPPacketSource(opt.getLocalPort());
169 else
170 src = new UDPPacketSource(opt.getLocalAddr(), opt.getLocalPort());
172 struct Param p = {opt, dev, c, a, *src, seq};
174 std::cout << "dev created (opened)" << std::endl;
175 std::cout << "dev opened - actual name is '" << p.dev.getActualName() << "'" << std::endl;
176 std::cout << "dev type is '" << p.dev.getTypeString() << "'" << std::endl;
178 pthread_t senderThread;
179 pthread_create(&senderThread, NULL, sender, &p);
180 pthread_t receiverThread;
181 pthread_create(&receiverThread, NULL, receiver, &p);
183 int ret = sig.run();
185 pthread_cancel(senderThread);
186 pthread_cancel(receiverThread);
187 pthread_join(senderThread, NULL);
188 pthread_join(receiverThread, NULL);
190 delete src;
192 return ret;