Exemple #1
0
static json_object_t opensensordata_cache_get(opensensordata_t* osd, const char* name)
{
        int err;
        char buffer[512];
        char filename[512];

        if ((osd->cache == NULL) || (strlen(osd->cache) == 0)) {
                log_err("OpenSensorData: Invalid cache dir.");
                return json_null();
        }

        opensensordata_get_cache_file(osd, name, filename, 512);

        json_object_t def = json_load(filename, &err, buffer, 512);
        if (err) {
                log_err("%s", buffer); 
                json_unref(def);
                return json_null();
        } else if (!json_isobject(def)) {
                log_err("OpenSensorData: Invalid definition: %s", filename); 
                json_unref(def);
                return json_null();
        }

        // Check whether the response was an error object.
        json_object_t e = json_object_get(def, "error");
        if (!json_isnull(e)) {
                const char* msg = json_object_getstr(def, "msg");
                log_err("OpenSensorData: Server returned an error: %s", msg);
                json_unref(def);
                return json_null();
        }
        
        return def;
}
Exemple #2
0
int main(int argc, char **argv)
{
	jsmn_parser p;
	jsmntok_t tok[TOK_MAX];
	char * js;
	int len;
	int r;

	js = json_load(argv[1], &len);
	printf("got[%.*s]\n", len, js);

	while (len > 0) {

		jsmn_init(&p);

		r = jsmn_parse(&p, js, len, tok, TOK_MAX);

		if (r == JSMN_ERROR_NOMEM) {
			fprintf(stderr, "Err: Not enough tokens were provided!\n");
			return 1;
		}

		if (r == JSMN_ERROR_INVAL) {
			fprintf(stderr, "Err: Invalid character!\n");
			return 1;
		}

		if (r == JSMN_ERROR_PART) {
			fprintf(stderr, "Err: not a full JSON packet!\n");
			return 1;
		}

		if (r == 0) {
			fprintf(stderr, "Warn: empty JSON packet!\n");
			return 1;
		}

		if (json_traverse(js, tok) <= 0) {
			fprintf(stderr, "Err: tree traversal failed!\n");
			return 1;
		}
	
		js += tok[0].end;
		len -= tok[0].end;
	
/*
		printf("Parsed result %d\n", r);
		printf("len = %d\n", len);
		printf("p.pos = %d\n", p.pos);
		printf("tok[0].end = %d\n", tok[0].end);

		tok[0].start = JSMN_NULL;
		tok[0].end = JSMN_NULL; */
	}

	return 0;
}
Exemple #3
0
void monster::load_info(std::string data, std::vector <mtype *> *mtypes)
{
    std::stringstream dump;
    dump << data;
    if ( dump.peek() == '{' ) {
        picojson::value pdata;
        dump >> pdata;
        std::string jsonerr = picojson::get_last_error();
        if ( ! jsonerr.empty() ) {
            debugmsg("Bad monster json\n%s", jsonerr.c_str() );
        } else {
            json_load(pdata, mtypes);
        }
        return;
    } else {
Exemple #4
0
JsonParser::JsonParser(const char* fileName)
    : numTokens_(0),
    tokens_(nullptr),
    json_() {
    if (fileExists(fileName)) {
        std::ifstream file(fileName, std::ios::binary | std::ios::ate);
        std::size_t size = std::size_t(file.tellg());
        file.seekg(0, std::ios::beg);
        json_.resize(size + 1u);
        if (file.read(json_.data(), size)) {
            json_[size] = '\0';
            int read = 0;
            numTokens_ = json_num(json_.data(), json_.size());
            tokens_ = (json_token*)std::calloc(numTokens_, sizeof(json_token));
            json_load(tokens_, numTokens_, &read, json_.data(), json_.size());
        }
    }
}
/**
 * Get Data Key from server
 */
static int get_data_key(const char *token, char *masterkeyid, char *datakeyid, char *datakey)
{
	int ret;
	CURL *curl;
	CURLcode res;
	char serverurl[URL_SIZE];
	char recvdata[BUF_SIZE+2];
	char headerstring[HEADER_SIZE];
	void *jsonhandler=NULL;
	char *jsonmasterkeyid = NULL;
	char *jsondatakeyid = NULL;
	int timeout;
	char *jsonkeyencryptedbase64 = NULL;
	struct curl_slist* headers = NULL;
	size_t len;

	// make request header
	memset(headerstring, 0, HEADER_SIZE);
	sprintf(headerstring, "Authorization: Token %s", token);
	headers = curl_slist_append(headers, headerstring);

	// make request url
	memset(serverurl, 0, URL_SIZE);
	memset(recvdata, 0, BUF_SIZE+2);
	if (strlen(datakeyid) > 0)
		sprintf(serverurl, "http://%s/data/key/%s", fcgienc_datakeyserver, datakeyid);
	else
		sprintf(serverurl, "http://%s/data/key", fcgienc_datakeyserver);

	curl = curl_easy_init();
	if(curl) {
		curl_easy_setopt(curl, CURLOPT_URL, serverurl);
		//		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
		//		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFn);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)recvdata);
		curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5);

		/* Perform the request, res will get the return code */ 
		res = curl_easy_perform(curl);
		log_keythread("KEY-THREAD - curl request : %s, header : %s", serverurl, headerstring);

		/* Check for errors */ 
		if(res != CURLE_OK)
		{
			log_keythread("KEY-THREAD - curl failed : %s", curl_easy_strerror(res));

			ret = -1;
			goto DATAKEY_EXIT;
		}
	}

	// process json response
	jsonhandler = json_load(recvdata);
	log_keythread("KEY-THREAD - curl response : %s", recvdata);
	
	// get data key id
	jsondatakeyid = json_get_string(jsonhandler, "key_id");
	if (!jsondatakeyid)
	{
		log_keythread("KEY-THREAD - not found \"data key id\" in response : %s", recvdata);

		ret = -1;
		goto DATAKEY_EXIT;
	}

	// get master key id
	jsonmasterkeyid = json_get_string(jsonhandler, "master_key_id");
	if (!jsonmasterkeyid)
	{
		log_keythread("KEY-THREAD - not found master key id in response : %s", recvdata);

		ret = -1;
		goto DATAKEY_EXIT;
	}
	if (strcmp(jsonmasterkeyid, masterkeyid))
	{
		log_keythread("KEY-THREAD - unmatched master key id in old-%s : new-%s", masterkeyid, jsonmasterkeyid);

		len = strlen(jsonmasterkeyid);
		if (len > 255)
			len = 255;
		memcpy(masterkeyid, jsonmasterkeyid, len);
		masterkeyid[len] = 0;
	}

	// get refresh interval
	timeout = json_get_integer(jsonhandler, "refresh_interval");
	if (timeout < 0)
	{
		log_keythread("KEY-THREAD - not found \"data key refresh_interval\" in response : %s", recvdata);
		ret = -1;
		goto DATAKEY_EXIT;
	}

	// get key encrypted base64
	jsonkeyencryptedbase64 = json_get_string(jsonhandler, "key_encrypted_base64");
	if (!jsonkeyencryptedbase64)
	{
		log_keythread("KEY-THREAD - not found \"key_encrypted_base64\" in response : %s", recvdata);
		ret = -1;
		goto DATAKEY_EXIT;
	}

	// store into variable
	len = strlen(jsondatakeyid);
	if (len > 255)
		len = 255;
	memcpy(datakeyid, jsondatakeyid, len);
	datakeyid[len] = 0;
	
	len = strlen(jsonkeyencryptedbase64);
	if (len > 255)
		len = 255;
	memcpy(datakey, jsonkeyencryptedbase64, len);
	datakey[len] = 0;

	ret = timeout;

DATAKEY_EXIT:
	if (headers) curl_slist_free_all( headers ) ;
	if (curl) curl_easy_cleanup(curl);

	if (jsonhandler) json_unload(jsonhandler);
	return ret;
}
/**
 * Get Authentication Token from server
 */
static int get_auth_token(char *tokenstr)
{
	int ret;
	CURL *curl = NULL;
	CURLcode res;
	char serverurl[URL_SIZE];
	char senddata[BUF_SIZE+2];
	char recvdata[BUF_SIZE+2];
	char *token;
	char *timestring = NULL;
	void *jsonhandler = NULL;
	int timediff;
	struct curl_slist* headers = NULL;
	size_t len;

	// check parameters
	if (!tokenstr)
		return -1;

	// add the application/json content-type
	// so the server knows how to interpret our HTTP POST body
	headers = curl_slist_append(headers, "Content-Type: application/json");

	// create serverurl
	memset(serverurl, 0, URL_SIZE);
	memset(senddata, 0, BUF_SIZE+2);
	memset(recvdata, 0, BUF_SIZE+2);
	sprintf(serverurl, "http://%s/auth", fcgienc_authserver);
	sprintf(senddata, "{\"username\":\"%s\",\"password\":\"%s\"}", fcgienc_username, fcgienc_password);

	curl = curl_easy_init();
	if(curl) 
	{
		// setup curl
		curl_easy_setopt(curl, CURLOPT_URL, serverurl);
		// curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
		// curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
		curl_easy_setopt(curl, CURLOPT_POST, 1);
		curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(senddata));
		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
		curl_easy_setopt(curl, CURLOPT_READFUNCTION, readFn);
		curl_easy_setopt(curl, CURLOPT_READDATA, (void *)senddata);
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFn);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)recvdata);
		curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5);

		/* Perform the request, res will get the return code */ 
		res = curl_easy_perform(curl);
		log_keythread("KEY-THREAD - curl request : %s, senddata : %s", serverurl, senddata);

		/* Check for errors */ 
		if(res != CURLE_OK)
		{
			log_keythread("KEY-THREAD - curl failed : %s", curl_easy_strerror(res));

			ret = -1;
			goto AUTH_REQUEST_EXIT;
		}
	}

	// process json response
	jsonhandler = json_load(recvdata);
	log_keythread("KEY-THREAD - curl response : %s", recvdata);

	// get token
	token = json_get_string(jsonhandler, "token");
	if (!token)
	{
		log_keythread("KEY-THREAD - not found \"token\" in response : %s", recvdata);
		
		ret = -1;
		goto AUTH_REQUEST_EXIT;
	}

	// expiration_time: <UTC time when the token will expire; e.g. 2015-08-11T20:31:17.341Z>
	timestring = json_get_string(jsonhandler, "expiration_time");
	if (!timestring)
	{
		log_keythread("KEY-THREAD - not found \"expiration_time\" in response : %s", recvdata);
		
		ret = -1;
		goto AUTH_REQUEST_EXIT;
	}

	// store token
	len = strlen(token);
	if (len > 255)
		len = 255;
	memcpy(tokenstr, token, len);
	tokenstr[len] = 0;

	// store into memcache
	timediff = (int)time_utc_diff(timestring);

	ret = timediff;

