lnav/src/collation-functions.cc

156 lines
4.8 KiB
C++
Raw Normal View History

/**
* Copyright (c) 2013, Timothy Stack
*
* All rights reserved.
2013-05-28 04:35:00 +00:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
2013-05-28 04:35:00 +00:00
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Timothy Stack nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
2013-05-28 04:35:00 +00:00
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2022-03-16 22:38:08 +00:00
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @file logfile_sub_source.hh
*/
#include <arpa/inet.h>
#include <netinet/in.h>
2022-03-16 22:38:08 +00:00
#include <sqlite3.h>
#include <string.h>
2013-06-10 13:39:52 +00:00
#include <sys/socket.h>
2021-06-03 04:09:50 +00:00
#include "base/strnatcmp.h"
2022-03-16 22:38:08 +00:00
#include "config.h"
#include "log_level.hh"
2022-03-25 23:38:11 +00:00
constexpr int MAX_ADDR_LEN = 128;
2022-03-16 22:38:08 +00:00
static int
try_inet_pton(int p_len, const char* p, char* n)
{
2022-03-16 22:38:08 +00:00
static int ADDR_FAMILIES[] = {AF_INET, AF_INET6};
char buf[MAX_ADDR_LEN + 1];
2022-03-16 22:38:08 +00:00
int retval = AF_MAX;
2013-05-28 04:35:00 +00:00
strncpy(buf, p, p_len);
buf[p_len] = '\0';
2017-04-06 04:42:54 +00:00
for (int family : ADDR_FAMILIES) {
if (inet_pton(family, buf, n) == 1) {
retval = family;
2013-05-28 04:35:00 +00:00
break;
}
}
2013-05-28 04:35:00 +00:00
return retval;
}
2022-03-16 22:38:08 +00:00
static int
convert_v6_to_v4(int family, char* n)
{
2022-03-16 22:38:08 +00:00
struct in6_addr* ia = (struct in6_addr*) n;
2022-03-16 22:38:08 +00:00
if (family == AF_INET6
&& (IN6_IS_ADDR_V4COMPAT(ia) || IN6_IS_ADDR_V4MAPPED(ia)))
{
2013-05-28 04:35:00 +00:00
family = AF_INET;
memmove(n, n + 12, sizeof(struct in_addr));
}
2013-05-28 04:35:00 +00:00
return family;
}
2022-03-16 22:38:08 +00:00
static int
ipaddress(void* ptr, int a_len, const void* a_in, int b_len, const void* b_in)
{
2022-03-16 22:38:08 +00:00
char a_addr[sizeof(struct in6_addr)], b_addr[sizeof(struct in6_addr)];
const char *a_str = (const char*) a_in, *b_str = (const char*) b_in;
int a_family, b_family, retval;
2013-05-28 04:35:00 +00:00
2017-04-06 04:42:54 +00:00
if ((a_len > MAX_ADDR_LEN) || (b_len > MAX_ADDR_LEN)) {
return strnatcasecmp(a_len, a_str, b_len, b_str);
2013-05-28 04:35:00 +00:00
}
2017-04-06 04:42:54 +00:00
int v4res = 0;
if (ipv4cmp(a_len, a_str, b_len, b_str, &v4res)) {
return v4res;
}
2013-05-28 04:35:00 +00:00
a_family = try_inet_pton(a_len, a_str, a_addr);
b_family = try_inet_pton(b_len, b_str, b_addr);
if (a_family == AF_MAX && b_family == AF_MAX) {
return strnatcasecmp(a_len, a_str, b_len, b_str);
2022-03-16 22:38:08 +00:00
} else if (a_family == AF_MAX && b_family != AF_MAX) {
2013-05-28 04:35:00 +00:00
retval = -1;
2022-03-16 22:38:08 +00:00
} else if (a_family != AF_MAX && b_family == AF_MAX) {
2013-05-28 04:35:00 +00:00
retval = 1;
2022-03-16 22:38:08 +00:00
} else {
2013-05-28 04:35:00 +00:00
a_family = convert_v6_to_v4(a_family, a_addr);
b_family = convert_v6_to_v4(b_family, b_addr);
if (a_family == b_family) {
2022-03-16 22:38:08 +00:00
retval = memcmp(a_addr,
b_addr,
a_family == AF_INET ? sizeof(struct in_addr)
: sizeof(struct in6_addr));
} else if (a_family == AF_INET) {
2013-05-28 04:35:00 +00:00
retval = -1;
2022-03-16 22:38:08 +00:00
} else {
2013-05-28 04:35:00 +00:00
retval = 1;
}
}
return retval;
}
2022-03-16 22:38:08 +00:00
static int
sql_strnatcmp(
void* ptr, int a_len, const void* a_in, int b_len, const void* b_in)
{
2022-03-16 22:38:08 +00:00
return strnatcmp(a_len, (char*) a_in, b_len, (char*) b_in);
}
2022-03-16 22:38:08 +00:00
static int
sql_strnatcasecmp(
void* ptr, int a_len, const void* a_in, int b_len, const void* b_in)
{
2022-03-16 22:38:08 +00:00
return strnatcasecmp(a_len, (char*) a_in, b_len, (char*) b_in);
}
2022-03-16 22:38:08 +00:00
static int
sql_loglevelcmp(
void* ptr, int a_len, const void* a_in, int b_len, const void* b_in)
{
2022-03-16 22:38:08 +00:00
return levelcmp((const char*) a_in, a_len, (const char*) b_in, b_len);
}
2022-03-16 22:38:08 +00:00
int
register_collation_functions(sqlite3* db)
{
sqlite3_create_collation(db, "ipaddress", SQLITE_UTF8, nullptr, ipaddress);
2022-03-16 22:38:08 +00:00
sqlite3_create_collation(
db, "naturalcase", SQLITE_UTF8, nullptr, sql_strnatcmp);
sqlite3_create_collation(
db, "naturalnocase", SQLITE_UTF8, nullptr, sql_strnatcasecmp);
sqlite3_create_collation(
db, "loglevel", SQLITE_UTF8, nullptr, sql_loglevelcmp);
2013-05-28 04:35:00 +00:00
return 0;
}