network.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. typedef struct SocketVtable SocketVtable;
  16. typedef struct PlugVtable PlugVtable;
  17. struct Socket {
  18. const struct SocketVtable *vt;
  19. };
  20. struct SocketVtable {
  21. Plug *(*plug) (Socket *s, Plug *p);
  22. /* use a different plug (return the old one) */
  23. /* if p is NULL, it doesn't change the plug */
  24. /* but it does return the one it's using */
  25. void (*close) (Socket *s);
  26. int (*write) (Socket *s, const void *data, int len);
  27. int (*write_oob) (Socket *s, const void *data, int len);
  28. void (*write_eof) (Socket *s);
  29. void (*flush) (Socket *s);
  30. void (*set_frozen) (Socket *s, bool is_frozen);
  31. /* ignored by tcp, but vital for ssl */
  32. const char *(*socket_error) (Socket *s);
  33. SocketPeerInfo *(*peer_info) (Socket *s);
  34. };
  35. typedef union { void *p; int i; } accept_ctx_t;
  36. typedef Socket *(*accept_fn_t)(accept_ctx_t ctx, Plug *plug);
  37. struct Plug {
  38. const struct PlugVtable *vt;
  39. };
  40. struct PlugVtable {
  41. void (*log)(Plug *p, int type, SockAddr *addr, int port,
  42. const char *error_msg, int error_code);
  43. /*
  44. * Passes the client progress reports on the process of setting
  45. * up the connection.
  46. *
  47. * - type==0 means we are about to try to connect to address
  48. * `addr' (error_msg and error_code are ignored)
  49. * - type==1 means we have failed to connect to address `addr'
  50. * (error_msg and error_code are supplied). This is not a
  51. * fatal error - we may well have other candidate addresses
  52. * to fall back to. When it _is_ fatal, the closing()
  53. * function will be called.
  54. * - type==2 means that error_msg contains a line of generic
  55. * logging information about setting up the connection. This
  56. * will typically be a wodge of standard-error output from a
  57. * proxy command, so the receiver should probably prefix it to
  58. * indicate this.
  59. */
  60. void (*closing)
  61. (Plug *p, const char *error_msg, int error_code, bool calling_back);
  62. /* error_msg is NULL iff it is not an error (ie it closed normally) */
  63. /* calling_back != 0 iff there is a Plug function */
  64. /* currently running (would cure the fixme in try_send()) */
  65. void (*receive) (Plug *p, int urgent, char *data, int len);
  66. /*
  67. * - urgent==0. `data' points to `len' bytes of perfectly
  68. * ordinary data.
  69. *
  70. * - urgent==1. `data' points to `len' bytes of data,
  71. * which were read from before an Urgent pointer.
  72. *
  73. * - urgent==2. `data' points to `len' bytes of data,
  74. * the first of which was the one at the Urgent mark.
  75. */
  76. void (*sent) (Plug *p, int bufsize);
  77. /*
  78. * The `sent' function is called when the pending send backlog
  79. * on a socket is cleared or partially cleared. The new backlog
  80. * size is passed in the `bufsize' parameter.
  81. */
  82. int (*accepting)(Plug *p, accept_fn_t constructor, accept_ctx_t ctx);
  83. /*
  84. * `accepting' is called only on listener-type sockets, and is
  85. * passed a constructor function+context that will create a fresh
  86. * Socket describing the connection. It returns nonzero if it
  87. * doesn't want the connection for some reason, or 0 on success.
  88. */
  89. };
  90. /* proxy indirection layer */
  91. /* NB, control of 'addr' is passed via new_connection, which takes
  92. * responsibility for freeing it */
  93. Socket *new_connection(SockAddr *addr, const char *hostname,
  94. int port, bool privport,
  95. bool oobinline, bool nodelay, bool keepalive,
  96. Plug *plug, Conf *conf);
  97. Socket *new_listener(const char *srcaddr, int port, Plug *plug,
  98. bool local_host_only, Conf *conf, int addressfamily);
  99. SockAddr *name_lookup(const char *host, int port, char **canonicalname,
  100. Conf *conf, int addressfamily, LogContext *logctx,
  101. const char *lookup_reason_for_logging);
  102. bool proxy_for_destination (SockAddr *addr, const char *hostname, int port,
  103. Conf *conf);
  104. /* platform-dependent callback from new_connection() */
  105. /* (same caveat about addr as new_connection()) */
  106. Socket *platform_new_connection(SockAddr *addr, const char *hostname,
  107. int port, bool privport,
  108. bool oobinline, bool nodelay, bool keepalive,
  109. Plug *plug, Conf *conf);
  110. /* socket functions */
  111. void sk_init(void); /* called once at program startup */
  112. void sk_cleanup(void); /* called just before program exit */
  113. SockAddr *sk_namelookup(const char *host, char **canonicalname, int address_family);
  114. SockAddr *sk_nonamelookup(const char *host);
  115. void sk_getaddr(SockAddr *addr, char *buf, int buflen);
  116. bool sk_addr_needs_port(SockAddr *addr);
  117. bool sk_hostname_is_local(const char *name);
  118. bool sk_address_is_local(SockAddr *addr);
  119. bool sk_address_is_special_local(SockAddr *addr);
  120. int sk_addrtype(SockAddr *addr);
  121. void sk_addrcopy(SockAddr *addr, char *buf);
  122. void sk_addr_free(SockAddr *addr);
  123. /* sk_addr_dup generates another SockAddr which contains the same data
  124. * as the original one and can be freed independently. May not actually
  125. * physically _duplicate_ it: incrementing a reference count so that
  126. * one more free is required before it disappears is an acceptable
  127. * implementation. */
  128. SockAddr *sk_addr_dup(SockAddr *addr);
  129. /* NB, control of 'addr' is passed via sk_new, which takes responsibility
  130. * for freeing it, as for new_connection() */
  131. Socket *sk_new(SockAddr *addr, int port, bool privport, bool oobinline,
  132. bool nodelay, bool keepalive, Plug *p);
  133. Socket *sk_newlistener(const char *srcaddr, int port, Plug *plug,
  134. bool local_host_only, int address_family);
  135. #define sk_plug(s,p) (((s)->vt->plug) (s, p))
  136. #define sk_close(s) (((s)->vt->close) (s))
  137. #define sk_write(s,buf,len) (((s)->vt->write) (s, buf, len))
  138. #define sk_write_oob(s,buf,len) (((s)->vt->write_oob) (s, buf, len))
  139. #define sk_write_eof(s) (((s)->vt->write_eof) (s))
  140. #define sk_flush(s) (((s)->vt->flush) (s))
  141. #define plug_log(p,type,addr,port,msg,code) \
  142. (((p)->vt->log) (p, type, addr, port, msg, code))
  143. #define plug_closing(p,msg,code,callback) \
  144. (((p)->vt->closing) (p, msg, code, callback))
  145. #define plug_receive(p,urgent,buf,len) \
  146. (((p)->vt->receive) (p, urgent, buf, len))
  147. #define plug_sent(p,bufsize) \
  148. (((p)->vt->sent) (p, bufsize))
  149. #define plug_accepting(p, constructor, ctx) \
  150. (((p)->vt->accepting)(p, constructor, ctx))
  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)->vt->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)->vt->set_frozen) (s, is_frozen))
  176. /*
  177. * Return a structure giving some information about the other end of
  178. * the socket. May be NULL, if nothing is available at all. If it is
  179. * not NULL, then it is dynamically allocated, and should be freed by
  180. * a call to sk_free_peer_info(). See below for the definition.
  181. */
  182. #define sk_peer_info(s) (((s)->vt->peer_info) (s))
  183. /*
  184. * The structure returned from sk_peer_info, and a function to free
  185. * one (in misc.c).
  186. */
  187. struct SocketPeerInfo {
  188. int addressfamily;
  189. /*
  190. * Text form of the IPv4 or IPv6 address of the other end of the
  191. * socket, if available, in the standard text representation.
  192. */
  193. const char *addr_text;
  194. /*
  195. * Binary form of the same address. Filled in if and only if
  196. * addr_text is not NULL. You can tell which branch of the union
  197. * is used by examining 'addressfamily'.
  198. */
  199. union {
  200. unsigned char ipv6[16];
  201. unsigned char ipv4[4];
  202. } addr_bin;
  203. /*
  204. * Remote port number, or -1 if not available.
  205. */
  206. int port;
  207. /*
  208. * Free-form text suitable for putting in log messages. For IP
  209. * sockets, repeats the address and port information from above.
  210. * But it can be completely different, e.g. for Unix-domain
  211. * sockets it gives information about the uid, gid and pid of the
  212. * connecting process.
  213. */
  214. const char *log_text;
  215. };
  216. void sk_free_peer_info(SocketPeerInfo *pi);
  217. /*
  218. * Simple wrapper on getservbyname(), needed by ssh.c. Returns the
  219. * port number, in host byte order (suitable for printf and so on).
  220. * Returns 0 on failure. Any platform not supporting getservbyname
  221. * can just return 0 - this function is not required to handle
  222. * numeric port specifications.
  223. */
  224. int net_service_lookup(char *service);
  225. /*
  226. * Look up the local hostname; return value needs freeing.
  227. * May return NULL.
  228. */
  229. char *get_hostname(void);
  230. /*
  231. * Trivial socket implementation which just stores an error. Found in
  232. * errsock.c.
  233. */
  234. Socket *new_error_socket_fmt(Plug *plug, const char *fmt, ...);
  235. /*
  236. * Trivial plug that does absolutely nothing. Found in nullplug.c.
  237. */
  238. extern Plug *const nullplug;
  239. /* ----------------------------------------------------------------------
  240. * Functions defined outside the network code, which have to be
  241. * declared in this header file rather than the main putty.h because
  242. * they use types defined here.
  243. */
  244. /*
  245. * Exports from be_misc.c.
  246. */
  247. void backend_socket_log(Seat *seat, LogContext *logctx,
  248. int type, SockAddr *addr, int port,
  249. const char *error_msg, int error_code, Conf *conf,
  250. bool session_started);
  251. void log_proxy_stderr(Plug *plug, bufchain *buf, const void *vdata, int len);
  252. #endif