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

UDP/TCP Bind #39

Open
wants to merge 7 commits 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ src/config.[ch]
src/worker.[ch]
src/misc.[ch]
src/initby.[ch]
src/shdict.[ch]
src/initworkerby.[ch]
src/variable.[ch]
src/args.[ch]
Expand All @@ -56,3 +55,4 @@ html_out/
ebooks.sh
*.pdf
*.patch
*.project
139 changes: 134 additions & 5 deletions src/ngx_stream_lua_socket_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@


static int ngx_stream_lua_socket_tcp(lua_State *L);
static int ngx_stream_lua_socket_tcp_bind(lua_State *L);
static int ngx_stream_lua_socket_tcp_connect(lua_State *L);
#if (NGX_STREAM_SSL)
static int ngx_stream_lua_socket_tcp_sslhandshake(lua_State *L);
Expand Down Expand Up @@ -142,12 +143,15 @@ static void ngx_stream_lua_ssl_handshake_handler(ngx_connection_t *c);
static int ngx_stream_lua_ssl_free_session(lua_State *L);
#endif
static void ngx_stream_lua_socket_tcp_close_connection(ngx_connection_t *c);
static void ngx_stream_lua_inject_socket_option_consts(lua_State *L);


enum {
SOCKET_CTX_INDEX = 1,
SOCKET_TIMEOUT_INDEX = 2,
SOCKET_KEY_INDEX = 3
SOCKET_KEY_INDEX = 3,
SOCKET_BIND_INDEX = 4, /* only in upstream cosocket */
SOCKET_IP_TRANSPARENT_INDEX = 5
};


Expand Down Expand Up @@ -203,11 +207,24 @@ static char ngx_stream_lua_ssl_session_metatable_key;
#endif


static void
ngx_stream_lua_inject_socket_option_consts(lua_State *L)
{
/* {{{ socket option constants */
#if (NGX_HAVE_TRANSPARENT_PROXY)
lua_pushinteger(L, NGX_STREAM_LUA_SOCKET_OPTION_TRANSPARENT);
lua_setfield(L, -2, "IP_TRANSPARENT");
#endif
}


void
ngx_stream_lua_inject_socket_tcp_api(ngx_log_t *log, lua_State *L)
{
ngx_int_t rc;

ngx_stream_lua_inject_socket_option_consts(L);

lua_createtable(L, 0, 3 /* nrec */); /* ngx.socket */

lua_pushcfunction(L, ngx_stream_lua_socket_tcp);
Expand Down Expand Up @@ -277,7 +294,10 @@ ngx_stream_lua_inject_socket_tcp_api(ngx_log_t *log, lua_State *L)

/* {{{tcp object metatable */
lua_pushlightuserdata(L, &ngx_stream_lua_tcp_socket_metatable_key);
lua_createtable(L, 0 /* narr */, 11 /* nrec */);
lua_createtable(L, 0 /* narr */, 12 /* nrec */);

lua_pushcfunction(L, ngx_stream_lua_socket_tcp_bind);
lua_setfield(L, -2, "bind");

lua_pushcfunction(L, ngx_stream_lua_socket_tcp_connect);
lua_setfield(L, -2, "connect");
Expand Down Expand Up @@ -396,7 +416,7 @@ ngx_stream_lua_socket_tcp(lua_State *L)
ngx_stream_lua_check_context(L, ctx, NGX_STREAM_LUA_CONTEXT_CONTENT
| NGX_STREAM_LUA_CONTEXT_TIMER);

lua_createtable(L, 3 /* narr */, 1 /* nrec */);
lua_createtable(L, 4 /* narr */, 1 /* nrec */);
lua_pushlightuserdata(L, &ngx_stream_lua_tcp_socket_metatable_key);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
Expand All @@ -407,6 +427,54 @@ ngx_stream_lua_socket_tcp(lua_State *L)
}


static int
ngx_stream_lua_socket_tcp_bind(lua_State *L)
{
ngx_stream_session_t *s;
ngx_stream_lua_ctx_t *ctx;
int n;
u_char *text;
size_t len;
ngx_addr_t *local;

n = lua_gettop(L);
if (n != 2) {
return luaL_error(L, "expecting 2 arguments, but got %d",
lua_gettop(L));
}

s = ngx_stream_lua_get_session(L);
if (s == NULL) {
return luaL_error(L, "no request found");
}

ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module);
if (ctx == NULL) {
return luaL_error(L, "no ctx found");
}

