📄 server.c
字号:
int sd;
unsigned int sasize;
int i;
sasize=sizeof(*p_client_sa);
if((sd=accept(s,(struct sockaddr *)p_client_sa,&sasize)),0)
{
perror("accept error");
return(-1);
}
for(i=0;i<ClientNUM;i++)
{
if(p_client_sa->sin_addr.s_addr==ClientIP[i].s_addr)
{
printf("Authenticated User From %s Log In\n",inet_ntoa(p_client_sa->sin_addr));
return sd;
}
}
close(sd);
printf("Not Authenticated User Log In\nRefuse to Offer Service\nConnection Closed\n");
return(-1);
}
unsigned int gettimestamp()
{
char tmpbuf[12];
unsigned int timestamp;
struct tm *today;
time_t ltime=time(NULL);
today = localtime( <ime );
strftime( tmpbuf,12,"%y%m%d", today );
//printf( tmpbuf );
//timestamp = *((unsigned int*)tmpbuf);
timestamp = atoi(tmpbuf);
//printf("******%d******\n",timestamp);
return timestamp;
}
unsigned int my_addr(int s)
{
struct sockaddr_in * psa;
socklen_t len=sizeof(struct sockaddr_in);
psa=(struct sockaddr_in *)malloc(sizeof(*psa));
if(getsockname(s,(struct sockaddr *)psa,&len)==0)
{
return psa->sin_addr.s_addr;
}
return 0;
}
void head1_info_print(Message_Head1 * phead)
{
printf("\noper_type is %d\n",phead->oper_type);
printf("packet_length is %d\n",phead->pack_length);
printf("packet_date is %d\n",phead->pack_date);
printf("head_length is %d\n",phead->head_length);
printf("record_numb is %d\n",phead->record_numb);
}
void body1_info_print(Message_Body1 * pbody)
{
printf("\ntrader_serial is %d\n",pbody->trader_serial);
printf("contract_id is %s\n",pbody->contract_id);
printf("currency is %d\n",pbody->currency);
}
void body2_info_print(Message_Body2 * pbody)
{
printf("\ntrader_serial is %d\n",pbody->trader_serial);
printf("contract_id is %s\n",pbody->contract_id);
printf("account_balance is %d\n",pbody->account_balance);
}
void tail_info_print(Message_Tail * ptail)
{
struct in_addr temp;
printf("\ntail_length is %d\n",ptail->tail_length);
printf("tail_field_numb is %d\n",ptail->tail_field_numb);
printf("tail_field_dat.traderid is %d\n",ptail->tail_field_data.trader_id);
temp.s_addr=ptail->tail_field_data.trader_id;
printf("traderid is %s\n",inet_ntoa(temp));
printf("check code is %d\n",ptail->tail_field_data.check_code);
}
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;
}
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 + -