Ver Mensaje Individual
  #1 (permalink)  
Antiguo 04/04/2013, 22:44
Avatar de guzzano
guzzano
 
Fecha de Ingreso: julio-2010
Ubicación: Isla de Margarita
Mensajes: 162
Antigüedad: 13 años, 8 meses
Puntos: 13
Recomendaciones con sockets

Buenos días, he estado programando una función usando sockets para descargar un fichero, pero siento un poco insatisfecho, aunque esta funciona perfecto. Necesito alguna opinión o recomendación acerca de ella.

Código C:
Ver original
  1. /*        d_package.c - Descarga de los paquetes
  2.  
  3.    Este programa es software libre: usted puede redistribuirlo y/o
  4.    modificarlo bajo los términos de la Licencia Pública General de
  5.    GNU  según es publicada por la Free Software Foundation, bien
  6.    sea la versión 3 de la Licencia, o (a su elección) cualquier
  7.    versión posterior.
  8.    
  9.    Este programa se distribuye con la esperanza de que sea útil,
  10.    pero SIN NINGUNA GARANTÍA, incluso sin la garantía implícita de
  11.    COMERCIALIZACIÓN o IDONEIDAD PARA UN PROPÓSITO PARTICULAR. Consulte
  12.    la GNU General Public License para más detalles.
  13.  
  14.    Debería haber recibido una copia de la Licencia Pública General de GNU
  15.    junto con este programa. Si no, véase <http://www.gnu.org/licenses/>.
  16.  
  17.    Escrito por Alberto 'guzzan0' José Guilarte para w0rmlinux */
  18.    
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <fcntl.h>
  24.  
  25. #include <netdb.h>
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29. #include <arpa/inet.h>
  30.  
  31. #include "sha256.c"
  32. #include "include/d_package.h"
  33.  
  34. int download_package (char const *name_package,
  35.               char const *mirror,
  36.               char const *architecture,
  37.               char const *sha256_check,
  38.               char const *dir_save_package) {
  39.    
  40.     int status_sock;
  41.     int size_header = strlen(name_package)+strlen(architecture)+20;
  42.     int p_file = open(dir_save_package, O_WRONLY | O_TRUNC | O_CREAT, 664);
  43.    
  44.     if (p_file < 0)
  45.         return 1;
  46.    
  47.     ssize_t bytes_rev;
  48.     ssize_t bytes_recv = 0;
  49.    
  50.     char http_header[size_header];
  51.     char buffer[256];
  52.    
  53.     if (p_file == -1)
  54.         return 1;
  55.    
  56.     if ((status_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
  57.         return 0;
  58.        
  59.     struct hostent *addr_sock;
  60.     struct sockaddr_in st_sock;
  61.    
  62.     if ((addr_sock = gethostbyname(mirror)) == NULL)
  63.         return 0;
  64.  
  65.     st_sock.sin_family = AF_INET;
  66.     st_sock.sin_port = htons(80);
  67.     st_sock.sin_addr = *((struct in_addr *)addr_sock->h_addr);
  68.    
  69.     memset(&(st_sock.sin_zero), 0, 8);
  70.  
  71.     if ((connect(status_sock, (struct sockaddr *)&st_sock, sizeof(st_sock))) == -1)
  72.         return 0;
  73.    
  74.     snprintf(http_header, sizeof(size_header), "GET /package/%s/%c/%s.tar", architecture, name_package[0], name_package);
  75.    
  76.     if ((send(status_sock, http_header, sizeof(http_header), 0)) <= 0)
  77.         return 0;
  78.    
  79.     memset(buffer, '\0', sizeof(buffer));
  80.    
  81.     while ((bytes_rev = recv(status_sock, buffer, sizeof(buffer), 0)) > 0) {
  82.         if (bytes_rev < 0)
  83.             return 0;
  84.        
  85.         write(p_file, buffer, bytes_rev);
  86.        
  87.         bytes_recv += bytes_rev;
  88.     }
  89.    
  90.     close(status_sock);
  91.     close(p_file);
  92.        
  93.     if ((sha256_compare(dir_save_package, sha256_check)) == 32) {
  94.         return 2;
  95.     }
  96.     else {
  97.         remove(dir_save_package);
  98.     }
  99.    
  100.     return 1;
  101. }

Lo único que ya sé, es que aún no he hecho el soporte para ipv6, pero cualquier recomendación, estaría agradecido.

Saludos.