Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
Windows port
Browse files Browse the repository at this point in the history
Base64Encode mem leak fix
Switched DNS to TADNS
  • Loading branch information
TETYYS committed Oct 24, 2015
1 parent 05a1b95 commit c857dd5
Show file tree
Hide file tree
Showing 43 changed files with 2,650 additions and 823 deletions.
28 changes: 12 additions & 16 deletions Base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,26 @@
#include <stdbool.h>
#include <string.h>

size_t MEM_OUT Base64Encode(const unsigned char* buffer, size_t length, char** b64text)
size_t MEM_OUT Base64Encode(const unsigned char *In, size_t Len, OUT char **Out)
{
// outputs with NUL
BIO *bio, *b64;
BUF_MEM *bufferPtr;

b64 = BIO_new(BIO_f_base64());
bio = BIO_new(BIO_s_mem());
bio = BIO_push(b64, bio);
BIO *bio = BIO_push(BIO_new(BIO_f_base64()), BIO_new(BIO_s_mem()));

BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
BIO_write(bio, buffer, length);
BIO_write(bio, In, Len);
BIO_flush(bio);
BIO_get_mem_ptr(bio, &bufferPtr);
BIO_set_close(bio, BIO_NOCLOSE);
BIO_free_all(bio);

size_t len = bufferPtr->length;

*Out = malloc(len + 1);
memcpy(*Out, bufferPtr->data, len);
(*Out)[len] = 0x00;

BIO_free_all(bio);

*b64text = realloc(bufferPtr->data, bufferPtr->length + 1); // HACK HACK oops
(*b64text)[bufferPtr->length] = 0x00;
return bufferPtr->length;
return len;
}

static size_t CalcDecodeLength(const char* b64input)
Expand All @@ -47,15 +46,12 @@ static size_t CalcDecodeLength(const char* b64input)

