| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #include "p2p/base/stun_server.h" |
| 12 |
| 13 | #include <string> |
| 14 | #include <utility> |
| 15 |
| 16 | #include "absl/strings/string_view.h" |
| 17 | #include "rtc_base/byte_buffer.h" |
| 18 | #include "rtc_base/logging.h" |
| 19 |
| 20 | namespace cricket { |
| 21 |
| 22 | StunServer::StunServer(rtc::AsyncUDPSocket* socket) : socket_(socket) { |
| 23 | socket_->SignalReadPacket.connect(this, &StunServer::OnPacket); |
| 24 | } |
| 25 |
| 26 | StunServer::~StunServer() { |
| 27 | socket_->SignalReadPacket.disconnect(this); |
| 28 | } |
| 29 |
| 30 | void StunServer::OnPacket(rtc::AsyncPacketSocket* socket, |
| 31 | const char* buf, |
| 32 | size_t size, |
| 33 | const rtc::SocketAddress& remote_addr, |
| 34 | const int64_t& /* packet_time_us */) { |
| 35 | // Parse the STUN message; eat any messages that fail to parse. |
| 36 | rtc::ByteBufferReader bbuf(buf, size); |
| 37 | StunMessage msg; |
| 38 | if (!msg.Read(&bbuf)) { |
| 39 | return; |
| 40 | } |
| 41 |
| 42 | // TODO(?): If unknown non-optional (<= 0x7fff) attributes are found, send a |
| 43 | // 420 "Unknown Attribute" response. |
| 44 |
| 45 | // Send the message to the appropriate handler function. |
| 46 | switch (msg.type()) { |
| 47 | case STUN_BINDING_REQUEST: |
| 48 | OnBindingRequest(&msg, remote_addr); |
| 49 | break; |
| 50 |
| 51 | default: |
| 52 | SendErrorResponse(msg, remote_addr, 600, "Operation Not Supported"); |
| 53 | } |
| 54 | } |
| 55 |
| 56 | void StunServer::OnBindingRequest(StunMessage* msg, |
| 57 | const rtc::SocketAddress& remote_addr) { |
| 58 | StunMessage response(STUN_BINDING_RESPONSE, msg->transaction_id()); |
| 59 | GetStunBindResponse(msg, remote_addr, &response); |
| 60 | SendResponse(response, remote_addr); |
| 61 | } |
| 62 |
| 63 | void StunServer::SendErrorResponse(const StunMessage& msg, |
| 64 | const rtc::SocketAddress& addr, |
| 65 | int error_code, |
| 66 | absl::string_view error_desc) { |
| 67 | StunMessage err_msg(GetStunErrorResponseType(msg.type()), |
| 68 | msg.transaction_id()); |
| 69 |
| 70 | auto err_code = StunAttribute::CreateErrorCode(); |
| 71 | err_code->SetCode(error_code); |
| 72 | err_code->SetReason(std::string(error_desc)); |
| 73 | err_msg.AddAttribute(std::move(err_code)); |
| 74 |
| 75 | SendResponse(err_msg, addr); |
| 76 | } |
| 77 |
| 78 | void StunServer::SendResponse(const StunMessage& msg, |
| 79 | const rtc::SocketAddress& addr) { |
| 80 | rtc::ByteBufferWriter buf; |
| 81 | msg.Write(&buf); |
| 82 | rtc::PacketOptions options; |
| 83 | if (socket_->SendTo(buf.Data(), buf.Length(), addr, options) < 0) |
| 84 | RTC_LOG_ERR(LS_ERROR) << "sendto"; |
| 85 | } |
| 86 |
| 87 | void StunServer::GetStunBindResponse(StunMessage* message, |
| 88 | const rtc::SocketAddress& remote_addr, |
| 89 | StunMessage* response) const { |
| 90 | RTC_DCHECK_EQ(response->type(), STUN_BINDING_RESPONSE); |
| 91 | RTC_DCHECK_EQ(response->transaction_id(), message->transaction_id()); |
| 92 |
| 93 | // Tell the user the address that we received their message from. |
| 94 | std::unique_ptr<StunAddressAttribute> mapped_addr; |
| 95 | if (message->IsLegacy()) { |
| 96 | mapped_addr = StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS); |
| 97 | } else { |
| 98 | mapped_addr = StunAttribute::CreateXorAddress(STUN_ATTR_XOR_MAPPED_ADDRESS); |
| 99 | } |
| 100 | mapped_addr->SetAddress(remote_addr); |
| 101 | response->AddAttribute(std::move(mapped_addr)); |
| 102 | } |
| 103 |
| 104 | } // namespace cricket |
| 105 |