Advertisement
Guest User

SERVIDOR

a guest
Jun 18th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11.  
  12.  
  13.  
  14. #define MAXBUFFER 512
  15. #define PUERTO 4950
  16.  
  17. int main(int argc, char * argv[]){
  18.  
  19.  
  20.     int sd, n_sd;
  21.     struct sockaddr_in server,cliente;
  22.     socklen_t longCliente = sizeof(cliente);
  23.     char buffer[MAXBUFFER];
  24.  
  25.     if((sd=socket(PF_INET,SOCK_STREAM,0)) < 0){
  26.         perror("socket");
  27.         exit(1);
  28.     }
  29.  
  30.     memset(&server,0,sizeof(server));
  31.     server.sin_family = AF_INET;
  32.     server.sin_port = htons(PUERTO);
  33.     server.sin_addr.s_addr = INADDR_ANY;
  34.  
  35.     if(bind(sd,(struct sockaddr *)&server, sizeof(server)) < 0){
  36.         perror("bind");
  37.         exit(1);
  38.     }
  39.  
  40.     if(listen(sd,5) < 0){
  41.         perror("listen");
  42.         exit(1);
  43.     }
  44.  
  45.     while(1){
  46.  
  47.         printf("Esperando conexión...\n");
  48.  
  49.         n_sd = accept(sd,(struct sockaddr *)&cliente, &longCliente);
  50.         if(n_sd < 0){
  51.             perror("accept");
  52.             close(sd);
  53.             exit(1);
  54.         }
  55.  
  56.  
  57.             FILE * file = fopen("claves.txt","r");
  58.             if(file == NULL){
  59.                 perror("fopen");
  60.                 close(n_sd);
  61.                 close(sd);
  62.                 exit(1);
  63.             }
  64.  
  65.         uint32_t longBigEndian;
  66.  
  67.         if((read(n_sd,&longBigEndian,sizeof(uint32_t))) != sizeof(uint32_t)){
  68.             perror("longBigEndian");
  69.             fclose(file);
  70.             close(n_sd);
  71.             close(sd);
  72.             exit(1);
  73.         }
  74.  
  75.         int longitud = ntohl(longBigEndian);
  76.  
  77.         if((read(n_sd,buffer,longitud)) != longitud){
  78.             perror("buffer");
  79.             fclose(file);
  80.             close(n_sd);
  81.             close(sd);
  82.             exit(1);
  83.         }
  84.  
  85.         buffer[longitud] = '\0';
  86.  
  87.         char linea[MAXBUFFER];
  88.         int encontrado = 0;
  89.         char * user;
  90.         char * pass;
  91.         uint32_t cero = htonl(0);
  92.         uint32_t m_uno = htonl(-1);
  93.  
  94.         while(fgets(linea,MAXBUFFER-1,file) != NULL){
  95.             linea[strlen(linea)-1] = '\0';
  96.             char * arroba = strstr(linea, "@");
  97.             *arroba = '\0';
  98.             user = linea;
  99.             pass = arroba+1;
  100.            
  101.             if(strcmp(buffer,user) == 0){
  102.                 encontrado = 1;
  103.                 break;
  104.             }
  105.         }
  106.         printf("%s\n",user);
  107.         printf("%s\n",pass );
  108.         if(encontrado == 1){
  109.             int encontrado_pass = 0;
  110.             if(write(n_sd,&cero,sizeof(uint32_t)) != sizeof(uint32_t)){
  111.                 perror("write");
  112.                 fclose(file);
  113.                 close(n_sd);
  114.                 close(sd);
  115.                 exit(1);
  116.             }
  117.        
  118.             //Contraseña
  119.  
  120.             if((read(n_sd,&longBigEndian,sizeof(uint32_t))) != sizeof(uint32_t)){
  121.                 perror("longBigEndian");
  122.                 fclose(file);
  123.                 close(n_sd);
  124.                 close(sd);
  125.                 exit(1);
  126.             }
  127.  
  128.             int longitud = ntohl(longBigEndian);
  129.  
  130.             if((read(n_sd,buffer,longitud)) != longitud){
  131.                 perror("buffer");
  132.                 fclose(file);
  133.                 close(n_sd);
  134.                 close(sd);
  135.                 exit(1);
  136.             }
  137.  
  138.             if(strcmp(buffer,pass) == 0){
  139.                 encontrado_pass = 1;
  140.             }
  141.  
  142.             if(encontrado_pass == 1){
  143.                 if(write(n_sd,&cero,sizeof(uint32_t)) != sizeof(uint32_t)){
  144.                     perror("write");
  145.                     fclose(file);
  146.                     close(n_sd);
  147.                     close(sd);
  148.                     exit(1);
  149.                 }
  150.             }else{
  151.  
  152.                 if(write(n_sd,&m_uno,sizeof(uint32_t)) != sizeof(uint32_t)){
  153.                     perror("write");
  154.                     fclose(file);
  155.                     close(n_sd);
  156.                     close(sd);
  157.                     exit(1);
  158.                 }
  159.             }
  160.        
  161.         }else{
  162.  
  163.             if(write(n_sd,&m_uno,sizeof(uint32_t)) != sizeof(uint32_t)){
  164.                 perror("write");
  165.                 fclose(file);
  166.                 close(n_sd);
  167.                 close(sd);
  168.                 exit(1);
  169.             }
  170.         }
  171.         fclose(file);
  172.         close(n_sd);
  173.     }
  174.  
  175.     close(sd);
  176.  
  177.  
  178. return 0;
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement