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