Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "name_server.h"
  3. #include "csapp.h"
  4.  
  5.  
  6. char listen_port[PORT_LEN];
  7. int listen_socket = -1; // listen socket. initialized to -1.
  8.  
  9. int num_active_clients = 0;
  10. client_t *clients[MAX_USERS]; // malloc(MAX_USERS * sizeof(client_t)); // array of pointers to structs representing active clients.
  11.  
  12.  
  13.  
  14. void client_session(int connfd){
  15.  
  16. rio_t rio;
  17. char listen_buf[MAXLINE];
  18. command_t command;
  19. args_t args;
  20.  
  21. char *username, *password; // these pointers will serve different
  22. char *ip, *port; // purposes based on the current command.
  23. char *message;
  24. int user = 0;
  25.  
  26. Rio_readinitb(&rio, connfd);
  27.  
  28.  
  29. while (Rio_readlineb(&rio, listen_buf, MAXLINE) != 0){
  30.  
  31. if(num_active_clients == 2){
  32. printf("clients1 %s \n", clients[0]->username);
  33. printf("clients2 %s \n", clients[1]->username);
  34. }
  35. command = parse_command(listen_buf, args);
  36.  
  37. username = args[0];
  38. ip = args[2];
  39. port = args[3];
  40.  
  41. if(num_active_clients == 2){
  42. printf("clients3 %s \n", clients[1]->username);
  43. }
  44.  
  45. client_t* client = malloc(sizeof(client_t));
  46. client->username = malloc(sizeof(char)*MAXLINE);
  47. client->ip = malloc(sizeof(char)*MAXLINE);
  48. client->port = malloc(sizeof(char)*MAXLINE);
  49. clients[num_active_clients] = client;
  50.  
  51.  
  52. switch (command)
  53. {
  54. case LOGIN:
  55. if(((strcmp("thomas", args[0]) == 0) && (strcmp("hansen", args[1]) == 0))
  56. || ((strcmp("william", args[0]) == 0) && (strcmp("pedersen", args[1]) == 0)) ){
  57.  
  58.  
  59. sprintf(clients[num_active_clients]->username, "%s", username);
  60. sprintf(clients[num_active_clients]->ip, "%s", ip);
  61. sprintf(clients[num_active_clients]->port, "%s", port);
  62.  
  63. clients[num_active_clients]->logged_in = 1;
  64. num_active_clients ++;
  65.  
  66. sprintf(listen_buf, "You are now logged in as %s \n", clients[num_active_clients -1]->username);
  67. Rio_writen(connfd, listen_buf, strlen(listen_buf));
  68. break;
  69. }
  70. else
  71. {
  72. sprintf(listen_buf, "Wrong Login \n");
  73. Rio_writen(connfd, listen_buf, strlen(listen_buf));;
  74. break;
  75. }
  76.  
  77. case LOOKUP:
  78. user = 0;
  79. if (username != NULL){
  80. for (int i = 0; i < num_active_clients; i++)
  81. {
  82. if (strcmp(clients[i]->username, username) == 0){
  83. sprintf(listen_buf, "%s " , clients[i]->username);
  84. strcat(listen_buf, clients[i]->ip);
  85. strcat(listen_buf, " ");
  86. strcat(listen_buf, clients[i]->port);
  87. user = 1;
  88. }
  89. }
  90. }
  91. else
  92. {
  93. sprintf(listen_buf, "");
  94. for (int i = 0; i < num_active_clients; i++)
  95. {
  96. strcat(listen_buf, clients[i]->username);
  97. strcat(listen_buf, " ");
  98. strcat(listen_buf, clients[i]->ip);
  99. strcat(listen_buf, " ");
  100. strcat(listen_buf, clients[i]->port);
  101. if((i + 1) < num_active_clients){
  102. strcat(listen_buf, " ");
  103. }
  104. }
  105. user = 1;
  106. }
  107. if (user == 0){
  108. sprintf(listen_buf, "%s", "User does not exist, or not logged in");
  109. }
  110.  
  111.  
  112. strcat(listen_buf, "\n");
  113. user = 0;
  114. Rio_writen(connfd, listen_buf, strlen(listen_buf));
  115. break;
  116.  
  117. case LOGOUT:
  118. sprintf(listen_buf, " >> Goodbye ... \n");
  119. clients[num_active_clients -1]->logged_in = 0;
  120. free(clients[num_active_clients -1]);
  121. num_active_clients --;
  122. Rio_writen(connfd, listen_buf, strlen(listen_buf));
  123. break;
  124. }
  125. }
  126. }
  127.  
  128. void *thread(void *vargp)
  129. {
  130. int connfd = *((int *)vargp);
  131. Pthread_detach(pthread_self()); //line:conc:echoservert:detach
  132. Free(vargp); //line:conc:echoservert:free
  133. client_session(connfd);
  134. Close(connfd);
  135. return NULL;
  136. }
  137.  
  138. int main(int argc, char **argv) {
  139.  
  140. if (argc != MAIN_ARGNUM + 1) {
  141. fprintf(stderr, "Usage: %s <listening port>\n", argv[0]);
  142. exit(EXIT_FAILURE);
  143. } else if (!is_valid_port(argv[1])) {
  144. fprintf(stderr, ">> Invalid port number: %s\n", argv[1]);
  145. exit(EXIT_FAILURE);
  146. }
  147.  
  148. snprintf(listen_port, PORT_LEN, "%s", argv[1]);
  149.  
  150. for (int i = 0; i < MAX_USERS; i++)
  151. clients[i] = NULL;
  152.  
  153. printf(">> Accepting peers on port %s ...\n", listen_port);
  154.  
  155. /*
  156. * TODO: SETUP LISTENING SOCKET HERE
  157. * HINT: remember that you are free to use everything from csapp.c ;) ;)
  158. */
  159. listen_socket = Open_listenfd(argv[1]);
  160.  
  161. socklen_t clientlen;
  162. int *connfdp;
  163. struct sockaddr_storage clientaddr;
  164. char client_hostname[MAXLINE], client_port[MAXLINE];
  165. pthread_t tid;
  166.  
  167. int running = 1;
  168.  
  169.  
  170. while (running) {
  171.  
  172. clientlen = sizeof(struct sockaddr_storage);
  173. connfdp = Malloc(sizeof(int));
  174. *connfdp = Accept(listen_socket, (SA *)&clientaddr, &clientlen);
  175. Pthread_create(&tid, NULL, thread, connfdp);
  176. Getnameinfo((SA *) &clientaddr, clientlen, client_hostname, MAXLINE,
  177. client_port, MAXLINE, 0);
  178. printf("Connected to (%s, %s) via thread %d\n",
  179. client_hostname, client_port, (int) tid);
  180.  
  181. }
  182. exit(EXIT_SUCCESS);
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement