2000-10-23 15:55:11 +04:00
|
|
|
/*
|
|
|
|
* Networking abstraction in PuTTY.
|
|
|
|
*
|
|
|
|
* The way this works is: a back end can choose to open any number
|
|
|
|
* of sockets - including zero, which might be necessary in some.
|
2001-03-13 13:22:45 +03:00
|
|
|
* It can register a bunch of callbacks (most notably for when
|
|
|
|
* data is received) for each socket, and it can call the networking
|
|
|
|
* abstraction to send data without having to worry about blocking.
|
|
|
|
* The stuff behind the abstraction takes care of selects and
|
|
|
|
* nonblocking writes and all that sort of painful gubbins.
|
2000-10-23 15:55:11 +04:00
|
|
|
*/
|
|
|
|
|
2000-10-24 14:47:49 +04:00
|
|
|
#ifndef PUTTY_NETWORK_H
|
|
|
|
#define PUTTY_NETWORK_H
|
|
|
|
|
2003-01-12 18:26:10 +03:00
|
|
|
#ifndef DONE_TYPEDEFS
|
|
|
|
#define DONE_TYPEDEFS
|
|
|
|
typedef struct config_tag Config;
|
|
|
|
typedef struct backend_tag Backend;
|
|
|
|
typedef struct terminal_tag Terminal;
|
|
|
|
#endif
|
|
|
|
|
2000-10-23 15:55:11 +04:00
|
|
|
typedef struct SockAddr_tag *SockAddr;
|
2001-03-13 13:22:45 +03:00
|
|
|
/* pay attention to levels of indirection */
|
|
|
|
typedef struct socket_function_table **Socket;
|
|
|
|
typedef struct plug_function_table **Plug;
|
|
|
|
|
2003-05-10 12:35:54 +04:00
|
|
|
#ifndef OSSOCKET_DEFINED
|
|
|
|
typedef void *OSSocket;
|
|
|
|
#endif
|
|
|
|
|
2001-03-13 13:22:45 +03:00
|
|
|
struct socket_function_table {
|
2001-05-06 18:35:20 +04:00
|
|
|
Plug(*plug) (Socket s, Plug p);
|
|
|
|
/* use a different plug (return the old one) */
|
|
|
|
/* if p is NULL, it doesn't change the plug */
|
|
|
|
/* but it does return the one it's using */
|
2001-03-13 13:22:45 +03:00
|
|
|
void (*close) (Socket s);
|
2003-01-10 21:33:35 +03:00
|
|
|
int (*write) (Socket s, const char *data, int len);
|
|
|
|
int (*write_oob) (Socket s, const char *data, int len);
|
2001-03-13 13:22:45 +03:00
|
|
|
void (*flush) (Socket s);
|
2002-03-23 20:47:21 +03:00
|
|
|
void (*set_private_ptr) (Socket s, void *ptr);
|
|
|
|
void *(*get_private_ptr) (Socket s);
|
|
|
|
void (*set_frozen) (Socket s, int is_frozen);
|
2001-05-06 18:35:20 +04:00
|
|
|
/* ignored by tcp, but vital for ssl */
|
2003-05-04 18:18:18 +04:00
|
|
|
const char *(*socket_error) (Socket s);
|
2001-03-13 13:22:45 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct plug_function_table {
|
2005-01-16 17:29:34 +03:00
|
|
|
void (*log)(Plug p, int type, SockAddr addr, int port,
|
|
|
|
const char *error_msg, int error_code);
|
|
|
|
/*
|
|
|
|
* Passes the client progress reports on the process of setting
|
|
|
|
* up the connection.
|
|
|
|
*
|
|
|
|
* - type==0 means we are about to try to connect to address
|
|
|
|
* `addr' (error_msg and error_code are ignored)
|
|
|
|
* - type==1 means we have failed to connect to address `addr'
|
|
|
|
* (error_msg and error_code are supplied). This is not a
|
|
|
|
* fatal error - we may well have other candidate addresses
|
|
|
|
* to fall back to. When it _is_ fatal, the closing()
|
|
|
|
* function will be called.
|
|
|
|
*/
|
2001-03-13 13:22:45 +03:00
|
|
|
int (*closing)
|
2003-05-04 18:18:18 +04:00
|
|
|
(Plug p, const char *error_msg, int error_code, int calling_back);
|
2001-05-06 18:35:20 +04:00
|
|
|
/* error_msg is NULL iff it is not an error (ie it closed normally) */
|
|
|
|
/* calling_back != 0 iff there is a Plug function */
|
|
|
|
/* currently running (would cure the fixme in try_send()) */
|
2001-03-13 13:22:45 +03:00
|
|
|
int (*receive) (Plug p, int urgent, char *data, int len);
|
2001-05-06 18:35:20 +04:00
|
|
|
/*
|
|
|
|
* - urgent==0. `data' points to `len' bytes of perfectly
|
|
|
|
* ordinary data.
|
|
|
|
*
|
|
|
|
* - urgent==1. `data' points to `len' bytes of data,
|
|
|
|
* which were read from before an Urgent pointer.
|
|
|
|
*
|
|
|
|
* - urgent==2. `data' points to `len' bytes of data,
|
|
|
|
* the first of which was the one at the Urgent mark.
|
|
|
|
*/
|
2001-08-25 21:09:23 +04:00
|
|
|
void (*sent) (Plug p, int bufsize);
|
|
|
|
/*
|
|
|
|
* The `sent' function is called when the pending send backlog
|
|
|
|
* on a socket is cleared or partially cleared. The new backlog
|
|
|
|
* size is passed in the `bufsize' parameter.
|
|
|
|
*/
|
2003-05-10 12:35:54 +04:00
|
|
|
int (*accepting)(Plug p, OSSocket sock);
|
2001-08-09 00:44:35 +04:00
|
|
|
/*
|
|
|
|
* returns 0 if the host at address addr is a valid host for connecting or error
|
|
|
|
*/
|
2001-03-13 13:22:45 +03:00
|
|
|
};
|
2001-01-24 13:11:18 +03:00
|
|
|
|
2002-03-23 20:47:21 +03:00
|
|
|
/* proxy indirection layer */
|
2003-08-07 20:04:33 +04:00
|
|
|
/* NB, control of 'addr' is passed via new_connection, which takes
|
|
|
|
* responsibility for freeing it */
|
2002-03-23 20:47:21 +03:00
|
|
|
Socket new_connection(SockAddr addr, char *hostname,
|
|
|
|
int port, int privport,
|
2004-06-20 21:07:38 +04:00
|
|
|
int oobinline, int nodelay, int keepalive,
|
|
|
|
Plug plug, const Config *cfg);
|
2003-01-12 18:26:10 +03:00
|
|
|
Socket new_listener(char *srcaddr, int port, Plug plug, int local_host_only,
|
2004-12-30 19:45:11 +03:00
|
|
|
const Config *cfg, int addressfamily);
|
2003-01-12 18:26:10 +03:00
|
|
|
SockAddr name_lookup(char *host, int port, char **canonicalname,
|
2004-12-30 19:45:11 +03:00
|
|
|
const Config *cfg, int addressfamily);
|
2002-03-23 20:47:21 +03:00
|
|
|
|
2003-06-06 14:42:14 +04:00
|
|
|
/* platform-dependent callback from new_connection() */
|
2003-08-07 20:04:33 +04:00
|
|
|
/* (same caveat about addr as new_connection()) */
|
2003-06-06 14:42:14 +04:00
|
|
|
Socket platform_new_connection(SockAddr addr, char *hostname,
|
|
|
|
int port, int privport,
|
2004-06-20 21:07:38 +04:00
|
|
|
int oobinline, int nodelay, int keepalive,
|
|
|
|
Plug plug, const Config *cfg);
|
2003-06-06 14:42:14 +04:00
|
|
|
|
2002-03-23 20:47:21 +03:00
|
|
|
/* socket functions */
|
2000-10-23 15:55:11 +04:00
|
|
|
|
|
|
|
void sk_init(void); /* called once at program startup */
|
2002-03-06 23:13:22 +03:00
|
|
|
void sk_cleanup(void); /* called just before program exit */
|
2000-10-23 15:55:11 +04:00
|
|
|
|
2004-12-30 19:45:11 +03:00
|
|
|
SockAddr sk_namelookup(const char *host, char **canonicalname, int address_family);
|
2003-01-12 18:26:10 +03:00
|
|
|
SockAddr sk_nonamelookup(const char *host);
|
2001-09-08 02:39:01 +04:00
|
|
|
void sk_getaddr(SockAddr addr, char *buf, int buflen);
|
2002-12-18 15:18:54 +03:00
|
|
|
int sk_hostname_is_local(char *name);
|
|
|
|
int sk_address_is_local(SockAddr addr);
|
2002-04-27 19:01:18 +04:00
|
|
|
int sk_addrtype(SockAddr addr);
|
|
|
|
void sk_addrcopy(SockAddr addr, char *buf);
|
2000-10-23 15:55:11 +04:00
|
|
|
void sk_addr_free(SockAddr addr);
|
|
|
|
|
2003-08-07 20:04:33 +04:00
|
|
|
/* NB, control of 'addr' is passed via sk_new, which takes responsibility
|
|
|
|
* for freeing it, as for new_connection() */
|
2001-05-06 18:35:20 +04:00
|
|
|
Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
|
2004-06-20 21:07:38 +04:00
|
|
|
int nodelay, int keepalive, Plug p);
|
2001-03-13 13:22:45 +03:00
|
|
|
|
2004-12-30 19:45:11 +03:00
|
|
|
Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only, int address_family);
|
2001-08-09 00:44:35 +04:00
|
|
|
|
2003-05-10 12:35:54 +04:00
|
|
|
Socket sk_register(OSSocket sock, Plug plug);
|
2001-08-09 00:44:35 +04:00
|
|
|
|
2001-03-13 13:22:45 +03:00
|
|
|
#define sk_plug(s,p) (((*s)->plug) (s, p))
|
|
|
|
#define sk_close(s) (((*s)->close) (s))
|
|
|
|
#define sk_write(s,buf,len) (((*s)->write) (s, buf, len))
|
|
|
|
#define sk_write_oob(s,buf,len) (((*s)->write_oob) (s, buf, len))
|
|
|
|
#define sk_flush(s) (((*s)->flush) (s))
|
|
|
|
|
|
|
|
#ifdef DEFINE_PLUG_METHOD_MACROS
|
2005-01-16 17:29:34 +03:00
|
|
|
#define plug_log(p,type,addr,port,msg,code) (((*p)->log) (p, type, addr, port, msg, code))
|
2001-03-13 13:22:45 +03:00
|
|
|
#define plug_closing(p,msg,code,callback) (((*p)->closing) (p, msg, code, callback))
|
|
|
|
#define plug_receive(p,urgent,buf,len) (((*p)->receive) (p, urgent, buf, len))
|
2001-08-25 21:09:23 +04:00
|
|
|
#define plug_sent(p,bufsize) (((*p)->sent) (p, bufsize))
|
2001-08-27 19:59:37 +04:00
|
|
|
#define plug_accepting(p, sock) (((*p)->accepting)(p, sock))
|
2001-03-13 13:22:45 +03:00
|
|
|
#endif
|
2000-10-23 15:55:11 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Each socket abstraction contains a `void *' private field in
|
|
|
|
* which the client can keep state.
|
2001-03-13 13:22:45 +03:00
|
|
|
*
|
|
|
|
* This is perhaps unnecessary now that we have the notion of a plug,
|
|
|
|
* but there is some existing code that uses it, so it stays.
|
2000-10-23 15:55:11 +04:00
|
|
|
*/
|
2002-03-23 20:47:21 +03:00
|
|
|
#define sk_set_private_ptr(s, ptr) (((*s)->set_private_ptr) (s, ptr))
|
|
|
|
#define sk_get_private_ptr(s) (((*s)->get_private_ptr) (s))
|
2000-10-23 15:55:11 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Special error values are returned from sk_namelookup and sk_new
|
|
|
|
* if there's a problem. These functions extract an error message,
|
|
|
|
* or return NULL if there's no problem.
|
|
|
|
*/
|
2003-05-04 18:18:18 +04:00
|
|
|
const char *sk_addr_error(SockAddr addr);
|
2001-03-13 13:22:45 +03:00
|
|
|
#define sk_socket_error(s) (((*s)->socket_error) (s))
|
|
|
|
|
2001-08-09 00:44:35 +04:00
|
|
|
/*
|
|
|
|
* Set the `frozen' flag on a socket. A frozen socket is one in
|
2001-08-25 21:09:23 +04:00
|
|
|
* which all READABLE notifications are ignored, so that data is
|
|
|
|
* not accepted from the peer until the socket is unfrozen. This
|
|
|
|
* exists for two purposes:
|
|
|
|
*
|
|
|
|
* - Port forwarding: when a local listening port receives a
|
|
|
|
* connection, we do not want to receive data from the new
|
|
|
|
* socket until we have somewhere to send it. Hence, we freeze
|
|
|
|
* the socket until its associated SSH channel is ready; then we
|
|
|
|
* unfreeze it and pending data is delivered.
|
|
|
|
*
|
|
|
|
* - Socket buffering: if an SSH channel (or the whole connection)
|
|
|
|
* backs up or presents a zero window, we must freeze the
|
|
|
|
* associated local socket in order to avoid unbounded buffer
|
|
|
|
* growth.
|
2001-08-09 00:44:35 +04:00
|
|
|
*/
|
2002-03-23 20:47:21 +03:00
|
|
|
#define sk_set_frozen(s, is_frozen) (((*s)->set_frozen) (s, is_frozen))
|
2001-03-13 13:22:45 +03:00
|
|
|
|
2001-10-30 23:57:22 +03:00
|
|
|
/*
|
|
|
|
* Call this after an operation that might have tried to send on a
|
|
|
|
* socket, to clean up any pending network errors.
|
|
|
|
*/
|
|
|
|
void net_pending_errors(void);
|
|
|
|
|
2002-10-30 20:57:31 +03:00
|
|
|
/*
|
|
|
|
* Simple wrapper on getservbyname(), needed by ssh.c. Returns the
|
|
|
|
* port number, in host byte order (suitable for printf and so on).
|
|
|
|
* Returns 0 on failure. Any platform not supporting getservbyname
|
|
|
|
* can just return 0 - this function is not required to handle
|
|
|
|
* numeric port specifications.
|
|
|
|
*/
|
|
|
|
int net_service_lookup(char *service);
|
|
|
|
|
2001-03-13 13:22:45 +03:00
|
|
|
/********** SSL stuff **********/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This section is subject to change, but you get the general idea
|
|
|
|
* of what it will eventually look like.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct certificate *Certificate;
|
|
|
|
typedef struct our_certificate *Our_Certificate;
|
|
|
|
/* to be defined somewhere else, somehow */
|
|
|
|
|
|
|
|
typedef struct ssl_client_socket_function_table **SSL_Client_Socket;
|
|
|
|
typedef struct ssl_client_plug_function_table **SSL_Client_Plug;
|
|
|
|
|
|
|
|
struct ssl_client_socket_function_table {
|
|
|
|
struct socket_function_table base;
|
|
|
|
void (*renegotiate) (SSL_Client_Socket s);
|
2001-05-06 18:35:20 +04:00
|
|
|
/* renegotiate the cipher spec */
|
2001-03-13 13:22:45 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ssl_client_plug_function_table {
|
|
|
|
struct plug_function_table base;
|
|
|
|
int (*refuse_cert) (SSL_Client_Plug p, Certificate cert[]);
|
2001-05-06 18:35:20 +04:00
|
|
|
/* do we accept this certificate chain? If not, why not? */
|
|
|
|
/* cert[0] is the server's certificate, cert[] is NULL-terminated */
|
|
|
|
/* the last certificate may or may not be the root certificate */
|
|
|
|
Our_Certificate(*client_cert) (SSL_Client_Plug p);
|
|
|
|
/* the server wants us to identify ourselves */
|
|
|
|
/* may return NULL if we want anonymity */
|
2001-03-13 13:22:45 +03:00
|
|
|
};
|
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
SSL_Client_Socket sk_ssl_client_over(Socket s, /* pre-existing (tcp) connection */
|
|
|
|
SSL_Client_Plug p);
|
2001-03-13 13:22:45 +03:00
|
|
|
|
|
|
|
#define sk_renegotiate(s) (((*s)->renegotiate) (s))
|
2000-10-24 14:47:49 +04:00
|
|
|
|
|
|
|
#endif
|