2 * Copyright (c) 2016, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <tr_name_internal.h>
40 #include <trp_internal.h>
46 /* static prototypes */
47 static TRP_INFOREC_DATA *trp_inforec_route_new(TALLOC_CTX *mem_ctx);
48 static void trp_inforec_route_print(TRP_INFOREC_DATA *);
49 static TRP_INFOREC_DATA *trp_inforec_comm_new(TALLOC_CTX *mem_ctx);
50 static void trp_inforec_comm_print(TRP_INFOREC_DATA *);
53 struct trp_inforec_type_entry {
55 TRP_INFOREC_TYPE type;
56 TRP_INFOREC_DATA *(*allocate)(TALLOC_CTX *);
57 void (*print)(TRP_INFOREC_DATA *);
59 static struct trp_inforec_type_entry trp_inforec_type_table[] = {
60 { "route", TRP_INFOREC_TYPE_ROUTE, trp_inforec_route_new, trp_inforec_route_print },
61 { "comm", TRP_INFOREC_TYPE_COMMUNITY, trp_inforec_comm_new, trp_inforec_comm_print },
62 { NULL, TRP_INFOREC_TYPE_UNKNOWN, NULL, NULL } /* must be the last entry */
66 /* look up an entry in the trp_inforec_type_table */
67 static struct trp_inforec_type_entry *get_trp_inforec_type_entry(TRP_INFOREC_TYPE msgtype)
69 struct trp_inforec_type_entry *entry=trp_inforec_type_table;
71 while ((entry->type != TRP_INFOREC_TYPE_UNKNOWN)
72 && (entry->type != msgtype)) {
78 /* translate strings to codes */
79 TRP_INFOREC_TYPE trp_inforec_type_from_string(const char *s)
81 struct trp_inforec_type_entry *entry=trp_inforec_type_table;
83 while ((entry->type != TRP_INFOREC_TYPE_UNKNOWN)
84 && (strcasecmp(s, entry->name)!=0)) {
89 /* translate codes to strings (do not need to be freed)
90 * Returns NULL on an unknown code */
91 const char *trp_inforec_type_to_string(TRP_INFOREC_TYPE msgtype)
93 struct trp_inforec_type_entry *entry=get_trp_inforec_type_entry(msgtype);
97 /* called by talloc when destroying an update message body */
98 static int trp_inforec_route_destructor(void *object)
100 TRP_INFOREC_ROUTE *body=talloc_get_type_abort(object, TRP_INFOREC_ROUTE);
102 /* clean up TR_NAME data, which are not managed by talloc */
103 if (body->trust_router != NULL)
104 tr_free_name(body->trust_router);
105 if (body->next_hop != NULL)
106 tr_free_name(body->next_hop);
110 static TRP_INFOREC_DATA *trp_inforec_route_new(TALLOC_CTX *mem_ctx)
112 TRP_INFOREC_DATA *new_data=talloc(mem_ctx, TRP_INFOREC_DATA);
113 TRP_INFOREC_ROUTE *new_rec=NULL;
118 new_rec=talloc(new_data, TRP_INFOREC_ROUTE);
119 if (new_rec == NULL) {
120 talloc_free(new_data);
123 new_rec->trust_router=NULL;
124 new_rec->next_hop=NULL;
125 new_rec->next_hop_port=0;
126 new_rec->metric=TRP_METRIC_INFINITY;
128 talloc_set_destructor((void *)new_rec, trp_inforec_route_destructor);
129 new_data->route=new_rec;
136 static int trp_inforec_comm_destructor(void *obj)
138 TRP_INFOREC_COMM *rec=talloc_get_type_abort(obj, TRP_INFOREC_COMM);
139 if (rec->owner_realm!=NULL)
140 tr_free_name(rec->owner_realm);
141 if (rec->owner_contact!=NULL)
142 tr_free_name(rec->owner_contact);
143 if (rec->provenance!=NULL)
144 json_decref(rec->provenance);
148 static TRP_INFOREC_DATA *trp_inforec_comm_new(TALLOC_CTX *mem_ctx)
150 TRP_INFOREC_DATA *new_data=talloc(mem_ctx, TRP_INFOREC_DATA);
151 TRP_INFOREC_COMM *new_rec=NULL;
156 new_rec=talloc(new_data, TRP_INFOREC_COMM);
158 talloc_free(new_data);
161 new_rec->comm_type=TR_COMM_UNKNOWN;
162 new_rec->role=TR_ROLE_UNKNOWN;
164 new_rec->owner_realm=NULL;
165 new_rec->owner_contact=NULL;
166 new_rec->expiration_interval=0;
167 new_rec->provenance=NULL;
169 talloc_set_destructor((void *)new_rec, trp_inforec_comm_destructor);
170 new_data->comm=new_rec;
176 TRP_INFOREC *trp_inforec_get_next(TRP_INFOREC *rec)
184 static TRP_INFOREC *trp_inforec_get_tail(TRP_INFOREC *rec)
186 while ((rec->next)!=NULL)
187 rec=trp_inforec_get_next(rec);
191 void trp_inforec_set_next(TRP_INFOREC *rec, TRP_INFOREC *next_rec)
197 TRP_INFOREC_TYPE trp_inforec_get_type(TRP_INFOREC *rec)
202 return TRP_INFOREC_TYPE_UNKNOWN;
205 void trp_inforec_set_type(TRP_INFOREC *rec, TRP_INFOREC_TYPE type)
211 TR_NAME *trp_inforec_get_trust_router(TRP_INFOREC *rec)
214 case TRP_INFOREC_TYPE_ROUTE:
215 if (rec->data->route!=NULL)
216 return rec->data->route->trust_router;
224 int trp_inforec_get_trust_router_port(TRP_INFOREC *rec)
227 case TRP_INFOREC_TYPE_ROUTE:
228 if (rec->data->route!=NULL)
229 return rec->data->route->trust_router_port;
236 TR_NAME *trp_inforec_dup_trust_router(TRP_INFOREC *rec)
238 return tr_dup_name(trp_inforec_get_trust_router(rec));
241 TRP_RC trp_inforec_set_trust_router(TRP_INFOREC *rec, TR_NAME *trust_router, int port)
244 case TRP_INFOREC_TYPE_ROUTE:
245 if (rec->data->route!=NULL) {
246 rec->data->route->trust_router=trust_router;
256 TR_NAME *trp_inforec_get_next_hop(TRP_INFOREC *rec)
259 case TRP_INFOREC_TYPE_ROUTE:
260 if (rec->data->route!=NULL)
261 return rec->data->route->next_hop;
269 TR_NAME *trp_inforec_dup_next_hop(TRP_INFOREC *rec)
271 return tr_dup_name(trp_inforec_get_next_hop(rec));
275 * Set the next hop for the inforec
277 * Returns TRP_SUCCESS if it set the next hop value for the inforec.
278 * Returns TRP_UNSUPPORTED if the inforec does not have a next hop record but
279 * otherwise nothing went wrong.
280 * Returns TRP_ERROR or another error if there was a failure.
284 * @return TRP_SUCCESS if the value was set, TRP_UNSUPPORTED if the inforec does not support next hop, or an error code on failure
286 TRP_RC trp_inforec_set_next_hop(TRP_INFOREC *rec, TR_NAME *next_hop, int port)
288 /* Any inforec types that support next_hop should set it here. */
290 case TRP_INFOREC_TYPE_ROUTE:
291 if (rec->data->route==NULL)
293 rec->data->route->next_hop = next_hop;
294 rec->data->route->next_hop_port = port;
298 /* next hop not used for other records */
299 return TRP_UNSUPPORTED;
304 int trp_inforec_get_next_hop_port(TRP_INFOREC *rec)
307 case TRP_INFOREC_TYPE_ROUTE:
308 if (rec->data->route!=NULL)
309 return rec->data->route->next_hop_port;
316 unsigned int trp_inforec_get_metric(TRP_INFOREC *rec)
319 case TRP_INFOREC_TYPE_ROUTE:
320 if (rec->data->route!=NULL)
321 return rec->data->route->metric;
326 return TRP_METRIC_INVALID;
329 TRP_RC trp_inforec_set_metric(TRP_INFOREC *rec, unsigned int metric)
332 case TRP_INFOREC_TYPE_ROUTE:
333 if (rec->data->route!=NULL) {
334 rec->data->route->metric=metric;
344 unsigned int trp_inforec_get_interval(TRP_INFOREC *rec)
347 case TRP_INFOREC_TYPE_ROUTE:
348 if (rec->data->route!=NULL)
349 return rec->data->route->interval;
351 case TRP_INFOREC_TYPE_COMMUNITY:
352 if (rec->data->comm!=NULL)
353 return rec->data->comm->interval;
358 return TRP_INTERVAL_INVALID;
361 TRP_RC trp_inforec_set_interval(TRP_INFOREC *rec, unsigned int interval)
364 case TRP_INFOREC_TYPE_ROUTE:
365 if (rec->data->route!=NULL) {
366 rec->data->route->interval=interval;
370 case TRP_INFOREC_TYPE_COMMUNITY:
371 if (rec->data->comm!=NULL) {
372 rec->data->comm->interval=interval;
381 time_t trp_inforec_get_exp_interval(TRP_INFOREC *rec)
384 case TRP_INFOREC_TYPE_COMMUNITY:
385 if (rec->data->comm!=NULL)
386 return rec->data->comm->expiration_interval;
394 TRP_RC trp_inforec_set_exp_interval(TRP_INFOREC *rec, time_t expint)
397 case TRP_INFOREC_TYPE_COMMUNITY:
398 if (rec->data->comm!=NULL) {
399 rec->data->comm->expiration_interval=expint;
409 TR_COMM_TYPE trp_inforec_get_comm_type(TRP_INFOREC *rec)
412 case TRP_INFOREC_TYPE_COMMUNITY:
413 if (rec->data->comm!=NULL)
414 return rec->data->comm->comm_type;
419 return TR_COMM_UNKNOWN;
422 TRP_RC trp_inforec_set_comm_type(TRP_INFOREC *rec, TR_COMM_TYPE type)
425 case TRP_INFOREC_TYPE_COMMUNITY:
426 if (rec->data->comm!=NULL) {
427 rec->data->comm->comm_type=type;
437 TR_REALM_ROLE trp_inforec_get_role(TRP_INFOREC *rec)
440 case TRP_INFOREC_TYPE_COMMUNITY:
441 if (rec->data->comm!=NULL)
442 return rec->data->comm->role;
447 return TR_ROLE_UNKNOWN;
450 TRP_RC trp_inforec_set_role(TRP_INFOREC *rec, TR_REALM_ROLE role)
453 case TRP_INFOREC_TYPE_COMMUNITY:
454 if (rec->data->comm!=NULL) {
455 rec->data->comm->role=role;
465 TR_APC *trp_inforec_get_apcs(TRP_INFOREC *rec)
468 case TRP_INFOREC_TYPE_COMMUNITY:
469 if (rec->data->comm!=NULL)
470 return rec->data->comm->apcs;
478 TRP_RC trp_inforec_set_apcs(TRP_INFOREC *rec, TR_APC *apcs)
481 case TRP_INFOREC_TYPE_COMMUNITY:
482 if (rec->data->comm!=NULL) {
483 rec->data->comm->apcs=apcs;
484 talloc_steal(rec, apcs);
495 TR_NAME *trp_inforec_get_owner_realm(TRP_INFOREC *rec)
498 case TRP_INFOREC_TYPE_COMMUNITY:
499 if (rec->data->comm!=NULL)
500 return rec->data->comm->owner_realm;
508 TRP_RC trp_inforec_set_owner_realm(TRP_INFOREC *rec, TR_NAME *name)
511 case TRP_INFOREC_TYPE_COMMUNITY:
512 if (rec->data->comm!=NULL) {
513 rec->data->comm->owner_realm=name;
523 TR_NAME *trp_inforec_get_owner_contact(TRP_INFOREC *rec)
526 case TRP_INFOREC_TYPE_COMMUNITY:
527 if (rec->data->comm!=NULL)
528 return rec->data->comm->owner_contact;
536 TRP_RC trp_inforec_set_owner_contact(TRP_INFOREC *rec, TR_NAME *name)
539 case TRP_INFOREC_TYPE_COMMUNITY:
540 if (rec->data->comm!=NULL) {
541 rec->data->comm->owner_contact=name;
551 /* caller needs to incref the output if they're going to hang on to it */
552 json_t *trp_inforec_get_provenance(TRP_INFOREC *rec)
555 case TRP_INFOREC_TYPE_COMMUNITY:
556 if (rec->data->comm!=NULL)
557 return rec->data->comm->provenance;
565 /* increments the reference count */
566 TRP_RC trp_inforec_set_provenance(TRP_INFOREC *rec, json_t *prov)
569 case TRP_INFOREC_TYPE_COMMUNITY:
570 if (rec->data->comm!=NULL) {
571 if (rec->data->comm->provenance!=NULL)
572 json_decref(rec->data->comm->provenance);
573 rec->data->comm->provenance=prov;
584 static TRP_RC trp_inforec_add_to_provenance(TRP_INFOREC *rec, TR_NAME *name)
589 case TRP_INFOREC_TYPE_ROUTE:
590 /* no provenance list */
592 case TRP_INFOREC_TYPE_COMMUNITY:
593 jname=tr_name_to_json_string(name);
596 if (rec->data->comm->provenance==NULL) {
597 rec->data->comm->provenance=json_array();
598 if (rec->data->comm->provenance==NULL) {
603 if (0!=json_array_append_new(rec->data->comm->provenance, jname)) {
614 TR_NAME *trp_inforec_dup_origin(TRP_INFOREC *rec)
616 TR_NAME *origin=NULL;
617 json_t *prov=trp_inforec_get_provenance(rec);
623 s=json_string_value(json_array_get(prov, 0));
625 tr_debug("trp_inforec_dup_origin: empty origin in provenance list.");
628 origin=tr_new_name(s);
632 /* generic record type */
633 TRP_INFOREC *trp_inforec_new(TALLOC_CTX *mem_ctx, TRP_INFOREC_TYPE type)
635 TRP_INFOREC *new_rec=talloc(mem_ctx, TRP_INFOREC);
636 TRP_INFOREC_DATA *data=NULL;
637 struct trp_inforec_type_entry *dtype=get_trp_inforec_type_entry(type);
639 if ((new_rec != NULL) && (dtype->type != TRP_INFOREC_TYPE_UNKNOWN)) {
640 trp_inforec_set_type(new_rec, type);
641 trp_inforec_set_next(new_rec, NULL);
642 if (dtype->allocate!=NULL) {
643 data=dtype->allocate(new_rec);
647 talloc_free(new_rec);
655 void trp_inforec_free(TRP_INFOREC *rec)
661 static int trp_upd_destructor(void *object)
663 TRP_UPD *upd=talloc_get_type_abort(object, TRP_UPD);
664 if (upd->realm!=NULL)
665 tr_free_name(upd->realm);
667 tr_free_name(upd->comm);
669 tr_free_name(upd->peer);
673 TRP_UPD *trp_upd_new(TALLOC_CTX *mem_ctx)
675 TRP_UPD *new_body=talloc(mem_ctx, TRP_UPD);
677 if (new_body!=NULL) {
678 new_body->realm=NULL;
680 new_body->records=NULL;
682 talloc_set_destructor((void *)new_body, trp_upd_destructor);
687 void trp_upd_free(TRP_UPD *update)
693 TRP_INFOREC *trp_upd_get_inforec(TRP_UPD *upd)
701 void trp_upd_set_inforec(TRP_UPD *upd, TRP_INFOREC *rec)
707 void trp_upd_add_inforec(TRP_UPD *upd, TRP_INFOREC *rec)
709 tr_debug("trp_upd_add_inforec: adding record.");
710 if (upd->records==NULL)
713 trp_inforec_set_next(trp_inforec_get_tail(upd->records), rec);
714 talloc_steal(upd, rec);
718 * Removes and frees the selected inforec.
720 * @param upd Update to remove from
721 * @param rec Inforec to remove
723 void trp_upd_remove_inforec(TRP_UPD *upd, TRP_INFOREC *rec)
725 TRP_INFOREC *this=upd->records;
727 /* special case for the first element */
729 upd->records=upd->records->next;
730 trp_inforec_free(this);
734 while (this->next!=NULL) {
735 if (this->next==rec) {
736 this->next=this->next->next; /* this->next is not null */
737 trp_inforec_free(rec);
743 size_t trp_upd_num_inforecs(TRP_UPD *upd)
746 TRP_INFOREC *this=upd->records;
748 while (this != NULL) {
756 TR_NAME *trp_upd_get_realm(TRP_UPD *upd)
761 TR_NAME *trp_upd_dup_realm(TRP_UPD *upd)
763 return tr_dup_name(upd->realm);
766 void trp_upd_set_realm(TRP_UPD *upd, TR_NAME *realm)
768 if (upd->realm!=NULL)
769 tr_free_name(upd->realm);
773 TR_NAME *trp_upd_get_comm(TRP_UPD *upd)
778 TR_NAME *trp_upd_dup_comm(TRP_UPD *upd)
780 return tr_dup_name(upd->comm);
783 void trp_upd_set_comm(TRP_UPD *upd, TR_NAME *comm)
786 tr_free_name(upd->comm);
790 TR_NAME *trp_upd_get_peer(TRP_UPD *upd)
795 TR_NAME *trp_upd_dup_peer(TRP_UPD *upd)
797 return tr_dup_name(upd->peer);
800 void trp_upd_set_peer(TRP_UPD *upd, TR_NAME *peer)
805 void trp_upd_set_next_hop(TRP_UPD *upd, const char *hostname, unsigned int port)
807 TRP_INFOREC *rec=NULL;
810 for (rec=trp_upd_get_inforec(upd); rec!=NULL; rec=trp_inforec_get_next(rec)) {
811 switch (trp_inforec_set_next_hop(rec, cpy=tr_new_name(hostname), port)) {
813 /* Success, the TR_NAME in cpy is now stored with the inforec */
816 case TRP_UNSUPPORTED:
817 /* No error, but the inforec does not accept a next_hop. Free our copy. */
822 tr_err("trp_upd_set_next_hop: error setting next hop.");
829 void trp_upd_add_to_provenance(TRP_UPD *upd, TR_NAME *name)
831 TRP_INFOREC *rec=NULL;
833 /* add it to all inforecs */
834 for (rec=trp_upd_get_inforec(upd); rec!=NULL; rec=trp_inforec_get_next(rec)) {
835 if (TRP_SUCCESS!=trp_inforec_add_to_provenance(rec, name))
836 tr_err("trp_upd_set_peer: error adding peer to provenance list.");
841 static void trp_inforec_route_print(TRP_INFOREC_DATA *data)
843 if (data->route!=NULL) {
844 tr_info(" trust_router=%.*s\n metric=%d\n interval=%d]\n",
845 data->route->trust_router->len, data->route->trust_router->buf,
846 data->route->metric, data->route->interval);
850 static void trp_inforec_comm_print(TRP_INFOREC_DATA *data)
852 if (data->comm!=NULL) {
853 tr_info(" type=%s\n role=%s\n owner=%.*s\n contact=%.*s]\n",
854 tr_comm_type_to_str(data->comm->comm_type),
855 tr_realm_role_to_str(data->comm->role),
856 data->comm->owner_realm->len, data->comm->owner_realm->buf,
857 data->comm->owner_contact->len, data->comm->owner_contact->buf);
858 /* TODO: print apcs */