]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tunnel.cc
SourceLayout: rename ClientSocketContext to Http::Stream
[thirdparty/squid.git] / src / tunnel.cc
1 /*
2 * Copyright (C) 1996-2016 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 /* DEBUG: section 26 Secure Sockets Layer Proxy */
10
11 #include "squid.h"
12 #include "acl/FilledChecklist.h"
13 #include "base/CbcPointer.h"
14 #include "CachePeer.h"
15 #include "cbdata.h"
16 #include "client_side.h"
17 #include "client_side_request.h"
18 #include "comm.h"
19 #include "comm/Connection.h"
20 #include "comm/ConnOpener.h"
21 #include "comm/Read.h"
22 #include "comm/Write.h"
23 #include "errorpage.h"
24 #include "fde.h"
25 #include "FwdState.h"
26 #include "globals.h"
27 #include "http.h"
28 #include "http/Stream.h"
29 #include "HttpRequest.h"
30 #include "HttpStateFlags.h"
31 #include "ip/QosConfig.h"
32 #include "LogTags.h"
33 #include "MemBuf.h"
34 #include "PeerSelectState.h"
35 #include "SBuf.h"
36 #include "SquidConfig.h"
37 #include "SquidTime.h"
38 #include "ssl/BlindPeerConnector.h"
39 #include "StatCounters.h"
40 #if USE_OPENSSL
41 #include "ssl/bio.h"
42 #include "ssl/ServerBump.h"
43 #endif
44 #include "tools.h"
45 #if USE_DELAY_POOLS
46 #include "DelayId.h"
47 #endif
48
49 #include <climits>
50 #include <cerrno>
51
52 /**
53 * TunnelStateData is the state engine performing the tasks for
54 * setup of a TCP tunnel from an existing open client FD to a server
55 * then shuffling binary data between the resulting FD pair.
56 */
57 /*
58 * TODO 1: implement a read/write API on ConnStateData to send/receive blocks
59 * of pre-formatted data. Then we can use that as the client side of the tunnel
60 * instead of re-implementing it here and occasionally getting the ConnStateData
61 * read/write state wrong.
62 *
63 * TODO 2: then convert this into a AsyncJob, possibly a child of 'Server'
64 */
65 class TunnelStateData
66 {
67 CBDATA_CLASS(TunnelStateData);
68
69 public:
70 TunnelStateData();
71 ~TunnelStateData();
72 TunnelStateData(const TunnelStateData &); // do not implement
73 TunnelStateData &operator =(const TunnelStateData &); // do not implement
74
75 class Connection;
76 static void ReadClient(const Comm::ConnectionPointer &, char *buf, size_t len, Comm::Flag errcode, int xerrno, void *data);
77 static void ReadServer(const Comm::ConnectionPointer &, char *buf, size_t len, Comm::Flag errcode, int xerrno, void *data);
78 static void WriteClientDone(const Comm::ConnectionPointer &, char *buf, size_t len, Comm::Flag flag, int xerrno, void *data);
79 static void WriteServerDone(const Comm::ConnectionPointer &, char *buf, size_t len, Comm::Flag flag, int xerrno, void *data);
80
81 /// Starts reading peer response to our CONNECT request.
82 void readConnectResponse();
83
84 /// Called when we may be done handling a CONNECT exchange with the peer.
85 void connectExchangeCheckpoint();
86
87 bool noConnections() const;
88 char *url;
89 CbcPointer<ClientHttpRequest> http;
90 HttpRequest::Pointer request;
91 AccessLogEntryPointer al;
92 Comm::ConnectionList serverDestinations;
93
94 const char * getHost() const {
95 return (server.conn != NULL && server.conn->getPeer() ? server.conn->getPeer()->host : request->url.host());
96 };
97
98 /// Whether we are writing a CONNECT request to a peer.
99 bool waitingForConnectRequest() const { return connectReqWriting; }
100 /// Whether we are reading a CONNECT response from a peer.
101 bool waitingForConnectResponse() const { return connectRespBuf; }
102 /// Whether we are waiting for the CONNECT request/response exchange with the peer.
103 bool waitingForConnectExchange() const { return waitingForConnectRequest() || waitingForConnectResponse(); }
104
105 /// Whether the client sent a CONNECT request to us.
106 bool clientExpectsConnectResponse() const {
107 #if USE_OPENSSL
108 // We are bumping and we had already send "OK CONNECTED"
109 if (http.valid() && http->getConn() && http->getConn()->serverBump() && http->getConn()->serverBump()->step > Ssl::bumpStep1)
110 return false;
111 #endif
112 return !(request != NULL &&
113 (request->flags.interceptTproxy || request->flags.intercepted));
114 }
115
116 /// Sends "502 Bad Gateway" error response to the client,
117 /// if it is waiting for Squid CONNECT response, closing connections.
118 void informUserOfPeerError(const char *errMsg, size_t);
119
120 class Connection
121 {
122
123 public:
124 Connection() : len (0), buf ((char *)xmalloc(SQUID_TCP_SO_RCVBUF)), size_ptr(NULL), delayedLoops(0),
125 readPending(NULL), readPendingFunc(NULL) {}
126
127 ~Connection();
128
129 int bytesWanted(int lower=0, int upper = INT_MAX) const;
130 void bytesIn(int const &);
131 #if USE_DELAY_POOLS
132
133 void setDelayId(DelayId const &);
134 #endif
135
136 void error(int const xerrno);
137 int debugLevelForError(int const xerrno) const;
138 void closeIfOpen();
139 void dataSent (size_t amount);
140 /// writes 'b' buffer, setting the 'writer' member to 'callback'.
141 void write(const char *b, int size, AsyncCall::Pointer &callback, FREE * free_func);
142 int len;
143 char *buf;
144 AsyncCall::Pointer writer; ///< pending Comm::Write callback
145 uint64_t *size_ptr; /* pointer to size in an ConnStateData for logging */
146
147 Comm::ConnectionPointer conn; ///< The currently connected connection.
148 uint8_t delayedLoops; ///< how many times a read on this connection has been postponed.
149
150 // XXX: make these an AsyncCall when event API can handle them
151 TunnelStateData *readPending;
152 EVH *readPendingFunc;
153 private:
154 #if USE_DELAY_POOLS
155
156 DelayId delayId;
157 #endif
158
159 };
160
161 Connection client, server;
162 int *status_ptr; ///< pointer for logging HTTP status
163 LogTags *logTag_ptr; ///< pointer for logging Squid processing code
164 MemBuf *connectRespBuf; ///< accumulates peer CONNECT response when we need it
165 bool connectReqWriting; ///< whether we are writing a CONNECT request to a peer
166 SBuf preReadClientData;
167 time_t started; ///< when this tunnel was initiated.
168
169 void copyRead(Connection &from, IOCB *completion);
170
171 /// continue to set up connection to a peer, going async for SSL peers
172 void connectToPeer();
173
174 private:
175 #if USE_OPENSSL
176 /// Gives PeerConnector access to Answer in the TunnelStateData callback dialer.
177 class MyAnswerDialer: public CallDialer, public Ssl::PeerConnector::CbDialer
178 {
179 public:
180 typedef void (TunnelStateData::*Method)(Security::EncryptorAnswer &);
181
182 MyAnswerDialer(Method method, TunnelStateData *tunnel):
183 method_(method), tunnel_(tunnel), answer_() {}
184
185 /* CallDialer API */
186 virtual bool canDial(AsyncCall &call) { return tunnel_.valid(); }
187 void dial(AsyncCall &call) { ((&(*tunnel_))->*method_)(answer_); }
188 virtual void print(std::ostream &os) const {
189 os << '(' << tunnel_.get() << ", " << answer_ << ')';
190 }
191
192 /* Ssl::PeerConnector::CbDialer API */
193 virtual Security::EncryptorAnswer &answer() { return answer_; }
194
195 private:
196 Method method_;
197 CbcPointer<TunnelStateData> tunnel_;
198 Security::EncryptorAnswer answer_;
199 };
200 #endif
201
202 /// callback handler after connection setup (including any encryption)
203 void connectedToPeer(Security::EncryptorAnswer &answer);
204
205 public:
206 bool keepGoingAfterRead(size_t len, Comm::Flag errcode, int xerrno, Connection &from, Connection &to);
207 void copy(size_t len, Connection &from, Connection &to, IOCB *);
208 void handleConnectResponse(const size_t chunkSize);
209 void readServer(char *buf, size_t len, Comm::Flag errcode, int xerrno);
210 void readClient(char *buf, size_t len, Comm::Flag errcode, int xerrno);
211 void writeClientDone(char *buf, size_t len, Comm::Flag flag, int xerrno);
212 void writeServerDone(char *buf, size_t len, Comm::Flag flag, int xerrno);
213
214 static void ReadConnectResponseDone(const Comm::ConnectionPointer &, char *buf, size_t len, Comm::Flag errcode, int xerrno, void *data);
215 void readConnectResponseDone(char *buf, size_t len, Comm::Flag errcode, int xerrno);
216 void copyClientBytes();
217 };
218
219 static const char *const conn_established = "HTTP/1.1 200 Connection established\r\n\r\n";
220
221 static CNCB tunnelConnectDone;
222 static ERCB tunnelErrorComplete;
223 static CLCB tunnelServerClosed;
224 static CLCB tunnelClientClosed;
225 static CTCB tunnelTimeout;
226 static PSC tunnelPeerSelectComplete;
227 static EVH tunnelDelayedClientRead;
228 static EVH tunnelDelayedServerRead;
229 static void tunnelConnected(const Comm::ConnectionPointer &server, void *);
230 static void tunnelRelayConnectRequest(const Comm::ConnectionPointer &server, void *);
231
232 static void
233 tunnelServerClosed(const CommCloseCbParams &params)
234 {
235 TunnelStateData *tunnelState = (TunnelStateData *)params.data;
236 debugs(26, 3, HERE << tunnelState->server.conn);
237 tunnelState->server.conn = NULL;
238 tunnelState->server.writer = NULL;
239
240 if (tunnelState->request != NULL)
241 tunnelState->request->hier.stopPeerClock(false);
242
243 if (tunnelState->noConnections()) {
244 // ConnStateData pipeline should contain the CONNECT we are performing
245 // but it may be invalid already (bug 4392)
246 if (tunnelState->http.valid() && tunnelState->http->getConn()) {
247 auto ctx = tunnelState->http->getConn()->pipeline.front();
248 if (ctx != nullptr)
249 ctx->finished();
250 }
251 delete tunnelState;
252 return;
253 }
254
255 if (!tunnelState->client.writer) {
256 tunnelState->client.conn->close();
257 return;
258 }
259 }
260
261 static void
262 tunnelClientClosed(const CommCloseCbParams &params)
263 {
264 TunnelStateData *tunnelState = (TunnelStateData *)params.data;
265 debugs(26, 3, HERE << tunnelState->client.conn);
266 tunnelState->client.conn = NULL;
267 tunnelState->client.writer = NULL;
268
269 if (tunnelState->noConnections()) {
270 // ConnStateData pipeline should contain the CONNECT we are performing
271 // but it may be invalid already (bug 4392)
272 if (tunnelState->http.valid() && tunnelState->http->getConn()) {
273 auto ctx = tunnelState->http->getConn()->pipeline.front();
274 if (ctx != nullptr)
275 ctx->finished();
276 }
277 delete tunnelState;
278 return;
279 }
280
281 if (!tunnelState->server.writer) {
282 tunnelState->server.conn->close();
283 return;
284 }
285 }
286
287 TunnelStateData::TunnelStateData() :
288 url(NULL),
289 http(),
290 request(NULL),
291 status_ptr(NULL),
292 logTag_ptr(NULL),
293 connectRespBuf(NULL),
294 connectReqWriting(false),
295 started(squid_curtime)
296 {
297 debugs(26, 3, "TunnelStateData constructed this=" << this);
298 client.readPendingFunc = &tunnelDelayedClientRead;
299 server.readPendingFunc = &tunnelDelayedServerRead;
300 }
301
302 TunnelStateData::~TunnelStateData()
303 {
304 debugs(26, 3, "TunnelStateData destructed this=" << this);
305 assert(noConnections());
306 xfree(url);
307 serverDestinations.clear();
308 delete connectRespBuf;
309 }
310
311 TunnelStateData::Connection::~Connection()
312 {
313 if (readPending)
314 eventDelete(readPendingFunc, readPending);
315
316 safe_free(buf);
317 }
318
319 int
320 TunnelStateData::Connection::bytesWanted(int lowerbound, int upperbound) const
321 {
322 #if USE_DELAY_POOLS
323 return delayId.bytesWanted(lowerbound, upperbound);
324 #else
325
326 return upperbound;
327 #endif
328 }
329
330 void
331 TunnelStateData::Connection::bytesIn(int const &count)
332 {
333 debugs(26, 3, HERE << "len=" << len << " + count=" << count);
334 #if USE_DELAY_POOLS
335 delayId.bytesIn(count);
336 #endif
337
338 len += count;
339 }
340
341 int
342 TunnelStateData::Connection::debugLevelForError(int const xerrno) const
343 {
344 #ifdef ECONNRESET
345
346 if (xerrno == ECONNRESET)
347 return 2;
348
349 #endif
350
351 if (ignoreErrno(xerrno))
352 return 3;
353
354 return 1;
355 }
356
357 /* Read from server side and queue it for writing to the client */
358 void
359 TunnelStateData::ReadServer(const Comm::ConnectionPointer &c, char *buf, size_t len, Comm::Flag errcode, int xerrno, void *data)
360 {
361 TunnelStateData *tunnelState = (TunnelStateData *)data;
362 assert(cbdataReferenceValid(tunnelState));
363 debugs(26, 3, HERE << c);
364
365 tunnelState->readServer(buf, len, errcode, xerrno);
366 }
367
368 void
369 TunnelStateData::readServer(char *, size_t len, Comm::Flag errcode, int xerrno)
370 {
371 debugs(26, 3, HERE << server.conn << ", read " << len << " bytes, err=" << errcode);
372 server.delayedLoops=0;
373
374 /*
375 * Bail out early on Comm::ERR_CLOSING
376 * - close handlers will tidy up for us
377 */
378
379 if (errcode == Comm::ERR_CLOSING)
380 return;
381
382 if (len > 0) {
383 server.bytesIn(len);
384 statCounter.server.all.kbytes_in += len;
385 statCounter.server.other.kbytes_in += len;
386 }
387
388 if (keepGoingAfterRead(len, errcode, xerrno, server, client))
389 copy(len, server, client, WriteClientDone);
390 }
391
392 /// Called when we read [a part of] CONNECT response from the peer
393 void
394 TunnelStateData::readConnectResponseDone(char *, size_t len, Comm::Flag errcode, int xerrno)
395 {
396 debugs(26, 3, server.conn << ", read " << len << " bytes, err=" << errcode);
397 assert(waitingForConnectResponse());
398
399 if (errcode == Comm::ERR_CLOSING)
400 return;
401
402 if (len > 0) {
403 connectRespBuf->appended(len);
404 server.bytesIn(len);
405 statCounter.server.all.kbytes_in += len;
406 statCounter.server.other.kbytes_in += len;
407 }
408
409 if (keepGoingAfterRead(len, errcode, xerrno, server, client))
410 handleConnectResponse(len);
411 }
412
413 void
414 TunnelStateData::informUserOfPeerError(const char *errMsg, const size_t sz)
415 {
416 server.len = 0;
417
418 if (logTag_ptr)
419 *logTag_ptr = LOG_TCP_TUNNEL;
420
421 if (!clientExpectsConnectResponse()) {
422 // closing the connection is the best we can do here
423 debugs(50, 3, server.conn << " closing on error: " << errMsg);
424 server.conn->close();
425 return;
426 }
427
428 // if we have no reply suitable to relay, use 502 Bad Gateway
429 if (!sz || sz > static_cast<size_t>(connectRespBuf->contentSize())) {
430 ErrorState *err = new ErrorState(ERR_CONNECT_FAIL, Http::scBadGateway, request.getRaw());
431 *status_ptr = Http::scBadGateway;
432 err->callback = tunnelErrorComplete;
433 err->callback_data = this;
434 errorSend(http->getConn()->clientConnection, err);
435 return;
436 }
437
438 // if we need to send back the server response. write its headers to the client
439 server.len = sz;
440 memcpy(server.buf, connectRespBuf->content(), server.len);
441 copy(server.len, server, client, TunnelStateData::WriteClientDone);
442 // then close the server FD to prevent any relayed keep-alive causing CVE-2015-5400
443 server.closeIfOpen();
444 }
445
446 /* Read from client side and queue it for writing to the server */
447 void
448 TunnelStateData::ReadConnectResponseDone(const Comm::ConnectionPointer &, char *buf, size_t len, Comm::Flag errcode, int xerrno, void *data)
449 {
450 TunnelStateData *tunnelState = (TunnelStateData *)data;
451 assert (cbdataReferenceValid (tunnelState));
452
453 tunnelState->readConnectResponseDone(buf, len, errcode, xerrno);
454 }
455
456 /// Parses [possibly incomplete] CONNECT response and reacts to it.
457 /// If the tunnel is being closed or more response data is needed, returns false.
458 /// Otherwise, the caller should handle the remaining read data, if any.
459 void
460 TunnelStateData::handleConnectResponse(const size_t chunkSize)
461 {
462 assert(waitingForConnectResponse());
463
464 // Ideally, client and server should use MemBuf or better, but current code
465 // never accumulates more than one read when shoveling data (XXX) so it does
466 // not need to deal with MemBuf complexity. To keep it simple, we use a
467 // dedicated MemBuf for accumulating CONNECT responses. TODO: When shoveling
468 // is optimized, reuse server.buf for CONNEC response accumulation instead.
469
470 /* mimic the basic parts of HttpStateData::processReplyHeader() */
471 HttpReply rep;
472 Http::StatusCode parseErr = Http::scNone;
473 const bool eof = !chunkSize;
474 connectRespBuf->terminate(); // HttpMsg::parse requires terminated string
475 const bool parsed = rep.parse(connectRespBuf->content(), connectRespBuf->contentSize(), eof, &parseErr);
476 if (!parsed) {
477 if (parseErr > 0) { // unrecoverable parsing error
478 informUserOfPeerError("malformed CONNECT response from peer", 0);
479 return;
480 }
481
482 // need more data
483 assert(!eof);
484 assert(!parseErr);
485
486 if (!connectRespBuf->hasSpace()) {
487 informUserOfPeerError("huge CONNECT response from peer", 0);
488 return;
489 }
490
491 // keep reading
492 readConnectResponse();
493 return;
494 }
495
496 // CONNECT response was successfully parsed
497 *status_ptr = rep.sline.status();
498
499 // we need to relay the 401/407 responses when login=PASS(THRU)
500 const char *pwd = server.conn->getPeer()->login;
501 const bool relay = pwd && (strcmp(pwd, "PASS") != 0 || strcmp(pwd, "PASSTHRU") != 0) &&
502 (*status_ptr == Http::scProxyAuthenticationRequired ||
503 *status_ptr == Http::scUnauthorized);
504
505 // bail if we did not get an HTTP 200 (Connection Established) response
506 if (rep.sline.status() != Http::scOkay) {
507 // if we ever decide to reuse the peer connection, we must extract the error response first
508 informUserOfPeerError("unsupported CONNECT response status code", (relay ? rep.hdr_sz : 0));
509 return;
510 }
511
512 if (rep.hdr_sz < connectRespBuf->contentSize()) {
513 // preserve bytes that the server already sent after the CONNECT response
514 server.len = connectRespBuf->contentSize() - rep.hdr_sz;
515 memcpy(server.buf, connectRespBuf->content()+rep.hdr_sz, server.len);
516 } else {
517 // reset; delay pools were using this field to throttle CONNECT response
518 server.len = 0;
519 }
520
521 delete connectRespBuf;
522 connectRespBuf = NULL;
523 connectExchangeCheckpoint();
524 }
525
526 void
527 TunnelStateData::Connection::error(int const xerrno)
528 {
529 /* XXX fixme xstrerror and xerrno... */
530 errno = xerrno;
531
532 debugs(50, debugLevelForError(xerrno), HERE << conn << ": read/write failure: " << xstrerror());
533
534 if (!ignoreErrno(xerrno))
535 conn->close();
536 }
537
538 /* Read from client side and queue it for writing to the server */
539 void
540 TunnelStateData::ReadClient(const Comm::ConnectionPointer &, char *buf, size_t len, Comm::Flag errcode, int xerrno, void *data)
541 {
542 TunnelStateData *tunnelState = (TunnelStateData *)data;
543 assert (cbdataReferenceValid (tunnelState));
544
545 tunnelState->readClient(buf, len, errcode, xerrno);
546 }
547
548 void
549 TunnelStateData::readClient(char *, size_t len, Comm::Flag errcode, int xerrno)
550 {
551 debugs(26, 3, HERE << client.conn << ", read " << len << " bytes, err=" << errcode);
552 client.delayedLoops=0;
553
554 /*
555 * Bail out early on Comm::ERR_CLOSING
556 * - close handlers will tidy up for us
557 */
558
559 if (errcode == Comm::ERR_CLOSING)
560 return;
561
562 if (len > 0) {
563 client.bytesIn(len);
564 statCounter.client_http.kbytes_in += len;
565 }
566
567 if (keepGoingAfterRead(len, errcode, xerrno, client, server))
568 copy(len, client, server, WriteServerDone);
569 }
570
571 /// Updates state after reading from client or server.
572 /// Returns whether the caller should use the data just read.
573 bool
574 TunnelStateData::keepGoingAfterRead(size_t len, Comm::Flag errcode, int xerrno, Connection &from, Connection &to)
575 {
576 debugs(26, 3, HERE << "from={" << from.conn << "}, to={" << to.conn << "}");
577
578 /* I think this is to prevent free-while-in-a-callback behaviour
579 * - RBC 20030229
580 * from.conn->close() / to.conn->close() done here trigger close callbacks which may free TunnelStateData
581 */
582 const CbcPointer<TunnelStateData> safetyLock(this);
583
584 /* Bump the source connection read timeout on any activity */
585 if (Comm::IsConnOpen(from.conn)) {
586 AsyncCall::Pointer timeoutCall = commCbCall(5, 4, "tunnelTimeout",
587 CommTimeoutCbPtrFun(tunnelTimeout, this));
588 commSetConnTimeout(from.conn, Config.Timeout.read, timeoutCall);
589 }
590
591 /* Bump the dest connection read timeout on any activity */
592 /* see Bug 3659: tunnels can be weird, with very long one-way transfers */
593 if (Comm::IsConnOpen(to.conn)) {
594 AsyncCall::Pointer timeoutCall = commCbCall(5, 4, "tunnelTimeout",
595 CommTimeoutCbPtrFun(tunnelTimeout, this));
596 commSetConnTimeout(to.conn, Config.Timeout.read, timeoutCall);
597 }
598
599 if (errcode)
600 from.error (xerrno);
601 else if (len == 0 || !Comm::IsConnOpen(to.conn)) {
602 debugs(26, 3, HERE << "Nothing to write or client gone. Terminate the tunnel.");
603 from.conn->close();
604
605 /* Only close the remote end if we've finished queueing data to it */
606 if (from.len == 0 && Comm::IsConnOpen(to.conn) ) {
607 to.conn->close();
608 }
609 } else if (cbdataReferenceValid(this)) {
610 return true;
611 }
612
613 return false;
614 }
615
616 void
617 TunnelStateData::copy(size_t len, Connection &from, Connection &to, IOCB *completion)
618 {
619 debugs(26, 3, HERE << "Schedule Write");
620 AsyncCall::Pointer call = commCbCall(5,5, "TunnelBlindCopyWriteHandler",
621 CommIoCbPtrFun(completion, this));
622 to.write(from.buf, len, call, NULL);
623 }
624
625 /* Writes data from the client buffer to the server side */
626 void
627 TunnelStateData::WriteServerDone(const Comm::ConnectionPointer &, char *buf, size_t len, Comm::Flag flag, int xerrno, void *data)
628 {
629 TunnelStateData *tunnelState = (TunnelStateData *)data;
630 assert (cbdataReferenceValid (tunnelState));
631 tunnelState->server.writer = NULL;
632
633 tunnelState->writeServerDone(buf, len, flag, xerrno);
634 }
635
636 void
637 TunnelStateData::writeServerDone(char *, size_t len, Comm::Flag flag, int xerrno)
638 {
639 debugs(26, 3, HERE << server.conn << ", " << len << " bytes written, flag=" << flag);
640
641 /* Error? */
642 if (flag != Comm::OK) {
643 if (flag != Comm::ERR_CLOSING) {
644 debugs(26, 4, HERE << "calling TunnelStateData::server.error(" << xerrno <<")");
645 server.error(xerrno); // may call comm_close
646 }
647 return;
648 }
649
650 /* EOF? */
651 if (len == 0) {
652 debugs(26, 4, HERE << "No read input. Closing server connection.");
653 server.conn->close();
654 return;
655 }
656
657 /* Valid data */
658 statCounter.server.all.kbytes_out += len;
659 statCounter.server.other.kbytes_out += len;
660 client.dataSent(len);
661
662 /* If the other end has closed, so should we */
663 if (!Comm::IsConnOpen(client.conn)) {
664 debugs(26, 4, HERE << "Client gone away. Shutting down server connection.");
665 server.conn->close();
666 return;
667 }
668
669 const CbcPointer<TunnelStateData> safetyLock(this); /* ??? should be locked by the caller... */
670
671 if (cbdataReferenceValid(this))
672 copyClientBytes();
673 }
674
675 /* Writes data from the server buffer to the client side */
676 void
677 TunnelStateData::WriteClientDone(const Comm::ConnectionPointer &, char *buf, size_t len, Comm::Flag flag, int xerrno, void *data)
678 {
679 TunnelStateData *tunnelState = (TunnelStateData *)data;
680 assert (cbdataReferenceValid (tunnelState));
681 tunnelState->client.writer = NULL;
682
683 tunnelState->writeClientDone(buf, len, flag, xerrno);
684 }
685
686 void
687 TunnelStateData::Connection::dataSent(size_t amount)
688 {
689 debugs(26, 3, HERE << "len=" << len << " - amount=" << amount);
690 assert(amount == (size_t)len);
691 len =0;
692 /* increment total object size */
693
694 if (size_ptr)
695 *size_ptr += amount;
696 }
697
698 void
699 TunnelStateData::Connection::write(const char *b, int size, AsyncCall::Pointer &callback, FREE * free_func)
700 {
701 writer = callback;
702 Comm::Write(conn, b, size, callback, free_func);
703 }
704
705 void
706 TunnelStateData::writeClientDone(char *, size_t len, Comm::Flag flag, int xerrno)
707 {
708 debugs(26, 3, HERE << client.conn << ", " << len << " bytes written, flag=" << flag);
709
710 /* Error? */
711 if (flag != Comm::OK) {
712 if (flag != Comm::ERR_CLOSING) {
713 debugs(26, 4, HERE << "Closing client connection due to comm flags.");
714 client.error(xerrno); // may call comm_close
715 }
716 return;
717 }
718
719 /* EOF? */
720 if (len == 0) {
721 debugs(26, 4, HERE << "Closing client connection due to 0 byte read.");
722 client.conn->close();
723 return;
724 }
725
726 /* Valid data */
727 statCounter.client_http.kbytes_out += len;
728 server.dataSent(len);
729
730 /* If the other end has closed, so should we */
731 if (!Comm::IsConnOpen(server.conn)) {
732 debugs(26, 4, HERE << "Server has gone away. Terminating client connection.");
733 client.conn->close();
734 return;
735 }
736
737 CbcPointer<TunnelStateData> safetyLock(this); /* ??? should be locked by the caller... */
738
739 if (cbdataReferenceValid(this))
740 copyRead(server, ReadServer);
741 }
742
743 static void
744 tunnelTimeout(const CommTimeoutCbParams &io)
745 {
746 TunnelStateData *tunnelState = static_cast<TunnelStateData *>(io.data);
747 debugs(26, 3, HERE << io.conn);
748 /* Temporary lock to protect our own feets (comm_close -> tunnelClientClosed -> Free) */
749 CbcPointer<TunnelStateData> safetyLock(tunnelState);
750
751 tunnelState->client.closeIfOpen();
752 tunnelState->server.closeIfOpen();
753 }
754
755 void
756 TunnelStateData::Connection::closeIfOpen()
757 {
758 if (Comm::IsConnOpen(conn))
759 conn->close();
760 }
761
762 static void
763 tunnelDelayedClientRead(void *data)
764 {
765 if (!data)
766 return;
767
768 TunnelStateData *tunnel = static_cast<TunnelStateData*>(data);
769 tunnel->client.readPending = NULL;
770 static uint64_t counter=0;
771 debugs(26, 7, "Client read(2) delayed " << ++counter << " times");
772 tunnel->copyRead(tunnel->client, TunnelStateData::ReadClient);
773 }
774
775 static void
776 tunnelDelayedServerRead(void *data)
777 {
778 if (!data)
779 return;
780
781 TunnelStateData *tunnel = static_cast<TunnelStateData*>(data);
782 tunnel->server.readPending = NULL;
783 static uint64_t counter=0;
784 debugs(26, 7, "Server read(2) delayed " << ++counter << " times");
785 tunnel->copyRead(tunnel->server, TunnelStateData::ReadServer);
786 }
787
788 void
789 TunnelStateData::copyRead(Connection &from, IOCB *completion)
790 {
791 assert(from.len == 0);
792 // If only the minimum permitted read size is going to be attempted
793 // then we schedule an event to try again in a few I/O cycles.
794 // Allow at least 1 byte to be read every (0.3*10) seconds.
795 int bw = from.bytesWanted(1, SQUID_TCP_SO_RCVBUF);
796 if (bw == 1 && ++from.delayedLoops < 10) {
797 from.readPending = this;
798 eventAdd("tunnelDelayedServerRead", from.readPendingFunc, from.readPending, 0.3, true);
799 return;
800 }
801
802 AsyncCall::Pointer call = commCbCall(5,4, "TunnelBlindCopyReadHandler",
803 CommIoCbPtrFun(completion, this));
804 comm_read(from.conn, from.buf, bw, call);
805 }
806
807 void
808 TunnelStateData::readConnectResponse()
809 {
810 assert(waitingForConnectResponse());
811
812 AsyncCall::Pointer call = commCbCall(5,4, "readConnectResponseDone",
813 CommIoCbPtrFun(ReadConnectResponseDone, this));
814 comm_read(server.conn, connectRespBuf->space(),
815 server.bytesWanted(1, connectRespBuf->spaceSize()), call);
816 }
817
818 void
819 TunnelStateData::copyClientBytes()
820 {
821 if (preReadClientData.length()) {
822 size_t copyBytes = preReadClientData.length() > SQUID_TCP_SO_RCVBUF ? SQUID_TCP_SO_RCVBUF : preReadClientData.length();
823 memcpy(client.buf, preReadClientData.rawContent(), copyBytes);
824 preReadClientData.consume(copyBytes);
825 client.bytesIn(copyBytes);
826 if (keepGoingAfterRead(copyBytes, Comm::OK, 0, client, server))
827 copy(copyBytes, client, server, TunnelStateData::WriteServerDone);
828 } else
829 copyRead(client, ReadClient);
830 }
831
832 /**
833 * Set the HTTP status for this request and sets the read handlers for client
834 * and server side connections.
835 */
836 static void
837 tunnelStartShoveling(TunnelStateData *tunnelState)
838 {
839 assert(!tunnelState->waitingForConnectExchange());
840 *tunnelState->status_ptr = Http::scOkay;
841 if (tunnelState->logTag_ptr)
842 *tunnelState->logTag_ptr = LOG_TCP_TUNNEL;
843 if (cbdataReferenceValid(tunnelState)) {
844
845 // Shovel any payload already pushed into reply buffer by the server response
846 if (!tunnelState->server.len)
847 tunnelState->copyRead(tunnelState->server, TunnelStateData::ReadServer);
848 else {
849 debugs(26, DBG_DATA, "Tunnel server PUSH Payload: \n" << Raw("", tunnelState->server.buf, tunnelState->server.len) << "\n----------");
850 tunnelState->copy(tunnelState->server.len, tunnelState->server, tunnelState->client, TunnelStateData::WriteClientDone);
851 }
852
853 if (tunnelState->http.valid() && tunnelState->http->getConn() && !tunnelState->http->getConn()->inBuf.isEmpty()) {
854 SBuf * const in = &tunnelState->http->getConn()->inBuf;
855 debugs(26, DBG_DATA, "Tunnel client PUSH Payload: \n" << *in << "\n----------");
856 tunnelState->preReadClientData.append(*in);
857 in->consume(); // ConnStateData buffer accounting after the shuffle.
858 }
859 tunnelState->copyClientBytes();
860 }
861 }
862
863 /**
864 * All the pieces we need to write to client and/or server connection
865 * have been written.
866 * Call the tunnelStartShoveling to start the blind pump.
867 */
868 static void
869 tunnelConnectedWriteDone(const Comm::ConnectionPointer &conn, char *, size_t, Comm::Flag flag, int, void *data)
870 {
871 TunnelStateData *tunnelState = (TunnelStateData *)data;
872 debugs(26, 3, HERE << conn << ", flag=" << flag);
873 tunnelState->client.writer = NULL;
874
875 if (flag != Comm::OK) {
876 *tunnelState->status_ptr = Http::scInternalServerError;
877 tunnelErrorComplete(conn->fd, data, 0);
878 return;
879 }
880
881 tunnelStartShoveling(tunnelState);
882 }
883
884 /// Called when we are done writing CONNECT request to a peer.
885 static void
886 tunnelConnectReqWriteDone(const Comm::ConnectionPointer &conn, char *, size_t, Comm::Flag flag, int, void *data)
887 {
888 TunnelStateData *tunnelState = (TunnelStateData *)data;
889 debugs(26, 3, conn << ", flag=" << flag);
890 tunnelState->server.writer = NULL;
891 assert(tunnelState->waitingForConnectRequest());
892
893 if (flag != Comm::OK) {
894 *tunnelState->status_ptr = Http::scInternalServerError;
895 tunnelErrorComplete(conn->fd, data, 0);
896 return;
897 }
898
899 tunnelState->connectReqWriting = false;
900 tunnelState->connectExchangeCheckpoint();
901 }
902
903 void
904 TunnelStateData::connectExchangeCheckpoint()
905 {
906 if (waitingForConnectResponse()) {
907 debugs(26, 5, "still reading CONNECT response on " << server.conn);
908 } else if (waitingForConnectRequest()) {
909 debugs(26, 5, "still writing CONNECT request on " << server.conn);
910 } else {
911 assert(!waitingForConnectExchange());
912 debugs(26, 3, "done with CONNECT exchange on " << server.conn);
913 tunnelConnected(server.conn, this);
914 }
915 }
916
917 /*
918 * handle the write completion from a proxy request to an upstream origin
919 */
920 static void
921 tunnelConnected(const Comm::ConnectionPointer &server, void *data)
922 {
923 TunnelStateData *tunnelState = (TunnelStateData *)data;
924 debugs(26, 3, HERE << server << ", tunnelState=" << tunnelState);
925
926 if (!tunnelState->clientExpectsConnectResponse())
927 tunnelStartShoveling(tunnelState); // ssl-bumped connection, be quiet
928 else {
929 AsyncCall::Pointer call = commCbCall(5,5, "tunnelConnectedWriteDone",
930 CommIoCbPtrFun(tunnelConnectedWriteDone, tunnelState));
931 tunnelState->client.write(conn_established, strlen(conn_established), call, NULL);
932 }
933 }
934
935 static void
936 tunnelErrorComplete(int fd/*const Comm::ConnectionPointer &*/, void *data, size_t)
937 {
938 TunnelStateData *tunnelState = (TunnelStateData *)data;
939 debugs(26, 3, HERE << "FD " << fd);
940 assert(tunnelState != NULL);
941 /* temporary lock to save our own feets (comm_close -> tunnelClientClosed -> Free) */
942 CbcPointer<TunnelStateData> safetyLock(tunnelState);
943
944 if (Comm::IsConnOpen(tunnelState->client.conn))
945 tunnelState->client.conn->close();
946
947 if (Comm::IsConnOpen(tunnelState->server.conn))
948 tunnelState->server.conn->close();
949 }
950
951 static void
952 tunnelConnectDone(const Comm::ConnectionPointer &conn, Comm::Flag status, int xerrno, void *data)
953 {
954 TunnelStateData *tunnelState = (TunnelStateData *)data;
955
956 if (status != Comm::OK) {
957 debugs(26, 4, HERE << conn << ", comm failure recovery.");
958 /* At this point only the TCP handshake has failed. no data has been passed.
959 * we are allowed to re-try the TCP-level connection to alternate IPs for CONNECT.
960 */
961 debugs(26, 4, "removing server 1 of " << tunnelState->serverDestinations.size() <<
962 " from destinations (" << tunnelState->serverDestinations[0] << ")");
963 tunnelState->serverDestinations.erase(tunnelState->serverDestinations.begin());
964 time_t fwdTimeout = tunnelState->started + Config.Timeout.forward;
965 if (fwdTimeout > squid_curtime && tunnelState->serverDestinations.size() > 0) {
966 // find remaining forward_timeout available for this attempt
967 fwdTimeout -= squid_curtime;
968 if (fwdTimeout > Config.Timeout.connect)
969 fwdTimeout = Config.Timeout.connect;
970 /* Try another IP of this destination host */
971 GetMarkingsToServer(tunnelState->request.getRaw(), *tunnelState->serverDestinations[0]);
972 debugs(26, 4, HERE << "retry with : " << tunnelState->serverDestinations[0]);
973 AsyncCall::Pointer call = commCbCall(26,3, "tunnelConnectDone", CommConnectCbPtrFun(tunnelConnectDone, tunnelState));
974 Comm::ConnOpener *cs = new Comm::ConnOpener(tunnelState->serverDestinations[0], call, fwdTimeout);
975 cs->setHost(tunnelState->url);
976 AsyncJob::Start(cs);
977 } else {
978 debugs(26, 4, HERE << "terminate with error.");
979 ErrorState *err = new ErrorState(ERR_CONNECT_FAIL, Http::scServiceUnavailable, tunnelState->request.getRaw());
980 *tunnelState->status_ptr = Http::scServiceUnavailable;
981 err->xerrno = xerrno;
982 // on timeout is this still: err->xerrno = ETIMEDOUT;
983 err->port = conn->remote.port();
984 err->callback = tunnelErrorComplete;
985 err->callback_data = tunnelState;
986 errorSend(tunnelState->client.conn, err);
987 if (tunnelState->request != NULL)
988 tunnelState->request->hier.stopPeerClock(false);
989 }
990 return;
991 }
992
993 #if USE_DELAY_POOLS
994 /* no point using the delayIsNoDelay stuff since tunnel is nice and simple */
995 if (conn->getPeer() && conn->getPeer()->options.no_delay)
996 tunnelState->server.setDelayId(DelayId());
997 #endif
998
999 tunnelState->request->hier.note(conn, tunnelState->getHost());
1000
1001 tunnelState->server.conn = conn;
1002 tunnelState->request->peer_host = conn->getPeer() ? conn->getPeer()->host : NULL;
1003 comm_add_close_handler(conn->fd, tunnelServerClosed, tunnelState);
1004
1005 debugs(26, 4, HERE << "determine post-connect handling pathway.");
1006 if (conn->getPeer()) {
1007 tunnelState->request->peer_login = conn->getPeer()->login;
1008 tunnelState->request->peer_domain = conn->getPeer()->domain;
1009 tunnelState->request->flags.auth_no_keytab = conn->getPeer()->options.auth_no_keytab;
1010 tunnelState->request->flags.proxying = !(conn->getPeer()->options.originserver);
1011 } else {
1012 tunnelState->request->peer_login = NULL;
1013 tunnelState->request->peer_domain = NULL;
1014 tunnelState->request->flags.auth_no_keytab = false;
1015 tunnelState->request->flags.proxying = false;
1016 }
1017
1018 if (tunnelState->request->flags.proxying)
1019 tunnelState->connectToPeer();
1020 else {
1021 tunnelConnected(conn, tunnelState);
1022 }
1023
1024 AsyncCall::Pointer timeoutCall = commCbCall(5, 4, "tunnelTimeout",
1025 CommTimeoutCbPtrFun(tunnelTimeout, tunnelState));
1026 commSetConnTimeout(conn, Config.Timeout.read, timeoutCall);
1027 }
1028
1029 void
1030 tunnelStart(ClientHttpRequest * http)
1031 {
1032 debugs(26, 3, HERE);
1033 /* Create state structure. */
1034 TunnelStateData *tunnelState = NULL;
1035 ErrorState *err = NULL;
1036 HttpRequest *request = http->request;
1037 char *url = http->uri;
1038
1039 /*
1040 * client_addr.isNoAddr() indicates this is an "internal" request
1041 * from peer_digest.c, asn.c, netdb.c, etc and should always
1042 * be allowed. yuck, I know.
1043 */
1044
1045 if (Config.accessList.miss && !request->client_addr.isNoAddr()) {
1046 /*
1047 * Check if this host is allowed to fetch MISSES from us (miss_access)
1048 * default is to allow.
1049 */
1050 ACLFilledChecklist ch(Config.accessList.miss, request, NULL);
1051 ch.src_addr = request->client_addr;
1052 ch.my_addr = request->my_addr;
1053 if (ch.fastCheck() == ACCESS_DENIED) {
1054 debugs(26, 4, HERE << "MISS access forbidden.");
1055 err = new ErrorState(ERR_FORWARDING_DENIED, Http::scForbidden, request);
1056 http->al->http.code = Http::scForbidden;
1057 errorSend(http->getConn()->clientConnection, err);
1058 return;
1059 }
1060 }
1061
1062 debugs(26, 3, request->method << ' ' << url << ' ' << request->http_ver);
1063 ++statCounter.server.all.requests;
1064 ++statCounter.server.other.requests;
1065
1066 tunnelState = new TunnelStateData;
1067 #if USE_DELAY_POOLS
1068 tunnelState->server.setDelayId(DelayId::DelayClient(http));
1069 #endif
1070 tunnelState->url = xstrdup(url);
1071 tunnelState->request = request;
1072 tunnelState->server.size_ptr = &http->out.size;
1073 tunnelState->client.size_ptr = &http->al->http.clientRequestSz.payloadData;
1074 tunnelState->status_ptr = &http->al->http.code;
1075 tunnelState->logTag_ptr = &http->logType;
1076 tunnelState->client.conn = http->getConn()->clientConnection;
1077 tunnelState->http = http;
1078 tunnelState->al = http->al;
1079 //tunnelState->started is set in TunnelStateData ctor
1080
1081 comm_add_close_handler(tunnelState->client.conn->fd,
1082 tunnelClientClosed,
1083 tunnelState);
1084
1085 AsyncCall::Pointer timeoutCall = commCbCall(5, 4, "tunnelTimeout",
1086 CommTimeoutCbPtrFun(tunnelTimeout, tunnelState));
1087 commSetConnTimeout(tunnelState->client.conn, Config.Timeout.lifetime, timeoutCall);
1088
1089 peerSelect(&(tunnelState->serverDestinations), request, http->al,
1090 NULL,
1091 tunnelPeerSelectComplete,
1092 tunnelState);
1093 }
1094
1095 void
1096 TunnelStateData::connectToPeer()
1097 {
1098 #if USE_OPENSSL
1099 if (CachePeer *p = server.conn->getPeer()) {
1100 if (p->secure.encryptTransport) {
1101 AsyncCall::Pointer callback = asyncCall(5,4,
1102 "TunnelStateData::ConnectedToPeer",
1103 MyAnswerDialer(&TunnelStateData::connectedToPeer, this));
1104 Ssl::BlindPeerConnector *connector =
1105 new Ssl::BlindPeerConnector(request, server.conn, callback);
1106 AsyncJob::Start(connector); // will call our callback
1107 return;
1108 }
1109 }
1110 #endif
1111
1112 Security::EncryptorAnswer nil;
1113 connectedToPeer(nil);
1114 }
1115
1116 void
1117 TunnelStateData::connectedToPeer(Security::EncryptorAnswer &answer)
1118 {
1119 if (ErrorState *error = answer.error.get()) {
1120 *status_ptr = error->httpStatus;
1121 error->callback = tunnelErrorComplete;
1122 error->callback_data = this;
1123 errorSend(client.conn, error);
1124 answer.error.clear(); // preserve error for errorSendComplete()
1125 return;
1126 }
1127
1128 tunnelRelayConnectRequest(server.conn, this);
1129 }
1130
1131 static void
1132 tunnelRelayConnectRequest(const Comm::ConnectionPointer &srv, void *data)
1133 {
1134 TunnelStateData *tunnelState = (TunnelStateData *)data;
1135 assert(!tunnelState->waitingForConnectExchange());
1136 HttpHeader hdr_out(hoRequest);
1137 HttpStateFlags flags;
1138 debugs(26, 3, HERE << srv << ", tunnelState=" << tunnelState);
1139 memset(&flags, '\0', sizeof(flags));
1140 flags.proxying = tunnelState->request->flags.proxying;
1141 MemBuf mb;
1142 mb.init();
1143 mb.appendf("CONNECT %s HTTP/1.1\r\n", tunnelState->url);
1144 HttpStateData::httpBuildRequestHeader(tunnelState->request.getRaw(),
1145 NULL, /* StoreEntry */
1146 tunnelState->al, /* AccessLogEntry */
1147 &hdr_out,
1148 flags); /* flags */
1149 hdr_out.packInto(&mb);
1150 hdr_out.clean();
1151 mb.append("\r\n", 2);
1152
1153 debugs(11, 2, "Tunnel Server REQUEST: " << tunnelState->server.conn <<
1154 ":\n----------\n" << mb.buf << "\n----------");
1155
1156 AsyncCall::Pointer writeCall = commCbCall(5,5, "tunnelConnectReqWriteDone",
1157 CommIoCbPtrFun(tunnelConnectReqWriteDone,
1158 tunnelState));
1159
1160 tunnelState->server.write(mb.buf, mb.size, writeCall, mb.freeFunc());
1161 tunnelState->connectReqWriting = true;
1162
1163 tunnelState->connectRespBuf = new MemBuf;
1164 // SQUID_TCP_SO_RCVBUF: we should not accumulate more than regular I/O buffer
1165 // can hold since any CONNECT response leftovers have to fit into server.buf.
1166 // 2*SQUID_TCP_SO_RCVBUF: HttpMsg::parse() zero-terminates, which uses space.
1167 tunnelState->connectRespBuf->init(SQUID_TCP_SO_RCVBUF, 2*SQUID_TCP_SO_RCVBUF);
1168 tunnelState->readConnectResponse();
1169
1170 assert(tunnelState->waitingForConnectExchange());
1171
1172 AsyncCall::Pointer timeoutCall = commCbCall(5, 4, "tunnelTimeout",
1173 CommTimeoutCbPtrFun(tunnelTimeout, tunnelState));
1174 commSetConnTimeout(srv, Config.Timeout.read, timeoutCall);
1175 }
1176
1177 static void
1178 tunnelPeerSelectComplete(Comm::ConnectionList *peer_paths, ErrorState *err, void *data)
1179 {
1180 TunnelStateData *tunnelState = (TunnelStateData *)data;
1181
1182 if (peer_paths == NULL || peer_paths->size() < 1) {
1183 debugs(26, 3, HERE << "No paths found. Aborting CONNECT");
1184 if (!err) {
1185 err = new ErrorState(ERR_CANNOT_FORWARD, Http::scServiceUnavailable, tunnelState->request.getRaw());
1186 }
1187 *tunnelState->status_ptr = err->httpStatus;
1188 err->callback = tunnelErrorComplete;
1189 err->callback_data = tunnelState;
1190 errorSend(tunnelState->client.conn, err);
1191 return;
1192 }
1193 delete err;
1194
1195 GetMarkingsToServer(tunnelState->request.getRaw(), *tunnelState->serverDestinations[0]);
1196
1197 if (tunnelState->request != NULL)
1198 tunnelState->request->hier.startPeerClock();
1199
1200 debugs(26, 3, HERE << "paths=" << peer_paths->size() << ", p[0]={" << (*peer_paths)[0] << "}, serverDest[0]={" <<
1201 tunnelState->serverDestinations[0] << "}");
1202
1203 AsyncCall::Pointer call = commCbCall(26,3, "tunnelConnectDone", CommConnectCbPtrFun(tunnelConnectDone, tunnelState));
1204 Comm::ConnOpener *cs = new Comm::ConnOpener(tunnelState->serverDestinations[0], call, Config.Timeout.connect);
1205 cs->setHost(tunnelState->url);
1206 AsyncJob::Start(cs);
1207 }
1208
1209 CBDATA_CLASS_INIT(TunnelStateData);
1210
1211 bool
1212 TunnelStateData::noConnections() const
1213 {
1214 return !Comm::IsConnOpen(server.conn) && !Comm::IsConnOpen(client.conn);
1215 }
1216
1217 #if USE_DELAY_POOLS
1218 void
1219 TunnelStateData::Connection::setDelayId(DelayId const &newDelay)
1220 {
1221 delayId = newDelay;
1222 }
1223
1224 #endif
1225
1226 #if USE_OPENSSL
1227 void
1228 switchToTunnel(HttpRequest *request, Comm::ConnectionPointer &clientConn, Comm::ConnectionPointer &srvConn)
1229 {
1230 debugs(26,5, "Revert to tunnel FD " << clientConn->fd << " with FD " << srvConn->fd);
1231 /* Create state structure. */
1232 TunnelStateData *tunnelState = NULL;
1233 const SBuf url(request->effectiveRequestUri());
1234
1235 debugs(26, 3, request->method << " " << url << " " << request->http_ver);
1236 ++statCounter.server.all.requests;
1237 ++statCounter.server.other.requests;
1238
1239 tunnelState = new TunnelStateData;
1240 tunnelState->url = SBufToCstring(url);
1241 tunnelState->request = request;
1242 tunnelState->server.size_ptr = NULL; //Set later if Http::Stream is available
1243
1244 // Temporary static variable to store the unneeded for our case status code
1245 static int status_code = 0;
1246 tunnelState->status_ptr = &status_code;
1247 tunnelState->client.conn = clientConn;
1248
1249 ConnStateData *conn;
1250 if ((conn = request->clientConnectionManager.get())) {
1251 Http::StreamPointer context = conn->pipeline.front();
1252 if (context != nullptr && context->http != nullptr) {
1253 tunnelState->logTag_ptr = &context->http->logType;
1254 tunnelState->server.size_ptr = &context->http->out.size;
1255
1256 #if USE_DELAY_POOLS
1257 /* no point using the delayIsNoDelay stuff since tunnel is nice and simple */
1258 if (srvConn->getPeer() && srvConn->getPeer()->options.no_delay)
1259 tunnelState->server.setDelayId(DelayId::DelayClient(context->http));
1260 #endif
1261 }
1262 }
1263
1264 comm_add_close_handler(tunnelState->client.conn->fd,
1265 tunnelClientClosed,
1266 tunnelState);
1267
1268 AsyncCall::Pointer timeoutCall = commCbCall(5, 4, "tunnelTimeout",
1269 CommTimeoutCbPtrFun(tunnelTimeout, tunnelState));
1270 commSetConnTimeout(tunnelState->client.conn, Config.Timeout.lifetime, timeoutCall);
1271 fd_table[clientConn->fd].read_method = &default_read_method;
1272 fd_table[clientConn->fd].write_method = &default_write_method;
1273
1274 tunnelState->request->hier.note(srvConn, tunnelState->getHost());
1275
1276 tunnelState->server.conn = srvConn;
1277 tunnelState->request->peer_host = srvConn->getPeer() ? srvConn->getPeer()->host : NULL;
1278 comm_add_close_handler(srvConn->fd, tunnelServerClosed, tunnelState);
1279
1280 debugs(26, 4, "determine post-connect handling pathway.");
1281 if (srvConn->getPeer()) {
1282 tunnelState->request->peer_login = srvConn->getPeer()->login;
1283 tunnelState->request->peer_domain = srvConn->getPeer()->domain;
1284 tunnelState->request->flags.auth_no_keytab = srvConn->getPeer()->options.auth_no_keytab;
1285 tunnelState->request->flags.proxying = !(srvConn->getPeer()->options.originserver);
1286 } else {
1287 tunnelState->request->peer_login = NULL;
1288 tunnelState->request->peer_domain = NULL;
1289 tunnelState->request->flags.auth_no_keytab = false;
1290 tunnelState->request->flags.proxying = false;
1291 }
1292
1293 timeoutCall = commCbCall(5, 4, "tunnelTimeout",
1294 CommTimeoutCbPtrFun(tunnelTimeout, tunnelState));
1295 commSetConnTimeout(srvConn, Config.Timeout.read, timeoutCall);
1296 fd_table[srvConn->fd].read_method = &default_read_method;
1297 fd_table[srvConn->fd].write_method = &default_write_method;
1298
1299 auto ssl = fd_table[srvConn->fd].ssl.get();
1300 assert(ssl);
1301 BIO *b = SSL_get_rbio(ssl);
1302 Ssl::ServerBio *srvBio = static_cast<Ssl::ServerBio *>(b->ptr);
1303 const MemBuf &buf = srvBio->rBufData();
1304
1305 AsyncCall::Pointer call = commCbCall(5,5, "tunnelConnectedWriteDone",
1306 CommIoCbPtrFun(tunnelConnectedWriteDone, tunnelState));
1307 tunnelState->client.write(buf.content(), buf.contentSize(), call, NULL);
1308 }
1309 #endif //USE_OPENSSL
1310