Imported Upstream version 0.3.4
[anytun.git] / src / anytun-showtables.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 "datatypes.h"
34
35 #include "log.h"
36 #include "buffer.h"
37 #include "keyDerivation.h"
38 #include "seqWindow.h"
39 #include "connectionList.h"
40 #include "routingTable.h"
41 #include "networkAddress.h"
42 #include "syncCommand.h"
43
44 #include <sstream>
45 #include <iostream>
46 #include <string>
47
48 #include <boost/archive/text_oarchive.hpp>
49 #include <boost/archive/text_iarchive.hpp>
50
51
52 void output()
53 {
54   ConnectionList& cl(gConnectionList);
55   if(!cl.empty()) {
56     ConnectionMap::iterator it = cl.getBeginUnlocked();
57     mux_t mux = it->first;
58     ConnectionParam& conn(it->second);
59     std::cout << "Client " << mux << ": " ;
60     if(conn.remote_end_==PacketSourceEndpoint()) {
61       std::cout<< "not registered";
62     } else {
63       std::cout<< conn.remote_end_;
64     }
65     std::cout << std::endl;
66     //std::cout << "Connection: Keyderivation-Type: " << conn.kd_.printType() << std::endl;
67     cl.clear();
68   } else {
69     network_address_type_t types[] = {ipv4,ipv6,ethernet};
70     for(int types_idx=0; types_idx<3; types_idx++) {
71       network_address_type_t type = types[types_idx];
72       if(!gRoutingTable.empty(type)) {
73         RoutingMap::iterator it = gRoutingTable.getBeginUnlocked(type);
74         NetworkPrefix pref(it->first);
75         std::cout << "Route: " << pref.toString() << "/" << (int)pref.getNetworkPrefixLength() << " -> ";
76         mux_t mux = it->second;
77         std::cout << mux << std::endl;
78         gRoutingTable.clear(type);
79       }
80     }
81   }
82 }
83
84 void readExactly(size_t toread, std::iostream& result)
85 {
86   size_t hasread = 0;
87   while(toread > hasread && std::cin.good()) {
88     char a[1];
89     std::cin.read(a,1);
90     result.write(a,1);
91     hasread++;
92   }
93 }
94
95 void readAndProcessOne()
96 {
97   size_t message_lenght ;
98   std::stringstream message_lenght_stream;
99   readExactly(5,message_lenght_stream);
100   message_lenght_stream >> message_lenght;
101   std::stringstream void_stream;
102   readExactly(1,void_stream); //skip space
103   if(!message_lenght) {
104     return;
105   }
106   std::stringstream sync_command_stream;
107   readExactly(message_lenght, sync_command_stream);
108   //std::cout << message_lenght << std::endl;
109   //std::cout << sync_command_stream.str()<< std::endl;
110   boost::archive::text_iarchive ia(sync_command_stream);
111   SyncCommand scom(gConnectionList);
112   ia >> scom;
113 }
114
115 int main(int argc, char* argv[])
116 {
117   int ret = 0;
118
119   while(std::cin.good()) {
120     try {
121       readAndProcessOne();
122     } catch(std::exception& e) {
123       std::cout << "uncaught exception, exiting: " << e.what() << std::endl;
124     }
125     output();
126   }
127   return ret;
128 }
129