Commit d6b3ab99 authored by Matthieu Boutier's avatar Matthieu Boutier Committed by Juliusz Chroboczek

Add function to compare prefixes, with an enumeration.

parent 740e8228
......@@ -28,6 +28,7 @@ THE SOFTWARE.
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
......@@ -488,3 +489,30 @@ daemonise()
return 1;
}
enum prefix_status
prefix_cmp(const unsigned char *p1, unsigned char plen1,
const unsigned char *p2, unsigned char plen2)
{
int min = MIN(plen1, plen2);
unsigned char mask = 0xFF;
int i = 0;
while(i < min / 8) {
if(p1[i] != p2[i])
return PST_DISJOINT;
i++;
}
min -= i * 8;
if(min != 0) {
mask <<= 8 - min;
if((p1[i] & mask) != (p2[i] & mask))
return PST_DISJOINT;
}
if(plen1 < plen2)
return PST_LESS_SPECIFIC;
if(plen1 > plen2)
return PST_MORE_SPECIFIC;
return PST_EQUALS;
}
......@@ -112,6 +112,16 @@ int v4mapped(const unsigned char *address) ATTRIBUTE ((pure));
void v4tov6(unsigned char *dst, const unsigned char *src);
int daemonise(void);
enum prefix_status {
PST_DISJOINT = 1 << 0,
PST_EQUALS = 1 << 1,
PST_MORE_SPECIFIC = 1 << 2,
PST_LESS_SPECIFIC = 1 << 3,
};
enum prefix_status
prefix_cmp(const unsigned char *p1, unsigned char plen1,
const unsigned char *p2, unsigned char plen2);
/* If debugging is disabled, we want to avoid calling format_address
for every omitted debugging message. So debug is a macro. But
vararg macros are not portable. */
......
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