AUTH_REQUEST_EXIT:
	if (headers) curl_slist_free_all( headers ) ;
	if (curl) curl_easy_cleanup(curl);

	if (jsonhandler) json_unload(jsonhandler);

	return ret;
}
Exemple #7
0
int icelua_init(void)
{
	int i, argct;
	
	// create states
	lstate_client = (boot_mode & 1 ? luaL_newstate() : NULL);
	lstate_server = (boot_mode & 2 ? luaL_newstate() : NULL);
	
	if(lstate_client != NULL)
	{
		// create temp state for loading config
		lua_State *Lc = luaL_newstate();
		int v;
		float f;

		// load config
#ifndef DEDI
		if(!json_load(Lc, "clsave/config.json"))
		{
			// set video stuff 
			lua_getfield(Lc, -1, "video");

			lua_getfield(Lc, -1, "width");
			v = lua_tointeger(Lc, -1);
			if(v >= 0) screen_width = v;
			lua_pop(Lc, 1);

			lua_getfield(Lc, -1, "height");
			v = lua_tointeger(Lc, -1);
			if(v >= 0) screen_height = v;
			lua_pop(Lc, 1);
			
			lua_getfield(Lc, -1, "cubeshift");
			v = lua_tointeger(Lc, -1);
			if(v != 0) screen_cubeshift = -v;
			lua_pop(Lc, 1);

			lua_getfield(Lc, -1, "antialiasinglevel");
			v = lua_tointeger(Lc, -1);
			if(v >= 0) screen_antialiasing_level = v;
			lua_pop(Lc, 1);

			lua_getfield(Lc, -1, "smoothlighting");
			v = lua_toboolean(Lc, -1);
			if(!lua_isnil(Lc, -1)) screen_smooth_lighting = v;
			lua_pop(Lc, 1);

			lua_getfield(Lc, -1, "vbo");
			v = lua_toboolean(Lc, -1);
			if(!lua_isnil(Lc, -1)) gl_use_vbo = v;
			lua_pop(Lc, 1);

			lua_getfield(Lc, -1, "gl_expand_textures");
			v = lua_toboolean(Lc, -1);
			if(!lua_isnil(Lc, -1)) gl_expand_textures = v;
			lua_pop(Lc, 1);

			lua_getfield(Lc, -1, "gl_chunk_size");
			v = lua_tointeger(Lc, -1);
			if(v > 0) gl_chunk_size = v;
			lua_pop(Lc, 1);

			lua_getfield(Lc, -1, "gl_visible_chunks");
			v = lua_tointeger(Lc, -1);
			if(v > 0) gl_visible_chunks = v;
			lua_pop(Lc, 1);

			lua_getfield(Lc, -1, "gl_chunks_tesselated_per_frame");
			v = lua_tointeger(Lc, -1);
			if(v > 0) gl_chunks_tesselated_per_frame = v;
			lua_pop(Lc, 1);

			lua_getfield(Lc, -1, "fullscreen");
			v = lua_toboolean(Lc, -1);
			if(!lua_isnil(Lc, -1)) screen_fullscreen = v;
			lua_pop(Lc, 1);
			
			// drop table
			lua_pop(Lc, 1);

			// set audio stuff 
			lua_getfield(Lc, -1, "audio");

			lua_getfield(Lc, -1, "freq");
			v = lua_tointeger(Lc, -1);
			if(v >= 0) wav_mfreq = v;
			lua_pop(Lc, 1);
			
			lua_getfield(Lc, -1, "bufsize");
			v = lua_tointeger(Lc, -1);
			if(v >= 0) wav_bufsize = v;
			lua_pop(Lc, 1);
			
			lua_getfield(Lc, -1, "volume");
			f = lua_tonumber(Lc, -1);
			if(!lua_isnil(Lc, -1)) wav_gvol = f;
			lua_pop(Lc, 1);
			
			// drop table
			lua_pop(Lc, 1);
		}
#endif
	}
	
	// create tables
	if(lstate_client != NULL)
	{
		lua_newtable(lstate_client);
		lua_setglobal(lstate_client, "client");
		lua_newtable(lstate_client);
		lua_setglobal(lstate_client, "common");
		lua_pushvalue(lstate_client, LUA_GLOBALSINDEX);
		lua_setglobal(lstate_client, "_G");
	}
	
	if(lstate_server != NULL)
	{
		lua_newtable(lstate_server);
		lua_setglobal(lstate_server, "server");
		lua_newtable(lstate_server);
		lua_setglobal(lstate_server, "common");
		lua_pushvalue(lstate_server, LUA_GLOBALSINDEX);
		lua_setglobal(lstate_server, "_G");
	}
	
	// load stuff into them
#ifndef DEDI
	icelua_loadfuncs(lstate_client, "client", icelua_client);
	icelua_loadfuncs(lstate_client, "client", icelua_common);
	icelua_loadfuncs(lstate_client, "common", icelua_common);
	icelua_loadfuncs(lstate_client, "client", icelua_common_client);
	icelua_loadfuncs(lstate_client, "common", icelua_common_client);
#endif
	icelua_loadfuncs(lstate_server, "server", icelua_server);
	icelua_loadfuncs(lstate_server, "server", icelua_common);
	icelua_loadfuncs(lstate_server, "common", icelua_common);
	icelua_loadfuncs(lstate_server, "server", icelua_common_server);
	icelua_loadfuncs(lstate_server, "common", icelua_common_server);
	
	// load some lua base libraries
	icelua_loadbasefuncs(lstate_client);
	icelua_loadbasefuncs(lstate_server);
	
	// shove some pathnames / versions in
	if(lstate_server != NULL)
	{
		lua_getglobal(lstate_server, "common");
		lua_getglobal(lstate_server, "server");
		lua_pushstring(lstate_server, mod_basedir+4);
		lua_setfield(lstate_server, -2, "base_dir");
		lua_pop(lstate_server, 1);
		lua_pushstring(lstate_server, mod_basedir+4);
		lua_setfield(lstate_server, -2, "base_dir");
		lua_pop(lstate_server, 1);
		
		icelua_pushversion(lstate_server, "common");
		icelua_pushversion(lstate_server, "server");
	}
	
	if(lstate_client != NULL)
	{
		icelua_pushversion(lstate_client, "common");
		icelua_pushversion(lstate_client, "client");
	}
	
	/*
	NOTE:
	to call stuff, use lua_pcall.
	DO NOT use lua_call! if it fails, it will TERMINATE the program!
	*/
	
	// quick test
	// TODO: set up a "convert/filter file path" function
	// TODO: split the client/server inits
	char xpath[128];
	snprintf(xpath, 128, "%s/main_server.lua", mod_basedir);
	
	if((lstate_server != NULL) && luaL_loadfile(lstate_server, xpath) != 0)
	{
		printf("ERROR loading server Lua: %s\n", lua_tostring(lstate_server, -1));
		return 1;
	}
	
	argct = (main_largstart == -1 || (main_largstart >= main_argc)
		? 0
		: main_argc - main_largstart);
	
	if(lstate_server != NULL)
	{
		for(i = 0; i < argct; i++)
			lua_pushstring(lstate_server, main_argv[i+main_largstart]);
		if(lua_pcall(lstate_server, argct, 0, 0) != 0)
		{
			printf("ERROR running server Lua: %s\n", lua_tostring(lstate_server, -1));
			lua_pop(lstate_server, 1);
			return 1;
		}
	}
	
	if(lstate_client != NULL && mod_basedir != NULL)
		if(icelua_initfetch())
			return 1;
	
	// dispatch initial connect
	if(lstate_server != NULL && lstate_client != NULL)
	{
		lua_getglobal(lstate_server, "server");
		lua_getfield(lstate_server, -1, "hook_connect");
		lua_remove(lstate_server, -2);
		if(!lua_isnil(lstate_server, -1))
		{
			lua_pushboolean(lstate_server, 1);
			lua_newtable(lstate_server);
			
			lua_pushstring(lstate_server, "local");
			lua_setfield(lstate_server, -2, "proto");
			lua_pushnil(lstate_server);
			lua_setfield(lstate_server, -2, "addr");
			
			if(lua_pcall(lstate_server, 2, 0, 0) != 0)
			{
				printf("ERROR running server Lua (hook_connect): %s\n", lua_tostring(lstate_server, -1));
				lua_pop(lstate_server, 2);
				return 1;
			}
		} else {
			lua_pop(lstate_server, 1);
		}
	}
	
	return 0;
}