ngx_stream_lua_check_context(L, ctx, NGX_STREAM_LUA_CONTEXT_CONTENT
| NGX_STREAM_LUA_CONTEXT_TIMER);

luaL_checktype(L, 1, LUA_TTABLE);

text = (u_char *) luaL_checklstring(L, 2, &len);
local = ngx_stream_lua_parse_addr(L, text, len);
if (local == NULL) {
lua_pushnil(L);
lua_pushfstring(L, "bad address");
return 2;
}

/* TODO: we may reuse the userdata here */
lua_rawseti(L, 1, SOCKET_BIND_INDEX);
ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
"lua tcp socket bind ip: %V", &local->name);
lua_pushboolean(L, 1);
return 1;
}


static int
ngx_stream_lua_socket_tcp_connect(lua_State *L)
{
Expand All @@ -423,6 +491,7 @@ ngx_stream_lua_socket_tcp_connect(lua_State *L)
ngx_int_t rc;
ngx_stream_lua_srv_conf_t *lscf;
ngx_peer_connection_t *pc;
ngx_addr_t *local;
int timeout;
unsigned custom_pool;
int key_index;
Expand Down Expand Up @@ -579,6 +648,25 @@ ngx_stream_lua_socket_tcp_connect(lua_State *L)

dd("lua peer connection log: %p", pc->log);

lua_rawgeti(L, 1, SOCKET_BIND_INDEX);
local = lua_touserdata(L, -1);
lua_pop(L, 1);

if (local) {
u->peer.local = local;
}

#if (NGX_HAVE_TRANSPARENT_PROXY)
lua_rawgeti(L, 1, SOCKET_IP_TRANSPARENT_INDEX);

if (lua_tointeger(L, -1) > 0) {
pc->transparent = 1;
ngx_log_debug0(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
"stream lua set TCP upstream with IP_TRANSPARENT");
}
lua_pop(L, 1);
#endif

lua_rawgeti(L, 1, SOCKET_TIMEOUT_INDEX);
timeout = (ngx_int_t) lua_tointeger(L, -1);
lua_pop(L, 1);
Expand Down Expand Up @@ -2505,8 +2593,49 @@ ngx_stream_lua_socket_tcp_close(lua_State *L)
static int
ngx_stream_lua_socket_tcp_setoption(lua_State *L)
{
/* TODO */
return 0;
ngx_stream_session_t *s;
ngx_stream_lua_ctx_t *ctx;
int n;
int option;

n = lua_gettop(L);

if (n < 2) {
return luaL_error(L, "ngx.socket setoption: expecting 2 or 3 "
"arguments (including the object) but seen %d",
lua_gettop(L));
}

s = ngx_stream_lua_get_session(L);
if (s == NULL) {
return luaL_error(L, "no request found");
}

ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module);
if (ctx == NULL) {
return luaL_error(L, "no ctx found");
}

ngx_stream_lua_check_context(L, ctx, NGX_STREAM_LUA_CONTEXT_CONTENT
| NGX_STREAM_LUA_CONTEXT_TIMER);

luaL_checktype(L, 1, LUA_TTABLE);

option = luaL_checkint(L, 2);

switch (option) {
#if (NGX_HAVE_TRANSPARENT_PROXY)
case NGX_STREAM_LUA_SOCKET_OPTION_TRANSPARENT:
lua_rawseti(L, 1, SOCKET_IP_TRANSPARENT_INDEX);
lua_pushboolean(L, 1);
break;
#endif
default:
return luaL_error(L, "invalid tcp socket option: %d", option);

}

return 1;
}


Expand Down
3 changes: 3 additions & 0 deletions src/ngx_stream_lua_socket_tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#define NGX_STREAM_LUA_SOCKET_FT_CLIENTABORT 0x0080
#define NGX_STREAM_LUA_SOCKET_FT_SSL 0x0100

#if (NGX_HAVE_TRANSPARENT_PROXY)
#define NGX_STREAM_LUA_SOCKET_OPTION_TRANSPARENT 1
#endif

typedef struct ngx_stream_lua_socket_tcp_upstream_s
ngx_stream_lua_socket_tcp_upstream_t;
Expand Down
Loading