Exemplo n.º 1
0
void ubus_method_add_return(struct ubus_method *self, const char *name, const char *signature){
	blob_offset_t ofs = blob_open_array(&self->signature); 
		blob_put_int(&self->signature, UBUS_METHOD_PARAM_OUT); 
		blob_put_string(&self->signature, name); 
		blob_put_string(&self->signature, signature); 
	blob_close_array(&self->signature, ofs); 
}
Exemplo n.º 2
0
int orange_lua_table_to_blob(lua_State *L, struct blob *b, bool table){
	bool rv = true;

	if(lua_type(L, -1) != LUA_TTABLE) {
		DEBUG("%s: can only format a table (or array)\n", __FUNCTION__); 
		return false; 
	}

	lua_pushnil(L); 
	while(lua_next(L, -2)){
		lua_pushvalue(L, -2); 

		const char *key = table ? lua_tostring(L, -1) : NULL;

		if(key) blob_put_string(b, key); 

		switch (lua_type(L, -2)){
			case LUA_TBOOLEAN:
				blob_put_int(b, (uint8_t)lua_toboolean(L, -2));
				break;
		#ifdef LUA_TINT
			case LUA_TINT:
		#endif
			case LUA_TNUMBER:
				if(fabsf(lua_tointeger(L, -2) - lua_tonumber(L, -2)) < FLT_EPSILON)
					blob_put_int(b, lua_tointeger(L, -2)); 
				else 
					blob_put_real(b, lua_tonumber(L, -2));
				break;
			case LUA_TSTRING:
			case LUA_TUSERDATA:
			case LUA_TLIGHTUSERDATA:
				blob_put_string(b, lua_tostring(L, -2));
				break;
			case LUA_TTABLE:
				lua_pushvalue(L, -2); 
				if (_lua_format_blob_is_array(L)){
					blob_offset_t c = blob_open_array(b);
					rv = orange_lua_table_to_blob(L, b, false);
					blob_close_array(b, c);
				} else {
					blob_offset_t c = blob_open_table(b);
					rv = orange_lua_table_to_blob(L, b, true);
					blob_close_table(b, c);
				}
				// pop the value of the table we pushed earlier
				lua_pop(L, 1); 
				break;
			default:
				rv = false;
				break;
		}
		// pop both the value we pushed and the item require to be poped after calling next
		lua_pop(L, 2); 
	}
	//lua_pop(L, 1); 
	return rv;
}
Exemplo n.º 3
0
int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
{
	struct ubus_request req;
	int ret;

	blob_buf_init(&b, 0);

	if (obj->name && obj->type) {
		blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name);

		if (obj->type->id)
			blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id);
		else if (!ubus_push_object_type(obj->type))
			return UBUS_STATUS_INVALID_ARGUMENT;
	}

	if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0) < 0)
		return UBUS_STATUS_INVALID_ARGUMENT;

	req.raw_data_cb = ubus_add_object_cb;
	req.priv = obj;
	ret = ubus_complete_request(ctx, &req, 0);
	if (ret)
		return ret;

	if (!obj->id)
		return UBUS_STATUS_NO_DATA;

	return 0;
}
Exemplo n.º 4
0
static int test_method(struct ubus_method *self, struct ubus_context *ctx, struct ubus_object *obj,
		  struct ubus_request *req, struct blob_field *msg)
{
	void *t;
	
	struct blob bb; 
	blob_init(&bb, 0, 0);

	t = blob_open_table(&bb);
	blob_put_string(&bb, "foo"); 
	blob_put_string(&bb, "bar"); 
	blob_put_string(&bb, "bar"); 
	blob_put_int(&bb, 11);
	blob_close_table(&bb, t);

	//ubus_send_reply(ctx, req, blob_head(&bb));
	return 0;
}
Exemplo n.º 5
0
int main(void){
	orange_debug_level+=4; 

	struct orange *app = orange_new("test-plugins", "test-pwfile", "test-acls");
	struct blob out; 
	struct blob args; 
	blob_init(&out, 0, 0); 
	blob_init(&args, 0, 0); 

	blob_offset_t o = blob_open_table(&args); 
	blob_put_string(&args, "cmd"); 
	blob_put_string(&args, "echo 'Hello From Defferred!'"); 
	blob_put_string(&args, "msg"); 
	blob_put_string(&args, "Hello You"); 
	blob_put_string(&args, "arr"); 
	blob_offset_t a = blob_open_array(&args); 
	blob_put_int(&args, 1); 
	blob_offset_t t = blob_open_table(&args); 
	blob_put_string(&args, "a"); 
	blob_put_string(&args, "b"); 
	blob_close_table(&args, t); 
	blob_close_array(&args, a); 
	blob_close_table(&args, o); 

	struct orange_user *admin = orange_user_new("admin"); 
	orange_user_add_acl(admin, "test-acl"); 
	orange_add_user(app, &admin); 

	struct orange_sid sid; 
	TEST(orange_login_plaintext(app, "admin", "admin", &sid) == 0); 
	TEST(orange_call(app, sid.hash, "/test", "echo", blob_field_first_child(blob_head(&args)), &out) == 0);   
	TEST(orange_call(app, sid.hash, "/test", "noexist", blob_field_first_child(blob_head(&args)), &out) < 0);   
	TEST(orange_call(app, sid.hash, "/test", "test_c_calls", blob_field_first_child(blob_head(&args)), &out) == 0);   

	// test deferred
	struct blob def_args; 
	blob_init(&def_args, 0, 0); 
	o = blob_open_table(&def_args); 
	blob_put_string(&def_args, "cmd"); 
	char cmd[64]; 
	char cookie[16]; 
	snprintf(cookie, sizeof(cookie), "%lu", time(NULL)); 
	snprintf(cmd, sizeof(cmd), "printf %s > test-deferred.out", cookie); 
	blob_put_string(&def_args, cmd); 
	blob_close_table(&def_args, o); 
	int r = system("rm test-deferred.out"); 
	TEST(orange_call(app, sid.hash, "/test", "deferred_shell", blob_field_first_child(blob_head(&def_args)), &out) == 0);   
	FILE *f; 
	sleep(2); 
	TEST((f = fopen("test-deferred.out", "r")) == NULL); 
	sleep(3); 
	TEST(f = fopen("test-deferred.out", "r")); 
	// try to read the file
	char cmdr[32] = {0}; 
	TEST(fread(cmdr, 1, 32, f) > 0); 
	TEST(strcmp(cookie, cmdr) == 0); 
	blob_free(&def_args); 
	
	TEST(orange_logout(app, sid.hash) == 0); 

	blob_free(&out); 
	blob_free(&args); 
	orange_delete(&app); 

	return 0; 
}