Пример #1
0
struct json_object *util_mapping_to_json(struct ndctl_mapping *mapping)
{
	struct json_object *jmapping = json_object_new_object();
	struct ndctl_dimm *dimm = ndctl_mapping_get_dimm(mapping);
	struct json_object *jobj;

	if (!jmapping)
		return NULL;

	jobj = json_object_new_string(ndctl_dimm_get_devname(dimm));
	if (!jobj)
		goto err;
	json_object_object_add(jmapping, "dimm", jobj);

	jobj = json_object_new_int64(ndctl_mapping_get_offset(mapping));
	if (!jobj)
		goto err;
	json_object_object_add(jmapping, "offset", jobj);

	jobj = json_object_new_int64(ndctl_mapping_get_length(mapping));
	if (!jobj)
		goto err;
	json_object_object_add(jmapping, "length", jobj);

	return jmapping;
 err:
	json_object_put(jmapping);
	return NULL;
}
Пример #2
0
json_object* NdpiStats::getJSONObject(NetworkInterface *iface) {
  char *unknown = iface->get_ndpi_proto_name(NDPI_PROTOCOL_UNKNOWN);
  json_object *my_object;
  
  my_object = json_object_new_object();

  for(int proto_id=0; proto_id<MAX_NDPI_PROTOS; proto_id++) {
    if(counters[proto_id] != NULL) {
      char *name = iface->get_ndpi_proto_name(proto_id);
      
      if((proto_id > 0) && (name == unknown)) break;

      if(name != NULL) {
	json_object *inner, *inner1;

	inner = json_object_new_object();

	inner1 = json_object_new_object();
	json_object_object_add(inner1, "sent", json_object_new_int64(counters[proto_id]->bytes.sent));
	json_object_object_add(inner1, "rcvd", json_object_new_int64(counters[proto_id]->bytes.rcvd));
	json_object_object_add(inner, "bytes", inner1);

	inner1 = json_object_new_object();
	json_object_object_add(inner1, "sent", json_object_new_int64(counters[proto_id]->packets.sent));
	json_object_object_add(inner1, "rcvd", json_object_new_int64(counters[proto_id]->packets.rcvd));
	json_object_object_add(inner, "packets", inner1);

	json_object_object_add(my_object, name, inner);
      }
    }
  }

  return(my_object);
}
Пример #3
0
bool appbase_push_frame(struct appbase *ab,
		const unsigned char *data, size_t length,
		struct timeval *timestamp)
{
	CURLcode response_code;
	struct json_internal json;
	size_t b64_size = 0;
	char *b64_data;

	if (!ab || !ab->curl || !ab->url || !ab->json || !data || !length || !timestamp)
		return false;

	/* Transform raw frame data into base64 */
	b64_size = modp_b64_encode_len(length);
	b64_data = ec_malloc(b64_size);
	if (modp_b64_encode(b64_data, (char *) data, length) == -1)
		return false;

	/*
	 * Generate a JSON object with the format:
	 *
	 * 	{
	 * 		"image": "<data>",
	 * 		"sec": "<seconds>",
	 * 		"usec": "<milliseconds>"
	 * 	}
	 */
	json_object_object_add(ab->json, AB_KEY_IMAGE, json_object_new_string(b64_data));
	json_object_object_add(ab->json, AB_KEY_SEC, json_object_new_int64(timestamp->tv_sec));
	json_object_object_add(ab->json, AB_KEY_USEC, json_object_new_int64(timestamp->tv_usec));

	json.json = json_object_to_json_string_ext(ab->json, JSON_C_TO_STRING_PLAIN);
	json.length = strlen(json.json);
	json.offset = 0;

	curl_easy_setopt(ab->curl, CURLOPT_URL, ab->url);
	curl_easy_setopt(ab->curl, CURLOPT_UPLOAD, 1L);
	curl_easy_setopt(ab->curl, CURLOPT_INFILESIZE, json.length);
	curl_easy_setopt(ab->curl, CURLOPT_READDATA, &json);
	curl_easy_setopt(ab->curl, CURLOPT_READFUNCTION, reader_cb);

	response_code = curl_easy_perform(ab->curl);

	/*
	 * No need to free the JSON string.
	 * We call json_object_put() on the root JSON object in appbase_close(),
	 * and it will release the whole JSON object, including this string
	 * for us.
	 */
	json.length = 0;
	json.offset = 0;
	free(b64_data);

	return (response_code == CURLE_OK);
}
Пример #4
0
json_object* Flow::processJson(ProcessInfo *proc) {
  json_object *inner;
  
  inner = json_object_new_object();
  json_object_object_add(inner, "cpu_id", json_object_new_int64(proc->cpu_id));
  json_object_object_add(inner, "pid", json_object_new_int64(proc->pid));
  json_object_object_add(inner, "father_pid", json_object_new_int64(proc->father_pid));
  json_object_object_add(inner, "name", json_object_new_string(proc->name));
  json_object_object_add(inner, "father_name", json_object_new_string(proc->father_name));
  json_object_object_add(inner, "user_name", json_object_new_string(proc->user_name));
  
  return(inner);
}
static struct json_object *respondd_provider_nodeinfo(void) {
	struct ffffm_wifi_info *i = NULL;
	struct json_object *ret = NULL, *wireless = NULL;

	wireless = json_object_new_object();
	if (!wireless)
		goto end;

	ret = json_object_new_object();
	if (!ret)
		goto end;

        i = ffffm_get_wifi_info();
	if (!i)
		goto end;

        struct json_object *v;