bool MEM_OUT Base64Decode(char* b64message, unsigned char** buffer, size_t* length)
{
BIO *bio, *b64;

int decodeLen = CalcDecodeLength(b64message);
*buffer = (unsigned char*)malloc(decodeLen + 1);
(*buffer)[decodeLen] = '\0';

bio = BIO_new_mem_buf(b64message, -1);
b64 = BIO_new(BIO_f_base64());
bio = BIO_push(b64, bio);
BIO *bio = BIO_push(BIO_new(BIO_f_base64()), BIO_new_mem_buf(b64message, -1));

BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
*length = BIO_read(bio, *buffer, strlen(b64message));
Expand Down
2 changes: 1 addition & 1 deletion Base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
#include <stddef.h>
#include <stdbool.h>

size_t MEM_OUT Base64Encode(const unsigned char* buffer, size_t length, char** b64text);
size_t MEM_OUT Base64Encode(const unsigned char* buffer, size_t Len, char** Out);
bool MEM_OUT Base64Decode(char* b64message, unsigned char** buffer, size_t* length);
73 changes: 69 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,72 @@
cmake_minimum_required (VERSION 2.6)
project (LIVEPROXIES)
project (LiveProxies)
set(CMAKE_BUILD_TYPE Debug)

set (CMAKE_C_FLAGS "--std=gnu99 -pthread ${CMAKE_C_FLAGS}")
if (WIN32)
set (CMAKE_C_FLAGS "--std=gnu99 -Wall ${CMAKE_C_FLAGS}")
ELSE()
set (CMAKE_C_FLAGS "--std=gnu99 -Wall -pthread ${CMAKE_C_FLAGS}")
ENDIF()

add_executable(LiveProxies Base64.c Global.c Harvester.c Interface.c IPv6Map.c LiveProxies.c Logger.c ProxyLists.c ProxyRemove.c ProxyRequest.c Server.c PBKDF2.c SingleCheck.c HtmlTemplate.c Websocket.c Stats.c)
target_link_libraries(LiveProxies event GeoIP crypto ssl python2.7 pcre config m util anl curl)
add_executable(LiveProxies Base64.c Global.c Harvester.c Interface.c IPv6Map.c LiveProxies.c Logger.c ProxyLists.c ProxyRemove.c ProxyRequest.c Server.c PBKDF2.c SingleCheck.c HtmlTemplate.c Websocket.c Stats.c DNS.c CPH_Threads.c
Base64.h Global.h Harvester.h Interface.h IPv6Map.h LiveProxies.h Logger.h ProxyLists.h ProxyRemove.h ProxyRequest.h Server.h PBKDF2.h SingleCheck.h HtmlTemplate.h Websocket.h Stats.h DNS.h CPH_Threads.h
PortableEndian.h
tadns.c tadns.h llist.h
)

if (WIN32)
include_directories(
C:/LIB/openssl/include
C:/LIB/libevent/cmake__/include
C:/LIB/libconfig/libconfig-1.5/lib
C:/Python27/include
C:/LIB/libmaxminddb/include
"C:/Program Files (x86)/GnuWin32/include"
C:/LIB/curl-7.45.0/builds/libcurl-vc-x86-release-dll-ssl-dll-zlib-dll-ipv6-sspi/include
)
#link_directories(
# C:/LIB/openssl/lib
# C:/LIB/openssl/lib
# C:/LIB/libevent/bin/lib/Release
# C:/LIB/libconfig/libconfig-1.5/Release
# C:/LIB/libmaxminddb/projects/VS12/Release
# C:/LIB/unbound-1.5.5/.libs/libunbound.a
# C:/Python27/libs
# "C:/Program Files (x86)/GnuWin32/lib"
# C:/LIB/curl-7.45.0/builds/libcurl-vc-x86-release-dll-ssl-dll-zlib-dll-ipv6-sspi/lib
#)
IF (MINGW)
# MINGW
target_link_libraries(
LiveProxies
C:/LIB/libevent/cmake__/lib/libevent.a
C:/LIB/libmaxminddb/projects/VS12/Release/libmaxminddb.a
C:/LIB/openssl-1.0.2d/libssl.a
C:/LIB/openssl-1.0.2d/libcrypto.a
C:/Python27/libs/libpython27.a
"C:/Program Files (x86)/GnuWin32/lib/libpcre.dll.a"
C:/LIB/libconfig/libconfig-1.5/lib/.libs/libconfig.dll.a
C:/LIB/curl-7.45.0/lib/.libs/libcurl.dll.a
Ws2_32
Shlwapi
)
ELSE()
# MSVC
target_link_libraries(
LiveProxies
C:/LIB/libevent/bin/lib/Release/event.lib
C:/LIB/libmaxminddb/projects/VS12/Release/libmaxminddb.lib
C:/OpenSSL-Win32/lib/libeay32.lib
C:/Python27/libs/python27.lib
"C:/Program Files (x86)/GnuWin32/lib/pcre.lib"
C:/LIB/libconfig/libconfig-1.5/Release/libconfig.lib
C:/LIB/curl-7.45.0/builds/libcurl-vc-x86-release-dll-ssl-dll-zlib-dll-ipv6-sspi/lib/libcurl.lib
Ws2_32
Shlwapi
)
ENDIF()

add_definitions(-DWIN32_LEAN_AND_MEAN)
ELSE()
target_link_libraries(LiveProxies event maxminddb crypto ssl python2.7 pcre config m util anl curl)
ENDIF()
33 changes: 33 additions & 0 deletions CPH_Threads.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "CPH_Threads.h"
#if defined _WIN32 || defined _WIN64
#include <windows.h>
const DWORD MS_VC_EXCEPTION = 0x406D1388;

#pragma pack(push,8)
typedef struct tagTHREADNAME_INFO {
DWORD dwType; // Must be 0x1000.
LPCSTR szName; // Pointer to name (in user addr space).
DWORD dwThreadID; // Thread ID (-1=caller thread).
DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;
#pragma pack(pop)

int pthread_create(pthread_t *thread, void *attr, void *(*start_routine) (void *), void *arg)
{
*thread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)start_routine, arg, NULL, NULL);
return thread == NULL ? GetLastError() : ERROR_SUCCESS;
}

