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    const talk_base::PacketTime& packet_time) {
47  // Parse the STUN message; eat any messages that fail to parse.
48  talk_base::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
68void StunServer::OnBindingRequest(
69    StunMessage* msg, const talk_base::SocketAddress& remote_addr) {
70  StunMessage response;
71  response.SetType(STUN_BINDING_RESPONSE);
72  response.SetTransactionID(msg->transaction_id());
73
74  // Tell the user the address that we received their request from.
75  StunAddressAttribute* mapped_addr;
76  if (!msg->IsLegacy()) {
77    mapped_addr = StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS);
78  } else {
79    mapped_addr = StunAttribute::CreateXorAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
80  }
81  mapped_addr->SetAddress(remote_addr);
82  response.AddAttribute(mapped_addr);
83
84  SendResponse(response, remote_addr);
85}
86
87void StunServer::SendErrorResponse(
88    const StunMessage& msg, const talk_base::SocketAddress& addr,
89    int error_code, const char* error_desc) {
90  StunMessage err_msg;
91  err_msg.SetType(GetStunErrorResponseType(msg.type()));
92  err_msg.SetTransactionID(msg.transaction_id());
93
94  StunErrorCodeAttribute* err_code = StunAttribute::CreateErrorCode();
95  err_code->SetCode(error_code);
96  err_code->SetReason(error_desc);
97  err_msg.AddAttribute(err_code);
98
99  SendResponse(err_msg, addr);
100}
101
102void StunServer::SendResponse(
103    const StunMessage& msg, const talk_base::SocketAddress& addr) {
104  talk_base::ByteBuffer buf;
105  msg.Write(&buf);
106  if (socket_->SendTo(
107      buf.Data(), buf.Length(), addr, talk_base::DSCP_NO_CHANGE) < 0)
108    LOG_ERR(LS_ERROR) << "sendto";
109}
110
111}  // namespace cricket
112