network.h 12 KB

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