Esempio n. 1
0
/**
 * get a token from the tokens array
 *
 */
static int proxy_tokenize_get(lua_State *L) {
	GPtrArray *tokens = *(GPtrArray **)luaL_checkself(L); 
	int ndx = luaL_checkinteger(L, 2);
	sql_token *token;
	sql_token **token_p;

	if (tokens->len > G_MAXINT) {
		return 0;
	}

	/* lua uses 1 is starting index */
	if (ndx < 1 || ndx > (int)tokens->len) {
		return 0;
	}

	token = tokens->pdata[ndx - 1];
	if (NULL == token) {
		lua_pushnil(L);

		return 1;
	}

	token_p = lua_newuserdata(L, sizeof(token));                          /* (sp += 1) */
	*token_p = token;

	sql_tokenizer_lua_token_getmetatable(L);
	lua_setmetatable(L, -2);             /* tie the metatable to the udata   (sp -= 1) */

	return 1;
}
static int lua_mysqld_columns_length(lua_State *L) {
	network_mysqld_columns *cols = *(network_mysqld_columns **)luaL_checkself(L);

	lua_pushinteger(L, cols->len);

	return 1;
}
Esempio n. 3
0
static int proxy_resultset_fields_gc(lua_State *L) {
	GRef *ref = *(GRef **)luaL_checkself(L);

	g_ref_unref(ref);
    
	return 0;
}
Esempio n. 4
0
int proxy_item_exist(lua_State *L) {
       int exist = 0;
       gsize keysize = 0;
       chassis_plugin *p = srv->modules->pdata[1];/*proxy plugin*/
       chassis_plugin_config *config = p->config;
       GHashTable *pwd_table = config->pwd_table[config->pwdtable_index];
       GHashTable *ip_table = config->pwd_table[config->iptable_index];
	network_backends_t *bs = *(network_backends_t **)luaL_checkself(L);
	gchar *key = luaL_checklstring(L, 2, &keysize);
       if(strchr(key, '.') == NULL) {
              gchar **pwds = g_strsplit(key, ":", 2);
              if(pwds && pwds[0]) {
                     if(NULL == g_hash_table_lookup(pwd_table, pwds[0]))
                            exist = 0;
                     else
                            exist = 1;
              }
              g_strfreev(pwds);
       }else {
              guint sum = ipstr_to_value(key);
              if(g_hash_table_contains(ip_table, &sum) == FALSE)
                     exist = 0;
              else
                     exist = 1;
       }
       lua_pushinteger(L, exist);
       return 1;
}
Esempio n. 5
0
/**
 * get the info about a backend
 *
 * proxy.backend[0].
 *   connected_clients => clients using this backend
 *   address           => ip:port or unix-path of to the backend
 *   state             => int(BACKEND_STATE_UP|BACKEND_STATE_DOWN)
 *   type              => int(BACKEND_TYPE_RW|BACKEND_TYPE_RO)
 *
 * @return nil or requested information
 * @see backend_state_t backend_type_t
 */
static int proxy_backend_get(lua_State *L) {
	network_backend_t *backend = *(network_backend_t **)luaL_checkself(L);
	gsize keysize = 0;
	const char *key = luaL_checklstring(L, 2, &keysize);

	if (strleq(key, keysize, C("connected_clients"))) {
		lua_pushinteger(L, backend->connected_clients);
	} else if (strleq(key, keysize, C("dst"))) {
		network_address_lua_push(L, backend->addr);
	} else if (strleq(key, keysize, C("state"))) {
		lua_pushinteger(L, backend->state);
	} else if (strleq(key, keysize, C("type"))) {
		lua_pushinteger(L, backend->type);
	} else if (strleq(key, keysize, C("uuid"))) {
		if (backend->uuid->len) {
			lua_pushlstring(L, S(backend->uuid));
		} else {
			lua_pushnil(L);
		}
	} else if (strleq(key, keysize, C("weight"))) {
		lua_pushinteger(L, backend->weight);
	} else {
		lua_pushnil(L);
	}

	return 1;
}
Esempio n. 6
0
static int proxy_tokenize_len(lua_State *L) {
	GPtrArray *tokens = *(GPtrArray **)luaL_checkself(L); 

	lua_pushinteger(L, tokens->len);

	return 1;
}
Esempio n. 7
0
/**
 * get a field from the result-set
 *
 */
