network.h 11 KB

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