Advertisement
Guest User

Untitled

a guest
Nov 29th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. #define TIMEOUT 45
  2. #define BUFFSIZE 1000
  3. #define USERNAME 1
  4. #define PASSWORD 2
  5.  
  6. int handle_robot(int klient) {
  7. request_login(klient); //poslali jsme zadost o login
  8. struct timeval timeout;
  9. timeout.tv_sec = TIMEOUT;
  10. timeout.tv_usec = 0;
  11. fd_set sockets;
  12. char buffer[BUFFSIZE];
  13.  
  14. while(1) {
  15. int expecting = USERNAME;
  16. int passwordInteger = 0;
  17. string username;
  18. string password;
  19. unsigned char prevByte;
  20. unsigned char currentByte;
  21. FD_ZERO (&sockets);
  22. FD_SET (klient, &sockets);
  23.  
  24. if (int r = select (klient + 1, &sockets, NULL, NULL, &timeout) < 0) {
  25. cout << "Could not receive data from client." << endl;
  26. close(klient);
  27. return -1;
  28. }
  29.  
  30. if (!FD_ISSET(klient, &sockets)) {
  31. cout << "Connection timeout." << endl;
  32. close(klient);
  33. return 0;
  34. }
  35. int bytesCount = recv (klient, buffer, BUFFSIZE, 0);
  36.  
  37. if ( bytesCount <= 0) {
  38. cout << "Could not read from sock." << endl;
  39. close(klient);
  40. return -1;
  41. }
  42.  
  43. for (int i = 0; i < bytesCount; i++) {
  44. currentByte = buffer[i];
  45.  
  46. switch (expecting) {
  47. case USERNAME:
  48. if (currentByte == '\n' && prevByte == '\r') {
  49. passwordInteger -= '\r';
  50. int userSize = username.length();
  51. username.resize(userSize - 1);
  52.  
  53. request_password(klient);
  54. expecting = PASSWORD;
  55. prevByte = currentByte;
  56. break;
  57. } else {
  58. passwordInteger += currentByte;
  59. username.push_back(currentByte);
  60. prevByte = currentByte;
  61. break;
  62. }
  63. case PASSWORD:
  64. if (currentByte == '\n' && prevByte == '\r') {
  65. int passwordSize = password.length();
  66. password.resize (passwordSize - 1);
  67. if (username.substr(0,5) != "Robot" || atoi(password.c_str()) != passwordInteger) {
  68. login_failed(klient);
  69. close(klient);
  70. return -1;
  71. } else {
  72. login_ok(klient);
  73. }
  74. } else {
  75. password.push_back(currentByte);
  76. prevByte = currentByte;
  77. break;
  78. }
  79.  
  80.  
  81.  
  82. //zde konci switch podle statusu
  83. }
  84.  
  85. //zde konci for cyklus
  86. }
  87. //zde konci while cyklus
  88. }
  89.  
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement