Mercurial > hg > nginx-quic
annotate src/event/ngx_event_udp.c @ 8876:ca78312db071
Perl: combining unknown headers during $r->header_in() lookup.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Mon, 30 May 2022 21:25:38 +0300 |
parents | 32b0ba4855a6 |
children | ce6d9cf0f567 3108d4d668e4 |
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 |
7286 | 15 struct ngx_udp_connection_s { |
16 ngx_rbtree_node_t node; | |
17 ngx_connection_t *connection; | |
18 ngx_buf_t *buffer; | |
19 }; | |
20 | |
21 | |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
22 static void ngx_close_accepted_udp_connection(ngx_connection_t *c); |
7286 | 23 static ssize_t ngx_udp_shared_recv(ngx_connection_t *c, u_char *buf, |
24 size_t size); | |
25 static ngx_int_t ngx_insert_udp_connection(ngx_connection_t *c); | |
26 static ngx_connection_t *ngx_lookup_udp_connection(ngx_listening_t *ls, | |
27 struct sockaddr *sockaddr, socklen_t socklen, | |
28 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
|
29 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
30 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
31 void |
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_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
|
33 { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
34 ssize_t n; |
7286 | 35 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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 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
|
47 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
|
48 |
8840
0f6cc8f73744
Core: added function for local source address cmsg.
Vladimir Homutov <vl@nginx.com>
parents:
8396
diff
changeset
|
49 #if (NGX_HAVE_ADDRINFO_CMSG) |
0f6cc8f73744
Core: added function for local source address cmsg.
Vladimir Homutov <vl@nginx.com>
parents:
8396
diff
changeset
|
50 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
|
51 #endif |
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 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
|
54 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
|
55 return; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
56 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
57 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
58 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
|
59 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
60 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
61 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
|
62 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
63 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
|
64 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
|
65 } |
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 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
|
68 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
|
69 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
|
70 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
71 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
|
72 "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
|
73 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
74 do { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
75 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
|
76 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
77 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
|
78 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
|
79 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
80 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
|
81 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
|
82 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
|
83 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
|
84 |
8840
0f6cc8f73744
Core: added function for local source address cmsg.
Vladimir Homutov <vl@nginx.com>
parents:
8396
diff
changeset
|
85 #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
|
86 if (ls->wildcard) { |
8840
0f6cc8f73744
Core: added function for local source address cmsg.
Vladimir Homutov <vl@nginx.com>
parents:
8396
diff
changeset
|
87 msg.msg_control = &msg_control; |
0f6cc8f73744
Core: added function for local source address cmsg.
Vladimir Homutov <vl@nginx.com>
parents:
8396
diff
changeset
|
88 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
|
89 |
8840
0f6cc8f73744
Core: added function for local source address cmsg.
Vladimir Homutov <vl@nginx.com>
parents:
8396
diff
changeset
|
90 ngx_memzero(&msg_control, sizeof(msg_control)); |
0f6cc8f73744
Core: added function for local source address cmsg.
Vladimir Homutov <vl@nginx.com>
parents:
8396
diff
changeset
|
91 } |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
92 #endif |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
93 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
94 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
|
95 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
96 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
|
97 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
|
98 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
99 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
|
100 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
|
101 "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
|
102 return; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
103 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
104 |
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, 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
|
106 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
107 return; |
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 |
8840
0f6cc8f73744
Core: added function for local source address cmsg.
Vladimir Homutov <vl@nginx.com>
parents:
8396
diff
changeset
|
110 #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
|
111 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
|
112 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
|
113 "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
|
114 continue; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
115 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
116 #endif |
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 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
|
119 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
|
120 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
121 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
|
122 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
|
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 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
|
126 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
127 /* |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
128 * 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
|
129 * 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
|
130 */ |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
131 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
132 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
|
133 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
|
134 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
|
135 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
136 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
137 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
|
138 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
|
139 |
8840
0f6cc8f73744
Core: added function for local source address cmsg.
Vladimir Homutov <vl@nginx.com>
parents:
8396
diff
changeset
|
140 #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
|
141 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
142 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
|
143 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
|
144 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
145 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
|
146 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
|
147 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
148 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
|
149 cmsg != NULL; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
150 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
|
151 { |
8840
0f6cc8f73744
Core: added function for local source address cmsg.
Vladimir Homutov <vl@nginx.com>
parents:
8396
diff
changeset
|
152 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
|
153 break; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
154 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
155 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
156 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
157 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
158 #endif |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
159 |
7286 | 160 c = ngx_lookup_udp_connection(ls, sockaddr, socklen, local_sockaddr, |
161 local_socklen); | |
162 | |
163 if (c) { | |
164 | |
165 #if (NGX_DEBUG) | |
166 if (c->log->log_level & NGX_LOG_DEBUG_EVENT) { | |
167 ngx_log_handler_pt handler; | |
168 | |
169 handler = c->log->handler; | |
170 c->log->handler = NULL; | |
171 | |
172 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, | |
173 "recvmsg: fd:%d n:%z", c->fd, n); | |
174 | |
175 c->log->handler = handler; | |
176 } | |
177 #endif | |
178 | |
179 ngx_memzero(&buf, sizeof(ngx_buf_t)); | |
180 | |
181 buf.pos = buffer; | |
182 buf.last = buffer + n; | |
183 | |
184 rev = c->read; | |
185 | |
186 c->udp->buffer = &buf; | |
7440
6d4bc025c5a7
Prevented scheduling events on a shared connection.
Roman Arutyunyan <arut@nginx.com>
parents:
7393
diff
changeset
|
187 |
7286 | 188 rev->ready = 1; |
7440
6d4bc025c5a7
Prevented scheduling events on a shared connection.
Roman Arutyunyan <arut@nginx.com>
parents:
7393
diff
changeset
|
189 rev->active = 0; |
7286 | 190 |
191 rev->handler(rev); | |
192 | |
7391
27559d4a5151
Stream: fixed possible use of a freed connection.
Vladimir Homutov <vl@nginx.com>
parents:
7286
diff
changeset
|
193 if (c->udp) { |
27559d4a5151
Stream: fixed possible use of a freed connection.
Vladimir Homutov <vl@nginx.com>
parents:
7286
diff
changeset
|
194 c->udp->buffer = NULL; |
27559d4a5151
Stream: fixed possible use of a freed connection.
Vladimir Homutov <vl@nginx.com>
parents:
7286
diff
changeset
|
195 } |
27559d4a5151
Stream: fixed possible use of a freed connection.
Vladimir Homutov <vl@nginx.com>
parents:
7286
diff
changeset
|
196 |
7286 | 197 rev->ready = 0; |
7440
6d4bc025c5a7
Prevented scheduling events on a shared connection.
Roman Arutyunyan <arut@nginx.com>
parents:
7393
diff
changeset
|
198 rev->active = 1; |
7286 | 199 |
200 goto next; | |
201 } | |
202 | |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
203 #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
|
204 (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
|
205 #endif |
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 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
|
208 - 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
|
209 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
210 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
|
211 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
|
212 return; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
213 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
214 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
215 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
|
216 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
|
217 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
|
218 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
219 #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
|
220 (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
|
221 #endif |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
222 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
223 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
|
224 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
|
225 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
|
226 return; |
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 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
229 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
|
230 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
|
231 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
|
232 return; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
233 } |
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 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
|
236 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
237 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
|
238 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
|
239 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
|
240 return; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
241 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
242 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
243 *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
|
244 |
7286 | 245 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
|
246 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
|
247 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
|
248 |
8843
32b0ba4855a6
HTTP/2: made it possible to flush response headers (ticket #1743).
Maxim Dounin <mdounin@mdounin.ru>
parents:
8840
diff
changeset
|
249 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
|
250 |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
251 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
|
252 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
|
253 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
|
254 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
255 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
|
256 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
|
257 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
|
258 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
|
259 return; |
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 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
262 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
|
263 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
264 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
265 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
|
266 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
|
267 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
268 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
|
269 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
|
270 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
|
271 return; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
272 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
273 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
274 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
|
275 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
276 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
|
277 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
|
278 |
7440
6d4bc025c5a7
Prevented scheduling events on a shared connection.
Roman Arutyunyan <arut@nginx.com>
parents:
7393
diff
changeset
|
279 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
|
280 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
|
281 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
282 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
|
283 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
|
284 |
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 * 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
|
287 * 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
|
288 * |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
289 * 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
|
290 * - 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
|
291 * 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
|
292 */ |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
293 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
294 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
|
295 |
8396
fdc3d40979b0
Introduced the "keepalive_time" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7440
diff
changeset
|
296 c->start_time = ngx_current_msec; |
fdc3d40979b0
Introduced the "keepalive_time" directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
7440
diff
changeset
|
297 |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
298 #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
|
299 (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
|
300 #endif |
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 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
|
303 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
|
304 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
|
305 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
|
306 return; |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
307 } |
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 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
|
310 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
|
311 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
|
312 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
|
313 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
|
314 return; |
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 } |
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 (NGX_DEBUG) |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
319 { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
320 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
|
321 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
|
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_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
|
324 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
325 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
|
326 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
|
327 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
|
328 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
|
329 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
330 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
|
331 "*%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
|
332 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
|
333 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
334 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
335 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
336 #endif |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
337 |
7286 | 338 if (ngx_insert_udp_connection(c) != NGX_OK) { |
339 ngx_close_accepted_udp_connection(c); | |
340 return; | |
341 } | |
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 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
|
344 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
|
345 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
346 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
|
347 |
7286 | 348 next: |
349 | |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
350 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
|
351 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
|
352 } |
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 } 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
|
355 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
356 |
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 static void |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
359 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
|
360 { |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
361 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
|
362 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
363 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
|
364 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
365 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
|
366 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
|
367 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
368 |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
369 #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
|
370 (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
|
371 #endif |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
372 } |
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
373 |
7286 | 374 |
375 static ssize_t | |
376 ngx_udp_shared_recv(ngx_connection_t *c, u_char *buf, size_t size) | |
377 { | |
378 ssize_t n; | |
379 ngx_buf_t *b; | |
380 | |
381 if (c->udp == NULL || c->udp->buffer == NULL) { | |
382 return NGX_AGAIN; | |
383 } | |
384 | |
385 b = c->udp->buffer; | |
386 | |
387 n = ngx_min(b->last - b->pos, (ssize_t) size); | |
388 | |
389 ngx_memcpy(buf, b->pos, n); | |
390 | |
391 c->udp->buffer = NULL; | |
7440
6d4bc025c5a7
Prevented scheduling events on a shared connection.
Roman Arutyunyan <arut@nginx.com>
parents:
7393
diff
changeset
|
392 |
7286 | 393 c->read->ready = 0; |
7440
6d4bc025c5a7
Prevented scheduling events on a shared connection.
Roman Arutyunyan <arut@nginx.com>
parents:
7393
diff
changeset
|
394 c->read->active = 1; |
7286 | 395 |
396 return n; | |
397 } | |
398 | |
399 | |
400 void | |
401 ngx_udp_rbtree_insert_value(ngx_rbtree_node_t *temp, | |
402 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel) | |
403 { | |
404 ngx_int_t rc; | |
405 ngx_connection_t *c, *ct; | |
406 ngx_rbtree_node_t **p; | |
407 ngx_udp_connection_t *udp, *udpt; | |
408 | |
409 for ( ;; ) { | |
410 | |
411 if (node->key < temp->key) { | |
412 | |
413 p = &temp->left; | |
414 | |
415 } else if (node->key > temp->key) { | |
416 | |
417 p = &temp->right; | |
418 | |
419 } else { /* node->key == temp->key */ | |
420 | |
421 udp = (ngx_udp_connection_t *) node; | |
422 c = udp->connection; | |
423 | |
424 udpt = (ngx_udp_connection_t *) temp; | |
425 ct = udpt->connection; | |
426 | |
427 rc = ngx_cmp_sockaddr(c->sockaddr, c->socklen, | |
428 ct->sockaddr, ct->socklen, 1); | |
429 | |
430 if (rc == 0 && c->listening->wildcard) { | |
431 rc = ngx_cmp_sockaddr(c->local_sockaddr, c->local_socklen, | |
432 ct->local_sockaddr, ct->local_socklen, 1); | |
433 } | |
434 | |
435 p = (rc < 0) ? &temp->left : &temp->right; | |
436 } | |
437 | |
438 if (*p == sentinel) { | |
439 break; | |
440 } | |
441 | |
442 temp = *p; | |
443 } | |
444 | |
445 *p = node; | |
446 node->parent = temp; | |
447 node->left = sentinel; | |
448 node->right = sentinel; | |
449 ngx_rbt_red(node); | |
450 } | |
451 | |
452 | |
453 static ngx_int_t | |
454 ngx_insert_udp_connection(ngx_connection_t *c) | |
455 { | |
456 uint32_t hash; | |
457 ngx_pool_cleanup_t *cln; | |
458 ngx_udp_connection_t *udp; | |
459 | |
460 if (c->udp) { | |
461 return NGX_OK; | |
462 } | |
463 | |
464 udp = ngx_pcalloc(c->pool, sizeof(ngx_udp_connection_t)); | |
465 if (udp == NULL) { | |
466 return NGX_ERROR; | |
467 } | |
468 | |
469 udp->connection = c; | |
470 | |
471 ngx_crc32_init(hash); | |
472 ngx_crc32_update(&hash, (u_char *) c->sockaddr, c->socklen); | |
473 | |
474 if (c->listening->wildcard) { | |
475 ngx_crc32_update(&hash, (u_char *) c->local_sockaddr, c->local_socklen); | |
476 } | |
477 | |
478 ngx_crc32_final(hash); | |
479 | |
480 udp->node.key = hash; | |
481 | |
482 cln = ngx_pool_cleanup_add(c->pool, 0); | |
483 if (cln == NULL) { | |
484 return NGX_ERROR; | |
485 } | |
486 | |
487 cln->data = c; | |
488 cln->handler = ngx_delete_udp_connection; | |
489 | |
490 ngx_rbtree_insert(&c->listening->rbtree, &udp->node); | |
491 | |
492 c->udp = udp; | |
493 | |
494 return NGX_OK; | |
495 } | |
496 | |
497 | |
7393
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
498 void |
7286 | 499 ngx_delete_udp_connection(void *data) |
500 { | |
501 ngx_connection_t *c = data; | |
502 | |
7393
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
503 if (c->udp == NULL) { |
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
504 return; |
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
505 } |
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
506 |
7286 | 507 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
|
508 |
27559d4a5151
Stream: fixed possible use of a freed connection.
Vladimir Homutov <vl@nginx.com>
parents:
7286
diff
changeset
|
509 c->udp = NULL; |
7286 | 510 } |
511 | |
512 | |
513 static ngx_connection_t * | |
514 ngx_lookup_udp_connection(ngx_listening_t *ls, struct sockaddr *sockaddr, | |
515 socklen_t socklen, struct sockaddr *local_sockaddr, socklen_t local_socklen) | |
516 { | |
517 uint32_t hash; | |
518 ngx_int_t rc; | |
519 ngx_connection_t *c; | |
520 ngx_rbtree_node_t *node, *sentinel; | |
521 ngx_udp_connection_t *udp; | |
522 | |
523 #if (NGX_HAVE_UNIX_DOMAIN) | |
524 | |
525 if (sockaddr->sa_family == AF_UNIX) { | |
526 struct sockaddr_un *saun = (struct sockaddr_un *) sockaddr; | |
527 | |
528 if (socklen <= (socklen_t) offsetof(struct sockaddr_un, sun_path) | |
529 || saun->sun_path[0] == '\0') | |
530 { | |
531 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ngx_cycle->log, 0, | |
532 "unbound unix socket"); | |
533 return NULL; | |
534 } | |
535 } | |
536 | |
7285
88a624c9b491
Events: moved ngx_recvmsg() to new file src/event/ngx_event_udp.c.
Roman Arutyunyan <arut@nginx.com>
parents:
diff
changeset
|
537 #endif |
7286 | 538 |
539 node = ls->rbtree.root; | |
540 sentinel = ls->rbtree.sentinel; | |
541 | |
542 ngx_crc32_init(hash); | |
543 ngx_crc32_update(&hash, (u_char *) sockaddr, socklen); | |
544 | |
545 if (ls->wildcard) { | |
546 ngx_crc32_update(&hash, (u_char *) local_sockaddr, local_socklen); | |
547 } | |
548 | |
549 ngx_crc32_final(hash); | |
550 | |
551 while (node != sentinel) { | |
552 | |
553 if (hash < node->key) { | |
554 node = node->left; | |
555 continue; | |
556 } | |
557 | |
558 if (hash > node->key) { | |
559 node = node->right; | |
560 continue; | |
561 } | |
562 | |
563 /* hash == node->key */ | |
564 | |
565 udp = (ngx_udp_connection_t *) node; | |
566 | |
567 c = udp->connection; | |
568 | |
569 rc = ngx_cmp_sockaddr(sockaddr, socklen, | |
570 c->sockaddr, c->socklen, 1); | |
571 | |
572 if (rc == 0 && ls->wildcard) { | |
573 rc = ngx_cmp_sockaddr(local_sockaddr, local_socklen, | |
574 c->local_sockaddr, c->local_socklen, 1); | |
575 } | |
576 | |
577 if (rc == 0) { | |
578 return c; | |
579 } | |
580 | |
581 node = (rc < 0) ? node->left : node->right; | |
582 } | |
583 | |
584 return NULL; | |
585 } | |
586 | |
7393
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
587 #else |
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
588 |
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
589 void |
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
590 ngx_delete_udp_connection(void *data) |
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
591 { |
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
592 return; |
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
593 } |
4698cede59ff
Stream: proxy_requests directive.
Vladimir Homutov <vl@nginx.com>
parents:
7391
diff
changeset
|
594 |
7286 | 595 #endif |