New upstream version 0.3.6
[anytun.git] / src / packetSource.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 methods 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-2014 Markus Grüneis, 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  *  In addition, as a special exception, the copyright holders give
33  *  permission to link the code of portions of this program with the
34  *  OpenSSL library under certain conditions as described in each
35  *  individual source file, and distribute linked combinations
36  *  including the two.
37  *  You must obey the GNU General Public License in all respects
38  *  for all of the code used other than OpenSSL.  If you modify
39  *  file(s) with this exception, you may extend this exception to your
40  *  version of the file(s), but you are not obligated to do so.  If you
41  *  do not wish to do so, delete this exception statement from your
42  *  version.  If you delete this exception statement from all source
43  *  files in the program, then also delete it here.
44  */
45
46 #include <boost/asio.hpp>
47 #include <boost/bind.hpp>
48 #include <boost/thread.hpp>
49
50 #include "datatypes.h"
51 #include "packetSource.h"
52 #include "log.h"
53 #include "resolver.h"
54 #include "options.h"
55 #include "signalController.h"
56 #include "anytunError.h"
57
58 void PacketSource::waitUntilReady()
59 {
60   ready_sem_.down();
61 }
62
63 UDPPacketSource::UDPPacketSource(std::string localaddr, std::string port)
64 {
65   gResolver.resolveUdp(localaddr, port, boost::bind(&UDPPacketSource::onResolve, this, _1), boost::bind(&UDPPacketSource::onError, this, _1), gOpt.getResolvAddrType());
66 }
67
68 UDPPacketSource::~UDPPacketSource()
69 {
70   std::list<SocketsElement>::iterator it = sockets_.begin();
71   for(; it != sockets_.end(); ++it) {
72     /// this might be a needed by the receiver thread, TODO cleanup
73     //    delete[](it->buf_);
74     //    delete(it->sem_);
75     //    delete(it->sock_);
76   }
77 }
78
79 void UDPPacketSource::onResolve(PacketSourceResolverIt it)
80 {
81   while(it != PacketSourceResolverIt()) {
82     PacketSourceEndpoint e = *it;
83     cLog.msg(Log::PRIO_NOTICE) << "opening socket: " << e;
84
85     SocketsElement sock;
86     sock.buf_ = NULL;
87     sock.len_ = 0;
88     sock.sem_ = NULL;
89     sock.sock_ = new proto::socket(io_service_);
90     if(!sock.sock_) {
91       AnytunError::throwErr() << "memory error";
92     }
93
94     sock.sock_->open(e.protocol());
95 #if !defined(_MSC_VER) && !defined(MINGW)
96     if(e.protocol() == proto::v6()) {
97       sock.sock_->set_option(boost::asio::ip::v6_only(true));
98     }
99 #endif
100     sock.sock_->bind(e);
101     sockets_.push_back(sock);
102
103     it++;
104   }
105
106   // prepare multi-socket recv
107   if(sockets_.size() > 1) {
108     std::list<SocketsElement>::iterator it = sockets_.begin();
109     for(; it != sockets_.end(); ++it) {
110       it->len_ = MAX_PACKET_LENGTH;
111       it->buf_ = new uint8_t[it->len_];
112       if(!it->buf_) {
113         AnytunError::throwErr() << "memory error";
114       }
115
116       it->sem_ = new Semaphore();
117       if(!it->sem_) {
118         delete[](it->buf_);
119         AnytunError::throwErr() << "memory error";
120       }
121
122       boost::thread(boost::bind(&UDPPacketSource::recv_thread, this, it));
123       it->sem_->up();
124     }
125
126   }
127
128   ready_sem_.up();
129 }
130
131 void UDPPacketSource::onError(const std::runtime_error& e)
132 {
133   gSignalController.inject(SIGERROR, e.what());
134 }
135
136 void UDPPacketSource::recv_thread(std::list<SocketsElement>::iterator it)
137 {
138   cLog.msg(Log::PRIO_INFO) << "started receiver thread for " << it->sock_->local_endpoint();
139
140   ThreadResult result;
141   result.it_ = it;
142   for(;;) {
143     it->sem_->down();
144     result.len_ = static_cast<uint32_t>(it->sock_->receive_from(boost::asio::buffer(it->buf_, it->len_), result.remote_));
145     {
146       Lock lock(thread_result_mutex_);
147       thread_result_queue_.push(result);
148     }
149     thread_result_sem_.up();
150   }
151 }
152
153 uint32_t UDPPacketSource::recv(uint8_t* buf, uint32_t len, PacketSourceEndpoint& remote)
154 {
155   if(sockets_.size() == 1) {
156     return static_cast<uint32_t>(sockets_.front().sock_->receive_from(boost::asio::buffer(buf, len), remote));
157   }
158
159   thread_result_sem_.down();
160   ThreadResult result;
161   {
162     Lock lock(thread_result_mutex_);
163     result = thread_result_queue_.front();
164     thread_result_queue_.pop();
165   }
166   remote = result.remote_;
167   std::memcpy(buf, result.it_->buf_, (len < result.len_) ? len : result.len_);
168   len = (len < result.len_) ? len : result.len_;
169   result.it_->sem_->up();
170
171   return len;
172 }
173
174 void UDPPacketSource::send(uint8_t* buf, uint32_t len, PacketSourceEndpoint remote)
175 {
176   std::list<SocketsElement>::iterator it = sockets_.begin();
177   for(; it != sockets_.end(); ++it) {
178     if(it->sock_->local_endpoint().protocol() == remote.protocol()) {
179       it->sock_->send_to(boost::asio::buffer(buf, len), remote);
180       return;
181     }
182   }
183   cLog.msg(Log::PRIO_WARNING) << "no suitable socket found for remote endpoint protocol: " << remote;
184 }
185