io.h 4.44 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>
Niels Möller's avatar
Niels Möller committed
35
#include <netinet/in.h>
Niels Möller's avatar
Niels Möller committed
36

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

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

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

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

53 54 55
/* 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? */
56
#define CLOSE_EOF 1
Niels Möller's avatar
Niels Möller committed
57 58 59 60 61 62 63 64 65

/* EPIPE when writing */
#define CLOSE_BROKEN_PIPE 2

#define CLOSE_WRITE_FAILED 3

/* #define CLOSE_READ_FAILED 4 */

#define CLOSE_PROTOCOL_FAILURE 5
66

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

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

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

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

       ; Called before poll
       (prepare method void)

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

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

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

Niels Möller's avatar
Niels Möller committed
103 104 105 106 107
#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)))

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

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

#define connect_fd listen_fd

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

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

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

Niels Möller's avatar
Niels Möller committed
153
int io_iter(struct io_backend *b);
Niels Möller's avatar
Niels Möller committed
154 155 156 157 158 159 160 161
void io_run(struct io_backend *b);

int get_inaddr(struct sockaddr_in	* addr,
	       const char		* host,
	       const char		* service,
	       const char		* protocol);

void io_set_nonblocking(int fd);
162 163
void io_set_close_on_exec(int fd);
void io_init_fd(int fd);
Niels Möller's avatar
Niels Möller committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

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,
179
				     struct close_callback *close_callback);
Niels Möller's avatar
Niels Möller committed
180

181 182 183 184 185
struct io_fd *io_read(struct io_backend *b,
		      int fd,
		      struct read_handler *read_callback,
		      struct close_callback *close_callback);

186 187 188 189
struct io_fd *io_write(struct io_backend *b,
		       int fd,
		       UINT32 block_size,
		       struct close_callback *close_callback);
190

191 192 193
/* Marks a file for close, without touching the close_Reason field. */
void kill_fd(struct lsh_fd *fd);

194 195
void close_fd(struct lsh_fd *fd, int reason);

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

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