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-2009 Othmar Gsenger, Erwin Nindl,
15 * Christian Pointner <satp@wirdorange.org>
17 * This file is part of Anytun.
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
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.
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/>.
33 #include <boost/asio.hpp>
34 #include <boost/bind.hpp>
35 #include <boost/thread.hpp>
37 #include "datatypes.h"
38 #include "packetSource.h"
42 #include "signalController.h"
43 #include "anytunError.h"
45 void PacketSource::waitUntilReady()
50 UDPPacketSource::UDPPacketSource(std::string localaddr, std::string port)
52 gResolver.resolveUdp(localaddr, port, boost::bind(&UDPPacketSource::onResolve, this, _1), boost::bind(&UDPPacketSource::onError, this, _1), gOpt.getResolvAddrType());
55 UDPPacketSource::~UDPPacketSource()
57 std::list<SocketsElement>::iterator it = sockets_.begin();
58 for(; it != sockets_.end(); ++it) {
59 /// this might be a needed by the receiver thread, TODO cleanup
60 // delete[](it->buf_);
66 void UDPPacketSource::onResolve(PacketSourceResolverIt& it)
68 while(it != PacketSourceResolverIt()) {
69 PacketSourceEndpoint e = *it;
70 cLog.msg(Log::PRIO_NOTICE) << "opening socket: " << e;
76 sock.sock_ = new proto::socket(io_service_);
78 AnytunError::throwErr() << "memory error";
81 sock.sock_->open(e.protocol());
83 if(e.protocol() == proto::v6()) {
84 sock.sock_->set_option(boost::asio::ip::v6_only(true));
88 sockets_.push_back(sock);
93 // prepare multi-socket recv
94 if(sockets_.size() > 1) {
95 std::list<SocketsElement>::iterator it = sockets_.begin();
96 for(; it != sockets_.end(); ++it) {
97 it->len_ = MAX_PACKET_LENGTH;
98 it->buf_ = new uint8_t[it->len_];
100 AnytunError::throwErr() << "memory error";
103 it->sem_ = new Semaphore();
106 AnytunError::throwErr() << "memory error";
109 boost::thread(boost::bind(&UDPPacketSource::recv_thread, this, it));
118 void UDPPacketSource::onError(const std::runtime_error& e)
120 gSignalController.inject(SIGERROR, e.what());
123 void UDPPacketSource::recv_thread(std::list<SocketsElement>::iterator it)
125 cLog.msg(Log::PRIO_INFO) << "started receiver thread for " << it->sock_->local_endpoint();
131 result.len_ = static_cast<uint32_t>(it->sock_->receive_from(boost::asio::buffer(it->buf_, it->len_), result.remote_));
133 Lock lock(thread_result_mutex_);
134 thread_result_queue_.push(result);
136 thread_result_sem_.up();
140 uint32_t UDPPacketSource::recv(uint8_t* buf, uint32_t len, PacketSourceEndpoint& remote)
142 if(sockets_.size() == 1) {
143 return static_cast<uint32_t>(sockets_.front().sock_->receive_from(boost::asio::buffer(buf, len), remote));
146 thread_result_sem_.down();
149 Lock lock(thread_result_mutex_);
150 result = thread_result_queue_.front();
151 thread_result_queue_.pop();
153 remote = result.remote_;
154 std::memcpy(buf, result.it_->buf_, (len < result.len_) ? len : result.len_);
155 len = (len < result.len_) ? len : result.len_;
156 result.it_->sem_->up();
161 void UDPPacketSource::send(uint8_t* buf, uint32_t len, PacketSourceEndpoint remote)
163 std::list<SocketsElement>::iterator it = sockets_.begin();
164 for(; it != sockets_.end(); ++it) {
165 if(it->sock_->local_endpoint().protocol() == remote.protocol()) {
166 it->sock_->send_to(boost::asio::buffer(buf, len), remote);
170 cLog.msg(Log::PRIO_WARNING) << "no suitable socket found for remote endpoint protocol: " << remote;