y_node: add ifaddr to check local address node

This commit is contained in:
2025-10-18 08:50:06 +02:00
parent 14f5a53fff
commit 360a6c20df
2 changed files with 26 additions and 1 deletions
+3
View File
@@ -8,11 +8,14 @@
//#include "y_socket_t/y_socket_t.h" //#include "y_socket_t/y_socket_t.h"
#include "list_t/list_t.h" #include "list_t/list_t.h"
#include <sys/types.h>
#include <ifaddrs.h>
typedef struct y_node_t{ typedef struct y_node_t{
//char *id; //char *id;
struct sockaddr_storage addr; struct sockaddr_storage addr;
socklen_t addr_len; socklen_t addr_len;
int local_addr;/* 0 if not local address */
} y_NODE_T; } y_NODE_T;
+23 -1
View File
@@ -170,6 +170,8 @@ void * add_node_to_nodes(void* arg){
} }
void update_nodes(y_NODE_T node, struct main_list_y_NODE_T *nodes){ void update_nodes(y_NODE_T node, struct main_list_y_NODE_T *nodes){
struct ifaddrs *if_addr=NULL;
getifaddrs(&if_addr);
#if 0 #if 0
void* update_nodes(void* arg) void* update_nodes(void* arg)
struct arg_update_nodes * argU=(struct arg_update_nodes*)arg; struct arg_update_nodes * argU=(struct arg_update_nodes*)arg;
@@ -188,8 +190,28 @@ void* update_nodes(void* arg)
fprintf(stderr, "getnameinfo: %s\n", gai_strerror(status)); fprintf(stderr, "getnameinfo: %s\n", gai_strerror(status));
#endif #endif
if(NULL == search_node_in_list_y_NODE_T(nodes, node)) if(NULL == search_node_in_list_y_NODE_T(nodes, node)){
int c_af = node.addr.ss_family;
for(struct ifaddrs *cur_ifa = if_addr; cur_ifa; cur_ifa=cur_ifa->ifa_next){
if( c_af == cur_ifa->ifa_addr->sa_family){
if(c_af==AF_INET){
if(((struct sockaddr_in*)&(node.addr))->sin_addr.s_addr == ((struct sockaddr_in*)(cur_ifa->ifa_addr))->sin_addr.s_addr ){
node.local_addr = 1;
break;
}
}else if(c_af==AF_INET6){
if(memcmp(((struct sockaddr_in6*)&(node.addr))->sin6_addr.s6_addr , ((struct sockaddr_in6*)&(cur_ifa->ifa_addr))->sin6_addr.s6_addr, 8)==0){
node.local_addr = 1;
break;
}
}
}
}
push_back_list_y_NODE_T(nodes, node); push_back_list_y_NODE_T(nodes, node);
printf("debug: // /// // // update_nodes local_addr=%d\n",node.local_addr );
}
if(if_addr) freeifaddrs(if_addr);
// return NULL; // return NULL;
} }