Ejemplo n.º 1
0
static void
dbrelay_write_json_log(json_t *json, dbrelay_request_t *request, char *error_string)
{
   	json_add_key(json, "log");
   	json_new_object(json);
   	if (request->sql) json_add_string(json, "sql", request->sql);
    	json_add_string(json, "error", error_string);
        json_end_object(json);
        json_end_object(json);
}
Ejemplo n.º 2
0
int main()
{
	int i = 0;
    int rc;
	while (i++ < 1)
	{
		json_t *jso = json_new();
		json_add_string(jso, "string1", "string1_val");
		json_add_string(jso, "string2", "string2_val");
		json_add_int(jso, "integer1", 12345);
		printf("%s\n", json_get_string(jso, "string1"));
		printf("%s\n%d\n", json_get_string(jso, "string2"),
			json_get_int(jso, "integer1"));

		json_padd_string(jso, "sub-object/sub-string", "sub-string-val");
		json_padd_string(jso, "object1/sub-object/sub-string", "sub-string-val");
		char *path = "object1/sub-object1/sub-sub-object1//sub-sub-string1";
		json_padd_string(jso, path, "sub-sub-string1-val ");
		printf("sub-sub-string1=[%s]\n", json_pget_string(jso, path));
		json_add_string(jso, "string3", "string3_val");
		json_padd_string(jso, "string4", "string4_val");
//		LOG("\n%s", json_to_string(jso));

        array_t *arr = json_new_array( jso, "array0" );
        rc = json_array_add_string( arr, 0, "array-item", "array-item0-val" );
        rc = json_array_add_int( arr, 0, "array-item-int", 1234 );
        rc = json_array_add_string( arr, 1, "array-item", "array-item1-val" );
        rc = json_array_add_int( arr, 1, "array-item-int", 2234 );
        rc = json_array_add_string( arr, 2, "array-item", "array-item2-val" );
        rc = json_array_add_int( arr, 2, "array-item-int", 3234 );
        /*
        LOG( "rc = [%d][%s][%s][%s][%d]", rc, json_array_get_string( arr, 0, "array-item"),
            json_array_get_string( arr, 1, "array-item" ),
            json_array_get_string( arr, 2, "array-item" ),
            json_array_get_int( arr, 2, "array-item-int") );
            */

        //printf( "====[%d]===[%s]-=-----\n", json_array_getlen(arr),json_array_get_string( arr, 0, "array-item" ) );
        json_t *tmp1 = json_array_get_item( arr, 1 );
        //printf( "-----------\n%s\n--------\n", json_to_string(tmp1) );
		//printf("\n%s\n", json_to_string(jso));
        LOG( "\n%s\n", json_to_string(jso) );
		json_free(&jso);
	}
	//system("pause");
}
Ejemplo n.º 3
0
void json_add_hex(struct json_result *result, const char *fieldname,
		  const void *data, size_t len)
{
	char hex[hex_str_size(len)];

	hex_encode(data, len, hex, sizeof(hex));
	json_add_string(result, fieldname, hex);
}
Ejemplo n.º 4
0
void json_add_address(char **result, const char *fieldname, bool test_net,
		      const struct protocol_address *addr)
{
	char *str = pettycoin_to_base58(*result, test_net, addr, false);

	json_add_string(result, fieldname, str);
	tal_free(str);
}
Ejemplo n.º 5
0
void json_add_hex(char **result, const char *fieldname, const void *data,
		  size_t len)
{
	char *hex = to_hex(*result, data, len);

	json_add_string(result, fieldname, hex);
	tal_free(hex);
}
Ejemplo n.º 6
0
static void json_add_time(struct json_result *result, const char *fieldname,
			  struct timespec ts)
{
	char timebuf[100];