static int proxy_resultset_fields_get(lua_State *L) {
	GRef *ref = *(GRef **)luaL_checkself(L);
	proxy_resultset_t *res = ref->udata;
	GPtrArray *fields = res->fields;
	MYSQL_FIELD *field;
	MYSQL_FIELD **field_p;
	lua_Integer ndx = luaL_checkinteger(L, 2);

	/* protect the compare */
	if (fields->len > G_MAXINT) {
		return 0;
	}
    
	if (ndx < 1 || ndx > (lua_Integer)fields->len) {
		lua_pushnil(L);
        
		return 1;
	}
    
	field = fields->pdata[ndx - 1]; /** lua starts at 1, C at 0 */
    
	field_p = lua_newuserdata(L, sizeof(field));
	*field_p = field;
    
	proxy_getmetatable(L, methods_proxy_resultset_fields_field);
	lua_setmetatable(L, -2);
    
	return 1;
}
Esempio n. 8
0
static int proxy_tokenize_gc(lua_State *L) {
	GPtrArray *tokens = *(GPtrArray **)luaL_checkself(L); 

	sql_tokens_free(tokens);

	return 0;
}
Esempio n. 9
0
static int proxy_resultset_fields_len(lua_State *L) {
	GRef *ref = *(GRef **)luaL_checkself(L);
	proxy_resultset_t *res = ref->udata;
	GPtrArray *fields = res->fields;
    lua_pushinteger(L, fields->len);
    return 1;
}
static int lua_mysqld_column_get(lua_State *L) {
	network_mysqld_column *col = *(network_mysqld_column **)luaL_checkself(L);
	gsize keysize = 0;
	const char *key = luaL_checklstring(L, 2, &keysize);

	LUA_UDATA_EXPORT_CSTR(col, name);
	LUA_UDATA_EXPORT_CSTR(col, org_name);
	
	if (strleq(C("type"), key, keysize)) {
		lua_pushstring(L, network_mysqld_column_get_typestring(col));
		return 1;
	}

	if (strleq(C("is_nullable"), key, keysize)) {
		lua_pushboolean(L, (col->flags & NOT_NULL_FLAG) ? 0 : 1);
		return 1;
	}

	if (strleq(C("length"), key, keysize)) {
		lua_pushinteger(L, col->max_length);
		return 1;
	}

	return 0;
}
Esempio n. 11
0
static int proxy_backends_len(lua_State *L) {
	network_backends_t *bs = *(network_backends_t **)luaL_checkself(L);

	lua_pushinteger(L, network_backends_count(bs));

	return 1;
}
Esempio n. 12
0
static int proxy_queue_len(lua_State *L) {
	/* we expect 2 parameters */
	GQueue *q = *(GQueue **)luaL_checkself(L);
    
	lua_pushinteger(L, q->length);
    
	return 1;
}
Esempio n. 13
0
static int proxy_queue_reset(lua_State *L) {
	/* we expect 2 parameters */
	GQueue *q = *(GQueue **)luaL_checkself(L);

	network_injection_queue_reset(q);
    
	return 0;
}
static int lua_mysqld_table_set(lua_State *L) {
	network_mysqld_table *tbl = *(network_mysqld_table **)luaL_checkself(L);
	gsize keysize = 0;
	const char *key = luaL_checklstring(L, 2, &keysize);

	LUA_UDATA_IMPORT_STR(tbl, db_name);
	LUA_UDATA_IMPORT_STR(tbl, table_name);

	return 0;
}
static int lua_mysqld_columns_get(lua_State *L) {
	network_mysqld_columns *cols = *(network_mysqld_columns **)luaL_checkself(L);
	gint ndx = luaL_checkint(L, 2);

	if (ndx < 1 || ndx > cols->len) {
		/* out of range */
		return 0;
	}

	return lua_mysqld_column_push(L, cols->pdata[ndx - 1]);
}
Esempio n. 16
0
static int proxy_queue_add(lua_State *L, proxy_queue_add_t type) {
	GQueue *q = *(GQueue **)luaL_checkself(L);
	int resp_type = luaL_checkinteger(L, 2);
	size_t str_len;
	const char *str = luaL_checklstring(L, 3, &str_len);
	injection *inj;

	GString *query = g_string_sized_new(str_len);
	g_string_append_len(query, str, str_len);

	inj = injection_new(resp_type, query);
	inj->resultset_is_needed = FALSE;

	/* check the 4th (last) param */
	switch (luaL_opt(L, lua_istable, 4, -1)) {
	case -1:
		/* none or nil */
		break;
	case 1:
		lua_getfield(L, 4, "resultset_is_needed");
		if (lua_isnil(L, -1)) {
			/* no defined */
		} else if (lua_isboolean(L, -1)) {
			inj->resultset_is_needed = lua_toboolean(L, -1);
		} else {
			switch (type) {
			case PROXY_QUEUE_ADD_APPEND:
				return luaL_argerror(L, 4, ":append(..., { resultset_is_needed = boolean } ), is %s");
			case PROXY_QUEUE_ADD_PREPEND:
				return luaL_argerror(L, 4, ":prepend(..., { resultset_is_needed = boolean } ), is %s");
			}
		}

		lua_pop(L, 1);
		break;
	default:
		proxy_lua_dumpstack_verbose(L);
		luaL_typerror(L, 4, "table");
		break;
	}

	switch (type) {
	case PROXY_QUEUE_ADD_APPEND:
		network_injection_queue_append(q, inj);
		return 0;
	case PROXY_QUEUE_ADD_PREPEND:
		network_injection_queue_prepend(q, inj);
		return 0;
	}

	g_assert_not_reached();
}
static int lua_mysqld_table_get(lua_State *L) {
	network_mysqld_table *tbl = *(network_mysqld_table **)luaL_checkself(L);
	gsize keysize = 0;
	const char *key = luaL_checklstring(L, 2, &keysize);

	LUA_UDATA_EXPORT_STR(tbl, db_name);
	LUA_UDATA_EXPORT_STR(tbl, table_name);

	if (strleq(C("columns"), key, keysize)) {
		/* columns is a array */
		return lua_mysqld_columns_push(L, tbl->columns);
	}
	
	return 0;
}
Esempio n. 18
0
int proxy_clientip_get(lua_State *L) {
	GPtrArray *clientip_vec = *(GPtrArray **)luaL_checkself(L);
	int clientip_ndx = luaL_checkinteger(L, 2) - 1; /** lua is indexes from 1, C from 0 */

	if (clientip_ndx < 0 || clientip_ndx >= clientip_vec->len ) {
		lua_pushnil(L);
		return 1;
	} else {
              guint ip = *(guint*)clientip_vec->pdata[clientip_ndx];
              gchar *addr = ip_to_str(ip);
              lua_pushlstring(L, addr, strlen(addr));
              g_free(addr);
       }
	return 1;
}
Esempio n. 19
0
/**
 * set proxy.global.backends.addslave
 *
 * add slave server into mysql backends
 *
 * @return nil or the backend
 */
