From f5df6b241f38a5830920038c05d41ed4444efe63 Mon Sep 17 00:00:00 2001
From: jiangheng <jiangheng14@huawei.com>
Date: Mon, 5 Feb 2024 17:41:16 +0800
Subject: add struct gz addr

---
 src/core/tcp.c         |  2 +-
 src/core/udp.c         |  2 +-
 src/include/lwipopts.h |  1 +
 src/include/reg_sock.h | 36 +++++++++++++++++++++++++++++++++---
 4 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/src/core/tcp.c b/src/core/tcp.c
index 17f922e..9f240b8 100644
--- a/src/core/tcp.c
+++ b/src/core/tcp.c
@@ -1161,7 +1161,7 @@ tcp_new_port(void)
 
       if (__atomic_load_n(&port_state[tcp_port - TCP_LOCAL_PORT_RANGE_START], __ATOMIC_ACQUIRE) == 0) {
 #if GAZELLE_ENABLE
-        if (port_in_stack_queue(pcb->remote_ip, pcb->local_ip, pcb->remote_port, tcp_port)) {
+        if (port_in_stack_queue((gz_addr_t *)&pcb->remote_ip, (gz_addr_t *)&pcb->local_ip, pcb->remote_port, tcp_port)) {
             tmp_port = tcp_port;
             __atomic_store_n(&port_state[tcp_port - TCP_LOCAL_PORT_RANGE_START], 1, __ATOMIC_RELEASE);
             break;
diff --git a/src/core/udp.c b/src/core/udp.c
index 02ffe36..ca82e51 100644
--- a/src/core/udp.c
+++ b/src/core/udp.c
@@ -132,7 +132,7 @@ udp_new_port(struct udp_pcb *dst_pcb)
     }
 
     if (__atomic_load_n(&port_state[udp_port - UDP_LOCAL_PORT_RANGE_START], __ATOMIC_ACQUIRE) == 0) {
-      if (port_in_stack_queue(dst_pcb->remote_ip, dst_pcb->local_ip, dst_pcb->remote_port, udp_port)) {
+      if (port_in_stack_queue((gz_addr_t *)&dst_pcb->remote_ip, (gz_addr_t *)&dst_pcb->local_ip, dst_pcb->remote_port, udp_port)) {
           tmp_port = udp_port;
           __atomic_store_n(&port_state[udp_port - UDP_LOCAL_PORT_RANGE_START], 1, __ATOMIC_RELEASE);
           break;
diff --git a/src/include/lwipopts.h b/src/include/lwipopts.h
index 11bc65a..4cd2d7a 100644
--- a/src/include/lwipopts.h
+++ b/src/include/lwipopts.h
@@ -178,6 +178,7 @@
 */
 #define LWIP_IPV6                1
 #define IP6_HLEN                 40
+#define LWIP_IPV6_SCOPES         1
 
 /*
    ---------------------------------
diff --git a/src/include/reg_sock.h b/src/include/reg_sock.h
index 5a5e971..a11102e 100644
--- a/src/include/reg_sock.h
+++ b/src/include/reg_sock.h
@@ -34,7 +34,35 @@
 #define __REG_SOCK_H__
 
 #include <stdbool.h>
-#include "lwip/ip_addr.h"
+
+#include "lwipopts.h"
+
+/*  compatible with ip4_addr_t */
+struct gz_ip4 {
+  uint32_t addr;
+};
+
+/*  compatible with ip6_addr_t */
+#if LWIP_IPV6
+struct gz_ip6 {
+  uint32_t addr[4];
+#if LWIP_IPV6_SCOPES
+  uint8_t zone;
+#endif /* LWIP_IPV6_SCOPES */
+};
+#endif /* LWIP_IPV6 */
+
+/* gazelle ip address, compatible with ip_addr_t */
+typedef struct gz_addr {
+    union {
+#if LWIP_IPV6
+        struct gz_ip6 ip6;
+#endif /* LWIP_IPV6 */
+        struct gz_ip4 ip4;
+    } u_addr;
+    /** @ref lwip_ip_addr_type */
+    uint8_t type;
+} gz_addr_t;
 
 enum reg_ring_type {
     REG_RING_TCP_LISTEN = 0,
@@ -45,10 +73,12 @@ enum reg_ring_type {
 };
 
 struct gazelle_quintuple {
-	uint32_t protocol;
+    uint32_t protocol;
     /* net byte order */
     uint16_t src_port;
     uint16_t dst_port;
+
+    /* TODO: replace with gz_addr_t */
     uint32_t src_ip;
     uint32_t dst_ip;
 #if LWIP_IPV6
@@ -65,6 +95,6 @@ struct reg_ring_msg {
 };
 
 extern int vdev_reg_xmit(enum reg_ring_type type, struct gazelle_quintuple *qtuple);
-extern bool port_in_stack_queue(ip_addr_t src_ip, ip_addr_t dst_ip, uint16_t src_port, uint16_t dst_port);
+extern bool port_in_stack_queue(gz_addr_t *src_ip, gz_addr_t *dst_ip, uint16_t src_port, uint16_t dst_port);
 
 #endif /* __REG_SOCK_H__ */
-- 
2.33.0

