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 2017/11/14 23:28:45 UTC
[trafficserver] 01/05: Add support for TSIpAddrParse as a
conveninent function to parse IP address
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
commit 98a2d2d21d62e9413186aa475248dc47d12e7c54
Author: Kit Chan <ki...@apache.org>
AuthorDate: Thu Sep 14 17:24:23 2017 -0700
Add support for TSIpAddrParse as a conveninent function to parse IP address
---
.../api/functions/TSIpAddrParse.en.rst | 51 ++++++++++++++++++++++
plugins/experimental/ts_lua/ts_lua_fetch.c | 40 +++++------------
proxy/InkAPI.cc | 13 ++++++
proxy/api/ts/experimental.h | 3 ++
4 files changed, 77 insertions(+), 30 deletions(-)
diff --git a/doc/developer-guide/api/functions/TSIpAddrParse.en.rst b/doc/developer-guide/api/functions/TSIpAddrParse.en.rst
new file mode 100644
index 0000000..0f7234c
--- /dev/null
+++ b/doc/developer-guide/api/functions/TSIpAddrParse.en.rst
@@ -0,0 +1,51 @@
+.. Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed
+ with this work for additional information regarding copyright
+ ownership. The ASF licenses this file to you under the Apache
+ License, Version 2.0 (the "License"); you may not use this file
+ except in compliance with the License. You may obtain a copy of
+ the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied. See the License for the specific language governing
+ permissions and limitations under the License.
+
+.. include:: ../../../common.defs
+
+.. default-domain:: c
+
+TSIpAddrParse
+*************
+
+Synopsis
+========
+
+`#include <ts/experimental.h>`
+
+.. function:: TSReturnCode TSIpAddrParse(const char * str, int str_len, sockaddr* addr)
+
+Description
+===========
+
+:arg:`str` is expected to be an explicit address, not a hostname. No hostname resolution is done. This attempts to recognize and process a port value if present. It is set appropriately, or to zero if no port was found or it was malformed.
+
+It is intended to deal with the brackets that can optionally surround an IP address (usually IPv6) which in turn are used to differentiate between an address and an attached port. E.g.
+
+.. code-block:: none
+
+[FE80:9312::192:168:1:1]:80
+
+Return values
+=============
+
+It returns :data:`TS_SUCCESS` on success, or :data:`TS_ERROR` on failure.
+
+Notes
+=====
+
+This API may be changed in the future version since it is experimental.
+
diff --git a/plugins/experimental/ts_lua/ts_lua_fetch.c b/plugins/experimental/ts_lua/ts_lua_fetch.c
index cf5d5e9..7b2d6a4 100644
--- a/plugins/experimental/ts_lua/ts_lua_fetch.c
+++ b/plugins/experimental/ts_lua/ts_lua_fetch.c
@@ -16,17 +16,13 @@
limitations under the License.
*/
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-
#include "ts_lua_util.h"
#include "ts_lua_io.h"
#include "ts_lua_fetch.h"
#define TS_LUA_EVENT_FETCH_OVER 20010
-#define TS_LUA_FETCH_CLIENT_ADDRESS "127.0.0.1"
-#define TS_LUA_FETCH_CLIENT_PORT 33333
+#define TS_LUA_FETCH_CLIENT_ADDRPORT "127.0.0.1:33333"
+#define TS_LUA_FETCH_CLIENT_ADDRPORT_LEN 15
#define TS_LUA_FETCH_USER_AGENT "TS Fetcher/1.0"
static int ts_lua_fetch(lua_State *L);
@@ -203,15 +199,14 @@ static int
ts_lua_fetch_one_item(lua_State *L, const char *url, size_t url_len, ts_lua_fetch_info *fi)
{
TSCont contp;
- int tb, flags, host_len, port, n;
+ int tb, flags, host_len, n;
int cl, ht, ua;
const char *method, *key, *value, *body, *opt;
const char *addr, *ptr, *host;
size_t method_len, key_len, value_len, body_len;
size_t addr_len, opt_len, i, left;
char c;
- struct sockaddr_in clientaddr;
- char ipstr[32];
+ struct sockaddr clientaddr;
char buf[32];
tb = lua_istable(L, -1);
@@ -252,8 +247,6 @@ ts_lua_fetch_one_item(lua_State *L, const char *url, size_t url_len, ts_lua_fetc
/* cliaddr */
memset(&clientaddr, 0, sizeof(clientaddr));
- clientaddr.sin_family = AF_INET;
- const char *p = NULL;
if (tb) {
lua_pushlstring(L, "cliaddr", sizeof("cliaddr") - 1);
@@ -261,32 +254,19 @@ ts_lua_fetch_one_item(lua_State *L, const char *url, size_t url_len, ts_lua_fetc
if (lua_isstring(L, -1)) {
addr = luaL_checklstring(L, -1, &addr_len);
- p = strstr(addr, ":");
- if (p && p - addr < 32) {
- port = atoi(p + 1);
- strncpy(ipstr, addr, p - addr);
- ipstr[p - addr] = 0;
- clientaddr.sin_port = htons(port);
- if (!inet_aton(ipstr, (struct in_addr *)&clientaddr.sin_addr.s_addr)) {
- p = NULL;
- TSError("[%s] Client ip parse failed! Using default. [ip: %s]", TS_LUA_DEBUG_TAG, ipstr);
+
+ if (TS_ERROR == TSIpAddrParse(addr, addr_len, &clientaddr)) {
+ TSError("[%s] Client ip parse failed! Using default.", TS_LUA_DEBUG_TAG);
+ if (TS_ERROR == TSIpAddrParse(TS_LUA_FETCH_CLIENT_ADDRPORT, TS_LUA_FETCH_CLIENT_ADDRPORT_LEN, &clientaddr)) {
+ TSError("[%s] Default client ip parse failed!", TS_LUA_DEBUG_TAG);
+ return 0;
}
- } else {
- p = NULL;
- TSError("[%s] Client ip parse failed! Using default. [addrstr: %s]", TS_LUA_DEBUG_TAG, addr);
}
}
lua_pop(L, 1);
}
- if (!p) {
- clientaddr.sin_port = htons(TS_LUA_FETCH_CLIENT_PORT);
- if (!inet_aton(TS_LUA_FETCH_CLIENT_ADDRESS, (struct in_addr *)&clientaddr.sin_addr.s_addr)) {
- TSError("[%s] Error using default for client ip in fetch API. [ip: %s]", TS_LUA_DEBUG_TAG, TS_LUA_FETCH_CLIENT_ADDRESS);
- }
- }
-
/* option */
flags = TS_FETCH_FLAGS_DECHUNK; // dechunk the body default
diff --git a/proxy/InkAPI.cc b/proxy/InkAPI.cc
index ead3577..b4b911c 100644
--- a/proxy/InkAPI.cc
+++ b/proxy/InkAPI.cc
@@ -2428,6 +2428,19 @@ TSUrlPercentEncode(TSMBuffer bufp, TSMLoc obj, char *dst, size_t dst_size, size_
return ret;
}
+// pton
+TSReturnCode
+TSIpAddrParse(const char *str, size_t str_len, sockaddr *addr)
+{
+ sdk_assert(sdk_sanity_check_null_ptr((void *)str) == TS_SUCCESS);
+
+ if (0 != ats_ip_pton(ts::ConstBuffer(str, str_len), addr)) {
+ return TS_ERROR;
+ }
+
+ return TS_SUCCESS;
+}
+
////////////////////////////////////////////////////////////////////
//
// MIME Headers
diff --git a/proxy/api/ts/experimental.h b/proxy/api/ts/experimental.h
index 5181d88..dd19cbf 100644
--- a/proxy/api/ts/experimental.h
+++ b/proxy/api/ts/experimental.h
@@ -231,6 +231,9 @@ tsapi int TSHttpTxnLookingUpTypeGet(TSHttpTxn txnp);
tsapi void TSHttpTxnServerPush(TSHttpTxn txnp, const char *url, int url_len);
+/* ip addr parsing */
+tsapi TSReturnCode TSIpAddrParse(const char *str, size_t str_len, struct sockaddr *addr);
+
/**
Attempt to attach the contp continuation to sockets that have already been
opened by the traffic manager and defined as belonging to plugins (based on
--
To stop receiving notification emails like this one, please contact
"commits@trafficserver.apache.org" <co...@trafficserver.apache.org>.