	if (i->c24 != FFFFM_INVALID_CHANNEL) {
		v = json_object_new_int64(i->c24);
		if (!v)
			goto end;
		json_object_object_add(wireless, "chan2", v);
	}
	if (i->c50 != FFFFM_INVALID_CHANNEL) {
		v = json_object_new_int64(i->c50);
		if (!v)
			goto end;
		json_object_object_add(wireless, "chan5", v);
	}
	if (i->t24 != FFFFM_INVALID_TXPOWER) {
		v = json_object_new_int64(i->t24);
		if (!v)
			goto end;
		json_object_object_add(wireless, "txpower2", v);
	}
	if (i->t50 != FFFFM_INVALID_TXPOWER) {
		v = json_object_new_int64(i->t50);
		if (!v)
			goto end;
		json_object_object_add(wireless, "txpower5", v);
	}

	json_object_object_add(ret, "wireless", wireless);
end:
        free(i);
	return ret;
	
}
Пример #6
0
json_object *hostspec_collect_block_device() {
  json_object *obj = json_object_new_object();
  assert(obj);

  DIR *dir = opendir("/sys/block");
  if (!dir) {
    ULOG_ERR("Unable to open /sys/block: %s\n", strerror(errno));
    goto error;
  }

  struct dirent *ent;
  while ((ent = readdir(dir))) {
    char const *name = ent->d_name;

    if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
      continue;
    }

    json_object *obj_dev = json_object_new_object();
    assert(obj_dev);
    json_object_object_add(obj, name, obj_dev);

    char buf[1024], *p;
    if ((p = fgets_close(buf, sizeof buf, fopenf("r", "/sys/block/%s/size", name)))) {
      int64_t size;
      if (json_parse_int64(p, &size) == 0) {
        json_object_object_add(obj_dev, "size", json_object_new_int64(size));
      }
    }

    if ((p = fgets_close(buf, sizeof buf, fopenf("r", "/sys/block/%s/removable", name)))) {
      int64_t removable;
      if (json_parse_int64(p, &removable) == 0) {
        json_object_object_add(obj_dev, "removable", json_object_new_int64(removable));
      }
    }
  }

  closedir(dir);
  return obj;

error:
  if (dir) {
    closedir(dir);
  }

  json_object_put(obj);
  return NULL;
}
Пример #7
0
int save_keyframe_info(struct json_object *arr_obj, long p, long t)
{
	if(NULL == arr_obj)
	{
		return -1;
	}
	//fprintf(stdout, "%s insert 1 keyframe into json obj.", getSystemTime(timeChar));
	struct json_object *infoObj = json_object_new_object();
	json_object_object_add(infoObj, "p", json_object_new_int64(p));
	json_object_object_add(infoObj, "t", json_object_new_int64(t));
	
	json_object_array_add(arr_obj, infoObj);

	return 0;
}
Пример #8
0
/* Queue functionality */
void addRawMessageToRestQueue(char *data, int length) {
	if(!Rest.uri) {
		return;
	}
	struct queue_message *curr = malloc(sizeof(struct queue_message));
	struct timeval tv;

	gettimeofday(&tv, NULL);
	unsigned long long millisecondsSinceEpoch =
	    (unsigned long long)(tv.tv_sec) * 1000 +
	    (unsigned long long)(tv.tv_usec) / 1000;

	data[length-2] = '\0';
	json_object *json = json_object_new_object();
	json_object *adsb_data = json_object_new_string(data);
	json_object *timestamp = json_object_new_int64(millisecondsSinceEpoch);

	json_object_object_add(json, "message",  adsb_data);
	json_object_object_add(json, "timestamp", timestamp);

	curr->message = json;

	addMessageToQueue(curr);
//	printf("Message added %s\n", data);
}
Пример #9
0
static struct json_object *create_fake_gps_rmc_obj()
{
    struct json_object *obj = NULL, *obj_add = NULL;
    struct timeval tv;

    if (!(obj = json_object_new_object()))
        return NULL;

    gettimeofday(&tv, NULL);
	if (!(obj_add = json_object_new_int64(tv.tv_sec))) {
        json_object_put(obj);
        return NULL;
    }
    json_object_object_add(obj, "timestamp", obj_add);

    if (!(obj_add = json_object_new_int(META_TYPE_POS))) {
        json_object_put(obj);
        return NULL;
    }
    json_object_object_add(obj, "event_type", obj_add);

    if (!(obj_add = json_object_new_string("$GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*68"))) {
        json_object_put(obj);
        return NULL;
    }

    json_object_object_add(obj, "nmea_string", obj_add);

    return obj;
}
Пример #10
0
static struct json_object *create_fake_gps_gga_obj()
{
    struct json_object *obj = NULL, *obj_add = NULL;
    struct timeval tv;

    if (!(obj = json_object_new_object()))
        return NULL;

    gettimeofday(&tv, NULL);
	if (!(obj_add = json_object_new_int64(tv.tv_sec))) {
        json_object_put(obj);
        return NULL;
    }
    json_object_object_add(obj, "timestamp", obj_add);

    if (!(obj_add = json_object_new_int(META_TYPE_POS))) {
        json_object_put(obj);
        return NULL;
    }
    json_object_object_add(obj, "event_type", obj_add);

    if (!(obj_add = json_object_new_string("$GPGGA,190406.0,5103.732280,N,01701.493660,E,1,05,1.7,130.0,M,42.0,M,,*53"))) {
        json_object_put(obj);
        return NULL;
    }

    json_object_object_add(obj, "nmea_string", obj_add);

