2010-11-20 10:37:14 +00:00
|
|
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
|
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
2007-08-22 07:52:33 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
2007-11-08 09:11:32 +00:00
|
|
|
#include <ctype.h>
|
2007-08-22 07:52:33 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
2008-08-14 17:19:13 +00:00
|
|
|
#include "system.h"
|
2014-11-24 16:09:01 +00:00
|
|
|
#include "confusables.h"
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2015-08-22 19:07:13 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2015-02-07 22:15:58 +00:00
|
|
|
#if defined(WEBSOCKETS)
|
|
|
|
#include "engine/shared/websockets.h"
|
|
|
|
#endif
|
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
#if defined(CONF_FAMILY_UNIX)
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
/* unix net includes */
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <errno.h>
|
2011-04-13 18:37:12 +00:00
|
|
|
#include <netdb.h>
|
2007-08-22 07:52:33 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <fcntl.h>
|
2007-10-02 16:19:25 +00:00
|
|
|
#include <pthread.h>
|
2011-03-28 18:11:28 +00:00
|
|
|
#include <arpa/inet.h>
|
2007-08-22 07:52:33 +00:00
|
|
|
|
|
|
|
#include <dirent.h>
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-09-30 15:52:15 +00:00
|
|
|
#if defined(CONF_PLATFORM_MACOSX)
|
2015-01-11 02:38:59 +00:00
|
|
|
// some lock and pthread functions are already defined in headers
|
|
|
|
// included from Carbon.h
|
|
|
|
// this prevents having duplicate definitions of those
|
|
|
|
#define _lock_set_user_
|
|
|
|
#define _task_user_
|
|
|
|
|
2008-09-30 15:52:15 +00:00
|
|
|
#include <Carbon/Carbon.h>
|
|
|
|
#endif
|
2015-07-09 00:08:14 +00:00
|
|
|
|
2014-06-16 11:29:18 +00:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
#include <android/log.h>
|
|
|
|
#endif
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
#elif defined(CONF_FAMILY_WINDOWS)
|
2011-04-13 18:37:12 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2015-09-05 02:49:22 +00:00
|
|
|
#undef _WIN32_WINNT
|
2008-05-10 17:18:56 +00:00
|
|
|
#define _WIN32_WINNT 0x0501 /* required for mingw to get getaddrinfo to work */
|
2007-08-22 07:52:33 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#include <fcntl.h>
|
2007-11-18 22:06:41 +00:00
|
|
|
#include <direct.h>
|
|
|
|
#include <errno.h>
|
2014-08-22 12:18:16 +00:00
|
|
|
#include <process.h>
|
2014-12-31 14:07:44 +00:00
|
|
|
#include <shellapi.h>
|
2015-03-05 23:53:59 +00:00
|
|
|
#include <wincrypt.h>
|
2007-08-22 07:52:33 +00:00
|
|
|
#else
|
|
|
|
#error NOT IMPLEMENTED
|
|
|
|
#endif
|
|
|
|
|
2011-09-08 23:41:53 +00:00
|
|
|
#if defined(CONF_PLATFORM_SOLARIS)
|
|
|
|
#include <sys/filio.h>
|
|
|
|
#endif
|
|
|
|
|
2007-10-04 09:49:38 +00:00
|
|
|
#if defined(__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2014-12-06 18:19:39 +00:00
|
|
|
#ifdef FUZZING
|
|
|
|
static unsigned char gs_NetData[1024];
|
|
|
|
static int gs_NetPosition = 0;
|
|
|
|
static int gs_NetSize = 0;
|
|
|
|
#endif
|
|
|
|
|
2008-02-24 16:03:58 +00:00
|
|
|
IOHANDLE io_stdin() { return (IOHANDLE)stdin; }
|
|
|
|
IOHANDLE io_stdout() { return (IOHANDLE)stdout; }
|
|
|
|
IOHANDLE io_stderr() { return (IOHANDLE)stderr; }
|
|
|
|
|
2008-03-10 00:48:45 +00:00
|
|
|
static DBG_LOGGER loggers[16];
|
|
|
|
static int num_loggers = 0;
|
|
|
|
|
2008-04-05 14:50:43 +00:00
|
|
|
static NETSTATS network_stats = {0};
|
2008-10-02 12:29:19 +00:00
|
|
|
static MEMSTATS memory_stats = {0};
|
2008-04-05 14:50:43 +00:00
|
|
|
|
2011-03-28 18:11:28 +00:00
|
|
|
static NETSOCKET invalid_socket = {NETTYPE_INVALID, -1, -1};
|
|
|
|
|
2015-02-07 22:15:58 +00:00
|
|
|
#define AF_WEBSOCKET_INET (0xee)
|
|
|
|
|
2008-03-10 00:48:45 +00:00
|
|
|
void dbg_logger(DBG_LOGGER logger)
|
|
|
|
{
|
|
|
|
loggers[num_loggers++] = logger;
|
|
|
|
}
|
2007-08-22 07:52:33 +00:00
|
|
|
|
|
|
|
void dbg_assert_imp(const char *filename, int line, int test, const char *msg)
|
|
|
|
{
|
|
|
|
if(!test)
|
|
|
|
{
|
|
|
|
dbg_msg("assert", "%s(%d): %s", filename, line, msg);
|
|
|
|
dbg_break();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void dbg_break()
|
|
|
|
{
|
2012-08-17 16:32:56 +00:00
|
|
|
*((volatile unsigned*)0) = 0x0;
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 19:38:23 +00:00
|
|
|
#if !defined(CONF_PLATFORM_MACOSX)
|
2014-09-09 21:02:05 +00:00
|
|
|
#define QUEUE_SIZE 16
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char q[QUEUE_SIZE][1024*4];
|
|
|
|
int begin;
|
|
|
|
int end;
|
2014-09-11 19:12:18 +00:00
|
|
|
SEMAPHORE mutex;
|
|
|
|
SEMAPHORE notempty;
|
|
|
|
SEMAPHORE notfull;
|
2014-09-09 21:02:05 +00:00
|
|
|
} Queue;
|
|
|
|
|
|
|
|
static int dbg_msg_threaded = 0;
|
|
|
|
static Queue log_queue;
|
|
|
|
|
|
|
|
int queue_empty(Queue *q)
|
|
|
|
{
|
|
|
|
return q->begin == q->end;
|
|
|
|
}
|
|
|
|
|
|
|
|
int queue_full(Queue *q)
|
|
|
|
{
|
|
|
|
return ((q->end+1) % QUEUE_SIZE) == q->begin;
|
|
|
|
}
|
|
|
|
|
2014-09-11 19:38:23 +00:00
|
|
|
void dbg_msg_thread(void *v)
|
|
|
|
{
|
|
|
|
char str[1024*4];
|
|
|
|
int i;
|
|
|
|
int f;
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
semaphore_wait(&log_queue.notempty);
|
|
|
|
semaphore_wait(&log_queue.mutex);
|
|
|
|
f = queue_full(&log_queue);
|
|
|
|
|
|
|
|
str_copy(str, log_queue.q[log_queue.begin], sizeof(str));
|
|
|
|
|
|
|
|
log_queue.begin = (log_queue.begin + 1) % QUEUE_SIZE;
|
|
|
|
|
|
|
|
if(f)
|
|
|
|
semaphore_signal(&log_queue.notfull);
|
|
|
|
|
|
|
|
if(!queue_empty(&log_queue))
|
|
|
|
semaphore_signal(&log_queue.notempty);
|
|
|
|
|
|
|
|
semaphore_signal(&log_queue.mutex);
|
|
|
|
|
|
|
|
for(i = 0; i < num_loggers; i++)
|
|
|
|
loggers[i](str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void dbg_enable_threaded()
|
|
|
|
{
|
|
|
|
Queue *q;
|
|
|
|
void *Thread;
|
|
|
|
|
|
|
|
q = &log_queue;
|
|
|
|
q->begin = 0;
|
|
|
|
q->end = 0;
|
|
|
|
semaphore_init(&q->mutex);
|
|
|
|
semaphore_init(&q->notempty);
|
|
|
|
semaphore_init(&q->notfull);
|
|
|
|
semaphore_signal(&q->mutex);
|
|
|
|
semaphore_signal(&q->notfull);
|
|
|
|
|
|
|
|
dbg_msg_threaded = 1;
|
|
|
|
|
2015-04-07 17:07:38 +00:00
|
|
|
Thread = thread_init(dbg_msg_thread, 0);
|
2015-09-05 02:49:22 +00:00
|
|
|
thread_detach(Thread);
|
2014-09-11 19:38:23 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
void dbg_msg(const char *sys, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
2008-03-10 00:48:45 +00:00
|
|
|
char *msg;
|
2014-09-09 21:02:05 +00:00
|
|
|
int len;
|
2014-09-11 16:23:59 +00:00
|
|
|
int e;
|
2010-05-29 07:25:38 +00:00
|
|
|
|
2011-04-09 06:41:31 +00:00
|
|
|
//str_format(str, sizeof(str), "[%08x][%s]: ", (int)time(0), sys);
|
2011-02-13 05:35:13 +00:00
|
|
|
time_t rawtime;
|
2011-04-09 06:41:31 +00:00
|
|
|
struct tm* timeinfo;
|
2011-02-13 05:35:13 +00:00
|
|
|
char timestr [80];
|
|
|
|
|
|
|
|
time ( &rawtime );
|
|
|
|
timeinfo = localtime ( &rawtime );
|
|
|
|
|
|
|
|
strftime (timestr,sizeof(timestr),"%y-%m-%d %H:%M:%S",timeinfo);
|
2011-08-13 00:11:06 +00:00
|
|
|
|
2014-09-11 19:38:23 +00:00
|
|
|
#if !defined(CONF_PLATFORM_MACOSX)
|
2014-09-09 21:02:05 +00:00
|
|
|
if(dbg_msg_threaded)
|
|
|
|
{
|
2014-09-11 19:12:18 +00:00
|
|
|
semaphore_wait(&log_queue.notfull);
|
|
|
|
semaphore_wait(&log_queue.mutex);
|
2014-09-11 16:23:59 +00:00
|
|
|
e = queue_empty(&log_queue);
|
2014-09-09 21:02:05 +00:00
|
|
|
|
|
|
|
str_format(log_queue.q[log_queue.end], sizeof(log_queue.q[log_queue.end]), "[%s][%s]: ", timestr, sys);
|
|
|
|
|
|
|
|
len = strlen(log_queue.q[log_queue.end]);
|
|
|
|
msg = (char *)log_queue.q[log_queue.end] + len;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
_vsnprintf(msg, sizeof(log_queue.q[log_queue.end])-len, fmt, args);
|
|
|
|
#else
|
|
|
|
vsnprintf(msg, sizeof(log_queue.q[log_queue.end])-len, fmt, args);
|
|
|
|
#endif
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
log_queue.end = (log_queue.end + 1) % QUEUE_SIZE;
|
|
|
|
|
|
|
|
if(e)
|
2014-09-11 19:12:18 +00:00
|
|
|
semaphore_signal(&log_queue.notempty);
|
2014-09-09 21:02:05 +00:00
|
|
|
|
|
|
|
if(!queue_full(&log_queue))
|
2014-09-11 19:12:18 +00:00
|
|
|
semaphore_signal(&log_queue.notfull);
|
2014-09-09 21:02:05 +00:00
|
|
|
|
2014-09-11 19:12:18 +00:00
|
|
|
semaphore_signal(&log_queue.mutex);
|
2014-09-09 21:02:05 +00:00
|
|
|
}
|
|
|
|
else
|
2014-09-11 19:38:23 +00:00
|
|
|
#endif
|
2014-09-09 21:02:05 +00:00
|
|
|
{
|
|
|
|
char str[1024*4];
|
2014-09-11 16:23:59 +00:00
|
|
|
int i;
|
|
|
|
|
2014-09-09 21:02:05 +00:00
|
|
|
str_format(str, sizeof(str), "[%s][%s]: ", timestr, sys);
|
|
|
|
|
|
|
|
len = strlen(str);
|
|
|
|
msg = (char *)str + len;
|
2010-05-29 07:25:38 +00:00
|
|
|
|
2014-09-09 21:02:05 +00:00
|
|
|
va_start(args, fmt);
|
2008-03-10 00:48:45 +00:00
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
2014-09-09 21:02:05 +00:00
|
|
|
_vsnprintf(msg, sizeof(str)-len, fmt, args);
|
2008-03-10 00:48:45 +00:00
|
|
|
#else
|
2014-09-09 21:02:05 +00:00
|
|
|
vsnprintf(msg, sizeof(str)-len, fmt, args);
|
2007-08-22 07:52:33 +00:00
|
|
|
#endif
|
2014-09-09 21:02:05 +00:00
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
for(i = 0; i < num_loggers; i++)
|
|
|
|
loggers[i](str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-11 22:15:35 +00:00
|
|
|
static void logger_stdout(const char *line)
|
|
|
|
{
|
2008-03-10 00:48:45 +00:00
|
|
|
printf("%s\n", line);
|
2008-03-23 01:48:31 +00:00
|
|
|
fflush(stdout);
|
2014-06-16 11:29:18 +00:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "DDNet", "%s", line);
|
|
|
|
#endif
|
2008-03-10 00:48:45 +00:00
|
|
|
}
|
2008-01-16 22:14:06 +00:00
|
|
|
|
2008-03-10 00:48:45 +00:00
|
|
|
static void logger_debugger(const char *line)
|
|
|
|
{
|
2008-02-11 21:49:26 +00:00
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
2008-03-10 00:48:45 +00:00
|
|
|
OutputDebugString(line);
|
|
|
|
OutputDebugString("\n");
|
2008-02-11 21:49:26 +00:00
|
|
|
#endif
|
2008-03-10 00:48:45 +00:00
|
|
|
}
|
2008-01-16 22:14:06 +00:00
|
|
|
|
2008-03-10 00:48:45 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
static IOHANDLE logfile = 0;
|
2008-03-10 00:48:45 +00:00
|
|
|
static void logger_file(const char *line)
|
|
|
|
{
|
|
|
|
io_write(logfile, line, strlen(line));
|
2011-12-29 22:36:53 +00:00
|
|
|
io_write_newline(logfile);
|
2008-03-10 00:48:45 +00:00
|
|
|
io_flush(logfile);
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 19:12:11 +00:00
|
|
|
void dbg_logger_stdout() { dbg_logger(logger_stdout); }
|
2014-05-11 22:15:35 +00:00
|
|
|
|
2008-03-10 00:48:45 +00:00
|
|
|
void dbg_logger_debugger() { dbg_logger(logger_debugger); }
|
|
|
|
void dbg_logger_file(const char *filename)
|
|
|
|
{
|
|
|
|
logfile = io_open(filename, IOFLAG_WRITE);
|
|
|
|
if(logfile)
|
|
|
|
dbg_logger(logger_file);
|
|
|
|
else
|
|
|
|
dbg_msg("dbg/logger", "failed to open '%s' for logging", filename);
|
|
|
|
|
|
|
|
}
|
|
|
|
/* */
|
|
|
|
|
2008-10-02 12:29:19 +00:00
|
|
|
typedef struct MEMHEADER
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
|
|
|
const char *filename;
|
|
|
|
int line;
|
|
|
|
int size;
|
2008-10-02 12:29:19 +00:00
|
|
|
struct MEMHEADER *prev;
|
|
|
|
struct MEMHEADER *next;
|
|
|
|
} MEMHEADER;
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2008-10-02 12:29:19 +00:00
|
|
|
typedef struct MEMTAIL
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
|
|
|
int guard;
|
2008-10-02 12:29:19 +00:00
|
|
|
} MEMTAIL;
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2008-10-02 12:29:19 +00:00
|
|
|
static struct MEMHEADER *first = 0;
|
2008-10-06 16:44:34 +00:00
|
|
|
static const int MEM_GUARD_VAL = 0xbaadc0de;
|
2007-08-25 08:48:24 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
void *mem_alloc_debug(const char *filename, int line, unsigned size, unsigned alignment)
|
|
|
|
{
|
|
|
|
/* TODO: fix alignment */
|
|
|
|
/* TODO: add debugging */
|
2011-12-29 11:37:28 +00:00
|
|
|
MEMTAIL *tail;
|
2008-10-02 12:29:19 +00:00
|
|
|
MEMHEADER *header = (struct MEMHEADER *)malloc(size+sizeof(MEMHEADER)+sizeof(MEMTAIL));
|
2011-12-29 11:37:28 +00:00
|
|
|
dbg_assert(header != 0, "mem_alloc failure");
|
2012-08-17 16:32:56 +00:00
|
|
|
if(!header)
|
|
|
|
return NULL;
|
2011-12-29 11:37:28 +00:00
|
|
|
tail = (struct MEMTAIL *)(((char*)(header+1))+size);
|
2007-08-22 07:52:33 +00:00
|
|
|
header->size = size;
|
|
|
|
header->filename = filename;
|
|
|
|
header->line = line;
|
2008-10-02 12:29:19 +00:00
|
|
|
|
|
|
|
memory_stats.allocated += header->size;
|
|
|
|
memory_stats.total_allocations++;
|
|
|
|
memory_stats.active_allocations++;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-10-06 16:44:34 +00:00
|
|
|
tail->guard = MEM_GUARD_VAL;
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2008-10-02 12:29:19 +00:00
|
|
|
header->prev = (MEMHEADER *)0;
|
2007-08-22 07:52:33 +00:00
|
|
|
header->next = first;
|
|
|
|
if(first)
|
|
|
|
first->prev = header;
|
|
|
|
first = header;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
/*dbg_msg("mem", "++ %p", header+1); */
|
|
|
|
return header+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mem_free(void *p)
|
|
|
|
{
|
|
|
|
if(p)
|
|
|
|
{
|
2008-10-02 12:29:19 +00:00
|
|
|
MEMHEADER *header = (MEMHEADER *)p - 1;
|
|
|
|
MEMTAIL *tail = (MEMTAIL *)(((char*)(header+1))+header->size);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-10-06 16:44:34 +00:00
|
|
|
if(tail->guard != MEM_GUARD_VAL)
|
2007-08-22 07:52:33 +00:00
|
|
|
dbg_msg("mem", "!! %p", p);
|
|
|
|
/* dbg_msg("mem", "-- %p", p); */
|
2008-10-02 12:29:19 +00:00
|
|
|
memory_stats.allocated -= header->size;
|
|
|
|
memory_stats.active_allocations--;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
if(header->prev)
|
|
|
|
header->prev->next = header->next;
|
|
|
|
else
|
|
|
|
first = header->next;
|
|
|
|
if(header->next)
|
|
|
|
header->next->prev = header->prev;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
free(header);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-05 10:46:24 +00:00
|
|
|
void mem_debug_dump(IOHANDLE file)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
|
|
|
char buf[1024];
|
2008-10-02 12:29:19 +00:00
|
|
|
MEMHEADER *header = first;
|
2011-03-05 10:46:24 +00:00
|
|
|
if(!file)
|
|
|
|
file = io_open("memory.txt", IOFLAG_WRITE);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-03-05 10:46:24 +00:00
|
|
|
if(file)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
2011-03-05 10:46:24 +00:00
|
|
|
while(header)
|
|
|
|
{
|
2011-12-29 22:36:53 +00:00
|
|
|
str_format(buf, sizeof(buf), "%s(%d): %d", header->filename, header->line, header->size);
|
2011-03-05 10:46:24 +00:00
|
|
|
io_write(file, buf, strlen(buf));
|
2011-12-29 22:36:53 +00:00
|
|
|
io_write_newline(file);
|
2011-03-05 10:46:24 +00:00
|
|
|
header = header->next;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-03-05 10:46:24 +00:00
|
|
|
io_close(file);
|
|
|
|
}
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void mem_copy(void *dest, const void *source, unsigned size)
|
|
|
|
{
|
|
|
|
memcpy(dest, source, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mem_move(void *dest, const void *source, unsigned size)
|
|
|
|
{
|
|
|
|
memmove(dest, source, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mem_zero(void *block,unsigned size)
|
|
|
|
{
|
|
|
|
memset(block, 0, size);
|
|
|
|
}
|
|
|
|
|
2008-10-07 16:17:58 +00:00
|
|
|
int mem_check_imp()
|
2008-10-06 16:44:34 +00:00
|
|
|
{
|
|
|
|
MEMHEADER *header = first;
|
|
|
|
while(header)
|
|
|
|
{
|
|
|
|
MEMTAIL *tail = (MEMTAIL *)(((char*)(header+1))+header->size);
|
|
|
|
if(tail->guard != MEM_GUARD_VAL)
|
|
|
|
{
|
|
|
|
dbg_msg("mem", "Memory check failed at %s(%d): %d", header->filename, header->line, header->size);
|
2008-10-07 16:17:58 +00:00
|
|
|
return 0;
|
2008-10-06 16:44:34 +00:00
|
|
|
}
|
|
|
|
header = header->next;
|
|
|
|
}
|
2008-10-07 16:17:58 +00:00
|
|
|
|
|
|
|
return 1;
|
2008-10-06 16:44:34 +00:00
|
|
|
}
|
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
IOHANDLE io_open(const char *filename, int flags)
|
|
|
|
{
|
|
|
|
if(flags == IOFLAG_READ)
|
|
|
|
return (IOHANDLE)fopen(filename, "rb");
|
|
|
|
if(flags == IOFLAG_WRITE)
|
|
|
|
return (IOHANDLE)fopen(filename, "wb");
|
|
|
|
return 0x0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned io_read(IOHANDLE io, void *buffer, unsigned size)
|
|
|
|
{
|
|
|
|
return fread(buffer, 1, size, (FILE*)io);
|
|
|
|
}
|
|
|
|
|
2010-09-03 19:17:32 +00:00
|
|
|
unsigned io_skip(IOHANDLE io, int size)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
|
|
|
fseek((FILE*)io, size, SEEK_CUR);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int io_seek(IOHANDLE io, int offset, int origin)
|
|
|
|
{
|
|
|
|
int real_origin;
|
|
|
|
|
|
|
|
switch(origin)
|
|
|
|
{
|
2007-08-25 08:48:24 +00:00
|
|
|
case IOSEEK_START:
|
2007-08-22 07:52:33 +00:00
|
|
|
real_origin = SEEK_SET;
|
|
|
|
break;
|
2007-08-25 08:48:24 +00:00
|
|
|
case IOSEEK_CUR:
|
2007-08-22 07:52:33 +00:00
|
|
|
real_origin = SEEK_CUR;
|
|
|
|
break;
|
2007-08-25 08:48:24 +00:00
|
|
|
case IOSEEK_END:
|
2007-08-22 07:52:33 +00:00
|
|
|
real_origin = SEEK_END;
|
2011-05-04 23:43:27 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
2011-05-04 23:43:27 +00:00
|
|
|
return fseek((FILE*)io, offset, real_origin);
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
long int io_tell(IOHANDLE io)
|
|
|
|
{
|
|
|
|
return ftell((FILE*)io);
|
|
|
|
}
|
|
|
|
|
|
|
|
long int io_length(IOHANDLE io)
|
|
|
|
{
|
|
|
|
long int length;
|
|
|
|
io_seek(io, 0, IOSEEK_END);
|
|
|
|
length = io_tell(io);
|
2007-08-25 08:48:24 +00:00
|
|
|
io_seek(io, 0, IOSEEK_START);
|
2007-08-22 07:52:33 +00:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned io_write(IOHANDLE io, const void *buffer, unsigned size)
|
|
|
|
{
|
|
|
|
return fwrite(buffer, 1, size, (FILE*)io);
|
|
|
|
}
|
|
|
|
|
2011-12-29 22:36:53 +00:00
|
|
|
unsigned io_write_newline(IOHANDLE io)
|
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
return fwrite("\r\n", 1, 2, (FILE*)io);
|
|
|
|
#else
|
|
|
|
return fwrite("\n", 1, 1, (FILE*)io);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
int io_close(IOHANDLE io)
|
|
|
|
{
|
|
|
|
fclose((FILE*)io);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-03-10 00:48:45 +00:00
|
|
|
int io_flush(IOHANDLE io)
|
|
|
|
{
|
|
|
|
fflush((FILE*)io);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-07 17:07:38 +00:00
|
|
|
void *thread_init(void (*threadfunc)(void *), void *u)
|
2008-02-10 21:54:52 +00:00
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_UNIX)
|
|
|
|
pthread_t id;
|
|
|
|
pthread_create(&id, NULL, (void *(*)(void*))threadfunc, u);
|
|
|
|
return (void*)id;
|
|
|
|
#elif defined(CONF_FAMILY_WINDOWS)
|
|
|
|
return CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadfunc, u, 0, NULL);
|
|
|
|
#else
|
|
|
|
#error not implemented
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void thread_wait(void *thread)
|
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_UNIX)
|
|
|
|
pthread_join((pthread_t)thread, NULL);
|
|
|
|
#elif defined(CONF_FAMILY_WINDOWS)
|
|
|
|
WaitForSingleObject((HANDLE)thread, INFINITE);
|
|
|
|
#else
|
|
|
|
#error not implemented
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void thread_destroy(void *thread)
|
|
|
|
{
|
2008-03-29 11:44:03 +00:00
|
|
|
#if defined(CONF_FAMILY_UNIX)
|
|
|
|
void *r = 0;
|
|
|
|
pthread_join((pthread_t)thread, &r);
|
|
|
|
#else
|
|
|
|
/*#error not implemented*/
|
|
|
|
#endif
|
2008-02-10 21:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void thread_yield()
|
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_UNIX)
|
|
|
|
sched_yield();
|
|
|
|
#elif defined(CONF_FAMILY_WINDOWS)
|
|
|
|
Sleep(0);
|
|
|
|
#else
|
|
|
|
#error not implemented
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
void thread_sleep(int milliseconds)
|
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_UNIX)
|
|
|
|
usleep(milliseconds*1000);
|
|
|
|
#elif defined(CONF_FAMILY_WINDOWS)
|
|
|
|
Sleep(milliseconds);
|
|
|
|
#else
|
|
|
|
#error not implemented
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-05-01 13:48:09 +00:00
|
|
|
void thread_detach(void *thread)
|
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_UNIX)
|
|
|
|
pthread_detach((pthread_t)(thread));
|
|
|
|
#elif defined(CONF_FAMILY_WINDOWS)
|
|
|
|
CloseHandle(thread);
|
|
|
|
#else
|
|
|
|
#error not implemented
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-02-10 21:54:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2007-10-02 16:19:25 +00:00
|
|
|
#if defined(CONF_FAMILY_UNIX)
|
|
|
|
typedef pthread_mutex_t LOCKINTERNAL;
|
2007-10-04 09:49:38 +00:00
|
|
|
#elif defined(CONF_FAMILY_WINDOWS)
|
|
|
|
typedef CRITICAL_SECTION LOCKINTERNAL;
|
2007-10-02 16:19:25 +00:00
|
|
|
#else
|
|
|
|
#error not implemented on this platform
|
|
|
|
#endif
|
|
|
|
|
|
|
|
LOCK lock_create()
|
|
|
|
{
|
2007-10-04 09:49:38 +00:00
|
|
|
LOCKINTERNAL *lock = (LOCKINTERNAL*)mem_alloc(sizeof(LOCKINTERNAL), 4);
|
2007-10-02 16:19:25 +00:00
|
|
|
|
|
|
|
#if defined(CONF_FAMILY_UNIX)
|
2007-10-04 09:49:38 +00:00
|
|
|
pthread_mutex_init(lock, 0x0);
|
|
|
|
#elif defined(CONF_FAMILY_WINDOWS)
|
|
|
|
InitializeCriticalSection((LPCRITICAL_SECTION)lock);
|
2007-10-02 16:19:25 +00:00
|
|
|
#else
|
|
|
|
#error not implemented on this platform
|
|
|
|
#endif
|
2007-10-06 17:01:06 +00:00
|
|
|
return (LOCK)lock;
|
2007-10-02 16:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void lock_destroy(LOCK lock)
|
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_UNIX)
|
2007-10-06 17:01:06 +00:00
|
|
|
pthread_mutex_destroy((LOCKINTERNAL *)lock);
|
2007-10-04 09:49:38 +00:00
|
|
|
#elif defined(CONF_FAMILY_WINDOWS)
|
|
|
|
DeleteCriticalSection((LPCRITICAL_SECTION)lock);
|
2007-10-02 16:19:25 +00:00
|
|
|
#else
|
|
|
|
#error not implemented on this platform
|
|
|
|
#endif
|
|
|
|
mem_free(lock);
|
|
|
|
}
|
|
|
|
|
2015-04-07 17:07:38 +00:00
|
|
|
int lock_trylock(LOCK lock)
|
2007-10-02 16:19:25 +00:00
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_UNIX)
|
2007-10-06 17:01:06 +00:00
|
|
|
return pthread_mutex_trylock((LOCKINTERNAL *)lock);
|
2007-10-04 09:49:38 +00:00
|
|
|
#elif defined(CONF_FAMILY_WINDOWS)
|
2012-07-19 08:01:57 +00:00
|
|
|
return !TryEnterCriticalSection((LPCRITICAL_SECTION)lock);
|
2007-10-02 16:19:25 +00:00
|
|
|
#else
|
|
|
|
#error not implemented on this platform
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void lock_wait(LOCK lock)
|
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_UNIX)
|
2007-10-06 17:01:06 +00:00
|
|
|
pthread_mutex_lock((LOCKINTERNAL *)lock);
|
2007-10-04 09:49:38 +00:00
|
|
|
#elif defined(CONF_FAMILY_WINDOWS)
|
|
|
|
EnterCriticalSection((LPCRITICAL_SECTION)lock);
|
2007-10-02 16:19:25 +00:00
|
|
|
#else
|
|
|
|
#error not implemented on this platform
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-04-07 17:07:38 +00:00
|
|
|
void lock_unlock(LOCK lock)
|
2007-10-02 16:19:25 +00:00
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_UNIX)
|
2007-10-06 17:01:06 +00:00
|
|
|
pthread_mutex_unlock((LOCKINTERNAL *)lock);
|
2007-10-04 09:49:38 +00:00
|
|
|
#elif defined(CONF_FAMILY_WINDOWS)
|
|
|
|
LeaveCriticalSection((LPCRITICAL_SECTION)lock);
|
2007-10-02 16:19:25 +00:00
|
|
|
#else
|
|
|
|
#error not implemented on this platform
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-06-10 10:14:41 +00:00
|
|
|
#if !defined(CONF_PLATFORM_MACOSX)
|
|
|
|
#if defined(CONF_FAMILY_UNIX)
|
|
|
|
void semaphore_init(SEMAPHORE *sem) { sem_init(sem, 0, 0); }
|
|
|
|
void semaphore_wait(SEMAPHORE *sem) { sem_wait(sem); }
|
|
|
|
void semaphore_signal(SEMAPHORE *sem) { sem_post(sem); }
|
|
|
|
void semaphore_destroy(SEMAPHORE *sem) { sem_destroy(sem); }
|
|
|
|
#elif defined(CONF_FAMILY_WINDOWS)
|
|
|
|
void semaphore_init(SEMAPHORE *sem) { *sem = CreateSemaphore(0, 0, 10000, 0); }
|
2012-07-21 16:53:09 +00:00
|
|
|
void semaphore_wait(SEMAPHORE *sem) { WaitForSingleObject((HANDLE)*sem, INFINITE); }
|
2012-06-10 10:14:41 +00:00
|
|
|
void semaphore_signal(SEMAPHORE *sem) { ReleaseSemaphore((HANDLE)*sem, 1, NULL); }
|
|
|
|
void semaphore_destroy(SEMAPHORE *sem) { CloseHandle((HANDLE)*sem); }
|
|
|
|
#else
|
|
|
|
#error not implemented on this platform
|
|
|
|
#endif
|
2012-01-01 14:56:28 +00:00
|
|
|
#endif
|
|
|
|
|
2014-11-18 17:21:30 +00:00
|
|
|
static int new_tick = -1;
|
2014-11-09 23:08:50 +00:00
|
|
|
|
|
|
|
void set_new_tick()
|
|
|
|
{
|
|
|
|
new_tick = 1;
|
|
|
|
}
|
2012-01-01 14:56:28 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
/* ----- time ----- */
|
|
|
|
int64 time_get()
|
|
|
|
{
|
2014-11-09 23:08:50 +00:00
|
|
|
static int64 last = 0;
|
|
|
|
if(!new_tick)
|
|
|
|
return last;
|
2014-11-09 23:17:31 +00:00
|
|
|
if(new_tick != -1)
|
|
|
|
new_tick = 0;
|
2014-11-09 23:08:50 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
#if defined(CONF_FAMILY_UNIX)
|
|
|
|
struct timeval val;
|
|
|
|
gettimeofday(&val, NULL);
|
2014-11-09 23:08:50 +00:00
|
|
|
last = (int64)val.tv_sec*(int64)1000000+(int64)val.tv_usec;
|
|
|
|
return last;
|
2007-08-22 07:52:33 +00:00
|
|
|
#elif defined(CONF_FAMILY_WINDOWS)
|
2014-11-10 13:09:42 +00:00
|
|
|
{
|
|
|
|
int64 t;
|
|
|
|
QueryPerformanceCounter((PLARGE_INTEGER)&t);
|
|
|
|
if(t<last) /* for some reason, QPC can return values in the past */
|
|
|
|
return last;
|
|
|
|
last = t;
|
|
|
|
return t;
|
|
|
|
}
|
2007-08-22 07:52:33 +00:00
|
|
|
#else
|
|
|
|
#error not implemented
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 time_freq()
|
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_UNIX)
|
|
|
|
return 1000000;
|
|
|
|
#elif defined(CONF_FAMILY_WINDOWS)
|
|
|
|
int64 t;
|
|
|
|
QueryPerformanceFrequency((PLARGE_INTEGER)&t);
|
|
|
|
return t;
|
|
|
|
#else
|
|
|
|
#error not implemented
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----- network ----- */
|
2011-03-28 18:11:28 +00:00
|
|
|
static void netaddr_to_sockaddr_in(const NETADDR *src, struct sockaddr_in *dest)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
2011-03-28 18:11:28 +00:00
|
|
|
mem_zero(dest, sizeof(struct sockaddr_in));
|
2015-02-07 22:15:58 +00:00
|
|
|
if(src->type != NETTYPE_IPV4 && src->type != NETTYPE_WEBSOCKET_IPV4)
|
2011-03-28 18:11:28 +00:00
|
|
|
{
|
|
|
|
dbg_msg("system", "couldn't convert NETADDR of type %d to ipv4", src->type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dest->sin_family = AF_INET;
|
|
|
|
dest->sin_port = htons(src->port);
|
|
|
|
mem_copy(&dest->sin_addr.s_addr, src->ip, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netaddr_to_sockaddr_in6(const NETADDR *src, struct sockaddr_in6 *dest)
|
|
|
|
{
|
|
|
|
mem_zero(dest, sizeof(struct sockaddr_in6));
|
|
|
|
if(src->type != NETTYPE_IPV6)
|
|
|
|
{
|
|
|
|
dbg_msg("system", "couldn't not convert NETADDR of type %d to ipv6", src->type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dest->sin6_family = AF_INET6;
|
|
|
|
dest->sin6_port = htons(src->port);
|
|
|
|
mem_copy(&dest->sin6_addr.s6_addr, src->ip, 16);
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
static void sockaddr_to_netaddr(const struct sockaddr *src, NETADDR *dst)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
2011-03-28 18:11:28 +00:00
|
|
|
if(src->sa_family == AF_INET)
|
|
|
|
{
|
|
|
|
mem_zero(dst, sizeof(NETADDR));
|
|
|
|
dst->type = NETTYPE_IPV4;
|
|
|
|
dst->port = htons(((struct sockaddr_in*)src)->sin_port);
|
|
|
|
mem_copy(dst->ip, &((struct sockaddr_in*)src)->sin_addr.s_addr, 4);
|
|
|
|
}
|
2015-02-07 22:15:58 +00:00
|
|
|
else if(src->sa_family == AF_WEBSOCKET_INET)
|
|
|
|
{
|
|
|
|
mem_zero(dst, sizeof(NETADDR));
|
|
|
|
dst->type = NETTYPE_WEBSOCKET_IPV4;
|
|
|
|
dst->port = htons(((struct sockaddr_in*)src)->sin_port);
|
|
|
|
mem_copy(dst->ip, &((struct sockaddr_in*)src)->sin_addr.s_addr, 4);
|
|
|
|
}
|
2011-03-28 18:11:28 +00:00
|
|
|
else if(src->sa_family == AF_INET6)
|
|
|
|
{
|
|
|
|
mem_zero(dst, sizeof(NETADDR));
|
|
|
|
dst->type = NETTYPE_IPV6;
|
|
|
|
dst->port = htons(((struct sockaddr_in6*)src)->sin6_port);
|
|
|
|
mem_copy(dst->ip, &((struct sockaddr_in6*)src)->sin6_addr.s6_addr, 16);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mem_zero(dst, sizeof(struct sockaddr));
|
|
|
|
dbg_msg("system", "couldn't convert sockaddr of family %d", src->sa_family);
|
|
|
|
}
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
int net_addr_comp(const NETADDR *a, const NETADDR *b)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
2008-07-06 11:21:21 +00:00
|
|
|
return mem_comp(a, b, sizeof(NETADDR));
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
2011-12-29 22:36:53 +00:00
|
|
|
void net_addr_str(const NETADDR *addr, char *string, int max_length, int add_port)
|
2008-08-17 07:05:16 +00:00
|
|
|
{
|
2015-02-07 22:15:58 +00:00
|
|
|
if(addr->type == NETTYPE_IPV4 || addr->type == NETTYPE_WEBSOCKET_IPV4)
|
2011-04-22 17:52:27 +00:00
|
|
|
{
|
2011-12-29 22:36:53 +00:00
|
|
|
if(add_port != 0)
|
2011-04-22 17:52:27 +00:00
|
|
|
str_format(string, max_length, "%d.%d.%d.%d:%d", addr->ip[0], addr->ip[1], addr->ip[2], addr->ip[3], addr->port);
|
|
|
|
else
|
|
|
|
str_format(string, max_length, "%d.%d.%d.%d", addr->ip[0], addr->ip[1], addr->ip[2], addr->ip[3]);
|
|
|
|
}
|
2008-08-17 07:05:16 +00:00
|
|
|
else if(addr->type == NETTYPE_IPV6)
|
|
|
|
{
|
2011-12-29 22:36:53 +00:00
|
|
|
if(add_port != 0)
|
2011-04-22 17:52:27 +00:00
|
|
|
str_format(string, max_length, "[%x:%x:%x:%x:%x:%x:%x:%x]:%d",
|
|
|
|
(addr->ip[0]<<8)|addr->ip[1], (addr->ip[2]<<8)|addr->ip[3], (addr->ip[4]<<8)|addr->ip[5], (addr->ip[6]<<8)|addr->ip[7],
|
|
|
|
(addr->ip[8]<<8)|addr->ip[9], (addr->ip[10]<<8)|addr->ip[11], (addr->ip[12]<<8)|addr->ip[13], (addr->ip[14]<<8)|addr->ip[15],
|
|
|
|
addr->port);
|
|
|
|
else
|
|
|
|
str_format(string, max_length, "[%x:%x:%x:%x:%x:%x:%x:%x]",
|
|
|
|
(addr->ip[0]<<8)|addr->ip[1], (addr->ip[2]<<8)|addr->ip[3], (addr->ip[4]<<8)|addr->ip[5], (addr->ip[6]<<8)|addr->ip[7],
|
|
|
|
(addr->ip[8]<<8)|addr->ip[9], (addr->ip[10]<<8)|addr->ip[11], (addr->ip[12]<<8)|addr->ip[13], (addr->ip[14]<<8)|addr->ip[15]);
|
2008-08-17 07:05:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
str_format(string, max_length, "unknown type %d", addr->type);
|
|
|
|
}
|
|
|
|
|
2011-03-28 18:11:28 +00:00
|
|
|
static int priv_net_extract(const char *hostname, char *host, int max_host, int *port)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
*port = 0;
|
|
|
|
host[0] = 0;
|
|
|
|
|
|
|
|
if(hostname[0] == '[')
|
|
|
|
{
|
|
|
|
// ipv6 mode
|
2011-03-30 10:08:33 +00:00
|
|
|
for(i = 1; i < max_host && hostname[i] && hostname[i] != ']'; i++)
|
2011-03-28 18:11:28 +00:00
|
|
|
host[i-1] = hostname[i];
|
|
|
|
host[i-1] = 0;
|
|
|
|
if(hostname[i] != ']') // malformatted
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
i++;
|
|
|
|
if(hostname[i] == ':')
|
|
|
|
*port = atol(hostname+i+1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// generic mode (ipv4, hostname etc)
|
|
|
|
for(i = 0; i < max_host-1 && hostname[i] && hostname[i] != ':'; i++)
|
|
|
|
host[i] = hostname[i];
|
|
|
|
host[i] = 0;
|
|
|
|
|
|
|
|
if(hostname[i] == ':')
|
|
|
|
*port = atol(hostname+i+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
int net_host_lookup(const char *hostname, NETADDR *addr, int types)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
2008-02-24 16:03:58 +00:00
|
|
|
struct addrinfo hints;
|
2014-12-28 00:40:15 +00:00
|
|
|
struct addrinfo *result = NULL;
|
2008-02-24 16:03:58 +00:00
|
|
|
int e;
|
2011-03-28 18:11:28 +00:00
|
|
|
char host[256];
|
|
|
|
int port = 0;
|
|
|
|
|
|
|
|
if(priv_net_extract(hostname, host, sizeof(host), &port))
|
|
|
|
return -1;
|
2014-12-10 02:39:15 +00:00
|
|
|
|
2011-03-28 18:11:28 +00:00
|
|
|
dbg_msg("host lookup", "host='%s' port=%d %d", host, port, types);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-02-24 16:03:58 +00:00
|
|
|
mem_zero(&hints, sizeof(hints));
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-03-28 18:11:28 +00:00
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
|
|
|
|
if(types == NETTYPE_IPV4)
|
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
else if(types == NETTYPE_IPV6)
|
|
|
|
hints.ai_family = AF_INET6;
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2011-03-28 18:11:28 +00:00
|
|
|
e = getaddrinfo(host, NULL, &hints, &result);
|
2013-08-07 23:47:49 +00:00
|
|
|
|
2014-12-28 00:40:15 +00:00
|
|
|
if(!result)
|
2008-02-24 16:03:58 +00:00
|
|
|
return -1;
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2014-12-28 00:40:15 +00:00
|
|
|
if(e != 0)
|
|
|
|
{
|
|
|
|
freeaddrinfo(result);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
sockaddr_to_netaddr(result->ai_addr, addr);
|
2011-03-28 18:11:28 +00:00
|
|
|
addr->port = port;
|
2013-08-07 23:47:49 +00:00
|
|
|
freeaddrinfo(result);
|
2008-02-24 16:03:58 +00:00
|
|
|
return 0;
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
2008-09-03 21:02:30 +00:00
|
|
|
static int parse_int(int *out, const char **str)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
*out = 0;
|
|
|
|
if(**str < '0' || **str > '9')
|
2011-04-13 18:37:12 +00:00
|
|
|
return -1;
|
|
|
|
|
2008-09-03 21:02:30 +00:00
|
|
|
i = **str - '0';
|
|
|
|
(*str)++;
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
if(**str < '0' || **str > '9')
|
|
|
|
{
|
|
|
|
*out = i;
|
2011-04-13 18:37:12 +00:00
|
|
|
return 0;
|
2008-09-03 21:02:30 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-09-03 21:02:30 +00:00
|
|
|
i = (i*10) + (**str - '0');
|
|
|
|
(*str)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_char(char c, const char **str)
|
|
|
|
{
|
|
|
|
if(**str != c) return -1;
|
|
|
|
(*str)++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_uint8(unsigned char *out, const char **str)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if(parse_int(&i, str) != 0) return -1;
|
|
|
|
if(i < 0 || i > 0xff) return -1;
|
|
|
|
*out = i;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_uint16(unsigned short *out, const char **str)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if(parse_int(&i, str) != 0) return -1;
|
|
|
|
if(i < 0 || i > 0xffff) return -1;
|
|
|
|
*out = i;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int net_addr_from_str(NETADDR *addr, const char *string)
|
|
|
|
{
|
|
|
|
const char *str = string;
|
|
|
|
mem_zero(addr, sizeof(NETADDR));
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-09-03 21:02:30 +00:00
|
|
|
if(str[0] == '[')
|
|
|
|
{
|
2011-03-28 18:11:28 +00:00
|
|
|
/* ipv6 */
|
|
|
|
struct sockaddr_in6 sa6;
|
|
|
|
char buf[128];
|
2011-03-28 20:08:52 +00:00
|
|
|
int i;
|
2011-03-30 10:08:33 +00:00
|
|
|
str++;
|
2011-03-28 18:11:28 +00:00
|
|
|
for(i = 0; i < 127 && str[i] && str[i] != ']'; i++)
|
|
|
|
buf[i] = str[i];
|
|
|
|
buf[i] = 0;
|
|
|
|
str += i;
|
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
2011-03-28 20:08:52 +00:00
|
|
|
{
|
|
|
|
int size;
|
|
|
|
sa6.sin6_family = AF_INET6;
|
|
|
|
size = (int)sizeof(sa6);
|
|
|
|
if(WSAStringToAddress(buf, AF_INET6, NULL, (struct sockaddr *)&sa6, &size) != 0)
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-28 18:11:28 +00:00
|
|
|
#else
|
2015-07-12 01:08:58 +00:00
|
|
|
sa6.sin6_family = AF_INET6;
|
|
|
|
|
|
|
|
if(inet_pton(AF_INET6, buf, &sa6.sin6_addr) != 1)
|
2011-03-28 18:11:28 +00:00
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
sockaddr_to_netaddr((struct sockaddr *)&sa6, addr);
|
|
|
|
|
|
|
|
if(*str == ']')
|
|
|
|
{
|
|
|
|
str++;
|
|
|
|
if(*str == ':')
|
|
|
|
{
|
|
|
|
str++;
|
|
|
|
if(parse_uint16(&addr->port, &str))
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
2008-09-03 21:02:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* ipv4 */
|
|
|
|
if(parse_uint8(&addr->ip[0], &str)) return -1;
|
|
|
|
if(parse_char('.', &str)) return -1;
|
|
|
|
if(parse_uint8(&addr->ip[1], &str)) return -1;
|
|
|
|
if(parse_char('.', &str)) return -1;
|
|
|
|
if(parse_uint8(&addr->ip[2], &str)) return -1;
|
|
|
|
if(parse_char('.', &str)) return -1;
|
|
|
|
if(parse_uint8(&addr->ip[3], &str)) return -1;
|
|
|
|
if(*str == ':')
|
|
|
|
{
|
|
|
|
str++;
|
|
|
|
if(parse_uint16(&addr->port, &str)) return -1;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-09-03 21:02:30 +00:00
|
|
|
addr->type = NETTYPE_IPV4;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-09-03 21:02:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-28 18:11:28 +00:00
|
|
|
static void priv_net_close_socket(int sock)
|
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
closesocket(sock);
|
|
|
|
#else
|
|
|
|
close(sock);
|
|
|
|
#endif
|
|
|
|
}
|
2008-09-03 21:02:30 +00:00
|
|
|
|
2011-03-28 18:11:28 +00:00
|
|
|
static int priv_net_close_all_sockets(NETSOCKET sock)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
2011-03-28 18:11:28 +00:00
|
|
|
/* close down ipv4 */
|
|
|
|
if(sock.ipv4sock >= 0)
|
|
|
|
{
|
|
|
|
priv_net_close_socket(sock.ipv4sock);
|
|
|
|
sock.ipv4sock = -1;
|
|
|
|
sock.type &= ~NETTYPE_IPV4;
|
|
|
|
}
|
|
|
|
|
2015-02-07 22:15:58 +00:00
|
|
|
#if defined(WEBSOCKETS)
|
|
|
|
/* close down websocket_ipv4 */
|
|
|
|
if(sock.web_ipv4sock >= 0)
|
|
|
|
{
|
|
|
|
websocket_destroy(sock.web_ipv4sock);
|
|
|
|
sock.web_ipv4sock = -1;
|
|
|
|
sock.type &= ~NETTYPE_WEBSOCKET_IPV4;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-28 18:11:28 +00:00
|
|
|
/* close down ipv6 */
|
|
|
|
if(sock.ipv6sock >= 0)
|
|
|
|
{
|
|
|
|
priv_net_close_socket(sock.ipv6sock);
|
|
|
|
sock.ipv6sock = -1;
|
|
|
|
sock.type &= ~NETTYPE_IPV6;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int priv_net_create_socket(int domain, int type, struct sockaddr *addr, int sockaddrlen)
|
|
|
|
{
|
|
|
|
int sock, e;
|
2007-08-22 07:52:33 +00:00
|
|
|
|
|
|
|
/* create socket */
|
2011-03-28 18:11:28 +00:00
|
|
|
sock = socket(domain, type, 0);
|
2007-08-22 07:52:33 +00:00
|
|
|
if(sock < 0)
|
|
|
|
{
|
2011-12-04 16:09:30 +00:00
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
char buf[128];
|
|
|
|
int error = WSAGetLastError();
|
|
|
|
if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, 0, buf, sizeof(buf), 0) == 0)
|
|
|
|
buf[0] = 0;
|
|
|
|
dbg_msg("net", "failed to create socket with domain %d and type %d (%d '%s')", domain, type, error, buf);
|
|
|
|
#else
|
2011-03-28 18:11:28 +00:00
|
|
|
dbg_msg("net", "failed to create socket with domain %d and type %d (%d '%s')", domain, type, errno, strerror(errno));
|
2011-12-04 16:09:30 +00:00
|
|
|
#endif
|
2011-03-28 18:11:28 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-13 09:49:35 +00:00
|
|
|
#if defined(CONF_FAMILY_UNIX)
|
|
|
|
/* on tcp sockets set SO_REUSEADDR
|
|
|
|
to fix port rebind on restart */
|
|
|
|
if (domain == AF_INET && type == SOCK_STREAM)
|
|
|
|
{
|
|
|
|
int option = 1;
|
|
|
|
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-28 18:11:28 +00:00
|
|
|
/* set to IPv6 only if thats what we are creating */
|
2011-06-18 15:27:14 +00:00
|
|
|
#if defined(IPV6_V6ONLY) /* windows sdk 6.1 and higher */
|
2011-03-28 18:11:28 +00:00
|
|
|
if(domain == AF_INET6)
|
|
|
|
{
|
|
|
|
int ipv6only = 1;
|
|
|
|
setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&ipv6only, sizeof(ipv6only));
|
|
|
|
}
|
2011-06-18 15:27:14 +00:00
|
|
|
#endif
|
2011-03-28 18:11:28 +00:00
|
|
|
|
|
|
|
/* bind the socket */
|
|
|
|
e = bind(sock, addr, sockaddrlen);
|
|
|
|
if(e != 0)
|
|
|
|
{
|
2011-12-04 16:09:30 +00:00
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
char buf[128];
|
|
|
|
int error = WSAGetLastError();
|
|
|
|
if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, 0, buf, sizeof(buf), 0) == 0)
|
|
|
|
buf[0] = 0;
|
2011-12-04 16:12:44 +00:00
|
|
|
dbg_msg("net", "failed to bind socket with domain %d and type %d (%d '%s')", domain, type, error, buf);
|
2011-12-04 16:09:30 +00:00
|
|
|
#else
|
2011-03-28 18:11:28 +00:00
|
|
|
dbg_msg("net", "failed to bind socket with domain %d and type %d (%d '%s')", domain, type, errno, strerror(errno));
|
2011-12-04 16:09:30 +00:00
|
|
|
#endif
|
2011-03-28 18:11:28 +00:00
|
|
|
priv_net_close_socket(sock);
|
|
|
|
return -1;
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-03-28 18:11:28 +00:00
|
|
|
/* return the newly created socket */
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
NETSOCKET net_udp_create(NETADDR bindaddr)
|
|
|
|
{
|
|
|
|
NETSOCKET sock = invalid_socket;
|
|
|
|
NETADDR tmpbindaddr = bindaddr;
|
2011-07-06 16:20:46 +00:00
|
|
|
int broadcast = 1;
|
2013-02-24 16:14:55 +00:00
|
|
|
int recvsize = 65536;
|
2011-03-28 18:11:28 +00:00
|
|
|
|
|
|
|
if(bindaddr.type&NETTYPE_IPV4)
|
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
int socket = -1;
|
|
|
|
|
|
|
|
/* bind, we should check for error */
|
|
|
|
tmpbindaddr.type = NETTYPE_IPV4;
|
|
|
|
netaddr_to_sockaddr_in(&tmpbindaddr, &addr);
|
|
|
|
socket = priv_net_create_socket(AF_INET, SOCK_DGRAM, (struct sockaddr *)&addr, sizeof(addr));
|
|
|
|
if(socket >= 0)
|
|
|
|
{
|
|
|
|
sock.type |= NETTYPE_IPV4;
|
|
|
|
sock.ipv4sock = socket;
|
2011-07-06 16:20:46 +00:00
|
|
|
|
2013-02-24 16:14:55 +00:00
|
|
|
/* set boardcast */
|
|
|
|
setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (const char*)&broadcast, sizeof(broadcast));
|
2011-07-06 16:20:46 +00:00
|
|
|
|
2013-02-24 16:14:55 +00:00
|
|
|
/* set receive buffer size */
|
|
|
|
setsockopt(socket, SOL_SOCKET, SO_RCVBUF, (char*)&recvsize, sizeof(recvsize));
|
2014-12-07 13:19:03 +00:00
|
|
|
|
2014-12-08 17:44:01 +00:00
|
|
|
{
|
|
|
|
/* set DSCP/TOS */
|
|
|
|
int iptos = 0x10 /* IPTOS_LOWDELAY */;
|
|
|
|
//int iptos = 46; /* High Priority */
|
|
|
|
setsockopt(socket, IPPROTO_IP, IP_TOS, (char*)&iptos, sizeof(iptos));
|
|
|
|
}
|
2013-02-24 16:14:55 +00:00
|
|
|
}
|
2011-03-28 18:11:28 +00:00
|
|
|
}
|
|
|
|
|
2015-02-07 22:15:58 +00:00
|
|
|
#if defined(WEBSOCKETS)
|
|
|
|
if(bindaddr.type&NETTYPE_WEBSOCKET_IPV4)
|
|
|
|
{
|
|
|
|
int socket = -1;
|
|
|
|
|
|
|
|
/* bind, we should check for error */
|
|
|
|
tmpbindaddr.type = NETTYPE_WEBSOCKET_IPV4;
|
|
|
|
|
|
|
|
char addr_str[NETADDR_MAXSTRSIZE];
|
|
|
|
net_addr_str(&tmpbindaddr, addr_str, sizeof(addr_str), 0);
|
|
|
|
socket = websocket_create(addr_str, tmpbindaddr.port);
|
|
|
|
|
|
|
|
if (socket >= 0) {
|
|
|
|
sock.type |= NETTYPE_WEBSOCKET_IPV4;
|
|
|
|
sock.web_ipv4sock = socket;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-28 18:11:28 +00:00
|
|
|
if(bindaddr.type&NETTYPE_IPV6)
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 addr;
|
|
|
|
int socket = -1;
|
|
|
|
|
|
|
|
/* bind, we should check for error */
|
|
|
|
tmpbindaddr.type = NETTYPE_IPV6;
|
|
|
|
netaddr_to_sockaddr_in6(&tmpbindaddr, &addr);
|
|
|
|
socket = priv_net_create_socket(AF_INET6, SOCK_DGRAM, (struct sockaddr *)&addr, sizeof(addr));
|
|
|
|
if(socket >= 0)
|
|
|
|
{
|
|
|
|
sock.type |= NETTYPE_IPV6;
|
|
|
|
sock.ipv6sock = socket;
|
2011-07-06 16:20:46 +00:00
|
|
|
|
2013-02-24 16:14:55 +00:00
|
|
|
/* set boardcast */
|
|
|
|
setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (const char*)&broadcast, sizeof(broadcast));
|
2011-07-06 16:20:46 +00:00
|
|
|
|
2013-02-24 16:14:55 +00:00
|
|
|
/* set receive buffer size */
|
|
|
|
setsockopt(socket, SOL_SOCKET, SO_RCVBUF, (char*)&recvsize, sizeof(recvsize));
|
2014-12-07 13:19:03 +00:00
|
|
|
|
2014-12-08 17:44:01 +00:00
|
|
|
{
|
|
|
|
/* set DSCP/TOS */
|
|
|
|
int iptos = 0x10 /* IPTOS_LOWDELAY */;
|
|
|
|
//int iptos = 46; /* High Priority */
|
|
|
|
setsockopt(socket, IPPROTO_IP, IP_TOS, (char*)&iptos, sizeof(iptos));
|
|
|
|
}
|
2013-02-24 16:14:55 +00:00
|
|
|
}
|
2011-03-28 18:11:28 +00:00
|
|
|
}
|
|
|
|
|
2013-02-24 16:14:55 +00:00
|
|
|
/* set non-blocking */
|
|
|
|
net_set_non_blocking(sock);
|
|
|
|
|
2014-12-06 18:19:39 +00:00
|
|
|
#ifdef FUZZING
|
|
|
|
IOHANDLE file = io_open("bar.txt", IOFLAG_READ);
|
|
|
|
gs_NetPosition = 0;
|
|
|
|
gs_NetSize = io_length(file);
|
|
|
|
io_read(file, gs_NetData, 1024);
|
|
|
|
io_close(file);
|
|
|
|
#endif /* FUZZING */
|
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
/* return */
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
int net_udp_send(NETSOCKET sock, const NETADDR *addr, const void *data, int size)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
2014-12-06 18:19:39 +00:00
|
|
|
#ifndef FUZZING
|
2011-03-28 18:11:28 +00:00
|
|
|
int d = -1;
|
|
|
|
|
|
|
|
if(addr->type&NETTYPE_IPV4)
|
|
|
|
{
|
|
|
|
if(sock.ipv4sock >= 0)
|
|
|
|
{
|
|
|
|
struct sockaddr_in sa;
|
|
|
|
if(addr->type&NETTYPE_LINK_BROADCAST)
|
|
|
|
{
|
|
|
|
mem_zero(&sa, sizeof(sa));
|
|
|
|
sa.sin_port = htons(addr->port);
|
|
|
|
sa.sin_family = AF_INET;
|
|
|
|
sa.sin_addr.s_addr = INADDR_BROADCAST;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
netaddr_to_sockaddr_in(addr, &sa);
|
|
|
|
|
|
|
|
d = sendto((int)sock.ipv4sock, (const char*)data, size, 0, (struct sockaddr *)&sa, sizeof(sa));
|
|
|
|
}
|
|
|
|
else
|
2015-06-16 19:39:22 +00:00
|
|
|
dbg_msg("net", "can't send ipv4 traffic to this socket");
|
2011-03-28 18:11:28 +00:00
|
|
|
}
|
|
|
|
|
2015-02-07 22:15:58 +00:00
|
|
|
#if defined(WEBSOCKETS)
|
|
|
|
if(addr->type&NETTYPE_WEBSOCKET_IPV4)
|
|
|
|
{
|
|
|
|
if(sock.web_ipv4sock >= 0)
|
|
|
|
d = websocket_send(sock.web_ipv4sock, (const unsigned char*)data, size, addr->port);
|
|
|
|
else
|
2015-06-16 19:39:22 +00:00
|
|
|
dbg_msg("net", "can't send websocket_ipv4 traffic to this socket");
|
2015-02-07 22:15:58 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-28 18:11:28 +00:00
|
|
|
if(addr->type&NETTYPE_IPV6)
|
|
|
|
{
|
|
|
|
if(sock.ipv6sock >= 0)
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 sa;
|
|
|
|
if(addr->type&NETTYPE_LINK_BROADCAST)
|
|
|
|
{
|
|
|
|
mem_zero(&sa, sizeof(sa));
|
|
|
|
sa.sin6_port = htons(addr->port);
|
|
|
|
sa.sin6_family = AF_INET6;
|
|
|
|
sa.sin6_addr.s6_addr[0] = 0xff; /* multicast */
|
|
|
|
sa.sin6_addr.s6_addr[1] = 0x02; /* link local scope */
|
|
|
|
sa.sin6_addr.s6_addr[15] = 1; /* all nodes */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
netaddr_to_sockaddr_in6(addr, &sa);
|
|
|
|
|
|
|
|
d = sendto((int)sock.ipv6sock, (const char*)data, size, 0, (struct sockaddr *)&sa, sizeof(sa));
|
|
|
|
}
|
|
|
|
else
|
2015-06-16 19:39:22 +00:00
|
|
|
dbg_msg("net", "can't send ipv6 traffic to this socket");
|
2011-03-28 18:11:28 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
else
|
2015-06-16 19:39:22 +00:00
|
|
|
dbg_msg("net", "can't send to network of type %d", addr->type);
|
2011-03-28 18:11:28 +00:00
|
|
|
*/
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
/*if(d < 0)
|
2008-08-17 07:05:16 +00:00
|
|
|
{
|
|
|
|
char addrstr[256];
|
|
|
|
net_addr_str(addr, addrstr, sizeof(addrstr));
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-03-28 18:11:28 +00:00
|
|
|
dbg_msg("net", "sendto error (%d '%s')", errno, strerror(errno));
|
2008-08-17 07:05:16 +00:00
|
|
|
dbg_msg("net", "\tsock = %d %x", sock, sock);
|
|
|
|
dbg_msg("net", "\tsize = %d %x", size, size);
|
|
|
|
dbg_msg("net", "\taddr = %s", addrstr);
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
}*/
|
2008-04-05 14:50:43 +00:00
|
|
|
network_stats.sent_bytes += size;
|
|
|
|
network_stats.sent_packets++;
|
2007-08-22 07:52:33 +00:00
|
|
|
return d;
|
2014-12-06 18:19:39 +00:00
|
|
|
#else
|
|
|
|
return size;
|
|
|
|
#endif /* FUZZING */
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
int net_udp_recv(NETSOCKET sock, NETADDR *addr, void *data, int maxsize)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
2014-12-06 18:19:39 +00:00
|
|
|
#ifndef FUZZING
|
2011-03-28 18:11:28 +00:00
|
|
|
char sockaddrbuf[128];
|
|
|
|
socklen_t fromlen;// = sizeof(sockaddrbuf);
|
|
|
|
int bytes = 0;
|
|
|
|
|
|
|
|
if(bytes == 0 && sock.ipv4sock >= 0)
|
|
|
|
{
|
|
|
|
fromlen = sizeof(struct sockaddr_in);
|
|
|
|
bytes = recvfrom(sock.ipv4sock, (char*)data, maxsize, 0, (struct sockaddr *)&sockaddrbuf, &fromlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(bytes <= 0 && sock.ipv6sock >= 0)
|
|
|
|
{
|
|
|
|
fromlen = sizeof(struct sockaddr_in6);
|
|
|
|
bytes = recvfrom(sock.ipv6sock, (char*)data, maxsize, 0, (struct sockaddr *)&sockaddrbuf, &fromlen);
|
|
|
|
}
|
|
|
|
|
2015-02-07 22:15:58 +00:00
|
|
|
#if defined(WEBSOCKETS)
|
|
|
|
if(bytes <= 0 && sock.web_ipv4sock >= 0)
|
|
|
|
{
|
|
|
|
fromlen = sizeof(struct sockaddr);
|
|
|
|
bytes = websocket_recv(sock.web_ipv4sock, data, maxsize, (struct sockaddr_in *)&sockaddrbuf, fromlen);
|
|
|
|
((struct sockaddr_in *)&sockaddrbuf)->sin_family = AF_WEBSOCKET_INET;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
if(bytes > 0)
|
|
|
|
{
|
2011-03-28 18:11:28 +00:00
|
|
|
sockaddr_to_netaddr((struct sockaddr *)&sockaddrbuf, addr);
|
2008-04-05 14:50:43 +00:00
|
|
|
network_stats.recv_bytes += bytes;
|
|
|
|
network_stats.recv_packets++;
|
2007-08-22 07:52:33 +00:00
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
else if(bytes == 0)
|
|
|
|
return 0;
|
|
|
|
return -1; /* error */
|
2014-12-06 18:19:39 +00:00
|
|
|
#else
|
|
|
|
addr->type = NETTYPE_IPV4;
|
|
|
|
addr->port = 11111;
|
|
|
|
addr->ip[0] = 127;
|
|
|
|
addr->ip[1] = 0;
|
|
|
|
addr->ip[2] = 0;
|
|
|
|
addr->ip[3] = 1;
|
|
|
|
|
|
|
|
int CurrentData = 0;
|
|
|
|
while (gs_NetPosition < gs_NetSize && CurrentData < maxsize)
|
|
|
|
{
|
|
|
|
if(gs_NetData[gs_NetPosition] == '\n')
|
|
|
|
{
|
|
|
|
gs_NetPosition++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
((unsigned char*)data)[CurrentData] = gs_NetData[gs_NetPosition];
|
|
|
|
CurrentData++;
|
|
|
|
gs_NetPosition++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gs_NetPosition >= gs_NetSize)
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
return CurrentData;
|
|
|
|
#endif /* FUZZING */
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
int net_udp_close(NETSOCKET sock)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
2011-03-28 18:11:28 +00:00
|
|
|
return priv_net_close_all_sockets(sock);
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 16:20:46 +00:00
|
|
|
NETSOCKET net_tcp_create(NETADDR bindaddr)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
2011-03-28 18:11:28 +00:00
|
|
|
NETSOCKET sock = invalid_socket;
|
2011-07-06 16:20:46 +00:00
|
|
|
NETADDR tmpbindaddr = bindaddr;
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2011-07-06 16:20:46 +00:00
|
|
|
if(bindaddr.type&NETTYPE_IPV4)
|
2011-03-28 18:11:28 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
2011-07-06 16:20:46 +00:00
|
|
|
int socket = -1;
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2011-07-06 16:20:46 +00:00
|
|
|
/* bind, we should check for error */
|
|
|
|
tmpbindaddr.type = NETTYPE_IPV4;
|
|
|
|
netaddr_to_sockaddr_in(&tmpbindaddr, &addr);
|
|
|
|
socket = priv_net_create_socket(AF_INET, SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr));
|
|
|
|
if(socket >= 0)
|
|
|
|
{
|
|
|
|
sock.type |= NETTYPE_IPV4;
|
|
|
|
sock.ipv4sock = socket;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(bindaddr.type&NETTYPE_IPV6)
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 addr;
|
|
|
|
int socket = -1;
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2011-03-28 18:11:28 +00:00
|
|
|
/* bind, we should check for error */
|
2011-07-06 16:20:46 +00:00
|
|
|
tmpbindaddr.type = NETTYPE_IPV6;
|
|
|
|
netaddr_to_sockaddr_in6(&tmpbindaddr, &addr);
|
|
|
|
socket = priv_net_create_socket(AF_INET6, SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr));
|
|
|
|
if(socket >= 0)
|
|
|
|
{
|
|
|
|
sock.type |= NETTYPE_IPV6;
|
|
|
|
sock.ipv6sock = socket;
|
|
|
|
}
|
2011-03-28 18:11:28 +00:00
|
|
|
}
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2011-04-13 18:37:12 +00:00
|
|
|
/* return */
|
|
|
|
return sock;
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 16:20:46 +00:00
|
|
|
int net_set_non_blocking(NETSOCKET sock)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
2010-08-16 00:21:18 +00:00
|
|
|
unsigned long mode = 1;
|
2011-03-28 18:11:28 +00:00
|
|
|
if(sock.ipv4sock >= 0)
|
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
ioctlsocket(sock.ipv4sock, FIONBIO, (unsigned long *)&mode);
|
|
|
|
#else
|
|
|
|
ioctl(sock.ipv4sock, FIONBIO, (unsigned long *)&mode);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sock.ipv6sock >= 0)
|
|
|
|
{
|
2007-08-22 07:52:33 +00:00
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
2011-03-28 18:11:28 +00:00
|
|
|
ioctlsocket(sock.ipv6sock, FIONBIO, (unsigned long *)&mode);
|
2007-08-22 07:52:33 +00:00
|
|
|
#else
|
2011-03-28 18:11:28 +00:00
|
|
|
ioctl(sock.ipv6sock, FIONBIO, (unsigned long *)&mode);
|
2007-08-22 07:52:33 +00:00
|
|
|
#endif
|
2011-03-28 18:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 16:20:46 +00:00
|
|
|
int net_set_blocking(NETSOCKET sock)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
2010-08-16 00:21:18 +00:00
|
|
|
unsigned long mode = 0;
|
2011-03-28 18:11:28 +00:00
|
|
|
if(sock.ipv4sock >= 0)
|
|
|
|
{
|
2007-08-22 07:52:33 +00:00
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
2011-03-28 18:11:28 +00:00
|
|
|
ioctlsocket(sock.ipv4sock, FIONBIO, (unsigned long *)&mode);
|
2007-08-22 07:52:33 +00:00
|
|
|
#else
|
2011-03-28 18:11:28 +00:00
|
|
|
ioctl(sock.ipv4sock, FIONBIO, (unsigned long *)&mode);
|
2007-08-22 07:52:33 +00:00
|
|
|
#endif
|
2011-03-28 18:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(sock.ipv6sock >= 0)
|
|
|
|
{
|
2007-08-22 07:52:33 +00:00
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
2011-03-28 18:11:28 +00:00
|
|
|
ioctlsocket(sock.ipv6sock, FIONBIO, (unsigned long *)&mode);
|
2007-08-22 07:52:33 +00:00
|
|
|
#else
|
2011-03-28 18:11:28 +00:00
|
|
|
ioctl(sock.ipv6sock, FIONBIO, (unsigned long *)&mode);
|
2007-08-22 07:52:33 +00:00
|
|
|
#endif
|
2011-03-28 18:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
int net_tcp_listen(NETSOCKET sock, int backlog)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
2011-07-30 11:40:01 +00:00
|
|
|
int err = -1;
|
2011-03-28 18:11:28 +00:00
|
|
|
if(sock.ipv4sock >= 0)
|
2011-07-30 11:40:01 +00:00
|
|
|
err = listen(sock.ipv4sock, backlog);
|
2011-03-28 18:11:28 +00:00
|
|
|
if(sock.ipv6sock >= 0)
|
2011-07-30 11:40:01 +00:00
|
|
|
err = listen(sock.ipv6sock, backlog);
|
|
|
|
return err;
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
int net_tcp_accept(NETSOCKET sock, NETSOCKET *new_sock, NETADDR *a)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
|
|
|
int s;
|
|
|
|
socklen_t sockaddr_len;
|
|
|
|
|
2011-03-28 18:11:28 +00:00
|
|
|
*new_sock = invalid_socket;
|
|
|
|
|
|
|
|
if(sock.ipv4sock >= 0)
|
|
|
|
{
|
2011-07-30 11:40:01 +00:00
|
|
|
struct sockaddr_in addr;
|
|
|
|
sockaddr_len = sizeof(addr);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2011-07-30 11:40:01 +00:00
|
|
|
s = accept(sock.ipv4sock, (struct sockaddr *)&addr, &sockaddr_len);
|
2011-08-11 08:59:14 +00:00
|
|
|
|
2011-03-28 18:11:28 +00:00
|
|
|
if (s != -1)
|
|
|
|
{
|
2011-07-30 11:40:01 +00:00
|
|
|
sockaddr_to_netaddr((const struct sockaddr *)&addr, a);
|
2011-03-28 18:11:28 +00:00
|
|
|
new_sock->type = NETTYPE_IPV4;
|
|
|
|
new_sock->ipv4sock = s;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2011-03-28 18:11:28 +00:00
|
|
|
if(sock.ipv6sock >= 0)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
2011-07-30 11:40:01 +00:00
|
|
|
struct sockaddr_in6 addr;
|
|
|
|
sockaddr_len = sizeof(addr);
|
2011-03-28 18:11:28 +00:00
|
|
|
|
2011-07-30 11:40:01 +00:00
|
|
|
s = accept(sock.ipv6sock, (struct sockaddr *)&addr, &sockaddr_len);
|
2011-08-11 08:59:14 +00:00
|
|
|
|
2011-03-28 18:11:28 +00:00
|
|
|
if (s != -1)
|
|
|
|
{
|
2011-07-30 11:40:01 +00:00
|
|
|
sockaddr_to_netaddr((const struct sockaddr *)&addr, a);
|
2011-03-28 18:11:28 +00:00
|
|
|
new_sock->type = NETTYPE_IPV6;
|
|
|
|
new_sock->ipv6sock = s;
|
|
|
|
return s;
|
|
|
|
}
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
2011-03-28 18:11:28 +00:00
|
|
|
|
2011-07-30 11:40:01 +00:00
|
|
|
return -1;
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
int net_tcp_connect(NETSOCKET sock, const NETADDR *a)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
2011-07-06 16:20:46 +00:00
|
|
|
if(a->type&NETTYPE_IPV4)
|
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
netaddr_to_sockaddr_in(a, &addr);
|
|
|
|
return connect(sock.ipv4sock, (struct sockaddr *)&addr, sizeof(addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(a->type&NETTYPE_IPV6)
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 addr;
|
|
|
|
netaddr_to_sockaddr_in6(a, &addr);
|
|
|
|
return connect(sock.ipv6sock, (struct sockaddr *)&addr, sizeof(addr));
|
|
|
|
}
|
|
|
|
|
2011-07-30 11:40:01 +00:00
|
|
|
return -1;
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 16:20:46 +00:00
|
|
|
int net_tcp_connect_non_blocking(NETSOCKET sock, NETADDR bindaddr)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
2011-03-28 18:11:28 +00:00
|
|
|
int res = 0;
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2011-07-06 16:20:46 +00:00
|
|
|
net_set_non_blocking(sock);
|
|
|
|
res = net_tcp_connect(sock, &bindaddr);
|
|
|
|
net_set_blocking(sock);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
int net_tcp_send(NETSOCKET sock, const void *data, int size)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
2011-07-30 11:40:01 +00:00
|
|
|
int bytes = -1;
|
2011-07-06 16:20:46 +00:00
|
|
|
|
|
|
|
if(sock.ipv4sock >= 0)
|
|
|
|
bytes = send((int)sock.ipv4sock, (const char*)data, size, 0);
|
|
|
|
if(sock.ipv6sock >= 0)
|
|
|
|
bytes = send((int)sock.ipv6sock, (const char*)data, size, 0);
|
2011-08-11 08:59:14 +00:00
|
|
|
|
2011-03-28 18:11:28 +00:00
|
|
|
return bytes;
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
int net_tcp_recv(NETSOCKET sock, void *data, int maxsize)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
2011-07-30 11:40:01 +00:00
|
|
|
int bytes = -1;
|
2011-07-06 16:20:46 +00:00
|
|
|
|
|
|
|
if(sock.ipv4sock >= 0)
|
|
|
|
bytes = recv((int)sock.ipv4sock, (char*)data, maxsize, 0);
|
|
|
|
if(sock.ipv6sock >= 0)
|
|
|
|
bytes = recv((int)sock.ipv6sock, (char*)data, maxsize, 0);
|
2011-08-11 08:59:14 +00:00
|
|
|
|
2011-03-28 18:11:28 +00:00
|
|
|
return bytes;
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
int net_tcp_close(NETSOCKET sock)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
2011-03-28 18:11:28 +00:00
|
|
|
return priv_net_close_all_sockets(sock);
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int net_errno()
|
|
|
|
{
|
2011-07-30 11:40:01 +00:00
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
return WSAGetLastError();
|
|
|
|
#else
|
2007-08-22 07:52:33 +00:00
|
|
|
return errno;
|
2011-07-30 11:40:01 +00:00
|
|
|
#endif
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int net_would_block()
|
|
|
|
{
|
2011-07-30 11:40:01 +00:00
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
return net_errno() == WSAEWOULDBLOCK;
|
|
|
|
#else
|
2007-08-22 07:52:33 +00:00
|
|
|
return net_errno() == EWOULDBLOCK;
|
2011-07-30 11:40:01 +00:00
|
|
|
#endif
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int net_init()
|
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
WSADATA wsaData;
|
|
|
|
int err = WSAStartup(MAKEWORD(1, 1), &wsaData);
|
|
|
|
dbg_assert(err == 0, "network initialization failed.");
|
|
|
|
return err==0?0:1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-27 12:57:56 +00:00
|
|
|
int fs_listdir_info(const char *dir, FS_LISTDIR_INFO_CALLBACK cb, int type, void *user)
|
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
WIN32_FIND_DATA finddata;
|
|
|
|
HANDLE handle;
|
|
|
|
char buffer[1024*2];
|
|
|
|
int length;
|
|
|
|
str_format(buffer, sizeof(buffer), "%s/*", dir);
|
|
|
|
|
|
|
|
handle = FindFirstFileA(buffer, &finddata);
|
|
|
|
|
|
|
|
if (handle == INVALID_HANDLE_VALUE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
str_format(buffer, sizeof(buffer), "%s/", dir);
|
|
|
|
length = str_length(buffer);
|
|
|
|
|
|
|
|
/* add all the entries */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
str_copy(buffer+length, finddata.cFileName, (int)sizeof(buffer)-length);
|
|
|
|
if(cb(finddata.cFileName, fs_getmtime(buffer), fs_is_dir(buffer), type, user))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (FindNextFileA(handle, &finddata));
|
|
|
|
|
|
|
|
FindClose(handle);
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
struct dirent *entry;
|
|
|
|
char buffer[1024*2];
|
|
|
|
int length;
|
|
|
|
DIR *d = opendir(dir);
|
|
|
|
|
|
|
|
if(!d)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
str_format(buffer, sizeof(buffer), "%s/", dir);
|
|
|
|
length = str_length(buffer);
|
|
|
|
|
|
|
|
while((entry = readdir(d)) != NULL)
|
|
|
|
{
|
|
|
|
str_copy(buffer+length, entry->d_name, (int)sizeof(buffer)-length);
|
|
|
|
if(cb(entry->d_name, fs_getmtime(buffer), fs_is_dir(buffer), type, user))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* close the directory and return */
|
|
|
|
closedir(d);
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-09-24 11:38:03 +00:00
|
|
|
int fs_listdir(const char *dir, FS_LISTDIR_CALLBACK cb, int type, void *user)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
WIN32_FIND_DATA finddata;
|
|
|
|
HANDLE handle;
|
|
|
|
char buffer[1024*2];
|
2010-09-28 22:53:53 +00:00
|
|
|
int length;
|
2008-02-11 21:49:26 +00:00
|
|
|
str_format(buffer, sizeof(buffer), "%s/*", dir);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
|
|
|
handle = FindFirstFileA(buffer, &finddata);
|
|
|
|
|
|
|
|
if (handle == INVALID_HANDLE_VALUE)
|
|
|
|
return 0;
|
|
|
|
|
2010-09-28 22:53:53 +00:00
|
|
|
str_format(buffer, sizeof(buffer), "%s/", dir);
|
|
|
|
length = str_length(buffer);
|
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
/* add all the entries */
|
|
|
|
do
|
|
|
|
{
|
2010-09-28 22:53:53 +00:00
|
|
|
str_copy(buffer+length, finddata.cFileName, (int)sizeof(buffer)-length);
|
2011-02-21 10:23:30 +00:00
|
|
|
if(cb(finddata.cFileName, fs_is_dir(buffer), type, user))
|
|
|
|
break;
|
2010-09-28 22:53:53 +00:00
|
|
|
}
|
|
|
|
while (FindNextFileA(handle, &finddata));
|
2007-08-22 07:52:33 +00:00
|
|
|
|
|
|
|
FindClose(handle);
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
struct dirent *entry;
|
2010-09-28 22:53:53 +00:00
|
|
|
char buffer[1024*2];
|
|
|
|
int length;
|
2007-08-22 07:52:33 +00:00
|
|
|
DIR *d = opendir(dir);
|
|
|
|
|
|
|
|
if(!d)
|
|
|
|
return 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-09-28 22:53:53 +00:00
|
|
|
str_format(buffer, sizeof(buffer), "%s/", dir);
|
|
|
|
length = str_length(buffer);
|
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
while((entry = readdir(d)) != NULL)
|
2010-09-28 22:53:53 +00:00
|
|
|
{
|
|
|
|
str_copy(buffer+length, entry->d_name, (int)sizeof(buffer)-length);
|
2011-02-21 10:23:30 +00:00
|
|
|
if(cb(entry->d_name, fs_is_dir(buffer), type, user))
|
|
|
|
break;
|
2010-09-28 22:53:53 +00:00
|
|
|
}
|
2007-08-22 07:52:33 +00:00
|
|
|
|
|
|
|
/* close the directory and return */
|
|
|
|
closedir(d);
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
2007-09-25 19:48:52 +00:00
|
|
|
|
2007-11-08 09:11:32 +00:00
|
|
|
int fs_storage_path(const char *appname, char *path, int max)
|
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
2008-05-10 17:18:56 +00:00
|
|
|
char *home = getenv("APPDATA");
|
|
|
|
if(!home)
|
2008-08-05 21:37:33 +00:00
|
|
|
return -1;
|
2007-11-18 22:06:41 +00:00
|
|
|
_snprintf(path, max, "%s/%s", home, appname);
|
|
|
|
return 0;
|
2007-11-08 09:11:32 +00:00
|
|
|
#else
|
|
|
|
char *home = getenv("HOME");
|
2008-10-06 16:44:34 +00:00
|
|
|
#if !defined(CONF_PLATFORM_MACOSX)
|
2007-11-08 09:11:32 +00:00
|
|
|
int i;
|
2008-10-06 16:44:34 +00:00
|
|
|
#endif
|
2007-11-08 09:11:32 +00:00
|
|
|
if(!home)
|
2008-08-05 21:37:33 +00:00
|
|
|
return -1;
|
2007-11-18 22:06:41 +00:00
|
|
|
|
2008-03-22 15:09:49 +00:00
|
|
|
#if defined(CONF_PLATFORM_MACOSX)
|
|
|
|
snprintf(path, max, "%s/Library/Application Support/%s", home, appname);
|
|
|
|
#else
|
2007-11-08 09:11:32 +00:00
|
|
|
snprintf(path, max, "%s/.%s", home, appname);
|
|
|
|
for(i = strlen(home)+2; path[i]; i++)
|
|
|
|
path[i] = tolower(path[i]);
|
2008-03-22 15:09:49 +00:00
|
|
|
#endif
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-08-05 21:37:33 +00:00
|
|
|
return 0;
|
2007-11-08 09:11:32 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int fs_makedir(const char *path)
|
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
2007-11-18 22:06:41 +00:00
|
|
|
if(_mkdir(path) == 0)
|
|
|
|
return 0;
|
|
|
|
if(errno == EEXIST)
|
|
|
|
return 0;
|
2008-08-05 21:37:33 +00:00
|
|
|
return -1;
|
2007-11-08 09:11:32 +00:00
|
|
|
#else
|
|
|
|
if(mkdir(path, 0755) == 0)
|
|
|
|
return 0;
|
|
|
|
if(errno == EEXIST)
|
|
|
|
return 0;
|
2008-08-05 21:37:33 +00:00
|
|
|
return -1;
|
2007-11-08 09:11:32 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-10-01 17:16:22 +00:00
|
|
|
int fs_is_dir(const char *path)
|
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
/* TODO: do this smarter */
|
|
|
|
WIN32_FIND_DATA finddata;
|
|
|
|
HANDLE handle;
|
|
|
|
char buffer[1024*2];
|
|
|
|
str_format(buffer, sizeof(buffer), "%s/*", path);
|
|
|
|
|
|
|
|
if ((handle = FindFirstFileA(buffer, &finddata)) == INVALID_HANDLE_VALUE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
FindClose(handle);
|
|
|
|
return 1;
|
|
|
|
#else
|
|
|
|
struct stat sb;
|
|
|
|
if (stat(path, &sb) == -1)
|
|
|
|
return 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-10-01 17:16:22 +00:00
|
|
|
if (S_ISDIR(sb.st_mode))
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-08-22 15:57:58 +00:00
|
|
|
time_t fs_getmtime(const char *path)
|
|
|
|
{
|
2015-08-26 13:44:25 +00:00
|
|
|
struct stat sb;
|
2015-08-22 15:57:58 +00:00
|
|
|
if (stat(path, &sb) == -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return sb.st_mtime;
|
|
|
|
}
|
|
|
|
|
2008-10-01 17:16:22 +00:00
|
|
|
int fs_chdir(const char *path)
|
|
|
|
{
|
2010-11-17 17:36:19 +00:00
|
|
|
if(fs_is_dir(path))
|
2008-10-01 17:16:22 +00:00
|
|
|
{
|
2010-11-17 17:36:19 +00:00
|
|
|
if(chdir(path))
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
2008-10-01 17:16:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-12-11 22:10:13 +00:00
|
|
|
char *fs_getcwd(char *buffer, int buffer_size)
|
|
|
|
{
|
|
|
|
if(buffer == 0)
|
|
|
|
return 0;
|
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
return _getcwd(buffer, buffer_size);
|
|
|
|
#else
|
|
|
|
return getcwd(buffer, buffer_size);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-10-07 21:51:07 +00:00
|
|
|
int fs_parent_dir(char *path)
|
2010-09-12 11:15:59 +00:00
|
|
|
{
|
2010-09-16 10:48:32 +00:00
|
|
|
char *parent = 0;
|
|
|
|
for(; *path; ++path)
|
2010-09-12 11:15:59 +00:00
|
|
|
{
|
2010-09-16 10:48:32 +00:00
|
|
|
if(*path == '/' || *path == '\\')
|
|
|
|
parent = path;
|
2010-09-12 11:15:59 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-09-16 10:48:32 +00:00
|
|
|
if(parent)
|
2010-10-07 21:51:07 +00:00
|
|
|
{
|
2010-09-16 10:48:32 +00:00
|
|
|
*parent = 0;
|
2010-10-07 21:51:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
2010-09-12 11:15:59 +00:00
|
|
|
}
|
|
|
|
|
2010-12-07 23:13:59 +00:00
|
|
|
int fs_remove(const char *filename)
|
|
|
|
{
|
|
|
|
if(remove(filename) != 0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-07 23:09:18 +00:00
|
|
|
int fs_rename(const char *oldname, const char *newname)
|
2015-07-09 00:08:14 +00:00
|
|
|
{
|
2015-03-14 16:49:18 +00:00
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
2015-07-28 00:12:05 +00:00
|
|
|
if(MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED) != 0)
|
2015-03-14 16:49:18 +00:00
|
|
|
return 1;
|
|
|
|
#else
|
2010-12-07 23:09:18 +00:00
|
|
|
if(rename(oldname, newname) != 0)
|
|
|
|
return 1;
|
2015-03-14 16:49:18 +00:00
|
|
|
#endif
|
2010-12-07 23:09:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-25 19:48:52 +00:00
|
|
|
void swap_endian(void *data, unsigned elem_size, unsigned num)
|
|
|
|
{
|
|
|
|
char *src = (char*) data;
|
|
|
|
char *dst = src + (elem_size - 1);
|
|
|
|
|
|
|
|
while(num)
|
|
|
|
{
|
|
|
|
unsigned n = elem_size>>1;
|
|
|
|
char tmp;
|
|
|
|
while(n)
|
|
|
|
{
|
|
|
|
tmp = *src;
|
|
|
|
*src = *dst;
|
|
|
|
*dst = tmp;
|
|
|
|
|
|
|
|
src++;
|
|
|
|
dst--;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
|
|
|
|
src = src + (elem_size>>1);
|
|
|
|
dst = src + (elem_size - 1);
|
|
|
|
num--;
|
|
|
|
}
|
|
|
|
}
|
2007-10-04 09:49:38 +00:00
|
|
|
|
2008-02-10 15:32:30 +00:00
|
|
|
int net_socket_read_wait(NETSOCKET sock, int time)
|
|
|
|
{
|
2011-04-13 18:37:12 +00:00
|
|
|
struct timeval tv;
|
|
|
|
fd_set readfds;
|
2011-03-28 18:11:28 +00:00
|
|
|
int sockid;
|
2008-02-10 15:32:30 +00:00
|
|
|
|
2014-11-11 12:00:02 +00:00
|
|
|
tv.tv_sec = time / 1000000;
|
|
|
|
tv.tv_usec = time % 1000000;
|
2011-03-28 18:11:28 +00:00
|
|
|
sockid = 0;
|
2008-02-10 15:32:30 +00:00
|
|
|
|
2011-04-13 18:37:12 +00:00
|
|
|
FD_ZERO(&readfds);
|
|
|
|
if(sock.ipv4sock >= 0)
|
2011-03-28 18:11:28 +00:00
|
|
|
{
|
|
|
|
FD_SET(sock.ipv4sock, &readfds);
|
|
|
|
sockid = sock.ipv4sock;
|
|
|
|
}
|
|
|
|
if(sock.ipv6sock >= 0)
|
|
|
|
{
|
|
|
|
FD_SET(sock.ipv6sock, &readfds);
|
|
|
|
if(sock.ipv6sock > sockid)
|
|
|
|
sockid = sock.ipv6sock;
|
|
|
|
}
|
2015-02-07 22:15:58 +00:00
|
|
|
#if defined(WEBSOCKETS)
|
|
|
|
if(sock.web_ipv4sock >= 0)
|
|
|
|
{
|
|
|
|
int maxfd = websocket_fd_set(sock.web_ipv4sock, &readfds);
|
|
|
|
if (maxfd > sockid)
|
|
|
|
sockid = maxfd;
|
|
|
|
}
|
|
|
|
#endif
|
2008-02-10 15:32:30 +00:00
|
|
|
|
2011-04-13 18:37:12 +00:00
|
|
|
/* don't care about writefds and exceptfds */
|
2014-01-15 15:52:22 +00:00
|
|
|
if(time < 0)
|
|
|
|
select(sockid+1, &readfds, NULL, NULL, NULL);
|
|
|
|
else
|
|
|
|
select(sockid+1, &readfds, NULL, NULL, &tv);
|
2011-03-28 18:11:28 +00:00
|
|
|
|
|
|
|
if(sock.ipv4sock >= 0 && FD_ISSET(sock.ipv4sock, &readfds))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if(sock.ipv6sock >= 0 && FD_ISSET(sock.ipv6sock, &readfds))
|
|
|
|
return 1;
|
|
|
|
|
2011-04-13 18:37:12 +00:00
|
|
|
return 0;
|
2008-02-10 15:32:30 +00:00
|
|
|
}
|
|
|
|
|
2011-12-29 22:36:53 +00:00
|
|
|
int time_timestamp()
|
2008-02-10 21:54:52 +00:00
|
|
|
{
|
|
|
|
return time(0);
|
|
|
|
}
|
|
|
|
|
2008-02-11 21:49:26 +00:00
|
|
|
void str_append(char *dst, const char *src, int dst_size)
|
|
|
|
{
|
|
|
|
int s = strlen(dst);
|
|
|
|
int i = 0;
|
|
|
|
while(s < dst_size)
|
|
|
|
{
|
|
|
|
dst[s] = src[i];
|
|
|
|
if(!src[i]) /* check for null termination */
|
|
|
|
break;
|
|
|
|
s++;
|
|
|
|
i++;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-02-11 21:49:26 +00:00
|
|
|
dst[dst_size-1] = 0; /* assure null termination */
|
|
|
|
}
|
|
|
|
|
|
|
|
void str_copy(char *dst, const char *src, int dst_size)
|
|
|
|
{
|
|
|
|
strncpy(dst, src, dst_size);
|
|
|
|
dst[dst_size-1] = 0; /* assure null termination */
|
|
|
|
}
|
|
|
|
|
2008-11-08 08:27:11 +00:00
|
|
|
int str_length(const char *str)
|
|
|
|
{
|
|
|
|
return (int)strlen(str);
|
|
|
|
}
|
|
|
|
|
2013-07-21 02:52:23 +00:00
|
|
|
int str_format(char *buffer, int buffer_size, const char *format, ...)
|
2008-02-11 21:49:26 +00:00
|
|
|
{
|
2013-07-21 02:52:23 +00:00
|
|
|
int ret;
|
2008-02-11 21:49:26 +00:00
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
2013-07-21 02:52:23 +00:00
|
|
|
ret = _vsnprintf(buffer, buffer_size, format, ap);
|
2011-04-13 18:37:12 +00:00
|
|
|
va_end(ap);
|
2008-02-11 21:49:26 +00:00
|
|
|
#else
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
2013-07-21 02:52:23 +00:00
|
|
|
ret = vsnprintf(buffer, buffer_size, format, ap);
|
2011-04-13 18:37:12 +00:00
|
|
|
va_end(ap);
|
2008-02-11 21:49:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
buffer[buffer_size-1] = 0; /* assure null termination */
|
2013-07-21 02:52:23 +00:00
|
|
|
return ret;
|
2008-02-11 21:49:26 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 21:54:47 +00:00
|
|
|
char *str_trim_words(char *str, int words)
|
|
|
|
{
|
2015-07-09 00:00:40 +00:00
|
|
|
while (words && *str)
|
|
|
|
{
|
|
|
|
if (isspace(*str) && !isspace(*(str + 1)))
|
|
|
|
words--;
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
return str;
|
2015-02-19 21:54:47 +00:00
|
|
|
}
|
|
|
|
|
2008-02-24 16:03:58 +00:00
|
|
|
/* makes sure that the string only contains the characters between 32 and 127 */
|
2008-03-17 01:22:25 +00:00
|
|
|
void str_sanitize_strong(char *str_in)
|
2008-02-24 16:03:58 +00:00
|
|
|
{
|
2008-03-17 01:22:25 +00:00
|
|
|
unsigned char *str = (unsigned char *)str_in;
|
2008-02-24 16:03:58 +00:00
|
|
|
while(*str)
|
|
|
|
{
|
|
|
|
*str &= 0x7f;
|
|
|
|
if(*str < 32)
|
|
|
|
*str = 32;
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-12 13:22:07 +00:00
|
|
|
/* makes sure that the string only contains the characters between 32 and 255 */
|
|
|
|
void str_sanitize_cc(char *str_in)
|
|
|
|
{
|
|
|
|
unsigned char *str = (unsigned char *)str_in;
|
|
|
|
while(*str)
|
|
|
|
{
|
|
|
|
if(*str < 32)
|
|
|
|
*str = ' ';
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-24 16:03:58 +00:00
|
|
|
/* makes sure that the string only contains the characters between 32 and 255 + \r\n\t */
|
2008-03-17 01:22:25 +00:00
|
|
|
void str_sanitize(char *str_in)
|
2008-02-24 16:03:58 +00:00
|
|
|
{
|
2008-03-17 01:22:25 +00:00
|
|
|
unsigned char *str = (unsigned char *)str_in;
|
2008-02-24 16:03:58 +00:00
|
|
|
while(*str)
|
|
|
|
{
|
|
|
|
if(*str < 32 && !(*str == '\r') && !(*str == '\n') && !(*str == '\t'))
|
|
|
|
*str = ' ';
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-25 16:41:15 +00:00
|
|
|
char *str_skip_to_whitespace(char *str)
|
|
|
|
{
|
|
|
|
while(*str && (*str != ' ' && *str != '\t' && *str != '\n'))
|
|
|
|
str++;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2010-08-12 13:22:07 +00:00
|
|
|
char *str_skip_whitespaces(char *str)
|
|
|
|
{
|
|
|
|
while(*str && (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r'))
|
|
|
|
str++;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2008-03-29 17:20:21 +00:00
|
|
|
/* case */
|
|
|
|
int str_comp_nocase(const char *a, const char *b)
|
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
return _stricmp(a,b);
|
|
|
|
#else
|
|
|
|
return strcasecmp(a,b);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-06-09 21:28:20 +00:00
|
|
|
int str_comp_nocase_num(const char *a, const char *b, const int num)
|
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
return _strnicmp(a, b, num);
|
|
|
|
#else
|
|
|
|
return strncasecmp(a, b, num);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-06-13 16:54:04 +00:00
|
|
|
int str_comp(const char *a, const char *b)
|
|
|
|
{
|
|
|
|
return strcmp(a, b);
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int str_comp_num(const char *a, const char *b, const int num)
|
|
|
|
{
|
|
|
|
return strncmp(a, b, num);
|
|
|
|
}
|
2009-06-13 16:54:04 +00:00
|
|
|
|
2010-09-28 22:53:53 +00:00
|
|
|
int str_comp_filenames(const char *a, const char *b)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
for(; *a && *b; ++a, ++b)
|
|
|
|
{
|
|
|
|
if(*a >= '0' && *a <= '9' && *b >= '0' && *b <= '9')
|
|
|
|
{
|
|
|
|
result = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if(!result)
|
|
|
|
result = *a - *b;
|
|
|
|
++a; ++b;
|
|
|
|
}
|
|
|
|
while(*a >= '0' && *a <= '9' && *b >= '0' && *b <= '9');
|
|
|
|
|
|
|
|
if(*a >= '0' && *a <= '9')
|
|
|
|
return 1;
|
|
|
|
else if(*b >= '0' && *b <= '9')
|
|
|
|
return -1;
|
|
|
|
else if(result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(*a != *b)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return *a - *b;
|
|
|
|
}
|
|
|
|
|
2008-03-29 17:20:21 +00:00
|
|
|
const char *str_find_nocase(const char *haystack, const char *needle)
|
|
|
|
{
|
|
|
|
while(*haystack) /* native implementation */
|
|
|
|
{
|
|
|
|
const char *a = haystack;
|
|
|
|
const char *b = needle;
|
|
|
|
while(*a && *b && tolower(*a) == tolower(*b))
|
|
|
|
{
|
|
|
|
a++;
|
|
|
|
b++;
|
|
|
|
}
|
|
|
|
if(!(*b))
|
|
|
|
return haystack;
|
2009-06-13 16:54:04 +00:00
|
|
|
haystack++;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-06-13 16:54:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *str_find(const char *haystack, const char *needle)
|
|
|
|
{
|
|
|
|
while(*haystack) /* native implementation */
|
|
|
|
{
|
|
|
|
const char *a = haystack;
|
|
|
|
const char *b = needle;
|
|
|
|
while(*a && *b && *a == *b)
|
|
|
|
{
|
|
|
|
a++;
|
|
|
|
b++;
|
|
|
|
}
|
|
|
|
if(!(*b))
|
|
|
|
return haystack;
|
2008-03-29 17:20:21 +00:00
|
|
|
haystack++;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-03-29 17:20:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-05 14:50:43 +00:00
|
|
|
void str_hex(char *dst, int dst_size, const void *data, int data_size)
|
|
|
|
{
|
|
|
|
static const char hex[] = "0123456789ABCDEF";
|
|
|
|
int b;
|
|
|
|
|
|
|
|
for(b = 0; b < data_size && b < dst_size/4-4; b++)
|
|
|
|
{
|
|
|
|
dst[b*3] = hex[((const unsigned char *)data)[b]>>4];
|
|
|
|
dst[b*3+1] = hex[((const unsigned char *)data)[b]&0xf];
|
|
|
|
dst[b*3+2] = ' ';
|
|
|
|
dst[b*3+3] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-27 12:57:56 +00:00
|
|
|
void str_timestamp_ex(time_t time_data, char *buffer, int buffer_size, const char *format)
|
2010-12-07 23:26:55 +00:00
|
|
|
{
|
|
|
|
struct tm *time_info;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-12-07 23:26:55 +00:00
|
|
|
time_info = localtime(&time_data);
|
2015-08-27 12:57:56 +00:00
|
|
|
strftime(buffer, buffer_size, format, time_info);
|
2010-12-07 23:26:55 +00:00
|
|
|
buffer[buffer_size-1] = 0; /* assure null termination */
|
|
|
|
}
|
|
|
|
|
2015-08-27 12:57:56 +00:00
|
|
|
void str_timestamp(char *buffer, int buffer_size)
|
|
|
|
{
|
|
|
|
time_t time_data;
|
|
|
|
time(&time_data);
|
|
|
|
str_timestamp_ex(time_data, buffer, buffer_size, "%Y-%m-%d_%H-%M-%S");
|
|
|
|
}
|
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
int mem_comp(const void *a, const void *b, int size)
|
|
|
|
{
|
|
|
|
return memcmp(a,b,size);
|
|
|
|
}
|
|
|
|
|
2008-10-02 12:29:19 +00:00
|
|
|
const MEMSTATS *mem_stats()
|
|
|
|
{
|
|
|
|
return &memory_stats;
|
|
|
|
}
|
|
|
|
|
2008-04-05 14:50:43 +00:00
|
|
|
void net_stats(NETSTATS *stats_inout)
|
|
|
|
{
|
|
|
|
*stats_inout = network_stats;
|
|
|
|
}
|
|
|
|
|
2008-09-30 15:52:15 +00:00
|
|
|
void gui_messagebox(const char *title, const char *message)
|
|
|
|
{
|
|
|
|
#if defined(CONF_PLATFORM_MACOSX)
|
|
|
|
DialogRef theItem;
|
|
|
|
DialogItemIndex itemIndex;
|
|
|
|
|
|
|
|
/* FIXME: really needed? can we rely on glfw? */
|
|
|
|
/* HACK - get events without a bundle */
|
|
|
|
ProcessSerialNumber psn;
|
|
|
|
GetCurrentProcess(&psn);
|
|
|
|
TransformProcessType(&psn,kProcessTransformToForegroundApplication);
|
|
|
|
SetFrontProcess(&psn);
|
|
|
|
/* END HACK */
|
|
|
|
|
|
|
|
CreateStandardAlert(kAlertStopAlert,
|
2008-10-02 14:37:48 +00:00
|
|
|
CFStringCreateWithCString(NULL, title, kCFStringEncodingASCII),
|
|
|
|
CFStringCreateWithCString(NULL, message, kCFStringEncodingASCII),
|
|
|
|
NULL,
|
|
|
|
&theItem);
|
2008-09-30 15:52:15 +00:00
|
|
|
|
|
|
|
RunStandardAlert(theItem, NULL, &itemIndex);
|
|
|
|
#elif defined(CONF_FAMILY_UNIX)
|
|
|
|
static char cmd[1024];
|
2011-02-13 12:06:19 +00:00
|
|
|
int err;
|
2008-09-30 15:52:15 +00:00
|
|
|
/* use xmessage which is available on nearly every X11 system */
|
2008-09-30 17:10:19 +00:00
|
|
|
snprintf(cmd, sizeof(cmd), "xmessage -center -title '%s' '%s'",
|
2008-09-30 15:52:15 +00:00
|
|
|
title,
|
|
|
|
message);
|
|
|
|
|
2011-02-13 12:06:19 +00:00
|
|
|
err = system(cmd);
|
|
|
|
dbg_msg("gui/msgbox", "result = %i", err);
|
2008-09-30 15:52:15 +00:00
|
|
|
#elif defined(CONF_FAMILY_WINDOWS)
|
|
|
|
MessageBox(NULL,
|
|
|
|
message,
|
|
|
|
title,
|
|
|
|
MB_ICONEXCLAMATION | MB_OK);
|
|
|
|
#else
|
|
|
|
/* this is not critical */
|
|
|
|
#warning not implemented
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-11-08 08:27:11 +00:00
|
|
|
int str_isspace(char c) { return c == ' ' || c == '\n' || c == '\t'; }
|
2008-09-30 15:52:15 +00:00
|
|
|
|
2009-06-15 08:15:53 +00:00
|
|
|
char str_uppercase(char c)
|
|
|
|
{
|
|
|
|
if(c >= 'a' && c <= 'z')
|
|
|
|
return 'A' + (c-'a');
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int str_toint(const char *str) { return atoi(str); }
|
2015-08-20 10:51:30 +00:00
|
|
|
int str_toint_base(const char *str, int base) { return strtol(str, NULL, base); }
|
2010-05-29 07:25:38 +00:00
|
|
|
float str_tofloat(const char *str) { return atof(str); }
|
|
|
|
|
2008-09-30 15:52:15 +00:00
|
|
|
|
2014-11-24 15:22:00 +00:00
|
|
|
int str_utf8_comp_names(const char *a, const char *b)
|
|
|
|
{
|
|
|
|
int codeA;
|
|
|
|
int codeB;
|
|
|
|
int diff;
|
|
|
|
|
|
|
|
while(*a && *b)
|
|
|
|
{
|
2014-11-26 01:05:40 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
codeA = str_utf8_decode(&a);
|
|
|
|
}
|
|
|
|
while(*a && !str_utf8_isspace(codeA));
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
codeB = str_utf8_decode(&b);
|
|
|
|
}
|
|
|
|
while(*b && !str_utf8_isspace(codeB));
|
|
|
|
|
2014-11-24 15:22:00 +00:00
|
|
|
diff = codeA - codeB;
|
|
|
|
|
|
|
|
if((diff < 0 && !str_utf8_is_confusable(codeA, codeB))
|
|
|
|
|| (diff > 0 && !str_utf8_is_confusable(codeB, codeA)))
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *a - *b;
|
|
|
|
}
|
|
|
|
|
2014-11-08 19:14:12 +00:00
|
|
|
int str_utf8_isspace(int code)
|
|
|
|
{
|
|
|
|
return code > 0x20 && code != 0xA0 && code != 0x034F && code != 0x2800 &&
|
|
|
|
(code < 0x2000 || code > 0x200F) && (code < 0x2028 || code > 0x202F) &&
|
|
|
|
(code < 0x205F || code > 0x2064) && (code < 0x206A || code > 0x206F) &&
|
|
|
|
(code < 0xFE00 || code > 0xFE0F) && code != 0xFEFF &&
|
|
|
|
(code < 0xFFF9 || code > 0xFFFC);
|
|
|
|
}
|
|
|
|
|
2013-04-26 15:10:05 +00:00
|
|
|
const char *str_utf8_skip_whitespaces(const char *str)
|
2013-04-01 18:30:58 +00:00
|
|
|
{
|
2013-04-26 15:10:05 +00:00
|
|
|
const char *str_old;
|
2013-04-01 18:30:58 +00:00
|
|
|
int code;
|
|
|
|
|
|
|
|
while(*str)
|
|
|
|
{
|
|
|
|
str_old = str;
|
|
|
|
code = str_utf8_decode(&str);
|
|
|
|
|
|
|
|
// check if unicode is not empty
|
2014-11-08 19:14:12 +00:00
|
|
|
if(str_utf8_isspace(code))
|
2013-04-01 18:30:58 +00:00
|
|
|
{
|
|
|
|
return str_old;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
2009-06-13 08:22:37 +00:00
|
|
|
|
2015-07-11 17:26:57 +00:00
|
|
|
int str_utf8_isstart(char c)
|
2009-06-13 08:22:37 +00:00
|
|
|
{
|
2011-04-13 18:37:12 +00:00
|
|
|
if((c&0xC0) == 0x80) /* 10xxxxxx */
|
2009-06-13 08:22:37 +00:00
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int str_utf8_rewind(const char *str, int cursor)
|
|
|
|
{
|
|
|
|
while(cursor)
|
|
|
|
{
|
|
|
|
cursor--;
|
|
|
|
if(str_utf8_isstart(*(str + cursor)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
int str_utf8_forward(const char *str, int cursor)
|
|
|
|
{
|
|
|
|
const char *buf = str + cursor;
|
|
|
|
if(!buf[0])
|
|
|
|
return cursor;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-06-13 08:22:37 +00:00
|
|
|
if((*buf&0x80) == 0x0) /* 0xxxxxxx */
|
|
|
|
return cursor+1;
|
|
|
|
else if((*buf&0xE0) == 0xC0) /* 110xxxxx */
|
|
|
|
{
|
|
|
|
if(!buf[1]) return cursor+1;
|
|
|
|
return cursor+2;
|
|
|
|
}
|
|
|
|
else if((*buf & 0xF0) == 0xE0) /* 1110xxxx */
|
|
|
|
{
|
|
|
|
if(!buf[1]) return cursor+1;
|
|
|
|
if(!buf[2]) return cursor+2;
|
2010-10-29 21:08:23 +00:00
|
|
|
return cursor+3;
|
2009-06-13 08:22:37 +00:00
|
|
|
}
|
|
|
|
else if((*buf & 0xF8) == 0xF0) /* 11110xxx */
|
|
|
|
{
|
|
|
|
if(!buf[1]) return cursor+1;
|
|
|
|
if(!buf[2]) return cursor+2;
|
|
|
|
if(!buf[3]) return cursor+3;
|
2010-10-29 21:08:23 +00:00
|
|
|
return cursor+4;
|
2009-06-13 08:22:37 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-06-13 08:22:37 +00:00
|
|
|
/* invalid */
|
|
|
|
return cursor+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int str_utf8_encode(char *ptr, int chr)
|
|
|
|
{
|
|
|
|
/* encode */
|
|
|
|
if(chr <= 0x7F)
|
|
|
|
{
|
|
|
|
ptr[0] = (char)chr;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if(chr <= 0x7FF)
|
|
|
|
{
|
|
|
|
ptr[0] = 0xC0|((chr>>6)&0x1F);
|
|
|
|
ptr[1] = 0x80|(chr&0x3F);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else if(chr <= 0xFFFF)
|
|
|
|
{
|
|
|
|
ptr[0] = 0xE0|((chr>>12)&0x0F);
|
2010-10-29 21:08:23 +00:00
|
|
|
ptr[1] = 0x80|((chr>>6)&0x3F);
|
|
|
|
ptr[2] = 0x80|(chr&0x3F);
|
2009-06-13 08:22:37 +00:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
else if(chr <= 0x10FFFF)
|
|
|
|
{
|
|
|
|
ptr[0] = 0xF0|((chr>>18)&0x07);
|
2010-10-29 21:08:23 +00:00
|
|
|
ptr[1] = 0x80|((chr>>12)&0x3F);
|
|
|
|
ptr[2] = 0x80|((chr>>6)&0x3F);
|
|
|
|
ptr[3] = 0x80|(chr&0x3F);
|
2009-06-13 08:22:37 +00:00
|
|
|
return 4;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-06-13 08:22:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-30 19:09:43 +00:00
|
|
|
static unsigned char str_byte_next(const char **ptr)
|
2009-06-13 08:22:37 +00:00
|
|
|
{
|
2015-06-30 19:09:43 +00:00
|
|
|
unsigned char byte = **ptr;
|
|
|
|
(*ptr)++;
|
|
|
|
return byte;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2015-06-30 19:09:43 +00:00
|
|
|
static void str_byte_rewind(const char **ptr)
|
|
|
|
{
|
|
|
|
(*ptr)--;
|
|
|
|
}
|
|
|
|
|
|
|
|
int str_utf8_decode(const char **ptr)
|
|
|
|
{
|
|
|
|
// As per https://encoding.spec.whatwg.org/#utf-8-decoder.
|
|
|
|
unsigned char utf8_lower_boundary = 0x80;
|
|
|
|
unsigned char utf8_upper_boundary = 0xBF;
|
|
|
|
int utf8_code_point = 0;
|
|
|
|
int utf8_bytes_seen = 0;
|
|
|
|
int utf8_bytes_needed = 0;
|
|
|
|
while(1)
|
2009-06-13 08:22:37 +00:00
|
|
|
{
|
2015-06-30 19:09:43 +00:00
|
|
|
unsigned char byte = str_byte_next(ptr);
|
|
|
|
if(utf8_bytes_needed == 0)
|
2009-06-13 08:22:37 +00:00
|
|
|
{
|
2015-08-26 02:02:52 +00:00
|
|
|
if(byte <= 0x7F)
|
2015-06-30 19:09:43 +00:00
|
|
|
{
|
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
else if(0x80 <= byte && byte <= 0xDF)
|
|
|
|
{
|
|
|
|
utf8_bytes_needed = 1;
|
|
|
|
utf8_code_point = byte - 0xC0;
|
|
|
|
}
|
|
|
|
else if(0xE0 <= byte && byte <= 0xEF)
|
|
|
|
{
|
|
|
|
if(byte == 0xE0) utf8_lower_boundary = 0xA0;
|
|
|
|
if(byte == 0xED) utf8_upper_boundary = 0x9F;
|
|
|
|
utf8_bytes_needed = 2;
|
|
|
|
utf8_code_point = byte - 0xE0;
|
|
|
|
}
|
|
|
|
else if(0xF0 <= byte && byte <= 0xF4)
|
|
|
|
{
|
|
|
|
if(byte == 0xF0) utf8_lower_boundary = 0x90;
|
|
|
|
if(byte == 0xF4) utf8_upper_boundary = 0x8F;
|
|
|
|
utf8_bytes_needed = 3;
|
|
|
|
utf8_code_point = byte - 0xF0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -1; // Error.
|
|
|
|
}
|
|
|
|
utf8_code_point = utf8_code_point << (6 * utf8_bytes_needed);
|
|
|
|
continue;
|
2009-06-13 08:22:37 +00:00
|
|
|
}
|
2015-06-30 19:09:43 +00:00
|
|
|
if(!(utf8_lower_boundary <= byte && byte <= utf8_upper_boundary))
|
2009-06-13 08:22:37 +00:00
|
|
|
{
|
2015-06-30 19:09:43 +00:00
|
|
|
// Resetting variables not necessary, will be done when
|
|
|
|
// the function is called again.
|
|
|
|
str_byte_rewind(ptr);
|
|
|
|
return -1;
|
2009-06-13 08:22:37 +00:00
|
|
|
}
|
2015-06-30 19:09:43 +00:00
|
|
|
utf8_lower_boundary = 0x80;
|
|
|
|
utf8_upper_boundary = 0xBF;
|
|
|
|
utf8_bytes_seen += 1;
|
|
|
|
utf8_code_point = utf8_code_point + ((byte - 0x80) << (6 * (utf8_bytes_needed - utf8_bytes_seen)));
|
|
|
|
if(utf8_bytes_seen != utf8_bytes_needed)
|
2009-06-13 08:22:37 +00:00
|
|
|
{
|
2015-06-30 19:09:43 +00:00
|
|
|
continue;
|
2009-06-13 08:22:37 +00:00
|
|
|
}
|
2015-06-30 19:09:43 +00:00
|
|
|
// Resetting variables not necessary, see above.
|
|
|
|
return utf8_code_point;
|
|
|
|
}
|
2009-06-13 08:22:37 +00:00
|
|
|
}
|
|
|
|
|
2010-09-30 22:55:16 +00:00
|
|
|
int str_utf8_check(const char *str)
|
|
|
|
{
|
|
|
|
while(*str)
|
|
|
|
{
|
|
|
|
if((*str&0x80) == 0x0)
|
2011-04-13 18:37:12 +00:00
|
|
|
str++;
|
2010-09-30 22:55:16 +00:00
|
|
|
else if((*str&0xE0) == 0xC0 && (*(str+1)&0xC0) == 0x80)
|
|
|
|
str += 2;
|
|
|
|
else if((*str&0xF0) == 0xE0 && (*(str+1)&0xC0) == 0x80 && (*(str+2)&0xC0) == 0x80)
|
|
|
|
str += 3;
|
|
|
|
else if((*str&0xF8) == 0xF0 && (*(str+1)&0xC0) == 0x80 && (*(str+2)&0xC0) == 0x80 && (*(str+3)&0xC0) == 0x80)
|
|
|
|
str += 4;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-06-15 13:01:04 +00:00
|
|
|
|
|
|
|
unsigned str_quickhash(const char *str)
|
|
|
|
{
|
|
|
|
unsigned hash = 5381;
|
|
|
|
for(; *str; str++)
|
|
|
|
hash = ((hash << 5) + hash) + (*str); /* hash * 33 + c */
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2014-08-22 12:18:16 +00:00
|
|
|
int pid()
|
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
return _getpid();
|
|
|
|
#else
|
|
|
|
return getpid();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-03-13 14:13:19 +00:00
|
|
|
void shell_execute(const char *file)
|
2014-12-31 14:07:44 +00:00
|
|
|
{
|
2015-04-18 12:53:11 +00:00
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
ShellExecute(NULL, NULL, file, NULL, NULL, SW_SHOWDEFAULT);
|
|
|
|
#elif defined(CONF_FAMILY_UNIX)
|
|
|
|
char* argv[2];
|
|
|
|
argv[0] = (char*) file;
|
|
|
|
argv[1] = NULL;
|
|
|
|
pid_t pid = fork();
|
|
|
|
if(!pid)
|
|
|
|
execv(file, argv);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int os_compare_version(int major, int minor)
|
|
|
|
{
|
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
OSVERSIONINFO ver;
|
|
|
|
mem_zero(&ver, sizeof(OSVERSIONINFO));
|
|
|
|
ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
|
|
|
|
GetVersionEx(&ver);
|
2015-04-18 14:23:43 +00:00
|
|
|
if(ver.dwMajorVersion > major || (ver.dwMajorVersion == major && ver.dwMinorVersion > minor))
|
2015-04-18 12:53:11 +00:00
|
|
|
return 1;
|
|
|
|
else if(ver.dwMajorVersion == major && ver.dwMinorVersion == minor)
|
|
|
|
return 0;
|
2015-04-18 14:23:43 +00:00
|
|
|
else
|
2015-04-18 12:53:11 +00:00
|
|
|
return -1;
|
2015-04-18 19:33:51 +00:00
|
|
|
#else
|
|
|
|
return 0; // unimplemented
|
2015-04-18 12:53:11 +00:00
|
|
|
#endif
|
2014-12-31 14:07:44 +00:00
|
|
|
}
|
2009-06-15 13:01:04 +00:00
|
|
|
|
2015-03-05 23:53:59 +00:00
|
|
|
struct SECURE_RANDOM_DATA
|
|
|
|
{
|
|
|
|
int initialized;
|
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
HCRYPTPROV provider;
|
|
|
|
#else
|
|
|
|
IOHANDLE urandom;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct SECURE_RANDOM_DATA secure_random_data = { 0 };
|
|
|
|
|
|
|
|
int secure_random_init()
|
|
|
|
{
|
|
|
|
if(secure_random_data.initialized)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
2015-03-15 10:59:17 +00:00
|
|
|
if(CryptAcquireContext(&secure_random_data.provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
|
2015-03-05 23:53:59 +00:00
|
|
|
{
|
|
|
|
secure_random_data.initialized = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
secure_random_data.urandom = io_open("/dev/urandom", IOFLAG_READ);
|
|
|
|
if(secure_random_data.urandom)
|
|
|
|
{
|
|
|
|
secure_random_data.initialized = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-06-21 17:46:55 +00:00
|
|
|
void secure_random_fill(void *bytes, size_t length)
|
2015-03-05 23:53:59 +00:00
|
|
|
{
|
|
|
|
if(!secure_random_data.initialized)
|
|
|
|
{
|
|
|
|
dbg_msg("secure", "called secure_random_fill before secure_random_init");
|
|
|
|
dbg_break();
|
|
|
|
}
|
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
if(!CryptGenRandom(secure_random_data.provider, length, bytes))
|
|
|
|
{
|
|
|
|
dbg_msg("secure", "CryptGenRandom failed, last_error=%d", GetLastError());
|
|
|
|
dbg_break();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if(length != io_read(secure_random_data.urandom, bytes, length))
|
|
|
|
{
|
|
|
|
dbg_msg("secure", "io_read returned with a short read");
|
|
|
|
dbg_break();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-10-04 09:49:38 +00:00
|
|
|
#if defined(__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|