static int proxy_backends_set(lua_State *L) {
	network_backends_t *bs = *(network_backends_t **)luaL_checkself(L);
	gsize keysize = 0;
	const char *key = luaL_checklstring(L, 2, &keysize);

	if (strleq(key, keysize, C("addslave"))) {
        	network_backends_add(bs, g_strdup(lua_tostring(L, -1)), BACKEND_TYPE_RO);
	} else if (strleq(key, keysize, C("addmaster"))) {
        	network_backends_add(bs, g_strdup(lua_tostring(L, -1)), BACKEND_TYPE_RW);
	} else if (strleq(key, keysize, C("removebackend"))) {
        	network_backends_remove(bs, lua_tointeger(L, -1));
	} else {
		return luaL_error(L, "proxy.global.backends.%s is not writable", key);
	}
	return 1;
}
Esempio n. 20
0
int proxy_user_password_get(lua_State *L) {
	user_password *user_pwd = *(user_password **)luaL_checkself(L);
	gsize keysize = 0;
	const char *key = luaL_checklstring(L, 2, &keysize);

	if (strleq(key, keysize, C("user"))) {
              lua_pushlstring(L, user_pwd->user, strlen(user_pwd->user));
	} else if (strleq(key, keysize, C("pwd"))) {
                char* enpwds = pwds_encrypt(user_pwd->pwd);
                lua_pushlstring(L, enpwds, strlen(enpwds));
                g_free(enpwds);
	} else {
		lua_pushnil(L);
	}

	return 1;
}
Esempio n. 21
0
int proxy_user_vec_get(lua_State *L) {
       user_password *user_pwd;
       user_password **user_pwd_p;
	GPtrArray *user_vec = *(GPtrArray **)luaL_checkself(L);
	int user_ndx = luaL_checkinteger(L, 2) - 1; /** lua is indexes from 1, C from 0 */

	if (user_ndx < 0 || user_ndx >= user_vec->len ) {
		lua_pushnil(L);
		return 1;
	} else {
              user_pwd = user_vec->pdata[user_ndx];
       }
	user_pwd_p = lua_newuserdata(L, sizeof(user_password *)); /* the table underneath proxy.global.user_vec[ndx] */
	*user_pwd_p = user_pwd;

	network_user_password_lua_getmetatable(L);
	lua_setmetatable(L, -2);

	return 1;
}
Esempio n. 22
0
static int proxy_resultset_field_get(lua_State *L) {
	MYSQL_FIELD *field = *(MYSQL_FIELD **)luaL_checkself(L);
	gsize keysize = 0;
	const char *key = luaL_checklstring(L, 2, &keysize);
        
	if (strleq(key, keysize, C("type"))) {
		lua_pushinteger(L, field->type);
	} else if (strleq(key, keysize, C("name"))) {
		lua_pushstring(L, field->name);
	} else if (strleq(key, keysize, C("org_name"))) {
		lua_pushstring(L, field->org_name);
	} else if (strleq(key, keysize, C("org_table"))) {
		lua_pushstring(L, field->org_table);
	} else if (strleq(key, keysize, C("table"))) {
		lua_pushstring(L, field->table);
	} else {
		lua_pushnil(L);
	}
    
	return 1;
}
Esempio n. 23
0
static int proxy_injection_get(lua_State *L) {
	injection *inj = *(injection **)luaL_checkself(L);
	gsize keysize = 0;
	const char *key = luaL_checklstring(L, 2, &keysize);
    
	if (strleq(key, keysize, C("type"))) {
		lua_pushinteger(L, inj->id); /** DEPRECATED: use "inj.id" instead */
	} else if (strleq(key, keysize, C("id"))) {
		lua_pushinteger(L, inj->id);
	} else if (strleq(key, keysize, C("query"))) {
		lua_pushlstring(L, inj->query->str, inj->query->len);
	} else if (strleq(key, keysize, C("query_time"))) {
		lua_pushinteger(L, chassis_calc_rel_microseconds(inj->ts_read_query, inj->ts_read_query_result_first));
	} else if (strleq(key, keysize, C("response_time"))) {
		lua_pushinteger(L, chassis_calc_rel_microseconds(inj->ts_read_query, inj->ts_read_query_result_last));
	} else if (strleq(key, keysize, C("resultset"))) {
		/* fields, rows */
		proxy_resultset_t *res;
        
		res = proxy_resultset_new();

		/* only expose the resultset if really needed 
		   FIXME: if the resultset is encoded in binary form, we can't provide it either.
		 */
		if (inj->resultset_is_needed && !inj->qstat.binary_encoded) {	
			res->result_queue = inj->result_queue;
		}
		res->qstat = inj->qstat;
		res->rows  = inj->rows;
		res->bytes = inj->bytes;
	
		proxy_resultset_lua_push(L, res);
	} else {
		g_message("%s.%d: inj[%s] ... not found", __FILE__, __LINE__, key);
        
		lua_pushnil(L);
	}
    
	return 1;
}
Esempio n. 24
0
static int proxy_tokenize_token_get(lua_State *L) {
	sql_token *token = *(sql_token **)luaL_checkself(L); 
	size_t keysize;
	const char *key = luaL_checklstring(L, 2, &keysize);

	if (strleq(key, keysize, C("text"))) {
		lua_pushlstring(L, S(token->text));
		return 1;
	} else if (strleq(key, keysize, C("token_id"))) {
		lua_pushinteger(L, token->token_id);
		return 1;
	} else if (strleq(key, keysize, C("token_name"))) {
		size_t token_name_len;
		const char *token_name = sql_token_get_name(token->token_id, &token_name_len);
		lua_pushlstring(L, token_name, token_name_len);
		return 1;
	} else {
		luaL_error(L, "tokens[...] has no %s field", key);
	}

	return 0;
}
Esempio n. 25
0
/**
 * get proxy.global.backends[ndx]
 *
 * get the backend from the array of mysql backends.
 *
 * @return nil or the backend
 * @see proxy_backend_get
 */