void pthread_setname_np(pthread_t thread, char* threadName)
{
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = threadName;
info.dwThreadID = thread;
info.dwFlags = 0;

/*__try {
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
} __except (EXCEPTION_EXECUTE_HANDLER) { }*/
}
#endif
19 changes: 19 additions & 0 deletions CPH_Threads.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#ifdef __linux__
#include <pthread.h>
#elif defined _WIN32 || defined _WIN64
#include <windows.h>
typedef HANDLE pthread_mutex_t;
typedef HANDLE pthread_t;

#define pthread_mutex_init(x, a) *x = CreateMutex(NULL, FALSE, NULL)
#define pthread_mutex_lock(x) WaitForSingleObject(*x, INFINITE);
#define pthread_mutex_unlock(x) ReleaseMutex(*x);
#define pthread_mutex_destroy(x) CloseHandle(*x);

#define pthread_detach(x) CloseHandle(x);
int pthread_create(pthread_t *thread, void *attr, void *(*start_routine) (void *), void *arg);
void pthread_setname_np(pthread_t thread, char* threadName);

#endif
4 changes: 4 additions & 0 deletions Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include <stdbool.h>
#include "IPv6Map.h"

#if defined _WIN32 || defined _WIN64
#include <Winsock2.h>
#endif

uint64_t RemoveThreadInterval;
uint64_t CheckingInterval;
uint64_t GlobalTimeout;
Expand Down
75 changes: 75 additions & 0 deletions DNS.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "DNS.h"
#include "Logger.h"
#include <stdlib.h>
#include <event2/event.h>
#ifdef __linux__
#include <unistd.h>
#endif
#include <stdbool.h>
#include <assert.h>
#include "ProxyRequest.h"
#include "Config.h"

static void DNSResolveCallError(DNS_LOOKUP_ASYNC_EX *Ex)
{
// void *context, DNS_RR_TYPE qtype, const char *name, const unsigned char *addr, size_t addrlen
struct dns_cb_data data;
data.name = NULL;
data.addr = NULL;
data.addr_len = 0;
data.context = Ex;
data.error = DNS_ERROR;
data.query_type = 0;
Ex->fxDone(&data);
}

static void DNSResolveLevEventToDNSProcess(evutil_socket_t fd, short what, void *arg)
{
DNS_LOOKUP_ASYNC_EX *ex = (DNS_LOOKUP_ASYNC_EX*)arg;
Log(LOG_LEVEL_DEBUG, "DNS process WHAT %d", what);
if (what == EV_TIMEOUT) {
DNSResolveCallError(ex);
goto free;
}

dns_poll(ex->dnsCtx);

if (ex->resolveDone)
goto free;

Log(LOG_LEVEL_DEBUG, "DNS process WHAT %d END", what);

return;
free:
dns_fini(ex->dnsCtx);
if (ex->evDNS != NULL)
event_del(ex->evDNS);
free(ex);
}

void DNSResolveAsync(void *Ex, char *Domain, bool IPv6, dns_callback_t fxDone)
{
DNS_LOOKUP_ASYNC_EX *ex = malloc(sizeof(DNS_LOOKUP_ASYNC_EX));
ex->object = Ex;
ex->dnsCtx = NULL;
ex->evDNS = NULL;
ex->resolveDone = false;
ex->fxDone = fxDone;

struct dns *dnsCtx = dns_init();
if (!dnsCtx) {
Log(LOG_LEVEL_ERROR, "Failed to initialize tadns context");
DNSResolveCallError(ex);
free(ex);
return;
}
ex->dnsCtx = dnsCtx;

dns_queue(dnsCtx, ex, Domain, IPv6 ? DNS_RR_TYPE_AAAA : DNS_RR_TYPE_A, fxDone);

Log(LOG_LEVEL_DEBUG, "DNS FD %d", dns_get_fd(dnsCtx));

struct event *fdEvent = event_new(levRequestBase, dns_get_fd(dnsCtx), EV_READ | EV_PERSIST, DNSResolveLevEventToDNSProcess, ex);
ex->evDNS = fdEvent;
event_add(fdEvent, &GlobalTimeoutTV);
}
Loading

0 comments on commit c857dd5

Please sign in to comment.