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;
58 read_handler
= nullptr;
59 write_handler
= nullptr;
60 readMethod_
= nullptr;
61 writeMethod_
= nullptr;
64 /// Clear the fde class back to NULL equivalent.
65 void clear() { *this = fde(); }
67 /// True if comm_close for this fd has been called
68 bool closing() const { return flags
.close_request
; }
70 /// set I/O methods for a freshly opened descriptor
71 void setIo(READ_HANDLER
*, WRITE_HANDLER
*);
73 /// Use default I/O methods. When called after useBufferedIo(), the caller
74 /// is responsible for any (unread or unwritten) buffered data.
77 /// use I/O methods that maintain an internal-to-them buffer
78 void useBufferedIo(READ_HANDLER
*, WRITE_HANDLER
*);
80 int read(int fd
, char *buf
, int len
) { return readMethod_(fd
, buf
, len
); }
81 int write(int fd
, const char *buf
, int len
) { return writeMethod_(fd
, buf
, len
); }
83 /* NOTE: memset is used on fdes today. 20030715 RBC */
84 static void DumpStats(StoreEntry
*);
86 char const *remoteAddr() const;
87 void dumpStats(StoreEntry
&, int) const;
88 bool readPending(int) const;
90 /// record a transaction on this FD
91 void noteUse() { ++pconn
.uses
; }
95 /// global table of FD and their state.
98 unsigned int type
= 0;
99 unsigned short remote_port
= 0;
101 Ip::Address local_addr
;
102 tos_t tosToServer
= '\0'; /**< The TOS value for packets going towards the server.
103 See also tosFromServer. */
104 nfmark_t nfmarkToServer
= 0; /**< The netfilter mark for packets going towards the server.
105 See also nfConnmarkFromServer. */
107 char ipaddr
[MAX_IPSTRLEN
]; /* dotted decimal address of peer */
108 char desc
[FD_DESC_SZ
];
112 bool close_request
= false; ///< true if file_ or comm_close has been called
113 bool write_daemon
= false;
114 bool socket_eof
= false;
115 bool nolinger
= false;
116 bool nonblocking
= false;
118 bool called_connect
= false;
119 bool nodelay
= false;
120 bool close_on_exec
= false;
121 /// buffering readMethod_ has data to give (regardless of socket state)
122 bool read_pending
= false;
123 //bool write_pending; //XXX seems not to be used
124 bool transparent
= false;
127 int64_t bytes_read
= 0;
128 int64_t bytes_written
= 0;
131 int uses
= 0; /* ie # req's over persistent conn */
135 /// pointer to client info used in client write limiter or nullptr if not present
136 ClientInfo
* clientInfo
= nullptr;
137 MessageBucket::Pointer writeQuotaHandler
; ///< response write limiter, if configured
139 unsigned epoll_state
= 0;
143 void *read_data
= nullptr;
145 void *write_data
= nullptr;
146 AsyncCall::Pointer timeoutHandler
;
148 time_t writeStart
= 0;
149 void *lifetime_data
= nullptr;
150 AsyncCall::Pointer closeHandler
;
151 AsyncCall::Pointer halfClosedReader
; /// read handler for half-closed fds
152 Security::SessionPointer ssl
;
153 Security::ContextPointer dynamicTlsContext
; ///< cached and then freed when fd is closed
156 long handle
= (long)nullptr;
159 tos_t tosFromServer
= '\0'; /**< Stores the TOS flags of the packets from the remote server.
160 See FwdState::dispatch(). Note that this differs to
161 tosToServer in that this is the value we *receive* from the,
162 connection, whereas tosToServer is the value to set on packets
164 unsigned int nfConnmarkFromServer
= 0; /**< Stores the Netfilter mark value of the connection from the remote
165 server. See FwdState::dispatch(). Note that this differs to
166 nfmarkToServer in that this is the value we *receive* from the,
167 connection, whereas nfmarkToServer is the value to set on packets
170 // TODO: Remove: Auto-convert legacy SetSelect() callers to AsyncCalls like
171 // comm_add_close_handler(CLCB) does, making readMethod_/writeMethod_
172 // AsyncCalls and giving each read/write a dedicated context instead.
173 /// What the I/O handlers are supposed to work on.
174 CodeContextPointer codeContext
;
177 // I/O methods connect Squid to the device/stack/library fde represents
178 READ_HANDLER
*readMethod_
= nullptr; ///< imports bytes into Squid
179 WRITE_HANDLER
*writeMethod_
= nullptr; ///< exports Squid bytes
182 #define fd_table fde::Table
187 FD_READ_METHOD(int fd
, char *buf
, int len
)
189 return fd_table
[fd
].read(fd
, buf
, len
);
193 FD_WRITE_METHOD(int fd
, const char *buf
, int len
)
195 return fd_table
[fd
].write(fd
, buf
, len
);
198 #endif /* SQUID_FDE_H */