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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef P2P_BASE_STUN_SERVER_H_ |
| 12 | #define P2P_BASE_STUN_SERVER_H_ |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 16 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 17 | #include <memory> |
| 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "p2p/base/stun.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "rtc_base/async_packet_socket.h" |
| 21 | #include "rtc_base/async_udp_socket.h" |
| 22 | #include "rtc_base/socket_address.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 23 | #include "rtc_base/third_party/sigslot/sigslot.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 24 | |
| 25 | namespace cricket { |
| 26 | |
| 27 | const int STUN_SERVER_PORT = 3478; |
| 28 | |
| 29 | class StunServer : public sigslot::has_slots<> { |
| 30 | public: |
| 31 | // Creates a STUN server, which will listen on the given socket. |
| 32 | explicit StunServer(rtc::AsyncUDPSocket* socket); |
| 33 | // Removes the STUN server from the socket and deletes the socket. |
deadbeef | 8d517c4 | 2017-02-19 14:12:24 -0800 | [diff] [blame] | 34 | ~StunServer() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 35 | |
| 36 | protected: |
| 37 | // Slot for AsyncSocket.PacketRead: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 38 | void OnPacket(rtc::AsyncPacketSocket* socket, |
| 39 | const char* buf, |
| 40 | size_t size, |
| 41 | const rtc::SocketAddress& remote_addr, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 42 | const int64_t& packet_time_us); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 43 | |
| 44 | // Handlers for the different types of STUN/TURN requests: |
| 45 | virtual void OnBindingRequest(StunMessage* msg, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 46 | const rtc::SocketAddress& addr); |
| 47 | void OnAllocateRequest(StunMessage* msg, const rtc::SocketAddress& addr); |
| 48 | void OnSharedSecretRequest(StunMessage* msg, const rtc::SocketAddress& addr); |
| 49 | void OnSendRequest(StunMessage* msg, const rtc::SocketAddress& addr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 50 | |
| 51 | // Sends an error response to the given message back to the user. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 52 | void SendErrorResponse(const StunMessage& msg, |
| 53 | const rtc::SocketAddress& addr, |
| 54 | int error_code, |
| 55 | const char* error_desc); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 56 | |
| 57 | // Sends the given message to the appropriate destination. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 58 | void SendResponse(const StunMessage& msg, const rtc::SocketAddress& addr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 59 | |
| 60 | // A helper method to compose a STUN binding response. |
Min Wang | 1e00dbc | 2019-06-26 13:08:29 -0500 | [diff] [blame] | 61 | void GetStunBindResponse(StunMessage* request, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 62 | const rtc::SocketAddress& remote_addr, |
| 63 | StunMessage* response) const; |
| 64 | |
| 65 | private: |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 66 | std::unique_ptr<rtc::AsyncUDPSocket> socket_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | } // namespace cricket |
| 70 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 71 | #endif // P2P_BASE_STUN_SERVER_H_ |