0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-19 00:10:59 +01:00

Updates to match utilities.

This commit is contained in:
Jason Volk 2016-08-18 03:08:57 -07:00
parent b7a84d3650
commit e1cccd482b
2 changed files with 127 additions and 76 deletions

View file

@ -39,11 +39,16 @@ namespace ircd {
* match_cidr - compares u!h@addr with u!h@addr/cidr * match_cidr - compares u!h@addr with u!h@addr/cidr
* match_ips - compares addr with addr/cidr in ascii form * match_ips - compares addr with addr/cidr in ascii form
*/ */
extern int match(const char *mask, const char *name); bool match(const char *const &mask, const char *const &name);
extern int match_mask(const char *oldmask, const char *newmask); bool match(const std::string &mask, const std::string &name);
extern int match_esc(const char *mask, const char *name); bool match_mask(const char *const &mask, const char *const &name);
extern int match_cidr(const char *mask, const char *name); bool match_mask(const std::string &mask, const std::string &name);
extern int match_ips(const char *mask, const char *name); bool match_cidr(const char *const &mask, const char *const &name);
bool match_cidr(const std::string &mask, const std::string &name);
bool match_ips(const char *const &mask, const char *const &name);
bool match_ips(const std::string &mask, const std::string &name);
int match_esc(const char *const &mask, const char *const &name);
int match_esc(const std::string &mask, const std::string &name);
/* /*
* comp_with_mask - compares to IP address * comp_with_mask - compares to IP address
@ -58,37 +63,29 @@ int comp_with_mask_sock(struct sockaddr *addr, struct sockaddr *dest, unsigned i
* *
* collapse_esc() - collapse with support for escaping chars * collapse_esc() - collapse with support for escaping chars
*/ */
extern char *collapse(char *pattern); char *collapse(char *const &pattern);
extern char *collapse_esc(char *pattern); char *collapse_esc(char *const &pattern);
/* /*
* irccmp - case insensitive comparison of s1 and s2 * irccmp - case insensitive comparison of s1 and s2
*/ */
extern int irccmp(const char *s1, const char *s2); int irccmp(const char *const &s1, const char *const &s2);
/* int irccmp(const std::string &s1, const std::string &s2);
* ircncmp - counted case insensitive comparison of s1 and s2 int ircncmp(const char *const &s1, const char *const &s2, size_t n);
*/
extern int ircncmp(const char *s1, const char *s2, int n);
/* Below are used for radix trees and the like */ /* Below are used for radix trees and the like */
static inline void irccasecanon(char *str) inline void
irccasecanon(char *str)
{ {
while (*str) for (; *str; ++str)
{
*str = rfc1459::toupper(*str); *str = rfc1459::toupper(*str);
str++;
}
return;
} }
static inline void strcasecanon(char *str) inline void
strcasecanon(char *str)
{ {
while (*str) for (; *str; ++str)
{ *str = toupper(uint8_t(*str));
*str = toupper((unsigned char)*str);
str++;
}
return;
} }
} // namespace ircd } // namespace ircd

View file

