y_socket : fix unitialize variable with valgrind!

This commit is contained in:
2025-06-26 21:56:32 +02:00
parent a1fe7c983d
commit cb84a03b0d
2 changed files with 56 additions and 18 deletions
+16
View File
@@ -271,12 +271,27 @@ GENERATE_LIST_ALL(TYPE_PTR)
#define GEN_HEAD_PTR_LIST(type)\
void remove_all_ptr_type_list_##type(struct main_list_##type *var_list);\
void purge_ptr_type_list_##type(struct main_list_##type *var_list);\
void free_##type(type arg);\
#define GEN_FUNC_PTR_LIST_FREE(type)\
void remove_all_ptr_type_list_##type(struct main_list_##type *var_list){\
struct list_##type *tmp = var_list->begin_list;\
while(tmp){\
var_list->current_list = tmp;\
tmp = tmp->next;\
free_##type(var_list->current_list->value);\
free(var_list->current_list);\
}\
var_list->begin_list = NULL;\
var_list->current_list = NULL;\
var_list->end_list = NULL;\
var_list->size = 0;\
var_list->current_index = 0;\
}\
void purge_ptr_type_list_##type(struct main_list_##type *var_list){\
struct list_##type *tmp = var_list->begin_list;\
while(tmp){\
@@ -292,6 +307,7 @@ GENERATE_LIST_ALL(TYPE_PTR)
var_list->current_index = 0;\
free(var_list);\
}\
\
void free_##type(type arg)\
+40 -18
View File
@@ -111,14 +111,14 @@ struct send_arg{
};
void y_socket_send_file_for_all_nodes(struct pollfd *fds, struct main_list_y_NODE_T *nodes, char * filename){
char tempAddr[BUF_SIZE];
char tempAddr[BUF_SIZE+1];
int c_af;
// char host[NI_MAXHOST], service[NI_MAXSERV];
char buf_send[BUF_SIZE];
char buf_send[BUF_SIZE+1];
int fd_file;
int retread;
/*int status = getnameinfo((struct sockaddr*)&(node.addr), node.addr_len, host, NI_MAXHOST, service, NI_MAXSERV, NI_NUMERICHOST);
#if 0
int status = getnameinfo((struct sockaddr*)&(node.addr), node.addr_len, host, NI_MAXHOST, service, NI_MAXSERV, NI_NUMERICHOST);
if(status)
// printf("debug: status ==0 : success: Received successfully from %s:%s\n", host,service);
// else
@@ -127,12 +127,8 @@ void y_socket_send_file_for_all_nodes(struct pollfd *fds, struct main_list_y_NOD
if(NULL == search_node_in_list_y_NODE_T(nodes, node))
push_back_list_y_NODE_T(nodes, node);
*/
/*
//UPPER
for(int i=0; i<nread; ++i)
if((buf[i] >='a') && (buf[i]<='z'))
buf[i]=buf[i]+'A'-'a';*/
#endif
fd_file = open( filename , O_RDONLY);
if(fd_file == -1){
fprintf(stderr,"error opening file |%s| for reading\n",filename);
@@ -140,17 +136,18 @@ void y_socket_send_file_for_all_nodes(struct pollfd *fds, struct main_list_y_NOD
}
//memset(buf_send, 0, BUF_SIZE+1);
while((retread = read(fd_file, buf_send, BUF_SIZE) ) > 0 ){
buf_send[retread]='\0';
//memset(msgRet, 0, BUF_SIZE + NI_MAXHOST + NI_MAXSERV + 100);
// sprintf(msgRet, "from %s:%s =%s",host, service, buf);
// len_msgRet = strlen(msgRet);
printf("sending response %s :\n",buf_send);
printf("debug: sending response %s :\n",buf_send);
FOR_LIST_FORM_BEGIN(y_NODE_T, nodes){
//memset(tempAddr, 0, BUF_SIZE+1);
c_af=(nodes->current_list->value).addr.ss_family;
//memset(tempAddr, 0, BUF_SIZE);
if(c_af==AF_INET){
if(NULL == inet_ntop(c_af,
&(GET_IN_type_ADDR(&(nodes->current_list->value),)),
@@ -171,6 +168,7 @@ void y_socket_send_file_for_all_nodes(struct pollfd *fds, struct main_list_y_NOD
}
#endif
printf("debug: destination %s :\n",tempAddr);
#if 1
if(sendto(fds[(c_af==AF_INET6)].fd,
@@ -184,9 +182,10 @@ void y_socket_send_file_for_all_nodes(struct pollfd *fds, struct main_list_y_NOD
){
fprintf(stderr, "Error sending response to %s\n",tempAddr);
}else
printf("sending response to %s\n",tempAddr);
printf("debug: sending response to %s\n",tempAddr);
#endif
}
//memset(buf_send, 0, BUF_SIZE+1);
}
close(fd_file);
@@ -261,7 +260,8 @@ int flags = fcntl(fds[af].fd, F_GETFL);
freeaddrinfo(result);
}
void y_socket_handler_(char * buf, struct pollfd *fds, struct main_list_y_NODE_T *nodes) {
void y_socket_handler_(char * buf, struct pollfd *fds, struct y_socket_t *sock){
struct main_list_y_NODE_T *nodes = sock->nodes;
printf("\n\n:::::::::::::::::::::::::::handler: : \n\n%s\n\n::::::::::::::::::::::::::\n",buf);
if(strncmp(buf, "GET", 3)==0){
if(strncmp(buf+4,"file",4)==0){
@@ -269,6 +269,13 @@ void y_socket_handler_(char * buf, struct pollfd *fds, struct main_list_y_NODE_T
y_socket_send_file_for_all_nodes(fds, nodes, filename) ;
}
}
if(strncmp(buf, "UPDATE", 6)==0){
if(strncmp(buf+7,"kill",4)==0){
pthread_mutex_lock(sock->mut_go_on);
sock->go_on = 0;
pthread_mutex_unlock(sock->mut_go_on);
}
}
}
void *y_socket_poll_fds(void *arg){
struct y_socket_t * argSock = (struct y_socket_t*)arg;
@@ -287,6 +294,7 @@ void *y_socket_poll_fds(void *arg){
ssize_t nread;
char buf[BUF_SIZE];
struct main_list_y_ptr_STRING *m_str=create_var_list_y_ptr_STRING();
char *temp_all_buf=NULL;
// char msgRet[BUF_SIZE + NI_MAXHOST + NI_MAXSERV + 100];
// int len_msgRet;
@@ -304,7 +312,7 @@ void *y_socket_poll_fds(void *arg){
}
for(af = v4; af<=v6;++af){
if(fds[af].revents && POLLIN){
remove_all_list_in_y_ptr_STRING(m_str);
remove_all_ptr_type_list_y_ptr_STRING(m_str);
memset(buf, 0, BUF_SIZE);
while((nread = recvfrom(fds[af].fd, buf, BUF_SIZE, 0,
(struct sockaddr *)&(node.addr), &(node.addr_len))) == BUF_SIZE){
@@ -332,7 +340,10 @@ void *y_socket_poll_fds(void *arg){
update_nodes(node, argSock->nodes);
char *temp_all_buf=NULL;
if(temp_all_buf){
free(temp_all_buf);
temp_all_buf=NULL;
}
/*size_t total_buf = */ copy_list_y_ptr_STRING_to_one_string(&temp_all_buf , m_str);
//printf("msg : %s\n",buf);
@@ -340,18 +351,29 @@ void *y_socket_poll_fds(void *arg){
///
///
y_socket_handler_(temp_all_buf, fds, argSock->nodes);
y_socket_handler_(temp_all_buf, fds, argSock);
///
}
}
#if 0
// printf("nread = %ld: buf=%s\nlen_buf=%ld\ncmp=%d\n",nread,buf,strlen(buf),strncmp(buf,"SHUTDOWN SERVER",15));
if(strncmp(buf,"SHUTDOWN SERVER",15) == 0){
printf("leave poll thread, bye!\n");
break;
//return NULL;
}
#endif
}
purge_ptr_type_list_y_ptr_STRING(m_str);
if(temp_all_buf){
free(temp_all_buf);
temp_all_buf=NULL;
}
return NULL;
}
#define str(x) # x