Small patches
This commit is contained in:
parent
2837fead2d
commit
b620ee4560
9 changed files with 522 additions and 40 deletions
|
@ -1,6 +1,6 @@
|
|||
#Becenév
|
||||
#nickname=Pelda
|
||||
#A program által használt port (Alapértelmezett: 6381)
|
||||
#A program által használt port (Alapértelmezett: 6327)
|
||||
port=6327
|
||||
#Letiltja a külső csatlakozást (Alapértelmezett: false)
|
||||
localmode=false
|
||||
|
|
453
lib/debugmalloc/debugmalloc-impl.h
Normal file
453
lib/debugmalloc/debugmalloc-impl.h
Normal file
|
@ -0,0 +1,453 @@
|
|||
#ifndef DEBUGMALLOC_IMPL_H
|
||||
#define DEBUGMALLOC_IMPL_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
|
||||
/* make getpid and putenv "crossplatform". deprecated on windows but they work just fine,
|
||||
* however not declared. */
|
||||
#ifdef _WIN32
|
||||
/* windows */
|
||||
#include <process.h>
|
||||
#ifdef _MSC_VER
|
||||
/* visual studio, getenv/getpid deprecated warning */
|
||||
#pragma warning(disable: 4996)
|
||||
#else
|
||||
/* other windows. the declaration is unfortunately hidden
|
||||
* in mingw header files by ifdefs. */
|
||||
int putenv(const char *);
|
||||
#endif
|
||||
#else
|
||||
/* posix */
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
|
||||
/* size of canary in bytes. should be multiple of largest alignment
|
||||
* required by any data type (usually 8 or 16) */
|
||||
enum { debugmalloc_canary_size = 64 };
|
||||
|
||||
/* canary byte */
|
||||
enum { debugmalloc_canary_char = 'K' };
|
||||
|
||||
/* hash table size for allocated entries */
|
||||
enum { debugmalloc_tablesize = 256 };
|
||||
|
||||
|
||||
/* linked list entry for allocated blocks */
|
||||
typedef struct DebugmallocElem {
|
||||
void *real_mem; /* the address of the real allocation */
|
||||
void *user_mem; /* address shown to the user */
|
||||
size_t size; /* size of block requested by user */
|
||||
|
||||
char file[64]; /* malloc called in this file */
|
||||
unsigned line; /* malloc called at this line in file */
|
||||
char func[32]; /* allocation function called (malloc, calloc, realloc) */
|
||||
char expr[128]; /* expression calculating the size of allocation */
|
||||
|
||||
struct DebugmallocElem *prev, *next; /* for doubly linked list */
|
||||
} DebugmallocElem;
|
||||
|
||||
|
||||
/* debugmalloc singleton, storing all state */
|
||||
typedef struct DebugmallocData {
|
||||
char logfile[256]; /* log file name or empty string */
|
||||
DebugmallocElem head[debugmalloc_tablesize], tail[debugmalloc_tablesize]; /* head and tail elements of allocation lists */
|
||||
} DebugmallocData;
|
||||
|
||||
|
||||
/* this forward declaration is required by the singleton manager function */
|
||||
static DebugmallocData * debugmalloc_create(void);
|
||||
|
||||
|
||||
/* creates singleton instance. as this function is static included to different
|
||||
* translation units, multiple instances of the static variables are created.
|
||||
* to make sure it is really a singleton, these instances must know each other
|
||||
* somethow. an environment variable is used for that purpose, ie. the address
|
||||
* of the singleton allocated is stored by the operating system.
|
||||
* this implementation is not thread-safe. */
|
||||
static DebugmallocData * debugmalloc_singleton(void) {
|
||||
static char envstr[100];
|
||||
static void *instance = NULL;
|
||||
|
||||
/* if we do not know the address of the singleton:
|
||||
* - maybe we are the one to create it (env variable also does not exist)
|
||||
* - or it is already created, and stored in the env variable. */
|
||||
if (instance == NULL) {
|
||||
char envvarname[100] = "";
|
||||
sprintf(envvarname, "%s%d", "debugmallocsingleton", (int) getpid());
|
||||
char *envptr = getenv(envvarname);
|
||||
if (envptr == NULL) {
|
||||
/* no env variable: create singleton. */
|
||||
instance = debugmalloc_create();
|
||||
sprintf(envstr, "%s=%p", envvarname, instance);
|
||||
putenv(envstr);
|
||||
} else {
|
||||
/* another copy of this function already created it. */
|
||||
int ok = sscanf(envptr, "%p", &instance);
|
||||
if (ok != 1) {
|
||||
fprintf(stderr, "debugmalloc: nem lehet ertelmezni: %s!\n", envptr);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (DebugmallocData *) instance;
|
||||
}
|
||||
|
||||
|
||||
/* better version of strncpy, always terminates string with \0. */
|
||||
static void debugmalloc_strlcpy(char *dest, char const *src, size_t destsize) {
|
||||
strncpy(dest, src, destsize);
|
||||
dest[destsize - 1] = '\0';
|
||||
}
|
||||
|
||||
|
||||
/* set the name of the log file for debugmalloc. empty filename
|
||||
* means logging to stderr. */
|
||||
static void debugmalloc_log_file(char const *logfilename) {
|
||||
if (logfilename == NULL)
|
||||
logfilename = "";
|
||||
DebugmallocData *instance = debugmalloc_singleton();
|
||||
debugmalloc_strlcpy(instance->logfile, logfilename, sizeof(instance->logfile));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* printf to the log file, or stderr. */
|
||||
static void debugmalloc_log(char const *format, ...) {
|
||||
DebugmallocData *instance = debugmalloc_singleton();
|
||||
FILE *f = stderr;
|
||||
if (strcmp(instance->logfile, "") != 0) {
|
||||
f = fopen(instance->logfile, "at");
|
||||
if (f == NULL) {
|
||||
f = stderr;
|
||||
fprintf(stderr, "debugmalloc: nem tudom megnyitni a %s fajlt irasra!\n", instance->logfile);
|
||||
strcpy(instance->logfile, "");
|
||||
}
|
||||
}
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
vfprintf(f, format, ap);
|
||||
va_end(ap);
|
||||
|
||||
if (f != stderr)
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
|
||||
/* initialize a memory block allocated for the user. the start and the end
|
||||
* of the block is initialized with the canary characters. if 'zero' is
|
||||
* true, the user memory area is zero-initialized, otherwise it is also
|
||||
* filled with the canary character to simulate garbage in memory. */
|
||||
static void debugmalloc_memory_init(DebugmallocElem *elem, bool zero) {
|
||||
unsigned char *real_mem = (unsigned char *) elem->real_mem;
|
||||
unsigned char *user_mem = (unsigned char *) elem->user_mem;
|
||||
unsigned char *canary1 = real_mem;
|
||||
unsigned char *canary2 = real_mem + debugmalloc_canary_size + elem->size;
|
||||
memset(canary1, debugmalloc_canary_char, debugmalloc_canary_size);
|
||||
memset(canary2, debugmalloc_canary_char, debugmalloc_canary_size);
|
||||
memset(user_mem, zero ? 0 : debugmalloc_canary_char, elem->size);
|
||||
}
|
||||
|
||||
/* check canary, return true if ok, false if corrupted. */
|
||||
static bool debugmalloc_canary_ok(DebugmallocElem const *elem) {
|
||||
unsigned char *real_mem = (unsigned char *) elem->real_mem;
|
||||
unsigned char *canary1 = real_mem;
|
||||
unsigned char *canary2 = real_mem + debugmalloc_canary_size + elem->size;
|
||||
for (size_t i = 0; i < debugmalloc_canary_size; ++i) {
|
||||
if (canary1[i] != debugmalloc_canary_char)
|
||||
return false;
|
||||
if (canary2[i] != debugmalloc_canary_char)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* dump memory contents to log file. */
|
||||
static void debugmalloc_dump_memory(char const *mem, size_t size) {
|
||||
for (unsigned y = 0; y < (size + 15) / 16; y++) {
|
||||
char line[80];
|
||||
int pos = 0;
|
||||
pos += sprintf(line + pos, " %04x ", y * 16);
|
||||
for (unsigned x = 0; x < 16; x++) {
|
||||
if (y * 16 + x < size)
|
||||
pos += sprintf(line + pos, "%02x ", mem[y * 16 + x]);
|
||||
else
|
||||
pos += sprintf(line + pos, " ");
|
||||
}
|
||||
pos += sprintf(line + pos, " ");
|
||||
for (unsigned x = 0; x < 16; x++) {
|
||||
if (y * 16 + x < size) {
|
||||
unsigned char c = mem[y * 16 + x];
|
||||
pos += sprintf(line + pos, "%c", isprint(c) ? c : '.');
|
||||
}
|
||||
else {
|
||||
pos += sprintf(line + pos, " ");
|
||||
}
|
||||
}
|
||||
debugmalloc_log("%s\n", line);
|
||||
}
|
||||
}
|
||||
|
||||
/* dump data of allocated memory block.
|
||||
* if the canary is corrupted, it is also written to the log. */
|
||||
static void debugmalloc_dump_elem(DebugmallocElem const *elem) {
|
||||
bool canary_ok = debugmalloc_canary_ok(elem);
|
||||
|
||||
debugmalloc_log(" MEMORIATERULET: %p, %u bajt, kanari: %s\n"
|
||||
" foglalas: %s:%u, %s(%s)\n",
|
||||
elem->user_mem, (unsigned) elem->size, canary_ok ? "ok" : "**SERULT**",
|
||||
elem->file, elem->line,
|
||||
elem->func, elem->expr);
|
||||
|
||||
if (!canary_ok) {
|
||||
debugmalloc_log(" ELOTTE kanari: \n");
|
||||
debugmalloc_dump_memory((char const *) elem->real_mem, debugmalloc_canary_size);
|
||||
}
|
||||
|
||||
debugmalloc_log(" memoria eleje: \n");
|
||||
debugmalloc_dump_memory((char const *) elem->user_mem, elem->size > 64 ? 64 : elem->size);
|
||||
|
||||
if (!canary_ok) {
|
||||
debugmalloc_log(" UTANA kanari: \n");
|
||||
debugmalloc_dump_memory((char const *) elem->real_mem + debugmalloc_canary_size + elem->size, debugmalloc_canary_size);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* dump data of all memory blocks allocated. */
|
||||
static void debugmalloc_dump(void) {
|
||||
DebugmallocData *instance = debugmalloc_singleton();
|
||||
debugmalloc_log("** DEBUGMALLOC DUMP ************************************\n");
|
||||
for (size_t i = 0; i < debugmalloc_tablesize; i++) {
|
||||
DebugmallocElem *head = &instance->head[i];
|
||||
for (DebugmallocElem *iter = head->next; iter->next != NULL; iter = iter->next)
|
||||
debugmalloc_dump_elem(iter);
|
||||
}
|
||||
debugmalloc_log("** DEBUGMALLOC DUMP VEGE *******************************\n");
|
||||
}
|
||||
|
||||
|
||||
/* called at program exit to dump data if there is a leak,
|
||||
* ie. allocated block remained. */
|
||||
static void debugmalloc_atexit_dump(void) {
|
||||
DebugmallocData *instance = debugmalloc_singleton();
|
||||
bool ok = true;
|
||||
for (size_t i = 0; i < debugmalloc_tablesize; i++) {
|
||||
if (instance->head[i].next != &instance->tail[i]) {
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!ok) {
|
||||
debugmalloc_log("********************************************************\n"
|
||||
"* MEMORIASZIVARGAS VAN A PROGRAMBAN!!!\n"
|
||||
"********************************************************\n");
|
||||
debugmalloc_dump();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* hash function for bucket hash. */
|
||||
static size_t debugmalloc_hash(void *address) {
|
||||
/* the last few bits are ignored, as they are usually zero, because of
|
||||
* alignment purposes. all tested architectures used 16 byte allocation. */
|
||||
size_t cut = (size_t)address >> 4;
|
||||
return cut % debugmalloc_tablesize;
|
||||
}
|
||||
|
||||
|
||||
/* insert element to hash table. */
|
||||
static void debugmalloc_insert(DebugmallocElem *entry) {
|
||||
DebugmallocData *instance = debugmalloc_singleton();
|
||||
size_t idx = debugmalloc_hash(entry->user_mem);
|
||||
DebugmallocElem *head = &instance->head[idx];
|
||||
entry->prev = head;
|
||||
entry->next = head->next;
|
||||
head->next->prev = entry;
|
||||
head->next = entry;
|
||||
}
|
||||
|
||||
|
||||
/* find element in hash table, given with the memory address that the user sees.
|
||||
* @return the linked list entry, or null if not found. */
|
||||
static DebugmallocElem * debugmalloc_find(void *mem) {
|
||||
DebugmallocData *instance = debugmalloc_singleton();
|
||||
size_t idx = debugmalloc_hash(mem);
|
||||
DebugmallocElem *head = &instance->head[idx];
|
||||
for (DebugmallocElem *iter = head->next; iter->next != NULL; iter = iter->next)
|
||||
if (iter->user_mem == mem)
|
||||
return iter;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/* count number of blocks allocated */
|
||||
static int debugmalloc_allocated_count(void) {
|
||||
DebugmallocData *instance = debugmalloc_singleton();
|
||||
int count = 0;
|
||||
for (size_t i = 0; i < debugmalloc_tablesize; i++) {
|
||||
for (DebugmallocElem *iter = instance->head[i].next; iter != &instance->tail[i]; iter = iter->next)
|
||||
count += 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
/* allocate memory. this function is called via the macro. */
|
||||
static void *debugmalloc_malloc_full(size_t size, char const *func, char const *expr, char const *file, unsigned line, bool zero) {
|
||||
/* imitate standard malloc: return null if size is zero */
|
||||
if (size == 0)
|
||||
return NULL;
|
||||
|
||||
/* allocate more memory, make room for canary */
|
||||
void *real_mem = malloc(size + 2 * debugmalloc_canary_size);
|
||||
if (real_mem == NULL) {
|
||||
debugmalloc_log("debugmalloc: %s @ %s:%u: nem sikerult %u meretu memoriat foglalni!\n", func, file, line, (unsigned) size);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* allocate memory for linked list element */
|
||||
DebugmallocElem *newentry = (DebugmallocElem *) malloc(sizeof(DebugmallocElem));
|
||||
if (newentry == NULL) {
|
||||
free(real_mem);
|
||||
debugmalloc_log("debugmalloc: %s @ %s:%u: le tudtam foglalni %u memoriat, de utana a sajatnak nem, sry\n", func, file, line, (unsigned) size);
|
||||
abort();
|
||||
}
|
||||
|
||||
/* metadata of allocation: caller function, code line etc. */
|
||||
debugmalloc_strlcpy(newentry->func, func, sizeof(newentry->func));
|
||||
debugmalloc_strlcpy(newentry->expr, expr, sizeof(newentry->expr));
|
||||
debugmalloc_strlcpy(newentry->file, file, sizeof(newentry->file));
|
||||
newentry->line = line;
|
||||
|
||||
/* address of allocated memory chunk */
|
||||
newentry->real_mem = real_mem;
|
||||
newentry->user_mem = (unsigned char *) real_mem + debugmalloc_canary_size;
|
||||
newentry->size = size;
|
||||
debugmalloc_memory_init(newentry, zero);
|
||||
|
||||
/* store in list and return pointer to user area */
|
||||
debugmalloc_insert(newentry);
|
||||
return newentry->user_mem;
|
||||
}
|
||||
|
||||
|
||||
/* free memory and remove list item. before deleting, the chuck is filled with
|
||||
* the canary byte to make sure that the user will see garbage if the memory
|
||||
* is accessed after freeing. */
|
||||
static void debugmalloc_free_inner(DebugmallocElem *deleted) {
|
||||
/* fill with garbage */
|
||||
memset(deleted->real_mem, debugmalloc_canary_char, deleted->size + 2 * debugmalloc_canary_size);
|
||||
|
||||
/* free memory and remove from linked list */
|
||||
free(deleted->real_mem);
|
||||
deleted->next->prev = deleted->prev;
|
||||
deleted->prev->next = deleted->next;
|
||||
free(deleted);
|
||||
}
|
||||
|
||||
|
||||
/* free memory - called via the macro.
|
||||
* as all allocations are tracked in the list, this function can call abort()
|
||||
* if a block is freed twice or the free function is called with an invalid address. */
|
||||
static void debugmalloc_free_full(void *mem, char const *func, char const *file, unsigned line) {
|
||||
/* imitate standard free function: if ptr is null, no operation is performed */
|
||||
if (mem == NULL)
|
||||
return;
|
||||
|
||||
/* find allocation, abort if not found */
|
||||
DebugmallocElem *deleted = debugmalloc_find(mem);
|
||||
if (deleted == NULL) {
|
||||
debugmalloc_log("debugmalloc: %s @ %s:%u: olyan teruletet akarsz felszabaditani, ami nincs lefoglalva!\n", func, file, line);
|
||||
abort();
|
||||
}
|
||||
|
||||
/* check canary and then free memory */
|
||||
if (!debugmalloc_canary_ok(deleted)) {
|
||||
debugmalloc_log("debugmalloc: %s @ %s:%u: a %p memoriateruletet tulindexelted!\n", func, file, line, mem);
|
||||
debugmalloc_dump_elem(deleted);
|
||||
}
|
||||
debugmalloc_free_inner(deleted);
|
||||
}
|
||||
|
||||
|
||||
/* realloc-like function. */
|
||||
static void *debugmalloc_realloc_full(void *oldmem, size_t newsize, char const *func, char const *expr, char const *file, unsigned line) {
|
||||
/* imitate standard realloc: equivalent to malloc if first param is NULL */
|
||||
if (oldmem == NULL)
|
||||
return debugmalloc_malloc_full(newsize, func, expr, file, line, 0);
|
||||
|
||||
/* imitate standard realloc: equivalent to free if size is null. */
|
||||
if (newsize == 0) {
|
||||
debugmalloc_free_full(oldmem, func, file, line);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* find old allocation. abort if not found. */
|
||||
DebugmallocElem *entry = debugmalloc_find(oldmem);
|
||||
if (entry == NULL) {
|
||||
debugmalloc_log("debugmalloc: %s @ %s:%u: olyan teruletet akarsz atmeretezni, ami nincs lefoglalva!\n", func, file, line);
|
||||
abort();
|
||||
}
|
||||
|
||||
/* create new allocation */
|
||||
void *newmem = debugmalloc_malloc_full(newsize, func, expr, file, line, false);
|
||||
if (newmem == NULL) {
|
||||
debugmalloc_log("debugmalloc: %s @ %s:%u: nem sikerult uj memoriat foglalni az atmeretezeshez!\n", func, file, line);
|
||||
/* imitate standard realloc: original block is untouched, but return NULL */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* copy old data */
|
||||
size_t smaller = entry->size < newsize ? entry->size : newsize;
|
||||
memcpy(newmem, oldmem, smaller);
|
||||
debugmalloc_free_inner(entry);
|
||||
return newmem;
|
||||
}
|
||||
|
||||
|
||||
/* initialize debugmalloc singleton. returns the newly allocated instance */
|
||||
static DebugmallocData * debugmalloc_create(void) {
|
||||
/* config check */
|
||||
if (debugmalloc_canary_size % 16 != 0) {
|
||||
debugmalloc_log("debugmalloc: a kanari merete legyen 16-tal oszthato\n");
|
||||
abort();
|
||||
}
|
||||
if (debugmalloc_canary_char == 0) {
|
||||
debugmalloc_log("debugmalloc: a kanari legyen 0-tol kulonbozo\n");
|
||||
abort();
|
||||
}
|
||||
/* avoid compiler warning if these functions are not used */
|
||||
(void) debugmalloc_realloc_full;
|
||||
(void) debugmalloc_log_file;
|
||||
(void) debugmalloc_allocated_count;
|
||||
|
||||
/* create and initialize instance */
|
||||
DebugmallocData *instance = (DebugmallocData *) malloc(sizeof(DebugmallocData));
|
||||
if (instance == NULL) {
|
||||
debugmalloc_log("debugmalloc: nem sikerult elinditani a memoriakezelest\n");
|
||||
abort();
|
||||
}
|
||||
memset(instance, 0, sizeof(DebugmallocData));
|
||||
for (size_t i = 0; i < debugmalloc_tablesize; i++) {
|
||||
instance->head[i].prev = NULL;
|
||||
instance->head[i].next = &instance->tail[i];
|
||||
instance->tail[i].next = NULL;
|
||||
instance->tail[i].prev = &instance->head[i];
|
||||
}
|
||||
|
||||
atexit(debugmalloc_atexit_dump);
|
||||
return instance;
|
||||
}
|
||||
|
||||
#endif
|
26
lib/debugmalloc/debugmalloc.h
Normal file
26
lib/debugmalloc/debugmalloc.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
#ifndef DEBUGMALLOC_H
|
||||
#define DEBUGMALLOC_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "debugmalloc-impl.h"
|
||||
|
||||
/* These macro-like functions forward all allocation/free
|
||||
* calls to debugmalloc. Usage is the same, malloc(size)
|
||||
* gives the address of a new memory block, free(ptr)
|
||||
* deallocates etc.
|
||||
*
|
||||
* If you use this file, make sure that you include this
|
||||
* in *ALL* translation units (*.c) of your source. The
|
||||
* builtin free() function cannot deallocate a memory block
|
||||
* that was allocated via debugmalloc, yet the name of
|
||||
* the functions are the same!
|
||||
*
|
||||
* If there is a conflict with the macro names used here,
|
||||
* you can include debugmalloc-nomacro.h instead. */
|
||||
|
||||
#define malloc(S) debugmalloc_malloc_full((S), "malloc", #S, __FILE__, __LINE__, false)
|
||||
#define calloc(N,S) debugmalloc_malloc_full((N)*(S), "calloc", #N ", " #S, __FILE__, __LINE__, true)
|
||||
#define realloc(P,S) debugmalloc_realloc_full((P), (S), "realloc", #S, __FILE__, __LINE__)
|
||||
#define free(P) debugmalloc_free_full((P), "free", __FILE__, __LINE__)
|
||||
|
||||
#endif
|
|
@ -40,11 +40,12 @@ void map_addPair(Map *m, Pair p) {
|
|||
if (m->length >= m->size) {
|
||||
assert(m->length == m->size);
|
||||
size_t new_size = (m->size + 2) * 2;
|
||||
Pair *new_list = realloc(m->pairs, new_size * sizeof(Pair));
|
||||
if (new_list == 0)
|
||||
printf("OUT OF MEMORY!");
|
||||
m->pairs = realloc(m->pairs, new_size * sizeof(Pair));
|
||||
if (m->pairs == NULL) {
|
||||
printf("OUT OF MEMORY!");
|
||||
abort();
|
||||
}
|
||||
m->size = new_size;
|
||||
m->pairs = new_list;
|
||||
}
|
||||
m->pairs[m->length++] = p;
|
||||
map_sort(m);
|
||||
|
|
10
main.c
10
main.c
|
@ -8,6 +8,7 @@
|
|||
#include "modules/config.h"
|
||||
#include "modules/server.h"
|
||||
#include "lib/tcp-listener.h"
|
||||
#include "lib/debugmalloc/debugmalloc.h"
|
||||
|
||||
SOCKET listening;
|
||||
SOCKET web_sock;
|
||||
|
@ -16,7 +17,6 @@ void closeSocks(){
|
|||
logger_log("Closing socket...");
|
||||
closesocket(listening);
|
||||
closesocket(web_sock);
|
||||
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
|
@ -28,6 +28,7 @@ int main(void) {
|
|||
|
||||
#if defined(WIN32)
|
||||
WSADATA ws;
|
||||
|
||||
int r1 = WSAStartup(MAKEWORD(2,2),&ws);
|
||||
if(r1 != 0){
|
||||
logger_log("Error at WSAStartup.");
|
||||
|
@ -59,7 +60,6 @@ int main(void) {
|
|||
PeerList peerList1;
|
||||
peer_initList(&peerList1);
|
||||
|
||||
|
||||
WebIO webIo;
|
||||
|
||||
res = webio_create(config,mynode, &webIo);
|
||||
|
@ -73,7 +73,9 @@ int main(void) {
|
|||
logger_log("Starting main loop...");
|
||||
|
||||
serverThread(listening,&master,webIo,peerList1,mynode);
|
||||
free(peerList1.array);
|
||||
free(config.pairs);
|
||||
if(peerList1.size >0)free(peerList1.array);
|
||||
//Ezzel mi a baj?
|
||||
if(config.size > 0) free(config.pairs);
|
||||
closeSocks();
|
||||
return 0;
|
||||
}
|
|
@ -92,16 +92,19 @@ int peer_ConnetctTo(char* ip, int port, PeerList* peerList, Node_data my, fd_set
|
|||
if(peers != NULL) {
|
||||
char* tmp = strtok(peers,",");
|
||||
while(tmp != NULL){
|
||||
char ip[NI_MAXHOST];
|
||||
int port;
|
||||
sscanf(tmp, "%[^:]:%d", ip, &port);
|
||||
if(!peer_IP_isFound(*peerList,ip,port))
|
||||
peer_ConnetctTo(ip,port,peerList,my,fdSet);
|
||||
char ip1[NI_MAXHOST];
|
||||
int port1;
|
||||
if (sscanf(tmp, "%[^:]:%d", ip1, &port1) != 2) {
|
||||
tmp = strtok(NULL, ",");
|
||||
continue;
|
||||
}
|
||||
if(!peer_IP_isFound(*peerList,ip1,port1))
|
||||
peer_ConnetctTo(ip1,port1,peerList,my,fdSet);
|
||||
tmp = strtok(NULL,",");
|
||||
}
|
||||
}
|
||||
free(m.pairs);
|
||||
logger_log("Peer validated (%s->%s)!",inet_ntoa(hint.sin_addr),node.id);
|
||||
//free(m.pairs);
|
||||
//logger_log("Peer validated (%s->%s)!",inet_ntoa(hint.sin_addr),node.id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -53,7 +53,7 @@ void serverThread(SOCKET listening, fd_set *master, WebIO webIo, PeerList list,N
|
|||
}
|
||||
} else {
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
SOCKET sock = list.array[i].socket;
|
||||
SOCKET sock = list.array[i].socket;
|
||||
if(!FD_ISSET(sock,©))
|
||||
continue;
|
||||
char buf[DEFAULT_BUFLEN];
|
||||
|
|
|
@ -4,13 +4,6 @@
|
|||
//
|
||||
#include "webio.h"
|
||||
|
||||
#ifdef RANDOM_PORT
|
||||
#define DEFAULT_INTERFACE_PORT "0"
|
||||
#else
|
||||
#define DEFAULT_INTERFACE_PORT "5081"
|
||||
#endif
|
||||
|
||||
|
||||
int webio_create(Config config, struct Node_data myData, WebIO *webIo){
|
||||
char *port = map_getValue(config, "interface-port");
|
||||
if (port == NULL)
|
||||
|
@ -50,7 +43,7 @@ int webio_handleRequest(WebIO wio, const PeerList *list){
|
|||
int res = recv(client,buf,8192,0);
|
||||
if(res <=0){
|
||||
logger_log("Error with web interface!");
|
||||
close(client);
|
||||
closesocket(client);
|
||||
return -1;
|
||||
}
|
||||
char req[10];
|
||||
|
@ -117,7 +110,7 @@ char* webio_getFiletype(char* filename){
|
|||
return type;
|
||||
}
|
||||
|
||||
int webio_handleGETrequest(SOCKET client, WebIO wio, char* file, const PeerList *list){
|
||||
static int webio_handleGETrequest(SOCKET client, WebIO wio, char* file, const PeerList *list){
|
||||
|
||||
char buf[8192];
|
||||
sscanf(buf,"%*s %s",file);
|
||||
|
@ -200,12 +193,12 @@ int webio_handleGETrequest(SOCKET client, WebIO wio, char* file, const PeerList
|
|||
return -1;
|
||||
}
|
||||
shutdown(client,SD_BOTH);
|
||||
close(client);
|
||||
closesocket(client);
|
||||
|
||||
free(response);
|
||||
}
|
||||
|
||||
int webio_handlePOSTrequest(SOCKET client, WebIO wio, const PeerList *list, Map post){
|
||||
static int webio_handlePOSTrequest(SOCKET client, WebIO wio, const PeerList *list, Map post){
|
||||
shutdown(client,SD_RECEIVE);
|
||||
char *response = "HTTP/1.1 304 Not Modified ";
|
||||
|
||||
|
@ -221,6 +214,7 @@ int webio_handlePOSTrequest(SOCKET client, WebIO wio, const PeerList *list, Map
|
|||
char folder[72];
|
||||
sprintf(folder,"%s/peers/",wio.folder);
|
||||
#if defined(_WIN32)
|
||||
|
||||
mkdir(folder);
|
||||
#else
|
||||
mkdir(folder, 0777); // notice that 777 is different than 0777
|
||||
|
@ -247,7 +241,7 @@ int webio_handlePOSTrequest(SOCKET client, WebIO wio, const PeerList *list, Map
|
|||
logger_log("Message sent to %s",map_getValue(post,"id"));
|
||||
}else map_dump(post);
|
||||
}
|
||||
void webio_getHeader(char* folder,char result[]) {
|
||||
static void webio_getHeader(char* folder,char result[]) {
|
||||
|
||||
char path[65];
|
||||
strcpy(path, folder);
|
||||
|
@ -266,7 +260,7 @@ void webio_getHeader(char* folder,char result[]) {
|
|||
result = "<html>";
|
||||
}
|
||||
|
||||
void webio_getIndex(char* folder, const PeerList *list, char *outputBuffer){
|
||||
static void webio_getIndex(char* folder, const PeerList *list, char *outputBuffer){
|
||||
char content[8192] ="";
|
||||
char header[4096] ="";
|
||||
|
||||
|
@ -299,7 +293,7 @@ void webio_getIndex(char* folder, const PeerList *list, char *outputBuffer){
|
|||
strcpy(outputBuffer,content);
|
||||
}
|
||||
|
||||
void webio_getPeerPage(char *folder, char *id, bool online, char *outputBuffer) {
|
||||
static void webio_getPeerPage(char *folder, char *id, bool online, char *outputBuffer) {
|
||||
char content[8192] = "";
|
||||
char header[4096] = "";
|
||||
|
||||
|
@ -324,7 +318,7 @@ void webio_getPeerPage(char *folder, char *id, bool online, char *outputBuffer)
|
|||
strcpy(outputBuffer, content);
|
||||
}
|
||||
|
||||
bool webio_isPeerFound(char* folder,char *id) {
|
||||
static bool webio_isPeerFound(char* folder,char *id) {
|
||||
|
||||
char file[129];
|
||||
sprintf(file,"%s/peers/%s.txt",folder,id);
|
||||
|
|
|
@ -7,6 +7,9 @@
|
|||
#include "../lib/tcp-listener.h"
|
||||
#include "peer.h"
|
||||
#include "config.h"
|
||||
#ifdef __MINGW32__
|
||||
#include <dirent.h>
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct webio{
|
||||
|
@ -20,19 +23,19 @@ int webio_create(Config config, struct Node_data myData, WebIO *webIo);
|
|||
|
||||
int webio_handleRequest(WebIO wio, const PeerList *list);
|
||||
|
||||
int webio_handleGETrequest(SOCKET client, WebIO wio, char * file, const PeerList *list);
|
||||
static int webio_handleGETrequest(SOCKET client, WebIO wio, char * file, const PeerList *list);
|
||||
|
||||
int webio_handlePOSTrequest(SOCKET client, WebIO wio, const PeerList *list, Map post);
|
||||
static int webio_handlePOSTrequest(SOCKET client, WebIO wio, const PeerList *list, Map post);
|
||||
|
||||
char* webio_getMIMEtype(char* filename);
|
||||
static char* webio_getMIMEtype(char* filename);
|
||||
|
||||
char* webio_getFiletype(char* filename);
|
||||
static char* webio_getFiletype(char* filename);
|
||||
|
||||
void webio_getHeader(char* folder, char result[]);
|
||||
static void webio_getHeader(char* folder, char result[]);
|
||||
|
||||
void webio_getIndex(char* folder, const PeerList *list, char *outputBuffer);
|
||||
static void webio_getIndex(char* folder, const PeerList *list, char *outputBuffer);
|
||||
|
||||
void webio_getPeerPage(char* folder, char *id, bool online, char *outputBuffer);
|
||||
static void webio_getPeerPage(char* folder, char *id, bool online, char *outputBuffer);
|
||||
|
||||
bool webio_isPeerFound(char* folder,char* id);
|
||||
static bool webio_isPeerFound(char* folder,char* id);
|
||||
|
||||
|
|
Loading…
Reference in a new issue