mirror of
https://github.com/zotanmew/nginx-rtmp-module.git
synced 2024-05-11 23:01:09 +02:00
implemented encrypted handshake
This commit is contained in:
parent
606675085d
commit
bd40fe63f9
4
config
4
config
|
@ -13,10 +13,12 @@ CORE_MODULES="$CORE_MODULES
|
|||
|
||||
NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
|
||||
$ngx_addon_dir/ngx_rtmp.c \
|
||||
$ngx_addon_dir/ngx_rtmp_init.c \
|
||||
$ngx_addon_dir/ngx_rtmp_handshake.c \
|
||||
$ngx_addon_dir/ngx_rtmp_handler.c \
|
||||
$ngx_addon_dir/ngx_rtmp_amf.c \
|
||||
$ngx_addon_dir/ngx_rtmp_send.c \
|
||||
$ngx_addon_dir/ngx_rtmp_shared.c \
|
||||
$ngx_addon_dir/ngx_rtmp_handler.c \
|
||||
$ngx_addon_dir/ngx_rtmp_receive.c \
|
||||
$ngx_addon_dir/ngx_rtmp_core_module.c \
|
||||
$ngx_addon_dir/ngx_rtmp_cmd_module.c \
|
||||
|
|
858
doc/rtmp.py
Normal file
858
doc/rtmp.py
Normal file
File diff suppressed because one or more lines are too long
|
@ -776,4 +776,3 @@ ngx_rtmp_rmemcpy(void *dst, void* src, size_t n)
|
|||
|
||||
return dst;
|
||||
}
|
||||
|
||||
|
|
24
ngx_rtmp.h
24
ngx_rtmp.h
|
@ -101,18 +101,9 @@ typedef struct {
|
|||
|
||||
#define NGX_LOG_DEBUG_RTMP NGX_LOG_DEBUG_CORE
|
||||
|
||||
#define NGX_RTMP_HANDSHAKE_SIZE 1536
|
||||
|
||||
#define NGX_RTMP_DEFAULT_CHUNK_SIZE 128
|
||||
|
||||
|
||||
/* RTMP handshake stages */
|
||||
#define NGX_RTMP_HS_READ_DATA 0
|
||||
#define NGX_RTMP_HS_WRITE_DATA 1
|
||||
#define NGX_RTMP_HS_WRITE_ECHO 2
|
||||
#define NGX_RTMP_HS_READ_ECHO 3
|
||||
|
||||
|
||||
/* RTMP message types */
|
||||
#define NGX_RTMP_MSG_CHUNK_SIZE 1
|
||||
#define NGX_RTMP_MSG_ABORT 2
|
||||
|
@ -200,9 +191,9 @@ typedef struct {
|
|||
uint32_t vcodecs;
|
||||
ngx_str_t page_url;
|
||||
|
||||
/* TODO: allocate this bufs from shared pool */
|
||||
ngx_buf_t hs_in_buf;
|
||||
ngx_buf_t hs_out_buf;
|
||||
/* handshake data */
|
||||
ngx_buf_t *hs_in;
|
||||
ngx_buf_t *hs_out1, *hs_out2;
|
||||
ngx_uint_t hs_stage;
|
||||
|
||||
/* connection timestamps */
|
||||
|
@ -347,9 +338,14 @@ char* ngx_rtmp_message_type(uint8_t type);
|
|||
char* ngx_rtmp_user_message_type(uint16_t evt);
|
||||
#endif
|
||||
|
||||
void ngx_rtmp_init_connection(ngx_connection_t *c);
|
||||
|
||||
void ngx_rtmp_init_connection(ngx_connection_t *c);
|
||||
void ngx_rtmp_finalize_session(ngx_rtmp_session_t *s);
|
||||
u_char * ngx_rtmp_log_error(ngx_log_t *log, u_char *buf, size_t len);
|
||||
void ngx_rtmp_handshake(ngx_rtmp_session_t *s);
|
||||
void ngx_rtmp_free_handshake_buffers(ngx_rtmp_session_t *s);
|
||||
void ngx_rtmp_cycle(ngx_rtmp_session_t *s);
|
||||
|
||||
|
||||
ngx_int_t ngx_rtmp_set_chunk_size(ngx_rtmp_session_t *s, ngx_uint_t size);
|
||||
|
||||
|
||||
|
|
|
@ -1,24 +1,12 @@
|
|||
|
||||
/*
|
||||
* Copyright (c) 2012 Roman Arutyunyan
|
||||
*/
|
||||
|
||||
|
||||
#include <ngx_config.h>
|
||||
#include <ngx_core.h>
|
||||
#include <ngx_event.h>
|
||||
#include <strings.h>
|
||||
|
||||
#include "ngx_rtmp.h"
|
||||
#include "ngx_rtmp_amf.h"
|
||||
|
||||
|
||||
static void ngx_rtmp_init_session(ngx_connection_t *c);
|
||||
static void ngx_rtmp_close_connection(ngx_connection_t *c);
|
||||
|
||||
static void ngx_rtmp_handshake_recv(ngx_event_t *rev);
|
||||
static void ngx_rtmp_handshake_send(ngx_event_t *rev);
|
||||
|
||||
static void ngx_rtmp_recv(ngx_event_t *rev);
|
||||
static void ngx_rtmp_send(ngx_event_t *rev);
|
||||
static ngx_int_t ngx_rtmp_receive_message(ngx_rtmp_session_t *s,
|
||||
|
@ -28,8 +16,9 @@ static ngx_int_t ngx_rtmp_finalize_set_chunk_size(ngx_rtmp_session_t *s);
|
|||
|
||||
#ifdef NGX_DEBUG
|
||||
char*
|
||||
ngx_rtmp_message_type(uint8_t type) {
|
||||
static char* types[] = {
|
||||
ngx_rtmp_message_type(uint8_t type)
|
||||
{
|
||||
static char* types[] = {
|
||||
"?",
|
||||
"chunk_size",
|
||||
"abort",
|
||||
|
@ -62,8 +51,9 @@ ngx_rtmp_message_type(uint8_t type) {
|
|||
|
||||
|
||||
char*
|
||||
ngx_rtmp_user_message_type(uint16_t evt) {
|
||||
static char* evts[] = {
|
||||
ngx_rtmp_user_message_type(uint16_t evt)
|
||||
{
|
||||
static char* evts[] = {
|
||||
"stream_begin",
|
||||
"stream_eof",
|
||||
"stream dry",
|
||||
|
@ -79,390 +69,17 @@ ngx_rtmp_user_message_type(uint16_t evt) {
|
|||
}
|
||||
#endif
|
||||
|
||||
void
|
||||
ngx_rtmp_init_connection(ngx_connection_t *c)
|
||||
{
|
||||
ngx_uint_t i;
|
||||
ngx_rtmp_port_t *port;
|
||||
struct sockaddr *sa;
|
||||
struct sockaddr_in *sin;
|
||||
ngx_rtmp_log_ctx_t *ctx;
|
||||
ngx_rtmp_in_addr_t *addr;
|
||||
ngx_rtmp_session_t *s;
|
||||
ngx_rtmp_addr_conf_t *addr_conf;
|
||||
#if (NGX_HAVE_INET6)
|
||||
struct sockaddr_in6 *sin6;
|
||||
ngx_rtmp_in6_addr_t *addr6;
|
||||
#endif
|
||||
|
||||
|
||||
/* find the server configuration for the address:port */
|
||||
|
||||
/* AF_INET only */
|
||||
|
||||
port = c->listening->servers;
|
||||
|
||||
if (port->naddrs > 1) {
|
||||
|
||||
/*
|
||||
* There are several addresses on this port and one of them
|
||||
* is the "*:port" wildcard so getsockname() is needed to determine
|
||||
* the server address.
|
||||
*
|
||||
* AcceptEx() already gave this address.
|
||||
*/
|
||||
|
||||
if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) {
|
||||
ngx_rtmp_close_connection(c);
|
||||
return;
|
||||
}
|
||||
|
||||
sa = c->local_sockaddr;
|
||||
|
||||
switch (sa->sa_family) {
|
||||
|
||||
#if (NGX_HAVE_INET6)
|
||||
case AF_INET6:
|
||||
sin6 = (struct sockaddr_in6 *) sa;
|
||||
|
||||
addr6 = port->addrs;
|
||||
|
||||
/* the last address is "*" */
|
||||
|
||||
for (i = 0; i < port->naddrs - 1; i++) {
|
||||
if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
addr_conf = &addr6[i].conf;
|
||||
|
||||
break;
|
||||
#endif
|
||||
|
||||
default: /* AF_INET */
|
||||
sin = (struct sockaddr_in *) sa;
|
||||
|
||||
addr = port->addrs;
|
||||
|
||||
/* the last address is "*" */
|
||||
|
||||
for (i = 0; i < port->naddrs - 1; i++) {
|
||||
if (addr[i].addr == sin->sin_addr.s_addr) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
addr_conf = &addr[i].conf;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
} else {
|
||||
switch (c->local_sockaddr->sa_family) {
|
||||
|
||||
#if (NGX_HAVE_INET6)
|
||||
case AF_INET6:
|
||||
addr6 = port->addrs;
|
||||
addr_conf = &addr6[0].conf;
|
||||
break;
|
||||
#endif
|
||||
|
||||
default: /* AF_INET */
|
||||
addr = port->addrs;
|
||||
addr_conf = &addr[0].conf;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
s = ngx_pcalloc(c->pool, sizeof(ngx_rtmp_session_t));
|
||||
if (s == NULL) {
|
||||
ngx_rtmp_close_connection(c);
|
||||
return;
|
||||
}
|
||||
|
||||
s->main_conf = addr_conf->ctx->main_conf;
|
||||
s->srv_conf = addr_conf->ctx->srv_conf;
|
||||
|
||||
s->addr_text = &addr_conf->addr_text;
|
||||
|
||||
c->data = s;
|
||||
s->connection = c;
|
||||
|
||||
ngx_log_error(NGX_LOG_INFO, c->log, 0, "*%ui client connected",
|
||||
c->number, &c->addr_text);
|
||||
|
||||
ctx = ngx_palloc(c->pool, sizeof(ngx_rtmp_log_ctx_t));
|
||||
if (ctx == NULL) {
|
||||
ngx_rtmp_close_connection(c);
|
||||
return;
|
||||
}
|
||||
|
||||
ctx->client = &c->addr_text;
|
||||
ctx->session = s;
|
||||
|
||||
c->log->connection = c->number;
|
||||
c->log->handler = ngx_rtmp_log_error;
|
||||
c->log->data = ctx;
|
||||
c->log->action = NULL;
|
||||
|
||||
c->log_error = NGX_ERROR_INFO;
|
||||
|
||||
ngx_rtmp_init_session(c);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
ngx_rtmp_init_session(ngx_connection_t *c)
|
||||
{
|
||||
ngx_rtmp_session_t *s;
|
||||
ngx_rtmp_core_main_conf_t *cmcf;
|
||||
ngx_rtmp_core_srv_conf_t *cscf;
|
||||
ngx_buf_t *b;
|
||||
size_t n, size;
|
||||
ngx_rtmp_handler_pt *h;
|
||||
ngx_array_t *ch;
|
||||
|
||||
s = c->data;
|
||||
|
||||
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
|
||||
|
||||
s->ctx = ngx_pcalloc(c->pool, sizeof(void *) * ngx_rtmp_max_module);
|
||||
if (s->ctx == NULL) {
|
||||
ngx_rtmp_close_connection(c);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
s->in_streams = ngx_pcalloc(c->pool, sizeof(ngx_rtmp_stream_t)
|
||||
* cscf->max_streams);
|
||||
if (s->in_streams == NULL) {
|
||||
ngx_rtmp_close_connection(c);
|
||||
return;
|
||||
}
|
||||
size = NGX_RTMP_HANDSHAKE_SIZE + 1;
|
||||
|
||||
s->epoch = ngx_current_msec;
|
||||
s->timeout = cscf->timeout;
|
||||
ngx_rtmp_set_chunk_size(s, NGX_RTMP_DEFAULT_CHUNK_SIZE);
|
||||
|
||||
|
||||
/* start handshake */
|
||||
b = &s->hs_in_buf;
|
||||
b->start = b->pos = b->last = ngx_pcalloc(s->in_pool, size);
|
||||
b->end = b->start + size;
|
||||
b->temporary = 1;
|
||||
|
||||
b = &s->hs_out_buf;
|
||||
b->start = b->pos = b->last = ngx_pcalloc(s->in_pool, size);
|
||||
b->end = b->start + size;
|
||||
b->temporary = 1;
|
||||
|
||||
c->write->handler = ngx_rtmp_handshake_send;
|
||||
c->read->handler = ngx_rtmp_handshake_recv;
|
||||
|
||||
/* call connect callbacks */
|
||||
cmcf = ngx_rtmp_get_module_main_conf(s, ngx_rtmp_core_module);
|
||||
|
||||
ch = &cmcf->events[NGX_RTMP_CONNECT];
|
||||
h = ch->elts;
|
||||
for(n = 0; n < ch->nelts; ++n, ++h) {
|
||||
if (*h) {
|
||||
if ((*h)(s, NULL, NULL) != NGX_OK) {
|
||||
ngx_rtmp_finalize_session(s);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ngx_rtmp_handshake_recv(c->read);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ngx_rtmp_handshake_recv(ngx_event_t *rev)
|
||||
ngx_rtmp_cycle(ngx_rtmp_session_t *s)
|
||||
{
|
||||
ssize_t n;
|
||||
ngx_connection_t *c;
|
||||
ngx_rtmp_session_t *s;
|
||||
ngx_buf_t *b;
|
||||
u_char *p;
|
||||
|
||||
c = rev->data;
|
||||
s = c->data;
|
||||
|
||||
if (c->destroyed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (rev->timedout) {
|
||||
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
||||
c->timedout = 1;
|
||||
ngx_rtmp_finalize_session(s);
|
||||
return;
|
||||
}
|
||||
|
||||
if (rev->timer_set) {
|
||||
ngx_del_timer(rev);
|
||||
}
|
||||
|
||||
b = (s->hs_stage == NGX_RTMP_HS_READ_DATA)
|
||||
? &s->hs_in_buf
|
||||
: &s->hs_out_buf;
|
||||
|
||||
while (b->last != b->end) {
|
||||
|
||||
n = c->recv(c, b->last, b->end - b->last);
|
||||
|
||||
if (n == NGX_ERROR || n == 0) {
|
||||
ngx_rtmp_finalize_session(s);
|
||||
return;
|
||||
}
|
||||
|
||||
if (n == NGX_AGAIN) {
|
||||
ngx_add_timer(rev, s->timeout);
|
||||
if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
|
||||
ngx_rtmp_finalize_session(s);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
b->last += n;
|
||||
}
|
||||
|
||||
if (rev->active) {
|
||||
ngx_del_event(c->read, NGX_READ_EVENT, 0);
|
||||
}
|
||||
|
||||
++s->hs_stage;
|
||||
|
||||
if (s->hs_stage == NGX_RTMP_HS_WRITE_DATA) {
|
||||
|
||||
if (*b->pos != NGX_RTMP_VERSION) {
|
||||
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ERROR,
|
||||
"invalid handshake signature");
|
||||
ngx_rtmp_finalize_session(s);
|
||||
return;
|
||||
}
|
||||
|
||||
/* version is never needed anymore */
|
||||
++b->pos;
|
||||
|
||||
/* store current time as our epoch */
|
||||
s->epoch = ngx_current_msec;
|
||||
|
||||
/* read client epoch */
|
||||
p = (u_char*)&s->peer_epoch;
|
||||
*p++ = b->pos[3];
|
||||
*p++ = b->pos[2];
|
||||
*p++ = b->pos[1];
|
||||
*p++ = b->pos[0];
|
||||
|
||||
/* prepare output signature:
|
||||
* set version, set epoch, fill zeroes */
|
||||
p = (u_char*)&s->epoch;
|
||||
b = &s->hs_out_buf;
|
||||
b->pos[0] = NGX_RTMP_VERSION;
|
||||
b->pos[4] = *p++;
|
||||
b->pos[3] = *p++;
|
||||
b->pos[2] = *p++;
|
||||
b->pos[1] = *p++;
|
||||
b->pos[5] = b->pos[6] = b->pos[7] = b->pos[8] = 0;
|
||||
for(b->last = b->pos + 9, n = 1;
|
||||
b->last < b->end;
|
||||
++b->last, ++n)
|
||||
{
|
||||
*b->last = (u_char)(n & 0xff);
|
||||
}
|
||||
|
||||
/* reply timestamp is the same as out epoch */
|
||||
/*ngx_memcpy(s->hs_in_buf.pos + 4, b->pos + 1, 4);*/
|
||||
|
||||
ngx_rtmp_handshake_send(c->write);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* handshake done */
|
||||
ngx_reset_pool(s->in_pool);
|
||||
|
||||
c = s->connection;
|
||||
c->read->handler = ngx_rtmp_recv;
|
||||
c->write->handler = ngx_rtmp_send;
|
||||
|
||||
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, c->log, 0,
|
||||
"RTMP handshake done; epoch=%uD peer_epoch=%uD",
|
||||
s->epoch, s->peer_epoch);
|
||||
|
||||
ngx_rtmp_recv(rev);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ngx_rtmp_handshake_send(ngx_event_t *wev)
|
||||
{
|
||||
ngx_int_t n;
|
||||
ngx_connection_t *c;
|
||||
ngx_rtmp_session_t *s;
|
||||
ngx_buf_t *b;
|
||||
|
||||
c = wev->data;
|
||||
s = c->data;
|
||||
|
||||
if (c->destroyed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (wev->timedout) {
|
||||
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
||||
"client timed out");
|
||||
c->timedout = 1;
|
||||
ngx_rtmp_finalize_session(s);
|
||||
return;
|
||||
}
|
||||
|
||||
if (wev->timer_set) {
|
||||
ngx_del_timer(wev);
|
||||
}
|
||||
|
||||
restart:
|
||||
|
||||
b = (s->hs_stage == NGX_RTMP_HS_WRITE_DATA)
|
||||
? &s->hs_out_buf
|
||||
: &s->hs_in_buf;
|
||||
|
||||
while(b->pos != b->last) {
|
||||
|
||||
n = c->send(c, b->pos, b->last - b->pos);
|
||||
|
||||
if (n == NGX_ERROR) {
|
||||
ngx_rtmp_finalize_session(s);
|
||||
return;
|
||||
}
|
||||
|
||||
if (n == NGX_AGAIN || n == 0) {
|
||||
ngx_add_timer(c->write, s->timeout);
|
||||
if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
|
||||
ngx_rtmp_finalize_session(s);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
b->pos += n;
|
||||
}
|
||||
|
||||
++s->hs_stage;
|
||||
|
||||
if (s->hs_stage == NGX_RTMP_HS_WRITE_ECHO) {
|
||||
goto restart;
|
||||
}
|
||||
|
||||
if (wev->active) {
|
||||
ngx_del_event(wev, NGX_WRITE_EVENT, 0);
|
||||
}
|
||||
|
||||
b = &s->hs_out_buf;
|
||||
b->pos = b->last = b->start + 1;
|
||||
ngx_rtmp_handshake_recv(c->read);
|
||||
ngx_rtmp_recv(c->read);
|
||||
}
|
||||
|
||||
|
||||
|
@ -493,7 +110,7 @@ ngx_rtmp_alloc_in_buf(ngx_rtmp_session_t *s)
|
|||
}
|
||||
|
||||
|
||||
void
|
||||
static void
|
||||
ngx_rtmp_recv(ngx_event_t *rev)
|
||||
{
|
||||
ngx_int_t n;
|
||||
|
@ -778,6 +395,7 @@ ngx_rtmp_recv(ngx_event_t *rev)
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
ngx_rtmp_send(ngx_event_t *wev)
|
||||
{
|
||||
|
@ -1179,117 +797,3 @@ ngx_rtmp_finalize_set_chunk_size(ngx_rtmp_session_t *s)
|
|||
}
|
||||
|
||||
|
||||
static void
|
||||
ngx_rtmp_close_connection(ngx_connection_t *c)
|
||||
{
|
||||
ngx_pool_t *pool;
|
||||
|
||||
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0, "close connection");
|
||||
|
||||
pool = c->pool;
|
||||
ngx_close_connection(c);
|
||||
ngx_destroy_pool(pool);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
ngx_rtmp_close_session_handler(ngx_event_t *e)
|
||||
{
|
||||
ngx_rtmp_session_t *s;
|
||||
ngx_connection_t *c;
|
||||
ngx_rtmp_core_main_conf_t *cmcf;
|
||||
ngx_rtmp_core_srv_conf_t *cscf;
|
||||
ngx_rtmp_handler_pt *h;
|
||||
ngx_array_t *dh;
|
||||
size_t n;
|
||||
|
||||
s = e->data;
|
||||
c = s->connection;
|
||||
|
||||
cmcf = ngx_rtmp_get_module_main_conf(s, ngx_rtmp_core_module);
|
||||
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
|
||||
|
||||
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0, "close session");
|
||||
|
||||
if (s) {
|
||||
dh = &cmcf->events[NGX_RTMP_DISCONNECT];
|
||||
h = dh->elts;
|
||||
|
||||
for(n = 0; n < dh->nelts; ++n, ++h) {
|
||||
if (*h) {
|
||||
(*h)(s, NULL, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
if (s->in_old_pool) {
|
||||
ngx_destroy_pool(s->in_old_pool);
|
||||
}
|
||||
|
||||
if (s->in_pool) {
|
||||
ngx_destroy_pool(s->in_pool);
|
||||
}
|
||||
}
|
||||
|
||||
while (s->out_pos != s->out_last) {
|
||||
ngx_rtmp_free_shared_chain(cscf, s->out[s->out_pos++]);
|
||||
s->out_pos %= NGX_RTMP_OUT_QUEUE;
|
||||
}
|
||||
|
||||
ngx_rtmp_close_connection(c);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ngx_rtmp_finalize_session(ngx_rtmp_session_t *s)
|
||||
{
|
||||
ngx_event_t *e;
|
||||
ngx_connection_t *c;
|
||||
|
||||
/* deferred session finalize;
|
||||
* schedule handler here */
|
||||
|
||||
c = s->connection;
|
||||
|
||||
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0, "finalize session");
|
||||
|
||||
c->destroyed = 1;
|
||||
e = &s->close;
|
||||
e->data = s;
|
||||
e->handler = ngx_rtmp_close_session_handler;
|
||||
e->log = c->log;
|
||||
|
||||
ngx_post_event(e, &ngx_posted_events);
|
||||
}
|
||||
|
||||
|
||||
u_char *
|
||||
ngx_rtmp_log_error(ngx_log_t *log, u_char *buf, size_t len)
|
||||
{
|
||||
u_char *p;
|
||||
ngx_rtmp_session_t *s;
|
||||
ngx_rtmp_log_ctx_t *ctx;
|
||||
|
||||
if (log->action) {
|
||||
p = ngx_snprintf(buf, len, " while %s", log->action);
|
||||
len -= p - buf;
|
||||
buf = p;
|
||||
}
|
||||
|
||||
ctx = log->data;
|
||||
|
||||
p = ngx_snprintf(buf, len, ", client: %V", ctx->client);
|
||||
len -= p - buf;
|
||||
buf = p;
|
||||
|
||||
s = ctx->session;
|
||||
|
||||
if (s == NULL) {
|
||||
return p;
|
||||
}
|
||||
|
||||
p = ngx_snprintf(buf, len, ", server: %V", s->addr_text);
|
||||
len -= p - buf;
|
||||
buf = p;
|
||||
|
||||
return p;
|
||||
}
|
||||
|
|
|
@ -3,18 +3,19 @@
|
|||
*/
|
||||
|
||||
|
||||
#include <ngx_config.h>
|
||||
#include <ngx_core.h>
|
||||
#include <ngx_event.h>
|
||||
#include "ngx_rtmp.h"
|
||||
|
||||
#ifdef NGX_SSL
|
||||
#include <openssl/hmac.h>
|
||||
#include <openssl/sha.h>
|
||||
#endif
|
||||
|
||||
|
||||
static void ngx_rtmp_handshake_send(ngx_event_t *wev);
|
||||
static void ngx_rtmp_handshake_recv(ngx_event_t *rev);
|
||||
static void ngx_rtmp_handshake_done(ngx_rtmp_session_t *s);
|
||||
|
||||
|
||||
/* Handshake keys */
|
||||
static const u_char
|
||||
static u_char
|
||||
ngx_rtmp_server_key[] = {
|
||||
'G', 'e', 'n', 'u', 'i', 'n', 'e', ' ', 'A', 'd', 'o', 'b', 'e', ' ',
|
||||
'F', 'l', 'a', 's', 'h', ' ', 'M', 'e', 'd', 'i', 'a', ' ',
|
||||
|
@ -27,7 +28,7 @@ ngx_rtmp_server_key[] = {
|
|||
};
|
||||
|
||||
|
||||
static const u_char
|
||||
static u_char
|
||||
ngx_rtmp_client_key[] = {
|
||||
'G', 'e', 'n', 'u', 'i', 'n', 'e', ' ', 'A', 'd', 'o', 'b', 'e', ' ',
|
||||
'F', 'l', 'a', 's', 'h', ' ', 'P', 'l', 'a', 'y', 'e', 'r', ' ',
|
||||
|
@ -50,29 +51,30 @@ ngx_rtmp_server_version[4] = {
|
|||
|
||||
|
||||
static ngx_str_t ngx_rtmp_server_full_key
|
||||
= { ngx_rtmp_server_key, sizeof(ngx_rtmp_server_key) };
|
||||
static ngx_str_t ngx_rtmp_server_partial_key;
|
||||
= { ngx_rtmp_server_key, 36 };
|
||||
= { sizeof(ngx_rtmp_server_key), ngx_rtmp_server_key };
|
||||
static ngx_str_t ngx_rtmp_server_partial_key
|
||||
= { 36, ngx_rtmp_server_key };
|
||||
|
||||
static ngx_str_t ngx_rtmp_client_full_key
|
||||
= { ngx_rtmp_client_key, sizeof(ngx_rtmp_client_key) };
|
||||
static ngx_str_t ngx_rtmp_client_partial_key
|
||||
= { ngx_rtmp_client_key, 30 };
|
||||
= { 30, ngx_rtmp_client_key };
|
||||
|
||||
|
||||
static ngx_int_t
|
||||
ngx_rtmp_make_digest(ngx_str_t *key, ngx_buf_t *src,
|
||||
u_char *skip, u_char *dst, ngx_log_t *log)
|
||||
{
|
||||
#ifdef NGX_SSL
|
||||
HMAC_CTX hmac;
|
||||
unsigned int len; /* TODO */
|
||||
ngx_int_t rc;
|
||||
|
||||
rc = NGX_ERROR;
|
||||
HMAC_CTX_init(&hmac);
|
||||
|
||||
if (HMAC_Init_ex(&hmac, key->data, key->len,
|
||||
EVP_sha256, NULL) == 0)
|
||||
EVP_sha256(), NULL) == 0)
|
||||
{
|
||||
ngx_log_error(NGX_LOG_INFO, log, 0, "HMAC_Init_ex error");
|
||||
return NGX_ERROR;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (skip && src->pos <= skip && skip <= src->last) {
|
||||
|
@ -80,37 +82,36 @@ ngx_rtmp_make_digest(ngx_str_t *key, ngx_buf_t *src,
|
|||
&& HMAC_Update(&hmac, src->pos, skip - src->pos) == 0)
|
||||
{
|
||||
ngx_log_error(NGX_LOG_INFO, log, 0, "HMAC_Update error");
|
||||
return NGX_ERROR;
|
||||
goto out;
|
||||
}
|
||||
if (src->last != skip + NGX_RTMP_KEYLEN
|
||||
&& HMAC_Update(&hmac, skip + NGX_RTMP_KEYLEN,
|
||||
src->last - skip - NGX_RTMP_KEYLEN) == 0)
|
||||
{
|
||||
ngx_log_error(NGX_LOG_INFO, log, 0, "HMAC_Update error");
|
||||
return NGX_ERROR;
|
||||
goto out;
|
||||
}
|
||||
} else if (HMAC_Update(&hmac, src->pos, src->last - src->pos) == 0) {
|
||||
ngx_log_error(NGX_LOG_INFO, log, 0, "HMAC_Update error");
|
||||
return NGX_ERROR;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (HMAC_Final(&hmac, dst, &len) == 0) {
|
||||
ngx_log_error(NGX_LOG_INFO, log, 0, "HMAC_Final error");
|
||||
return NGX_ERROR;
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* TODO: free? */
|
||||
rc = NGX_OK;
|
||||
|
||||
return NGX_OK;
|
||||
out:
|
||||
HMAC_CTX_cleanup(&hmac);
|
||||
|
||||
#else /* NGX_SSL */
|
||||
return NGX_ERROR;
|
||||
#endif
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
static ngx_int_t
|
||||
ngx_rtmp_get_digest(ngx_buf_t *b, size_t base, ngx_log_t *log)
|
||||
ngx_rtmp_find_digest(ngx_buf_t *b, size_t base, ngx_log_t *log)
|
||||
{
|
||||
size_t n, offs;
|
||||
u_char digest[NGX_RTMP_KEYLEN];
|
||||
|
@ -123,8 +124,8 @@ ngx_rtmp_get_digest(ngx_buf_t *b, size_t base, ngx_log_t *log)
|
|||
offs = (offs % 728) + base + 4;
|
||||
p = b->pos + offs;
|
||||
|
||||
if (ngx_rtmp_make_digest(&ngx_rtmp_client_partial_key,
|
||||
b, p, digest, log) != NGX_OK)
|
||||
if (ngx_rtmp_make_digest(&ngx_rtmp_client_partial_key, b,
|
||||
p, digest, log) != NGX_OK)
|
||||
{
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
@ -138,15 +139,16 @@ ngx_rtmp_get_digest(ngx_buf_t *b, size_t base, ngx_log_t *log)
|
|||
|
||||
|
||||
static ngx_int_t
|
||||
ngx_rtmp_put_digest(ngx_buf_t *b, size_t base, ngx_log_t *log)
|
||||
ngx_rtmp_write_digest(ngx_buf_t *b, size_t base, ngx_log_t *log)
|
||||
{
|
||||
size_t n, offs;
|
||||
u_char *p;
|
||||
|
||||
offs = 0;
|
||||
for (n = 0; n < 4; ++n) {
|
||||
for (n = 8; n < 12; ++n) {
|
||||
offs += b->pos[base + n];
|
||||
}
|
||||
offs = (offs % 728) + base + 4;
|
||||
offs = (offs % 728) + base + 12;
|
||||
p = b->pos + offs;
|
||||
|
||||
if (ngx_rtmp_make_digest(&ngx_rtmp_server_partial_key,
|
||||
|
@ -160,29 +162,29 @@ ngx_rtmp_put_digest(ngx_buf_t *b, size_t base, ngx_log_t *log)
|
|||
|
||||
|
||||
static void
|
||||
ngx_rtmp_make_random_buffer(ngx_buf_t *b)
|
||||
ngx_rtmp_fill_random_buffer(ngx_buf_t *b)
|
||||
{
|
||||
u_char *p;
|
||||
|
||||
for (p = b->pos; p != b->last; ++p) {
|
||||
*p = rand();
|
||||
for (; b->last != b->end; ++b->last) {
|
||||
*b->last = rand();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static ngx_buf_t *
|
||||
ngx_rtmp_alloc_handshake_buffer(ngx_rtmp_session_t *s)
|
||||
ngx_rtmp_alloc_handshake_buffer(ngx_rtmp_session_t *s, int short_buf)
|
||||
{
|
||||
ngx_rtmp_core_srv_conf_t *cscf;
|
||||
ngx_chain_t *cl;
|
||||
ngx_buf_t *b;
|
||||
|
||||
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
|
||||
|
||||
if (cscf->free_hs) {
|
||||
cl = cscf->free_hs;
|
||||
b = cl->buf;
|
||||
cscf->free_hs = cl->next;
|
||||
ngx_free_chain(cacf->pool, cl);
|
||||
ngx_free_chain(cscf->pool, cl);
|
||||
|
||||
} else {
|
||||
b = ngx_pcalloc(cscf->pool, sizeof(ngx_buf_t));
|
||||
if (b == NULL) {
|
||||
|
@ -196,7 +198,12 @@ ngx_rtmp_alloc_handshake_buffer(ngx_rtmp_session_t *s)
|
|||
b->end = b->start + NGX_RTMP_HANDSHAKE_BUFSIZE;
|
||||
}
|
||||
|
||||
b->pos = b->last = b->start;
|
||||
if (short_buf) {
|
||||
b->pos = b->last = b->start + 1;
|
||||
} else {
|
||||
b->pos = b->last = b->start;
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
|
@ -206,7 +213,6 @@ ngx_rtmp_free_handshake_buffer(ngx_rtmp_session_t *s, ngx_buf_t *b)
|
|||
{
|
||||
ngx_rtmp_core_srv_conf_t *cscf;
|
||||
ngx_chain_t *cl;
|
||||
ngx_buf_t *b;
|
||||
|
||||
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
|
||||
cl = ngx_alloc_chain_link(cscf->pool);
|
||||
|
@ -220,103 +226,119 @@ ngx_rtmp_free_handshake_buffer(ngx_rtmp_session_t *s, ngx_buf_t *b)
|
|||
}
|
||||
|
||||
|
||||
void
|
||||
ngx_rtmp_free_handshake_buffers(ngx_rtmp_session_t *s)
|
||||
{
|
||||
if (s->hs_in) {
|
||||
ngx_rtmp_free_handshake_buffer(s, s->hs_in);
|
||||
s->hs_in = NULL;
|
||||
}
|
||||
|
||||
if (s->hs_out1) {
|
||||
ngx_rtmp_free_handshake_buffer(s, s->hs_out1);
|
||||
s->hs_out1 = NULL;
|
||||
}
|
||||
|
||||
if (s->hs_out2) {
|
||||
ngx_rtmp_free_handshake_buffer(s, s->hs_out2);
|
||||
s->hs_out2 = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static ngx_int_t
|
||||
ngx_rtmp_old_handshake_response(ngx_rtmp_session_t *s)
|
||||
{
|
||||
ngx_buf_t *b;
|
||||
u_char *src;
|
||||
size_t len;
|
||||
|
||||
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
||||
"RTMP old-style handshake");
|
||||
|
||||
src = s->hs_in->pos + 8;
|
||||
len = s->hs_in->last - src;
|
||||
|
||||
b = s->hs_out1;
|
||||
*b->last++ = '\x03';
|
||||
b->last = ngx_rtmp_rcpymem(b->last, &s->epoch, 4);
|
||||
ngx_memzero(b->last, 4);
|
||||
b->last = ngx_cpymem(b->last + 4, src, len);
|
||||
|
||||
b = s->hs_out2;
|
||||
b->last = ngx_rtmp_rcpymem(b->last, &s->peer_epoch, 4);
|
||||
ngx_memzero(b->last, 4);
|
||||
b->last = ngx_cpymem(b->last + 4, src, len);
|
||||
|
||||
return NGX_OK;
|
||||
}
|
||||
|
||||
|
||||
static ngx_int_t
|
||||
ngx_rtmp_handshake_response(ngx_rtmp_session_t *s)
|
||||
{
|
||||
u_char *p, *pp;
|
||||
ngx_buf_t b;
|
||||
u_char *p;
|
||||
ngx_buf_t *b;
|
||||
ngx_int_t offs;
|
||||
u_char digest[NGX_RTMP_KEYLEN];
|
||||
ngx_str_t key;
|
||||
|
||||
|
||||
s->hs_out1 = ngx_rtmp_alloc_handshake_buffer(s);
|
||||
s->hs_out1->last = s->hs_out1.end;
|
||||
s->hs_out2 = ngx_rtmp_alloc_handshake_buffer(s);
|
||||
s->hs_out1->last = s->hs_out2.end - 1;
|
||||
|
||||
|
||||
/* read input buffer */
|
||||
b = *s->hs_in;
|
||||
p = b->pos;
|
||||
if (*p != '\x03') {
|
||||
b = s->hs_in;
|
||||
if (*b->pos != '\x03') {
|
||||
ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
|
||||
"Unexpected RTMP version: %i", (int)*p);
|
||||
"Unexpected RTMP version: %i", (ngx_int_t)*b->pos);
|
||||
return NGX_ERROR;
|
||||
}
|
||||
++p;
|
||||
pp = (u_char *)&s->peer_epoch + 3;
|
||||
*pp-- = *p++;
|
||||
*pp-- = *p++;
|
||||
*pp-- = *p++;
|
||||
*pp-- = *p++;
|
||||
if (
|
||||
#ifndef NGX_SSL
|
||||
1 ||
|
||||
#endif
|
||||
*(uint32_t *)p == 0)
|
||||
{
|
||||
/*TODO*/
|
||||
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
||||
"RTMP epoch=%uD", s->peer_epoch);
|
||||
ngx_memzero(p, 4);
|
||||
p += 4;
|
||||
ngx_memcpy(p, s->hs_in->pos + 9, s->hs_out1->last - p);
|
||||
p = s->hs_out;
|
||||
ngx_memzero(p, 8);
|
||||
p += 8;
|
||||
ngx_memcpy(pp, s->hs_in->pos + 9, s->hs_out2->last - p);
|
||||
return NGX_OK;
|
||||
}
|
||||
++b->pos;
|
||||
ngx_rtmp_rmemcpy(&s->peer_epoch, b->pos, 4);
|
||||
|
||||
p = b->pos + 4;
|
||||
ngx_log_debug5(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
||||
"RTMP client version=%i.%i.%i.%i epoch=%uD",
|
||||
(ngx_int_t)p[0], (ngx_int_t)p[1],
|
||||
(ngx_int_t)p[2], (ngx_int_t)p[3],
|
||||
(ngx_int_t)p[3], (ngx_int_t)p[2],
|
||||
(ngx_int_t)p[1], (ngx_int_t)p[0],
|
||||
s->peer_epoch);
|
||||
p += 4;
|
||||
b.pos = p;
|
||||
offs = ngx_rtmp_get_digest(&b, 764, s->connection->log);
|
||||
if (*(uint32_t *)p == 0) {
|
||||
return ngx_rtmp_old_handshake_response(s);
|
||||
}
|
||||
|
||||
offs = ngx_rtmp_find_digest(b, 772, s->connection->log);
|
||||
if (offs == NGX_ERROR) {
|
||||
offs = ngx_rtmp_get_digest(&b, 0, s->connection->log);
|
||||
offs = ngx_rtmp_find_digest(b, 8, s->connection->log);
|
||||
}
|
||||
if (offs == NGX_ERROR) {
|
||||
ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
|
||||
"RTMP digest not found");
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
||||
"RTMP digest found at pos=%i", offs);
|
||||
|
||||
/* create first output buffer */
|
||||
b = *s->hs_out1;
|
||||
p = b.pos;
|
||||
*p++ = '\x03';
|
||||
pp = (u_char *)&s->epoch + 3;
|
||||
*p++ = *pp--;
|
||||
*p++ = *pp--;
|
||||
*p++ = *pp--;
|
||||
*p++ = *pp--;
|
||||
p = ngx_cpymem(p, ngx_rtmp_server_version, 4);
|
||||
b.pos = p;
|
||||
ngx_rtmp_make_random_buffer(&b);
|
||||
if (ngx_rtmp_put_digest(&b, 0, s->connection->log) != NGX_OK) {
|
||||
b = s->hs_out1;
|
||||
*b->last++ = '\x03';
|
||||
b->last = ngx_rtmp_rcpymem(b->last, &s->epoch, 4);
|
||||
b->last = ngx_cpymem(b->last, ngx_rtmp_server_version, 4);
|
||||
ngx_rtmp_fill_random_buffer(b);
|
||||
++b->pos;
|
||||
if (ngx_rtmp_write_digest(b, 0, s->connection->log) != NGX_OK) {
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
--b->pos;
|
||||
|
||||
/* create second output buffer */
|
||||
b = *s->hs_out2;
|
||||
p = b.pos;
|
||||
p = ngx_cpymem(b, s->hs_out1->pos + 1, 8);
|
||||
ngx_rtmp_make_random_buffer(&b);
|
||||
if (ngx_rtmp_make_digest(&ngx_rtmp_server_full_key, &b,
|
||||
b = s->hs_out2;
|
||||
ngx_rtmp_fill_random_buffer(b);
|
||||
if (ngx_rtmp_make_digest(&ngx_rtmp_server_full_key, b,
|
||||
NULL, digest, s->connection->log) != NGX_OK)
|
||||
{
|
||||
return NGX_ERROR;
|
||||
}
|
||||
key.data = digest;
|
||||
key.len = sizeof(digest);
|
||||
p = b.last - key.len;
|
||||
if (ngx_rtmp_make_digest(&key, &b, p, p, s->connection->log) != NGX_OK) {
|
||||
p = b->last - key.len;
|
||||
if (ngx_rtmp_make_digest(&key, b, p, p, s->connection->log) != NGX_OK) {
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
|
@ -324,11 +346,156 @@ ngx_rtmp_handshake_response(ngx_rtmp_session_t *s)
|
|||
}
|
||||
|
||||
|
||||
ngx_int_t
|
||||
ngx_rtmp_response(ngx_rtmp_session_t *s)
|
||||
static void
|
||||
ngx_rtmp_handshake_done(ngx_rtmp_session_t *s)
|
||||
{
|
||||
s->hs_in = ngx_rtmp_alloc_handshake_buffer(s);
|
||||
ngx_rtmp_free_handshake_buffers(s);
|
||||
|
||||
return ngx_rtmp_handshake_recv(s->connection->read);
|
||||
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
||||
"RTMP handshake done");
|
||||
|
||||
ngx_rtmp_cycle(s);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
ngx_rtmp_handshake_recv(ngx_event_t *rev)
|
||||
{
|
||||
ssize_t n;
|
||||
ngx_connection_t *c;
|
||||
ngx_rtmp_session_t *s;
|
||||
ngx_buf_t *b;
|
||||
|
||||
c = rev->data;
|
||||
s = c->data;
|
||||
|
||||
if (c->destroyed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (rev->timedout) {
|
||||
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
||||
c->timedout = 1;
|
||||
ngx_rtmp_finalize_session(s);
|
||||
return;
|
||||
}
|
||||
|
||||
if (rev->timer_set) {
|
||||
ngx_del_timer(rev);
|
||||
}
|
||||
|
||||
b = s->hs_in;
|
||||
|
||||
while (b->last != b->end) {
|
||||
n = c->recv(c, b->last, b->end - b->last);
|
||||
|
||||
if (n == NGX_ERROR || n == 0) {
|
||||
ngx_rtmp_finalize_session(s);
|
||||
return;
|
||||
}
|
||||
|
||||
if (n == NGX_AGAIN) {
|
||||
ngx_add_timer(rev, s->timeout);
|
||||
if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
|
||||
ngx_rtmp_finalize_session(s);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
b->last += n;
|
||||
}
|
||||
|
||||
if (rev->active) {
|
||||
ngx_del_event(c->read, NGX_READ_EVENT, 0);
|
||||
}
|
||||
|
||||
if (++s->hs_stage == 1) {
|
||||
s->hs_out1 = ngx_rtmp_alloc_handshake_buffer(s, 0);
|
||||
s->hs_out2 = ngx_rtmp_alloc_handshake_buffer(s, 1);
|
||||
if (ngx_rtmp_handshake_response(s) != NGX_OK) {
|
||||
ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
||||
"RTMP handshake error");
|
||||
ngx_rtmp_finalize_session(s);
|
||||
return;
|
||||
}
|
||||
ngx_rtmp_handshake_send(c->write);
|
||||
return;
|
||||
}
|
||||
|
||||
ngx_rtmp_handshake_done(s);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
ngx_rtmp_handshake_send(ngx_event_t *wev)
|
||||
{
|
||||
ngx_int_t n;
|
||||
ngx_connection_t *c;
|
||||
ngx_rtmp_session_t *s;
|
||||
ngx_buf_t *b;
|
||||
|
||||
c = wev->data;
|
||||
s = c->data;
|
||||
|
||||
if (c->destroyed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (wev->timedout) {
|
||||
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
||||
"client timed out");
|
||||
c->timedout = 1;
|
||||
ngx_rtmp_finalize_session(s);
|
||||
return;
|
||||
}
|
||||
|
||||
if (wev->timer_set) {
|
||||
ngx_del_timer(wev);
|
||||
}
|
||||
|
||||
restart:
|
||||
|
||||
b = (s->hs_stage == 1 ? s->hs_out1 : s->hs_out2);
|
||||
|
||||
while(b->pos != b->last) {
|
||||
n = c->send(c, b->pos, b->last - b->pos);
|
||||
|
||||
if (n == NGX_ERROR) {
|
||||
ngx_rtmp_finalize_session(s);
|
||||
return;
|
||||
}
|
||||
|
||||
if (n == NGX_AGAIN || n == 0) {
|
||||
ngx_add_timer(c->write, s->timeout);
|
||||
if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
|
||||
ngx_rtmp_finalize_session(s);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
b->pos += n;
|
||||
}
|
||||
|
||||
if (++s->hs_stage == 2) {
|
||||
goto restart;
|
||||
}
|
||||
|
||||
s->hs_in->pos = s->hs_in->last = s->hs_in->start + 1;
|
||||
ngx_rtmp_handshake_recv(c->read);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ngx_rtmp_handshake(ngx_rtmp_session_t *s)
|
||||
{
|
||||
ngx_connection_t *c;
|
||||
|
||||
c = s->connection;
|
||||
c->read->handler = ngx_rtmp_handshake_recv;
|
||||
c->write->handler = ngx_rtmp_handshake_send;
|
||||
|
||||
s->hs_in = ngx_rtmp_alloc_handshake_buffer(s, 0);
|
||||
|
||||
ngx_rtmp_handshake_recv(c->read);
|
||||
}
|
||||
|
||||
|
|
311
ngx_rtmp_init.c
Normal file
311
ngx_rtmp_init.c
Normal file
|
@ -0,0 +1,311 @@
|
|||
/*
|
||||
* Copyright (c) 2012 Roman Arutyunyan
|
||||
*/
|
||||
|
||||
|
||||
#include "ngx_rtmp.h"
|
||||
|
||||
|
||||
static void ngx_rtmp_close_connection(ngx_connection_t *c);
|
||||
static void ngx_rtmp_init_session(ngx_connection_t *c);
|
||||
static u_char * ngx_rtmp_log_error(ngx_log_t *log, u_char *buf, size_t len);
|
||||
|
||||
|
||||
void
|
||||
ngx_rtmp_init_connection(ngx_connection_t *c)
|
||||
{
|
||||
ngx_uint_t i;
|
||||
ngx_rtmp_port_t *port;
|
||||
struct sockaddr *sa;
|
||||
struct sockaddr_in *sin;
|
||||
ngx_rtmp_log_ctx_t *ctx;
|
||||
ngx_rtmp_in_addr_t *addr;
|
||||
ngx_rtmp_session_t *s;
|
||||
ngx_rtmp_addr_conf_t *addr_conf;
|
||||
#if (NGX_HAVE_INET6)
|
||||
struct sockaddr_in6 *sin6;
|
||||
ngx_rtmp_in6_addr_t *addr6;
|
||||
#endif
|
||||
|
||||
|
||||
/* find the server configuration for the address:port */
|
||||
|
||||
/* AF_INET only */
|
||||
|
||||
port = c->listening->servers;
|
||||
|
||||
if (port->naddrs > 1) {
|
||||
|
||||
/*
|
||||
* There are several addresses on this port and one of them
|
||||
* is the "*:port" wildcard so getsockname() is needed to determine
|
||||
* the server address.
|
||||
*
|
||||
* AcceptEx() already gave this address.
|
||||
*/
|
||||
|
||||
if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) {
|
||||
ngx_rtmp_close_connection(c);
|
||||
return;
|
||||
}
|
||||
|
||||
sa = c->local_sockaddr;
|
||||
|
||||
switch (sa->sa_family) {
|
||||
|
||||
#if (NGX_HAVE_INET6)
|
||||
case AF_INET6:
|
||||
sin6 = (struct sockaddr_in6 *) sa;
|
||||
|
||||
addr6 = port->addrs;
|
||||
|
||||
/* the last address is "*" */
|
||||
|
||||
for (i = 0; i < port->naddrs - 1; i++) {
|
||||
if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
addr_conf = &addr6[i].conf;
|
||||
|
||||
break;
|
||||
#endif
|
||||
|
||||
default: /* AF_INET */
|
||||
sin = (struct sockaddr_in *) sa;
|
||||
|
||||
addr = port->addrs;
|
||||
|
||||
/* the last address is "*" */
|
||||
|
||||
for (i = 0; i < port->naddrs - 1; i++) {
|
||||
if (addr[i].addr == sin->sin_addr.s_addr) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
addr_conf = &addr[i].conf;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
} else {
|
||||
switch (c->local_sockaddr->sa_family) {
|
||||
|
||||
#if (NGX_HAVE_INET6)
|
||||
case AF_INET6:
|
||||
addr6 = port->addrs;
|
||||
addr_conf = &addr6[0].conf;
|
||||
break;
|
||||
#endif
|
||||
|
||||
default: /* AF_INET */
|
||||
addr = port->addrs;
|
||||
addr_conf = &addr[0].conf;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
s = ngx_pcalloc(c->pool, sizeof(ngx_rtmp_session_t));
|
||||
if (s == NULL) {
|
||||
ngx_rtmp_close_connection(c);
|
||||
return;
|
||||
}
|
||||
|
||||
s->main_conf = addr_conf->ctx->main_conf;
|
||||
s->srv_conf = addr_conf->ctx->srv_conf;
|
||||
|
||||
s->addr_text = &addr_conf->addr_text;
|
||||
|
||||
c->data = s;
|
||||
s->connection = c;
|
||||
|
||||
ngx_log_error(NGX_LOG_INFO, c->log, 0, "*%ui client connected",
|
||||
c->number, &c->addr_text);
|
||||
|
||||
ctx = ngx_palloc(c->pool, sizeof(ngx_rtmp_log_ctx_t));
|
||||
if (ctx == NULL) {
|
||||
ngx_rtmp_close_connection(c);
|
||||
return;
|
||||
}
|
||||
|
||||
ctx->client = &c->addr_text;
|
||||
ctx->session = s;
|
||||
|
||||
c->log->connection = c->number;
|
||||
c->log->handler = ngx_rtmp_log_error;
|
||||
c->log->data = ctx;
|
||||
c->log->action = NULL;
|
||||
|
||||
c->log_error = NGX_ERROR_INFO;
|
||||
|
||||
ngx_rtmp_init_session(c);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
ngx_rtmp_init_session(ngx_connection_t *c)
|
||||
{
|
||||
ngx_rtmp_session_t *s;
|
||||
ngx_rtmp_core_main_conf_t *cmcf;
|
||||
ngx_rtmp_core_srv_conf_t *cscf;
|
||||
size_t n;
|
||||
ngx_rtmp_handler_pt *h;
|
||||
ngx_array_t *ch;
|
||||
|
||||
s = c->data;
|
||||
|
||||
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
|
||||
|
||||
s->ctx = ngx_pcalloc(c->pool, sizeof(void *) * ngx_rtmp_max_module);
|
||||
if (s->ctx == NULL) {
|
||||
ngx_rtmp_close_connection(c);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
s->in_streams = ngx_pcalloc(c->pool, sizeof(ngx_rtmp_stream_t)
|
||||
* cscf->max_streams);
|
||||
if (s->in_streams == NULL) {
|
||||
ngx_rtmp_close_connection(c);
|
||||
return;
|
||||
}
|
||||
|
||||
s->epoch = ngx_current_msec;
|
||||
s->timeout = cscf->timeout;
|
||||
ngx_rtmp_set_chunk_size(s, NGX_RTMP_DEFAULT_CHUNK_SIZE);
|
||||
|
||||
|
||||
/* call connect callbacks */
|
||||
cmcf = ngx_rtmp_get_module_main_conf(s, ngx_rtmp_core_module);
|
||||
|
||||
ch = &cmcf->events[NGX_RTMP_CONNECT];
|
||||
h = ch->elts;
|
||||
for(n = 0; n < ch->nelts; ++n, ++h) {
|
||||
if (*h) {
|
||||
if ((*h)(s, NULL, NULL) != NGX_OK) {
|
||||
ngx_rtmp_finalize_session(s);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ngx_rtmp_handshake(s);
|
||||
}
|
||||
|
||||
|
||||
static u_char *
|
||||
ngx_rtmp_log_error(ngx_log_t *log, u_char *buf, size_t len)
|
||||
{
|
||||
u_char *p;
|
||||
ngx_rtmp_session_t *s;
|
||||
ngx_rtmp_log_ctx_t *ctx;
|
||||
|
||||
if (log->action) {
|
||||
p = ngx_snprintf(buf, len, " while %s", log->action);
|
||||
len -= p - buf;
|
||||
buf = p;
|
||||
}
|
||||
|
||||
ctx = log->data;
|
||||
|
||||
p = ngx_snprintf(buf, len, ", client: %V", ctx->client);
|
||||
len -= p - buf;
|
||||
buf = p;
|
||||
|
||||
s = ctx->session;
|
||||
|
||||
if (s == NULL) {
|
||||
return p;
|
||||
}
|
||||
|
||||
p = ngx_snprintf(buf, len, ", server: %V", s->addr_text);
|
||||
len -= p - buf;
|
||||
buf = p;
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
ngx_rtmp_close_connection(ngx_connection_t *c)
|
||||
{
|
||||
ngx_pool_t *pool;
|
||||
|
||||
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0, "close connection");
|
||||
|
||||
pool = c->pool;
|
||||
ngx_close_connection(c);
|
||||
ngx_destroy_pool(pool);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
ngx_rtmp_close_session_handler(ngx_event_t *e)
|
||||
{
|
||||
ngx_rtmp_session_t *s;
|
||||
ngx_connection_t *c;
|
||||
ngx_rtmp_core_main_conf_t *cmcf;
|
||||
ngx_rtmp_core_srv_conf_t *cscf;
|
||||
ngx_rtmp_handler_pt *h;
|
||||
ngx_array_t *dh;
|
||||
size_t n;
|
||||
|
||||
s = e->data;
|
||||
c = s->connection;
|
||||
|
||||
cmcf = ngx_rtmp_get_module_main_conf(s, ngx_rtmp_core_module);
|
||||
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
|
||||
|
||||
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0, "close session");
|
||||
|
||||
if (s) {
|
||||
dh = &cmcf->events[NGX_RTMP_DISCONNECT];
|
||||
h = dh->elts;
|
||||
|
||||
for(n = 0; n < dh->nelts; ++n, ++h) {
|
||||
if (*h) {
|
||||
(*h)(s, NULL, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
if (s->in_old_pool) {
|
||||
ngx_destroy_pool(s->in_old_pool);
|
||||
}
|
||||
|
||||
if (s->in_pool) {
|
||||
ngx_destroy_pool(s->in_pool);
|
||||
}
|
||||
|
||||
ngx_rtmp_free_handshake_buffers(s);
|
||||
|
||||
while (s->out_pos != s->out_last) {
|
||||
ngx_rtmp_free_shared_chain(cscf, s->out[s->out_pos++]);
|
||||
s->out_pos %= NGX_RTMP_OUT_QUEUE;
|
||||
}
|
||||
}
|
||||
|
||||
ngx_rtmp_close_connection(c);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ngx_rtmp_finalize_session(ngx_rtmp_session_t *s)
|
||||
{
|
||||
ngx_event_t *e;
|
||||
ngx_connection_t *c;
|
||||
|
||||
c = s->connection;
|
||||
|
||||
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0, "finalize session");
|
||||
|
||||
c->destroyed = 1;
|
||||
e = &s->close;
|
||||
e->data = s;
|
||||
e->handler = ngx_rtmp_close_session_handler;
|
||||
e->log = c->log;
|
||||
|
||||
ngx_post_event(e, &ngx_posted_events);
|
||||
}
|
||||
|
Loading…
Reference in a new issue