2 * Copyright (C) 1996-2017 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/RefCount.h"
14 #include "comm/Connection.h"
17 #include "http/StatusCode.h"
18 #include "ip/Address.h"
19 #include "PeerSelectState.h"
20 #include "security/forward.h"
22 #include "ssl/support.h"
28 typedef RefCount
<AccessLogEntry
> AccessLogEntryPointer
;
30 typedef RefCount
<PconnPool
> PconnPoolPointer
;
38 class CertValidationResponse
;
43 * Returns the TOS value that we should be setting on the connection
44 * to the server, based on the ACL.
46 tos_t
GetTosToServer(HttpRequest
* request
);
49 * Returns the Netfilter mark value that we should be setting on the
50 * connection to the server, based on the ACL.
52 nfmark_t
GetNfmarkToServer(HttpRequest
* request
);
54 /// Sets initial TOS value and Netfilter for the future outgoing connection.
55 void GetMarkingsToServer(HttpRequest
* request
, Comm::Connection
&conn
);
59 class FwdState
: public RefCountable
, public PeerSelectionInitiator
61 CBDATA_CHILD(FwdState
);
64 typedef RefCount
<FwdState
> Pointer
;
66 static void initModule();
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
);
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();
83 void fail(ErrorState
*err
);
84 void unregister(Comm::ConnectionPointer
&conn
);
85 void unregister(int fd
);
87 void handleUnregisteredServerEnd();
89 bool reforwardableStatus(const Http::StatusCode s
) const;
90 void serverClosed(int fd
);
92 void connectDone(const Comm::ConnectionPointer
& conn
, Comm::Flag status
, int xerrno
);
93 void connectTimeout(int fd
);
95 bool checkRetriable();
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
);
102 bool dontRetry() { return flags
.dont_retry
; }
104 void dontRetry(bool val
) { flags
.dont_retry
= val
; }
106 /** return a ConnectionPointer to the current server connection (may or may not be open) */
107 Comm::ConnectionPointer
const & serverConnection() const { return serverConn
; };
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
);
115 /* PeerSelectionInitiator API */
116 virtual void noteDestination(Comm::ConnectionPointer conn
) override
;
117 virtual void noteDestinationsEnd(ErrorState
*selectionError
) override
;
119 #if STRICT_ORIGINAL_DST
120 void selectPeerForIntercepted();
122 static void logReplyStatus(int tries
, const Http::StatusCode status
);
123 void doneWithRetries();
126 ErrorState
*makeConnectingError(const err_type type
) const;
127 void connectedToPeer(Security::EncryptorAnswer
&answer
);
128 static void RegisterWithCacheManager(void);
130 /// stops monitoring server connection for closure and updates pconn stats
131 void closeServerConnection(const char *reason
);
133 void syncWithServerConn(const char *host
);
134 void syncHierNote(const Comm::ConnectionPointer
&server
, const char *host
);
138 HttpRequest
*request
;
139 AccessLogEntryPointer al
; ///< info for the future access.log entry
141 static void abort(void*);
146 Comm::ConnectionPointer clientConn
; ///< a possibly open connection to the client.
150 // AsyncCalls which we set and may need cancelling.
152 AsyncCall::Pointer connector
; ///< a call linking us to the ConnOpener producing serverConn.
156 bool connected_okay
; ///< TCP link ever opened properly. This affects retry of POST,PUT,CONNECT,etc
158 bool forward_completed
;
161 /** connections to open, in order, until successful */
162 Comm::ConnectionList serverDestinations
;
164 Comm::ConnectionPointer serverConn
; ///< a successfully opened connection to a server.
166 AsyncCall::Pointer closeHandler
; ///< The serverConn close handler
168 /// possible pconn race states
169 typedef enum { raceImpossible
, racePossible
, raceHappened
} PconnRace
;
170 PconnRace pconnRace
; ///< current pconn race state
173 void getOutgoingAddress(HttpRequest
* request
, Comm::ConnectionPointer conn
);
175 #endif /* SQUID_FORWARD_H */