network.h 9.5 KB

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