diff --git a/privatebet/bet.c b/privatebet/bet.c index a622248c4c92..96becc20ad47 100644 --- a/privatebet/bet.c +++ b/privatebet/bet.c @@ -53,10 +53,13 @@ uint8_t sharenrs[256]; struct rpcrequest_info *LP_garbage_collector; struct enc_share { uint8_t bytes[sizeof(bits256)+crypto_box_NONCEBYTES+crypto_box_ZEROBYTES]; }; struct enc_share *g_shares=NULL; -//struct enc_share g_shares[CARDS777_MAXPLAYERS*CARDS777_MAXPLAYERS*CARDS777_MAXCARDS]; bits256 v_hash[CARDS777_MAXCARDS][CARDS777_MAXCARDS]; bits256 g_hash[CARDS777_MAXPLAYERS][CARDS777_MAXCARDS]; + +int32_t sharesflag[CARDS777_MAXCARDS][CARDS777_MAXPLAYERS]; +bits256 playershares[CARDS777_MAXCARDS][CARDS777_MAXPLAYERS]; +bits256 deckid; uint32_t LP_rand() { uint32_t retval; @@ -437,63 +440,97 @@ int main(int argc,const char *argv[]) range = (range % 52) + 1; numplayers = (numplayers % (CARDS777_MAXPLAYERS-1)) + 2; + range=2; + numplayers=2; printf("%s:%d, range:%d, numplayers:%d\n",__FUNCTION__,__LINE__,range,numplayers); // for dcv - BET_dcv=calloc(1,sizeof(struct privatebet_info)); - BET_dcv->pubsock = BET_nanosock(1,bindaddr,NN_PUB); - BET_dcv->pullsock = BET_nanosock(1,bindaddr1,NN_PULL); - BET_dcv->maxplayers = (Maxplayers < CARDS777_MAXPLAYERS) ? Maxplayers : CARDS777_MAXPLAYERS; - BET_dcv->maxchips = CARDS777_MAXCHIPS; - BET_dcv->chipsize = CARDS777_CHIPSIZE; - BET_dcv->numplayers=numplayers; - BET_betinfo_set(BET_dcv,"demo",range,0,Maxplayers); - if ( OS_thread_create(&dcv_t,NULL,(void *)BET_hostdcv,(void *)BET_dcv) != 0 ) - { - printf("error launching BET_hostloop for pub.%d pull.%d\n",BET_dcv->pubsock,BET_dcv->pullsock); - exit(-1); - } + if((argc==2)&&(strcmp(argv[1],"dcv")==0)) + { + BET_dcv=calloc(1,sizeof(struct privatebet_info)); + BET_dcv->pubsock = BET_nanosock(1,bindaddr,NN_PUB); + BET_dcv->pullsock = BET_nanosock(1,bindaddr1,NN_PULL); + BET_dcv->maxplayers = (Maxplayers < CARDS777_MAXPLAYERS) ? Maxplayers : CARDS777_MAXPLAYERS; + BET_dcv->maxchips = CARDS777_MAXCHIPS; + BET_dcv->chipsize = CARDS777_CHIPSIZE; + BET_dcv->numplayers=numplayers; + BET_betinfo_set(BET_dcv,"demo",range,0,Maxplayers); + if ( OS_thread_create(&dcv_t,NULL,(void *)BET_hostdcv,(void *)BET_dcv) != 0 ) + { + printf("error launching BET_hostloop for pub.%d pull.%d\n",BET_dcv->pubsock,BET_dcv->pullsock); + exit(-1); + } + if(pthread_join(dcv_t,NULL)) + { + printf("\nError in joining the main thread for dcv"); + } + } // for bvv - BET_bvv=calloc(1,sizeof(struct privatebet_info)); - BET_bvv->subsock = BET_nanosock(0,bindaddr,NN_SUB); - BET_bvv->pushsock = BET_nanosock(0,bindaddr1,NN_PUSH); - BET_bvv->maxplayers = (Maxplayers < CARDS777_MAXPLAYERS) ? Maxplayers : CARDS777_MAXPLAYERS; - BET_bvv->maxchips = CARDS777_MAXCHIPS; - BET_bvv->chipsize = CARDS777_CHIPSIZE; - BET_bvv->numplayers=numplayers; - BET_bvv->myplayerid=0; - BET_betinfo_set(BET_bvv,"demo",range,0,Maxplayers); - if ( OS_thread_create(&bvv_t,NULL,(void *)BET_clientbvv,(void *)BET_bvv) != 0 ) - { - printf("error launching BET_clientloop for sub.%d push.%d\n",BET_bvv->subsock,BET_bvv->pushsock); - exit(-1); - } + else if((argc==2)&&(strcmp(argv[1],"bvv")==0)) + { + BET_bvv=calloc(1,sizeof(struct privatebet_info)); + BET_bvv->subsock = BET_nanosock(0,bindaddr,NN_SUB); + BET_bvv->pushsock = BET_nanosock(0,bindaddr1,NN_PUSH); + BET_bvv->maxplayers = (Maxplayers < CARDS777_MAXPLAYERS) ? Maxplayers : CARDS777_MAXPLAYERS; + BET_bvv->maxchips = CARDS777_MAXCHIPS; + BET_bvv->chipsize = CARDS777_CHIPSIZE; + BET_bvv->numplayers=numplayers; + BET_bvv->myplayerid=0; + BET_betinfo_set(BET_bvv,"demo",range,0,Maxplayers); + if ( OS_thread_create(&bvv_t,NULL,(void *)BET_clientbvv,(void *)BET_bvv) != 0 ) + { + printf("error launching BET_clientloop for sub.%d push.%d\n",BET_bvv->subsock,BET_bvv->pushsock); + exit(-1); + } + if(pthread_join(bvv_t,NULL)) + { + printf("\nError in joining the main thread for bvvv"); + } + } // for players - BET_players=calloc(numplayers,sizeof(struct privatebet_info*)); - for(int i=0;isubsock = BET_nanosock(0,bindaddr,NN_SUB); + BET_players[i]->pushsock = BET_nanosock(0,bindaddr1,NN_PUSH); + BET_players[i]->maxplayers = (Maxplayers < CARDS777_MAXPLAYERS) ? Maxplayers : CARDS777_MAXPLAYERS; + BET_players[i]->maxchips = CARDS777_MAXCHIPS; + BET_players[i]->chipsize = CARDS777_CHIPSIZE; + BET_players[i]->numplayers=numplayers; + BET_players[i]->myplayerid=atoi(argv[2]); + BET_betinfo_set(BET_players[i],"demo",range,0,Maxplayers); + if (OS_thread_create(&players_t[i],NULL,(void *)BET_clientplayer,(void *)BET_players[i]) != 0 ) + { + printf("error launching BET_clientloop for sub.%d push.%d\n",BET_players[i]->subsock,BET_players[i]->pushsock); + exit(-1); + } + /*}*/ + /*for(int i=0;isubsock = BET_nanosock(0,bindaddr,NN_SUB); - BET_players[i]->pushsock = BET_nanosock(0,bindaddr1,NN_PUSH); - BET_players[i]->maxplayers = (Maxplayers < CARDS777_MAXPLAYERS) ? Maxplayers : CARDS777_MAXPLAYERS; - BET_players[i]->maxchips = CARDS777_MAXCHIPS; - BET_players[i]->chipsize = CARDS777_CHIPSIZE; - BET_players[i]->numplayers=numplayers; - BET_players[i]->myplayerid=i; - BET_betinfo_set(BET_players[i],"demo",range,0,Maxplayers); - if (OS_thread_create(&players_t[i],NULL,(void *)BET_clientplayer,(void *)BET_players[i]) != 0 ) - { - printf("error launching BET_clientloop for sub.%d push.%d\n",BET_players[i]->subsock,BET_players[i]->pushsock); - exit(-1); - } + printf("\nInvalid Usage"); + printf("\nFor DCV: ./bet dcv"); + printf("\nFor BVV: ./bet bvv"); + printf("\nFor Player: ./bet player player_id"); } - + #if 0 if(pthread_join(dcv_t,NULL)) { printf("\nError in joining the main thread for dcv"); @@ -511,6 +548,7 @@ int main(int argc,const char *argv[]) printf("\nError in joining the main thread for player %d",i); } } + #endif return 0; } @@ -777,35 +815,67 @@ bits256 t_sg777_player_decode(struct privatebet_info *bet,int32_t cardID,int num uint8_t **shares,flag=0; uint32_t playerid; char share_str[177]; + pthread_t t_req,t_rcv,t_res; + struct privatebet_share *share_info=NULL; + + share_info=calloc(1,sizeof(struct privatebet_share)); + share_info->bvv_public_key=public_key_b; + share_info->player_key=key; + share_info->subsock=bet->subsock; + share_info->myplayerid=bet->myplayerid; + share_info->range=bet->range; + share_info->numplayers=bet->numplayers; + share_info->pushsock=bet->pushsock; + + + if ( OS_thread_create(&t_res,NULL,(void *)BET_response,share_info) != 0 ) + { + printf("error launching BET_response thread"); + exit(-1); + } + sleep(5); + if ( OS_thread_create(&t_req,NULL,(void *)BET_request,share_info) != 0 ) + { + printf("error launching BET_request thread"); + exit(-1); + } + if(pthread_join(t_req,NULL)) + { + printf("\nError in joining the main thread for t_req"); + } + if(pthread_join(t_res,NULL)) + { + printf("\nError in joining the main thread for t_res"); + } + + for(int i=0;irange;i++) + { + printf("\ncard:%d",i); + for(int j=0;jnumplayers;j++) + { + printf("\nshare:%s",bits256_str(str,playershares[i][j])); + } + } + + + #if 1 shares=calloc(numplayers,sizeof(uint8_t*)); for(i=0;imyplayerid){ - tmp=BET_request_share(cardID,i,bet,public_key_b,key); - } - else{ - temp=g_shares[bet->myplayerid*bet->numplayers*bet->range + (cardID*bet->numplayers + bet->myplayerid)]; - recvlen = sizeof(temp); - if ( (ptr= BET_decrypt(decipher,sizeof(decipher),public_key_b,key.priv,temp.bytes,&recvlen)) == 0 ) - printf("decrypt error "); - else - { - memcpy(tmp.bytes,ptr,recvlen); - } - } - memcpy(cardshares[i].bytes,tmp.bytes,sizeof(bits256)); - } + M=(numplayers/2)+1; for(i=0;imyplayerid; + for (i=0; irange;i++) + { + for(int j=0;jnumplayers;j++) + { + sharesflag[i][j]=0; + } + } + + for(int i=0;irange;i++) + { + for(int j=0;jnumplayers;j++) + { + if((j!=shareInfo->myplayerid) && (sharesflag[i][j]==0)) + { + shareReq=cJSON_CreateObject(); + cJSON_AddStringToObject(shareReq,"messageid","request_share"); + cJSON_AddNumberToObject(shareReq,"ofCardID",i); + cJSON_AddNumberToObject(shareReq,"ofPlayerID",j); + cJSON_AddNumberToObject(shareReq,"forPlayerID",shareInfo->myplayerid); + buf=cJSON_Print(shareReq); + bytes=nn_send(shareInfo->pushsock,buf,strlen(buf),0); + cJSON_Delete(shareReq); + } + else if((j==shareInfo->myplayerid) && (sharesflag[i][j]==0)) { + ofCardID=jint(shareInfo,"ofCardID"); + ofPlayerID=jint(shareInfo,"ofPlayerID"); + forPlayerID=jint(shareInfo,"forPlayerID"); + //temp=g_shares[ofPlayerID*shareInfo->numplayers*shareInfo->range + (ofCardID*shareInfo->numplayers + forPlayerID)]; + temp=g_shares[shareInfo->myplayerid*shareInfo->numplayers*shareInfo->range + (i*shareInfo->numplayers + shareInfo->myplayerid)]; + recvlen = sizeof(temp); + if ( (ptr= BET_decrypt(decipher,sizeof(decipher),shareInfo->bvv_public_key,shareInfo->player_key.priv,temp.bytes,&recvlen)) == 0 ) + printf("decrypt error "); + else + { + memcpy(share.bytes,ptr,recvlen); + playershares[i][j]=share; + sharesflag[i][j]=1; + } + + + } + } + } + +} + +void* BET_response(void* _ptr) +{ + + int32_t ofCardID,ofPlayerID,forPlayerID,subsock,pushsock; + struct enc_share temp; + char str[65],enc_str[177],*buf; + bits256 share; + uint8_t decipher[sizeof(bits256) + 1024],*ptr; int32_t recvlen; + struct privatebet_share *share_info=_ptr; + cJSON *share_res; + char bindaddr[128]="ipc:///tmp/bet.ipc",bindaddr1[128]="ipc:///tmp/bet1.ipc"; + //subsock=BET_nanosock(0,bindaddr,NN_SUB); + //pushsock=BET_nanosock(0,bindaddr1,NN_PUSH); + subsock=share_info->subsock; + pushsock=share_info->pushsock; + + int flag=1; + while(flag) + { + flag=0; + int bytes = nn_recv (subsock, &buf, NN_MSG, 0); + if(bytes>0) + { + + share_res=cJSON_Parse(buf); + if(0==strcmp(cJSON_str(cJSON_GetObjectItem(share_res,"messageid")),"request_share")) + { + ofCardID=jint(share_res,"ofCardID"); + ofPlayerID=jint(share_res,"ofPlayerID"); + forPlayerID=jint(share_res,"forPlayerID"); + if((ofPlayerID==share_info->myplayerid)&&(forPlayerID!=share_info->myplayerid)) + { + temp=g_shares[ofPlayerID*share_info->numplayers*share_info->range + (ofCardID*share_info->numplayers + forPlayerID)]; + recvlen = sizeof(temp); + if ( (ptr= BET_decrypt(decipher,sizeof(decipher),share_info->bvv_public_key,share_info->player_key.priv,temp.bytes,&recvlen)) == 0 ) + printf("decrypt error "); + else + { + memcpy(share.bytes,ptr,recvlen); + cJSON_Delete(share_res); + share_res=cJSON_CreateObject(); + cJSON_AddStringToObject(share_res,"messageid","receive_share"); + cJSON_AddNumberToObject(share_res,"ofCardID",ofCardID); + cJSON_AddNumberToObject(share_res,"ofPlayerID",ofPlayerID); + cJSON_AddNumberToObject(share_res,"forPlayerID",forPlayerID); + jaddbits256(share_res,"share",share); + } + if(pushsock>=0){ + char *buf=NULL; + buf=cJSON_Print(share_res); + int bytes=nn_send(pushsock,buf,strlen(buf),0); + } + } + } + else if(0==strcmp(cJSON_str(cJSON_GetObjectItem(share_res,"messageid")),"receive_share")) + { + ofCardID=jint(share_res,"ofCardID"); + ofPlayerID=jint(share_res,"ofPlayerID"); + forPlayerID=jint(share_res,"forPlayerID"); + if(share_info->myplayerid==forPlayerID) + { + sharesflag[ofCardID][ofPlayerID]=1; + playershares[ofCardID][ofPlayerID]=jbits256(share_res,"share"); + } + } + } + for(int i=0;irange;i++) + { + for(int j=0;jnumplayers;j++) + { + if(sharesflag[i][j]==0) + { + flag=1; + break; + } + } + if(flag) + break; + } + sleep(5); + } + nn_shutdown(pushsock,0); + nn_shutdown(subsock,0); +} + + bits256 BET_request_share(int32_t ofCardID,int32_t ofPlayerID,struct privatebet_info *bet,bits256 bvv_public_key,struct pair256 player_key) { cJSON *shareInfo=NULL; @@ -505,7 +648,7 @@ void* BET_clientplayer(void * _ptr) int32_t permis[CARDS777_MAXCARDS],numcards,numplayers; struct pair256 key;struct privatebet_info *bet = _ptr; char str[65],share_str[177]; - cJSON *playerInfo,*gameInfo,*cjsonplayercards,*cjsonblindedcards,*cjsonshamirshards,*cjsoncardprods,*item; + cJSON *playerInfo,*gameInfo,*cjsonplayercards,*cjsonblindedcards,*cjsonshamirshards,*cjsoncardprods,*item,*cjsong_hash; numplayers=bet->numplayers; numcards=bet->range; @@ -526,15 +669,18 @@ void* BET_clientplayer(void * _ptr) char *rendered=cJSON_Print(playerInfo); int bytes=nn_send(bet->pushsock,rendered,strlen(rendered),0); + printf("\n%s:%d:bytes:%d,buf:%s",__FUNCTION__,__LINE__,bytes,rendered); while (1) { char *buf = NULL; int bytes = nn_recv (bet->subsock, &buf, NN_MSG, 0); if(bytes>0) { + gameInfo=cJSON_Parse(buf); if(0==strcmp(cJSON_str(cJSON_GetObjectItem(gameInfo,"messageid")),"decode")) { + printf("\n%s:%d:%s",__FUNCTION__,__LINE__,buf); public_key_b=jbits256(gameInfo,"public_key_b"); g_shares=(struct enc_share*)malloc(CARDS777_MAXPLAYERS*CARDS777_MAXPLAYERS*CARDS777_MAXCARDS*sizeof(struct enc_share)); cjsonblindedcards=cJSON_GetObjectItem(gameInfo,"blindedcards"); @@ -568,10 +714,12 @@ void* BET_clientplayer(void * _ptr) vcalc_sha256(0,v_hash[i][j].bytes,temp.bytes,sizeof(temp)); } } - for(int i=0;(imyplayerid==0));i++) + + for(int i=0;imyplayerid][i],cardprods[bet->myplayerid],playerprivs,numcards); - if ( bits256_nonz(decoded256) == 0 ) + + if ( bits256_nonz(decoded256) == 0 ) errs++; else { @@ -588,12 +736,16 @@ void* BET_clientplayer(void * _ptr) decoded[i] = k; } } - decodebad += errs; + decodebad += errs; decodegood+= (numcards - errs); + printf("\nCards Decoded:%d, errored:%d",decodegood,decodebad); } else if(0==strcmp(cJSON_str(cJSON_GetObjectItem(gameInfo,"messageid")),"init_d")) { + printf("\n%s:%d:%s",__FUNCTION__,__LINE__,buf); + deckid=jbits256(gameInfo,"deckid"); cjsoncardprods=cJSON_GetObjectItem(gameInfo,"cardprods"); + for(int i=0;ibvv_public_key=public_key_b; + share_info->player_key=key; + share_info->subsock=bet->subsock; + share_info->myplayerid=bet->myplayerid; + share_info->range=bet->range; + share_info->numplayers=bet->numplayers; + share_info->pushsock=bet->pushsock; + BET_response(share_info); + free(share_info); + //BET_give_share(gameInfo,bet,public_key_b,key); } } } @@ -699,7 +875,7 @@ void* BET_clientbvv(void * _ptr) } } char *rendered=cJSON_Print(gameInfo); - nn_send(bet->pubsock,rendered,strlen(rendered),0); + nn_send(bet->pushsock,rendered,strlen(rendered),0); flag=0; } } diff --git a/privatebet/host.c b/privatebet/host.c index f90a720e4057..879e556aa582 100644 --- a/privatebet/host.c +++ b/privatebet/host.c @@ -349,7 +349,7 @@ void* BET_hostdcv(void * _ptr) { uint32_t numplayers,range,playerID,bytes; char str[65]; - cJSON *gameInfo=NULL,*playerInfo=NULL,*item=NULL,*cjsoncardprods=NULL,*cjsonfinalcards=NULL; + cJSON *gameInfo=NULL,*playerInfo=NULL,*item=NULL,*cjsoncardprods=NULL,*cjsonfinalcards=NULL,*cjsong_hash=NULL; bits256 playercards[CARDS777_MAXPLAYERS][CARDS777_MAXCARDS],cardprods[CARDS777_MAXPLAYERS][CARDS777_MAXCARDS],finalcards[CARDS777_MAXPLAYERS][CARDS777_MAXCARDS],deckid; struct privatebet_info *bet = _ptr; range=bet->range; @@ -365,6 +365,7 @@ void* BET_hostdcv(void * _ptr) int bytes=nn_recv(bet->pullsock,&buf,NN_MSG,0); if(bytes>0) { + printf("\n%s:%d,buf:%s",__FUNCTION__,__LINE__,buf); gameInfo=cJSON_Parse(buf); if(0==strcmp(cJSON_str(cJSON_GetObjectItem(gameInfo,"messageid")),"init")) { @@ -404,7 +405,17 @@ void* BET_hostdcv(void * _ptr) { cJSON_AddItemToArray(cjsonfinalcards,cJSON_CreateString(bits256_str(str,finalcards[i][j]))); } - } + } + + cJSON_AddItemToObject(gameInfo,"g_hash",cjsong_hash=cJSON_CreateArray()); + for(int i=0;ipubsock,rendered,strlen(rendered),0); while(1) @@ -414,6 +425,7 @@ void* BET_hostdcv(void * _ptr) if(bytes>0) { bytes=nn_send(bet->pubsock,buf,strlen(buf),0); + } } nn_shutdown(bet->pullsock,0); diff --git a/sh.exe.stackdump b/sh.exe.stackdump new file mode 100644 index 000000000000..5bcd3c242cb1 --- /dev/null +++ b/sh.exe.stackdump @@ -0,0 +1,11 @@ +Stack trace: +Frame Function Args +00180309488 0018005CCCE (0018022C830, 0018021EC39, 00180309488, 000FFFFBA00) +00180309488 00180046419 (00000000002, 00000000003, 00000000002, 000C0000000) +00180309488 00180046452 (00000000002, 00180309798, 00180309488, 00000000008) +00180309488 00180057285 (00000000000, 000FFFFCC18, 000FFFFCC63, 0060003ACC8) +000FFFFCCC0 00180057330 (645C655C725C635C, 695C745C6E5C655C, 6D5C2D5C6C5C615C, 675C615C6E5C615C) +000FFFFCCC0 00180046BD7 (00000000000, 00000000000, 00000000000, 00000000000) +00000000000 001800456B3 (00000000000, 00000000000, 00000000000, 00000000000) +000FFFFFFF0 00180045764 (00000000000, 00000000000, 00000000000, 00000000000) +End of stack trace