Blame samples/socks5-proxy/defs.h

Packit b5b901
/* Copyright StrongLoop, Inc. All rights reserved.
Packit b5b901
 *
Packit b5b901
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit b5b901
 * of this software and associated documentation files (the "Software"), to
Packit b5b901
 * deal in the Software without restriction, including without limitation the
Packit b5b901
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit b5b901
 * sell copies of the Software, and to permit persons to whom the Software is
Packit b5b901
 * furnished to do so, subject to the following conditions:
Packit b5b901
 *
Packit b5b901
 * The above copyright notice and this permission notice shall be included in
Packit b5b901
 * all copies or substantial portions of the Software.
Packit b5b901
 *
Packit b5b901
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit b5b901
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit b5b901
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit b5b901
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit b5b901
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit b5b901
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit b5b901
 * IN THE SOFTWARE.
Packit b5b901
 */
Packit b5b901
Packit b5b901
#ifndef DEFS_H_
Packit b5b901
#define DEFS_H_
Packit b5b901
Packit b5b901
#include "s5.h"
Packit b5b901
#include "uv.h"
Packit b5b901
Packit b5b901
#include <assert.h>
Packit b5b901
#include <netinet/in.h>  /* sockaddr_in, sockaddr_in6 */
Packit b5b901
#include <stddef.h>      /* size_t, ssize_t */
Packit b5b901
#include <stdint.h>
Packit b5b901
#include <sys/socket.h>  /* sockaddr */
Packit b5b901
Packit b5b901
struct client_ctx;
Packit b5b901
Packit b5b901
typedef struct {
Packit b5b901
  const char *bind_host;
Packit b5b901
  unsigned short bind_port;
Packit b5b901
  unsigned int idle_timeout;
Packit b5b901
} server_config;
Packit b5b901
Packit b5b901
typedef struct {
Packit b5b901
  unsigned int idle_timeout;  /* Connection idle timeout in ms. */
Packit b5b901
  uv_tcp_t tcp_handle;
Packit b5b901
  uv_loop_t *loop;
Packit b5b901
} server_ctx;
Packit b5b901
Packit b5b901
typedef struct {
Packit b5b901
  unsigned char rdstate;
Packit b5b901
  unsigned char wrstate;
Packit b5b901
  unsigned int idle_timeout;
Packit b5b901
  struct client_ctx *client;  /* Backlink to owning client context. */
Packit b5b901
  ssize_t result;
Packit b5b901
  union {
Packit b5b901
    uv_handle_t handle;
Packit b5b901
    uv_stream_t stream;
Packit b5b901
    uv_tcp_t tcp;
Packit b5b901
    uv_udp_t udp;
Packit b5b901
  } handle;
Packit b5b901
  uv_timer_t timer_handle;  /* For detecting timeouts. */
Packit b5b901
  uv_write_t write_req;
Packit b5b901
  /* We only need one of these at a time so make them share memory. */
Packit b5b901
  union {
Packit b5b901
    uv_getaddrinfo_t addrinfo_req;
Packit b5b901
    uv_connect_t connect_req;
Packit b5b901
    uv_req_t req;
Packit b5b901
    struct sockaddr_in6 addr6;
Packit b5b901
    struct sockaddr_in addr4;
Packit b5b901
    struct sockaddr addr;
Packit b5b901
    char buf[2048];  /* Scratch space. Used to read data into. */
Packit b5b901
  } t;
Packit b5b901
} conn;
Packit b5b901
Packit b5b901
typedef struct client_ctx {
Packit b5b901
  unsigned int state;
Packit b5b901
  server_ctx *sx;  /* Backlink to owning server context. */
Packit b5b901
  s5_ctx parser;  /* The SOCKS protocol parser. */
Packit b5b901
  conn incoming;  /* Connection with the SOCKS client. */
Packit b5b901
  conn outgoing;  /* Connection with upstream. */
Packit b5b901
} client_ctx;
Packit b5b901
Packit b5b901
/* server.c */
Packit b5b901
int server_run(const server_config *cf, uv_loop_t *loop);
Packit b5b901
int can_auth_none(const server_ctx *sx, const client_ctx *cx);
Packit b5b901
int can_auth_passwd(const server_ctx *sx, const client_ctx *cx);
Packit b5b901
int can_access(const server_ctx *sx,
Packit b5b901
               const client_ctx *cx,
Packit b5b901
               const struct sockaddr *addr);
Packit b5b901
Packit b5b901
/* client.c */
Packit b5b901
void client_finish_init(server_ctx *sx, client_ctx *cx);
Packit b5b901
Packit b5b901
/* util.c */
Packit b5b901
#if defined(__GNUC__)
Packit b5b901
# define ATTRIBUTE_FORMAT_PRINTF(a, b) __attribute__((format(printf, a, b)))
Packit b5b901
#else
Packit b5b901
# define ATTRIBUTE_FORMAT_PRINTF(a, b)
Packit b5b901
#endif
Packit b5b901
void pr_info(const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
Packit b5b901
void pr_warn(const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
Packit b5b901
void pr_err(const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
Packit b5b901
void *xmalloc(size_t size);
Packit b5b901
Packit b5b901
/* main.c */
Packit b5b901
const char *_getprogname(void);
Packit b5b901
Packit b5b901
/* getopt.c */
Packit b5b901
#if !HAVE_UNISTD_H
Packit b5b901
extern char *optarg;
Packit b5b901
int getopt(int argc, char **argv, const char *options);
Packit b5b901
#endif
Packit b5b901
Packit b5b901
/* ASSERT() is for debug checks, CHECK() for run-time sanity checks.
Packit b5b901
 * DEBUG_CHECKS is for expensive debug checks that we only want to
Packit b5b901
 * enable in debug builds but still want type-checked by the compiler
Packit b5b901
 * in release builds.
Packit b5b901
 */
Packit b5b901
#if defined(NDEBUG)
Packit b5b901
# define ASSERT(exp)
Packit b5b901
# define CHECK(exp)   do { if (!(exp)) abort(); } while (0)
Packit b5b901
# define DEBUG_CHECKS (0)
Packit b5b901
#else
Packit b5b901
# define ASSERT(exp)  assert(exp)
Packit b5b901
# define CHECK(exp)   assert(exp)
Packit b5b901
# define DEBUG_CHECKS (1)
Packit b5b901
#endif
Packit b5b901
Packit b5b901
#define UNREACHABLE() CHECK(!"Unreachable code reached.")
Packit b5b901
Packit b5b901
/* This macro looks complicated but it's not: it calculates the address
Packit b5b901
 * of the embedding struct through the address of the embedded struct.
Packit b5b901
 * In other words, if struct A embeds struct B, then we can obtain
Packit b5b901
 * the address of A by taking the address of B and subtracting the
Packit b5b901
 * field offset of B in A.
Packit b5b901
 */
Packit b5b901
#define CONTAINER_OF(ptr, type, field)                                        \
Packit b5b901
  ((type *) ((char *) (ptr) - ((char *) &((type *) 0)->field)))
Packit b5b901
Packit b5b901
#endif  /* DEFS_H_ */