Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- 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 >
- #define MAX_CLIENTS 5# define BUFLEN 256
- using namespace std;
- void error(const char * msg) {
- perror(msg);
- exit(1);
- }
- int main(int argc, char * argv[]) {
- char * name;
- char * passwd;
- int sockfd, newsockfd, portno;
- socklen_t clilen;
- char buffer[BUFLEN];
- struct sockaddr_in serv_addr, cli_addr;
- int n, i, j;
- string aux;
- fd_set read_fds; //multimea de citire folosita in select()
- fd_set tmp_fds; //multime folosita temporar
- int fdmax; //valoare maxima file descriptor din multimea read_fds
- if (argc < 2) {
- fprintf(stderr, "Usage : %s portn", argv[0]);
- exit(1);
- }
- //golim multimea de descriptori de citire (read_fds) si multimea tmp_fds
- FD_ZERO( & read_fds);
- FD_ZERO( & tmp_fds);
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (sockfd < 0)
- error("ERROR opening socket");
- portno = atoi(argv[1]);
- memset((char * ) & serv_addr, 0, sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_addr.s_addr = INADDR_ANY; // foloseste adresa IP a masinii
- serv_addr.sin_port = htons(portno);
- if (bind(sockfd, (struct sockaddr * ) & serv_addr, sizeof(struct sockaddr)) < 0)
- error("ERROR on binding");
- listen(sockfd, MAX_CLIENTS);
- //adaugam noul file descriptor (socketul pe care se asculta conexiuni) in multimea read_fds
- FD_SET(sockfd, & read_fds);
- fdmax = sockfd;
- //citim fisierul user_list
- std::map < string, string > logs;
- std::map < int, string > names;
- std::map < int, char * > user_names;
- std::map < int, int > brute_force;
- char concat[BUFLEN];
- i = 0;
- int cont = 0;
- string line, s;
- ifstream myfile(argv[2]);
- if (myfile.is_open()) {
- getline(myfile, line);
- while (getline(myfile, line)) {
- char * dup = strdup(line.c_str());
- dup = strtok(dup, " ");
- if (cont == 0) {
- strcpy(concat, dup);
- strcat(concat, " ");
- } else {
- strcat(concat, dup);
- strcat(concat, " ");
- }
- cont++;
- passwd = strtok(NULL, " ");
- logs[string(dup)] = string(passwd);
- if (cont == 9) {
- user_names[i] = concat;
- cont = 0;
- i++;
- }
- free(dup);
- }
- myfile.close();
- } else cout << "Unable to open file";
- if (cont != 0)
- user_names[i] = concat;
- printf(" %s ", user_names[0]);
- int max_users = i;
- int numb_to_send = htonl(i);
- ofstream myfile2("log");
- if (myfile2.is_open()) {
- }
- // main loop
- while (1) {
- tmp_fds = read_fds;
- if (select(fdmax + 1, & tmp_fds, NULL, NULL, NULL) == -1)
- error("ERROR in select");
- for (i = 0; i <= fdmax; i++) {
- if (FD_ISSET(i, & tmp_fds)) {
- //if (i == 0)
- {
- //memset(buffer, 0 , BUFLEN);
- //scanf("%s",buffer);
- //if(strncmp(buffer,"quit",4)==0)
- {
- // for(int s = 0 ; s < fdmax; ++s)
- // close(s);
- // return 0;
- }
- }
- if (i == sockfd) {
- // a venit ceva pe socketul inactiv(cel cu listen) = o noua conexiune
- // actiunea serverului: accept()
- clilen = sizeof(cli_addr);
- if ((newsockfd = accept(sockfd, (struct sockaddr * ) & cli_addr, & clilen)) == -1) {
- error("ERROR in accept");
- } else {
- //adaug noul socket intors de accept() la multimea descriptorilor de citire
- FD_SET(newsockfd, & read_fds);
- if (newsockfd > fdmax) {
- fdmax = newsockfd;
- }
- }
- printf("Noua conexiune de la %s, port %d, socket_client %d\n ", inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port), newsockfd);
- } else {
- // am primit date pe unul din socketii cu care vorbesc cu clientii
- //actiunea serverului: recv()
- memset(buffer, 0, BUFLEN);
- if ((n = recv(i, buffer, sizeof(buffer), 0)) <= 0) {
- if (n == 0) {
- //conexiunea s-a inchis
- printf("selectserver: socket %d hung up\n", i);
- } else {
- error("ERROR in recv");
- }
- close(i);
- FD_CLR(i, & read_fds); // scoatem din multimea de citire socketul pe care
- } else { //recv intoarce >0
- printf("Am primit de la clientul de pe socketul %d, mesajul: %s\n", i, buffer);
- if ((strncmp(buffer, "login ", 6) == 0)) {
- name = strtok(buffer, " ");
- name = strtok(NULL, " ");
- passwd = strtok(NULL, " \n");
- myfile2.close();
- if ((logs.count(name) == 0) || (strcmp(logs[name].c_str(), passwd))) {
- if (brute_force.count(i) == 0)
- brute_force[i] = 1;
- else if (brute_force[i] == 1)
- brute_force[i] = 2;
- else {
- strcpy(buffer, "-8 Brute force detectat");
- send(i, buffer, strlen(buffer), 0);
- close(i);
- FD_CLR(i, & read_fds);
- }
- strcpy(buffer, "-3 User/parola gresita");
- send(i, buffer, strlen(buffer), 0);
- aux = string(buffer);
- myfile2 << aux;
- myfile2.close();
- } else {
- //trimit numele utilizatorului si il adaug in lista celor ce pot face alte actiuni
- // la logout il scot
- strcat(name, ">");
- send(i, name, strlen(name), 0);
- names[i] = string(name);
- }
- }
- if (names.count(i) != 0) {
- if (strncmp(buffer, "quit", 4) == 0) {
- close(i);
- FD_CLR(i, & read_fds);
- } else if (strncmp(buffer, "logout", 6) == 0) {
- names.erase(i);
- } else if (strncmp(buffer, "getuserlist", 11) == 0) {
- //send(i,"!user!" , 6 , 0);
- write(i, & numb_to_send, sizeof(numb_to_send));
- for (int k = 0; k < max_users; k++)
- send(i, user_names[k], strlen(user_names[k]), 0);
- }
- }
- }
- }
- }
- }
- }
- close(sockfd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement