network.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. #ifdef MPEXT
  127. // Resolve ambiguity with OpenSSL
  128. #define sk_new putty_sk_new
  129. #endif
  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, bool privport, bool oobinline,
  133. bool nodelay, bool keepalive, Plug *p,
  134. #ifdef MPEXT
  135. int timeout,
  136. int sndbuf,
  137. const char *srcaddr
  138. #endif
  139. );
  140. Socket *sk_newlistener(const char *srcaddr, int port, Plug *plug,
  141. bool local_host_only, int address_family);
  142. static inline Plug *sk_plug(Socket *s, Plug *p)
  143. { return s->vt->plug(s, p); }
  144. static inline void sk_close(Socket *s)
  145. { s->vt->close(s); }
  146. static inline size_t sk_write(Socket *s, const void *data, size_t len)
  147. { return s->vt->write(s, data, len); }
  148. static inline size_t sk_write_oob(Socket *s, const void *data, size_t len)
  149. { return s->vt->write_oob(s, data, len); }
  150. static inline void sk_write_eof(Socket *s)
  151. { s->vt->write_eof(s); }
  152. static inline void plug_log(
  153. Plug *p, int type, SockAddr *addr, int port, const char *msg, int code)
  154. { p->vt->log(p, type, addr, port, msg, code); }
  155. static inline void plug_closing(
  156. Plug *p, const char *msg, int code, bool calling_back)
  157. { p->vt->closing(p, msg, code, calling_back); }
  158. static inline void plug_receive(Plug *p, int urg, const char *data, size_t len)
  159. { p->vt->receive(p, urg, data, len); }
  160. static inline void plug_sent (Plug *p, size_t bufsize)
  161. { p->vt->sent(p, bufsize); }
  162. static inline int plug_accepting(Plug *p, accept_fn_t cons, accept_ctx_t ctx)
  163. { return p->vt->accepting(p, cons, ctx); }
  164. /*
  165. * Special error values are returned from sk_namelookup and sk_new
  166. * if there's a problem. These functions extract an error message,
  167. * or return NULL if there's no problem.
  168. */
  169. const char *sk_addr_error(SockAddr *addr);
  170. static inline const char *sk_socket_error(Socket *s)
  171. { return s->vt->socket_error(s); }
  172. /*
  173. * Set the `frozen' flag on a socket. A frozen socket is one in
  174. * which all READABLE notifications are ignored, so that data is
  175. * not accepted from the peer until the socket is unfrozen. This
  176. * exists for two purposes:
  177. *
  178. * - Port forwarding: when a local listening port receives a
  179. * connection, we do not want to receive data from the new
  180. * socket until we have somewhere to send it. Hence, we freeze
  181. * the socket until its associated SSH channel is ready; then we
  182. * unfreeze it and pending data is delivered.
  183. *
  184. * - Socket buffering: if an SSH channel (or the whole connection)
  185. * backs up or presents a zero window, we must freeze the
  186. * associated local socket in order to avoid unbounded buffer
  187. * growth.
  188. */
  189. static inline void sk_set_frozen(Socket *s, bool is_frozen)
  190. { s->vt->set_frozen(s, is_frozen); }
  191. /*
  192. * Return a structure giving some information about the other end of
  193. * the socket. May be NULL, if nothing is available at all. If it is
  194. * not NULL, then it is dynamically allocated, and should be freed by
  195. * a call to sk_free_peer_info(). See below for the definition.
  196. */
  197. static inline SocketPeerInfo *sk_peer_info(Socket *s)
  198. { return s->vt->peer_info(s); }
  199. /*
  200. * The structure returned from sk_peer_info, and a function to free
  201. * one (in misc.c).
  202. */
  203. struct SocketPeerInfo {
  204. int addressfamily;
  205. /*
  206. * Text form of the IPv4 or IPv6 address of the other end of the
  207. * socket, if available, in the standard text representation.
  208. */
  209. const char *addr_text;
  210. /*
  211. * Binary form of the same address. Filled in if and only if
  212. * addr_text is not NULL. You can tell which branch of the union
  213. * is used by examining 'addressfamily'.
  214. */
  215. union {
  216. unsigned char ipv6[16];
  217. unsigned char ipv4[4];
  218. } addr_bin;
  219. /*
  220. * Remote port number, or -1 if not available.
  221. */
  222. int port;
  223. /*
  224. * Free-form text suitable for putting in log messages. For IP
  225. * sockets, repeats the address and port information from above.
  226. * But it can be completely different, e.g. for Unix-domain
  227. * sockets it gives information about the uid, gid and pid of the
  228. * connecting process.
  229. */
  230. const char *log_text;
  231. };
  232. void sk_free_peer_info(SocketPeerInfo *pi);
  233. /*
  234. * Simple wrapper on getservbyname(), needed by ssh.c. Returns the
  235. * port number, in host byte order (suitable for printf and so on).
  236. * Returns 0 on failure. Any platform not supporting getservbyname
  237. * can just return 0 - this function is not required to handle
  238. * numeric port specifications.
  239. */
  240. int net_service_lookup(char *service);
  241. /*
  242. * Look up the local hostname; return value needs freeing.
  243. * May return NULL.
  244. */
  245. char *get_hostname(void);
  246. /*
  247. * Trivial socket implementation which just stores an error. Found in
  248. * errsock.c.
  249. */
  250. Socket *new_error_socket_fmt(Plug *plug, const char *fmt, ...)
  251. PRINTF_LIKE(2, 3);
  252. /*
  253. * Trivial plug that does absolutely nothing. Found in nullplug.c.
  254. */
  255. extern Plug *const nullplug;
  256. /* ----------------------------------------------------------------------
  257. * Functions defined outside the network code, which have to be
  258. * declared in this header file rather than the main putty.h because
  259. * they use types defined here.
  260. */
  261. /*
  262. * Exports from be_misc.c.
  263. */
  264. void backend_socket_log(Seat *seat, LogContext *logctx,
  265. int type, SockAddr *addr, int port,
  266. const char *error_msg, int error_code, Conf *conf,
  267. bool session_started);
  268. typedef struct ProxyStderrBuf {
  269. char buf[8192];
  270. size_t size;
  271. } ProxyStderrBuf;
  272. void psb_init(ProxyStderrBuf *psb);
  273. void log_proxy_stderr(
  274. Plug *plug, ProxyStderrBuf *psb, const void *vdata, size_t len);
  275. #endif