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");
83 static void *trp_inforec_route_new(TALLOC_CTX *mem_ctx)
85 TRP_INFOREC_ROUTE *new_rec=talloc(mem_ctx, TRP_INFOREC_ROUTE);
87 if (new_rec != NULL) {
90 new_rec->trust_router=NULL;
91 new_rec->metric=TRP_METRIC_INFINITY;
93 talloc_set_destructor((void *)new_rec, trp_inforec_route_destructor);
98 TRP_INFOREC *trp_inforec_get_next(TRP_INFOREC *rec)
106 void trp_inforec_set_next(TRP_INFOREC *rec, TRP_INFOREC *next_rec)
112 TRP_INFOREC_TYPE trp_inforec_get_type(TRP_INFOREC *rec)
117 return TRP_INFOREC_TYPE_UNKNOWN;
120 void trp_inforec_set_type(TRP_INFOREC *rec, TRP_INFOREC_TYPE type)
126 TR_NAME *trp_inforec_get_comm(TRP_INFOREC *rec)
129 case TRP_INFOREC_TYPE_ROUTE:
130 if (rec->data.route!=NULL)
131 return rec->data.route->comm;
139 TRP_RC trp_inforec_set_comm(TRP_INFOREC *rec, TR_NAME *comm)
142 case TRP_INFOREC_TYPE_ROUTE:
143 if (rec->data.route!=NULL) {
144 rec->data.route->comm=comm;
154 TR_NAME *trp_inforec_get_realm(TRP_INFOREC *rec)
157 case TRP_INFOREC_TYPE_ROUTE:
158 if (rec->data.route!=NULL)
159 return rec->data.route->realm;
167 TRP_RC trp_inforec_set_realm(TRP_INFOREC *rec, TR_NAME *realm)
170 case TRP_INFOREC_TYPE_ROUTE:
171 if (rec->data.route!=NULL) {
172 rec->data.route->realm=realm;
182 TR_NAME *trp_inforec_get_trust_router(TRP_INFOREC *rec)
185 case TRP_INFOREC_TYPE_ROUTE:
186 if (rec->data.route!=NULL)
187 return rec->data.route->trust_router;
195 TRP_RC trp_inforec_set_trust_router(TRP_INFOREC *rec, TR_NAME *trust_router)
198 case TRP_INFOREC_TYPE_ROUTE:
199 if (rec->data.route!=NULL) {
200 rec->data.route->trust_router=trust_router;
210 unsigned int trp_inforec_get_metric(TRP_INFOREC *rec)
213 case TRP_INFOREC_TYPE_ROUTE:
214 if (rec->data.route!=NULL)
215 return rec->data.route->metric;
220 return TRP_METRIC_INVALID;
223 TRP_RC trp_inforec_set_metric(TRP_INFOREC *rec, unsigned int metric)
226 case TRP_INFOREC_TYPE_ROUTE:
227 if (rec->data.route!=NULL) {
228 rec->data.route->metric=metric;
238 unsigned int trp_inforec_get_interval(TRP_INFOREC *rec)
241 case TRP_INFOREC_TYPE_ROUTE:
242 if (rec->data.route!=NULL)
243 return rec->data.route->interval;
248 return TRP_INTERVAL_INVALID;
251 TRP_RC trp_inforec_set_interval(TRP_INFOREC *rec, unsigned int interval)
254 case TRP_INFOREC_TYPE_ROUTE:
255 if (rec->data.route!=NULL) {
256 rec->data.route->interval=interval;
266 /* for internal use only; must set rec->type before calling this */
267 static TRP_RC trp_inforec_set_data(TRP_INFOREC *rec, void *data)
273 case TRP_INFOREC_TYPE_ROUTE:
274 rec->data.route=talloc_get_type(data, TRP_INFOREC_ROUTE);
282 /* generic record type */
283 TRP_INFOREC *trp_inforec_new(TALLOC_CTX *mem_ctx, TRP_INFOREC_TYPE type)
285 TRP_INFOREC *new_rec=talloc(mem_ctx, TRP_INFOREC);
286 struct trp_inforec_type_entry *dtype=get_trp_inforec_type_entry(type);
288 if ((new_rec != NULL) && (dtype->type != TRP_INFOREC_TYPE_UNKNOWN)) {
289 trp_inforec_set_type(new_rec, type);
290 trp_inforec_set_next(new_rec, NULL);
291 if (dtype->allocate!=NULL) {
292 if (TRP_SUCCESS!=trp_inforec_set_data(new_rec, dtype->allocate(new_rec))) {
293 talloc_free(new_rec);
301 void trp_inforec_free(TRP_INFOREC *rec)
307 TRP_UPD *trp_upd_new(TALLOC_CTX *mem_ctx)
309 TRP_UPD *new_body=talloc(mem_ctx, TRP_UPD);
311 if (new_body!=NULL) {
312 new_body->records=NULL;
317 void trp_upd_free(TRP_UPD *update)
323 TRP_INFOREC *trp_upd_get_inforec(TRP_UPD *upd)
331 void trp_upd_set_inforec(TRP_UPD *upd, TRP_INFOREC *rec)
338 static void trp_inforec_route_print(TRP_INFOREC_DATA data)
340 if (data.route!=NULL) {
341 printf(" community=%.*s\n realm=%.*s\n trust_router=%.*s\n metric=%d\n interval=%d]\n",
342 data.route->comm->len, data.route->comm->buf,
343 data.route->realm->len, data.route->realm->buf,
344 data.route->trust_router->len, data.route->trust_router->buf,
345 data.route->metric, data.route->interval);