]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - network.h
first pass
[PuTTY.git] / network.h
1 /*
2  * Networking abstraction in PuTTY.
3  *
4  * The way this works is: a back end can choose to open any number
5  * of sockets - including zero, which might be necessary in some.
6  * It can register a bunch of callbacks (most notably for when 
7  * data is received) for each socket, and it can call the networking
8  * abstraction to send data without having to worry about blocking.
9  * The stuff behind the abstraction takes care of selects and
10  * nonblocking writes and all that sort of painful gubbins.
11  */
12
13 #ifndef PUTTY_NETWORK_H
14 #define PUTTY_NETWORK_H
15
16 #ifndef DONE_TYPEDEFS
17 #define DONE_TYPEDEFS
18 typedef struct conf_tag Conf;
19 typedef struct backend_tag Backend;
20 typedef struct terminal_tag Terminal;
21 #endif
22
23 typedef struct SockAddr_tag *SockAddr;
24 /* pay attention to levels of indirection */
25 typedef struct socket_function_table **Socket;
26 typedef struct plug_function_table **Plug;
27
28 struct socket_function_table {
29     Plug(*plug) (Socket s, Plug p);
30     /* use a different plug (return the old one) */
31     /* if p is NULL, it doesn't change the plug */
32     /* but it does return the one it's using */
33     void (*close) (Socket s);
34     int (*write) (Socket s, const char *data, int len);
35     int (*write_oob) (Socket s, const char *data, int len);
36     void (*write_eof) (Socket s);
37     void (*flush) (Socket s);
38     void (*set_frozen) (Socket s, int is_frozen);
39     /* ignored by tcp, but vital for ssl */
40     const char *(*socket_error) (Socket s);
41     char *(*peer_info) (Socket s);
42 };
43
44 typedef union { void *p; int i; } accept_ctx_t;
45 typedef Socket (*accept_fn_t)(accept_ctx_t ctx, Plug plug);
46
47 struct plug_function_table {
48     void (*log)(Plug p, int type, SockAddr addr, int port,
49                 const char *error_msg, int error_code);
50     /*
51      * Passes the client progress reports on the process of setting
52      * up the connection.
53      * 
54      *  - type==0 means we are about to try to connect to address
55      *    `addr' (error_msg and error_code are ignored)
56      *  - type==1 means we have failed to connect to address `addr'
57      *    (error_msg and error_code are supplied). This is not a
58      *    fatal error - we may well have other candidate addresses
59      *    to fall back to. When it _is_ fatal, the closing()
60      *    function will be called.
61      *  - type==2 means that error_msg contains a line of generic
62      *    logging information about setting up the connection. This
63      *    will typically be a wodge of standard-error output from a
64      *    proxy command, so the receiver should probably prefix it to
65      *    indicate this.
66      */
67     int (*closing)
68      (Plug p, const char *error_msg, int error_code, int calling_back);
69     /* error_msg is NULL iff it is not an error (ie it closed normally) */
70     /* calling_back != 0 iff there is a Plug function */
71     /* currently running (would cure the fixme in try_send()) */
72     int (*receive) (Plug p, int urgent, char *data, int len);
73     /*
74      *  - urgent==0. `data' points to `len' bytes of perfectly
75      *    ordinary data.
76      * 
77      *  - urgent==1. `data' points to `len' bytes of data,
78      *    which were read from before an Urgent pointer.
79      * 
80      *  - urgent==2. `data' points to `len' bytes of data,
81      *    the first of which was the one at the Urgent mark.
82      */
83     void (*sent) (Plug p, int bufsize);
84     /*
85      * The `sent' function is called when the pending send backlog
86      * on a socket is cleared or partially cleared. The new backlog
87      * size is passed in the `bufsize' parameter.
88      */
89     int (*accepting)(Plug p, accept_fn_t constructor, accept_ctx_t ctx);
90     /*
91      * `accepting' is called only on listener-type sockets, and is
92      * passed a constructor function+context that will create a fresh
93      * Socket describing the connection. It returns nonzero if it
94      * doesn't want the connection for some reason, or 0 on success.
95      */
96 };
97
98 /* proxy indirection layer */
99 /* NB, control of 'addr' is passed via new_connection, which takes
100  * responsibility for freeing it */
101 Socket new_connection(SockAddr addr, const char *hostname,
102                       int port, int privport,
103                       int oobinline, int nodelay, int keepalive,
104                       Plug plug, Conf *conf);
105 Socket new_listener(const char *srcaddr, int port, Plug plug,
106                     int local_host_only, Conf *conf, int addressfamily);
107 SockAddr name_lookup(const char *host, int port, char **canonicalname,
108                      Conf *conf, int addressfamily, void *frontend_for_logging,
109                      const char *lookup_reason_for_logging);
110 int proxy_for_destination (SockAddr addr, const char *hostname, int port,
111                            Conf *conf);
112
113 /* platform-dependent callback from new_connection() */
114 /* (same caveat about addr as new_connection()) */
115 Socket platform_new_connection(SockAddr addr, const char *hostname,
116                                int port, int privport,
117                                int oobinline, int nodelay, int keepalive,
118                                Plug plug, Conf *conf);
119
120 /* socket functions */
121
122 void sk_init(void);                    /* called once at program startup */
123 void sk_cleanup(void);                 /* called just before program exit */
124
125 SockAddr sk_namelookup(const char *host, char **canonicalname, int address_family);
126 SockAddr sk_nonamelookup(const char *host);
127 void sk_getaddr(SockAddr addr, char *buf, int buflen);
128 int sk_addr_needs_port(SockAddr addr);
129 int sk_hostname_is_local(const char *name);
130 int sk_address_is_local(SockAddr addr);
131 int sk_address_is_special_local(SockAddr addr);
132 int sk_addrtype(SockAddr addr);
133 void sk_addrcopy(SockAddr addr, char *buf);
134 void sk_addr_free(SockAddr addr);
135 /* sk_addr_dup generates another SockAddr which contains the same data
136  * as the original one and can be freed independently. May not actually
137  * physically _duplicate_ it: incrementing a reference count so that
138  * one more free is required before it disappears is an acceptable
139  * implementation. */
140 SockAddr sk_addr_dup(SockAddr addr);
141
142 /* NB, control of 'addr' is passed via sk_new, which takes responsibility
143  * for freeing it, as for new_connection() */
144 Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
145               int nodelay, int keepalive, Plug p);
146
147 Socket sk_newlistener(const char *srcaddr, int port, Plug plug,
148                       int local_host_only, int address_family);
149
150 #define sk_plug(s,p) (((*s)->plug) (s, p))
151 #define sk_close(s) (((*s)->close) (s))
152 #define sk_write(s,buf,len) (((*s)->write) (s, buf, len))
153 #define sk_write_oob(s,buf,len) (((*s)->write_oob) (s, buf, len))
154 #define sk_write_eof(s) (((*s)->write_eof) (s))
155 #define sk_flush(s) (((*s)->flush) (s))
156
157 #ifdef DEFINE_PLUG_METHOD_MACROS
158 #define plug_log(p,type,addr,port,msg,code) (((*p)->log) (p, type, addr, port, msg, code))
159 #define plug_closing(p,msg,code,callback) (((*p)->closing) (p, msg, code, callback))
160 #define plug_receive(p,urgent,buf,len) (((*p)->receive) (p, urgent, buf, len))
161 #define plug_sent(p,bufsize) (((*p)->sent) (p, bufsize))
162 #define plug_accepting(p, constructor, ctx) (((*p)->accepting)(p, constructor, ctx))
163 #endif
164
165 /*
166  * Special error values are returned from sk_namelookup and sk_new
167  * if there's a problem. These functions extract an error message,
168  * or return NULL if there's no problem.
169  */
170 const char *sk_addr_error(SockAddr addr);
171 #define sk_socket_error(s) (((*s)->socket_error) (s))
172
173 /*
174  * Set the `frozen' flag on a socket. A frozen socket is one in
175  * which all READABLE notifications are ignored, so that data is
176  * not accepted from the peer until the socket is unfrozen. This
177  * exists for two purposes:
178  * 
179  *  - Port forwarding: when a local listening port receives a
180  *    connection, we do not want to receive data from the new
181  *    socket until we have somewhere to send it. Hence, we freeze
182  *    the socket until its associated SSH channel is ready; then we
183  *    unfreeze it and pending data is delivered.
184  * 
185  *  - Socket buffering: if an SSH channel (or the whole connection)
186  *    backs up or presents a zero window, we must freeze the
187  *    associated local socket in order to avoid unbounded buffer
188  *    growth.
189  */
190 #define sk_set_frozen(s, is_frozen) (((*s)->set_frozen) (s, is_frozen))
191
192 /*
193  * Return a (dynamically allocated) string giving some information
194  * about the other end of the socket, suitable for putting in log
195  * files. May be NULL if nothing is available at all.
196  */
197 #define sk_peer_info(s) (((*s)->peer_info) (s))
198
199 /*
200  * Simple wrapper on getservbyname(), needed by ssh.c. Returns the
201  * port number, in host byte order (suitable for printf and so on).
202  * Returns 0 on failure. Any platform not supporting getservbyname
203  * can just return 0 - this function is not required to handle
204  * numeric port specifications.
205  */
206 int net_service_lookup(char *service);
207
208 /*
209  * Look up the local hostname; return value needs freeing.
210  * May return NULL.
211  */
212 char *get_hostname(void);
213
214 /*
215  * Trivial socket implementation which just stores an error. Found in
216  * errsock.c.
217  */
218 Socket new_error_socket(const char *errmsg, Plug plug);
219
220 /* ----------------------------------------------------------------------
221  * Functions defined outside the network code, which have to be
222  * declared in this header file rather than the main putty.h because
223  * they use types defined here.
224  */
225
226 /*
227  * Exports from be_misc.c.
228  */
229 void backend_socket_log(void *frontend, int type, SockAddr addr, int port,
230                         const char *error_msg, int error_code, Conf *conf,
231                         int session_started);
232 #ifndef BUFCHAIN_TYPEDEF
233 typedef struct bufchain_tag bufchain;  /* rest of declaration in misc.c */
234 #define BUFCHAIN_TYPEDEF
235 #endif
236 void log_proxy_stderr(Plug plug, bufchain *buf, const void *vdata, int len);
237
238 #endif