52 static uint32_t srep_version = 0;
54 static uint32_t SRepIncrVersion(
void)
56 return ++srep_version;
59 static uint32_t SRepGetVersion(
void)
69 static uint32_t SRepGetEffectiveVersion(
void)
74 static void SRepCIDRFreeUserData(
void *data)
80 static SCRadix4Config iprep_radix4_config = { SRepCIDRFreeUserData, NULL };
81 static SCRadix6Config iprep_radix6_config = { SRepCIDRFreeUserData, NULL };
83 static void SRepCIDRAddNetblock(
SRepCIDRTree *cidr_ctx,
char *ip,
int cat, uint8_t value)
87 FatalError(
"Error allocating memory. Exiting");
90 user_data->
version = SRepGetVersion();
91 user_data->
rep[cat] = value;
93 if (strchr(ip,
':') != NULL) {
96 &cidr_ctx->
srep_ipv6_tree[cat], &iprep_radix6_config, ip, (
void *)user_data)) {
105 &cidr_ctx->
srep_ipv4_tree[cat], &iprep_radix4_config, ip, (
void *)user_data)) {
113 static int8_t SRepCIDRGetIPv4IPRep(
SRepCIDRTree *cidr_ctx, uint8_t *ipv4_addr, uint8_t cat)
115 void *user_data = NULL;
117 if (user_data == NULL)
124 static int8_t SRepCIDRGetIPv6IPRep(
SRepCIDRTree *cidr_ctx, uint8_t *ipv6_addr, uint8_t cat)
126 void *user_data = NULL;
128 if (user_data == NULL)
141 else if (PacketIsIPv6(p))
153 else if (PacketIsIPv6(p))
164 SCLogDebug(
"effective Reputation version %u", SRepGetEffectiveVersion());
177 static void SRepInitComplete(
void)
180 SCLogDebug(
"effective Reputation version %u", SRepGetEffectiveVersion());
197 if (h->
iprep == NULL)
200 uint32_t eversion = SRepGetEffectiveVersion();
203 SCLogDebug(
"host %p has reputation version %u, "
204 "effective version is %u", h, r->
version, eversion);
212 static int SRepCatSplitLine(
char *line, uint8_t *cat,
char *shortname,
size_t shortname_len)
214 size_t line_len = strlen(line);
215 char *ptrs[2] = {NULL,NULL};
218 char *origline = line;
220 while (i < (
int)line_len) {
221 if (line[i] ==
',' || line[i] ==
'\n' || line[i] ==
'\0' || i == (
int)(line_len - 1)) {
230 if (line >= origline + line_len)
232 if (strlen(line) == 0)
252 strlcpy(shortname, ptrs[1], shortname_len);
261 static int SRepSplitLine(
SRepCIDRTree *cidr_ctx,
char *line,
Address *ip, uint8_t *cat, uint8_t *value)
263 size_t line_len = strlen(line);
264 char *ptrs[3] = {NULL,NULL,NULL};
267 char *origline = line;
269 while (i < (
int)line_len) {
270 if (line[i] ==
',' || line[i] ==
'\n' || line[i] ==
'\r' || line[i] ==
'\0' ||
271 i == (
int)(line_len - 1)) {
280 if (line >= origline + line_len)
282 if (strlen(line) == 0)
297 if (strcmp(ptrs[0],
"ip") == 0)
307 if (strchr(ptrs[0],
'/') != NULL) {
308 SRepCIDRAddNetblock(cidr_ctx, ptrs[0], c, v);
311 if (inet_pton(AF_INET, ptrs[0], &ip->
address) == 1) {
313 }
else if (inet_pton(AF_INET6, ptrs[0], &ip->
address) == 1) {
326 #define SREP_SHORTNAME_LEN 32
333 if (strcmp(srep_cat_table[cat], shortname) == 0)
340 static int SRepLoadCatFile(
const char *filename)
343 FILE *fp = fopen(filename,
"r");
346 SCLogError(
"opening ip rep file %s: %s", filename, strerror(errno));
357 static inline size_t GetEffectiveLineLen(
const char *line)
359 size_t len = strlen(line);
361 if (
len == 0 || line[0] ==
'\n' || line[0] ==
'\r' || line[0] ==
' ' || line[0] ==
'#' ||
370 char line[8192] =
"";
372 memset(&a, 0x00,
sizeof(a));
374 memset(&srep_cat_table, 0x00,
sizeof(srep_cat_table));
376 BUG_ON(SRepGetVersion() > 0);
378 while(fgets(line, (
int)
sizeof(line), fp) != NULL) {
379 size_t len = GetEffectiveLineLen(line);
384 if (line[
len - 1] ==
'\n' || line[
len - 1] ==
'\r') {
385 line[
len - 1] =
'\0';
390 if (SRepCatSplitLine(line, &cat, shortname,
sizeof(shortname)) == 0) {
400 if (strlen(srep_cat_table[i]) == 0)
402 SCLogDebug(
"CAT %d, name %s", i, srep_cat_table[i]);
407 static int SRepLoadFile(
SRepCIDRTree *cidr_ctx,
char *filename)
410 FILE *fp = fopen(filename,
"r");
413 SCLogError(
"opening ip rep file %s: %s", filename, strerror(errno));
426 char line[8192] =
"";
428 while(fgets(line, (
int)
sizeof(line), fp) != NULL) {
429 size_t len = GetEffectiveLineLen(line);
434 if (line[
len - 1] ==
'\n' || line[
len - 1] ==
'\r') {
435 line[
len - 1] =
'\0';
439 memset(&a, 0x00,
sizeof(a));
442 uint8_t cat = 0, value = 0;
443 int r = SRepSplitLine(cidr_ctx, line, &a, &cat, &value);
447 if (a.
family == AF_INET) {
459 SCLogError(
"failed to get a host, increase host.memcap");
464 if (h->
iprep == NULL) {
466 if (h->
iprep != NULL) {
470 if (h->
iprep != NULL) {
475 if (rep->
version != SRepGetVersion()) {
479 rep->
version = SRepGetVersion();
480 rep->
rep[cat] = value;
482 SCLogDebug(
"host %p iprep %p setting cat %u to value %u",
483 h, h->
iprep, cat, value);
488 if (rep->
rep[i] == 0)
491 SCLogDebug(
"--> host %p iprep %p cat %d to value %u",
511 static char *SRepCompleteFilePath(
char *file)
513 const char *defaultpath = NULL;
518 if (
SCConfGet(
"default-reputation-path", &defaultpath) == 1) {
520 size_t path_len =
sizeof(char) * (strlen(defaultpath) +
525 strlcpy(path, defaultpath, path_len);
526 #if defined OS_WIN32 || defined __CYGWIN__
527 if (path[strlen(path) - 1] !=
'\\')
528 strlcat(path,
"\\\\", path_len);
530 if (path[strlen(path) - 1] !=
'/')
561 const char *filename = NULL;
575 if (SRepGetVersion() == 0) {
581 (void)
SCConfGet(
"reputation-categories-file", &filename);
583 if (filename == NULL && files == NULL) {
594 if (filename == NULL) {
595 SCLogError(
"\"reputation-categories-file\" not set");
601 if (SRepLoadCatFile(filename) < 0) {
603 "categories file %s",
615 char *sfile = SRepCompleteFilePath(file->
val);
617 SCLogInfo(
"Loading reputation file: %s", sfile);
619 int r = SRepLoadFile(cidr_ctx, sfile);