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.
32
#include "talk/p2p/base/stunserver.h"
33
#include "talk/base/bytebuffer.h"
34
#include "talk/base/logging.h"
38
StunServer::StunServer(talk_base::AsyncUDPSocket* socket) : socket_(socket) {
39
socket_->SignalReadPacket.connect(this, &StunServer::OnPacket);
42
StunServer::~StunServer() {
43
socket_->SignalReadPacket.disconnect(this);
46
void StunServer::OnPacket(
47
talk_base::AsyncPacketSocket* socket, const char* buf, size_t size,
48
const talk_base::SocketAddress& remote_addr) {
50
// TODO: If appropriate, look for the magic cookie before parsing.
52
// Parse the STUN message.
53
talk_base::ByteBuffer bbuf(buf, size);
55
if (!msg.Read(&bbuf)) {
56
SendErrorResponse(msg, remote_addr, 400, "Bad Request");
60
// TODO: If this is UDP, then we shouldn't allow non-fully-parsed messages.
62
// TODO: If unknown non-optiional (<= 0x7fff) attributes are found, send a
63
// 420 "Unknown Attribute" response.
65
// TODO: Check that a message-integrity attribute was given (or send 401
66
// "Unauthorized"). Check that a username attribute was given (or send
67
// 432 "Missing Username"). Look up the username and password. If it
68
// is missing or the HMAC is wrong, send 431 "Integrity Check Failure".
70
// Send the message to the appropriate handler function.
72
case STUN_BINDING_REQUEST:
73
OnBindingRequest(&msg, remote_addr);
76
case STUN_ALLOCATE_REQUEST:
77
OnAllocateRequest(&msg, remote_addr);
81
SendErrorResponse(msg, remote_addr, 600, "Operation Not Supported");
85
void StunServer::OnBindingRequest(
86
StunMessage* msg, const talk_base::SocketAddress& remote_addr) {
88
response.SetType(STUN_BINDING_RESPONSE);
89
response.SetTransactionID(msg->transaction_id());
91
// Tell the user the address that we received their request from.
92
StunAddressAttribute* mapped_addr;
93
if (!msg->IsLegacy()) {
94
mapped_addr = StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS);
96
mapped_addr = StunAttribute::CreateAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
98
mapped_addr->SetPort(remote_addr.port());
99
mapped_addr->SetIP(remote_addr.ipaddr());
100
response.AddAttribute(mapped_addr);
102
// TODO: Add username and message-integrity.
104
SendResponse(response, remote_addr);
107
void StunServer::OnAllocateRequest(
108
StunMessage* msg, const talk_base::SocketAddress& addr) {
109
SendErrorResponse(*msg, addr, 600, "Operation Not Supported");
112
void StunServer::OnSharedSecretRequest(
113
StunMessage* msg, const talk_base::SocketAddress& addr) {
114
SendErrorResponse(*msg, addr, 600, "Operation Not Supported");
117
void StunServer::OnSendRequest(StunMessage* msg,
118
const talk_base::SocketAddress& addr) {
119
SendErrorResponse(*msg, addr, 600, "Operation Not Supported");
122
void StunServer::SendErrorResponse(
123
const StunMessage& msg, const talk_base::SocketAddress& addr,
124
int error_code, const char* error_desc) {
127
err_msg.SetType(GetStunErrorResponseType(msg.type()));
128
err_msg.SetTransactionID(msg.transaction_id());
130
StunErrorCodeAttribute* err_code = StunAttribute::CreateErrorCode();
131
err_code->SetErrorClass(error_code / 100);
132
err_code->SetNumber(error_code % 100);
133
err_code->SetReason(error_desc);
134
err_msg.AddAttribute(err_code);
136
SendResponse(err_msg, addr);
139
void StunServer::SendResponse(
140
const StunMessage& msg, const talk_base::SocketAddress& addr) {
142
talk_base::ByteBuffer buf;
145
// TODO: Allow response addr attribute if sent from another stun server.
147
if (socket_->SendTo(buf.Data(), buf.Length(), addr) < 0)
148
LOG_ERR(LS_ERROR) << "sendto";
151
} // namespace cricket