Home | History | Annotate | Download | only in p2p
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_H_
      6 #define CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_H_
      7 
      8 #include "base/memory/weak_ptr.h"
      9 #include "content/common/content_export.h"
     10 #include "content/common/p2p_socket_type.h"
     11 #include "content/public/browser/render_process_host.h"
     12 #include "net/base/ip_endpoint.h"
     13 #include "net/udp/datagram_socket.h"
     14 
     15 namespace IPC {
     16 class Sender;
     17 }
     18 
     19 namespace net {
     20 class URLRequestContextGetter;
     21 }
     22 
     23 namespace rtc {
     24 struct PacketOptions;
     25 }
     26 
     27 namespace content {
     28 class P2PMessageThrottler;
     29 
     30 namespace packet_processing_helpers {
     31 
     32 // This method can handle only RTP packet, otherwise this method must not be
     33 // called. It will try to do, 1. update absolute send time extension header
     34 // if present with current time and 2. update HMAC in RTP packet.
     35 // If abs_send_time is 0, ApplyPacketOption will get current time from system.
     36 CONTENT_EXPORT bool ApplyPacketOptions(char* data,
     37                                        size_t length,
     38                                        const rtc::PacketOptions& options,
     39                                        uint32 abs_send_time);
     40 
     41 // Helper method which finds RTP ofset and length if the packet is encapsulated
     42 // in a TURN Channel Message or TURN Send Indication message.
     43 CONTENT_EXPORT bool GetRtpPacketStartPositionAndLength(
     44     const char* data,
     45     size_t length,
     46     size_t* rtp_start_pos,
     47     size_t* rtp_packet_length);
     48 
     49 // Helper method which updates absoulute send time extension if present.
     50 CONTENT_EXPORT bool UpdateRtpAbsSendTimeExtension(char* rtp,
     51                                                   size_t length,
     52                                                   int extension_id,
     53                                                   uint32 abs_send_time);
     54 
     55 }  // packet_processing_helpers
     56 
     57 // Base class for P2P sockets.
     58 class CONTENT_EXPORT P2PSocketHost {
     59  public:
     60   static const int kStunHeaderSize = 20;
     61   // Creates P2PSocketHost of the specific type.
     62   static P2PSocketHost* Create(IPC::Sender* message_sender,
     63                                int socket_id,
     64                                P2PSocketType type,
     65                                net::URLRequestContextGetter* url_context,
     66                                P2PMessageThrottler* throttler);
     67 
     68   virtual ~P2PSocketHost();
     69 
     70   // Initalizes the socket. Returns false when initiazations fails.
     71   virtual bool Init(const net::IPEndPoint& local_address,
     72                     const P2PHostAndIPEndPoint& remote_address) = 0;
     73 
     74   // Sends |data| on the socket to |to|.
     75   virtual void Send(const net::IPEndPoint& to,
     76                     const std::vector<char>& data,
     77                     const rtc::PacketOptions& options,
     78                     uint64 packet_id) = 0;
     79 
     80   virtual P2PSocketHost* AcceptIncomingTcpConnection(
     81       const net::IPEndPoint& remote_address, int id) = 0;
     82 
     83   virtual bool SetOption(P2PSocketOption option, int value) = 0;
     84 
     85   void StartRtpDump(
     86       bool incoming,
     87       bool outgoing,
     88       const RenderProcessHost::WebRtcRtpPacketCallback& packet_callback);
     89   void StopRtpDump(bool incoming, bool outgoing);
     90 
     91  protected:
     92   friend class P2PSocketHostTcpTestBase;
     93 
     94   // This should match suffix IPProtocolType defined in histograms.xml.
     95   enum ProtocolType { UDP = 0x1, TCP = 0x2 };
     96 
     97   // TODO(mallinath) - Remove this below enum and use one defined in
     98   // libjingle/souce/talk/p2p/base/stun.h
     99   enum StunMessageType {
    100     STUN_BINDING_REQUEST = 0x0001,
    101     STUN_BINDING_RESPONSE = 0x0101,
    102     STUN_BINDING_ERROR_RESPONSE = 0x0111,
    103     STUN_SHARED_SECRET_REQUEST = 0x0002,
    104     STUN_SHARED_SECRET_RESPONSE = 0x0102,
    105     STUN_SHARED_SECRET_ERROR_RESPONSE = 0x0112,
    106     STUN_ALLOCATE_REQUEST = 0x0003,
    107     STUN_ALLOCATE_RESPONSE = 0x0103,
    108     STUN_ALLOCATE_ERROR_RESPONSE = 0x0113,
    109     STUN_SEND_REQUEST = 0x0004,
    110     STUN_SEND_RESPONSE = 0x0104,
    111     STUN_SEND_ERROR_RESPONSE = 0x0114,
    112     STUN_DATA_INDICATION = 0x0115,
    113     TURN_SEND_INDICATION = 0x0016,
    114     TURN_DATA_INDICATION = 0x0017,
    115     TURN_CREATE_PERMISSION_REQUEST = 0x0008,
    116     TURN_CREATE_PERMISSION_RESPONSE = 0x0108,
    117     TURN_CREATE_PERMISSION_ERROR_RESPONSE = 0x0118,
    118     TURN_CHANNEL_BIND_REQUEST = 0x0009,
    119     TURN_CHANNEL_BIND_RESPONSE = 0x0109,
    120     TURN_CHANNEL_BIND_ERROR_RESPONSE = 0x0119,
    121   };
    122 
    123   enum State {
    124     STATE_UNINITIALIZED,
    125     STATE_CONNECTING,
    126     STATE_TLS_CONNECTING,
    127     STATE_OPEN,
    128     STATE_ERROR,
    129   };
    130 
    131   P2PSocketHost(IPC::Sender* message_sender,
    132                 int socket_id,
    133                 ProtocolType protocol_type);
    134 
    135   // Verifies that the packet |data| has a valid STUN header. In case
    136   // of success stores type of the message in |type|.
    137   static bool GetStunPacketType(const char* data, int data_size,
    138                                 StunMessageType* type);
    139   static bool IsRequestOrResponse(StunMessageType type);
    140 
    141   // Calls |packet_dump_callback_| to record the RTP header.
    142   void DumpRtpPacket(const char* packet, size_t length, bool incoming);
    143 
    144   // A helper to dump the packet on the IO thread.
    145   void DumpRtpPacketOnIOThread(scoped_ptr<uint8[]> packet_header,
    146                                size_t header_length,
    147                                size_t packet_length,
    148                                bool incoming);
    149 
    150   // Used by subclasses to track the metrics of delayed bytes and packets.
    151   void IncrementDelayedPackets();
    152   void IncrementTotalSentPackets();
    153   void IncrementDelayedBytes(uint32 size);
    154   void DecrementDelayedBytes(uint32 size);
    155 
    156   IPC::Sender* message_sender_;
    157   int id_;
    158   State state_;
    159   bool dump_incoming_rtp_packet_;
    160   bool dump_outgoing_rtp_packet_;
    161   RenderProcessHost::WebRtcRtpPacketCallback packet_dump_callback_;
    162 
    163   base::WeakPtrFactory<P2PSocketHost> weak_ptr_factory_;
    164 
    165   ProtocolType protocol_type_;
    166 
    167  private:
    168   // Track total delayed packets for calculating how many packets are
    169   // delayed by system at the end of call.
    170   uint32 send_packets_delayed_total_;
    171   uint32 send_packets_total_;
    172 
    173   // Track the maximum of consecutive delayed bytes caused by system's
    174   // EWOULDBLOCK.
    175   int32 send_bytes_delayed_max_;
    176   int32 send_bytes_delayed_cur_;
    177 
    178   DISALLOW_COPY_AND_ASSIGN(P2PSocketHost);
    179 };
    180 
    181 }  // namespace content
    182 
    183 #endif  // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_H_
    184