2 * Copyright (C) 1996-2014 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"
20 #include "ssl/support.h"
26 typedef RefCount
<AccessLogEntry
> AccessLogEntryPointer
;
28 typedef RefCount
<PconnPool
> PconnPoolPointer
;
36 class CertValidationResponse
;
37 class PeerConnectorAnswer
;
42 * Returns the TOS value that we should be setting on the connection
43 * to the server, based on the ACL.
45 tos_t
GetTosToServer(HttpRequest
* request
);
48 * Returns the Netfilter mark value that we should be setting on the
49 * connection to the server, based on the ACL.
51 nfmark_t
GetNfmarkToServer(HttpRequest
* request
);
53 /// Sets initial TOS value and Netfilter for the future outgoing connection.
54 void GetMarkingsToServer(HttpRequest
* request
, Comm::Connection
&conn
);
58 class FwdState
: public RefCountable
61 typedef RefCount
<FwdState
> Pointer
;
63 static void initModule();
65 /// Initiates request forwarding to a peer or origin server.
66 static void Start(const Comm::ConnectionPointer
&client
, StoreEntry
*, HttpRequest
*, const AccessLogEntryPointer
&alp
);
67 /// Same as Start() but no master xaction info (AccessLogEntry) available.
68 static void fwdStart(const Comm::ConnectionPointer
&client
, StoreEntry
*, HttpRequest
*);
70 /// This is the real beginning of server connection. Call it whenever
71 /// the forwarding server destination has changed and a new one needs to be opened.
72 /// Produces the cannot-forward error on fail if no better error exists.
73 void startConnectionOrFail();
75 void fail(ErrorState
*err
);
76 void unregister(Comm::ConnectionPointer
&conn
);
77 void unregister(int fd
);
79 void handleUnregisteredServerEnd();
81 bool reforwardableStatus(const Http::StatusCode s
) const;
82 void serverClosed(int fd
);
84 void connectDone(const Comm::ConnectionPointer
& conn
, Comm::Flag status
, int xerrno
);
85 void connectTimeout(int fd
);
86 time_t timeLeft() const; ///< the time left before the forwarding timeout expired
88 bool checkRetriable();
90 /// Pops a connection from connection pool if available. If not
91 /// checks the peer stand-by connection pool for available connection.
92 Comm::ConnectionPointer
pconnPop(const Comm::ConnectionPointer
&dest
, const char *domain
);
93 void pconnPush(Comm::ConnectionPointer
& conn
, const char *domain
);
95 bool dontRetry() { return flags
.dont_retry
; }
97 void dontRetry(bool val
) { flags
.dont_retry
= val
; }
99 /** return a ConnectionPointer to the current server connection (may or may not be open) */
100 Comm::ConnectionPointer
const & serverConnection() const { return serverConn
; };
103 // hidden for safer management of self; use static fwdStart
104 FwdState(const Comm::ConnectionPointer
&client
, StoreEntry
*, HttpRequest
*, const AccessLogEntryPointer
&alp
);
105 void start(Pointer aSelf
);
107 #if STRICT_ORIGINAL_DST
108 void selectPeerForIntercepted();
110 static void logReplyStatus(int tries
, const Http::StatusCode status
);
111 void doneWithRetries();
114 ErrorState
*makeConnectingError(const err_type type
) const;
116 void connectedToPeer(Ssl::PeerConnectorAnswer
&answer
);
118 static void RegisterWithCacheManager(void);
120 /// stops monitoring server connection for closure and updates pconn stats
121 void closeServerConnection(const char *reason
);
125 HttpRequest
*request
;
126 AccessLogEntryPointer al
; ///< info for the future access.log entry
128 static void abort(void*);
133 Comm::ConnectionPointer clientConn
; ///< a possibly open connection to the client.
137 // AsyncCalls which we set and may need cancelling.
139 AsyncCall::Pointer connector
; ///< a call linking us to the ConnOpener producing serverConn.
143 bool connected_okay
; ///< TCP link ever opened properly. This affects retry of POST,PUT,CONNECT,etc
145 bool forward_completed
;
148 /** connections to open, in order, until successful */
149 Comm::ConnectionList serverDestinations
;
151 Comm::ConnectionPointer serverConn
; ///< a successfully opened connection to a server.
153 /// possible pconn race states
154 typedef enum { raceImpossible
, racePossible
, raceHappened
} PconnRace
;
155 PconnRace pconnRace
; ///< current pconn race state
157 // NP: keep this last. It plays with private/public
158 CBDATA_CLASS2(FwdState
);
161 void getOutgoingAddress(HttpRequest
* request
, Comm::ConnectionPointer conn
);
163 #endif /* SQUID_FORWARD_H */