tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ConnectionEntry.h (9924B)


      1 /* vim:set ts=4 sw=2 sts=2 et cin: */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #ifndef ConnectionEntry_h__
      7 #define ConnectionEntry_h__
      8 
      9 #include "PendingTransactionInfo.h"
     10 #include "PendingTransactionQueue.h"
     11 #include "DnsAndConnectSocket.h"
     12 #include "DashboardTypes.h"
     13 #include "mozilla/WeakPtr.h"
     14 
     15 namespace mozilla {
     16 namespace net {
     17 
     18 // ConnectionEntry
     19 //
     20 // nsHttpConnectionMgr::mCT maps connection info hash key to ConnectionEntry
     21 // object, which contains list of active and idle connections as well as the
     22 // list of pending transactions.
     23 class ConnectionEntry : public SupportsWeakPtr {
     24 public:
     25  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ConnectionEntry)
     26  explicit ConnectionEntry(nsHttpConnectionInfo* ci);
     27 
     28  void ReschedTransaction(nsHttpTransaction* aTrans);
     29 
     30  nsTArray<RefPtr<PendingTransactionInfo>>* GetTransactionPendingQHelper(
     31      nsAHttpTransaction* trans);
     32 
     33  void InsertTransactionSorted(
     34      nsTArray<RefPtr<PendingTransactionInfo>>& pendingQ,
     35      PendingTransactionInfo* pendingTransInfo,
     36      bool aInsertAsFirstForTheSamePriority = false);
     37 
     38  void InsertTransaction(PendingTransactionInfo* aPendingTransInfo,
     39                         bool aInsertAsFirstForTheSamePriority = false);
     40 
     41  size_t UrgentStartQueueLength();
     42 
     43  void PrintPendingQ();
     44 
     45  void Compact();
     46 
     47  void CancelAllTransactions(nsresult reason);
     48 
     49  nsresult CloseIdleConnection(nsHttpConnection* conn);
     50  void CloseIdleConnections();
     51  void CloseIdleConnections(uint32_t maxToClose);
     52  void CloseExtendedCONNECTConnections();
     53  void ClosePendingConnections();
     54  nsresult RemoveIdleConnection(nsHttpConnection* conn);
     55  bool IsInIdleConnections(HttpConnectionBase* conn);
     56  size_t IdleConnectionsLength() const { return mIdleConns.Length(); }
     57  void InsertIntoIdleConnections(nsHttpConnection* conn);
     58  already_AddRefed<nsHttpConnection> GetIdleConnection(bool respectUrgency,
     59                                                       bool urgentTrans,
     60                                                       bool* onlyUrgent);
     61 
     62  size_t ActiveConnsLength() const { return mActiveConns.Length(); }
     63  void InsertIntoActiveConns(HttpConnectionBase* conn);
     64  bool IsInActiveConns(HttpConnectionBase* conn);
     65  nsresult RemoveActiveConnection(HttpConnectionBase* conn);
     66  nsresult RemovePendingConnection(HttpConnectionBase* conn);
     67  void MakeAllDontReuseExcept(HttpConnectionBase* conn);
     68  bool FindConnToClaim(PendingTransactionInfo* pendingTransInfo);
     69  void CloseActiveConnections();
     70  void CloseAllActiveConnsWithNullTransactcion(nsresult aCloseCode);
     71 
     72  bool IsInExtendedCONNECTConns(HttpConnectionBase* conn);
     73  void InsertIntoExtendedCONNECTConns(HttpConnectionBase* conn);
     74  void RemoveExtendedCONNECTConns(HttpConnectionBase* conn);
     75 
     76  HttpConnectionBase* GetH2orH3ActiveConn();
     77  // Find an H2 tunnel connection (nsHttpConnection with UsingSpdy()) in active
     78  // connections. This is used for WebSocket/WebTransport through H3 proxy.
     79  already_AddRefed<nsHttpConnection> GetH2TunnelActiveConn();
     80  // Make an active spdy connection DontReuse.
     81  // TODO: this is a helper function and should nbe improved.
     82  bool MakeFirstActiveSpdyConnDontReuse();
     83 
     84  void ClosePersistentConnections();
     85 
     86  uint32_t PruneDeadConnections();
     87  void MakeConnectionPendingAndDontReuse(HttpConnectionBase* conn);
     88  void VerifyTraffic();
     89  void PruneNoTraffic();
     90  uint32_t TimeoutTick();
     91 
     92  void MoveConnection(HttpConnectionBase* proxyConn, ConnectionEntry* otherEnt);
     93 
     94  size_t DnsAndConnectSocketsLength() const {
     95    return mDnsAndConnectSockets.Length();
     96  }
     97 
     98  void InsertIntoDnsAndConnectSockets(DnsAndConnectSocket* sock);
     99  void RemoveDnsAndConnectSocket(DnsAndConnectSocket* dnsAndSock, bool abandon);
    100  void CloseAllDnsAndConnectSockets();
    101 
    102  HttpRetParams GetConnectionData();
    103  Http3ConnectionStatsParams GetHttp3ConnectionStatsData();
    104  void LogConnections();
    105 
    106  const RefPtr<nsHttpConnectionInfo> mConnInfo;
    107 
    108  bool AvailableForDispatchNow();
    109 
    110  // calculate the number of half open sockets that have not had at least 1
    111  // connection complete
    112  uint32_t UnconnectedDnsAndConnectSockets() const;
    113 
    114  // Remove a particular DnsAndConnectSocket from the mDnsAndConnectSocket array
    115  bool RemoveDnsAndConnectSocket(DnsAndConnectSocket*);
    116 
    117  bool MaybeProcessCoalescingKeys(nsIDNSAddrRecord* dnsRecord,
    118                                  bool aIsHttp3 = false);
    119 
    120  nsresult CreateDnsAndConnectSocket(nsAHttpTransaction* trans, uint32_t caps,
    121                                     bool speculative, bool urgentStart,
    122                                     bool allow1918,
    123                                     PendingTransactionInfo* pendingTransInfo);
    124 
    125  // Spdy sometimes resolves the address in the socket manager in order
    126  // to re-coalesce sharded HTTP hosts. The dotted decimal address is
    127  // combined with the Anonymous flag and OA from the connection information
    128  // to build the hash key for hosts in the same ip pool.
    129  //
    130 
    131  nsTArray<nsCString> mCoalescingKeys;
    132 
    133  // This is a list of addresses matching the coalescing keys.
    134  // This is necessary to check if the origin's DNS entries
    135  // contain the IP address of the active connection.
    136  nsTArray<NetAddr> mAddresses;
    137 
    138  // To have the UsingSpdy flag means some host with the same connection
    139  // entry has done NPN=spdy/* at some point. It does not mean every
    140  // connection is currently using spdy.
    141  bool mUsingSpdy : 1;
    142 
    143  // Determines whether or not we can continue to use spdy-enabled
    144  // connections in the future. This is generally set to false either when
    145  // some connection here encounters connection-based auth (such as NTLM)
    146  // or when some connection here encounters a server that is totally
    147  // busted (such as it fails to properly perform the h2 handshake).
    148  bool mCanUseSpdy : 1;
    149 
    150  // Flags to remember our happy-eyeballs decision.
    151  // Reset only by Ctrl-F5 reload.
    152  // True when we've first connected an IPv4 server for this host,
    153  // initially false.
    154  bool mPreferIPv4 : 1;
    155  // True when we've first connected an IPv6 server for this host,
    156  // initially false.
    157  bool mPreferIPv6 : 1;
    158 
    159  // True if this connection entry has initiated a socket
    160  bool mUsedForConnection : 1;
    161 
    162  bool mDoNotDestroy : 1;
    163 
    164  bool IsHttp3() const { return mConnInfo->IsHttp3(); }
    165  bool IsHttp3ProxyConnection() const {
    166    return mConnInfo->IsHttp3ProxyConnection();
    167  }
    168  bool AllowHttp2() const { return mCanUseSpdy; }
    169  void DisallowHttp2();
    170  void DontReuseHttp3Conn();
    171 
    172  // Set the IP family preference flags according the connected family
    173  void RecordIPFamilyPreference(uint16_t family);
    174  // Resets all flags to their default values
    175  void ResetIPFamilyPreference();
    176  // True iff there is currently an established IP family preference
    177  bool PreferenceKnown() const;
    178 
    179  // Return the count of pending transactions for all window ids.
    180  size_t PendingQueueLength() const;
    181  size_t PendingQueueLengthForWindow(uint64_t windowId) const;
    182 
    183  void AppendPendingUrgentStartQ(
    184      nsTArray<RefPtr<PendingTransactionInfo>>& result);
    185 
    186  // Append transactions to the |result| whose window id
    187  // is equal to |windowId|.
    188  // NOTE: maxCount == 0 will get all transactions in the queue.
    189  void AppendPendingQForFocusedWindow(
    190      uint64_t windowId, nsTArray<RefPtr<PendingTransactionInfo>>& result,
    191      uint32_t maxCount = 0);
    192 
    193  // Append transactions whose window id isn't equal to |windowId|.
    194  // NOTE: windowId == 0 will get all transactions for both
    195  // focused and non-focused windows.
    196  void AppendPendingQForNonFocusedWindows(
    197      uint64_t windowId, nsTArray<RefPtr<PendingTransactionInfo>>& result,
    198      uint32_t maxCount = 0);
    199 
    200  // Remove the empty pendingQ in |mPendingTransactionTable|.
    201  void RemoveEmptyPendingQ();
    202 
    203  void PrintDiagnostics(nsCString& log, uint32_t aMaxPersistConns);
    204 
    205  bool RestrictConnections();
    206 
    207  // Return total active connection count, which is the sum of
    208  // active connections and unconnected half open connections.
    209  uint32_t TotalActiveConnections() const;
    210 
    211  bool RemoveTransFromPendingQ(nsHttpTransaction* aTrans);
    212 
    213  void MaybeUpdateEchConfig(nsHttpConnectionInfo* aConnInfo);
    214 
    215  bool AllowToRetryDifferentIPFamilyForHttp3(nsresult aError);
    216  void SetRetryDifferentIPFamilyForHttp3(uint16_t aIPFamily);
    217 
    218  void SetServerCertHashes(nsTArray<RefPtr<nsIWebTransportHash>>&& aHashes);
    219 
    220  const nsTArray<RefPtr<nsIWebTransportHash>>& GetServerCertHashes();
    221 
    222  const nsCString& OriginFrameHashKey();
    223 
    224 private:
    225  void InsertIntoIdleConnections_internal(nsHttpConnection* conn);
    226  void RemoveFromIdleConnectionsIndex(size_t inx);
    227  bool RemoveFromIdleConnections(nsHttpConnection* conn);
    228 
    229  nsTArray<RefPtr<nsHttpConnection>> mIdleConns;  // idle persistent connections
    230  nsTArray<RefPtr<HttpConnectionBase>> mActiveConns;  // active connections
    231  // When a connection is added to this mPendingConns list, it is primarily
    232  // to keep the connection alive and to continue serving its ongoing
    233  // transaction. While in this list, the connection will not be available to
    234  // serve any new transactions and will remain here until its current
    235  // transaction is complete.
    236  nsTArray<RefPtr<HttpConnectionBase>> mPendingConns;
    237  // Tunneled connections used for extended CONNECT that needs to be cleaned up
    238  // on shutdown
    239  nsTArray<RefPtr<HttpConnectionBase>> mExtendedCONNECTConns;
    240 
    241  nsTArray<RefPtr<DnsAndConnectSocket>>
    242      mDnsAndConnectSockets;  // dns resolution and half open connections
    243 
    244  // If serverCertificateHashes are used, these are stored here
    245  nsTArray<RefPtr<nsIWebTransportHash>> mServerCertHashes;
    246 
    247  PendingTransactionQueue mPendingQ;
    248  ~ConnectionEntry();
    249 
    250  nsCString mOriginFrameHashKey;
    251 
    252  bool mRetriedDifferentIPFamilyForHttp3 = false;
    253 };
    254 
    255 }  // namespace net
    256 }  // namespace mozilla
    257 
    258 #endif  // !ConnectionEntry_h__