Skip to content

Commit

Permalink
strpool moved to uthash
Browse files Browse the repository at this point in the history
  • Loading branch information
bol-van committed Jul 22, 2018
1 parent 667afc8 commit 4d7f419
Show file tree
Hide file tree
Showing 12 changed files with 1,294 additions and 108 deletions.
Binary file modified binaries/armhf/tpws
Binary file not shown.
Binary file modified binaries/mips32r1-lsb/tpws
Binary file not shown.
Binary file modified binaries/mips32r1-msb/tpws
Binary file not shown.
Binary file modified binaries/x86/tpws
Binary file not shown.
Binary file modified binaries/x86_64/tpws
Binary file not shown.
2 changes: 1 addition & 1 deletion readme.txt
Expand Up @@ -124,7 +124,7 @@ tpws - это transparent proxy.
--unixeol ; конвертировать 0D0A в 0A и использовать везде 0A
--hostlist=<filename> ; действовать только над доменами, входящими в список из filename. поддомены автоматически учитываются. в файле должен быть хост на каждой строке.
; список читается 1 раз при старте и хранится в памяти в виде иерархической структуры для быстрого поиска.
; для списка РКН может потребоваться система с 128 Mb памяти ! расчитывайте требование RAM для процесса как 10-15 кратный размер файла списка.
; для списка РКН может потребоваться система с 128 Mb памяти ! расчитывайте требование RAM для процесса как 3-5 кратный размер файла списка.
; по сигналу HUP список будет перечитан при следующем принятом соединении
Параметры манипуляции могут сочетаться в любых комбинациях.
Есть исключения : split-pos заменяет split-http-req. hostdot и hosttab взаимоисключающи.
Expand Down
83 changes: 0 additions & 83 deletions tpws/chartree.c

This file was deleted.

16 changes: 0 additions & 16 deletions tpws/chartree.h

This file was deleted.

50 changes: 50 additions & 0 deletions tpws/strpool.c
@@ -0,0 +1,50 @@
#include "strpool.h"
#include <string.h>
#include <stdlib.h>

#undef uthash_nonfatal_oom
#define uthash_nonfatal_oom(elt) ut_oom_recover(elt)

static bool oom=false;
static void ut_oom_recover(strpool *elem)
{
oom=true;
}

bool StrPoolAddStr(strpool **pp,const char *s)
{
strpool *elem;
if (!(elem = (strpool*)malloc(sizeof(strpool))))
return false;
if (!(elem->str = strdup(s)))
{
free(elem);
return false;
}
oom = false;
HASH_ADD_KEYPTR( hh, *pp, elem->str, strlen(elem->str), elem );
if (oom)
{
free(elem->str);
free(elem);
return false;
}
return true;
}
bool StrPoolCheckStr(strpool *p,const char *s)
{
strpool *elem;
HASH_FIND_STR( p, s, elem);
return elem!=NULL;
}

void StrPoolDestroy(strpool **p)
{
strpool *elem,*tmp;
HASH_ITER(hh, *p, elem, tmp) {
free(elem->str);
HASH_DEL(*p, elem);
free(elem);
}
*p = NULL;
}
18 changes: 18 additions & 0 deletions tpws/strpool.h
@@ -0,0 +1,18 @@
#pragma once

#include <stdbool.h>
#include <ctype.h>

//#define HASH_BLOOM 20
#define HASH_NONFATAL_OOM 1
#define HASH_FUNCTION HASH_BER
#include "uthash.h"

typedef struct strpool {
char *str; /* key */
UT_hash_handle hh; /* makes this structure hashable */
} strpool;

void StrPoolDestroy(strpool **p);
bool StrPoolAddStr(strpool **pp,const char *s);
bool StrPoolCheckStr(strpool *p,const char *s);
16 changes: 8 additions & 8 deletions tpws/tpws.c
Expand Up @@ -24,9 +24,9 @@

#include "tpws.h"
#include "tpws_conn.h"
#include "chartree.h"
#include "strpool.h"

bool LoadHostList(cptr **hostlist, char *filename)
bool LoadHostList(strpool **hostlist, char *filename)
{
char *p, s[256];
FILE *F = fopen(filename, "rt");
Expand All @@ -42,9 +42,9 @@ bool LoadHostList(cptr **hostlist, char *filename)
{
for (p = s + strlen(s) - 1; p >= s && (*p == '\r' || *p == '\n'); p--) *p = 0;
for (p = s; *p; p++) *p=tolower(*p);
if (!CharTreeAddStr(hostlist, s))
if (!StrPoolAddStr(hostlist, s))
{
CharTreeDestroy(*hostlist);
StrPoolDestroy(hostlist);
*hostlist = NULL;
fprintf(stderr, "Not enough memory to store host list : %s\n", filename);
fclose(F);
Expand Down Expand Up @@ -72,7 +72,7 @@ struct params_s
int split_pos;
int maxconn;
char hostfile[256];
cptr *hostlist;
strpool *hostlist;
};

struct params_s params;
Expand Down Expand Up @@ -104,7 +104,7 @@ void dohup()
{
if (params.hostlist)
{
CharTreeDestroy(params.hostlist);
StrPoolDestroy(&params.hostlist);
if (!LoadHostList(&params.hostlist, params.hostfile))
exit(1);
}
Expand Down Expand Up @@ -221,7 +221,7 @@ bool handle_epollin(tproxy_conn_t *conn, int *data_transferred) {
p = Host;
while (p)
{
bInHostList = CharTreeCheckStr(params.hostlist, p);
bInHostList = StrPoolCheckStr(params.hostlist, p);
printf("Hostlist check for %s : %s\n", p, bInHostList ? "positive" : "negative");
if (bInHostList) break;
p = strchr(p, '.');
Expand Down Expand Up @@ -851,7 +851,7 @@ int main(int argc, char *argv[]) {
retval = event_loop(listen_fd);
close(listen_fd);

if (params.hostlist) CharTreeDestroy(params.hostlist);
if (params.hostlist) StrPoolDestroy(&params.hostlist);

fprintf(stderr, "Will exit\n");

Expand Down

0 comments on commit 4d7f419

Please sign in to comment.