defs.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* Copyright StrongLoop, Inc. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #ifndef DEFS_H_
  22. #define DEFS_H_
  23. #include "s5.h"
  24. #include "uv.h"
  25. #include <assert.h>
  26. #include <netinet/in.h> /* sockaddr_in, sockaddr_in6 */
  27. #include <stddef.h> /* size_t, ssize_t */
  28. #include <stdint.h>
  29. #include <sys/socket.h> /* sockaddr */
  30. struct client_ctx;
  31. typedef struct {
  32. const char *bind_host;
  33. unsigned short bind_port;
  34. unsigned int idle_timeout;
  35. } server_config;
  36. typedef struct {
  37. unsigned int idle_timeout; /* Connection idle timeout in ms. */
  38. uv_tcp_t tcp_handle;
  39. uv_loop_t *loop;
  40. } server_ctx;
  41. typedef struct {
  42. unsigned char rdstate;
  43. unsigned char wrstate;
  44. unsigned int idle_timeout;
  45. struct client_ctx *client; /* Backlink to owning client context. */
  46. ssize_t result;
  47. union {
  48. uv_handle_t handle;
  49. uv_stream_t stream;
  50. uv_tcp_t tcp;
  51. uv_udp_t udp;
  52. } handle;
  53. uv_timer_t timer_handle; /* For detecting timeouts. */
  54. uv_write_t write_req;
  55. /* We only need one of these at a time so make them share memory. */
  56. union {
  57. uv_getaddrinfo_t addrinfo_req;
  58. uv_connect_t connect_req;
  59. uv_req_t req;
  60. struct sockaddr_in6 addr6;
  61. struct sockaddr_in addr4;
  62. struct sockaddr addr;
  63. char buf[2048]; /* Scratch space. Used to read data into. */
  64. } t;
  65. } conn;
  66. typedef struct client_ctx {
  67. unsigned int state;
  68. server_ctx *sx; /* Backlink to owning server context. */
  69. s5_ctx parser; /* The SOCKS protocol parser. */
  70. conn incoming; /* Connection with the SOCKS client. */
  71. conn outgoing; /* Connection with upstream. */
  72. } client_ctx;
  73. /* server.c */
  74. int server_run(const server_config *cf, uv_loop_t *loop);
  75. int can_auth_none(const server_ctx *sx, const client_ctx *cx);
  76. int can_auth_passwd(const server_ctx *sx, const client_ctx *cx);
  77. int can_access(const server_ctx *sx,
  78. const client_ctx *cx,
  79. const struct sockaddr *addr);
  80. /* client.c */
  81. void client_finish_init(server_ctx *sx, client_ctx *cx);
  82. /* util.c */
  83. #if defined(__GNUC__)
  84. # define ATTRIBUTE_FORMAT_PRINTF(a, b) __attribute__((format(printf, a, b)))
  85. #else
  86. # define ATTRIBUTE_FORMAT_PRINTF(a, b)
  87. #endif
  88. void pr_info(const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
  89. void pr_warn(const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
  90. void pr_err(const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
  91. void *xmalloc(size_t size);
  92. /* main.c */
  93. const char *_getprogname(void);
  94. /* getopt.c */
  95. #if !HAVE_UNISTD_H
  96. extern char *optarg;
  97. int getopt(int argc, char **argv, const char *options);
  98. #endif
  99. /* ASSERT() is for debug checks, CHECK() for run-time sanity checks.
  100. * DEBUG_CHECKS is for expensive debug checks that we only want to
  101. * enable in debug builds but still want type-checked by the compiler
  102. * in release builds.
  103. */
  104. #if defined(NDEBUG)
  105. # define ASSERT(exp)
  106. # define CHECK(exp) do { if (!(exp)) abort(); } while (0)
  107. # define DEBUG_CHECKS (0)
  108. #else
  109. # define ASSERT(exp) assert(exp)
  110. # define CHECK(exp) assert(exp)
  111. # define DEBUG_CHECKS (1)
  112. #endif
  113. #define UNREACHABLE() CHECK(!"Unreachable code reached.")
  114. /* This macro looks complicated but it's not: it calculates the address
  115. * of the embedding struct through the address of the embedded struct.
  116. * In other words, if struct A embeds struct B, then we can obtain
  117. * the address of A by taking the address of B and subtracting the
  118. * field offset of B in A.
  119. */
  120. #define CONTAINER_OF(ptr, type, field) \
  121. ((type *) ((char *) (ptr) - ((char *) &((type *) 0)->field)))
  122. #endif /* DEFS_H_ */