forked from sergey-dryabzhinsky/nginx-rtmp-module
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ngx_rtmp_proxy_protocol.c
197 lines (141 loc) · 3.78 KB
/
ngx_rtmp_proxy_protocol.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Copyright (C) Roman Arutyunyan
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <nginx.h>
#include "ngx_rtmp_proxy_protocol.h"
static void ngx_rtmp_proxy_protocol_recv(ngx_event_t *rev);
void
ngx_rtmp_proxy_protocol(ngx_rtmp_session_t *s)
{
ngx_event_t *rev;
ngx_connection_t *c;
c = s->connection;
rev = c->read;
rev->handler = ngx_rtmp_proxy_protocol_recv;
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"proxy_protocol: start");
if (rev->ready) {
/* the deferred accept(), rtsig, aio, iocp */
if (ngx_use_accept_mutex) {
ngx_post_event(rev, &ngx_posted_events);
return;
}
rev->handler(rev);
return;
}
ngx_add_timer(rev, s->timeout);
if (ngx_handle_read_event(rev, 0) != NGX_OK) {
ngx_rtmp_finalize_session(s);
return;
}
}
static void
ngx_rtmp_proxy_protocol_recv(ngx_event_t *rev)
{
u_char buf[107], *p, *pp, *text;
size_t len;
ssize_t n;
ngx_err_t err;
ngx_int_t i;
ngx_addr_t addr;
ngx_connection_t *c;
ngx_rtmp_session_t *s;
c = rev->data;
s = c->data;
if (c->destroyed) {
return;
}
if (rev->timedout) {
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
"proxy_protocol: recv: client timed out");
c->timedout = 1;
ngx_rtmp_finalize_session(s);
return;
}
if (rev->timer_set) {
ngx_del_timer(rev);
}
n = recv(c->fd, (char *) buf, sizeof(buf), MSG_PEEK);
err = ngx_socket_errno;
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, c->log, 0, "recv(): %d", n);
if (n == -1) {
if (err == NGX_EAGAIN) {
ngx_add_timer(rev, s->timeout);
if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
ngx_rtmp_finalize_session(s);
}
return;
}
ngx_rtmp_finalize_session(s);
return;
}
p = buf;
if (n <= 8 && ngx_strncmp(p, "PROXY ", 6) != 0) {
goto bad_header;
}
n -= 6;
p += 6;
ngx_memzero(&addr, sizeof(ngx_addr_t));
if (n >= 7 && ngx_strncmp(p, "UNKNOWN", 7) == 0) {
n -= 7;
p += 7;
goto skip;
}
if (n < 5 || ngx_strncmp(p, "TCP", 3) != 0
|| (p[3] != '4' && p[3] != '6') || p[4] != ' ')
{
goto bad_header;
}
n -= 5;
p += 5;
pp = ngx_strlchr(p, p + n, ' ');
if (pp == NULL) {
goto bad_header;
}
if (ngx_parse_addr(s->connection->pool, &addr, p, pp - p) != NGX_OK) {
goto bad_header;
}
n -= pp - p;
p = pp;
skip:
for (i = 0; i + 1 < n; i++) {
if (p[i] == CR && p[i + 1] == LF) {
break;
}
}
if (i + 1 >= n) {
goto bad_header;
}
n = p - buf + i + 2;
if (c->recv(c, buf, n) != n) {
goto failed;
}
if (addr.socklen) {
text = ngx_palloc(s->connection->pool, NGX_SOCKADDR_STRLEN);
if (text == NULL) {
goto failed;
}
len = ngx_sock_ntop(addr.sockaddr,
#if (nginx_version >= 1005003)
addr.socklen,
#endif
text, NGX_SOCKADDR_STRLEN, 0);
if (len == 0) {
goto failed;
}
c->sockaddr = addr.sockaddr;
c->socklen = addr.socklen;
c->addr_text.data = text;
c->addr_text.len = len;
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, c->log, 0,
"proxy_protocol: remote_addr:'%V'", &c->addr_text);
}
ngx_rtmp_handshake(s);
return;
bad_header:
ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxy_protocol: bad header");
failed:
ngx_rtmp_finalize_session(s);
}