    return obj;
}
Пример #11
0
int cli_jsonint64(json_object *obj, const char* key, int64_t i)
{
    json_type objty;
    json_object *fpobj;
    if (NULL == obj) {
        cli_dbgmsg("json: no parent object specified to cli_jsonint64\n");
        return CL_ENULLARG;
    }
    objty = json_object_get_type(obj);

    if (objty == json_type_object) {
        if (NULL == key) {
            cli_dbgmsg("json: null string specified as key to cli_jsonint64\n");
            return CL_ENULLARG;
        }
    }
    else if (objty != json_type_array) {
        return CL_EARG;
    }

    fpobj = json_object_new_int64(i);
    if (NULL == fpobj) {
        cli_errmsg("json: no memory for json int object.\n");
        return CL_EMEM;
    }

    if (objty == json_type_object)
        json_object_object_add(obj, key, fpobj);
    else if (objty == json_type_array)
        json_object_array_add(obj, fpobj);

    return CL_SUCCESS;
}
Пример #12
0
json_object* PacketStats::getJSONObject() {
  json_object *my_object;

  my_object = json_object_new_object();

  if(upTo64 > 0) json_object_object_add(my_object, "upTo64", json_object_new_int64(upTo64));
  if(upTo128 > 0) json_object_object_add(my_object, "upTo128", json_object_new_int64(upTo128));
  if(upTo256 > 0) json_object_object_add(my_object, "upTo256", json_object_new_int64(upTo256));
  if(upTo512 > 0) json_object_object_add(my_object, "upTo512", json_object_new_int64(upTo512));
  if(upTo1024 > 0) json_object_object_add(my_object, "upTo1024", json_object_new_int64(upTo1024));
  if(upTo1518 > 0) json_object_object_add(my_object, "upTo1518", json_object_new_int64(upTo1518));
  if(upTo2500 > 0) json_object_object_add(my_object, "upTo2500", json_object_new_int64(upTo2500));
  if(upTo6500 > 0) json_object_object_add(my_object, "upTo6500", json_object_new_int64(upTo6500));
  if(upTo9000 > 0) json_object_object_add(my_object, "upTo9000", json_object_new_int64(upTo9000));
  if(above9000 > 0) json_object_object_add(my_object, "above9000", json_object_new_int64(above9000));
  
  return(my_object);
}
DLL_PUBLIC int result(judgm_manage_info *info,line_result_data *res_data){
    manage_result_info *res_info;
    json_object *jso_item;
    char tpath[PATH_MAX + 1];

    res_info = (manage_result_info*)info->private_data;
    res_info->count++;

    if(res_data->status > res_info->result){
	res_info->result = res_data->status;
    }
    res_info->totalscore += res_data->score;
    res_info->totalruntime += res_data->runtime;
    if(res_data->memory > res_info->maxmemory){
	res_info->maxmemory = res_data->memory;
    }

    jso_item = json_object_new_object();
    json_object_object_add(jso_item,"status",json_object_new_int(res_data->status));
    json_object_object_add(jso_item,"score",json_object_new_double(res_data->score));
    json_object_object_add(jso_item,"runtime",json_object_new_int64(res_data->runtime));
    json_object_object_add(jso_item,"memory",json_object_new_int64(res_data->memory / 1024UL));
    if(strlen(res_data->err_msg) > 0){
	printf("  strlen %d\n",strlen(res_data->err_msg));
	json_object_object_add(jso_item,"errmsg",json_object_new_string(res_data->err_msg));
    }
    json_object_array_put_idx(res_info->jso_resarray,res_data->id - 1,jso_item);
    
    printf("jmod count %d %d\n",res_info->count,res_info->allcount);

    if(res_info->count == res_info->allcount){
	snprintf(tpath,sizeof(tpath),"%s/result",info->res_path);
	json_object_to_file_ext(tpath,res_info->jso_res,JSON_C_TO_STRING_PLAIN);

	info->result = res_info->result;
	info->score = res_info->totalscore;
	info->runtime = res_info->totalruntime;
	info->memory = res_info->maxmemory;
	
	delete res_info;
	return 1;
    }
    return 0;
}
json_object* container_to_json_native_object_array(ArrayContainer container) {
  int i;
  json_object* ret = json_object_new_array();
  void** data = (void**)container.data;

  for (i = 0; i < container.num; ++i)
    json_object_array_add(ret, json_object_new_int64((int64_t)data[i]));
  g_free(container.data);
  return ret;
}
Пример #15
0
/**
 * Add new key - value pair to json object.
 * @param osName  Key name.
 * @param nValue Long value.
 *
 * @since GDAL 2.3
 */
