552f440ff890b0d05e845ed39a4753f42dce637b
[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 #include <tr_json_util.h>
42
43 char *trp_peer_to_str(TALLOC_CTX *memctx, TRP_PEER *peer, const char *sep)
44 {
45   if (sep==NULL)
46     sep=", ";
47   return talloc_asprintf(memctx,
48                          "%s:%u%s0x%04X",
49                          peer->server, peer->port, sep,
50                          peer->linkcost);
51 }
52
53 /* helper for encoding to json */
54 static json_t *server_to_json_string(const char *server, int port)
55 {
56   char *s = talloc_asprintf(NULL, "%s:%u", server, port);
57   json_t *jstr = json_string(s);
58   talloc_free(s);
59   return jstr;
60 }
61
62 static json_t *last_attempt_to_json_string(TRP_PEER *peer)
63 {
64   struct timespec ts_zero = {0, 0};
65   char *s = NULL;
66   json_t *jstr = NULL;
67
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));
70
71     if (s) {
72       jstr = json_string(s);
73       free(s);
74     }
75   }
76
77   return jstr;
78 }
79
80 json_t *trp_peer_to_json(TRP_PEER *peer)
81 {
82   json_t *peer_json = NULL;
83   json_t *retval = NULL;
84
85   peer_json = json_object();
86   if (peer_json == NULL)
87     goto cleanup;
88
89
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_SKIP(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));
107
108   /* succeeded - set the return value and increment the reference count */
109   retval = peer_json;
110   json_incref(retval);
111
112 cleanup:
113   if (peer_json)
114     json_decref(peer_json);
115   return retval;
116 }