static char * ngx_stream_return(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_stream_return_srv_conf_t *rscf = conf; ngx_str_t *value; ngx_stream_core_srv_conf_t *cscf; ngx_stream_compile_complex_value_t ccv; if (rscf->text.value.data) { return "is duplicate"; } value = cf->args->elts; ngx_memzero(&ccv, sizeof(ngx_stream_compile_complex_value_t)); ccv.cf = cf; ccv.value = &value[1]; ccv.complex_value = &rscf->text; if (ngx_stream_compile_complex_value(&ccv) != NGX_OK) { return NGX_CONF_ERROR; } cscf = ngx_stream_conf_get_module_srv_conf(cf, ngx_stream_core_module); cscf->handler = ngx_stream_return_handler; return NGX_CONF_OK; }
static char * ngx_stream_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_stream_proxy_srv_conf_t *pscf = conf; ngx_url_t u; ngx_str_t *value, *url; ngx_stream_core_srv_conf_t *cscf; if (pscf->upstream) { return "is duplicate"; } cscf = ngx_stream_conf_get_module_srv_conf(cf, ngx_stream_core_module); cscf->handler = ngx_stream_proxy_handler; value = cf->args->elts; url = &value[1]; ngx_memzero(&u, sizeof(ngx_url_t)); u.url = *url; u.no_resolve = 1; pscf->upstream = ngx_stream_upstream_add(cf, &u, 0); if (pscf->upstream == NULL) { return NGX_CONF_ERROR; } return NGX_CONF_OK; }
static char *conn_srv_game_srv(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_stream_core_srv_conf_t *cscf; cscf = ngx_stream_conf_get_module_srv_conf(cf, ngx_stream_core_module); cscf->handler = on_game_srv_connected; return NGX_CONF_OK; }
static char * ngx_stream_hello(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_stream_hello_srv_conf_t *hscf; ngx_str_t *value; ngx_stream_core_srv_conf_t *cscf; cscf = ngx_stream_conf_get_module_srv_conf(cf, ngx_stream_core_module); cscf->handler = ngx_stream_hello_handler; hscf = ngx_stream_conf_get_module_srv_conf(cf, ngx_stream_hello_module); value = cf->args->elts; hscf->hello = value[1]; return NGX_CONF_OK; }
static char * ngx_stream_upstream_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ssize_t size; ngx_str_t *value; ngx_stream_upstream_srv_conf_t *uscf; ngx_stream_upstream_main_conf_t *umcf; uscf = ngx_stream_conf_get_module_srv_conf(cf, ngx_stream_upstream_module); umcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_upstream_module); value = cf->args->elts; if (!value[1].len) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid zone name \"%V\"", &value[1]); return NGX_CONF_ERROR; } if (cf->args->nelts == 3) { size = ngx_parse_size(&value[2]); if (size == NGX_ERROR) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid zone size \"%V\"", &value[2]); return NGX_CONF_ERROR; } if (size < (ssize_t) (8 * ngx_pagesize)) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "zone \"%V\" is too small", &value[1]); return NGX_CONF_ERROR; } } else { size = 0; } uscf->shm_zone = ngx_shared_memory_add(cf, &value[1], size, &ngx_stream_upstream_module); if (uscf->shm_zone == NULL) { return NGX_CONF_ERROR; } uscf->shm_zone->init = ngx_stream_upstream_init_zone; uscf->shm_zone->data = umcf; uscf->shm_zone->noreuse = 1; return NGX_CONF_OK; }
static char * ngx_stream_upstream_least_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_stream_upstream_srv_conf_t *uscf; uscf = ngx_stream_conf_get_module_srv_conf(cf, ngx_stream_upstream_module); if (uscf->peer.init_upstream) { ngx_conf_log_error(NGX_LOG_WARN, cf, 0, "load balancing method redefined"); } uscf->peer.init_upstream = ngx_stream_upstream_init_least_conn; uscf->flags = NGX_STREAM_UPSTREAM_CREATE | NGX_STREAM_UPSTREAM_WEIGHT | NGX_STREAM_UPSTREAM_MAX_FAILS | NGX_STREAM_UPSTREAM_FAIL_TIMEOUT | NGX_STREAM_UPSTREAM_DOWN | NGX_STREAM_UPSTREAM_BACKUP; return NGX_CONF_OK; }
static char * ngx_stream_app(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { // ngx_conf_t pcf; char *rv; ngx_stream_core_srv_conf_t *cscf; cscf = ngx_stream_conf_get_module_srv_conf(cf, ngx_stream_core_module); cscf->handler = ngx_stream_app_handler; return NGX_CONF_OK; /* pcf = *cf; cf->cmd_type = NGX_STREAM_APP_CONF; rv = ngx_conf_parse(cf, NULL); *cf = pcf; char *rv; void *mconf; ngx_uint_t m; ngx_conf_t pcf; ngx_stream_module_t *module; ngx_stream_conf_ctx_t *ctx, *stream_ctx; ngx_stream_app_srv_conf_t *ascf, **ascfp; ngx_stream_core_main_conf_t *cmcf; ngx_stream_core_srv_conf_t *cscf; ctx = ngx_pcalloc(cf->pool, sizeof(ngx_stream_conf_ctx_t)); if (ctx == NULL) { return NGX_CONF_ERROR; } stream_ctx = cf->ctx; ctx->main_conf = stream_ctx->main_conf; ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_stream_max_module); if (ctx->srv_conf == NULL) { return NGX_CONF_ERROR; } for (m = 0; ngx_modules[m]; m++) { if (ngx_modules[m]->type != NGX_STREAM_MODULE) { continue; } module = ngx_modules[m]->ctx; if (module->create_srv_conf) { mconf = module->create_srv_conf(cf); if (mconf == NULL) { return NGX_CONF_ERROR; } ctx->srv_conf[ngx_modules[m]->ctx_index] = mconf; } } ascf = ctx->srv_conf[ngx_stream_app_module.ctx_index]; ascf->ctx = ctx; cmcf = ctx->main_conf[ngx_stream_core_module.ctx_index]; ascfp = ngx_array_push(&cmcf->servers); if (ascfp == NULL) { return NGX_CONF_ERROR; } *ascfp = ascf; cscf = ngx_stream_conf_get_module_srv_conf(cf, ngx_stream_core_module); cscf->handler = ngx_stream_app_handler; pcf = *cf; cf->ctx = ctx; cf->cmd_type = NGX_STREAM_APP_CONF; rv = ngx_conf_parse(cf, NULL); *cf = pcf; */ return rv; }
static char * ngx_stream_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_stream_core_srv_conf_t *cscf = conf; ngx_str_t *value, size; ngx_url_t u; ngx_uint_t i, n, backlog; ngx_stream_listen_t *ls, *als; ngx_stream_core_main_conf_t *cmcf; cscf->listen = 1; value = cf->args->elts; ngx_memzero(&u, sizeof(ngx_url_t)); u.url = value[1]; u.listen = 1; if (ngx_parse_url(cf->pool, &u) != NGX_OK) { if (u.err) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s in \"%V\" of the \"listen\" directive", u.err, &u.url); } return NGX_CONF_ERROR; } cmcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_core_module); ls = ngx_array_push_n(&cmcf->listen, u.naddrs); if (ls == NULL) { return NGX_CONF_ERROR; } ngx_memzero(ls, sizeof(ngx_stream_listen_t)); ls->backlog = NGX_LISTEN_BACKLOG; ls->rcvbuf = -1; ls->sndbuf = -1; ls->type = SOCK_STREAM; ls->ctx = cf->ctx; #if (NGX_HAVE_INET6) ls->ipv6only = 1; #endif backlog = 0; for (i = 2; i < cf->args->nelts; i++) { #if !(NGX_WIN32) if (ngx_strcmp(value[i].data, "udp") == 0) { ls->type = SOCK_DGRAM; continue; } #endif if (ngx_strcmp(value[i].data, "bind") == 0) { ls->bind = 1; continue; } if (ngx_strncmp(value[i].data, "backlog=", 8) == 0) { ls->backlog = ngx_atoi(value[i].data + 8, value[i].len - 8); ls->bind = 1; if (ls->backlog == NGX_ERROR || ls->backlog == 0) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid backlog \"%V\"", &value[i]); return NGX_CONF_ERROR; } backlog = 1; continue; } if (ngx_strncmp(value[i].data, "rcvbuf=", 7) == 0) { size.len = value[i].len - 7; size.data = value[i].data + 7; ls->rcvbuf = ngx_parse_size(&size); ls->bind = 1; if (ls->rcvbuf == NGX_ERROR) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid rcvbuf \"%V\"", &value[i]); return NGX_CONF_ERROR; } continue; } if (ngx_strncmp(value[i].data, "sndbuf=", 7) == 0) { size.len = value[i].len - 7; size.data = value[i].data + 7; ls->sndbuf = ngx_parse_size(&size); ls->bind = 1; if (ls->sndbuf == NGX_ERROR) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid sndbuf \"%V\"", &value[i]); return NGX_CONF_ERROR; } continue; } if (ngx_strncmp(value[i].data, "ipv6only=o", 10) == 0) { #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY) if (ngx_strcmp(&value[i].data[10], "n") == 0) { ls->ipv6only = 1; } else if (ngx_strcmp(&value[i].data[10], "ff") == 0) { ls->ipv6only = 0; } else { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid ipv6only flags \"%s\"", &value[i].data[9]); return NGX_CONF_ERROR; } ls->bind = 1; continue; #else ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "bind ipv6only is not supported " "on this platform"); return NGX_CONF_ERROR; #endif } if (ngx_strcmp(value[i].data, "reuseport") == 0) { #if (NGX_HAVE_REUSEPORT) ls->reuseport = 1; ls->bind = 1; #else ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "reuseport is not supported " "on this platform, ignored"); #endif continue; } if (ngx_strcmp(value[i].data, "ssl") == 0) { #if (NGX_STREAM_SSL) ngx_stream_ssl_conf_t *sslcf; sslcf = ngx_stream_conf_get_module_srv_conf(cf, ngx_stream_ssl_module); sslcf->listen = 1; sslcf->file = cf->conf_file->file.name.data; sslcf->line = cf->conf_file->line; ls->ssl = 1; continue; #else ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "the \"ssl\" parameter requires " "ngx_stream_ssl_module"); return NGX_CONF_ERROR; #endif } if (ngx_strncmp(value[i].data, "so_keepalive=", 13) == 0) { if (ngx_strcmp(&value[i].data[13], "on") == 0) { ls->so_keepalive = 1; } else if (ngx_strcmp(&value[i].data[13], "off") == 0) { ls->so_keepalive = 2; } else { #if (NGX_HAVE_KEEPALIVE_TUNABLE) u_char *p, *end; ngx_str_t s; end = value[i].data + value[i].len; s.data = value[i].data + 13; p = ngx_strlchr(s.data, end, ':'); if (p == NULL) { p = end; } if (p > s.data) { s.len = p - s.data; ls->tcp_keepidle = ngx_parse_time(&s, 1); if (ls->tcp_keepidle == (time_t) NGX_ERROR) { goto invalid_so_keepalive; } } s.data = (p < end) ? (p + 1) : end; p = ngx_strlchr(s.data, end, ':'); if (p == NULL) { p = end; } if (p > s.data) { s.len = p - s.data; ls->tcp_keepintvl = ngx_parse_time(&s, 1); if (ls->tcp_keepintvl == (time_t) NGX_ERROR) { goto invalid_so_keepalive; } } s.data = (p < end) ? (p + 1) : end; if (s.data < end) { s.len = end - s.data; ls->tcp_keepcnt = ngx_atoi(s.data, s.len); if (ls->tcp_keepcnt == NGX_ERROR) { goto invalid_so_keepalive; } } if (ls->tcp_keepidle == 0 && ls->tcp_keepintvl == 0 && ls->tcp_keepcnt == 0) { goto invalid_so_keepalive; } ls->so_keepalive = 1; #else ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "the \"so_keepalive\" parameter accepts " "only \"on\" or \"off\" on this platform"); return NGX_CONF_ERROR; #endif } ls->bind = 1; continue; #if (NGX_HAVE_KEEPALIVE_TUNABLE) invalid_so_keepalive: ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid so_keepalive value: \"%s\"", &value[i].data[13]); return NGX_CONF_ERROR; #endif } if (ngx_strcmp(value[i].data, "proxy_protocol") == 0) { ls->proxy_protocol = 1; continue; } ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "the invalid \"%V\" parameter", &value[i]); return NGX_CONF_ERROR; } if (ls->type == SOCK_DGRAM) { if (backlog) { return "\"backlog\" parameter is incompatible with \"udp\""; } #if (NGX_STREAM_SSL) if (ls->ssl) { return "\"ssl\" parameter is incompatible with \"udp\""; } #endif if (ls->so_keepalive) { return "\"so_keepalive\" parameter is incompatible with \"udp\""; } if (ls->proxy_protocol) { return "\"proxy_protocol\" parameter is incompatible with \"udp\""; } } als = cmcf->listen.elts; for (n = 0; n < u.naddrs; n++) { ls[n] = ls[0]; ls[n].sockaddr = u.addrs[n].sockaddr; ls[n].socklen = u.addrs[n].socklen; ls[n].addr_text = u.addrs[n].name; ls[n].wildcard = ngx_inet_wildcard(ls[n].sockaddr); for (i = 0; i < cmcf->listen.nelts - u.naddrs + n; i++) { if (ls[n].type != als[i].type) { continue; } if (ngx_cmp_sockaddr(als[i].sockaddr, als[i].socklen, ls[n].sockaddr, ls[n].socklen, 1) != NGX_OK) { continue; } ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "duplicate \"%V\" address and port pair", &ls[n].addr_text); return NGX_CONF_ERROR; } } return NGX_CONF_OK; }