Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define TIMEOUT 45
- #define BUFFSIZE 1000
- #define USERNAME 1
- #define PASSWORD 2
- int handle_robot(int klient) {
- request_login(klient); //poslali jsme zadost o login
- struct timeval timeout;
- timeout.tv_sec = TIMEOUT;
- timeout.tv_usec = 0;
- fd_set sockets;
- char buffer[BUFFSIZE];
- while(1) {
- int expecting = USERNAME;
- int passwordInteger = 0;
- string username;
- string password;
- unsigned char prevByte;
- unsigned char currentByte;
- FD_ZERO (&sockets);
- FD_SET (klient, &sockets);
- if (int r = select (klient + 1, &sockets, NULL, NULL, &timeout) < 0) {
- cout << "Could not receive data from client." << endl;
- close(klient);
- return -1;
- }
- if (!FD_ISSET(klient, &sockets)) {
- cout << "Connection timeout." << endl;
- close(klient);
- return 0;
- }
- int bytesCount = recv (klient, buffer, BUFFSIZE, 0);
- if ( bytesCount <= 0) {
- cout << "Could not read from sock." << endl;
- close(klient);
- return -1;
- }
- for (int i = 0; i < bytesCount; i++) {
- currentByte = buffer[i];
- switch (expecting) {
- case USERNAME:
- if (currentByte == '\n' && prevByte == '\r') {
- passwordInteger -= '\r';
- int userSize = username.length();
- username.resize(userSize - 1);
- request_password(klient);
- expecting = PASSWORD;
- prevByte = currentByte;
- break;
- } else {
- passwordInteger += currentByte;
- username.push_back(currentByte);
- prevByte = currentByte;
- break;
- }
- case PASSWORD:
- if (currentByte == '\n' && prevByte == '\r') {
- int passwordSize = password.length();
- password.resize (passwordSize - 1);
- if (username.substr(0,5) != "Robot" || atoi(password.c_str()) != passwordInteger) {
- login_failed(klient);
- close(klient);
- return -1;
- } else {
- login_ok(klient);
- }
- } else {
- password.push_back(currentByte);
- prevByte = currentByte;
- break;
- }
- //zde konci switch podle statusu
- }
- //zde konci for cyklus
- }
- //zde konci while cyklus
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement