ngx_connection.h revision e18a033b
1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
6
7
8#ifndef _NGX_CONNECTION_H_INCLUDED_
9#define _NGX_CONNECTION_H_INCLUDED_
10
11
12#include <ngx_config.h>
13#include <ngx_core.h>
14
15
16typedef struct ngx_listening_s  ngx_listening_t;
17
18struct ngx_listening_s {
19    ngx_socket_t        fd;
20
21    struct sockaddr    *sockaddr;
22    socklen_t           socklen;    /* size of sockaddr */
23    size_t              addr_text_max_len;
24    ngx_str_t           addr_text;
25
26    int                 type;
27
28    int                 backlog;
29    int                 rcvbuf;
30    int                 sndbuf;
31#if (NGX_HAVE_KEEPALIVE_TUNABLE)
32    int                 keepidle;
33    int                 keepintvl;
34    int                 keepcnt;
35#endif
36
37    /* handler of accepted connection */
38    ngx_connection_handler_pt   handler;
39
40    void               *servers;  /* array of ngx_http_in_addr_t, for example */
41
42    ngx_log_t           log;
43    ngx_log_t          *logp;
44
45    size_t              pool_size;
46    /* should be here because of the AcceptEx() preread */
47    size_t              post_accept_buffer_size;
48    /* should be here because of the deferred accept */
49    ngx_msec_t          post_accept_timeout;
50
51    ngx_listening_t    *previous;
52    ngx_connection_t   *connection;
53
54    ngx_uint_t          worker;
55
56    unsigned            open:1;
57    unsigned            remain:1;
58    unsigned            ignore:1;
59
60    unsigned            bound:1;       /* already bound */
61    unsigned            inherited:1;   /* inherited from previous process */
62    unsigned            nonblocking_accept:1;
63    unsigned            listen:1;
64    unsigned            nonblocking:1;
65    unsigned            shared:1;    /* shared between threads or processes */
66    unsigned            addr_ntop:1;
67    unsigned            wildcard:1;
68
69#if (NGX_HAVE_INET6)
70    unsigned            ipv6only:1;
71#endif
72    unsigned            reuseport:1;
73    unsigned            add_reuseport:1;
74    unsigned            keepalive:2;
75
76    unsigned            deferred_accept:1;
77    unsigned            delete_deferred:1;
78    unsigned            add_deferred:1;
79#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
80    char               *accept_filter;
81#endif
82#if (NGX_HAVE_SETFIB)
83    int                 setfib;
84#endif
85
86#if (NGX_HAVE_TCP_FASTOPEN)
87    int                 fastopen;
88#endif
89
90};
91
92
93typedef enum {
94    NGX_ERROR_ALERT = 0,
95    NGX_ERROR_ERR,
96    NGX_ERROR_INFO,
97    NGX_ERROR_IGNORE_ECONNRESET,
98    NGX_ERROR_IGNORE_EINVAL
99} ngx_connection_log_error_e;
100
101
102typedef enum {
103    NGX_TCP_NODELAY_UNSET = 0,
104    NGX_TCP_NODELAY_SET,
105    NGX_TCP_NODELAY_DISABLED
106} ngx_connection_tcp_nodelay_e;
107
108
109typedef enum {
110    NGX_TCP_NOPUSH_UNSET = 0,
111    NGX_TCP_NOPUSH_SET,
112    NGX_TCP_NOPUSH_DISABLED
113} ngx_connection_tcp_nopush_e;
114
115
116#define NGX_LOWLEVEL_BUFFERED  0x0f
117#define NGX_SSL_BUFFERED       0x01
118#define NGX_HTTP_V2_BUFFERED   0x02
119
120
121struct ngx_connection_s {
122    void               *data;
123    ngx_event_t        *read;
124    ngx_event_t        *write;
125
126    ngx_socket_t        fd;
127
128    ngx_recv_pt         recv;
129    ngx_send_pt         send;
130    ngx_recv_chain_pt   recv_chain;
131    ngx_send_chain_pt   send_chain;
132
133    ngx_listening_t    *listening;
134
135    off_t               sent;
136
137    ngx_log_t          *log;
138
139    ngx_pool_t         *pool;
140
141    int                 type;
142
143    struct sockaddr    *sockaddr;
144    socklen_t           socklen;
145    ngx_str_t           addr_text;
146
147    ngx_str_t           proxy_protocol_addr;
148    in_port_t           proxy_protocol_port;
149
150#if (NGX_SSL || NGX_COMPAT)
151    ngx_ssl_connection_t  *ssl;
152#endif
153
154    struct sockaddr    *local_sockaddr;
155    socklen_t           local_socklen;
156
157    ngx_buf_t          *buffer;
158
159    ngx_queue_t         queue;
160
161    ngx_atomic_uint_t   number;
162
163    ngx_uint_t          requests;
164
165    unsigned            buffered:8;
166
167    unsigned            log_error:3;     /* ngx_connection_log_error_e */
168
169    unsigned            timedout:1;
170    unsigned            error:1;
171    unsigned            destroyed:1;
172
173    unsigned            idle:1;
174    unsigned            reusable:1;
175    unsigned            close:1;
176    unsigned            shared:1;
177
178    unsigned            sendfile:1;
179    unsigned            sndlowat:1;
180    unsigned            tcp_nodelay:2;   /* ngx_connection_tcp_nodelay_e */
181    unsigned            tcp_nopush:2;    /* ngx_connection_tcp_nopush_e */
182
183    unsigned            need_last_buf:1;
184
185#if (NGX_HAVE_AIO_SENDFILE || NGX_COMPAT)
186    unsigned            busy_count:2;
187#endif
188
189#if (NGX_THREADS || NGX_COMPAT)
190    ngx_thread_task_t  *sendfile_task;
191#endif
192};
193
194
195#define ngx_set_connection_log(c, l)                                         \
196                                                                             \
197    c->log->file = l->file;                                                  \
198    c->log->next = l->next;                                                  \
199    c->log->writer = l->writer;                                              \
200    c->log->wdata = l->wdata;                                                \
201    if (!(c->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {                   \
202        c->log->log_level = l->log_level;                                    \
203    }
204
205
206ngx_listening_t *ngx_create_listening(ngx_conf_t *cf, struct sockaddr *sockaddr,
207    socklen_t socklen);
208ngx_int_t ngx_clone_listening(ngx_conf_t *cf, ngx_listening_t *ls);
209ngx_int_t ngx_set_inherited_sockets(ngx_cycle_t *cycle);
210ngx_int_t ngx_open_listening_sockets(ngx_cycle_t *cycle);
211void ngx_configure_listening_sockets(ngx_cycle_t *cycle);
212void ngx_close_listening_sockets(ngx_cycle_t *cycle);
213void ngx_close_connection(ngx_connection_t *c);
214void ngx_close_idle_connections(ngx_cycle_t *cycle);
215ngx_int_t ngx_connection_local_sockaddr(ngx_connection_t *c, ngx_str_t *s,
216    ngx_uint_t port);
217ngx_int_t ngx_connection_error(ngx_connection_t *c, ngx_err_t err, char *text);
218
219ngx_connection_t *ngx_get_connection(ngx_socket_t s, ngx_log_t *log);
220void ngx_free_connection(ngx_connection_t *c);
221
222void ngx_reusable_connection(ngx_connection_t *c, ngx_uint_t reusable);
223
224#endif /* _NGX_CONNECTION_H_INCLUDED_ */
225