blob: 92707b0954cb6cd2c83c745cb79e09eb8ff98d12 [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
11#ifndef WEBRTC_P2P_BASE_STUNSERVER_H_
12#define WEBRTC_P2P_BASE_STUNSERVER_H_
13
kwiberg3ec46792016-04-27 07:22:53 -070014#include <memory>
15
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000016#include "webrtc/p2p/base/stun.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020017#include "webrtc/rtc_base/asyncudpsocket.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000018
19namespace cricket {
20
21const int STUN_SERVER_PORT = 3478;
22
23class 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.
deadbeef8d517c42017-02-19 14:12:24 -080028 ~StunServer() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000029
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:
kwiberg3ec46792016-04-27 07:22:53 -070062 std::unique_ptr<rtc::AsyncUDPSocket> socket_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000063};
64
65} // namespace cricket
66
67#endif // WEBRTC_P2P_BASE_STUNSERVER_H_