Buenos días, tengo un problemas con una función, todo funciona perfecto, pero cuando lo paso por valgrind me da un error con la función 'send' supuestamente. Así que no sé realmente, ya me cansé de verificar y por eso pido su ayuda.
Función:
Código C:
Ver original#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
extern struct {
int res_http;
int long size_f;
} http_header;
int download_package (const char * name_package, const char * dir_package, const char * server_mirror, const char * protocol)
{
int long total_size_bytes = 0;
int size_bytes;
int sockfd;
char buffer[1024];
struct addrinfo *recv_s;
struct addrinfo *result;
struct addrinfo hints;
bzero(&(hints), 8);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
if (getaddrinfo(server_mirror, protocol, &hints, &recv_s) < 0) {
printf("%c[%d;%dm[Error]%c[%dm Error al resolver el DNS de %s\n", 27, 1, 31, 27, 0, server_mirror
); return -1;
}
for (result = recv_s ; result != NULL ; result = result->ai_next) {
if ((sockfd = socket(result->ai_family, result->ai_socktype, result->ai_protocol)) < 0)
continue;
if (connect(sockfd, result->ai_addr, result->ai_addrlen) < 0) {
close(sockfd);
continue;
}
break;
freeaddrinfo(recv_s);
}
if (result == NULL) {
printf("%c[%d;%dm[Error]%c[%dm No se pudo conectar al servidor. \n", 27, 1, 31, 27, 0); return -1;
}
snprintf(header
, sizeof(header
), "GET /package/%c/%s.tar HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", name_package
[0], name_package
, server_mirror
);
if (send(sockfd, header, sizeof(header), result->ai_flags) < 0) {
printf("%c[%d;%dm[Error]%c[%dm No se pudo enviar la petición al servidor.\n", 27, 1, 31, 27, 0);
freeaddrinfo(result);
close(sockfd);
return -1;
}
bzero(&buffer, sizeof(buffer));
/* Esto envia la primera petición, no sé
* si funcionará así. Ya veremos... */
if ((size_bytes = recv(sockfd, buffer, sizeof(buffer)-1, 0)) > 0) {
if (!strstr(buffer
, "HTTP/1.1 200 OK")) {
close(sockfd);
freeaddrinfo(result);
return -1;
}
}
do {
total_size_bytes += size_bytes;
} while ((size_bytes = recv(sockfd, buffer, sizeof(buffer)-1, 0)) > 0);
/* Aquí toda falta, guardar los datos
* y hacer la verificación de tamaño
* según HTTP y SHA256
*/
freeaddrinfo(result);
return -2;
}
Lo que me envía valgrind es
Código C:
Ver original[guzzano@localhost src]$ valgrind -v --track-origins=yes ./w0rm
[...]
--25703-- Discarding syms at 0x442ea40-0x4435e9c in /usr/lib/libnss_files-2.16.so due to munmap()
--25703-- Discarding syms at 0x443a8a0-0x443b674 in /usr/lib/libnss_mdns4_minimal.so.2 due to munmap()
--25703-- Discarding syms at 0x443ec40-0x4442318 in /usr/lib/libnss_dns-2.16.so due to munmap()
--25703-- Discarding syms at 0x4777f700-0x4778e0c4 in /usr/lib/libresolv-2.16.so due to munmap()
==25703==
==25703== HEAP SUMMARY:
==25703== in use at
exit: 0 bytes in
0 blocks
==25703== total heap usage: 78 allocs, 78 frees, 6,722 bytes allocated
==25703==
==25703== All heap blocks were freed -- no leaks are possible
==25703==
==25703== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==25703==
==25703== 1 errors in context 1 of 1:
==25703== Syscall param socketcall.send(msg) points to uninitialised byte(s)
==25703== at 0x47587821: send (in /usr/lib/libc-2.16.so)
==25703== by 0x804886B: download_package (in /home/guzzano/Escritorio/w0rm/src/w0rm)
==25703== by 0x8048980: main (in /home/guzzano/Escritorio/w0rm/src/w0rm)
==25703== Address 0xbecdda5e is on thread 1s stack
==25703== Uninitialised value was created by a stack allocation
==25703== at 0x8048655: download_package (in /home/guzzano/Escritorio/w0rm/src/w0rm)
==25703==
==25703== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Muchas gracias.