int /*main( int argc, char **argv ) {*/ test_message(){ char *comeback, one[] = "the quick brown fox", two[]="jumps over the lazy dog"; pthread_t receive = pthread_self(); int size; /* Don't start if we can't get the message system working. */ if (messages_init() == MSG_OK) { /* Send and receive from destination process 1 (without threads running, we'll receive on thread 1 in torch's pthread implementation. */ /* Send a single message for starters. */ if (send_message_to_thread( receive, one, strlen(one)+1) != MSG_OK) { printf( "first failed\n" ); } if (receive_message( &receive, &comeback, &size) == MSG_OK) { printf ("received message 1--%s--size %d\n", comeback, size ); } else { printf ("first receive failed\n"); } /* Ensure that we can have some capacity in our message system. */ if (send_message_to_thread( receive, two, strlen(two)+1) != MSG_OK) { printf( "second 1 failed\n" ); } if (send_message_to_thread( receive, one, strlen(one)+1) != MSG_OK) { printf( "second 2 failed\n" ); } if (receive_message( &receive, &comeback, &size) == MSG_OK) { printf ("received message 2--%s--size %d\n", comeback, size ); } else { printf ("second 1 receive failed\n"); } if (receive_message( &receive, &comeback, &size) == MSG_OK) { printf ("received message 3--%s--size %d\n", comeback, size ); } else { printf ("second 2 receive failed\n"); } /* Clean up the message system. */ messages_end(); } return 1; }
void *server(void *t) { pthread_t server_thread_id; static char *processedMessage; static char *comeback; int size; while(1){ server_thread_id = pthread_self(); if (receive_message( &server_thread_id, &comeback, &size) == MSG_OK) { printf("Server thread received : %s\n", comeback); processedMessage = processServiceMessageReceived(comeback); free(comeback); printf("Server --> Client : %s\n", processedMessage ); fflush(stdout); if (send_message_to_thread( server_thread_id, processedMessage, strlen(processedMessage)+1) != MSG_OK) { printf( "sending message from client to auth thread failed\n" ); } } } pthread_exit(NULL); }
void *auth_thread() { pthread_t auth_thread_id; static char *processedMessage; static char *comeback; int size; while(1){ auth_thread_id = pthread_self(); if (receive_message( &auth_thread_id, &comeback, &size) == MSG_OK) { printf("Auth thread received : %s\n", comeback); processedMessage = checkIfUserPassExist(comeback); free(comeback); printf("Auth --> Client : %s\n", processedMessage ); fflush(stdout); if (send_message_to_thread( auth_thread_id, processedMessage, strlen(processedMessage)+1) != MSG_OK) { printf( "sending message from client to auth thread failed\n" ); } } } pthread_exit(NULL); }
/* The auth thread waits for a user name and pass to get sent to it from the client thread which it will then authenticate. It will return a valid authentication string or invalid if the user is authenticated or not. */ void *authThread(void *auth) { List_t *userPassList; userPassFile *lastNode = NULL; userPassFile *userPassNode = NULL; userPassList = readInUsersFromFile(((authStruct *)auth)->fileName); char messageToSend[CHAR_MAX]; char username[CHAR_MAX]; // Wait for username pass to authenticate from another thread pthread_t receive; char *messageReceived; int isAuthed; int size; isAuthed = 0; // Wait to receive a message if (receive_message( &receive, &messageReceived, &size) == MSG_OK) { printf ("Auth received message 1--%s--size %d\n", messageReceived, size ); // Check if matches list of username and passwords // If the head is not null if ((userPassList != NULL) && (userPassList->head != NULL)) { // Get the head node of the list List_head_info(userPassList, (void *)&userPassNode); // Loop through the list until null to check for authorized user:pass while (userPassNode != NULL) { if (strcmp(userPassNode->usernamePassword, messageReceived) == 0) { isAuthed = 1; } // Get next node List_next_node(userPassList, (void *)&lastNode, (void *)&userPassNode); } // Send message back to client thread that auth passed with authentication string if (isAuthed == 1) { // Create message to send back in the form 1:username:password:secretValueTicket strcpy (messageToSend, "1:"); strcat (messageToSend, messageReceived); strcat (messageToSend, ":"); strcat (messageToSend, ((authStruct *)auth)->secretValue); // Send message to auth thread if (send_message_to_thread( clientThreadRef, messageToSend, strlen(messageToSend) + 1) != MSG_OK) { printf( "Auth 1 first failed\n" ); } } // Send message back to client thread that it failed auth else { //Get username with regex sscanf( messageReceived, "%[^:]", username); //Create message to send on failure strcpy (messageToSend, "0:"); strcat (messageToSend, username); strcat (messageToSend, ":0"); // Send message to auth thread if (send_message_to_thread( clientThreadRef, messageToSend, strlen(messageToSend) + 1) != MSG_OK) { printf( "Auth 0 first failed\n" ); } } } } else { printf ("first receive failed\n"); } return NULL; }
/* The client thread reads in a username and password and then tries to authenticate with the authentication thread. If it receives a proper message back it should allow the user to run a service name with two arguments */ void *clientThread() { char inputbuffer[BUF_MAX]; char username[CHAR_MAX]; char password[CHAR_MAX]; char messageToSend[CHAR_MAX]; pthread_t receive; char *messageReceived; int size; //Read in user input printf("Enter a username: "******"%s", username) == 1) { } else { printf ("Incorrect number of parameters read.\n"); } } // Nothing to read in else { printf ("Nothing read in.\n"); } //Read in user input printf("Enter your password: "******"%s", password) == 1) { //Read in password } else { printf ("Incorrect number of parameters read.\n"); } } // Nothing to read in else { printf ("Nothing read in.\n"); } // Create message of user:pass to send to authentication thread strcpy (messageToSend, username); strcat (messageToSend, ":"); strcat (messageToSend, password); // Send message to auth thread if (send_message_to_thread( authThreadRef, messageToSend, strlen(messageToSend) + 1) != MSG_OK) { printf( "first failed\n" ); } // Wait for username and pass auth string // Receive response from authentication thread if (receive_message( &receive, &messageReceived, &size) == MSG_OK) { printf ("Client received message 1--%s--size %d\n", messageReceived, size ); } else { printf ("first receive failed\n"); } /* After this I would make logic for reading in a service name with two arguments, then pass those to the ticket and service threads, which would then get a response back */ return NULL; }
void *client(void *t) { char message[300]; pthread_t client_thread_id = pthread_self(); char flag[2]; char username[SIZE_USERNAME+1]; char authstring[200]; int param1, param2; char servicename[SIZE_SERVICENAME+1]; thread_ids_t *threadids = (thread_ids_t*)t; int size; for (;;) { char buf[1000]; char *comebackFromAuth, *comebackFromTicket, *comebackFromServer; comebackFromTicket = (char *)malloc(sizeof(char)*400); comebackFromServer = (char *)malloc(sizeof(char)*400); printf("Please enter username and password: e.g username:password \n"); fgets(buf, 1000, stdin); /* Send and receive from destination process 1 (without threads running, we'll receive on thread 1 in torch's pthread implementation. */ printf( "Client --> Auth : %s\n",buf ); if (send_message_to_thread( threadids->auth_thread_id, buf, strlen(buf)+1) != MSG_OK) { printf( "sending message from client to auth thread failed\n" ); } if (receive_message( &client_thread_id, &comebackFromAuth, &size) == MSG_OK) { pthread_mutex_lock(&print_lock); printf("Client thread received : %s\n", comebackFromAuth); fflush(stdout); pthread_mutex_unlock(&print_lock); sscanf (comebackFromAuth,"%[^:]:%[^:]:%s",flag,username,authstring); free(comebackFromAuth); /* printf("flag: %s username: %s authstring: %s \n", flag,username,authstring); */ if(!strcmp(flag,"0")==0) { /* Get input from user again : operation parameter1 parameter2*/ printf("Please enter the operation: e.g add 1 2 \n"); fgets(buf, 1000, stdin); sscanf (buf,"%s %d %d",servicename,¶m1,¶m2); /* printf("servicename: %s param1: %d param2: %d \n",servicename,param1,param2); */ } else{ /* if the flag is 0 then dont continue*/ continue; } sprintf(comebackFromTicket, "%s:%s:%s",username,servicename, AUTH_STRING ); printf( "Client --> Ticket : %s\n",comebackFromTicket ); if (send_message_to_thread( threadids->ticket_thread_id, comebackFromTicket, strlen(comebackFromTicket)+1) != MSG_OK) { printf( "sending message from client to ticket thread failed\n" ); } if (receive_message( &client_thread_id, &comebackFromTicket, &size) == MSG_OK) { pthread_mutex_lock(&print_lock); printf("Client thread received : %s\n", comebackFromTicket); fflush(stdout); pthread_mutex_unlock(&print_lock); strcpy(message, comebackFromTicket); free(comebackFromTicket); sprintf(comebackFromServer, "%s:%s:%d:%d:%s", username, servicename, param1, param2, message ); printf( "Client --> Server : %s\n",comebackFromServer ); if (send_message_to_thread( threadids->server_thread_id, comebackFromServer, strlen(comebackFromServer)+1) != MSG_OK) { printf( "sending message from client to server thread failed\n" ); } if (receive_message( &client_thread_id, &comebackFromServer, &size) == MSG_OK) { pthread_mutex_lock(&print_lock); printf("Client thread received : %s\n", comebackFromServer); fflush(stdout); pthread_mutex_unlock(&print_lock); free(comebackFromServer); } } } } /* end for */ pthread_exit(NULL); }