Imported Upstream version 0.3.4
[anytun.git] / src / anytun-config.cpp
1 /*
2  *  anytun
3  *
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.
12  *
13  *
14  *  Copyright (C) 2007-2009 Othmar Gsenger, Erwin Nindl,
15  *                          Christian Pointner <satp@wirdorange.org>
16  *
17  *  This file is part of Anytun.
18  *
19  *  Anytun is free software: you can redistribute it and/or modify
20  *  it under the terms of the GNU General Public License as published by
21  *  the Free Software Foundation, either version 3 of the License, or
22  *  any later version.
23  *
24  *  Anytun is distributed in the hope that it will be useful,
25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *  GNU General Public License for more details.
28  *
29  *  You should have received a copy of the GNU General Public License
30  *  along with anytun.  If not, see <http://www.gnu.org/licenses/>.
31  */
32
33 #include <iostream>
34 #include <poll.h>
35
36 #include "datatypes.h"
37
38 #include "log.h"
39 #include "buffer.h"
40 #include "keyDerivation.h"
41 #include "keyDerivationFactory.h"
42 #include "options.h"
43 #include "connectionList.h"
44 #include "routingTable.h"
45 #include "networkAddress.h"
46 #include "packetSource.h"
47 #include "resolver.h"
48
49 #include "syncQueue.h"
50 #include "syncCommand.h"
51
52
53
54 void createConnection(const PacketSourceEndpoint& remote_end, ConnectionList& cl, uint16_t seqSize, SyncQueue& queue, mux_t mux, Semaphore& sem)
55 {
56   SeqWindow* seq = new SeqWindow(seqSize);
57   seq_nr_t seq_nr_ = 0;
58   KeyDerivation* kd = KeyDerivationFactory::create(gOpt.getKdPrf());
59   kd->init(gOpt.getKey(), gOpt.getSalt(), gOpt.getPassphrase());
60   kd->setRole(gOpt.getRole());
61   cLog.msg(Log::PRIO_NOTICE) << "added connection remote host " << remote_end;
62   ConnectionParam connparam((*kd), (*seq), seq_nr_, remote_end);
63   cl.addConnection(connparam, mux);
64
65   std::ostringstream sout;
66   boost::archive::text_oarchive oa(sout);
67   const SyncCommand scom(cl, mux);
68
69   oa << scom;
70   std::cout <<  std::setw(5) << std::setfill('0') << sout.str().size()<< ' ' << sout.str() << std::endl;
71
72   NetworkList routes = gOpt.getRoutes();
73   NetworkList::const_iterator rit;
74   for(rit = routes.begin(); rit != routes.end(); ++rit) {
75     NetworkAddress addr(rit->net_addr.c_str());
76     NetworkPrefix prefix(addr, rit->prefix_length);
77
78     gRoutingTable.addRoute(prefix, mux);
79
80     std::ostringstream sout2;
81     boost::archive::text_oarchive oa2(sout2);
82     const SyncCommand scom2(prefix);
83
84     oa2 << scom2;
85     std::cout <<  std::setw(5) << std::setfill('0') << sout2.str().size()<< ' ' << sout2.str() << std::endl;
86   }
87   sem.up();
88 }
89
90 void createConnectionResolver(PacketSourceResolverIt& it, ConnectionList& cl, uint16_t seqSize, SyncQueue& queue, mux_t mux, Semaphore& sem)
91 {
92   createConnection(*it, cl, seqSize, queue, mux, sem);
93 }
94
95 void createConnectionError(const std::exception& e, Semaphore& sem, int& ret)
96 {
97   cLog.msg(Log::PRIO_ERROR) << "uncaught runtime error: " << e.what();
98   ret = -1;
99   sem.up();
100 }
101
102 int main(int argc, char* argv[])
103 {
104   try {
105     if(!gOpt.parse(argc, argv)) {
106       exit(0);
107     }
108
109     StringList targets = gOpt.getLogTargets();
110     for(StringList::const_iterator it = targets.begin(); it != targets.end(); ++it) {
111       cLog.addTarget(*it);
112     }
113   } catch(syntax_error& e) {
114     std::cerr << e << std::endl;
115     gOpt.printUsage();
116     exit(-1);
117   }
118
119   gOpt.parse_post(); // print warnings
120
121   gResolver.init();
122
123   ConnectionList cl;
124   SyncQueue queue;
125
126   Semaphore sem;
127   int ret = 0;
128   UDPPacketSource::proto::endpoint endpoint;
129   // allow emtpy endpoint!!!
130   gResolver.resolveUdp(gOpt.getRemoteAddr(), gOpt.getRemotePort(),
131                        boost::bind(createConnectionResolver, _1, boost::ref(cl), gOpt.getSeqWindowSize(), boost::ref(queue), gOpt.getMux(), boost::ref(sem)),
132                        boost::bind(createConnectionError, _1, boost::ref(sem), boost::ref(ret)),
133                        gOpt.getResolvAddrType());
134   sem.down();
135   return ret;
136 }
137