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
37 #include "commandHandler.h"
38 #include "../buffer.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
));
73 u_int16_t remote_port
;
78 buf
.setLength(MAX_COMMAND_LENGTH
);
79 len
= self
->control_sock_
.recvFrom(buf
.getBuf(), buf
.getLength(), remote_host
, remote_port
);
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;
95 self
->running_
= false;
99 bool CommandHandler::isRunning()
106 std::string
CommandHandler::handle(std::string command
)
108 istringstream
iss(command
);
114 oss
<< cookie
<< " ";
116 if(iss
.bad() || iss
.eof()) {
117 oss
<< RET_ERR_SYNTAX
;
122 std::vector
<std::string
> params
;
123 while(!iss
.bad() && !iss
.eof()) {
126 params
.push_back(tmp
);
129 switch(std::toupper(cmd
[0]))
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]);
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]);
140 if(params
.size() < 2) { oss
<< RET_ERR_SYNTAX
; break; }
141 oss
<< handleDelete(params
[0], params
[1], (params
.size() < 3) ? "" : params
[2]);
144 if(cmd
.length() > 1 && cmd
[1] == 'F') {
145 if(params
.size() < 1) { oss
<< RET_ERR_SYNTAX
; break; }
146 oss
<< handleVersionF(params
[0]);
149 oss
<< handleVersion();
155 oss
<< RET_ERR_SYNTAX
;
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
;
170 RtpSession
& session
= gRtpSessionTable
.getOrNewSession(call_id
, 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
);
189 session
.setRemotePort1(rport
);
190 session
.setRemoteAddr1(addr
);
193 oss
<< session
.getLocalPort2();
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
);
213 session
.setRemotePort2(rport
);
214 session
.setRemoteAddr2(addr
);
215 session
.isComplete(true);
216 SyncCommand
sc(call_id
);
220 oss
<< session
.getLocalPort1();
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
);
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
;
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
))
263 string
CommandHandler::handleInfo()
265 std::cout
<< "received info command, ignoring" << std::endl
;