network.h 12 KB

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