commit
7e889d449c
@ -0,0 +1,2 @@
|
||||
sergate
|
||||
.tags*
|
||||
@ -0,0 +1,34 @@
|
||||
OUT = sergate
|
||||
|
||||
#CC = clang
|
||||
CC = gcc
|
||||
CFLAGS = -Wall -O2
|
||||
|
||||
#CXX = clang++
|
||||
CXX = g++
|
||||
CXXFLAGS = -Wall -O2
|
||||
|
||||
ODIR = obj
|
||||
SDIR = src
|
||||
LIBS = -pthread -lm
|
||||
INC = -Iinclude
|
||||
|
||||
C_SRCS := $(wildcard $(SDIR)/*.c)
|
||||
CXX_SRCS := $(wildcard $(SDIR)/*.cpp)
|
||||
C_OBJS := $(patsubst $(SDIR)/%.c,$(ODIR)/%.o,$(C_SRCS))
|
||||
CXX_OBJS := $(patsubst $(SDIR)/%.cpp,$(ODIR)/%.o,$(CXX_SRCS))
|
||||
OBJS := $(C_OBJS) $(CXX_OBJS)
|
||||
|
||||
$(ODIR)/%.o: $(SDIR)/%.c
|
||||
$(CC) -c $(INC) $(CFLAGS) $< -o $@
|
||||
|
||||
$(ODIR)/%.o: $(SDIR)/%.cpp
|
||||
$(CXX) -c $(INC) $(CXXFLAGS) $< -o $@
|
||||
|
||||
$(OUT): $(OBJS)
|
||||
$(CXX) $(LIBS) $^ -o $(OUT)
|
||||
|
||||
.PHONY: clean
|
||||
|
||||
clean:
|
||||
rm -f $(ODIR)/*.o $(OUT)
|
||||
@ -0,0 +1,139 @@
|
||||
#ifndef ARGPARSE_H
|
||||
#define ARGPARSE_H
|
||||
|
||||
/**
|
||||
* Command-line arguments parsing library.
|
||||
*
|
||||
* This module is inspired by parse-options.c (git) and python's argparse
|
||||
* module.
|
||||
*
|
||||
* Arguments parsing is common task in cli program, but traditional `getopt`
|
||||
* libraries are not easy to use. This library provides high-level arguments
|
||||
* parsing solutions.
|
||||
*
|
||||
* The program defines what arguments it requires, and `argparse` will figure
|
||||
* out how to parse those out of `argc` and `argv`, it also automatically
|
||||
* generates help and usage messages and issues errors when users give the
|
||||
* program invalid arguments.
|
||||
*
|
||||
* Reserved namespaces:
|
||||
* argparse
|
||||
* OPT
|
||||
* Author: Yecheng Fu <cofyc.jackson@gmail.com>
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct argparse;
|
||||
struct argparse_option;
|
||||
|
||||
typedef int argparse_callback(struct argparse *this_,
|
||||
const struct argparse_option *option);
|
||||
|
||||
enum argparse_flag {
|
||||
ARGPARSE_STOP_AT_NON_OPTION = 1,
|
||||
};
|
||||
|
||||
enum argparse_option_type {
|
||||
/* special */
|
||||
ARGPARSE_OPT_END,
|
||||
/* options with no arguments */
|
||||
ARGPARSE_OPT_BOOLEAN,
|
||||
ARGPARSE_OPT_BIT,
|
||||
/* options with arguments (optional or required) */
|
||||
ARGPARSE_OPT_INTEGER,
|
||||
ARGPARSE_OPT_STRING,
|
||||
};
|
||||
|
||||
enum argparse_option_flags {
|
||||
OPT_NONEG = 1, /* Negation disabled. */
|
||||
};
|
||||
|
||||
/*
|
||||
* Argparse option struct.
|
||||
*
|
||||
* `type`:
|
||||
* holds the type of the option, you must have an ARGPARSE_OPT_END last in your
|
||||
* array.
|
||||
*
|
||||
* `short_name`:
|
||||
* the character to use as a short option name, '\0' if none.
|
||||
*
|
||||
* `long_name`:
|
||||
* the long option name, without the leading dash, NULL if none.
|
||||
*
|
||||
* `value`:
|
||||
* stores pointer to the value to be filled.
|
||||
*
|
||||
* `help`:
|
||||
* the short help message associated to what the option does.
|
||||
* Must never be NULL (except for ARGPARSE_OPT_END).
|
||||
*
|
||||
* `callback`:
|
||||
* function is called when corresponding argument is parsed.
|
||||
*
|
||||
* `data`:
|
||||
* associated data. Callbacks can use it like they want.
|
||||
*
|
||||
* `flags`:
|
||||
* option flags.
|
||||
*
|
||||
*/
|
||||
struct argparse_option {
|
||||
enum argparse_option_type type;
|
||||
const char short_name;
|
||||
const char *long_name;
|
||||
void *value;
|
||||
const char *help;
|
||||
argparse_callback *callback;
|
||||
intptr_t data;
|
||||
int flags;
|
||||
};
|
||||
|
||||
/*
|
||||
* argpparse
|
||||
*/
|
||||
struct argparse {
|
||||
// user supplied
|
||||
const struct argparse_option *options;
|
||||
const char *usage;
|
||||
int flags;
|
||||
// internal context
|
||||
int argc;
|
||||
const char **argv;
|
||||
const char **out;
|
||||
int cpidx;
|
||||
const char *optvalue; // current option value
|
||||
};
|
||||
|
||||
// builtin callbacks
|
||||
int argparse_help_cb(struct argparse *this_,
|
||||
const struct argparse_option *option);
|
||||
|
||||
// builtin option macros
|
||||
#define OPT_END() { ARGPARSE_OPT_END, 0 }
|
||||
#define OPT_BOOLEAN(...) { ARGPARSE_OPT_BOOLEAN, __VA_ARGS__ }
|
||||
#define OPT_BIT(...) { ARGPARSE_OPT_BIT, __VA_ARGS__ }
|
||||
#define OPT_INTEGER(...) { ARGPARSE_OPT_INTEGER, __VA_ARGS__ }
|
||||
#define OPT_STRING(...) { ARGPARSE_OPT_STRING, __VA_ARGS__ }
|
||||
#define OPT_HELP() OPT_BOOLEAN('h', "help", 0, "Show this help message and exit", argparse_help_cb)
|
||||
|
||||
int argparse_init(struct argparse *this_, struct argparse_option *options,
|
||||
const char *usage, int flags);
|
||||
int argparse_parse(struct argparse *this_, int argc, const char **argv);
|
||||
void argparse_usage(struct argparse *this_);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@ -0,0 +1,149 @@
|
||||
/* (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. */
|
||||
#ifndef BASE_DETECT_H
|
||||
#define BASE_DETECT_H
|
||||
|
||||
/*
|
||||
this file detected the family, platform and architecture
|
||||
to compile for.
|
||||
*/
|
||||
|
||||
/* platforms */
|
||||
|
||||
/* windows Family */
|
||||
#if defined(WIN64) || defined(_WIN64)
|
||||
/* Hmm, is this IA64 or x86-64? */
|
||||
#define CONF_FAMILY_WINDOWS 1
|
||||
#define CONF_FAMILY_STRING "windows"
|
||||
#define CONF_PLATFORM_WIN64 1
|
||||
#define CONF_PLATFORM_STRING "win64"
|
||||
#elif defined(WIN32) || defined(_WIN32) || defined(__CYGWIN32__) || defined(__MINGW32__)
|
||||
#define CONF_FAMILY_WINDOWS 1
|
||||
#define CONF_FAMILY_STRING "windows"
|
||||
#define CONF_PLATFORM_WIN32 1
|
||||
#define CONF_PLATFORM_STRING "win32"
|
||||
#endif
|
||||
|
||||
/* unix family */
|
||||
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
||||
#define CONF_FAMILY_UNIX 1
|
||||
#define CONF_FAMILY_STRING "unix"
|
||||
#define CONF_PLATFORM_FREEBSD 1
|
||||
#define CONF_PLATFORM_STRING "freebsd"
|
||||
#endif
|
||||
|
||||
#if defined(__OpenBSD__)
|
||||
#define CONF_FAMILY_UNIX 1
|
||||
#define CONF_FAMILY_STRING "unix"
|
||||
#define CONF_PLATFORM_OPENBSD 1
|
||||
#define CONF_PLATFORM_STRING "openbsd"
|
||||
#endif
|
||||
|
||||
#if defined(__LINUX__) || defined(__linux__)
|
||||
#define CONF_FAMILY_UNIX 1
|
||||
#define CONF_FAMILY_STRING "unix"
|
||||
#define CONF_PLATFORM_LINUX 1
|
||||
#define CONF_PLATFORM_STRING "linux"
|
||||
#endif
|
||||
|
||||
#if defined(__GNU__) || defined(__gnu__)
|
||||
#define CONF_FAMILY_UNIX 1
|
||||
#define CONF_FAMILY_STRING "unix"
|
||||
#define CONF_PLATFORM_HURD 1
|
||||
#define CONF_PLATFORM_STRING "gnu"
|
||||
#endif
|
||||
|
||||
#if defined(MACOSX) || defined(__APPLE__) || defined(__DARWIN__)
|
||||
#define CONF_FAMILY_UNIX 1
|
||||
#define CONF_FAMILY_STRING "unix"
|
||||
#define CONF_PLATFORM_MACOSX 1
|
||||
#define CONF_PLATFORM_STRING "macosx"
|
||||
#endif
|
||||
|
||||
#if defined(__sun)
|
||||
#define CONF_FAMILY_UNIX 1
|
||||
#define CONF_FAMILY_STRING "unix"
|
||||
#define CONF_PLATFORM_SOLARIS 1
|
||||
#define CONF_PLATFORM_STRING "solaris"
|
||||
#endif
|
||||
|
||||
/* beos family */
|
||||
#if defined(__BeOS) || defined(__BEOS__)
|
||||
#define CONF_FAMILY_BEOS 1
|
||||
#define CONF_FAMILY_STRING "beos"
|
||||
#define CONF_PLATFORM_BEOS 1
|
||||
#define CONF_PLATFORM_STRING "beos"
|
||||
#endif
|
||||
|
||||
|
||||
/* use gcc endianness definitions when available */
|
||||
#if defined(__GNUC__) && !defined(__APPLE__) && !defined(__MINGW32__) && !defined(__sun)
|
||||
#if defined(__FreeBSD__) || defined(__OpenBSD__)
|
||||
#include <sys/endian.h>
|
||||
#else
|
||||
#include <endian.h>
|
||||
#endif
|
||||
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
#define CONF_ARCH_ENDIAN_LITTLE 1
|
||||
#elif __BYTE_ORDER == __BIG_ENDIAN
|
||||
#define CONF_ARCH_ENDIAN_BIG 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
/* architectures */
|
||||
#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(CONF_PLATFORM_WIN32)
|
||||
#define CONF_ARCH_IA32 1
|
||||
#define CONF_ARCH_STRING "ia32"
|
||||
#if !defined(CONF_ARCH_ENDIAN_LITTLE) && !defined(CONF_ARCH_ENDIAN_BIG)
|
||||
#define CONF_ARCH_ENDIAN_LITTLE 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(__ia64__) || defined(_M_IA64)
|
||||
#define CONF_ARCH_IA64 1
|
||||
#define CONF_ARCH_STRING "ia64"
|
||||
#if !defined(CONF_ARCH_ENDIAN_LITTLE) && !defined(CONF_ARCH_ENDIAN_BIG)
|
||||
#define CONF_ARCH_ENDIAN_LITTLE 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(__amd64__) || defined(__x86_64__) || defined(_M_X64)
|
||||
#define CONF_ARCH_AMD64 1
|
||||
#define CONF_ARCH_STRING "amd64"
|
||||
#if !defined(CONF_ARCH_ENDIAN_LITTLE) && !defined(CONF_ARCH_ENDIAN_BIG)
|
||||
#define CONF_ARCH_ENDIAN_LITTLE 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(__powerpc__) || defined(__ppc__)
|
||||
#define CONF_ARCH_PPC 1
|
||||
#define CONF_ARCH_STRING "ppc"
|
||||
#if !defined(CONF_ARCH_ENDIAN_LITTLE) && !defined(CONF_ARCH_ENDIAN_BIG)
|
||||
#define CONF_ARCH_ENDIAN_BIG 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(__sparc__)
|
||||
#define CONF_ARCH_SPARC 1
|
||||
#define CONF_ARCH_STRING "sparc"
|
||||
#if !defined(CONF_ARCH_ENDIAN_LITTLE) && !defined(CONF_ARCH_ENDIAN_BIG)
|
||||
#define CONF_ARCH_ENDIAN_BIG 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef CONF_FAMILY_STRING
|
||||
#define CONF_FAMILY_STRING "unknown"
|
||||
#endif
|
||||
|
||||
#ifndef CONF_PLATFORM_STRING
|
||||
#define CONF_PLATFORM_STRING "unknown"
|
||||
#endif
|
||||
|
||||
#ifndef CONF_ARCH_STRING
|
||||
#define CONF_ARCH_STRING "unknown"
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@ -0,0 +1,269 @@
|
||||
|
||||
/* vim: set et ts=3 sw=3 sts=3 ft=c:
|
||||
*
|
||||
* Copyright (C) 2012, 2013, 2014 James McLaughlin et al. All rights reserved.
|
||||
* https://github.com/udp/json-parser
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef _JSON_H
|
||||
#define _JSON_H
|
||||
|
||||
#ifndef json_char
|
||||
#define json_char char
|
||||
#endif
|
||||
|
||||
#ifndef json_int_t
|
||||
#ifndef _MSC_VER
|
||||
#include <inttypes.h>
|
||||
#define json_int_t int64_t
|
||||
#else
|
||||
#define json_int_t __int64
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
#include <string.h>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
#endif
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned long max_memory;
|
||||
int settings;
|
||||
|
||||
/* Custom allocator support (leave null to use malloc/free)
|
||||
*/
|
||||
|
||||
void * (* mem_alloc) (size_t, int zero, void * user_data);
|
||||
void (* mem_free) (void *, void * user_data);
|
||||
|
||||
void * user_data; /* will be passed to mem_alloc and mem_free */
|
||||
|
||||
} json_settings;
|
||||
|
||||
#define json_enable_comments 0x01
|
||||
|
||||
typedef enum
|
||||
{
|
||||
json_none,
|
||||
json_object,
|
||||
json_array,
|
||||
json_integer,
|
||||
json_double,
|
||||
json_string,
|
||||
json_boolean,
|
||||
json_null
|
||||
|
||||
} json_type;
|
||||
|
||||
extern const struct _json_value json_value_none;
|
||||
|
||||
typedef struct _json_value
|
||||
{
|
||||
struct _json_value * parent;
|
||||
|
||||
json_type type;
|
||||
|
||||
union
|
||||
{
|
||||
int boolean;
|
||||
json_int_t integer;
|
||||
double dbl;
|
||||
|
||||
struct
|
||||
{
|
||||
unsigned int length;
|
||||
json_char * ptr; /* null terminated */
|
||||
|
||||
} string;
|
||||
|
||||
struct
|
||||
{
|
||||
unsigned int length;
|
||||
|
||||
struct
|
||||
{
|
||||
json_char * name;
|
||||
unsigned int name_length;
|
||||
|
||||
struct _json_value * value;
|
||||
|
||||
} * values;
|
||||
|
||||
#if defined(__cplusplus) && __cplusplus >= 201103L
|
||||
decltype(values) begin () const
|
||||
{ return values;
|
||||
}
|
||||
decltype(values) end () const
|
||||
{ return values + length;
|
||||
}
|
||||
#endif
|
||||
|
||||
} object;
|
||||
|
||||
struct
|
||||
{
|
||||
unsigned int length;
|
||||
struct _json_value ** values;
|
||||
|
||||
#if defined(__cplusplus) && __cplusplus >= 201103L
|
||||
decltype(values) begin () const
|
||||
{ return values;
|
||||
}
|
||||
decltype(values) end () const
|
||||
{ return values + length;
|
||||
}
|
||||
#endif
|
||||
|
||||
} array;
|
||||
|
||||
} u;
|
||||
|
||||
union
|
||||
{
|
||||
struct _json_value * next_alloc;
|
||||
void * object_mem;
|
||||
|
||||
} _reserved;
|
||||
|
||||
|
||||
/* Some C++ operator sugar */
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
public:
|
||||
|
||||
inline _json_value ()
|
||||
{ memset (this, 0, sizeof (_json_value));
|
||||
}
|
||||
|
||||
inline const struct _json_value &operator [] (int index) const
|
||||
{
|
||||
if (type != json_array || index < 0
|
||||
|| ((unsigned int) index) >= u.array.length)
|
||||
{
|
||||
return json_value_none;
|
||||
}
|
||||
|
||||
return *u.array.values [index];
|
||||
}
|
||||
|
||||
inline const struct _json_value &operator [] (const char * index) const
|
||||
{
|
||||
if (type != json_object)
|
||||
return json_value_none;
|
||||
|
||||
for (unsigned int i = 0; i < u.object.length; ++ i)
|
||||
if (!strcmp (u.object.values [i].name, index))
|
||||
return *u.object.values [i].value;
|
||||
|
||||
return json_value_none;
|
||||
}
|
||||
|
||||
inline operator const char * () const
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case json_string:
|
||||
return u.string.ptr;
|
||||
|
||||
default:
|
||||
return "";
|
||||
};
|
||||
}
|
||||
|
||||
inline operator json_int_t () const
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case json_integer:
|
||||
return u.integer;
|
||||
|
||||
case json_double:
|
||||
return (json_int_t) u.dbl;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
};
|
||||
}
|
||||
|
||||
inline operator bool () const
|
||||
{
|
||||
if (type != json_boolean)
|
||||
return false;
|
||||
|
||||
return u.boolean != 0;
|
||||
}
|
||||
|
||||
inline operator double () const
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case json_integer:
|
||||
return (double) u.integer;
|
||||
|
||||
case json_double:
|
||||
return u.dbl;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} json_value;
|
||||
|
||||
json_value * json_parse (const json_char * json,
|
||||
size_t length);
|
||||
|
||||
#define json_error_max 128
|
||||
json_value * json_parse_ex (json_settings * settings,
|
||||
const json_char * json,
|
||||
size_t length,
|
||||
char * error);
|
||||
|
||||
void json_value_free (json_value *);
|
||||
|
||||
|
||||
/* Not usually necessary, unless you used a custom mem_alloc and now want to
|
||||
* use a custom mem_free.
|
||||
*/
|
||||
void json_value_free_ex (json_settings * settings,
|
||||
json_value *);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1 @@
|
||||
*.o
|
||||
@ -0,0 +1,322 @@
|
||||
#include "argparse.h"
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define OPT_UNSET 1
|
||||
|
||||
static const char *
|
||||
prefix_skip(const char *str, const char *prefix)
|
||||
{
|
||||
size_t len = strlen(prefix);
|
||||
return strncmp(str, prefix, len) ? NULL : str + len;
|
||||
}
|
||||
|
||||
int
|
||||
prefix_cmp(const char *str, const char *prefix)
|
||||
{
|
||||
for (;; str++, prefix++)
|
||||
if (!*prefix)
|
||||
return 0;
|
||||
else if (*str != *prefix)
|
||||
return (unsigned char)*prefix - (unsigned char)*str;
|
||||
}
|
||||
|
||||
static void
|
||||
argparse_error(struct argparse *this_, const struct argparse_option *opt,
|
||||
const char *reason)
|
||||
{
|
||||
if (!strncmp(this_->argv[0], "--", 2)) {
|
||||
fprintf(stderr, "error: option `%s` %s\n", opt->long_name, reason);
|
||||
exit(-1);
|
||||
} else {
|
||||
fprintf(stderr, "error: option `%c` %s\n", opt->short_name, reason);
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
argparse_getvalue(struct argparse *this_, const struct argparse_option *opt,
|
||||
int flags)
|
||||
{
|
||||
const char *s = NULL;
|
||||
if (!opt->value)
|
||||
goto skipped;
|
||||
switch (opt->type) {
|
||||
case ARGPARSE_OPT_BOOLEAN:
|
||||
if (flags & OPT_UNSET) {
|
||||
*(int *)opt->value = *(int *)opt->value - 1;
|
||||
} else {
|
||||
*(int *)opt->value = *(int *)opt->value + 1;
|
||||
}
|
||||
if (*(int *)opt->value < 0) {
|
||||
*(int *)opt->value = 0;
|
||||
}
|
||||
break;
|
||||
case ARGPARSE_OPT_BIT:
|
||||
if (flags & OPT_UNSET) {
|
||||
*(int *)opt->value &= ~opt->data;
|
||||
} else {
|
||||
*(int *)opt->value |= opt->data;
|
||||
}
|
||||
break;
|
||||
case ARGPARSE_OPT_STRING:
|
||||
if (this_->optvalue) {
|
||||
*(const char **)opt->value = this_->optvalue;
|
||||
this_->optvalue = NULL;
|
||||
} else if (this_->argc > 1) {
|
||||
this_->argc--;
|
||||
*(const char **)opt->value = *++this_->argv;
|
||||
} else {
|
||||
argparse_error(this_, opt, "requires a value");
|
||||
}
|
||||
break;
|
||||
case ARGPARSE_OPT_INTEGER:
|
||||
if (this_->optvalue) {
|
||||
*(int *)opt->value = strtol(this_->optvalue, (char **)&s, 0);
|
||||
this_->optvalue = NULL;
|
||||
} else if (this_->argc > 1) {
|
||||
this_->argc--;
|
||||
*(int *)opt->value = strtol(*++this_->argv, (char **)&s, 0);
|
||||
} else {
|
||||
argparse_error(this_, opt, "requires a value");
|
||||
}
|
||||
if (*s)
|
||||
argparse_error(this_, opt, "expects a numerical value");
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
|
||||
skipped:
|
||||
if (opt->callback) {
|
||||
return opt->callback(this_, opt);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
argparse_options_check(const struct argparse_option *options)
|
||||
{
|
||||
for (; options->type != ARGPARSE_OPT_END; options++) {
|
||||
switch (options->type) {
|
||||
case ARGPARSE_OPT_END:
|
||||
case ARGPARSE_OPT_BOOLEAN:
|
||||
case ARGPARSE_OPT_BIT:
|
||||
case ARGPARSE_OPT_INTEGER:
|
||||
case ARGPARSE_OPT_STRING:
|
||||
continue;
|
||||
default:
|
||||
fprintf(stderr, "wrong option type: %d", options->type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
argparse_short_opt(struct argparse *this_, const struct argparse_option *options)
|
||||
{
|
||||
for (; options->type != ARGPARSE_OPT_END; options++) {
|
||||
if (options->short_name == *this_->optvalue) {
|
||||
this_->optvalue = this_->optvalue[1] ? this_->optvalue + 1 : NULL;
|
||||
return argparse_getvalue(this_, options, 0);
|
||||
}
|
||||
}
|
||||
return -2;
|
||||
}
|
||||
|
||||
static int
|
||||
argparse_long_opt(struct argparse *this_, const struct argparse_option *options)
|
||||
{
|
||||
for (; options->type != ARGPARSE_OPT_END; options++) {
|
||||
const char *rest;
|
||||
int opt_flags = 0;
|
||||
if (!options->long_name)
|
||||
continue;
|
||||
|
||||
rest = prefix_skip(this_->argv[0] + 2, options->long_name);
|
||||
if (!rest) {
|
||||
// Negation allowed?
|
||||
if (options->flags & OPT_NONEG) {
|
||||
continue;
|
||||
}
|
||||
// Only boolean/bit allow negation.
|
||||
if (options->type != ARGPARSE_OPT_BOOLEAN && options->type != ARGPARSE_OPT_BIT) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!prefix_cmp(this_->argv[0] + 2, "no-")) {
|
||||
rest = prefix_skip(this_->argv[0] + 2 + 3, options->long_name);
|
||||
if (!rest)
|
||||
continue;
|
||||
opt_flags |= OPT_UNSET;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (*rest) {
|
||||
if (*rest != '=')
|
||||
continue;
|
||||
this_->optvalue = rest + 1;
|
||||
}
|
||||
return argparse_getvalue(this_, options, opt_flags);
|
||||
}
|
||||
return -2;
|
||||
}
|
||||
|
||||
int
|
||||
argparse_init(struct argparse *this_, struct argparse_option *options,
|
||||
const char *usage, int flags)
|
||||
{
|
||||
memset(this_, 0, sizeof(*this_));
|
||||
this_->options = options;
|
||||
this_->usage = usage;
|
||||
this_->flags = flags;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
argparse_parse(struct argparse *this_, int argc, const char **argv)
|
||||
{
|
||||
this_->argc = argc - 1;
|
||||
this_->argv = argv + 1;
|
||||
this_->out = argv;
|
||||
|
||||
argparse_options_check(this_->options);
|
||||
|
||||
for (; this_->argc; this_->argc--, this_->argv++) {
|
||||
const char *arg = this_->argv[0];
|
||||
if (arg[0] != '-' || !arg[1]) {
|
||||
if (this_->flags & ARGPARSE_STOP_AT_NON_OPTION) {
|
||||
goto end;
|
||||
}
|
||||
// if it's not option or is a single char '-', copy verbatimly
|
||||
this_->out[this_->cpidx++] = this_->argv[0];
|
||||
continue;
|
||||
}
|
||||
// short option
|
||||
if (arg[1] != '-') {
|
||||
this_->optvalue = arg + 1;
|
||||
switch (argparse_short_opt(this_, this_->options)) {
|
||||
case -1:
|
||||
break;
|
||||
case -2:
|
||||
goto unknown;
|
||||
}
|
||||
while (this_->optvalue) {
|
||||
switch (argparse_short_opt(this_, this_->options)) {
|
||||
case -1:
|
||||
break;
|
||||
case -2:
|
||||
goto unknown;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// if '--' presents
|
||||
if (!arg[2]) {
|
||||
this_->argc--;
|
||||
this_->argv++;
|
||||
break;
|
||||
}
|
||||
// long option
|
||||
switch (argparse_long_opt(this_, this_->options)) {
|
||||
case -1:
|
||||
break;
|
||||
case -2:
|
||||
goto unknown;
|
||||
}
|
||||
continue;
|
||||
|
||||
unknown:
|
||||
fprintf(stderr, "error: unknown option `%s`\n", this_->argv[0]);
|
||||
argparse_usage(this_);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
end:
|
||||
memmove(this_->out + this_->cpidx, this_->argv,
|
||||
this_->argc * sizeof(*this_->out));
|
||||
this_->out[this_->cpidx + this_->argc] = NULL;
|
||||
|
||||
return this_->cpidx + this_->argc;
|
||||
}
|
||||
|
||||
void
|
||||
argparse_usage(struct argparse *this_)
|
||||
{
|
||||
fprintf(stdout, "Usage: %s\n", this_->usage);
|
||||
fputc('\n', stdout);
|
||||
|
||||
const struct argparse_option *options;
|
||||
|
||||
// figure out best width
|
||||
size_t usage_opts_width = 0;
|
||||
size_t len;
|
||||
options = this_->options;
|
||||
for (; options->type != ARGPARSE_OPT_END; options++) {
|
||||
len = 0;
|
||||
if ((options)->short_name) {
|
||||
len += 2;
|
||||
}
|
||||
if ((options)->short_name && (options)->long_name) {
|
||||
len += 2; // separator ", "
|
||||
}
|
||||
if ((options)->long_name) {
|
||||
len += strlen((options)->long_name) + 2;
|
||||
}
|
||||
if (options->type == ARGPARSE_OPT_INTEGER) {
|
||||
len += strlen("=<int>");
|
||||
} else if (options->type == ARGPARSE_OPT_STRING) {
|
||||
len += strlen("=<str>");
|
||||
}
|
||||
len = ceil((float)len / 4) * 4;
|
||||
if (usage_opts_width < len) {
|
||||
usage_opts_width = len;
|
||||
}
|
||||
}
|
||||
usage_opts_width += 4; // 4 spaces prefix
|
||||
|
||||
options = this_->options;
|
||||
for (; options->type != ARGPARSE_OPT_END; options++) {
|
||||
size_t pos;
|
||||
int pad;
|
||||
pos = fprintf(stdout, " ");
|
||||
if (options->short_name) {
|
||||
pos += fprintf(stdout, "-%c", options->short_name);
|
||||
}
|
||||
if (options->long_name && options->short_name) {
|
||||
pos += fprintf(stdout, ", ");
|
||||
}
|
||||
if (options->long_name) {
|
||||
pos += fprintf(stdout, "--%s", options->long_name);
|
||||
}
|
||||
if (options->type == ARGPARSE_OPT_INTEGER) {
|
||||
pos += fprintf(stdout, "=<int>");
|
||||
} else if (options->type == ARGPARSE_OPT_STRING) {
|
||||
pos += fprintf(stdout, "=<str>");
|
||||
}
|
||||
if (pos <= usage_opts_width) {
|
||||
pad = usage_opts_width - pos;
|
||||
} else {
|
||||
fputc('\n', stdout);
|
||||
pad = usage_opts_width;
|
||||
}
|
||||
fprintf(stdout, "%*s%s\n", pad + 2, "", options->help);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
argparse_help_cb(struct argparse *this_, const struct argparse_option *option)
|
||||
{
|
||||
(void)option;
|
||||
argparse_usage(this_);
|
||||
exit(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
@ -0,0 +1,949 @@
|
||||
/* vim: set et ts=3 sw=3 sts=3 ft=c:
|
||||
*
|
||||
* Copyright (C) 2012, 2013, 2014 James McLaughlin et al. All rights reserved.
|
||||
* https://github.com/udp/json-parser
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "json.h"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#ifndef _CRT_SECURE_NO_WARNINGS
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
const struct _json_value json_value_none; /* zero-d by ctor */
|
||||
#else
|
||||
const struct _json_value json_value_none = { 0 };
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
|
||||
typedef unsigned short json_uchar;
|
||||
|
||||
static unsigned char hex_value (json_char c)
|
||||
{
|
||||
if (isdigit(c))
|
||||
return c - '0';
|
||||
|
||||
switch (c) {
|
||||
case 'a': case 'A': return 0x0A;
|
||||
case 'b': case 'B': return 0x0B;
|
||||
case 'c': case 'C': return 0x0C;
|
||||
case 'd': case 'D': return 0x0D;
|
||||
case 'e': case 'E': return 0x0E;
|
||||
case 'f': case 'F': return 0x0F;
|
||||
default: return 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned long used_memory;
|
||||
|
||||
unsigned int uint_max;
|
||||
unsigned long ulong_max;
|
||||
|
||||
json_settings settings;
|
||||
int first_pass;
|
||||
|
||||
} json_state;
|
||||
|
||||
static void * default_alloc (size_t size, int zero, void * user_data)
|
||||
{
|
||||
return zero ? calloc (1, size) : malloc (size);
|
||||
}
|
||||
|
||||
static void default_free (void * ptr, void * user_data)
|
||||
{
|
||||
free (ptr);
|
||||
}
|
||||
|
||||
static void * json_alloc (json_state * state, unsigned long size, int zero)
|
||||
{
|
||||
if ((state->ulong_max - state->used_memory) < size)
|
||||
return 0;
|
||||
|
||||
if (state->settings.max_memory
|
||||
&& (state->used_memory += size) > state->settings.max_memory)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return state->settings.mem_alloc (size, zero, state->settings.user_data);
|
||||
}
|
||||
|
||||
static int new_value
|
||||
(json_state * state, json_value ** top, json_value ** root, json_value ** alloc, json_type type)
|
||||
{
|
||||
json_value * value;
|
||||
int values_size;
|
||||
|
||||
if (!state->first_pass)
|
||||
{
|
||||
value = *top = *alloc;
|
||||
*alloc = (*alloc)->_reserved.next_alloc;
|
||||
|
||||
if (!*root)
|
||||
*root = value;
|
||||
|
||||
switch (value->type)
|
||||
{
|
||||
case json_array:
|
||||
|
||||
if (! (value->u.array.values = (json_value **) json_alloc
|
||||
(state, value->u.array.length * sizeof (json_value *), 0)) )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
value->u.array.length = 0;
|
||||
break;
|
||||
|
||||
case json_object:
|
||||
|
||||
values_size = sizeof (*value->u.object.values) * value->u.object.length;
|
||||
|
||||
if (! ((*(void **) &value->u.object.values) = json_alloc
|
||||
(state, values_size + ((unsigned long) value->u.object.values), 0)) )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
value->_reserved.object_mem = (*(char **) &value->u.object.values) + values_size;
|
||||
|
||||
value->u.object.length = 0;
|
||||
break;
|
||||
|
||||
case json_string:
|
||||
|
||||
if (! (value->u.string.ptr = (json_char *) json_alloc
|
||||
(state, (value->u.string.length + 1) * sizeof (json_char), 0)) )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
value->u.string.length = 0;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
value = (json_value *) json_alloc (state, sizeof (json_value), 1);
|
||||
|
||||
if (!value)
|
||||
return 0;
|
||||
|
||||
if (!*root)
|
||||
*root = value;
|
||||
|
||||
value->type = type;
|
||||
value->parent = *top;
|
||||
|
||||
if (*alloc)
|
||||
(*alloc)->_reserved.next_alloc = value;
|
||||
|
||||
*alloc = *top = value;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define e_off \
|
||||
((int) (i - cur_line_begin))
|
||||
|
||||
#define whitespace \
|
||||
case '\n': ++ cur_line; cur_line_begin = i; \
|
||||
case ' ': case '\t': case '\r'
|
||||
|
||||
#define string_add(b) \
|
||||
do { if (!state.first_pass) string [string_length] = b; ++ string_length; } while (0);
|
||||
|
||||
static const long
|
||||
flag_next = 1 << 0,
|
||||
flag_reproc = 1 << 1,
|
||||
flag_need_comma = 1 << 2,
|
||||
flag_seek_value = 1 << 3,
|
||||
flag_escaped = 1 << 4,
|
||||
flag_string = 1 << 5,
|
||||
flag_need_colon = 1 << 6,
|
||||
flag_done = 1 << 7,
|
||||
flag_num_negative = 1 << 8,
|
||||
flag_num_zero = 1 << 9,
|
||||
flag_num_e = 1 << 10,
|
||||
flag_num_e_got_sign = 1 << 11,
|
||||
flag_num_e_negative = 1 << 12,
|
||||
flag_line_comment = 1 << 13,
|
||||
flag_block_comment = 1 << 14;
|
||||
|
||||
json_value * json_parse_ex (json_settings * settings,
|
||||
const json_char * json,
|
||||
size_t length,
|
||||
char * error_buf)
|
||||
{
|
||||
json_char error [json_error_max];
|
||||
unsigned int cur_line;
|
||||
const json_char * cur_line_begin, * i, * end;
|
||||
json_value * top, * root, * alloc = 0;
|
||||
json_state state = { 0 };
|
||||
long flags;
|
||||
long num_digits = 0, num_e = 0;
|
||||
json_int_t num_fraction = 0;
|
||||
|
||||
/* Skip UTF-8 BOM
|
||||
*/
|
||||
if (length >= 3 && ((unsigned char) json [0]) == 0xEF
|
||||
&& ((unsigned char) json [1]) == 0xBB
|
||||
&& ((unsigned char) json [2]) == 0xBF)
|
||||
{
|
||||
json += 3;
|
||||
length -= 3;
|
||||
}
|
||||
|
||||
error[0] = '\0';
|
||||
end = (json + length);
|
||||
|
||||
memcpy (&state.settings, settings, sizeof (json_settings));
|
||||
|
||||
if (!state.settings.mem_alloc)
|
||||
state.settings.mem_alloc = default_alloc;
|
||||
|
||||
if (!state.settings.mem_free)
|
||||
state.settings.mem_free = default_free;
|
||||
|
||||
memset (&state.uint_max, 0xFF, sizeof (state.uint_max));
|
||||
memset (&state.ulong_max, 0xFF, sizeof (state.ulong_max));
|
||||
|
||||
state.uint_max -= 8; /* limit of how much can be added before next check */
|
||||
state.ulong_max -= 8;
|
||||
|
||||
for (state.first_pass = 1; state.first_pass >= 0; -- state.first_pass)
|
||||
{
|
||||
json_uchar uchar;
|
||||
unsigned char uc_b1, uc_b2, uc_b3, uc_b4;
|
||||
json_char * string = 0;
|
||||
unsigned int string_length = 0;
|
||||
|
||||
top = root = 0;
|
||||
flags = flag_seek_value;
|
||||
|
||||
cur_line = 1;
|
||||
cur_line_begin = json;
|
||||
|
||||
for (i = json ;; ++ i)
|
||||
{
|
||||
json_char b = (i == end ? 0 : *i);
|
||||
|
||||
if (flags & flag_string)
|
||||
{
|
||||
if (!b)
|
||||
{ sprintf (error, "Unexpected EOF in string (at %d:%d)", cur_line, e_off);
|
||||
goto e_failed;
|
||||
}
|
||||
|
||||
if (string_length > state.uint_max)
|
||||
goto e_overflow;
|
||||
|
||||
if (flags & flag_escaped)
|
||||
{
|
||||
flags &= ~ flag_escaped;
|
||||
|
||||
switch (b)
|
||||
{
|
||||
case 'b': string_add ('\b'); break;
|
||||
case 'f': string_add ('\f'); break;
|
||||
case 'n': string_add ('\n'); break;
|
||||
case 'r': string_add ('\r'); break;
|
||||
case 't': string_add ('\t'); break;
|
||||
case 'u':
|
||||
|
||||
if (end - i < 4 ||
|
||||
(uc_b1 = hex_value (*++ i)) == 0xFF || (uc_b2 = hex_value (*++ i)) == 0xFF
|
||||
|| (uc_b3 = hex_value (*++ i)) == 0xFF || (uc_b4 = hex_value (*++ i)) == 0xFF)
|
||||
{
|
||||
sprintf (error, "Invalid character value `%c` (at %d:%d)", b, cur_line, e_off);
|
||||
goto e_failed;
|
||||
}
|
||||
|
||||
uc_b1 = uc_b1 * 16 + uc_b2;
|
||||
uc_b2 = uc_b3 * 16 + uc_b4;
|
||||
|
||||
uchar = ((json_char) uc_b1) * 256 + uc_b2;
|
||||
|
||||
if (sizeof (json_char) >= sizeof (json_uchar) || (uc_b1 == 0 && uc_b2 <= 0x7F))
|
||||
{
|
||||
string_add ((json_char) uchar);
|
||||
break;
|
||||
}
|
||||
|
||||
if (uchar <= 0x7FF)
|
||||
{
|
||||
if (state.first_pass)
|
||||
string_length += 2;
|
||||
else
|
||||
{ string [string_length ++] = 0xC0 | ((uc_b2 & 0xC0) >> 6) | ((uc_b1 & 0x7) << 2);
|
||||
string [string_length ++] = 0x80 | (uc_b2 & 0x3F);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (state.first_pass)
|
||||
string_length += 3;
|
||||
else
|
||||
{ string [string_length ++] = 0xE0 | ((uc_b1 & 0xF0) >> 4);
|
||||
string [string_length ++] = 0x80 | ((uc_b1 & 0xF) << 2) | ((uc_b2 & 0xC0) >> 6);
|
||||
string [string_length ++] = 0x80 | (uc_b2 & 0x3F);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
string_add (b);
|
||||
};
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (b == '\\')
|
||||
{
|
||||
flags |= flag_escaped;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (b == '"')
|
||||
{
|
||||
if (!state.first_pass)
|
||||
string [string_length] = 0;
|
||||
|
||||
flags &= ~ flag_string;
|
||||
string = 0;
|
||||
|
||||
switch (top->type)
|
||||
{
|
||||
case json_string:
|
||||
|
||||
top->u.string.length = string_length;
|
||||
flags |= flag_next;
|
||||
|
||||
break;
|
||||
|
||||
case json_object:
|
||||
|
||||
if (state.first_pass)
|
||||
(*(json_char **) &top->u.object.values) += string_length + 1;
|
||||
else
|
||||
{
|
||||
top->u.object.values [top->u.object.length].name
|
||||
= (json_char *) top->_reserved.object_mem;
|
||||
|
||||
top->u.object.values [top->u.object.length].name_length
|
||||
= string_length;
|
||||
|
||||
(*(json_char **) &top->_reserved.object_mem) += string_length + 1;
|
||||
}
|
||||
|
||||
flags |= flag_seek_value | flag_need_colon;
|
||||
continue;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
string_add (b);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (state.settings.settings & json_enable_comments)
|
||||
{
|
||||
if (flags & (flag_line_comment | flag_block_comment))
|
||||
{
|
||||
if (flags & flag_line_comment)
|
||||
{
|
||||
if (b == '\r' || b == '\n' || !b)
|
||||
{
|
||||
flags &= ~ flag_line_comment;
|
||||
-- i; /* so null can be reproc'd */
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (flags & flag_block_comment)
|
||||
{
|
||||
if (!b)
|
||||
{ sprintf (error, "%d:%d: Unexpected EOF in block comment", cur_line, e_off);
|
||||
goto e_failed;
|
||||
}
|
||||
|
||||
if (b == '*' && i < (end - 1) && i [1] == '/')
|
||||
{
|
||||
flags &= ~ flag_block_comment;
|
||||
++ i; /* skip closing sequence */
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (b == '/')
|
||||
{
|
||||
if (! (flags & (flag_seek_value | flag_done)) && top->type != json_object)
|
||||
{
|
||||
sprintf (error, "%d:%d: Comment not allowed here", cur_line, e_off);
|
||||
goto e_failed;
|
||||
}
|
||||
|
||||
if (++ i == end)
|
||||
{ sprintf (error, "%d:%d: EOF unexpected", cur_line, e_off);
|
||||
goto e_failed;
|
||||
}
|
||||
|
||||
switch (b = *i)
|
||||
{
|
||||
case '/':
|
||||
flags |= flag_line_comment;
|
||||
continue;
|
||||
|
||||
case '*':
|
||||
flags |= flag_block_comment;
|
||||
continue;
|
||||
|
||||
default:
|
||||
sprintf (error, "%d:%d: Unexpected `%c` in comment opening sequence", cur_line, e_off, b);
|
||||
goto e_failed;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (flags & flag_done)
|
||||
{
|
||||
if (!b)
|
||||
break;
|
||||
|
||||
switch (b)
|
||||
{
|
||||
whitespace:
|
||||
continue;
|
||||
|
||||
default:
|
||||
sprintf (error, "%d:%d: Trailing garbage: `%c`", cur_line, e_off, b);
|
||||
goto e_failed;
|
||||
};
|
||||
}
|
||||
|
||||
if (flags & flag_seek_value)
|
||||
{
|
||||
switch (b)
|
||||
{
|
||||
whitespace:
|
||||
continue;
|
||||
|
||||
case ']':
|
||||
|
||||
if (top->type == json_array)
|
||||
flags = (flags & ~ (flag_need_comma | flag_seek_value)) | flag_next;
|
||||
else
|
||||
{ sprintf (error, "%d:%d: Unexpected ]", cur_line, e_off);
|
||||
goto e_failed;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
if (flags & flag_need_comma)
|
||||
{
|
||||
if (b == ',')
|
||||
{ flags &= ~ flag_need_comma;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{ sprintf (error, "%d:%d: Expected , before %c", cur_line, e_off, b);
|
||||
goto e_failed;
|
||||
}
|
||||
}
|
||||
|
||||
if (flags & flag_need_colon)
|
||||
{
|
||||
if (b == ':')
|
||||
{ flags &= ~ flag_need_colon;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{ sprintf (error, "%d:%d: Expected : before %c", cur_line, e_off, b);
|
||||
goto e_failed;
|
||||
}
|
||||
}
|
||||
|
||||
flags &= ~ flag_seek_value;
|
||||
|
||||
switch (b)
|
||||
{
|
||||
case '{':
|
||||
|
||||
if (!new_value (&state, &top, &root, &alloc, json_object))
|
||||
goto e_alloc_failure;
|
||||
|
||||
continue;
|
||||
|
||||
case '[':
|
||||
|
||||
if (!new_value (&state, &top, &root, &alloc, json_array))
|
||||
goto e_alloc_failure;
|
||||
|
||||
flags |= flag_seek_value;
|
||||
continue;
|
||||
|
||||
case '"':
|
||||
|
||||
if (!new_value (&state, &top, &root, &alloc, json_string))
|
||||
goto e_alloc_failure;
|
||||
|
||||
flags |= flag_string;
|
||||
|
||||
string = top->u.string.ptr;
|
||||
string_length = 0;
|
||||
|
||||
continue;
|
||||
|
||||
case 't':
|
||||
|
||||
if ((end - i) < 3 || *(++ i) != 'r' || *(++ i) != 'u' || *(++ i) != 'e')
|
||||
goto e_unknown_value;
|
||||
|
||||
if (!new_value (&state, &top, &root, &alloc, json_boolean))
|
||||
goto e_alloc_failure;
|
||||
|
||||
top->u.boolean = 1;
|
||||
|
||||
flags |= flag_next;
|
||||
break;
|
||||
|
||||
case 'f':
|
||||
|
||||
if ((end - i) < 4 || *(++ i) != 'a' || *(++ i) != 'l' || *(++ i) != 's' || *(++ i) != 'e')
|
||||
goto e_unknown_value;
|
||||
|
||||
if (!new_value (&state, &top, &root, &alloc, json_boolean))
|
||||
goto e_alloc_failure;
|
||||
|
||||
flags |= flag_next;
|
||||
break;
|
||||
|
||||
case 'n':
|
||||
|
||||
if ((end - i) < 3 || *(++ i) != 'u' || *(++ i) != 'l' || *(++ i) != 'l')
|
||||
goto e_unknown_value;
|
||||
|
||||
if (!new_value (&state, &top, &root, &alloc, json_null))
|
||||
goto e_alloc_failure;
|
||||
|
||||
flags |= flag_next;
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
if (isdigit (b) || b == '-')
|
||||
{
|
||||
if (!new_value (&state, &top, &root, &alloc, json_integer))
|
||||
goto e_alloc_failure;
|
||||
|
||||
if (!state.first_pass)
|
||||
{
|
||||
while (isdigit (b) || b == '+' || b == '-'
|
||||
|| b == 'e' || b == 'E' || b == '.')
|
||||
{
|
||||
if ( (++ i) == end)
|
||||
{
|
||||
b = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
b = *i;
|
||||
}
|
||||
|
||||
flags |= flag_next | flag_reproc;
|
||||
break;
|
||||
}
|
||||
|
||||
flags &= ~ (flag_num_negative | flag_num_e |
|
||||
flag_num_e_got_sign | flag_num_e_negative |
|
||||
flag_num_zero);
|
||||
|
||||
num_digits = 0;
|
||||
num_fraction = 0;
|
||||
num_e = 0;
|
||||
|
||||
if (b != '-')
|
||||
{
|
||||
flags |= flag_reproc;
|
||||
break;
|
||||
}
|
||||
|
||||
flags |= flag_num_negative;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{ sprintf (error, "%d:%d: Unexpected %c when seeking value", cur_line, e_off, b);
|
||||
goto e_failed;
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (top->type)
|
||||
{
|
||||
case json_object:
|
||||
|
||||
switch (b)
|
||||
{
|
||||
whitespace:
|
||||
continue;
|
||||
|
||||
case '"':
|
||||
|
||||
if (flags & flag_need_comma)
|
||||
{
|
||||
sprintf (error, "%d:%d: Expected , before \"", cur_line, e_off);
|
||||
goto e_failed;
|
||||
}
|
||||
|
||||
flags |= flag_string;
|
||||
|
||||
string = (json_char *) top->_reserved.object_mem;
|
||||
string_length = 0;
|
||||
|
||||
break;
|
||||
|
||||
case '}':
|
||||
|
||||
flags = (flags & ~ flag_need_comma) | flag_next;
|
||||
break;
|
||||
|
||||
case ',':
|
||||
|
||||
if (flags & flag_need_comma)
|
||||
{
|
||||
flags &= ~ flag_need_comma;
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
|
||||
sprintf (error, "%d:%d: Unexpected `%c` in object", cur_line, e_off, b);
|
||||
goto e_failed;
|
||||
};
|
||||
|
||||
break;
|
||||
|
||||
case json_integer:
|
||||
case json_double:
|
||||
|
||||
if (isdigit (b))
|
||||
{
|
||||
++ num_digits;
|
||||
|
||||
if (top->type == json_integer || flags & flag_num_e)
|
||||
{
|
||||
if (! (flags & flag_num_e))
|
||||
{
|
||||
if (flags & flag_num_zero)
|
||||
{ sprintf (error, "%d:%d: Unexpected `0` before `%c`", cur_line, e_off, b);
|
||||
goto e_failed;
|
||||
}
|
||||
|
||||
if (num_digits == 1 && b == '0')
|
||||
flags |= flag_num_zero;
|
||||
}
|
||||
else
|
||||
{
|
||||
flags |= flag_num_e_got_sign;
|
||||
num_e = (num_e * 10) + (b - '0');
|
||||
continue;
|
||||
}
|
||||
|
||||
top->u.integer = (top->u.integer * 10) + (b - '0');
|
||||
continue;
|
||||
}
|
||||
|
||||
num_fraction = (num_fraction * 10) + (b - '0');
|
||||
continue;
|
||||
}
|
||||
|
||||
if (b == '+' || b == '-')
|
||||
{
|
||||
if ( (flags & flag_num_e) && !(flags & flag_num_e_got_sign))
|
||||
{
|
||||
flags |= flag_num_e_got_sign;
|
||||
|
||||
if (b == '-')
|
||||
flags |= flag_num_e_negative;
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (b == '.' && top->type == json_integer)
|
||||
{
|
||||
if (!num_digits)
|
||||
{ sprintf (error, "%d:%d: Expected digit before `.`", cur_line, e_off);
|
||||
goto e_failed;
|
||||
}
|
||||
|
||||
top->type = json_double;
|
||||
top->u.dbl = (double) top->u.integer;
|
||||
|
||||
num_digits = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! (flags & flag_num_e))
|
||||
{
|
||||
if (top->type == json_double)
|
||||
{
|
||||
if (!num_digits)
|
||||
{ sprintf (error, "%d:%d: Expected digit after `.`", cur_line, e_off);
|
||||
goto e_failed;
|
||||
}
|
||||
|
||||
top->u.dbl += ((double) num_fraction) / (pow (10, (double) num_digits));
|
||||
}
|
||||
|
||||
if (b == 'e' || b == 'E')
|
||||
{
|
||||
flags |= flag_num_e;
|
||||
|
||||
if (top->type == json_integer)
|
||||
{
|
||||
top->type = json_double;
|
||||
top->u.dbl = (double) top->u.integer;
|
||||
}
|
||||
|
||||
num_digits = 0;
|
||||
flags &= ~ flag_num_zero;
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!num_digits)
|
||||
{ sprintf (error, "%d:%d: Expected digit after `e`", cur_line, e_off);
|
||||
goto e_failed;
|
||||
}
|
||||
|
||||
top->u.dbl *= pow (10, (double) (flags & flag_num_e_negative ? - num_e : num_e));
|
||||
}
|
||||
|
||||
if (flags & flag_num_negative)
|
||||
{
|
||||
if (top->type == json_integer)
|
||||
top->u.integer = - top->u.integer;
|
||||
else
|
||||
top->u.dbl = - top->u.dbl;
|
||||
}
|
||||
|
||||
flags |= flag_next | flag_reproc;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
}
|
||||
|
||||
if (flags & flag_reproc)
|
||||
{
|
||||
flags &= ~ flag_reproc;
|
||||
-- i;
|
||||
}
|
||||
|
||||
if (flags & flag_next)
|
||||
{
|
||||
flags = (flags & ~ flag_next) | flag_need_comma;
|
||||
|
||||
if (!top->parent)
|
||||
{
|
||||
/* root value done */
|
||||
|
||||
flags |= flag_done;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (top->parent->type == json_array)
|
||||
flags |= flag_seek_value;
|
||||
|
||||
if (!state.first_pass)
|
||||
{
|
||||
json_value * parent = top->parent;
|
||||
|
||||
switch (parent->type)
|
||||
{
|
||||
case json_object:
|
||||
|
||||
parent->u.object.values
|
||||
[parent->u.object.length].value = top;
|
||||
|
||||
break;
|
||||
|
||||
case json_array:
|
||||
|
||||
parent->u.array.values
|
||||
[parent->u.array.length] = top;
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
}
|
||||
|
||||
if ( (++ top->parent->u.array.length) > state.uint_max)
|
||||
goto e_overflow;
|
||||
|
||||
top = top->parent;
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
alloc = root;
|
||||
}
|
||||
|
||||
return root;
|
||||
|
||||
e_unknown_value:
|
||||
|
||||
sprintf (error, "%d:%d: Unknown value", cur_line, e_off);
|
||||
goto e_failed;
|
||||
|
||||
e_alloc_failure:
|
||||
|
||||
strcpy (error, "Memory allocation failure");
|
||||
goto e_failed;
|
||||
|
||||
e_overflow:
|
||||
|
||||
sprintf (error, "%d:%d: Too long (caught overflow)", cur_line, e_off);
|
||||
goto e_failed;
|
||||
|
||||
e_failed:
|
||||
|
||||
if (error_buf)
|
||||
{
|
||||
if (*error)
|
||||
strcpy (error_buf, error);
|
||||
else
|
||||
strcpy (error_buf, "Unknown error");
|
||||
}
|
||||
|
||||
if (state.first_pass)
|
||||
alloc = root;
|
||||
|
||||
while (alloc)
|
||||
{
|
||||
top = alloc->_reserved.next_alloc;
|
||||
state.settings.mem_free (alloc, state.settings.user_data);
|
||||
alloc = top;
|
||||
}
|
||||
|
||||
if (!state.first_pass)
|
||||
json_value_free_ex (&state.settings, root);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
json_value * json_parse (const json_char * json, size_t length)
|
||||
{
|
||||
json_settings settings = { 0 };
|
||||
return json_parse_ex (&settings, json, length, 0);
|
||||
}
|
||||
|
||||
void json_value_free_ex (json_settings * settings, json_value * value)
|
||||
{
|
||||
json_value * cur_value;
|
||||
|
||||
if (!value)
|
||||
return;
|
||||
|
||||
value->parent = 0;
|
||||
|
||||
while (value)
|
||||
{
|
||||
switch (value->type)
|
||||
{
|
||||
case json_array:
|
||||
|
||||
if (!value->u.array.length)
|
||||
{
|
||||
settings->mem_free (value->u.array.values, settings->user_data);
|
||||
break;
|
||||
}
|
||||
|
||||
value = value->u.array.values [-- value->u.array.length];
|
||||
continue;
|
||||
|
||||
case json_object:
|
||||
|
||||
if (!value->u.object.length)
|
||||
{
|
||||
settings->mem_free (value->u.object.values, settings->user_data);
|
||||
break;
|
||||
}
|
||||
|
||||
value = value->u.object.values [-- value->u.object.length].value;
|
||||
continue;
|
||||
|
||||
case json_string:
|
||||
|
||||
settings->mem_free (value->u.string.ptr, settings->user_data);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
|
||||
cur_value = value;
|
||||
value = value->parent;
|
||||
settings->mem_free (cur_value, settings->user_data);
|
||||
}
|
||||
}
|
||||
|
||||
void json_value_free (json_value * value)
|
||||
{
|
||||
json_settings settings = { 0 };
|
||||
settings.mem_free = default_free;
|
||||
json_value_free_ex (&settings, value);
|
||||
}
|
||||
@ -0,0 +1,453 @@
|
||||
#define __STDC_FORMAT_MACROS
|
||||
#include <inttypes.h>
|
||||
#include <time.h>
|
||||
#include <detect.h>
|
||||
#include <system.h>
|
||||
#include <argparse.h>
|
||||
#include <json.h>
|
||||
#include "server.h"
|
||||
#include "main.h"
|
||||
|
||||
#if defined(CONF_FAMILY_UNIX)
|
||||
#include <signal.h>
|
||||
#endif
|
||||
|
||||
#ifndef PRId64
|
||||
#define PRId64 "I64d"
|
||||
#endif
|
||||
|
||||
static volatile int gs_Running = 1;
|
||||
static volatile int gs_ReloadConfig = 0;
|
||||
|
||||
static void ExitFunc(int Signal)
|
||||
{
|
||||
printf("[EXIT] Caught signal %d\n", Signal);
|
||||
gs_Running = 0;
|
||||
}
|
||||
|
||||
static void ReloadFunc(int Signal)
|
||||
{
|
||||
printf("[RELOAD] Caught signal %d\n", Signal);
|
||||
gs_ReloadConfig = 1;
|
||||
}
|
||||
|
||||
CConfig::CConfig()
|
||||
{
|
||||
// Initialize to default values
|
||||
m_Verbose = false; // -v, --verbose
|
||||
str_copy(m_aConfigFile, "config.json", sizeof(m_aConfigFile)); // -c, --config
|
||||
str_copy(m_aWebDir, "../web/", sizeof(m_aJSONFile)); // -d, --web-dir
|
||||
str_copy(m_aTemplateFile, "template.html", sizeof(m_aTemplateFile));
|
||||
str_copy(m_aJSONFile, "json/stats.json", sizeof(m_aJSONFile));
|
||||
str_copy(m_aBindAddr, "", sizeof(m_aBindAddr)); // -b, --bind
|
||||
m_Port = 35601; // -p, --port
|
||||
}
|
||||
|
||||
CMain::CMain(CConfig Config) : m_Config(Config)
|
||||
{
|
||||
mem_zero(m_aClients, sizeof(m_aClients));
|
||||
for(int i = 0; i < NET_MAX_CLIENTS; i++)
|
||||
m_aClients[i].m_ClientNetID = -1;
|
||||
}
|
||||
|
||||
CMain::CClient *CMain::ClientNet(int ClientNetID)
|
||||
{
|
||||
if(ClientNetID < 0 || ClientNetID >= NET_MAX_CLIENTS)
|
||||
return 0;
|
||||
|
||||
for(int i = 0; i < NET_MAX_CLIENTS; i++)
|
||||
{
|
||||
if(Client(i)->m_ClientNetID == ClientNetID)
|
||||
return Client(i);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CMain::ClientNetToClient(int ClientNetID)
|
||||
{
|
||||
if(ClientNetID < 0 || ClientNetID >= NET_MAX_CLIENTS)
|
||||
return -1;
|
||||
|
||||
for(int i = 0; i < NET_MAX_CLIENTS; i++)
|
||||
{
|
||||
if(Client(i)->m_ClientNetID == ClientNetID)
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
void CMain::OnNewClient(int ClientNetID, int ClientID)
|
||||
{
|
||||
dbg_msg("main", "OnNewClient(ncid=%d, cid=%d)", ClientNetID, ClientID);
|
||||
Client(ClientID)->m_ClientNetID = ClientNetID;
|
||||
Client(ClientID)->m_ClientNetType = m_Server.Network()->ClientAddr(ClientNetID)->type;
|
||||
Client(ClientID)->m_TimeConnected = time_get();
|
||||
Client(ClientID)->m_Connected = true;
|
||||
|
||||
if(Client(ClientID)->m_ClientNetType == NETTYPE_IPV4)
|
||||
Client(ClientID)->m_Stats.m_Online4 = true;
|
||||
else if(Client(ClientID)->m_ClientNetType == NETTYPE_IPV6)
|
||||
Client(ClientID)->m_Stats.m_Online6 = true;
|
||||
}
|
||||
|
||||
void CMain::OnDelClient(int ClientNetID)
|
||||
{
|
||||
int ClientID = ClientNetToClient(ClientNetID);
|
||||
dbg_msg("main", "OnDelClient(ncid=%d, cid=%d)", ClientNetID, ClientID);
|
||||
if(ClientID >= 0 && ClientID < NET_MAX_CLIENTS)
|
||||
{
|
||||
Client(ClientID)->m_Connected = false;
|
||||
Client(ClientID)->m_ClientNetID = -1;
|
||||
Client(ClientID)->m_ClientNetType = NETTYPE_INVALID;
|
||||
mem_zero(&Client(ClientID)->m_Stats, sizeof(CClient::CStats));
|
||||
}
|
||||
}
|
||||
|
||||
int CMain::HandleMessage(int ClientNetID, char *pMessage)
|
||||
{
|
||||
CClient *pClient = ClientNet(ClientNetID);
|
||||
if(!pClient)
|
||||
return true;
|
||||
|
||||
if(str_comp_num(pMessage, "update", sizeof("update")-1) == 0)
|
||||
{
|
||||
char *pData = str_skip_whitespaces(&pMessage[sizeof("update")-1]);
|
||||
|
||||
// parse json data
|
||||
json_settings JsonSettings;
|
||||
mem_zero(&JsonSettings, sizeof(JsonSettings));
|
||||
char aError[256];
|
||||
json_value *pJsonData = json_parse_ex(&JsonSettings, pData, strlen(pData), aError);
|
||||
if(!pJsonData)
|
||||
{
|
||||
dbg_msg("main", "JSON Error: %s", aError);
|
||||
if(pClient->m_Stats.m_Pong)
|
||||
m_Server.Network()->Send(ClientNetID, "1");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// extract data
|
||||
const json_value &rStart = (*pJsonData);
|
||||
if(rStart["uptime"].type)
|
||||
pClient->m_Stats.m_Uptime = rStart["uptime"].u.integer;
|
||||
if(rStart["load"].type)
|
||||
pClient->m_Stats.m_Load = rStart["load"].u.dbl;
|
||||
if(rStart["network_rx"].type)
|
||||
pClient->m_Stats.m_NetworkRx = rStart["network_rx"].u.integer;
|
||||
if(rStart["network_tx"].type)
|
||||
pClient->m_Stats.m_NetworkTx = rStart["network_tx"].u.integer;
|
||||
if(rStart["network_in"].type)
|
||||
pClient->m_Stats.m_NetworkIN = rStart["network_in"].u.integer;
|
||||
if(rStart["network_out"].type)
|
||||
pClient->m_Stats.m_NetworkOUT = rStart["network_out"].u.integer;
|
||||
if(rStart["memory_total"].type)
|
||||
pClient->m_Stats.m_MemTotal = rStart["memory_total"].u.integer;
|
||||
if(rStart["memory_used"].type)
|
||||
pClient->m_Stats.m_MemUsed = rStart["memory_used"].u.integer;
|
||||
if(rStart["swap_total"].type)
|
||||
pClient->m_Stats.m_SwapTotal = rStart["swap_total"].u.integer;
|
||||
if(rStart["swap_used"].type)
|
||||
pClient->m_Stats.m_SwapUsed = rStart["swap_used"].u.integer;
|
||||
if(rStart["hdd_total"].type)
|
||||
pClient->m_Stats.m_HDDTotal = rStart["hdd_total"].u.integer;
|
||||
if(rStart["hdd_used"].type)
|
||||
pClient->m_Stats.m_HDDUsed = rStart["hdd_used"].u.integer;
|
||||
if(rStart["cpu"].type)
|
||||
pClient->m_Stats.m_CPU = rStart["cpu"].u.dbl;
|
||||
if(rStart["online4"].type && pClient->m_ClientNetType == NETTYPE_IPV6)
|
||||
pClient->m_Stats.m_Online4 = rStart["online4"].u.boolean;
|
||||
if(rStart["online6"].type && pClient->m_ClientNetType == NETTYPE_IPV4)
|
||||
pClient->m_Stats.m_Online6 = rStart["online6"].u.boolean;
|
||||
if(rStart["custom"].type == json_string)
|
||||
str_copy(pClient->m_Stats.m_aCustom, rStart["custom"].u.string.ptr, sizeof(pClient->m_Stats.m_aCustom));
|
||||
|
||||
if(m_Config.m_Verbose)
|
||||
{
|
||||
if(rStart["online4"].type)
|
||||
dbg_msg("main", "Online4: %s\nUptime: %" PRId64 "\nLoad: %f\nNetworkRx: %" PRId64 "\nNetworkTx: %" PRId64 "\nNetworkIN: %" PRId64 "\nNetworkOUT: %" PRId64 "\nMemTotal: %" PRId64 "\nMemUsed: %" PRId64 "\nSwapTotal: %" PRId64 "\nSwapUsed: %" PRId64 "\nHDDTotal: %" PRId64 "\nHDDUsed: %" PRId64 "\nCPU: %f\n",
|
||||
rStart["online4"].u.boolean ? "true" : "false",
|
||||
pClient->m_Stats.m_Uptime, pClient->m_Stats.m_Load, pClient->m_Stats.m_NetworkRx, pClient->m_Stats.m_NetworkTx, pClient->m_Stats.m_NetworkIN, pClient->m_Stats.m_NetworkOUT, pClient->m_Stats.m_MemTotal, pClient->m_Stats.m_MemUsed, pClient->m_Stats.m_SwapTotal, pClient->m_Stats.m_SwapUsed, pClient->m_Stats.m_HDDTotal, pClient->m_Stats.m_HDDUsed, pClient->m_Stats.m_CPU);
|
||||
else if(rStart["online6"].type)
|
||||
dbg_msg("main", "Online6: %s\nUptime: %" PRId64 "\nLoad: %f\nNetworkRx: %" PRId64 "\nNetworkTx: %" PRId64 "\nNetworkIN: %" PRId64 "\nNetworkOUT: %" PRId64 "\nMemTotal: %" PRId64 "\nMemUsed: %" PRId64 "\nSwapTotal: %" PRId64 "\nSwapUsed: %" PRId64 "\nHDDTotal: %" PRId64 "\nHDDUsed: %" PRId64 "\nCPU: %f\n",
|
||||
rStart["online6"].u.boolean ? "true" : "false",
|
||||
pClient->m_Stats.m_Uptime, pClient->m_Stats.m_Load, pClient->m_Stats.m_NetworkRx, pClient->m_Stats.m_NetworkTx, pClient->m_Stats.m_NetworkIN, pClient->m_Stats.m_NetworkOUT, pClient->m_Stats.m_MemTotal, pClient->m_Stats.m_MemUsed, pClient->m_Stats.m_SwapTotal, pClient->m_Stats.m_SwapUsed, pClient->m_Stats.m_HDDTotal, pClient->m_Stats.m_HDDUsed, pClient->m_Stats.m_CPU);
|
||||
else
|
||||
dbg_msg("main", "Uptime: %" PRId64 "\nLoad: %f\nNetworkRx: %" PRId64 "\nNetworkTx: %" PRId64 "\nNetworkIN: %" PRId64 "\nNetworkOUT: %" PRId64 "\nMemTotal: %" PRId64 "\nMemUsed: %" PRId64 "\nSwapTotal: %" PRId64 "\nSwapUsed: %" PRId64 "\nHDDTotal: %" PRId64 "\nHDDUsed: %" PRId64 "\nCPU: %f\n",
|
||||
pClient->m_Stats.m_Uptime, pClient->m_Stats.m_Load, pClient->m_Stats.m_NetworkRx, pClient->m_Stats.m_NetworkTx, pClient->m_Stats.m_NetworkIN, pClient->m_Stats.m_NetworkOUT, pClient->m_Stats.m_MemTotal, pClient->m_Stats.m_MemUsed, pClient->m_Stats.m_SwapTotal, pClient->m_Stats.m_SwapUsed, pClient->m_Stats.m_HDDTotal, pClient->m_Stats.m_HDDUsed, pClient->m_Stats.m_CPU);
|
||||
}
|
||||
|
||||
// clean up
|
||||
json_value_free(pJsonData);
|
||||
|
||||
if(pClient->m_Stats.m_Pong)
|
||||
m_Server.Network()->Send(ClientNetID, "0");
|
||||
return 0;
|
||||
}
|
||||
else if(str_comp_num(pMessage, "pong", sizeof("pong")-1) == 0)
|
||||
{
|
||||
char *pData = str_skip_whitespaces(&pMessage[sizeof("pong")-1]);
|
||||
|
||||
if(!str_comp(pData, "0") || !str_comp(pData, "off"))
|
||||
pClient->m_Stats.m_Pong = false;
|
||||
else if(!str_comp(pData, "1") || !str_comp(pData, "on"))
|
||||
pClient->m_Stats.m_Pong = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(pClient->m_Stats.m_Pong)
|
||||
m_Server.Network()->Send(ClientNetID, "1");
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void CMain::JSONUpdateThread(void *pUser)
|
||||
{
|
||||
CJSONUpdateThreadData *m_pJSONUpdateThreadData = (CJSONUpdateThreadData *)pUser;
|
||||
CClient *pClients = m_pJSONUpdateThreadData->pClients;
|
||||
CConfig *pConfig = m_pJSONUpdateThreadData->pConfig;
|
||||
|
||||
while(gs_Running)
|
||||
{
|
||||
char aFileBuf[2048*NET_MAX_CLIENTS];
|
||||
char *pBuf = aFileBuf;
|
||||
|
||||
str_format(pBuf, sizeof(aFileBuf), "{\n\"servers\": [\n");
|
||||
pBuf += strlen(pBuf);
|
||||
|
||||
for(int i = 0; i < NET_MAX_CLIENTS; i++)
|
||||
{
|
||||
if(!pClients[i].m_Active || pClients[i].m_Disabled)
|
||||
continue;
|
||||
|
||||
if(pClients[i].m_Connected)
|
||||
{
|
||||
// Uptime
|
||||
char aUptime[16];
|
||||
int Days = pClients[i].m_Stats.m_Uptime/60.0/60.0/24.0;
|
||||
if(Days > 0)
|
||||
{
|
||||
if(Days > 1)
|
||||
str_format(aUptime, sizeof(aUptime), "%d 天", Days);
|
||||
else
|
||||
str_format(aUptime, sizeof(aUptime), "%d 天", Days);
|
||||
}
|
||||
else
|
||||
str_format(aUptime, sizeof(aUptime), "%02d:%02d:%02d", (int)(pClients[i].m_Stats.m_Uptime/60.0/60.0), (int)((pClients[i].m_Stats.m_Uptime/60)%60), (int)((pClients[i].m_Stats.m_Uptime)%60));
|
||||
|
||||
str_format(pBuf, sizeof(aFileBuf) - (pBuf - aFileBuf), "{ \"name\": \"%s\", \"type\": \"%s\", \"host\": \"%s\", \"location\": \"%s\", \"online4\": %s, \"online6\": %s, \"uptime\": \"%s\", \"load\": %.2f, \"network_rx\": %" PRId64 ", \"network_tx\": %" PRId64 ", \"network_in\": %" PRId64 ", \"network_out\": %" PRId64 ", \"cpu\": %d, \"memory_total\": %" PRId64 ", \"memory_used\": %" PRId64 ", \"swap_total\": %" PRId64 ", \"swap_used\": %" PRId64 ", \"hdd_total\": %" PRId64 ", \"hdd_used\": %" PRId64 ", \"custom\": \"%s\" },\n",
|
||||
pClients[i].m_aName, pClients[i].m_aType, pClients[i].m_aHost, pClients[i].m_aLocation, pClients[i].m_Stats.m_Online4 ? "true" : "false", pClients[i].m_Stats.m_Online6 ? "true" : "false", aUptime, pClients[i].m_Stats.m_Load, pClients[i].m_Stats.m_NetworkRx, pClients[i].m_Stats.m_NetworkTx, pClients[i].m_Stats.m_NetworkIN, pClients[i].m_Stats.m_NetworkOUT, (int)pClients[i].m_Stats.m_CPU, pClients[i].m_Stats.m_MemTotal, pClients[i].m_Stats.m_MemUsed, pClients[i].m_Stats.m_SwapTotal, pClients[i].m_Stats.m_SwapUsed, pClients[i].m_Stats.m_HDDTotal, pClients[i].m_Stats.m_HDDUsed, pClients[i].m_Stats.m_aCustom);
|
||||
pBuf += strlen(pBuf);
|
||||
}
|
||||
else
|
||||
{
|
||||
str_format(pBuf, sizeof(aFileBuf) - (pBuf - aFileBuf), "{ \"name\": \"%s\", \"type\": \"%s\", \"host\": \"%s\", \"location\": \"%s\", \"online4\": false, \"online6\": false },\n",
|
||||
pClients[i].m_aName, pClients[i].m_aType, pClients[i].m_aHost, pClients[i].m_aLocation);
|
||||
pBuf += strlen(pBuf);
|
||||
}
|
||||
}
|
||||
if(!m_pJSONUpdateThreadData->m_ReloadRequired)
|
||||
str_format(pBuf - 2, sizeof(aFileBuf) - (pBuf - aFileBuf), "\n],\n\"updated\": \"%lld\"\n}", (long long)time(/*ago*/0));
|
||||
else
|
||||
{
|
||||
str_format(pBuf - 2, sizeof(aFileBuf) - (pBuf - aFileBuf), "\n],\n\"updated\": \"%lld\",\n\"reload\": true\n}", (long long)time(/*ago*/0));
|
||||
m_pJSONUpdateThreadData->m_ReloadRequired--;
|
||||
}
|
||||
pBuf += strlen(pBuf);
|
||||
|
||||
char aJSONFileTmp[1024];
|
||||
str_format(aJSONFileTmp, sizeof(aJSONFileTmp), "%s~", pConfig->m_aJSONFile);
|
||||
IOHANDLE File = io_open(aJSONFileTmp, IOFLAG_WRITE);
|
||||
if(!File)
|
||||
{
|
||||
dbg_msg("main", "Couldn't open %s", aJSONFileTmp);
|
||||
exit(1);
|
||||
}
|
||||
io_write(File, aFileBuf, (pBuf - aFileBuf));
|
||||
io_flush(File);
|
||||
io_close(File);
|
||||
fs_rename(aJSONFileTmp, pConfig->m_aJSONFile);
|
||||
thread_sleep(1000);
|
||||
}
|
||||
fs_remove(pConfig->m_aJSONFile);
|
||||
}
|
||||
|
||||
int CMain::ReadConfig()
|
||||
{
|
||||
// read and parse config
|
||||
IOHANDLE File = io_open(m_Config.m_aConfigFile, IOFLAG_READ);
|
||||
if(!File)
|
||||
{
|
||||
dbg_msg("main", "Couldn't open %s", m_Config.m_aConfigFile);
|
||||
return 1;
|
||||
}
|
||||
int FileSize = (int)io_length(File);
|
||||
char *pFileData = (char *)mem_alloc(FileSize + 1, 1);
|
||||
|
||||
io_read(File, pFileData, FileSize);
|
||||
pFileData[FileSize] = 0;
|
||||
io_close(File);
|
||||
|
||||
// parse json data
|
||||
json_settings JsonSettings;
|
||||
mem_zero(&JsonSettings, sizeof(JsonSettings));
|
||||
char aError[256];
|
||||
json_value *pJsonData = json_parse_ex(&JsonSettings, pFileData, strlen(pFileData), aError);
|
||||
if(!pJsonData)
|
||||
{
|
||||
dbg_msg("main", "JSON Error in file %s: %s", m_Config.m_aConfigFile, aError);
|
||||
mem_free(pFileData);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// reset clients
|
||||
for(int i = 0; i < NET_MAX_CLIENTS; i++)
|
||||
{
|
||||
if(!Client(i)->m_Active || !Client(i)->m_Connected)
|
||||
continue;
|
||||
|
||||
m_Server.Network()->Drop(Client(i)->m_ClientNetID, "Server reloading...");
|
||||
}
|
||||
mem_zero(m_aClients, sizeof(m_aClients));
|
||||
for(int i = 0; i < NET_MAX_CLIENTS; i++)
|
||||
m_aClients[i].m_ClientNetID = -1;
|
||||
|
||||
// extract data
|
||||
int ID = 0;
|
||||
const json_value &rStart = (*pJsonData)["servers"];
|
||||
if(rStart.type == json_array)
|
||||
{
|
||||
for(unsigned i = 0; i < rStart.u.array.length; i++)
|
||||
{
|
||||
if(ID < 0 || ID >= NET_MAX_CLIENTS)
|
||||
continue;
|
||||
|
||||
Client(ID)->m_Active = true;
|
||||
Client(ID)->m_Disabled = rStart[i]["disabled"].u.boolean;
|
||||
str_copy(Client(ID)->m_aName, rStart[i]["name"].u.string.ptr, sizeof(Client(ID)->m_aName));
|
||||
str_copy(Client(ID)->m_aUsername, rStart[i]["username"].u.string.ptr, sizeof(Client(ID)->m_aUsername));
|
||||
str_copy(Client(ID)->m_aType, rStart[i]["type"].u.string.ptr, sizeof(Client(ID)->m_aType));
|
||||
str_copy(Client(ID)->m_aHost, rStart[i]["host"].u.string.ptr, sizeof(Client(ID)->m_aHost));
|
||||
str_copy(Client(ID)->m_aLocation, rStart[i]["location"].u.string.ptr, sizeof(Client(ID)->m_aLocation));
|
||||
str_copy(Client(ID)->m_aPassword, rStart[i]["password"].u.string.ptr, sizeof(Client(ID)->m_aPassword));
|
||||
|
||||
if(m_Config.m_Verbose)
|
||||
{
|
||||
if(Client(ID)->m_Disabled)
|
||||
dbg_msg("main", "[#%d: Name: \"%s\", Username: \"%s\", Type: \"%s\", Host: \"%s\", Location: \"%s\", Password: \"%s\"]",
|
||||
ID, Client(ID)->m_aName, Client(ID)->m_aUsername, Client(ID)->m_aType, Client(ID)->m_aHost, Client(ID)->m_aLocation, Client(ID)->m_aPassword);
|
||||
else
|
||||
dbg_msg("main", "#%d: Name: \"%s\", Username: \"%s\", Type: \"%s\", Host: \"%s\", Location: \"%s\", Password: \"%s\"",
|
||||
ID, Client(ID)->m_aName, Client(ID)->m_aUsername, Client(ID)->m_aType, Client(ID)->m_aHost, Client(ID)->m_aLocation, Client(ID)->m_aPassword);
|
||||
|
||||
}
|
||||
ID++;
|
||||
}
|
||||
}
|
||||
|
||||
// clean up
|
||||
json_value_free(pJsonData);
|
||||
mem_free(pFileData);
|
||||
|
||||
// tell clients to reload the page
|
||||
m_JSONUpdateThreadData.m_ReloadRequired = 2;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CMain::Run()
|
||||
{
|
||||
if(m_Server.Init(this, m_Config.m_aBindAddr, m_Config.m_Port))
|
||||
return 1;
|
||||
|
||||
if(ReadConfig())
|
||||
return 1;
|
||||
|
||||
// Start JSON Update Thread
|
||||
m_JSONUpdateThreadData.m_ReloadRequired = 2;
|
||||
m_JSONUpdateThreadData.pClients = m_aClients;
|
||||
m_JSONUpdateThreadData.pConfig = &m_Config;
|
||||
void *LoadThread = thread_create(JSONUpdateThread, &m_JSONUpdateThreadData);
|
||||
//thread_detach(LoadThread);
|
||||
|
||||
while(gs_Running)
|
||||
{
|
||||
if(gs_ReloadConfig)
|
||||
{
|
||||
if(ReadConfig())
|
||||
return 1;
|
||||
m_Server.NetBan()->UnbanAll();
|
||||
gs_ReloadConfig = 0;
|
||||
}
|
||||
|
||||
m_Server.Update();
|
||||
|
||||
// wait for incomming data
|
||||
net_socket_read_wait(*m_Server.Network()->Socket(), 10);
|
||||
}
|
||||
|
||||
dbg_msg("server", "Closing.");
|
||||
m_Server.Network()->Close();
|
||||
thread_wait(LoadThread);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, const char *argv[])
|
||||
{
|
||||
int RetVal;
|
||||
dbg_logger_stdout();
|
||||
|
||||
#if defined(CONF_FAMILY_UNIX)
|
||||
signal(SIGINT, ExitFunc);
|
||||
signal(SIGTERM, ExitFunc);
|
||||
signal(SIGQUIT, ExitFunc);
|
||||
signal(SIGHUP, ReloadFunc);
|
||||
#endif
|
||||
|
||||
char aUsage[128];
|
||||
CConfig Config;
|
||||
str_format(aUsage, sizeof(aUsage), "%s [options]", argv[0]);
|
||||
const char *pConfigFile = 0;
|
||||
const char *pWebDir = 0;
|
||||
const char *pBindAddr = 0;
|
||||
|
||||
struct argparse_option aOptions[] = {
|
||||
OPT_HELP(),
|
||||
OPT_BOOLEAN('v', "verbose", &Config.m_Verbose, "Verbose output", 0),
|
||||
OPT_STRING('c', "config", &pConfigFile, "Config file to use", 0),
|
||||
OPT_STRING('d', "web-dir", &pWebDir, "Location of the web directory", 0),
|
||||
OPT_STRING('b', "bind", &pBindAddr, "Bind to address", 0),
|
||||
OPT_INTEGER('p', "port", &Config.m_Port, "Listen on port", 0),
|
||||
OPT_END(),
|
||||
};
|
||||
struct argparse Argparse;
|
||||
argparse_init(&Argparse, aOptions, aUsage, 0);
|
||||
argc = argparse_parse(&Argparse, argc, argv);
|
||||
|
||||
if(pConfigFile)
|
||||
str_copy(Config.m_aConfigFile, pConfigFile, sizeof(Config.m_aConfigFile));
|
||||
if(pWebDir)
|
||||
str_copy(Config.m_aWebDir, pWebDir, sizeof(Config.m_aWebDir));
|
||||
if(pBindAddr)
|
||||
str_copy(Config.m_aBindAddr, pBindAddr, sizeof(Config.m_aBindAddr));
|
||||
|
||||
if(Config.m_aWebDir[strlen(Config.m_aWebDir)-1] != '/')
|
||||
str_append(Config.m_aWebDir, "/", sizeof(Config.m_aWebDir));
|
||||
if(!fs_is_dir(Config.m_aWebDir))
|
||||
{
|
||||
dbg_msg("main", "ERROR: Can't find web directory: %s", Config.m_aWebDir);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char aTmp[1024];
|
||||
str_format(aTmp, sizeof(aTmp), "%s%s", Config.m_aWebDir, Config.m_aJSONFile);
|
||||
str_copy(Config.m_aJSONFile, aTmp, sizeof(Config.m_aJSONFile));
|
||||
|
||||
CMain Main(Config);
|
||||
RetVal = Main.Run();
|
||||
|
||||
return RetVal;
|
||||
}
|
||||
@ -0,0 +1,90 @@
|
||||
#ifndef MAIN_H
|
||||
#define MAIN_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "server.h"
|
||||
|
||||
class CConfig
|
||||
{
|
||||
public:
|
||||
bool m_Verbose;
|
||||
char m_aConfigFile[1024];
|
||||
char m_aWebDir[1024];
|
||||
char m_aTemplateFile[1024];
|
||||
char m_aJSONFile[1024];
|
||||
char m_aBindAddr[256];
|
||||
int m_Port;
|
||||
|
||||
CConfig();
|
||||
};
|
||||
|
||||
class CMain
|
||||
{
|
||||
CConfig m_Config;
|
||||
CServer m_Server;
|
||||
|
||||
struct CClient
|
||||
{
|
||||
bool m_Active;
|
||||
bool m_Disabled;
|
||||
bool m_Connected;
|
||||
int m_ClientNetID;
|
||||
int m_ClientNetType;
|
||||
char m_aUsername[128];
|
||||
char m_aName[128];
|
||||
char m_aType[128];
|
||||
char m_aHost[128];
|
||||
char m_aLocation[128];
|
||||
char m_aPassword[128];
|
||||
|
||||
int64 m_TimeConnected;
|
||||
int64 m_LastUpdate;
|
||||
|
||||
struct CStats
|
||||
{
|
||||
bool m_Online4;
|
||||
bool m_Online6;
|
||||
int64_t m_Uptime;
|
||||
double m_Load;
|
||||
int64_t m_NetworkRx;
|
||||
int64_t m_NetworkTx;
|
||||
int64_t m_NetworkIN;
|
||||
int64_t m_NetworkOUT;
|
||||
int64_t m_MemTotal;
|
||||
int64_t m_MemUsed;
|
||||
int64_t m_SwapTotal;
|
||||
int64_t m_SwapUsed;
|
||||
int64_t m_HDDTotal;
|
||||
int64_t m_HDDUsed;
|
||||
double m_CPU;
|
||||
char m_aCustom[512];
|
||||
// Options
|
||||
bool m_Pong;
|
||||
} m_Stats;
|
||||
} m_aClients[NET_MAX_CLIENTS];
|
||||
|
||||
struct CJSONUpdateThreadData
|
||||
{
|
||||
CClient *pClients;
|
||||
CConfig *pConfig;
|
||||
volatile short m_ReloadRequired;
|
||||
} m_JSONUpdateThreadData;
|
||||
|
||||
static void JSONUpdateThread(void *pUser);
|
||||
public:
|
||||
CMain(CConfig Config);
|
||||
|
||||
void OnNewClient(int ClienNettID, int ClientID);
|
||||
void OnDelClient(int ClientNetID);
|
||||
int HandleMessage(int ClientNetID, char *pMessage);
|
||||
int ReadConfig();
|
||||
int Run();
|
||||
|
||||
CClient *Client(int ClientID) { return &m_aClients[ClientID]; }
|
||||
CClient *ClientNet(int ClientNetID);
|
||||
const CConfig *Config() const { return &m_Config; }
|
||||
int ClientNetToClient(int ClientNetID);
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
@ -0,0 +1,463 @@
|
||||
#include <math.h>
|
||||
#include "netban.h"
|
||||
|
||||
bool CNetBan::StrAllnum(const char *pStr)
|
||||
{
|
||||
while(*pStr)
|
||||
{
|
||||
if(!(*pStr >= '0' && *pStr <= '9'))
|
||||
return false;
|
||||
pStr++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
CNetBan::CNetHash::CNetHash(const NETADDR *pAddr)
|
||||
{
|
||||
if(pAddr->type==NETTYPE_IPV4)
|
||||
m_Hash = (pAddr->ip[0]+pAddr->ip[1]+pAddr->ip[2]+pAddr->ip[3])&0xFF;
|
||||
else
|
||||
m_Hash = (pAddr->ip[0]+pAddr->ip[1]+pAddr->ip[2]+pAddr->ip[3]+pAddr->ip[4]+pAddr->ip[5]+pAddr->ip[6]+pAddr->ip[7]+
|
||||
pAddr->ip[8]+pAddr->ip[9]+pAddr->ip[10]+pAddr->ip[11]+pAddr->ip[12]+pAddr->ip[13]+pAddr->ip[14]+pAddr->ip[15])&0xFF;
|
||||
m_HashIndex = 0;
|
||||
}
|
||||
|
||||
CNetBan::CNetHash::CNetHash(const CNetRange *pRange)
|
||||
{
|
||||
m_Hash = 0;
|
||||
m_HashIndex = 0;
|
||||
for(int i = 0; pRange->m_LB.ip[i] == pRange->m_UB.ip[i]; ++i)
|
||||
{
|
||||
m_Hash += pRange->m_LB.ip[i];
|
||||
++m_HashIndex;
|
||||
}
|
||||
m_Hash &= 0xFF;
|
||||
}
|
||||
|
||||
int CNetBan::CNetHash::MakeHashArray(const NETADDR *pAddr, CNetHash aHash[17])
|
||||
{
|
||||
int Length = pAddr->type==NETTYPE_IPV4 ? 4 : 16;
|
||||
aHash[0].m_Hash = 0;
|
||||
aHash[0].m_HashIndex = 0;
|
||||
for(int i = 1, Sum = 0; i <= Length; ++i)
|
||||
{
|
||||
Sum += pAddr->ip[i-1];
|
||||
aHash[i].m_Hash = Sum&0xFF;
|
||||
aHash[i].m_HashIndex = i%Length;
|
||||
}
|
||||
return Length;
|
||||
}
|
||||
|
||||
|
||||
template<class T, int HashCount>
|
||||
typename CNetBan::CBan<T> *CNetBan::CBanPool<T, HashCount>::Add(const T *pData, const CBanInfo *pInfo, const CNetHash *pNetHash)
|
||||
{
|
||||
if(!m_pFirstFree)
|
||||
return 0;
|
||||
|
||||
// create new ban
|
||||
CBan<T> *pBan = m_pFirstFree;
|
||||
pBan->m_Data = *pData;
|
||||
pBan->m_Info = *pInfo;
|
||||
pBan->m_NetHash = *pNetHash;
|
||||
if(pBan->m_pNext)
|
||||
pBan->m_pNext->m_pPrev = pBan->m_pPrev;
|
||||
if(pBan->m_pPrev)
|
||||
pBan->m_pPrev->m_pNext = pBan->m_pNext;
|
||||
else
|
||||
m_pFirstFree = pBan->m_pNext;
|
||||
|
||||
// add it to the hash list
|
||||
if(m_paaHashList[pNetHash->m_HashIndex][pNetHash->m_Hash])
|
||||
m_paaHashList[pNetHash->m_HashIndex][pNetHash->m_Hash]->m_pHashPrev = pBan;
|
||||
pBan->m_pHashPrev = 0;
|
||||
pBan->m_pHashNext = m_paaHashList[pNetHash->m_HashIndex][pNetHash->m_Hash];
|
||||
m_paaHashList[pNetHash->m_HashIndex][pNetHash->m_Hash] = pBan;
|
||||
|
||||
// insert it into the used list
|
||||
if(m_pFirstUsed)
|
||||
{
|
||||
for(CBan<T> *p = m_pFirstUsed; ; p = p->m_pNext)
|
||||
{
|
||||
if(p->m_Info.m_Expires == CBanInfo::EXPIRES_NEVER || (pInfo->m_Expires != CBanInfo::EXPIRES_NEVER && pInfo->m_Expires <= p->m_Info.m_Expires))
|
||||
{
|
||||
// insert before
|
||||
pBan->m_pNext = p;
|
||||
pBan->m_pPrev = p->m_pPrev;
|
||||
if(p->m_pPrev)
|
||||
p->m_pPrev->m_pNext = pBan;
|
||||
else
|
||||
m_pFirstUsed = pBan;
|
||||
p->m_pPrev = pBan;
|
||||
break;
|
||||
}
|
||||
|
||||
if(!p->m_pNext)
|
||||
{
|
||||
// last entry
|
||||
p->m_pNext = pBan;
|
||||
pBan->m_pPrev = p;
|
||||
pBan->m_pNext = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_pFirstUsed = pBan;
|
||||
pBan->m_pNext = pBan->m_pPrev = 0;
|
||||
}
|
||||
|
||||
// update ban count
|
||||
++m_CountUsed;
|
||||
|
||||
return pBan;
|
||||
}
|
||||
|
||||
template<class T, int HashCount>
|
||||
int CNetBan::CBanPool<T, HashCount>::Remove(CBan<T> *pBan)
|
||||
{
|
||||
if(pBan == 0)
|
||||
return -1;
|
||||
|
||||
// remove from hash list
|
||||
if(pBan->m_pHashNext)
|
||||
pBan->m_pHashNext->m_pHashPrev = pBan->m_pHashPrev;
|
||||
if(pBan->m_pHashPrev)
|
||||
pBan->m_pHashPrev->m_pHashNext = pBan->m_pHashNext;
|
||||
else
|
||||
m_paaHashList[pBan->m_NetHash.m_HashIndex][pBan->m_NetHash.m_Hash] = pBan->m_pHashNext;
|
||||
pBan->m_pHashNext = pBan->m_pHashPrev = 0;
|
||||
|
||||
// remove from used list
|
||||
if(pBan->m_pNext)
|
||||
pBan->m_pNext->m_pPrev = pBan->m_pPrev;
|
||||
if(pBan->m_pPrev)
|
||||
pBan->m_pPrev->m_pNext = pBan->m_pNext;
|
||||
else
|
||||
m_pFirstUsed = pBan->m_pNext;
|
||||
|
||||
// add to recycle list
|
||||
if(m_pFirstFree)
|
||||
m_pFirstFree->m_pPrev = pBan;
|
||||
pBan->m_pPrev = 0;
|
||||
pBan->m_pNext = m_pFirstFree;
|
||||
m_pFirstFree = pBan;
|
||||
|
||||
// update ban count
|
||||
--m_CountUsed;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<class T, int HashCount>
|
||||
void CNetBan::CBanPool<T, HashCount>::Update(CBan<CDataType> *pBan, const CBanInfo *pInfo)
|
||||
{
|
||||
pBan->m_Info = *pInfo;
|
||||
|
||||
// remove from used list
|
||||
if(pBan->m_pNext)
|
||||
pBan->m_pNext->m_pPrev = pBan->m_pPrev;
|
||||
if(pBan->m_pPrev)
|
||||
pBan->m_pPrev->m_pNext = pBan->m_pNext;
|
||||
else
|
||||
m_pFirstUsed = pBan->m_pNext;
|
||||
|
||||
// insert it into the used list
|
||||
if(m_pFirstUsed)
|
||||
{
|
||||
for(CBan<T> *p = m_pFirstUsed; ; p = p->m_pNext)
|
||||
{
|
||||
if(p->m_Info.m_Expires == CBanInfo::EXPIRES_NEVER || (pInfo->m_Expires != CBanInfo::EXPIRES_NEVER && pInfo->m_Expires <= p->m_Info.m_Expires))
|
||||
{
|
||||
// insert before
|
||||
pBan->m_pNext = p;
|
||||
pBan->m_pPrev = p->m_pPrev;
|
||||
if(p->m_pPrev)
|
||||
p->m_pPrev->m_pNext = pBan;
|
||||
else
|
||||
m_pFirstUsed = pBan;
|
||||
p->m_pPrev = pBan;
|
||||
break;
|
||||
}
|
||||
|
||||
if(!p->m_pNext)
|
||||
{
|
||||
// last entry
|
||||
p->m_pNext = pBan;
|
||||
pBan->m_pPrev = p;
|
||||
pBan->m_pNext = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_pFirstUsed = pBan;
|
||||
pBan->m_pNext = pBan->m_pPrev = 0;
|
||||
}
|
||||
}
|
||||
|
||||
template<class T, int HashCount>
|
||||
void CNetBan::CBanPool<T, HashCount>::Reset()
|
||||
{
|
||||
mem_zero(m_paaHashList, sizeof(m_paaHashList));
|
||||
mem_zero(m_aBans, sizeof(m_aBans));
|
||||
m_pFirstUsed = 0;
|
||||
m_CountUsed = 0;
|
||||
|
||||
for(int i = 1; i < MAX_BANS-1; ++i)
|
||||
{
|
||||
m_aBans[i].m_pNext = &m_aBans[i+1];
|
||||
m_aBans[i].m_pPrev = &m_aBans[i-1];
|
||||
}
|
||||
|
||||
m_aBans[0].m_pNext = &m_aBans[1];
|
||||
m_aBans[MAX_BANS-1].m_pPrev = &m_aBans[MAX_BANS-2];
|
||||
m_pFirstFree = &m_aBans[0];
|
||||
}
|
||||
|
||||
template<class T, int HashCount>
|
||||
typename CNetBan::CBan<T> *CNetBan::CBanPool<T, HashCount>::Get(int Index) const
|
||||
{
|
||||
if(Index < 0 || Index >= Num())
|
||||
return 0;
|
||||
|
||||
for(CNetBan::CBan<T> *pBan = m_pFirstUsed; pBan; pBan = pBan->m_pNext, --Index)
|
||||
{
|
||||
if(Index == 0)
|
||||
return pBan;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
void CNetBan::MakeBanInfo(const CBan<T> *pBan, char *pBuf, unsigned BuffSize, int Type) const
|
||||
{
|
||||
if(pBan == 0 || pBuf == 0)
|
||||
{
|
||||
if(BuffSize > 0)
|
||||
pBuf[0] = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
// build type based part
|
||||
char aBuf[256];
|
||||
if(Type == MSGTYPE_PLAYER)
|
||||
str_copy(aBuf, "You have been banned", sizeof(aBuf));
|
||||
else
|
||||
{
|
||||
char aTemp[256];
|
||||
switch(Type)
|
||||
{
|
||||
case MSGTYPE_LIST:
|
||||
str_format(aBuf, sizeof(aBuf), "%s banned", NetToString(&pBan->m_Data, aTemp, sizeof(aTemp))); break;
|
||||
case MSGTYPE_BANADD:
|
||||
str_format(aBuf, sizeof(aBuf), "banned %s", NetToString(&pBan->m_Data, aTemp, sizeof(aTemp))); break;
|
||||
case MSGTYPE_BANREM:
|
||||
str_format(aBuf, sizeof(aBuf), "unbanned %s", NetToString(&pBan->m_Data, aTemp, sizeof(aTemp))); break;
|
||||
default:
|
||||
aBuf[0] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// add info part
|
||||
if(pBan->m_Info.m_Expires != CBanInfo::EXPIRES_NEVER)
|
||||
{
|
||||
int Mins = ((pBan->m_Info.m_Expires-time_timestamp()) + 59) / 60;
|
||||
if(Mins <= 1)
|
||||
str_format(pBuf, BuffSize, "%s for 1 minute (%s)", aBuf, pBan->m_Info.m_aReason);
|
||||
else
|
||||
str_format(pBuf, BuffSize, "%s for %d minutes (%s)", aBuf, Mins, pBan->m_Info.m_aReason);
|
||||
}
|
||||
else
|
||||
str_format(pBuf, BuffSize, "%s for life (%s)", aBuf, pBan->m_Info.m_aReason);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
int CNetBan::Ban(T *pBanPool, const typename T::CDataType *pData, int Seconds, const char *pReason)
|
||||
{
|
||||
// do not ban localhost
|
||||
if(NetMatch(pData, &m_LocalhostIPV4) || NetMatch(pData, &m_LocalhostIPV6))
|
||||
{
|
||||
dbg_msg("net_ban", "ban failed (localhost)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int Stamp = Seconds > 0 ? time_timestamp()+Seconds : CBanInfo::EXPIRES_NEVER;
|
||||
|
||||
// set up info
|
||||
CBanInfo Info = {0};
|
||||
Info.m_Expires = Stamp;
|
||||
str_copy(Info.m_aReason, pReason, sizeof(Info.m_aReason));
|
||||
|
||||
// check if it already exists
|
||||
CNetHash NetHash(pData);
|
||||
CBan<typename T::CDataType> *pBan = pBanPool->Find(pData, &NetHash);
|
||||
if(pBan)
|
||||
{
|
||||
// adjust the ban
|
||||
pBanPool->Update(pBan, &Info);
|
||||
char aBuf[128];
|
||||
MakeBanInfo(pBan, aBuf, sizeof(aBuf), MSGTYPE_LIST);
|
||||
dbg_msg("net_ban", aBuf);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// add ban and print result
|
||||
pBan = pBanPool->Add(pData, &Info, &NetHash);
|
||||
if(pBan)
|
||||
{
|
||||
char aBuf[128];
|
||||
MakeBanInfo(pBan, aBuf, sizeof(aBuf), MSGTYPE_BANADD);
|
||||
dbg_msg("net_ban", aBuf);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
dbg_msg("net_ban", "ban failed (full banlist)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
int CNetBan::Unban(T *pBanPool, const typename T::CDataType *pData)
|
||||
{
|
||||
CNetHash NetHash(pData);
|
||||
CBan<typename T::CDataType> *pBan = pBanPool->Find(pData, &NetHash);
|
||||
if(pBan)
|
||||
{
|
||||
char aBuf[256];
|
||||
MakeBanInfo(pBan, aBuf, sizeof(aBuf), MSGTYPE_BANREM);
|
||||
pBanPool->Remove(pBan);
|
||||
dbg_msg("net_ban", aBuf);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
dbg_msg("net_ban", "unban failed (invalid entry)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
void CNetBan::Init()
|
||||
{
|
||||
m_BanAddrPool.Reset();
|
||||
m_BanRangePool.Reset();
|
||||
|
||||
net_host_lookup("localhost", &m_LocalhostIPV4, NETTYPE_IPV4);
|
||||
net_host_lookup("localhost", &m_LocalhostIPV6, NETTYPE_IPV6);
|
||||
}
|
||||
|
||||
void CNetBan::Update()
|
||||
{
|
||||
int Now = time_timestamp();
|
||||
|
||||
// remove expired bans
|
||||
char aBuf[256], aNetStr[256];
|
||||
while(m_BanAddrPool.First() && m_BanAddrPool.First()->m_Info.m_Expires != CBanInfo::EXPIRES_NEVER && m_BanAddrPool.First()->m_Info.m_Expires < Now)
|
||||
{
|
||||
str_format(aBuf, sizeof(aBuf), "ban %s expired", NetToString(&m_BanAddrPool.First()->m_Data, aNetStr, sizeof(aNetStr)));
|
||||
dbg_msg("net_ban", aBuf);
|
||||
m_BanAddrPool.Remove(m_BanAddrPool.First());
|
||||
}
|
||||
while(m_BanRangePool.First() && m_BanRangePool.First()->m_Info.m_Expires != CBanInfo::EXPIRES_NEVER && m_BanRangePool.First()->m_Info.m_Expires < Now)
|
||||
{
|
||||
str_format(aBuf, sizeof(aBuf), "ban %s expired", NetToString(&m_BanRangePool.First()->m_Data, aNetStr, sizeof(aNetStr)));
|
||||
dbg_msg("net_ban", aBuf);
|
||||
m_BanRangePool.Remove(m_BanRangePool.First());
|
||||
}
|
||||
}
|
||||
|
||||
int CNetBan::BanAddr(const NETADDR *pAddr, int Seconds, const char *pReason)
|
||||
{
|
||||
return Ban(&m_BanAddrPool, pAddr, Seconds, pReason);
|
||||
}
|
||||
|
||||
int CNetBan::BanRange(const CNetRange *pRange, int Seconds, const char *pReason)
|
||||
{
|
||||
if(pRange->IsValid())
|
||||
return Ban(&m_BanRangePool, pRange, Seconds, pReason);
|
||||
|
||||
dbg_msg("net_ban", "ban failed (invalid range)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int CNetBan::UnbanByAddr(const NETADDR *pAddr)
|
||||
{
|
||||
return Unban(&m_BanAddrPool, pAddr);
|
||||
}
|
||||
|
||||
int CNetBan::UnbanByRange(const CNetRange *pRange)
|
||||
{
|
||||
if(pRange->IsValid())
|
||||
return Unban(&m_BanRangePool, pRange);
|
||||
|
||||
dbg_msg("net_ban", "ban failed (invalid range)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int CNetBan::UnbanByIndex(int Index)
|
||||
{
|
||||
int Result;
|
||||
char aBuf[256];
|
||||
CBanAddr *pBan = m_BanAddrPool.Get(Index);
|
||||
if(pBan)
|
||||
{
|
||||
NetToString(&pBan->m_Data, aBuf, sizeof(aBuf));
|
||||
Result = m_BanAddrPool.Remove(pBan);
|
||||
}
|
||||
else
|
||||
{
|
||||
CBanRange *pBan = m_BanRangePool.Get(Index-m_BanAddrPool.Num());
|
||||
if(pBan)
|
||||
{
|
||||
NetToString(&pBan->m_Data, aBuf, sizeof(aBuf));
|
||||
Result = m_BanRangePool.Remove(pBan);
|
||||
}
|
||||
else
|
||||
{
|
||||
dbg_msg("net_ban", "unban failed (invalid index)");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
char aMsg[256];
|
||||
str_format(aMsg, sizeof(aMsg), "unbanned index %i (%s)", Index, aBuf);
|
||||
dbg_msg("net_ban", aMsg);
|
||||
return Result;
|
||||
}
|
||||
|
||||
void CNetBan::UnbanAll()
|
||||
{
|
||||
m_BanAddrPool.Reset();
|
||||
m_BanRangePool.Reset();
|
||||
}
|
||||
|
||||
bool CNetBan::IsBanned(const NETADDR *pAddr, char *pBuf, unsigned BufferSize) const
|
||||
{
|
||||
CNetHash aHash[17];
|
||||
int Length = CNetHash::MakeHashArray(pAddr, aHash);
|
||||
|
||||
// check ban adresses
|
||||
CBanAddr *pBan = m_BanAddrPool.Find(pAddr, &aHash[Length]);
|
||||
if(pBan)
|
||||
{
|
||||
MakeBanInfo(pBan, pBuf, BufferSize, MSGTYPE_PLAYER);
|
||||
return true;
|
||||
}
|
||||
|
||||
// check ban ranges
|
||||
for(int i = Length-1; i >= 0; --i)
|
||||
{
|
||||
for(CBanRange *pBan = m_BanRangePool.First(&aHash[i]); pBan; pBan = pBan->m_pHashNext)
|
||||
{
|
||||
if(NetMatch(&pBan->m_Data, pAddr, i, Length))
|
||||
{
|
||||
MakeBanInfo(pBan, pBuf, BufferSize, MSGTYPE_PLAYER);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@ -0,0 +1,179 @@
|
||||
#ifndef NETBAN_H
|
||||
#define NETBAN_H
|
||||
|
||||
#include <system.h>
|
||||
|
||||
inline int NetComp(const NETADDR *pAddr1, const NETADDR *pAddr2)
|
||||
{
|
||||
return mem_comp(pAddr1, pAddr2, pAddr1->type==NETTYPE_IPV4 ? 8 : 20);
|
||||
}
|
||||
|
||||
class CNetRange
|
||||
{
|
||||
public:
|
||||
NETADDR m_LB;
|
||||
NETADDR m_UB;
|
||||
|
||||
bool IsValid() const { return m_LB.type == m_UB.type && NetComp(&m_LB, &m_UB) < 0; }
|
||||
};
|
||||
|
||||
inline int NetComp(const CNetRange *pRange1, const CNetRange *pRange2)
|
||||
{
|
||||
return NetComp(&pRange1->m_LB, &pRange2->m_LB) || NetComp(&pRange1->m_UB, &pRange2->m_UB);
|
||||
}
|
||||
|
||||
|
||||
class CNetBan
|
||||
{
|
||||
protected:
|
||||
bool NetMatch(const NETADDR *pAddr1, const NETADDR *pAddr2) const
|
||||
{
|
||||
return NetComp(pAddr1, pAddr2) == 0;
|
||||
}
|
||||
|
||||
bool NetMatch(const CNetRange *pRange, const NETADDR *pAddr, int Start, int Length) const
|
||||
{
|
||||
return pRange->m_LB.type == pAddr->type && (Start == 0 || mem_comp(&pRange->m_LB.ip[0], &pAddr->ip[0], Start) == 0) &&
|
||||
mem_comp(&pRange->m_LB.ip[Start], &pAddr->ip[Start], Length-Start) <= 0 && mem_comp(&pRange->m_UB.ip[Start], &pAddr->ip[Start], Length-Start) >= 0;
|
||||
}
|
||||
|
||||
bool NetMatch(const CNetRange *pRange, const NETADDR *pAddr) const
|
||||
{
|
||||
return NetMatch(pRange, pAddr, 0, pRange->m_LB.type==NETTYPE_IPV4 ? 4 : 16);
|
||||
}
|
||||
|
||||
const char *NetToString(const NETADDR *pData, char *pBuffer, unsigned BufferSize) const
|
||||
{
|
||||
char aAddrStr[NETADDR_MAXSTRSIZE];
|
||||
net_addr_str(pData, aAddrStr, sizeof(aAddrStr), false);
|
||||
str_format(pBuffer, BufferSize, "'%s'", aAddrStr);
|
||||
return pBuffer;
|
||||
}
|
||||
|
||||
const char *NetToString(const CNetRange *pData, char *pBuffer, unsigned BufferSize) const
|
||||
{
|
||||
char aAddrStr1[NETADDR_MAXSTRSIZE], aAddrStr2[NETADDR_MAXSTRSIZE];
|
||||
net_addr_str(&pData->m_LB, aAddrStr1, sizeof(aAddrStr1), false);
|
||||
net_addr_str(&pData->m_UB, aAddrStr2, sizeof(aAddrStr2), false);
|
||||
str_format(pBuffer, BufferSize, "'%s' - '%s'", aAddrStr1, aAddrStr2);
|
||||
return pBuffer;
|
||||
}
|
||||
|
||||
// todo: move?
|
||||
static bool StrAllnum(const char *pStr);
|
||||
|
||||
class CNetHash
|
||||
{
|
||||
public:
|
||||
int m_Hash;
|
||||
int m_HashIndex; // matching parts for ranges, 0 for addr
|
||||
|
||||
CNetHash() {}
|
||||
CNetHash(const NETADDR *pAddr);
|
||||
CNetHash(const CNetRange *pRange);
|
||||
|
||||
static int MakeHashArray(const NETADDR *pAddr, CNetHash aHash[17]);
|
||||
};
|
||||
|
||||
struct CBanInfo
|
||||
{
|
||||
enum
|
||||
{
|
||||
EXPIRES_NEVER=-1,
|
||||
REASON_LENGTH=64,
|
||||
};
|
||||
int m_Expires;
|
||||
char m_aReason[REASON_LENGTH];
|
||||
};
|
||||
|
||||
template<class T> struct CBan
|
||||
{
|
||||
T m_Data;
|
||||
CBanInfo m_Info;
|
||||
CNetHash m_NetHash;
|
||||
|
||||
// hash list
|
||||
CBan *m_pHashNext;
|
||||
CBan *m_pHashPrev;
|
||||
|
||||
// used or free list
|
||||
CBan *m_pNext;
|
||||
CBan *m_pPrev;
|
||||
};
|
||||
|
||||
template<class T, int HashCount> class CBanPool
|
||||
{
|
||||
public:
|
||||
typedef T CDataType;
|
||||
|
||||
CBan<CDataType> *Add(const CDataType *pData, const CBanInfo *pInfo, const CNetHash *pNetHash);
|
||||
int Remove(CBan<CDataType> *pBan);
|
||||
void Update(CBan<CDataType> *pBan, const CBanInfo *pInfo);
|
||||
void Reset();
|
||||
|
||||
int Num() const { return m_CountUsed; }
|
||||
bool IsFull() const { return m_CountUsed == MAX_BANS; }
|
||||
|
||||
CBan<CDataType> *First() const { return m_pFirstUsed; }
|
||||
CBan<CDataType> *First(const CNetHash *pNetHash) const { return m_paaHashList[pNetHash->m_HashIndex][pNetHash->m_Hash]; }
|
||||
CBan<CDataType> *Find(const CDataType *pData, const CNetHash *pNetHash) const
|
||||
{
|
||||
for(CBan<CDataType> *pBan = m_paaHashList[pNetHash->m_HashIndex][pNetHash->m_Hash]; pBan; pBan = pBan->m_pHashNext)
|
||||
{
|
||||
if(NetComp(&pBan->m_Data, pData) == 0)
|
||||
return pBan;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
CBan<CDataType> *Get(int Index) const;
|
||||
|
||||
private:
|
||||
enum
|
||||
{
|
||||
MAX_BANS=1024,
|
||||
};
|
||||
|
||||
CBan<CDataType> *m_paaHashList[HashCount][256];
|
||||
CBan<CDataType> m_aBans[MAX_BANS];
|
||||
CBan<CDataType> *m_pFirstFree;
|
||||
CBan<CDataType> *m_pFirstUsed;
|
||||
int m_CountUsed;
|
||||
};
|
||||
|
||||
typedef CBanPool<NETADDR, 1> CBanAddrPool;
|
||||
typedef CBanPool<CNetRange, 16> CBanRangePool;
|
||||
typedef CBan<NETADDR> CBanAddr;
|
||||
typedef CBan<CNetRange> CBanRange;
|
||||
|
||||
template<class T> void MakeBanInfo(const CBan<T> *pBan, char *pBuf, unsigned BuffSize, int Type) const;
|
||||
template<class T> int Ban(T *pBanPool, const typename T::CDataType *pData, int Seconds, const char *pReason);
|
||||
template<class T> int Unban(T *pBanPool, const typename T::CDataType *pData);
|
||||
|
||||
CBanAddrPool m_BanAddrPool;
|
||||
CBanRangePool m_BanRangePool;
|
||||
NETADDR m_LocalhostIPV4, m_LocalhostIPV6;
|
||||
|
||||
public:
|
||||
enum
|
||||
{
|
||||
MSGTYPE_PLAYER=0,
|
||||
MSGTYPE_LIST,
|
||||
MSGTYPE_BANADD,
|
||||
MSGTYPE_BANREM,
|
||||
};
|
||||
|
||||
virtual ~CNetBan() {}
|
||||
void Init();
|
||||
void Update();
|
||||
|
||||
virtual int BanAddr(const NETADDR *pAddr, int Seconds, const char *pReason);
|
||||
virtual int BanRange(const CNetRange *pRange, int Seconds, const char *pReason);
|
||||
int UnbanByAddr(const NETADDR *pAddr);
|
||||
int UnbanByRange(const CNetRange *pRange);
|
||||
int UnbanByIndex(int Index);
|
||||
void UnbanAll();
|
||||
bool IsBanned(const NETADDR *pAddr, char *pBuf, unsigned BufferSize) const;
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -0,0 +1,144 @@
|
||||
#include <system.h>
|
||||
#include "netban.h"
|
||||
#include "network.h"
|
||||
|
||||
bool CNetwork::Open(NETADDR BindAddr, CNetBan *pNetBan)
|
||||
{
|
||||
// zero out the whole structure
|
||||
mem_zero(this, sizeof(*this));
|
||||
m_Socket.type = NETTYPE_INVALID;
|
||||
m_Socket.ipv4sock = -1;
|
||||
m_Socket.ipv6sock = -1;
|
||||
m_pNetBan = pNetBan;
|
||||
|
||||
// open socket
|
||||
m_Socket = net_tcp_create(BindAddr);
|
||||
if(!m_Socket.type)
|
||||
return false;
|
||||
if(net_tcp_listen(m_Socket, NET_MAX_CLIENTS))
|
||||
return false;
|
||||
net_set_non_blocking(m_Socket);
|
||||
|
||||
for(int i = 0; i < NET_MAX_CLIENTS; i++)
|
||||
m_aSlots[i].m_Connection.Reset();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void CNetwork::SetCallbacks(NETFUNC_NEWCLIENT pfnNewClient, NETFUNC_DELCLIENT pfnDelClient, void *pUser)
|
||||
{
|
||||
m_pfnNewClient = pfnNewClient;
|
||||
m_pfnDelClient = pfnDelClient;
|
||||
m_UserPtr = pUser;
|
||||
}
|
||||
|
||||
int CNetwork::Close()
|
||||
{
|
||||
for(int i = 0; i < NET_MAX_CLIENTS; i++)
|
||||
m_aSlots[i].m_Connection.Disconnect("Closing connection.");
|
||||
|
||||
net_tcp_close(m_Socket);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CNetwork::Drop(int ClientID, const char *pReason)
|
||||
{
|
||||
if(m_pfnDelClient)
|
||||
m_pfnDelClient(ClientID, pReason, m_UserPtr);
|
||||
|
||||
m_aSlots[ClientID].m_Connection.Disconnect(pReason);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CNetwork::AcceptClient(NETSOCKET Socket, const NETADDR *pAddr)
|
||||
{
|
||||
char aError[256] = { 0 };
|
||||
int FreeSlot = -1;
|
||||
|
||||
// look for free slot or multiple client
|
||||
for(int i = 0; i < NET_MAX_CLIENTS; i++)
|
||||
{
|
||||
if(FreeSlot == -1 && m_aSlots[i].m_Connection.State() == NET_CONNSTATE_OFFLINE)
|
||||
FreeSlot = i;
|
||||
if(m_aSlots[i].m_Connection.State() != NET_CONNSTATE_OFFLINE)
|
||||
{
|
||||
if(net_addr_comp(pAddr, m_aSlots[i].m_Connection.PeerAddress()) == 0)
|
||||
{
|
||||
str_copy(aError, "Only one client per IP allowed.", sizeof(aError));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// accept client
|
||||
if(!aError[0] && FreeSlot != -1)
|
||||
{
|
||||
m_aSlots[FreeSlot].m_Connection.Init(Socket, pAddr);
|
||||
if(m_pfnNewClient)
|
||||
m_pfnNewClient(FreeSlot, m_UserPtr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// reject client
|
||||
if(!aError[0])
|
||||
str_copy(aError, "No free slot available.", sizeof(aError));
|
||||
|
||||
net_tcp_send(Socket, aError, str_length(aError));
|
||||
net_tcp_close(Socket);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int CNetwork::Update()
|
||||
{
|
||||
NETSOCKET Socket;
|
||||
NETADDR Addr;
|
||||
|
||||
if(net_tcp_accept(m_Socket, &Socket, &Addr) > 0)
|
||||
{
|
||||
// check if we should just drop the packet
|
||||
char aBuf[128];
|
||||
if(NetBan() && NetBan()->IsBanned(&Addr, aBuf, sizeof(aBuf)))
|
||||
{
|
||||
// banned, reply with a message and drop
|
||||
net_tcp_send(Socket, aBuf, str_length(aBuf));
|
||||
net_tcp_close(Socket);
|
||||
}
|
||||
else
|
||||
AcceptClient(Socket, &Addr);
|
||||
}
|
||||
|
||||
for(int i = 0; i < NET_MAX_CLIENTS; i++)
|
||||
{
|
||||
if(m_aSlots[i].m_Connection.State() == NET_CONNSTATE_ONLINE)
|
||||
m_aSlots[i].m_Connection.Update();
|
||||
if(m_aSlots[i].m_Connection.State() == NET_CONNSTATE_ERROR)
|
||||
Drop(i, m_aSlots[i].m_Connection.ErrorString());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CNetwork::Recv(char *pLine, int MaxLength, int *pClientID)
|
||||
{
|
||||
for(int i = 0; i < NET_MAX_CLIENTS; i++)
|
||||
{
|
||||
if(m_aSlots[i].m_Connection.State() == NET_CONNSTATE_ONLINE && m_aSlots[i].m_Connection.Recv(pLine, MaxLength))
|
||||
{
|
||||
if(pClientID)
|
||||
*pClientID = i;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CNetwork::Send(int ClientID, const char *pLine)
|
||||
{
|
||||
if(m_aSlots[ClientID].m_Connection.State() == NET_CONNSTATE_ONLINE)
|
||||
return m_aSlots[ClientID].m_Connection.Send(pLine);
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
@ -0,0 +1,87 @@
|
||||
#ifndef NETWORK_H
|
||||
#define NETWORK_H
|
||||
|
||||
enum
|
||||
{
|
||||
NET_CONNSTATE_OFFLINE=0,
|
||||
NET_CONNSTATE_CONNECT=1,
|
||||
NET_CONNSTATE_PENDING=2,
|
||||
NET_CONNSTATE_ONLINE=3,
|
||||
NET_CONNSTATE_ERROR=4,
|
||||
|
||||
NET_MAX_PACKETSIZE = 1400,
|
||||
NET_MAX_CLIENTS = 32
|
||||
};
|
||||
|
||||
typedef int (*NETFUNC_DELCLIENT)(int ClientID, const char* pReason, void *pUser);
|
||||
typedef int (*NETFUNC_NEWCLIENT)(int ClientID, void *pUser);
|
||||
|
||||
class CNetworkClient
|
||||
{
|
||||
private:
|
||||
int m_State;
|
||||
|
||||
NETADDR m_PeerAddr;
|
||||
NETSOCKET m_Socket;
|
||||
|
||||
char m_aBuffer[NET_MAX_PACKETSIZE];
|
||||
int m_BufferOffset;
|
||||
|
||||
char m_aErrorString[256];
|
||||
|
||||
bool m_LineEndingDetected;
|
||||
char m_aLineEnding[3];
|
||||
|
||||
public:
|
||||
void Init(NETSOCKET Socket, const NETADDR *pAddr);
|
||||
void Disconnect(const char *pReason);
|
||||
|
||||
int State() const { return m_State; }
|
||||
const NETADDR *PeerAddress() const { return &m_PeerAddr; }
|
||||
const char *ErrorString() const { return m_aErrorString; }
|
||||
|
||||
void Reset();
|
||||
int Update();
|
||||
int Send(const char *pLine);
|
||||
int Recv(char *pLine, int MaxLength);
|
||||
};
|
||||
|
||||
class CNetwork
|
||||
{
|
||||
private:
|
||||
struct CSlot
|
||||
{
|
||||
CNetworkClient m_Connection;
|
||||
};
|
||||
|
||||
NETSOCKET m_Socket;
|
||||
class CNetBan *m_pNetBan;
|
||||
CSlot m_aSlots[NET_MAX_CLIENTS];
|
||||
|
||||
NETFUNC_NEWCLIENT m_pfnNewClient;
|
||||
NETFUNC_DELCLIENT m_pfnDelClient;
|
||||
void *m_UserPtr;
|
||||
|
||||
public:
|
||||
void SetCallbacks(NETFUNC_NEWCLIENT pfnNewClient, NETFUNC_DELCLIENT pfnDelClient, void *pUser);
|
||||
|
||||
//
|
||||
bool Open(NETADDR BindAddr, CNetBan *pNetBan);
|
||||
int Close();
|
||||
|
||||
//
|
||||
int Recv(char *pLine, int MaxLength, int *pClientID = 0);
|
||||
int Send(int ClientID, const char *pLine);
|
||||
int Update();
|
||||
|
||||
//
|
||||
int AcceptClient(NETSOCKET Socket, const NETADDR *pAddr);
|
||||
int Drop(int ClientID, const char *pReason);
|
||||
|
||||
// status requests
|
||||
const NETADDR *ClientAddr(int ClientID) const { return m_aSlots[ClientID].m_Connection.PeerAddress(); }
|
||||
const NETSOCKET *Socket() const { return &m_Socket; }
|
||||
class CNetBan *NetBan() const { return m_pNetBan; }
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -0,0 +1,184 @@
|
||||
#include <system.h>
|
||||
#include "network.h"
|
||||
|
||||
void CNetworkClient::Reset()
|
||||
{
|
||||
m_State = NET_CONNSTATE_OFFLINE;
|
||||
mem_zero(&m_PeerAddr, sizeof(m_PeerAddr));
|
||||
m_aErrorString[0] = 0;
|
||||
|
||||
m_Socket.type = NETTYPE_INVALID;
|
||||
m_Socket.ipv4sock = -1;
|
||||
m_Socket.ipv6sock = -1;
|
||||
m_aBuffer[0] = 0;
|
||||
m_BufferOffset = 0;
|
||||
|
||||
m_LineEndingDetected = false;
|
||||
#if defined(CONF_FAMILY_WINDOWS)
|
||||
m_aLineEnding[0] = '\r';
|
||||
m_aLineEnding[1] = '\n';
|
||||
m_aLineEnding[2] = 0;
|
||||
#else
|
||||
m_aLineEnding[0] = '\n';
|
||||
m_aLineEnding[1] = 0;
|
||||
m_aLineEnding[2] = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void CNetworkClient::Init(NETSOCKET Socket, const NETADDR *pAddr)
|
||||
{
|
||||
Reset();
|
||||
|
||||
m_Socket = Socket;
|
||||
net_set_non_blocking(m_Socket);
|
||||
|
||||
m_PeerAddr = *pAddr;
|
||||
m_State = NET_CONNSTATE_ONLINE;
|
||||
}
|
||||
|
||||
void CNetworkClient::Disconnect(const char *pReason)
|
||||
{
|
||||
if(State() == NET_CONNSTATE_OFFLINE)
|
||||
return;
|
||||
|
||||
if(pReason && pReason[0])
|
||||
Send(pReason);
|
||||
|
||||
net_tcp_close(m_Socket);
|
||||
|
||||
Reset();
|
||||
}
|
||||
|
||||
int CNetworkClient::Update()
|
||||
{
|
||||
if(State() == NET_CONNSTATE_ONLINE)
|
||||
{
|
||||
if((int)(sizeof(m_aBuffer)) <= m_BufferOffset)
|
||||
{
|
||||
m_State = NET_CONNSTATE_ERROR;
|
||||
str_copy(m_aErrorString, "too weak connection (out of buffer)", sizeof(m_aErrorString));
|
||||
return -1;
|
||||
}
|
||||
|
||||
int Bytes = net_tcp_recv(m_Socket, m_aBuffer+m_BufferOffset, (int)(sizeof(m_aBuffer))-m_BufferOffset);
|
||||
|
||||
if(Bytes > 0)
|
||||
{
|
||||
m_BufferOffset += Bytes;
|
||||
}
|
||||
else if(Bytes < 0)
|
||||
{
|
||||
if(net_would_block()) // no data received
|
||||
return 0;
|
||||
|
||||
m_State = NET_CONNSTATE_ERROR; // error
|
||||
str_copy(m_aErrorString, "connection failure", sizeof(m_aErrorString));
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_State = NET_CONNSTATE_ERROR;
|
||||
str_copy(m_aErrorString, "remote end closed the connection", sizeof(m_aErrorString));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CNetworkClient::Recv(char *pLine, int MaxLength)
|
||||
{
|
||||
if(State() == NET_CONNSTATE_ONLINE)
|
||||
{
|
||||
if(m_BufferOffset)
|
||||
{
|
||||
// find message start
|
||||
int StartOffset = 0;
|
||||
while(m_aBuffer[StartOffset] == '\r' || m_aBuffer[StartOffset] == '\n')
|
||||
{
|
||||
// detect clients line ending format
|
||||
if(!m_LineEndingDetected)
|
||||
{
|
||||
m_aLineEnding[0] = m_aBuffer[StartOffset];
|
||||
if(StartOffset+1 < m_BufferOffset && (m_aBuffer[StartOffset+1] == '\r' || m_aBuffer[StartOffset+1] == '\n') &&
|
||||
m_aBuffer[StartOffset] != m_aBuffer[StartOffset+1])
|
||||
m_aLineEnding[1] = m_aBuffer[StartOffset+1];
|
||||
m_LineEndingDetected = true;
|
||||
}
|
||||
|
||||
if(++StartOffset >= m_BufferOffset)
|
||||
{
|
||||
m_BufferOffset = 0;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// find message end
|
||||
int EndOffset = StartOffset;
|
||||
while(m_aBuffer[EndOffset] != '\r' && m_aBuffer[EndOffset] != '\n')
|
||||
{
|
||||
if(++EndOffset >= m_BufferOffset)
|
||||
{
|
||||
if(StartOffset > 0)
|
||||
{
|
||||
mem_move(m_aBuffer, m_aBuffer+StartOffset, m_BufferOffset-StartOffset);
|
||||
m_BufferOffset -= StartOffset;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// extract message and update buffer
|
||||
if(MaxLength-1 < EndOffset-StartOffset)
|
||||
{
|
||||
if(StartOffset > 0)
|
||||
{
|
||||
mem_move(m_aBuffer, m_aBuffer+StartOffset, m_BufferOffset-StartOffset);
|
||||
m_BufferOffset -= StartOffset;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
mem_copy(pLine, m_aBuffer+StartOffset, EndOffset-StartOffset);
|
||||
pLine[EndOffset-StartOffset] = 0;
|
||||
str_sanitize_cc(pLine);
|
||||
mem_move(m_aBuffer, m_aBuffer+EndOffset, m_BufferOffset-EndOffset);
|
||||
m_BufferOffset -= EndOffset;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CNetworkClient::Send(const char *pLine)
|
||||
{
|
||||
if(State() != NET_CONNSTATE_ONLINE)
|
||||
return -1;
|
||||
|
||||
char aBuf[1024];
|
||||
str_copy(aBuf, pLine, (int)(sizeof(aBuf))-2);
|
||||
int Length = str_length(aBuf);
|
||||
aBuf[Length] = m_aLineEnding[0];
|
||||
aBuf[Length+1] = m_aLineEnding[1];
|
||||
aBuf[Length+2] = m_aLineEnding[2];
|
||||
Length += 3;
|
||||
const char *pData = aBuf;
|
||||
|
||||
while(1)
|
||||
{
|
||||
int Send = net_tcp_send(m_Socket, pData, Length);
|
||||
if(Send < 0)
|
||||
{
|
||||
m_State = NET_CONNSTATE_ERROR;
|
||||
str_copy(m_aErrorString, "failed to send packet", sizeof(m_aErrorString));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(Send >= Length)
|
||||
break;
|
||||
|
||||
pData += Send;
|
||||
Length -= Send;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,204 @@
|
||||
#include <system.h>
|
||||
#include "netban.h"
|
||||
#include "network.h"
|
||||
#include "main.h"
|
||||
#include "server.h"
|
||||
|
||||
int CServer::NewClientCallback(int ClientID, void *pUser)
|
||||
{
|
||||
CServer *pThis = (CServer *)pUser;
|
||||
|
||||
char aAddrStr[NETADDR_MAXSTRSIZE];
|
||||
net_addr_str(pThis->m_Network.ClientAddr(ClientID), aAddrStr, sizeof(aAddrStr), true);
|
||||
if(pThis->Main()->Config()->m_Verbose)
|
||||
dbg_msg("server", "Connection accepted. ncid=%d addr=%s'", ClientID, aAddrStr);
|
||||
|
||||
pThis->m_aClients[ClientID].m_State = CClient::STATE_CONNECTED;
|
||||
pThis->m_aClients[ClientID].m_TimeConnected = time_get();
|
||||
pThis->m_Network.Send(ClientID, "Authentication required:");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CServer::DelClientCallback(int ClientID, const char *pReason, void *pUser)
|
||||
{
|
||||
CServer *pThis = (CServer *)pUser;
|
||||
|
||||
char aAddrStr[NETADDR_MAXSTRSIZE];
|
||||
net_addr_str(pThis->m_Network.ClientAddr(ClientID), aAddrStr, sizeof(aAddrStr), true);
|
||||
if(pThis->Main()->Config()->m_Verbose)
|
||||
dbg_msg("server", "Client dropped. ncid=%d addr=%s reason='%s'", ClientID, aAddrStr, pReason);
|
||||
|
||||
if(pThis->m_aClients[ClientID].m_State == CClient::STATE_AUTHED)
|
||||
pThis->Main()->OnDelClient(ClientID);
|
||||
pThis->m_aClients[ClientID].m_State = CClient::STATE_EMPTY;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CServer::Init(CMain *pMain, const char *Bind, int Port)
|
||||
{
|
||||
m_pMain = pMain;
|
||||
m_NetBan.Init();
|
||||
|
||||
for(int i = 0; i < NET_MAX_CLIENTS; i++)
|
||||
m_aClients[i].m_State = CClient::STATE_EMPTY;
|
||||
|
||||
m_Ready = false;
|
||||
|
||||
if(Port == 0)
|
||||
{
|
||||
dbg_msg("server", "Will not bind to port 0.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
NETADDR BindAddr;
|
||||
if(Bind[0] && net_host_lookup(Bind, &BindAddr, NETTYPE_ALL) == 0)
|
||||
{
|
||||
// got bindaddr
|
||||
BindAddr.type = NETTYPE_ALL;
|
||||
BindAddr.port = Port;
|
||||
}
|
||||
else
|
||||
{
|
||||
mem_zero(&BindAddr, sizeof(BindAddr));
|
||||
BindAddr.type = NETTYPE_ALL;
|
||||
BindAddr.port = Port;
|
||||
}
|
||||
|
||||
if(m_Network.Open(BindAddr, &m_NetBan))
|
||||
{
|
||||
m_Network.SetCallbacks(NewClientCallback, DelClientCallback, this);
|
||||
m_Ready = true;
|
||||
dbg_msg("server", "Bound to %s:%d", Bind, Port);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
dbg_msg("server", "Couldn't open socket. Port (%d) might already be in use.", Port);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void CServer::Update()
|
||||
{
|
||||
if(!m_Ready)
|
||||
return;
|
||||
|
||||
m_NetBan.Update();
|
||||
m_Network.Update();
|
||||
|
||||
char aBuf[NET_MAX_PACKETSIZE];
|
||||
int ClientID;
|
||||
|
||||
while(m_Network.Recv(aBuf, (int)(sizeof(aBuf))-1, &ClientID))
|
||||
{
|
||||
dbg_assert(m_aClients[ClientID].m_State != CClient::STATE_EMPTY, "Got message from empty slot.");
|
||||
if(m_aClients[ClientID].m_State == CClient::STATE_CONNECTED)
|
||||
{
|
||||
int ID = -1;
|
||||
char aUsername[128] = {0};
|
||||
char aPassword[128] = {0};
|
||||
const char *pTmp;
|
||||
|
||||
if(!(pTmp = str_find(aBuf, ":"))
|
||||
|| (unsigned)(pTmp - aBuf) > sizeof(aUsername) || (unsigned)(str_length(pTmp) - 1) > sizeof(aPassword))
|
||||
{
|
||||
m_Network.NetBan()->BanAddr(m_Network.ClientAddr(ClientID), 60, "You're an idiot, go away.");
|
||||
m_Network.Drop(ClientID, "Fuck off.");
|
||||
return;
|
||||
}
|
||||
|
||||
str_copy(aUsername, aBuf, pTmp - aBuf + 1);
|
||||
str_copy(aPassword, pTmp + 1, sizeof(aPassword));
|
||||
if(!*aUsername || !*aPassword)
|
||||
{
|
||||
m_Network.NetBan()->BanAddr(m_Network.ClientAddr(ClientID), 60, "You're an idiot, go away.");
|
||||
m_Network.Drop(ClientID, "Username and password must not be blank.");
|
||||
return;
|
||||
}
|
||||
|
||||
for(int i = 0; i < NET_MAX_CLIENTS; i++)
|
||||
{
|
||||
if(!Main()->Client(i)->m_Active)
|
||||
continue;
|
||||
|
||||
if(str_comp(Main()->Client(i)->m_aUsername, aUsername) == 0 && str_comp(Main()->Client(i)->m_aPassword, aPassword) == 0)
|
||||
ID = i;
|
||||
}
|
||||
|
||||
if(ID == -1)
|
||||
{
|
||||
m_Network.NetBan()->BanAddr(m_Network.ClientAddr(ClientID), 60, "Wrong username and/or password.");
|
||||
m_Network.Drop(ClientID, "Wrong username and/or password.");
|
||||
}
|
||||
else if(Main()->Client(ID)->m_ClientNetID != -1)
|
||||
{
|
||||
m_Network.Drop(ClientID, "Only one connection per user allowed.");
|
||||
}
|
||||
else
|
||||
{
|
||||
m_aClients[ClientID].m_State = CClient::STATE_AUTHED;
|
||||
m_aClients[ClientID].m_LastReceived = time_get();
|
||||
m_Network.Send(ClientID, "Authentication successful. Access granted.");
|
||||
|
||||
if(m_Network.ClientAddr(ClientID)->type == NETTYPE_IPV4)
|
||||
m_Network.Send(ClientID, "You are connecting via: IPv4");
|
||||
else if(m_Network.ClientAddr(ClientID)->type == NETTYPE_IPV6)
|
||||
m_Network.Send(ClientID, "You are connecting via: IPv6");
|
||||
|
||||
if(Main()->Config()->m_Verbose)
|
||||
dbg_msg("server", "ncid=%d authed", ClientID);
|
||||
Main()->OnNewClient(ClientID, ID);
|
||||
}
|
||||
}
|
||||
else if(m_aClients[ClientID].m_State == CClient::STATE_AUTHED)
|
||||
{
|
||||
m_aClients[ClientID].m_LastReceived = time_get();
|
||||
if(Main()->Config()->m_Verbose)
|
||||
dbg_msg("server", "ncid=%d cmd='%s'", ClientID, aBuf);
|
||||
|
||||
if(str_comp(aBuf, "logout") == 0)
|
||||
m_Network.Drop(ClientID, "Logout. Bye Bye ~");
|
||||
else
|
||||
Main()->HandleMessage(ClientID, aBuf);
|
||||
}
|
||||
}
|
||||
|
||||
for(int i = 0; i < NET_MAX_CLIENTS; ++i)
|
||||
{
|
||||
if(m_aClients[i].m_State == CClient::STATE_CONNECTED &&
|
||||
time_get() > m_aClients[i].m_TimeConnected + 5 * time_freq())
|
||||
{
|
||||
m_Network.NetBan()->BanAddr(m_Network.ClientAddr(i), 30, "Authentication timeout.");
|
||||
m_Network.Drop(i, "Authentication timeout.");
|
||||
}
|
||||
else if(m_aClients[i].m_State == CClient::STATE_AUTHED &&
|
||||
time_get() > m_aClients[i].m_LastReceived + 15 * time_freq())
|
||||
m_Network.Drop(i, "Timeout.");
|
||||
}
|
||||
}
|
||||
|
||||
void CServer::Send(int ClientID, const char *pLine)
|
||||
{
|
||||
if(!m_Ready)
|
||||
return;
|
||||
|
||||
if(ClientID == -1)
|
||||
{
|
||||
for(int i = 0; i < NET_MAX_CLIENTS; i++)
|
||||
{
|
||||
if(m_aClients[i].m_State == CClient::STATE_AUTHED)
|
||||
m_Network.Send(i, pLine);
|
||||
}
|
||||
}
|
||||
else if(ClientID >= 0 && ClientID < NET_MAX_CLIENTS && m_aClients[ClientID].m_State == CClient::STATE_AUTHED)
|
||||
m_Network.Send(ClientID, pLine);
|
||||
}
|
||||
|
||||
void CServer::Shutdown()
|
||||
{
|
||||
if(!m_Ready)
|
||||
return;
|
||||
|
||||
m_Network.Close();
|
||||
}
|
||||
@ -0,0 +1,46 @@
|
||||
#ifndef SERVER_H
|
||||
#define SERVER_H
|
||||
|
||||
#include "netban.h"
|
||||
#include "network.h"
|
||||
|
||||
class CServer
|
||||
{
|
||||
class CClient
|
||||
{
|
||||
public:
|
||||
enum
|
||||
{
|
||||
STATE_EMPTY=0,
|
||||
STATE_CONNECTED,
|
||||
STATE_AUTHED,
|
||||
};
|
||||
|
||||
int m_State;
|
||||
int64 m_TimeConnected;
|
||||
int64 m_LastReceived;
|
||||
};
|
||||
CClient m_aClients[NET_MAX_CLIENTS];
|
||||
|
||||
CNetwork m_Network;
|
||||
CNetBan m_NetBan;
|
||||
|
||||
class CMain *m_pMain;
|
||||
|
||||
bool m_Ready;
|
||||
|
||||
static int NewClientCallback(int ClientID, void *pUser);
|
||||
static int DelClientCallback(int ClientID, const char *pReason, void *pUser);
|
||||
|
||||
public:
|
||||
int Init(CMain *pMain, const char *Bind, int Port);
|
||||
void Update();
|
||||
void Send(int ClientID, const char *pLine);
|
||||
void Shutdown();
|
||||
|
||||
CNetwork *Network() { return &m_Network; }
|
||||
CNetBan *NetBan() { return &m_NetBan; }
|
||||
CMain *Main() { return m_pMain; }
|
||||
};
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,55 @@
|
||||
body { background: #ebebeb url('../img/light.png'); }
|
||||
.announcement { color: #777; border-bottom: solid 3px #d0d0d0; background-color: #fff; padding: 10px 10px; text-align: center; transition: 0.3s; }
|
||||
.announcement:hover { border-bottom: solid 3px #159e83; transition: 0.3s; }
|
||||
.announcement p { display: inline-block;font-size: 15px;margin: 0;line-height: 1;text-indent: 5px; }
|
||||
.announcement i { display: inline-block;font-size: 15px;margin: 0;line-height: 1;color: #444; }
|
||||
.navbar { min-height: 50px; }
|
||||
.navbar-top {background-color: #4a4a4a !important;/* border-bottom: 6px solid #159e83; */-moz-box-shadow: 0 -4px 0 rgba(0,0,0,.1);/* box-shadow: 0 6px 0 rgba(0,0,0,.1); */}
|
||||
.navbar-brand { color: #fff; padding: 10px; font-size: 20px; }
|
||||
.dropdown .dropdown-toggle { padding-bottom: 10px; padding-top: 10px; }
|
||||
.navbar-inverse .navbar-brand { color: #fff; padding: 15px 20px 10px; font-size: 20px; }
|
||||
.content { background: #ffffff; padding: 20px; border: 1px #cecece solid; -webkit-box-shadow: 0 1px 10px rgba(0, 0, 0, .1); -moz-box-shadow: 0 1px 10px rgba(0, 0, 0, .1); box-shadow: 0 1px 10px rgba(0, 0, 0, .1); margin-bottom: 20px; margin-top: 20px; }
|
||||
.table { background: #ffffff; margin-bottom: 0; border-collapse: collapse; border-radius: 3px; }
|
||||
.table th, .table td { text-align: center; }
|
||||
.table-striped tbody > tr.even > td, .table-striped tbody > tr.even > th { background-color: #F9F9F9; }
|
||||
.table-striped tbody > tr.odd > td, .table-striped tbody > tr.odd > th { background-color: #FFF; }
|
||||
.progress { margin-bottom: 0; }
|
||||
.progress-bar { color: #000; }
|
||||
.table-hover > tbody > tr:hover > td { background: #E6E6E6; }
|
||||
tr.even.expandRow > :hover { background: #F9F9F9 !important; }
|
||||
tr.odd.expandRow > :hover { background: #FFF !important; }
|
||||
.expandRow > td { padding: 0 !important; border-top: 0px !important; }
|
||||
#cpu, #ram, #hdd, #network, #traffic { min-width: 55px; max-width: 100px; }
|
||||
|
||||
@media only screen and (max-width: 992px) {
|
||||
#location, tr td:nth-child(4) { display:none; visibility:hidden; }
|
||||
}
|
||||
@media only screen and (max-width: 720px) {
|
||||
#type, tr td:nth-child(3) { display:none; visibility:hidden; }
|
||||
#location, tr td:nth-child(4) { display:none; visibility:hidden; }
|
||||
#uptime, tr td:nth-child(5) { display:none; visibility:hidden; }
|
||||
}
|
||||
@media only screen and (max-width: 600px) {
|
||||
#type, tr td:nth-child(3) { display:none; visibility:hidden; }
|
||||
#location, tr td:nth-child(4) { display:none; visibility:hidden; }
|
||||
#uptime, tr td:nth-child(5) { display:none; visibility:hidden; }
|
||||
#load, tr td:nth-child(6) { display:none; visibility:hidden; }
|
||||
}
|
||||
@media only screen and (max-width: 533px) {
|
||||
#type, tr td:nth-child(3) { display:none; visibility:hidden; }
|
||||
#location, tr td:nth-child(4) { display:none; visibility:hidden; }
|
||||
#uptime, tr td:nth-child(5) { display:none; visibility:hidden; }
|
||||
#traffic, tr td:nth-child(8) { display:none; visibility:hidden; }
|
||||
#load, tr td:nth-child(6) { display:none; visibility:hidden; }
|
||||
}
|
||||
@media only screen and (max-width: 450px) {
|
||||
body { font-size: 10px; }
|
||||
.content { padding: 0; }
|
||||
#name, tr td:nth-child(2) { min-width: 20px; max-width: 60px; text-overflow: ellipsis; white-space: nowrap; overflow: hidden; }
|
||||
#type, tr td:nth-child(3) { display:none; visibility:hidden; }
|
||||
#location, tr td:nth-child(4) { display:none; visibility:hidden; }
|
||||
#uptime, tr td:nth-child(5) { display:none; visibility:hidden; }
|
||||
#traffic, tr td:nth-child(8) { display:none; visibility:hidden; }
|
||||
#hdd, tr td:nth-child(11) { display:none; visibility:hidden; }
|
||||
#cpu, #ram { min-width: 20px; max-width: 40px; }
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 44 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 600 B |
Binary file not shown.
|
After Width: | Height: | Size: 19 KiB |
@ -0,0 +1,2 @@
|
||||
stats.json
|
||||
stats.json~
|
||||
Loading…
Reference in New Issue