Use json_is_true() in place of json_boolean_value() for compatibility
[trust_router.git] / trp / trp_ptable.c
1 /*
2  * Copyright (c) 2016, 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 <time.h>
36 #include <talloc.h>
37
38 #include <tr_name_internal.h>
39 #include <trp_internal.h>
40 #include <tr_gss_names.h>
41 #include <trp_ptable.h>
42 #include <tr_debug.h>
43 #include <trp_peer.h>
44
45
46 TRP_PTABLE *trp_ptable_new(TALLOC_CTX *memctx)
47 {
48   TRP_PTABLE *ptbl=talloc(memctx, TRP_PTABLE);
49   if (ptbl!=NULL) {
50     ptbl->head=NULL;
51   }
52   return ptbl;
53 }
54
55 void trp_ptable_free(TRP_PTABLE *ptbl)
56 {
57   talloc_free(ptbl);
58 }
59
60 TRP_RC trp_ptable_add(TRP_PTABLE *ptbl, TRP_PEER *newpeer)
61 {
62   if (ptbl->head==NULL)
63     ptbl->head=newpeer;
64   else
65     trp_peer_tail(ptbl->head)->next=newpeer;
66
67   talloc_steal(ptbl, newpeer);
68   return TRP_SUCCESS;
69 }
70
71 /* peer pointer is invalid after successful removal. Does nothing and returns
72  * TRP_ERROR if peer is not in the list. */
73 TRP_RC trp_ptable_remove(TRP_PTABLE *ptbl, TRP_PEER *peer)
74 {
75   TRP_PEER *cur=NULL;
76   TRP_PEER *last=NULL;
77   if (ptbl->head!=NULL) {
78     if (ptbl->head==peer) {
79       /* special case for removing head of list */
80       cur=ptbl->head;
81       ptbl->head=ptbl->head->next; /* advance the head */
82       trp_peer_free(cur);
83     }
84     for (cur=ptbl->head->next; cur!=NULL; last=cur,cur=cur->next) {
85       if (cur==peer) {
86         if (last!=NULL)
87           last->next=cur->next;
88         trp_peer_free(cur);
89         return TRP_SUCCESS;
90       }
91     }
92   }
93   return TRP_ERROR;
94 }
95
96 TRP_PEER *trp_ptable_find_gss_name(TRP_PTABLE *ptbl, TR_NAME *gssname)
97 {
98   TRP_PEER *cur=ptbl->head;
99   while ((cur!=NULL) && (!tr_gss_names_matches(trp_peer_get_gss_names(cur), gssname)))
100     cur=cur->next;
101   return cur;
102 }
103
104 TRP_PEER *trp_ptable_find_servicename(TRP_PTABLE *ptbl, TR_NAME *servicename)
105 {
106   TRP_PEER *cur=ptbl->head;
107   while ((cur!=NULL) && (0 != tr_name_cmp(trp_peer_get_servicename(cur), servicename)))
108     cur=cur->next;
109   return cur;
110 }
111
112 TRP_PTABLE_ITER *trp_ptable_iter_new(TALLOC_CTX *mem_ctx)
113 {
114   TRP_PTABLE_ITER *iter=talloc(mem_ctx, TRP_PTABLE_ITER);
115   *iter=NULL;
116   return iter;
117 }
118
119 TRP_PEER *trp_ptable_iter_first(TRP_PTABLE_ITER *iter, TRP_PTABLE *ptbl)
120 {
121   if (ptbl==NULL)
122     *iter=NULL;
123   else
124     *iter=ptbl->head;
125   return *iter;
126 }
127
128 TRP_PEER *trp_ptable_iter_next(TRP_PTABLE_ITER *iter)
129 {
130   if (*iter!=NULL)
131     *iter=(*iter)->next;
132   return *iter;
133 }
134
135 void trp_ptable_iter_free(TRP_PTABLE_ITER *iter)
136 {
137   talloc_free(iter);
138 }
139