You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by ki...@apache.org on 2018/04/17 02:52:38 UTC

[trafficserver] branch master updated: add support for TSHttpTxnOutgoingAddrSet in ts_lua

This is an automated email from the ASF dual-hosted git repository.

kichan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 29d4364  add support for TSHttpTxnOutgoingAddrSet in ts_lua
29d4364 is described below

commit 29d43648fe26b902dd0d921ba58b3471d61f5920
Author: Kit Chan <ki...@apache.org>
AuthorDate: Sun Apr 15 21:03:34 2018 -0700

    add support for TSHttpTxnOutgoingAddrSet in ts_lua
---
 doc/admin-guide/plugins/ts_lua.en.rst              | 20 +++++++++
 .../experimental/ts_lua/ts_lua_server_request.c    | 50 ++++++++++++++++++++++
 2 files changed, 70 insertions(+)

diff --git a/doc/admin-guide/plugins/ts_lua.en.rst b/doc/admin-guide/plugins/ts_lua.en.rst
index cf8dff6..9f0a08b 100644
--- a/doc/admin-guide/plugins/ts_lua.en.rst
+++ b/doc/admin-guide/plugins/ts_lua.en.rst
@@ -1602,6 +1602,26 @@ Here is an example:
 
 `TOP <#ts-lua-plugin>`_
 
+ts.server_request.server_addr.set_outgoing_addr
+-----------------------------------------------
+**syntax:** *ts.server_request.server_addr.set_outgoing_addr()*
+
+**context:** earlier than or inside function @ TS_LUA_HOOK_SEND_REQUEST_HDR hook point
+
+**description**: This function can be used to set outgoing socket address for the request to origin.
+
+The ts.server_request.server_addr.set_outgoing_addr function requires three inputs, ip is a string, port and family is number.
+
+Here is an example:
+
+::
+
+    function do_global_send_request()
+        ts.server_request.server_addr.set_outgoing_addr("192.168.231.17", 80, TS_LUA_AF_INET)
+    end
+
+`TOP <#ts-lua-plugin>`_
+
 ts.server_request.get_url_host
 ------------------------------
 **syntax:** *host = ts.server_request.get_url_host()*
diff --git a/plugins/experimental/ts_lua/ts_lua_server_request.c b/plugins/experimental/ts_lua/ts_lua_server_request.c
index 67802d4..9cccade 100644
--- a/plugins/experimental/ts_lua/ts_lua_server_request.c
+++ b/plugins/experimental/ts_lua/ts_lua_server_request.c
@@ -73,6 +73,7 @@ static int ts_lua_server_request_server_addr_get_port(lua_State *L);
 static int ts_lua_server_request_server_addr_get_addr(lua_State *L);
 static int ts_lua_server_request_server_addr_set_addr(lua_State *L);
 static int ts_lua_server_request_server_addr_get_outgoing_port(lua_State *L);
+static int ts_lua_server_request_server_addr_set_outgoing_addr(lua_State *L);
 
 void
 ts_lua_inject_server_request_api(lua_State *L)
@@ -120,6 +121,9 @@ ts_lua_inject_server_request_server_addr_api(lua_State *L)
   lua_pushcfunction(L, ts_lua_server_request_server_addr_get_outgoing_port);
   lua_setfield(L, -2, "get_outgoing_port");
 
+  lua_pushcfunction(L, ts_lua_server_request_server_addr_set_outgoing_addr);
+  lua_setfield(L, -2, "set_outgoing_addr");
+
   lua_setfield(L, -2, "server_addr");
 
   lua_pushinteger(L, AF_INET);
@@ -810,3 +814,49 @@ ts_lua_server_request_server_addr_set_addr(lua_State *L)
 
   return 0;
 }
+
+static int
+ts_lua_server_request_server_addr_set_outgoing_addr(lua_State *L)
+{
+  union {
+    struct sockaddr_in sin4;
+    struct sockaddr_in6 sin6;
+    struct sockaddr sa;
+  } addr;
+  ts_lua_http_ctx *http_ctx;
+  int n;
+  int port;
+  int family;
+  const char *sip;
+  size_t sip_len;
+
+  GET_HTTP_CONTEXT(http_ctx, L);
+
+  n = lua_gettop(L);
+
+  if (n == 3) {
+    sip    = luaL_checklstring(L, 1, &sip_len);
+    port   = luaL_checknumber(L, 2);
+    family = luaL_checknumber(L, 3);
+
+    if (family == AF_INET) {
+      addr.sin4.sin_family = AF_INET;
+      addr.sin4.sin_port   = htons(port);
+      if (!inet_pton(family, sip, &addr.sin4.sin_addr)) {
+        return luaL_error(L, "invalid ipv4 address");
+      }
+    } else {
+      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");
+      }
+    }
+
+    TSHttpTxnOutgoingAddrSet(http_ctx->txnp, &addr.sa);
+  } else {
+    return luaL_error(L, "incorrect # of arguments to ts.server_request.addr.set_outgoing_addr, receiving %d instead of 3", n);
+  }
+
+  return 0;
+}

-- 
To stop receiving notification emails like this one, please contact
kichan@apache.org.