Imported Upstream version 0.3.3
[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 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-2009 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
33 #include <boost/asio.hpp>
34 #include <boost/bind.hpp>
35 #include <boost/thread.hpp>
36
37 #include "datatypes.h"
38 #include "packetSource.h"
39 #include "log.h"
40 #include "resolver.h"
41 #include "options.h"
42 #include "signalController.h"
43 #include "anytunError.h"
44
45 void PacketSource::waitUntilReady()
46 {
47   ready_sem_.down();
48 }
49
50 UDPPacketSource::UDPPacketSource(std::string localaddr, std::string port)
51 {
52   gResolver.resolveUdp(localaddr, port, boost::bind(&UDPPacketSource::onResolve, this, _1), boost::bind(&UDPPacketSource::onError, this, _1), gOpt.getResolvAddrType());
53 }
54
55 UDPPacketSource::~UDPPacketSource()
56 {
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_);
61 //    delete(it->sem_);
62 //    delete(it->sock_);
63   }
64 }
65
66 void UDPPacketSource::onResolve(PacketSourceResolverIt& it)
67 {
68   while(it != PacketSourceResolverIt()) {
69     PacketSourceEndpoint e = *it;
70     cLog.msg(Log::PRIO_NOTICE) << "opening socket: " << e;
71
72     SocketsElement sock;
73     sock.buf_ = NULL;
74     sock.len_ = 0;
75     sock.sem_ = NULL;
76     sock.sock_ = new proto::socket(io_service_);
77     if(!sock.sock_)
78       AnytunError::throwErr() << "memory error";
79
80     sock.sock_->open(e.protocol());
81 #ifndef _MSC_VER
82     if(e.protocol() == proto::v6())
83       sock.sock_->set_option(boost::asio::ip::v6_only(true));
84 #endif
85     sock.sock_->bind(e);
86     sockets_.push_back(sock);
87
88     it++;
89   }
90
91       // prepare multi-socket recv
92   if(sockets_.size() > 1) {
93     std::list<SocketsElement>::iterator it = sockets_.begin();
94     for(;it != sockets_.end(); ++it) {
95       it->len_ = MAX_PACKET_LENGTH;
96       it->buf_ = new u_int8_t[it->len_];
97       if(!it->buf_)
98         AnytunError::throwErr() << "memory error";
99       
100       it->sem_ = new Semaphore();
101       if(!it->sem_) {
102         delete[](it->buf_);
103         AnytunError::throwErr() << "memory error";
104       }
105
106       boost::thread(boost::bind(&UDPPacketSource::recv_thread, this, it));
107       it->sem_->up();
108     }
109
110   }
111
112   ready_sem_.up();
113 }
114
115 void UDPPacketSource::onError(const std::runtime_error& e)
116 {
117   gSignalController.inject(SIGERROR, e.what());
118 }
119
120 void UDPPacketSource::recv_thread(std::list<SocketsElement>::iterator it)
121 {
122   cLog.msg(Log::PRIO_INFO) << "started receiver thread for " << it->sock_->local_endpoint();
123
124   ThreadResult result;
125   result.it_ = it;
126   for(;;) {
127     it->sem_->down();
128     result.len_ = static_cast<u_int32_t>(it->sock_->receive_from(boost::asio::buffer(it->buf_, it->len_), result.remote_));
129     {
130       Lock lock(thread_result_mutex_);
131       thread_result_queue_.push(result);
132     }
133     thread_result_sem_.up();
134   }
135 }
136
137 u_int32_t UDPPacketSource::recv(u_int8_t* buf, u_int32_t len, PacketSourceEndpoint& remote)
138 {
139   if(sockets_.size() == 1)
140     return static_cast<u_int32_t>(sockets_.front().sock_->receive_from(boost::asio::buffer(buf, len), remote));
141
142   thread_result_sem_.down();
143   ThreadResult result;
144   {
145     Lock lock(thread_result_mutex_);
146     result = thread_result_queue_.front();
147     thread_result_queue_.pop();
148   }
149   remote = result.remote_;
150   std::memcpy(buf, result.it_->buf_, (len < result.len_) ? len : result.len_);
151   len = (len < result.len_) ? len : result.len_;
152   result.it_->sem_->up();
153
154   return len;
155 }
156
157 void UDPPacketSource::send(u_int8_t* buf, u_int32_t len, PacketSourceEndpoint remote)
158 {
159   std::list<SocketsElement>::iterator it = sockets_.begin();
160   for(;it != sockets_.end(); ++it) {
161     if(it->sock_->local_endpoint().protocol() == remote.protocol()) {
162       it->sock_->send_to(boost::asio::buffer(buf, len), remote);
163       return;
164     }
165   }
166   cLog.msg(Log::PRIO_WARNING) << "no suitable socket found for remote endpoint protocol: " << remote;
167 }
168