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 "content/common/content_export.h"
      9 #include "content/public/common/p2p_socket_type.h"
     10 #include "net/base/ip_endpoint.h"
     11 #include "net/udp/datagram_socket.h"
     12 
     13 namespace IPC {
     14 class Sender;
     15 }
     16 
     17 namespace net {
     18 class URLRequestContextGetter;
     19 }
     20 
     21 namespace content {
     22 class P2PMessageThrottler;
     23 
     24 // Base class for P2P sockets.
     25 class CONTENT_EXPORT P2PSocketHost {
     26  public:
     27   static const int kStunHeaderSize = 20;
     28   // Creates P2PSocketHost of the specific type.
     29   static P2PSocketHost* Create(IPC::Sender* message_sender,
     30                                int id, P2PSocketType type,
     31                                net::URLRequestContextGetter* url_context,
     32                                P2PMessageThrottler* throttler);
     33 
     34   virtual ~P2PSocketHost();
     35 
     36   // Initalizes the socket. Returns false when initiazations fails.
     37   virtual bool Init(const net::IPEndPoint& local_address,
     38                     const net::IPEndPoint& remote_address) = 0;
     39 
     40   // Sends |data| on the socket to |to|.
     41   virtual void Send(const net::IPEndPoint& to,
     42                     const std::vector<char>& data,
     43                     net::DiffServCodePoint dscp,
     44                     uint64 packet_id) = 0;
     45 
     46   virtual P2PSocketHost* AcceptIncomingTcpConnection(
     47       const net::IPEndPoint& remote_address, int id) = 0;
     48 
     49  protected:
     50   friend class P2PSocketHostTcpTestBase;
     51 
     52   // TODO(mallinath) - Remove this below enum and use one defined in
     53   // libjingle/souce/talk/p2p/base/stun.h
     54   enum StunMessageType {
     55     STUN_BINDING_REQUEST = 0x0001,
     56     STUN_BINDING_RESPONSE = 0x0101,
     57     STUN_BINDING_ERROR_RESPONSE = 0x0111,
     58     STUN_SHARED_SECRET_REQUEST = 0x0002,
     59     STUN_SHARED_SECRET_RESPONSE = 0x0102,
     60     STUN_SHARED_SECRET_ERROR_RESPONSE = 0x0112,
     61     STUN_ALLOCATE_REQUEST = 0x0003,
     62     STUN_ALLOCATE_RESPONSE = 0x0103,
     63     STUN_ALLOCATE_ERROR_RESPONSE = 0x0113,
     64     STUN_SEND_REQUEST = 0x0004,
     65     STUN_SEND_RESPONSE = 0x0104,
     66     STUN_SEND_ERROR_RESPONSE = 0x0114,
     67     STUN_DATA_INDICATION = 0x0115,
     68     TURN_SEND_INDICATION = 0x0016,
     69     TURN_DATA_INDICATION = 0x0017,
     70     TURN_CREATE_PERMISSION_REQUEST = 0x0008,
     71     TURN_CREATE_PERMISSION_RESPONSE = 0x0108,
     72     TURN_CREATE_PERMISSION_ERROR_RESPONSE = 0x0118,
     73     TURN_CHANNEL_BIND_REQUEST = 0x0009,
     74     TURN_CHANNEL_BIND_RESPONSE = 0x0109,
     75     TURN_CHANNEL_BIND_ERROR_RESPONSE = 0x0119,
     76   };
     77 
     78   enum State {
     79     STATE_UNINITIALIZED,
     80     STATE_CONNECTING,
     81     STATE_TLS_CONNECTING,
     82     STATE_OPEN,
     83     STATE_ERROR,
     84   };
     85 
     86   P2PSocketHost(IPC::Sender* message_sender, int id);
     87 
     88   // Verifies that the packet |data| has a valid STUN header. In case
     89   // of success stores type of the message in |type|.
     90   static bool GetStunPacketType(const char* data, int data_size,
     91                                 StunMessageType* type);
     92   static bool IsRequestOrResponse(StunMessageType type);
     93 
     94   IPC::Sender* message_sender_;
     95   int id_;
     96   State state_;
     97 
     98   DISALLOW_COPY_AND_ASSIGN(P2PSocketHost);
     99 };
    100 
    101 }  // namespace content
    102 
    103 #endif  // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_H_
    104