@ -18,8 +18,6 @@
* *
*/ */
namespace ircd {
/* /*
* Compare if a given string (name) matches the given * Compare if a given string (name) matches the given
* mask (which can contain wild cards: '*' - match any * mask (which can contain wild cards: '*' - match any
@ -41,7 +39,9 @@ namespace ircd {
* @param[in] name String to check against \a mask. * @param[in] name String to check against \a mask.
* @return Zero if \a mask matches \a name, non-zero if no match. * @return Zero if \a mask matches \a name, non-zero if no match.
*/ */
int match(const char *mask, const char *name) bool
ircd::match(const char *const &mask,
const char *const &name)
{ {
const char *m = mask, *n = name; const char *m = mask, *n = name;
const char *m_tmp = mask, *n_tmp = name; const char *m_tmp = mask, *n_tmp = name;
@ -100,6 +100,13 @@ int match(const char *mask, const char *name)
} }
} }
bool
ircd::match(const std::string &mask,
const std::string &name)
{
return match(mask.c_str(), name.c_str());
}
/** Check a mask against a mask. /** Check a mask against a mask.
* This test checks using traditional IRC wildcards only: '*' means * This test checks using traditional IRC wildcards only: '*' means
* match zero or more characters of any type; '?' means match exactly * match zero or more characters of any type; '?' means match exactly
@ -111,7 +118,9 @@ int match(const char *mask, const char *name)
* @param[in] name New wildcard-containing mask. * @param[in] name New wildcard-containing mask.
* @return 1 if \a name is equal to or more specific than \a mask, 0 otherwise. * @return 1 if \a name is equal to or more specific than \a mask, 0 otherwise.
*/ */
int match_mask(const char *mask, const char *name) bool
ircd::match_mask(const char *const &mask,
const char *const &name)
{ {
const char *m = mask, *n = name; const char *m = mask, *n = name;
const char *m_tmp = mask, *n_tmp = name; const char *m_tmp = mask, *n_tmp = name;
@ -172,9 +181,16 @@ int match_mask(const char *mask, const char *name)
} }
} }
bool
ircd::match_mask(const std::string &mask,
const std::string &name)
{
return match_mask(mask.c_str(), name.c_str());
}
// ACK! This dies when it's less that this and we have long lines to parse
#define MATCH_MAX_CALLS 512
#define MATCH_MAX_CALLS 512 /* ACK! This dies when it's less that this
and we have long lines to parse */
/** Check a string against a mask. /** Check a string against a mask.
* This test checks using extended wildcards: '*' means match zero * This test checks using extended wildcards: '*' means match zero
* or more characters of any type; '?' means match exactly one * or more characters of any type; '?' means match exactly one
@ -190,7 +206,8 @@ int match_mask(const char *mask, const char *name)
* @return Zero if \a mask matches \a name, non-zero if no match. * @return Zero if \a mask matches \a name, non-zero if no match.
*/ */
int int
match_esc(const char *mask, const char *name) ircd::match_esc(const char *const &mask,
const char *const &name)
{ {
const unsigned char *m = (const unsigned char *)mask; const unsigned char *m = (const unsigned char *)mask;
const unsigned char *n = (const unsigned char *)name; const unsigned char *n = (const unsigned char *)name;
@ -304,40 +321,11 @@ match_esc(const char *mask, const char *name)
return 0; return 0;
} }
int comp_with_mask(void *addr, void *dest, unsigned int mask) int
ircd::match_esc(const std::string &mask,
const std::string &name)
{ {
if (memcmp(addr, dest, mask / 8) == 0) return match_esc(mask.c_str(), name.c_str());
{
int n = mask / 8;
int m = ((-1) << (8 - (mask % 8)));
if (mask % 8 == 0 || (((unsigned char *) addr)[n] & m) == (((unsigned char *) dest)[n] & m))
{
return (1);
}
}
return (0);
}
int comp_with_mask_sock(struct sockaddr *addr, struct sockaddr *dest, unsigned int mask)
{
void *iaddr = NULL;
void *idest = NULL;
if (addr->sa_family == AF_INET)
{
iaddr = &((struct sockaddr_in *)(void *)addr)->sin_addr;
idest = &((struct sockaddr_in *)(void *)dest)->sin_addr;
}
#ifdef RB_IPV6
else
{
iaddr = &((struct sockaddr_in6 *)(void *)addr)->sin6_addr;
idest = &((struct sockaddr_in6 *)(void *)dest)->sin6_addr;
}
#endif
return (comp_with_mask(iaddr, idest, mask));
} }
/* /*
@ -345,7 +333,9 @@ int comp_with_mask_sock(struct sockaddr *addr, struct sockaddr *dest, unsigned i
* *
* Input - cidr ip mask, address * Input - cidr ip mask, address
*/ */
int match_ips(const char *s1, const char *s2) bool
ircd::match_ips(const char *const &s1,
const char *const &s2)
{ {
struct rb_sockaddr_storage ipaddr, maskaddr; struct rb_sockaddr_storage ipaddr, maskaddr;
char mask[BUFSIZE]; char mask[BUFSIZE];
@ -401,13 +391,21 @@ int match_ips(const char *s1, const char *s2)
return 0; return 0;
} }
bool
ircd::match_ips(const std::string &s1,
const std::string &s2)
{
return match_ips(s1.c_str(), s2.c_str());
}
/* match_cidr() /* match_cidr()
* *
* Input - mask, address * Input - mask, address
* Ouput - 1 = Matched 0 = Did not match * Ouput - 1 = Matched 0 = Did not match
*/ */
bool
int match_cidr(const char *s1, const char *s2) ircd::match_cidr(const char *const &s1,
const char *const &s2)
{ {
struct rb_sockaddr_storage ipaddr, maskaddr; struct rb_sockaddr_storage ipaddr, maskaddr;
char mask[BUFSIZE]; char mask[BUFSIZE];
@ -477,11 +475,57 @@ int match_cidr(const char *s1, const char *s2)
return 0; return 0;
} }
bool
ircd::match_cidr(const std::string &s1,
const std::string &s2)
{
return match_cidr(s1.c_str(), s2.c_str());
}
int
ircd::comp_with_mask(void *addr, void *dest, unsigned int mask)
{
if (memcmp(addr, dest, mask / 8) == 0)
{
int n = mask / 8;
int m = ((-1) << (8 - (mask % 8)));
if (mask % 8 == 0 || (((unsigned char *) addr)[n] & m) == (((unsigned char *) dest)[n] & m))
{
return (1);
}
}
return (0);
}
int
ircd::comp_with_mask_sock(struct sockaddr *addr, struct sockaddr *dest, unsigned int mask)
{
void *iaddr = NULL;
void *idest = NULL;
if (addr->sa_family == AF_INET)
{
iaddr = &((struct sockaddr_in *)(void *)addr)->sin_addr;
idest = &((struct sockaddr_in *)(void *)dest)->sin_addr;
}
#ifdef RB_IPV6
else
{
iaddr = &((struct sockaddr_in6 *)(void *)addr)->sin6_addr;
idest = &((struct sockaddr_in6 *)(void *)dest)->sin6_addr;
}
#endif
return (comp_with_mask(iaddr, idest, mask));
}
/* collapse() /* collapse()
* *
* collapses a string containing multiple *'s. * collapses a string containing multiple *'s.
*/ */
char *collapse(char *pattern) char *
ircd::collapse(char *const &pattern)
{ {
char *p = pattern, *po = pattern; char *p = pattern, *po = pattern;
char c; char c;
@ -513,7 +557,8 @@ char *collapse(char *pattern)
* *
* The collapse() function with support for escaping characters * The collapse() function with support for escaping characters
*/ */
char *collapse_esc(char *pattern) char *
ircd::collapse_esc(char *const &pattern)
{ {
char *p = pattern, *po = pattern; char *p = pattern, *po = pattern;
char c; char c;
@ -552,7 +597,9 @@ char *collapse_esc(char *pattern)
* <0, if s1 lexicographically less than s2 * <0, if s1 lexicographically less than s2
* >0, if s1 lexicographically greater than s2 * >0, if s1 lexicographically greater than s2
*/ */
int irccmp(const char *s1, const char *s2) int
ircd::irccmp(const char *const &s1,
const char *const &s2)
{ {
const unsigned char *str1 = (const unsigned char *)s1; const unsigned char *str1 = (const unsigned char *)s1;
const unsigned char *str2 = (const unsigned char *)s2; const unsigned char *str2 = (const unsigned char *)s2;
@ -571,7 +618,17 @@ int irccmp(const char *s1, const char *s2)
return (res); return (res);
} }
int ircncmp(const char *s1, const char *s2, int n) int
ircd::irccmp(const std::string &s1,
const std::string &s2)
{
return irccmp(s1.c_str(), s2.c_str());
}
int
ircd::ircncmp(const char *const &s1,
const char *const &s2,
size_t n)
{ {
const unsigned char *str1 = (const unsigned char *)s1; const unsigned char *str1 = (const unsigned char *)s1;
const unsigned char *str2 = (const unsigned char *)s2; const unsigned char *str2 = (const unsigned char *)s2;
@ -589,6 +646,3 @@ int ircncmp(const char *s1, const char *s2, int n)
} }
return (res); return (res);
} }
}