//========================================================================================== //接受处理数据包 int login_loop(pcap_t *pcap_handle) { struct pcap_pkthdr *pkt_header; const u_char *pkt_data;//存储数据包的内容 int pcap_rec; int time_cnt = 0; int time_max = 500;//设置超时时间 struct packet_eap *p = get_packet();//数据包 //start验证 pcap_sendpacket(pcap_handle,p->start,18 + p->start[17]); while ((pcap_rec = pcap_next_ex(pcap_handle,&pkt_header,&pkt_data)) != -1){ if((pcap_rec == 0) || (pkt_data[12]!=0x88) || (pkt_data[13]!=0x8e)){ if(time_cnt++ > time_max){//超时处理 printf("%s time out!!! need reconnect...\n", what_time()); time_cnt = 0; } continue; } switch(pkt_data[18]){ case 0x01://code:request if ( pkt_data[22]==0x01){ //Type: identity[rfc3748] (1) printf("%s response, identity\n", what_time()); p->id[19]=pkt_data[19]; pcap_sendpacket(pcap_handle,p->id,18 + p->id[17]); } if ( pkt_data[22]==0x04){ //Type: MD5-Challenge [RFC3748] (4) make_passwd_pkt(p->passwd, pkt_data); pcap_sendpacket(pcap_handle,p->passwd,18 + p->passwd[17]); printf("%s resopnse, MD5-challenge\n", what_time()); } break; case 0x03://code:sussess printf("%s %s login successfully\n", what_time(), username); printf("%s press ctrl+c to logout...\n", what_time()); break; case 0x04://code:failed printf("%s %s login failed. unknow error\n", what_time(), username); break; } } return -1; //========================================================================================== }
void whetstones(long xtra, long x100, int calibrate) { long n1,n2,n3,n4,n5,n6,n7,n8,i,ix,n1mult; SPDP x,y,z; long j,k,l; SPDP e1[4]; SPDP timea; SPDP timeb; SPDP t = 0.49999975; SPDP t0 = t; SPDP t1 = 0.50000025; SPDP t2 = 2.0; Check=0.0; n1 = 12*x100; n2 = 14*x100; n3 = 345*x100; n4 = 210*x100; n5 = 32*x100; n6 = 899*x100; n7 = 616*x100; n8 = 93*x100; n1mult = 10; /* Section 1, Array elements */ e1[0] = 1.0; e1[1] = -1.0; e1[2] = -1.0; e1[3] = -1.0; timea = what_time(); { for (ix=0; ix<xtra; ix++) { for(i=0; i<n1*n1mult; i++) { e1[0] = (e1[0] + e1[1] + e1[2] - e1[3]) * t; e1[1] = (e1[0] + e1[1] - e1[2] + e1[3]) * t; e1[2] = (e1[0] - e1[1] + e1[2] + e1[3]) * t; e1[3] = (-e1[0] + e1[1] + e1[2] + e1[3]) * t; } t = 1.0 - t; } t = t0; } timeb = (what_time()-timea)/(SPDP)(n1mult); pout("N1 floating point\0",(float)(n1*16)*(float)(xtra), 1,e1[3],timeb,calibrate,1); /* Section 2, Array as parameter */ timea = what_time(); { for (ix=0; ix<xtra; ix++) { for(i=0; i<n2; i++) { pa(e1,t,t2); } t = 1.0 - t; } t = t0; } timeb = what_time()-timea; pout("N2 floating point\0",(float)(n2*96)*(float)(xtra), 1,e1[3],timeb,calibrate,2); /* Section 3, Conditional jumps */ j = 1; timea = what_time(); { for (ix=0; ix<xtra; ix++) { for(i=0; i<n3; i++) { if(j==1) j = 2; else j = 3; if(j>2) j = 0; else j = 1; if(j<1) j = 1; else j = 0; } } } timeb = what_time()-timea; pout("N3 if then else \0",(float)(n3*3)*(float)(xtra), 2,(SPDP)(j),timeb,calibrate,3); /* Section 4, Integer arithmetic */ j = 1; k = 2; l = 3; timea = what_time(); { for (ix=0; ix<xtra; ix++) { for(i=0; i<n4; i++) { j = j *(k-j)*(l-k); k = l * k - (l-j) * k; l = (l-k) * (k+j); e1[l-2] = j + k + l; e1[k-2] = j * k * l; } } } timeb = what_time()-timea; x = e1[0]+e1[1]; pout("N4 fixed point \0",(float)(n4*15)*(float)(xtra), 2,x,timeb,calibrate,4); /* Section 5, Trig functions */ x = 0.5; y = 0.5; timea = what_time(); { for (ix=0; ix<xtra; ix++) { for(i=1; i<n5; i++) { x = t*atan(t2*sin(x)*cos(x)/(cos(x+y)+cos(x-y)-1.0)); y = t*atan(t2*sin(y)*cos(y)/(cos(x+y)+cos(x-y)-1.0)); } t = 1.0 - t; } t = t0; } timeb = what_time()-timea; pout("N5 sin,cos etc. \0",(float)(n5*26)*(float)(xtra), 2,y,timeb,calibrate,5); /* Section 6, Procedure calls */ x = 1.0; y = 1.0; z = 1.0; timea = what_time(); { for (ix=0; ix<xtra; ix++) { for(i=0; i<n6; i++) { p3(&x,&y,&z,t,t1,t2); } } } timeb = what_time()-timea; pout("N6 floating point\0",(float)(n6*6)*(float)(xtra), 1,z,timeb,calibrate,6); /* Section 7, Array refrences */ j = 0; k = 1; l = 2; e1[0] = 1.0; e1[1] = 2.0; e1[2] = 3.0; timea = what_time(); { for (ix=0; ix<xtra; ix++) { for(i=0;i<n7;i++) { po(e1,j,k,l); } } } timeb = what_time()-timea; pout("N7 assignments \0",(float)(n7*3)*(float)(xtra), 2,e1[2],timeb,calibrate,7); /* Section 8, Standard functions */ x = 0.75; timea = what_time(); { for (ix=0; ix<xtra; ix++) { for(i=0; i<n8; i++) { x = sqrt(exp(log(x)/t1)); } } } timeb = what_time()-timea; pout("N8 exp,sqrt etc. \0",(float)(n8*4)*(float)(xtra), 2,x,timeb,calibrate,8); return; }
int main(int argc, char **argv) { int maxi, maxfd, listenfd, connfd, sockfd; int nready; ssize_t i, n; fd_set rset, allset; char buf[MAXLINE], str[INET_ADDRSTRLEN]; char climsg[100], nread; socklen_t clilen; struct sockaddr_in cliaddr, servaddr; struct client_info client[FD_SETSIZ]; FILE *fp, *ffp = stdin; if ((fp=fopen("test//msg.txt","w+"))==NULL) printf("Error in opening %s\n","msg.txt"), exit(1); else printf("File opening succeed\n"); //fputs("shaf dsfdsaf dgdasg gdasg", fp); //fclose(fp); int is_command = 0; if (argc != 2) { printf("Usage: %s <port>\n", argv[0]); exit(1); } /*初始socket*/ listenfd = socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr= htonl(INADDR_ANY); servaddr.sin_port = htons(atoi(argv[1])); /*命名socket*/ bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); /*監聽client端連線*/ listen(listenfd, LISTENQ); /*初始一些資料*/ maxfd = listenfd; maxi = -1; for (i = 0; i< FD_SETSIZ; i++) client[i].clientfd = -1; /*初始select集合*/ FD_ZERO(&allset); FD_SET(listenfd, &allset); FD_SET(fileno(ffp), &allset);//將鍵盤輸入檔案子加到集合裡 /*用迴圈方式一直處理client端要求*/ while(1) { rset = allset; nready = select(maxfd + 1, &rset, NULL, NULL, NULL); /*儲存使用者聊天紀錄並且關掉server*/ if(FD_ISSET(fileno(ffp), &rset))//當鍵盤輸入時為真 { ioctl(0,FIONREAD,&read);//輸入ctrl+D時會進入下列條件式 if(nread == 0) { printf("server shutdown\n"); fclose(fp); exit(1); } } /*如果會進入此迴圈,表示是server端發出的要求,有client要連線*/ if (FD_ISSET(listenfd, &rset)) { /*接受client端連線*/ clilen = sizeof(cliaddr); connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen); /*儲存使用者資訊*/ for (i = 0; i < FD_SETSIZ; i++) if (client[i].clientfd < 0) { client[i].clientfd = connfd;//紀錄client端檔案子 memcpy(&client[i].cliaddr, &cliaddr, clilen); printf("client in: %s:%d [sock:%d]\n", inet_ntop(AF_INET, &client[i].cliaddr.sin_addr, str, sizeof(str)), ntohs(client[i].cliaddr.sin_port), client[i].clientfd); sprintf(climsg, "[client@%s:%d]===>\n", inet_ntop(AF_INET, &client[i].cliaddr.sin_addr, str, sizeof(str)), ntohs(client[i].cliaddr.sin_port)); broadcast(client, maxi, climsg, strlen(climsg));//通知所有上線人,該員登入 break;//當一個client[i]資訊儲存完,馬上離開 } #if 0 if (i == FD_SETSIZ) { printf("too many clients\n"); exit(1); } #endif if (i == FD_SETSIZ) { close(connfd); goto out_of_num; } FD_SET(connfd, &allset); if (connfd > maxfd) maxfd = connfd; if (i > maxi) maxi = i; out_of_num: if (--nready <= 0) continue; } /*反之,表示由client發的要求,傳送訊息或是離開聊天室*/ for (i = 0; i <= maxi; i++) { if ((sockfd = client[i].clientfd) < 0) continue; if (FD_ISSET(sockfd, &rset)) { /*client端離開聊天室*/ if ((n = read(sockfd, buf, MAXLINE)) == 0) { printf("client out: %s:%d [sock:%d]\n", inet_ntop(AF_INET, &client[i].cliaddr.sin_addr, str, sizeof(str)), ntohs(client[i].cliaddr.sin_port), client[i].clientfd); close(sockfd); FD_CLR(sockfd, &allset); client[i].clientfd = -1; //maxi--; sprintf(climsg, "[client@%s:%d]<===\n", inet_ntop(AF_INET, &client[i].cliaddr.sin_addr, str, sizeof(str)), ntohs(client[i].cliaddr.sin_port)); broadcast(client, maxi, climsg, strlen(climsg)); } else//client端傳送訊息 { #if 0 for (j = 0; j <= maxi; j++) { if (client[j].clientfd < 0) continue; write(client[j].clientfd, buf, n); } #endif sprintf(climsg, "[client@%s:%d]:", inet_ntop(AF_INET, &client[i].cliaddr.sin_addr, str, sizeof(str)), ntohs(client[i].cliaddr.sin_port)); buf[n-1] = '\0'; if(strcmp(buf,"//num") == 0)//顯示聊天室人數 { is_command = 1; test_broadcast(client, maxi, fp, i, is_command, buf);///TEST!!!! command(client, i, maxi, buf, n); } else if(strcmp(buf,"//exit") == 0)//離開聊天室 { printf("client out: %s:%d [sock:%d]\n", inet_ntop(AF_INET, &client[i].cliaddr.sin_addr, str, sizeof(str)), ntohs(client[i].cliaddr.sin_port), client[i].clientfd); close(sockfd); FD_CLR(sockfd, &allset); client[i].clientfd = -1; sprintf(climsg, "[client@%s:%d]<===\n", inet_ntop(AF_INET, &client[i].cliaddr.sin_addr, str, sizeof(str)), ntohs(client[i].cliaddr.sin_port)); broadcast(client, maxi, climsg, strlen(climsg)); is_command = 1; } else if(strcmp(buf,"//date") == 0)//顯示目前時間日期 { is_command = 1;//表示client端是傳送指令,將is_command設為1 test_broadcast(client, maxi, fp, i, is_command, buf);///TEST!!!! what_time(client, i, buf); } if(is_command==0)//如果is_command為0表示,client端址傳送聊天訊息 { is_command = 0;//清為0 test_broadcast(client, maxi, fp, i, is_command, buf);//TEST buf[n-1] = '\n'; broadcast(client, maxi, climsg, strlen(climsg)); broadcast(client, maxi, buf, n); } is_command = 0;//清為0 } if (--nready <= 0) break; } } } }