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