void CPLJSONObject::Add(const std::string &osName, GInt64 nValue)
{
    std::string objectName;
    CPLJSONObject object = GetObjectByPath( osName, objectName );
    if( object.IsValid() &&
        json_object_get_type(TO_JSONOBJ(object.m_poJsonObject)) ==
            json_type_object )
    {
        json_object *poVal = json_object_new_int64( static_cast<int64_t>(nValue) );
        json_object_object_add( TO_JSONOBJ(object.GetInternalHandle()),
                                           objectName.c_str(), poVal );
    }
}
Пример #16
0
int api_notification_list(ONION_FUNC_PROTO_STR)
{
	const char * userid = onion_request_get_query(req, "userid");
	const char * appkey = onion_request_get_query(req, "appkey");
	const char * sessid = onion_request_get_query(req, "sessid");

	if (userid == NULL || sessid == NULL || appkey == NULL) {
		return api_error(p, req, res, API_RT_WRONGPARAM);
	}

	struct userec *ue = getuser(userid);
	if (ue == 0) {
		return api_error(p, req, res, API_RT_NOSUCHUSER);
	}

	int r = check_user_session(ue, sessid, appkey);
	if (r != API_RT_SUCCESSFUL) {
		free(ue);
		return api_error(p, req, res, r);
	}

	struct json_object *obj = json_tokener_parse("{\"errcode\": 0, \"notifications\": []}");
	struct json_object *noti_array = json_object_object_get(obj, "notifications");
	NotifyItemList allNotifyItems = parse_notification(ue->userid);
	struct json_object * item = NULL;
	struct NotifyItem * currItem;
	struct boardmem *b;
	for (currItem = (struct NotifyItem *)allNotifyItems; currItem != NULL; currItem = currItem->next) {
		item = json_object_new_object();
		json_object_object_add(item, "board", json_object_new_string(currItem->board));
		json_object_object_add(item, "noti_time", json_object_new_int64(currItem->noti_time));
		json_object_object_add(item, "from_userid", json_object_new_string(currItem->from_userid));
		json_object_object_add(item, "title", json_object_new_string(currItem->title_utf));
		json_object_object_add(item, "type", json_object_new_int(currItem->type));
		b = getboardbyname(currItem->board);
		json_object_object_add(item, "secstr", json_object_new_string(b->header.sec1));

		json_object_array_add(noti_array, item);
	}

	free_notification(allNotifyItems);

	api_set_json_header(res);
	onion_response_write0(res, json_object_to_json_string(obj));
	json_object_put(obj);
	free(ue);

	return OCS_PROCESSED;
}
Пример #17
0
/*
* 2.1.2	上传DTS日志
*返回类型: unsigned char*
*返回值: json格式数据
*author  mleaf_hexi
*mail:[email protected]
*/
static unsigned char* reportDeviceLog(void) 
{
	char struuidget[36];
	int L = 0;
	int n;
	unsigned char*reportDeviceLog_string;
	unsigned char DeviceLog_buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 +
								  LWS_SEND_BUFFER_POST_PADDING];
	json_object *mainjson=json_object_new_object();
	json_object *header=json_object_new_object();
	json_object *action=json_object_new_object();
	json_object *data=json_object_new_object();
	
	
	json_object *action1=json_object_new_array();
	json_object *requestId=json_object_new_array();
	json_object_array_add(action1,json_object_new_string("REPORT_DTS_DEVICE_LOG"));
	uuidget(struuidget);//读取UUID到struuidget
	printf("uuidget = %s\n", struuidget);
	json_object_array_add(requestId,json_object_new_string(struuidget));//获取UUID写入requestId
	json_object_object_add(action,"action",action1);
	json_object_object_add(action,"requestId",requestId);
	json_object_object_add(mainjson,"header",action);


	json_object_object_add(data,
	   "fromDatetime",json_object_new_int64(9223372036854775807));
	json_object_object_add(data,
	   "toDatetime",json_object_new_int(39));
	json_object_object_add(data,
	   "logData",json_object_new_string("Logdatafromdtsdevice1asdfaew"));
	json_object_object_add(mainjson,"data",data);

	
	const unsigned char *str=json_object_to_json_string(mainjson);
	printf("%s\n",str);
	for (n = 0; n < 1; n++)//转换成libwebsocket 可以发送的数据
		   L += sprintf((char *)&DeviceLog_buf[LWS_SEND_BUFFER_PRE_PADDING + L],"%s",json_object_to_json_string(mainjson));
	reportDeviceLog_string=&DeviceLog_buf[LWS_SEND_BUFFER_PRE_PADDING];
	json_object_put(action);//在程序最后释放资源
	json_object_put(action1);
	json_object_put(header);
	json_object_put(requestId);
	json_object_put(data);
	json_object_put(mainjson);
	
	return reportDeviceLog_string;
	
}
Пример #18
0
json_object * kvspath_request_json (int64_t id)
{
    json_object *o = json_object_new_object ();
    json_object *ar = json_object_new_array ();
    json_object *v = json_object_new_int64 (id);

    if (!o || !ar || !v) {
        Jput (o);
        Jput (ar);
        Jput (v);
        return (NULL);
    }
    json_object_array_add (ar, v);
    json_object_object_add (o, "ids", ar);
    return (o);
}
Пример #19
0
json_object* CategoryStats::getJSONObject() {
  json_object *my_object;
  
  my_object = json_object_new_object();

  for(int id=0; id<ntop->get_flashstart()->getNumCategories(); id++) {
    if(categories[id] > 0) {
      char *name = ntop->get_flashstart()->getCategoryName(id);
      
      json_object_object_add(my_object, name, 
			     json_object_new_int64(categories[id]));
    }
  }
  
  return(my_object);
}
Пример #20
0
static rsRetVal
addCtrForReporting(json_object *to, const uchar* field_name, intctr_t value) {
	json_object *v;
	DEFiRet;

	/*We should migrate libfastjson to support uint64_t in addition to int64_t.
	  Although no counter is likely to grow to int64 max-value, this is theoritically
	  incorrect (as intctr_t is uint64)*/
	CHKmalloc(v = json_object_new_int64((int64_t) value));

	json_object_object_add(to, (const char*) field_name, v);
finalize_it:
	/* v cannot be NULL in error case, as this would only happen during malloc fail,
	 * which itself sets it to NULL -- so not doing cleanup here.
	 */
	RETiRet;
}
Пример #21
0
//! Builds a basic sensor output json object. Returns the newly allocated json
json_object *build_sensor_output(const char *sensor_name, const char *description, time_t timestamp, int ttl, json_object *tags, json_object *values) {
    json_object *event = json_object_new_object();
    if(sensor_name != NULL) {
        json_object_object_add(event, SENSOR_NAME_KEY, json_object_new_string(sensor_name));
    }
    if(description != NULL) {
        json_object_object_add(event, SENSOR_DESCRIPTION_KEY, json_object_new_string(description));
    }
    json_object_object_add(event, SENSOR_TIMESTAMP_KEY, json_object_new_int64((long)timestamp));
    json_object_object_add(event, SENSOR_TTL_KEY, json_object_new_int(ttl));
    json_object_object_add(event, SENSOR_TAGS_KEY, tags);
    //copy the values
    const char *tmp_data = json_object_to_json_string(values);
    LOGTRACE("Building sensor output from %s\n", tmp_data);
    json_object *sensor_data = json_tokener_parse(tmp_data);
    json_object_object_add(event, SENSOR_DATA_KEY, sensor_data);
    return event;
}
Пример #22
0
static
void processor_setup_allocated_memory(processor_state_t *self, json_object *request)
{
    json_object *allocated_memory_obj;
    if (json_object_object_get_ex(request, "allocated_memory", &allocated_memory_obj))
        return;
    json_object *allocated_objects_obj;
    if (!json_object_object_get_ex(request, "allocated_objects", &allocated_objects_obj))
        return;
    json_object *allocated_bytes_obj;
    if (json_object_object_get_ex(request, "allocated_bytes", &allocated_bytes_obj)) {
        long allocated_objects = json_object_get_int64(allocated_objects_obj);
        long allocated_bytes = json_object_get_int64(allocated_bytes_obj);
        // assume 64bit ruby
        long allocated_memory = allocated_bytes + allocated_objects * 40;
        json_object_object_add(request, "allocated_memory", json_object_new_int64(allocated_memory));
        // printf("[D] allocated memory: %lu\n", allocated_memory);
    }
}
Пример #23
0
static rsRetVal
addCtrForReporting(json_object *to, const uchar* field_name, intctr_t value) {
	json_object *v = NULL;
	DEFiRet;

	/*We should migrate libfastjson to support uint64_t in addition to int64_t.
	  Although no counter is likely to grow to int64 max-value, this is theoritically
	  incorrect (as intctr_t is uint64)*/
	CHKmalloc(v = json_object_new_int64((int64_t) value));

	json_object_object_add(to, (const char*) field_name, v);
finalize_it:
	if (iRet != RS_RET_OK) {
		if (v != NULL) {
			json_object_put(v);
		}
	}
	RETiRet;
}
Пример #24
0
json_object* LocalTrafficStats::getJSONObject() {
  json_object *my_object;
  json_object *my_stats;

  my_object = json_object_new_object();

  my_stats = json_object_new_object();
  if(packets.local2local > 0) json_object_object_add(my_object, "local2local", json_object_new_int64(packets.local2local));
  if(packets.local2remote > 0) json_object_object_add(my_object, "local2remote", json_object_new_int64(packets.local2remote));
  if(packets.remote2local > 0) json_object_object_add(my_object, "remote2local", json_object_new_int64(packets.remote2local));
  if(packets.remote2remote > 0) json_object_object_add(my_object, "remote2remote", json_object_new_int64(packets.remote2remote));
  json_object_object_add(my_object, "packets", my_stats);
  
  my_stats = json_object_new_object();
  if(bytes.local2local > 0) json_object_object_add(my_object, "local2local", json_object_new_int64(bytes.local2local));
  if(bytes.local2remote > 0) json_object_object_add(my_object, "local2remote", json_object_new_int64(bytes.local2remote));
  if(bytes.remote2local > 0) json_object_object_add(my_object, "remote2local", json_object_new_int64(bytes.remote2local));
  if(bytes.remote2remote > 0) json_object_object_add(my_object, "remote2remote", json_object_new_int64(bytes.remote2remote));
  json_object_object_add(my_object, "bytes", my_stats);
  
  return(my_object);
}
/**
 *  Append a value to the array
 *  @param  value
 */
