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>
42 char *trp_peer_to_str(TALLOC_CTX *memctx, TRP_PEER *peer, const char *sep)
46 return talloc_asprintf(memctx,
48 peer->server, peer->port, sep,
52 /* helper for encoding to json */
53 static json_t *server_to_json_string(const char *server, unsigned int port)
55 char *s = talloc_asprintf(NULL, "%s:%u", server, port);
56 json_t *jstr = json_string(s);
61 static json_t *last_attempt_to_json_string(TRP_PEER *peer)
63 struct timespec ts_zero = {0, 0};
67 if (tr_cmp_timespec(trp_peer_get_last_conn_attempt(peer), &ts_zero) == 0) {
70 s = timespec_to_str(trp_peer_get_last_conn_attempt(peer));
74 jstr = json_string(s);
81 /* helper for below */
82 #define OBJECT_SET_OR_FAIL(jobj, key, val) \
85 json_object_set_new((jobj),(key),(val)); \
90 json_t *trp_peer_to_json(TRP_PEER *peer)
92 json_t *peer_json = NULL;
93 json_t *retval = NULL;
95 peer_json = json_object();
96 if (peer_json == NULL)
100 OBJECT_SET_OR_FAIL(peer_json, "server",
101 server_to_json_string(trp_peer_get_server(peer),
102 trp_peer_get_port(peer)));
103 OBJECT_SET_OR_FAIL(peer_json, "servicename",
104 tr_name_to_json_string(trp_peer_get_servicename(peer)));
105 OBJECT_SET_OR_FAIL(peer_json, "linkcost",
106 json_integer(trp_peer_get_linkcost(peer)));
107 OBJECT_SET_OR_FAIL(peer_json, "connected_to",
108 json_boolean(trp_peer_get_outgoing_status(peer) == PEER_CONNECTED));
109 OBJECT_SET_OR_FAIL(peer_json, "connected_from",
110 json_boolean(trp_peer_get_incoming_status(peer) == PEER_CONNECTED));
111 OBJECT_SET_OR_FAIL(peer_json, "last_connection_attempt",
112 last_attempt_to_json_string(peer));
113 OBJECT_SET_OR_FAIL(peer_json, "allowed_credentials",
114 gss_names_to_json_array(trp_peer_get_gss_names(peer)));
116 /* succeeded - set the return value and increment the reference count */
122 json_decref(peer_json);