	sprintf(timebuf, "%lu.%09u",
		(unsigned long)ts.tv_sec,
		(unsigned)ts.tv_nsec);
	json_add_string(result, fieldname, timebuf);
}
Ejemplo n.º 7
0
static void add_skipped(struct log_info *info)
{
	if (info->num_skipped) {
		json_array_start(info->response, NULL);
		json_add_string(info->response, "type", "SKIPPED");
		json_add_num(info->response, "num_skipped", info->num_skipped);
		json_array_end(info->response);
		info->num_skipped = 0;
	}
}
Ejemplo n.º 8
0
static void json_stop(struct command *cmd,
		      const char *buffer, const jsmntok_t *params)
{
	struct json_result *response = new_json_result(cmd);

	/* This can't have closed yet! */
	cmd->jcon->stop = true;
	json_add_string(response, NULL, "Shutting down");
	command_success(cmd, response);
}
Ejemplo n.º 9
0
static void dbrelay_append_log_json(json_t *json, dbrelay_request_t *request, char *error_string)
{
   int i;
   char tmp[20];

   json_add_key(json, "log");
   json_new_object(json);
   if (request->flags & DBRELAY_FLAG_ECHOSQL) json_add_string(json, "sql", request->sql);
   if (strlen(error_string)) {
      json_add_string(json, "error", error_string);
   }
   i = 0;
   while (request->params[i]) {
      sprintf(tmp, "param%d", i);
      json_add_string(json, tmp, request->params[i]);
      i++;
   }
   json_end_object(json);

   json_end_object(json);
}
Ejemplo n.º 10
0
static void log_to_json(unsigned int skipped,
			struct timerel diff,
			enum log_level level,
			const char *prefix,
			const char *log,
			struct log_info *info)
{
	info->num_skipped += skipped;

	if (level < info->level) {
		info->num_skipped++;
		return;
	}

	add_skipped(info);

	json_array_start(info->response, NULL);
	json_add_string(info->response, "type",
			level == LOG_BROKEN ? "BROKEN"
			: level == LOG_UNUSUAL ? "UNUSUAL"
			: level == LOG_INFORM ? "INFO"
			: level == LOG_DBG ? "DEBUG"
			: level == LOG_IO ? "IO"
			: "UNKNOWN");
	json_add_time(info->response, "time", diff.ts);
	json_add_string(info->response, "source", prefix);
	if (level == LOG_IO) {
		if (log[0])
			json_add_string(info->response, "direction", "IN");
		else
			json_add_string(info->response, "direction", "OUT");

		json_add_hex(info->response, "data", log+1, tal_count(log)-1);
	} else
		json_add_string(info->response, "log", log);

	json_array_end(info->response);
}
Ejemplo n.º 11
0
/*
 * echo request parameters in json output
 */
