Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <pthread.h>
  9.  
  10.  
  11. struct thread_reqs{
  12.    
  13.     FILE *fp;
  14.     char *message_buffer_compare;
  15.    
  16. };
  17.  
  18. void *parent_thread(void *thread){
  19.  
  20.         struct thread_reqs *thread_req = thread;
  21.  
  22.         int connection_status;
  23.         int size1, size2;
  24.         struct sockaddr_in server_address_thread_req0;
  25.  
  26.         // "credentials"
  27.         char username[] = "testuser";
  28.         char password[] = "userpass";
  29.  
  30.         // create socket fd
  31.         int sockfd_thread_req0 = socket(AF_INET, SOCK_STREAM, 0);
  32.         // specify address for socket
  33.         server_address_thread_req0.sin_family = AF_INET;
  34.         server_address_thread_req0.sin_port = htons(9002);
  35.         server_address_thread_req0.sin_addr.s_addr = INADDR_ANY; //later to change to whatever the server address is
  36.         // connect to the server socket
  37.         connection_status = connect(sockfd_thread_req0, (struct sockaddr *) &server_address_thread_req0, sizeof(server_address_thread_req0));
  38.         if(connection_status == -1){
  39.             printf("Error making connection to server, Thread 2\n");
  40.             perror("Connection error: parent");
  41.             return 0;
  42.         }
  43.        
  44.        
  45.        
  46.         // loop the parent process to send any changes to the 'clipboard' file to the server
  47.         while(1){
  48.  
  49.             char size_buf[20];
  50.             memset(size_buf, 0, sizeof(size_buf));
  51.  
  52.             // send credentials to server first
  53.             sprintf(size_buf, "%ld", strlen(username) + 1);
  54.             printf("We are sending username size: %s over the buffer\n", size_buf);
  55.             send(sockfd_thread_req0, size_buf, sizeof(char), 0);
  56.             memset(size_buf, 0, sizeof(size_buf));
  57.             send(sockfd_thread_req0, username, sizeof(char) * strlen(username) + 1, 0);
  58.             printf("Sent username: %s over the socket\n", username);
  59.  
  60.             sprintf(size_buf, "%ld", strlen(password) + 1);
  61.             printf("We are sending password size: %s over the buffer\n", size_buf);    
  62.             send(sockfd_thread_req0, size_buf, sizeof(char), 0);
  63.             memset(size_buf, 0, sizeof(size_buf));
  64.             send(sockfd_thread_req0, password, sizeof(char) * strlen(password) + 1, 0);
  65.             printf("Sent password: %s over the socket\n", password);
  66.  
  67.             printf("Username and password sent: parent process\n");
  68.  
  69.             // receive message from server confirming authentication
  70.             if(recv(sockfd_thread_req0, size_buf, sizeof(char), 0) == -1){
  71.                 perror("Auth message fail Parent");
  72.             } else {
  73.                 printf("Auth message received Parent: %s\n", size_buf);
  74.             }
  75.  
  76.  
  77.             if(strcmp("Y", size_buf) == 0){
  78.  
  79.  
  80.                 // Get the size of the current file so we can dynamically allocate
  81.                 // space and read the message into a buffer so it can be sent
  82.                 fseek(thread_req->fp, 0, SEEK_END);
  83.                 size2 = ftell(thread_req->fp);
  84.                 fseek(thread_req->fp, 0, SEEK_SET);
  85.  
  86.                 char *client_message_buffer = (char *)calloc(sizeof(char), size2 + 1);
  87.  
  88.                 fread(client_message_buffer, size2 + 1, 1, thread_req->fp);
  89.                
  90.                
  91.                 // compare the sizes of the buffers to check for changes
  92.                 // so that memory can be allocated if required
  93.  
  94.                 if(size2 > size1){
  95.  
  96.                     thread_req->message_buffer_compare = (char *)realloc(thread_req->message_buffer_compare, sizeof(char) * (size2 + 1));
  97.  
  98.                 }
  99.                
  100.                 sprintf(size_buf, "%d", size2);
  101.                 size2 = size1;
  102.  
  103.                 // if message changes at all, send it to the server
  104.                 if(strcmp(client_message_buffer, thread_req->message_buffer_compare) != 0){
  105.  
  106.                     // send the size of the message to the server          
  107.                     if(send(sockfd_thread_req0, size_buf, sizeof(size_buf), 0) == -1){
  108.                         perror("Error sending size of message: parent");
  109.                     }
  110.  
  111.                     // send the message to the server
  112.                     if(send(sockfd_thread_req0, client_message_buffer, size2 + 1, 0) == -1){
  113.                         perror("Error sending actual message: parent");
  114.                     }
  115.  
  116.                     // copy the message from the buffer to the comparison buffer
  117.                     strcpy(thread_req->message_buffer_compare, client_message_buffer);
  118.  
  119.                     // free the allocated buffer
  120.                     free(client_message_buffer);
  121.  
  122.                 } else {
  123.                    
  124.                     // reset the loop due to no change on the current clipboard
  125.                     close(sockfd_thread_req0);
  126.                     printf("Parent: Resetting loop due to no change on the clipboard");
  127.                     continue;  
  128.                 }
  129.  
  130.                 // reset the loop due to a succesful change on the clipboard
  131.                 close(sockfd_thread_req0);
  132.                 printf("Parent: Resetting loop due to successful change on the clipboard");
  133.                 continue;
  134.            
  135.             } else {
  136.                 printf("Parent: Authentication failed, username or password incorrect\nProgram Exit\n");
  137.                 close(sockfd_thread_req0);
  138.                 return 0;
  139.             }  
  140.         }
  141.    
  142.        
  143. }
  144.  
  145.  
  146.  
  147.  
  148. int main(int argc, char **argv){
  149.    
  150.  
  151.     int connection_status;
  152.     int size1, size2;
  153.     struct sockaddr_in server_address;
  154.     struct thread_reqs thread_req0;
  155.     pthread_t thread0;
  156.    
  157.  
  158.     // return a FD to the 'clipboard' file
  159.     // (in the future this will be the interface
  160.     //  to paste messages to, or maybe a GUI pointing
  161.     // to the same space in memory)
  162.     thread_req0.fp = fopen("clipboard", "w+");
  163.  
  164.     // create a pointer to a struct full of variables to use in other threads
  165.     struct thread_reqs *p_thread_req0 = &thread_req0;
  166.  
  167.     // "credentials"
  168.     char username[] = "testuser";
  169.     char password[] = "userpass";
  170.    
  171.    
  172.     // get the size so we can allocate memory
  173.     // for string to reallocate in a loop later
  174.     fseek(thread_req0.fp, 0, SEEK_END);
  175.     size1 = ftell(thread_req0.fp);
  176.     fseek(thread_req0.fp, 0, SEEK_SET);
  177.  
  178.     thread_req0.message_buffer_compare = (char *)calloc(sizeof(char), size1 + 1);
  179.    
  180.     // Create the thread for the other process,
  181.     // this will share the memory of the file for reading and writing
  182.  
  183.     int thread_req0_status = pthread_create(&thread0, NULL, parent_thread, p_thread_req0); 
  184.  
  185.     // code for child process
  186.  
  187.         // loop the child process to poll the server for any changes to apply to the 'clipboard' file
  188.         while(1){  
  189.        
  190.             int sockfd;
  191.  
  192.             // create socket fd
  193.             if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
  194.                 perror("Child");
  195.             }
  196.             // specify address for socket
  197.             server_address.sin_family = AF_INET;
  198.             server_address.sin_port = htons(9004);
  199.             server_address.sin_addr.s_addr = INADDR_ANY; // later to change to whatever the server address is
  200.            
  201.             // connect to the server socket
  202.             connection_status = connect(sockfd, (struct sockaddr *) &server_address, sizeof(server_address));
  203.             if(connection_status == -1){
  204.                 printf("Error making connection to server, child proc\n");
  205.                 perror("Connection error: child");
  206.                 return 0;
  207.             }  
  208.  
  209.             char changed_state[] = "unchanged";
  210.             char changed_compare[] = "changed";
  211.             char unchanged_reset[] = "unchanged";
  212.             char size_buf[20];
  213.            
  214.             memset(size_buf, 0, sizeof(size_buf));
  215.            
  216.             // send credentials to server first
  217.             sprintf(size_buf, "%ld", strlen(username) + 1);
  218.             //printf("We are sending username size: %s over the buffer\n", size_buf);
  219.             send(sockfd, size_buf, sizeof(char), 0);
  220.             memset(size_buf, 0, sizeof(size_buf));
  221.             send(sockfd, username, sizeof(char) * strlen(username) + 1, 0);
  222.             //printf("Sent username: %s over the socket\n", username);
  223.  
  224.             sprintf(size_buf, "%ld", strlen(password) + 1);
  225.             //printf("We are sending password size: %s over the buffer\n", size_buf);      
  226.             send(sockfd, size_buf, sizeof(char), 0);
  227.             memset(size_buf, 0, sizeof(size_buf));
  228.             send(sockfd, password, sizeof(char) * strlen(password) + 1, 0);
  229.             //printf("Sent password: %s over the socket\n", password);
  230.  
  231.             //printf("Username and password sent: child process\n");
  232.  
  233.             // receive message from server confirming authentication
  234.             if(recv(sockfd, size_buf, sizeof(char), 0) == -1){
  235.                 //perror("Auth message fail");
  236.             } else {
  237.                 //printf("Auth message received: %s\n", size_buf);
  238.             }
  239.            
  240.             if(strcmp("Y", size_buf) == 0){
  241.  
  242.                 memset(size_buf, 0, sizeof(size_buf));
  243.  
  244.                 // read response from server which returns a state that tells the client it's time to update the 'clipboard' file
  245.                 if(recv(sockfd, changed_state, sizeof(unchanged_reset) + 1, 0) == -1){
  246.                     perror("receive state response error");
  247.                 }
  248.                 printf("State response: %s received from server child process\n", changed_state);
  249.                
  250.                 // when the server comes back with a changed_state message we read
  251.                 if(strcmp(changed_state, changed_compare) != 0){
  252.                    
  253.                     printf("Child: Resetting loop due to unchanged message from server\n");
  254.  
  255.                     // wait before polling the server again
  256.                     fflush(stdout);
  257.                     close(sockfd);
  258.                     sleep(5);
  259.                     continue;
  260.                    
  261.                     } else {
  262.  
  263.                     if(recv(sockfd, size_buf, sizeof(size_buf), 0) == -1){
  264.                         perror("Receving size of message, child process");
  265.                     }
  266.                     size1 = atoi(size_buf);
  267.                    
  268.                     char *message_buffer = (char *)calloc(sizeof(char), size1 + 1);
  269.            
  270.                     if(recv(sockfd, message_buffer, size1, 0) == -1){
  271.                         perror("message received, child process");
  272.                     }
  273.                    
  274.                     fwrite(message_buffer, size1 + 1, 1, thread_req0.fp);
  275.                     strcpy(changed_state, unchanged_reset);
  276.                     free(message_buffer);
  277.                    
  278.                 }
  279.             } else {
  280.                 printf("Child Authentication failed, username or password incorrect\nProgram Exit\n");
  281.                 close(sockfd);
  282.                 return 0;
  283.             }
  284.  
  285.         // wait before polling the server again
  286.         fflush(stdout);
  287.         sleep(3);
  288.  
  289.         }        
  290.  
  291.  
  292.     fclose(thread_req0.fp);
  293.  
  294.     return 0;
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement