Copilot commented on code in PR #12650:
URL: https://github.com/apache/trafficserver/pull/12650#discussion_r2507609869


##########
plugins/lua/ts_lua_client_request.cc:
##########
@@ -1139,3 +1147,83 @@ ts_lua_client_request_get_ssl_curve(lua_State *L)
 
   return 1;
 }
+
+static int
+ts_lua_client_request_client_addr_get_verified_addr(lua_State *L)
+{
+  struct sockaddr const *verified_addr;
+  ts_lua_http_ctx       *http_ctx;
+  int                    family   = AF_UNSPEC;
+  char                   vip[128] = "";
+
+  GET_HTTP_CONTEXT(http_ctx, L);
+
+  if (TSHttpTxnVerifiedAddrGet(http_ctx->txnp, &verified_addr) == TS_SUCCESS) {
+    if (verified_addr->sa_family == AF_INET) {
+      inet_ntop(AF_INET, (const void *)&((struct sockaddr_in 
*)verified_addr)->sin_addr, vip, sizeof(vip));
+      family = AF_INET;
+      lua_pushstring(L, vip);
+      lua_pushnumber(L, family);
+    } else if (verified_addr->sa_family == AF_INET6) {
+      inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 
*)verified_addr)->sin6_addr, vip, sizeof(vip));
+      family = AF_INET6;
+      lua_pushstring(L, vip);
+      lua_pushnumber(L, family);
+    } else {
+      lua_pushnil(L);
+      lua_pushnil(L);
+    }
+  } else {
+    lua_pushnil(L);
+    lua_pushnil(L);
+  }
+
+  return 2;
+}
+
+static int
+ts_lua_client_request_client_addr_set_verified_addr(lua_State *L)
+{
+  union {
+    struct sockaddr_in  sin4;
+    struct sockaddr_in6 sin6;
+    struct sockaddr     sa;
+  } addr;
+  memset(&addr, 0, sizeof(addr));
+  ts_lua_http_ctx *http_ctx;
+  int              n;
+  int              family;
+  const char      *vip;
+  size_t           vip_len;
+
+  GET_HTTP_CONTEXT(http_ctx, L);
+
+  n = lua_gettop(L);
+
+  if (n == 2) {
+    vip    = luaL_checklstring(L, 1, &vip_len);
+    family = luaL_checknumber(L, 2);
+
+    if (family == AF_INET) {
+      addr.sin4.sin_family = AF_INET;
+      addr.sin4.sin_port   = 0;
+      if (!inet_pton(family, vip, &addr.sin4.sin_addr)) {
+        return luaL_error(L, "invalid ipv4 address");
+      }
+    } else if (family == AF_INET6) {
+      addr.sin6.sin6_family = AF_INET6;
+      addr.sin6.sin6_port   = 0;
+      if (!inet_pton(family, vip, &addr.sin6.sin6_addr)) {
+        return luaL_error(L, "invalid ipv6 address");
+      }
+    } else {
+      return luaL_error(L, "invalid address family");

Review Comment:
   Error messages should be capitalized for consistency with other error 
messages in the codebase. Change 'invalid ipv4 address' to 'Invalid IPv4 
address' and similarly update the IPv6 error message on line 1217 to 'Invalid 
IPv6 address'. Also update 'invalid address family' on line 1220 to 'Invalid 
address family'.
   ```suggestion
           return luaL_error(L, "Invalid IPv4 address");
         }
       } else if (family == AF_INET6) {
         addr.sin6.sin6_family = AF_INET6;
         addr.sin6.sin6_port   = 0;
         if (!inet_pton(family, vip, &addr.sin6.sin6_addr)) {
           return luaL_error(L, "Invalid IPv6 address");
         }
       } else {
         return luaL_error(L, "Invalid address family");
   ```



##########
plugins/lua/ts_lua_client_request.cc:
##########
@@ -1139,3 +1147,83 @@ ts_lua_client_request_get_ssl_curve(lua_State *L)
 
   return 1;
 }
+
+static int
+ts_lua_client_request_client_addr_get_verified_addr(lua_State *L)
+{
+  struct sockaddr const *verified_addr;
+  ts_lua_http_ctx       *http_ctx;
+  int                    family   = AF_UNSPEC;
+  char                   vip[128] = "";
+
+  GET_HTTP_CONTEXT(http_ctx, L);
+
+  if (TSHttpTxnVerifiedAddrGet(http_ctx->txnp, &verified_addr) == TS_SUCCESS) {
+    if (verified_addr->sa_family == AF_INET) {
+      inet_ntop(AF_INET, (const void *)&((struct sockaddr_in 
*)verified_addr)->sin_addr, vip, sizeof(vip));
+      family = AF_INET;
+      lua_pushstring(L, vip);
+      lua_pushnumber(L, family);
+    } else if (verified_addr->sa_family == AF_INET6) {
+      inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 
*)verified_addr)->sin6_addr, vip, sizeof(vip));
+      family = AF_INET6;
+      lua_pushstring(L, vip);
+      lua_pushnumber(L, family);
+    } else {
+      lua_pushnil(L);
+      lua_pushnil(L);
+    }
+  } else {
+    lua_pushnil(L);
+    lua_pushnil(L);
+  }
+
+  return 2;
+}
+
+static int
+ts_lua_client_request_client_addr_set_verified_addr(lua_State *L)
+{
+  union {
+    struct sockaddr_in  sin4;
+    struct sockaddr_in6 sin6;
+    struct sockaddr     sa;
+  } addr;
+  memset(&addr, 0, sizeof(addr));
+  ts_lua_http_ctx *http_ctx;
+  int              n;
+  int              family;
+  const char      *vip;
+  size_t           vip_len;
+
+  GET_HTTP_CONTEXT(http_ctx, L);
+
+  n = lua_gettop(L);
+
+  if (n == 2) {
+    vip    = luaL_checklstring(L, 1, &vip_len);
+    family = luaL_checknumber(L, 2);
+
+    if (family == AF_INET) {
+      addr.sin4.sin_family = AF_INET;
+      addr.sin4.sin_port   = 0;
+      if (!inet_pton(family, vip, &addr.sin4.sin_addr)) {
+        return luaL_error(L, "invalid ipv4 address");
+      }
+    } else if (family == AF_INET6) {
+      addr.sin6.sin6_family = AF_INET6;
+      addr.sin6.sin6_port   = 0;
+      if (!inet_pton(family, vip, &addr.sin6.sin6_addr)) {
+        return luaL_error(L, "invalid ipv6 address");
+      }
+    } else {
+      return luaL_error(L, "invalid address family");
+    }
+
+    TSHttpTxnVerifiedAddrSet(http_ctx->txnp, &addr.sa);
+  } else {
+    return luaL_error(L, "incorrect # of arguments to 
ts.client_request.client_addr.set_verified_addr, receiving %d instead of 2", n);

Review Comment:
   The error message uses '#' as an abbreviation for 'number'. For consistency 
with similar error messages in ts_lua_server_request.cc (lines 989, 1037), use 
the same format: 'incorrect # of arguments'.



##########
plugins/lua/ts_lua_client_request.cc:
##########
@@ -1139,3 +1147,83 @@ ts_lua_client_request_get_ssl_curve(lua_State *L)
 
   return 1;
 }
+
+static int
+ts_lua_client_request_client_addr_get_verified_addr(lua_State *L)
+{
+  struct sockaddr const *verified_addr;
+  ts_lua_http_ctx       *http_ctx;
+  int                    family   = AF_UNSPEC;
+  char                   vip[128] = "";
+
+  GET_HTTP_CONTEXT(http_ctx, L);
+
+  if (TSHttpTxnVerifiedAddrGet(http_ctx->txnp, &verified_addr) == TS_SUCCESS) {
+    if (verified_addr->sa_family == AF_INET) {
+      inet_ntop(AF_INET, (const void *)&((struct sockaddr_in 
*)verified_addr)->sin_addr, vip, sizeof(vip));
+      family = AF_INET;
+      lua_pushstring(L, vip);
+      lua_pushnumber(L, family);
+    } else if (verified_addr->sa_family == AF_INET6) {
+      inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 
*)verified_addr)->sin6_addr, vip, sizeof(vip));
+      family = AF_INET6;
+      lua_pushstring(L, vip);
+      lua_pushnumber(L, family);
+    } else {
+      lua_pushnil(L);
+      lua_pushnil(L);
+    }
+  } else {
+    lua_pushnil(L);
+    lua_pushnil(L);
+  }
+
+  return 2;
+}
+
+static int
+ts_lua_client_request_client_addr_set_verified_addr(lua_State *L)
+{
+  union {
+    struct sockaddr_in  sin4;
+    struct sockaddr_in6 sin6;
+    struct sockaddr     sa;
+  } addr;
+  memset(&addr, 0, sizeof(addr));

Review Comment:
   The `memset` call should be placed after the union declaration on line 1191, 
not on line 1192. In C++, it's conventional to declare variables at the point 
of first use and initialize immediately. Move the memset to immediately follow 
the closing brace of the union declaration.
   ```suggestion
     } addr; memset(&addr, 0, sizeof(addr));
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to