static int proxy_backends_get(lua_State *L) {
	network_backend_t *backend;
	network_backend_t **backend_p;

	network_backends_t *bs = *(network_backends_t **)luaL_checkself(L);
	int backend_ndx = luaL_checkinteger(L, 2) - 1; /** lua is indexes from 1, C from 0 */

	/* check that we are in range for a _int_ */
	if (NULL == (backend = network_backends_get(bs, backend_ndx))) {
		lua_pushnil(L);

		return 1;
	}

	backend_p = lua_newuserdata(L, sizeof(backend)); /* the table underneath proxy.global.backends[ndx] */
	*backend_p = backend;

	network_backend_lua_getmetatable(L);
	lua_setmetatable(L, -2);

	return 1;
}
Esempio n. 26
0
static int proxy_backend_set(lua_State *L) {
	network_backend_t *backend = *(network_backend_t **)luaL_checkself(L);
	gsize keysize = 0;
	const char *key = luaL_checklstring(L, 2, &keysize);

	if (strleq(key, keysize, C("state"))) {
		backend->state = lua_tointeger(L, -1);
	} else if (strleq(key, keysize, C("uuid"))) {
		if (lua_isstring(L, -1)) {
			size_t s_len = 0;
			const char *s = lua_tolstring(L, -1, &s_len);

			g_string_assign_len(backend->uuid, s, s_len);
		} else if (lua_isnil(L, -1)) {
			g_string_truncate(backend->uuid, 0);
		} else {
			return luaL_error(L, "proxy.global.backends[...].%s has to be a string", key);
		}
	} else {
		return luaL_error(L, "proxy.global.backends[...].%s is not writable", key);
	}
	return 1;
}
Esempio n. 27
0
static int proxy_backends_set(lua_State *L) {
	network_backends_t *bs = *(network_backends_t **)luaL_checkself(L);
	gsize keysize = 0;
	const char *key = luaL_checklstring(L, 2, &keysize);

	if (strleq(key, keysize, C("addslave"))) {
        	network_backends_add(bs, g_strdup(lua_tostring(L, -1)), BACKEND_TYPE_RO);
	} else if (strleq(key, keysize, C("addmaster"))) {
        	network_backends_add(bs, g_strdup(lua_tostring(L, -1)), BACKEND_TYPE_RW);
       } else if (strleq(key, keysize, C("changemaster"))) {
              //change_standby_to_master(bs);
              GThread* t = g_thread_new("change_master_thread", change_master_thread_func, srv);
//              g_thread_join(t);
	} else if (strleq(key, keysize, C("addstandby"))) {
        	network_backends_add(bs, g_strdup(lua_tostring(L, -1)), BACKEND_TYPE_SY);
	} else if (strleq(key, keysize, C("removebackend"))) {
        	network_backends_remove(bs, lua_tointeger(L, -1));
	} else if (strleq(key, keysize, C("addpwds"))) {
              gchar* pwds = g_strdup(lua_tostring(L, -1));
        	network_backends_add_pwds(srv, pwds);
              g_free(pwds);
	} else if (strleq(key, keysize, C("addenpwds"))) {
                gchar* enpwds = convert_pwds(lua_tostring(L, -1));
                network_backends_add_pwds(srv, enpwds);
                g_free(enpwds);
	} else if (strleq(key, keysize, C("removepwds"))) {
              gchar* users = g_strdup(lua_tostring(L, -1));
        	network_backends_remove_pwds(srv, users);
              g_free(users);
	} else if (strleq(key, keysize, C("saveconfig"))) {
                network_save_config(srv);
       } else {
		return luaL_error(L, "proxy.global.backends.%s is not writable", key);
	}
	return 1;
}
Esempio n. 28
0
/**
 * a settor for the tokens
 *
 * only allow to unset a token in the tokens array to free its memory
 */
static int proxy_tokenize_set(lua_State *L) {
	GPtrArray *tokens = *(GPtrArray **)luaL_checkself(L); 
	int ndx = luaL_checkinteger(L, 2);
	sql_token *token;

	luaL_checktype(L, 3, LUA_TNIL); /* for now we can only use = nil */

	if (tokens->len > G_MAXINT) {
		return 0;
	}

	/* lua uses 1 is starting index */
	if (ndx < 1 || ndx > (int)tokens->len) {
		return 0;
	}

	token = tokens->pdata[ndx - 1];
	if (NULL != token) {
		sql_token_free(token);
		tokens->pdata[ndx - 1] = NULL;
	}

	return 0;
}
Esempio n. 29
0
int proxy_clientip_vec_len(lua_State *L) {
       GPtrArray *clientip_vec = *(GPtrArray **)luaL_checkself(L);
	lua_pushinteger(L, clientip_vec->len);

	return 1;
}
Esempio n. 30
0
int proxy_user_vec_len(lua_State *L) {
       GPtrArray *user_vec = *(GPtrArray **)luaL_checkself(L);
	lua_pushinteger(L, user_vec->len);

	return 1;
}