diff --git a/doc/admin-guide/plugins/lua.en.rst b/doc/admin-guide/plugins/lua.en.rst index d065d3d1e33..c7982381cd3 100644 --- a/doc/admin-guide/plugins/lua.en.rst +++ b/doc/admin-guide/plugins/lua.en.rst @@ -1838,6 +1838,7 @@ Socket address family TS_LUA_AF_INET (2) TS_LUA_AF_INET6 (10) + TS_LUA_AF_UNIX (1) :ref:`TOP ` diff --git a/plugins/lua/ts_lua_client_request.cc b/plugins/lua/ts_lua_client_request.cc index ac2671cf10c..0fc9e534ba8 100644 --- a/plugins/lua/ts_lua_client_request.cc +++ b/plugins/lua/ts_lua_client_request.cc @@ -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); @@ -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; } @@ -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); @@ -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; } @@ -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); diff --git a/plugins/lua/ts_lua_http.cc b/plugins/lua/ts_lua_http.cc index 4326224f782..c59464cba68 100644 --- a/plugins/lua/ts_lua_http.cc +++ b/plugins/lua/ts_lua_http.cc @@ -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); diff --git a/plugins/lua/ts_lua_server_request.cc b/plugins/lua/ts_lua_server_request.cc index 26c28974360..913b7fba478 100644 --- a/plugins/lua/ts_lua_server_request.cc +++ b/plugins/lua/ts_lua_server_request.cc @@ -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"); } static void @@ -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; @@ -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); @@ -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; } @@ -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); @@ -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; } @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/plugins/lua/ts_lua_vconn.cc b/plugins/lua/ts_lua_vconn.cc index 0c7dd4d34f2..62781be4303 100644 --- a/plugins/lua/ts_lua_vconn.cc +++ b/plugins/lua/ts_lua_vconn.cc @@ -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); @@ -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);