int dispatch_init( ) { int ret; ret=_init_policy_list(&process_policy); ret=_init_policy_list(&aspect_policy); policy_head_template=memdb_get_template(DTYPE_DISPATCH,SUBTYPE_POLICY_HEAD); match_rule_template=memdb_get_template(DTYPE_DISPATCH,SUBTYPE_MATCH_HEAD); route_rule_template=memdb_get_template(DTYPE_DISPATCH,SUBTYPE_ROUTE_RULE); return 0; }
int read_default_json_desc(void * root,void * record) { int ret; void * data; void * struct_template; DB_RECORD * db_record=record; void * temp_node; if(db_record->head.type <=0) return -EINVAL; struct_template=memdb_get_template(db_record->head.type,db_record->head.subtype); if(struct_template==NULL) return -EINVAL; ret=Galloc0(&data,struct_size(struct_template)); if(ret<0) return ret; ret=json_2_struct(root,data,struct_template); // namelist->elem_no=json_get_elemno(temp_node); db_record->record=data; ret=memdb_comp_uuid(db_record); if(ret<0) return ret; ret=memdb_store_record(db_record); return ret; }
int read_namelist_json_desc(void * root,void * record) { int ret; struct struct_namelist * namelist; DB_RECORD * db_record=record; void * namelist_template; void * temp_node; ret=Galloc0(&namelist,sizeof(struct struct_namelist)); if(ret<0) return ret; if(db_record->head.type!=DB_NAMELIST) return -EINVAL; temp_node=json_find_elem("elemlist",root); if(temp_node==NULL) return -EINVAL; namelist_template=memdb_get_template(DB_NAMELIST,0); if(namelist_template==NULL) return -EINVAL; ret=json_2_struct(root,namelist,namelist_template); // namelist->elem_no=json_get_elemno(temp_node); db_record->record=namelist; ret=memdb_comp_uuid(db_record); if(ret<0) return ret; ret=memdb_store_record(db_record); return ret; }
int read_recordtype_json_desc(void * root,void * record) { int ret; void * data; void * struct_template; DB_RECORD * db_record=record; DB_RECORD * struct_record; struct struct_desc_record * struct_desc; struct struct_recordtype * recordtype; void * temp_node; if(db_record->head.type <=0) return -EINVAL; struct_template=memdb_get_template(db_record->head.type,db_record->head.subtype); if(struct_template==NULL) return -EINVAL; ret=Galloc0(&recordtype,sizeof(struct struct_recordtype)); if(ret<0) return ret; temp_node=json_find_elem("uuid",root); if(temp_node==NULL) return -EINVAL; char * uuid_str=json_get_valuestr(temp_node); if(!Isvaliduuid(uuid_str)) { struct_record=memdb_find_byname(uuid_str,DB_STRUCT_DESC,0); if(struct_record==NULL) return -EINVAL; Memcpy(recordtype->uuid,struct_record->head.uuid,DIGEST_SIZE); ret=json_remove_node(temp_node); } ret=json_2_struct(root,recordtype,struct_template); // namelist->elem_no=json_get_elemno(temp_node); db_record->record=recordtype; ret=memdb_comp_uuid(db_record); if(ret<0) return ret; ret=memdb_store_record(db_record); if(ret<0) return ret; ret=memdb_register_dynamicdb(recordtype->type,recordtype->subtype); return ret; }
void * find_key_info(void * key_info) { struct trust_demo_keyinfo * in_keyinfo=key_info; struct trust_demo_keyinfo * db_keyinfo; DB_RECORD * record; void * comp_template; int ret; comp_template=memdb_get_template(DTYPE_TRUST_DEMO,SUBTYPE_KEY_INFO); ret=struct_set_flag(comp_template,CUBE_ELEM_FLAG_TEMP,"vtpm_uuid,owner,peer,usage,key_type"); if(ret<0) return NULL; record=memdb_get_first(DTYPE_TRUST_DEMO,SUBTYPE_KEY_INFO); while(record!=NULL) { db_keyinfo=record->record; if(db_keyinfo==NULL) return NULL; if(struct_part_compare(db_keyinfo,in_keyinfo,comp_template,CUBE_ELEM_FLAG_TEMP)) break; record=memdb_get_next(DTYPE_TRUST_DEMO,SUBTYPE_KEY_INFO); } return record; }
int proc_tcm_makeidentity(void * sub_proc,void * recv_msg) { int ret=0; int fd; BYTE local_uuid[DIGEST_SIZE]; BYTE Buf[DIGEST_SIZE*32]; BYTE NameBuf[DIGEST_SIZE*4]; UINT32 result; RECORD(TCM_PIK_DESC,USERINFO) * pik_userinfo; RECORD(MESSAGE,SIZED_BINDATA) req_blob; RECORD(VTCM_IN_KEY,TCM_BIN_KEY) tcm_pik; RECORD(VTCM_IN_KEY,TCM_BIN_PUBKEY) tcm_pikpub; RECORD(TCM_KEY_MANAGE,PRIVATE_KEY) tcm_pik_info; void * new_msg; int key_len; void * tcm_key_template; UINT32 smkHandle; UINT32 ownerHandle; UINT32 keyHandle; UINT32 keyAuthHandle; char uuid[DIGEST_SIZE*2+1]; DB_RECORD * db_record; printf("begin pik makeidentity!\n"); // get pik_userinfo from message ret=message_get_record(recv_msg,&pik_userinfo,0); if(ret<0) return -EINVAL; if(pik_userinfo==NULL) return -EINVAL; // get this node's machine uuid ret=proc_share_data_getvalue("uuid",pik_userinfo->node_uuid); if(ret<0) return ret; // get this node's hostname ret=gethostname(pik_userinfo->node_name,DIGEST_SIZE); if(ret!=0) Memset(pik_userinfo->node_name,0,DIGEST_SIZE); // build tcm session ret=TCM_APCreate(TCM_ET_OWNER, NULL, "ooo", &ownerHandle); printf("ownerHandle is : %x\n",ownerHandle); if(ret<0) { print_cubeerr("TCM_APCreate failed!\n"); return -EINVAL; } ret=TCM_APCreate(TCM_ET_SMK, NULL, "sss", &smkHandle); printf("smkHandle is : %x\n",smkHandle); if(ret<0) { printf("TCM_APCreate failed!\n"); return -EINVAL; } int userinfolen; BYTE * req; int reqlen; // get userinfo blob tcm_key_template=memdb_get_template(TYPE_PAIR(TCM_PIK_DESC,USERINFO)); if(tcm_key_template==NULL) return -EINVAL; userinfolen=struct_2_blob(pik_userinfo,Buf,tcm_key_template); if(userinfolen<0) return userinfolen; db_record=memdb_store(pik_userinfo,TYPE_PAIR(TCM_PIK_DESC,USERINFO),NULL); if(db_record==NULL) return -EINVAL; // do makeidentity ret = TCM_MakeIdentity(ownerHandle, smkHandle, userinfolen,Buf,"kkk", &tcm_pik, &req, &reqlen); if(ret<0) { print_cubeerr("TCM_MakeIdentity failed!\n"); return -EINVAL; } // build an expand message record for req data req_blob.size=reqlen; req_blob.bindata=req; // terminate session ret=TCM_APTerminate(ownerHandle); if(ret<0) { print_cubeerr("TCM_APTerminate failed!\n"); return -EINVAL; } ret=TCM_APTerminate(smkHandle); if(ret<0) { print_cubeerr("TCM_APTerminate failed!\n"); return -EINVAL; } // generate TCM pik info ,left pubkey_uuid for fill tcm_key_template=memdb_get_template(TYPE_PAIR(VTCM_IN_KEY,TCM_BIN_KEY)); if(tcm_key_template==NULL) return -EINVAL; key_len=struct_2_blob(&tcm_pik,Buf,tcm_key_template); if(key_len<0) return key_len; Memset(&tcm_pik_info,0,sizeof(tcm_pik_info)); calculate_context_sm3(Buf,key_len,tcm_pik_info.uuid); Memcpy(tcm_pik_info.vtcm_uuid,local_uuid,DIGEST_SIZE); tcm_pik_info.issmkwrapped=1; tcm_pik_info.key_usage=TCM_KEY_IDENTITY; tcm_pik_info.key_flags=TCM_ISVOLATILE|TCM_PCRIGNOREDONREAD; // store pik file digest_to_uuid(tcm_pik_info.uuid,uuid); uuid[DIGEST_SIZE*2]=0; printf("get pik's uuid is %s!\n",uuid); Strcpy(NameBuf,"tcmkey/"); Strcat(NameBuf,uuid); fd=open(NameBuf,O_CREAT|O_WRONLY,0666); if(fd<0) return fd; write(fd,Buf,key_len); close(fd); // Get pikpub from pik // ret=TCM_ExGetPubkeyFromTcmkey(&tcm_pikpub,&tcm_pik); if(ret!=0) { print_cubeerr("Get Pubpik failed!\n"); return -EINVAL; } tcm_key_template=memdb_get_template(TYPE_PAIR(VTCM_IN_KEY,TCM_BIN_PUBKEY)); if(tcm_key_template==NULL) return -EINVAL; key_len=struct_2_blob(&tcm_pikpub,Buf,tcm_key_template); if(key_len<0) return key_len; calculate_context_sm3(Buf,key_len,tcm_pik_info.pubkey_uuid); db_record=memdb_store(&tcm_pik_info,TYPE_PAIR(TCM_KEY_MANAGE,PRIVATE_KEY),NULL); // build a message and send it new_msg=message_create(TYPE_PAIR(TCM_PIK_DESC,USERINFO),recv_msg); if(new_msg==NULL) return -EINVAL; ret=message_add_record(new_msg,pik_userinfo); if(ret<0) return ret; ret=message_add_expand_data(new_msg,TYPE_PAIR(MESSAGE,SIZED_BINDATA),&req_blob); if(ret<0) return -EINVAL; ret=message_add_expand_data(new_msg,TYPE_PAIR(VTCM_IN_KEY,TCM_BIN_PUBKEY),&tcm_pikpub); if(ret<0) return -EINVAL; ret=ex_module_sendmsg(sub_proc,new_msg); return ret; }
int proc_tcm_activateidentity(void * sub_proc,void * recv_msg) { int ret=0; int fd; BYTE Buf[DIGEST_SIZE*32]; BYTE NameBuf[DIGEST_SIZE*4]; BYTE KeyBuf[DIGEST_SIZE*4]; BYTE digest[DIGEST_SIZE]; UINT32 result; DB_RECORD * db_record; RECORD(TCM_PIK_DESC,USERINFO) * pik_userinfo; RECORD(TCM_PIK_DESC,CADATA) * pik_cadata; RECORD(TCM_PIK_DESC,PIKCERT) * pik_cert; RECORD(VTCM_IN_KEY,TCM_BIN_KEY) tcm_pik; RECORD(VTCM_IN_KEY,TCM_BIN_PUBKEY) tcm_pikpub; RECORD(TCM_KEY_MANAGE,PRIVATE_KEY) * tcm_pik_info; TCM_SYMMETRIC_KEY symmkey; void * new_msg; int key_len; void * tcm_key_template; UINT32 smkHandle; UINT32 ownerHandle; UINT32 keyHandle; UINT32 keyAuthHandle; char uuid[DIGEST_SIZE*2+1]; printf("begin pik activateidentity!\n"); // get pik cadata from message ret=message_get_record(recv_msg,&pik_cadata,0); if(ret<0) return -EINVAL; if(pik_cadata==NULL) return -EINVAL; // find pikinfo record db_record=memdb_find_first(TYPE_PAIR(TCM_KEY_MANAGE,PRIVATE_KEY),"pubkey_uuid",pik_cadata->pikpub_uuid); if(db_record==NULL) { print_cubeerr("can't find pik record!\n"); return -EINVAL; } tcm_pik_info=db_record->record; // get pik file name digest_to_uuid(tcm_pik_info->uuid,uuid); uuid[DIGEST_SIZE*2]=0; printf("get pik's uuid is %s!\n",uuid); Strcpy(NameBuf,"tcmkey/"); Strcat(NameBuf,uuid); ret=TCM_ExLoadTcmKey(&tcm_pik,NameBuf); if(ret!=0) { print_cubeerr("Load TCMKey from file failed!\n"); return ret; } // Load pik to TCM ret=TCM_APCreate(TCM_ET_SMK, NULL, "sss", &smkHandle); printf("smkHandle is : %x\n",smkHandle); if(ret<0) { printf("TCM_APCreate failed!\n"); return -EINVAL; } ret=TCM_LoadKey(smkHandle,NameBuf,&keyHandle); if(ret!=0) { print_cubeerr("TCM_LoadKey failed!\n"); return ret; } ret=TCM_APTerminate(smkHandle); if(ret!=0) { printf("TCM_APTerminate failed!\n"); return ret; } // do the activateidentity ret=TCM_APCreate(TCM_ET_OWNER, NULL, "ooo", &ownerHandle); printf("ownerHandle is : %x\n",ownerHandle); if(ret<0) { print_cubeerr("TCM_APCreate failed!\n"); return -EINVAL; } ret=TCM_APCreate(TCM_ET_KEYHANDLE,keyHandle, "kkk", &keyAuthHandle); printf("pikHandle is : %x\n",keyAuthHandle); if(ret!=0) { printf("TCM_APCreate failed!\n"); return -EINVAL; } ret=TCM_ActivateIdentity(keyHandle,keyAuthHandle,ownerHandle, pik_cadata->symmkey_len,pik_cadata->symmkey,&symmkey,"ooo","kkk"); if(ret!=0) { printf("TCM_ActivateIdentity failed!\n"); return -EINVAL; } ret=TCM_APTerminate(ownerHandle); if(ret<0) { printf("TCM_APTerminate failed!\n"); return -EINVAL; } ret=TCM_APTerminate(keyAuthHandle); if(ret<0) { printf("TCM_APTerminate failed!\n"); return -EINVAL; } ret=TCM_EvictKey(keyHandle); if(ret<0) { printf("TCM_APTerminate failed!\n"); return -EINVAL; } // decrypt cert blob int blobsize; BYTE * cert; int certsize; ret=TCM_ExSymmkeyDecrypt(&symmkey,pik_cadata->cert,pik_cadata->certlen,&cert,&certsize); if(ret!=0) { printf("decrypt cert blob file error!\n"); return -EINVAL; } int offset; for(offset=0;cert[offset]==0;offset++) { if((offset>=16)|| (offset>=certsize)) { print_cubeerr("cert data failed!\n"); return -EINVAL; } } // build pik cert, it is organized by userinfo, pubkey and ca_conts // pik_cert=Talloc0(sizeof(*pik_cert)); if(pik_cert==NULL) return -ENOMEM; tcm_key_template=memdb_get_template(TYPE_PAIR(TCM_PIK_DESC,VERIFYDATA)); if(tcm_key_template==NULL) return -EINVAL; ret=blob_2_struct(cert+offset,&pik_cert->verifydata,tcm_key_template); if(ret<0) return -EINVAL; db_record=memdb_find(pik_cadata->userinfo_uuid,TYPE_PAIR(TCM_PIK_DESC,USERINFO)); if(db_record==NULL) { print_cubeerr("can't find user info data!\n"); return -EINVAL; } pik_userinfo=db_record->record; tcm_key_template=memdb_get_template(TYPE_PAIR(TCM_PIK_DESC,USERINFO)); if(tcm_key_template==NULL) return -EINVAL; ret=struct_clone(pik_userinfo,&pik_cert->userinfo,tcm_key_template); if(ret<0) return -EINVAL; ret=TCM_ExGetPubkeyFromTcmkey(&tcm_pikpub,&tcm_pik); if(ret!=0) { print_cubeerr("Get Pubpik failed!\n"); return -EINVAL; } tcm_key_template=memdb_get_template(TYPE_PAIR(VTCM_IN_KEY,TCM_BIN_PUBKEY)); if(tcm_key_template==NULL) return -EINVAL; ret=struct_clone(&tcm_pikpub,&pik_cert->pikpub,tcm_key_template); if(ret<0) return -EINVAL; // build a message and send it new_msg=message_create(TYPE_PAIR(TCM_PIK_DESC,PIKCERT),recv_msg); if(new_msg==NULL) return -EINVAL; ret=message_add_record(new_msg,pik_cert); if(ret<0) return ret; ret=ex_module_sendmsg(sub_proc,new_msg); return ret; }
int json_2_message(char * json_str,void ** message) { void * root_node; void * head_node; void * tag_node; void * record_node; void * curr_record; void * expand_node; void * curr_expand; void * record_value; void * expand_value; struct message_box * msg_box; MSG_HEAD * msg_head; MSG_EXPAND * msg_expand; int record_no; int expand_no; void * precord; void * pexpand; int i; int ret; char buffer[DIGEST_SIZE]; int offset; int type; int subtype; offset=json_solve_str(&root_node,json_str); if(offset<0) return offset; // get json node's head head_node=json_find_elem("HEAD",root_node); if(head_node==NULL) head_node=json_find_elem("head",root_node); if(head_node==NULL) return -EINVAL; tag_node=json_find_elem("tag",head_node); if(tag_node!=NULL) // default tag value is "MESG" { ret=json_node_getvalue(tag_node,buffer,10); if(ret!=4) return -EINVAL; if(Memcmp(buffer,"MESG",ret)!=0) return -EINVAL; } msg_box=message_init(); msg_head=message_get_head(msg_box); json_2_struct(head_node,msg_head,msg_box->head_template); // get json node's record // init message box ret=message_record_init(msg_box); if(ret<0) return ret; record_node=json_find_elem("RECORD",root_node); if(record_node==NULL) record_node=json_find_elem("record",root_node); if(record_node==NULL) return -EINVAL; curr_record=json_get_first_child(record_node); if(curr_record==NULL) return -EINVAL; char node_name[DIGEST_SIZE*2]; ret=json_node_getname(curr_record,node_name); if(!strcmp(node_name,"BIN_FORMAT")) { BYTE * radix64_string; radix64_string=malloc(4096); if(radix64_string==NULL) return -ENOMEM; ret=json_node_getvalue(curr_record,radix64_string,4096); if(ret<0) return -EINVAL; int radix64_len=strnlen(radix64_string,4096); msg_head->record_size=radix_to_bin_len(radix64_len); msg_box->blob=malloc(msg_head->record_size); if(msg_box->blob==NULL) return -ENOMEM; ret=radix64_to_bin(msg_box->blob,radix64_len,radix64_string); } else { for(i=0;i<msg_head->record_num;i++) { if(curr_record==NULL) return -EINVAL; ret=Galloc0(&precord,struct_size(msg_box->record_template)); if(ret<=0) return -EINVAL; json_2_struct(curr_record,precord,msg_box->record_template); message_add_record(msg_box,precord); curr_record=json_get_next_child(record_node); } } // get json_node's expand expand_no=msg_head->expand_num; msg_head->expand_num=0; expand_node=json_find_elem("EXPAND",root_node); if(expand_node==NULL) expand_node=json_find_elem("expand",root_node); if(expand_node!=NULL) { char buf[20]; void * curr_expand_template; curr_expand=json_get_first_child(expand_node); for(i=0;i<expand_no;i++) { if(curr_expand==NULL) return -EINVAL; ret=Galloc0(&msg_expand,struct_size(message_get_expand_template())); if(ret<0) return -ENOMEM; ret=json_2_struct(curr_expand,&msg_expand,message_get_expand_template()); if(ret<0) return ret; void * tempnode; if((tempnode=json_find_elem(curr_expand,"BIN_DATA"))==NULL) { curr_expand_template=memdb_get_template(msg_expand->type,msg_expand->subtype); if(curr_expand_template==NULL) return -EINVAL; struct_free(msg_expand,message_get_expand_template()); ret=Galloc(&msg_expand,struct_size(curr_expand_template)); if(ret<0) return -ENOMEM; ret=json_2_struct(curr_expand,msg_expand,curr_expand_template); if(ret<0) return ret; } message_add_expand(msg_box,msg_expand); curr_expand=json_get_next_child(expand_node); } } *message=msg_box; msg_box->box_state = MSG_BOX_RECOVER; return offset; }
int read_typelist_json_desc(void * root,void * record) { int ret; struct struct_namelist * namelist; struct struct_namelist * baselist; struct struct_typelist * typelist; struct struct_typelist * basetypelist; int * temp_node; void * namelist_template; void * typelist_template; DB_RECORD * db_record=record; DB_RECORD * namelist_record; ret=Galloc0(&typelist,sizeof(struct struct_typelist)); if(ret<0) return ret; if(db_record->head.type!=DB_TYPELIST) return -EINVAL; // store the namelist (if exists) and compute the uuid temp_node=json_find_elem("uuid",root); if(temp_node == NULL) { // this typelist use namelist describe, // we should finish namelist store first ret=Galloc0(&namelist_record,sizeof(DB_RECORD)); if(ret<0) return ret; namelist_record->head.type=DB_NAMELIST; ret=read_namelist_json_desc(root,namelist_record); if(ret<0) return ret; Memcpy(typelist->uuid,namelist_record->head.uuid,DIGEST_SIZE); namelist=namelist_record->record; typelist->elem_no=namelist->elem_no; } else { typelist_template=memdb_get_template(DB_TYPELIST,0); if(typelist_template==NULL) return -EINVAL; ret=json_2_struct(root,typelist,typelist_template); namelist_record=memdb_find(typelist->uuid,DB_NAMELIST,0); if(namelist_record==NULL) return -EINVAL; namelist=namelist_record->record; if(typelist->elem_no==0) typelist->elem_no=namelist->elem_no; } db_record->record=typelist; ret=memdb_store_record(db_record); // merge the typelist namelist_record=memdb_find_byname("typeenumlist",DB_NAMELIST,0); if(namelist_record==NULL) return -EINVAL; baselist=_merge_namelist(namelist_record->record,db_record->tail); if(baselist==NULL) return -EINVAL; ret=memdb_remove_byname("typeenumlist",DB_NAMELIST,0); if(ret<0) return -EINVAL; memdb_store(baselist,DB_NAMELIST,0,"typeenumlist"); typeenumlist=baselist; return ret; }
int read_subtypelist_json_desc(void * root,void * record) { int ret; struct struct_namelist * namelist; struct struct_subtypelist * subtypelist; int * temp_node; void * namelist_template; void * subtypelist_template; DB_RECORD * db_record=record; DB_RECORD * namelist_record; ret=Galloc0(&subtypelist,sizeof(struct struct_subtypelist)); if(ret<0) return ret; if(db_record->head.type!=DB_SUBTYPELIST) return -EINVAL; // store the namelist (if exists) and compute the uuid temp_node=json_find_elem("uuid",root); if(temp_node == NULL) { // this typelist use namelist describe, // we should finish namelist store first ret=Galloc0(&namelist_record,sizeof(DB_RECORD)); if(ret<0) return ret; namelist_record->head.type=DB_NAMELIST; ret=read_namelist_json_desc(root,namelist_record); if(ret<0) { Free0(namelist_record); return ret; } Memcpy(subtypelist->uuid,namelist_record->head.uuid,DIGEST_SIZE); namelist=namelist_record->record; subtypelist->elem_no=namelist->elem_no; temp_node=json_find_elem("type",root); if(temp_node==NULL) { return -EINVAL; } subtypelist->type=memdb_get_typeno(json_get_valuestr(temp_node)); if(subtypelist->type<0) { return -EINVAL; } } else { subtypelist_template=memdb_get_template(DB_SUBTYPELIST,0); if(subtypelist_template==NULL) return -EINVAL; ret=json_2_struct(root,subtypelist,subtypelist_template); namelist_record=memdb_find(subtypelist->uuid,DB_NAMELIST,0); if(namelist_record==NULL) return -EINVAL; namelist=namelist_record->record; if(subtypelist->elem_no==0) subtypelist->elem_no=namelist->elem_no; } db_record->record=subtypelist; ret=memdb_store_record(db_record); return ret; }
void * dispatch_read_policy(void * policy_node) { void * match_policy_node; void * route_policy_node; void * match_rule_node; void * route_rule_node; void * temp_node; char buffer[1024]; char * temp_str; int ret; MATCH_RULE * temp_match_rule; ROUTE_RULE * temp_route_rule; DISPATCH_POLICY * policy=dispatch_policy_create(); if(policy==NULL) return -EINVAL; temp_node=json_find_elem("sender",policy_node); if(temp_node!=NULL) { ret=json_node_getvalue(temp_node,buffer,1024); if(ret<0) return ret; if(ret>DIGEST_SIZE) return -EINVAL; Memcpy(policy->sender,buffer,ret); } // get the match policy json node match_policy_node=json_find_elem("MATCH_RULES",policy_node); if(match_policy_node==NULL) return -EINVAL; // get the match policy json node route_policy_node=json_find_elem("ROUTE_RULES",policy_node); if(route_policy_node==NULL) return -EINVAL; // read the match rule match_rule_node=json_get_first_child(match_policy_node); while(match_rule_node!=NULL) { void * record_template; ret=Galloc0(&temp_match_rule,sizeof(MATCH_RULE)); if(ret<0) return NULL; ret=json_2_struct(match_rule_node,temp_match_rule,match_rule_template); if(ret<0) return -EINVAL; temp_node=json_find_elem("value",match_rule_node); if(temp_node!=NULL) { void * record_desc; void * value_struct; record_template=memdb_get_template(temp_match_rule->type,temp_match_rule->subtype); if(record_template==NULL) return NULL; record_template=clone_struct_template(record_template); if(record_template==NULL) return NULL; temp_match_rule->match_template=record_template; ret=json_marked_struct(temp_node,record_template,match_flag); if(ret<0) return NULL; ret=Galloc0(&value_struct,struct_size(record_template)); if(ret<0) { free_struct_template(record_template); return NULL; } ret=json_2_part_struct(temp_node,value_struct,temp_match_rule->match_template,match_flag); if(ret<0) return NULL; temp_match_rule->value=value_struct; ret=dispatch_policy_addmatchrule(policy,temp_match_rule); if(ret<0) return NULL; } // __route_policy_add(policy->match_list,temp_match_rule); match_rule_node=json_get_next_child(match_policy_node); } // read the route policy // first,read the main route policy route_rule_node=json_get_first_child(route_policy_node); while(route_rule_node!=NULL) { ret=Galloc0(&temp_route_rule,sizeof(ROUTE_RULE)); if(ret<0) return NULL; temp_route_rule=malloc(sizeof(ROUTE_RULE)); ret=json_2_struct(route_rule_node,temp_route_rule,route_rule_template); if(ret<0) return -EINVAL; ret=dispatch_policy_addrouterule(policy,temp_route_rule); if(ret<0) return NULL; route_rule_node=json_get_next_child(route_policy_node); } return policy; }
int router_dup_activemsg_info (void * message) { int ret; struct message_box * msg_box=message; MSG_HEAD * msg_head; MSG_HEAD * new_msg_head; if(message==NULL) return -EINVAL; void * active_msg=message_get_activemsg(message); if(active_msg==NULL) return 0; if(active_msg==message) { msg_head=message_get_head(message); // msg_head->ljump++; return 0; } msg_head=message_get_head(active_msg); new_msg_head=message_get_head(message); message_set_flow(msg_box,msg_head->flow); message_set_state(msg_box,msg_head->state); // message_set_flag(msg_box,msg_head->flag); message_set_route(msg_box,msg_head->route); new_msg_head->ljump=msg_head->ljump; new_msg_head->rjump=msg_head->rjump; MSG_EXPAND * old_expand; MSG_EXPAND * new_expand; void * flow_expand; ret=message_get_define_expand(active_msg,&old_expand,DTYPE_MSG_EXPAND,SUBTYPE_FLOW_TRACE); if(old_expand!=NULL) { new_expand=Calloc0(sizeof(MSG_EXPAND)); if(new_expand==NULL) return -ENOMEM; Memcpy(new_expand,old_expand,sizeof(MSG_EXPAND_HEAD)); void * struct_template =memdb_get_template(DTYPE_MSG_EXPAND,SUBTYPE_FLOW_TRACE); new_expand->expand = clone_struct(old_expand->expand,struct_template); message_add_expand(message,new_expand); } ret=message_get_define_expand(active_msg,&old_expand,DTYPE_MSG_EXPAND,SUBTYPE_ASPECT_POINT); if(old_expand!=NULL) { new_expand=Calloc0(sizeof(MSG_EXPAND)); if(new_expand==NULL) return -ENOMEM; Memcpy(new_expand,old_expand,sizeof(MSG_EXPAND_HEAD)); void * struct_template =memdb_get_template(DTYPE_MSG_EXPAND,SUBTYPE_ASPECT_POINT); new_expand->expand = clone_struct(old_expand->expand,struct_template); message_add_expand(message,new_expand); } ret=message_get_define_expand(active_msg,&old_expand,DTYPE_MSG_EXPAND,SUBTYPE_ROUTE_RECORD); if(old_expand!=NULL) { new_expand=Calloc0(sizeof(MSG_EXPAND)); if(new_expand==NULL) return -ENOMEM; Memcpy(new_expand,old_expand,sizeof(MSG_EXPAND_HEAD)); void * struct_template =memdb_get_template(DTYPE_MSG_EXPAND,SUBTYPE_ROUTE_RECORD); new_expand->expand = clone_struct(old_expand->expand,struct_template); message_add_expand(message,new_expand); } message_set_policy(message,message_get_policy(active_msg)); return 1; }
int rule_get_target(void * router_rule,void * message,void **result) { BYTE * target; int ret; char buffer[DIGEST_SIZE*2]; ROUTE_RULE * rule= (ROUTE_RULE *)router_rule; *result=NULL; switch(rule->target_type){ case ROUTE_TARGET_NAME: case ROUTE_TARGET_PORT: case ROUTE_TARGET_LOCAL: target=Talloc0(DIGEST_SIZE); Strncpy(target,rule->target_name,DIGEST_SIZE); break; case ROUTE_TARGET_UUID: ret=Strnlen(rule->target_name,DIGEST_SIZE*2); target=Talloc0(DIGEST_SIZE); if(ret ==DIGEST_SIZE*2) { if(target==NULL) return -ENOMEM; ret=uuid_to_digest(rule->target_name,target); if(ret<0) return ret; } else if(ret<DIGEST_SIZE/4*3) { Memcpy(target,rule->target_name,ret); } else return -EINVAL; break; case ROUTE_TARGET_CONN: target=Talloc0(DIGEST_SIZE); if(target==NULL) return -ENOMEM; ret=Strnlen(rule->target_name,DIGEST_SIZE*2); if(ret>DIGEST_SIZE/4*3-1) return -EINVAL; target[0]=':'; Memcpy(target+1,rule->target_name,ret); break; case ROUTE_TARGET_RECORD: { ret=message_read_elem(message,rule->target_name,0,&target); if(ret<0) return -EINVAL; if(target==NULL) return -EINVAL; break; } case ROUTE_TARGET_EXPAND: { int expand_type; int expand_subtype; MSG_EXPAND * expand; void * expand_template; void * expand_data; int len; len=Strlen(rule->target_name); if(len>DIGEST_SIZE*3) return -EINVAL; int i=0; int offset=0; offset=message_read_typestr(rule->target_name,&expand_type,&expand_subtype); if(offset<0) return offset; while(rule->target_name[offset++]!=':') { if(offset>=len) return -EINVAL; } ret = message_get_define_expand(message,&expand,expand_type,expand_subtype); if(ret<0) return -EINVAL; if(expand==NULL) return -EINVAL; expand_template=memdb_get_template(expand_type,expand_subtype); if(expand_template==NULL) return -EINVAL; target=Talloc(DIGEST_SIZE*2+1); ret=struct_read_elem(rule->target_name+offset,expand->expand,target,expand_template); if(ret<0) return ret; if(target==NULL) return -EINVAL; break; } /* case ROUTE_TARGET_SPLIT: return -EINVAL; case ROUTE_TARGET_MIXUUID: { void * json_root; void * json_elem; const int repeat_time=2; char mixvalue[repeat_time][DIGEST_SIZE*2]; char tag[16]; char uuid[DIGEST_SIZE*2]; int i; ret=json_solve_str(&json_root,rule->target_name); if(ret<0) return ret; if(json_get_type(json_root)!=JSON_ELEM_MAP) return -EINVAL; memset(uuid,0,DIGEST_SIZE*2); char * value; char name[DIGEST_SIZE*2]; for(i=0;i<repeat_time;i++) { memset(mixvalue[i],0,DIGEST_SIZE*2); if(i==0) { // first json_elem is the node uuid value json_elem=json_get_first_child(json_root); } else { json_elem=json_get_next_child(json_root); } ret=json_node_getname(json_elem,name); if(ret<0) return ret; if(json_get_type(json_elem)!=JSON_ELEM_STRING) return -EINVAL; ret=json_node_getvalue(json_elem,tag,15); // tag is "RECORD" means this elem defines an elem in the message's record if(strcmp(tag,"RECORD")==0) { void * record; ret=message_read_elem(message,name,0,&record); if(ret<0) return ret; int len=strlen(record); if(len<DIGEST_SIZE*2) { Memcpy(mixvalue[i],record,len); } else { Memcpy(mixvalue[i],record,DIGEST_SIZE*2); } } // tag is "name" means this elem defines remote proc's value else if(strcmp(tag,"NAME")==0) { int len=strlen(name); if(len<DIGEST_SIZE*2) { Memcpy(mixvalue[i],name,len); } else { Memcpy(mixvalue[i],name,DIGEST_SIZE*2); } } else if(strncmp(tag,"EXPAND",6)==0) { char * type=tag+7; if(strlen(type)!=4) { return -EINVAL; } void * expand; ret = message_get_define_expand(message,&expand,type); if(ret<0) return -EINVAL; if(expand==NULL) return -EINVAL; void * struct_template=load_record_template(type); if(struct_template==NULL) return -EINVAL; ret=struct_read_elem(name,expand,mixvalue[i],struct_template); free_struct_template(struct_template); if(ret<0) return ret; } else { return -EINVAL; } if(i==0) continue; if(i==1) { comp_proc_uuid(mixvalue[0],mixvalue[1],uuid); continue; } comp_proc_uuid(uuid,mixvalue[i],uuid); } target=malloc(DIGEST_SIZE*2); Memcpy(target,uuid,DIGEST_SIZE*2); break; } */ case ROUTE_TARGET_ERROR: default: return -EINVAL; } *result=target; return rule->target_type; }