Skip to content
Merged
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
1 change: 1 addition & 0 deletions doc/admin-guide/plugins/lua.en.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1838,6 +1838,7 @@ Socket address family

TS_LUA_AF_INET (2)
TS_LUA_AF_INET6 (10)
TS_LUA_AF_UNIX (1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these variables are really used when we are making changes to the outgoing address. So there is no need to support AF_UNIX here



:ref:`TOP <admin-plugins-ts-lua>`
Expand Down
10 changes: 6 additions & 4 deletions plugins/lua/ts_lua_client_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ ts_lua_client_request_client_addr_get_port(lua_State *L)
{
struct sockaddr const *client_ip;
ts_lua_http_ctx *http_ctx;
int port;
int port = 0;

GET_HTTP_CONTEXT(http_ctx, L);

Expand All @@ -802,7 +802,7 @@ ts_lua_client_request_client_addr_get_port(lua_State *L)
} else {
if (client_ip->sa_family == AF_INET) {
port = ((struct sockaddr_in *)client_ip)->sin_port;
} else {
} else if (client_ip->sa_family == AF_INET6) {
port = ((struct sockaddr_in6 *)client_ip)->sin6_port;
}

Expand All @@ -817,7 +817,7 @@ ts_lua_client_request_client_addr_get_incoming_port(lua_State *L)
{
struct sockaddr const *incoming_addr;
ts_lua_http_ctx *http_ctx;
int port;
int port = 0;

GET_HTTP_CONTEXT(http_ctx, L);

Expand All @@ -829,7 +829,7 @@ ts_lua_client_request_client_addr_get_incoming_port(lua_State *L)
} else {
if (incoming_addr->sa_family == AF_INET) {
port = ((struct sockaddr_in *)incoming_addr)->sin_port;
} else {
} else if (incoming_addr->sa_family == AF_INET6) {
port = ((struct sockaddr_in6 *)incoming_addr)->sin6_port;
}

Expand Down Expand Up @@ -866,6 +866,8 @@ ts_lua_client_request_client_addr_get_addr(lua_State *L)
port = ntohs(((struct sockaddr_in6 *)client_ip)->sin6_port);
inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)client_ip)->sin6_addr, cip, sizeof(cip));
family = AF_INET6;
} else if (client_ip->sa_family == AF_UNIX) {
family = AF_UNIX;
}

lua_pushstring(L, cip);
Expand Down
2 changes: 2 additions & 0 deletions plugins/lua/ts_lua_http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,8 @@ ts_lua_http_get_ssn_remote_addr(lua_State *L)
port = ntohs(((struct sockaddr_in6 *)client_ip)->sin6_port);
inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)client_ip)->sin6_addr, cip, sizeof(cip));
family = AF_INET6;
} else if (client_ip->sa_family == AF_UNIX) {
family = AF_UNIX;
}

lua_pushstring(L, cip);
Expand Down
29 changes: 22 additions & 7 deletions plugins/lua/ts_lua_server_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ ts_lua_inject_server_request_server_addr_api(lua_State *L)

lua_pushinteger(L, AF_INET6);
lua_setglobal(L, "TS_LUA_AF_INET6");

lua_pushinteger(L, AF_UNIX);
lua_setglobal(L, "TS_LUA_AF_UNIX");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these variables are really used when we are making changes to the outgoing address. So there is no need to support AF_UNIX here

}

static void
Expand Down Expand Up @@ -768,7 +771,11 @@ ts_lua_server_request_server_addr_get_ip(lua_State *L)
inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)server_ip)->sin6_addr, sip, sizeof(sip));
}

lua_pushstring(L, sip);
if (sip[0] == '\0') {
lua_pushnil(L);
} else {
lua_pushstring(L, sip);
}
}