void Array::append(int64_t value)
{
    // add property
    json_object_array_add(_json, json_object_new_int64(value));
}
Пример #26
0
uint8_t* ss_metadata_prepare_sflow(
    const char* source, const char* rule, nn_queue_t* nn_queue,
    sflow_sample_t* sample, ss_ioc_entry_t* iptr) {
    int          irv;
    json_object* jobject = NULL;
    json_object* item    = NULL;
    uint8_t*     rv      = NULL;
    uint8_t*     jstring = NULL;

    jobject = json_object_new_object();
    if (jobject == NULL) {
        RTE_LOG(ERR, EXTRACTOR, "could not allocate sflow json object\n");
        goto error_out;
    }

    item = json_object_new_string(source);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "source", item);

    item = json_object_new_int64((int64_t) __sync_add_and_fetch(&nn_queue->tx_messages, 1));
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "seq_num", item);

    irv = ss_metadata_prepare_sflow_ip(jobject, "source_ip", &sample->source_ip);
    if (irv) goto error_out;
    irv = ss_metadata_prepare_sflow_ip(jobject, "agent_ip", &sample->agent_ip);
    if (irv) goto error_out;

    item = json_object_new_int((int32_t) sample->agent_sub_id);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "agent_sub_id", item);

    item = json_object_new_int((int32_t) sample->packet_seq_num);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "packet_seq_num", item);

    item = json_object_new_double(sample->sys_up_time / 1000.0);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "sys_up_time", item);

    item = json_object_new_string(sflow_sample_type_dump(sample->sample_type));
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "sample_type", item);
    
    item = json_object_new_string(sflow_sample_format_dump(sample->sample_type, sample->data_format));
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "sample_format", item);

    item = json_object_new_int((int32_t) sample->sample_seq_num);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "sample_seq_num", item);

    item = json_object_new_string(sflow_ds_type_dump(sample->ds_type));
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "ds_type", item);

    item = json_object_new_int((int32_t) sample->ds_index);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "ds_index", item);

    item = json_object_new_int((int32_t) sample->sample_rate);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "sample_rate", item);

    item = json_object_new_int((int32_t) sample->sample_pool);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "sample_pool", item);

    item = json_object_new_int((int32_t) sample->drop_count);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "drop_count", item);

    item = json_object_new_string(sflow_port_id_dump(sample->input_port_format, sample->input_port));
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "input_port", item);

    item = json_object_new_string(sflow_port_id_dump(sample->output_port_format, sample->output_port));
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "output_port", item);

    item = json_object_new_string(sflow_header_protocol_dump(sample->header.protocol));
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "sample_protocol", item);
    
    item = json_object_new_int((int32_t) sample->header.packet_size);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "packet_length", item);
    
    item = json_object_new_int((int32_t) sample->header.stripped_size);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "stripped_length", item);
    
    item = json_object_new_int((int32_t) sample->header.header_size);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "header_length", item);

    item = json_object_new_int((int32_t) sample->eth_type);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "eth_type", item);

    irv = ss_metadata_prepare_sflow_mac(jobject, "smac", (uint8_t*) &sample->src_eth);
    if (irv) goto error_out;

    irv = ss_metadata_prepare_sflow_mac(jobject, "dmac", (uint8_t*) &sample->src_eth);
    if (irv) goto error_out;

    item = json_object_new_int((int32_t) sample->rx_vlan);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "rx_vlan", item);
    item = json_object_new_int((int32_t) sample->tx_vlan);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "tx_vlan", item);
    
    irv = ss_metadata_prepare_sflow_ip(jobject, "sip", &sample->src_ip);
    if (irv == -1) goto error_out;
    irv = ss_metadata_prepare_sflow_ip(jobject, "dip", &sample->dst_ip);
    if (irv == -1) goto error_out;
    irv = ss_metadata_prepare_sflow_ip(jobject, "natsip", &sample->nat_src_ip);
    if (irv == -1) goto error_out;
    irv = ss_metadata_prepare_sflow_ip(jobject, "natdip", &sample->nat_dst_ip);
    if (irv == -1) goto error_out;

    item = json_object_new_int((int32_t) sample->ip_protocol);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "ip_protocol", item);

    item = json_object_new_int((int32_t) sample->ip_tot_len);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "ip_length", item);

    item = json_object_new_int((int32_t) sample->ip_ttl);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "ttl", item);

    // XXX: fix this field
    item = json_object_new_int((int32_t) sample->udp_len);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "l4_length", item);

    if (sample->ip_protocol == IPPROTO_ICMP || sample->ip_protocol == IPPROTO_ICMPV6) {
        item = json_object_new_int((int32_t) sample->src_port);
        if (item == NULL) goto error_out;
        json_object_object_add(jobject, "icmp_type", item);

        item = json_object_new_int((int32_t) sample->dst_port);
        if (item == NULL) goto error_out;
        json_object_object_add(jobject, "icmp_code", item);
    }
    else {
        item = json_object_new_int((int32_t) sample->src_port);
        if (item == NULL) goto error_out;
        json_object_object_add(jobject, "sport", item);

        item = json_object_new_int((int32_t) sample->dst_port);
        if (item == NULL) goto error_out;
        json_object_object_add(jobject, "dport", item);
    }

    item = json_object_new_int((int32_t) sample->nat_src_port);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "natsport", item);

    item = json_object_new_int((int32_t) sample->nat_dst_port);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "natdport", item);

    if (sample->ip_protocol == IPPROTO_TCP) {
        item = json_object_new_int((int32_t) sample->tcp_flags);
        if (item == NULL) goto error_out;
        json_object_object_add(jobject, "tcp_flags", item);
    }

    if (sample->src_user[0]) {
        item = json_object_new_string(sample->src_user);
        if (item == NULL) goto error_out;
        json_object_object_add(jobject, "src_user", item);
    }
    
    if (sample->dst_user[0]) {
        item = json_object_new_string(sample->dst_user);
        if (item == NULL) goto error_out;
        json_object_object_add(jobject, "dst_user", item);
    }

    if (iptr) {
        irv = ss_metadata_prepare_ioc(source, rule, nn_queue, iptr, jobject);
        if (irv) goto error_out;
    }

    item = NULL;

    // XXX: NOTE: String pointer is internal to JSON object.
    jstring = (uint8_t*) json_object_to_json_string_ext(jobject, JSON_C_TO_STRING_SPACED);
    rv = (uint8_t*) je_strdup((char*)jstring);
    if (!rv) goto error_out;

    json_object_put(jobject); jobject = NULL;
    
    return rv;
    
    error_out:
    fprintf(stderr, "could not serialize sflow metadata\n");
    if (rv)      { je_free(rv); rv = NULL; }
    if (jobject) { json_object_put(jobject); jobject = NULL; }
    if (item)    { json_object_put(item);    item = NULL;    }

    return NULL;
}
Пример #27
0
json_object* OGRGeoJSONWriteFeature( OGRFeature* poFeature, int bWriteBBOX, int nCoordPrecision )
{
    CPLAssert( NULL != poFeature );

    json_object* poObj = json_object_new_object();
    CPLAssert( NULL != poObj );

    json_object_object_add( poObj, "type",
                            json_object_new_string("Feature") );

/* -------------------------------------------------------------------- */
/*      Write FID if available                                          */
/* -------------------------------------------------------------------- */
    if ( poFeature->GetFID() != OGRNullFID )
    {
        json_object_object_add( poObj, "id",
                                json_object_new_int64(poFeature->GetFID()) );
    }

/* -------------------------------------------------------------------- */
/*      Write feature attributes to GeoJSON "properties" object.        */
/* -------------------------------------------------------------------- */
    json_object* poObjProps = NULL;

    poObjProps = OGRGeoJSONWriteAttributes( poFeature );
    json_object_object_add( poObj, "properties", poObjProps );

/* -------------------------------------------------------------------- */
/*      Write feature geometry to GeoJSON "geometry" object.            */
/*      Null geometries are allowed, according to the GeoJSON Spec.     */
/* -------------------------------------------------------------------- */
    json_object* poObjGeom = NULL;

    OGRGeometry* poGeometry = poFeature->GetGeometryRef();
    if ( NULL != poGeometry )
    {
        poObjGeom = OGRGeoJSONWriteGeometry( poGeometry, nCoordPrecision );

        if ( bWriteBBOX && !poGeometry->IsEmpty() )
        {
            OGREnvelope3D sEnvelope;
            poGeometry->getEnvelope(&sEnvelope);

            json_object* poObjBBOX = json_object_new_array();
            json_object_array_add(poObjBBOX,
                            json_object_new_double_with_precision(sEnvelope.MinX, nCoordPrecision));
            json_object_array_add(poObjBBOX,
                            json_object_new_double_with_precision(sEnvelope.MinY, nCoordPrecision));
            if (poGeometry->getCoordinateDimension() == 3)
                json_object_array_add(poObjBBOX,
                            json_object_new_double_with_precision(sEnvelope.MinZ, nCoordPrecision));
            json_object_array_add(poObjBBOX,
                            json_object_new_double_with_precision(sEnvelope.MaxX, nCoordPrecision));
            json_object_array_add(poObjBBOX,
                            json_object_new_double_with_precision(sEnvelope.MaxY, nCoordPrecision));
            if (poGeometry->getCoordinateDimension() == 3)
                json_object_array_add(poObjBBOX,
                            json_object_new_double_with_precision(sEnvelope.MaxZ, nCoordPrecision));

            json_object_object_add( poObj, "bbox", poObjBBOX );
        }
    }
    
    json_object_object_add( poObj, "geometry", poObjGeom );

    return poObj;
}
Пример #28
0
json_object* OGRGeoJSONWriteAttributes( OGRFeature* poFeature )
{
    CPLAssert( NULL != poFeature );

    json_object* poObjProps = json_object_new_object();
    CPLAssert( NULL != poObjProps );

    OGRFeatureDefn* poDefn = poFeature->GetDefnRef();
    for( int nField = 0; nField < poDefn->GetFieldCount(); ++nField )
    {
        json_object* poObjProp;
        OGRFieldDefn* poFieldDefn = poDefn->GetFieldDefn( nField );
        CPLAssert( NULL != poFieldDefn );
        OGRFieldType eType = poFieldDefn->GetType();
        OGRFieldSubType eSubType = poFieldDefn->GetSubType();

        if( !poFeature->IsFieldSet(nField) )
        {
            poObjProp = NULL;
        }
        else if( OFTInteger == eType )
        {
            if( eSubType == OFSTBoolean )
                poObjProp = json_object_new_boolean( 
                    poFeature->GetFieldAsInteger( nField ) );
            else
                poObjProp = json_object_new_int( 
                    poFeature->GetFieldAsInteger( nField ) );
        }
        else if( OFTInteger64 == eType )
        {
            if( eSubType == OFSTBoolean )
                poObjProp = json_object_new_boolean( 
                    (json_bool)poFeature->GetFieldAsInteger64( nField ) );
            else
                poObjProp = json_object_new_int64( 
                    poFeature->GetFieldAsInteger64( nField ) );
        }
        else if( OFTReal == eType )
        {
            poObjProp = json_object_new_double( 
                poFeature->GetFieldAsDouble(nField) );
        }
        else if( OFTString == eType )
        {
            poObjProp = json_object_new_string( 
                poFeature->GetFieldAsString(nField) );
        }
        else if( OFTIntegerList == eType )
        {
            int nSize = 0;
            const int* panList = poFeature->GetFieldAsIntegerList(nField, &nSize);
            poObjProp = json_object_new_array();
            for(int i=0;i<nSize;i++)
            {
                if( eSubType == OFSTBoolean )
                    json_object_array_add(poObjProp,
                            json_object_new_boolean(panList[i]));
                else
                    json_object_array_add(poObjProp,
                            json_object_new_int(panList[i]));
            }
        }
        else if( OFTInteger64List == eType )
        {
            int nSize = 0;
            const GIntBig* panList = poFeature->GetFieldAsInteger64List(nField, &nSize);
            poObjProp = json_object_new_array();
            for(int i=0;i<nSize;i++)
            {
                if( eSubType == OFSTBoolean )
                    json_object_array_add(poObjProp,
                            json_object_new_boolean((json_bool)panList[i]));
                else
                    json_object_array_add(poObjProp,
                            json_object_new_int64(panList[i]));
            }
        }
        else if( OFTRealList == eType )
        {
            int nSize = 0;
            const double* padfList = poFeature->GetFieldAsDoubleList(nField, &nSize);
            poObjProp = json_object_new_array();
            for(int i=0;i<nSize;i++)
            {
                json_object_array_add(poObjProp,
                            json_object_new_double(padfList[i]));
            }
        }
        else if( OFTStringList == eType )
        {
            char** papszStringList = poFeature->GetFieldAsStringList(nField);
            poObjProp = json_object_new_array();
            for(int i=0; papszStringList && papszStringList[i]; i++)
            {
                json_object_array_add(poObjProp,
                            json_object_new_string(papszStringList[i]));
            }
        }
        else
        {
            poObjProp = json_object_new_string( 
                 poFeature->GetFieldAsString(nField) );
        }

        json_object_object_add( poObjProps,
                                poFieldDefn->GetNameRef(),
                                poObjProp );
    }

    return poObjProps;
}
Пример #29
0
static struct json_object *region_to_json(struct ndctl_region *region)
{
	struct json_object *jregion = json_object_new_object();
	struct json_object *jobj, *jmappings = NULL;
	struct ndctl_interleave_set *iset;
	struct ndctl_mapping *mapping;

