henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 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 | |
| 11 | #ifndef WEBRTC_P2P_BASE_STUNSERVER_H_ |
| 12 | #define WEBRTC_P2P_BASE_STUNSERVER_H_ |
| 13 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 16 | #include "webrtc/p2p/base/stun.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 17 | #include "webrtc/rtc_base/asyncudpsocket.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 18 | |
| 19 | namespace cricket { |
| 20 | |
| 21 | const int STUN_SERVER_PORT = 3478; |
| 22 | |
| 23 | class StunServer : public sigslot::has_slots<> { |
| 24 | public: |
| 25 | // Creates a STUN server, which will listen on the given socket. |
| 26 | explicit StunServer(rtc::AsyncUDPSocket* socket); |
| 27 | // Removes the STUN server from the socket and deletes the socket. |
deadbeef | 8d517c4 | 2017-02-19 14:12:24 -0800 | [diff] [blame] | 28 | ~StunServer() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 29 | |
| 30 | protected: |
| 31 | // Slot for AsyncSocket.PacketRead: |
| 32 | void OnPacket( |
| 33 | rtc::AsyncPacketSocket* socket, const char* buf, size_t size, |
| 34 | const rtc::SocketAddress& remote_addr, |
| 35 | const rtc::PacketTime& packet_time); |
| 36 | |
| 37 | // Handlers for the different types of STUN/TURN requests: |
| 38 | virtual void OnBindingRequest(StunMessage* msg, |
| 39 | const rtc::SocketAddress& addr); |
| 40 | void OnAllocateRequest(StunMessage* msg, |
| 41 | const rtc::SocketAddress& addr); |
| 42 | void OnSharedSecretRequest(StunMessage* msg, |
| 43 | const rtc::SocketAddress& addr); |
| 44 | void OnSendRequest(StunMessage* msg, |
| 45 | const rtc::SocketAddress& addr); |
| 46 | |
| 47 | // Sends an error response to the given message back to the user. |
| 48 | void SendErrorResponse( |
| 49 | const StunMessage& msg, const rtc::SocketAddress& addr, |
| 50 | int error_code, const char* error_desc); |
| 51 | |
| 52 | // Sends the given message to the appropriate destination. |
| 53 | void SendResponse(const StunMessage& msg, |
| 54 | const rtc::SocketAddress& addr); |
| 55 | |
| 56 | // A helper method to compose a STUN binding response. |
| 57 | void GetStunBindReqponse(StunMessage* request, |
| 58 | const rtc::SocketAddress& remote_addr, |
| 59 | StunMessage* response) const; |
| 60 | |
| 61 | private: |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 62 | std::unique_ptr<rtc::AsyncUDPSocket> socket_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | } // namespace cricket |
| 66 | |
| 67 | #endif // WEBRTC_P2P_BASE_STUNSERVER_H_ |