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 "comm.h"
13 #include "defines.h"
14 #include "ip/Address.h"
15 #include "ip/forward.h"
16 #include "security/forward.h"
17 #include "typedefs.h" //DRCB, DWCB
18 
19 #if USE_DELAY_POOLS
20 class ClientInfo;
21 #endif
22 
23 /**
24  * READ_HANDLER functions return < 0 if, and only if, they fail with an error.
25  * On error, they must pass back an error code in 'errno'.
26  */
27 typedef int READ_HANDLER(int, char *, int);
28 
29 /**
30  * WRITE_HANDLER functions return < 0 if, and only if, they fail with an error.
31  * On error, they must pass back an error code in 'errno'.
32  */
33 typedef int WRITE_HANDLER(int, const char *, int);
34 
35 class dwrite_q;
36 class _fde_disk
37 {
38 public:
39     DWCB *wrt_handle;
40     void *wrt_handle_data;
41     dwrite_q *write_q;
42     dwrite_q *write_q_tail;
43     off_t offset;
_fde_disk()44     _fde_disk() { memset(this, 0, sizeof(_fde_disk)); }
45 };
46 
47 class fde
48 {
49 
50 public:
fde()51     fde() { clear(); };
52 
53     /// True if comm_close for this fd has been called
closing()54     bool closing() { return flags.close_request; }
55 
56     /* NOTE: memset is used on fdes today. 20030715 RBC */
57     static void DumpStats (StoreEntry *);
58 
59     char const *remoteAddr() const;
60     void dumpStats (StoreEntry &, int);
61     bool readPending(int);
62     void noteUse();
63 
64 public:
65 
66     /// global table of FD and their state.
67     static fde* Table;
68 
69     unsigned int type;
70     unsigned short remote_port;
71 
72     Ip::Address local_addr;
73     tos_t tosToServer;          /**< The TOS value for packets going towards the server.
74                                         See also tosFromServer. */
75     nfmark_t nfmarkToServer;    /**< The netfilter mark for packets going towards the server.
76                                         See also nfmarkFromServer. */
77     int sock_family;
78     char ipaddr[MAX_IPSTRLEN];            /* dotted decimal address of peer */
79     char desc[FD_DESC_SZ];
80 
81     struct _fde_flags {
82         bool open;
83         bool close_request; ///< true if file_ or comm_close has been called
84         bool write_daemon;
85         bool socket_eof;
86         bool nolinger;
87         bool nonblocking;
88         bool ipc;
89         bool called_connect;
90         bool nodelay;
91         bool close_on_exec;
92         bool read_pending;
93         //bool write_pending; //XXX seems not to be used
94         bool transparent;
95     } flags;
96 
97     int64_t bytes_read;
98     int64_t bytes_written;
99 
100     struct {
101         int uses;                   /* ie # req's over persistent conn */
102     } pconn;
103 
104 #if USE_DELAY_POOLS
105     ClientInfo * clientInfo;/* pointer to client info used in client write limiter or NULL if not present */
106 #endif
107     unsigned epoll_state;
108 
109     _fde_disk disk;
110     PF *read_handler;
111     void *read_data;
112     PF *write_handler;
113     void *write_data;
114     AsyncCall::Pointer timeoutHandler;
115     time_t timeout;
116     time_t writeStart;
117     void *lifetime_data;
118     AsyncCall::Pointer closeHandler;
119     AsyncCall::Pointer halfClosedReader; /// read handler for half-closed fds
120     READ_HANDLER *read_method;
121     WRITE_HANDLER *write_method;
122     Security::SessionPointer ssl;
123     Security::ContextPointer dynamicTlsContext; ///< cached and then freed when fd is closed
124 #if _SQUID_WINDOWS_
125     struct {
126         long handle;
127     } win32;
128 #endif
129     tos_t tosFromServer;                /**< Stores the TOS flags of the packets from the remote server.
130                                             See FwdState::dispatch(). Note that this differs to
131                                             tosToServer in that this is the value we *receive* from the,
132                                             connection, whereas tosToServer is the value to set on packets
133                                             *leaving* Squid.  */
134     unsigned int nfmarkFromServer;      /**< Stores the Netfilter mark value of the connection from the remote
135                                             server. See FwdState::dispatch(). Note that this differs to
136                                             nfmarkToServer in that this is the value we *receive* from the,
137                                             connection, whereas nfmarkToServer is the value to set on packets
138                                             *leaving* Squid.   */
139 
140     /** Clear the fde class back to NULL equivalent. */
clear()141     inline void clear() {
142         type = 0;
143         remote_port = 0;
144         local_addr.setEmpty();
145         tosToServer = '\0';
146         nfmarkToServer = 0;
147         sock_family = 0;
148         memset(ipaddr, '\0', MAX_IPSTRLEN);
149         memset(desc,'\0',FD_DESC_SZ);
150         memset(&flags,0,sizeof(_fde_flags));
151         bytes_read = 0;
152         bytes_written = 0;
153         pconn.uses = 0;
154 #if USE_DELAY_POOLS
155         clientInfo = NULL;
156 #endif
157         epoll_state = 0;
158         read_handler = NULL;
159         read_data = NULL;
160         write_handler = NULL;
161         write_data = NULL;
162         timeoutHandler = NULL;
163         timeout = 0;
164         writeStart = 0;
165         lifetime_data = NULL;
166         closeHandler = NULL;
167         halfClosedReader = NULL;
168         read_method = NULL;
169         write_method = NULL;
170         ssl.reset();
171         dynamicTlsContext.reset();
172 #if _SQUID_WINDOWS_
173         win32.handle = (long)NULL;
174 #endif
175         tosFromServer = '\0';
176         nfmarkFromServer = 0;
177     }
178 };
179 
180 #define fd_table fde::Table
181 
182 int fdNFree(void);
183 
184 #define FD_READ_METHOD(fd, buf, len) (*fd_table[fd].read_method)(fd, buf, len)
185 #define FD_WRITE_METHOD(fd, buf, len) (*fd_table[fd].write_method)(fd, buf, len)
186 
187 #endif /* SQUID_FDE_H */
188 
189