	if (!jregion)
		return NULL;

	jobj = json_object_new_string(ndctl_region_get_devname(region));
	if (!jobj)
		goto err;
	json_object_object_add(jregion, "dev", jobj);

	jobj = json_object_new_int64(ndctl_region_get_size(region));
	if (!jobj)
		goto err;
	json_object_object_add(jregion, "size", jobj);

	jobj = json_object_new_int64(ndctl_region_get_available_size(region));
	if (!jobj)
		goto err;
	json_object_object_add(jregion, "available_size", jobj);

	switch (ndctl_region_get_type(region)) {
	case ND_DEVICE_REGION_PMEM:
		jobj = json_object_new_string("pmem");
		break;
	case ND_DEVICE_REGION_BLK:
		jobj = json_object_new_string("blk");
		break;
	default:
		jobj = NULL;
	}
	if (!jobj)
		goto err;
	json_object_object_add(jregion, "type", jobj);

	iset = ndctl_region_get_interleave_set(region);
	if (iset) {
		jobj = json_object_new_int64(
				ndctl_interleave_set_get_cookie(iset));
		if (!jobj)
			fail("\n");
		else
			json_object_object_add(jregion, "iset_id", jobj);
	}

	ndctl_mapping_foreach(region, mapping) {
		struct ndctl_dimm *dimm = ndctl_mapping_get_dimm(mapping);
		struct json_object *jmapping;

		if (!list.dimms)
			break;

		if (!list.idle && !ndctl_dimm_is_enabled(dimm))
			continue;

		if (!jmappings) {
			jmappings = json_object_new_array();
			if (!jmappings) {
				fail("\n");
				continue;
			}
			json_object_object_add(jregion, "mappings", jmappings);
		}

		jmapping = util_mapping_to_json(mapping);
		if (!jmapping) {
			fail("\n");
			continue;
		}
		json_object_array_add(jmappings, jmapping);
	}

