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