stun_server.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  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. #ifndef P2P_BASE_STUN_SERVER_H_
  11. #define P2P_BASE_STUN_SERVER_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <memory>
  15. #include "api/transport/stun.h"
  16. #include "rtc_base/async_packet_socket.h"
  17. #include "rtc_base/async_udp_socket.h"
  18. #include "rtc_base/socket_address.h"
  19. #include "rtc_base/third_party/sigslot/sigslot.h"
  20. namespace cricket {
  21. const int STUN_SERVER_PORT = 3478;
  22. class StunServer : public sigslot::has_slots<> {
  23. public:
  24. // Creates a STUN server, which will listen on the given socket.
  25. explicit StunServer(rtc::AsyncUDPSocket* socket);
  26. // Removes the STUN server from the socket and deletes the socket.
  27. ~StunServer() override;
  28. protected:
  29. // Slot for AsyncSocket.PacketRead:
  30. void 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. // Handlers for the different types of STUN/TURN requests:
  36. virtual void OnBindingRequest(StunMessage* msg,
  37. const rtc::SocketAddress& addr);
  38. void OnAllocateRequest(StunMessage* msg, const rtc::SocketAddress& addr);
  39. void OnSharedSecretRequest(StunMessage* msg, const rtc::SocketAddress& addr);
  40. void OnSendRequest(StunMessage* msg, const rtc::SocketAddress& addr);
  41. // Sends an error response to the given message back to the user.
  42. void SendErrorResponse(const StunMessage& msg,
  43. const rtc::SocketAddress& addr,
  44. int error_code,
  45. const char* error_desc);
  46. // Sends the given message to the appropriate destination.
  47. void SendResponse(const StunMessage& msg, const rtc::SocketAddress& addr);
  48. // A helper method to compose a STUN binding response.
  49. void GetStunBindResponse(StunMessage* request,
  50. const rtc::SocketAddress& remote_addr,
  51. StunMessage* response) const;
  52. private:
  53. std::unique_ptr<rtc::AsyncUDPSocket> socket_;
  54. };
  55. } // namespace cricket
  56. #endif // P2P_BASE_STUN_SERVER_H_