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