Add support for show peers monitoring request
[trust_router.git] / trp / trp_ptable_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 <trp_peer.h>
37 #include <trp_ptable.h>
38
39 /* this is horribly inefficient but should be ok for small peer tables */
40 char *trp_ptable_to_str(TALLOC_CTX *memctx, TRP_PTABLE *ptbl, const char *sep, const char *lineterm)
41 {
42   TALLOC_CTX *tmpctx=talloc_new(NULL);
43   TRP_PEER *peer=NULL;
44   char *result=talloc_strdup(tmpctx, "");
45
46   if (lineterm==NULL)
47     lineterm="\n";
48
49   /* this leaves intermediate result strings in the tmpctx context, we'll free these when
50    * we're done */
51   for (peer=ptbl->head; peer!=NULL; peer=peer->next)
52     result=talloc_asprintf(tmpctx, "%s%s%s", result, lineterm, trp_peer_to_str(tmpctx, peer, sep));
53
54   talloc_steal(memctx, result); /* hand result over to caller */
55   talloc_free(tmpctx); /* free detritus */
56   return result;
57 }
58
59 json_t *trp_ptable_to_json(TRP_PTABLE *ptbl)
60 {
61   TRP_PTABLE_ITER *iter = trp_ptable_iter_new(NULL);
62   json_t *ptbl_json = json_array();
63   TRP_PEER *peer = trp_ptable_iter_first(iter, ptbl);
64   while(peer) {
65     json_array_append_new(ptbl_json, trp_peer_to_json(peer));
66     peer = trp_ptable_iter_next(iter);
67   }
68   trp_ptable_iter_free(iter);
69   return ptbl_json;
70 }