From 2489b4b21632c9016f87c6fe963d27ef3e20951b Mon Sep 17 00:00:00 2001
From: zhengjiebing <zhengjiebing_yewu@cmss.chinamobile.com>
Date: Fri, 5 Jan 2024 14:32:48 +0800
Subject: [PATCH] match_host_addr func support ipv6

---
 src/lstack/api/lstack_wrap.c           | 12 +++++++++++-
 src/lstack/core/lstack_cfg.c           | 12 +++++++++---
 src/lstack/core/lstack_control_plane.c |  2 +-
 src/lstack/include/lstack_cfg.h        |  2 +-
 src/lstack/netif/lstack_vdev.c         |  2 +-
 5 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/src/lstack/api/lstack_wrap.c b/src/lstack/api/lstack_wrap.c
index 56f89b2..3db62c7 100644
--- a/src/lstack/api/lstack_wrap.c
+++ b/src/lstack/api/lstack_wrap.c
@@ -217,7 +217,17 @@ static int32_t do_bind(int32_t s, const struct sockaddr *name, socklen_t namelen
         return g_wrap_api->bind_fn(s, name, namelen);
     }
 
-    if (match_host_addr(((struct sockaddr_in *)name)->sin_addr.s_addr)) {
+    ip_addr_t sock_addr = IPADDR_ANY_TYPE_INIT;
+    if (name->sa_family == AF_INET) {
+        sock_addr.type = IPADDR_TYPE_V4;
+        sock_addr.u_addr.ip4.addr = ((struct sockaddr_in *)name)->sin_addr.s_addr;
+    } else if (name->sa_family == AF_INET6) {
+        sock_addr.type = IPADDR_TYPE_V6;
+        memcpy_s(sock_addr.u_addr.ip6.addr, IPV6_ADDR_LEN,
+            ((struct sockaddr_in6 *)name)->sin6_addr.s6_addr, IPV6_ADDR_LEN);
+    }
+
+    if (match_host_addr(&sock_addr)) {
         /* maybe kni addr */
         if (posix_api->bind_fn(s, name, namelen) != 0) {
             SET_CONN_TYPE_LIBOS(sock->conn);
diff --git a/src/lstack/core/lstack_cfg.c b/src/lstack/core/lstack_cfg.c
index d9c23fb..c1f5680 100644
--- a/src/lstack/core/lstack_cfg.c
+++ b/src/lstack/core/lstack_cfg.c
@@ -253,11 +253,17 @@ static int32_t parse_host_addr6(void)
     return 0;
 }
 
-int32_t match_host_addr(uint32_t addr)
+int32_t match_host_addr(ip_addr_t *addr)
 {
     /* network byte order */
-    if (addr == g_config_params.host_addr.addr || addr == INADDR_ANY) {
-        return 1;
+    if (IP_IS_V4_VAL(*addr)) {
+        if (ip4_addr_cmp(&addr->u_addr.ip4, &g_config_params.host_addr) || ip4_addr_isany_val(addr->u_addr.ip4)) {
+            return 1;
+        }
+    } else if (IP_IS_V6_VAL(*addr)) {
+        if (ip6_addr_cmp(&addr->u_addr.ip6, &g_config_params.host_addr6) || ip6_addr_isany_val(addr->u_addr.ip6)) {
+            return 1;
+        }
     }
     return 0;
 }
diff --git a/src/lstack/core/lstack_control_plane.c b/src/lstack/core/lstack_control_plane.c
index 2d629c8..a9a3814 100644
--- a/src/lstack/core/lstack_control_plane.c
+++ b/src/lstack/core/lstack_control_plane.c
@@ -366,7 +366,7 @@ static int32_t reg_conn(enum tcp_list_state table_state, enum reg_ring_type reg_
         qtuple.dst_ip = conn->conn_list[i].rip;
         qtuple.dst_port = lwip_htons(conn->conn_list[i].r_port);
 
-        if ((table_state == LISTEN_LIST) && (!match_host_addr(qtuple.src_ip.u_addr.ip4.addr))) {
+        if ((table_state == LISTEN_LIST) && (!match_host_addr((ip_addr_t *)&qtuple.src_ip))) {
             continue;
         }
 
diff --git a/src/lstack/include/lstack_cfg.h b/src/lstack/include/lstack_cfg.h
index 82e96d8..c1074f8 100644
--- a/src/lstack/include/lstack_cfg.h
+++ b/src/lstack/include/lstack_cfg.h
@@ -133,7 +133,7 @@ int gazelle_param_init(int *argc, char **argv);
 int gazelle_copy_param(const char *param, bool is_double,
     int *argc, char argv[][PATH_MAX]);
 
-int match_host_addr(uint32_t ipv4);
+int match_host_addr(ip_addr_t *addr);
 int32_t init_stack_numa_cpuset(struct protocol_stack *stack);
 
 #endif /* GAZELLE_NET_CFG_H */
diff --git a/src/lstack/netif/lstack_vdev.c b/src/lstack/netif/lstack_vdev.c
index 9a79dc3..c845f7a 100644
--- a/src/lstack/netif/lstack_vdev.c
+++ b/src/lstack/netif/lstack_vdev.c
@@ -230,7 +230,7 @@ int32_t vdev_reg_xmit(enum reg_ring_type type, struct gazelle_quintuple *qtuple)
     struct protocol_stack *stack = get_protocol_stack();
 
     if (type == REG_RING_TCP_LISTEN || type == REG_RING_TCP_LISTEN_CLOSE) {
-        if (!match_host_addr(qtuple->src_ip.u_addr.ip4.addr)) {
+        if (!match_host_addr((ip_addr_t *)&qtuple->src_ip)) {
             LSTACK_LOG(INFO, LSTACK, "lstack ip not match in conf.\n");
             return 0;
         }
-- 
2.33.0

