add uniq server addresses

pull/37/head
YuQing 2020-02-19 10:14:47 +08:00
parent 11a441e1f8
commit 89ad53974f
10 changed files with 558 additions and 291 deletions

View File

@ -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 test file src/tests/test_pthread_lock.c
* add uniq_skiplist.[hc] * add uniq_skiplist.[hc]
* add function split_string_ex * add function split_string_ex

View File

@ -47,7 +47,7 @@ int fast_buffer_check(FastBuffer *buffer, const int inc_len)
int alloc_size; int alloc_size;
char *buff; char *buff;
if (buffer->alloc_size > buffer->length + inc_len) if (buffer->alloc_size >= buffer->length + inc_len)
{ {
return 0; return 0;
} }

View File

@ -159,6 +159,13 @@ int log_init2();
#define log_destroy() log_destroy_ex(&g_log_context) #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 /** init function, use stderr for output by default
* parameters: * parameters:
* pContext: the log context * pContext: the log context

View File

@ -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]); 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 #ifdef __cplusplus
} }

File diff suppressed because it is too large Load Diff

View File

@ -6,6 +6,7 @@
#include "common_define.h" #include "common_define.h"
#include "connection_pool.h" #include "connection_pool.h"
#include "ini_file_reader.h" #include "ini_file_reader.h"
#include "fast_buffer.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -14,6 +15,9 @@ extern "C" {
#define FC_MAX_SERVER_IP_COUNT 8 #define FC_MAX_SERVER_IP_COUNT 8
#define FC_MAX_GROUP_COUNT 4 #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 typedef struct
{ {
int net_type; int net_type;
@ -26,6 +30,12 @@ typedef struct {
FCAddressInfo *addrs; FCAddressInfo *addrs;
} FCAddressArray; } FCAddressArray;
typedef struct {
int alloc;
int count;
FCAddressInfo **addrs;
} FCAddressPtrArray;
typedef struct typedef struct
{ {
string_t group_name; string_t group_name;
@ -48,12 +58,13 @@ typedef struct
typedef struct typedef struct
{ {
FCServerGroupInfo *server_group; FCServerGroupInfo *server_group;
FCAddressArray address_array; FCAddressPtrArray address_array;
} FCGroupAddresses; } FCGroupAddresses;
typedef struct typedef struct
{ {
int id; //server id int id; //server id
FCAddressArray uniq_addresses;
FCGroupAddresses group_addrs[FC_MAX_GROUP_COUNT]; FCGroupAddresses group_addrs[FC_MAX_GROUP_COUNT];
} FCServerInfo; } FCServerInfo;
@ -87,15 +98,15 @@ typedef struct
FCServerInfoArray by_id; //sorted by server id FCServerInfoArray by_id; //sorted by server id
FCServerMapArray by_ip_port; //sorted by IP and port FCServerMapArray by_ip_port; //sorted by IP and port
} sorted_server_arrays; } 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); 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); 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) const char *ip_addr, const int port)
{ {
string_t saddr; 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); 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); 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) const string_t *group_name)
{ {
FCServerGroupInfo *group; 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) const char *group_name)
{ {
string_t gname; 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); 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 char *config_filename, const int default_port,
const int min_hosts_each_group, const bool share_between_groups); 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 char *config_filename)
{ {
const int default_port = 0; 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); 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 char *caption, const int default_port,
const int min_hosts_each_group, const bool share_between_groups); 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) char *content)
{ {
const char *caption = "from-buffer"; 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); 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 #ifdef __cplusplus
} }

View File

@ -20,6 +20,15 @@
#include <fcntl.h> #include <fcntl.h>
#include <sys/ioctl.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) #if defined(OS_LINUX) || defined(OS_FREEBSD)
#include <ifaddrs.h> #include <ifaddrs.h>
#endif #endif
@ -2242,12 +2251,18 @@ int getifconfigs(FastIFConfig *if_configs, const int max_count, int *count)
} }
#endif #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; 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) if (memcmp(ip, "10.", 3) == 0)
{ {
@ -2271,3 +2286,36 @@ int fc_get_net_type(const char *ip)
return FC_NET_TYPE_OUTER; 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;
}
}

View File

@ -27,6 +27,19 @@
#define FC_SUB_NET_TYPE_INNER_172 (FC_NET_TYPE_INNER | 8) #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_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) #define FAST_WRITE_BUFF_SIZE (256 * 1024)
typedef struct fast_if_config { 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); 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) 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) switch (net_type)
{ {
case FC_NET_TYPE_NONE: case FC_NET_TYPE_ANY:
return "none"; return NET_TYPE_ANY_STR;
case FC_NET_TYPE_OUTER: case FC_NET_TYPE_OUTER:
return "outer"; return NET_TYPE_OUTER_STR;
case FC_NET_TYPE_INNER: case FC_NET_TYPE_INNER:
return "inner"; return NET_TYPE_INNER_STR;
case FC_SUB_NET_TYPE_INNER_10: case FC_SUB_NET_TYPE_INNER_10:
return "inner-10"; return SUB_NET_TYPE_INNER_10_STR;
case FC_SUB_NET_TYPE_INNER_172: case FC_SUB_NET_TYPE_INNER_172:
return "inner-172"; return SUB_NET_TYPE_INNER_172_STR;
case FC_SUB_NET_TYPE_INNER_192: case FC_SUB_NET_TYPE_INNER_192:
return "inner-192"; return SUB_NET_TYPE_INNER_192_STR;
default: default:
return "UNKOWN"; return NET_TYPE_UNKOWN_STR;
} }
} }

View File

@ -2,12 +2,17 @@
[group-inner] [group-inner]
port = 5108 port = 5108
# outer: extranet IP, such as 202.102.100.1 # net type for matching ip address, empty for any net type
# inner: intranet IP such as 172.16.1.5 or 192.168.3.17 # value can be one of followings:
# inner-10: 10 leading network, such as 10.32.1.100 ## any: match any/all net type
# inner-172: 172 leading network, such as 172.17.0.4 ## outer: extranet IP, such as 202.102.100.1
# inner-192: 192 leading network, such as 192.168.0.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 net_type = inner-172
# ip prefix for matching ip address, empty for any ip address
ip_prefix = 172. ip_prefix = 172.
[group-outer] [group-outer]

View File

@ -15,10 +15,11 @@ int main(int argc, char *argv[])
{ {
int result; int result;
const char *config_filename = "servers.conf"; const char *config_filename = "servers.conf";
FCServerContext ctx; FCServerConfig ctx;
const int default_port = 1111; const int default_port = 1111;
const int min_hosts_each_group = 1; const int min_hosts_each_group = 1;
const bool share_between_groups = true; const bool share_between_groups = true;
FastBuffer buffer;
if (argc > 1) { if (argc > 1) {
config_filename = argv[1]; config_filename = argv[1];
@ -33,8 +34,16 @@ int main(int argc, char *argv[])
return result; 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); fc_server_destroy(&ctx);
return 0; return 0;
} }