+/*
+ * Copyright (c) 2016, JANET(UK)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of JANET(UK) nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
#include <jansson.h>
#include <talloc.h>
-#include <trust_router/tr_name.h>
+#include <tr_name_internal.h>
#include <trp_internal.h>
#include <tr_debug.h>
-
-/* static prototypes */
-static void *trp_inforec_route_new(TALLOC_CTX *mem_ctx);
-static void trp_inforec_route_print(TRP_INFOREC_DATA);
-
-
-struct trp_inforec_type_entry {
- const char *name;
- TRP_INFOREC_TYPE type;
- void *(*allocate)(TALLOC_CTX *);
- void (*print)(TRP_INFOREC_DATA);
-};
-static struct trp_inforec_type_entry trp_inforec_type_table[] = {
- { "route", TRP_INFOREC_TYPE_ROUTE, trp_inforec_route_new, trp_inforec_route_print },
- { "comm", TRP_INFOREC_TYPE_COMMUNITY, NULL, NULL },
- { NULL, TRP_INFOREC_TYPE_UNKNOWN, NULL, NULL } /* must be the last entry */
-};
-
-
-/* look up an entry in the trp_inforec_type_table */
-static struct trp_inforec_type_entry *get_trp_inforec_type_entry(TRP_INFOREC_TYPE msgtype)
-{
- struct trp_inforec_type_entry *entry=trp_inforec_type_table;
-
- while ((entry->type != TRP_INFOREC_TYPE_UNKNOWN)
- && (entry->type != msgtype)) {
- entry ++;
- }
- return entry;
-}
-
-/* translate strings to codes */
-TRP_INFOREC_TYPE trp_inforec_type_from_string(const char *s)
-{
- struct trp_inforec_type_entry *entry=trp_inforec_type_table;
-
- while ((entry->type != TRP_INFOREC_TYPE_UNKNOWN)
- && (strcmp(s, entry->name)!=0)) {
- entry++;
- }
- return entry->type;
-}
-/* translate codes to strings (do not need to be freed)
- * Returns NULL on an unknown code */
-const char *trp_inforec_type_to_string(TRP_INFOREC_TYPE msgtype)
-{
- struct trp_inforec_type_entry *entry=get_trp_inforec_type_entry(msgtype);
- return entry->name;
-}
-
-
-/* called by talloc when destroying an update message body */
-static int trp_inforec_route_destructor(void *object)
+static int trp_req_destructor(void *object)
{
- TRP_INFOREC_ROUTE *body=talloc_get_type_abort(object, TRP_INFOREC_ROUTE);
+ TRP_REQ *req=talloc_get_type_abort(object, TRP_REQ);
/* clean up TR_NAME data, which are not managed by talloc */
- if (body->comm != NULL) {
- tr_free_name(body->comm);
- body->comm=NULL;
- tr_debug("trp_inforec_route_destructor: freed community");
- }
- if (body->realm != NULL) {
- tr_free_name(body->realm);
- body->realm=NULL;
- tr_debug("trp_inforec_route_destructor: freed realm");
- }
- if (body->trust_router != NULL) {
- tr_free_name(body->trust_router);
- body->trust_router=NULL;
- tr_debug("trp_inforec_route_destructor: freed trust_router");
- }
-
- return 0;
-}
+ if (req->comm != NULL)
+ tr_free_name(req->comm);
-static void *trp_inforec_route_new(TALLOC_CTX *mem_ctx)
-{
- TRP_INFOREC_ROUTE *new_rec=talloc(mem_ctx, TRP_INFOREC_ROUTE);
+ if (req->realm != NULL)
+ tr_free_name(req->realm);
- if (new_rec != NULL) {
- new_rec->comm=NULL;
- new_rec->realm=NULL;
- new_rec->trust_router=NULL;
- new_rec->metric=TRP_METRIC_INFINITY;
- new_rec->interval=0;
- talloc_set_destructor((void *)new_rec, trp_inforec_route_destructor);
- }
- return new_rec;
-}
-
-TR_NAME *trp_inforec_get_comm(TRP_INFOREC *rec)
-{
- switch (rec->type) {
- case TRP_INFOREC_TYPE_ROUTE:
- if (rec->data.route!=NULL)
- return rec->data.route->comm;
- break;
- default:
- break;
- }
- return NULL;
-}
+ if (req->peer != NULL)
+ tr_free_name(req->peer);
-TRP_RC trp_inforec_set_comm(TRP_INFOREC *rec, TR_NAME *comm)
-{
- switch (rec->type) {
- case TRP_INFOREC_TYPE_ROUTE:
- if (rec->data.route!=NULL) {
- rec->data.route->comm=comm;
- return TRP_SUCCESS;
- }
- break;
- default:
- break;
- }
- return TRP_ERROR;
+ return 0;
}
-TR_NAME *trp_inforec_get_realm(TRP_INFOREC *rec)
+TRP_REQ *trp_req_new(TALLOC_CTX *mem_ctx)
{
- switch (rec->type) {
- case TRP_INFOREC_TYPE_ROUTE:
- if (rec->data.route!=NULL)
- return rec->data.route->realm;
- break;
- default:
- break;
- }
- return NULL;
-}
+ TRP_REQ *new_req=talloc(mem_ctx, TRP_REQ);
-TRP_RC trp_inforec_set_realm(TRP_INFOREC *rec, TR_NAME *realm)
-{
- switch (rec->type) {
- case TRP_INFOREC_TYPE_ROUTE:
- if (rec->data.route!=NULL) {
- rec->data.route->realm=realm;
- return TRP_SUCCESS;
- }
- break;
- default:
- break;
+ if (new_req != NULL) {
+ new_req->comm=NULL;
+ new_req->realm=NULL;
+ new_req->peer=NULL;
}
- return TRP_ERROR;
-}
-TR_NAME *trp_inforec_get_trust_router(TRP_INFOREC *rec)
-{
- switch (rec->type) {
- case TRP_INFOREC_TYPE_ROUTE:
- if (rec->data.route!=NULL)
- return rec->data.route->trust_router;
- break;
- default:
- break;
- }
- return NULL;
+ talloc_set_destructor((void *)new_req, trp_req_destructor);
+ return new_req;
}
-TRP_RC trp_inforec_set_trust_router(TRP_INFOREC *rec, TR_NAME *trust_router)
+void trp_req_free(TRP_REQ *req)
{
- switch (rec->type) {
- case TRP_INFOREC_TYPE_ROUTE:
- if (rec->data.route!=NULL) {
- rec->data.route->trust_router=trust_router;
- return TRP_SUCCESS;
- }
- break;
- default:
- break;
- }
- return TRP_ERROR;
+ if (req!=NULL)
+ talloc_free(req);
}
-unsigned int trp_inforec_get_metric(TRP_INFOREC *rec)
+TR_NAME *trp_req_get_comm(TRP_REQ *req)
{
- switch (rec->type) {
- case TRP_INFOREC_TYPE_ROUTE:
- if (rec->data.route!=NULL)
- return rec->data.route->metric;
- break;
- default:
- break;
- }
- return TRP_METRIC_INVALID;
+ if (req!=NULL)
+ return req->comm;
+ else
+ return NULL;
}
-TRP_RC trp_inforec_set_metric(TRP_INFOREC *rec, unsigned int metric)
+void trp_req_set_comm(TRP_REQ *req, TR_NAME *comm)
{
- switch (rec->type) {
- case TRP_INFOREC_TYPE_ROUTE:
- if (rec->data.route!=NULL) {
- rec->data.route->metric=metric;
- return TRP_SUCCESS;
- }
- break;
- default:
- break;
- }
- return TRP_ERROR;
+ if (req)
+ req->comm=comm;
}
-unsigned int trp_inforec_get_interval(TRP_INFOREC *rec)
+TR_NAME *trp_req_get_realm(TRP_REQ *req)
{
- switch (rec->type) {
- case TRP_INFOREC_TYPE_ROUTE:
- if (rec->data.route!=NULL)
- return rec->data.route->interval;
- break;
- default:
- break;
- }
- return TRP_INTERVAL_INVALID;
+ if (req!=NULL)
+ return req->realm;
+ else
+ return NULL;
}
-TRP_RC trp_inforec_set_interval(TRP_INFOREC *rec, unsigned int interval)
-{
- switch (rec->type) {
- case TRP_INFOREC_TYPE_ROUTE:
- if (rec->data.route!=NULL) {
- rec->data.route->interval=interval;
- return TRP_SUCCESS;
- default:
- break;
- }
- break;
- }
- return TRP_ERROR;
-}
-/* for internal use only; must set rec->type before calling this */
-static TRP_RC trp_inforec_set_data(TRP_INFOREC *rec, void *data)
+void trp_req_set_realm(TRP_REQ *req, TR_NAME *realm)
{
- if (data==NULL)
- return TRP_ERROR;
-
- switch (rec->type) {
- case TRP_INFOREC_TYPE_ROUTE:
- rec->data.route=talloc_get_type(data, TRP_INFOREC_ROUTE);
- break;
- default:
- return TRP_BADTYPE;
- }
- return TRP_SUCCESS;
+ if (req)
+ req->realm=realm;
}
-/* generic record type */
-TRP_INFOREC *trp_inforec_new(TALLOC_CTX *mem_ctx, TRP_INFOREC_TYPE type)
+TR_NAME *trp_req_get_peer(TRP_REQ *req)
{
- TRP_INFOREC *new_rec=talloc(mem_ctx, TRP_INFOREC);
- struct trp_inforec_type_entry *dtype=get_trp_inforec_type_entry(type);
-
- if ((new_rec != NULL) && (dtype->type != TRP_INFOREC_TYPE_UNKNOWN)) {
- new_rec->next=NULL;
- new_rec->type=type;
- if (dtype->allocate!=NULL) {
- if (TRP_SUCCESS!=trp_inforec_set_data(new_rec, dtype->allocate(new_rec))) {
- talloc_free(new_rec);
- new_rec=NULL;
- }
- }
- }
- return new_rec;
+ if (req!=NULL)
+ return req->peer;
+ else
+ return NULL;
}
-void trp_inforec_free(TRP_INFOREC *rec)
-{
- if (rec!=NULL)
- talloc_free(rec);
-}
-TRP_UPD *trp_upd_new(TALLOC_CTX *mem_ctx)
+void trp_req_set_peer(TRP_REQ *req, TR_NAME *peer)
{
- TRP_UPD *new_body=talloc(mem_ctx, TRP_UPD);
-
- if (new_body!=NULL) {
- new_body->records=NULL;
- }
- return new_body;
+ if (req)
+ req->peer=peer;
}
-void trp_upd_free(TRP_UPD *update)
+/* Defines what we use as a wildcard for realm or community name.
+ * Must not be a valid name for either of those. Currently, we
+ * use the empty string. */
+static int trp_req_name_is_wildcard(TR_NAME *name)
{
- if (update!=NULL)
- talloc_free(update);
+ return (name!=NULL) && (name->len==0);
}
-
-/* pretty print */
-static void trp_inforec_route_print(TRP_INFOREC_DATA data)
+int trp_req_is_wildcard(TRP_REQ *req)
{
- if (data.route!=NULL) {
- printf(" community=%.*s\n realm=%.*s\n trust_router=%.*s\n metric=%d\n interval=%d]\n",
- data.route->comm->len, data.route->comm->buf,
- data.route->realm->len, data.route->realm->buf,
- data.route->trust_router->len, data.route->trust_router->buf,
- data.route->metric, data.route->interval);
- }
+ return (req!=NULL) && trp_req_name_is_wildcard(req->comm) && trp_req_name_is_wildcard(req->realm);
}
-
-static int trp_req_destructor(void *object)
+TRP_RC trp_req_make_wildcard(TRP_REQ *req)
{
- TRP_REQ *body=talloc_get_type_abort(object, TRP_REQ);
-
- /* clean up TR_NAME data, which are not managed by talloc */
- if (body->comm != NULL) {
- tr_free_name(body->comm);
- body->comm=NULL;
- tr_debug("trp_req_destructor: freed community");
- }
- if (body->realm != NULL) {
- tr_free_name(body->realm);
- body->realm=NULL;
- tr_debug("trp_req_destructor: freed realm");
- }
- return 0;
-}
+ if (req==NULL)
+ return TRP_BADARG;
-TRP_REQ *trp_req_new(TALLOC_CTX *mem_ctx)
-{
- TRP_REQ *new_body=talloc(mem_ctx, TRP_REQ);
+ req->comm=tr_new_name("");
+ if (req->comm==NULL)
+ return TRP_NOMEM;
- if (new_body != NULL) {
- new_body->comm=NULL;
- new_body->realm=NULL;
+ req->realm=tr_new_name("");
+ if (req->realm==NULL) {
+ tr_free_name(req->comm);
+ req->comm=NULL;
+ return TRP_NOMEM;
}
- talloc_set_destructor((void *)new_body, trp_req_destructor);
- return new_body;
-}
-
-void trp_req_free(TRP_REQ *req)
-{
- if (req!=NULL)
- talloc_free(req);
+ return TRP_SUCCESS;
}
-