int main(int argc, char *argv[]) { FILE *inputFile; int count, i; int beginNode, endNode, start, end, costValue, maxVertex = 0; inputFile = fopen("map.txt", "r"); initialize(); fscanf(inputFile,"%d", &count); fflush(stdin); for (i = 0; i < count; i++){ fscanf(inputFile,"%d%d%d", &beginNode, &endNode, &costValue); if (maxVertex < beginNode) { maxVertex = beginNode; } cost[beginNode][endNode] = costValue; } fscanf(inputFile,"%d%d", &start, &end); dijkstra(start, end, maxVertex); traceRoute(start, end); return 0; }
int main(int argc , char **argv ) { /* 世界のマップ */ int map[MAPSIZ*MAPSIZ]={ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1, -1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1, -1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1, -1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1, -1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1, -1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1, -1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1, -1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1, -1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1, -1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1, -1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1, -1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1, -1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1, -1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 }; int retmap[MAPSIZ*MAPSIZ]; struct EE e; int i , j; bzero( &e , sizeof( e ) ); traceRoute( 8 , 9 , map , retmap , MAPSIZ , MAPSIZ ); for(i=0;i<MAPSIZ;i++){ for(j=0;j<MAPSIZ;j++){ fprintf( stderr , "%d " , retmap[j + i*MAPSIZ] ); } fprintf( stderr , "\n" ); } }
/* * Creates a new thread for each client and handles all client requests. */ void * handleRequest(void *arg) { params details = *(params*)arg; int connFD = details.connFD; int numberOfTraceroutes = 0; char *ipaddress =new char[15](); int isStrictOn = details.isStrictOn; ipaddress = inet_ntoa(details.clientAddress.sin_addr); //LOG clientConnectedLog(ipaddress,details.clientAddress.sin_port); time_t startTime = NULL; char mess[1024]; strcpy(mess,PROMPT); sendMessageToClient(mess, connFD); pthread_t countThread; countdownParams connDetails; strcpy(connDetails.ipAddress, ipaddress); connDetails.port_no = details.clientAddress.sin_port; connDetails.socketDescriptor = connFD; connDetails.parentThread = pthread_self(); connDetails.startTime = time(NULL); pthread_create(&countThread, NULL, countdown,(void*)&connDetails); //continuously run the server while (1) { char buff[1024]; size_t buffSize = (size_t)sizeof(buff); memset(&buff, 0,buffSize); recv(connFD, &buff, buffSize,0); cout<<"\n Client "<<connFD<<" : "<<buff; if(buff==NULL || strlen(buff)<=0){ continue;} Command *cmd = new Command(buff); //help command if(strcmp(cmd->command, "help")==0) { //kill previous timer if((pthread_cancel(countThread))!=0) cout<<"\n Cannot kill thread"; /* //Send help file FILE *fp = fopen(HELP_FILE, "r"); char line[1024];*/ ifstream infile; infile.open("help.txt"); string line, message_to_send; if (infile.is_open()) { while (!infile.eof()) { getline(infile, line); cout<<"line " <<line<<endl; message_to_send = message_to_send + line + "\n"; } } cout<<"message_to_send "<<message_to_send<<endl; strcpy(mess, message_to_send.c_str()); sendMessageToClient(mess, connFD); /*while (fgets(line, sizeof(line), fp)) { //cout<<line<<"\n"; strcpy(mess, line); strcat(mess, "\n"); sendMessageToClient(mess, connFD); }*/ //start new timer connDetails.startTime = time(NULL); pthread_create(&countThread, NULL, countdown, (void*)&connDetails); } else if(strcmp(cmd->command, "traceroute")==0 && cmd->total_args==1) { //kill previous timer if((pthread_cancel(countThread))!=0) cout<<"\n Cannot kill thread"; char *tracerouteCommands[100]; int totalTracerouteCommands=0; /*determine command type*/ if(strcmp(cmd->args[0], "me")==0) { //This is traceroute me command char *command = new char[1024](); strcat(command, cmd->command); strcat(command, " "); strcat(command, ipaddress); tracerouteCommands[totalTracerouteCommands++]=command; }else { //normal traceroute //check if argument is a file FILE *batchFile = fopen(cmd->args[0], "r"); //arg[0] contains the value of argument to traceroute. //read commands from file if(batchFile!=NULL) { char line[1024]; while (fgets(line, sizeof(line), batchFile)) { Command *c = new Command(line); char *command = new char[1024](); strcat(command, c->command); strcat(command, " "); strcat(command, c->args[0]); bool isArgumentValid = validateTarget(c->args[0]); if(isArgumentValid) tracerouteCommands[totalTracerouteCommands++]=command; else { strcpy(mess, "\nInvalid parameter to traceroute command."); sendMessageToClient(mess, connFD); } } fclose(batchFile); } else { //read command from user input char *command = new char[1024](); strcat(command, cmd->command); strcat(command, " "); bool isArgumentValid = validateTarget(cmd->args[0]); if(isArgumentValid==true) { cout<<"Valid IP"; strcat(command, cmd->args[0]); int res = strcmp(cmd->args[0],ipaddress); cout<<" "<<ipaddress<<" "<<cmd->args[0]; cout<<"IP ADDR COMP"<<res; if((isStrictOn &&(strcmp(cmd->args[0],ipaddress)!=0))) { //LOG strictviolatedLog(ipaddress, details.clientAddress.sin_port, command); char mess[1024]; strcpy(mess, "\nCannot traceroute to host other than yourself\n"); sendMessageToClient(mess, connFD); } else tracerouteCommands[totalTracerouteCommands++]=command; } else { cout<<"Invalid IP"; strcpy(mess, "Invalid parameter to traceroute command."); sendMessageToClient(mess, connFD); } } } int index=0; while(totalTracerouteCommands>0) { if(numberOfTraceroutes==0) { startTime = time(NULL); } double diffInSec = difftime(time(NULL), startTime); if((numberOfTraceroutes+1<=details.maxRate) && (diffInSec<=details.kTimeUnit)) { numberOfTraceroutes++; cout<<"\n Executing "<<numberOfTraceroutes<<" "<<diffInSec/details.kTimeUnit<<":"<<(int)diffInSec%details.kTimeUnit; //LOG simpleTrtLog(ipaddress,cmd->args[0],details.clientAddress.sin_port); traceRoute(tracerouteCommands[index++], connFD); } else if ((numberOfTraceroutes+1>details.maxRate) && (diffInSec<=details.kTimeUnit)) { totalTracerouteCommands=1; strcpy(mess, "Rate limit excedded"); sendMessageToClient(mess, connFD); rateLimitExceededLog(ipaddress, details.clientAddress.sin_port, tracerouteCommands[index]); } else if((numberOfTraceroutes+1>details.maxRate) && (diffInSec>details.kTimeUnit)) { numberOfTraceroutes=0; startTime=time(NULL); cout<<"\n Executing "<<numberOfTraceroutes<<" "<<diffInSec/details.kTimeUnit<<":"<<(int)diffInSec%details.kTimeUnit; //LOG simpleTrtLog(ipaddress,cmd->args[0],details.clientAddress.sin_port); traceRoute(tracerouteCommands[index++], connFD); } else if((numberOfTraceroutes+1<=details.maxRate) && (diffInSec>details.kTimeUnit)) { numberOfTraceroutes=0; startTime=time(NULL); cout<<"\n Executing "<<numberOfTraceroutes<<" "<<diffInSec/details.kTimeUnit<<":"<<(int)diffInSec%details.kTimeUnit; //LOG simpleTrtLog(ipaddress,cmd->args[0],details.clientAddress.sin_port); traceRoute(tracerouteCommands[index++], connFD); } else{ } totalTracerouteCommands--; } //start new timer connDetails.startTime = time(NULL); pthread_create(&countThread, NULL, countdown, (void*)&connDetails); } else if(strcmp(cmd->command, "quit")==0) { //kill previous timer if((pthread_cancel(countThread))!=0) cout<<"\n Cannot kill thread"; //close connection strcpy(mess, "terminate"); sendMessageToClient(mess, connFD); //LOG clientDisconnetedLog(ipaddress,details.clientAddress.sin_port); if(close(connFD)==0) cout<<"\nClient "<<connFD<<" closed"; else cout<<"\n Error closing client"<<connFD; //modify global remaining connections using semaphore sem_wait(&mutex); remCon = (remCon+1>=maxCon)?maxCon:++remCon; cout<<"\n REMAINING CONNECTIONS : "<<remCon; sem_post(&mutex); pthread_exit((void*)&connFD); } else { //kill previous timer if((pthread_cancel(countThread))!=0) cout<<"\n Cannot kill thread"; strcpy(mess, "\nInvalid Command\n"); sendMessageToClient(mess, connFD); //start new timer connDetails.startTime = time(NULL); pthread_create(&countThread, NULL, countdown, (void*)&connDetails); } strcpy(mess,PROMPT); sendMessageToClient(mess, connFD); cmd->~Command(); } return NULL; }