working version with crypto
[anytun.git] / anyrtpproxy / commandHandler.cpp
blob032d00182f5e1eca9d9200310371098239b7b373
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 <sstream>
32 #include <vector>
34 #include <iomanip>
35 #include <iostream>
37 #include "commandHandler.h"
38 #include "../buffer.h"
39 #include "../log.h"
40 #include "../syncQueue.h"
41 #include "../syncCommand.h"
42 #include "../rtpSessionTable.h"
43 #include "callIdQueue.h"
45 #define MAX_COMMAND_LENGTH 1000
47 CommandHandler::CommandHandler(SyncQueue& q, u_int16_t lp,PortWindow & pw) : queue_(q), running_(true), control_sock_(lp),
48 local_address_("0.0.0.0"), local_port_(lp),port_window_(pw)
50 pthread_create(&thread_, NULL, run, this);
53 CommandHandler::CommandHandler(SyncQueue& q, string la, u_int16_t lp,PortWindow & pw) : queue_(q), running_(true), control_sock_(la, lp),
54 local_address_(la), local_port_(lp),port_window_(pw)
56 pthread_create(&thread_, NULL, run, this);
59 CommandHandler::~CommandHandler()
61 pthread_cancel(thread_);
62 pthread_join(thread_, NULL);
65 void* CommandHandler::run(void* s)
67 CommandHandler* self = reinterpret_cast<CommandHandler*>(s);
69 Buffer buf(u_int32_t(MAX_COMMAND_LENGTH));
70 try
72 string remote_host;
73 u_int16_t remote_port;
75 int len;
76 while(1)
78 buf.setLength(MAX_COMMAND_LENGTH);
79 len = self->control_sock_.recvFrom(buf.getBuf(), buf.getLength(), remote_host, remote_port);
80 buf.setLength(len);
82 std::string ret = self->handle(std::string(reinterpret_cast<char*>(buf.getBuf()), buf.getLength())); // TODO: reinterpret is ugly
84 cLog.msg(Log::PRIO_DEBUG) << "CommandHandler received Command from " << remote_host << ":" << remote_port
85 << ", ret='" << ret << "'";
87 self->control_sock_.sendTo(ret.c_str(), ret.length(), remote_host, remote_port);
90 catch(SocketException &e)
92 self->running_ = false;
93 pthread_exit(NULL);
95 self->running_ = false;
96 pthread_exit(NULL);
99 bool CommandHandler::isRunning()
101 return running_;
106 std::string CommandHandler::handle(std::string command)
108 istringstream iss(command);
109 ostringstream oss;
110 std::string cookie;
111 std::string cmd;
113 iss >> cookie;
114 oss << cookie << " ";
116 if(iss.bad() || iss.eof()) {
117 oss << RET_ERR_SYNTAX;
118 return oss.str();
120 iss >> cmd;
122 std::vector<std::string> params;
123 while(!iss.bad() && !iss.eof()) {
124 std::string tmp;
125 iss >> tmp;
126 params.push_back(tmp);
129 switch(std::toupper(cmd[0]))
131 case CMD_REQUEST:
132 if(params.size() < 4) { oss << RET_ERR_SYNTAX; break; }
133 oss << handleRequest(cmd.erase(0,1), params[0], params[1], params[2], params[3], (params.size() < 5) ? "" : params[4]);
134 break;
135 case CMD_RESPONSE:
136 if(params.size() < 4) { oss << RET_ERR_SYNTAX; break; }
137 oss << handleResponse(cmd.erase(0,1), params[0], params[1], params[2], params[3], (params.size() < 5) ? "" : params[4]);
138 break;
139 case CMD_DELETE:
140 if(params.size() < 2) { oss << RET_ERR_SYNTAX; break; }
141 oss << handleDelete(params[0], params[1], (params.size() < 3) ? "" : params[2]);
142 break;
143 case CMD_VERSION:
144 if(cmd.length() > 1 && cmd[1] == 'F') {
145 if(params.size() < 1) { oss << RET_ERR_SYNTAX; break; }
146 oss << handleVersionF(params[0]);
147 break;
149 oss << handleVersion();
150 break;
151 case CMD_INFO:
152 oss << handleInfo();
153 break;
154 default:
155 oss << RET_ERR_SYNTAX;
156 break;
159 return oss.str();
162 string CommandHandler::handleRequest(string modifiers, string call_id, string addr, string port, string from_tag, string to_tag)
164 std::cout << "received request[" << modifiers << "] command ('" << call_id << "','" << addr << "','" << port
165 << "','" << from_tag << "','" << to_tag << "')" << std::endl;
167 try
169 bool is_new;
170 RtpSession& session = gRtpSessionTable.getOrNewSession(call_id, is_new);
171 if(is_new)
173 u_int16_t port1 = port_window_.newPort(); // TODO: get next available port
174 u_int16_t port2 = port_window_.newPort(); // TODO: get next available port
175 if( !port1 || !port2)
177 if( port1) port_window_.freePort(port1);
178 if( port2) port_window_.freePort(port2);
179 throw std::runtime_error("no free port found");
182 session.setLocalAddr("0.0.0.0"); // TODO: read this from config
183 session.setLocalPort1(port1);
184 session.setLocalPort2(port2);
186 istringstream iss(port);
187 u_int16_t rport;
188 iss >> rport;
189 session.setRemotePort1(rport);
190 session.setRemoteAddr1(addr);
192 ostringstream oss;
193 oss << session.getLocalPort2();
194 return oss.str();
196 catch(std::exception& e)
198 return RET_ERR_UNKNOWN; // TODO: change to corret error value
202 string CommandHandler::handleResponse(string modifiers, string call_id, string addr, string port, string from_tag, string to_tag)
204 std::cout << "received response[" << modifiers << "] command ('" << call_id << "','" << addr << "','" << port
205 << "','" << from_tag << "','" << to_tag << "')" << std::endl;
209 RtpSession& session = gRtpSessionTable.getSession(call_id);
210 istringstream iss(port);
211 u_int16_t rport;
212 iss >> rport;
213 session.setRemotePort2(rport);
214 session.setRemoteAddr2(addr);
215 session.isComplete(true);
216 SyncCommand sc(call_id);
217 queue_.push(sc);
219 ostringstream oss;
220 oss << session.getLocalPort1();
221 return oss.str();
223 catch(std::exception& e)
225 return RET_ERR_UNKNOWN; // TODO: change to corret error value
229 string CommandHandler::handleDelete(string call_id, string from_tag, string to_tag)
231 std::cout << "received delete command ('" << call_id << "','" << from_tag << "','" << to_tag << "')" << std::endl;
235 RtpSession& session = gRtpSessionTable.getSession(call_id);
236 session.isDead(true);
237 SyncCommand sc(call_id);
238 queue_.push(sc);
240 return RET_OK;
242 catch(std::exception& e)
244 return RET_ERR_UNKNOWN; // TODO: change to corret error value
248 string CommandHandler::handleVersion()
250 std::cout << "received version command" << std::endl;
251 return BASE_VERSION;
254 string CommandHandler::handleVersionF(string date_code)
256 std::cout << "received version[F] command ('" << date_code << "')" << std::endl;
257 if(!date_code.compare(SUP_VERSION))
258 return "1";
260 return "0";
263 string CommandHandler::handleInfo()
265 std::cout << "received info command, ignoring" << std::endl;
266 return RET_OK;