📄 authen.c
字号:
#include <pthread.h>
#include "common.h"
pthread_mutex_t DatabaseMutex;
pthread_mutex_t KeyMutex;
R_RSA_PUBLIC_KEY ServerPublicKey;
R_RSA_PRIVATE_KEY ServerPrivateKey;
R_RANDOM_STRUCT randomStruct; /*线程内为只读*/
void *ComServer(void* ConnFdBuf);
void main(int argc, char* argv[])
{
char HomePath[100];
char Username[21]; char Password[21];
char *ptr;
R_RSA_PROTO_KEY protoKey;
ptr=getenv(HomePath); /*读取环境变量和配置文件*/
if (ptr!=NULL) strcpy(HomePath,ptr);
else strcpy(HomePath, "/usr/authen");
if (argc<2) {
printf("\nDatabase Username:");
scanf("%s", Username);
printf("Password for %s:", Username);
scanf("%s", Password);
}
else if (argc<3) {
if (strcmp(argv[1],"default")==0) {
strcpy(Username, "newgsm");
strcpy(Password, "pengxl");
}
else {
strcpy(Username, argv[1]);
printf("\nPassword for %s:", Username);
scanf("%s", Password);
}
}
else {
strcpy(Username, argv[1]);
strcpy(Password, argv[2]);
}
if (ConnectToDatabase(Username, Password) < 0) {
CommonShow("连接数据库失败...程序即将退出!");
pthread_exit(NULL);
}
pthread_mutex_init(&DatabaseMutex,NULL);
pthread_mutex_init(&KeyMutex,NULL);
R_RandomCreate(&randomStruct);
protoKey.bits=KEY_BITS;
protoKey.useFermat4 = 1;
if (R_GeneratePEMKeys(&ServerPublicKey, &ServerPrivateKey, &protoKey, &randomStruct)==IDOK) {
ListenServer(); /*侦听处理主函数*/
CommonShow("程序正常退出。。。");
}
else {
CommonShow("生成钥匙对出错,程序退出。。");
return;
}
}
/****************************************************************************/
int ListenServer()
{
struct sockaddr_in servaddr, Cliaddr;
size_t Clilen;
int ListenFd,ConnFd;
char ConnFdBuf[21];
int OptionValue;
fd_set SocketSet;
int nReady;
struct timeval tvTimeOut;
pthread_t tid;
if((ListenFd=socket(PF_INET,SOCK_STREAM,0))<0) {
CommonShow("socket创建出错!");
close(ListenFd);
return(-1);
}
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htons(INADDR_ANY);
servaddr.sin_port=htons(LISTEN_PORT);
OptionValue=1;
if (setsockopt(ListenFd,SOL_SOCKET,SO_REUSEADDR,&OptionValue,sizeof(OptionValue))<0) {
CommonShow("setsockopt出错!");
close(ListenFd);
return(-2);
}
if (bind(ListenFd, (struct sockaddr *)&servaddr, sizeof(struct sockaddr))<0) {
CommonShow("bind出错!");
close(ListenFd);
return(-3);
}
if (listen(ListenFd,MAX_REQUEST)<0) {
CommonShow("listen出错!");
close(ListenFd);
return(-4);
}
if (SetNonblock(ListenFd)<0) {
CommonShow("SetNonBlock出错!");
close(ListenFd);
return(-5);
}
tvTimeOut.tv_sec=TIME_OUT;
CommonShow("开始侦听......");
/*开始主循环*/
FD_ZERO(&SocketSet);
FD_SET(ListenFd, &SocketSet);
while(1) {
CommonShow("正在侦听......");
if ((nReady=select(1,&SocketSet,NULL,NULL,&tvTimeOut))<0) {
if (errno==EINTR) {
CommonShow("select程序侦听被中断!,ListenServer");
}
CommonShow("select侦听取状态出错!,ListenServer");
close(ListenFd);
return(-6);
}
/*nReady==0 Timeout
nReady>0 a new connect request */
if (nReady>0) {
Clilen=sizeof(Cliaddr);
if ((ConnFd=accept(ListenFd,(struct sockaddr *)&Cliaddr,&Clilen))<0) {
CommonShow("accept出错!");
close(ListenFd);
return(-7);
} /*end of accept*/
sprintf(ConnFdBuf, "%d", ConnFd);
if (pthread_create(&tid, NULL, ComServer, (void *)ConnFdBuf)!=0) {
CommonShow("新线程ComServer出错!");
close(ListenFd);
return(-8);
};
CommonShow("成功建立一个新线程!!");
}
/*处理控制信息*/
if (MessageServer()<0) {
break;
}
} /*while主循环结束 */
close(ListenFd);
return(0);
}
/***************************************************************************/
int MessageServer()
{
return(0);
}
/***************************************************************************/
void *ComServer(void* ConnFdBuf)
{
int iIndex;
int ConnFd;
pthread_t selfid;
char Buf[MAX_BUFFER_SIZE];
char BufBack[MAX_BUFFER_SIZE];
char MsgType[3];
R_RSA_PUBLIC_KEY ClientPublicKey;
int ClientPublicKeyReady=0;
int iLen=0;
int iMsgLen=0;
ConnFd=atoi((char *)ConnFdBuf);
selfid=pthread_self();
if (SetBlock(ConnFd)<0) {
CommonShow("SetBlock出错,ComServer");
close(ConnFd);
pthread_exit(NULL);
}
while(1) {
if ((iMsgLen=ReadRemotePakeage(ConnFd,Buf))<0) {
CommonShow("ReadRemotePakeage出错,ComServer");
close(ConnFd);
pthread_exit(NULL);
}
strncpy(MsgType,Buf+4,2);MsgType[2]=0;
if ((strcmp(MsgType,"RQ")==0)||(strcmp(MsgType,"EX")==0)) {
if ((iLen=ProcessPackage(Buf, BufBack, iMsgLen, &ClientPublicKey, &ClientPublicKeyReady))<=0) { /*调用小奔的后台处理函数,小奔向回写缓冲区Bufback写数据*/
CommonShow("处理数据包出错!");
close(ConnFd);
pthread_exit(NULL);
}
strncpy(MsgType,BufBack+4,2);MsgType[2]=0;
if (SendRemotePakeage(ConnFd, BufBack, iLen)<0) {
CommonShow("发送数据包出错!");
close(ConnFd);
pthread_exit(NULL);
}
if (strcmp(MsgType,"RP")==0) {
CommonShow("成功返回登录应答包!连接关闭");
close(ConnFd);
pthread_exit(NULL);
}
}
} /*end of while*/
}
/************************************************************
*ReturnValue: 数据包字节数 *
************************************************************/
int ReadRemotePakeage(int ConnFd, char *Buf)
{
int MsgLen;
int n;
fd_set ConnSet;
struct timeval tvConnTimeOut;
FD_ZERO(&ConnSet);
FD_SET(ConnFd, &ConnSet);
tvConnTimeOut.tv_sec=TIME_OUT;
if ((n=select(1,&ConnSet,NULL,NULL,&tvConnTimeOut))<=0) {
CommonShow("select出错!ReadRemotePakeage");
return(-1);
}
/*调用Readn函数从缓冲区中读入数据到buf*/
if ((n=Readn(ConnFd, Buf, MSGLEN_BYTES))<0) {
CommonShow("ReadRemotePackage error when read lenth");
return(-2);
}
else if(n==0) return(-3);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -