]> git.ipfire.org Git - thirdparty/squid.git/blob - src/FwdState.h
Happy Eyeballs: Use each fully resolved forwarding destination ASAP.
[thirdparty/squid.git] / src / FwdState.h
1 /*
2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
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
9 #ifndef SQUID_FORWARD_H
10 #define SQUID_FORWARD_H
11
12 #include "base/RefCount.h"
13 #include "comm.h"
14 #include "comm/Connection.h"
15 #include "err_type.h"
16 #include "fde.h"
17 #include "http/StatusCode.h"
18 #include "ip/Address.h"
19 #include "PeerSelectState.h"
20 #include "security/forward.h"
21 #if USE_OPENSSL
22 #include "ssl/support.h"
23 #endif
24
25 /* forward decls */
26
27 class AccessLogEntry;
28 typedef RefCount<AccessLogEntry> AccessLogEntryPointer;
29 class PconnPool;
30 typedef RefCount<PconnPool> PconnPoolPointer;
31 class ErrorState;
32 class HttpRequest;
33
34 #if USE_OPENSSL
35 namespace Ssl
36 {
37 class ErrorDetail;
38 class CertValidationResponse;
39 };
40 #endif
41
42 /**
43 * Returns the TOS value that we should be setting on the connection
44 * to the server, based on the ACL.
45 */
46 tos_t GetTosToServer(HttpRequest * request);
47
48 /**
49 * Returns the Netfilter mark value that we should be setting on the
50 * connection to the server, based on the ACL.
51 */
52 nfmark_t GetNfmarkToServer(HttpRequest * request);
53
54 /// Sets initial TOS value and Netfilter for the future outgoing connection.
55 void GetMarkingsToServer(HttpRequest * request, Comm::Connection &conn);
56
57 class HelperReply;
58
59 class FwdState: public RefCountable, public PeerSelectionInitiator
60 {
61 CBDATA_CHILD(FwdState);
62
63 public:
64 typedef RefCount<FwdState> Pointer;
65 virtual ~FwdState();
66 static void initModule();
67
68 /// Initiates request forwarding to a peer or origin server.
69 static void Start(const Comm::ConnectionPointer &client, StoreEntry *, HttpRequest *, const AccessLogEntryPointer &alp);
70 /// Same as Start() but no master xaction info (AccessLogEntry) available.
71 static void fwdStart(const Comm::ConnectionPointer &client, StoreEntry *, HttpRequest *);
72 /// time left to finish the whole forwarding process (which started at fwdStart)
73 static time_t ForwardTimeout(const time_t fwdStart);
74 /// Whether there is still time to re-try after a previous connection failure.
75 /// \param fwdStart The start time of the peer selection/connection process.
76 static bool EnoughTimeToReForward(const time_t fwdStart);
77
78 /// This is the real beginning of server connection. Call it whenever
79 /// the forwarding server destination has changed and a new one needs to be opened.
80 /// Produces the cannot-forward error on fail if no better error exists.
81 void startConnectionOrFail();
82
83 void fail(ErrorState *err);
84 void unregister(Comm::ConnectionPointer &conn);
85 void unregister(int fd);
86 void complete();
87 void handleUnregisteredServerEnd();
88 int reforward();
89 bool reforwardableStatus(const Http::StatusCode s) const;
90 void serverClosed(int fd);
91 void connectStart();
92 void connectDone(const Comm::ConnectionPointer & conn, Comm::Flag status, int xerrno);
93 void connectTimeout(int fd);
94 bool checkRetry();
95 bool checkRetriable();
96 void dispatch();
97 /// Pops a connection from connection pool if available. If not
98 /// checks the peer stand-by connection pool for available connection.
99 Comm::ConnectionPointer pconnPop(const Comm::ConnectionPointer &dest, const char *domain);
100 void pconnPush(Comm::ConnectionPointer & conn, const char *domain);
101
102 bool dontRetry() { return flags.dont_retry; }
103
104 void dontRetry(bool val) { flags.dont_retry = val; }
105
106 /** return a ConnectionPointer to the current server connection (may or may not be open) */
107 Comm::ConnectionPointer const & serverConnection() const { return serverConn; };
108
109 private:
110 // hidden for safer management of self; use static fwdStart
111 FwdState(const Comm::ConnectionPointer &client, StoreEntry *, HttpRequest *, const AccessLogEntryPointer &alp);
112 void start(Pointer aSelf);
113 void stopAndDestroy(const char *reason);
114
115 /* PeerSelectionInitiator API */
116 virtual void noteDestination(Comm::ConnectionPointer conn) override;
117 virtual void noteDestinationsEnd(ErrorState *selectionError) override;
118
119 #if STRICT_ORIGINAL_DST
120 void selectPeerForIntercepted();
121 #endif
122 static void logReplyStatus(int tries, const Http::StatusCode status);
123 void doneWithRetries();
124 void completed();
125 void retryOrBail();
126 ErrorState *makeConnectingError(const err_type type) const;
127 void connectedToPeer(Security::EncryptorAnswer &answer);
128 static void RegisterWithCacheManager(void);
129
130 /// stops monitoring server connection for closure and updates pconn stats
131 void closeServerConnection(const char *reason);
132
133 void syncWithServerConn(const char *host);
134 void syncHierNote(const Comm::ConnectionPointer &server, const char *host);
135
136 public:
137 StoreEntry *entry;
138 HttpRequest *request;
139 AccessLogEntryPointer al; ///< info for the future access.log entry
140
141 static void abort(void*);
142
143 private:
144 Pointer self;
145 ErrorState *err;
146 Comm::ConnectionPointer clientConn; ///< a possibly open connection to the client.
147 time_t start_t;
148 int n_tries;
149
150 // AsyncCalls which we set and may need cancelling.
151 struct {
152 AsyncCall::Pointer connector; ///< a call linking us to the ConnOpener producing serverConn.
153 } calls;
154
155 struct {
156 bool connected_okay; ///< TCP link ever opened properly. This affects retry of POST,PUT,CONNECT,etc
157 bool dont_retry;
158 bool forward_completed;
159 } flags;
160
161 /** connections to open, in order, until successful */
162 Comm::ConnectionList serverDestinations;
163
164 Comm::ConnectionPointer serverConn; ///< a successfully opened connection to a server.
165
166 AsyncCall::Pointer closeHandler; ///< The serverConn close handler
167
168 /// possible pconn race states
169 typedef enum { raceImpossible, racePossible, raceHappened } PconnRace;
170 PconnRace pconnRace; ///< current pconn race state
171 };
172
173 void getOutgoingAddress(HttpRequest * request, Comm::ConnectionPointer conn);
174
175 #endif /* SQUID_FORWARD_H */
176