4 #include <trust_router/tr_name.h>
5 #include <trp_internal.h>
9 /* static prototypes */
10 static void *trp_inforec_route_new(TALLOC_CTX *mem_ctx);
11 static void trp_inforec_route_print(TRP_INFOREC_DATA);
14 struct trp_inforec_type_entry {
16 TRP_INFOREC_TYPE type;
17 void *(*allocate)(TALLOC_CTX *);
18 void (*print)(TRP_INFOREC_DATA);
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 */
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)
30 struct trp_inforec_type_entry *entry=trp_inforec_type_table;
32 while ((entry->type != TRP_INFOREC_TYPE_UNKNOWN)
33 && (entry->type != msgtype)) {
39 /* translate strings to codes */
40 TRP_INFOREC_TYPE trp_inforec_type_from_string(const char *s)
42 struct trp_inforec_type_entry *entry=trp_inforec_type_table;
44 while ((entry->type != TRP_INFOREC_TYPE_UNKNOWN)
45 && (strcmp(s, entry->name)!=0)) {
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)
54 struct trp_inforec_type_entry *entry=get_trp_inforec_type_entry(msgtype);
58 /* called by talloc when destroying an update message body */
59 static int trp_inforec_route_destructor(void *object)
61 TRP_INFOREC_ROUTE *body=talloc_get_type_abort(object, TRP_INFOREC_ROUTE);
63 /* clean up TR_NAME data, which are not managed by talloc */
64 if (body->comm != NULL) {
65 tr_free_name(body->comm);
67 tr_debug("trp_inforec_route_destructor: freed community");
69 if (body->realm != NULL) {
70 tr_free_name(body->realm);
72 tr_debug("trp_inforec_route_destructor: freed realm");
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");
79 if (body->next_hop != NULL) {
80 tr_free_name(body->next_hop);
82 tr_debug("trp_inforec_route_destructor: freed next_hop");
88 static void *trp_inforec_route_new(TALLOC_CTX *mem_ctx)
90 TRP_INFOREC_ROUTE *new_rec=talloc(mem_ctx, TRP_INFOREC_ROUTE);
92 if (new_rec != 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;
100 talloc_set_destructor((void *)new_rec, trp_inforec_route_destructor);
105 TRP_INFOREC *trp_inforec_get_next(TRP_INFOREC *rec)
113 static TRP_INFOREC *trp_inforec_get_tail(TRP_INFOREC *rec)
115 while ((rec->next)!=NULL)
116 rec=trp_inforec_get_next(rec);
120 void trp_inforec_set_next(TRP_INFOREC *rec, TRP_INFOREC *next_rec)
126 TRP_INFOREC_TYPE trp_inforec_get_type(TRP_INFOREC *rec)
131 return TRP_INFOREC_TYPE_UNKNOWN;
134 void trp_inforec_set_type(TRP_INFOREC *rec, TRP_INFOREC_TYPE type)
140 TR_NAME *trp_inforec_get_comm(TRP_INFOREC *rec)
143 case TRP_INFOREC_TYPE_ROUTE:
144 if (rec->data.route!=NULL)
145 return rec->data.route->comm;
153 TR_NAME *trp_inforec_dup_comm(TRP_INFOREC *rec)
155 return tr_dup_name(trp_inforec_get_comm(rec));
158 TRP_RC trp_inforec_set_comm(TRP_INFOREC *rec, TR_NAME *comm)
161 case TRP_INFOREC_TYPE_ROUTE:
162 if (rec->data.route!=NULL) {
163 rec->data.route->comm=comm;
173 TR_NAME *trp_inforec_get_realm(TRP_INFOREC *rec)
176 case TRP_INFOREC_TYPE_ROUTE:
177 if (rec->data.route!=NULL)
178 return rec->data.route->realm;
186 TR_NAME *trp_inforec_dup_realm(TRP_INFOREC *rec)
188 return tr_dup_name(trp_inforec_get_realm(rec));
191 TRP_RC trp_inforec_set_realm(TRP_INFOREC *rec, TR_NAME *realm)
194 case TRP_INFOREC_TYPE_ROUTE:
195 if (rec->data.route!=NULL) {
196 rec->data.route->realm=realm;
206 TR_NAME *trp_inforec_get_trust_router(TRP_INFOREC *rec)
209 case TRP_INFOREC_TYPE_ROUTE:
210 if (rec->data.route!=NULL)
211 return rec->data.route->trust_router;
219 TR_NAME *trp_inforec_dup_trust_router(TRP_INFOREC *rec)
221 return tr_dup_name(trp_inforec_get_trust_router(rec));
224 TRP_RC trp_inforec_set_trust_router(TRP_INFOREC *rec, TR_NAME *trust_router)
227 case TRP_INFOREC_TYPE_ROUTE:
228 if (rec->data.route!=NULL) {
229 rec->data.route->trust_router=trust_router;
239 /* TODO: need to return hostname/port --jlr */
240 TR_NAME *trp_inforec_get_next_hop(TRP_INFOREC *rec)
243 case TRP_INFOREC_TYPE_ROUTE:
244 if (rec->data.route!=NULL)
245 return rec->data.route->next_hop;
253 TR_NAME *trp_inforec_dup_next_hop(TRP_INFOREC *rec)
255 return tr_dup_name(trp_inforec_get_next_hop(rec));
258 TRP_RC trp_inforec_set_next_hop(TRP_INFOREC *rec, TR_NAME *next_hop)
261 case TRP_INFOREC_TYPE_ROUTE:
262 if (rec->data.route!=NULL) {
263 rec->data.route->next_hop=next_hop;
273 unsigned int trp_inforec_get_metric(TRP_INFOREC *rec)
276 case TRP_INFOREC_TYPE_ROUTE:
277 if (rec->data.route!=NULL)
278 return rec->data.route->metric;
283 return TRP_METRIC_INVALID;
286 TRP_RC trp_inforec_set_metric(TRP_INFOREC *rec, unsigned int metric)
289 case TRP_INFOREC_TYPE_ROUTE:
290 if (rec->data.route!=NULL) {
291 rec->data.route->metric=metric;
301 unsigned int trp_inforec_get_interval(TRP_INFOREC *rec)
304 case TRP_INFOREC_TYPE_ROUTE:
305 if (rec->data.route!=NULL)
306 return rec->data.route->interval;
311 return TRP_INTERVAL_INVALID;
314 TRP_RC trp_inforec_set_interval(TRP_INFOREC *rec, unsigned int interval)
317 case TRP_INFOREC_TYPE_ROUTE:
318 if (rec->data.route!=NULL) {
319 rec->data.route->interval=interval;
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)
336 case TRP_INFOREC_TYPE_ROUTE:
337 rec->data.route=talloc_get_type(data, TRP_INFOREC_ROUTE);
345 /* generic record type */
346 TRP_INFOREC *trp_inforec_new(TALLOC_CTX *mem_ctx, TRP_INFOREC_TYPE type)
348 TRP_INFOREC *new_rec=talloc(mem_ctx, TRP_INFOREC);
349 struct trp_inforec_type_entry *dtype=get_trp_inforec_type_entry(type);
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);
364 void trp_inforec_free(TRP_INFOREC *rec)
370 static int trp_upd_destructor(void *object)
372 TRP_UPD *upd=talloc_get_type_abort(object, TRP_UPD);
374 tr_free_name(upd->peer);
378 TRP_UPD *trp_upd_new(TALLOC_CTX *mem_ctx)
380 TRP_UPD *new_body=talloc(mem_ctx, TRP_UPD);
382 if (new_body!=NULL) {
383 new_body->records=NULL;
385 talloc_set_destructor((void *)new_body, trp_upd_destructor);
390 void trp_upd_free(TRP_UPD *update)
396 TRP_INFOREC *trp_upd_get_inforec(TRP_UPD *upd)
404 void trp_upd_set_inforec(TRP_UPD *upd, TRP_INFOREC *rec)
410 void trp_upd_add_inforec(TRP_UPD *upd, TRP_INFOREC *rec)
412 tr_debug("trp_upd_add_inforec: adding record.");
413 if (upd->records==NULL)
416 trp_inforec_set_next(trp_inforec_get_tail(upd->records), rec);
417 talloc_steal(upd, rec);
420 TR_NAME *trp_upd_get_peer(TRP_UPD *upd)
425 TR_NAME *trp_upd_dup_peer(TRP_UPD *upd)
427 return tr_dup_name(upd->peer);
430 void trp_upd_set_peer(TRP_UPD *upd, TR_NAME *peer)
435 void trp_upd_set_next_hop(TRP_UPD *upd, const char *hostname, unsigned int port)
437 TRP_INFOREC *rec=NULL;
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.");
449 static void trp_inforec_route_print(TRP_INFOREC_DATA data)
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);