Guest User

Untitled

a guest
Nov 20th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. telnet 127.0.0.1 30000
  2.  
  3. #include "../includes/Main.h"
  4.  
  5. void handle_shutdown(int sig){
  6. if(sock)
  7. close(sock);
  8. fclose(lg);
  9. fclose(usr);
  10. printf("nInterrupt has been expected!n");
  11. exit(0);
  12. }
  13.  
  14. void login(int sock){
  15.  
  16. char buffer[20];
  17.  
  18. say(sock, "Enter your login: ");
  19. read_in(sock, buffer, sizeof(buffer));
  20.  
  21. char temp[20];
  22. int trash;
  23. while(fscanf(usr, "%s%i", temp, trash) != EOF){
  24. if(!strcmp(temp, buffer)){
  25. say(sock, "User with the same login doesn't exist!n");
  26. char result[100];
  27. sprintf(result, "Tried to connect with login %s.", buffer);
  28. log(result);
  29. return;
  30. }
  31. }
  32.  
  33. char result[50];
  34. sprintf(result, "./users/%s.txt", buffer);
  35. FILE *cUser = fopen(result, "r");
  36. char pass[20];
  37. fscanf(cUser, "%s", pass);
  38. say(sock, "Enter your password: ");
  39. char p[20];
  40. read_in(sock, p, sizeof(p));
  41. if(!strcmp(pass, p)){
  42. char result[100];
  43. sprintf(result, "User with login %s is now online.", buffer);
  44. log(result);
  45. say(sock, "Successful!");
  46. }else{
  47. char result[100];
  48. sprintf(result, "Tryed to login with invalide password as %s.", buffer);
  49. log(result);
  50. say(sock, "Invalide password!n");
  51. }
  52.  
  53. }
  54.  
  55. void reg(int sock){
  56. char buffer[20];
  57.  
  58. say(sock, "Enter your login: ");
  59. read_in(sock, buffer, sizeof(buffer));
  60. char temp[20];
  61. int trash;
  62. while(fscanf(usr, "%s%i", temp, trash) != EOF){
  63. say(sock, temp);
  64. if(!strstr(temp, buffer) || buffer[0] == ''){
  65. say(sock, "User with the same login has been already registered!n");
  66. return;
  67. }
  68. }
  69.  
  70. say(sock, "Enter your password: ");
  71. char pass[30];
  72. read_in(sock, pass, sizeof(pass));
  73.  
  74. fprintf(usr, "%s1", buffer);
  75.  
  76. char url[50];
  77. sprintf(url, "./users/%s.txt", buffer);
  78.  
  79. FILE *user = fopen(url, "w");
  80. fprintf(user, "%s", pass);
  81. fclose(user);
  82. char result[100];
  83. sprintf(result, "User with name %s has been registered.", buffer);
  84. log(result);
  85. }
  86.  
  87. int main(int argc, char *argv[]){
  88. catchSig(SIGINT, handle_shutdown);
  89.  
  90. lg = fopen("Log.txt", "w");
  91. usr = fopen("./users/Exists.txt", "a+");
  92.  
  93. sock = create_socket();
  94.  
  95. bind_to_port(sock, 30000);
  96.  
  97. listen(sock, 10);
  98.  
  99. puts("Waiting for connection.");
  100.  
  101. while(1){
  102. struct sockaddr_storage client_addr;
  103. unsigned int address_size = sizeof(client_addr);
  104. int connect_d = accept(sock, (struct sockaddr *) &client_addr, &address_size);
  105.  
  106. say(connect_d, "Choose operation: rn");
  107.  
  108. char buffer[255];
  109. int c = read_in(connect_d, buffer, sizeof(buffer));
  110. if(buffer[0] == ''){
  111. say(connect_d, "You sent nothing!");
  112. }else if(c == -1)
  113. error("Can't read data!");
  114. else{
  115. if(strstr(buffer, "login")){
  116. login(connect_d);
  117. }else if(strstr(buffer, "register")){
  118. reg(connect_d);
  119. }
  120. }
  121. close(connect_d);
  122. }
  123. fclose(lg);
  124. fclose(usr);
  125. return 0;
  126. }
Add Comment
Please, Sign In to add comment