add uniq server addresses
parent
11a441e1f8
commit
89ad53974f
2
HISTORY
2
HISTORY
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
Version 1.44 2020-02-17
|
||||
Version 1.44 2020-02-19
|
||||
* add test file src/tests/test_pthread_lock.c
|
||||
* add uniq_skiplist.[hc]
|
||||
* add function split_string_ex
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ int fast_buffer_check(FastBuffer *buffer, const int inc_len)
|
|||
int alloc_size;
|
||||
char *buff;
|
||||
|
||||
if (buffer->alloc_size > buffer->length + inc_len)
|
||||
if (buffer->alloc_size >= buffer->length + inc_len)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -159,6 +159,13 @@ int log_init2();
|
|||
|
||||
#define log_destroy() log_destroy_ex(&g_log_context)
|
||||
|
||||
#define log_it1(priority, text, text_len) \
|
||||
log_it_ex1(&g_log_context, priority, text, text_len)
|
||||
|
||||
#define log_it2(caption, text, text_len, bNeedSync, bNeedLock) \
|
||||
log_it_ex2(&g_log_context, caption, text, text_len, bNeedSync, bNeedLock)
|
||||
|
||||
|
||||
/** init function, use stderr for output by default
|
||||
* parameters:
|
||||
* pContext: the log context
|
||||
|
|
|
|||
|
|
@ -41,11 +41,12 @@ int my_md5_file(char *filename, unsigned char digest[16]);
|
|||
*/
|
||||
int my_md5_buffer(char *buffer, unsigned int len, unsigned char digest[16]);
|
||||
|
||||
void my_md5_init (MD5_CTX *);
|
||||
void my_md5_init (MD5_CTX *context);
|
||||
|
||||
void my_md5_update (MD5_CTX *, unsigned char *, unsigned int);
|
||||
void my_md5_update (MD5_CTX *context, unsigned char *input,
|
||||
unsigned int inputLen);
|
||||
|
||||
void my_md5_final (unsigned char [16], MD5_CTX *);
|
||||
void my_md5_final (unsigned char digest[16], MD5_CTX *context);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -6,6 +6,7 @@
|
|||
#include "common_define.h"
|
||||
#include "connection_pool.h"
|
||||
#include "ini_file_reader.h"
|
||||
#include "fast_buffer.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
|
@ -14,6 +15,9 @@ extern "C" {
|
|||
#define FC_MAX_SERVER_IP_COUNT 8
|
||||
#define FC_MAX_GROUP_COUNT 4
|
||||
|
||||
#define FC_SID_SERVER_COUNT(ctx) (ctx).sorted_server_arrays.by_id.count
|
||||
#define FC_SID_SERVERS(ctx) (ctx).sorted_server_arrays.by_id.servers
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int net_type;
|
||||
|
|
@ -26,6 +30,12 @@ typedef struct {
|
|||
FCAddressInfo *addrs;
|
||||
} FCAddressArray;
|
||||
|
||||
typedef struct {
|
||||
int alloc;
|
||||
int count;
|
||||
FCAddressInfo **addrs;
|
||||
} FCAddressPtrArray;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
string_t group_name;
|
||||
|
|
@ -48,12 +58,13 @@ typedef struct
|
|||
typedef struct
|
||||
{
|
||||
FCServerGroupInfo *server_group;
|
||||
FCAddressArray address_array;
|
||||
FCAddressPtrArray address_array;
|
||||
} FCGroupAddresses;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int id; //server id
|
||||
FCAddressArray uniq_addresses;
|
||||
FCGroupAddresses group_addrs[FC_MAX_GROUP_COUNT];
|
||||
} FCServerInfo;
|
||||
|
||||
|
|
@ -84,18 +95,18 @@ typedef struct
|
|||
bool share_between_groups; //if an address shared between different groups
|
||||
FCServerGroupArray group_array;
|
||||
struct {
|
||||
FCServerInfoArray by_id; //sorted by server id
|
||||
FCServerInfoArray by_id; //sorted by server id
|
||||
FCServerMapArray by_ip_port; //sorted by IP and port
|
||||
} sorted_server_arrays;
|
||||
} FCServerContext;
|
||||
} FCServerConfig;
|
||||
|
||||
FCServerInfo *fc_server_get_by_id(FCServerContext *ctx,
|
||||
FCServerInfo *fc_server_get_by_id(FCServerConfig *ctx,
|
||||
const int server_id);
|
||||
|
||||
FCServerInfo *fc_server_get_by_ip_port_ex(FCServerContext *ctx,
|
||||
FCServerInfo *fc_server_get_by_ip_port_ex(FCServerConfig *ctx,
|
||||
const string_t *ip_addr, const int port);
|
||||
|
||||
static inline FCServerInfo *fc_server_get_by_ip_port(FCServerContext *ctx,
|
||||
static inline FCServerInfo *fc_server_get_by_ip_port(FCServerConfig *ctx,
|
||||
const char *ip_addr, const int port)
|
||||
{
|
||||
string_t saddr;
|
||||
|
|
@ -103,10 +114,10 @@ static inline FCServerInfo *fc_server_get_by_ip_port(FCServerContext *ctx,
|
|||
return fc_server_get_by_ip_port_ex(ctx, &saddr, port);
|
||||
}
|
||||
|
||||
FCServerGroupInfo *fc_server_get_group_by_name(FCServerContext *ctx,
|
||||
FCServerGroupInfo *fc_server_get_group_by_name(FCServerConfig *ctx,
|
||||
const string_t *group_name);
|
||||
|
||||
static inline int fc_server_get_group_index_ex(FCServerContext *ctx,
|
||||
static inline int fc_server_get_group_index_ex(FCServerConfig *ctx,
|
||||
const string_t *group_name)
|
||||
{
|
||||
FCServerGroupInfo *group;
|
||||
|
|
@ -118,7 +129,7 @@ static inline int fc_server_get_group_index_ex(FCServerContext *ctx,
|
|||
}
|
||||
}
|
||||
|
||||
static inline int fc_server_get_group_index(FCServerContext *ctx,
|
||||
static inline int fc_server_get_group_index(FCServerConfig *ctx,
|
||||
const char *group_name)
|
||||
{
|
||||
string_t gname;
|
||||
|
|
@ -126,11 +137,11 @@ static inline int fc_server_get_group_index(FCServerContext *ctx,
|
|||
return fc_server_get_group_index_ex(ctx, &gname);
|
||||
}
|
||||
|
||||
int fc_server_load_from_file_ex(FCServerContext *ctx,
|
||||
int fc_server_load_from_file_ex(FCServerConfig *ctx,
|
||||
const char *config_filename, const int default_port,
|
||||
const int min_hosts_each_group, const bool share_between_groups);
|
||||
|
||||
static inline int fc_server_load_from_file(FCServerContext *ctx,
|
||||
static inline int fc_server_load_from_file(FCServerConfig *ctx,
|
||||
const char *config_filename)
|
||||
{
|
||||
const int default_port = 0;
|
||||
|
|
@ -140,11 +151,11 @@ static inline int fc_server_load_from_file(FCServerContext *ctx,
|
|||
default_port, min_hosts_each_group, share_between_groups);
|
||||
}
|
||||
|
||||
int fc_server_load_from_buffer_ex(FCServerContext *ctx, char *content,
|
||||
int fc_server_load_from_buffer_ex(FCServerConfig *ctx, char *content,
|
||||
const char *caption, const int default_port,
|
||||
const int min_hosts_each_group, const bool share_between_groups);
|
||||
|
||||
static inline int fc_server_load_from_buffer(FCServerContext *ctx,
|
||||
static inline int fc_server_load_from_buffer(FCServerConfig *ctx,
|
||||
char *content)
|
||||
{
|
||||
const char *caption = "from-buffer";
|
||||
|
|
@ -155,9 +166,11 @@ static inline int fc_server_load_from_buffer(FCServerContext *ctx,
|
|||
default_port, min_hosts_each_group, share_between_groups);
|
||||
}
|
||||
|
||||
void fc_server_destroy(FCServerContext *ctx);
|
||||
void fc_server_destroy(FCServerConfig *ctx);
|
||||
|
||||
void fc_server_to_log(FCServerContext *ctx);
|
||||
int fc_server_to_config_string(FCServerConfig *ctx, FastBuffer *buffer);
|
||||
|
||||
void fc_server_to_log(FCServerConfig *ctx);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,15 @@
|
|||
#include <fcntl.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#define SUB_NET_TYPE_INNER_10_STR2 "inner_10"
|
||||
#define SUB_NET_TYPE_INNER_172_STR2 "inner_172"
|
||||
#define SUB_NET_TYPE_INNER_192_STR2 "inner_192"
|
||||
|
||||
#define SUB_NET_TYPE_INNER_10_STR3 "inner10"
|
||||
#define SUB_NET_TYPE_INNER_172_STR3 "inner172"
|
||||
#define SUB_NET_TYPE_INNER_192_STR3 "inner192"
|
||||
|
||||
|
||||
#if defined(OS_LINUX) || defined(OS_FREEBSD)
|
||||
#include <ifaddrs.h>
|
||||
#endif
|
||||
|
|
@ -2242,12 +2251,18 @@ int getifconfigs(FastIFConfig *if_configs, const int max_count, int *count)
|
|||
}
|
||||
#endif
|
||||
|
||||
int fc_get_net_type(const char *ip)
|
||||
int fc_get_net_type_by_ip(const char *ip)
|
||||
{
|
||||
if (ip == NULL || (int)strlen(ip) < 8)
|
||||
int len;
|
||||
if (ip == NULL)
|
||||
{
|
||||
return FC_NET_TYPE_NONE;
|
||||
}
|
||||
len = strlen(ip);
|
||||
if (len < 8)
|
||||
{
|
||||
return (len < 7) ? FC_NET_TYPE_NONE : FC_NET_TYPE_OUTER;
|
||||
}
|
||||
|
||||
if (memcmp(ip, "10.", 3) == 0)
|
||||
{
|
||||
|
|
@ -2271,3 +2286,36 @@ int fc_get_net_type(const char *ip)
|
|||
|
||||
return FC_NET_TYPE_OUTER;
|
||||
}
|
||||
|
||||
int fc_get_net_type_by_name(const char *net_type)
|
||||
{
|
||||
if (net_type == NULL || *net_type == '\0') {
|
||||
return FC_NET_TYPE_ANY;
|
||||
}
|
||||
|
||||
if (strcasecmp(net_type, NET_TYPE_ANY_STR) == 0) {
|
||||
return FC_NET_TYPE_ANY;
|
||||
} else if (strcasecmp(net_type, NET_TYPE_OUTER_STR) == 0) {
|
||||
return FC_NET_TYPE_OUTER;
|
||||
} else if (strcasecmp(net_type, NET_TYPE_INNER_STR) == 0) {
|
||||
return FC_NET_TYPE_INNER;
|
||||
} else if (strcasecmp(net_type, SUB_NET_TYPE_INNER_10_STR) == 0 ||
|
||||
strcasecmp(net_type, SUB_NET_TYPE_INNER_10_STR2) == 0 ||
|
||||
strcasecmp(net_type, SUB_NET_TYPE_INNER_10_STR3) == 0)
|
||||
{
|
||||
return FC_SUB_NET_TYPE_INNER_10;
|
||||
} else if (strcasecmp(net_type, SUB_NET_TYPE_INNER_172_STR) == 0 ||
|
||||
strcasecmp(net_type, SUB_NET_TYPE_INNER_172_STR2) == 0 ||
|
||||
strcasecmp(net_type, SUB_NET_TYPE_INNER_172_STR3) == 0)
|
||||
{
|
||||
return FC_SUB_NET_TYPE_INNER_172;
|
||||
} else if (strcasecmp(net_type, SUB_NET_TYPE_INNER_192_STR) == 0 ||
|
||||
strcasecmp(net_type, SUB_NET_TYPE_INNER_192_STR2) == 0 ||
|
||||
strcasecmp(net_type, SUB_NET_TYPE_INNER_192_STR3) == 0)
|
||||
{
|
||||
return FC_SUB_NET_TYPE_INNER_192;
|
||||
} else {
|
||||
return FC_NET_TYPE_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,6 +27,19 @@
|
|||
#define FC_SUB_NET_TYPE_INNER_172 (FC_NET_TYPE_INNER | 8)
|
||||
#define FC_SUB_NET_TYPE_INNER_192 (FC_NET_TYPE_INNER | 16)
|
||||
|
||||
#define FC_NET_TYPE_ANY (FC_NET_TYPE_OUTER | FC_NET_TYPE_INNER | \
|
||||
FC_SUB_NET_TYPE_INNER_10 | FC_SUB_NET_TYPE_INNER_172 | \
|
||||
FC_SUB_NET_TYPE_INNER_192)
|
||||
|
||||
#define NET_TYPE_ANY_STR "any"
|
||||
#define NET_TYPE_OUTER_STR "outer"
|
||||
#define NET_TYPE_INNER_STR "inner"
|
||||
#define NET_TYPE_UNKOWN_STR "UNKOWN"
|
||||
|
||||
#define SUB_NET_TYPE_INNER_10_STR "inner-10"
|
||||
#define SUB_NET_TYPE_INNER_172_STR "inner-172"
|
||||
#define SUB_NET_TYPE_INNER_192_STR "inner-192"
|
||||
|
||||
#define FAST_WRITE_BUFF_SIZE (256 * 1024)
|
||||
|
||||
typedef struct fast_if_config {
|
||||
|
|
@ -587,7 +600,9 @@ static inline void tcp_dont_try_again_when_interrupt()
|
|||
tcp_set_try_again_when_interrupt(false);
|
||||
}
|
||||
|
||||
int fc_get_net_type(const char *ip);
|
||||
int fc_get_net_type_by_name(const char *net_type);
|
||||
|
||||
int fc_get_net_type_by_ip(const char *ip);
|
||||
|
||||
static inline bool is_network_error(const int err_no)
|
||||
{
|
||||
|
|
@ -616,20 +631,20 @@ static inline const char *get_net_type_caption(const int net_type)
|
|||
{
|
||||
switch (net_type)
|
||||
{
|
||||
case FC_NET_TYPE_NONE:
|
||||
return "none";
|
||||
case FC_NET_TYPE_ANY:
|
||||
return NET_TYPE_ANY_STR;
|
||||
case FC_NET_TYPE_OUTER:
|
||||
return "outer";
|
||||
return NET_TYPE_OUTER_STR;
|
||||
case FC_NET_TYPE_INNER:
|
||||
return "inner";
|
||||
return NET_TYPE_INNER_STR;
|
||||
case FC_SUB_NET_TYPE_INNER_10:
|
||||
return "inner-10";
|
||||
return SUB_NET_TYPE_INNER_10_STR;
|
||||
case FC_SUB_NET_TYPE_INNER_172:
|
||||
return "inner-172";
|
||||
return SUB_NET_TYPE_INNER_172_STR;
|
||||
case FC_SUB_NET_TYPE_INNER_192:
|
||||
return "inner-192";
|
||||
return SUB_NET_TYPE_INNER_192_STR;
|
||||
default:
|
||||
return "UNKOWN";
|
||||
return NET_TYPE_UNKOWN_STR;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,12 +2,17 @@
|
|||
[group-inner]
|
||||
port = 5108
|
||||
|
||||
# outer: extranet IP, such as 202.102.100.1
|
||||
# inner: intranet IP such as 172.16.1.5 or 192.168.3.17
|
||||
# inner-10: 10 leading network, such as 10.32.1.100
|
||||
# inner-172: 172 leading network, such as 172.17.0.4
|
||||
# inner-192: 192 leading network, such as 192.168.0.1
|
||||
# net type for matching ip address, empty for any net type
|
||||
# value can be one of followings:
|
||||
## any: match any/all net type
|
||||
## outer: extranet IP, such as 202.102.100.1
|
||||
## inner: intranet IP such as 172.16.1.5 or 192.168.3.17
|
||||
## inner-10: 10 leading network, such as 10.32.1.100
|
||||
## inner-172: 172 leading network, such as 172.17.0.4
|
||||
## inner-192: 192 leading network, such as 192.168.0.1
|
||||
net_type = inner-172
|
||||
|
||||
# ip prefix for matching ip address, empty for any ip address
|
||||
ip_prefix = 172.
|
||||
|
||||
[group-outer]
|
||||
|
|
|
|||
|
|
@ -15,10 +15,11 @@ int main(int argc, char *argv[])
|
|||
{
|
||||
int result;
|
||||
const char *config_filename = "servers.conf";
|
||||
FCServerContext ctx;
|
||||
FCServerConfig ctx;
|
||||
const int default_port = 1111;
|
||||
const int min_hosts_each_group = 1;
|
||||
const bool share_between_groups = true;
|
||||
FastBuffer buffer;
|
||||
|
||||
if (argc > 1) {
|
||||
config_filename = argv[1];
|
||||
|
|
@ -33,8 +34,16 @@ int main(int argc, char *argv[])
|
|||
return result;
|
||||
}
|
||||
|
||||
fc_server_to_log(&ctx);
|
||||
if ((result=fast_buffer_init_ex(&buffer, 1024)) != 0) {
|
||||
return result;
|
||||
}
|
||||
fc_server_to_config_string(&ctx, &buffer);
|
||||
printf("%.*s", buffer.length, buffer.data);
|
||||
//printf("%.*s\n(%d)", buffer.length, buffer.data, buffer.length);
|
||||
|
||||
fast_buffer_destroy(&buffer);
|
||||
|
||||
//fc_server_to_log(&ctx);
|
||||
fc_server_destroy(&ctx);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue