2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
12 #include "base/CodeContext.h" /* XXX: Remove by de-inlining ctor and clear() */
13 #include "base/forward.h"
16 #include "ip/Address.h"
17 #include "ip/forward.h"
18 #include "security/forward.h"
19 #include "typedefs.h" //DRCB, DWCB
22 #include "MessageBucket.h"
28 * READ_HANDLER functions return < 0 if, and only if, they fail with an error.
29 * On error, they must pass back an error code in 'errno'.
31 typedef int READ_HANDLER(int, char *, int);
34 * WRITE_HANDLER functions return < 0 if, and only if, they fail with an error.
35 * On error, they must pass back an error code in 'errno'.
37 typedef int WRITE_HANDLER(int, const char *, int);
42 _fde_disk() { wrt_handle
= nullptr; }
45 void *wrt_handle_data
= nullptr;
46 dwrite_q
*write_q
= nullptr;
47 dwrite_q
*write_q_tail
= nullptr;
56 // TODO: Merge with comm_init() to reduce initialization order dependencies.
57 /// Configures fd_table (a.k.a. fde::Table).
58 /// Call once, after learning the number of supported descriptors (i.e.
59 /// setMaxFD()) and before dereferencing fd_table (e.g., before Comm I/O).
65 read_handler
= nullptr;
66 write_handler
= nullptr;
67 readMethod_
= nullptr;
68 writeMethod_
= nullptr;
71 /// Clear the fde class back to NULL equivalent.
72 void clear() { *this = fde(); }
74 /// True if comm_close for this fd has been called
75 bool closing() const { return flags
.close_request
; }
77 /// set I/O methods for a freshly opened descriptor
78 void setIo(READ_HANDLER
*, WRITE_HANDLER
*);
80 /// Use default I/O methods. When called after useBufferedIo(), the caller
81 /// is responsible for any (unread or unwritten) buffered data.
84 /// use I/O methods that maintain an internal-to-them buffer
85 void useBufferedIo(READ_HANDLER
*, WRITE_HANDLER
*);
87 int read(int fd
, char *buf
, int len
) { return readMethod_(fd
, buf
, len
); }
88 int write(int fd
, const char *buf
, int len
) { return writeMethod_(fd
, buf
, len
); }
90 /* NOTE: memset is used on fdes today. 20030715 RBC */
91 static void DumpStats(StoreEntry
*);
93 char const *remoteAddr() const;
94 void dumpStats(StoreEntry
&, int) const;
95 bool readPending(int) const;
97 /// record a transaction on this FD
98 void noteUse() { ++pconn
.uses
; }
102 /// global table of FD and their state.
105 unsigned int type
= 0;
106 unsigned short remote_port
= 0;
108 Ip::Address local_addr
;
109 tos_t tosToServer
= '\0'; /**< The TOS value for packets going towards the server.
110 See also tosFromServer. */
111 nfmark_t nfmarkToServer
= 0; /**< The netfilter mark for packets going towards the server.
112 See also nfConnmarkFromServer. */
114 char ipaddr
[MAX_IPSTRLEN
]; /* dotted decimal address of peer */
115 char desc
[FD_DESC_SZ
];
119 bool close_request
= false; ///< true if file_ or comm_close has been called
120 bool write_daemon
= false;
121 bool socket_eof
= false;
122 bool nolinger
= false;
123 bool nonblocking
= false;
125 bool called_connect
= false;
126 bool nodelay
= false;
127 bool close_on_exec
= false;
128 /// buffering readMethod_ has data to give (regardless of socket state)
129 bool read_pending
= false;
130 //bool write_pending; //XXX seems not to be used
131 bool transparent
= false;
134 int64_t bytes_read
= 0;
135 int64_t bytes_written
= 0;
138 int uses
= 0; /* ie # req's over persistent conn */
142 /// pointer to client info used in client write limiter or nullptr if not present
143 ClientInfo
* clientInfo
= nullptr;
144 MessageBucket::Pointer writeQuotaHandler
; ///< response write limiter, if configured
146 unsigned epoll_state
= 0;
150 void *read_data
= nullptr;
152 void *write_data
= nullptr;
153 AsyncCall::Pointer timeoutHandler
;
155 time_t writeStart
= 0;
156 void *lifetime_data
= nullptr;
157 AsyncCall::Pointer closeHandler
;
158 AsyncCall::Pointer halfClosedReader
; /// read handler for half-closed fds
159 Security::SessionPointer ssl
;
160 Security::ContextPointer dynamicTlsContext
; ///< cached and then freed when fd is closed
163 long handle
= (long)nullptr;
166 tos_t tosFromServer
= '\0'; /**< Stores the TOS flags of the packets from the remote server.
167 See FwdState::dispatch(). Note that this differs to
168 tosToServer in that this is the value we *receive* from the,
169 connection, whereas tosToServer is the value to set on packets
171 unsigned int nfConnmarkFromServer
= 0; /**< Stores the Netfilter mark value of the connection from the remote
172 server. See FwdState::dispatch(). Note that this differs to
173 nfmarkToServer in that this is the value we *receive* from the,
174 connection, whereas nfmarkToServer is the value to set on packets
177 // TODO: Remove: Auto-convert legacy SetSelect() callers to AsyncCalls like
178 // comm_add_close_handler(CLCB) does, making readMethod_/writeMethod_
179 // AsyncCalls and giving each read/write a dedicated context instead.
180 /// What the I/O handlers are supposed to work on.
181 CodeContextPointer codeContext
;
184 // I/O methods connect Squid to the device/stack/library fde represents
185 READ_HANDLER
*readMethod_
= nullptr; ///< imports bytes into Squid
186 WRITE_HANDLER
*writeMethod_
= nullptr; ///< exports Squid bytes
189 #define fd_table fde::Table
194 FD_READ_METHOD(int fd
, char *buf
, int len
)
196 return fd_table
[fd
].read(fd
, buf
, len
);
200 FD_WRITE_METHOD(int fd
, const char *buf
, int len
)
202 return fd_table
[fd
].write(fd
, buf
, len
);
205 #endif /* SQUID_FDE_H */