src/core/ngx_connection.h - nginx

Data types defined

Macros defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #ifndef _NGX_CONNECTION_H_INCLUDED_
  6. #define _NGX_CONNECTION_H_INCLUDED_


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


  9. typedef struct ngx_listening_s  ngx_listening_t;

  10. struct ngx_listening_s {
  11.     ngx_socket_t        fd;

  12.     struct sockaddr    *sockaddr;
  13.     socklen_t           socklen;    /* size of sockaddr */
  14.     size_t              addr_text_max_len;
  15.     ngx_str_t           addr_text;

  16.     int                 type;
  17.     int                 protocol;

  18.     int                 backlog;
  19.     int                 rcvbuf;
  20.     int                 sndbuf;
  21. #if (NGX_HAVE_KEEPALIVE_TUNABLE)
  22.     int                 keepidle;
  23.     int                 keepintvl;
  24.     int                 keepcnt;
  25. #endif

  26.     /* handler of accepted connection */
  27.     ngx_connection_handler_pt   handler;

  28.     void               *servers;  /* array of ngx_http_in_addr_t, for example */

  29.     ngx_log_t           log;
  30.     ngx_log_t          *logp;

  31.     size_t              pool_size;
  32.     /* should be here because of the AcceptEx() preread */
  33.     size_t              post_accept_buffer_size;

  34.     ngx_listening_t    *previous;
  35.     ngx_connection_t   *connection;

  36.     ngx_rbtree_t        rbtree;
  37.     ngx_rbtree_node_t   sentinel;

  38.     ngx_uint_t          worker;

  39.     unsigned            open:1;
  40.     unsigned            remain:1;
  41.     unsigned            ignore:1;

  42.     unsigned            bound:1;       /* already bound */
  43.     unsigned            inherited:1;   /* inherited from previous process */
  44.     unsigned            nonblocking_accept:1;
  45.     unsigned            listen:1;
  46.     unsigned            nonblocking:1;
  47.     unsigned            shared:1;    /* shared between threads or processes */
  48.     unsigned            addr_ntop:1;
  49.     unsigned            wildcard:1;

  50. #if (NGX_HAVE_INET6)
  51.     unsigned            ipv6only:1;
  52. #endif
  53.     unsigned            reuseport:1;
  54.     unsigned            add_reuseport:1;
  55.     unsigned            keepalive:2;
  56.     unsigned            quic:1;

  57.     unsigned            change_protocol:1;

  58.     unsigned            deferred_accept:1;
  59.     unsigned            delete_deferred:1;
  60.     unsigned            add_deferred:1;
  61. #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
  62.     char               *accept_filter;
  63. #endif
  64. #if (NGX_HAVE_SETFIB)
  65.     int                 setfib;
  66. #endif

  67. #if (NGX_HAVE_TCP_FASTOPEN)
  68.     int                 fastopen;
  69. #endif

  70. };


  71. typedef enum {
  72.     NGX_ERROR_ALERT = 0,
  73.     NGX_ERROR_ERR,
  74.     NGX_ERROR_INFO,
  75.     NGX_ERROR_IGNORE_ECONNRESET,
  76.     NGX_ERROR_IGNORE_EINVAL,
  77.     NGX_ERROR_IGNORE_EMSGSIZE
  78. } ngx_connection_log_error_e;


  79. typedef enum {
  80.     NGX_TCP_NODELAY_UNSET = 0,
  81.     NGX_TCP_NODELAY_SET,
  82.     NGX_TCP_NODELAY_DISABLED
  83. } ngx_connection_tcp_nodelay_e;


  84. typedef enum {
  85.     NGX_TCP_NOPUSH_UNSET = 0,
  86.     NGX_TCP_NOPUSH_SET,
  87.     NGX_TCP_NOPUSH_DISABLED
  88. } ngx_connection_tcp_nopush_e;


  89. #define NGX_LOWLEVEL_BUFFERED  0x0f
  90. #define NGX_SSL_BUFFERED       0x01
  91. #define NGX_HTTP_V2_BUFFERED   0x02


  92. struct ngx_connection_s {
  93.     void               *data;
  94.     ngx_event_t        *read;
  95.     ngx_event_t        *write;

  96.     ngx_socket_t        fd;

  97.     ngx_recv_pt         recv;
  98.     ngx_send_pt         send;
  99.     ngx_recv_chain_pt   recv_chain;
  100.     ngx_send_chain_pt   send_chain;

  101.     ngx_listening_t    *listening;

  102.     off_t               sent;

  103.     ngx_log_t          *log;

  104.     ngx_pool_t         *pool;

  105.     int                 type;

  106.     struct sockaddr    *sockaddr;
  107.     socklen_t           socklen;
  108.     ngx_str_t           addr_text;

  109.     ngx_proxy_protocol_t  *proxy_protocol;

  110. #if (NGX_QUIC || NGX_COMPAT)
  111.     ngx_quic_stream_t     *quic;
  112. #endif

  113. #if (NGX_SSL || NGX_COMPAT)
  114.     ngx_ssl_connection_t  *ssl;
  115. #endif

  116.     ngx_udp_connection_t  *udp;

  117.     struct sockaddr    *local_sockaddr;
  118.     socklen_t           local_socklen;

  119.     ngx_buf_t          *buffer;

  120.     ngx_queue_t         queue;

  121.     ngx_atomic_uint_t   number;

  122.     ngx_msec_t          start_time;
  123.     ngx_uint_t          requests;

  124.     unsigned            buffered:8;

  125.     unsigned            log_error:3;     /* ngx_connection_log_error_e */

  126.     unsigned            timedout:1;
  127.     unsigned            error:1;
  128.     unsigned            destroyed:1;
  129.     unsigned            pipeline:1;

  130.     unsigned            idle:1;
  131.     unsigned            reusable:1;
  132.     unsigned            close:1;
  133.     unsigned            shared:1;

  134.     unsigned            sendfile:1;
  135.     unsigned            sndlowat:1;
  136.     unsigned            tcp_nodelay:2;   /* ngx_connection_tcp_nodelay_e */
  137.     unsigned            tcp_nopush:2;    /* ngx_connection_tcp_nopush_e */

  138.     unsigned            need_last_buf:1;
  139.     unsigned            need_flush_buf:1;

  140. #if (NGX_HAVE_SENDFILE_NODISKIO || NGX_COMPAT)
  141.     unsigned            busy_count:2;
  142. #endif

  143. #if (NGX_THREADS || NGX_COMPAT)
  144.     ngx_thread_task_t  *sendfile_task;
  145. #endif
  146. };


  147. #define ngx_set_connection_log(c, l)                                         \
  148.                                                                              \
  149.     c->log->file = l->file;                                                  \
  150.     c->log->next = l->next;                                                  \
  151.     c->log->writer = l->writer;                                              \
  152.     c->log->wdata = l->wdata;                                                \
  153.     if (!(c->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {                   \
  154.         c->log->log_level = l->log_level;                                    \
  155.     }


  156. ngx_listening_t *ngx_create_listening(ngx_conf_t *cf, struct sockaddr *sockaddr,
  157.     socklen_t socklen);
  158. ngx_int_t ngx_clone_listening(ngx_cycle_t *cycle, ngx_listening_t *ls);
  159. ngx_int_t ngx_set_inherited_sockets(ngx_cycle_t *cycle);
  160. ngx_int_t ngx_open_listening_sockets(ngx_cycle_t *cycle);
  161. void ngx_configure_listening_sockets(ngx_cycle_t *cycle);
  162. void ngx_close_listening_sockets(ngx_cycle_t *cycle);
  163. void ngx_close_connection(ngx_connection_t *c);
  164. void ngx_close_idle_connections(ngx_cycle_t *cycle);
  165. ngx_int_t ngx_connection_local_sockaddr(ngx_connection_t *c, ngx_str_t *s,
  166.     ngx_uint_t port);
  167. ngx_int_t ngx_tcp_nodelay(ngx_connection_t *c);
  168. ngx_int_t ngx_connection_error(ngx_connection_t *c, ngx_err_t err, char *text);

  169. ngx_connection_t *ngx_get_connection(ngx_socket_t s, ngx_log_t *log);
  170. void ngx_free_connection(ngx_connection_t *c);

  171. void ngx_reusable_connection(ngx_connection_t *c, ngx_uint_t reusable);

  172. #endif /* _NGX_CONNECTION_H_INCLUDED_ */