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.
37 #include <trp_ptable.h>
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)
42 TALLOC_CTX *tmpctx=talloc_new(NULL);
44 char *result=talloc_strdup(tmpctx, "");
49 /* this leaves intermediate result strings in the tmpctx context, we'll free these when
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));
54 talloc_steal(memctx, result); /* hand result over to caller */
55 talloc_free(tmpctx); /* free detritus */
59 json_t *trp_ptable_to_json(TRP_PTABLE *ptbl)
61 TRP_PTABLE_ITER *iter = trp_ptable_iter_new(NULL);
62 json_t *ptbl_json = json_array();
63 TRP_PEER *peer = NULL;
65 for (peer = trp_ptable_iter_first(iter, ptbl);
67 peer = trp_ptable_iter_next(iter)) {
68 json_array_append_new(ptbl_json, trp_peer_to_json(peer));
70 trp_ptable_iter_free(iter);