Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support tcp binding ip:port or ip of ipv4 or ipv6 #2381

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ngx_http_lua_socket_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ ngx_http_lua_socket_tcp_bind(lua_State *L)

text = (u_char *) luaL_checklstring(L, 2, &len);

local = ngx_http_lua_parse_addr(L, text, len);
local = ngx_http_lua_parse_addr_port(L, text, len);
if (local == NULL) {
lua_pushnil(L);
lua_pushfstring(L, "bad address");
Expand Down Expand Up @@ -3430,7 +3430,7 @@ ngx_http_lua_socket_tcp_handler(ngx_event_t *ev)
static ngx_int_t
ngx_http_lua_socket_tcp_get_peer(ngx_peer_connection_t *pc, void *data)
{
/* empty */
pc->type = SOCK_STREAM;
return NGX_OK;
}

Expand Down
55 changes: 55 additions & 0 deletions src/ngx_http_lua_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -4555,4 +4555,59 @@ ngx_http_lua_parse_addr(lua_State *L, u_char *text, size_t len)
}


ngx_addr_t *
ngx_http_lua_parse_addr_port(lua_State *L, u_char *text, size_t len)
{
u_char *p, *last;
ngx_int_t port;
ngx_addr_t *addr;
size_t plen;

addr = ngx_http_lua_parse_addr(L, text, len);
if (addr != NULL) {
return addr;
}

last = text + len;

#if (NGX_HAVE_INET6)
if (len && text[0] == '[') {
p = ngx_strlchr(text, last, ']');
if (p == NULL || p == last - 1 || *++p != ':') {
return NULL;
}

text++;
len -= 2;
} else
#endif

{
p = ngx_strlchr(text, last, ':');
if (p == NULL) {
return NULL;
}
}

p++;
plen = last - p;

port = ngx_atoi(p, plen);
if (port < 1 || port > 65535) {
return NULL;
}

len -= plen + 1;

addr = ngx_http_lua_parse_addr(L, text, len);
if (addr == NULL) {
return NULL;
}

ngx_inet_set_port(addr->sockaddr, (in_port_t) port);

return addr;
}


/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
2 changes: 2 additions & 0 deletions src/ngx_http_lua_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ ngx_int_t ngx_http_lua_decode_base64mime(ngx_str_t *dst, ngx_str_t *src);

ngx_addr_t *ngx_http_lua_parse_addr(lua_State *L, u_char *text, size_t len);

ngx_addr_t *ngx_http_lua_parse_addr_port(lua_State *L, u_char *text, size_t len);

size_t ngx_http_lua_escape_log(u_char *dst, u_char *src, size_t size);


Expand Down
Loading