Advertisement
Guest User

servertryng

a guest
Jul 1st, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 21.11 KB | None | 0 0
  1.  
  2. /*
  3. *  C Implementation: nameServer
  4. *
  5. * Description:
  6. *
  7. *
  8. * Author: MCarmen de Toro <mc@mc>, (C) 2015
  9. *
  10. * Copyright: See COPYING file that comes with this distribution
  11. *
  12. */
  13.  
  14. #include "nameServer.h"
  15.  
  16.  
  17.  
  18. /* Reads a line ended with \n from the file pointer.  */
  19. /* Return: a line ended not with an EOL but with a 0 or NULL if the end of the
  20. file is reached */
  21. char *readLine(FILE *file, char *line, int sizeOfLine)
  22. {
  23.  
  24.   int line_length;
  25.  
  26.   if (fgets(line, sizeOfLine, file) != NULL)
  27.   {
  28.     line_length = strlen(line)-1;
  29.     line[line_length] = 0;    
  30.   }
  31.   else
  32.   {
  33.     line = NULL;
  34.   }
  35.  
  36.   return line;
  37. }
  38.  
  39.  
  40. /**
  41.  * Creates a DNSEntry variable from the content of a file line and links it
  42.  * to the DNSTable.
  43.  * @param line the line from the file to be parsed
  44.  * @param delim the character between tokens.
  45.  */
  46. struct _DNSEntry* buildADNSEntryFromALine(char *line, char *token_delim)
  47. {
  48.  
  49.   char *token;
  50.   struct _IP *ip_struct = malloc(sizeof(struct _IP));
  51.   struct _IP *last_ip_struct;
  52.   struct _DNSEntry* dnsEntry = malloc(sizeof(struct _DNSEntry));
  53.   int firstIP = 1;
  54.  
  55.  
  56.   //getting the domain name
  57.   token = strtok(line, token_delim);
  58.   strcpy(dnsEntry->domainName, token);
  59.   dnsEntry->numberOfIPs = 0;
  60.  
  61.   //getting the Ip's
  62.   while ((token = strtok(NULL, token_delim)) != NULL)
  63.   {
  64.     ip_struct = malloc(sizeof(struct _IP));
  65.     inet_aton((const char*)token, &(ip_struct->IP));
  66.     ip_struct->nextIP = NULL;
  67.     (dnsEntry->numberOfIPs)++;
  68.     if (firstIP == 1)
  69.     {
  70.       dnsEntry->first_ip = ip_struct;
  71.       last_ip_struct = ip_struct;
  72.       firstIP = 0;
  73.     }
  74.     else
  75.     {
  76.       last_ip_struct->nextIP = ip_struct;
  77.       last_ip_struct = ip_struct;
  78.     }
  79.   }  
  80.    
  81.     return dnsEntry;
  82. }
  83.  
  84. /* Reads a file with the dns information and loads into a _DNSTable structure.
  85. Each line of the file is a DNS entry.
  86. RETURNS: the DNS table */
  87. struct _DNSTable* loadDNSTableFromFile(char *fileName)
  88. {
  89.   FILE *file;
  90.   char line[1024];
  91.   struct _DNSEntry *dnsEntry;
  92.   struct _DNSEntry *lastDNSEntry;
  93.   struct _DNSTable *dnsTable = malloc(sizeof(struct _DNSTable));
  94.   int firstDNSEntry = 1;
  95.  
  96.   file = fopen(fileName, "r");
  97.   if (file==NULL)
  98.   {
  99.     perror("Problems opening the file");
  100.     printf("Errno: %d \n", errno);
  101.   }
  102.   else
  103.   {
  104.     //reading the following entries in the file
  105.     while(readLine(file, line, sizeof(line)) != NULL)
  106.     {
  107.       dnsEntry = buildADNSEntryFromALine(line, " ");
  108.       dnsEntry->nextDNSEntry = NULL;
  109.       if (firstDNSEntry == 1)
  110.       {
  111.         dnsTable->first_DNSentry = dnsEntry;
  112.         lastDNSEntry = dnsEntry;
  113.         firstDNSEntry = 0;
  114.       }
  115.       else
  116.       {
  117.         lastDNSEntry->nextDNSEntry = dnsEntry;
  118.         lastDNSEntry = dnsEntry;        
  119.       }  
  120.     }
  121.      
  122.    
  123.     fclose(file);
  124.   }
  125.  
  126.   return dnsTable;
  127. }
  128.  
  129.  
  130. /**
  131.  * Calculates the size of the message containing the DNS table. It does not
  132.  * include the message identifier.
  133.  * @param dnsTable a pointer to the DNSTable in memory.
  134.  */
  135. int getDNSTableMSGSize(struct _DNSTable* dnsTable)
  136. {
  137.   int table_size = 0;
  138.   int numberOfIPs_BYTES_SIZE = sizeof(short);
  139.  
  140.  
  141.   struct _DNSEntry *dnsEntry;
  142.  
  143.   dnsEntry = dnsTable->first_DNSentry;
  144.   if(dnsEntry != NULL)
  145.   {
  146.     do
  147.     {    
  148.       table_size +=  ( strlen(dnsEntry->domainName) + SPACE_BYTE_SIZE +
  149.         numberOfIPs_BYTES_SIZE + (dnsEntry->numberOfIPs * sizeof (long)) );
  150.     }while((dnsEntry=dnsEntry->nextDNSEntry) != NULL);
  151.   }
  152.  
  153.  
  154.   return table_size;
  155. }
  156.  
  157.  
  158.  
  159. /*Return a pointer to the last character copied in next_DNSEntry_ptr + 1 */
  160. /**
  161.  * Converts the DNSEntry passed as a parameter into a byte array pointed by
  162.  * next_DNSEntry_ptr. The representation will be
  163.  * domain_name\0number_of_ips[4byte_ip]*].
  164.  * @param dnsEntry the DNSEntry to be converted to a Byte Array.
  165.  * @param next_DNSEntry_ptr a pointer to Byte Array where to start copying
  166.  * the DNSEntry. The pointer moves to the end of the ByteArray representation.
  167.  */
  168. void dnsEntryToByteArray(struct _DNSEntry* dnsEntry, char **next_DNSEntry_ptr)
  169. {
  170.  
  171.   struct _IP* pIP;
  172.  
  173.   fflush(stdout);
  174.  
  175.   strcpy(*next_DNSEntry_ptr, dnsEntry->domainName);
  176.   //we leave one 0 between the name and the number of IP's of the domain
  177.   *next_DNSEntry_ptr += (strlen(dnsEntry->domainName) + 1);
  178.   stshort(dnsEntry->numberOfIPs, *next_DNSEntry_ptr);
  179.   *next_DNSEntry_ptr += sizeof(short);
  180.   if((pIP = dnsEntry->first_ip) != NULL)
  181.   {    
  182.     do    
  183.     {
  184.       stlong(pIP->IP.s_addr, *next_DNSEntry_ptr);      
  185.       *next_DNSEntry_ptr += sizeof(long);
  186.     }while((pIP = pIP->nextIP) != NULL);
  187.   }
  188.  
  189. }
  190.  
  191.  
  192. /*Dumps the dnstable into a byte array*/
  193. /*@Return a pointer to the byte array representing the DNS table */
  194. /*@param dnsTable the table to be serialized into an array of byes */
  195. /*@param _tableSize reference parameter that will be filled with the table size*/
  196. char *dnsTableToByteArray(struct _DNSTable* dnsTable, int *_tableSize)
  197. {
  198.   int tableSize = getDNSTableMSGSize(dnsTable);
  199.   *_tableSize = tableSize;
  200.  
  201.   char *dns_as_byteArray = malloc(tableSize);
  202.   char *next_dns_entry_in_the_dns_byteArray_ptr = dns_as_byteArray;
  203.   struct _DNSEntry *dnsEntry;
  204.  
  205.  
  206.   bzero(dns_as_byteArray, tableSize);
  207.  
  208.   dnsEntry = dnsTable->first_DNSentry;
  209.   do
  210.   {
  211.     dnsEntryToByteArray(dnsEntry, &next_dns_entry_in_the_dns_byteArray_ptr);
  212.   }while((dnsEntry=dnsEntry->nextDNSEntry) != NULL);
  213.  
  214.   return dns_as_byteArray;
  215.  
  216. }
  217.  
  218. /**
  219.  * Function that gets the dns_file name and port options from the program
  220.  * execution.
  221.  * @param argc the number of execution parameters
  222.  * @param argv the execution parameters
  223.  * @param reference parameter to set the dns_file name.
  224.  * @param reference parameter to set the port. If no port is specified
  225.  * the DEFAULT_PORT is returned.
  226.  */
  227. int getProgramOptions(int argc, char* argv[], char *dns_file, int *_port)
  228. {
  229.   int param;
  230.   int port = DEFAULT_PORT;
  231.  
  232.   // We process the application execution parameters.
  233.     while((param = getopt(argc, argv, "f:p:")) != -1){
  234.         switch((char) param){      
  235.             case 'f':
  236.                 strcpy(dns_file, optarg);              
  237.                 break;
  238.             case 'p':
  239.                 // Donat que hem inicialitzat amb valor DEFAULT_PORT (veure common.h)
  240.                 // la variable port, aquest codi nomes canvia el valor de port en cas
  241.                 // que haguem especificat un port diferent amb la opcio -p
  242.                 *_port = atoi(optarg);
  243.                 break;             
  244.             default:
  245.                 printf("Parametre %c desconegut\n\n", (char) param);
  246.                 return -1;
  247.         }
  248.     }
  249.    
  250.     return 0;
  251. }
  252.  
  253.  
  254. /**
  255.  * Function that generates the array of bytes with the dnsTable data and
  256.  * sends it.
  257.  * @param s the socket connected to the client.
  258.  * @param dnsTable the table with all the domains
  259.  */
  260. void process_LIST_RQ_msg(int sock, struct _DNSTable *dnsTable)
  261. {
  262.   char *dns_table_as_byteArray;
  263.   char *msg;
  264.   int dns_table_size;
  265.   int msg_size = 2;
  266.  
  267.  
  268.   dns_table_as_byteArray = dnsTableToByteArray(dnsTable, &dns_table_size);
  269.  
  270.   msg_size += dns_table_size;
  271.  
  272.   msg = malloc(dns_table_size + 2);
  273.   //TODO: set the operation code and the table data
  274.   stshort(4, msg); // Ponemos el codigo de operacion al mensaje a enviar
  275.   memcpy(msg+2, dns_table_as_byteArray, msg_size-2); // copiamos la tabla a la cadena de chars msg
  276.   //TODO: send the message
  277.   if(send(sock, msg, msg_size, 0)==-1){
  278.     printf("%s\n---------------------------------------------------\n[ERROR] SENDING \n---------------------------------------------------\n%s",KRED,KWHT);
  279.     }
  280.  
  281. }
  282. void process_HELLO_RQ_msg(int sock){
  283.     char buffer[MAX_BUFF_SIZE];
  284.     bzero(buffer,sizeof(buffer));
  285.     stshort(2,buffer);
  286.     strcpy(buffer+2,"HELLOWORLD"); 
  287.     if(send(sock,buffer,300,0)==-1){
  288.         printf("%s\n---------------------------------------------------\n[ERROR]  CANNOT SEND HELLOWORLD TO CLIENT  \n---------------------------------------------------\n%s",KRED,KWHT);
  289.                 }
  290.     else{        
  291.             printf("%s\n---------------------------------------------------\n[SUCCESFUL] SEND HELLOWORLD TO CLIENT  \n---------------------------------------------------\n%s",KCYN,KWHT);
  292.          }
  293. }
  294. void process_DOMAIN_RQ_msg(int sock, struct _DNSTable *dnsTable){
  295.       char buffer[MAX_BUFF_SIZE];
  296.       bzero(buffer,sizeof(buffer));
  297.      
  298.       char *sdominio=NULL;
  299.       char ipdominio[MAX_BUFF_SIZE];
  300.       int i=0;
  301.       int booleano=0;
  302.       int num=0;
  303.       int n_bytes;
  304.       int sizlng=0;
  305.       struct _DNSEntry *dnsEntry= dnsTable->first_DNSentry;
  306.       struct _IP *pIP;
  307.  
  308.  
  309.       n_bytes=recv(sock, buffer, sizeof(buffer), 0);
  310.       bzero(ipdominio,sizeof(buffer));
  311.       sdominio=strtok(buffer,"\0");
  312.      
  313.       /*memcpy(sdominio,buffer,n_bytes);
  314.        printf("hola\n");
  315.       strcpy(buffer,sdominio);*/
  316.       printf("El cliente pide la IP de:%s\n",sdominio);
  317.      
  318.       while(dnsEntry->nextDNSEntry+1 != NULL  && booleano!=1){
  319.  
  320.              if(strcmp(dnsEntry->domainName,sdominio)==0){
  321.                         printf("%s\n---------------------------------------------------\n[SUCCESFUL] DOMINIO ENCONTRADO EN DNS.TXT \n---------------------------------------------------\n%s",KMAG,KWHT);
  322.                         num=(dnsEntry->numberOfIPs);
  323.                         printf("%d \n",num);
  324.                  bzero(ipdominio,sizeof(buffer));
  325.                         stshort(6,ipdominio);
  326.                         pIP=dnsEntry->first_ip;
  327.                             for( i=0; i<num;i++){
  328.                          
  329.                                     printf("%s\n",inet_ntoa(pIP->IP));
  330.                                   stlong(pIP->IP.s_addr,ipdominio+2+sizlng);
  331.                                   sizlng+=sizeof(long);
  332.                                         //strcat(concatenated,inet_ntoa(dnsTable->first_DNSentry->first_ip->IP));
  333.                                         //strcat(concatenated,"\n");                                
  334.                                         pIP=pIP->nextIP;
  335.                                        
  336.                             } // end for*/
  337.                                   booleano=1;
  338.                         if(send(sock,ipdominio,sizlng+2,0)==-1){
  339.                             printf("%s\n---------------------------------------------------\n[ERROR]  CANNOT SEND IP TO CLIENT  \n---------------------------------------------------\n%s",KRED,KWHT);
  340.                                     }//end if (send
  341.                         else{        
  342.                                 printf("%s\n---------------------------------------------------\n[SUCCESFUL] SEND IPS FROM DOMAIN TO SERVER  \n---------------------------------------------------\n%s",KYEL,KWHT);
  343.                              }
  344.                         }//cierra el if del strcmp
  345.                 else{  
  346.              
  347.               dnsEntry=dnsEntry->nextDNSEntry;
  348.                            
  349.                     }  
  350.             }//cierra el while
  351.              
  352.               if(booleano==0){
  353.  
  354.                 char *msg;
  355.                 stshort(12,msg);
  356.                 if(send(sock,msg,sizeof(buffer),0)==-1){
  357.                           printf("%s\n---------------------------------------------------\n[ERROR]  CANNOT SEND IP TO CLIENT  \n---------------------------------------------------\n%s",KRED,KWHT);
  358.                               }//end if (send
  359.                         else{    
  360.                             printf("%s\n---------------------------------------------------\n[SUCCESFUL] SEND IPS FROM DOMAIN TO SERVER  \n---------------------------------------------------\n%s",KYEL,KWHT);
  361.                              }
  362.                             }
  363. }
  364. void process_CHANGE_RQ_msg(int sock, struct _DNSTable *dnsTable){
  365.       char buffer[MAX_BUFF_SIZE];
  366.       bzero(buffer,sizeof(buffer));    
  367.       char *sdominio;
  368.       char *ipdominio;
  369.       char domain[MAX_BUFF_SIZE];
  370.       int i=0;
  371.       int booleano=0;
  372.       int num=0;
  373.       int n_bytes;
  374.       int sizlng=0;
  375.         char IP[MAX_BUFF_SIZE];
  376.       char *token;
  377.       const char delim[2] = "·";
  378.         const char delim2[2] = "/";
  379.       struct _DNSEntry *dnsEntry= dnsTable->first_DNSentry;
  380.       struct in_addr *pIP;
  381.       n_bytes=recv(sock, buffer, sizeof(buffer), 0);
  382.         memcpy(sdominio,buffer,n_bytes);
  383.       strcpy(buffer,sdominio);
  384.       printf("Recidido %s \n",sdominio);
  385.       token = strtok(sdominio,delim);
  386.         memcpy(domain, token, strlen(token)+1);
  387.       printf("El dominio es = %s\n", domain);
  388.       token = strtok(NULL, delim);
  389.         memcpy(IP, token, strlen(token)+1);  
  390.         token = strtok(IP, delim2);
  391.         printf("%stoken2\n",token);
  392.         token = strtok(NULL, delim2);
  393.         printf ("La IP es: %s\n", token);
  394.     //--RECORRER LOS DOMINIOS----------------------------
  395.       inet_aton(IP, &pIP);
  396.     while(dnsEntry->nextDNSEntry != NULL  && booleano!=1){
  397.         if(strcmp(dnsEntry->domainName,domain)==0){
  398.  
  399.                         printf("%s\n---------------------------------------------------\n[SUCCESFUL] DOMINIO ENCONTRADO EN DNS.TXT \n---------------------------------------------------\n%s",KMAG,KWHT);
  400.                         num=(dnsEntry->numberOfIPs);
  401.                         printf("%d \n",num);
  402.                           bzero(ipdominio,sizeof(buffer));
  403.                   stshort(11,ipdominio);
  404.                           //dnsEntry->first_ip->IP.s_addr=pIP;
  405.                   printf("holaIpdone\n");
  406.                   booleano=1;
  407.                     if(send(sock,ipdominio,sizeof(buffer),0)==-1){
  408.                             printf("%s\n---------------------------------------------------\n[ERROR]  CANNOT SEND IP TO CLIENT  \n---------------------------------------------------\n%s",KRED,KWHT);
  409.                                     }//end if (send
  410.                     else{        
  411.                                 printf("%s\n---------------------------------------------------\n[SUCCESFUL] CHANGED IP FROM DOMAIN \n---------------------------------------------------\n%s",KYEL,KWHT);
  412.                              }
  413.                         }//cierra el if del strcmp
  414.                 else{   // no encontramos, probamos la siguiente
  415.              
  416.               dnsEntry=dnsEntry->nextDNSEntry;
  417.                            
  418.                     }  
  419.             }//cierra el while
  420.              
  421.               if(booleano==0){ // ENVIAR ERROR
  422.  
  423.                 char *msg;
  424.                 stshort(12,msg);
  425.                 if(send(sock,msg,sizeof(buffer),0)==-1){
  426.                           printf("%s\n---------------------------------------------------\n[ERROR]  CANNOT SEND IP TO CLIENT  \n---------------------------------------------------\n%s",KRED,KWHT);
  427.                               }//end if (send
  428.                         else{    
  429.                             printf("%s\n---------------------------------------------------\n[SUCCESFUL] SEND IPS FROM DOMAIN TO SERVER  \n---------------------------------------------------\n%s",KYEL,KWHT);
  430.                              }
  431.                             }
  432.  
  433. }
  434. void process_ADD_DOMAIN(int sock, char *buffer, struct _DNSTable *dnsTable, int bytes)
  435. {
  436.   printf("\n------------------AÑADIR IP A UN DOMINIO ---------------------------\n");
  437.     char nom_host[NAME_LENGTH];//on rebrem el nom del domini
  438.     char ip_b[20];//on guardem la ip a inserir
  439.   struct _DNSEntry *entry = dnsTable->first_DNSentry;
  440.     struct _DNSEntry *newentry = malloc(sizeof(struct _DNSEntry));
  441.     struct _DNSEntry *preventry;
  442.   struct _IP *ip = malloc(sizeof(struct _IP));
  443.     struct _IP *last_ip;
  444.     int done = 1;
  445.   int desp = 2;
  446.     int firstIP = 1;
  447.   memset(nom_host,'\0', sizeof(nom_host));
  448.   memcpy(nom_host, buffer+desp, sizeof(nom_host));
  449.     desp += strlen(nom_host)+1;
  450.   while((entry != NULL) && (done != 0)){
  451.                 done = strcmp(entry->domainName,nom_host);
  452.                 if(done != 0){
  453.                               preventry = entry;
  454.                         entry = entry->nextDNSEntry;
  455.                        
  456.                 }
  457.         }
  458.     if(entry == NULL){ //si es un nuevo dominio
  459.  
  460.         strcpy(newentry->domainName, nom_host);
  461.        
  462.     do{
  463.             ip = malloc(sizeof(struct _IP));
  464.             memcpy(ip_b, buffer+desp, sizeof(unsigned long));
  465.             ip->IP.s_addr = ldlong(ip_b);
  466.             ip->nextIP = NULL;
  467.             (newentry->numberOfIPs)++;
  468.             if(firstIP == 1){
  469.                 newentry->first_ip = ip;
  470.                 last_ip = ip;
  471.                 firstIP = 0;
  472.             }
  473.             else{
  474.                 last_ip->nextIP = ip;
  475.                 last_ip = ip;
  476.             }
  477.             desp += sizeof(unsigned long);
  478.         }while(desp < bytes);
  479.  
  480.    
  481.         newentry->nextDNSEntry = NULL;
  482.         preventry->nextDNSEntry = newentry;
  483.     } // FIN DE AÑADIR A UN DOMINIO QUE NO EXISTE
  484.     else{ //si el dominio ya existe
  485.     printf ("Adding Ip for :%s\n",entry->domainName);
  486.         last_ip = entry->first_ip;
  487.         while(last_ip->nextIP != NULL){
  488.             last_ip = last_ip->nextIP;
  489.         }
  490.         do{
  491.       printf("holahellegado al do\n");
  492.             ip = malloc(sizeof(struct _IP));
  493.             memcpy(ip_b, buffer+desp, sizeof(unsigned long));
  494.             ip->IP.s_addr = ldlong(ip_b);
  495.             ip->nextIP = NULL;
  496.             (entry->numberOfIPs)++;
  497.             last_ip->nextIP = ip;
  498.             last_ip = ip;
  499.             desp += sizeof(unsigned long);
  500.  
  501.         }while(desp < bytes);
  502.     printf("holafindeldo\n");
  503.     }//FIN ELSE
  504.  
  505.     send_OPOK(sock);
  506.     printf("\n-------------------FI DEL PROCES AFEGIR IP-------------------------\n");
  507.   return(0);
  508. }
  509.  
  510. void send_OPOK(int s)
  511. {
  512.     char msg[2];
  513.  
  514.     stshort(MSG_OP_OK, msg);
  515.     send(s, msg, sizeof(msg), 0);
  516. }
  517.  
  518.  
  519.  
  520. /**
  521.  * Receives and process the request from a client.
  522.  * @param s the socket connected to the client.
  523.  * @param dnsTable the table with all the domains
  524.  * @return 1 if the user has exit the client application therefore the
  525.  * connection whith the client has to be closed. 0 if the user is still
  526.  * interacting with the client application.
  527.  */
  528. int process_msg(int sock, struct _DNSTable *dnsTable)
  529. {
  530.   unsigned short op_code=0;
  531.   char buffer[MAX_BUFF_SIZE];
  532.  
  533.   bzero(buffer,sizeof(buffer));
  534.   int n_bytes;
  535.  
  536.  
  537.   int done = 0;
  538.   printf("%s\n---------------------------------------------------\n[SUCCESFUL] CLIENT CONNECTED WITH SERVER \n---------------------------------------------------\n%s",KCYN,KWHT);
  539.   ;
  540.  if((n_bytes=recv(sock, buffer, sizeof(buffer), 0))==-1){
  541.     printf("No recibe el server\n");
  542.     exit(-1);}
  543.   op_code = ldshort(buffer);
  544.  
  545. switch(op_code)
  546.   {
  547.     case MSG_HELLO_RQ:
  548.     process_HELLO_RQ_msg(sock);
  549.      break;
  550.     case MSG_LIST_RQ:
  551.       process_LIST_RQ_msg(sock, dnsTable);
  552.       break;
  553.     case MSG_DOMAIN_RQ:
  554.    process_DOMAIN_RQ_msg(sock, dnsTable);
  555.      break;
  556.    case MSG_ADD_DOMAIN:
  557.  
  558.       process_ADD_DOMAIN(sock, buffer, dnsTable, n_bytes);
  559.       break;  
  560.    case MSG_CHANGE_DOMAIN:
  561.    process_CHANGE_RQ_msg(sock, dnsTable);
  562.      break;              
  563.     case MSG_FINISH:
  564.       //TODO
  565.       done = 1;
  566.       break;
  567.     default:
  568.       perror("Message code does not exist.\n"); }
  569.  
  570.   return done;
  571. }
  572.  
  573.  
  574. int main (int argc, char * argv[])
  575. {
  576.   struct _DNSTable *dnsTable;
  577.   int port;
  578.   char dns_file[MAX_FILE_NAME_SIZE] ;
  579.  
  580.   getProgramOptions(argc, argv, dns_file, &port);
  581.  
  582.   dnsTable = loadDNSTableFromFile(dns_file);
  583.   printDNSTable(dnsTable);
  584.   //-----------------------------------------------------------------------------//
  585.   //A BORRAR
  586.   int pid; // fork
  587.   int goexit = 0;
  588.   //--------------
  589.   struct sockaddr_in serverAdr;
  590.   struct sockaddr_storage serverAlmacenamiento;
  591.   int fd1, fd2; // sockets
  592.   socklen_t addr_size; // tamaño direccion
  593.   //Creamos los sockets
  594.   if((fd1=socket(AF_INET, SOCK_STREAM,0))==-1){
  595.      perror("\n---------------------------------------------------\n[ERROR] SOCKET \n---------------------------------------------------\n");
  596.     exit(-1);
  597.   }
  598.   //Struct de la direccion del server
  599.   serverAdr.sin_family = AF_INET;
  600.   // Poner Puerto usando htons para el orden de los bytes
  601.   serverAdr.sin_port= htons(DEFAULT_PORT);
  602.   // Poner la IP --> Localhost
  603.   serverAdr.sin_addr.s_addr = INADDR_ANY;
  604.   //Poner todos los bits a 0
  605.   memset(serverAdr.sin_zero,'\0',sizeof serverAdr.sin_zero);
  606.  
  607.   //LA funcion bind asocia el socket dado por sockfd a la direccion local
  608.   //especificada por addr, para que el socket quede asignado al puerto
  609.   if((bind(fd1, (struct sockaddr *)&serverAdr, sizeof(serverAdr))==-1)){
  610.     perror("\n---------------------------------------------------\n[ERROR] BINDING \n---------------------------------------------------\n");
  611.     exit(-1);
  612.   }
  613. printf("%s\n---------------------------------------------------\n[SUCCESSFUL] BINDING \n---------------------------------------------------\n%s",KYEL,KNRM);
  614.   //Listen especifica que el socket desea aceptar conexiones
  615.   if(listen(fd1,100)==-1){
  616.     perror("Error in listen()");
  617.    }
  618.  printf("%s\n---------------------------------------------------\n[SUCCESSFUL] SERVER IS LISTENING\n---------------------------------------------------\n%s",KYEL,KNRM);
  619.   addr_size = sizeof serverAlmacenamiento;
  620.   while(1) {
  621.     goexit=0;
  622.      
  623.     //Accept devuelve un nuevo socket para la conexion (peticion)
  624.    
  625.      printf("hola\n");
  626.     if((fd2 = accept(fd1, (struct sockaddr *) &serverAlmacenamiento, &addr_size))==-1){
  627.       perror("\n---------------------------------------------------\n[ERROR] ACCEPTING \n---------------------------------------------------\n");
  628.       exit(-1);
  629.     }
  630.     printf("%s\n---------------------------------------------------\n[SUCCESSFUL] ACCEPT \n---------------------------------------------------\n%s",KYEL,KWHT);
  631.     pid=fork();
  632.     if(pid>0){
  633.       //pare
  634.       close(fd2);
  635.     }
  636.     else{
  637.       close(fd1);
  638.       while( goexit == 0){ goexit= process_msg(fd2, dnsTable);} // fork hijo
  639.       exit(0);
  640.  
  641.    }
  642.   }
  643.  // pare tanca fd2 -->
  644.   printf("%s",KWHT);
  645.   printf("FIN\n");
  646.   return 0;
  647. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement