Exemple #1
0
int ast_json_object_update_missing(struct ast_json *object, struct ast_json *other)
{
#if JANSSON_VERSION_HEX >= 0x020300
	return json_object_update_missing((json_t *)object, (json_t *)other);
#else
	struct ast_json_iter *iter = ast_json_object_iter(other);
	int ret = 0;

	if (object == NULL || other == NULL) {
		return -1;
	}

	while (iter != NULL && ret == 0) {
		const char *key = ast_json_object_iter_key(iter);

		if (ast_json_object_get(object, key) == NULL) {
			struct ast_json *value = ast_json_object_iter_value(iter);

			if (!value || ast_json_object_set(object, key, ast_json_ref(value))) {
				ret = -1;
			}
		}
		iter = ast_json_object_iter_next(other, iter);
	}
	return ret;
#endif
}
Exemple #2
0
//native json_object_update_missing(Handle:hObj, Handle:hOther);
static cell_t Native_json_object_update_missing(IPluginContext *pContext, const cell_t *params) {
	HandleError err;
	HandleSecurity sec;
	sec.pOwner = NULL;
	sec.pIdentity = myself->GetIdentity();

	// Param 1
	json_t *object;
	Handle_t hndlObject = static_cast<Handle_t>(params[1]);
	if ((err=g_pHandleSys->ReadHandle(hndlObject, htJanssonObject, &sec, (void **)&object)) != HandleError_None)
    {
        return pContext->ThrowNativeError("Invalid <Object> handle %x (error %d)", hndlObject, err);
    }

	// Param 2
	json_t *other;
	Handle_t hndlOther = static_cast<Handle_t>(params[2]);
	if ((err=g_pHandleSys->ReadHandle(hndlOther, htJanssonObject, &sec, (void **)&other)) != HandleError_None)
    {
        return pContext->ThrowNativeError("Invalid <Object> handle %x (error %d)", hndlOther, err);
    }

	bool bSuccess = (json_object_update_missing(object, other) == 0);
	return bSuccess;
}
Exemple #3
0
static void test_conditional_updates()
{
    json_t *object, *other;

    object = json_pack("{sisi}", "foo", 1, "bar", 2);
    other = json_pack("{sisi}", "foo", 3, "baz", 4);

    if(json_object_update_existing(object, other))
        fail("json_object_update_existing failed");

    if(json_object_size(object) != 2)
        fail("json_object_update_existing added new items");

    if(json_integer_value(json_object_get(object, "foo")) != 3)
        fail("json_object_update_existing failed to update existing key");

    if(json_integer_value(json_object_get(object, "bar")) != 2)
        fail("json_object_update_existing updated wrong key");

    json_decref(object);

    object = json_pack("{sisi}", "foo", 1, "bar", 2);

    if(json_object_update_missing(object, other))
        fail("json_object_update_missing failed");

    if(json_object_size(object) != 3)
        fail("json_object_update_missing didn't add new items");

    if(json_integer_value(json_object_get(object, "foo")) != 1)
        fail("json_object_update_missing updated existing key");

    if(json_integer_value(json_object_get(object, "bar")) != 2)
        fail("json_object_update_missing updated wrong key");

    if(json_integer_value(json_object_get(object, "baz")) != 4)
        fail("json_object_update_missing didn't add new items");

    json_decref(object);
    json_decref(other);
}
Exemple #4
0
int bgp_peer_log_msg(struct bgp_node *route, struct bgp_info *ri, safi_t safi, char *event_type, int output)
{
  char log_rk[SRVBUFLEN];
  struct bgp_peer *peer = ri->peer;
  struct bgp_attr *attr = ri->attr;
  int ret = 0, amqp_ret = 0, etype = BGP_LOGDUMP_ET_NONE;

  if (!strcmp(event_type, "dump")) etype = BGP_LOGDUMP_ET_DUMP;
  else if (!strcmp(event_type, "log")) etype = BGP_LOGDUMP_ET_LOG;
  
#ifdef WITH_RABBITMQ
  if ((config.nfacctd_bgp_msglog_amqp_routing_key && etype == BGP_LOGDUMP_ET_LOG) ||
      (config.bgp_table_dump_amqp_routing_key && etype == BGP_LOGDUMP_ET_DUMP))
    p_amqp_set_routing_key(peer->log->amqp_host, peer->log->filename);
#endif

  if (output == PRINT_OUTPUT_JSON) {
#ifdef WITH_JANSSON
    char ip_address[INET6_ADDRSTRLEN];
    json_t *obj = json_object(), *kv;

    char empty[] = "";
    char prefix_str[INET6_ADDRSTRLEN], nexthop_str[INET6_ADDRSTRLEN];
    char *aspath;

    /* no need for seq and timestamp for "dump" event_type */
    if (etype == BGP_LOGDUMP_ET_LOG) {
      kv = json_pack("{sI}", "seq", log_seq);
      json_object_update_missing(obj, kv);
      json_decref(kv);
      bgp_peer_log_seq_increment(&log_seq);

      kv = json_pack("{ss}", "timestamp", log_tstamp_str);
      json_object_update_missing(obj, kv);
      json_decref(kv);
    }

    addr_to_str(ip_address, &peer->addr);
    kv = json_pack("{ss}", "peer_ip_src", ip_address);
    json_object_update_missing(obj, kv);
    json_decref(kv);

    kv = json_pack("{ss}", "event_type", event_type);
    json_object_update_missing(obj, kv);
    json_decref(kv);

    memset(prefix_str, 0, INET6_ADDRSTRLEN);
    prefix2str(&route->p, prefix_str, INET6_ADDRSTRLEN);
    kv = json_pack("{ss}", "ip_prefix", prefix_str);
    json_object_update_missing(obj, kv);
    json_decref(kv);

    memset(nexthop_str, 0, INET6_ADDRSTRLEN);
    if (attr->mp_nexthop.family) addr_to_str(nexthop_str, &attr->mp_nexthop);
    else inet_ntop(AF_INET, &attr->nexthop, nexthop_str, INET6_ADDRSTRLEN);
    kv = json_pack("{ss}", "bgp_nexthop", nexthop_str);
    json_object_update_missing(obj, kv);
    json_decref(kv);

    if (ri && ri->extra && ri->extra->path_id) {
      kv = json_pack("{sI}", "as_path_id", ri->extra->path_id);
      json_object_update_missing(obj, kv);
      json_decref(kv);
    }

    aspath = attr->aspath ? attr->aspath->str : empty;
    kv = json_pack("{ss}", "as_path", aspath);
    json_object_update_missing(obj, kv);
    json_decref(kv);

    if (attr->community) {
      kv = json_pack("{ss}", "comms", attr->community->str);
      json_object_update_missing(obj, kv);
      json_decref(kv);
    }

    if (attr->ecommunity) {
      kv = json_pack("{ss}", "ecomms", attr->ecommunity->str);
      json_object_update_missing(obj, kv);
      json_decref(kv);
    }

    kv = json_pack("{sI}", "origin", attr->origin);
    json_object_update_missing(obj, kv);
    json_decref(kv);

    kv = json_pack("{sI}", "local_pref", attr->local_pref);
    json_object_update_missing(obj, kv);
    json_decref(kv);

    if (attr->med) {
      kv = json_pack("{sI}", "med", attr->med);
      json_object_update_missing(obj, kv);
      json_decref(kv);
    }

    if (safi == SAFI_MPLS_VPN) {
      u_char rd_str[SRVBUFLEN];

      bgp_rd2str(rd_str, &ri->extra->rd);
      kv = json_pack("{ss}", "rd", rd_str);
      json_object_update_missing(obj, kv);
      json_decref(kv);
    }

    if ((config.nfacctd_bgp_msglog_file && etype == BGP_LOGDUMP_ET_LOG) ||
	(config.bgp_table_dump_file && etype == BGP_LOGDUMP_ET_DUMP))
      write_and_free_json(peer->log->fd, obj);

#ifdef WITH_RABBITMQ
    if ((config.nfacctd_bgp_msglog_amqp_routing_key && etype == BGP_LOGDUMP_ET_LOG) ||
	(config.bgp_table_dump_amqp_routing_key && etype == BGP_LOGDUMP_ET_DUMP)) {
      amqp_ret = write_and_free_json_amqp(peer->log->amqp_host, obj);
      p_amqp_unset_routing_key(peer->log->amqp_host);
    }
#endif
#endif
  }

  return (ret | amqp_ret);
}
Exemple #5
0
int bgp_peer_log_close(struct bgp_peer *peer, int output, int type)
{
  char event_type[] = "log_close", peer_ip_src[] = "peer_ip_src", bmp_router[] = "bmp_router";
  struct bgp_peer_log *log_ptr;
  void *amqp_log_ptr;
  int ret = 0, amqp_ret = 0;;

  /* pointers to BGP or BMP vars */
  char *file, *amqp_routing_key, *lts, *pa_str;
  u_int64_t *ls;

  if (type == FUNC_TYPE_BGP) {
    file = config.nfacctd_bgp_msglog_file;
    amqp_routing_key = config.nfacctd_bgp_msglog_amqp_routing_key;

    pa_str = peer_ip_src;
    lts = log_tstamp_str;
    ls = &log_seq;
  }
  else if (type == FUNC_TYPE_BMP) {
    file = config.nfacctd_bmp_msglog_file;
    amqp_routing_key = config.nfacctd_bmp_msglog_amqp_routing_key;

    pa_str = bmp_router;
    lts = bmp_log_tstamp_str;
    ls = &bmp_log_seq;
  }
  else return ret;

  if (!peer || peer->log) return ret;

#ifdef WITH_RABBITMQ
  if (amqp_routing_key)
    p_amqp_set_routing_key(peer->log->amqp_host, peer->log->filename);
#endif

  log_ptr = peer->log;
  amqp_log_ptr = peer->log->amqp_host;

  assert(peer->log->refcnt);
  peer->log->refcnt--;
  peer->log = NULL;

  if (output == PRINT_OUTPUT_JSON) {
#ifdef WITH_JANSSON
    char ip_address[INET6_ADDRSTRLEN];
    json_t *obj = json_object(), *kv;

    kv = json_pack("{sI}", "seq", (*ls));
    json_object_update_missing(obj, kv);
    json_decref(kv);
    bgp_peer_log_seq_increment(ls);

    kv = json_pack("{ss}", "timestamp", lts);
    json_object_update_missing(obj, kv);
    json_decref(kv);

    addr_to_str(ip_address, &peer->addr);
    kv = json_pack("{ss}", pa_str, ip_address);
    json_object_update_missing(obj, kv);
    json_decref(kv);

    kv = json_pack("{ss}", "event_type", event_type);
    json_object_update_missing(obj, kv);
    json_decref(kv);

    if (file)
      write_and_free_json(log_ptr->fd, obj);

#ifdef WITH_RABBITMQ
    if (amqp_routing_key) {
      amqp_ret = write_and_free_json_amqp(amqp_log_ptr, obj);
      p_amqp_unset_routing_key(amqp_log_ptr);
    }
#endif
#endif
  }

  if (!log_ptr->refcnt) {
    if (file && !log_ptr->refcnt) {
      fclose(log_ptr->fd);
      memset(log_ptr, 0, sizeof(struct bgp_peer_log));
    }
  }

  return (ret | amqp_ret);
}
Exemple #6
0
int bgp_peer_log_init(struct bgp_peer *peer, int output, int type)
{
  int peer_idx, have_it, ret = 0, amqp_ret = 0;
  char log_filename[SRVBUFLEN], event_type[] = "log_init";
  char peer_ip_src[] = "peer_ip_src", bmp_router[] = "bmp_router";

  /* pointers to BGP or BMP vars */
  struct bgp_peer_log **bpl;
  char *file, *amqp_routing_key, *lts, *pa_str;
  int amqp_routing_key_rr, max_peers;
  u_int64_t *ls;

  if (type == FUNC_TYPE_BGP) {
    file = config.nfacctd_bgp_msglog_file;
    amqp_routing_key = config.nfacctd_bgp_msglog_amqp_routing_key;
    amqp_routing_key_rr = config.nfacctd_bgp_msglog_amqp_routing_key_rr;
    max_peers = config.nfacctd_bgp_max_peers;
    
    pa_str = peer_ip_src;
    lts = log_tstamp_str;
    ls = &log_seq;
    bpl = &peers_log;
  }
  else if (type == FUNC_TYPE_BMP) {
    file = config.nfacctd_bmp_msglog_file;
    amqp_routing_key = config.nfacctd_bmp_msglog_amqp_routing_key;
    amqp_routing_key_rr = config.nfacctd_bmp_msglog_amqp_routing_key_rr;
    max_peers = config.nfacctd_bmp_max_peers;

    pa_str = bmp_router;
    lts = bmp_log_tstamp_str;
    ls = &bmp_log_seq;
    bpl = &bmp_peers_log;
  }
  else if (type == FUNC_TYPE_SFLOW_COUNTER) {
    file = config.sfacctd_counter_file;
    amqp_routing_key = NULL; /* AMQP not supported */
    amqp_routing_key_rr = 0; /* AMQP not supported */
    max_peers = config.sfacctd_counter_max_nodes;

    pa_str = peer_ip_src;
    lts = sf_cnt_log_tstamp_str;
    ls = &sf_cnt_log_seq;
    bpl = &sf_cnt_log;
  }
  else return ret;

  if (!(*bpl) || !peer || peer->log) return ret;

  if (file)
    bgp_peer_log_dynname(log_filename, SRVBUFLEN, file, peer); 

  if (amqp_routing_key) {
    bgp_peer_log_dynname(log_filename, SRVBUFLEN, amqp_routing_key, peer); 
  }

  for (peer_idx = 0, have_it = 0; peer_idx < max_peers; peer_idx++) {
    if (!(*bpl)[peer_idx].refcnt) {
      if (file)
	(*bpl)[peer_idx].fd = open_logfile(log_filename, "a");

#ifdef WITH_RABBITMQ
      if (amqp_routing_key)
        (*bpl)[peer_idx].amqp_host = &bgp_daemon_msglog_amqp_host;
#endif
      
      strcpy((*bpl)[peer_idx].filename, log_filename);
      have_it = TRUE;
      break;
    }
    else if (!strcmp(log_filename, (*bpl)[peer_idx].filename)) {
      have_it = TRUE;
      break;
    }
  }

  if (have_it) {
    peer->log = &(*bpl)[peer_idx];
    (*bpl)[peer_idx].refcnt++;

#ifdef WITH_RABBITMQ
    if (amqp_routing_key)
      p_amqp_set_routing_key(peer->log->amqp_host, peer->log->filename);

    if (amqp_routing_key_rr && !p_amqp_get_routing_key_rr(peer->log->amqp_host)) {
      p_amqp_init_routing_key_rr(peer->log->amqp_host);
      p_amqp_set_routing_key_rr(peer->log->amqp_host, amqp_routing_key_rr);
    }
#endif

    if (output == PRINT_OUTPUT_JSON) {
#ifdef WITH_JANSSON
      char ip_address[INET6_ADDRSTRLEN];
      json_t *obj = json_object(), *kv;

      kv = json_pack("{sI}", "seq", (*ls));
      json_object_update_missing(obj, kv);
      json_decref(kv);
      bgp_peer_log_seq_increment(ls);

      kv = json_pack("{ss}", "timestamp", lts);
      json_object_update_missing(obj, kv);
      json_decref(kv);

      addr_to_str(ip_address, &peer->addr);
      kv = json_pack("{ss}", pa_str, ip_address);
      json_object_update_missing(obj, kv);
      json_decref(kv);

      kv = json_pack("{ss}", "event_type", event_type);
      json_object_update_missing(obj, kv);
      json_decref(kv);

      if (file)
	write_and_free_json(peer->log->fd, obj);

#ifdef WITH_RABBITMQ
      if (amqp_routing_key) {
	amqp_ret = write_and_free_json_amqp(peer->log->amqp_host, obj); 
	p_amqp_unset_routing_key(peer->log->amqp_host);
      }
#endif
#endif
    }
  }

  return (ret | amqp_ret);
}
Exemple #7
0
int la_codec_object_update_missing(la_codec_value_t *object, la_codec_value_t *other)
{
    return json_object_update_missing((json_t *) object, (json_t *) other);
}