📄 newserver.pc
字号:
int checksum(Message_Head1 * phead,Message_Tail * ptail,struct sockaddr_in * p_client_sa){ unsigned int a,b; printf("the real IP is %d\n",p_client_sa->sin_addr.s_addr); printf("the real IP is %s\n",inet_ntoa(p_client_sa->sin_addr)); a=(p_client_sa->sin_addr.s_addr+phead->pack_length)^phead->pack_date; b=ptail->tail_field_data.check_code; printf("caculated check_code is %d\n received check_code is %d\n",a,b); if(a!=b) { printf("packet authentication error\n packet canceled\n"); return -1; } return 0;}describe_body(short int oper_type,short int * precord_numb,DESCRIPT * pdescript){ switch(oper_type) { case 0x0001: case 0x0003: *precord_numb=3; pdescript[0].field_type='I'; pdescript[0].field_length=4; pdescript[0].field_descript=1; pdescript[1].field_type='C'; pdescript[1].field_length=20; pdescript[1].field_descript=2; pdescript[2].field_type='I'; pdescript[2].field_length=4; pdescript[2].field_descript=3; break; case 0x0002: case 0x0004: case 0x0006: *precord_numb=4; pdescript[0].field_type='I'; pdescript[0].field_length=4; pdescript[0].field_descript=1; pdescript[1].field_type='C'; pdescript[1].field_length=20; pdescript[1].field_descript=2; pdescript[2].field_type='I'; pdescript[2].field_length=4; pdescript[2].field_descript=4; pdescript[3].field_type='I'; pdescript[3].field_length=1; pdescript[3].field_descript=5; break; case 0x0005: *precord_numb=3; pdescript[0].field_type='I'; pdescript[0].field_length=4; pdescript[0].field_descript=1; pdescript[1].field_type='C'; pdescript[1].field_length=20; pdescript[1].field_descript=2; pdescript[2].field_type='I'; pdescript[2].field_length=4; pdescript[2].field_descript=4; break; default: printf("wrong oper_type"); }}void describe_tail(DESCRIPT * pdescript){ pdescript[0].field_type='I'; pdescript[0].field_length=4; pdescript[0].field_descript=7; pdescript[1].field_type='I'; pdescript[1].field_length=4; pdescript[1].field_descript=8; pdescript[2].field_type='I'; pdescript[2].field_length=4; pdescript[2].field_descript=9;}//Message_Head1 head_assemble1(short int oper_type)//{// Message_Head1 head;// head.oper_type=oper_type;// head.pack_length=0;// describe_body(oper_type,&(head.record_numb),head.descript);// head.pack_date=gettimestamp();// head.head_length=sizeof(head);// return head;//}Message_Head2 head_assemble2(short int oper_type){ Message_Head2 head; head.oper_type=oper_type; head.pack_length=0; describe_body(oper_type,&(head.record_numb),head.descript); head.pack_date=gettimestamp(); head.head_length=sizeof(head); return head;}Message_Tail tail_assemble(int sd,Message_Head2 * phead,Message_Body2 *pbody){ Message_Tail tail; tail.tail_length=sizeof(tail); tail.tail_field_numb=3; describe_tail(&tail.tail_field_descript); tail.tail_field_data.trader_id=my_addr(sd); tail.tail_field_data.check_code=(tail.tail_field_data.trader_id)^phead->pack_date; tail.tail_field_data.sum=pbody->account_balance; phead->pack_length=sizeof(*phead)+sizeof(*pbody)+sizeof(tail); return tail;}copy2buff(char * sendbuff,Message_Head2 * phead,Message_Body2 * pbody,Message_Tail * ptail){ int pos=0,i; memcpy(sendbuff+pos,&(phead->oper_type),sizeof(phead->oper_type)); pos+=sizeof(phead->oper_type); memcpy(sendbuff+pos,&(phead->pack_length),sizeof(phead->pack_length)); pos+=sizeof(phead->pack_length); memcpy(sendbuff+pos,&(phead->pack_date),sizeof(phead->pack_date)); pos+=sizeof(phead->pack_date); memcpy(sendbuff+pos,&(phead->head_length),sizeof(phead->head_length)); pos+=sizeof(phead->head_length); memcpy(sendbuff+pos,&(phead->record_numb),sizeof(phead->record_numb)); pos+=sizeof(phead->record_numb); for(i=0;i<4;i++) { memcpy(sendbuff+pos,&(phead->descript[i].field_type),sizeof(phead->descript[i].field_type)); pos+=sizeof(phead->descript[i].field_type); memcpy(sendbuff+pos,&(phead->descript[i].field_length),sizeof(phead->descript[i].field_length)); pos+=sizeof(phead->descript[i].field_length); memcpy(sendbuff+pos,&(phead->descript[i].field_descript),sizeof(phead->descript[i].field_descript)); pos+=sizeof(phead->descript[i].field_descript); } memcpy(sendbuff+pos,&(pbody->trader_serial),sizeof(pbody->trader_serial)); pos+=sizeof(pbody->trader_serial); memcpy(sendbuff+pos,&(pbody->contract_id),sizeof(pbody->contract_id)); pos+=sizeof(pbody->contract_id); memcpy(sendbuff+pos,&(pbody->account_balance),sizeof(pbody->account_balance)); pos+=sizeof(pbody->account_balance); memcpy(sendbuff+pos,&(pbody->resp_type),sizeof(pbody->resp_type)); pos+=sizeof(pbody->resp_type); memcpy(sendbuff+pos,&(ptail->tail_length),sizeof(ptail->tail_length)); pos+=sizeof(ptail->tail_length); memcpy(sendbuff+pos,&(ptail->tail_field_numb),sizeof(ptail->tail_field_numb)); pos+=sizeof(ptail->tail_field_numb); memcpy(sendbuff+pos,&(ptail->tail_field_descript.field_type),sizeof(ptail->tail_field_descript.field_type)); pos+=sizeof(ptail->tail_field_descript.field_type); memcpy(sendbuff+pos,&(ptail->tail_field_descript.field_length),sizeof(ptail->tail_field_descript.field_length)); pos+=sizeof(ptail->tail_field_descript.field_length); memcpy(sendbuff+pos,&(ptail->tail_field_descript.field_descript),sizeof(ptail->tail_field_descript.field_descript)); pos+=sizeof(ptail->tail_field_descript.field_descript); memcpy(sendbuff+pos,&(ptail->tail_field_data.trader_id),sizeof(ptail->tail_field_data.trader_id)); pos+=sizeof(ptail->tail_field_data.trader_id); memcpy(sendbuff+pos,&(ptail->tail_field_data.check_code),sizeof(ptail->tail_field_data.check_code)); pos+=sizeof(ptail->tail_field_data.check_code); memcpy(sendbuff+pos,&(ptail->tail_field_data.sum),sizeof(ptail->tail_field_data.sum)); pos+=sizeof(ptail->tail_field_data.sum);}format2net(Message_Head2 * phead,Message_Body2 * pbody,Message_Tail * ptail){ int i; phead->oper_type=htons(phead->oper_type); phead->pack_length=htonl(phead->pack_length); phead->pack_date=htonl(phead->pack_date); phead->head_length=htons(phead->head_length); phead->record_numb=htons(phead->record_numb); for(i=0;i<4;i++) { phead->descript[i].field_descript=htons(phead->descript[i].field_descript); } pbody->trader_serial=htonl(pbody->trader_serial); pbody->account_balance=htonl(pbody->account_balance); ptail->tail_length=htons(ptail->tail_length); ptail->tail_field_numb=htons(ptail->tail_field_numb); for(i=0;i<1;i++) { ptail->tail_field_descript.field_descript=htons(ptail->tail_field_descript.field_descript); } ptail->tail_field_data.check_code=htonl(ptail->tail_field_data.check_code); ptail->tail_field_data.sum=htonl(ptail->tail_field_data.sum);}format2local(Message_Head1 * phead,Message_Body1 * pbody,Message_Tail * ptail){ int i; phead->oper_type=ntohs(phead->oper_type); phead->pack_length=ntohl(phead->pack_length); phead->pack_date=ntohl(phead->pack_date); phead->head_length=ntohs(phead->head_length); phead->record_numb=ntohs(phead->record_numb); for(i=0;i<3;i++) { phead->descript[i].field_descript=ntohs(phead->descript[i].field_descript); } pbody->trader_serial=ntohl(pbody->trader_serial); pbody->currency=ntohl(pbody->currency); ptail->tail_length=ntohs(ptail->tail_length); ptail->tail_field_numb=ntohs(ptail->tail_field_numb); for(i=0;i<1;i++) { ptail->tail_field_descript.field_descript=ntohs(ptail->tail_field_descript.field_descript); } ptail->tail_field_data.check_code=ntohl(ptail->tail_field_data.check_code); ptail->tail_field_data.sum=ntohl(ptail->tail_field_data.sum);}int send_msg(short int oper_type,Message_Body2 body,int sd){ unsigned char buff[256]; Message_Head2 head; Message_Tail tail; memset(buff,0,sizeof(*buff)); head=head_assemble2(oper_type); tail=tail_assemble(sd,&head,&body); format2net(&head,&body,&tail); copy2buff(buff,&head,&body,&tail); //memcpy(buff,&head,sizeof(head)); //memcpy(buff+sizeof(head),&body,sizeof(body)); //memcpy(buff+sizeof(head)+sizeof(body),&tail,sizeof(tail)); if(send(sd,buff,sizeof(buff),0)<=0) { printf("ERROR in send message\n"); return(-1); } printf("Success in send message\n"); return(0);}int transaction(int sd,struct sockaddr_in * p_client_sa){ unsigned char buff[256]; int oper_type_back; Message_Head1 head; Message_Body1 body; Message_Tail tail; Message_Body2 body_back; memset(buff,0,sizeof(*buff)); if(recv(sd,buff,sizeof(buff),0)<0) { printf("socket read failed\n"); return(-1); } else { printf("Receive Request\n"); //copy2buff(buff,&head,&body,&tail); memcpy(&head,buff,sizeof(head)); memcpy(&body,(buff+sizeof(head)),sizeof(body)); memcpy(&tail,(buff+sizeof(head)+sizeof(body)),sizeof(tail)); if(checksum(&head,&tail,p_client_sa)<0) return(-1); format2local(&head,&body,&tail); head1_info_print(&head); body1_info_print(&body); tail_info_print(&tail); body_back.resp_type=accses(head.oper_type,body.trader_serial,body.contract_id,body.currency,&(body_back.account_balance)); body_back.trader_serial=body.trader_serial; strcpy(body_back.contract_id,body.contract_id); //writelog(RecordFile,&head,&body,&tail); //close(sd); switch(head.oper_type) { case 0x0001: printf("deposit money to account\n"); if(send_msg(0x0002,body_back,sd)<0) { if(affirm(1,body.trader_serial)) { printf("roll back fail\n"); writelog(RecordFile,&head,&body,&tail); } else { printf("roll back success\n"); } } else { if(affirm(0,body.trader_serial)==26) writelog(RecordFile,&head,&body,&tail); } break; case 0x0003: printf("get money from account\n"); if(send_msg(0x0004,body_back,sd)<0) { if(affirm(1,body.trader_serial)) { printf("roll back fail\n"); writelog(RecordFile,&head,&body,&tail); } else { printf("roll back success\n"); } } else { affirm(0,body.trader_serial); } break; case 0x0005: printf("query account\n"); send_msg(0x0006,body_back,sd); break; default: printf("wrong oper_type\n"); return(-1); } } return(0);}void clean(int sig){ pid_t pid; int stat_val; //(void)signal(SIGCHLD,SIG_DFL); pid=waitpid(-1,&stat_val,WNOHANG); if(WIFEXITED(stat_val)) printf("%d---Child exited with code %d\n",pid,WEXITSTATUS(stat_val)); else printf("Child terminated abnormally\n");}int main(int argc,char *argv[]){ int s; int sd; struct sockaddr_in sa; struct sockaddr_in client_sa; //signal(SIGCHLD,clean); signal(SIGCHLD,SIG_IGN); readlog(); sa=gethoststuff(); s=opensocket(); bindnlisten(s,sa); for(;;) { sd=acceptconn(s,&client_sa); if(sd!=-1) { if(fork()==0) { close(s); transaction(sd,&client_sa); printf("ended one trasaction\n"); close(sd); exit(1); } else close(sd); } } printf("press enter to quit\n"); getchar(); close(s);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -