Imported Upstream version 0.3
[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-2008 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 version 3 as
21  *  published by the Free Software Foundation.
22  *
23  *  Anytun is distributed in the hope that it will be useful,
24  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  *  GNU General Public License for more details.
27  *
28  *  You should have received a copy of the GNU General Public License
29  *  along with anytun.  If not, see <http://www.gnu.org/licenses/>.
30  */
31
32 #include <iostream>
33 #include <poll.h>
34
35 #include "datatypes.h"
36
37 #include "log.h"
38 #include "buffer.h"
39 #include "keyDerivation.h"
40 #include "keyDerivationFactory.h"
41 #include "options.h"
42 #include "connectionList.h"
43 #include "routingTable.h"
44 #include "networkAddress.h"
45 #include "packetSource.h"
46 #include "resolver.h"
47
48 #include "syncQueue.h"
49 #include "syncCommand.h"
50
51
52
53 void createConnection(const PacketSourceEndpoint & remote_end, ConnectionList & cl, u_int16_t seqSize, SyncQueue & queue, mux_t mux, Semaphore& sem)
54 {
55   SeqWindow * seq = new SeqWindow(seqSize);
56   seq_nr_t seq_nr_ = 0;
57   KeyDerivation * kd = KeyDerivationFactory::create(gOpt.getKdPrf());
58   kd->init(gOpt.getKey(), gOpt.getSalt(), gOpt.getPassphrase());
59   kd->setRole(gOpt.getRole());
60   cLog.msg(Log::PRIO_NOTICE) << "added connection remote host " << remote_end;
61   ConnectionParam connparam ((*kd), (*seq), seq_nr_, remote_end);
62   cl.addConnection(connparam, mux);
63
64   std::ostringstream sout;
65   boost::archive::text_oarchive oa(sout);
66   const SyncCommand scom(cl, mux);
67
68   oa << scom;
69   std::cout <<  std::setw(5) << std::setfill('0') << sout.str().size()<< ' ' << sout.str() << std::endl;
70
71   NetworkList routes = gOpt.getRoutes();
72   NetworkList::const_iterator rit;
73   for(rit = routes.begin(); rit != routes.end(); ++rit)
74   {
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 createConnectionError(const std::exception& e, Semaphore& sem, int& ret)
91 {
92   cLog.msg(Log::PRIO_ERROR) << "uncaught runtime error: " << e.what();
93   ret = -1;
94   sem.up();
95 }
96
97 int main(int argc, char* argv[])
98 {
99   try 
100   {
101     bool result = gOpt.parse(argc, argv);
102     if(!result) {
103       gOpt.printUsage();
104       exit(0);
105     }
106     StringList targets = gOpt.getLogTargets();
107     if(targets.empty()) {
108       cLog.addTarget("stderr:2");
109     }
110     else {
111       StringList::const_iterator it;
112       for(it = targets.begin();it != targets.end(); ++it)
113         cLog.addTarget(*it);
114     }
115   }
116   catch(syntax_error& e)
117   {
118     std::cerr << e << std::endl;
119     gOpt.printUsage();
120     exit(-1);
121   }
122
123   gOpt.parse_post(); // print warnings  
124
125   gResolver.init();
126
127         ConnectionList cl;
128         SyncQueue queue;
129
130   Semaphore sem;
131   int ret = 0;
132         UDPPacketSource::proto::endpoint endpoint;
133         // allow emtpy endpoint!!!
134         gResolver.resolveUdp(gOpt.getRemoteAddr(), gOpt.getRemotePort(), 
135                                                                                          boost::bind(createConnection, _1, boost::ref(cl), gOpt.getSeqWindowSize(), boost::ref(queue), gOpt.getMux(), boost::ref(sem)),
136                                                                                          boost::bind(createConnectionError, _1, boost::ref(sem), boost::ref(ret)), 
137                                                                                          gOpt.getResolvAddrType());
138         sem.down();
139   return ret;
140 }
141