Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

network.h

00001 /*
00002  * Networking abstraction in PuTTY.
00003  *
00004  * The way this works is: a back end can choose to open any number
00005  * of sockets - including zero, which might be necessary in some.
00006  * It can register a bunch of callbacks (most notably for when 
00007  * data is received) for each socket, and it can call the networking
00008  * abstraction to send data without having to worry about blocking.
00009  * The stuff behind the abstraction takes care of selects and
00010  * nonblocking writes and all that sort of painful gubbins.
00011  */
00012 
00013 #ifndef PUTTY_NETWORK_H
00014 #define PUTTY_NETWORK_H
00015 
00016 typedef struct SockAddr_tag *SockAddr;
00017 /* pay attention to levels of indirection */
00018 typedef struct socket_function_table **Socket;
00019 typedef struct plug_function_table **Plug;
00020 
00021 struct socket_function_table {
00022     Plug(*plug) (Socket s, Plug p);
00023     /* use a different plug (return the old one) */
00024     /* if p is NULL, it doesn't change the plug */
00025     /* but it does return the one it's using */
00026     void (*close) (Socket s);
00027     int (*write) (Socket s, char *data, int len);
00028     int (*write_oob) (Socket s, char *data, int len);
00029     void (*flush) (Socket s);
00030     void (*set_private_ptr) (Socket s, void *ptr);
00031     void *(*get_private_ptr) (Socket s);
00032     void (*set_frozen) (Socket s, int is_frozen);
00033     /* ignored by tcp, but vital for ssl */
00034     char *(*socket_error) (Socket s);
00035 };
00036 
00037 struct plug_function_table {
00038     int (*closing)
00039      (Plug p, char *error_msg, int error_code, int calling_back);
00040     /* error_msg is NULL iff it is not an error (ie it closed normally) */
00041     /* calling_back != 0 iff there is a Plug function */
00042     /* currently running (would cure the fixme in try_send()) */
00043     int (*receive) (Plug p, int urgent, char *data, int len);
00044     /*
00045      *  - urgent==0. `data' points to `len' bytes of perfectly
00046      *    ordinary data.
00047      * 
00048      *  - urgent==1. `data' points to `len' bytes of data,
00049      *    which were read from before an Urgent pointer.
00050      * 
00051      *  - urgent==2. `data' points to `len' bytes of data,
00052      *    the first of which was the one at the Urgent mark.
00053      */
00054     void (*sent) (Plug p, int bufsize);
00055     /*
00056      * The `sent' function is called when the pending send backlog
00057      * on a socket is cleared or partially cleared. The new backlog
00058      * size is passed in the `bufsize' parameter.
00059      */
00060     int (*accepting)(Plug p, void *sock);
00061     /*
00062      * returns 0 if the host at address addr is a valid host for connecting or error
00063      */
00064 };
00065 
00066 /* proxy indirection layer */
00067 Socket new_connection(SockAddr addr, char *hostname,
00068                       int port, int privport,
00069                       int oobinline, int nodelay, Plug plug);
00070 Socket new_listener(int port, Plug plug, int local_host_only);
00071 
00072 /* socket functions */
00073 
00074 void sk_init(void);                    /* called once at program startup */
00075 void sk_cleanup(void);                 /* called just before program exit */
00076 
00077 SockAddr sk_namelookup(char *host, char **canonicalname);
00078 void sk_getaddr(SockAddr addr, char *buf, int buflen);
00079 int sk_addrtype(SockAddr addr);
00080 void sk_addrcopy(SockAddr addr, char *buf);
00081 void sk_addr_free(SockAddr addr);
00082 
00083 Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
00084               int nodelay, Plug p);
00085 
00086 Socket sk_newlistener(int port, Plug plug, int local_host_only);
00087 
00088 Socket sk_register(void *sock, Plug plug);
00089 
00090 #define sk_plug(s,p) (((*s)->plug) (s, p))
00091 #define sk_close(s) (((*s)->close) (s))
00092 #define sk_write(s,buf,len) (((*s)->write) (s, buf, len))
00093 #define sk_write_oob(s,buf,len) (((*s)->write_oob) (s, buf, len))
00094 #define sk_flush(s) (((*s)->flush) (s))
00095 
00096 #ifdef DEFINE_PLUG_METHOD_MACROS
00097 #define plug_closing(p,msg,code,callback) (((*p)->closing) (p, msg, code, callback))
00098 #define plug_receive(p,urgent,buf,len) (((*p)->receive) (p, urgent, buf, len))
00099 #define plug_sent(p,bufsize) (((*p)->sent) (p, bufsize))
00100 #define plug_accepting(p, sock) (((*p)->accepting)(p, sock))
00101 #endif
00102 
00103 /*
00104  * Each socket abstraction contains a `void *' private field in
00105  * which the client can keep state.
00106  *
00107  * This is perhaps unnecessary now that we have the notion of a plug,
00108  * but there is some existing code that uses it, so it stays.
00109  */
00110 #define sk_set_private_ptr(s, ptr) (((*s)->set_private_ptr) (s, ptr))
00111 #define sk_get_private_ptr(s) (((*s)->get_private_ptr) (s))
00112 
00113 /*
00114  * Special error values are returned from sk_namelookup and sk_new
00115  * if there's a problem. These functions extract an error message,
00116  * or return NULL if there's no problem.
00117  */
00118 char *sk_addr_error(SockAddr addr);
00119 #define sk_socket_error(s) (((*s)->socket_error) (s))
00120 
00121 /*
00122  * Set the `frozen' flag on a socket. A frozen socket is one in
00123  * which all READABLE notifications are ignored, so that data is
00124  * not accepted from the peer until the socket is unfrozen. This
00125  * exists for two purposes:
00126  * 
00127  *  - Port forwarding: when a local listening port receives a
00128  *    connection, we do not want to receive data from the new
00129  *    socket until we have somewhere to send it. Hence, we freeze
00130  *    the socket until its associated SSH channel is ready; then we
00131  *    unfreeze it and pending data is delivered.
00132  * 
00133  *  - Socket buffering: if an SSH channel (or the whole connection)
00134  *    backs up or presents a zero window, we must freeze the
00135  *    associated local socket in order to avoid unbounded buffer
00136  *    growth.
00137  */
00138 #define sk_set_frozen(s, is_frozen) (((*s)->set_frozen) (s, is_frozen))
00139 
00140 /*
00141  * Call this after an operation that might have tried to send on a
00142  * socket, to clean up any pending network errors.
00143  */
00144 void net_pending_errors(void);
00145 
00146 /********** SSL stuff **********/
00147 
00148 /*
00149  * This section is subject to change, but you get the general idea
00150  * of what it will eventually look like.
00151  */
00152 
00153 typedef struct certificate *Certificate;
00154 typedef struct our_certificate *Our_Certificate;
00155     /* to be defined somewhere else, somehow */
00156 
00157 typedef struct ssl_client_socket_function_table **SSL_Client_Socket;
00158 typedef struct ssl_client_plug_function_table **SSL_Client_Plug;
00159 
00160 struct ssl_client_socket_function_table {
00161     struct socket_function_table base;
00162     void (*renegotiate) (SSL_Client_Socket s);
00163     /* renegotiate the cipher spec */
00164 };
00165 
00166 struct ssl_client_plug_function_table {
00167     struct plug_function_table base;
00168     int (*refuse_cert) (SSL_Client_Plug p, Certificate cert[]);
00169     /* do we accept this certificate chain?  If not, why not? */
00170     /* cert[0] is the server's certificate, cert[] is NULL-terminated */
00171     /* the last certificate may or may not be the root certificate */
00172      Our_Certificate(*client_cert) (SSL_Client_Plug p);
00173     /* the server wants us to identify ourselves */
00174     /* may return NULL if we want anonymity */
00175 };
00176 
00177 SSL_Client_Socket sk_ssl_client_over(Socket s,  /* pre-existing (tcp) connection */
00178                                      SSL_Client_Plug p);
00179 
00180 #define sk_renegotiate(s) (((*s)->renegotiate) (s))
00181 
00182 #endif

Generated on Sun Feb 9 13:01:28 2003 for PuTTY by doxygen1.2.18