2 * Copyright (c) 2016-2018, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <tr_gss_names.h>
41 #include <tr_json_util.h>
43 char *trp_peer_to_str(TALLOC_CTX *memctx, TRP_PEER *peer, const char *sep)
47 return talloc_asprintf(memctx,
49 peer->server, peer->port, sep,
53 /* helper for encoding to json */
54 static json_t *server_to_json_string(const char *server, unsigned int port)
56 char *s = talloc_asprintf(NULL, "%s:%u", server, port);
57 json_t *jstr = json_string(s);
62 static json_t *last_attempt_to_json_string(TRP_PEER *peer)
64 struct timespec ts_zero = {0, 0};
68 if (tr_cmp_timespec(trp_peer_get_last_conn_attempt(peer), &ts_zero) > 0) {
69 s = timespec_to_str(trp_peer_get_last_conn_attempt(peer));
72 jstr = json_string(s);
80 json_t *trp_peer_to_json(TRP_PEER *peer)
82 json_t *peer_json = NULL;
83 json_t *retval = NULL;
85 peer_json = json_object();
86 if (peer_json == NULL)
90 OBJECT_SET_OR_FAIL(peer_json, "server",
91 server_to_json_string(trp_peer_get_server(peer),
92 trp_peer_get_port(peer)));
93 OBJECT_SET_OR_FAIL(peer_json, "servicename",
94 tr_name_to_json_string(trp_peer_get_servicename(peer)));
95 OBJECT_SET_OR_FAIL(peer_json, "linkcost",
96 json_integer(trp_peer_get_linkcost(peer)));
97 OBJECT_SET_OR_FAIL(peer_json, "connected_to",
98 json_boolean(trp_peer_get_outgoing_status(peer) == PEER_CONNECTED));
99 OBJECT_SET_OR_FAIL(peer_json, "connected_from",
100 json_boolean(trp_peer_get_incoming_status(peer) == PEER_CONNECTED));
101 OBJECT_SET_OR_FAIL(peer_json, "last_connection_attempt",
102 last_attempt_to_json_string(peer));
103 OBJECT_SET_OR_FAIL(peer_json, "allowed_credentials",
104 tr_gss_names_to_json_array(trp_peer_get_gss_names(peer)));
105 OBJECT_SET_OR_FAIL(peer_json, "filters",
106 tr_filter_set_to_json(peer->filters));
108 /* succeeded - set the return value and increment the reference count */
114 json_decref(peer_json);