	if (!ndctl_region_is_enabled(region)) {
		jobj = json_object_new_string("disabled");
		if (!jobj)
			goto err;
		json_object_object_add(jregion, "state", jobj);
	}

	list_namespaces(region, jregion, NULL, false);
	return jregion;
 err:
	fail("\n");
	json_object_put(jregion);
	return NULL;
}
Пример #30
0
static struct json_object *create_fake_conn_obj(uint64_t l3_id, uint64_t l4_id,
        uint8_t event_param, char *event_value_str, uint64_t tstamp)
{
	struct json_object *obj = NULL, *obj_add = NULL;
    uint8_t rand_value = 0;
    uint64_t rand_value_64 = 0;

	if (!(obj = json_object_new_object()))
		return NULL;

	if (!(obj_add = json_object_new_int64((int64_t) tstamp))) {
        json_object_put(obj);
        return NULL;
    }
    json_object_object_add(obj, "timestamp", obj_add);

    if (l3_id)
        rand_value_64 = l3_id;
    else
        rand_value_64 = (uint64_t) random();

    if (!(obj_add = json_object_new_int64(rand_value_64))) {
        json_object_put(obj);
        return NULL;
    }
    json_object_object_add(obj, "l3_session_id", obj_add);

    if (l4_id)
        rand_value_64 = l4_id;
    else
        rand_value_64 = (uint64_t) random();

    if (!(obj_add = json_object_new_int64(rand_value_64))) {
        json_object_put(obj);
        return NULL;
    }
    json_object_object_add(obj, "l4_session_id", obj_add);

    if (!(obj_add = json_object_new_int(META_TYPE_CONNECTION))) {
        json_object_put(obj);
        return NULL;
    }
    json_object_object_add(obj, "event_type", obj_add);

    rand_value = (uint8_t) random();
    if (!(obj_add = json_object_new_int(rand_value))) {
        json_object_put(obj);
        return NULL;
    }
    json_object_object_add(obj, "interface_type", obj_add);

    rand_value = (uint8_t) random();
    if (!(obj_add = json_object_new_int(rand_value))) {
        json_object_put(obj);
        return NULL;
    }
    json_object_object_add(obj, "network_address_family", obj_add);

    if (!event_param)
        rand_value = (uint8_t) random();
    else
        rand_value = event_param;

    if (!(obj_add = json_object_new_int(rand_value))) {
        json_object_put(obj);
        return NULL;
    }
    json_object_object_add(obj, "event_param", obj_add);

    if (event_value_str) {
        if (!(obj_add = json_object_new_string(event_value_str))) {
            json_object_put(obj);
            return NULL;
        }
        json_object_object_add(obj, "event_value_str", obj_add);
    } else {
        rand_value = (uint8_t) random();
        if (!(obj_add = json_object_new_int(rand_value))) {
            json_object_put(obj);
            return NULL;
        }
        json_object_object_add(obj, "event_value", obj_add);
    }

    rand_value = (uint8_t) random();
    if (!(obj_add = json_object_new_int(rand_value))) {
        json_object_put(obj);
        return NULL;
    }
    json_object_object_add(obj, "interface_id_type", obj_add);

    if (!(obj_add = json_object_new_string("89470000140710276612"))) {
        json_object_put(obj);
        return NULL;
    }
    json_object_object_add(obj, "interface_id", obj_add);

    if (!(obj_add = json_object_new_string("192.168.0.153/24"))) {
        json_object_put(obj);
        return NULL;
    }
    json_object_object_add(obj, "network_address", obj_add);

    rand_value = (uint8_t) random();
    if (rand_value % 2) {
        obj_add = json_object_new_int(24201);

        if (!obj_add) {
            json_object_put(obj);
            return NULL;
        }

        json_object_object_add(obj, "network_provider", obj_add);
    }

    if (!(obj_add = json_object_new_int(-99))) {
        json_object_put(obj);
        return NULL;
    }
    json_object_object_add(obj, "signal_strength", obj_add);

	return obj;	
}