Mercurial > hg > nginx-quic
annotate src/event/ngx_event_udp.c @ 9023:3c33d39a51d3 quic
README: updated building from sources, added directives reference.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Wed, 08 Feb 2023 12:47:35 +0400 |
parents | af5adec171b4 |
children |
rev | line source |
---|---|
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
1 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
2 /* |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
3 * Copyright (C) Roman Arutyunyan |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
4 * Copyright (C) Nginx, Inc. |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
5 */ |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
6 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
7 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
8 #include <ngx_config.h> |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
9 #include <ngx_core.h> |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
10 #include <ngx_event.h> |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
11 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
12 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
13 #if !(NGX_WIN32) |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
14 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
15 static void ngx_close_accepted_udp_connection(ngx_connection_t *c); |
7286 | 16 static ssize_t ngx_udp_shared_recv(ngx_connection_t *c, u_char *buf, |
17 size_t size); | |
18 static ngx_int_t ngx_insert_udp_connection(ngx_connection_t *c); | |
19 static ngx_connection_t *ngx_lookup_udp_connection(ngx_listening_t *ls, | |
20 struct sockaddr *sockaddr, socklen_t socklen, | |
21 struct sockaddr *local_sockaddr, socklen_t local_socklen); | |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
22 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
23 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
24 void |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
25 ngx_event_recvmsg(ngx_event_t *ev) |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
26 { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
27 ssize_t n; |
7286 | 28 ngx_buf_t buf; |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
29 ngx_log_t *log; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
30 ngx_err_t err; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
31 socklen_t socklen, local_socklen; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
32 ngx_event_t *rev, *wev; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
33 struct iovec iov[1]; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
34 struct msghdr msg; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
35 ngx_sockaddr_t sa, lsa; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
36 struct sockaddr *sockaddr, *local_sockaddr; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
37 ngx_listening_t *ls; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
38 ngx_event_conf_t *ecf; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
39 ngx_connection_t *c, *lc; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
40 static u_char buffer[65535]; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
41 |
8840
0f6cc8f73744
Core: added function for local source address cmsg.
Vladimir Homutov <vl@nginx.com>
parents:
8396
diff
changeset
|
42 #if (NGX_HAVE_ADDRINFO_CMSG) |
0f6cc8f73744
Core: added function for local source address cmsg.
Vladimir Homutov <vl@nginx.com>
parents:
8396
diff
changeset
|
43 u_char msg_control[CMSG_SPACE(sizeof(ngx_addrinfo_t))]; |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
44 #endif |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
45 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
46 if (ev->timedout) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
47 if (ngx_enable_accept_events((ngx_cycle_t *) ngx_cycle) != NGX_OK) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
48 return; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
49 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
50 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
51 ev->timedout = 0; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
52 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
53 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
54 ecf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_event_core_module); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
55 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
56 if (!(ngx_event_flags & NGX_USE_KQUEUE_EVENT)) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
57 ev->available = ecf->multi_accept; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
58 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
59 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
60 lc = ev->data; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
61 ls = lc->listening; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
62 ev->ready = 0; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
63 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
64 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0, |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
65 "recvmsg on %V, ready: %d", &ls->addr_text, ev->available); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
66 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
67 do { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
68 ngx_memzero(&msg, sizeof(struct msghdr)); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
69 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
70 iov[0].iov_base = (void *) buffer; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
71 iov[0].iov_len = sizeof(buffer); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
72 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
73 msg.msg_name = &sa; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
74 msg.msg_namelen = sizeof(ngx_sockaddr_t); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
75 msg.msg_iov = iov; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
76 msg.msg_iovlen = 1; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
77 |
8840
0f6cc8f73744
Core: added function for local source address cmsg.
Vladimir Homutov <vl@nginx.com>
parents:
8396
diff
changeset
|
78 #if (NGX_HAVE_ADDRINFO_CMSG) |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
79 if (ls->wildcard) { |
8840
0f6cc8f73744
Core: added function for local source address cmsg.
Vladimir Homutov <vl@nginx.com>
parents:
8396
diff
changeset
|
80 msg.msg_control = &msg_control; |
0f6cc8f73744
Core: added function for local source address cmsg.
Vladimir Homutov <vl@nginx.com>
parents:
8396
diff
changeset
|
81 msg.msg_controllen = sizeof(msg_control); |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
82 |
8840
0f6cc8f73744
Core: added function for local source address cmsg.
Vladimir Homutov <vl@nginx.com>
parents:
8396
diff
changeset
|
83 ngx_memzero(&msg_control, sizeof(msg_control)); |
9001 | 84 } |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
85 #endif |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
86 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
87 n = recvmsg(lc->fd, &msg, 0); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
88 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
89 if (n == -1) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
90 err = ngx_socket_errno; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
91 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
92 if (err == NGX_EAGAIN) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
93 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ev->log, err, |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
94 "recvmsg() not ready"); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
95 return; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
96 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
97 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
98 ngx_log_error(NGX_LOG_ALERT, ev->log, err, "recvmsg() failed"); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
99 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
100 return; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
101 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
102 |
8840
0f6cc8f73744
Core: added function for local source address cmsg.
Vladimir Homutov <vl@nginx.com>
parents:
8396
diff
changeset
|
103 #if (NGX_HAVE_ADDRINFO_CMSG) |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
104 if (msg.msg_flags & (MSG_TRUNC|MSG_CTRUNC)) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
105 ngx_log_error(NGX_LOG_ALERT, ev->log, 0, |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
106 "recvmsg() truncated data"); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
107 continue; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
108 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
109 #endif |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
110 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
111 sockaddr = msg.msg_name; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
112 socklen = msg.msg_namelen; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
113 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
114 if (socklen > (socklen_t) sizeof(ngx_sockaddr_t)) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
115 socklen = sizeof(ngx_sockaddr_t); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
116 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
117 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
118 if (socklen == 0) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
119 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
120 /* |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
121 * on Linux recvmsg() returns zero msg_namelen |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
122 * when receiving packets from unbound AF_UNIX sockets |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
123 */ |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
124 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
125 socklen = sizeof(struct sockaddr); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
126 ngx_memzero(&sa, sizeof(struct sockaddr)); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
127 sa.sockaddr.sa_family = ls->sockaddr->sa_family; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
128 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
129 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
130 local_sockaddr = ls->sockaddr; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
131 local_socklen = ls->socklen; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
132 |
8840
0f6cc8f73744
Core: added function for local source address cmsg.
Vladimir Homutov <vl@nginx.com>
parents:
8396
diff
changeset
|
133 #if (NGX_HAVE_ADDRINFO_CMSG) |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
134 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
135 if (ls->wildcard) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
136 struct cmsghdr *cmsg; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
137 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
138 ngx_memcpy(&lsa, local_sockaddr, local_socklen); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
139 local_sockaddr = &lsa.sockaddr; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
140 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
141 for (cmsg = CMSG_FIRSTHDR(&msg); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
142 cmsg != NULL; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
143 cmsg = CMSG_NXTHDR(&msg, cmsg)) |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
144 { |
8840
0f6cc8f73744
Core: added function for local source address cmsg.
Vladimir Homutov <vl@nginx.com>
parents:
8396
diff
changeset
|
145 if (ngx_get_srcaddr_cmsg(cmsg, local_sockaddr) == NGX_OK) { |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
146 break; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
147 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
148 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
149 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
150 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
151 #endif |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
152 |
7286 | 153 c = ngx_lookup_udp_connection(ls, sockaddr, socklen, local_sockaddr, |
154 local_socklen); | |
155 | |
156 if (c) { | |
157 | |
158 #if (NGX_DEBUG) | |
159 if (c->log->log_level & NGX_LOG_DEBUG_EVENT) { | |
160 ngx_log_handler_pt handler; | |
161 | |
162 handler = c->log->handler; | |
163 c->log->handler = NULL; | |
164 | |
165 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
166 "recvmsg: fd:%d n:%z", c->fd, n); | |
167 | |
168 c->log->handler = handler; | |
169 } | |
170 #endif | |
171 | |
172 ngx_memzero(&buf, sizeof(ngx_buf_t)); | |
173 | |
174 buf.pos = buffer; | |
175 buf.last = buffer + n; | |
176 | |
177 rev = c->read; | |
178 | |
179 c->udp->buffer = &buf; | |
7440
6d4bc025c5a7
Prevented scheduling events on a shared connection.
Roman Arutyunyan <arut@nginx.com>
parents:
7393
diff
changeset
|
180 |
7286 | 181 rev->ready = 1; |
7440
6d4bc025c5a7
Prevented scheduling events on a shared connection.
Roman Arutyunyan <arut@nginx.com>
parents:
7393
diff
changeset
|
182 rev->active = 0; |
7286 | 183 |
184 rev->handler(rev); | |
185 | |
7391
27559d4a5151
Stream: fixed possible use of a freed connection.
Vladimir Homutov <vl@nginx.com>
parents:
7286
diff
changeset
|
186 if (c->udp) { |
27559d4a5151
Stream: fixed possible use of a freed connection.
Vladimir Homutov <vl@nginx.com>
parents:
7286
diff
changeset
|
187 c->udp->buffer = NULL; |
27559d4a5151
Stream: fixed possible use of a freed connection.
Vladimir Homutov <vl@nginx.com>
parents:
7286
diff
changeset
|
188 } |
27559d4a5151
Stream: fixed possible use of a freed connection.
Vladimir Homutov <vl@nginx.com>
parents:
7286
diff
changeset
|
189 |
7286 | 190 rev->ready = 0; |
7440
6d4bc025c5a7
Prevented scheduling events on a shared connection.
Roman Arutyunyan <arut@nginx.com>
parents:
7393
diff
changeset
|
191 rev->active = 1; |
7286 | 192 |
193 goto next; | |
194 } | |
195 | |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
196 #if (NGX_STAT_STUB) |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
197 (void) ngx_atomic_fetch_add(ngx_stat_accepted, 1); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
198 #endif |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
199 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
200 ngx_accept_disabled = ngx_cycle->connection_n / 8 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
201 - ngx_cycle->free_connection_n; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
202 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
203 c = ngx_get_connection(lc->fd, ev->log); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
204 if (c == NULL) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
205 return; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
206 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
207 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
208 c->shared = 1; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
209 c->type = SOCK_DGRAM; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
210 c->socklen = socklen; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
211 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
212 #if (NGX_STAT_STUB) |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
213 (void) ngx_atomic_fetch_add(ngx_stat_active, 1); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
214 #endif |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
215 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
216 c->pool = ngx_create_pool(ls->pool_size, ev->log); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
217 if (c->pool == NULL) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
218 ngx_close_accepted_udp_connection(c); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
219 return; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
220 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
221 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
222 c->sockaddr = ngx_palloc(c->pool, socklen); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
223 if (c->sockaddr == NULL) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
224 ngx_close_accepted_udp_connection(c); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
225 return; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
226 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
227 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
228 ngx_memcpy(c->sockaddr, sockaddr, socklen); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
229 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
230 log = ngx_palloc(c->pool, sizeof(ngx_log_t)); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
231 if (log == NULL) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
232 ngx_close_accepted_udp_connection(c); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
233 return; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
234 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
235 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
236 *log = ls->log; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
237 |
7286 | 238 c->recv = ngx_udp_shared_recv; |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
239 c->send = ngx_udp_send; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
240 c->send_chain = ngx_udp_send_chain; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
241 |
8843
32b0ba4855a6
HTTP/2: made it possible to flush response headers (ticket #1743).
Maxim Dounin <mdounin@mdounin.ru>
parents:
8840
diff
changeset
|
242 c->need_flush_buf = 1; |
32b0ba4855a6
HTTP/2: made it possible to flush response headers (ticket #1743).
Maxim Dounin <mdounin@mdounin.ru>
parents:
8840
diff
changeset
|
243 |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
244 c->log = log; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
245 c->pool->log = log; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
246 c->listening = ls; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
247 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
248 if (local_sockaddr == &lsa.sockaddr) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
249 local_sockaddr = ngx_palloc(c->pool, local_socklen); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
250 if (local_sockaddr == NULL) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
251 ngx_close_accepted_udp_connection(c); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
252 return; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
253 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
254 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
255 ngx_memcpy(local_sockaddr, &lsa, local_socklen); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
256 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
257 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
258 c->local_sockaddr = local_sockaddr; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
259 c->local_socklen = local_socklen; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
260 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
261 c->buffer = ngx_create_temp_buf(c->pool, n); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
262 if (c->buffer == NULL) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
263 ngx_close_accepted_udp_connection(c); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
264 return; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
265 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
266 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
267 c->buffer->last = ngx_cpymem(c->buffer->last, buffer, n); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
268 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
269 rev = c->read; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
270 wev = c->write; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
271 |
7440
6d4bc025c5a7
Prevented scheduling events on a shared connection.
Roman Arutyunyan <arut@nginx.com>
parents:
7393
diff
changeset
|
272 rev->active = 1; |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
273 wev->ready = 1; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
274 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
275 rev->log = log; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
276 wev->log = log; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
277 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
278 /* |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
279 * TODO: MT: - ngx_atomic_fetch_add() |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
280 * or protection by critical section or light mutex |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
281 * |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
282 * TODO: MP: - allocated in a shared memory |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
283 * - ngx_atomic_fetch_add() |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
284 * or protection by critical section or light mutex |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
285 */ |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
286 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
287 c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
288 |
8396
fdc3d40979b0
Introduced the "keepalive_time" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7440
diff
changeset
|
289 c->start_time = ngx_current_msec; |
fdc3d40979b0
Introduced the "keepalive_time" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7440
diff
changeset
|
290 |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
291 #if (NGX_STAT_STUB) |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
292 (void) ngx_atomic_fetch_add(ngx_stat_handled, 1); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
293 #endif |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
294 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
295 if (ls->addr_ntop) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
296 c->addr_text.data = ngx_pnalloc(c->pool, ls->addr_text_max_len); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
297 if (c->addr_text.data == NULL) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
298 ngx_close_accepted_udp_connection(c); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
299 return; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
300 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
301 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
302 c->addr_text.len = ngx_sock_ntop(c->sockaddr, c->socklen, |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
303 c->addr_text.data, |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
304 ls->addr_text_max_len, 0); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
305 if (c->addr_text.len == 0) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
306 ngx_close_accepted_udp_connection(c); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
307 return; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
308 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
309 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
310 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
311 #if (NGX_DEBUG) |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
312 { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
313 ngx_str_t addr; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
314 u_char text[NGX_SOCKADDR_STRLEN]; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
315 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
316 ngx_debug_accepted_connection(ecf, c); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
317 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
318 if (log->log_level & NGX_LOG_DEBUG_EVENT) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
319 addr.data = text; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
320 addr.len = ngx_sock_ntop(c->sockaddr, c->socklen, text, |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
321 NGX_SOCKADDR_STRLEN, 1); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
322 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
323 ngx_log_debug4(NGX_LOG_DEBUG_EVENT, log, 0, |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
324 "*%uA recvmsg: %V fd:%d n:%z", |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
325 c->number, &addr, c->fd, n); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
326 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
327 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
328 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
329 #endif |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
330 |
7286 | 331 if (ngx_insert_udp_connection(c) != NGX_OK) { |
332 ngx_close_accepted_udp_connection(c); | |
333 return; | |
334 } | |
335 | |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
336 log->data = NULL; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
337 log->handler = NULL; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
338 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
339 ls->handler(c); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
340 |
7286 | 341 next: |
342 | |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
343 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
344 ev->available -= n; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
345 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
346 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
347 } while (ev->available); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
348 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
349 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
350 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
351 static void |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
352 ngx_close_accepted_udp_connection(ngx_connection_t *c) |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
353 { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
354 ngx_free_connection(c); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
355 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
356 c->fd = (ngx_socket_t) -1; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
357 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
358 if (c->pool) { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
359 ngx_destroy_pool(c->pool); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
360 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
361 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
362 #if (NGX_STAT_STUB) |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
363 (void) ngx_atomic_fetch_add(ngx_stat_active, -1); |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
364 #endif |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
365 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
366 |
7286 | 367 |
368 static ssize_t | |
369 ngx_udp_shared_recv(ngx_connection_t *c, u_char *buf, size_t size) | |
370 { | |
371 ssize_t n; | |
372 ngx_buf_t *b; | |
373 | |
374 if (c->udp == NULL || c->udp->buffer == NULL) { | |
375 return NGX_AGAIN; | |
376 } | |
377 | |
378 b = c->udp->buffer; | |
379 | |
380 n = ngx_min(b->last - b->pos, (ssize_t) size); | |
381 | |
382 ngx_memcpy(buf, b->pos, n); | |
383 | |
384 c->udp->buffer = NULL; | |
7440
6d4bc025c5a7
Prevented scheduling events on a shared connection.
Roman Arutyunyan <arut@nginx.com>
parents:
7393
diff
changeset
|
385 |
7286 | 386 c->read->ready = 0; |
7440
6d4bc025c5a7
Prevented scheduling events on a shared connection.
Roman Arutyunyan <arut@nginx.com>
parents:
7393
diff
changeset
|
387 c->read->active = 1; |
7286 | 388 |
389 return n; | |
390 } | |
391 | |
392 | |
393 void | |
394 ngx_udp_rbtree_insert_value(ngx_rbtree_node_t *temp, | |
395 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel) | |
396 { | |
397 ngx_int_t rc; | |
398 ngx_connection_t *c, *ct; | |
399 ngx_rbtree_node_t **p; | |
400 ngx_udp_connection_t *udp, *udpt; | |
401 | |
402 for ( ;; ) { | |
403 | |
404 if (node->key < temp->key) { | |
405 | |
406 p = &temp->left; | |
407 | |
408 } else if (node->key > temp->key) { | |
409 | |
410 p = &temp->right; | |
411 | |
412 } else { /* node->key == temp->key */ | |
413 | |
414 udp = (ngx_udp_connection_t *) node; | |
415 c = udp->connection; | |
416 | |
417 udpt = (ngx_udp_connection_t *) temp; | |
418 ct = udpt->connection; | |
419 | |
420 rc = ngx_cmp_sockaddr(c->sockaddr, c->socklen, | |
421 ct->sockaddr, ct->socklen, 1); | |
422 | |
423 if (rc == 0 && c->listening->wildcard) { | |
424 rc = ngx_cmp_sockaddr(c->local_sockaddr, c->local_socklen, | |
425 ct->local_sockaddr, ct->local_socklen, 1); | |
426 } | |
427 | |
428 p = (rc < 0) ? &temp->left : &temp->right; | |
429 } | |
430 | |
431 if (*p == sentinel) { | |
432 break; | |
433 } | |
434 | |
435 temp = *p; | |
436 } | |
437 | |
438 *p = node; | |
439 node->parent = temp; | |
440 node->left = sentinel; | |
441 node->right = sentinel; | |
442 ngx_rbt_red(node); | |
443 } | |
444 | |
445 | |
446 static ngx_int_t | |
447 ngx_insert_udp_connection(ngx_connection_t *c) | |
448 { | |
449 uint32_t hash; | |
450 ngx_pool_cleanup_t *cln; | |
451 ngx_udp_connection_t *udp; | |
452 | |
453 if (c->udp) { | |
454 return NGX_OK; | |
455 } | |
456 | |
457 udp = ngx_pcalloc(c->pool, sizeof(ngx_udp_connection_t)); | |
458 if (udp == NULL) { | |
459 return NGX_ERROR; | |
460 } | |
461 | |
462 udp->connection = c; | |
463 | |
464 ngx_crc32_init(hash); | |
465 ngx_crc32_update(&hash, (u_char *) c->sockaddr, c->socklen); | |
466 | |
467 if (c->listening->wildcard) { | |
468 ngx_crc32_update(&hash, (u_char *) c->local_sockaddr, c->local_socklen); | |
469 } | |
470 | |
471 ngx_crc32_final(hash); | |
472 | |
473 udp->node.key = hash; | |
474 | |
475 cln = ngx_pool_cleanup_add(c->pool, 0); | |
476 if (cln == NULL) { | |
477 return NGX_ERROR; | |
478 } | |
479 | |
480 cln->data = c; | |
481 cln->handler = ngx_delete_udp_connection; | |
482 | |
483 ngx_rbtree_insert(&c->listening->rbtree, &udp->node); | |
484 | |
485 c->udp = udp; | |
486 | |
487 return NGX_OK; | |
488 } | |
489 | |
490 | |
7393
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
491 void |
7286 | 492 ngx_delete_udp_connection(void *data) |
493 { | |
494 ngx_connection_t *c = data; | |
495 | |
7393
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
496 if (c->udp == NULL) { |
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
497 return; |
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
498 } |
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
499 |
7286 | 500 ngx_rbtree_delete(&c->listening->rbtree, &c->udp->node); |
7391
27559d4a5151
Stream: fixed possible use of a freed connection.
Vladimir Homutov <vl@nginx.com>
parents:
7286
diff
changeset
|
501 |
27559d4a5151
Stream: fixed possible use of a freed connection.
Vladimir Homutov <vl@nginx.com>
parents:
7286
diff
changeset
|
502 c->udp = NULL; |
7286 | 503 } |
504 | |
505 | |
506 static ngx_connection_t * | |
507 ngx_lookup_udp_connection(ngx_listening_t *ls, struct sockaddr *sockaddr, | |
508 socklen_t socklen, struct sockaddr *local_sockaddr, socklen_t local_socklen) | |
509 { | |
510 uint32_t hash; | |
511 ngx_int_t rc; | |
512 ngx_connection_t *c; | |
513 ngx_rbtree_node_t *node, *sentinel; | |
514 ngx_udp_connection_t *udp; | |
515 | |
516 #if (NGX_HAVE_UNIX_DOMAIN) | |
517 | |
518 if (sockaddr->sa_family == AF_UNIX) { | |
519 struct sockaddr_un *saun = (struct sockaddr_un *) sockaddr; | |
520 | |
521 if (socklen <= (socklen_t) offsetof(struct sockaddr_un, sun_path) | |
522 || saun->sun_path[0] == '\0') | |
523 { | |
524 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ngx_cycle->log, 0, | |
525 "unbound unix socket"); | |
526 return NULL; | |
527 } | |
528 } | |
529 | |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
530 #endif |
7286 | 531 |
532 node = ls->rbtree.root; | |
533 sentinel = ls->rbtree.sentinel; | |
534 | |
535 ngx_crc32_init(hash); | |
536 ngx_crc32_update(&hash, (u_char *) sockaddr, socklen); | |
537 | |
538 if (ls->wildcard) { | |
539 ngx_crc32_update(&hash, (u_char *) local_sockaddr, local_socklen); | |
540 } | |
541 | |
542 ngx_crc32_final(hash); | |
543 | |
544 while (node != sentinel) { | |
545 | |
546 if (hash < node->key) { | |
547 node = node->left; | |
548 continue; | |
549 } | |
550 | |
551 if (hash > node->key) { | |
552 node = node->right; | |
553 continue; | |
554 } | |
555 | |
556 /* hash == node->key */ | |
557 | |
558 udp = (ngx_udp_connection_t *) node; | |
559 | |
560 c = udp->connection; | |
561 | |
562 rc = ngx_cmp_sockaddr(sockaddr, socklen, | |
563 c->sockaddr, c->socklen, 1); | |
564 | |
565 if (rc == 0 && ls->wildcard) { | |
566 rc = ngx_cmp_sockaddr(local_sockaddr, local_socklen, | |
567 c->local_sockaddr, c->local_socklen, 1); | |
568 } | |
569 | |
570 if (rc == 0) { | |
571 return c; | |
572 } | |
573 | |
574 node = (rc < 0) ? node->left : node->right; | |
575 } | |
576 | |
577 return NULL; | |
578 } | |
579 | |
7393
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
580 #else |
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
581 |
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
582 void |
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
583 ngx_delete_udp_connection(void *data) |
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
584 { |
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
585 return; |
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
586 } |
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
587 |
7286 | 588 #endif |