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 "talk/p2p/base/stunserver.h"
29
30#include "talk/base/bytebuffer.h"
31#include "talk/base/logging.h"
32
33namespace cricket {
34
35StunServer::StunServer(talk_base::AsyncUDPSocket* socket) : socket_(socket) {
36  socket_->SignalReadPacket.connect(this, &StunServer::OnPacket);
37}
38
39StunServer::~StunServer() {
40  socket_->SignalReadPacket.disconnect(this);
41}
42
43void StunServer::OnPacket(
44    talk_base::AsyncPacketSocket* socket, const char* buf, size_t size,
45    const talk_base::SocketAddress& remote_addr) {
46  // Parse the STUN message; eat any messages that fail to parse.
47  talk_base::ByteBuffer bbuf(buf, size);
48  StunMessage msg;
49  if (!msg.Read(&bbuf)) {
50    return;
51  }
52
53  // TODO: If unknown non-optional (<= 0x7fff) attributes are found, send a
54  //       420 "Unknown Attribute" response.
55
56  // Send the message to the appropriate handler function.
57  switch (msg.type()) {
58    case STUN_BINDING_REQUEST:
59      OnBindingRequest(&msg, remote_addr);
60      break;
61
62    default:
63      SendErrorResponse(msg, remote_addr, 600, "Operation Not Supported");
64  }
65}
66
67void StunServer::OnBindingRequest(
68    StunMessage* msg, const talk_base::SocketAddress& remote_addr) {
69  StunMessage response;
70  response.SetType(STUN_BINDING_RESPONSE);
71  response.SetTransactionID(msg->transaction_id());
72
73  // Tell the user the address that we received their request from.
74  StunAddressAttribute* mapped_addr;
75  if (!msg->IsLegacy()) {
76    mapped_addr = StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS);
77  } else {
78    mapped_addr = StunAttribute::CreateXorAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
79  }
80  mapped_addr->SetAddress(remote_addr);
81  response.AddAttribute(mapped_addr);
82
83  SendResponse(response, remote_addr);
84}
85
86void StunServer::SendErrorResponse(
87    const StunMessage& msg, const talk_base::SocketAddress& addr,
88    int error_code, const char* error_desc) {
89  StunMessage err_msg;
90  err_msg.SetType(GetStunErrorResponseType(msg.type()));
91  err_msg.SetTransactionID(msg.transaction_id());
92
93  StunErrorCodeAttribute* err_code = StunAttribute::CreateErrorCode();
94  err_code->SetCode(error_code);
95  err_code->SetReason(error_desc);
96  err_msg.AddAttribute(err_code);
97
98  SendResponse(err_msg, addr);
99}
100
101void StunServer::SendResponse(
102    const StunMessage& msg, const talk_base::SocketAddress& addr) {
103  talk_base::ByteBuffer buf;
104  msg.Write(&buf);
105  if (socket_->SendTo(buf.Data(), buf.Length(), addr) < 0)
106    LOG_ERR(LS_ERROR) << "sendto";
107}
108
109}  // namespace cricket
110