io.h 10 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
/* io.h
 *
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Niels Mller
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
J.H.M. Dassen's avatar
J.H.M. Dassen committed
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Niels Möller's avatar
Niels Möller committed
24
25
26
27
28
 */

#ifndef LSH_IO_H_INCLUDED
#define LSH_IO_H_INCLUDED

Niels Möller's avatar
Niels Möller committed
29
#include "abstract_io.h"
30
#include "command.h"
31
#include "resource.h"
Niels Möller's avatar
Niels Möller committed
32
33
#include "write_buffer.h"

Niels Möller's avatar
Niels Möller committed
34
35
#include <time.h>
#include <netdb.h>
36
37
/* For sig_atomic_t */
#include <signal.h>
38
#include <sys/types.h>
39
#include <sys/socket.h>
Niels Möller's avatar
Niels Möller committed
40
#include <netinet/in.h>
Niels Möller's avatar
Niels Möller committed
41

Niels Möller's avatar
Niels Möller committed
42

43
#define GABA_DECLARE
44
#include "io.h.x"
45
#undef GABA_DECLARE
46

47

48
49
50
/* Declare the class object, so that io_commands.c can refer to it for
 * type-checking. */
extern struct lsh_class io_backend_class;
51
52
53
54
55
56
57
58
59

/* GABA:
   (class
     (name lsh_callback)
     (vars
       (f method void)))
*/

#define LSH_CALLBACK(c) ((c)->f((c)))
60

61
62
/* The fd io callback is a closure, in order to support different
 * reading styles (buffered and consuming). Also used for writing. */
Niels Möller's avatar
Niels Möller committed
63
64
65

/* GABA:
   (class
66
     (name io_callback)
Niels Möller's avatar
Niels Möller committed
67
     (vars
68
       (f method void "struct lsh_fd *fd")))
Niels Möller's avatar
Niels Möller committed
69
70
*/

71
#define IO_CALLBACK(c, fd) ((c)->f((c), (fd)))
Niels Möller's avatar
Niels Möller committed
72

73
/* GABA:
74
75
   (class
     (name lsh_fd)
76
     (super resource)
77
78
     (vars
       (next object lsh_fd)
Niels Möller's avatar
Niels Möller committed
79
       (fd . int)
80

81
82
83
       ; For debugging purposes
       (label . "const char *")
       
84
85
86
       ;; (backend object io_backend)
       ;; (next_closed object lsh_fd)
       
87
       ; Used for raising i/o-exceptions.
Niels Möller's avatar
Niels Möller committed
88
89
       ; Also passed on to readers of the consuming type,
       ; which seems kind of bogus.
Niels Möller's avatar
Niels Möller committed
90
91
       (e object exception_handler)
       
92
       ; User's close callback
93
       (close_callback object lsh_callback)
94
95
96

       ; Called before poll
       (prepare method void)
97

98
99
100
101
102
       ; This flag is set by the backend if it detects that a
       ; connection is hanged up. 
       
       (hanged_up . int)
       
Niels Möller's avatar
Niels Möller committed
103
       (want_read . int)
104
       ; Called if poll indicates that data can be read. 
105
       (read object io_callback)
106

Niels Möller's avatar
Niels Möller committed
107
       (want_write . int)
108
       ; Called if poll indicates that data can be written.
109
       (write object io_callback)
110

111
       ; NOTE: We could put write_buffer inside the write callback,
112
113
114
115
116
117
118
       ; but it seems simpler to keep it here, as it is needed by the
       ; prepare and write_close methods.
       (write_buffer object write_buffer)
       
       ; Called to when fd is closed for writing.
       (write_close method void)))
*/
119

120
121
122
123
#define FD_PREPARE(fd) ((fd)->prepare(fd))
#define FD_READ(fd) IO_CALLBACK((fd)->read, (fd))
#define FD_WRITE(fd) IO_CALLBACK((fd)->write, (fd))
#define FD_WRITE_CLOSE(fd) ((fd)->write_close(fd))
Niels Möller's avatar
Niels Möller committed
124

125

Niels Möller's avatar
Niels Möller committed
126
127
128
129
130
131
132
/* Used for read handlers like read_line and read_packet that
 * processes a little data at a time, possibly replacing the handler
 * and leaving some data for the new one. */

/* GABA:
   (class
     (name io_buffered_read)
133
     (super io_callback)
Niels Möller's avatar
Niels Möller committed
134
135
136
137
138
     (vars
       (buffer_size . UINT32)
       (handler object read_handler)))
*/

139
struct io_callback *
Niels Möller's avatar
Niels Möller committed
140
141
142
make_buffered_read(UINT32 buffer_size,
		   struct read_handler *handler);

Niels Möller's avatar
Niels Möller committed
143
144
145
146
147
148
/* Used for read handlers like read_data, that know how much data they
 * can consume. */

