#include <tr_filter.h>
#include <trp_internal.h>
#include <tid_internal.h>
+#include <tr_inet_util.h>
#include <tr_debug.h>
/* Function types for handling filter fields generally. All target values
return tr_dup_name(trp_inforec_get_owner_realm(target->trp_inforec));
}
+/** Generic handlers for host:port fields*/
+static TR_NAME *tr_ff_get_hostname_and_port(TR_NAME *hn, int port)
+{
+ return tr_hostname_and_port_to_name(hn, port);
+}
+
+static int tr_ff_cmp_hostname_and_port(TR_NAME *hn, int port, int default_port, TR_NAME *val)
+{
+ int cmp = -1;
+ TR_NAME *n = NULL;
+
+ /* allow a match without :port if the default port is in use */
+ if ((port == default_port) && (tr_name_cmp(hn, val) == 0))
+ return 0;
+
+ /* need to match with the :port */
+ n = tr_ff_get_hostname_and_port(hn, port);
+
+ if (n) {
+ cmp = tr_name_cmp(n, val);
+ tr_free_name(n);
+ }
+ return cmp;
+}
+
/** Handlers for TRP trust_router field */
static int tr_ff_cmp_trp_trust_router(TR_FILTER_TARGET *target, TR_NAME *val)
{
- return tr_name_cmp(trp_inforec_get_trust_router(target->trp_inforec), val);
+ return tr_ff_cmp_hostname_and_port(trp_inforec_get_trust_router(target->trp_inforec),
+ trp_inforec_get_trust_router_port(target->trp_inforec),
+ TRP_PORT,
+ val);
}
static TR_NAME *tr_ff_get_trp_trust_router(TR_FILTER_TARGET *target)
{
- return tr_dup_name(trp_inforec_get_trust_router(target->trp_inforec));
+ return tr_ff_get_hostname_and_port(trp_inforec_get_trust_router(target->trp_inforec),
+ trp_inforec_get_trust_router_port(target->trp_inforec));
+}
+
+/** Handlers for TRP next_hop field */
+static int tr_ff_cmp_trp_next_hop(TR_FILTER_TARGET *target, TR_NAME *val)
+{
+ return tr_ff_cmp_hostname_and_port(trp_inforec_get_next_hop(target->trp_inforec),
+ trp_inforec_get_next_hop_port(target->trp_inforec),
+ TID_PORT,
+ val);
+}
+
+static TR_NAME *tr_ff_get_trp_next_hop(TR_FILTER_TARGET *target)
+{
+ return tr_ff_get_hostname_and_port(trp_inforec_get_next_hop(target->trp_inforec),
+ trp_inforec_get_next_hop_port(target->trp_inforec));
}
/** Handlers for TRP owner_contact field */
{TR_FILTER_TYPE_TRP_INBOUND, "trust_router", tr_ff_cmp_trp_trust_router, tr_ff_get_trp_trust_router},
{TR_FILTER_TYPE_TRP_OUTBOUND, "trust_router", tr_ff_cmp_trp_trust_router, tr_ff_get_trp_trust_router},
+ /* next_hop */
+ {TR_FILTER_TYPE_TRP_INBOUND, "next_hop", tr_ff_cmp_trp_next_hop, tr_ff_get_trp_next_hop},
+ {TR_FILTER_TYPE_TRP_OUTBOUND, "next_hop", tr_ff_cmp_trp_next_hop, tr_ff_get_trp_next_hop},
+
/* owner_realm */
{TR_FILTER_TYPE_TRP_INBOUND, "owner_realm", tr_ff_cmp_trp_owner_realm, tr_ff_get_trp_owner_realm},
{TR_FILTER_TYPE_TRP_OUTBOUND, "owner_realm", tr_ff_cmp_trp_owner_realm, tr_ff_get_trp_owner_realm},
/* Step through filter lines looking for a match. If a line matches, retval
* will be set to TR_FILTER_MATCH, so stop then. */
- this_fline = tr_filter_iter_first(filt_iter, filt);
- while(this_fline) {
+ for (this_fline = tr_filter_iter_first(filt_iter, filt);
+ this_fline != NULL;
+ this_fline = tr_filter_iter_next(filt_iter)) {
/* Assume we are going to succeed. If any specs fail to match, we'll set
* this to TR_FILTER_NO_MATCH. */
retval=TR_FILTER_MATCH;
- this_fspec = tr_fline_iter_first(fline_iter, this_fline);
- while(this_fspec) {
+ for (this_fspec = tr_fline_iter_first(fline_iter, this_fline);
+ this_fspec != NULL;
+ this_fspec = tr_fline_iter_next(fline_iter)) {
if (!tr_fspec_matches(this_fspec, filt->type, target)) {
retval=TR_FILTER_NO_MATCH; /* set this in case this is the last filter line */
break; /* give up on this filter line */
}
- this_fspec = tr_fline_iter_next(fline_iter);
}
if (retval==TR_FILTER_MATCH)
break;
- this_fline = tr_filter_iter_next(filt_iter);
}
if (retval==TR_FILTER_MATCH) {
talloc_free(fspec);
}
+/**
+ * Helper for tr_fspec_destructor - calls tr_free_name on its first argument
+ *
+ * @param item void pointer to a TR_NAME
+ * @param cookie ignored
+ */
+static void fspec_destruct_helper(void *item, void *cookie)
+{
+ TR_NAME *name = (TR_NAME *) item;
+ tr_free_name(name);
+}
static int tr_fspec_destructor(void *obj)
{
TR_FSPEC *fspec = talloc_get_type_abort(obj, TR_FSPEC);
tr_free_name(fspec->field);
if (fspec->match)
- g_ptr_array_unref(fspec->match);
+ tr_list_foreach(fspec->match, fspec_destruct_helper, NULL);
return 0;
}
if (fspec != NULL) {
fspec->field = NULL;
- fspec->match = g_ptr_array_new_with_free_func((GDestroyNotify) tr_free_name);
+ fspec->match = tr_list_new(fspec);
if (fspec->match == NULL) {
talloc_free(fspec);
return NULL;
return fspec;
}
-TR_NAME *tr_fspec_add_match(TR_FSPEC *fspec, TR_NAME *match)
+/* Helper function and cookie structure for finding a match. The helper is called
+ * for every item in the match list, even after a match is found. If a match is found,
+ * match should be pointed to the matching item. If this is not NULL, do not change it
+ * because a match has already been found. */
+struct fspec_match_cookie { TR_NAME *name; TR_NAME *match;};
+static void fspec_match_helper(void *item, void *data)
{
- guint old_len = fspec->match->len;
- g_ptr_array_add(fspec->match, match);
-
- if (fspec->match->len == old_len)
- return NULL; /* failed to add */
-
- return match;
+ TR_NAME *this_name = (TR_NAME *) item;
+ struct fspec_match_cookie *cookie = (struct fspec_match_cookie *) data;
+ if (cookie->match == NULL) {
+ if (tr_name_prefix_wildcard_match(cookie->name, this_name))
+ cookie->match = this_name;
+ }
}
-
/* returns 1 if the spec matches */
int tr_fspec_matches(TR_FSPEC *fspec, TR_FILTER_TYPE ftype, TR_FILTER_TARGET *target)
{
struct tr_filter_field_entry *field=NULL;
- TR_NAME *name=NULL;
- int retval=0;
-
- guint ii=0;
+ struct fspec_match_cookie cookie = {0};
if (fspec==NULL)
return 0;
return 0;
}
- name=field->get(target);
- if (name==NULL)
+ cookie.name = field->get(target);
+ if (cookie.name==NULL)
return 0; /* if there's no value, there's no match */
- if (g_ptr_array_find_with_equal_func(fspec->match,
- name,
- (GEqualFunc) tr_name_prefix_wildcard_match,
- &ii)) {
- retval=1;
+ cookie.match = NULL;
+ tr_list_foreach(fspec->match,
+ fspec_match_helper,
+ &cookie);
+ if (cookie.match) {
tr_debug("tr_fspec_matches: Field %.*s value \"%.*s\" matches \"%.*s\" for %s filter.",
fspec->field->len, fspec->field->buf,
- name->len, name->buf,
- ((TR_NAME *)g_ptr_array_index(fspec->match,ii))->len,
- ((TR_NAME *)g_ptr_array_index(fspec->match,ii))->buf,
+ cookie.name->len, cookie.name->buf,
+ cookie.match->len, cookie.match->buf,
tr_filter_type_to_string(ftype));
- }
-
- if (!retval) {
+ } else {
tr_debug("tr_fspec_matches: Field %.*s value \"%.*s\" does not match for %s filter.",
fspec->field->len, fspec->field->buf,
- name->len, name->buf,
+ cookie.name->len, cookie.name->buf,
tr_filter_type_to_string(ftype));
}
- tr_free_name(name);
- return retval;
+ return (cookie.match != NULL);
}
void tr_fline_free(TR_FLINE *fline)
talloc_free(fline);
}
-TR_FSPEC *tr_fline_add_spec(TR_FLINE *fline, TR_FSPEC *spec)
-{
- guint old_len = fline->specs->len;
- g_ptr_array_add(fline->specs, spec);
-
- if (old_len == fline->specs->len)
- return NULL; /* failed to add */
-
- talloc_steal(fline, spec);
- return spec;
-}
-
-static int tr_fline_destructor(void *object)
-{
- TR_FLINE *fline = talloc_get_type_abort(object, TR_FLINE);
- if (fline->specs)
- g_ptr_array_unref(fline->specs);
- return 0;
-}
-
TR_FLINE *tr_fline_new(TALLOC_CTX *mem_ctx)
{
TR_FLINE *fl = talloc(mem_ctx, TR_FLINE);
fl->action = TR_FILTER_ACTION_UNKNOWN;
fl->realm_cons = NULL;
fl->domain_cons = NULL;
- fl->specs = g_ptr_array_new();
+ fl->specs = tr_list_new(fl);
if (fl->specs == NULL) {
talloc_free(fl);
return NULL;
}
- talloc_set_destructor((void *)fl, tr_fline_destructor);
}
return fl;
}
-static int tr_filter_destructor(void *object)
-{
- TR_FILTER *filt = talloc_get_type_abort(object, TR_FILTER);
- if (filt->lines)
- g_ptr_array_unref(filt->lines);
- return 0;
-}
TR_FILTER *tr_filter_new(TALLOC_CTX *mem_ctx)
{
TR_FILTER *f = talloc(mem_ctx, TR_FILTER);
if (f != NULL) {
f->type = TR_FILTER_TYPE_UNKNOWN;
- f->lines = g_ptr_array_new();
+ f->lines = tr_list_new(f);
if (f->lines == NULL) {
talloc_free(f);
return NULL;
}
- talloc_set_destructor((void *)f, tr_filter_destructor);
}
return f;
}
}
/**
- * Add a TR_FLINE to a filter
- *
- * Steals the line into its context on success
- *
- * @param filt
- * @param line
- * @return line, or null on failure
- */
-TR_FLINE *tr_filter_add_line(TR_FILTER *filt, TR_FLINE *line)
-{
- guint old_len = filt->lines->len;
- g_ptr_array_add(filt->lines, line);
-
- if (old_len == filt->lines->len)
- return NULL; /* failed to add */
-
- talloc_steal(filt, line);
- return line;
-}
-
-/**
- * Iterator for TR_FLINES in a TR_FILTER
- *
- * @param mem_ctx
- * @return
- */
-TR_FILTER_ITER *tr_filter_iter_new(TALLOC_CTX *mem_ctx)
-{
- TR_FILTER_ITER *iter = talloc(mem_ctx, TR_FILTER_ITER);
- if (iter) {
- iter->filter = NULL;
- }
- return iter;
-}
-
-void tr_filter_iter_free(TR_FILTER_ITER *iter)
-{
- talloc_free(iter);
-}
-
-TR_FLINE *tr_filter_iter_first(TR_FILTER_ITER *iter, TR_FILTER *filter)
-{
- if (!iter || !filter)
- return NULL;
-
- iter->filter = filter;
- iter->ii = 0;
- return tr_filter_iter_next(iter);
-}
-
-TR_FLINE *tr_filter_iter_next(TR_FILTER_ITER *iter)
-{
- if (!iter)
- return NULL;
-
- if (iter->ii < iter->filter->lines->len)
- return g_ptr_array_index(iter->filter->lines, iter->ii++);
- return NULL;
-}
-
-TR_FLINE_ITER *tr_fline_iter_new(TALLOC_CTX *mem_ctx)
-{
- TR_FLINE_ITER *iter = talloc(mem_ctx, TR_FLINE_ITER);
- if (iter) {
- iter->fline = NULL;
- }
- return iter;
-}
-
-void tr_fline_iter_free(TR_FLINE_ITER *iter)
-{
- talloc_free(iter);
-}
-
-TR_FSPEC * tr_fline_iter_first(TR_FLINE_ITER *iter, TR_FLINE *fline)
-{
- if (!iter || !fline)
- return NULL;
-
- iter->fline = fline;
- iter->ii = 0;
- return tr_fline_iter_next(iter);
-}
-
-TR_FSPEC * tr_fline_iter_next(TR_FLINE_ITER *iter)
-{
- if (!iter)
- return NULL;
-
- if (iter->ii < iter->fline->specs->len)
- return g_ptr_array_index(iter->fline->specs, iter->ii++);
- return NULL;
-}
-
-TR_FSPEC_ITER *tr_fspec_iter_new(TALLOC_CTX *mem_ctx)
-{
- TR_FSPEC_ITER *iter = talloc(mem_ctx, TR_FSPEC_ITER);
- if (iter) {
- iter->fspec = NULL;
- }
- return iter;
-}
-
-void tr_fspec_iter_free(TR_FSPEC_ITER *iter)
-{
- talloc_free(iter);
-}
-
-TR_NAME *tr_fspec_iter_first(TR_FSPEC_ITER *iter, TR_FSPEC *fspec)
-{
- if (!iter || !fspec)
- return NULL;
-
- iter->fspec = fspec;
- iter->ii = 0;
- return tr_fspec_iter_next(iter);
-}
-
-TR_NAME *tr_fspec_iter_next(TR_FSPEC_ITER *iter)
-{
- if (!iter)
- return NULL;
-
- if (iter->ii < iter->fspec->match->len)
- return g_ptr_array_index(iter->fspec->match, iter->ii++);
- return NULL;
-}
-
-/**
* Check that a filter is valid, i.e., can be processed.
*
* @param filt Filter to verify
return 0; /* if we get here, either TR_FILTER_TYPE_UNKNOWN or an invalid value was found */
}
- this_fline = tr_filter_iter_first(filt_iter, filt);
- while(this_fline) {
+ for (this_fline = tr_filter_iter_first(filt_iter, filt);
+ this_fline != NULL;
+ this_fline = tr_filter_iter_next(filt_iter)) {
/* check that we recognize the action */
switch(this_fline->action) {
case TR_FILTER_ACTION_ACCEPT:
return 0;
}
- this_fspec = tr_fline_iter_first(fline_iter, this_fline);
- while(this_fspec) {
+ for (this_fspec = tr_fline_iter_first(fline_iter, this_fline);
+ this_fspec != NULL;
+ this_fspec = tr_fline_iter_next(fline_iter)) {
if (!tr_filter_validate_spec_field(filt->type, this_fspec)) {
talloc_free(tmp_ctx);
return 0;
}
/* check that at least one match is defined*/
- if (this_fspec->match->len == 0) {
+ if (tr_list_length(this_fspec->match) == 0) {
talloc_free(tmp_ctx);
return 0;
}
- this_fspec = tr_fline_iter_next(fline_iter);
}
- this_fline = tr_filter_iter_next(filt_iter);
}
/* We ran the gauntlet. Success! */