c6c1cb89f7c9578a598eb374ccd901c67ea7e166
[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 TRP_PTABLE *trp_ptable_new(TALLOC_CTX *memctx)
86 {
87   TRP_PTABLE *ptbl=talloc(memctx, TRP_PTABLE);
88   if (ptbl!=NULL) {
89     ptbl->head=NULL;
90   }
91   return ptbl;
92 }
93
94 void trp_ptable_free(TRP_PTABLE *ptbl)
95 {
96   talloc_free(ptbl);
97 }
98
99 TRP_RC trp_ptable_add(TRP_PTABLE *ptbl, TRP_PEER *newpeer)
100 {
101   if (ptbl->head==NULL) {
102     ptbl->head=newpeer;
103   } else {
104     trp_peer_tail(ptbl->head)->next=newpeer;
105     talloc_steal(ptbl, newpeer);
106   }
107   return TRP_SUCCESS;
108 }
109
110 /* peer pointer is invalid after successful removal. Does nothing and returns
111  * TRP_ERROR if peer is not in the list. */
112 TRP_RC trp_ptable_remove(TRP_PTABLE *ptbl, TRP_PEER *peer)
113 {
114   TRP_PEER *cur=NULL;
115   TRP_PEER *last=NULL;
116   if (ptbl->head!=NULL) {
117     if (ptbl->head==peer) {
118       /* special case for removing head of list */
119       cur=ptbl->head;
120       ptbl->head=ptbl->head->next; /* advance the head */
121       trp_peer_free(cur);
122     }
123     for (cur=ptbl->head->next; cur!=NULL; last=cur,cur=cur->next) {
124       if (cur==peer) {
125         if (last!=NULL)
126           last->next=cur->next;
127         trp_peer_free(cur);
128         return TRP_SUCCESS;
129       }
130     }
131   }
132   return TRP_ERROR;
133 }
134
135 char *trp_peer_to_str(TALLOC_CTX *memctx, TRP_PEER *peer, const char *sep)
136 {
137   if (sep==NULL)
138     sep=", ";
139   return talloc_asprintf(memctx,
140                          "%s:%u%s0x%04X",
141                          peer->server, peer->port, sep,
142                          peer->linkcost);
143 }
144
145 /* this is horribly inefficient but should be ok for small peer tables */
146 char *trp_ptable_to_str(TALLOC_CTX *memctx, TRP_PTABLE *ptbl, const char *sep, const char *lineterm)
147 {
148   TALLOC_CTX *tmpctx=talloc_new(NULL);
149   TRP_PEER *peer=NULL;
150   char *result=talloc_strdup(tmpctx, "");
151
152   if (lineterm==NULL)
153     lineterm="\n";
154
155   /* this leaves intermediate result strings in the tmpctx context, we'll free these when
156    * we're done */
157   for (peer=ptbl->head; peer!=NULL; peer=peer->next)
158     result=talloc_asprintf(tmpctx, "%s%s%s", result, lineterm, trp_peer_to_str(tmpctx, peer, sep));
159
160   talloc_steal(memctx, result); /* hand result over to caller */
161   talloc_free(tmpctx); /* free detritus */
162   return result;
163 }