3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #include "webrtc/p2p/base/stunserver.h"
30 #include "webrtc/base/bytebuffer.h"
31 #include "webrtc/base/logging.h"
35 StunServer::StunServer(rtc::AsyncUDPSocket* socket) : socket_(socket) {
36 socket_->SignalReadPacket.connect(this, &StunServer::OnPacket);
39 StunServer::~StunServer() {
40 socket_->SignalReadPacket.disconnect(this);
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);
50 if (!msg.Read(&bbuf)) {
54 // TODO: If unknown non-optional (<= 0x7fff) attributes are found, send a
55 // 420 "Unknown Attribute" response.
57 // Send the message to the appropriate handler function.
59 case STUN_BINDING_REQUEST:
60 OnBindingRequest(&msg, remote_addr);
64 SendErrorResponse(msg, remote_addr, 600, "Operation Not Supported");
68 void StunServer::OnBindingRequest(
69 StunMessage* msg, const rtc::SocketAddress& remote_addr) {
71 GetStunBindReqponse(msg, remote_addr, &response);
72 SendResponse(response, remote_addr);
75 void StunServer::SendErrorResponse(
76 const StunMessage& msg, const rtc::SocketAddress& addr,
77 int error_code, const char* error_desc) {
79 err_msg.SetType(GetStunErrorResponseType(msg.type()));
80 err_msg.SetTransactionID(msg.transaction_id());
82 StunErrorCodeAttribute* err_code = StunAttribute::CreateErrorCode();
83 err_code->SetCode(error_code);
84 err_code->SetReason(error_desc);
85 err_msg.AddAttribute(err_code);
87 SendResponse(err_msg, addr);
90 void StunServer::SendResponse(
91 const StunMessage& msg, const rtc::SocketAddress& addr) {
94 rtc::PacketOptions options;
95 if (socket_->SendTo(buf.Data(), buf.Length(), addr, options) < 0)
96 LOG_ERR(LS_ERROR) << "sendto";
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());
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);
110 mapped_addr = StunAttribute::CreateXorAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
112 mapped_addr->SetAddress(remote_addr);
113 response->AddAttribute(mapped_addr);
116 } // namespace cricket