1 /*
2  * Copyright (C) 1996-2021 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 #include "squid.h"
10 #include "anyp/PortCfg.h"
11 #include "comm/Connection.h"
12 #include "CommCalls.h"
13 #include "fde.h"
14 #include "globals.h"
15 
16 /* CommCommonCbParams */
17 
CommCommonCbParams(void * aData)18 CommCommonCbParams::CommCommonCbParams(void *aData):
19     data(cbdataReference(aData)), conn(), flag(Comm::OK), xerrno(0), fd(-1)
20 {
21 }
22 
CommCommonCbParams(const CommCommonCbParams & p)23 CommCommonCbParams::CommCommonCbParams(const CommCommonCbParams &p):
24     data(cbdataReference(p.data)), conn(p.conn), flag(p.flag), xerrno(p.xerrno), fd(p.fd)
25 {
26 }
27 
~CommCommonCbParams()28 CommCommonCbParams::~CommCommonCbParams()
29 {
30     cbdataReferenceDone(data);
31 }
32 
33 void
print(std::ostream & os) const34 CommCommonCbParams::print(std::ostream &os) const
35 {
36     if (conn != NULL)
37         os << conn;
38     else
39         os << "FD " << fd;
40 
41     if (xerrno)
42         os << ", errno=" << xerrno;
43     if (flag != Comm::OK)
44         os << ", flag=" << flag;
45     if (data)
46         os << ", data=" << data;
47 }
48 
49 /* CommAcceptCbParams */
50 
CommAcceptCbParams(void * aData)51 CommAcceptCbParams::CommAcceptCbParams(void *aData):
52     CommCommonCbParams(aData), xaction()
53 {
54 }
55 
56 void
print(std::ostream & os) const57 CommAcceptCbParams::print(std::ostream &os) const
58 {
59     CommCommonCbParams::print(os);
60 
61     if (xaction != NULL)
62         os << ", " << xaction->id;
63 }
64 
65 /* CommConnectCbParams */
66 
CommConnectCbParams(void * aData)67 CommConnectCbParams::CommConnectCbParams(void *aData):
68     CommCommonCbParams(aData)
69 {
70 }
71 
72 bool
syncWithComm()73 CommConnectCbParams::syncWithComm()
74 {
75     // drop the call if the call was scheduled before comm_close but
76     // is being fired after comm_close
77     if (fd >= 0 && fd_table[fd].closing()) {
78         debugs(5, 3, HERE << "dropping late connect call: FD " << fd);
79         return false;
80     }
81     return true; // now we are in sync and can handle the call
82 }
83 
84 /* CommIoCbParams */
85 
CommIoCbParams(void * aData)86 CommIoCbParams::CommIoCbParams(void *aData): CommCommonCbParams(aData),
87     buf(NULL), size(0)
88 {
89 }
90 
91 bool
syncWithComm()92 CommIoCbParams::syncWithComm()
93 {
94     // change parameters if the call was scheduled before comm_close but
95     // is being fired after comm_close
96     if ((conn->fd < 0 || fd_table[conn->fd].closing()) && flag != Comm::ERR_CLOSING) {
97         debugs(5, 3, HERE << "converting late call to Comm::ERR_CLOSING: " << conn);
98         flag = Comm::ERR_CLOSING;
99     }
100     return true; // now we are in sync and can handle the call
101 }
102 
103 void
print(std::ostream & os) const104 CommIoCbParams::print(std::ostream &os) const
105 {
106     CommCommonCbParams::print(os);
107     if (buf) {
108         os << ", size=" << size;
109         os << ", buf=" << (void*)buf;
110     }
111 }
112 
113 /* CommCloseCbParams */
114 
CommCloseCbParams(void * aData)115 CommCloseCbParams::CommCloseCbParams(void *aData):
116     CommCommonCbParams(aData)
117 {
118 }
119 
120 /* CommTimeoutCbParams */
121 
CommTimeoutCbParams(void * aData)122 CommTimeoutCbParams::CommTimeoutCbParams(void *aData):
123     CommCommonCbParams(aData)
124 {
125 }
126 
127 /* FdeCbParams */
128 
FdeCbParams(void * aData)129 FdeCbParams::FdeCbParams(void *aData):
130     CommCommonCbParams(aData)
131 {
132 }
133 
134 /* CommAcceptCbPtrFun */
135 
CommAcceptCbPtrFun(IOACB * aHandler,const CommAcceptCbParams & aParams)136 CommAcceptCbPtrFun::CommAcceptCbPtrFun(IOACB *aHandler,
137                                        const CommAcceptCbParams &aParams):
138     CommDialerParamsT<CommAcceptCbParams>(aParams),
139     handler(aHandler)
140 {
141 }
142 
CommAcceptCbPtrFun(const CommAcceptCbPtrFun & o)143 CommAcceptCbPtrFun::CommAcceptCbPtrFun(const CommAcceptCbPtrFun &o):
144     CommDialerParamsT<CommAcceptCbParams>(o.params),
145     handler(o.handler)
146 {
147 }
148 
149 void
dial()150 CommAcceptCbPtrFun::dial()
151 {
152     handler(params);
153 }
154 
155 void
print(std::ostream & os) const156 CommAcceptCbPtrFun::print(std::ostream &os) const
157 {
158     os << '(';
159     params.print(os);
160     os << ')';
161 }
162 
163 /* CommConnectCbPtrFun */
164 
CommConnectCbPtrFun(CNCB * aHandler,const CommConnectCbParams & aParams)165 CommConnectCbPtrFun::CommConnectCbPtrFun(CNCB *aHandler,
166         const CommConnectCbParams &aParams):
167     CommDialerParamsT<CommConnectCbParams>(aParams),
168     handler(aHandler)
169 {
170 }
171 
172 void
dial()173 CommConnectCbPtrFun::dial()
174 {
175     handler(params.conn, params.flag, params.xerrno, params.data);
176 }
177 
178 void
print(std::ostream & os) const179 CommConnectCbPtrFun::print(std::ostream &os) const
180 {
181     os << '(';
182     params.print(os);
183     os << ')';
184 }
185 
186 /* CommIoCbPtrFun */
187 
CommIoCbPtrFun(IOCB * aHandler,const CommIoCbParams & aParams)188 CommIoCbPtrFun::CommIoCbPtrFun(IOCB *aHandler, const CommIoCbParams &aParams):
189     CommDialerParamsT<CommIoCbParams>(aParams),
190     handler(aHandler)
191 {
192 }
193 
194 void
dial()195 CommIoCbPtrFun::dial()
196 {
197     handler(params.conn, params.buf, params.size, params.flag, params.xerrno, params.data);
198 }
199 
200 void
print(std::ostream & os) const201 CommIoCbPtrFun::print(std::ostream &os) const
202 {
203     os << '(';
204     params.print(os);
205     os << ')';
206 }
207 
208 /* CommCloseCbPtrFun */
209 
CommCloseCbPtrFun(CLCB * aHandler,const CommCloseCbParams & aParams)210 CommCloseCbPtrFun::CommCloseCbPtrFun(CLCB *aHandler,
211                                      const CommCloseCbParams &aParams):
212     CommDialerParamsT<CommCloseCbParams>(aParams),
213     handler(aHandler)
214 {
215 }
216 
217 void
dial()218 CommCloseCbPtrFun::dial()
219 {
220     handler(params);
221 }
222 
223 void
print(std::ostream & os) const224 CommCloseCbPtrFun::print(std::ostream &os) const
225 {
226     os << '(';
227     params.print(os);
228     os << ')';
229 }
230 
231 /* CommTimeoutCbPtrFun */
232 
CommTimeoutCbPtrFun(CTCB * aHandler,const CommTimeoutCbParams & aParams)233 CommTimeoutCbPtrFun::CommTimeoutCbPtrFun(CTCB *aHandler,
234         const CommTimeoutCbParams &aParams):
235     CommDialerParamsT<CommTimeoutCbParams>(aParams),
236     handler(aHandler)
237 {
238 }
239 
240 void
dial()241 CommTimeoutCbPtrFun::dial()
242 {
243     handler(params);
244 }
245 
246 void
print(std::ostream & os) const247 CommTimeoutCbPtrFun::print(std::ostream &os) const
248 {
249     os << '(';
250     params.print(os);
251     os << ')';
252 }
253 
254 /* FdeCbPtrFun */
255 
FdeCbPtrFun(FDECB * aHandler,const FdeCbParams & aParams)256 FdeCbPtrFun::FdeCbPtrFun(FDECB *aHandler, const FdeCbParams &aParams) :
257     CommDialerParamsT<FdeCbParams>(aParams),
258     handler(aHandler)
259 {
260 }
261 
262 void
dial()263 FdeCbPtrFun::dial()
264 {
265     handler(params);
266 }
267 
268 void
print(std::ostream & os) const269 FdeCbPtrFun::print(std::ostream &os) const
270 {
271     os << '(';
272     params.print(os);
273     os << ')';
274 }
275 
276