/* GABA:
   (class
     (name io_consuming_read)
149
     (super io_callback)
Niels Möller's avatar
Niels Möller committed
150
151
152
153
154
155
     (vars
       (query method UINT32)
       ; Returns the maximum number of octets that
       ; can be consumed immediately.
       (consumer object abstract_write)))
*/
Niels Möller's avatar
Niels Möller committed
156
157
158
159
160
161

#define READ_QUERY(r) ((r)->query((r)))

void init_consuming_read(struct io_consuming_read *self,
			 struct abstract_write *consumer);

162
163
164
165
166
167
/* Passed to the listen callback, and to other functions and commands
 * dealing with addresses. */
/* GABA:
   (class
     (name address_info)
     (vars
168
169
170
171
       ; An ipnumber, in decimal dot notation, ipv6 format, or
       ; a dns name.
       (ip string)
       ; The port number here is always in host byte order
172
       (port . UINT32))) */
173

174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* Used for listening and connecting to local sockets.
 * Both strings have to be NUL-terminated. */

/* GABA:
   (class
     (name local_info)
     (vars
       (directory string)
       (name string)))
*/

struct local_info *
make_local_info(struct lsh_string *directory,
		struct lsh_string *name);
     
/* Returned by listen. And also by connect, so this is an improper name.
 * Functions related to AF_UNIX sockets leave the peer field as NULL. */
191
/* GABA:
192
193
194
195
196
197
198
199
200
201
202
   (class
     (name listen_value)
     (vars
       (fd object lsh_fd)
       (peer object address_info)))
*/

struct listen_value *
make_listen_value(struct lsh_fd *fd,
		  struct address_info *peer);

203
204
205
206
207
208
/* I/O exceptions */
/* GABA:
   (class
     (name io_exception)
     (super exception)
     (vars
209
       ; NULL if no fd was involved
210
       (fd object lsh_fd)
211
       ; errno code, or zero if not available
212
213
214
215
216
217
218
219
220
221
222
       (error . int))))
*/

/* If msg is NULL, it is derived from errno */
struct exception *
make_io_exception(UINT32 type, struct lsh_fd *fd, int error, const char *msg);

/* Used in cases where the fd and errno are not available */
#define STATIC_IO_EXCEPTION(type, name) \
{ { STATIC_HEADER, (type), (name) }, NULL, 0}

223
224
extern const struct exception finish_read_exception;
extern const struct exception finish_io_exception;
Niels Möller's avatar
Niels Möller committed
225

226
227
struct io_backend *
make_io_backend(void);
Niels Möller's avatar
Niels Möller committed
228

Niels Möller's avatar
Niels Möller committed
229
int io_iter(struct io_backend *b);
Niels Möller's avatar
Niels Möller committed
230
231
void io_run(struct io_backend *b);

232
233
234
void
io_final(struct io_backend *b);

235
236
struct resource *
io_signal_handler(struct io_backend *b,
Niels Möller's avatar
Added    
Niels Möller committed
237
		  volatile sig_atomic_t *flag,
238
239
240
241
242
243
244
		  struct lsh_callback *action);

struct resource *
io_callout(struct io_backend *b,
	   UINT32 delay,
	   struct lsh_callback *action);

245
246
int blocking_read(int fd, struct read_handler *r);

Niels Möller's avatar
Niels Möller committed
247
int get_portno(const char *service, const char *protocol);
248

Niels Möller's avatar
Niels Möller committed
249
250
251
252
struct address_info *
make_address_info_c(const char *host,
		    const char *port,
		    int def);
253

Niels Möller's avatar
Niels Möller committed
254
255
256
struct address_info *
make_address_info(struct lsh_string *host, 
		  UINT32 port);
257

Niels Möller's avatar
Niels Möller committed
258
259
260
struct address_info *
sockaddr2info(size_t addr_len,
	      struct sockaddr *addr);
261

Niels Möller's avatar
Niels Möller committed
262
263
264
struct sockaddr *
address_info2sockaddr(socklen_t *length,
		      struct address_info *a,
265
		      const int *preference,
Niels Möller's avatar
Niels Möller committed
266
		      int lookup);
267

268
269
270
271
272
/* Returns an exception, if anything went wrong */
const struct exception *
write_raw(int fd, UINT32 length, const UINT8 *data);
const struct exception *
write_raw_with_poll(int fd, UINT32 length, const UINT8 *data);
273

274
275
276
const struct exception *
read_raw(int fd, UINT32 length, UINT8 *data);

Niels Möller's avatar
Niels Möller committed
277
void io_set_nonblocking(int fd);
278
void io_set_blocking(int fd);
279
280
void io_set_close_on_exec(int fd);
void io_init_fd(int fd);
Niels Möller's avatar
Niels Möller committed
281

