centralized ID type specific method assignement in generic constructor

This commit is contained in:
Martin Willi
2009-07-06 13:15:29 +02:00
parent bbf6a4c0ff
commit 0c31b9db84
+69 -105
View File
@@ -489,7 +489,7 @@ static id_type_t get_type(private_identification_t *this)
} }
/** /**
* Implementation of identification_t.contains_wildcards fro ID_DER_ASN1_DN. * Implementation of identification_t.contains_wildcards for ID_DER_ASN1_DN.
*/ */
static bool contains_wildcards_dn(private_identification_t *this) static bool contains_wildcards_dn(private_identification_t *this)
{ {
@@ -512,22 +512,11 @@ static bool contains_wildcards_dn(private_identification_t *this)
} }
/** /**
* Implementation of identification_t.contains_wildcards. * Implementation of identification_t.contains_wildcards using memchr(*).
*/ */
static bool contains_wildcards(private_identification_t *this) static bool contains_wildcards_memchr(private_identification_t *this)
{ {
switch (this->type) return memchr(this->encoded.ptr, '*', this->encoded.len) != NULL;
{
case ID_ANY:
return TRUE;
case ID_FQDN:
case ID_RFC822_ADDR:
return memchr(this->encoded.ptr, '*', this->encoded.len) != NULL;
case ID_DER_ASN1_DN:
return contains_wildcards_dn(this);
default:
return FALSE;
}
} }
/** /**
@@ -686,7 +675,7 @@ static id_match_t matches_binary(private_identification_t *this,
* Checks for a wildcard in other-string, and compares it against this-string. * Checks for a wildcard in other-string, and compares it against this-string.
*/ */
static id_match_t matches_string(private_identification_t *this, static id_match_t matches_string(private_identification_t *this,
private_identification_t *other) private_identification_t *other)
{ {
u_int len = other->encoded.len; u_int len = other->encoded.len;
@@ -865,20 +854,41 @@ static void destroy(private_identification_t *this)
/** /**
* Generic constructor used for the other constructors. * Generic constructor used for the other constructors.
*/ */
static private_identification_t *identification_create(void) static private_identification_t *identification_create(id_type_t type)
{ {
private_identification_t *this = malloc_thing(private_identification_t); private_identification_t *this = malloc_thing(private_identification_t);
this->public.get_encoding = (chunk_t (*) (identification_t*))get_encoding; this->public.get_encoding = (chunk_t (*) (identification_t*))get_encoding;
this->public.get_type = (id_type_t (*) (identification_t*))get_type; this->public.get_type = (id_type_t (*) (identification_t*))get_type;
this->public.contains_wildcards = (bool (*) (identification_t *this))contains_wildcards;
this->public.create_part_enumerator = (enumerator_t*(*)(identification_t*))create_part_enumerator; this->public.create_part_enumerator = (enumerator_t*(*)(identification_t*))create_part_enumerator;
this->public.clone = (identification_t* (*) (identification_t*))clone_; this->public.clone = (identification_t* (*) (identification_t*))clone_;
this->public.destroy = (void (*) (identification_t*))destroy; this->public.destroy = (void (*) (identification_t*))destroy;
/* we use these as defaults, the may be overloaded for special ID types */
this->public.equals = (bool (*) (identification_t*,identification_t*))equals_binary;
this->public.matches = (id_match_t (*) (identification_t*,identification_t*))matches_binary;
switch (type)
{
case ID_ANY:
this->public.matches = (id_match_t (*)(identification_t*,identification_t*))matches_any;
this->public.contains_wildcards = (bool (*) (identification_t *this))return_true;
break;
case ID_FQDN:
case ID_RFC822_ADDR:
this->public.matches = (id_match_t (*)(identification_t*,identification_t*))matches_string;
this->public.equals = (bool (*)(identification_t*,identification_t*))equals_strcasecmp;
this->public.contains_wildcards = (bool (*) (identification_t *this))contains_wildcards_memchr;
break;
case ID_DER_ASN1_DN:
this->public.equals = (bool (*)(identification_t*,identification_t*))equals_dn;
this->public.matches = (id_match_t (*)(identification_t*,identification_t*))matches_dn;
this->public.contains_wildcards = (bool (*) (identification_t *this))contains_wildcards_dn;
break;
default:
this->public.equals = (bool (*) (identification_t*,identification_t*))equals_binary;
this->public.matches = (id_match_t (*) (identification_t*,identification_t*))matches_binary;
this->public.contains_wildcards = (bool (*) (identification_t *this))return_false;
break;
}
this->type = type;
this->encoded = chunk_empty; this->encoded = chunk_empty;
return this; return this;
@@ -889,8 +899,9 @@ static private_identification_t *identification_create(void)
*/ */
identification_t *identification_create_from_string(char *string) identification_t *identification_create_from_string(char *string)
{ {
private_identification_t *this = identification_create(); private_identification_t *this;
chunk_t encoded;
if (string == NULL) if (string == NULL)
{ {
string = "%any"; string = "%any";
@@ -900,15 +911,16 @@ identification_t *identification_create_from_string(char *string)
/* we interpret this as an ASCII X.501 ID_DER_ASN1_DN. /* we interpret this as an ASCII X.501 ID_DER_ASN1_DN.
* convert from LDAP style or openssl x509 -subject style to ASN.1 DN * convert from LDAP style or openssl x509 -subject style to ASN.1 DN
*/ */
if (atodn(string, &this->encoded) != SUCCESS) if (atodn(string, &encoded) == SUCCESS)
{ {
this->type = ID_KEY_ID; this = identification_create(ID_DER_ASN1_DN);
this->encoded = encoded;
}
else
{
this = identification_create(ID_KEY_ID);
this->encoded = chunk_clone(chunk_create(string, strlen(string))); this->encoded = chunk_clone(chunk_create(string, strlen(string)));
return &this->public;
} }
this->type = ID_DER_ASN1_DN;
this->public.equals = (bool (*) (identification_t*,identification_t*))equals_dn;
this->public.matches = (id_match_t (*) (identification_t*,identification_t*))matches_dn;
return &this->public; return &this->public;
} }
else if (strchr(string, '@') == NULL) else if (strchr(string, '@') == NULL)
@@ -921,50 +933,43 @@ identification_t *identification_create_from_string(char *string)
|| streq(string, "0::0")) || streq(string, "0::0"))
{ {
/* any ID will be accepted */ /* any ID will be accepted */
this->type = ID_ANY; this = identification_create(ID_ANY);
this->public.matches = (id_match_t (*)
(identification_t*,identification_t*))matches_any;
return &this->public; return &this->public;
} }
else else
{ {
if (strchr(string, ':') == NULL) if (strchr(string, ':') == NULL)
{ {
/* try IPv4 */
struct in_addr address; struct in_addr address;
chunk_t chunk = {(void*)&address, sizeof(address)}; chunk_t chunk = {(void*)&address, sizeof(address)};
if (inet_pton(AF_INET, string, &address) <= 0) if (inet_pton(AF_INET, string, &address) > 0)
{ { /* is IPv4 */
/* not IPv4, mostly FQDN */ this = identification_create(ID_IPV4_ADDR);
this->type = ID_FQDN; this->encoded = chunk_clone(chunk);
this->encoded.ptr = strdup(string); }
this->encoded.len = strlen(string); else
this->public.matches = (id_match_t (*) { /* not IPv4, mostly FQDN */
(identification_t*,identification_t*))matches_string; this = identification_create(ID_FQDN);
this->public.equals = (bool (*) this->encoded = chunk_create(strdup(string), strlen(string));
(identification_t*,identification_t*))equals_strcasecmp;
return &this->public;
} }
this->encoded = chunk_clone(chunk);
this->type = ID_IPV4_ADDR;
return &this->public; return &this->public;
} }
else else
{ {
/* try IPv6 */
struct in6_addr address; struct in6_addr address;
chunk_t chunk = {(void*)&address, sizeof(address)}; chunk_t chunk = {(void*)&address, sizeof(address)};
if (inet_pton(AF_INET6, string, &address) <= 0) if (inet_pton(AF_INET6, string, &address) > 0)
{ { /* is IPv6 */
this->type = ID_KEY_ID; this = identification_create(ID_IPV6_ADDR);
this->encoded = chunk_clone(chunk_create(string, this->encoded = chunk_clone(chunk);
strlen(string))); }
return &this->public; else
{ /* not IPv4/6 fallback to KEY_ID */
this = identification_create(ID_KEY_ID);
this->encoded = chunk_create(strdup(string), strlen(string));
} }
this->encoded = chunk_clone(chunk);
this->type = ID_IPV6_ADDR;
return &this->public; return &this->public;
} }
} }
@@ -975,33 +980,24 @@ identification_t *identification_create_from_string(char *string)
{ {
if (*(string + 1) == '#') if (*(string + 1) == '#')
{ {
this = identification_create(ID_KEY_ID);
string += 2; string += 2;
this->type = ID_KEY_ID;
this->encoded = chunk_from_hex( this->encoded = chunk_from_hex(
chunk_create(string, strlen(string)), NULL); chunk_create(string, strlen(string)), NULL);
return &this->public; return &this->public;
} }
else else
{ {
this->type = ID_FQDN; this = identification_create(ID_FQDN);
this->encoded.ptr = strdup(string + 1); string += 1;
this->encoded.len = strlen(string + 1); this->encoded = chunk_create(strdup(string), strlen(string));
this->public.matches = (id_match_t (*)
(identification_t*,identification_t*))matches_string;
this->public.equals = (bool (*)
(identification_t*,identification_t*))equals_strcasecmp;
return &this->public; return &this->public;
} }
} }
else else
{ {
this->type = ID_RFC822_ADDR; this = identification_create(ID_RFC822_ADDR);
this->encoded.ptr = strdup(string); this->encoded = chunk_create(strdup(string), strlen(string));
this->encoded.len = strlen(string);
this->public.matches = (id_match_t (*)
(identification_t*,identification_t*))matches_string;
this->public.equals = (bool (*)
(identification_t*,identification_t*))equals_strcasecmp;
return &this->public; return &this->public;
} }
} }
@@ -1010,42 +1006,10 @@ identification_t *identification_create_from_string(char *string)
/* /*
* Described in header. * Described in header.
*/ */
identification_t *identification_create_from_encoding(id_type_t type, chunk_t encoded) identification_t *identification_create_from_encoding(id_type_t type,
chunk_t encoded)
{ {
private_identification_t *this = identification_create(); private_identification_t *this = identification_create(type);
this->type = type;
switch (type)
{
case ID_ANY:
this->public.matches = (id_match_t (*)
(identification_t*,identification_t*))matches_any;
break;
case ID_FQDN:
case ID_RFC822_ADDR:
this->public.matches = (id_match_t (*)
(identification_t*,identification_t*))matches_string;
this->public.equals = (bool (*)
(identification_t*,identification_t*))equals_strcasecmp;
break;
case ID_DER_ASN1_DN:
this->public.equals = (bool (*)
(identification_t*,identification_t*))equals_dn;
this->public.matches = (id_match_t (*)
(identification_t*,identification_t*))matches_dn;
break;
case ID_IPV4_ADDR:
case ID_IPV6_ADDR:
case ID_DER_ASN1_GN:
case ID_KEY_ID:
case ID_DER_ASN1_GN_URI:
case ID_PUBKEY_INFO_SHA1:
case ID_PUBKEY_SHA1:
case ID_CERT_DER_SHA1:
case ID_IETF_ATTR_STRING:
default:
break;
}
/* apply encoded chunk */ /* apply encoded chunk */
if (type != ID_ANY) if (type != ID_ANY)