| 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 "api/sequence_checker.h" |
| 18 | #include "rtc_base/async_packet_socket.h" |
| 19 | #include "rtc_base/byte_buffer.h" |
| 20 | #include "rtc_base/logging.h" |
| 21 | #include "rtc_base/network/received_packet.h" |
| 22 |
| 23 | namespace cricket { |
| 24 |
| 25 | StunServer::StunServer(rtc::AsyncUDPSocket* socket) : socket_(socket) { |
| 26 | socket_->RegisterReceivedPacketCallback( |
| 27 | [&](rtc::AsyncPacketSocket* socket, const rtc::ReceivedPacket& packet) { |
| 28 | OnPacket(socket, packet); |
| 29 | }); |
| 30 | } |
| 31 |
| 32 | StunServer::~StunServer() { |
| 33 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 34 | socket_->DeregisterReceivedPacketCallback(); |
| 35 | } |
| 36 |
| 37 | void StunServer::OnPacket(rtc::AsyncPacketSocket* socket, |
| 38 | const rtc::ReceivedPacket& packet) { |
| 39 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 40 | // Parse the STUN message; eat any messages that fail to parse. |
| 41 | rtc::ByteBufferReader bbuf(packet.payload()); |
| 42 | StunMessage msg; |
| 43 | if (!msg.Read(&bbuf)) { |
| 44 | return; |
| 45 | } |
| 46 |
| 47 | // TODO(?): If unknown non-optional (<= 0x7fff) attributes are found, |
| 48 | // send a |
| 49 | // 420 "Unknown Attribute" response. |
| 50 |
| 51 | // Send the message to the appropriate handler function. |
| 52 | switch (msg.type()) { |
| 53 | case STUN_BINDING_REQUEST: |
| 54 | OnBindingRequest(&msg, packet.source_address()); |
| 55 | break; |
| 56 |
| 57 | default: |
| 58 | SendErrorResponse(msg, packet.source_address(), 600, |
| 59 | "Operation Not Supported"); |
| 60 | } |
| 61 | } |
| 62 |
| 63 | void StunServer::OnBindingRequest(StunMessage* msg, |
| 64 | const rtc::SocketAddress& remote_addr) { |
| 65 | StunMessage response(STUN_BINDING_RESPONSE, msg->transaction_id()); |
| 66 | GetStunBindResponse(msg, remote_addr, &response); |
| 67 | SendResponse(response, remote_addr); |
| 68 | } |
| 69 |
| 70 | void StunServer::SendErrorResponse(const StunMessage& msg, |
| 71 | const rtc::SocketAddress& addr, |
| 72 | int error_code, |
| 73 | absl::string_view error_desc) { |
| 74 | StunMessage err_msg(GetStunErrorResponseType(msg.type()), |
| 75 | msg.transaction_id()); |
| 76 |
| 77 | auto err_code = StunAttribute::CreateErrorCode(); |
| 78 | err_code->SetCode(error_code); |
| 79 | err_code->SetReason(std::string(error_desc)); |
| 80 | err_msg.AddAttribute(std::move(err_code)); |
| 81 |
| 82 | SendResponse(err_msg, addr); |
| 83 | } |
| 84 |
| 85 | void StunServer::SendResponse(const StunMessage& msg, |
| 86 | const rtc::SocketAddress& addr) { |
| 87 | rtc::ByteBufferWriter buf; |
| 88 | msg.Write(&buf); |
| 89 | rtc::PacketOptions options; |
| 90 | if (socket_->SendTo(buf.Data(), buf.Length(), addr, options) < 0) |
| 91 | RTC_LOG_ERR(LS_ERROR) << "sendto"; |
| 92 | } |
| 93 |
| 94 | void StunServer::GetStunBindResponse(StunMessage* message, |
| 95 | const rtc::SocketAddress& remote_addr, |
| 96 | StunMessage* response) const { |
| 97 | RTC_DCHECK_EQ(response->type(), STUN_BINDING_RESPONSE); |
| 98 | RTC_DCHECK_EQ(response->transaction_id(), message->transaction_id()); |
| 99 |
| 100 | // Tell the user the address that we received their message from. |
| 101 | std::unique_ptr<StunAddressAttribute> mapped_addr; |
| 102 | if (message->IsLegacy()) { |
| 103 | mapped_addr = StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS); |
| 104 | } else { |
| 105 | mapped_addr = StunAttribute::CreateXorAddress(STUN_ATTR_XOR_MAPPED_ADDRESS); |
| 106 | } |
| 107 | mapped_addr->SetAddress(remote_addr); |
| 108 | response->AddAttribute(std::move(mapped_addr)); |
| 109 | } |
| 110 |
| 111 | } // namespace cricket |
| 112 |