I am having a memory problem when executing this program, but I cannot solve it. The program is the client of a very simplified torrent made with TCP sockets. I can't quite understand where the error occurs. However, it depends on how you run it, it doesn't produce it. I have read answers to similar problems but I cannot replicate it to my case
How could I solve it?
struct torrent_t Torrent_Client;
struct sockaddr_in client_addr;
unsigned int peer_count = 0;
uint8_t buffer_enviar[RAW_MESSAGE_SIZE];
char buffer_rebre[RAW_MESSAGE_SIZE];
//Copy char* to another char*
char* aux = (char*) malloc(strlen(argv[1]));
printf("%s",aux);
strcpy(aux,argv[1]);
strtok(aux,".");
printf("Nom fitxer %s",argv[1]);
printf("Nom fitxer %s",aux);
int create_torrent = create_torrent_from_metainfo_file(argv[1], &Torrent_Client, aux);
if(create_torrent < 0){
perror("Error al crear el torrent! :(");
exit(EXIT_FAILURE);
}
//Socket
int socket_client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //TCP!
if(socket_client == -1){
perror("Error al crear el socket! :(");
exit(EXIT_FAILURE);
}
memset(&client_addr,0,sizeof(client_addr));
client_addr.sin_family = AF_INET;
//Direcció IP del socket.
uint32_t direccion=0x0;
direccion |= ((uint32_t) Torrent_Client.peers[peer_count].peer_address[3]);
direccion = direccion << 8;
direccion |= ((uint32_t) Torrent_Client.peers[peer_count].peer_address[2]);
direccion = direccion << 8;
direccion |= ((uint32_t) Torrent_Client.peers[peer_count].peer_address[1]);
direccion = direccion << 8;
direccion |= ((uint32_t) Torrent_Client.peers[peer_count].peer_address[0]);
client_addr.sin_addr.s_addr = direccion;
//Port del socket.
client_addr.sin_port = Torrent_Client.peers[peer_count].peer_port;
//Connect
int connect_client = connect(socket_client, (const struct sockaddr*) &client_addr, sizeof(client_addr));
if(connect_client < 0){
perror("Error al fer el conect! :(");
close(socket_client);
exit(EXIT_FAILURE);
}
//Recorrem els blocs
for(uint64_t bloc_number = 0; bloc_number < Torrent_Client.block_count; bloc_number++){
//Variables:
struct block_t bloque_rebut;
memset(&buffer_enviar,0,sizeof(buffer_enviar));
memset(&buffer_rebre,0,sizeof(buffer_rebre));
for(int i = 0; i < 4; i++){
buffer_enviar[i] = (uint8_t)(MAGIC_NUMBER >> 8*i);
}
buffer_enviar[4] = MSG_REQUEST;
//buffer_enviar[5] = (uint8_t)(bloc_number);
for(int i = 5; i < RAW_MESSAGE_SIZE-1; i++){
buffer_enviar[i] = 0;
}
buffer_enviar[12] = (uint8_t)(bloc_number);
printf("Enviar al bloc %li \n",bloc_number);
ssize_t send_client = send(socket_client,buffer_enviar,sizeof(buffer_enviar),0);
if(send_client < 0){
perror("Error al fer al enviar el missatge! :(");
//close(socket_client);
exit(EXIT_FAILURE);
}
ssize_t recv_client = recv(socket_client,&buffer_rebre,sizeof(buffer_rebre),0);
if(recv_client < 0){
perror("Error al rebre el missatge! :(");
//close(socket_client);
exit(EXIT_FAILURE);
}
if (load_block(&Torrent_Client, 0, &bloque_rebut)) {
//perror("Error load");
//exit(EXIT_FAILURE); Estaria bé amb perror?
printf("Error load");
}uint64_t mida = get_block_size(&Torrent_Client, bloc_number);
bloque_rebut.size = mida;
//char buffer_bloc[mida];
ssize_t recv_payload = recv(socket_client,&bloque_rebut,mida,0);
if(recv_payload < 0){
perror("Error al rebre el payload! :(");
//close(socket_client);
exit(EXIT_FAILURE);
}
int guardar_bloc = store_block(&Torrent_Client, bloc_number, &bloque_rebut);
printf("%li",bloc_number);
if(guardar_bloc < 0){
//close(socket_client);
perror("Error al guardar el bloc! :(");
}
}
printf("Ha funcionat correctament el torrent client");
close(socket_client);
return 0;
}
I have updated the line char* aux = (char*) malloc(strlen(argv[1]));
to char* aux = (char*) malloc(strlen(argv[1])+1);
and I removed printf("%s",aux);