Connect to hard-coded peer and exchange route info. Buggy and incomplete.
[trust_router.git] / trp / trp_ptable.c
1 #include <time.h>
2 #include <talloc.h>
3
4 #include <trust_router/tr_name.h>
5 #include <trp_internal.h>
6 #include <trp_ptable.h>
7 #include <tr_debug.h>
8
9 TRP_PEER *trp_peer_new(TALLOC_CTX *memctx)
10 {
11   TRP_PEER *peer=talloc(memctx, TRP_PEER);
12   if (peer!=NULL) {
13     peer->next=NULL;
14     peer->server=NULL;
15     peer->port=0;
16     peer->linkcost=TRP_METRIC_INFINITY;
17     peer->last_conn_attempt=(struct timespec){0,0};
18   }
19   return peer;
20 }
21
22 void trp_peer_free(TRP_PEER *peer)
23 {
24   talloc_free(peer);
25 }
26
27 static TRP_PEER *trp_peer_tail(TRP_PEER *peer)
28 {
29   while (peer->next!=NULL) {
30     peer=peer->next;
31   }
32   return peer;
33 }
34
35 char *trp_peer_get_server(TRP_PEER *peer)
36 {
37   return peer->server;
38 }
39
40 /* copies input; on error, peer->gssname will be null */
41 void trp_peer_set_server(TRP_PEER *peer, char *server)
42 {
43   peer->server=talloc_strdup(peer, server); /* will be null on error */
44 }
45
46 /* get the peer name based on the server name; caller is responsible for freeing the TR_NAME */
47 TR_NAME *trp_peer_get_gssname(TRP_PEER *peer)
48 {
49   TR_NAME *gssname=NULL;
50   char *s=NULL;
51
52   if (0<asprintf(&s, "trustrouter@%s", peer->server)) {
53     if (tr_new_name(s)!=NULL) {
54       gssname=tr_new_name(s);
55     }
56     free(s);
57   }
58   return gssname;
59 }
60
61 unsigned int trp_peer_get_port(TRP_PEER *peer)
62 {
63   return peer->port;
64 }
65
66 void trp_peer_set_port(TRP_PEER *peer, unsigned int port)
67 {
68   peer->port=port;
69 }
70
71 unsigned int trp_peer_get_linkcost(TRP_PEER *peer)
72 {
73   if (peer!=NULL)
74     return peer->linkcost;
75   else
76     return 1;
77 }
78
79 void trp_peer_set_linkcost(TRP_PEER *peer, unsigned int linkcost)
80 {
81   if ((linkcost>TRP_METRIC_INFINITY) && (linkcost!=TRP_METRIC_INVALID)) {
82     /* This indicates a programming error, but probably means an already infinite metric
83      * was (incorrectly) incremented. Issue a warning and proceed with an infinite metric. */
84     tr_warning("trp_peer_set_linkcost: link cost > infinity encountered, setting to infinity");
85     linkcost=TRP_METRIC_INFINITY;
86   }
87   peer->linkcost=linkcost;
88 }
89
90 struct timespec *trp_peer_get_last_conn_attempt(TRP_PEER *peer)
91 {
92   return &(peer->last_conn_attempt);
93 }
94
95 void trp_peer_set_last_conn_attempt(TRP_PEER *peer, struct timespec *time)
96 {
97   peer->last_conn_attempt=*time;
98 }
99
100 TRP_PTABLE *trp_ptable_new(TALLOC_CTX *memctx)
101 {
102   TRP_PTABLE *ptbl=talloc(memctx, TRP_PTABLE);
103   if (ptbl!=NULL) {
104     ptbl->head=NULL;
105   }
106   return ptbl;
107 }
108
109 void trp_ptable_free(TRP_PTABLE *ptbl)
110 {
111   talloc_free(ptbl);
112 }
113
114 TRP_RC trp_ptable_add(TRP_PTABLE *ptbl, TRP_PEER *newpeer)
115 {
116   if (ptbl->head==NULL) {
117     ptbl->head=newpeer;
118   } else {
119     trp_peer_tail(ptbl->head)->next=newpeer;
120     talloc_steal(ptbl, newpeer);
121   }
122   return TRP_SUCCESS;
123 }
124
125 /* peer pointer is invalid after successful removal. Does nothing and returns
126  * TRP_ERROR if peer is not in the list. */
127 TRP_RC trp_ptable_remove(TRP_PTABLE *ptbl, TRP_PEER *peer)
128 {
129   TRP_PEER *cur=NULL;
130   TRP_PEER *last=NULL;
131   if (ptbl->head!=NULL) {
132     if (ptbl->head==peer) {
133       /* special case for removing head of list */
134       cur=ptbl->head;
135       ptbl->head=ptbl->head->next; /* advance the head */
136       trp_peer_free(cur);
137     }
138     for (cur=ptbl->head->next; cur!=NULL; last=cur,cur=cur->next) {
139       if (cur==peer) {
140         if (last!=NULL)
141           last->next=cur->next;
142         trp_peer_free(cur);
143         return TRP_SUCCESS;
144       }
145     }
146   }
147   return TRP_ERROR;
148 }
149
150 TRP_PEER *trp_ptable_find(TRP_PTABLE *ptbl, TR_NAME *gssname)
151 {
152   TRP_PEER *cur=ptbl->head;
153   while ((cur!=NULL) && (0 != tr_name_cmp(trp_peer_get_gssname(cur), gssname)))
154     cur=cur->next;
155   return cur;
156 }
157
158 char *trp_peer_to_str(TALLOC_CTX *memctx, TRP_PEER *peer, const char *sep)
159 {
160   if (sep==NULL)
161     sep=", ";
162   return talloc_asprintf(memctx,
163                          "%s:%u%s0x%04X",
164                          peer->server, peer->port, sep,
165                          peer->linkcost);
166 }
167
168 /* this is horribly inefficient but should be ok for small peer tables */
169 char *trp_ptable_to_str(TALLOC_CTX *memctx, TRP_PTABLE *ptbl, const char *sep, const char *lineterm)
170 {
171   TALLOC_CTX *tmpctx=talloc_new(NULL);
172   TRP_PEER *peer=NULL;
173   char *result=talloc_strdup(tmpctx, "");
174
175   if (lineterm==NULL)
176     lineterm="\n";
177
178   /* this leaves intermediate result strings in the tmpctx context, we'll free these when
179    * we're done */
180   for (peer=ptbl->head; peer!=NULL; peer=peer->next)
181     result=talloc_asprintf(tmpctx, "%s%s%s", result, lineterm, trp_peer_to_str(tmpctx, peer, sep));
182
183   talloc_steal(memctx, result); /* hand result over to caller */
184   talloc_free(tmpctx); /* free detritus */
185   return result;
186 }
187
188 TRP_PTABLE_ITER *trp_ptable_iter_new(TALLOC_CTX *mem_ctx)
189 {
190   TRP_PTABLE_ITER *iter=talloc(mem_ctx, TRP_PTABLE_ITER);
191   *iter=NULL;
192   return iter;
193 }
194
195 TRP_PEER *trp_ptable_iter_first(TRP_PTABLE_ITER *iter, TRP_PTABLE *ptbl)
196 {
197   *iter=ptbl->head;
198   return *iter;
199 }
200
201 TRP_PEER *trp_ptable_iter_next(TRP_PTABLE_ITER *iter)
202 {
203   *iter=(*iter)->next;
204   return *iter;
205 }
206
207 void trp_ptable_iter_free(TRP_PTABLE_ITER *iter)
208 {
209   talloc_free(iter);
210 }
211