Add support for "show realms" monitoring request
[trust_router.git] / trp / trp_peer_encoders.c
1 /*
2  * Copyright (c) 2016-2018, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
32  *
33  */
34
35 #include <talloc.h>
36 #include <jansson.h>
37
38 #include <tr_gss_names.h>
39 #include <trp_peer.h>
40 #include <tr_util.h>
41
42 char *trp_peer_to_str(TALLOC_CTX *memctx, TRP_PEER *peer, const char *sep)
43 {
44   if (sep==NULL)
45     sep=", ";
46   return talloc_asprintf(memctx,
47                          "%s:%u%s0x%04X",
48                          peer->server, peer->port, sep,
49                          peer->linkcost);
50 }
51
52 /* helper for encoding to json */
53 static json_t *server_to_json_string(const char *server, unsigned int port)
54 {
55   char *s = talloc_asprintf(NULL, "%s:%u", server, port);
56   json_t *jstr = json_string(s);
57   talloc_free(s);
58   return jstr;
59 }
60
61 static json_t *last_attempt_to_json_string(TRP_PEER *peer)
62 {
63   struct timespec ts_zero = {0, 0};
64   char *s = NULL;
65   json_t *jstr = NULL;
66
67   if (tr_cmp_timespec(trp_peer_get_last_conn_attempt(peer), &ts_zero) == 0) {
68     s = strdup("");
69   } else {
70     s = timespec_to_str(trp_peer_get_last_conn_attempt(peer));
71   }
72
73   if (s) {
74     jstr = json_string(s);
75     free(s);
76   }
77
78   return jstr;
79 }
80
81 /* helper for below */
82 #define OBJECT_SET_OR_FAIL(jobj, key, val)     \
83 do {                                           \
84   if (val)                                     \
85     json_object_set_new((jobj),(key),(val));   \
86   else                                         \
87     goto cleanup;                              \
88 } while (0)
89
90 json_t *trp_peer_to_json(TRP_PEER *peer)
91 {
92   json_t *peer_json = NULL;
93   json_t *retval = NULL;
94
95   peer_json = json_object();
96   if (peer_json == NULL)
97     goto cleanup;
98
99
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)));
115
116   /* succeeded - set the return value and increment the reference count */
117   retval = peer_json;
118   json_incref(retval);
119
120 cleanup:
121   if (peer_json)
122     json_decref(peer_json);
123   return retval;
124 }