]> git.ipfire.org Git - thirdparty/squid.git/blame - src/FwdState.h
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / FwdState.h
CommitLineData
bbc27441 1/*
f70aedc4 2 * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
bbc27441
AJ
3 *
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.
7 */
8
2087dca6 9#ifndef SQUID_FORWARD_H
10#define SQUID_FORWARD_H
3cd1350a 11
55622953 12#include "base/CbcPointer.h"
55622953 13#include "base/forward.h"
a70e75b7 14#include "base/RefCount.h"
f5e17947 15#include "clients/forward.h"
582c2af2
FC
16#include "comm.h"
17#include "comm/Connection.h"
55622953 18#include "comm/ConnOpener.h"
83b053a0 19#include "error/forward.h"
582c2af2 20#include "fde.h"
955394ce 21#include "http/StatusCode.h"
582c2af2 22#include "ip/Address.h"
6043e368 23#include "PeerSelectState.h"
9b7992d9 24#include "ResolvedPeers.h"
fcfdf7f9 25#include "security/forward.h"
cb4f4424 26#if USE_OPENSSL
268a2e8f
CT
27#include "ssl/support.h"
28#endif
582c2af2 29
62ee09ca 30/* forward decls */
31
4bf68cfa
AR
32class AccessLogEntry;
33typedef RefCount<AccessLogEntry> AccessLogEntryPointer;
aa839030 34class ErrorState;
06093389 35class HttpRequest;
55622953
CT
36class PconnPool;
37class ResolvedPeers;
38typedef RefCount<ResolvedPeers> ResolvedPeersPointer;
39
40class HappyConnOpener;
41typedef CbcPointer<HappyConnOpener> HappyConnOpenerPointer;
42class HappyConnOpenerAnswer;
62ee09ca 43
6ee88490 44/// Sets initial TOS value and Netfilter for the future outgoing connection.
55622953 45/// Updates the given Connection object, not the future transport connection.
6ee88490
CT
46void GetMarkingsToServer(HttpRequest * request, Comm::Connection &conn);
47
55622953
CT
48/// Recomputes and applies TOS value and Netfilter to the outgoing connection.
49/// Updates both the given Connection object and the transport connection.
50void ResetMarkingsToServer(HttpRequest *, Comm::Connection &);
51
4a77bb4e
CT
52class HelperReply;
53
6043e368 54class FwdState: public RefCountable, public PeerSelectionInitiator
3cd1350a 55{
6043e368 56 CBDATA_CHILD(FwdState);
5c2f68b7 57
3cd1350a 58public:
59 typedef RefCount<FwdState> Pointer;
6043e368 60 virtual ~FwdState();
3cd1350a 61 static void initModule();
62
4bf68cfa
AR
63 /// Initiates request forwarding to a peer or origin server.
64 static void Start(const Comm::ConnectionPointer &client, StoreEntry *, HttpRequest *, const AccessLogEntryPointer &alp);
65 /// Same as Start() but no master xaction info (AccessLogEntry) available.
be364179 66 static void fwdStart(const Comm::ConnectionPointer &client, StoreEntry *, HttpRequest *);
0ce8e93b
EB
67 /// time left to finish the whole forwarding process (which started at fwdStart)
68 static time_t ForwardTimeout(const time_t fwdStart);
69 /// Whether there is still time to re-try after a previous connection failure.
70 /// \param fwdStart The start time of the peer selection/connection process.
71 static bool EnoughTimeToReForward(const time_t fwdStart);
8652f8e7
AJ
72
73 /// This is the real beginning of server connection. Call it whenever
74 /// the forwarding server destination has changed and a new one needs to be opened.
75 /// Produces the cannot-forward error on fail if no better error exists.
3dde9e52 76 void useDestinations();
8652f8e7 77
3cd1350a 78 void fail(ErrorState *err);
00ae51e4 79 void unregister(Comm::ConnectionPointer &conn);
3cd1350a 80 void unregister(int fd);
81 void complete();
3e8c047e 82 void handleUnregisteredServerEnd();
3cd1350a 83 int reforward();
955394ce 84 bool reforwardableStatus(const Http::StatusCode s) const;
3cd1350a 85 void serverClosed(int fd);
86 void connectStart();
c8407295 87 void connectDone(const Comm::ConnectionPointer & conn, Comm::Flag status, int xerrno);
3cd1350a 88 bool checkRetry();
89 bool checkRetriable();
90 void dispatch();
55622953 91
642a305c 92 void pconnPush(Comm::ConnectionPointer & conn, const char *domain);
3cd1350a 93
94 bool dontRetry() { return flags.dont_retry; }
95
96 void dontRetry(bool val) { flags.dont_retry = val; }
97
25b0ce45
CT
98 /// get rid of a to-server connection that failed to become serverConn
99 void closePendingConnection(const Comm::ConnectionPointer &conn, const char *reason);
100
5229395c 101 /** return a ConnectionPointer to the current server connection (may or may not be open) */
00ae51e4 102 Comm::ConnectionPointer const & serverConnection() const { return serverConn; };
5229395c 103
3cd1350a 104private:
7a0fb323 105 // hidden for safer management of self; use static fwdStart
4bf68cfa 106 FwdState(const Comm::ConnectionPointer &client, StoreEntry *, HttpRequest *, const AccessLogEntryPointer &alp);
7a0fb323 107 void start(Pointer aSelf);
6043e368
AR
108 void stopAndDestroy(const char *reason);
109
110 /* PeerSelectionInitiator API */
111 virtual void noteDestination(Comm::ConnectionPointer conn) override;
112 virtual void noteDestinationsEnd(ErrorState *selectionError) override;
7a0fb323 113
55622953
CT
114 void noteConnection(HappyConnOpenerAnswer &);
115
32c32865 116#if STRICT_ORIGINAL_DST
7c75511b 117 void selectPeerForIntercepted();
32c32865 118#endif
955394ce 119 static void logReplyStatus(int tries, const Http::StatusCode status);
9d2760b6 120 void doneWithRetries();
802a8c1d 121 void completed();
3e8c047e 122 void retryOrBail();
3dde9e52
CT
123
124 void usePinned();
125
126 /// whether a pinned to-peer connection can be replaced with another one
127 /// (in order to retry or reforward a failed request)
128 bool pinnedCanRetry() const;
129
25b0ce45
CT
130 template <typename StepStart>
131 void advanceDestination(const char *stepDescription, const Comm::ConnectionPointer &conn, const StepStart &startStep);
132
2ac4f6b5 133 ErrorState *makeConnectingError(const err_type type) const;
fcfdf7f9 134 void connectedToPeer(Security::EncryptorAnswer &answer);
6852be71 135 static void RegisterWithCacheManager(void);
3cd1350a 136
25b0ce45 137 void establishTunnelThruProxy(const Comm::ConnectionPointer &);
f5e17947 138 void tunnelEstablishmentDone(Http::TunnelerAnswer &answer);
25b0ce45
CT
139 void secureConnectionToPeerIfNeeded(const Comm::ConnectionPointer &);
140 void secureConnectionToPeer(const Comm::ConnectionPointer &);
141 void successfullyConnectedToPeer(const Comm::ConnectionPointer &);
f5e17947 142
e8dca475
CT
143 /// stops monitoring server connection for closure and updates pconn stats
144 void closeServerConnection(const char *reason);
145
55622953 146 void syncWithServerConn(const Comm::ConnectionPointer &server, const char *host, const bool reused);
7d1dac79 147 void syncHierNote(const Comm::ConnectionPointer &server, const char *host);
4b77ea6b 148
3eebd267
EB
149 /// whether we have used up all permitted forwarding attempts
150 bool exhaustedTries() const;
151
f5e17947
CT
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;
154
55622953
CT
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(); }
158
159 void cancelOpening(const char *reason);
160
161 void notifyConnOpener();
162
3cd1350a 163public:
164 StoreEntry *entry;
165 HttpRequest *request;
4bf68cfa
AR
166 AccessLogEntryPointer al; ///< info for the future access.log entry
167
7e9f330d
EB
168 /// called by Store if the entry is no longer usable
169 static void HandleStoreAbort(FwdState *);
3cd1350a 170
171private:
3cd1350a 172 Pointer self;
173 ErrorState *err;
5c336a3b 174 Comm::ConnectionPointer clientConn; ///< a possibly open connection to the client.
3cd1350a 175 time_t start_t;
3eebd267 176 int n_tries; ///< the number of forwarding attempts so far
3cd1350a 177
55622953
CT
178 // AsyncCalls which we set and may need cancelling.
179 struct {
180 AsyncCall::Pointer connector; ///< a call linking us to the ConnOpener producing serverConn.
181 } calls;
182
26ac0430 183 struct {
e857372a
FC
184 bool connected_okay; ///< TCP link ever opened properly. This affects retry of POST,PUT,CONNECT,etc
185 bool dont_retry;
186 bool forward_completed;
55622953 187 bool destinationsFound; ///< at least one candidate path found
2fadd50d 188 } flags;
3cd1350a 189
55622953
CT
190 HappyConnOpenerPointer connOpener; ///< current connection opening job
191 ResolvedPeersPointer destinations; ///< paths for forwarding the request
00ae51e4 192 Comm::ConnectionPointer serverConn; ///< a successfully opened connection to a server.
9b7992d9 193 PeerConnectionPointer destinationReceipt; ///< peer selection result (or nil)
d2bf2f90 194
398bc066
CT
195 AsyncCall::Pointer closeHandler; ///< The serverConn close handler
196
bc81ee68
AR
197 /// possible pconn race states
198 typedef enum { raceImpossible, racePossible, raceHappened } PconnRace;
199 PconnRace pconnRace; ///< current pconn race state
3cd1350a 200};
2087dca6 201
7d74b4b8 202void getOutgoingAddress(HttpRequest * request, const Comm::ConnectionPointer &conn);
28618087 203
55622953
CT
204/// a collection of previously used persistent Squid-to-peer HTTP(S) connections
205extern PconnPool *fwdPconnPool;
206
d85b8894 207#endif /* SQUID_FORWARD_H */
f53969cc 208