#include #include #include #include #include #include #include #include #include #include #include "logAddServ.h" #include "parseInetAddr.h" #include "lineIOAddServ.h" #define SERVERPORT 3630 #define SERVERLOG "as.log" #define MAX_USER_NAME_SIZE (25 < MAX_MESSAGE_SIZE ? 25 : MAX_MESSAGE_SIZE) #define NAME_TIME_LIMIT 10 #define ANSW_TIME_LIMIT 1 struct pthreadArgs { FILE *logFile ; int connectSock ; struct sockaddr_in clientSockAddr ; } ; /* This is very picky */ int convertResponse(char *answerBuff, int answerLen) { int i, answer ; if ((answerLen != 8) || (answerBuff[6] != '\r') || (answerBuff[7] != '\n')) return -1 ; answer = 0 ; for (i=0; i<6; ++i) { if ((answerBuff[i] < '0') || (answerBuff[i] > '9')) return -1 ; answer = 10*answer + answerBuff[i] - (int)'0' ; } return answer ; } void *process_request(void *myArgs) { FILE *logFile; int sio; struct sockaddr_in clientAddr ; char clnt_name[MAX_CLIENT_NAME_SIZE+1] ; char user_name[MAX_USER_NAME_SIZE+2] ; int numRead ; struct timeval timeLimit ; /* Copy my arguments and free the memory */ logFile = ((struct pthreadArgs *)myArgs) -> logFile ; sio = ((struct pthreadArgs *)myArgs) -> connectSock ; (void) memcpy((void *)&clientAddr, (void *)&(((struct pthreadArgs *)myArgs) -> clientSockAddr), sizeof(struct sockaddr_in)) ; (void) free(myArgs) ; /* Back to the non-pthread version */ parse_inet_address(&clientAddr, clnt_name, MAX_CLIENT_NAME_SIZE+1) ; logServerAction(logFile, clnt_name, "connected") ; writeLine(sio, "Name?", 3) ; timeLimit.tv_sec = NAME_TIME_LIMIT ; timeLimit.tv_usec = 0 ; if ((numRead=readLine(sio, user_name, MAX_USER_NAME_SIZE, &timeLimit)) <= 0) logServerAction(logFile, clnt_name, "no name") ; else if ((numRead < 2) || (user_name[numRead-2] != '\r') || (user_name[numRead-1] != '\n')) { logUserResponse(logFile, clnt_name, user_name, numRead) ; logServerAction(logFile, clnt_name, "bad name") ; } else { int iRand, jRand, answr, respns ; char questBuff[20] ; char answrBuff[8] ; logUserResponse(logFile, clnt_name, user_name, numRead-2) ; iRand = rand() % 1000000 ; jRand = rand() % 1000000 ; answr = (iRand + jRand) % 1000000 ; sprintf(questBuff, "%06d+%06d=", iRand, jRand) ; writeLine(sio, questBuff, 10) ; timeLimit.tv_sec = ANSW_TIME_LIMIT ; timeLimit.tv_usec = 0 ; if ((numRead = readLine(sio, answrBuff, 8, &timeLimit)) <= 0) { if (numRead == -2) logServerAction(logFile, clnt_name, "late") ; else logServerAction(logFile, clnt_name, "no answer") ; } else { char *myResponse ; respns = convertResponse(answrBuff, numRead) ; if (respns == answr) myResponse = "good" ; else if (respns >= 0) myResponse = "bad" ; else { logUserResponse(logFile, clnt_name, answrBuff, numRead) ; myResponse = "ugly" ; } writeLine(sio, myResponse, 10) ; logServerAction(logFile, clnt_name, myResponse) ; } } close(sio) ; logServerAction(logFile, clnt_name, "finished") ; return ((void *)NULL) ; } void handlePipe(int i) { abort() ; return ; } main(int argc, char *argv[]) { int srendezvous, sio, clntsize ; struct sockaddr_in servsock, clntsock; char *logFileName ; FILE *logFile ; struct timeval ptime ; pthread_t newThread ; pthread_attr_t attribThread ; struct pthreadArgs *newThreadArgs ; struct sigaction pipeAction, oldAction ; (void) pthread_attr_init(&attribThread) ; (void) pthread_attr_setdetachstate(&attribThread, PTHREAD_CREATE_DETACHED) ; /* Initialize random number generate */ (void) gettimeofday(&ptime, (struct timezone *)NULL) ; srand(ptime.tv_usec) ; /* Open log file */ if (argc > 1) logFileName = argv[1] ; else logFileName = SERVERLOG ; logFile = fopen(logFileName, "a") ; if (logFile == (FILE *)NULL) { fprintf(stderr, "Unable to open log file: %s\n", logFileName) ; exit(1) ; } pipeAction.sa_handler = handlePipe ; pipeAction.sa_mask = 0 ; pipeAction.sa_flags = 0 ; (void) sigaction(SIGPIPE, &pipeAction, &oldAction) ; if ((srendezvous = socket(AF_INET, SOCK_STREAM, 0)) < 0) { fputs("Unable to create socket\n", stderr) ; exit(1) ; /* Now this is unlikely to happen */ } servsock.sin_family = AF_INET ; servsock.sin_port = htons(SERVERPORT) ; servsock.sin_addr.s_addr = INADDR_ANY ; if (bind(srendezvous, (struct sockaddr *) &servsock, sizeof(struct sockaddr_in))) { fprintf(stderr, "Unable to bind to port %d\n", SERVERPORT) ; if (errno == EADDRINUSE) fputs("Check if another server is still running\n\n" "If a server recently terminated, wait before restarting.\n", stderr) ; exit(1) ; } (void) listen(srendezvous, SOMAXCONN) ; /* No way to fail */ while (1) { clntsize = sizeof(struct sockaddr_in) ; sio = accept(srendezvous, (struct sockaddr *)&clntsock, &clntsize) ; if (sio<0) { write_log(logFile, "accept failure", strerror(errno)) ; exit(1) ; } else { void *newData ; if ((newData = malloc(sizeof(struct pthreadArgs))) == NULL) { write_log(logFile, "out of memory", "") ; exit(1) ; } else { newThreadArgs = (struct pthreadArgs *)newData ; newThreadArgs->logFile = logFile ; newThreadArgs->connectSock = sio ; memcpy((void *)&newThreadArgs->clientSockAddr, (void *)&clntsock, sizeof(struct sockaddr_in)) ; pthread_create(&newThread, &attribThread, process_request, (void *)newThreadArgs) ; } } } }