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