]> git.ipfire.org Git - thirdparty/squid.git/blob - src/fde.h
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / fde.h
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 #ifndef SQUID_FDE_H
10 #define SQUID_FDE_H
11
12 #include "base/CodeContext.h" /* XXX: Remove by de-inlining ctor and clear() */
13 #include "base/forward.h"
14 #include "comm.h"
15 #include "defines.h"
16 #include "ip/Address.h"
17 #include "ip/forward.h"
18 #include "security/forward.h"
19 #include "typedefs.h" //DRCB, DWCB
20
21 #if USE_DELAY_POOLS
22 #include "MessageBucket.h"
23 class ClientInfo;
24 #endif
25 class dwrite_q;
26
27 /**
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'.
30 */
31 typedef int READ_HANDLER(int, char *, int);
32
33 /**
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'.
36 */
37 typedef int WRITE_HANDLER(int, const char *, int);
38
39 class _fde_disk
40 {
41 public:
42 _fde_disk() { wrt_handle = nullptr; }
43
44 DWCB *wrt_handle;
45 void *wrt_handle_data = nullptr;
46 dwrite_q *write_q = nullptr;
47 dwrite_q *write_q_tail = nullptr;
48 off_t offset = 0;
49 };
50
51 class fde
52 {
53
54 public:
55
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).
60 static void Init();
61
62 fde() {
63 *ipaddr = 0;
64 *desc = 0;
65 read_handler = nullptr;
66 write_handler = nullptr;
67 readMethod_ = nullptr;
68 writeMethod_ = nullptr;
69 }
70
71 /// Clear the fde class back to NULL equivalent.
72 void clear() { *this = fde(); }
73
74 /// True if comm_close for this fd has been called
75 bool closing() const { return flags.close_request; }
76
77 /// set I/O methods for a freshly opened descriptor
78 void setIo(READ_HANDLER *, WRITE_HANDLER *);
79
80 /// Use default I/O methods. When called after useBufferedIo(), the caller
81 /// is responsible for any (unread or unwritten) buffered data.
82 void useDefaultIo();
83
84 /// use I/O methods that maintain an internal-to-them buffer
85 void useBufferedIo(READ_HANDLER *, WRITE_HANDLER *);
86
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); }
89
90 /* NOTE: memset is used on fdes today. 20030715 RBC */
91 static void DumpStats(StoreEntry *);
92
93 char const *remoteAddr() const;
94 void dumpStats(StoreEntry &, int) const;
95 bool readPending(int) const;
96
97 /// record a transaction on this FD
98 void noteUse() { ++pconn.uses; }
99
100 public:
101
102 /// global table of FD and their state.
103 static fde* Table;
104
105 unsigned int type = 0;
106 unsigned short remote_port = 0;
107
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. */
113 int sock_family = 0;
114 char ipaddr[MAX_IPSTRLEN]; /* dotted decimal address of peer */
115 char desc[FD_DESC_SZ];
116
117 struct _fde_flags {
118 bool open = false;
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;
124 bool ipc = 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;
132 } flags;
133
134 int64_t bytes_read = 0;
135 int64_t bytes_written = 0;
136
137 struct {
138 int uses = 0; /* ie # req's over persistent conn */
139 } pconn;
140
141 #if USE_DELAY_POOLS
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
145 #endif
146 unsigned epoll_state = 0;
147
148 _fde_disk disk;
149 PF *read_handler;
150 void *read_data = nullptr;
151 PF *write_handler;
152 void *write_data = nullptr;
153 AsyncCall::Pointer timeoutHandler;
154 time_t timeout = 0;
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
161 #if _SQUID_WINDOWS_
162 struct {
163 long handle = (long)nullptr;
164 } win32;
165 #endif
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
170 *leaving* Squid. */
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
175 *leaving* Squid. */
176
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;
182
183 private:
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
187 };
188
189 #define fd_table fde::Table
190
191 int fdNFree(void);
192
193 inline int
194 FD_READ_METHOD(int fd, char *buf, int len)
195 {
196 return fd_table[fd].read(fd, buf, len);
197 }
198
199 inline int
200 FD_WRITE_METHOD(int fd, const char *buf, int len)
201 {
202 return fd_table[fd].write(fd, buf, len);
203 }
204
205 #endif /* SQUID_FDE_H */
206