whois.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section 75 WHOIS protocol */
10 
11 #include "squid.h"
12 #include "comm.h"
13 #include "comm/Read.h"
14 #include "comm/Write.h"
15 #include "errorpage.h"
16 #include "FwdState.h"
17 #include "HttpReply.h"
18 #include "HttpRequest.h"
19 #include "SquidConfig.h"
20 #include "StatCounters.h"
21 #include "Store.h"
22 #include "tools.h"
23 
24 #include <cerrno>
25 
26 #define WHOIS_PORT 43
27 
29 {
31 
32 public:
33  void readReply(const Comm::ConnectionPointer &, char *aBuffer, size_t aBufferLength, Comm::Flag flag, int xerrno);
34  void setReplyToOK(StoreEntry *sentry);
38  char buf[BUFSIZ+1]; /* readReply adds terminating NULL */
40 };
41 
43 
47 
48 /* PUBLIC */
49 
50 static void
51 whoisWriteComplete(const Comm::ConnectionPointer &, char *buf, size_t, Comm::Flag, int, void *)
52 {
53  xfree(buf);
54 }
55 
56 void
58 {
59  WhoisState *p = new WhoisState;
60  p->request = fwd->request;
61  p->entry = fwd->entry;
62  p->fwd = fwd;
63  p->dataWritten = false;
64 
65  p->entry->lock("whoisStart");
67 
68  size_t l = p->request->url.path().length() + 3;
69  char *buf = (char *)xmalloc(l);
70 
71  const SBuf str_print = p->request->url.path().substr(1);
72  snprintf(buf, l, SQUIDSBUFPH "\r\n", SQUIDSBUFPRINT(str_print));
73 
74  AsyncCall::Pointer writeCall = commCbCall(5,5, "whoisWriteComplete",
76  Comm::Write(fwd->serverConnection(), buf, strlen(buf), writeCall, NULL);
77  AsyncCall::Pointer readCall = commCbCall(5,4, "whoisReadReply",
79  comm_read(fwd->serverConnection(), p->buf, BUFSIZ, readCall);
80  AsyncCall::Pointer timeoutCall = commCbCall(5, 4, "whoisTimeout",
83 }
84 
85 /* PRIVATE */
86 
87 static void
89 {
90  WhoisState *p = static_cast<WhoisState *>(io.data);
91  debugs(75, 3, HERE << io.conn << ", URL " << p->entry->url());
92  io.conn->close();
93 }
94 
95 static void
96 whoisReadReply(const Comm::ConnectionPointer &conn, char *buf, size_t len, Comm::Flag flag, int xerrno, void *data)
97 {
98  WhoisState *p = (WhoisState *)data;
99  p->readReply(conn, buf, len, flag, xerrno);
100 }
101 
102 void
104 {
105  HttpReply *reply = new HttpReply;
106  sentry->buffer();
107  reply->setHeaders(Http::scOkay, "Gatewaying", "text/plain", -1, -1, -2);
109  sentry->replaceHttpReply(reply);
110 }
111 
112 void
113 WhoisState::readReply(const Comm::ConnectionPointer &conn, char *aBuffer, size_t aBufferLength, Comm::Flag flag, int xerrno)
114 {
115  /* Bail out early on Comm::ERR_CLOSING - close handlers will tidy up for us */
116  if (flag == Comm::ERR_CLOSING)
117  return;
118 
119  aBuffer[aBufferLength] = '\0';
120  debugs(75, 3, HERE << conn << " read " << aBufferLength << " bytes");
121  debugs(75, 5, "{" << aBuffer << "}");
122 
123  if (flag != Comm::OK) {
124  debugs(50, 2, conn << ": read failure: " << xstrerr(xerrno));
125 
126  if (ignoreErrno(xerrno)) {
127  AsyncCall::Pointer call = commCbCall(5,4, "whoisReadReply",
129  comm_read(conn, aBuffer, BUFSIZ, call);
130  } else {
132  err->xerrno = xerrno;
133  fwd->fail(err);
134  conn->close();
135  }
136  return;
137  }
138 
139  if (aBufferLength > 0) {
140  if (!dataWritten)
142 
143  statCounter.server.all.kbytes_in += aBufferLength;
144  statCounter.server.http.kbytes_in += aBufferLength;
145 
146  /* No range support, we always grab it all */
147  dataWritten = true;
148  entry->append(aBuffer, aBufferLength);
149  entry->flush();
150 
151  AsyncCall::Pointer call = commCbCall(5,4, "whoisReadReply",
153  comm_read(conn, aBuffer, BUFSIZ, call);
154  return;
155  }
156 
157  /* no bytes read. stop reading */
158  entry->timestampsSet();
159  entry->flush();
160 
161  entry->makePublic();
162 
163  fwd->complete();
164  debugs(75, 3, "whoisReadReply: Done: " << entry->url());
165  conn->close();
166 }
167 
168 static void
170 {
171  WhoisState *p = (WhoisState *)params.data;
172  debugs(75, 3, "whoisClose: FD " << params.fd);
173  p->entry->unlock("whoisClose");
174  delete p;
175 }
176 
struct StatCounters::@129 server
struct StatCounters::@129::@138 http
StatCounters statCounter
Definition: StatCounters.cc:12
int unlock(const char *context)
Definition: store.cc:485
void CTCB(const CommTimeoutCbParams &params)
Definition: CommCalls.h:39
static IOCB whoisReadReply
Definition: whois.cc:46
bool timestampsSet()
Definition: store.cc:1525
Definition: SBuf.h:87
virtual void append(char const *, int)
Appends a c-string to existing packed data.
Definition: store.cc:844
void setReplyToOK(StoreEntry *sentry)
Definition: whois.cc:103
void makePublic(const KeyScope keyScope=ksDefault)
Definition: store.cc:142
virtual void buffer()
Definition: store.cc:1707
char buf[BUFSIZ+1]
Definition: whois.cc:38
Definition: Flag.h:16
virtual void flush()
Definition: store.cc:1718
static CLCB whoisClose
Definition: whois.cc:44
HttpRequest * request
Definition: FwdState.h:138
char * p
Definition: membanger.c:43
int conn
the current server connection FD
Definition: Transport.cc:26
void readReply(const Comm::ConnectionPointer &, char *aBuffer, size_t aBufferLength, Comm::Flag flag, int xerrno)
Definition: whois.cc:113
void replaceHttpReply(HttpReply *, bool andStartWriting=true)
Definition: store.cc:1854
StoreEntry * entry
Definition: whois.cc:35
Whois server.
Definition: Message.h:43
void whoisStart(FwdState *fwd)
Definition: whois.cc:57
Comm::ConnectionPointer conn
Definition: CommCalls.h:85
const char * xstrerr(int error)
Definition: xstrerror.cc:83
void const char HLPCB void * data
Definition: stub_helper.cc:16
int fd
FD which the call was about. Set by the async call creator.
Definition: CommCalls.h:90
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
void setHeaders(Http::StatusCode status, const char *reason, const char *ctype, int64_t clen, time_t lmt, time_t expires)
Definition: HttpReply.cc:151
void complete()
Definition: FwdState.cc:513
HttpRequest::Pointer request
Definition: whois.cc:36
void comm_read(const Comm::ConnectionPointer &conn, char *buf, int len, AsyncCall::Pointer &callback)
Definition: Read.h:57
#define CBDATA_CLASS(type)
Definition: cbdata.h:302
static CTCB whoisTimeout
Definition: whois.cc:45
URL url
the request URI
Definition: HttpRequest.h:103
void path(const char *p)
Definition: URL.h:77
bool dataWritten
Definition: whois.cc:39
#define BUFSIZ
Definition: defines.h:20
Comm::ConnectionPointer const & serverConnection() const
Definition: FwdState.h:107
int unsigned int const char *desc STUB void int len
Definition: stub_fd.cc:20
const char * url() const
Definition: store.cc:1672
void CLCB(const CommCloseCbParams &params)
Definition: CommCalls.h:42
void const char * buf
Definition: stub_helper.cc:16
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:147
void IOCB(const Comm::ConnectionPointer &conn, char *, size_t size, Comm::Flag flag, int xerrno, void *data)
Definition: CommCalls.h:36
Flag
Definition: Flag.h:15
int ignoreErrno(int ierrno)
Definition: comm.cc:1477
struct StatCounters::@129::@138 all
#define xmalloc
#define CBDATA_CLASS_INIT(type)
Definition: cbdata.h:318
FwdState::Pointer fwd
Definition: whois.cc:37
struct SquidConfig::@98 Timeout
void fail(ErrorState *err)
Definition: FwdState.cc:458
CommCbFunPtrCallT< Dialer > * commCbCall(int debugSection, int debugLevel, const char *callName, const Dialer &dialer)
Definition: CommCalls.h:342
AsyncCall::Pointer comm_add_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:961
int xerrno
Definition: errorpage.h:148
static void whoisWriteComplete(const Comm::ConnectionPointer &, char *buf, size_t, Comm::Flag, int, void *)
Definition: whois.cc:51
#define SQUIDSBUFPH
Definition: SBuf.h:32
#define xfree
#define SQUIDSBUFPRINT(s)
Definition: SBuf.h:33
StoreEntry * entry
Definition: FwdState.h:137
int commSetConnTimeout(const Comm::ConnectionPointer &conn, int timeout, AsyncCall::Pointer &callback)
Definition: comm.cc:552
class SquidConfig Config
Definition: SquidConfig.cc:12
#define NULL
Definition: types.h:166
uint32_t sources
The message sources.
Definition: Message.h:99
time_t read
Definition: SquidConfig.h:102
void Write(const Comm::ConnectionPointer &conn, const char *buf, int size, AsyncCall::Pointer &callback, FREE *free_func)
Definition: Write.cc:35
void lock(const char *context)
Definition: store.cc:448

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors