1 /*
2  * ProFTPD - FTP server daemon
3  * Copyright (c) 1997, 1998 Public Flood Software
4  * Copyright (c) 1999, 2000 MacGyver aka Habeeb J. Dihu <macgyver@tos.net>
5  * Copyright (c) 2001-2020 The ProFTPD Project team
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
20  *
21  * As a special exemption, Public Flood Software/MacGyver aka Habeeb J. Dihu
22  * and other respective copyright holders give permission to link this program
23  * with OpenSSL, and distribute the resulting executable, without including
24  * the source code for OpenSSL in the source distribution.
25  */
26 
27 /* Network IO stream layer */
28 
29 #ifndef PR_NETIO_H
30 #define PR_NETIO_H
31 
32 /* Network I/O stream types */
33 
34 /* This indicates that the netio being registered should be used when
35  * performing network I/O for the control connection.
36  */
37 #define PR_NETIO_STRM_CTRL		0x00010
38 
39 /* This indicates that the netio being registered should be used when
40  * performing network I/O for the data connection.
41  */
42 #define PR_NETIO_STRM_DATA		0x00020
43 
44 /* This indicates that the netio being registered should be used when
45  * performing network I/O for other connections (e.g. RFC931 lookups).
46  * This is rarely used.
47  */
48 #define PR_NETIO_STRM_OTHR		0x00040
49 
50 /* Network I/O stream direction */
51 #define PR_NETIO_IO_RD		1
52 #define PR_NETIO_IO_WR		2
53 
54 /* Network I/O stream session flags */
55 
56 /* This indicates that netio functions are allowed to be interrupted by
57  * EINTR, and to return -2.
58  */
59 #define PR_NETIO_SESS_INTR		(1 << 1)
60 
61 /* This is a temporary internal flag used to indicate that I/O on a
62  * network stream has been aborted, and should return -2 at the next
63  * possible instant.  In combination with NETIO_INTR and interruptible
64  * syscalls, this should be near instantly.  This flag cannot be tested
65  * for as it is cleared immediately after being detected.
66  */
67 #define PR_NETIO_SESS_ABORT	(1 << 2)
68 
69 /* Network I/O objects */
70 
71 typedef struct {
72 
73   /* Pointer to the buffer memory. */
74   char *buf;
75 
76   /* Total length of the buffer. */
77   unsigned long buflen;
78 
79   /* Pointer to the current byte in the buffer. */
80   char *current;
81 
82   /* Number of bytes left in the buffer. */
83   size_t remaining;
84 
85 } pr_buffer_t;
86 
87 typedef struct netio_rec pr_netio_t;
88 
89 typedef struct {
90 
91   /* Memory pool for this object. */
92   struct pool_rec *strm_pool;
93 
94   /* Stream type */
95   int strm_type;
96 
97   /* File descriptor for this I/O stream. */
98   int strm_fd;
99 
100   /* I/O mode: PR_NETIO_IO_RD or PR_NETIO_IO_WR.  Patterned after
101    * open(2).
102    */
103   int strm_mode;
104 
105   /* Poll interval for this stream. */
106   unsigned int strm_interval;
107 
108   /* Internal use. */
109   volatile unsigned long strm_flags;
110 
111   /* Buffer. */
112   pr_buffer_t *strm_buf;
113 
114   /* Arbitrary data for outside use. */
115   void *strm_data;
116 
117   /* errno, if applicable. */
118   int strm_errno;
119 
120   /* Private data for passing/retaining among modules. */
121   pr_table_t *notes;
122 
123   /* Pointer to the NetIO which opened this stream. */
124   pr_netio_t *strm_netio;
125 
126 } pr_netio_stream_t;
127 
128 #define PR_NETIO_ERRNO(s)	((s)->strm_errno)
129 #define PR_NETIO_FD(s)		((s)->strm_fd)
130 
131 struct netio_rec {
132   /* Memory pool for this object. */
133   struct pool_rec *pool;
134 
135   /* NetIO callbacks */
136   void (*abort)(pr_netio_stream_t *);
137   int (*close)(pr_netio_stream_t *);
138   pr_netio_stream_t *(*open)(pr_netio_stream_t *, int, int);
139   int (*poll)(pr_netio_stream_t *);
140   int (*postopen)(pr_netio_stream_t *);
141   int (*read)(pr_netio_stream_t *, char *, size_t);
142   pr_netio_stream_t *(*reopen)(pr_netio_stream_t *, int, int);
143   int (*shutdown)(pr_netio_stream_t *, int);
144   int (*write)(pr_netio_stream_t *, char *, size_t);
145 
146   /* Registering/owning module */
147   module *owner;
148   const char *owner_name;
149 };
150 
151 /* Network IO function prototypes */
152 pr_buffer_t *pr_netio_buffer_alloc(pr_netio_stream_t *nstrm);
153 
154 void pr_netio_abort(pr_netio_stream_t *);
155 int pr_netio_lingering_abort(pr_netio_stream_t *, long);
156 
157 int pr_netio_close(pr_netio_stream_t *);
158 int pr_netio_lingering_close(pr_netio_stream_t *, long);
159 #define NETIO_LINGERING_CLOSE_FL_NO_SHUTDOWN	0x00001
160 
161 char *pr_netio_gets(char *, size_t, pr_netio_stream_t *);
162 
163 pr_netio_stream_t *pr_netio_open(pool *, int, int, int);
164 
165 int pr_netio_postopen(pr_netio_stream_t *);
166 
167 int pr_netio_printf(pr_netio_stream_t *, const char *, ...);
168 int pr_netio_vprintf(pr_netio_stream_t *, const char *, va_list);
169 
170 /* pr_netio_printf_async() is for use inside alarm handlers, where no
171  * pr_netio_poll() blocking is allowed.  This is necessary because otherwise,
172  * pr_netio_poll() can potentially hang forever if the send queue is maxed and
173  * the socket has been closed.
174  */
175 int pr_netio_printf_async(pr_netio_stream_t *, char *,...);
176 
177 /* pr_netio_poll() is needed instead of simply blocking read/write because
178  * there is a race condition if the syscall _should_ be interrupted inside
179  * read(), or write(), but the signal is received before we actually hit the
180  * read or write call.  select() alleviates this problem by timing out
181  * (configurable by pr_netio_set_poll_interval()), restarting the syscall if
182  * PR_NETIO_SESS_INTR is not set, or returning if it is set and we were
183  * interrupted by a signal.  If after the timeout PR_NETIO_SESS_ABORT is set
184  * (presumably by a signal handler) or PR_NETIO_SESS_INTR & errno == EINTR,
185  * we return 1.  Otherwise, return zero when data is available, or -1 on
186  * other errors.
187  */
188 int pr_netio_poll(pr_netio_stream_t *);
189 
190 /* Read, from the given stream, into the buffer the requested size_t number
191  * of bytes.  The last argument is the minimum number of bytes to read before
192  * returning 1 (or greater).
193  */
194 int pr_netio_read(pr_netio_stream_t *, char *, size_t, int);
195 
196 pr_netio_stream_t *pr_netio_reopen(pr_netio_stream_t *, int, int);
197 
198 int pr_netio_shutdown(pr_netio_stream_t *, int);
199 
200 /* pr_netio_telnet_gets() is exactly like pr_netio_gets(), except a few special
201  * telnet characters are handled (which takes care of the [IAC]ABOR
202  * command, and odd clients
203  */
204 char *pr_netio_telnet_gets(char *, size_t, pr_netio_stream_t *,
205   pr_netio_stream_t *);
206 
207 /* Similar to pr_netio_telnet_gets(), except that it returns the number of
208  * bytes stored in the given buffer, or -1 if there was an error.
209  */
210 int pr_netio_telnet_gets2(char *, size_t, pr_netio_stream_t *,
211   pr_netio_stream_t *);
212 
213 int pr_netio_write(pr_netio_stream_t *, char *, size_t);
214 
215 /* This is a bit odd, because io_ functions are opaque, we can't be sure
216  * we are dealing with a conn_t or that it is in O_NONBLOCK mode.  Trying
217  * to do this without O_NONBLOCK would cause the kernel itself to block
218  * here, and thus invalidate the whole principal.  Instead we save
219  * the flags and put the fd in O_NONBLOCK mode.
220  */
221 int pr_netio_write_async(pr_netio_stream_t *, char *, size_t);
222 
223 void pr_netio_reset_poll_interval(pr_netio_stream_t *);
224 void pr_netio_set_poll_interval(pr_netio_stream_t *, unsigned int);
225 
226 /* Allocate a NetIO object, and set all of its NetIO callbacks to their
227  * default handlers.
228  */
229 pr_netio_t *pr_alloc_netio(pool *);
230 pr_netio_t *pr_alloc_netio2(pool *, module *, const char *);
231 
232 /* Register the given NetIO object and all its callbacks for the network
233  * I/O layer's use.  If given a NULL argument, it will automatically
234  * instantiate and register the default NetIO object.
235  */
236 int pr_register_netio(pr_netio_t *, int);
237 
238 /* Unregister the NetIO objects indicated by strm_types. */
239 int pr_unregister_netio(int);
240 
241 /* Peek at the NetIO registered for the given stream type. */
242 pr_netio_t *pr_get_netio(int);
243 
244 /* Initialize the network I/O layer. */
245 void init_netio(void);
246 
247 #endif /* PR_NETIO_H */
248