src/stream/ngx_stream.h - nginx

Data types defined

Macros defined

Source code


  1. /*
  2. * Copyright (C) Roman Arutyunyan
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #ifndef _NGX_STREAM_H_INCLUDED_
  6. #define _NGX_STREAM_H_INCLUDED_


  7. #include <ngx_config.h>
  8. #include <ngx_core.h>

  9. #if (NGX_STREAM_SSL)
  10. #include <ngx_stream_ssl_module.h>
  11. #endif


  12. typedef struct ngx_stream_session_s  ngx_stream_session_t;


  13. #include <ngx_stream_variables.h>
  14. #include <ngx_stream_script.h>
  15. #include <ngx_stream_upstream.h>
  16. #include <ngx_stream_upstream_round_robin.h>


  17. #define NGX_STREAM_OK                        200
  18. #define NGX_STREAM_BAD_REQUEST               400
  19. #define NGX_STREAM_FORBIDDEN                 403
  20. #define NGX_STREAM_INTERNAL_SERVER_ERROR     500
  21. #define NGX_STREAM_BAD_GATEWAY               502
  22. #define NGX_STREAM_SERVICE_UNAVAILABLE       503


  23. typedef struct {
  24.     void                         **main_conf;
  25.     void                         **srv_conf;
  26. } ngx_stream_conf_ctx_t;


  27. typedef struct {
  28.     struct sockaddr               *sockaddr;
  29.     socklen_t                      socklen;
  30.     ngx_str_t                      addr_text;

  31.     unsigned                       set:1;
  32.     unsigned                       default_server:1;
  33.     unsigned                       bind:1;
  34.     unsigned                       wildcard:1;
  35.     unsigned                       ssl:1;
  36. #if (NGX_HAVE_INET6)
  37.     unsigned                       ipv6only:1;
  38. #endif
  39.     unsigned                       deferred_accept:1;
  40.     unsigned                       reuseport:1;
  41.     unsigned                       so_keepalive:2;
  42.     unsigned                       proxy_protocol:1;

  43.     int                            backlog;
  44.     int                            rcvbuf;
  45.     int                            sndbuf;
  46.     int                            type;
  47.     int                            protocol;
  48. #if (NGX_HAVE_SETFIB)
  49.     int                            setfib;
  50. #endif
  51. #if (NGX_HAVE_TCP_FASTOPEN)
  52.     int                            fastopen;
  53. #endif
  54. #if (NGX_HAVE_KEEPALIVE_TUNABLE)
  55.     int                            tcp_keepidle;
  56.     int                            tcp_keepintvl;
  57.     int                            tcp_keepcnt;
  58. #endif

  59. #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
  60.     char                          *accept_filter;
  61. #endif
  62. } ngx_stream_listen_opt_t;


  63. typedef enum {
  64.     NGX_STREAM_POST_ACCEPT_PHASE = 0,
  65.     NGX_STREAM_PREACCESS_PHASE,
  66.     NGX_STREAM_ACCESS_PHASE,
  67.     NGX_STREAM_SSL_PHASE,
  68.     NGX_STREAM_PREREAD_PHASE,
  69.     NGX_STREAM_CONTENT_PHASE,
  70.     NGX_STREAM_LOG_PHASE
  71. } ngx_stream_phases;


  72. typedef struct ngx_stream_phase_handler_s  ngx_stream_phase_handler_t;

  73. typedef ngx_int_t (*ngx_stream_phase_handler_pt)(ngx_stream_session_t *s,
  74.     ngx_stream_phase_handler_t *ph);
  75. typedef ngx_int_t (*ngx_stream_handler_pt)(ngx_stream_session_t *s);
  76. typedef void (*ngx_stream_content_handler_pt)(ngx_stream_session_t *s);


  77. struct ngx_stream_phase_handler_s {
  78.     ngx_stream_phase_handler_pt    checker;
  79.     ngx_stream_handler_pt          handler;
  80.     ngx_uint_t                     next;
  81. };


  82. typedef struct {
  83.     ngx_stream_phase_handler_t    *handlers;
  84. } ngx_stream_phase_engine_t;


  85. typedef struct {
  86.     ngx_array_t                    handlers;
  87. } ngx_stream_phase_t;


  88. typedef struct {
  89.     ngx_array_t                    servers;     /* ngx_stream_core_srv_conf_t */

  90.     ngx_stream_phase_engine_t      phase_engine;

  91.     ngx_hash_t                     variables_hash;

  92.     ngx_array_t                    variables;        /* ngx_stream_variable_t */
  93.     ngx_array_t                    prefix_variables; /* ngx_stream_variable_t */
  94.     ngx_uint_t                     ncaptures;

  95.     ngx_uint_t                     server_names_hash_max_size;
  96.     ngx_uint_t                     server_names_hash_bucket_size;

  97.     ngx_uint_t                     variables_hash_max_size;
  98.     ngx_uint_t                     variables_hash_bucket_size;

  99.     ngx_hash_keys_arrays_t        *variables_keys;

  100.     ngx_array_t                   *ports;

  101.     ngx_stream_phase_t             phases[NGX_STREAM_LOG_PHASE + 1];
  102. } ngx_stream_core_main_conf_t;


  103. typedef struct {
  104.     /* array of the ngx_stream_server_name_t, "server_name" directive */
  105.     ngx_array_t                    server_names;

  106.     ngx_stream_content_handler_pt  handler;

  107.     ngx_stream_conf_ctx_t         *ctx;

  108.     u_char                        *file_name;
  109.     ngx_uint_t                     line;

  110.     ngx_str_t                      server_name;

  111.     ngx_flag_t                     tcp_nodelay;
  112.     size_t                         preread_buffer_size;
  113.     ngx_msec_t                     preread_timeout;

  114.     ngx_log_t                     *error_log;

  115.     ngx_msec_t                     resolver_timeout;
  116.     ngx_resolver_t                *resolver;

  117.     ngx_msec_t                     proxy_protocol_timeout;

  118.     unsigned                       listen:1;
  119. #if (NGX_PCRE)
  120.     unsigned                       captures:1;
  121. #endif
  122. } ngx_stream_core_srv_conf_t;


  123. /* list of structures to find core_srv_conf quickly at run time */


  124. typedef struct {
  125. #if (NGX_PCRE)
  126.     ngx_stream_regex_t            *regex;
  127. #endif
  128.     ngx_stream_core_srv_conf_t    *server;   /* virtual name server conf */
  129.     ngx_str_t                      name;
  130. } ngx_stream_server_name_t;


  131. typedef struct {
  132.     ngx_hash_combined_t            names;

  133.     ngx_uint_t                     nregex;
  134.     ngx_stream_server_name_t      *regex;
  135. } ngx_stream_virtual_names_t;


  136. typedef struct {
  137.     /* the default server configuration for this address:port */
  138.     ngx_stream_core_srv_conf_t    *default_server;

  139.     ngx_stream_virtual_names_t    *virtual_names;

  140.     unsigned                       ssl:1;
  141.     unsigned                       proxy_protocol:1;
  142. } ngx_stream_addr_conf_t;


  143. typedef struct {
  144.     in_addr_t                      addr;
  145.     ngx_stream_addr_conf_t         conf;
  146. } ngx_stream_in_addr_t;


  147. #if (NGX_HAVE_INET6)

  148. typedef struct {
  149.     struct in6_addr                addr6;
  150.     ngx_stream_addr_conf_t         conf;
  151. } ngx_stream_in6_addr_t;

  152. #endif


  153. typedef struct {
  154.     /* ngx_stream_in_addr_t or ngx_stream_in6_addr_t */
  155.     void                          *addrs;
  156.     ngx_uint_t                     naddrs;
  157. } ngx_stream_port_t;


  158. typedef struct {
  159.     int                            family;
  160.     int                            type;
  161.     in_port_t                      port;
  162.     ngx_array_t                    addrs; /* array of ngx_stream_conf_addr_t */
  163. } ngx_stream_conf_port_t;


  164. typedef struct {
  165.     ngx_stream_listen_opt_t        opt;

  166.     unsigned                       protocols:3;
  167.     unsigned                       protocols_set:1;
  168.     unsigned                       protocols_changed:1;

  169.     ngx_hash_t                     hash;
  170.     ngx_hash_wildcard_t           *wc_head;
  171.     ngx_hash_wildcard_t           *wc_tail;

  172. #if (NGX_PCRE)
  173.     ngx_uint_t                     nregex;
  174.     ngx_stream_server_name_t      *regex;
  175. #endif

  176.     /* the default server configuration for this address:port */
  177.     ngx_stream_core_srv_conf_t    *default_server;
  178.     ngx_array_t                    servers;
  179.                                       /* array of ngx_stream_core_srv_conf_t */
  180. } ngx_stream_conf_addr_t;


  181. struct ngx_stream_session_s {
  182.     uint32_t                       signature;         /* "STRM" */

  183.     ngx_connection_t              *connection;

  184.     off_t                          received;
  185.     time_t                         start_sec;
  186.     ngx_msec_t                     start_msec;

  187.     ngx_log_handler_pt             log_handler;

  188.     void                         **ctx;
  189.     void                         **main_conf;
  190.     void                         **srv_conf;

  191.     ngx_stream_virtual_names_t    *virtual_names;

  192.     ngx_stream_upstream_t         *upstream;
  193.     ngx_array_t                   *upstream_states;
  194.                                            /* of ngx_stream_upstream_state_t */
  195.     ngx_stream_variable_value_t   *variables;

  196. #if (NGX_PCRE)
  197.     ngx_uint_t                     ncaptures;
  198.     int                           *captures;
  199.     u_char                        *captures_data;
  200. #endif

  201.     ngx_int_t                      phase_handler;
  202.     ngx_uint_t                     status;

  203.     unsigned                       ssl:1;

  204.     unsigned                       stat_processing:1;

  205.     unsigned                       health_check:1;

  206.     unsigned                       limit_conn_status:2;
  207. };


  208. typedef struct {
  209.     ngx_int_t                    (*preconfiguration)(ngx_conf_t *cf);
  210.     ngx_int_t                    (*postconfiguration)(ngx_conf_t *cf);

  211.     void                        *(*create_main_conf)(ngx_conf_t *cf);
  212.     char                        *(*init_main_conf)(ngx_conf_t *cf, void *conf);

  213.     void                        *(*create_srv_conf)(ngx_conf_t *cf);
  214.     char                        *(*merge_srv_conf)(ngx_conf_t *cf, void *prev,
  215.                                                    void *conf);
  216. } ngx_stream_module_t;


  217. #define NGX_STREAM_MODULE       0x4d525453     /* "STRM" */

  218. #define NGX_STREAM_MAIN_CONF    0x02000000
  219. #define NGX_STREAM_SRV_CONF     0x04000000
  220. #define NGX_STREAM_UPS_CONF     0x08000000


  221. #define NGX_STREAM_MAIN_CONF_OFFSET  offsetof(ngx_stream_conf_ctx_t, main_conf)
  222. #define NGX_STREAM_SRV_CONF_OFFSET   offsetof(ngx_stream_conf_ctx_t, srv_conf)


  223. #define ngx_stream_get_module_ctx(s, module)   (s)->ctx[module.ctx_index]
  224. #define ngx_stream_set_ctx(s, c, module)       s->ctx[module.ctx_index] = c;
  225. #define ngx_stream_delete_ctx(s, module)       s->ctx[module.ctx_index] = NULL;


  226. #define ngx_stream_get_module_main_conf(s, module)                             \
  227.     (s)->main_conf[module.ctx_index]
  228. #define ngx_stream_get_module_srv_conf(s, module)                              \
  229.     (s)->srv_conf[module.ctx_index]

  230. #define ngx_stream_conf_get_module_main_conf(cf, module)                       \
  231.     ((ngx_stream_conf_ctx_t *) cf->ctx)->main_conf[module.ctx_index]
  232. #define ngx_stream_conf_get_module_srv_conf(cf, module)                        \
  233.     ((ngx_stream_conf_ctx_t *) cf->ctx)->srv_conf[module.ctx_index]

  234. #define ngx_stream_cycle_get_module_main_conf(cycle, module)                   \
  235.     (cycle->conf_ctx[ngx_stream_module.index] ?                                \
  236.         ((ngx_stream_conf_ctx_t *) cycle->conf_ctx[ngx_stream_module.index])   \
  237.             ->main_conf[module.ctx_index]:                                     \
  238.         NULL)


  239. #define NGX_STREAM_WRITE_BUFFERED  0x10


  240. ngx_int_t ngx_stream_add_listen(ngx_conf_t *cf,
  241.     ngx_stream_core_srv_conf_t *cscf, ngx_stream_listen_opt_t *lsopt);

  242. void ngx_stream_core_run_phases(ngx_stream_session_t *s);
  243. ngx_int_t ngx_stream_core_generic_phase(ngx_stream_session_t *s,
  244.     ngx_stream_phase_handler_t *ph);
  245. ngx_int_t ngx_stream_core_preread_phase(ngx_stream_session_t *s,
  246.     ngx_stream_phase_handler_t *ph);
  247. ngx_int_t ngx_stream_core_content_phase(ngx_stream_session_t *s,
  248.     ngx_stream_phase_handler_t *ph);

  249. ngx_int_t ngx_stream_validate_host(ngx_str_t *host, ngx_pool_t *pool,
  250.     ngx_uint_t alloc);
  251. ngx_int_t ngx_stream_find_virtual_server(ngx_stream_session_t *s,
  252.     ngx_str_t *host, ngx_stream_core_srv_conf_t **cscfp);

  253. void ngx_stream_init_connection(ngx_connection_t *c);
  254. void ngx_stream_session_handler(ngx_event_t *rev);
  255. void ngx_stream_finalize_session(ngx_stream_session_t *s, ngx_uint_t rc);


  256. extern ngx_module_t  ngx_stream_module;
  257. extern ngx_uint_t    ngx_stream_max_module;
  258. extern ngx_module_t  ngx_stream_core_module;


  259. typedef ngx_int_t (*ngx_stream_filter_pt)(ngx_stream_session_t *s,
  260.     ngx_chain_t *chain, ngx_uint_t from_upstream);


  261. extern ngx_stream_filter_pt  ngx_stream_top_filter;


  262. #endif /* _NGX_STREAM_H_INCLUDED_ */