1821b7657b03cdb24e95d47657de5ac644ec6ce6
[trust_router.git] / trp / trp_upd.c
1 #include <jansson.h>
2 #include <talloc.h>
3
4 #include <trust_router/tr_name.h>
5 #include <trp_internal.h>
6 #include <tr_debug.h>
7
8
9 /* static prototypes */
10 static void *trp_inforec_route_new(TALLOC_CTX *mem_ctx);
11 static void trp_inforec_route_print(TRP_INFOREC_DATA);
12
13
14 struct trp_inforec_type_entry {
15   const char *name;
16   TRP_INFOREC_TYPE type;
17   void *(*allocate)(TALLOC_CTX *);
18   void (*print)(TRP_INFOREC_DATA);
19 };
20 static struct trp_inforec_type_entry trp_inforec_type_table[] = {
21   { "route", TRP_INFOREC_TYPE_ROUTE, trp_inforec_route_new, trp_inforec_route_print },
22   { "comm", TRP_INFOREC_TYPE_COMMUNITY, NULL, NULL },
23   { NULL, TRP_INFOREC_TYPE_UNKNOWN, NULL, NULL } /* must be the last entry */
24 };
25
26
27 /* look up an entry in the trp_inforec_type_table */
28 static struct trp_inforec_type_entry *get_trp_inforec_type_entry(TRP_INFOREC_TYPE msgtype)
29 {
30   struct trp_inforec_type_entry *entry=trp_inforec_type_table;
31
32   while ((entry->type != TRP_INFOREC_TYPE_UNKNOWN)
33         && (entry->type != msgtype)) {
34     entry ++;
35   }
36   return entry;
37 }
38
39 /* translate strings to codes */
40 TRP_INFOREC_TYPE trp_inforec_type_from_string(const char *s)
41 {
42   struct trp_inforec_type_entry *entry=trp_inforec_type_table;
43
44   while ((entry->type != TRP_INFOREC_TYPE_UNKNOWN)
45         && (strcmp(s, entry->name)!=0)) {
46     entry++;
47   }
48   return entry->type;
49 }
50 /* translate codes to strings (do not need to be freed) 
51  * Returns NULL on an unknown code */
52 const char *trp_inforec_type_to_string(TRP_INFOREC_TYPE msgtype)
53 {
54   struct trp_inforec_type_entry *entry=get_trp_inforec_type_entry(msgtype);
55   return entry->name;
56 }
57
58 /* called by talloc when destroying an update message body */
59 static int trp_inforec_route_destructor(void *object)
60 {
61   TRP_INFOREC_ROUTE *body=talloc_get_type_abort(object, TRP_INFOREC_ROUTE);
62   
63   /* clean up TR_NAME data, which are not managed by talloc */
64   if (body->comm != NULL) {
65     tr_free_name(body->comm);
66     body->comm=NULL;
67     tr_debug("trp_inforec_route_destructor: freed community");
68   }
69   if (body->realm != NULL) {
70     tr_free_name(body->realm);
71     body->realm=NULL;
72     tr_debug("trp_inforec_route_destructor: freed realm");
73   }
74   if (body->trust_router != NULL) {
75     tr_free_name(body->trust_router);
76     body->trust_router=NULL;
77     tr_debug("trp_inforec_route_destructor: freed trust_router");
78   }
79   if (body->next_hop != NULL) {
80     tr_free_name(body->next_hop);
81     body->next_hop=NULL;
82     tr_debug("trp_inforec_route_destructor: freed next_hop");
83   }
84
85   return 0;
86 }
87
88 static void *trp_inforec_route_new(TALLOC_CTX *mem_ctx)
89 {
90   TRP_INFOREC_ROUTE *new_rec=talloc(mem_ctx, TRP_INFOREC_ROUTE);
91
92   if (new_rec != NULL) {
93     new_rec->comm=NULL;
94     new_rec->realm=NULL;
95     new_rec->trust_router=NULL;
96     new_rec->next_hop=NULL;
97     new_rec->next_hop_port=0;
98     new_rec->metric=TRP_METRIC_INFINITY;
99     new_rec->interval=0;
100     talloc_set_destructor((void *)new_rec, trp_inforec_route_destructor);
101   }
102   return new_rec;
103 }
104
105 TRP_INFOREC *trp_inforec_get_next(TRP_INFOREC *rec)
106 {
107   if (rec!=NULL)
108     return rec->next;
109   else
110     return NULL;
111 }
112
113 static TRP_INFOREC *trp_inforec_get_tail(TRP_INFOREC *rec)
114 {
115   while ((rec->next)!=NULL)
116     rec=trp_inforec_get_next(rec);
117   return rec;
118 }
119
120 void trp_inforec_set_next(TRP_INFOREC *rec, TRP_INFOREC *next_rec)
121 {
122   if (rec !=NULL)
123     rec->next=next_rec;
124 }
125
126 TRP_INFOREC_TYPE trp_inforec_get_type(TRP_INFOREC *rec)
127 {
128   if (rec)
129     return rec->type;
130   else
131     return TRP_INFOREC_TYPE_UNKNOWN;
132 }
133
134 void trp_inforec_set_type(TRP_INFOREC *rec, TRP_INFOREC_TYPE type)
135 {
136   if (rec!=NULL)
137     rec->type=type;
138 }
139
140 TR_NAME *trp_inforec_get_comm(TRP_INFOREC *rec)
141 {
142   switch (rec->type) {
143   case TRP_INFOREC_TYPE_ROUTE:
144     if (rec->data.route!=NULL)
145       return rec->data.route->comm;
146     break;
147   default:
148     break;
149   }
150   return NULL;
151 }
152
153 TR_NAME *trp_inforec_dup_comm(TRP_INFOREC *rec)
154 {
155   return tr_dup_name(trp_inforec_get_comm(rec));
156 }
157
158 TRP_RC trp_inforec_set_comm(TRP_INFOREC *rec, TR_NAME *comm)
159 {
160   switch (rec->type) {
161   case TRP_INFOREC_TYPE_ROUTE:
162     if (rec->data.route!=NULL) {
163       rec->data.route->comm=comm;
164       return TRP_SUCCESS;
165     }
166     break;
167   default:
168     break;
169   }
170   return TRP_ERROR;
171 }
172
173 TR_NAME *trp_inforec_get_realm(TRP_INFOREC *rec)
174 {
175   switch (rec->type) {
176   case TRP_INFOREC_TYPE_ROUTE:
177     if (rec->data.route!=NULL)
178       return rec->data.route->realm;
179     break;
180   default:
181     break;
182   }
183   return NULL;
184 }
185
186 TR_NAME *trp_inforec_dup_realm(TRP_INFOREC *rec)
187 {
188   return tr_dup_name(trp_inforec_get_realm(rec));
189 }
190
191 TRP_RC trp_inforec_set_realm(TRP_INFOREC *rec, TR_NAME *realm)
192 {
193   switch (rec->type) {
194   case TRP_INFOREC_TYPE_ROUTE:
195     if (rec->data.route!=NULL) {
196       rec->data.route->realm=realm;
197       return TRP_SUCCESS;
198     } 
199     break;
200   default:
201     break;
202   }
203   return TRP_ERROR;
204 }
205
206 TR_NAME *trp_inforec_get_trust_router(TRP_INFOREC *rec)
207 {
208   switch (rec->type) {
209   case TRP_INFOREC_TYPE_ROUTE:
210     if (rec->data.route!=NULL)
211       return rec->data.route->trust_router;
212     break;
213   default:
214     break;
215   }
216   return NULL;
217 }
218
219 TR_NAME *trp_inforec_dup_trust_router(TRP_INFOREC *rec)
220 {
221   return tr_dup_name(trp_inforec_get_trust_router(rec));
222 }
223
224 TRP_RC trp_inforec_set_trust_router(TRP_INFOREC *rec, TR_NAME *trust_router)
225 {
226   switch (rec->type) {
227   case TRP_INFOREC_TYPE_ROUTE:
228     if (rec->data.route!=NULL) {
229       rec->data.route->trust_router=trust_router;
230       return TRP_SUCCESS;
231     }
232     break;
233   default:
234     break;
235   }
236   return TRP_ERROR;
237 }
238
239 /* TODO: need to return hostname/port --jlr */
240 TR_NAME *trp_inforec_get_next_hop(TRP_INFOREC *rec)
241 {
242   switch (rec->type) {
243   case TRP_INFOREC_TYPE_ROUTE:
244     if (rec->data.route!=NULL)
245       return rec->data.route->next_hop;
246     break;
247   default:
248     break;
249   }
250   return NULL;
251 }
252
253 TR_NAME *trp_inforec_dup_next_hop(TRP_INFOREC *rec)
254 {
255   return tr_dup_name(trp_inforec_get_next_hop(rec));
256 }
257
258 TRP_RC trp_inforec_set_next_hop(TRP_INFOREC *rec, TR_NAME *next_hop)
259 {
260   switch (rec->type) {
261   case TRP_INFOREC_TYPE_ROUTE:
262     if (rec->data.route!=NULL) {
263       rec->data.route->next_hop=next_hop;
264       return TRP_SUCCESS;
265     }
266     break;
267   default:
268     break;
269   }
270   return TRP_ERROR;
271 }
272
273 unsigned int trp_inforec_get_metric(TRP_INFOREC *rec)
274 {
275   switch (rec->type) {
276   case TRP_INFOREC_TYPE_ROUTE:
277     if (rec->data.route!=NULL)
278       return rec->data.route->metric;
279     break;
280   default:
281     break;
282   }
283   return TRP_METRIC_INVALID;
284 }
285
286 TRP_RC trp_inforec_set_metric(TRP_INFOREC *rec, unsigned int metric)
287 {
288   switch (rec->type) {
289   case TRP_INFOREC_TYPE_ROUTE:
290     if (rec->data.route!=NULL) {
291       rec->data.route->metric=metric;
292       return TRP_SUCCESS;
293     }
294     break;
295   default:
296     break;
297   }
298   return TRP_ERROR;
299 }
300
301 unsigned int trp_inforec_get_interval(TRP_INFOREC *rec)
302 {
303   switch (rec->type) {
304   case TRP_INFOREC_TYPE_ROUTE:
305     if (rec->data.route!=NULL)
306       return rec->data.route->interval;
307     break;
308   default:
309     break;
310   }
311   return TRP_INTERVAL_INVALID;
312 }
313
314 TRP_RC trp_inforec_set_interval(TRP_INFOREC *rec, unsigned int interval)
315 {
316   switch (rec->type) {
317   case TRP_INFOREC_TYPE_ROUTE:
318     if (rec->data.route!=NULL) {
319       rec->data.route->interval=interval;
320       return TRP_SUCCESS;
321   default:
322     break;
323     }
324     break;
325   }
326   return TRP_ERROR;
327 }
328
329 /* for internal use only; must set rec->type before calling this */
330 static TRP_RC trp_inforec_set_data(TRP_INFOREC *rec, void *data)
331 {
332   if (data==NULL)
333     return TRP_ERROR;
334
335   switch (rec->type) {
336   case TRP_INFOREC_TYPE_ROUTE:
337     rec->data.route=talloc_get_type(data, TRP_INFOREC_ROUTE);
338     break;
339   default:
340     return TRP_BADTYPE;
341   }
342   return TRP_SUCCESS;
343 }
344
345 /* generic record type */
346 TRP_INFOREC *trp_inforec_new(TALLOC_CTX *mem_ctx, TRP_INFOREC_TYPE type)
347 {
348   TRP_INFOREC *new_rec=talloc(mem_ctx, TRP_INFOREC);
349   struct trp_inforec_type_entry *dtype=get_trp_inforec_type_entry(type);
350
351   if ((new_rec != NULL) && (dtype->type != TRP_INFOREC_TYPE_UNKNOWN)) {
352     trp_inforec_set_type(new_rec, type);
353     trp_inforec_set_next(new_rec, NULL);
354     if (dtype->allocate!=NULL) {
355       if (TRP_SUCCESS!=trp_inforec_set_data(new_rec, dtype->allocate(new_rec))) {
356         talloc_free(new_rec);
357         new_rec=NULL;
358       }
359     }
360   }
361   return new_rec;
362 }
363
364 void trp_inforec_free(TRP_INFOREC *rec)
365 {
366   if (rec!=NULL)
367     talloc_free(rec);
368 }
369
370 static int trp_upd_destructor(void *object)
371 {
372   TRP_UPD *upd=talloc_get_type_abort(object, TRP_UPD);
373   if (upd->peer!=NULL)
374     tr_free_name(upd->peer);
375   return 0;
376 }
377
378 TRP_UPD *trp_upd_new(TALLOC_CTX *mem_ctx)
379 {
380   TRP_UPD *new_body=talloc(mem_ctx, TRP_UPD);
381
382   if (new_body!=NULL) {
383     new_body->records=NULL;
384     new_body->peer=NULL;
385     talloc_set_destructor((void *)new_body, trp_upd_destructor);
386   }
387   return new_body;
388 }
389
390 void trp_upd_free(TRP_UPD *update)
391 {
392   if (update!=NULL)
393     talloc_free(update);
394 }
395
396 TRP_INFOREC *trp_upd_get_inforec(TRP_UPD *upd)
397 {
398   if (upd!=NULL)
399     return upd->records;
400   else
401     return NULL;
402 }
403
404 void trp_upd_set_inforec(TRP_UPD *upd, TRP_INFOREC *rec)
405 {
406   if (upd!=NULL)
407     upd->records=rec;
408 }
409
410 void trp_upd_add_inforec(TRP_UPD *upd, TRP_INFOREC *rec)
411 {
412   tr_debug("trp_upd_add_inforec: adding record.");
413   if (upd->records==NULL)
414     upd->records=rec;
415   else
416     trp_inforec_set_next(trp_inforec_get_tail(upd->records), rec);
417   talloc_steal(upd, rec);
418 }
419
420 TR_NAME *trp_upd_get_peer(TRP_UPD *upd)
421 {
422   return upd->peer;
423 }
424
425 TR_NAME *trp_upd_dup_peer(TRP_UPD *upd)
426 {
427   return tr_dup_name(upd->peer);
428 }
429
430 void trp_upd_set_peer(TRP_UPD *upd, TR_NAME *peer)
431 {
432   upd->peer=peer;
433 }
434
435 void trp_upd_set_next_hop(TRP_UPD *upd, const char *hostname, unsigned int port)
436 {
437   TRP_INFOREC *rec=NULL;
438   TR_NAME *cpy=NULL;
439   
440   for (rec=trp_upd_get_inforec(upd); rec!=NULL; rec=trp_inforec_get_next(rec)) {
441     if (trp_inforec_set_next_hop(rec, cpy=tr_new_name(hostname)) != TRP_SUCCESS) {
442       tr_err("trp_upd_set_peer: error setting peer.");
443       tr_free_name(cpy);
444     }
445   }
446 }
447
448 /* pretty print */
449 static void trp_inforec_route_print(TRP_INFOREC_DATA data)
450 {
451   if (data.route!=NULL) {
452     printf("     community=%.*s\n     realm=%.*s\n     trust_router=%.*s\n     metric=%d\n     interval=%d]\n",
453            data.route->comm->len, data.route->comm->buf,
454            data.route->realm->len, data.route->realm->buf,
455            data.route->trust_router->len, data.route->trust_router->buf,
456            data.route->metric, data.route->interval);
457   }
458 }
459