2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 #ifndef SQUID_FORWARD_H
10 #define SQUID_FORWARD_H
12 #include "base/CbcPointer.h"
13 #include "base/forward.h"
14 #include "base/RefCount.h"
15 #include "clients/forward.h"
17 #include "comm/Connection.h"
18 #include "comm/ConnOpener.h"
21 #include "http/StatusCode.h"
22 #include "ip/Address.h"
23 #include "PeerSelectState.h"
24 #include "security/forward.h"
26 #include "ssl/support.h"
32 typedef RefCount
<AccessLogEntry
> AccessLogEntryPointer
;
37 typedef RefCount
<ResolvedPeers
> ResolvedPeersPointer
;
39 class HappyConnOpener
;
40 typedef CbcPointer
<HappyConnOpener
> HappyConnOpenerPointer
;
41 class HappyConnOpenerAnswer
;
47 class CertValidationResponse
;
51 /// Sets initial TOS value and Netfilter for the future outgoing connection.
52 /// Updates the given Connection object, not the future transport connection.
53 void GetMarkingsToServer(HttpRequest
* request
, Comm::Connection
&conn
);
55 /// Recomputes and applies TOS value and Netfilter to the outgoing connection.
56 /// Updates both the given Connection object and the transport connection.
57 void ResetMarkingsToServer(HttpRequest
*, Comm::Connection
&);
61 class FwdState
: public RefCountable
, public PeerSelectionInitiator
63 CBDATA_CHILD(FwdState
);
66 typedef RefCount
<FwdState
> Pointer
;
68 static void initModule();
70 /// Initiates request forwarding to a peer or origin server.
71 static void Start(const Comm::ConnectionPointer
&client
, StoreEntry
*, HttpRequest
*, const AccessLogEntryPointer
&alp
);
72 /// Same as Start() but no master xaction info (AccessLogEntry) available.
73 static void fwdStart(const Comm::ConnectionPointer
&client
, StoreEntry
*, HttpRequest
*);
74 /// time left to finish the whole forwarding process (which started at fwdStart)
75 static time_t ForwardTimeout(const time_t fwdStart
);
76 /// Whether there is still time to re-try after a previous connection failure.
77 /// \param fwdStart The start time of the peer selection/connection process.
78 static bool EnoughTimeToReForward(const time_t fwdStart
);
80 /// This is the real beginning of server connection. Call it whenever
81 /// the forwarding server destination has changed and a new one needs to be opened.
82 /// Produces the cannot-forward error on fail if no better error exists.
83 void useDestinations();
85 void fail(ErrorState
*err
);
86 void unregister(Comm::ConnectionPointer
&conn
);
87 void unregister(int fd
);
89 void handleUnregisteredServerEnd();
91 bool reforwardableStatus(const Http::StatusCode s
) const;
92 void serverClosed(int fd
);
94 void connectDone(const Comm::ConnectionPointer
& conn
, Comm::Flag status
, int xerrno
);
96 bool checkRetriable();
99 void pconnPush(Comm::ConnectionPointer
& conn
, const char *domain
);
101 bool dontRetry() { return flags
.dont_retry
; }
103 void dontRetry(bool val
) { flags
.dont_retry
= val
; }
105 /** return a ConnectionPointer to the current server connection (may or may not be open) */
106 Comm::ConnectionPointer
const & serverConnection() const { return serverConn
; };
109 // hidden for safer management of self; use static fwdStart
110 FwdState(const Comm::ConnectionPointer
&client
, StoreEntry
*, HttpRequest
*, const AccessLogEntryPointer
&alp
);
111 void start(Pointer aSelf
);
112 void stopAndDestroy(const char *reason
);
114 /* PeerSelectionInitiator API */
115 virtual void noteDestination(Comm::ConnectionPointer conn
) override
;
116 virtual void noteDestinationsEnd(ErrorState
*selectionError
) override
;
118 void noteConnection(HappyConnOpenerAnswer
&);
120 #if STRICT_ORIGINAL_DST
121 void selectPeerForIntercepted();
123 static void logReplyStatus(int tries
, const Http::StatusCode status
);
124 void doneWithRetries();
130 /// whether a pinned to-peer connection can be replaced with another one
131 /// (in order to retry or reforward a failed request)
132 bool pinnedCanRetry() const;
134 ErrorState
*makeConnectingError(const err_type type
) const;
135 void connectedToPeer(Security::EncryptorAnswer
&answer
);
136 static void RegisterWithCacheManager(void);
138 void establishTunnelThruProxy();
139 void tunnelEstablishmentDone(Http::TunnelerAnswer
&answer
);
140 void secureConnectionToPeerIfNeeded();
141 void successfullyConnectedToPeer();
143 /// stops monitoring server connection for closure and updates pconn stats
144 void closeServerConnection(const char *reason
);
146 void syncWithServerConn(const Comm::ConnectionPointer
&server
, const char *host
, const bool reused
);
147 void syncHierNote(const Comm::ConnectionPointer
&server
, const char *host
);
149 /// whether we have used up all permitted forwarding attempts
150 bool exhaustedTries() const;
152 /// \returns the time left for this connection to become connected or 1 second if it is less than one second left
153 time_t connectingTimeout(const Comm::ConnectionPointer
&conn
) const;
155 /// whether we are waiting for HappyConnOpener
156 /// same as calls.connector but may differ from connOpener.valid()
157 bool opening() const { return connOpener
.set(); }
159 void cancelOpening(const char *reason
);
161 void notifyConnOpener();
165 HttpRequest
*request
;
166 AccessLogEntryPointer al
; ///< info for the future access.log entry
168 static void abort(void*);
173 Comm::ConnectionPointer clientConn
; ///< a possibly open connection to the client.
175 int n_tries
; ///< the number of forwarding attempts so far
177 // AsyncCalls which we set and may need cancelling.
179 AsyncCall::Pointer connector
; ///< a call linking us to the ConnOpener producing serverConn.
183 bool connected_okay
; ///< TCP link ever opened properly. This affects retry of POST,PUT,CONNECT,etc
185 bool forward_completed
;
186 bool destinationsFound
; ///< at least one candidate path found
189 HappyConnOpenerPointer connOpener
; ///< current connection opening job
190 ResolvedPeersPointer destinations
; ///< paths for forwarding the request
191 Comm::ConnectionPointer serverConn
; ///< a successfully opened connection to a server.
193 AsyncCall::Pointer closeHandler
; ///< The serverConn close handler
195 /// possible pconn race states
196 typedef enum { raceImpossible
, racePossible
, raceHappened
} PconnRace
;
197 PconnRace pconnRace
; ///< current pconn race state
200 void getOutgoingAddress(HttpRequest
* request
, Comm::ConnectionPointer conn
);
202 /// a collection of previously used persistent Squid-to-peer HTTP(S) connections
203 extern PconnPool
*fwdPconnPool
;
205 #endif /* SQUID_FORWARD_H */