blob: 8cfba4d8281274fa7e736a73f209005eeff0e89d [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
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 Anton10542f22019-01-11 09:11:00 -080011#ifndef P2P_BASE_STUN_SERVER_H_
12#define P2P_BASE_STUN_SERVER_H_
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
kwiberg3ec46792016-04-27 07:22:53 -070017#include <memory>
18
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "p2p/base/stun.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/async_packet_socket.h"
21#include "rtc_base/async_udp_socket.h"
22#include "rtc_base/socket_address.h"
Yves Gerey3e707812018-11-28 16:47:49 +010023#include "rtc_base/third_party/sigslot/sigslot.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000024
25namespace cricket {
26
27const int STUN_SERVER_PORT = 3478;
28
29class 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.
deadbeef8d517c42017-02-19 14:12:24 -080034 ~StunServer() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000035
36 protected:
37 // Slot for AsyncSocket.PacketRead:
Yves Gerey665174f2018-06-19 15:03:05 +020038 void OnPacket(rtc::AsyncPacketSocket* socket,
39 const char* buf,
40 size_t size,
41 const rtc::SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +010042 const int64_t& packet_time_us);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000043
44 // Handlers for the different types of STUN/TURN requests:
45 virtual void OnBindingRequest(StunMessage* msg,
Yves Gerey665174f2018-06-19 15:03:05 +020046 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.org269fb4b2014-10-28 22:20:11 +000050
51 // Sends an error response to the given message back to the user.
Yves Gerey665174f2018-06-19 15:03:05 +020052 void SendErrorResponse(const StunMessage& msg,
53 const rtc::SocketAddress& addr,
54 int error_code,
55 const char* error_desc);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000056
57 // Sends the given message to the appropriate destination.
Yves Gerey665174f2018-06-19 15:03:05 +020058 void SendResponse(const StunMessage& msg, const rtc::SocketAddress& addr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000059
60 // A helper method to compose a STUN binding response.
Min Wang1e00dbc2019-06-26 13:08:29 -050061 void GetStunBindResponse(StunMessage* request,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000062 const rtc::SocketAddress& remote_addr,
63 StunMessage* response) const;
64
65 private:
kwiberg3ec46792016-04-27 07:22:53 -070066 std::unique_ptr<rtc::AsyncUDPSocket> socket_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000067};
68
69} // namespace cricket
70
Steve Anton10542f22019-01-11 09:11:00 -080071#endif // P2P_BASE_STUN_SERVER_H_