]> git.ipfire.org Git - thirdparty/squid.git/blob - src/fde.h
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / fde.h
1 /*
2 * Copyright (C) 1996-2020 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 fde() {
56 *ipaddr = 0;
57 *desc = 0;
58 read_handler = nullptr;
59 write_handler = nullptr;
60 readMethod_ = nullptr;
61 writeMethod_ = nullptr;
62 }
63
64 /// Clear the fde class back to NULL equivalent.
65 void clear() { *this = fde(); }
66
67 /// True if comm_close for this fd has been called
68 bool closing() const { return flags.close_request; }
69
70 /// set I/O methods for a freshly opened descriptor
71 void setIo(READ_HANDLER *, WRITE_HANDLER *);
72
73 /// Use default I/O methods. When called after useBufferedIo(), the caller
74 /// is responsible for any (unread or unwritten) buffered data.
75 void useDefaultIo();
76
77 /// use I/O methods that maintain an internal-to-them buffer
78 void useBufferedIo(READ_HANDLER *, WRITE_HANDLER *);
79
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); }
82
83 /* NOTE: memset is used on fdes today. 20030715 RBC */
84 static void DumpStats(StoreEntry *);
85
86 char const *remoteAddr() const;
87 void dumpStats(StoreEntry &, int) const;
88 bool readPending(int) const;
89
90 /// record a transaction on this FD
91 void noteUse() { ++pconn.uses; }
92
93 public:
94
95 /// global table of FD and their state.
96 static fde* Table;
97
98 unsigned int type = 0;
99 unsigned short remote_port = 0;
100
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. */
106 int sock_family = 0;
107 char ipaddr[MAX_IPSTRLEN]; /* dotted decimal address of peer */
108 char desc[FD_DESC_SZ];
109
110 struct _fde_flags {
111 bool open = false;
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;
117 bool ipc = 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;
125 } flags;
126
127 int64_t bytes_read = 0;
128 int64_t bytes_written = 0;
129
130 struct {
131 int uses = 0; /* ie # req's over persistent conn */
132 } pconn;
133
134 #if USE_DELAY_POOLS
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
138 #endif
139 unsigned epoll_state = 0;
140
141 _fde_disk disk;
142 PF *read_handler;
143 void *read_data = nullptr;
144 PF *write_handler;
145 void *write_data = nullptr;
146 AsyncCall::Pointer timeoutHandler;
147 time_t timeout = 0;
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
154 #if _SQUID_WINDOWS_
155 struct {
156 long handle = (long)nullptr;
157 } win32;
158 #endif
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
163 *leaving* Squid. */
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
168 *leaving* Squid. */
169
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;
175
176 private:
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
180 };
181
182 #define fd_table fde::Table
183
184 int fdNFree(void);
185
186 inline int
187 FD_READ_METHOD(int fd, char *buf, int len)
188 {
189 return fd_table[fd].read(fd, buf, len);
190 }
191
192 inline int
193 FD_WRITE_METHOD(int fd, const char *buf, int len)
194 {
195 return fd_table[fd].write(fd, buf, len);
196 }
197
198 #endif /* SQUID_FDE_H */
199