return 1;
Expand All @@ -779,7 +786,7 @@ ts_lua_server_request_server_addr_get_port(lua_State *L)
{
struct sockaddr const *server_ip;
ts_lua_http_ctx *http_ctx;
int port;
int port = 0;

GET_HTTP_CONTEXT(http_ctx, L);

Expand All @@ -791,7 +798,7 @@ ts_lua_server_request_server_addr_get_port(lua_State *L)
} else {
if (server_ip->sa_family == AF_INET) {
port = ((struct sockaddr_in *)server_ip)->sin_port;
} else {
} else if (server_ip->sa_family == AF_INET6) {
port = ((struct sockaddr_in6 *)server_ip)->sin6_port;
}

Expand All @@ -806,7 +813,7 @@ ts_lua_server_request_server_addr_get_outgoing_port(lua_State *L)
{
struct sockaddr const *outgoing_addr;
ts_lua_http_ctx *http_ctx;
int port;
int port = 0;

GET_HTTP_CONTEXT(http_ctx, L);

Expand All @@ -818,7 +825,7 @@ ts_lua_server_request_server_addr_get_outgoing_port(lua_State *L)
} else {
if (outgoing_addr->sa_family == AF_INET) {
port = ((struct sockaddr_in *)outgoing_addr)->sin_port;
} else {
} else if (outgoing_addr->sa_family == AF_INET6) {
port = ((struct sockaddr_in6 *)outgoing_addr)->sin6_port;
}

Expand Down Expand Up @@ -855,6 +862,8 @@ ts_lua_server_request_server_addr_get_addr(lua_State *L)
port = ntohs(((struct sockaddr_in6 *)server_ip)->sin6_port);
inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)server_ip)->sin6_addr, sip, sizeof(sip));
family = AF_INET6;
} else if (server_ip->sa_family == AF_UNIX) {
family = AF_UNIX;
}

lua_pushstring(L, sip);
Expand Down Expand Up @@ -892,6 +901,8 @@ ts_lua_server_request_server_addr_get_nexthop_addr(lua_State *L)
port = ntohs(((struct sockaddr_in6 *)server_ip)->sin6_port);
inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)server_ip)->sin6_addr, sip, sizeof(sip));
family = AF_INET6;
} else if (server_ip->sa_family == AF_UNIX) {
family = AF_UNIX;
}

lua_pushstring(L, sip);
Expand Down Expand Up @@ -963,12 +974,14 @@ ts_lua_server_request_server_addr_set_addr(lua_State *L)
if (!inet_pton(family, sip, &addr.sin4.sin_addr)) {
return luaL_error(L, "invalid ipv4 address");
}
} else {
} else if (family == AF_INET6) {
addr.sin6.sin6_family = AF_INET6;
addr.sin6.sin6_port = htons(port);
if (!inet_pton(family, sip, &addr.sin6.sin6_addr)) {
return luaL_error(L, "invalid ipv6 address");
}
} else {
return luaL_error(L, "invalid address family");
}

TSHttpTxnServerAddrSet(http_ctx->txnp, &addr.sa);
Expand Down Expand Up @@ -1009,12 +1022,14 @@ ts_lua_server_request_server_addr_set_outgoing_addr(lua_State *L)
if (!inet_pton(family, sip, &addr.sin4.sin_addr)) {
return luaL_error(L, "invalid ipv4 address");
}
} else {
} else if (family == AF_INET6) {
addr.sin6.sin6_family = AF_INET6;
addr.sin6.sin6_port = htons(port);
if (!inet_pton(family, sip, &addr.sin6.sin6_addr)) {
return luaL_error(L, "invalid ipv6 address");
}
} else {
return luaL_error(L, "invalid address family");
}

TSHttpTxnOutgoingAddrSet(http_ctx->txnp, &addr.sa);
Expand Down
10 changes: 6 additions & 4 deletions plugins/lua/ts_lua_vconn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ static int
ts_lua_vconn_get_remote_addr(lua_State *L)
{
ts_lua_vconn_ctx *vconn_ctx;
int port;
int family;
char sip[128];
int port = 0;
int family = AF_UNSPEC;
char sip[128] = "";

GET_VCONN_CONTEXT(vconn_ctx, L);

Expand All @@ -73,10 +73,12 @@ ts_lua_vconn_get_remote_addr(lua_State *L)
port = ntohs(((struct sockaddr_in *)addr)->sin_port);
inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)addr)->sin_addr, sip, sizeof(sip));
family = AF_INET;
} else {
} else if (addr->sa_family == AF_INET6) {
port = ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)addr)->sin6_addr, sip, sizeof(sip));
family = AF_INET6;
} else if (addr->sa_family == AF_UNIX) {
family = AF_UNIX;
}

lua_pushstring(L, sip);
Expand Down