From 34a090b2cfb88cb2e711d02c0f3eddf3a3656fac Mon Sep 17 00:00:00 2001
From: compile_success <980965867@qq.com>
Date: Fri, 19 Jul 2024 07:34:47 +0000
Subject: [PATCH] support kernel connect

---
 src/lstack/api/lstack_epoll.c |  2 +-
 src/lstack/api/lstack_wrap.c  | 10 +++++++++-
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/lstack/api/lstack_epoll.c b/src/lstack/api/lstack_epoll.c
index 417499b..6426de4 100644
--- a/src/lstack/api/lstack_epoll.c
+++ b/src/lstack/api/lstack_epoll.c
@@ -439,7 +439,7 @@ int32_t lstack_rtw_epoll_ctl(int32_t epfd, int32_t op, int32_t fd, struct epoll_
 
     struct wakeup_poll *wakeup = epoll_sock->wakeup;
     struct lwip_sock *sock = lwip_get_socket(fd);
-    if (POSIX_IS_CLOSED(sock)) {
+    if (POSIX_IS_CLOSED(sock) || POSIX_IS_TYPE(sock, POSIX_KERNEL)) {
         return posix_api->epoll_ctl_fn(epfd, op, fd, event);
     }
 
diff --git a/src/lstack/api/lstack_wrap.c b/src/lstack/api/lstack_wrap.c
index b19990e..a6ea485 100644
--- a/src/lstack/api/lstack_wrap.c
+++ b/src/lstack/api/lstack_wrap.c
@@ -317,8 +317,16 @@ static int32_t do_connect(int32_t s, const struct sockaddr *name, socklen_t name
         ret = posix_api->connect_fn(s, name, namelen);
         POSIX_SET_TYPE(sock, POSIX_KERNEL);
     } else {
+	/* When the socket is POSIX_LWIP_OR_KERNEL, connect to lwip first and then connect to kernel. */
         ret = g_wrap_api->connect_fn(s, name, namelen);
-        POSIX_SET_TYPE(sock, POSIX_LWIP);
+        if (ret == 0) {
+            POSIX_SET_TYPE(sock, POSIX_LWIP);
+	} else {
+	    ret = posix_api->connect_fn(s, name, namelen);
+            if (ret == 0) {
+		POSIX_SET_TYPE(sock, POSIX_KERNEL);
+	    }
+	}
     }
 
     return ret;
-- 
2.33.0

