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 
16 typedef struct ngx_listening_s  ngx_listening_t;
17 
18 struct 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_rbtree_t        rbtree;
55     ngx_rbtree_node_t   sentinel;
56 
57     ngx_uint_t          worker;
58 
59     unsigned            open:1;
60     unsigned            remain:1;
61     unsigned            ignore:1;
62 
63     unsigned            bound:1;       /* already bound */
64     unsigned            inherited:1;   /* inherited from previous process */
65     unsigned            nonblocking_accept:1;
66     unsigned            listen:1;
67     unsigned            nonblocking:1;
68     unsigned            shared:1;    /* shared between threads or processes */
69     unsigned            addr_ntop:1;
70     unsigned            wildcard:1;
71 
72 #if (NGX_HAVE_INET6)
73     unsigned            ipv6only:1;
74 #endif
75     unsigned            reuseport:1;
76     unsigned            add_reuseport:1;
77     unsigned            keepalive:2;
78 
79     unsigned            deferred_accept:1;
80     unsigned            delete_deferred:1;
81     unsigned            add_deferred:1;
82 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
83     char               *accept_filter;
84 #endif
85 #if (NGX_HAVE_SETFIB)
86     int                 setfib;
87 #endif
88 
89 #if (NGX_HAVE_TCP_FASTOPEN)
90     int                 fastopen;
91 #endif
92 
93 #if (NGX_HAVE_FSTACK)
94     unsigned            belong_to_host:1;
95 #endif
96 };
97 
98 
99 typedef enum {
100     NGX_ERROR_ALERT = 0,
101     NGX_ERROR_ERR,
102     NGX_ERROR_INFO,
103     NGX_ERROR_IGNORE_ECONNRESET,
104     NGX_ERROR_IGNORE_EINVAL
105 } ngx_connection_log_error_e;
106 
107 
108 typedef enum {
109     NGX_TCP_NODELAY_UNSET = 0,
110     NGX_TCP_NODELAY_SET,
111     NGX_TCP_NODELAY_DISABLED
112 } ngx_connection_tcp_nodelay_e;
113 
114 
115 typedef enum {
116     NGX_TCP_NOPUSH_UNSET = 0,
117     NGX_TCP_NOPUSH_SET,
118     NGX_TCP_NOPUSH_DISABLED
119 } ngx_connection_tcp_nopush_e;
120 
121 
122 #define NGX_LOWLEVEL_BUFFERED  0x0f
123 #define NGX_SSL_BUFFERED       0x01
124 #define NGX_HTTP_V2_BUFFERED   0x02
125 
126 
127 struct ngx_connection_s {
128     void               *data;
129     ngx_event_t        *read;
130     ngx_event_t        *write;
131 
132     ngx_socket_t        fd;
133 
134     ngx_recv_pt         recv;
135     ngx_send_pt         send;
136     ngx_recv_chain_pt   recv_chain;
137     ngx_send_chain_pt   send_chain;
138 
139     ngx_listening_t    *listening;
140 
141     off_t               sent;
142 
143     ngx_log_t          *log;
144 
145     ngx_pool_t         *pool;
146 
147     int                 type;
148 
149     struct sockaddr    *sockaddr;
150     socklen_t           socklen;
151     ngx_str_t           addr_text;
152 
153     ngx_str_t           proxy_protocol_addr;
154     in_port_t           proxy_protocol_port;
155 
156 #if (NGX_SSL || NGX_COMPAT)
157     ngx_ssl_connection_t  *ssl;
158 #endif
159 
160     ngx_udp_connection_t  *udp;
161 
162     struct sockaddr    *local_sockaddr;
163     socklen_t           local_socklen;
164 
165     ngx_buf_t          *buffer;
166 
167     ngx_queue_t         queue;
168 
169     ngx_atomic_uint_t   number;
170 
171     ngx_uint_t          requests;
172 
173     unsigned            buffered:8;
174 
175     unsigned            log_error:3;     /* ngx_connection_log_error_e */
176 
177     unsigned            timedout:1;
178     unsigned            error:1;
179     unsigned            destroyed:1;
180 
181     unsigned            idle:1;
182     unsigned            reusable:1;
183     unsigned            close:1;
184     unsigned            shared:1;
185 
186     unsigned            sendfile:1;
187     unsigned            sndlowat:1;
188     unsigned            tcp_nodelay:2;   /* ngx_connection_tcp_nodelay_e */
189     unsigned            tcp_nopush:2;    /* ngx_connection_tcp_nopush_e */
190 
191     unsigned            need_last_buf:1;
192 
193 #if (NGX_HAVE_AIO_SENDFILE || NGX_COMPAT)
194     unsigned            busy_count:2;
195 #endif
196 
197 #if (NGX_THREADS || NGX_COMPAT)
198     ngx_thread_task_t  *sendfile_task;
199 #endif
200 };
201 
202 
203 #define ngx_set_connection_log(c, l)                                         \
204                                                                              \
205     c->log->file = l->file;                                                  \
206     c->log->next = l->next;                                                  \
207     c->log->writer = l->writer;                                              \
208     c->log->wdata = l->wdata;                                                \
209     if (!(c->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {                   \
210         c->log->log_level = l->log_level;                                    \
211     }
212 
213 
214 ngx_listening_t *ngx_create_listening(ngx_conf_t *cf, struct sockaddr *sockaddr,
215     socklen_t socklen);
216 ngx_int_t ngx_clone_listening(ngx_cycle_t *cycle, ngx_listening_t *ls);
217 ngx_int_t ngx_set_inherited_sockets(ngx_cycle_t *cycle);
218 ngx_int_t ngx_open_listening_sockets(ngx_cycle_t *cycle);
219 void ngx_configure_listening_sockets(ngx_cycle_t *cycle);
220 void ngx_close_listening_sockets(ngx_cycle_t *cycle);
221 void ngx_close_connection(ngx_connection_t *c);
222 void ngx_close_idle_connections(ngx_cycle_t *cycle);
223 ngx_int_t ngx_connection_local_sockaddr(ngx_connection_t *c, ngx_str_t *s,
224     ngx_uint_t port);
225 ngx_int_t ngx_tcp_nodelay(ngx_connection_t *c);
226 ngx_int_t ngx_connection_error(ngx_connection_t *c, ngx_err_t err, char *text);
227 
228 ngx_connection_t *ngx_get_connection(ngx_socket_t s, ngx_log_t *log);
229 void ngx_free_connection(ngx_connection_t *c);
230 
231 void ngx_reusable_connection(ngx_connection_t *c, ngx_uint_t reusable);
232 
233 #endif /* _NGX_CONNECTION_H_INCLUDED_ */
234