| 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 | #ifndef P2P_BASE_STUN_SERVER_H_ |
| 12 | #define P2P_BASE_STUN_SERVER_H_ |
| 13 |
| 14 | #include <stddef.h> |
| 15 |
| 16 | #include <memory> |
| 17 |
| 18 | #include "absl/strings/string_view.h" |
| 19 | #include "api/transport/stun.h" |
| 20 | #include "rtc_base/async_packet_socket.h" |
| 21 | #include "rtc_base/async_udp_socket.h" |
| 22 | #include "rtc_base/socket_address.h" |
| 23 |
| 24 | namespace cricket { |
| 25 |
| 26 | const int STUN_SERVER_PORT = 3478; |
| 27 |
| 28 | class StunServer { |
| 29 | public: |
| 30 | // Creates a STUN server, which will listen on the given socket. |
| 31 | explicit StunServer(rtc::AsyncUDPSocket* socket); |
| 32 | // Removes the STUN server from the socket and deletes the socket. |
| 33 | virtual ~StunServer(); |
| 34 |
| 35 | protected: |
| 36 | // Callback for packets from socket. |
| 37 | void OnPacket(rtc::AsyncPacketSocket* socket, |
| 38 | const rtc::ReceivedPacket& packet); |
| 39 |
| 40 | // Handlers for the different types of STUN/TURN requests: |
| 41 | virtual void OnBindingRequest(StunMessage* msg, |
| 42 | const rtc::SocketAddress& addr); |
| 43 | void OnAllocateRequest(StunMessage* msg, const rtc::SocketAddress& addr); |
| 44 | void OnSharedSecretRequest(StunMessage* msg, const rtc::SocketAddress& addr); |
| 45 | void OnSendRequest(StunMessage* msg, const rtc::SocketAddress& addr); |
| 46 |
| 47 | // Sends an error response to the given message back to the user. |
| 48 | void SendErrorResponse(const StunMessage& msg, |
| 49 | const rtc::SocketAddress& addr, |
| 50 | int error_code, |
| 51 | absl::string_view error_desc); |
| 52 |
| 53 | // Sends the given message to the appropriate destination. |
| 54 | void SendResponse(const StunMessage& msg, const rtc::SocketAddress& addr); |
| 55 |
| 56 | // A helper method to compose a STUN binding response. |
| 57 | void GetStunBindResponse(StunMessage* message, |
| 58 | const rtc::SocketAddress& remote_addr, |
| 59 | StunMessage* response) const; |
| 60 |
| 61 | private: |
| 62 | webrtc::SequenceChecker sequence_checker_; |
| 63 | std::unique_ptr<rtc::AsyncUDPSocket> socket_; |
| 64 | }; |
| 65 |
| 66 | } // namespace cricket |
| 67 |
| 68 | #endif // P2P_BASE_STUN_SERVER_H_ |
| 69 |