From 01415bbcacceae7f2fb1286fe8fe2665f4a755bf Mon Sep 17 00:00:00 2001 From: Jennifer Richards Date: Thu, 19 Apr 2018 12:58:10 -0400 Subject: [PATCH] Add support for show peers monitoring request --- common/tr_gss_names.c | 14 +++++++++ include/mon_internal.h | 1 + include/tr_gss_names.h | 2 ++ include/trp_peer.h | 1 + include/trp_ptable.h | 1 + mon/mon_common.c | 4 +++ tr/tr_trp_mons.c | 12 ++++++++ trp/trp_peer_encoders.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++ trp/trp_ptable_encoders.c | 13 ++++++++ 9 files changed, 125 insertions(+) diff --git a/common/tr_gss_names.c b/common/tr_gss_names.c index 12941b3..ef66d31 100644 --- a/common/tr_gss_names.c +++ b/common/tr_gss_names.c @@ -130,3 +130,17 @@ void tr_gss_names_iter_free(TR_GSS_NAMES_ITER *iter) { talloc_free(iter); } + +json_t *gss_names_to_json_array(TR_GSS_NAMES *gss_names) +{ + TR_GSS_NAMES_ITER *iter = tr_gss_names_iter_new(NULL); + json_t *jarray = json_array(); + TR_NAME *name = tr_gss_names_iter_first(iter, gss_names); + while (name) { + json_array_append_new(jarray, tr_name_to_json_string(name)); + name = tr_gss_names_iter_next(iter); + } + tr_gss_names_iter_free(iter); + return jarray; +} + diff --git a/include/mon_internal.h b/include/mon_internal.h index 45eb60b..c58b9d7 100644 --- a/include/mon_internal.h +++ b/include/mon_internal.h @@ -105,6 +105,7 @@ enum mon_opt_type { // Dynamic trust router state OPT_TYPE_SHOW_ROUTES, + OPT_TYPE_SHOW_PEERS, OPT_TYPE_SHOW_COMMUNITIES }; diff --git a/include/tr_gss_names.h b/include/tr_gss_names.h index 676c8e4..33590a3 100644 --- a/include/tr_gss_names.h +++ b/include/tr_gss_names.h @@ -59,4 +59,6 @@ TR_NAME *tr_gss_names_iter_first(TR_GSS_NAMES_ITER *iter, TR_GSS_NAMES *gn); TR_NAME *tr_gss_names_iter_next(TR_GSS_NAMES_ITER *iter); void tr_gss_names_iter_free(TR_GSS_NAMES_ITER *iter); +json_t *gss_names_to_json_array(TR_GSS_NAMES *gss_names); + #endif /* __TR_GSS_H__ */ diff --git a/include/trp_peer.h b/include/trp_peer.h index e0b7416..8886bef 100644 --- a/include/trp_peer.h +++ b/include/trp_peer.h @@ -90,5 +90,6 @@ TR_FILTER *trp_peer_get_filter(TRP_PEER *peer, TR_FILTER_TYPE ftype); /* trp_peer_encoders.c */ char *trp_peer_to_str(TALLOC_CTX *memctx, TRP_PEER *peer, const char *sep); +json_t *trp_peer_to_json(TRP_PEER *peer); #endif //TRUST_ROUTER_TRP_PEER_H diff --git a/include/trp_ptable.h b/include/trp_ptable.h index 43c320b..b26fac1 100644 --- a/include/trp_ptable.h +++ b/include/trp_ptable.h @@ -65,5 +65,6 @@ void trp_ptable_iter_free(TRP_PTABLE_ITER *iter); /* trp_ptable_encoders.c */ char *trp_ptable_to_str(TALLOC_CTX *memctx, TRP_PTABLE *ptbl, const char *sep, const char *lineterm); +json_t *trp_ptable_to_json(TRP_PTABLE *ptbl); #endif /* _TRP_PTABLE_H_ */ diff --git a/mon/mon_common.c b/mon/mon_common.c index 5197958..d97c943 100644 --- a/mon/mon_common.c +++ b/mon/mon_common.c @@ -105,6 +105,9 @@ const char *mon_opt_type_to_string(MON_OPT_TYPE opt_type) case OPT_TYPE_SHOW_ROUTES: return "routes"; + case OPT_TYPE_SHOW_PEERS: + return "peers"; + case OPT_TYPE_SHOW_COMMUNITIES: return "communities"; } @@ -127,6 +130,7 @@ MON_OPT_TYPE mon_opt_type_from_string(const char *s) return_if_matches(s, OPT_TYPE_SHOW_TID_REQ_ERR_COUNT); return_if_matches(s, OPT_TYPE_SHOW_TID_REQ_PENDING); return_if_matches(s, OPT_TYPE_SHOW_ROUTES); + return_if_matches(s, OPT_TYPE_SHOW_PEERS); return_if_matches(s, OPT_TYPE_SHOW_COMMUNITIES); return OPT_TYPE_UNKNOWN; } diff --git a/tr/tr_trp_mons.c b/tr/tr_trp_mons.c index 9c38f0d..d11076e 100644 --- a/tr/tr_trp_mons.c +++ b/tr/tr_trp_mons.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -49,9 +50,20 @@ static MON_RC handle_show_routes(void *cookie, json_t **response_ptr) return (*response_ptr == NULL) ? MON_NOMEM : MON_SUCCESS; } +static MON_RC handle_show_peers(void *cookie, json_t **response_ptr) +{ + TRPS_INSTANCE *trps = talloc_get_type_abort(cookie, TRPS_INSTANCE); + + *response_ptr = trp_ptable_to_json(trps->ptable); + return (*response_ptr == NULL) ? MON_NOMEM : MON_SUCCESS; +} + void tr_trp_register_mons_handlers(TRPS_INSTANCE *trps, MONS_INSTANCE *mons) { mons_register_handler(mons, MON_CMD_SHOW, OPT_TYPE_SHOW_ROUTES, handle_show_routes, trps); + mons_register_handler(mons, + MON_CMD_SHOW, OPT_TYPE_SHOW_PEERS, + handle_show_peers, trps); } diff --git a/trp/trp_peer_encoders.c b/trp/trp_peer_encoders.c index 5798110..f4edbeb 100644 --- a/trp/trp_peer_encoders.c +++ b/trp/trp_peer_encoders.c @@ -33,7 +33,11 @@ */ #include +#include + +#include #include +#include char *trp_peer_to_str(TALLOC_CTX *memctx, TRP_PEER *peer, const char *sep) { @@ -45,3 +49,76 @@ char *trp_peer_to_str(TALLOC_CTX *memctx, TRP_PEER *peer, const char *sep) peer->linkcost); } +/* helper for encoding to json */ +static json_t *server_to_json_string(const char *server, unsigned int port) +{ + char *s = talloc_asprintf(NULL, "%s:%u", server, port); + json_t *jstr = json_string(s); + talloc_free(s); + return jstr; +} + +static json_t *last_attempt_to_json_string(TRP_PEER *peer) +{ + struct timespec ts_zero = {0, 0}; + char *s = NULL; + json_t *jstr = NULL; + + if (tr_cmp_timespec(trp_peer_get_last_conn_attempt(peer), &ts_zero) == 0) { + s = strdup(""); + } else { + s = timespec_to_str(trp_peer_get_last_conn_attempt(peer)); + } + + if (s) { + jstr = json_string(s); + free(s); + } + + return jstr; +} + +/* helper for below */ +#define OBJECT_SET_OR_FAIL(jobj, key, val) \ +do { \ + if (val) \ + json_object_set_new((jobj),(key),(val)); \ + else \ + goto cleanup; \ +} while (0) + +json_t *trp_peer_to_json(TRP_PEER *peer) +{ + json_t *peer_json = NULL; + json_t *retval = NULL; + + peer_json = json_object(); + if (peer_json == NULL) + goto cleanup; + + + OBJECT_SET_OR_FAIL(peer_json, "server", + server_to_json_string(trp_peer_get_server(peer), + trp_peer_get_port(peer))); + OBJECT_SET_OR_FAIL(peer_json, "linkcost", + json_integer(trp_peer_get_linkcost(peer))); + OBJECT_SET_OR_FAIL(peer_json, "connected_to", + json_boolean(trp_peer_get_outgoing_status(peer) == PEER_CONNECTED)); + OBJECT_SET_OR_FAIL(peer_json, "connected_from", + json_boolean(trp_peer_get_incoming_status(peer) == PEER_CONNECTED)); + OBJECT_SET_OR_FAIL(peer_json, "servicename", + tr_name_to_json_string(trp_peer_get_servicename(peer))); + OBJECT_SET_OR_FAIL(peer_json, "allowed_credentials", + gss_names_to_json_array(trp_peer_get_gss_names(peer))); + OBJECT_SET_OR_FAIL(peer_json, "last_connection_attempt", + last_attempt_to_json_string(peer)); + + /* succeeded - set the return value and increment the reference count */ + retval = peer_json; + json_incref(retval); + +cleanup: + if (peer_json) + json_decref(peer_json); + return retval; +} diff --git a/trp/trp_ptable_encoders.c b/trp/trp_ptable_encoders.c index f584c2b..2ff8c9c 100644 --- a/trp/trp_ptable_encoders.c +++ b/trp/trp_ptable_encoders.c @@ -55,3 +55,16 @@ char *trp_ptable_to_str(TALLOC_CTX *memctx, TRP_PTABLE *ptbl, const char *sep, c talloc_free(tmpctx); /* free detritus */ return result; } + +json_t *trp_ptable_to_json(TRP_PTABLE *ptbl) +{ + TRP_PTABLE_ITER *iter = trp_ptable_iter_new(NULL); + json_t *ptbl_json = json_array(); + TRP_PEER *peer = trp_ptable_iter_first(iter, ptbl); + while(peer) { + json_array_append_new(ptbl_json, trp_peer_to_json(peer)); + peer = trp_ptable_iter_next(iter); + } + trp_ptable_iter_free(iter); + return ptbl_json; +} -- 2.1.4