io.h 4.62 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
23
 *
 *
 * $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
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 "resource.h"
Niels Möller's avatar
Niels Möller committed
31
32
#include "write_buffer.h"

Niels Möller's avatar
Niels Möller committed
33
34
#include <time.h>
#include <netdb.h>
35
#include <sys/types.h>
Niels Möller's avatar
Niels Möller committed
36
#include <netinet/in.h>
Niels Möller's avatar
Niels Möller committed
37

38
39
40
41
#define CLASS_DECLARE
#include "io.h.x"
#undef CLASS_DECLARE

Niels Möller's avatar
Niels Möller committed
42
/* A closed function with a file descriptor as argument */
43
44
45
46
/* CLASS:
   (class
     (name fd_callback)
     (vars
47
       (f indirect-method int "int fd")))
48
49
*/

Niels Möller's avatar
Niels Möller committed
50
#define FD_CALLBACK(c, fd) ((c)->f(&(c), (fd)))
Niels Möller's avatar
Niels Möller committed
51

52
53
/* Close callbacks are called with a reason as argument. */

54
55
56
/* End of file while reading.
 * Or when a closed write_buffer has been flushed successfully. */
/* FIXME: Should we use separate codes for these two events? */
57
#define CLOSE_EOF 1
Niels Möller's avatar
Niels Möller committed
58
59
60
61
62
63
64
65
66

/* EPIPE when writing */
#define CLOSE_BROKEN_PIPE 2

#define CLOSE_WRITE_FAILED 3

/* #define CLOSE_READ_FAILED 4 */

#define CLOSE_PROTOCOL_FAILURE 5
67

68
69
70
71
72
73
74
/* CLASS:
   (class
     (name close_callback)
     (vars
       (f method int "int reason")))
*/

75
76
#define CLOSE_CALLBACK(c, r) ((c)->f((c), (r)))

77
78
79
/* CLASS:
   (class
     (name lsh_fd)
80
     (super resource)
81
82
     (vars
       (next object lsh_fd)
83
       (fd simple int)
84
85

       ; User's close callback
86
       (close_reason simple int)
87
88
89
90
91
       (close_callback object close_callback)

       ; Called before poll
       (prepare method void)

92
       (want_read simple int)
93
94
95
       ; Called if poll indicates that data can be read. 
       (read method void)

96
       (want_write simple int)
97
98
99
       ; Called if poll indicates that data can be written.
       (write method void)

100
       ; (close_now simple int)
101
102
103
       (really_close method void)))
*/

Niels Möller's avatar
Niels Möller committed
104
105
106
107
108
#define PREPARE_FD(fd) ((fd)->prepare((fd)))
#define READ_FD(fd) ((fd)->read((fd)))
#define WRITE_FD(fd) ((fd)->write((fd)))
#define REALLY_CLOSE_FD(fd) ((fd)->really_close((fd)))

109
110
111
112
113
114
115
116
117
118
119
120
121
/* CLASS:
   (class
     (name io_fd)
     (super lsh_fd)
     (vars
       ; Reading 
       (handler object read_handler)
       ; Writing 
       (buffer object write_buffer)))
*/

/* CLASS:
   (class
122
     (name listen_fd)
123
124
125
126
     (super lsh_fd)
     (vars
       (callback object fd_callback)))
*/
Niels Möller's avatar
Niels Möller committed
127
128
129
130

#define connect_fd listen_fd

#if 0
Niels Möller's avatar
Niels Möller committed
131
132
struct callout
{
133
134
  struct lsh_object header;
  
Niels Möller's avatar
Niels Möller committed
135
  struct callout *next;
Niels Möller's avatar
Niels Möller committed
136
137
138
  struct callback *callout;
  time_t when;
};
Niels Möller's avatar
Niels Möller committed
139
#endif
Niels Möller's avatar
Niels Möller committed
140

141
142
143
144
145
146
147
/* CLASS:
   (class
     (name io_backend)
     (vars
       ; Linked list of fds. 
       (files object lsh_fd)
       ; Callouts
148
149
       ;; (callouts object callout)
       ))
150
151
*/

Niels Möller's avatar
Niels Möller committed
152
153
void init_backend(struct io_backend *b);

Niels Möller's avatar
Niels Möller committed
154
int io_iter(struct io_backend *b);
Niels Möller's avatar
Niels Möller committed
155
156
void io_run(struct io_backend *b);

157
158
int blocking_read(int fd, struct read_handler *r);

Niels Möller's avatar
Niels Möller committed
159
160
161
162
163
int get_inaddr(struct sockaddr_in	* addr,
	       const char		* host,
	       const char		* service,
	       const char		* protocol);

164
165
166
int write_raw(int fd, UINT32 length, UINT8 *data);
int write_raw_with_poll(int fd, UINT32 length, UINT8 *data);

Niels Möller's avatar
Niels Möller committed
167
void io_set_nonblocking(int fd);
168
169
void io_set_close_on_exec(int fd);
void io_init_fd(int fd);
Niels Möller's avatar
Niels Möller committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

struct connect_fd *io_connect(struct io_backend *b,
			      struct sockaddr_in *remote,
			      struct sockaddr_in *local,
			      struct fd_callback *f);

struct listen_fd *io_listen(struct io_backend *b,
			    struct sockaddr_in *local,
			    struct fd_callback *callback);


struct abstract_write *io_read_write(struct io_backend *b,
				     int fd,
				     struct read_handler *read_callback,
				     UINT32 block_size,
185
				     struct close_callback *close_callback);
Niels Möller's avatar
Niels Möller committed
186

187
188
189
190
191
struct io_fd *io_read(struct io_backend *b,
		      int fd,
		      struct read_handler *read_callback,
		      struct close_callback *close_callback);

192
193
194
195
struct io_fd *io_write(struct io_backend *b,
		       int fd,
		       UINT32 block_size,
		       struct close_callback *close_callback);
196

197
198
199
/* Marks a file for close, without touching the close_Reason field. */
void kill_fd(struct lsh_fd *fd);

200
201
void close_fd(struct lsh_fd *fd, int reason);

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

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