Skip to content

Commit c69a5e1

Browse files
authored
lua: minimal support for Unix socket incoming connections (#12510)
* lua: minimal support for Unix socket incoming connections. * lua: check address family value for server_request calls.
1 parent c63e168 commit c69a5e1

File tree

5 files changed

+37
-15
lines changed

5 files changed

+37
-15
lines changed

doc/admin-guide/plugins/lua.en.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,6 +1838,7 @@ Socket address family
18381838

18391839
TS_LUA_AF_INET (2)
18401840
TS_LUA_AF_INET6 (10)
1841+
TS_LUA_AF_UNIX (1)
18411842

18421843

18431844
:ref:`TOP <admin-plugins-ts-lua>`

plugins/lua/ts_lua_client_request.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ ts_lua_client_request_client_addr_get_port(lua_State *L)
790790
{
791791
struct sockaddr const *client_ip;
792792
ts_lua_http_ctx *http_ctx;
793-
int port;
793+
int port = 0;
794794

795795
GET_HTTP_CONTEXT(http_ctx, L);
796796

@@ -802,7 +802,7 @@ ts_lua_client_request_client_addr_get_port(lua_State *L)
802802
} else {
803803
if (client_ip->sa_family == AF_INET) {
804804
port = ((struct sockaddr_in *)client_ip)->sin_port;
805-
} else {
805+
} else if (client_ip->sa_family == AF_INET6) {
806806
port = ((struct sockaddr_in6 *)client_ip)->sin6_port;
807807
}
808808

@@ -817,7 +817,7 @@ ts_lua_client_request_client_addr_get_incoming_port(lua_State *L)
817817
{
818818
struct sockaddr const *incoming_addr;
819819
ts_lua_http_ctx *http_ctx;
820-
int port;
820+
int port = 0;
821821

822822
GET_HTTP_CONTEXT(http_ctx, L);
823823

@@ -829,7 +829,7 @@ ts_lua_client_request_client_addr_get_incoming_port(lua_State *L)
829829
} else {
830830
if (incoming_addr->sa_family == AF_INET) {
831831
port = ((struct sockaddr_in *)incoming_addr)->sin_port;
832-
} else {
832+
} else if (incoming_addr->sa_family == AF_INET6) {
833833
port = ((struct sockaddr_in6 *)incoming_addr)->sin6_port;
834834
}
835835

@@ -866,6 +866,8 @@ ts_lua_client_request_client_addr_get_addr(lua_State *L)
866866
port = ntohs(((struct sockaddr_in6 *)client_ip)->sin6_port);
867867
inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)client_ip)->sin6_addr, cip, sizeof(cip));
868868
family = AF_INET6;
869+
} else if (client_ip->sa_family == AF_UNIX) {
870+
family = AF_UNIX;
869871
}
870872

871873
lua_pushstring(L, cip);

plugins/lua/ts_lua_http.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,8 @@ ts_lua_http_get_ssn_remote_addr(lua_State *L)
11511151
port = ntohs(((struct sockaddr_in6 *)client_ip)->sin6_port);
11521152
inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)client_ip)->sin6_addr, cip, sizeof(cip));
11531153
family = AF_INET6;
1154+
} else if (client_ip->sa_family == AF_UNIX) {
1155+
family = AF_UNIX;
11541156
}
11551157

11561158
lua_pushstring(L, cip);

plugins/lua/ts_lua_server_request.cc

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ ts_lua_inject_server_request_server_addr_api(lua_State *L)
149149

150150
lua_pushinteger(L, AF_INET6);
151151
lua_setglobal(L, "TS_LUA_AF_INET6");
152+
153+
lua_pushinteger(L, AF_UNIX);
154+
lua_setglobal(L, "TS_LUA_AF_UNIX");
152155
}
153156

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

771-
lua_pushstring(L, sip);
774+
if (sip[0] == '\0') {
775+
lua_pushnil(L);
776+
} else {
777+
lua_pushstring(L, sip);
778+
}
772779
}
773780