static void dbrelay_append_request_json(json_t *json, dbrelay_request_t *request)
{
   json_add_key(json, "request");
   json_new_object(json);

   if (IS_SET(request->query_tag)) 
      json_add_string(json, "query_tag", request->query_tag);
   json_add_string(json, "sql_server", request->sql_server);
   json_add_string(json, "sql_user", request->sql_user);

   if (IS_SET(request->sql_port)) 
      json_add_string(json, "sql_port", request->sql_port);

   json_add_string(json, "sql_database", request->sql_database);

/*
 * do not return password back to client
   if (IS_SET(request->sql_password)) 
      json_add_string(json, "sql_password", request->sql_password);
*/

   json_end_object(json);

}
Ejemplo n.º 12
0
void dbrelay_write_json_colinfo(json_t *json, void *db, int colnum, int *maxcolname)
{
   char tmp[256], *colname, tmpcolname[256];
   int l;

   json_new_object(json);
   colname = api->colname(db, colnum);
   if (dbrelay_is_unnamed_column(colname)) {
      sprintf(tmpcolname, "%d", ++(*maxcolname));
      json_add_string(json, "name", tmpcolname);
   } else {
      l = atoi(colname); 
      if (l>0 && l>*maxcolname) {
         *maxcolname=l;
      }
      json_add_string(json, "name", colname);
   }
   api->coltype(db, colnum, tmp);
   json_add_string(json, "sql_type", tmp);
   l = api->collen(db, colnum);
   if (l!=0) {
      sprintf(tmp, "%d", l);
      json_add_number(json, "length", tmp);
   }
   l = api->colprec(db, colnum);
   if (l!=0) {
      sprintf(tmp, "%d", l);
      json_add_number(json, "precision", tmp);
   }
   l = api->colscale(db, colnum);
   if (l!=0) {
      sprintf(tmp, "%d", l);
      json_add_number(json, "scale", tmp);
   }
   json_end_object(json);
}
Ejemplo n.º 13
0
static void dbrelay_write_json_column_std(json_t *json, void *db, int colnum, char *colname)
{
   int colsize;
   char *tmp;

   colsize = api->collen(db, colnum);
   tmp = (char *) malloc(colsize > 256 ? colsize : 256);

   if (api->colvalue(db, colnum, tmp)==NULL) {
      json_add_null(json, colname);
   } else if (api->is_quoted(db, colnum)) {
      json_add_string(json, colname, tmp);
   } else {
      json_add_number(json, colname, tmp);
   }
   free(tmp);
}
Ejemplo n.º 14
0
void json_add_object(struct json_result *result, ...)
{
	va_list ap;
	const char *field;

	va_start(ap, result);
	json_object_start(result, NULL);
	while ((field = va_arg(ap, const char *)) != NULL) {
		jsmntype_t type = va_arg(ap, jsmntype_t);
		const char *value = va_arg(ap, const char *);
		if (type == JSMN_STRING)
			json_add_string(result, field, value);
		else
			json_add_literal(result, field, value, strlen(value));
	}
	json_object_end(result);
	va_end(ap);
}
Ejemplo n.º 15
0
static int devices_callback(MODULE *module, void *c)
{
	const char *pName;
	const int  *pType;
	size_t len;
	size_t this_item_len;
	unsigned char *this_item_start;

	ENUM_CTX *ctx = (ENUM_CTX *)c;

	module->get_var(MODULE_VAR_TERMINAL_NAME, (void**)&pName);
	module->get_var(MODULE_VAR_TERMINAL_TYPE, (void**)&pType);

	len = strlen(pName);

 	             // " + { + "name" + : + " + len + " + , + "id" + : + 13 + }
	this_item_len = 1 + 1 + 6      + 1 + 1 + len + 1 + 1 + 4    + 1 + 13 + 1;

	if (this_item_len <= ctx->bytes_left) {

		this_item_start = ctx->ptr;

		ctx->module_no++;
		if (ctx->module_no > 1)
			*ctx->ptr++ = ',';

		*ctx->ptr++ = '{';

		ctx->ptr = json_add_string(ctx->ptr, "name", pName, len);

		*ctx->ptr++ = ',';

		ctx->ptr = json_add_uint(ctx->ptr, "id", *pType);

		*ctx->ptr++ = '}';

		ctx->bytes_left -= (ctx->ptr - this_item_start);

		return 0;
	}

	return -1;
}
Ejemplo n.º 16
0
void json_add_tx(struct json_result *response, const char *fieldname,
		 struct state *state,
		 const union protocol_tx *tx,
		 const struct block *block,
		 unsigned int confirms)
{
	struct protocol_tx_id sha;

	json_object_start(response, fieldname);
	hash_tx(tx, &sha);
	json_add_tx_id(response, "txid", &sha);
	if (block)
		json_add_block_id(response, "block", &block->sha);
	json_add_num(response, "confirmations", confirms);
	json_add_num(response, "version", tx->hdr.version);
	json_add_num(response, "features", tx->hdr.features);

	switch (tx_type(tx)) {
	case TX_NORMAL:
		json_add_string(response, "type", "TX_NORMAL");
		json_add_pubkey(response, "input_key", &tx->normal.input_key);
		json_add_address(response, "output_addr",
				 state->test_net, &tx->normal.output_addr);
		json_add_num(response, "send_amount",
			     le32_to_cpu(tx->normal.send_amount));
		json_add_num(response, "change_amount",
			     le32_to_cpu(tx->normal.change_amount));
		json_add_signature(response, "signature",
				   &tx->normal.signature);
		json_add_inputs(response, tx);
		goto finish;
	case TX_FROM_GATEWAY:
		json_add_string(response, "type", "TX_FROM_GATEWAY");
		json_add_pubkey(response, "gateway_key",
				&tx->from_gateway.gateway_key);
		json_add_signature(response, "signature",
				   &tx->normal.signature);
		json_add_outputs(response, state, tx);
		goto finish;
	case TX_TO_GATEWAY:
		json_add_string(response, "type", "TX_TO_GATEWAY");
		json_add_pubkey(response, "input_key",
				&tx->to_gateway.input_key);
		json_add_address(response, "output_addr",
				 state->test_net,
				 &tx->to_gateway.to_gateway_addr);
		json_add_num(response, "send_amount",
			     le32_to_cpu(tx->to_gateway.send_amount));
		json_add_num(response, "change_amount",
			     le32_to_cpu(tx->to_gateway.change_amount));
		json_add_signature(response, "signature",
				   &tx->to_gateway.signature);
		json_add_inputs(response, tx);
		goto finish;
	case TX_CLAIM:
		json_add_string(response, "type", "TX_CLAIM");
		json_add_pubkey(response, "input_key", &tx->claim.input_key);
		json_add_num(response, "amount", le32_to_cpu(tx->claim.amount));
		json_add_signature(response, "claim", &tx->claim.signature);
		json_add_input(response, "input", &tx->claim.input);
		goto finish;
	}
	abort();

finish:
	json_object_end(response);
}
Ejemplo n.º 17
0
u_char *dbrelay_db_status(dbrelay_request_t *request)
{
   dbrelay_connection_t *connections;
   dbrelay_connection_t *conn;
   json_t *json = json_new();
   int i;
   char tmpstr[100];
   u_char *json_output;
   struct tm *ts;


   json_new_object(json);
   json_add_key(json, "status");
   json_new_object(json);

   json_add_key(json, "info");
   json_new_object(json);
   json_add_string(json, "build", DBRELAY_BUILD);
   sprintf(tmpstr, "0x%08x", dbrelay_get_ipc_key());
   json_add_string(json, "ipckey", tmpstr);
   json_end_object(json);

   json_add_key(json, "connections");
   json_new_array(json);

   connections = dbrelay_time_get_shmem(request);

   for (i=0; i<DBRELAY_MAX_CONN; i++) {
     conn = &connections[i];
     if (connections[i].pid!=0) {
        json_new_object(json);
        sprintf(tmpstr, "%u", conn->slot);
        json_add_number(json, "slot", tmpstr);
        sprintf(tmpstr, "%u", conn->pid);
        json_add_number(json, "pid", tmpstr);
        json_add_string(json, "name", conn->connection_name ? conn->connection_name : "");
        ts = localtime(&conn->tm_create);
        strftime(tmpstr, sizeof(tmpstr), "%Y-%m-%d %H:%M:%S", ts);
        json_add_string(json, "tm_created", tmpstr);
        ts = localtime(&conn->tm_accessed);
        strftime(tmpstr, sizeof(tmpstr), "%Y-%m-%d %H:%M:%S", ts);
        json_add_string(json, "tm_accessed", tmpstr);
        json_add_string(json, "sql_server", conn->sql_server ? conn->sql_server : "");
        json_add_string(json, "sql_port", conn->sql_port ? conn->sql_port : "");
        json_add_string(json, "sql_database", conn->sql_database ? conn->sql_database : "");
        json_add_string(json, "sql_user", conn->sql_user ? conn->sql_user : "");
        sprintf(tmpstr, "%ld", conn->connection_timeout);
        json_add_number(json, "connection_timeout", tmpstr);
        sprintf(tmpstr, "%u", conn->in_use);
        json_add_number(json, "in_use", tmpstr);
        json_add_string(json, "sock_path", conn->sock_path);
        sprintf(tmpstr, "%u", conn->helper_pid);
        json_add_number(json, "helper_pid", tmpstr);
        json_end_object(json);
     }
   }

   dbrelay_time_release_shmem(request, connections);

   json_end_array(json);
   json_end_object(json);
   json_end_object(json);

   json_output = (u_char *) json_to_string(json);
   json_free(json);

   return json_output;
}