network.h 9.4 KB

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