774781
return 1;
@@ -779,7 +786,7 @@ ts_lua_server_request_server_addr_get_port(lua_State *L)
779786
{
780787
struct sockaddr const *server_ip;
781788
ts_lua_http_ctx *http_ctx;
782-
int port;
789+
int port = 0;
783790

784791
GET_HTTP_CONTEXT(http_ctx, L);
785792

@@ -791,7 +798,7 @@ ts_lua_server_request_server_addr_get_port(lua_State *L)
791798
} else {
792799
if (server_ip->sa_family == AF_INET) {
793800
port = ((struct sockaddr_in *)server_ip)->sin_port;
794-
} else {
801+
} else if (server_ip->sa_family == AF_INET6) {
795802
port = ((struct sockaddr_in6 *)server_ip)->sin6_port;
796803
}
797804

@@ -806,7 +813,7 @@ ts_lua_server_request_server_addr_get_outgoing_port(lua_State *L)
806813
{
807814
struct sockaddr const *outgoing_addr;
808815
ts_lua_http_ctx *http_ctx;
809-
int port;
816+
int port = 0;
810817

811818
GET_HTTP_CONTEXT(http_ctx, L);
812819

@@ -818,7 +825,7 @@ ts_lua_server_request_server_addr_get_outgoing_port(lua_State *L)
818825
} else {
819826
if (outgoing_addr->sa_family == AF_INET) {
820827
port = ((struct sockaddr_in *)outgoing_addr)->sin_port;
821-
} else {
828+
} else if (outgoing_addr->sa_family == AF_INET6) {
822829
port = ((struct sockaddr_in6 *)outgoing_addr)->sin6_port;
823830
}
824831

@@ -855,6 +862,8 @@ ts_lua_server_request_server_addr_get_addr(lua_State *L)
855862
port = ntohs(((struct sockaddr_in6 *)server_ip)->sin6_port);
856863
inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)server_ip)->sin6_addr, sip, sizeof(sip));
857864
family = AF_INET6;
865+
} else if (server_ip->sa_family == AF_UNIX) {
866+
family = AF_UNIX;
858867
}
859868

860869
lua_pushstring(L, sip);
@@ -892,6 +901,8 @@ ts_lua_server_request_server_addr_get_nexthop_addr(lua_State *L)
892901
port = ntohs(((struct sockaddr_in6 *)server_ip)->sin6_port);
893902
inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)server_ip)->sin6_addr, sip, sizeof(sip));
894903
family = AF_INET6;
904+
} else if (server_ip->sa_family == AF_UNIX) {
905+
family = AF_UNIX;
895906
}
896907

897908
lua_pushstring(L, sip);
@@ -963,12 +974,14 @@ ts_lua_server_request_server_addr_set_addr(lua_State *L)
963974
if (!inet_pton(family, sip, &addr.sin4.sin_addr)) {
964975
return luaL_error(L, "invalid ipv4 address");
965976
}
966-
} else {
977+
} else if (family == AF_INET6) {
967978
addr.sin6.sin6_family = AF_INET6;
968979
addr.sin6.sin6_port = htons(port);
969980
if (!inet_pton(family, sip, &addr.sin6.sin6_addr)) {
970981
return luaL_error(L, "invalid ipv6 address");
971982
}
983+
} else {
984+
return luaL_error(L, "invalid address family");
972985
}
973986

974987
TSHttpTxnServerAddrSet(http_ctx->txnp, &addr.sa);
@@ -1009,12 +1022,14 @@ ts_lua_server_request_server_addr_set_outgoing_addr(lua_State *L)
10091022
if (!inet_pton(family, sip, &addr.sin4.sin_addr)) {
10101023
return luaL_error(L, "invalid ipv4 address");
10111024
}
1012-
} else {
1025+
} else if (family == AF_INET6) {
10131026
addr.sin6.sin6_family = AF_INET6;
10141027
addr.sin6.sin6_port = htons(port);
10151028
if (!inet_pton(family, sip, &addr.sin6.sin6_addr)) {
10161029
return luaL_error(L, "invalid ipv6 address");
10171030
}
1031+
} else {
1032+
return luaL_error(L, "invalid address family");
10181033
}
10191034

10201035
TSHttpTxnOutgoingAddrSet(http_ctx->txnp, &addr.sa);

plugins/lua/ts_lua_vconn.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ static int
5656
ts_lua_vconn_get_remote_addr(lua_State *L)
5757
{
5858
ts_lua_vconn_ctx *vconn_ctx;
59-
int port;
60-
int family;
61-
char sip[128];
59+
int port = 0;
60+
int family = AF_UNSPEC;
61+
char sip[128] = "";
6262

6363
GET_VCONN_CONTEXT(vconn_ctx, L);
6464

@@ -73,10 +73,12 @@ ts_lua_vconn_get_remote_addr(lua_State *L)
7373
port = ntohs(((struct sockaddr_in *)addr)->sin_port);
7474
inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)addr)->sin_addr, sip, sizeof(sip));
7575
family = AF_INET;
76-
} else {
76+
} else if (addr->sa_family == AF_INET6) {
7777
port = ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
7878
inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)addr)->sin6_addr, sip, sizeof(sip));
7979
family = AF_INET6;
80+
} else if (addr->sa_family == AF_UNIX) {
81+
family = AF_UNIX;
8082
}
8183

8284
lua_pushstring(L, sip);

0 commit comments

Comments
 (0)