io.h 5.93 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 "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>
36
#include <sys/socket.h>
Niels Möller's avatar
Niels Möller committed
37
#include <netinet/in.h>
Niels Möller's avatar
Niels Möller committed
38

39
#define GABA_DECLARE
40
#include "io.h.x"
41
#undef GABA_DECLARE
42

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

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

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

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

/* EPIPE when writing */
#define CLOSE_BROKEN_PIPE 2

#define CLOSE_WRITE_FAILED 3

/* #define CLOSE_READ_FAILED 4 */

#define CLOSE_PROTOCOL_FAILURE 5
69

70
/* GABA:
71
72
73
74
75
76
   (class
     (name close_callback)
     (vars
       (f method int "int reason")))
*/

77
78
#define CLOSE_CALLBACK(c, r) ((c)->f((c), (r)))

79
/* GABA:
80
81
   (class
     (name lsh_fd)
82
     (super resource)
83
84
     (vars
       (next object lsh_fd)
85
       (fd simple int)
86
87

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

       ; Called before poll
       (prepare method void)
Niels Möller's avatar
Niels Möller committed
93
94
95
96
       ;; Belongs in the write_buffer
       ; Called when some data has been successfully written
       ;;(report object report_write_success)
       
97
       (want_read simple int)
98
99
100
       ; Called if poll indicates that data can be read. 
       (read method void)

101
       (want_write simple int)
102
103
104
       ; Called if poll indicates that data can be written.
       (write method void)

105
       ; (close_now simple int)
106
107
108
       (really_close method void)))
*/

Niels Möller's avatar
Niels Möller committed
109
110
111
112
113
#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)))

114
/* GABA:
115
116
117
118
119
120
121
122
123
124
   (class
     (name io_fd)
     (super lsh_fd)
     (vars
       ; Reading 
       (handler object read_handler)
       ; Writing 
       (buffer object write_buffer)))
*/

125
126
127
128
129
130
/* Passed to the listen callback, and to other functions and commands
 * dealing with addresses. */
/* GABA:
   (class
     (name address_info)
     (vars
131
132
133
134
       ; An ipnumber, in decimal dot notation, ipv6 format, or
       ; a dns name.
       (ip string)
       ; The port number here is always in host byte order
135
       (port . UINT32))) */
136

137
/* GABA:
138
139
140
   (class
     (name fd_listen_callback)
     (vars
141
       (f method int int "struct address_info *")))
142
*/
143
#define FD_LISTEN_CALLBACK(c, fd, a) ((c)->f((c), (fd), (a)))
144

145
/* GABA:
146
   (class
147
     (name listen_fd)
148
149
     (super lsh_fd)
     (vars
150
       (callback object fd_listen_callback)))
151
*/
Niels Möller's avatar
Niels Möller committed
152

153
/* GABA:
154
155
156
157
158
159
   (class
     (name connect_fd)
     (super lsh_fd)
     (vars
       (callback object fd_callback)))
*/
Niels Möller's avatar
Niels Möller committed
160
161

#if 0
Niels Möller's avatar
Niels Möller committed
162
163
struct callout
{
164
165
  struct lsh_object header;
  
Niels Möller's avatar
Niels Möller committed
166
  struct callout *next;
Niels Möller's avatar
Niels Möller committed
167
168
169
  struct callback *callout;
  time_t when;
};
Niels Möller's avatar
Niels Möller committed
170
#endif
Niels Möller's avatar
Niels Möller committed
171

172
/* GABA:
173
174
175
176
177
178
   (class
     (name io_backend)
     (vars
       ; Linked list of fds. 
       (files object lsh_fd)
       ; Callouts
179
180
       ;; (callouts object callout)
       ))
181
182
*/

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

Niels Möller's avatar
Niels Möller committed
185
int io_iter(struct io_backend *b);
Niels Möller's avatar
Niels Möller committed
186
187
void io_run(struct io_backend *b);

188
189
int blocking_read(int fd, struct read_handler *r);

Niels Möller's avatar
Niels Möller committed
190
191
192
193
194
int get_inaddr(struct sockaddr_in	* addr,
	       const char		* host,
	       const char		* service,
	       const char		* protocol);

195
196
int get_portno(const char *s, const char *protocol);

197
198
199
200
201
int tcp_addr(struct sockaddr_in *sin,
	     UINT32 length,
	     UINT8 *addr,
	     UINT32 port);

202
203
204
struct address_info *make_address_info_c(const char *host,
					 const char *port);

205
206
207
struct address_info *make_address_info(struct lsh_string *host, 
				       UINT32 port);

208
209
210
211
212
213
struct address_info *sockaddr2info(size_t addr_len UNUSED,
				   struct sockaddr *addr);

int address_info2sockaddr_in(struct sockaddr_in *sin,
			     struct address_info *a);

214
215
216
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
217
void io_set_nonblocking(int fd);
218
219
void io_set_close_on_exec(int fd);
void io_init_fd(int fd);
Niels Möller's avatar
Niels Möller committed
220

221
222
223
struct io_fd *make_io_fd(struct io_backend *b,
			 int fd);

Niels Möller's avatar
Niels Möller committed
224
225
226
227
228
229
230
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,
231
			    struct fd_listen_callback *callback);
Niels Möller's avatar
Niels Möller committed
232

233
struct io_fd *io_read_write(struct io_fd *fd,
234
235
236
			    struct read_handler *read_callback,
			    UINT32 block_size,
			    struct close_callback *close_callback);
Niels Möller's avatar
Niels Möller committed
237

238
struct io_fd *io_read(struct io_fd *fd,
239
240
241
		      struct read_handler *read_callback,
		      struct close_callback *close_callback);

242
struct io_fd *io_write(struct io_fd *fd,
243
244
		       UINT32 block_size,
		       struct close_callback *close_callback);
245

246
/* Marks a file for close, without touching the close_reason field. */
247
248
void kill_fd(struct lsh_fd *fd);

249
250
void close_fd(struct lsh_fd *fd, int reason);

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

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