1 /*
2  *  Copyright 2013 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 P2P_BASE_ASYNC_STUN_TCP_SOCKET_H_
12 #define P2P_BASE_ASYNC_STUN_TCP_SOCKET_H_
13 
14 #include <stddef.h>
15 
16 #include "rtc_base/async_packet_socket.h"
17 #include "rtc_base/async_socket.h"
18 #include "rtc_base/async_tcp_socket.h"
19 #include "rtc_base/constructor_magic.h"
20 #include "rtc_base/socket_address.h"
21 
22 namespace cricket {
23 
24 class AsyncStunTCPSocket : public rtc::AsyncTCPSocketBase {
25  public:
26   // Binds and connects |socket| and creates AsyncTCPSocket for
27   // it. Takes ownership of |socket|. Returns NULL if bind() or
28   // connect() fail (|socket| is destroyed in that case).
29   static AsyncStunTCPSocket* Create(rtc::AsyncSocket* socket,
30                                     const rtc::SocketAddress& bind_address,
31                                     const rtc::SocketAddress& remote_address);
32 
33   AsyncStunTCPSocket(rtc::AsyncSocket* socket, bool listen);
34 
35   int Send(const void* pv,
36            size_t cb,
37            const rtc::PacketOptions& options) override;
38   void ProcessInput(char* data, size_t* len) override;
39   void HandleIncomingConnection(rtc::AsyncSocket* socket) override;
40 
41  private:
42   // This method returns the message hdr + length written in the header.
43   // This method also returns the number of padding bytes needed/added to the
44   // turn message. |pad_bytes| should be used only when |is_turn| is true.
45   size_t GetExpectedLength(const void* data, size_t len, int* pad_bytes);
46 
47   RTC_DISALLOW_COPY_AND_ASSIGN(AsyncStunTCPSocket);
48 };
49 
50 }  // namespace cricket
51 
52 #endif  // P2P_BASE_ASYNC_STUN_TCP_SOCKET_H_
53