/* // Copyright (c) 2010-2017 Intel Corporation // // Licensed 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 #include #include "prox_lua.h" #include "lua_compat.h" #include "parse_utils.h" static struct lua_State *lua_instance; static int l_mask(lua_State *L) { uint32_t val, mask; if (lua_gettop(L) != 2) { return luaL_error(L, "Expecting 2 argument and got %d\n", lua_gettop(L)); } if (!lua_isnumber(L, -1) || !lua_isnumber(L, -2)) { return luaL_error(L, "Expecting (integer, integer) as arguments\n"); } val = lua_tonumber(L, -1); mask = lua_tonumber(L, -2); lua_pushinteger(L, val & mask); return 1; } static int l_server_content(lua_State *L) { uint32_t beg, len; if (lua_gettop(L) != 2) { return luaL_error(L, "Expecting 2 argument and got %d\n", lua_gettop(L)); } if (!lua_isnumber(L, -1) || !lua_isnumber(L, -2)) { return luaL_error(L, "Expecting (integer, integer) as arguments\n"); } len = lua_tonumber(L, -1); beg = lua_tonumber(L, -2); lua_createtable(L, 0, 3); lua_pushinteger(L, beg); lua_setfield(L, -2, "beg"); lua_pushinteger(L, len); lua_setfield(L, -2, "len"); lua_pushinteger(L, 0); lua_setfield(L, -2, "peer"); return 1; } static int l_client_content(lua_State *L) { uint32_t beg, len; if (lua_gettop(L) != 2) { return luaL_error(L, "Expecting 2 argument and got %d\n", lua_gettop(L)); } if (!lua_isnumber(L, -1) || !lua_isnumber(L, -2)) { return luaL_error(L, "Expecting (integer, integer) as arguments\n"); } len = lua_tonumber(L, -1); beg = lua_tonumber(L, -2); lua_createtable(L, 0, 3); lua_pushinteger(L, beg); lua_setfield(L, -2, "beg"); lua_pushinteger(L, len); lua_setfield(L, -2, "len"); lua_pushinteger(L, 1); lua_setfield(L, -2, "peer"); return 1; } static int l_bin_read(lua_State *L) { const char *file_name = lua_tostring(L, -1); int beg = lua_tonumber(L, -2); int len = lua_gettop(L) == 3? lua_tonumber(L, -3) : -1; if (lua_gettop(L) == 2) { if (!lua_isnumber(L, -1) || !lua_isstring(L, -2)) { return luaL_error(L, "Expecting (string, integer) as arguments\n"); } file_name = lua_tostring(L, -2); beg = lua_tonumber(L, -1); len = -1; } else if (lua_gettop(L) == 3) { if (!lua_isnumber(L, -1) || !lua_isnumber(L, -2) || !lua_isstring(L, 3)) { return luaL_error(L, "Expecting (string, integer, integer) as arguments\n"); } file_name = lua_tostring(L, -3); beg = lua_tonumber(L, -2); len = lua_tonumber(L, -1); } else return luaL_error(L, "Expecting 2 or 3 arguments\n"); lua_createtable(L, 0, 3); lua_pushstring(L, file_name); lua_setfield(L, -2, "file_name"); lua_pushinteger(L, beg); lua_setfield(L, -2, "beg"); lua_pushinteger(L, len); lua_setfield(L, -2, "len"); return 1; } static int l_mac(lua_State *L) { int mac[6]; if (lua_isstring(L, -1)) { const char *arg = lua_tostring(L, -1); char arg2[128]; strncpy(arg2, arg, sizeof(arg2)); char *p = arg2; int count = 0; while ((p = strchr(p, ':'))) { count++; p++; } p = arg2; if (count != 5) return luaL_error(L, "Invalid MAC format\n"); lua_createtable(L, 6, 0); for (size_t i = 0; i < 6; ++i) { char *n = strchr(p, ':'); if (n) *n = 0; if (strlen(p) != 2) { return luaL_error(L, "Invalid MAC format\n"); } lua_pushinteger(L, strtol(p, NULL, 16)); lua_rawseti(L, -2, i + 1); p = n + 1; } return 1; } return luaL_error(L, "Invalid argument\n"); } static int l_ip(lua_State *L) { int ip[4]; if (lua_isnumber(L, -1)) { uint32_t arg = lua_tointeger(L, -1); ip[0] = arg >> 24 & 0xff; ip[1] = arg >> 16 & 0xff; ip[2] = arg >> 8 & 0xff; ip[3] = arg >> 0 & 0xff; lua_createtable(L, 4, 0); for (size_t i = 0; i < 4; ++i) { lua_pushinteger(L, ip[i]); lua_rawseti(L, -2, i + 1); } return 1; } if (lua_isstring(L, -1)) { const char *arg = lua_tostring(L, -1); if (sscanf(arg, "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]) != 4) { return luaL_error(L, "Invalid IP address format\n"); } lua_createtable(L, 4, 0); for (size_t i = 0; i < 4; ++i) {