Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / p2p / base / stunserver.cc
1 /*
2  * libjingle
3  * Copyright 2004--2005, Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *  1. Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright notice,
11  *     this list of conditions and the following disclaimer in the documentation
12  *     and/or other materials provided with the distribution.
13  *  3. The name of the author may not be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "webrtc/p2p/base/stunserver.h"
29
30 #include "webrtc/base/bytebuffer.h"
31 #include "webrtc/base/logging.h"
32
33 namespace cricket {
34
35 StunServer::StunServer(rtc::AsyncUDPSocket* socket) : socket_(socket) {
36   socket_->SignalReadPacket.connect(this, &StunServer::OnPacket);
37 }
38
39 StunServer::~StunServer() {
40   socket_->SignalReadPacket.disconnect(this);
41 }
42
43 void StunServer::OnPacket(
44     rtc::AsyncPacketSocket* socket, const char* buf, size_t size,
45     const rtc::SocketAddress& remote_addr,
46     const rtc::PacketTime& packet_time) {
47   // Parse the STUN message; eat any messages that fail to parse.
48   rtc::ByteBuffer bbuf(buf, size);
49   StunMessage msg;
50   if (!msg.Read(&bbuf)) {
51     return;
52   }
53
54   // TODO: If unknown non-optional (<= 0x7fff) attributes are found, send a
55   //       420 "Unknown Attribute" response.
56
57   // Send the message to the appropriate handler function.
58   switch (msg.type()) {
59     case STUN_BINDING_REQUEST:
60       OnBindingRequest(&msg, remote_addr);
61       break;
62
63     default:
64       SendErrorResponse(msg, remote_addr, 600, "Operation Not Supported");
65   }
66 }
67
68 void StunServer::OnBindingRequest(
69     StunMessage* msg, const rtc::SocketAddress& remote_addr) {
70   StunMessage response;
71   GetStunBindReqponse(msg, remote_addr, &response);
72   SendResponse(response, remote_addr);
73 }
74
75 void StunServer::SendErrorResponse(
76     const StunMessage& msg, const rtc::SocketAddress& addr,
77     int error_code, const char* error_desc) {
78   StunMessage err_msg;
79   err_msg.SetType(GetStunErrorResponseType(msg.type()));
80   err_msg.SetTransactionID(msg.transaction_id());
81
82   StunErrorCodeAttribute* err_code = StunAttribute::CreateErrorCode();
83   err_code->SetCode(error_code);
84   err_code->SetReason(error_desc);
85   err_msg.AddAttribute(err_code);
86
87   SendResponse(err_msg, addr);
88 }
89
90 void StunServer::SendResponse(
91     const StunMessage& msg, const rtc::SocketAddress& addr) {
92   rtc::ByteBuffer buf;
93   msg.Write(&buf);
94   rtc::PacketOptions options;
95   if (socket_->SendTo(buf.Data(), buf.Length(), addr, options) < 0)
96     LOG_ERR(LS_ERROR) << "sendto";
97 }
98
99 void StunServer::GetStunBindReqponse(StunMessage* request,
100                                      const rtc::SocketAddress& remote_addr,
101                                      StunMessage* response) const {
102   response->SetType(STUN_BINDING_RESPONSE);
103   response->SetTransactionID(request->transaction_id());
104
105   // Tell the user the address that we received their request from.
106   StunAddressAttribute* mapped_addr;
107   if (!request->IsLegacy()) {
108     mapped_addr = StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS);
109   } else {
110     mapped_addr = StunAttribute::CreateXorAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
111   }
112   mapped_addr->SetAddress(remote_addr);
113   response->AddAttribute(mapped_addr);
114 }
115
116 }  // namespace cricket