diff options
| author | 姜鹏辉 <[email protected]> | 2020-11-23 21:20:53 +0800 |
|---|---|---|
| committer | 姜鹏辉 <[email protected]> | 2020-11-23 21:20:53 +0800 |
| commit | 6972454742c518e503113e2974822fd49412d8f8 (patch) | |
| tree | 463bbb3898e0989b16ea1eec200d47004e3735e7 | |
| parent | 603c5e466a38dc666f5589b12975cb06b78ce21e (diff) | |
finish http bugs
| -rw-r--r-- | qq_file_send.c | 248 |
1 files changed, 125 insertions, 123 deletions
diff --git a/qq_file_send.c b/qq_file_send.c index 7b22cf7..c72e9b8 100644 --- a/qq_file_send.c +++ b/qq_file_send.c @@ -8,14 +8,15 @@ #include <MESA/stream.h> #include <nirvana_client.h> #include "http.h" -#define LOG_PATH "./log/qq_file/" -#define ONLINE_PUBLIC_SEND 0 +#define LOG_PATH "./log/qq_file/runtime.log" + +#define ONLINE_SEND 0 #define OFFLINE_SEND 1 #define NAME_SIZE 80 -//#define NIRVANA +#define MAX_HTABLE_NUM 24 + +#define NIRVANA -//#define ONLINE_LOCAL_SEND_SMALL 2 -//#define ONLINE_LOCAL_SEND_BIG 3 int QQ_FILE_SEND_VERSION_1_20201109 = 0; void qq_file_send_version_1_20201109() @@ -30,7 +31,8 @@ typedef struct int content_len; char filename[NAME_SIZE]; struct nirvana_streaming_asyctx *ctx; - int next_flag; + uint next_flag; + int len; uchar curdir; }qq_pme_info,*qq_pme_info_p; @@ -51,6 +53,7 @@ typedef struct typedef struct { char filename[NAME_SIZE]; + int len; struct nirvana_streaming_asyctx *ctx; }file_info; @@ -64,64 +67,25 @@ void *runtime_log; struct event_base *ev_base; struct nirvana_asyn_instance *instance_asyn; -void get_next_of_pat(const char *pat, int *next) -{ - next[0] = -1; - int i = 0, j = -1, size_of_pat = strlen(pat); - - while (i < size_of_pat) - { - if (j == -1 || pat[i] == pat[j]) - { - ++i; - ++j; - next[i] = j; - } - else - { - j = next[j]; - } - } -} - /* - * @Description: KMP - * @param: char * str [IN] primary string to be searched,it can be binary - * @param: char * pat [IN] pattern string to find,it can not be binary - * @param: int size_of_str [IN] the length of primary string - * @return: - -1 : not exist - else: position of pattern string in primary string + * @Description: use like strncmp + * @param: the main string + * @param: the pattern string to be searched + * @param: the len of pattern sting + * @return: 0 : success + -1 : fail */ -int kmp(char *str, char *pat, int size_of_str) +int bicmp(char *str,char *ptn,int len) { - int i = 0, j = 0, size_of_pat = strlen(pat); - int next[256]; - get_next_of_pat(pat, next); - - while (i < size_of_str && j < size_of_pat) - { - if (j == -1 || str[i] == pat[j]) - { - ++i; - ++j; - } - else - { - j = next[j]; - } - } - if (j == size_of_pat) - { - return i - j; - } - else + int i; + for(i = 0; i < len; i++) { - return -1; + if(str[i] != ptn[i]) + return -1; } + return 0; } - /* * @Description: Processing payload and extracting separator * @param: payload @@ -135,12 +99,12 @@ int parse_separator(char **payload,int payload_len,struct_separator_feature sepa { int uuid_len = 35; //this is a fixed value and will not change //match by header - if(strncmp(*payload,separator_feature.separator_header,separator_feature.header_len) != 0) + if( bicmp(*payload,separator_feature.separator_header, separator_feature.header_len) != 0) { return -1; } // match by the suffix which is after uuid - if( strncmp( (*payload)+separator_feature.uuid_deviation + uuid_len+1,separator_feature.uuid_suffix,separator_feature.uuid_suffix_len) != 0) + if( bicmp( (*payload)+separator_feature.uuid_deviation + uuid_len+1, separator_feature.uuid_suffix,separator_feature.uuid_suffix_len) != 0) { return -2; } @@ -157,25 +121,43 @@ int parse_separator(char **payload,int payload_len,struct_separator_feature sepa /* + * @Description: find end suffix in payload + * @param: + * @param: + * @param: + * @return: =0 : success + <0 : fail +*/ +int parse_end_suffix(char *payload,int payload_len,struct_separator_feature separator_feature) +{ + + if( bicmp(separator_feature.end_suffix,payload,separator_feature.end_suffix_len) != 0) + { + MESA_handle_runtime_log(runtime_log,RLOG_LV_DEBUG,"match","end suffix fail",payload_len); + return -1; + } + MESA_handle_runtime_log(runtime_log,RLOG_LV_DEBUG,"match","end suffix success",payload_len); + return 0; +} + +/* * @Description: Converts a string in hex to decimal(int) - * @param: char - * @return: int + * @param: a hex char ,range : 0-9a-f + * @return: int , range : 0-15 */ int hexch_to_int(char ch) { - int res; if ((ch >= 'A') && (ch <='F')) { - res = 10+ch-'A'; + return 10+ch-'A'; } else if ((ch >= 'a') && (ch <='f')) { - res = 10+ch-'a'; + return 10+ch-'a'; } else{ - res = ch-'0'; + return ch-'0'; } - return res; } /* @@ -200,7 +182,7 @@ void str_to_ascii(char *in_str,int str_len,char* out_str) * @param: [IN] profile name * @param: [OUT] p_separator_feature * @param: [IN] section name - * @param: [IN] feature type eg.ONLINE_PUBLIC_SEND/OFFLINE_SEND + * @param: [IN] feature type eg ONLINE_SEND/OFFLINE_SEND * @return: 0 : success >0: fail @@ -217,8 +199,8 @@ int read_profile_of_separator(const char *inf_file,struct_separator_feature *p_s read_res += MESA_load_profile_int_nodef(inf_file,section,"header_len",&(p_separator_feature->header_len)); //header - p_separator_feature->separator_header = (char *)malloc(p_separator_feature->header_len); - separator_header_array = (char *)malloc(p_separator_feature->uuid_suffix_len<<2); + p_separator_feature->separator_header = (char *)malloc(p_separator_feature->header_len+1); + separator_header_array = (char *)malloc(p_separator_feature->uuid_suffix_len<<2|1); if(MESA_load_profile_string_nodef(inf_file,section,"separator_header",separator_header_array,p_separator_feature->header_len<<2)<0) { read_res += -1; @@ -234,7 +216,7 @@ int read_profile_of_separator(const char *inf_file,struct_separator_feature *p_s read_res += MESA_load_profile_int_nodef(inf_file,section,"uuid_suffix_len",&(p_separator_feature->uuid_suffix_len)); //uuid_suffix - p_separator_feature->uuid_suffix = (char *)malloc(p_separator_feature->uuid_suffix_len); + p_separator_feature->uuid_suffix = (char *)malloc(p_separator_feature->uuid_suffix_len+1); uuid_suffix_array = (char *)malloc(p_separator_feature->uuid_suffix_len<<2); if(MESA_load_profile_string_nodef(inf_file,section,"uuid_suffix",uuid_suffix_array,p_separator_feature->uuid_suffix_len<<2)<0) { @@ -251,7 +233,7 @@ int read_profile_of_separator(const char *inf_file,struct_separator_feature *p_s read_res += MESA_load_profile_int_nodef(inf_file,section,"end_suffix_len",&(p_separator_feature->end_suffix_len)); //end_suffix - p_separator_feature->end_suffix = (char *)malloc(p_separator_feature->end_suffix_len); + p_separator_feature->end_suffix = (char *)malloc(p_separator_feature->end_suffix_len+1); end_suffix_array = (char *)malloc(p_separator_feature->end_suffix_len<<2); if(MESA_load_profile_string_nodef(inf_file,section,"end_suffix",end_suffix_array,p_separator_feature->end_suffix_len<<2)<0) { @@ -263,16 +245,30 @@ int read_profile_of_separator(const char *inf_file,struct_separator_feature *p_s return read_res; } +/* + * @Description: Used to delete value from hash table + * @param: file_info * + * @return: Null +*/ +void free_fileinfo(void *thisfile) +{ + //thisfile = thisfile; + file_info *p = (file_info *)thisfile; + free(p); +} + /* * @Description: different files have different uuids. modify the filename in pme according to the current uuid * @param: pme + * @param: content len + * @param: send_type * @return: 0: not exist 1: exist */ -int match_uuid(qq_pme_info **pme) +int match_uuid(qq_pme_info **pme,int content_len,int send_type) { qq_pme_info_p qq_pme = *pme; file_info* thisfile = (file_info *) MESA_htable_search(file_htable,(u_char *)qq_pme->uuid,strlen(qq_pme->uuid)); @@ -283,7 +279,8 @@ int match_uuid(qq_pme_info **pme) newfile = (file_info *)malloc(sizeof(file_info)); sprintf(qq_pme->filename,"%s/%d-%s",save_directory,file_cnt,qq_pme->uuid); file_cnt++; - strncpy(newfile->filename,qq_pme->filename,strlen(qq_pme->filename)); + strncpy(newfile->filename,qq_pme->filename,strlen(qq_pme->filename)); + newfile->len = content_len; #ifdef NIRVANA // nirvana section struct nirvana_streaming_asyctx *ctx; @@ -293,9 +290,11 @@ int match_uuid(qq_pme_info **pme) int opt_num = 0; streammeta.msg_type = NVN_MSG_TYPE_DOC_IM; - streammeta.msg_subtype = NVN_MSG_SUBTYPE_IM_QQ_ONLINE; - //NVN_MSG_SUBTYPE_IM_QQ_OFFLINE - streammeta.protocol = NVN_PROTOCOL_HTTP; + if(send_type == ONLINE_SEND) + streammeta.msg_subtype = NVN_MSG_SUBTYPE_IM_QQ_ONLINE; + else + streammeta.msg_subtype = NVN_MSG_SUBTYPE_IM_QQ_OFFLINE; + streammeta.protocol = NVN_PROTOCOL_QQ; streammeta.cont_code = 0; streammeta.live_streaming = 0; @@ -303,7 +302,7 @@ int match_uuid(qq_pme_info **pme) streammeta.dir = DIR_C2S; streammeta.request_single_flow = 0; - calculate_md5_for_progid(filename, strlen(filename), &progid); + calculate_md5_for_progid(newfile->filename, strlen(newfile->filename), &progid); streammeta.balance_seed = caculate_seed_using_sdbm_hash((char*)&progid, sizeof(progid)); ctx = nirvana_streaming_asyn_update_start(instance_asyn, &progid, &streammeta); if(ctx == NULL) @@ -314,16 +313,21 @@ int match_uuid(qq_pme_info **pme) qq_pme->ctx = ctx; newfile->ctx = ctx; #endif - MESA_htable_add(file_htable,(u_char *) qq_pme->uuid,strlen(qq_pme->uuid),(void *)newfile); + MESA_htable_add(file_htable,(u_char *) qq_pme->uuid, strlen(qq_pme->uuid), (void *)newfile); + if (MESA_htable_get_elem_num(file_htable) > MAX_HTABLE_NUM) + { + MESA_handle_runtime_log(runtime_log,RLOG_LV_FATAL,"expire","del oldest htable"); + MESA_htable_del_oldest_manual(file_htable, free_fileinfo, 1); + } return 0; } else { // there is a file point in htable // so change the pme - + thisfile->len += content_len; strncpy(qq_pme->filename,thisfile->filename,strlen(thisfile->filename)); - + qq_pme->len = thisfile->len; #ifdef NIRVANA qq_pme->ctx = thisfile->ctx; #endif @@ -332,20 +336,6 @@ int match_uuid(qq_pme_info **pme) } -/* - * @Description: Used to delete value from hash table - * @param: file_info * - * @return: Null -*/ -void free_fileinfo(void *thisfile) -{ - //thisfile = thisfile; - file_info *p = (file_info *)thisfile; - free(p->filename); - free(p); -} - - /* * @Description: free memory of pme @@ -394,7 +384,7 @@ uchar qq_file_send_entry(stSessionInfo* session_info,void **pme,int thread_seq,s if(res>0) { //printf("bmd5=%s\n",bmd5); - MESA_handle_runtime_log(runtime_log,RLOG_LV_INFO,"extract","find header,bmd5=%s",bmd5); + MESA_handle_runtime_log(runtime_log,RLOG_LV_DEBUG,"extract","find header,bmd5=%s",bmd5); //1aa23fc86a9ea7dbea7c987dcb4bea1f 32Byte @@ -413,17 +403,35 @@ uchar qq_file_send_entry(stSessionInfo* session_info,void **pme,int thread_seq,s break; case HTTP_CONTENT: { + int content_len = session_info->buflen; + char *content = (char *)malloc(content_len); + memcpy(content,session_info->buf,content_len); + if (content_len==0) + break; if (qq_pme->curdir != a_http->curdir) { - // to do ... - break; + // looking for end suffix + if(qq_pme->send_type == ONLINE_SEND) + { + res = parse_end_suffix(content,content_len,online_feature); + } + else + { + res = parse_end_suffix(content,content_len,offline_feature); + } + if(res == 0) + { +#ifdef NIRVANA + nirvana_streaming_asyn_update_end(qq_pme->ctx); + nirvana_streaming_asyn_update_cancel(qq_pme->ctx); +#endif + MESA_handle_runtime_log(runtime_log,RLOG_LV_INFO,"finish","save file uuid=%s,size:%d",qq_pme->uuid,qq_pme->len); + MESA_handle_runtime_log(runtime_log,RLOG_LV_INFO,"finish","total get %d file",file_cnt); + MESA_htable_del(file_htable,(u_char *)qq_pme->uuid,strlen(qq_pme->uuid),free_fileinfo); + } } else { - int content_len = session_info->buflen; - char *content = (char *)malloc(content_len); - memcpy(content,session_info->buf,content_len); - if(qq_pme->next_flag == 0) { res = parse_separator(&content,content_len,offline_feature,&qq_pme); @@ -434,7 +442,7 @@ uchar qq_file_send_entry(stSessionInfo* session_info,void **pme,int thread_seq,s if(res < 0) { // If both matches fail, the rule is wrong or the traffic is wrong - MESA_handle_runtime_log(runtime_log,RLOG_LV_FATAL,"extract","match feature fail,check rule!"); + MESA_handle_runtime_log(runtime_log,RLOG_LV_FATAL,"match","feature fail,check rule"); #ifdef NIRVANA nirvana_streaming_asyn_update_end(qq_pme->ctx); @@ -443,44 +451,33 @@ uchar qq_file_send_entry(stSessionInfo* session_info,void **pme,int thread_seq,s return PROT_STATE_DROPME; } else - MESA_handle_runtime_log(runtime_log,RLOG_LV_INFO,"extract","match online feature,uuid:%s",qq_pme->uuid); + MESA_handle_runtime_log(runtime_log,RLOG_LV_INFO,"match","online feature success,uuid:%s",qq_pme->uuid); } else { - MESA_handle_runtime_log(runtime_log,RLOG_LV_INFO,"extract","match offline feature,uuid:%s",qq_pme->uuid); + MESA_handle_runtime_log(runtime_log,RLOG_LV_INFO,"match","offline feature success,uuid:%s",qq_pme->uuid); } qq_pme->next_flag = 1; - match_uuid(&qq_pme); content_len = qq_pme->content_len; - //printf("%s\n",content); - + match_uuid(&qq_pme,content_len,qq_pme->send_type); } else { - //printf("%s\n",content); - ; + ;//may be do nothing } if(locally_save == 1) { FILE *fp = fopen(qq_pme->filename,"ab"); - printf("%d:%d\n",content_len,strlen(content)); fwrite(content,content_len,1,fp); fclose(fp); } #ifdef NIRVANA - nirvana_streaming_asyn_update_data(qq_pme->ctx,payload ,qq_pme->content_len); + nirvana_streaming_asyn_update_data(qq_pme->ctx,content ,content_len); #endif - break; } - } - case OP_STATE_CLOSE: -#ifdef NIRVANA - nirvana_streaming_asyn_update_end(qq_pme->ctx); - nirvana_streaming_asyn_update_cancel(qq_pme->ctx); -#endif - MESA_htable_del(file_htable,(u_char *)qq_pme->uuid,strlen(qq_pme->uuid),free_fileinfo); break; - + } + } return PROT_STATE_GIVEME; @@ -510,10 +507,11 @@ void qq_file_send_init() { printf("qq_file_send init\n"); int read_res = 0; - runtime_log = MESA_create_runtime_log_handle("./runtime.log", 20); - const char *inf_file = "./plug/protocol/qq_file_send/qq_file_send.inf"; - read_res += read_profile_of_separator(inf_file,&online_feature,"ONLINE_FEATURE",ONLINE_PUBLIC_SEND); + runtime_log = MESA_create_runtime_log_handle(LOG_PATH, 10); + + const char *inf_file = "./plug/business/qq_file_send/qq_file_send.inf"; + read_res += read_profile_of_separator(inf_file,&online_feature,"ONLINE_FEATURE",ONLINE_SEND); read_res += read_profile_of_separator(inf_file,&offline_feature,"OFFLINE_FEATURE",OFFLINE_SEND); //read_res += MESA_load_profile_string_nodef(inf_file,"CAPTURE","post_header",post_header,NAME_SIZE); @@ -538,8 +536,12 @@ void qq_file_send_init() // init a hashtable,key is uuid,value is pme file_htable = MESA_htable_born(); int *psafe = (int *)malloc(sizeof(int)); + int *pexpire_time = (int *)malloc(sizeof(int)); *psafe = 1; + *pexpire_time = 60; MESA_htable_set_opt(file_htable,MHO_THREAD_SAFE,psafe,sizeof(int)); + MESA_htable_set_opt(file_htable,MHO_EXPIRE_TIME,pexpire_time,sizeof(int)); + MESA_htable_mature(file_htable); MESA_htable_print_crtl(file_htable,0); |
