network.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #ifndef PUTTY_NETWORK_H
  13. #define PUTTY_NETWORK_H
  14. #ifndef DONE_TYPEDEFS
  15. #define DONE_TYPEDEFS
  16. typedef struct conf_tag Conf;
  17. typedef struct backend_tag Backend;
  18. typedef struct terminal_tag Terminal;
  19. #endif
  20. typedef struct SockAddr_tag *SockAddr;
  21. /* pay attention to levels of indirection */
  22. typedef struct socket_function_table **Socket;
  23. typedef struct plug_function_table **Plug;
  24. struct socket_function_table {
  25. Plug(*plug) (Socket s, Plug p);
  26. /* use a different plug (return the old one) */
  27. /* if p is NULL, it doesn't change the plug */
  28. /* but it does return the one it's using */
  29. void (*close) (Socket s);
  30. int (*write) (Socket s, const char *data, int len);
  31. int (*write_oob) (Socket s, const char *data, int len);
  32. void (*write_eof) (Socket s);
  33. void (*flush) (Socket s);
  34. void (*set_frozen) (Socket s, int is_frozen);
  35. /* ignored by tcp, but vital for ssl */
  36. const char *(*socket_error) (Socket s);
  37. char *(*peer_info) (Socket s);
  38. };
  39. typedef union { void *p; int i; } accept_ctx_t;
  40. typedef Socket (*accept_fn_t)(accept_ctx_t ctx, Plug plug);
  41. struct plug_function_table {
  42. void (*log)(Plug p, int type, SockAddr addr, int port,
  43. const char *error_msg, int error_code);
  44. /*
  45. * Passes the client progress reports on the process of setting
  46. * up the connection.
  47. *
  48. * - type==0 means we are about to try to connect to address
  49. * `addr' (error_msg and error_code are ignored)
  50. * - type==1 means we have failed to connect to address `addr'
  51. * (error_msg and error_code are supplied). This is not a
  52. * fatal error - we may well have other candidate addresses
  53. * to fall back to. When it _is_ fatal, the closing()
  54. * function will be called.
  55. */
  56. int (*closing)
  57. (Plug p, const char *error_msg, int error_code, int calling_back);
  58. /* error_msg is NULL iff it is not an error (ie it closed normally) */
  59. /* calling_back != 0 iff there is a Plug function */
  60. /* currently running (would cure the fixme in try_send()) */
  61. int (*receive) (Plug p, int urgent, char *data, int len);
  62. /*
  63. * - urgent==0. `data' points to `len' bytes of perfectly
  64. * ordinary data.
  65. *
  66. * - urgent==1. `data' points to `len' bytes of data,
  67. * which were read from before an Urgent pointer.
  68. *
  69. * - urgent==2. `data' points to `len' bytes of data,
  70. * the first of which was the one at the Urgent mark.
  71. */
  72. void (*sent) (Plug p, int bufsize);
  73. /*
  74. * The `sent' function is called when the pending send backlog
  75. * on a socket is cleared or partially cleared. The new backlog
  76. * size is passed in the `bufsize' parameter.
  77. */
  78. int (*accepting)(Plug p, accept_fn_t constructor, accept_ctx_t ctx);
  79. /*
  80. * `accepting' is called only on listener-type sockets, and is
  81. * passed a constructor function+context that will create a fresh
  82. * Socket describing the connection. It returns nonzero if it
  83. * doesn't want the connection for some reason, or 0 on success.
  84. */
  85. };
  86. /* proxy indirection layer */
  87. /* NB, control of 'addr' is passed via new_connection, which takes
  88. * responsibility for freeing it */
  89. Socket new_connection(SockAddr addr, char *hostname,
  90. int port, int privport,
  91. int oobinline, int nodelay, int keepalive,
  92. Plug plug, Conf *conf);
  93. Socket new_listener(char *srcaddr, int port, Plug plug, int local_host_only,
  94. Conf *conf, int addressfamily);
  95. SockAddr name_lookup(char *host, int port, char **canonicalname,
  96. Conf *conf, int addressfamily);
  97. int proxy_for_destination (SockAddr addr, const char *hostname, int port,
  98. Conf *conf);
  99. /* platform-dependent callback from new_connection() */
  100. /* (same caveat about addr as new_connection()) */
  101. Socket platform_new_connection(SockAddr addr, char *hostname,
  102. int port, int privport,
  103. int oobinline, int nodelay, int keepalive,
  104. Plug plug, Conf *conf);
  105. /* socket functions */
  106. void sk_init(void); /* called once at program startup */
  107. void sk_cleanup(void); /* called just before program exit */
  108. SockAddr sk_namelookup(const char *host, char **canonicalname, int address_family);
  109. SockAddr sk_nonamelookup(const char *host);
  110. void sk_getaddr(SockAddr addr, char *buf, int buflen);
  111. int sk_addr_needs_port(SockAddr addr);
  112. int sk_hostname_is_local(const char *name);
  113. int sk_address_is_local(SockAddr addr);
  114. int sk_address_is_special_local(SockAddr addr);
  115. int sk_addrtype(SockAddr addr);
  116. void sk_addrcopy(SockAddr addr, char *buf);
  117. void sk_addr_free(SockAddr addr);
  118. /* sk_addr_dup generates another SockAddr which contains the same data
  119. * as the original one and can be freed independently. May not actually
  120. * physically _duplicate_ it: incrementing a reference count so that
  121. * one more free is required before it disappears is an acceptable
  122. * implementation. */
  123. SockAddr sk_addr_dup(SockAddr addr);
  124. #ifdef MPEXT
  125. // Resolve ambiguity with OpenSSL
  126. #define sk_new putty_sk_new
  127. #endif
  128. /* NB, control of 'addr' is passed via sk_new, which takes responsibility
  129. * for freeing it, as for new_connection() */
  130. Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
  131. int nodelay, int keepalive, Plug p,
  132. #ifdef MPEXT
  133. int timeout,
  134. int sndbuf
  135. #endif
  136. );
  137. Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only, int address_family);
  138. #define sk_plug(s,p) (((*s)->plug) (s, p))
  139. #define sk_close(s) (((*s)->close) (s))
  140. #define sk_write(s,buf,len) (((*s)->write) (s, buf, len))
  141. #define sk_write_oob(s,buf,len) (((*s)->write_oob) (s, buf, len))
  142. #define sk_write_eof(s) (((*s)->write_eof) (s))
  143. #define sk_flush(s) (((*s)->flush) (s))
  144. #ifdef DEFINE_PLUG_METHOD_MACROS
  145. #define plug_log(p,type,addr,port,msg,code) (((*p)->log) (p, type, addr, port, msg, code))
  146. #define plug_closing(p,msg,code,callback) (((*p)->closing) (p, msg, code, callback))
  147. #define plug_receive(p,urgent,buf,len) (((*p)->receive) (p, urgent, buf, len))
  148. #define plug_sent(p,bufsize) (((*p)->sent) (p, bufsize))
  149. #define plug_accepting(p, constructor, ctx) (((*p)->accepting)(p, constructor, ctx))
  150. #endif
  151. /*
  152. * Special error values are returned from sk_namelookup and sk_new
  153. * if there's a problem. These functions extract an error message,
  154. * or return NULL if there's no problem.
  155. */
  156. const char *sk_addr_error(SockAddr addr);
  157. #define sk_socket_error(s) (((*s)->socket_error) (s))
  158. /*
  159. * Set the `frozen' flag on a socket. A frozen socket is one in
  160. * which all READABLE notifications are ignored, so that data is
  161. * not accepted from the peer until the socket is unfrozen. This
  162. * exists for two purposes:
  163. *
  164. * - Port forwarding: when a local listening port receives a
  165. * connection, we do not want to receive data from the new
  166. * socket until we have somewhere to send it. Hence, we freeze
  167. * the socket until its associated SSH channel is ready; then we
  168. * unfreeze it and pending data is delivered.
  169. *
  170. * - Socket buffering: if an SSH channel (or the whole connection)
  171. * backs up or presents a zero window, we must freeze the
  172. * associated local socket in order to avoid unbounded buffer
  173. * growth.
  174. */
  175. #define sk_set_frozen(s, is_frozen) (((*s)->set_frozen) (s, is_frozen))
  176. /*
  177. * Return a (dynamically allocated) string giving some information
  178. * about the other end of the socket, suitable for putting in log
  179. * files. May be NULL if nothing is available at all.
  180. */
  181. #define sk_peer_info(s) (((*s)->peer_info) (s))
  182. /*
  183. * Simple wrapper on getservbyname(), needed by ssh.c. Returns the
  184. * port number, in host byte order (suitable for printf and so on).
  185. * Returns 0 on failure. Any platform not supporting getservbyname
  186. * can just return 0 - this function is not required to handle
  187. * numeric port specifications.
  188. */
  189. int net_service_lookup(char *service);
  190. /*
  191. * Look up the local hostname; return value needs freeing.
  192. * May return NULL.
  193. */
  194. char *get_hostname(void);
  195. /*
  196. * Trivial socket implementation which just stores an error. Found in
  197. * errsock.c.
  198. */
  199. Socket new_error_socket(const char *errmsg, Plug plug);
  200. /********** SSL stuff **********/
  201. /*
  202. * This section is subject to change, but you get the general idea
  203. * of what it will eventually look like.
  204. */
  205. typedef struct certificate *Certificate;
  206. typedef struct our_certificate *Our_Certificate;
  207. /* to be defined somewhere else, somehow */
  208. typedef struct ssl_client_socket_function_table **SSL_Client_Socket;
  209. typedef struct ssl_client_plug_function_table **SSL_Client_Plug;
  210. struct ssl_client_socket_function_table {
  211. struct socket_function_table base;
  212. void (*renegotiate) (SSL_Client_Socket s);
  213. /* renegotiate the cipher spec */
  214. };
  215. struct ssl_client_plug_function_table {
  216. struct plug_function_table base;
  217. int (*refuse_cert) (SSL_Client_Plug p, Certificate cert[]);
  218. /* do we accept this certificate chain? If not, why not? */
  219. /* cert[0] is the server's certificate, cert[] is NULL-terminated */
  220. /* the last certificate may or may not be the root certificate */
  221. Our_Certificate(*client_cert) (SSL_Client_Plug p);
  222. /* the server wants us to identify ourselves */
  223. /* may return NULL if we want anonymity */
  224. };
  225. SSL_Client_Socket sk_ssl_client_over(Socket s, /* pre-existing (tcp) connection */
  226. SSL_Client_Plug p);
  227. #define sk_renegotiate(s) (((*s)->renegotiate) (s))
  228. #endif