2017-07-03 12:53:12 +08:00
|
|
|
#include "Maat_command.h"
|
2017-07-04 20:13:36 +08:00
|
|
|
#include "Maat_rule.h"
|
|
|
|
|
#include "Maat_rule_internal.h"
|
2017-07-03 12:53:12 +08:00
|
|
|
#include "config_monitor.h"
|
2017-07-04 20:13:36 +08:00
|
|
|
#include "map_str2int.h"
|
2017-07-03 12:53:12 +08:00
|
|
|
#include "hiredis.h"
|
2017-07-04 20:13:36 +08:00
|
|
|
#include <MESA/MESA_handle_logger.h>
|
2017-07-03 12:53:12 +08:00
|
|
|
#include <errno.h>
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
|
|
const char* maat_redis_monitor="MAAT_REDIS_MONITOR";
|
|
|
|
|
const char* maat_redis_command="MAAT_REDIS_COMMAND";
|
|
|
|
|
|
|
|
|
|
const char* rm_key_prefix[2]={"OBSOLETE_RULE","EFFECTIVE_RULE"};
|
2017-07-06 21:20:24 +08:00
|
|
|
const char* rm_status_sset="MAAT_UPDATE_STATUS";
|
2017-08-12 11:13:47 +08:00
|
|
|
const char* rm_expire_sset="MAAT_EXPIRE_TIMER";
|
2017-07-06 21:20:24 +08:00
|
|
|
const char* rm_label_sset="MAAT_LABEL_INDEX";
|
2017-08-24 17:39:40 +08:00
|
|
|
const char* rm_version_sset="MAAT_VERSION_TIMER";
|
2017-08-10 18:31:20 +08:00
|
|
|
const static int MAAT_REDIS_SYNC_TIME=30*60;
|
2017-07-03 12:53:12 +08:00
|
|
|
|
|
|
|
|
struct serial_rule_t //rm= Redis Maat
|
|
|
|
|
{
|
2017-07-03 19:54:47 +08:00
|
|
|
enum MAAT_OPERATION op;//0: delete, 1: add.
|
2017-07-03 12:53:12 +08:00
|
|
|
int rule_id;
|
2017-07-06 21:20:24 +08:00
|
|
|
int label_id;
|
|
|
|
|
long long timeout; // absolute unix time.
|
2017-07-03 19:54:47 +08:00
|
|
|
char table_name[256];
|
2017-07-03 12:53:12 +08:00
|
|
|
char* table_line;
|
|
|
|
|
};
|
|
|
|
|
|
2017-07-04 20:13:36 +08:00
|
|
|
struct _Maat_cmd_inner_t
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-06 21:20:24 +08:00
|
|
|
struct Maat_cmd_t cmd;
|
2017-07-03 12:53:12 +08:00
|
|
|
enum MAAT_OPERATION op;
|
|
|
|
|
int ref_cnt;
|
2017-07-04 20:13:36 +08:00
|
|
|
int region_size[MAX_EXPR_ITEM_NUM];
|
|
|
|
|
struct _Maat_cmd_inner_t* next;
|
2017-07-03 12:53:12 +08:00
|
|
|
};
|
2017-07-05 20:58:38 +08:00
|
|
|
int _wrap_redisGetReply(redisContext *c, redisReply **reply)
|
|
|
|
|
{
|
|
|
|
|
return redisGetReply(c, (void **)reply);
|
|
|
|
|
}
|
|
|
|
|
redisReply *_wrap_redisCommand(redisContext *c, const char *format, ...)
|
|
|
|
|
{
|
2017-08-12 11:13:47 +08:00
|
|
|
va_list ap;
|
|
|
|
|
void *reply = NULL;
|
2017-08-16 18:23:09 +08:00
|
|
|
int ret=0,retry=0;
|
|
|
|
|
while(reply==NULL&&retry<2)
|
|
|
|
|
{
|
|
|
|
|
va_start(ap,format);
|
|
|
|
|
reply = redisvCommand(c,format,ap);
|
|
|
|
|
va_end(ap);
|
|
|
|
|
if(reply==NULL)
|
|
|
|
|
{
|
|
|
|
|
ret=redisReconnect(c);
|
|
|
|
|
retry++;
|
|
|
|
|
if(ret==REDIS_OK)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-12 11:13:47 +08:00
|
|
|
return (redisReply *)reply;
|
2017-07-05 20:58:38 +08:00
|
|
|
}
|
2017-07-07 11:04:11 +08:00
|
|
|
int connect_redis_for_write(_Maat_feather_t * feather)
|
|
|
|
|
{
|
|
|
|
|
int ret=0;
|
2017-08-10 18:31:20 +08:00
|
|
|
redisReply* reply=NULL;
|
2017-07-07 11:04:11 +08:00
|
|
|
assert(feather->redis_write_ctx==NULL);
|
|
|
|
|
feather->redis_write_ctx=redisConnectWithTimeout(feather->redis_ip, feather->redis_port,feather->connect_timeout);
|
|
|
|
|
if(feather->redis_write_ctx==NULL)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(feather->logger,RLOG_LV_FATAL,maat_module
|
2017-08-12 11:13:47 +08:00
|
|
|
,"Redis connect %s:%d for write failed."
|
|
|
|
|
,feather->redis_ip,feather->redis_port);
|
2017-07-07 11:04:11 +08:00
|
|
|
ret=-1;
|
|
|
|
|
}
|
2017-08-10 18:31:20 +08:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
reply=_wrap_redisCommand(feather->redis_read_ctx, "select %d",feather->redis_index);
|
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
}
|
2017-07-07 11:04:11 +08:00
|
|
|
return ret;
|
|
|
|
|
}
|
2017-07-05 20:58:38 +08:00
|
|
|
long long read_redis_integer(const redisReply* reply)
|
|
|
|
|
{
|
|
|
|
|
switch(reply->type)
|
|
|
|
|
{
|
|
|
|
|
case REDIS_REPLY_INTEGER:
|
|
|
|
|
return reply->integer;
|
|
|
|
|
break;
|
|
|
|
|
case REDIS_REPLY_ARRAY:
|
|
|
|
|
assert(reply->element[0]->type==REDIS_REPLY_INTEGER);
|
|
|
|
|
return reply->element[0]->integer;
|
|
|
|
|
break;
|
|
|
|
|
case REDIS_REPLY_STRING:
|
|
|
|
|
return atoll(reply->str);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2017-07-06 21:20:24 +08:00
|
|
|
long long redis_server_time(redisContext* ctx)
|
|
|
|
|
{
|
|
|
|
|
long long server_time=0;
|
|
|
|
|
redisReply* data_reply=NULL;
|
|
|
|
|
data_reply=_wrap_redisCommand(ctx,"TIME");
|
2017-07-07 11:04:11 +08:00
|
|
|
assert(data_reply->type==REDIS_REPLY_ARRAY);
|
|
|
|
|
server_time=atoll(data_reply->element[0]->str);
|
2017-07-06 21:20:24 +08:00
|
|
|
freeReplyObject(data_reply);
|
|
|
|
|
return server_time;
|
|
|
|
|
}
|
2017-07-03 19:54:47 +08:00
|
|
|
enum MAAT_TABLE_TYPE type_region2table(const struct Maat_region_t* p)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-04 20:13:36 +08:00
|
|
|
enum MAAT_TABLE_TYPE ret=TABLE_TYPE_IP;
|
2017-07-03 12:53:12 +08:00
|
|
|
switch(p->region_type)
|
|
|
|
|
{
|
|
|
|
|
case REGION_IP:
|
|
|
|
|
ret=TABLE_TYPE_IP;
|
|
|
|
|
break;
|
|
|
|
|
case REGION_EXPR:
|
|
|
|
|
if(p->expr_rule.district==NULL)
|
|
|
|
|
{
|
|
|
|
|
ret=TABLE_TYPE_EXPR;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ret=TABLE_TYPE_EXPR_PLUS;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case REGION_INTERVAL:
|
|
|
|
|
ret=TABLE_TYPE_INTERVAL;
|
|
|
|
|
break;
|
|
|
|
|
case REGION_DIGEST:
|
|
|
|
|
ret=TABLE_TYPE_DIGEST;
|
|
|
|
|
break;
|
|
|
|
|
case REGION_SIMILARITY:
|
|
|
|
|
ret=TABLE_TYPE_SIMILARITY;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
void invalidate_line(char* line, enum MAAT_TABLE_TYPE type,int valid_column_seq)
|
|
|
|
|
{
|
|
|
|
|
unsigned int offset=0;
|
|
|
|
|
unsigned int i=0,j=0;
|
|
|
|
|
switch(type)
|
|
|
|
|
{
|
|
|
|
|
case TABLE_TYPE_EXPR:
|
|
|
|
|
offset=7;
|
|
|
|
|
break;
|
|
|
|
|
case TABLE_TYPE_IP:
|
|
|
|
|
offset=14;
|
|
|
|
|
break;
|
|
|
|
|
case TABLE_TYPE_COMPILE:
|
|
|
|
|
offset=8;
|
|
|
|
|
break;
|
|
|
|
|
case TABLE_TYPE_PLUGIN:
|
|
|
|
|
if(valid_column_seq<0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
offset=(unsigned int)valid_column_seq;
|
|
|
|
|
break;
|
|
|
|
|
case TABLE_TYPE_INTERVAL:
|
|
|
|
|
offset=5;
|
|
|
|
|
break;
|
|
|
|
|
case TABLE_TYPE_DIGEST:
|
|
|
|
|
offset=6;
|
|
|
|
|
break;
|
2017-08-16 18:23:09 +08:00
|
|
|
case TABLE_TYPE_SIMILARITY:
|
|
|
|
|
offset=5;
|
|
|
|
|
break;
|
2017-07-04 20:13:36 +08:00
|
|
|
case TABLE_TYPE_EXPR_PLUS:
|
|
|
|
|
offset=8;
|
|
|
|
|
break;
|
|
|
|
|
case TABLE_TYPE_GROUP:
|
|
|
|
|
offset=3;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
for(i=0;i<strlen(line);i++)
|
|
|
|
|
{
|
|
|
|
|
if(line[i]==' '||line[i]=='\t')
|
|
|
|
|
{
|
|
|
|
|
j++;
|
|
|
|
|
}
|
2017-07-05 20:58:38 +08:00
|
|
|
if(j==offset-1)
|
2017-07-04 20:13:36 +08:00
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-05 20:58:38 +08:00
|
|
|
i++;
|
2017-07-04 20:13:36 +08:00
|
|
|
assert(i<strlen(line));
|
|
|
|
|
assert(line[i]=='1');
|
|
|
|
|
line[i]='0';
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-07-06 21:20:24 +08:00
|
|
|
int del_rule_from_redis(redisContext* ctx, struct serial_rule_t* s_rule, long long new_version)
|
|
|
|
|
{
|
2017-07-07 11:04:11 +08:00
|
|
|
int append_cmd_cnt=0;
|
2017-07-06 21:20:24 +08:00
|
|
|
redisAppendCommand(ctx,"RENAME %s:%s,%d %s:%s,%d"
|
2017-08-12 11:13:47 +08:00
|
|
|
,rm_key_prefix[MAAT_OP_ADD]
|
|
|
|
|
,s_rule->table_name
|
|
|
|
|
,s_rule->rule_id
|
|
|
|
|
,rm_key_prefix[MAAT_OP_DEL]
|
|
|
|
|
,s_rule->table_name
|
|
|
|
|
,s_rule->rule_id
|
|
|
|
|
);
|
2017-07-06 21:20:24 +08:00
|
|
|
append_cmd_cnt++;
|
|
|
|
|
redisAppendCommand(ctx,"EXPIRE %s:%s,%d %d",rm_key_prefix[MAAT_OP_DEL]
|
2017-08-12 11:13:47 +08:00
|
|
|
,s_rule->table_name
|
|
|
|
|
,s_rule->rule_id
|
|
|
|
|
,MAAT_REDIS_SYNC_TIME);
|
2017-07-06 21:20:24 +08:00
|
|
|
append_cmd_cnt++;
|
|
|
|
|
//NX: Don't update already exisiting elements. Always add new elements.
|
|
|
|
|
redisAppendCommand(ctx,"ZADD %s NX %d DEL,%s,%d",rm_status_sset
|
2017-08-12 11:13:47 +08:00
|
|
|
,new_version
|
|
|
|
|
,s_rule->table_name
|
|
|
|
|
,s_rule->rule_id);
|
2017-07-06 21:20:24 +08:00
|
|
|
append_cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
// Try to remove from expiration sorted set, no matter wheather it exists or not.
|
|
|
|
|
redisAppendCommand(ctx,"ZREM %s %s,%d",rm_expire_sset
|
2017-08-12 11:13:47 +08:00
|
|
|
,s_rule->table_name
|
|
|
|
|
,s_rule->rule_id);
|
2017-07-06 21:20:24 +08:00
|
|
|
append_cmd_cnt++;
|
|
|
|
|
if(s_rule->label_id>0)
|
|
|
|
|
{
|
|
|
|
|
redisAppendCommand(ctx,"ZREM %s %d",rm_label_sset
|
2017-08-12 11:13:47 +08:00
|
|
|
,s_rule->rule_id);
|
2017-08-07 16:21:23 +08:00
|
|
|
append_cmd_cnt++;
|
2017-07-06 21:20:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return append_cmd_cnt;
|
|
|
|
|
}
|
2017-07-03 19:54:47 +08:00
|
|
|
void serialize_region(const struct Maat_region_t* p,int group_id, char* buff,int size)
|
|
|
|
|
{
|
|
|
|
|
int ret=0;
|
|
|
|
|
switch(p->region_type)
|
|
|
|
|
{
|
|
|
|
|
case REGION_IP:
|
|
|
|
|
ret=snprintf(buff,size,"%d\t%d\t%d\t%s\t%s\t%hu\t%hu\t%s\t%s\t%hu\t%hu\t%d\t%d\t1"
|
2017-08-12 11:13:47 +08:00
|
|
|
,p->region_id
|
|
|
|
|
,group_id
|
|
|
|
|
,p->ip_rule.addr_type
|
|
|
|
|
,p->ip_rule.src_ip
|
|
|
|
|
,p->ip_rule.mask_src_ip
|
|
|
|
|
,p->ip_rule.src_port
|
|
|
|
|
,p->ip_rule.mask_src_port
|
|
|
|
|
,p->ip_rule.dst_ip
|
|
|
|
|
,p->ip_rule.mask_dst_ip
|
|
|
|
|
,p->ip_rule.dst_port
|
|
|
|
|
,p->ip_rule.mask_dst_port
|
|
|
|
|
,p->ip_rule.protocol
|
|
|
|
|
,p->ip_rule.direction);
|
2017-07-03 19:54:47 +08:00
|
|
|
break;
|
|
|
|
|
case REGION_EXPR:
|
|
|
|
|
if(p->expr_rule.district==NULL)
|
|
|
|
|
{
|
|
|
|
|
ret=snprintf(buff,size,"%d\t%d\t%s\t%d\t%d\t%d\t1"
|
2017-08-12 11:13:47 +08:00
|
|
|
,p->region_id
|
|
|
|
|
,group_id
|
|
|
|
|
,p->expr_rule.keywords
|
|
|
|
|
,p->expr_rule.expr_type
|
|
|
|
|
,p->expr_rule.match_method
|
|
|
|
|
,p->expr_rule.hex_bin);
|
2017-07-03 19:54:47 +08:00
|
|
|
}
|
|
|
|
|
else //expr_plus
|
|
|
|
|
{
|
|
|
|
|
ret=snprintf(buff,size,"%d\t%d\t%s\t%s\t%d\t%d\t%d\t1"
|
2017-08-12 11:13:47 +08:00
|
|
|
,p->region_id
|
|
|
|
|
,group_id
|
|
|
|
|
,p->expr_rule.keywords
|
|
|
|
|
,p->expr_rule.district
|
|
|
|
|
,p->expr_rule.expr_type
|
|
|
|
|
,p->expr_rule.match_method
|
|
|
|
|
,p->expr_rule.hex_bin);
|
2017-07-03 19:54:47 +08:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case REGION_INTERVAL:
|
|
|
|
|
ret=snprintf(buff,size,"%d\t%d\t%u\t%u\t1"
|
2017-08-12 11:13:47 +08:00
|
|
|
,p->region_id
|
|
|
|
|
,group_id
|
|
|
|
|
,p->interval_rule.low_boundary
|
|
|
|
|
,p->interval_rule.up_boundary);
|
2017-07-03 19:54:47 +08:00
|
|
|
break;
|
|
|
|
|
case REGION_DIGEST:
|
|
|
|
|
ret=snprintf(buff,size,"%d\t%d\t%llu\t%s\t%hd\t1"
|
2017-08-12 11:13:47 +08:00
|
|
|
,p->region_id
|
|
|
|
|
,group_id
|
|
|
|
|
,p->digest_rule.orgin_len
|
|
|
|
|
,p->digest_rule.digest_string
|
|
|
|
|
,p->digest_rule.confidence_degree);
|
2017-07-03 19:54:47 +08:00
|
|
|
break;
|
2017-08-16 18:23:09 +08:00
|
|
|
case REGION_SIMILARITY:
|
2017-07-07 20:51:55 +08:00
|
|
|
ret=snprintf(buff,size,"%d\t%d\t%s\t%hd\t1"
|
2017-08-12 11:13:47 +08:00
|
|
|
,p->region_id
|
|
|
|
|
,group_id
|
|
|
|
|
,p->similarity_rule.target
|
|
|
|
|
,p->similarity_rule.threshold);
|
2017-07-03 19:54:47 +08:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
assert(ret<size);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
void empty_serial_rules(struct serial_rule_t* rule)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
|
|
|
|
if(rule->table_line!=NULL)
|
|
|
|
|
{
|
|
|
|
|
free(rule->table_line);
|
|
|
|
|
rule->table_line=NULL;
|
|
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
memset(rule,0,sizeof(struct serial_rule_t));
|
2017-07-03 12:53:12 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2017-07-06 21:20:24 +08:00
|
|
|
void set_serial_rule(struct serial_rule_t* rule,enum MAAT_OPERATION op,int rule_id,int label_id,const char* table_name,const char* line, long long timeout)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
|
|
|
|
rule->op=op;
|
2017-07-03 19:54:47 +08:00
|
|
|
rule->rule_id=rule_id;
|
2017-07-06 21:20:24 +08:00
|
|
|
rule->label_id=label_id;
|
|
|
|
|
rule->timeout=timeout;
|
2017-07-04 20:13:36 +08:00
|
|
|
assert(strlen(table_name)<sizeof(rule->table_name));
|
2017-07-26 09:52:44 +08:00
|
|
|
memset(rule->table_name, 0, sizeof(rule->table_name));
|
2017-07-03 12:53:12 +08:00
|
|
|
memcpy(rule->table_name,table_name,strlen(table_name));
|
|
|
|
|
if(line!=NULL)
|
|
|
|
|
{
|
|
|
|
|
rule->table_line=_maat_strdup(line);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-08-16 18:23:09 +08:00
|
|
|
int _wrap_redisReconnect(redisContext* c, void*logger)
|
|
|
|
|
{
|
|
|
|
|
int ret=0;
|
|
|
|
|
ret=redisReconnect(c);
|
|
|
|
|
if(ret==REDIS_OK)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
MESA_handle_runtime_log(logger, RLOG_LV_FATAL, maat_redis_monitor,"Reconnect success.");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger, RLOG_LV_FATAL, maat_redis_monitor,"Reconnect failed.");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-03 12:53:12 +08:00
|
|
|
int get_rm_key_list(unsigned int version,redisContext *c,struct serial_rule_t** list,void* logger, unsigned int* new_version,int *update_type)
|
|
|
|
|
{
|
2017-08-07 16:21:23 +08:00
|
|
|
redisReply* reply=NULL,*sub_reply=NULL,*tmp_reply=NULL;
|
2017-07-03 12:53:12 +08:00
|
|
|
char err_buff[256];
|
2017-07-04 20:13:36 +08:00
|
|
|
char op_str[4];
|
|
|
|
|
long long version_in_redis=0,nearest_rule_version=0;
|
2017-08-12 11:13:47 +08:00
|
|
|
int ret=0;
|
2017-08-07 16:21:23 +08:00
|
|
|
unsigned int i=0,full_idx =0,append_cmd_cnt=0;
|
2017-07-03 19:54:47 +08:00
|
|
|
struct serial_rule_t *s_rule=NULL;
|
2017-07-03 12:53:12 +08:00
|
|
|
|
2017-08-12 11:13:47 +08:00
|
|
|
reply=(redisReply*)redisCommand(c, "GET MAAT_VERSION");
|
|
|
|
|
if(reply!=NULL)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-08-12 11:13:47 +08:00
|
|
|
|
|
|
|
|
if(reply->type==REDIS_REPLY_NIL||reply->type==REDIS_REPLY_ERROR)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-08-12 11:13:47 +08:00
|
|
|
MESA_handle_runtime_log(logger, RLOG_LV_FATAL, maat_redis_monitor,"GET MAAT_VERSION failed, maybe Redis is busy.");
|
2017-08-24 17:39:40 +08:00
|
|
|
return -1;
|
2017-07-03 12:53:12 +08:00
|
|
|
}
|
2017-08-12 11:13:47 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
memset(err_buff,0,sizeof(err_buff));
|
|
|
|
|
__redis_strerror_r(errno,err_buff,sizeof(err_buff));
|
|
|
|
|
MESA_handle_runtime_log(logger, RLOG_LV_FATAL, maat_redis_monitor,
|
|
|
|
|
"GET MAAT_VERSION failed %s. Reconnecting...",err_buff);
|
2017-08-16 18:23:09 +08:00
|
|
|
_wrap_redisReconnect(c,logger);
|
2017-08-24 17:39:40 +08:00
|
|
|
return -1;
|
2017-07-03 12:53:12 +08:00
|
|
|
}
|
2017-07-05 20:58:38 +08:00
|
|
|
version_in_redis=read_redis_integer(reply);
|
2017-07-04 20:13:36 +08:00
|
|
|
assert(version_in_redis>=version);
|
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
if(version_in_redis==version)
|
|
|
|
|
{
|
2017-07-03 12:53:12 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
*new_version=version_in_redis;
|
2017-08-12 11:13:47 +08:00
|
|
|
|
2017-07-04 20:13:36 +08:00
|
|
|
if(version==0)
|
|
|
|
|
{
|
|
|
|
|
goto FULL_UPDATE;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-12 11:13:47 +08:00
|
|
|
|
2017-07-03 12:53:12 +08:00
|
|
|
//Returns all the elements in the sorted set at key with a score that version < score <= version_in_redis.
|
|
|
|
|
//The elements are considered to be ordered from low to high scores(version).
|
2017-07-06 21:20:24 +08:00
|
|
|
reply=(redisReply*)redisCommand(c, "ZRANGEBYSCORE %s (%d %d",rm_status_sset,version,version_in_redis);
|
2017-07-04 20:13:36 +08:00
|
|
|
if(reply==NULL)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
|
|
|
|
__redis_strerror_r(errno,err_buff,sizeof(err_buff));
|
|
|
|
|
MESA_handle_runtime_log(logger, RLOG_LV_FATAL, maat_redis_monitor,
|
2017-08-12 11:13:47 +08:00
|
|
|
"GET %s failed %s.",rm_status_sset,err_buff);
|
2017-08-24 17:39:40 +08:00
|
|
|
return -1;
|
2017-07-03 12:53:12 +08:00
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
assert(reply->type==REDIS_REPLY_ARRAY);
|
2017-08-12 11:13:47 +08:00
|
|
|
if(reply->elements==0)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger, RLOG_LV_FATAL, maat_redis_monitor,"Got nothing after ZRANGEBYSCORE %s (%d %d",rm_status_sset,version,version_in_redis);
|
|
|
|
|
freeReplyObject(reply);
|
2017-08-24 17:39:40 +08:00
|
|
|
return -1;
|
2017-08-12 11:13:47 +08:00
|
|
|
}
|
2017-07-06 21:20:24 +08:00
|
|
|
tmp_reply=_wrap_redisCommand(c, "ZSCORE %s %s",rm_status_sset,reply->element[0]->str);
|
2017-08-15 09:14:44 +08:00
|
|
|
if(tmp_reply->type!=REDIS_REPLY_STRING)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger, RLOG_LV_INFO, maat_redis_monitor,
|
|
|
|
|
"ZSCORE %s %s failed Version: %d->%d",rm_status_sset,reply->element[0]->str,version, version_in_redis);
|
|
|
|
|
free(tmp_reply);
|
|
|
|
|
free(reply);
|
|
|
|
|
goto FULL_UPDATE;
|
|
|
|
|
|
|
|
|
|
}
|
2017-07-05 20:58:38 +08:00
|
|
|
nearest_rule_version=read_redis_integer(tmp_reply);
|
2017-07-04 20:13:36 +08:00
|
|
|
freeReplyObject(tmp_reply);
|
|
|
|
|
tmp_reply=NULL;
|
|
|
|
|
if(nearest_rule_version!=version+1)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger, RLOG_LV_INFO, maat_redis_monitor,
|
2017-08-12 11:13:47 +08:00
|
|
|
"Noncontinuous VERSION Redis: %lld MAAT: %d.",nearest_rule_version,version);
|
2017-07-04 20:13:36 +08:00
|
|
|
|
2017-07-03 12:53:12 +08:00
|
|
|
goto FULL_UPDATE;
|
|
|
|
|
}
|
2017-08-10 18:31:20 +08:00
|
|
|
MESA_handle_runtime_log(logger, RLOG_LV_INFO, maat_redis_monitor,
|
2017-08-12 11:13:47 +08:00
|
|
|
"Inc Update form version %d to %lld (%lld entries).",version,version_in_redis,reply->elements);
|
|
|
|
|
|
2017-07-04 20:13:36 +08:00
|
|
|
s_rule=(struct serial_rule_t*)calloc(reply->elements,sizeof(struct serial_rule_t));
|
|
|
|
|
for(i=0;i<reply->elements;i++)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-04 20:13:36 +08:00
|
|
|
assert(reply->element[i]->type==REDIS_REPLY_STRING);
|
2017-07-05 20:58:38 +08:00
|
|
|
ret=sscanf(reply->element[i]->str,"%[^,],%[^,],%d",op_str,s_rule[i].table_name,&(s_rule[i].rule_id));
|
2017-07-03 12:53:12 +08:00
|
|
|
assert(ret==3);
|
2017-07-04 20:13:36 +08:00
|
|
|
if(strncmp(op_str,"ADD",strlen("ADD"))==0)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-03 19:54:47 +08:00
|
|
|
s_rule[i].op=MAAT_OP_ADD;
|
2017-07-03 12:53:12 +08:00
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
else if(strncmp(op_str,"DEL",strlen("DEL"))==0)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-03 19:54:47 +08:00
|
|
|
s_rule[i].op=MAAT_OP_DEL;
|
2017-07-03 12:53:12 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-03 19:54:47 +08:00
|
|
|
*list=s_rule;
|
2017-07-03 12:53:12 +08:00
|
|
|
*update_type=CM_UPDATE_TYPE_INC;
|
2017-07-04 20:13:36 +08:00
|
|
|
freeReplyObject(reply);
|
2017-07-03 12:53:12 +08:00
|
|
|
return i;
|
|
|
|
|
FULL_UPDATE:
|
|
|
|
|
MESA_handle_runtime_log(logger, RLOG_LV_INFO, maat_redis_monitor,
|
2017-08-12 11:13:47 +08:00
|
|
|
"Initiate full udpate from version %d to %lld.",version,version_in_redis);
|
2017-08-07 16:21:23 +08:00
|
|
|
append_cmd_cnt=0;
|
|
|
|
|
ret=redisAppendCommand(c, "MULTI");
|
|
|
|
|
append_cmd_cnt++;
|
|
|
|
|
ret=redisAppendCommand(c, "GET MAAT_VERSION");
|
|
|
|
|
append_cmd_cnt++;
|
|
|
|
|
ret=redisAppendCommand(c, "KEYS EFFECTIVE_RULE:*");
|
|
|
|
|
append_cmd_cnt++;
|
|
|
|
|
//consume reply "OK" and "QUEUED".
|
|
|
|
|
for(i=0;i<append_cmd_cnt;i++)
|
|
|
|
|
{
|
|
|
|
|
_wrap_redisGetReply(c, &reply);
|
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
reply=NULL;
|
|
|
|
|
}
|
|
|
|
|
reply=_wrap_redisCommand(c,"EXEC");
|
2017-07-04 20:13:36 +08:00
|
|
|
assert(reply->type==REDIS_REPLY_ARRAY);
|
2017-08-07 16:21:23 +08:00
|
|
|
*new_version=read_redis_integer(reply->element[0]);
|
|
|
|
|
sub_reply=reply->element[1];
|
|
|
|
|
assert(sub_reply->type==REDIS_REPLY_ARRAY);
|
|
|
|
|
s_rule=(struct serial_rule_t*)calloc(sub_reply->elements,sizeof(struct serial_rule_t));
|
|
|
|
|
for(full_idx=0;full_idx<sub_reply->elements;full_idx++)
|
2017-07-04 20:13:36 +08:00
|
|
|
{
|
2017-08-07 16:21:23 +08:00
|
|
|
assert(sub_reply->element[full_idx]->type==REDIS_REPLY_STRING);
|
|
|
|
|
ret=sscanf(sub_reply->element[full_idx]->str,"%*[^:]:%[^,],%d",s_rule[full_idx].table_name,&(s_rule[full_idx].rule_id));
|
|
|
|
|
s_rule[full_idx].op=MAAT_OP_ADD;
|
2017-07-03 12:53:12 +08:00
|
|
|
assert(ret==2);
|
|
|
|
|
}
|
2017-08-07 16:21:23 +08:00
|
|
|
freeReplyObject(reply);
|
2017-07-03 19:54:47 +08:00
|
|
|
*list=s_rule;
|
2017-07-03 12:53:12 +08:00
|
|
|
*update_type=CM_UPDATE_TYPE_FULL;
|
2017-08-12 11:13:47 +08:00
|
|
|
|
2017-08-07 16:21:23 +08:00
|
|
|
return full_idx ;
|
|
|
|
|
}
|
2017-08-12 11:13:47 +08:00
|
|
|
int get_rm_value(redisContext *c,struct serial_rule_t* rule_list,int rule_num,void* logger)
|
2017-08-07 16:21:23 +08:00
|
|
|
{
|
|
|
|
|
int i=0,ret=0,failed_cnt=0,idx=0;
|
|
|
|
|
int *retry_ids=(int*)malloc(sizeof(int)*rule_num);
|
|
|
|
|
char redis_cmd[256];
|
|
|
|
|
redisReply* reply=NULL;
|
|
|
|
|
for(i=0;i<rule_num;i++)
|
|
|
|
|
{
|
|
|
|
|
snprintf(redis_cmd,sizeof(redis_cmd),"GET %s:%s,%d",rm_key_prefix[rule_list[i].op]
|
2017-08-12 11:13:47 +08:00
|
|
|
,rule_list[i].table_name
|
|
|
|
|
,rule_list[i].rule_id);
|
2017-08-07 16:21:23 +08:00
|
|
|
ret=redisAppendCommand(c, redis_cmd);
|
|
|
|
|
assert(ret==REDIS_OK);
|
|
|
|
|
}
|
|
|
|
|
for(i=0;i<rule_num;i++)
|
|
|
|
|
{
|
|
|
|
|
ret=_wrap_redisGetReply(c,&reply);
|
|
|
|
|
if(reply->type==REDIS_REPLY_STRING)
|
|
|
|
|
{
|
|
|
|
|
rule_list[i].table_line=_maat_strdup(reply->str);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-08-12 11:13:47 +08:00
|
|
|
if(reply->type==REDIS_REPLY_NIL)
|
|
|
|
|
{
|
|
|
|
|
retry_ids[failed_cnt]=i;
|
|
|
|
|
failed_cnt++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_INFO,maat_redis_monitor
|
|
|
|
|
,"Redis GET %s:%s,%d failed",rm_key_prefix[rule_list[i].op]
|
|
|
|
|
,rule_list[i].table_name
|
|
|
|
|
,rule_list[i].rule_id);
|
|
|
|
|
free(retry_ids);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2017-08-07 16:21:23 +08:00
|
|
|
}
|
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
}
|
|
|
|
|
for(i=0;i<failed_cnt;i++)
|
|
|
|
|
{
|
|
|
|
|
idx=retry_ids[i];
|
|
|
|
|
snprintf(redis_cmd,sizeof(redis_cmd),"GET %s:%s,%d",rm_key_prefix[0]
|
2017-08-12 11:13:47 +08:00
|
|
|
,rule_list[idx].table_name
|
|
|
|
|
,rule_list[idx].rule_id);
|
2017-08-07 16:21:23 +08:00
|
|
|
reply=_wrap_redisCommand(c, redis_cmd);
|
|
|
|
|
assert(reply->type==REDIS_REPLY_STRING);
|
|
|
|
|
rule_list[idx].table_line=_maat_strdup(reply->str);
|
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
}
|
|
|
|
|
free(retry_ids);
|
2017-08-12 11:13:47 +08:00
|
|
|
return 0;
|
2017-07-03 12:53:12 +08:00
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
int calculate_serial_rule_num(struct _Maat_cmd_inner_t* _cmd,int * new_region_cnt, int* new_group_cnt)
|
|
|
|
|
{
|
|
|
|
|
int serial_num=0;
|
|
|
|
|
int i=0;
|
2017-07-06 21:20:24 +08:00
|
|
|
struct Maat_cmd_t* cmd=&(_cmd->cmd);
|
2017-07-04 20:13:36 +08:00
|
|
|
serial_num++;//compile rule
|
|
|
|
|
for(i=0;i<cmd->group_num;i++)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-04 20:13:36 +08:00
|
|
|
serial_num++;
|
|
|
|
|
if(cmd->groups[i].regions==NULL)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if(_cmd->op==MAAT_OP_ADD)
|
|
|
|
|
{
|
|
|
|
|
*new_region_cnt+=cmd->groups[i].region_num;
|
2017-07-06 21:20:24 +08:00
|
|
|
(*new_group_cnt)++;
|
2017-07-04 20:13:36 +08:00
|
|
|
}
|
2017-07-05 20:58:38 +08:00
|
|
|
//for MAAT_OP_DEL, if the group's refcnt>0, group->region_num=0.
|
|
|
|
|
//so it's OK to add.
|
|
|
|
|
serial_num+=cmd->groups[i].region_num;
|
2017-07-03 12:53:12 +08:00
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
return serial_num;
|
|
|
|
|
}
|
|
|
|
|
int reconstruct_cmd(struct _Maat_feather_t *feather, struct _Maat_cmd_inner_t* _cmd)
|
|
|
|
|
{
|
|
|
|
|
int i=0,j=0,grp_idx=0;
|
2017-07-06 21:20:24 +08:00
|
|
|
struct Maat_cmd_t* cmd=&(_cmd->cmd);
|
2017-07-04 20:13:36 +08:00
|
|
|
struct Maat_group_t* group_cmd=NULL;
|
|
|
|
|
struct Maat_region_t* region_cmd=NULL;
|
|
|
|
|
|
|
|
|
|
struct _Maat_compile_inner_t *compile_inner=NULL;
|
|
|
|
|
struct _Maat_group_inner_t* group_inner=NULL;
|
|
|
|
|
struct _Maat_region_inner_t* region_inner=NULL;
|
|
|
|
|
void* logger=feather->logger;
|
2017-08-12 11:13:47 +08:00
|
|
|
|
2017-07-04 20:13:36 +08:00
|
|
|
int config_id=cmd->compile.config_id;
|
2017-08-07 16:21:23 +08:00
|
|
|
if(feather->scanner==NULL)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_INFO,maat_redis_command
|
2017-08-12 11:13:47 +08:00
|
|
|
,"MAAT not ready.");
|
2017-08-07 16:21:23 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
compile_inner=(struct _Maat_compile_inner_t *)HASH_fetch_by_id(feather->scanner->compile_hash, config_id);
|
|
|
|
|
if(compile_inner==NULL)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-08-07 16:21:23 +08:00
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_INFO,maat_redis_command
|
2017-08-12 11:13:47 +08:00
|
|
|
,"config %d not exist."
|
|
|
|
|
,config_id);
|
2017-07-04 20:13:36 +08:00
|
|
|
return -1;
|
2017-07-03 12:53:12 +08:00
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
cmd->group_num=compile_inner->group_cnt;
|
|
|
|
|
assert(cmd->groups==NULL);
|
|
|
|
|
cmd->groups=(struct Maat_group_t*)calloc(sizeof(struct Maat_group_t),cmd->group_num);
|
|
|
|
|
for(i=0;i<compile_inner->group_boundary;i++)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-04 20:13:36 +08:00
|
|
|
group_inner=(struct _Maat_group_inner_t*)dynamic_array_read(compile_inner->groups,i);
|
|
|
|
|
if(group_inner==NULL)
|
2017-07-03 19:54:47 +08:00
|
|
|
{
|
2017-07-04 20:13:36 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
group_cmd=&(cmd->groups[grp_idx]);
|
|
|
|
|
group_cmd->group_id=group_inner->group_id;
|
2017-08-12 11:13:47 +08:00
|
|
|
|
2017-07-04 20:13:36 +08:00
|
|
|
if(group_inner->ref_cnt>0)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
group_cmd->region_num=group_inner->region_cnt;
|
|
|
|
|
group_cmd->regions=(struct Maat_region_t*)calloc(sizeof(struct Maat_region_t),group_cmd->region_num);
|
|
|
|
|
for(j=0;j<group_inner->region_boundary;j++)
|
|
|
|
|
{
|
|
|
|
|
region_inner=(struct _Maat_region_inner_t*)dynamic_array_read(group_inner->regions,i);
|
|
|
|
|
if(region_inner==NULL)
|
2017-07-03 19:54:47 +08:00
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
region_cmd=&(group_cmd->regions[group_cmd->region_num]);
|
|
|
|
|
region_cmd->table_name=_maat_strdup(feather->p_table_info[region_inner->table_id]->table_name[0]);
|
|
|
|
|
region_cmd->region_id=region_inner->table_id;
|
|
|
|
|
//NOTICE: region_type only avilable when OP_ADD,
|
|
|
|
|
region_cmd->region_type=REGION_EXPR;
|
|
|
|
|
group_cmd->region_num++;
|
2017-07-03 19:54:47 +08:00
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
grp_idx++;
|
2017-07-03 12:53:12 +08:00
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
return 0;
|
2017-07-03 12:53:12 +08:00
|
|
|
}
|
|
|
|
|
|
2017-07-04 20:13:36 +08:00
|
|
|
int build_serial_rule(_Maat_feather_t *feather,struct _Maat_cmd_inner_t* _cmd,struct serial_rule_t* list, int size)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-04 20:13:36 +08:00
|
|
|
struct Maat_group_t* p_group=NULL;
|
|
|
|
|
struct Maat_region_t* p_region=NULL;
|
|
|
|
|
struct Maat_rule_t* p_m_rule=NULL;
|
2017-07-06 21:20:24 +08:00
|
|
|
struct Maat_cmd_t* cmd=&(_cmd->cmd);
|
2017-07-04 20:13:36 +08:00
|
|
|
enum MAAT_OPERATION op=_cmd->op;
|
|
|
|
|
|
|
|
|
|
int rule_num=0,i=0,j=0;
|
|
|
|
|
p_m_rule=&(cmd->compile);
|
|
|
|
|
char line[1024];
|
2017-07-06 21:20:24 +08:00
|
|
|
time_t timeout=0;
|
|
|
|
|
if(_cmd->cmd.expire_after>0)
|
|
|
|
|
{
|
|
|
|
|
timeout=feather->server_time+_cmd->cmd.expire_after;
|
|
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
if(op==MAAT_OP_ADD)
|
|
|
|
|
{
|
|
|
|
|
snprintf(line,sizeof(line),"%d\t%d\t%hhd\t%hhd\t%hhd\t0\t%s\t1\t%d",p_m_rule->config_id
|
2017-08-12 11:13:47 +08:00
|
|
|
,p_m_rule->service_id
|
|
|
|
|
,p_m_rule->action
|
|
|
|
|
,p_m_rule->do_blacklist
|
|
|
|
|
,p_m_rule->do_log
|
|
|
|
|
,p_m_rule->service_defined
|
|
|
|
|
,cmd->group_num);
|
2017-07-06 21:20:24 +08:00
|
|
|
set_serial_rule(list+rule_num,MAAT_OP_ADD,cmd->compile.config_id,cmd->label_id,feather->compile_tn,line,timeout);
|
2017-07-04 20:13:36 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-06 21:20:24 +08:00
|
|
|
set_serial_rule(list+rule_num,MAAT_OP_DEL,cmd->compile.config_id,cmd->label_id,feather->compile_tn,NULL,timeout);
|
2017-07-04 20:13:36 +08:00
|
|
|
}
|
|
|
|
|
rule_num++;
|
2017-07-05 20:58:38 +08:00
|
|
|
for(i=0;i<cmd->group_num;i++)
|
2017-07-04 20:13:36 +08:00
|
|
|
{
|
|
|
|
|
p_group=&(cmd->groups[i]);
|
|
|
|
|
if(op==MAAT_OP_ADD)
|
|
|
|
|
{
|
2017-07-06 21:20:24 +08:00
|
|
|
if(feather->AUTO_NUMBERING_ON==1)
|
|
|
|
|
{
|
|
|
|
|
p_group->group_id=feather->base_grp_seq;
|
|
|
|
|
feather->base_grp_seq++;
|
|
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
snprintf(line,sizeof(line),"%d\t%d\t1",p_group->group_id
|
2017-08-12 11:13:47 +08:00
|
|
|
,p_m_rule->config_id);
|
2017-07-07 11:04:11 +08:00
|
|
|
set_serial_rule(list+rule_num,MAAT_OP_ADD,p_group->group_id,0,feather->group_tn,line,timeout);
|
2017-07-04 20:13:36 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-07-07 11:04:11 +08:00
|
|
|
set_serial_rule(list+rule_num,MAAT_OP_DEL,p_group->group_id,0,feather->group_tn,NULL,0);
|
2017-07-04 20:13:36 +08:00
|
|
|
}
|
|
|
|
|
rule_num++;
|
|
|
|
|
if(p_group->regions==NULL)//group reuse.
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
for(j=0;j<p_group->region_num;j++)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-04 20:13:36 +08:00
|
|
|
p_region=&(p_group->regions[j]);
|
|
|
|
|
if(op==MAAT_OP_ADD)
|
|
|
|
|
{
|
2017-07-06 21:20:24 +08:00
|
|
|
if(feather->AUTO_NUMBERING_ON==1)
|
|
|
|
|
{
|
|
|
|
|
p_region->region_id=feather->base_rgn_seq;
|
|
|
|
|
feather->base_rgn_seq++;
|
|
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
serialize_region(p_region, p_group->group_id, line, sizeof(line));
|
|
|
|
|
set_serial_rule(list+rule_num,MAAT_OP_ADD
|
2017-08-16 18:23:09 +08:00
|
|
|
,p_region->region_id,0,p_region->table_name,line,timeout);
|
2017-07-04 20:13:36 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
set_serial_rule(list+rule_num,MAAT_OP_DEL
|
2017-08-12 11:13:47 +08:00
|
|
|
,p_region->region_id,0,p_region->table_name,NULL,0);
|
2017-07-04 20:13:36 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
rule_num++;
|
2017-07-03 12:53:12 +08:00
|
|
|
}
|
|
|
|
|
}
|
2017-07-05 20:58:38 +08:00
|
|
|
assert(rule_num<=size);
|
2017-07-04 20:13:36 +08:00
|
|
|
return rule_num;
|
2017-07-03 12:53:12 +08:00
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
int mr_transaction_success(redisReply* data_reply)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-05 20:58:38 +08:00
|
|
|
if(data_reply->type==REDIS_REPLY_NIL)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-05 20:58:38 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
2017-07-03 12:53:12 +08:00
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
}
|
2017-08-24 17:39:40 +08:00
|
|
|
int _exec_serial_rule(redisContext* ctx,struct serial_rule_t* s_rule,int serial_rule_num, long long server_time);
|
2017-08-10 18:31:20 +08:00
|
|
|
int exec_serial_rule(redisContext* ctx,struct serial_rule_t* s_rule,int serial_rule_num, long long server_time)
|
2017-08-24 17:39:40 +08:00
|
|
|
{
|
|
|
|
|
int max_redis_batch=8*1024,batch_cnt=0;
|
|
|
|
|
int success_cnt=0,ret=0;
|
|
|
|
|
while(success_cnt<serial_rule_num)
|
|
|
|
|
{
|
|
|
|
|
batch_cnt=MIN(serial_rule_num-success_cnt,max_redis_batch);
|
|
|
|
|
ret=_exec_serial_rule(ctx,s_rule+success_cnt,batch_cnt, server_time);
|
|
|
|
|
if(ret==1)
|
|
|
|
|
{
|
|
|
|
|
success_cnt+=batch_cnt;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return success_cnt;
|
|
|
|
|
}
|
|
|
|
|
int _exec_serial_rule(redisContext* ctx,struct serial_rule_t* s_rule,int serial_rule_num, long long server_time)
|
2017-07-07 11:04:11 +08:00
|
|
|
{
|
|
|
|
|
int append_cmd_cnt=0,i=0;
|
|
|
|
|
long long maat_redis_version=0;
|
|
|
|
|
redisReply* data_reply=NULL;
|
|
|
|
|
int redis_transaction_success=1;
|
|
|
|
|
data_reply=_wrap_redisCommand(ctx, "WATCH MAAT_VERSION");
|
|
|
|
|
freeReplyObject(data_reply);
|
|
|
|
|
data_reply=_wrap_redisCommand(ctx, "GET MAAT_VERSION");
|
|
|
|
|
maat_redis_version=read_redis_integer(data_reply);
|
|
|
|
|
maat_redis_version++;
|
|
|
|
|
freeReplyObject(data_reply);
|
|
|
|
|
data_reply=_wrap_redisCommand(ctx,"MULTI");
|
|
|
|
|
freeReplyObject(data_reply);
|
|
|
|
|
append_cmd_cnt=0;
|
2017-08-16 18:23:09 +08:00
|
|
|
assert(server_time>0);
|
2017-07-07 11:04:11 +08:00
|
|
|
for(i=0;i<serial_rule_num;i++)
|
|
|
|
|
{
|
|
|
|
|
if(s_rule[i].op==MAAT_OP_ADD)
|
|
|
|
|
{
|
|
|
|
|
redisAppendCommand(ctx,"SET %s:%s,%d %s",rm_key_prefix[MAAT_OP_ADD]
|
2017-08-12 11:13:47 +08:00
|
|
|
,s_rule[i].table_name
|
|
|
|
|
,s_rule[i].rule_id
|
|
|
|
|
,s_rule[i].table_line);
|
2017-07-07 11:04:11 +08:00
|
|
|
append_cmd_cnt++;
|
|
|
|
|
//NX: Don't update already exisiting elements. Always add new elements.
|
|
|
|
|
redisAppendCommand(ctx,"ZADD %s NX %lld ADD,%s,%d",rm_status_sset
|
2017-08-12 11:13:47 +08:00
|
|
|
,maat_redis_version
|
|
|
|
|
,s_rule[i].table_name
|
|
|
|
|
,s_rule[i].rule_id);
|
2017-07-07 11:04:11 +08:00
|
|
|
append_cmd_cnt++;
|
|
|
|
|
if(s_rule[i].timeout>0)
|
|
|
|
|
{
|
|
|
|
|
redisAppendCommand(ctx,"ZADD %s NX %lld %s,%d",rm_expire_sset
|
2017-08-12 11:13:47 +08:00
|
|
|
,s_rule[i].timeout
|
|
|
|
|
,s_rule[i].table_name
|
|
|
|
|
,s_rule[i].rule_id);
|
2017-07-07 11:04:11 +08:00
|
|
|
append_cmd_cnt++;
|
|
|
|
|
}
|
|
|
|
|
if(s_rule[i].label_id>0)
|
|
|
|
|
{
|
|
|
|
|
redisAppendCommand(ctx,"ZADD %s NX %d %d",rm_label_sset
|
2017-08-12 11:13:47 +08:00
|
|
|
,s_rule[i].label_id
|
|
|
|
|
,s_rule[i].rule_id);
|
2017-07-07 11:04:11 +08:00
|
|
|
append_cmd_cnt++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
append_cmd_cnt+=del_rule_from_redis(ctx,s_rule+i,maat_redis_version);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
redisAppendCommand(ctx,"INCRBY MAAT_VERSION 1");
|
|
|
|
|
append_cmd_cnt++;
|
2017-08-10 18:31:20 +08:00
|
|
|
redisAppendCommand(ctx,"ZADD %s NX %d %d",rm_version_sset,server_time,maat_redis_version);
|
|
|
|
|
append_cmd_cnt++;
|
2017-07-07 11:04:11 +08:00
|
|
|
redisAppendCommand(ctx,"EXEC");
|
|
|
|
|
append_cmd_cnt++;
|
|
|
|
|
redis_transaction_success=1;
|
|
|
|
|
for(i=0;i<append_cmd_cnt;i++)
|
|
|
|
|
{
|
|
|
|
|
_wrap_redisGetReply(ctx, &data_reply);
|
|
|
|
|
if(0==mr_transaction_success(data_reply))
|
|
|
|
|
{
|
|
|
|
|
redis_transaction_success=0;
|
|
|
|
|
}
|
|
|
|
|
freeReplyObject(data_reply);
|
|
|
|
|
}
|
|
|
|
|
return redis_transaction_success;
|
|
|
|
|
}
|
2017-07-06 21:20:24 +08:00
|
|
|
int fix_table_name(_Maat_feather_t* feather,struct Maat_cmd_t* cmd)
|
2017-07-04 20:13:36 +08:00
|
|
|
{
|
|
|
|
|
int i=0,j=0,ret=0;
|
|
|
|
|
const char *table_name=NULL;
|
|
|
|
|
int table_id=0;
|
|
|
|
|
struct Maat_group_t* p_group=NULL;
|
|
|
|
|
struct Maat_region_t* p_region=NULL;
|
|
|
|
|
enum MAAT_TABLE_TYPE table_type;
|
2017-08-12 11:13:47 +08:00
|
|
|
|
2017-07-05 20:58:38 +08:00
|
|
|
struct _Maat_compile_inner_t *compile_rule=NULL;
|
|
|
|
|
if(feather->scanner!=NULL)
|
|
|
|
|
{
|
|
|
|
|
compile_rule=(struct _Maat_compile_inner_t*)HASH_fetch_by_id(feather->scanner->compile_hash, cmd->compile.config_id);
|
|
|
|
|
if(compile_rule!=NULL)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(feather->logger,RLOG_LV_FATAL,maat_module
|
2017-08-12 11:13:47 +08:00
|
|
|
,"Maat rule %d already exisits.",cmd->compile.config_id);
|
2017-07-05 20:58:38 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
for(i=0;i<cmd->group_num;i++)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-04 20:13:36 +08:00
|
|
|
p_group=&(cmd->groups[i]);
|
|
|
|
|
for(j=0;j<p_group->region_num;j++)
|
|
|
|
|
{
|
|
|
|
|
p_region=&(p_group->regions[j]);
|
|
|
|
|
table_name=p_region->table_name;
|
|
|
|
|
ret=map_str2int(feather->map_tablename2id, table_name, &table_id);
|
|
|
|
|
if(ret<0)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(feather->logger,RLOG_LV_FATAL,maat_module
|
2017-08-12 11:13:47 +08:00
|
|
|
,"Unknown table %s of Maat_cmd_t[%d]->group[%d]->region[%d]."
|
|
|
|
|
,table_name,cmd->compile.config_id,i,j);
|
2017-07-04 20:13:36 +08:00
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
table_type=type_region2table(p_region);
|
2017-07-05 20:58:38 +08:00
|
|
|
if(table_type!=feather->p_table_info[table_id]->table_type)
|
2017-07-04 20:13:36 +08:00
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(feather->logger,RLOG_LV_FATAL,maat_module
|
2017-08-12 11:13:47 +08:00
|
|
|
,"Table %s not support region type %d of Maat_cmd_t[%d]->group[%d]->region[%d]."
|
|
|
|
|
,table_name
|
|
|
|
|
,p_region->region_type
|
|
|
|
|
,cmd->compile.config_id,i,j);
|
2017-07-04 20:13:36 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
free((char*)p_region->table_name);
|
2017-07-05 20:58:38 +08:00
|
|
|
p_region->table_name=_maat_strdup(feather->p_table_info[table_id]->table_name[0]);
|
2017-07-04 20:13:36 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2017-07-06 21:20:24 +08:00
|
|
|
void check_maat_expiration(redisContext *ctx, void *logger)
|
|
|
|
|
{
|
|
|
|
|
unsigned int i=0,s_rule_num=0;
|
2017-07-07 11:04:11 +08:00
|
|
|
int ret=0;
|
2017-08-24 17:39:40 +08:00
|
|
|
int success_cnt=0;
|
2017-07-06 21:20:24 +08:00
|
|
|
redisReply* data_reply=NULL;
|
|
|
|
|
struct serial_rule_t* s_rule=NULL;
|
2017-07-07 11:04:11 +08:00
|
|
|
long long server_time=0;
|
2017-08-12 11:13:47 +08:00
|
|
|
|
2017-08-10 18:31:20 +08:00
|
|
|
server_time=redis_server_time(ctx);
|
|
|
|
|
|
2017-07-06 21:20:24 +08:00
|
|
|
data_reply=_wrap_redisCommand(ctx, "ZRANGEBYSCORE %s -inf %lld",rm_expire_sset,server_time);
|
2017-07-07 11:04:11 +08:00
|
|
|
if(data_reply->type!=REDIS_REPLY_ARRAY||data_reply->elements==0)
|
2017-07-06 21:20:24 +08:00
|
|
|
{
|
|
|
|
|
freeReplyObject(data_reply);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
s_rule_num=data_reply->elements;
|
2017-07-07 11:04:11 +08:00
|
|
|
s_rule=(struct serial_rule_t*)calloc(sizeof(struct serial_rule_t),s_rule_num);
|
2017-07-06 21:20:24 +08:00
|
|
|
for(i=0;i<s_rule_num;i++)
|
|
|
|
|
{
|
|
|
|
|
s_rule[i].op=MAAT_OP_DEL;
|
2017-07-07 11:04:11 +08:00
|
|
|
ret=sscanf(data_reply->element[i]->str,"%[^,],%d",s_rule[i].table_name,&(s_rule[i].rule_id));
|
2017-07-06 21:20:24 +08:00
|
|
|
assert(ret==2);
|
|
|
|
|
}
|
2017-07-07 11:04:11 +08:00
|
|
|
freeReplyObject(data_reply);
|
2017-08-24 17:39:40 +08:00
|
|
|
success_cnt=exec_serial_rule(ctx,s_rule, s_rule_num,server_time);
|
2017-08-12 11:13:47 +08:00
|
|
|
|
2017-08-24 17:39:40 +08:00
|
|
|
if(success_cnt==(int)s_rule_num)
|
2017-07-06 21:20:24 +08:00
|
|
|
{
|
2017-08-10 18:31:20 +08:00
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_INFO,maat_redis_monitor
|
2017-08-12 11:13:47 +08:00
|
|
|
,"Succesfully expired %d rules in Redis.", s_rule_num);
|
2017-07-06 21:20:24 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-08-10 18:31:20 +08:00
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_INFO,maat_redis_monitor
|
2017-08-12 11:13:47 +08:00
|
|
|
,"Failed to expired %d rules in Redis, try later.", s_rule_num);
|
2017-07-06 21:20:24 +08:00
|
|
|
}
|
2017-08-12 11:13:47 +08:00
|
|
|
|
2017-07-06 21:20:24 +08:00
|
|
|
free(s_rule);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-08-10 18:31:20 +08:00
|
|
|
void cleanup_update_status(redisContext *ctx, void *logger)
|
|
|
|
|
{
|
|
|
|
|
redisReply* reply=NULL,*sub_reply=NULL;
|
|
|
|
|
int append_cmd_cnt=0,i=0;
|
|
|
|
|
long long server_time=0, version_upper_bound=0,version_lower_bound=0,version_num=0,entry_num=0;
|
|
|
|
|
|
|
|
|
|
server_time=redis_server_time(ctx);
|
2017-08-12 11:13:47 +08:00
|
|
|
|
2017-08-10 18:31:20 +08:00
|
|
|
reply=_wrap_redisCommand(ctx,"MULTI");
|
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
redisAppendCommand(ctx, "ZRANGEBYSCORE %s -inf %lld",rm_version_sset,server_time-MAAT_REDIS_SYNC_TIME);
|
|
|
|
|
append_cmd_cnt++;
|
|
|
|
|
redisAppendCommand(ctx, "ZREMRANGEBYSCORE %s -inf %lld",rm_version_sset,server_time-MAAT_REDIS_SYNC_TIME);
|
|
|
|
|
append_cmd_cnt++;
|
|
|
|
|
//consume reply "OK" and "QUEUED".
|
|
|
|
|
for(i=0;i<append_cmd_cnt;i++)
|
|
|
|
|
{
|
|
|
|
|
_wrap_redisGetReply(ctx, &reply);
|
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
reply=NULL;
|
|
|
|
|
}
|
|
|
|
|
reply=_wrap_redisCommand(ctx,"EXEC");
|
|
|
|
|
assert(reply->type==REDIS_REPLY_ARRAY);
|
|
|
|
|
sub_reply=reply->element[0];
|
|
|
|
|
assert(sub_reply->type==REDIS_REPLY_ARRAY);
|
|
|
|
|
version_num=sub_reply->elements;
|
|
|
|
|
if(version_num==0)
|
|
|
|
|
{
|
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
version_lower_bound=read_redis_integer(sub_reply->element[0]);
|
|
|
|
|
version_upper_bound=read_redis_integer(sub_reply->element[sub_reply->elements-1]);
|
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
|
2017-08-15 09:14:44 +08:00
|
|
|
//To deal with maat_version reset to 0, do NOT use -inf as lower bound intentionally.
|
|
|
|
|
reply=_wrap_redisCommand(ctx,"ZREMRANGEBYSCORE %s %lld %lld",rm_status_sset,version_lower_bound,version_upper_bound);
|
2017-08-10 18:31:20 +08:00
|
|
|
entry_num=read_redis_integer(reply);
|
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_INFO,maat_redis_monitor
|
2017-08-12 11:13:47 +08:00
|
|
|
,"Clean up update status from version %lld to %lld (%lld versions, %lld entries)."
|
|
|
|
|
,version_lower_bound
|
|
|
|
|
,version_upper_bound
|
|
|
|
|
,version_num
|
|
|
|
|
,entry_num);
|
2017-08-10 18:31:20 +08:00
|
|
|
|
|
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
void redis_monitor_traverse(unsigned int version,redisContext *c,
|
2017-08-12 11:13:47 +08:00
|
|
|
void (*start)(unsigned int ,int ,void*),//vesion,CM_UPDATE_TYPE_*,u_para
|
|
|
|
|
int (*update)(const char* ,const char*,void* ),//table name ,line ,u_para
|
|
|
|
|
void (*finish)(void*),//u_para
|
|
|
|
|
void* u_para,
|
|
|
|
|
const unsigned char* dec_key,
|
|
|
|
|
_Maat_feather_t* feather)
|
2017-07-04 20:13:36 +08:00
|
|
|
{
|
|
|
|
|
unsigned int rule_num=0,i=0;
|
|
|
|
|
int table_id=0;
|
|
|
|
|
int ret=0;
|
|
|
|
|
struct serial_rule_t* rule_list=NULL;
|
2017-08-24 17:39:40 +08:00
|
|
|
int update_type=CM_UPDATE_TYPE_INC;
|
2017-07-04 20:13:36 +08:00
|
|
|
unsigned int new_version=0;
|
|
|
|
|
enum MAAT_TABLE_TYPE table_type;
|
|
|
|
|
void* logger=feather->logger;
|
2017-07-06 21:20:24 +08:00
|
|
|
if(feather->redis_write_ctx!=NULL)//authorized to write
|
|
|
|
|
{
|
|
|
|
|
//For thread safe, deliberately use redis_read_ctx but not redis_write_ctx.
|
|
|
|
|
check_maat_expiration(feather->redis_read_ctx, logger);
|
2017-08-10 18:31:20 +08:00
|
|
|
cleanup_update_status(feather->redis_read_ctx, logger);
|
2017-07-06 21:20:24 +08:00
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
rule_num=get_rm_key_list(version, c, &rule_list, logger,&new_version, &update_type);
|
2017-08-24 17:39:40 +08:00
|
|
|
if(rule_num<0||(rule_num==0&&update_type==CM_UPDATE_TYPE_INC))//error or nothing changed
|
2017-07-04 20:13:36 +08:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-08-24 17:39:40 +08:00
|
|
|
if(rule_num>0)
|
2017-08-12 11:13:47 +08:00
|
|
|
{
|
2017-08-24 17:39:40 +08:00
|
|
|
ret=get_rm_value(c,rule_list,rule_num, logger);
|
|
|
|
|
if(ret<0)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_INFO,maat_redis_monitor,"Get Redis value failed, abandon update.");
|
|
|
|
|
goto clean_up;
|
|
|
|
|
}
|
2017-08-12 11:13:47 +08:00
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
start(new_version,update_type,u_para);
|
|
|
|
|
for(i=0;i<rule_num;i++)
|
|
|
|
|
{
|
2017-08-24 17:39:40 +08:00
|
|
|
ret=map_str2int(feather->map_tablename2id,rule_list[i].table_name,&table_id);
|
|
|
|
|
if(ret<0)//Unrecognized table.
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
table_type=feather->p_table_info[table_id]->table_type;
|
2017-07-04 20:13:36 +08:00
|
|
|
if(rule_list[i].op==MAAT_OP_DEL)
|
|
|
|
|
{
|
2017-08-07 16:21:23 +08:00
|
|
|
invalidate_line(rule_list[i].table_line,table_type,feather->p_table_info[table_id]->valid_flag_column);
|
2017-07-04 20:13:36 +08:00
|
|
|
}
|
2017-08-07 16:21:23 +08:00
|
|
|
update(rule_list[i].table_name,rule_list[i].table_line,u_para);
|
2017-07-04 20:13:36 +08:00
|
|
|
}
|
|
|
|
|
finish(u_para);
|
2017-08-12 11:13:47 +08:00
|
|
|
clean_up:
|
2017-08-07 16:21:23 +08:00
|
|
|
for(i=0;i<rule_num;i++)
|
|
|
|
|
{
|
|
|
|
|
empty_serial_rules(rule_list+i);
|
|
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
free(rule_list);
|
|
|
|
|
rule_list=NULL;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-06 21:20:24 +08:00
|
|
|
void _maat_copy_region(struct Maat_region_t* dst,const struct Maat_region_t* src)
|
2017-07-04 20:13:36 +08:00
|
|
|
{
|
|
|
|
|
memcpy(dst,src,sizeof(struct Maat_region_t));
|
|
|
|
|
if(src->table_name!=NULL)
|
|
|
|
|
{
|
|
|
|
|
dst->table_name=_maat_strdup(src->table_name);
|
|
|
|
|
}
|
|
|
|
|
switch(dst->region_type)
|
|
|
|
|
{
|
|
|
|
|
case REGION_IP:
|
|
|
|
|
dst->ip_rule.src_ip=_maat_strdup(src->ip_rule.src_ip);
|
|
|
|
|
dst->ip_rule.mask_src_ip=_maat_strdup(src->ip_rule.mask_src_ip);
|
|
|
|
|
dst->ip_rule.dst_ip=_maat_strdup(src->ip_rule.dst_ip);
|
|
|
|
|
dst->ip_rule.mask_src_ip=_maat_strdup(src->ip_rule.mask_src_ip);
|
|
|
|
|
break;
|
|
|
|
|
case REGION_EXPR:
|
2017-07-03 12:53:12 +08:00
|
|
|
dst->expr_rule.keywords=_maat_strdup(src->expr_rule.keywords);
|
|
|
|
|
dst->expr_rule.district=_maat_strdup(src->expr_rule.district);
|
|
|
|
|
break;
|
|
|
|
|
case REGION_INTERVAL:
|
|
|
|
|
break;
|
|
|
|
|
case REGION_DIGEST:
|
|
|
|
|
dst->digest_rule.digest_string=_maat_strdup(src->digest_rule.digest_string);
|
|
|
|
|
break;
|
2017-07-08 16:42:51 +08:00
|
|
|
case REGION_SIMILARITY:
|
2017-07-07 20:51:55 +08:00
|
|
|
dst->similarity_rule.target=_maat_strdup(src->similarity_rule.target);
|
2017-07-03 12:53:12 +08:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-07-06 21:20:24 +08:00
|
|
|
void _maat_empty_region(struct Maat_region_t* p)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-04 20:13:36 +08:00
|
|
|
free((char*)p->table_name);
|
2017-07-03 12:53:12 +08:00
|
|
|
p->table_name=NULL;
|
|
|
|
|
switch(p->region_type)
|
|
|
|
|
{
|
|
|
|
|
case REGION_IP:
|
2017-07-04 20:13:36 +08:00
|
|
|
free((char*)p->ip_rule.src_ip);
|
|
|
|
|
free((char*)p->ip_rule.mask_src_ip);
|
|
|
|
|
free((char*)p->ip_rule.dst_ip);
|
|
|
|
|
free((char*)p->ip_rule.mask_src_ip);
|
2017-07-03 12:53:12 +08:00
|
|
|
break;
|
|
|
|
|
case REGION_EXPR:
|
2017-07-04 20:13:36 +08:00
|
|
|
free((char*)p->expr_rule.keywords);
|
|
|
|
|
free((char*)p->expr_rule.district);
|
2017-07-03 12:53:12 +08:00
|
|
|
break;
|
|
|
|
|
case REGION_INTERVAL:
|
|
|
|
|
break;
|
|
|
|
|
case REGION_DIGEST:
|
2017-07-04 20:13:36 +08:00
|
|
|
free((char*)p->digest_rule.digest_string);
|
2017-07-03 12:53:12 +08:00
|
|
|
break;
|
|
|
|
|
case REGION_SIMILARITY:
|
2017-07-07 20:51:55 +08:00
|
|
|
free((char*)p->similarity_rule.target);
|
2017-07-03 12:53:12 +08:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
memset(p,0,sizeof(struct Maat_region_t));
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
}
|
2017-07-07 11:04:11 +08:00
|
|
|
struct Maat_cmd_t* Maat_create_cmd(const struct Maat_rule_t* rule, int group_num)
|
2017-07-04 20:13:36 +08:00
|
|
|
{
|
|
|
|
|
int i=0;
|
|
|
|
|
struct _Maat_cmd_inner_t* _cmd=(struct _Maat_cmd_inner_t*)calloc(sizeof(struct _Maat_cmd_inner_t),1);
|
|
|
|
|
memcpy(&(_cmd->cmd.compile),rule,sizeof(_cmd->cmd.compile));
|
|
|
|
|
_cmd->ref_cnt=1;
|
|
|
|
|
_cmd->cmd.group_num=group_num;
|
2017-07-05 20:58:38 +08:00
|
|
|
if(group_num>0)
|
|
|
|
|
{
|
|
|
|
|
_cmd->cmd.groups=(struct Maat_group_t*)calloc(sizeof(struct Maat_group_t),group_num);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_cmd->cmd.groups=NULL;
|
|
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
for(i=0;i<group_num;i++)
|
|
|
|
|
{
|
|
|
|
|
_cmd->cmd.groups[i].regions=(struct Maat_region_t*)calloc(sizeof(struct Maat_region_t),1);
|
|
|
|
|
_cmd->region_size[i]=1;
|
|
|
|
|
}
|
2017-07-06 21:20:24 +08:00
|
|
|
return (struct Maat_cmd_t*)_cmd;
|
2017-07-04 20:13:36 +08:00
|
|
|
}
|
2017-07-06 21:20:24 +08:00
|
|
|
int Maat_cmd_set_group(Maat_feather_t feather,int group_id, const struct Maat_region_t* region, enum MAAT_OPERATION op)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-06 21:20:24 +08:00
|
|
|
_Maat_feather_t* _feather=(_Maat_feather_t*)feather;
|
|
|
|
|
if(_feather->AUTO_NUMBERING_ON==1)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
//struct _Maat_group_inner_t* group_inner=NULL;
|
|
|
|
|
//group_inner=(struct _Maat_group_inner_t*)HASH_fetch_by_id(_feather->scanner->group_hash, group_id);
|
|
|
|
|
//NOT implemented yet.
|
|
|
|
|
assert(0);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2017-07-07 11:04:11 +08:00
|
|
|
int Maat_cmd_set_line(Maat_feather_t feather,const struct Maat_line_t* line_rule, enum MAAT_OPERATION op)
|
2017-07-06 21:20:24 +08:00
|
|
|
{
|
|
|
|
|
_Maat_feather_t* _feather=(_Maat_feather_t*)feather;
|
|
|
|
|
int ret=0, table_id=0,retry=0;
|
|
|
|
|
struct serial_rule_t s_rule;
|
2017-08-16 18:23:09 +08:00
|
|
|
long long server_time=0,absolute_expire_time=0;
|
2017-07-07 11:04:11 +08:00
|
|
|
ret=map_str2int(_feather->map_tablename2id, line_rule->table_name, &table_id);
|
2017-07-06 21:20:24 +08:00
|
|
|
if(ret<0)
|
|
|
|
|
{
|
2017-07-07 11:04:11 +08:00
|
|
|
MESA_handle_runtime_log(_feather->logger,RLOG_LV_FATAL,maat_module
|
2017-08-12 11:13:47 +08:00
|
|
|
,"Command set line id %d failed: unknown table %s."
|
|
|
|
|
, line_rule->rule_id
|
|
|
|
|
, line_rule->table_name);
|
2017-07-04 20:13:36 +08:00
|
|
|
|
2017-07-06 21:20:24 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2017-07-07 11:04:11 +08:00
|
|
|
if(TABLE_TYPE_PLUGIN!=_feather->p_table_info[table_id]->table_type)
|
2017-07-04 20:13:36 +08:00
|
|
|
{
|
2017-07-07 11:04:11 +08:00
|
|
|
MESA_handle_runtime_log(_feather->logger,RLOG_LV_FATAL,maat_module
|
2017-08-12 11:13:47 +08:00
|
|
|
,"Command set line id %d failed: table %s is not a plugin table."
|
|
|
|
|
, line_rule->rule_id
|
|
|
|
|
, line_rule->table_name);
|
2017-07-06 21:20:24 +08:00
|
|
|
return -1;
|
2017-07-04 20:13:36 +08:00
|
|
|
}
|
2017-08-16 18:23:09 +08:00
|
|
|
|
|
|
|
|
server_time=redis_server_time(_feather->redis_write_ctx);
|
2017-07-07 11:04:11 +08:00
|
|
|
if( line_rule->expire_after>0)
|
2017-07-06 21:20:24 +08:00
|
|
|
{
|
2017-08-16 18:23:09 +08:00
|
|
|
absolute_expire_time=server_time+line_rule->expire_after;
|
2017-07-06 21:20:24 +08:00
|
|
|
}
|
2017-07-07 11:04:11 +08:00
|
|
|
set_serial_rule(&s_rule, op,line_rule->rule_id,line_rule->label_id,line_rule->table_name,line_rule->table_line, absolute_expire_time);
|
2017-07-06 21:20:24 +08:00
|
|
|
ret=0;
|
2017-08-24 17:39:40 +08:00
|
|
|
while(ret==0)
|
2017-07-06 21:20:24 +08:00
|
|
|
{
|
2017-08-16 18:23:09 +08:00
|
|
|
ret=exec_serial_rule(_feather->redis_write_ctx,&s_rule, 1,server_time);
|
2017-07-06 21:20:24 +08:00
|
|
|
retry++;
|
2017-08-07 16:21:23 +08:00
|
|
|
}
|
|
|
|
|
if(retry>10)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(_feather->logger,RLOG_LV_INFO,maat_module
|
2017-08-12 11:13:47 +08:00
|
|
|
,"Command set line id %d success after retry %d times."
|
|
|
|
|
, line_rule->rule_id
|
|
|
|
|
);
|
2017-07-06 21:20:24 +08:00
|
|
|
}
|
|
|
|
|
return 0;
|
2017-07-04 20:13:36 +08:00
|
|
|
}
|
2017-07-06 21:20:24 +08:00
|
|
|
void Maat_add_region2cmd(struct Maat_cmd_t* cmd,int which_group,const struct Maat_region_t* region)
|
2017-07-04 20:13:36 +08:00
|
|
|
{
|
|
|
|
|
struct _Maat_cmd_inner_t* _cmd=(struct _Maat_cmd_inner_t*)cmd;
|
|
|
|
|
struct Maat_region_t* dst=NULL;
|
|
|
|
|
struct Maat_group_t* p_group=NULL;
|
|
|
|
|
p_group=&(cmd->groups[which_group]);
|
|
|
|
|
assert(which_group<cmd->group_num);
|
2017-07-03 12:53:12 +08:00
|
|
|
assert(region->table_name!=NULL);
|
2017-07-04 20:13:36 +08:00
|
|
|
if(p_group->region_num==_cmd->region_size[which_group])
|
|
|
|
|
{
|
|
|
|
|
_cmd->region_size[which_group]*=2;
|
|
|
|
|
p_group->regions=(struct Maat_region_t*)realloc(p_group->regions,sizeof(struct Maat_region_t)*_cmd->region_size[which_group]);
|
|
|
|
|
}
|
|
|
|
|
dst=&(p_group->regions[p_group->region_num]);
|
|
|
|
|
p_group->region_num++;
|
2017-07-06 21:20:24 +08:00
|
|
|
_maat_copy_region(dst, region);
|
2017-07-03 12:53:12 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-06 21:20:24 +08:00
|
|
|
void Maat_free_cmd(struct Maat_cmd_t* cmd)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-04 20:13:36 +08:00
|
|
|
struct _Maat_cmd_inner_t* _cmd=(struct _Maat_cmd_inner_t*)cmd;
|
2017-07-03 12:53:12 +08:00
|
|
|
int i=0,j=0;
|
|
|
|
|
_cmd->ref_cnt--;
|
|
|
|
|
if(_cmd->ref_cnt>0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
for(i=0;i<cmd->group_num;i++)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-04 20:13:36 +08:00
|
|
|
for(j=0;j<cmd->groups[i].region_num;j++)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-06 21:20:24 +08:00
|
|
|
_maat_empty_region(&(cmd->groups[i].regions[j]));
|
2017-07-03 12:53:12 +08:00
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
free(cmd->groups[i].regions);
|
|
|
|
|
cmd->groups[i].regions=NULL;
|
2017-07-03 12:53:12 +08:00
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
free(cmd->groups);
|
|
|
|
|
cmd->groups=NULL;
|
2017-07-03 12:53:12 +08:00
|
|
|
_cmd->next=NULL;
|
|
|
|
|
free(_cmd);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-07-06 21:20:24 +08:00
|
|
|
int Maat_format_cmd(struct Maat_cmd_t* rule, char* buffer, int size)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-03 19:54:47 +08:00
|
|
|
//TODO
|
|
|
|
|
return 0;
|
2017-07-03 12:53:12 +08:00
|
|
|
}
|
2017-07-06 21:20:24 +08:00
|
|
|
int Maat_cmd(Maat_feather_t feather,struct Maat_cmd_t* raw_rule,enum MAAT_OPERATION op)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
|
|
|
|
int ret=0;
|
2017-07-06 21:20:24 +08:00
|
|
|
ret=Maat_cmd_append(feather,raw_rule,op);
|
2017-07-03 12:53:12 +08:00
|
|
|
if(ret<0)
|
|
|
|
|
{
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2017-07-06 21:20:24 +08:00
|
|
|
ret=Maat_cmd_commit(feather);
|
2017-07-03 12:53:12 +08:00
|
|
|
return ret;
|
|
|
|
|
}
|
2017-07-03 19:54:47 +08:00
|
|
|
|
2017-07-06 21:20:24 +08:00
|
|
|
int Maat_cmd_append(Maat_feather_t feather,struct Maat_cmd_t* cmd,enum MAAT_OPERATION op)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
|
|
|
|
_Maat_feather_t* _feather=(_Maat_feather_t*)feather;
|
2017-07-04 20:13:36 +08:00
|
|
|
struct _Maat_cmd_inner_t* _cmd=(struct _Maat_cmd_inner_t*)cmd;
|
|
|
|
|
int ret=0;
|
2017-07-03 12:53:12 +08:00
|
|
|
_cmd->op=op;
|
|
|
|
|
assert(op==MAAT_OP_DEL||op==MAAT_OP_ADD);
|
2017-07-07 11:04:11 +08:00
|
|
|
assert(_cmd->next==NULL);
|
2017-07-03 19:54:47 +08:00
|
|
|
if(op==MAAT_OP_DEL)
|
|
|
|
|
{
|
2017-07-04 20:13:36 +08:00
|
|
|
ret=reconstruct_cmd(_feather, _cmd);
|
2017-07-03 19:54:47 +08:00
|
|
|
}
|
|
|
|
|
else
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-03 19:54:47 +08:00
|
|
|
ret=fix_table_name(_feather, cmd);
|
|
|
|
|
}
|
|
|
|
|
if(ret<0)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
2017-07-03 12:53:12 +08:00
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
_cmd->ref_cnt++;
|
2017-07-04 10:02:18 +08:00
|
|
|
if(_feather->cmd_q_cnt==0)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-04 20:13:36 +08:00
|
|
|
assert(_feather->cmd_qtail==NULL);
|
|
|
|
|
assert(_feather->cmd_qtail==_feather->cmd_qhead);
|
2017-07-03 12:53:12 +08:00
|
|
|
_feather->cmd_qhead=_feather->cmd_qtail=_cmd;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_feather->cmd_qtail->next=_cmd;
|
|
|
|
|
_feather->cmd_qtail=_cmd;
|
|
|
|
|
}
|
2017-07-04 10:02:18 +08:00
|
|
|
_feather->cmd_q_cnt++;
|
2017-07-03 19:54:47 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
2017-07-03 12:53:12 +08:00
|
|
|
|
2017-07-06 21:20:24 +08:00
|
|
|
int Maat_cmd_commit(Maat_feather_t feather)
|
2017-07-03 19:54:47 +08:00
|
|
|
{
|
|
|
|
|
_Maat_feather_t* _feather=(_Maat_feather_t*)feather;
|
2017-07-03 20:15:39 +08:00
|
|
|
|
2017-07-07 11:04:11 +08:00
|
|
|
int ret=0,i=0,retry=0;
|
2017-07-04 20:13:36 +08:00
|
|
|
int new_region_num=0,new_group_num=0;
|
2017-07-03 19:54:47 +08:00
|
|
|
int serial_rule_num=0,serial_rule_idx=0;
|
2017-07-06 21:20:24 +08:00
|
|
|
int transection_success=1;
|
2017-07-04 20:13:36 +08:00
|
|
|
struct _Maat_cmd_inner_t* p=NULL,*n=NULL;
|
2017-07-03 19:54:47 +08:00
|
|
|
|
|
|
|
|
redisContext* ctx=NULL;
|
|
|
|
|
redisReply* data_reply=NULL;
|
|
|
|
|
|
|
|
|
|
struct serial_rule_t* s_rule=NULL;
|
2017-07-05 20:58:38 +08:00
|
|
|
if(_feather->REDIS_MODE_ON==0)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if(_feather->cmd_q_cnt==0)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2017-07-03 19:54:47 +08:00
|
|
|
if(_feather->redis_write_ctx==NULL)
|
|
|
|
|
{
|
2017-07-07 11:04:11 +08:00
|
|
|
ret=connect_redis_for_write(_feather);
|
|
|
|
|
if(ret!=0)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-03 19:54:47 +08:00
|
|
|
goto error_out;
|
2017-07-03 12:53:12 +08:00
|
|
|
}
|
|
|
|
|
}
|
2017-07-03 19:54:47 +08:00
|
|
|
ctx=_feather->redis_write_ctx;
|
2017-07-04 20:13:36 +08:00
|
|
|
for(i=0,p=_feather->cmd_qhead;i<_feather->cmd_q_cnt;i++)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-04 20:13:36 +08:00
|
|
|
serial_rule_num+=calculate_serial_rule_num(p, &new_region_num, &new_group_num);
|
2017-07-05 20:58:38 +08:00
|
|
|
p=p->next;
|
2017-07-03 12:53:12 +08:00
|
|
|
}
|
2017-07-06 21:20:24 +08:00
|
|
|
_feather->server_time=redis_server_time(ctx);
|
2017-07-03 12:53:12 +08:00
|
|
|
|
2017-07-07 11:04:11 +08:00
|
|
|
if(_feather->AUTO_NUMBERING_ON==1)
|
2017-07-06 21:20:24 +08:00
|
|
|
{
|
|
|
|
|
data_reply=_wrap_redisCommand(ctx,"INCRBY SEQUENCE_REGION %d",new_region_num);
|
|
|
|
|
assert(data_reply->type==REDIS_REPLY_INTEGER);
|
|
|
|
|
_feather->base_rgn_seq=data_reply->integer-new_region_num;
|
|
|
|
|
freeReplyObject(data_reply);
|
2017-08-12 11:13:47 +08:00
|
|
|
|
2017-07-06 21:20:24 +08:00
|
|
|
data_reply=_wrap_redisCommand(ctx,"INCRBY SEQUENCE_GROUP %d",new_group_num);
|
|
|
|
|
assert(data_reply->type==REDIS_REPLY_INTEGER);
|
|
|
|
|
_feather->base_grp_seq=data_reply->integer-new_group_num;
|
|
|
|
|
freeReplyObject(data_reply);
|
|
|
|
|
}
|
2017-07-03 19:54:47 +08:00
|
|
|
s_rule=(struct serial_rule_t*)calloc(sizeof(struct serial_rule_t),serial_rule_num);
|
|
|
|
|
|
2017-07-04 20:13:36 +08:00
|
|
|
for(i=0,p=_feather->cmd_qhead;i<_feather->cmd_q_cnt;i++)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
2017-07-04 20:13:36 +08:00
|
|
|
serial_rule_idx+=build_serial_rule(_feather,p,s_rule, serial_rule_num-serial_rule_idx);
|
2017-07-05 20:58:38 +08:00
|
|
|
p=p->next;
|
2017-07-03 19:54:47 +08:00
|
|
|
}
|
|
|
|
|
assert(serial_rule_idx==serial_rule_num);
|
2017-07-06 21:20:24 +08:00
|
|
|
transection_success=0;
|
2017-08-24 17:39:40 +08:00
|
|
|
while(transection_success<serial_rule_num)
|
2017-07-03 19:54:47 +08:00
|
|
|
{
|
2017-08-24 17:39:40 +08:00
|
|
|
transection_success+=exec_serial_rule(ctx, s_rule+transection_success,serial_rule_num-transection_success,_feather->server_time);
|
|
|
|
|
if(transection_success<serial_rule_num)
|
2017-07-05 20:58:38 +08:00
|
|
|
{
|
|
|
|
|
retry++;
|
|
|
|
|
assert(retry<5);
|
2017-07-03 12:53:12 +08:00
|
|
|
}
|
|
|
|
|
}
|
2017-07-04 20:13:36 +08:00
|
|
|
_feather->cmd_acc_num+=_feather->cmd_q_cnt;
|
2017-07-03 12:53:12 +08:00
|
|
|
error_out:
|
|
|
|
|
p=_feather->cmd_qhead;
|
2017-07-04 10:02:18 +08:00
|
|
|
for(i=0;i<_feather->cmd_q_cnt;i++)
|
2017-07-03 12:53:12 +08:00
|
|
|
{
|
|
|
|
|
n=p->next;
|
2017-07-06 21:20:24 +08:00
|
|
|
Maat_free_cmd((struct Maat_cmd_t* )p);
|
2017-07-03 12:53:12 +08:00
|
|
|
p=n;
|
|
|
|
|
}
|
|
|
|
|
_feather->cmd_qhead=_feather->cmd_qtail=NULL;
|
2017-07-04 10:02:18 +08:00
|
|
|
_feather->cmd_q_cnt=0;
|
2017-08-12 11:13:47 +08:00
|
|
|
|
2017-07-03 19:54:47 +08:00
|
|
|
for(i=0;i<serial_rule_num;i++)
|
|
|
|
|
{
|
2017-07-04 20:13:36 +08:00
|
|
|
empty_serial_rules(s_rule+i);
|
2017-07-03 19:54:47 +08:00
|
|
|
}
|
|
|
|
|
free(s_rule);
|
2017-07-03 12:53:12 +08:00
|
|
|
return ret;
|
|
|
|
|
}
|
2017-07-06 21:20:24 +08:00
|
|
|
long long Maat_cmd_incrby(Maat_feather_t feather,const char* key, int increment)
|
|
|
|
|
{
|
|
|
|
|
_Maat_feather_t* _feather=(_Maat_feather_t*)feather;
|
|
|
|
|
redisReply* data_reply=NULL;
|
|
|
|
|
long long result=0;
|
2017-07-07 11:04:11 +08:00
|
|
|
int ret=0;
|
|
|
|
|
if(_feather->redis_write_ctx==NULL)
|
|
|
|
|
{
|
|
|
|
|
ret=connect_redis_for_write(_feather);
|
|
|
|
|
if(ret!=0)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-06 21:20:24 +08:00
|
|
|
data_reply=_wrap_redisCommand(_feather->redis_write_ctx,"INCRBY %s %d", key, increment);
|
|
|
|
|
assert(data_reply->type==REDIS_REPLY_INTEGER);
|
|
|
|
|
result=data_reply->integer;
|
|
|
|
|
freeReplyObject(data_reply);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2017-07-07 11:04:11 +08:00
|
|
|
int Maat_cmd_select(Maat_feather_t feather, int label_id, int * output_ids, unsigned int size)
|
2017-07-06 21:20:24 +08:00
|
|
|
{
|
|
|
|
|
_Maat_feather_t* _feather=(_Maat_feather_t*)feather;
|
|
|
|
|
redisReply* data_reply=NULL;
|
|
|
|
|
unsigned int i=0;
|
2017-07-07 11:04:11 +08:00
|
|
|
int ret=0;
|
|
|
|
|
if(_feather->redis_write_ctx==NULL)
|
|
|
|
|
{
|
|
|
|
|
ret=connect_redis_for_write(_feather);
|
|
|
|
|
if(ret!=0)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-06 21:20:24 +08:00
|
|
|
data_reply=_wrap_redisCommand(_feather->redis_write_ctx,"ZRANGEBYSCORE %s %d %d"
|
2017-08-12 11:13:47 +08:00
|
|
|
,rm_label_sset
|
|
|
|
|
,label_id
|
|
|
|
|
,label_id);
|
2017-07-07 11:04:11 +08:00
|
|
|
for(i=0;i<data_reply->elements&&i<size;i++)
|
2017-07-06 21:20:24 +08:00
|
|
|
{
|
2017-07-07 11:04:11 +08:00
|
|
|
output_ids[i]=atoi(data_reply->element[i]->str);
|
2017-07-06 21:20:24 +08:00
|
|
|
}
|
|
|
|
|
freeReplyObject(data_reply);
|
|
|
|
|
return i;
|
|
|
|
|
}
|
2017-07-03 12:53:12 +08:00
|
|
|
|