282
283
284
285
struct lsh_fd *
make_lsh_fd(struct io_backend *b,
	    int fd, const char *label,
	    struct exception_handler *e);
286

Niels Möller's avatar
Niels Möller committed
287
288
struct exception_handler *
make_exc_finish_read_handler(struct lsh_fd *fd,
289
290
			     struct exception_handler *parent,
			     const char *context);
Niels Möller's avatar
Niels Möller committed
291

292
struct lsh_fd *
293
io_connect(struct io_backend *b,
Niels Möller's avatar
Niels Möller committed
294
295
	   struct sockaddr *remote,
	   socklen_t remote_length,
296
	   struct command_continuation *c,
297
	   struct exception_handler *e);
Niels Möller's avatar
Niels Möller committed
298

299
300
301
302
303
304
305
306
307
struct lsh_fd *
io_listen(struct io_backend *b,
	  struct sockaddr *local,
	  socklen_t length,
	  struct io_callback *callback,
	  struct exception_handler *e);

struct lsh_fd *
io_listen_local(struct io_backend *b,
308
		struct local_info *info,
309
310
		struct io_callback *callback,
		struct exception_handler *e);
Niels Möller's avatar
Niels Möller committed
311

312
313
struct lsh_fd *
io_connect_local(struct io_backend *b,
314
		 struct local_info *info,
315
316
317
		 struct command_continuation *c,
		 struct exception_handler *e);

318
319
320
321
struct io_callback *
make_listen_callback(struct io_backend *backend,
		     struct command_continuation *c,
		     struct exception_handler *e);
Niels Möller's avatar
Niels Möller committed
322

323
324
325
struct lsh_fd *io_read_write(struct lsh_fd *fd,
			     struct io_callback *read,
			     UINT32 block_size,
326
			     struct lsh_callback *close_callback);
327
328
329

struct lsh_fd *io_read(struct lsh_fd *fd,
		       struct io_callback *read,
330
		       struct lsh_callback *close_callback);
331

332
struct lsh_fd *io_write(struct lsh_fd *fd,
333
			UINT32 block_size,
334
			struct lsh_callback *close_callback);
335

336
void close_fd(struct lsh_fd *fd);
337

338
339
/* Stop reading from the fd, and close it as soon as the buffer
 * is completely written. */
340
void close_fd_nicely(struct lsh_fd *fd);
341

Niels Möller's avatar
Niels Möller committed
342
343
344
/* Stop reading, but if the fd has a write callback, keep it open. */
void close_fd_read(struct lsh_fd *fd);

Niels Möller's avatar
Niels Möller committed
345
346
347
348
349
350
351
struct lsh_fd *
io_write_file(struct io_backend *backend,
	      const char *fname, int flags,
	      int mode,
	      UINT32 block_size,
	      struct lsh_callback *c,
	      struct exception_handler *e);
Niels Möller's avatar
Niels Möller committed
352

353
struct lsh_fd *
354
355
356
357
io_read_file(struct io_backend *backend,
	     const char *fname, 
	     struct exception_handler *e);

358
359
360
int
lsh_make_pipe(int *fds);

361
362
363
int
lsh_copy_file(int src, int dst);

364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* Socket workaround */
#ifndef SHUTDOWN_WORKS_WITH_UNIX_SOCKETS

/* There's an how++ missing in the af_unix shutdown implementation of
 * some linux versions. Try an ugly workaround. */
#ifdef linux

/* From src/linux/include/net/sock.h */
#define RCV_SHUTDOWN	1
#define SEND_SHUTDOWN	2

#define SHUT_RD_UNIX RCV_SHUTDOWN
#define SHUT_WR_UNIX SEND_SHUTDOWN
#define SHUT_RD_WR_UNIX (RCV_SHUTDOWN | SEND_SHUTDOWN)

#else /* !linux */

/* Don't know how to work around the broken shutdown. So disable it
 * completely. */

#define SHUTDOWN_UNIX(fd, how) 0

#endif /* !linux */
#endif /* !SHUTDOWN_WORKS_WITH_UNIX_SOCKETS */

#ifndef SHUTDOWN_UNIX
#define SHUTDOWN_UNIX(fd, how) (shutdown((fd), (how)))
#endif

#ifndef SHUT_RD
#define SHUT_RD 0
#endif

#ifndef SHUT_WR
#define SHUT_WR 1
#endif

#ifndef SHUT_RD_WR
#define SHUT_RD_WR 2
#endif

#ifndef SHUT_RD_UNIX
#define SHUT_RD_UNIX SHUT_RD
#endif

#ifndef SHUT_WR_UNIX
#define SHUT_WR_UNIX SHUT_WR
#endif

#ifndef SHUT_RD_WR_UNIX
#define SHUT_RD_WR_UNIX SHUT_RD_WR
#endif

Niels Möller's avatar
Niels Möller committed
417
#endif /* LSH_IO_H_INCLUDED */