Commit 4a1becee authored by Lubos Slovak's avatar Lubos Slovak

Added function dname_matched_labels() +refactoring

- Finding of domain name labels separated to a function

Not tested yet.

refs #5
parent 1042c78c
......@@ -138,6 +138,32 @@ static int dnslib_dname_compare_labels(const uint8_t *label1,
return 0;
}
/*----------------------------------------------------------------------------*/
static void dnslib_dname_find_labels(const dnslib_dname_t *d1,
const dnslib_dname_t *d2,
const uint8_t **labels1,
const uint8_t **labels2, int *l1, int *l2)
{
const uint8_t *name1 = dnslib_dname_name(d1);
const uint8_t *pos1 = name1;
const uint size1 = dnslib_dname_size(d1);
const uint8_t *name2 = dnslib_dname_name(d2);
const uint8_t *pos2 = name2;
const uint size2 = dnslib_dname_size(d2);
while (pos1 - name1 < size1 && *pos1 != '\0') {
labels1[(*l1)++] = pos1;
pos1 += *pos1 + 1;
}
while (pos2 - name2 < size2 && *pos2 != '\0') {
labels2[(*l2)++] = pos2;
pos2 += *pos2 + 1;
}
}
/*----------------------------------------------------------------------------*/
/* API functions */
/*----------------------------------------------------------------------------*/
......@@ -346,23 +372,7 @@ int dnslib_dname_is_subdomain(const dnslib_dname_t *sub,
int l1 = 0;
int l2 = 0;
const uint8_t *name1 = dnslib_dname_name(sub);
const uint8_t *pos1 = name1;
const uint size1 = dnslib_dname_size(sub);
const uint8_t *name2 = dnslib_dname_name(domain);
const uint8_t *pos2 = name2;
const uint size2 = dnslib_dname_size(domain);
while (pos1 - name1 < size1 && *pos1 != '\0') {
labels1[l1++] = pos1;
pos1 += *pos1 + 1;
}
while (pos2 - name2 < size2 && *pos2 != '\0') {
labels2[l2++] = pos2;
pos2 += *pos2 + 1;
}
dnslib_dname_find_labels(sub, domain, labels1, labels2, &l1, &l2);
if (l1 <= l2) { // if sub does not have more labes than domain
return 0; // it is not its subdomain
......@@ -385,6 +395,36 @@ int dnslib_dname_is_subdomain(const dnslib_dname_t *sub,
/*----------------------------------------------------------------------------*/
int dnslib_dname_matched_labels(const dnslib_dname_t *dname1,
const dnslib_dname_t *dname2)
{
// jump to the last label and store addresses of labels
// on the way there
// TODO: consider storing label offsets in the domain name structure
const uint8_t *labels1[DNSLIB_MAX_DNAME_LABELS];
const uint8_t *labels2[DNSLIB_MAX_DNAME_LABELS];
int l1 = 0;
int l2 = 0;
dnslib_dname_find_labels(dname1, dname2, labels1, labels2, &l1, &l2);
// compare labels from last to first
int matched = 0;
while (l1 > 0 && l2 > 0) {
int res = dnslib_dname_compare_labels(labels1[--l1],
labels2[--l2]);
if (res == 0) {
++matched;
} else {
break;
}
}
return matched;
}
/*----------------------------------------------------------------------------*/
void dnslib_dname_free(dnslib_dname_t **dname)
{
if (dname == NULL || *dname == NULL) {
......@@ -414,22 +454,7 @@ int dnslib_dname_compare(const dnslib_dname_t *d1, const dnslib_dname_t *d2)
int l1 = 0;
int l2 = 0;
const uint8_t *pos1 = d1->name;
const uint8_t *pos2 = d2->name;
int i = 0;
while (i < d1->size && *pos1 != '\0') {
labels1[l1++] = pos1;
pos1 += *pos1 + 1;
++i;
}
i = 0;
while (i < d2->size && *pos2 != '\0') {
labels2[l2++] = pos2;
pos2 += *pos2 + 1;
++i;
}
dnslib_dname_find_labels(d1, d2, labels1, labels2, &l1, &l2);
// compare labels from last to first
while (l1 > 0 && l2 > 0) {
......
......@@ -169,6 +169,18 @@ dnslib_dname_t *dnslib_dname_left_chop(const dnslib_dname_t *dname);
int dnslib_dname_is_subdomain(const dnslib_dname_t *sub,
const dnslib_dname_t *domain);
/*!
* \brief Returns the number of labels common for the two domain names (counted
* from the rightmost label.
*
* \param dname1 First domain name.
* \param dname2 Second domain name.
*
* \return Number of labels common for the two domain names.
*/
int dnslib_dname_matched_labels(const dnslib_dname_t *dname1,
const dnslib_dname_t *dname2);
/*!
* \brief Destroys the given domain name.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment