e6bf16ac3b17672c1fd3fd3d9822198aafbd11e5
[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   struct timespec *last_conn_attempt;
66   char *s = NULL;
67   json_t *jstr = NULL;
68
69   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));
71
72     if (s) {
73       jstr = json_string(s);
74       free(s);
75     }
76   }
77
78   return jstr;
79 }
80
81 json_t *trp_peer_to_json(TRP_PEER *peer)
82 {
83   json_t *peer_json = NULL;
84   json_t *retval = NULL;
85
86   peer_json = json_object();
87   if (peer_json == NULL)
88     goto cleanup;
89
90
91   OBJECT_SET_OR_FAIL(peer_json, "server",
92                      server_to_json_string(trp_peer_get_server(peer),
93                                            trp_peer_get_port(peer)));
94   OBJECT_SET_OR_FAIL(peer_json, "servicename",
95                      tr_name_to_json_string(trp_peer_get_servicename(peer)));
96   OBJECT_SET_OR_FAIL(peer_json, "linkcost",
97                      json_integer(trp_peer_get_linkcost(peer)));
98   OBJECT_SET_OR_FAIL(peer_json, "connected_to",
99                      json_boolean(trp_peer_get_outgoing_status(peer) == PEER_CONNECTED));
100   OBJECT_SET_OR_FAIL(peer_json, "connected_from",
101                      json_boolean(trp_peer_get_incoming_status(peer) == PEER_CONNECTED));
102   OBJECT_SET_OR_SKIP(peer_json, "last_connection_attempt",
103                      last_attempt_to_json_string(peer));
104   OBJECT_SET_OR_FAIL(peer_json, "allowed_credentials",
105                      tr_gss_names_to_json_array(trp_peer_get_gss_names(peer)));
106   OBJECT_SET_OR_FAIL(peer_json, "filters",
107                      tr_filter_set_to_json(peer->filters));
108
109   /* succeeded - set the return value and increment the reference count */
110   retval = peer_json;
111   json_incref(retval);
112
113 cleanup:
114   if (peer_json)
115     json_decref(peer_json);
116   return retval;
117 }