You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "talk/p2p/base/stunserver.h"
  28. #include "talk/base/bytebuffer.h"
  29. #include "talk/base/logging.h"
  30. namespace cricket {
  31. StunServer::StunServer(talk_base::AsyncUDPSocket* socket) : socket_(socket) {
  32. socket_->SignalReadPacket.connect(this, &StunServer::OnPacket);
  33. }
  34. StunServer::~StunServer() {
  35. socket_->SignalReadPacket.disconnect(this);
  36. }
  37. void StunServer::OnPacket(
  38. talk_base::AsyncPacketSocket* socket, const char* buf, size_t size,
  39. const talk_base::SocketAddress& remote_addr) {
  40. // Parse the STUN message; eat any messages that fail to parse.
  41. talk_base::ByteBuffer bbuf(buf, size);
  42. StunMessage msg;
  43. if (!msg.Read(&bbuf)) {
  44. return;
  45. }
  46. // TODO: If unknown non-optional (<= 0x7fff) attributes are found, send a
  47. // 420 "Unknown Attribute" response.
  48. // Send the message to the appropriate handler function.
  49. switch (msg.type()) {
  50. case STUN_BINDING_REQUEST:
  51. OnBindingRequest(&msg, remote_addr);
  52. break;
  53. default:
  54. SendErrorResponse(msg, remote_addr, 600, "Operation Not Supported");
  55. }
  56. }
  57. void StunServer::OnBindingRequest(
  58. StunMessage* msg, const talk_base::SocketAddress& remote_addr) {
  59. StunMessage response;
  60. response.SetType(STUN_BINDING_RESPONSE);
  61. response.SetTransactionID(msg->transaction_id());
  62. // Tell the user the address that we received their request from.
  63. StunAddressAttribute* mapped_addr;
  64. if (!msg->IsLegacy()) {
  65. mapped_addr = StunAttribute::CreateAddress(STUN_ATTR_MAPPED_ADDRESS);
  66. } else {
  67. mapped_addr = StunAttribute::CreateXorAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
  68. }
  69. mapped_addr->SetAddress(remote_addr);
  70. response.AddAttribute(mapped_addr);
  71. SendResponse(response, remote_addr);
  72. }
  73. void StunServer::SendErrorResponse(
  74. const StunMessage& msg, const talk_base::SocketAddress& addr,
  75. int error_code, const char* error_desc) {
  76. StunMessage err_msg;
  77. err_msg.SetType(GetStunErrorResponseType(msg.type()));
  78. err_msg.SetTransactionID(msg.transaction_id());
  79. StunErrorCodeAttribute* err_code = StunAttribute::CreateErrorCode();
  80. err_code->SetCode(error_code);
  81. err_code->SetReason(error_desc);
  82. err_msg.AddAttribute(err_code);
  83. SendResponse(err_msg, addr);
  84. }
  85. void StunServer::SendResponse(
  86. const StunMessage& msg, const talk_base::SocketAddress& addr) {
  87. talk_base::ByteBuffer buf;
  88. msg.Write(&buf);
  89. if (socket_->SendTo(buf.Data(), buf.Length(), addr) < 0)
  90. LOG_ERR(LS_ERROR) << "sendto";
  91. }
  92. } // namespace cricket