Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.89 KB | None | 0 0
  1. #
  2. include < stdio.h > #include < string.h > #include < stdlib.h > #include < unistd.h > #include < sys / types.h > #include < sys / socket.h > #include < netinet / in .h > #include < arpa / inet.h > #include < map > #include < fstream > #include < string > #include < iostream >
  3.  
  4. #define MAX_CLIENTS 5# define BUFLEN 256
  5.  
  6. using namespace std;
  7. void error(const char * msg) {
  8. perror(msg);
  9. exit(1);
  10. }
  11.  
  12. int main(int argc, char * argv[]) {
  13.  
  14. char * name;
  15. char * passwd;
  16. int sockfd, newsockfd, portno;
  17. socklen_t clilen;
  18. char buffer[BUFLEN];
  19. struct sockaddr_in serv_addr, cli_addr;
  20. int n, i, j;
  21. string aux;
  22.  
  23. fd_set read_fds; //multimea de citire folosita in select()
  24. fd_set tmp_fds; //multime folosita temporar
  25. int fdmax; //valoare maxima file descriptor din multimea read_fds
  26.  
  27. if (argc < 2) {
  28. fprintf(stderr, "Usage : %s portn", argv[0]);
  29. exit(1);
  30. }
  31.  
  32. //golim multimea de descriptori de citire (read_fds) si multimea tmp_fds
  33. FD_ZERO( & read_fds);
  34. FD_ZERO( & tmp_fds);
  35.  
  36. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  37. if (sockfd < 0)
  38. error("ERROR opening socket");
  39.  
  40. portno = atoi(argv[1]);
  41.  
  42. memset((char * ) & serv_addr, 0, sizeof(serv_addr));
  43. serv_addr.sin_family = AF_INET;
  44. serv_addr.sin_addr.s_addr = INADDR_ANY; // foloseste adresa IP a masinii
  45. serv_addr.sin_port = htons(portno);
  46.  
  47. if (bind(sockfd, (struct sockaddr * ) & serv_addr, sizeof(struct sockaddr)) < 0)
  48. error("ERROR on binding");
  49.  
  50. listen(sockfd, MAX_CLIENTS);
  51.  
  52. //adaugam noul file descriptor (socketul pe care se asculta conexiuni) in multimea read_fds
  53. FD_SET(sockfd, & read_fds);
  54. fdmax = sockfd;
  55.  
  56. //citim fisierul user_list
  57. std::map < string, string > logs;
  58. std::map < int, string > names;
  59. std::map < int, char * > user_names;
  60. std::map < int, int > brute_force;
  61. char concat[BUFLEN];
  62. i = 0;
  63. int cont = 0;
  64. string line, s;
  65. ifstream myfile(argv[2]);
  66. if (myfile.is_open()) {
  67. getline(myfile, line);
  68. while (getline(myfile, line)) {
  69. char * dup = strdup(line.c_str());
  70. dup = strtok(dup, " ");
  71. if (cont == 0) {
  72. strcpy(concat, dup);
  73. strcat(concat, " ");
  74. } else {
  75. strcat(concat, dup);
  76. strcat(concat, " ");
  77. }
  78. cont++;
  79.  
  80. passwd = strtok(NULL, " ");
  81. logs[string(dup)] = string(passwd);
  82.  
  83. if (cont == 9) {
  84. user_names[i] = concat;
  85. cont = 0;
  86. i++;
  87. }
  88. free(dup);
  89.  
  90. }
  91. myfile.close();
  92.  
  93. } else cout << "Unable to open file";
  94.  
  95. if (cont != 0)
  96. user_names[i] = concat;
  97.  
  98. printf(" %s ", user_names[0]);
  99. int max_users = i;
  100. int numb_to_send = htonl(i);
  101. ofstream myfile2("log");
  102. if (myfile2.is_open()) {
  103.  
  104. }
  105.  
  106. // main loop
  107. while (1) {
  108. tmp_fds = read_fds;
  109. if (select(fdmax + 1, & tmp_fds, NULL, NULL, NULL) == -1)
  110. error("ERROR in select");
  111.  
  112. for (i = 0; i <= fdmax; i++) {
  113.  
  114. if (FD_ISSET(i, & tmp_fds)) {
  115. //if (i == 0)
  116. {
  117. //memset(buffer, 0 , BUFLEN);
  118. //scanf("%s",buffer);
  119. //if(strncmp(buffer,"quit",4)==0)
  120. {
  121. // for(int s = 0 ; s < fdmax; ++s)
  122. // close(s);
  123. // return 0;
  124.  
  125. }
  126. }
  127.  
  128. if (i == sockfd) {
  129. // a venit ceva pe socketul inactiv(cel cu listen) = o noua conexiune
  130. // actiunea serverului: accept()
  131. clilen = sizeof(cli_addr);
  132. if ((newsockfd = accept(sockfd, (struct sockaddr * ) & cli_addr, & clilen)) == -1) {
  133. error("ERROR in accept");
  134. } else {
  135. //adaug noul socket intors de accept() la multimea descriptorilor de citire
  136. FD_SET(newsockfd, & read_fds);
  137. if (newsockfd > fdmax) {
  138. fdmax = newsockfd;
  139. }
  140. }
  141. printf("Noua conexiune de la %s, port %d, socket_client %d\n ", inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port), newsockfd);
  142. } else {
  143. // am primit date pe unul din socketii cu care vorbesc cu clientii
  144. //actiunea serverului: recv()
  145. memset(buffer, 0, BUFLEN);
  146. if ((n = recv(i, buffer, sizeof(buffer), 0)) <= 0) {
  147. if (n == 0) {
  148. //conexiunea s-a inchis
  149. printf("selectserver: socket %d hung up\n", i);
  150. } else {
  151. error("ERROR in recv");
  152. }
  153. close(i);
  154. FD_CLR(i, & read_fds); // scoatem din multimea de citire socketul pe care
  155. } else { //recv intoarce >0
  156.  
  157. printf("Am primit de la clientul de pe socketul %d, mesajul: %s\n", i, buffer);
  158.  
  159. if ((strncmp(buffer, "login ", 6) == 0)) {
  160.  
  161. name = strtok(buffer, " ");
  162. name = strtok(NULL, " ");
  163. passwd = strtok(NULL, " \n");
  164.  
  165. myfile2.close();
  166.  
  167. if ((logs.count(name) == 0) || (strcmp(logs[name].c_str(), passwd))) {
  168.  
  169. if (brute_force.count(i) == 0)
  170. brute_force[i] = 1;
  171. else if (brute_force[i] == 1)
  172. brute_force[i] = 2;
  173. else {
  174. strcpy(buffer, "-8 Brute force detectat");
  175. send(i, buffer, strlen(buffer), 0);
  176. close(i);
  177. FD_CLR(i, & read_fds);
  178. }
  179. strcpy(buffer, "-3 User/parola gresita");
  180. send(i, buffer, strlen(buffer), 0);
  181. aux = string(buffer);
  182. myfile2 << aux;
  183. myfile2.close();
  184. } else {
  185. //trimit numele utilizatorului si il adaug in lista celor ce pot face alte actiuni
  186. // la logout il scot
  187. strcat(name, ">");
  188. send(i, name, strlen(name), 0);
  189. names[i] = string(name);
  190. }
  191.  
  192. }
  193. if (names.count(i) != 0) {
  194.  
  195. if (strncmp(buffer, "quit", 4) == 0) {
  196.  
  197. close(i);
  198. FD_CLR(i, & read_fds);
  199. } else if (strncmp(buffer, "logout", 6) == 0) {
  200. names.erase(i);
  201. } else if (strncmp(buffer, "getuserlist", 11) == 0) {
  202. //send(i,"!user!" , 6 , 0);
  203. write(i, & numb_to_send, sizeof(numb_to_send));
  204. for (int k = 0; k < max_users; k++)
  205. send(i, user_names[k], strlen(user_names[k]), 0);
  206. }
  207.  
  208. }
  209.  
  210. }
  211. }
  212. }
  213. }
  214. }
  215.  
  216. close(sockfd);
  217.  
  218. return 0;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement