1From 547f316821a3b24e028d539f7f48b5e3e5ba5c36 Mon Sep 17 00:00:00 2001 2From: compile_success <980965867@qq.com> 3Date: Wed, 19 Oct 2022 12:14:08 +0000 4Subject: [PATCH] add epoll_create1 and accept4 5 6--- 7 src/api/posix_api.c | 1 + 8 src/api/sockets.c | 34 ++++++++++++++++++++++++++++++---- 9 src/include/lwip/sockets.h | 21 +++++++++++++++++++++ 10 src/include/posix_api.h | 1 + 11 4 files changed, 53 insertions(+), 4 deletions(-) 12 13diff --git a/src/api/posix_api.c b/src/api/posix_api.c 14index 6afb9c6..e721381 100644 15--- a/src/api/posix_api.c 16+++ b/src/api/posix_api.c 17@@ -104,6 +104,7 @@ int posix_api_init(void) 18 CHECK_DLSYM_RET_RETURN(posix_api->fcntl64_fn = dlsym(handle, "fcntl64")); 19 CHECK_DLSYM_RET_RETURN(posix_api->pipe_fn = dlsym(handle, "pipe")); 20 CHECK_DLSYM_RET_RETURN(posix_api->epoll_create_fn = dlsym(handle, "epoll_create")); 21+ CHECK_DLSYM_RET_RETURN(posix_api->epoll_create1_fn = dlsym(handle, "epoll_create1")); 22 CHECK_DLSYM_RET_RETURN(posix_api->epoll_ctl_fn = dlsym(handle, "epoll_ctl")); 23 CHECK_DLSYM_RET_RETURN(posix_api->epoll_wait_fn = dlsym(handle, "epoll_wait")); 24 CHECK_DLSYM_RET_RETURN(posix_api->fork_fn = dlsym(handle, "fork")); 25diff --git a/src/api/sockets.c b/src/api/sockets.c 26index 4d4cea1..c939899 100644 27--- a/src/api/sockets.c 28+++ b/src/api/sockets.c 29@@ -543,10 +543,11 @@ get_socket_by_fd(int fd) 30 * @param newconn the netconn for which to allocate a socket 31 * @param accepted 1 if socket has been created by accept(), 32 * 0 if socket has been created by socket() 33+ * @param flags only support SOCK_CLOEXEC and SOCK_NONBLOCK 34 * @return the index of the new socket; -1 on error 35 */ 36 static int 37-alloc_socket(struct netconn *newconn, int accepted) 38+alloc_socket(struct netconn *newconn, int accepted, int flags) 39 { 40 int i; 41 SYS_ARCH_DECL_PROTECT(lev); 42@@ -570,12 +571,19 @@ alloc_socket(struct netconn *newconn, int accepted) 43 break; 44 } 45 46+ /*add CLOEXEC OR NONBLOCK OR NONE*/ 47+ type |= flags; 48+ 49 SYS_ARCH_PROTECT(lev); 50 i = posix_api->socket_fn(domain, type, protocol); 51 if (i == -1) { 52 goto err; 53 } 54 55+ if ((flags & O_NONBLOCK) != 0){ 56+ netconn_set_nonblocking(newconn, flags & O_NONBLOCK); 57+ } 58+ 59 if ((i < LWIP_SOCKET_OFFSET) || (i >= sockets_num + LWIP_SOCKET_OFFSET)) { 60 goto err; 61 } 62@@ -721,7 +729,7 @@ free_socket(struct lwip_sock *sock, int is_tcp) 63 */ 64 65 int 66-lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen) 67+lwip_accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags) 68 { 69 struct lwip_sock *sock, *nsock; 70 struct netconn *newconn; 71@@ -755,7 +763,7 @@ lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen) 72 } 73 LWIP_ASSERT("newconn != NULL", newconn != NULL); 74 75- newsock = alloc_socket(newconn, 1); 76+ newsock = alloc_socket(newconn, 1, flags); 77 if (newsock == -1) { 78 netconn_delete(newconn); 79 sock_set_errno(sock, ENFILE); 80@@ -827,6 +835,12 @@ lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen) 81 return newsock; 82 } 83 84+int 85+lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen) 86+{ 87+ return lwip_accept4(s, addr, addrlen, 0); 88+} 89+ 90 int 91 lwip_bind(int s, const struct sockaddr *name, socklen_t namelen) 92 { 93@@ -1823,6 +1837,10 @@ lwip_socket(int domain, int type, int protocol) 94 95 LWIP_UNUSED_ARG(domain); /* @todo: check this */ 96 97+ int flags = type & ~SOCK_TYPE_MASK; 98+ type &= SOCK_TYPE_MASK; 99+ 100+ 101 /* create a netconn */ 102 switch (type) { 103 case SOCK_RAW: 104@@ -1862,7 +1880,15 @@ lwip_socket(int domain, int type, int protocol) 105 return -1; 106 } 107 108- i = alloc_socket(conn, 0); 109+ if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)){ 110+ set_errno(EINVAL); 111+ return -1; 112+ } 113+ 114+ if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK)) 115+ flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK; 116+ 117+ i = alloc_socket(conn, 0, flags); 118 119 if (i == -1) { 120 netconn_delete(conn); 121diff --git a/src/include/lwip/sockets.h b/src/include/lwip/sockets.h 122index 4e7e671..3c5b87b 100644 123--- a/src/include/lwip/sockets.h 124+++ b/src/include/lwip/sockets.h 125@@ -573,6 +573,7 @@ void lwip_socket_thread_cleanup(void); /* LWIP_NETCONN_SEM_PER_THREAD==1: destro 126 #if LWIP_COMPAT_SOCKETS == 2 127 /* This helps code parsers/code completion by not having the COMPAT functions as defines */ 128 #define lwip_accept accept 129+#define lwip_accept4 accept4 130 #define lwip_bind bind 131 #define lwip_shutdown shutdown 132 #define lwip_getpeername getpeername 133@@ -614,7 +615,25 @@ int fcntl(int s, int cmd, ...); 134 #endif /* LWIP_POSIX_SOCKETS_IO_NAMES */ 135 #endif /* LWIP_COMPAT_SOCKETS == 2 */ 136 137+#ifndef O_CLOEXEC 138+#define O_CLOEXEC 02000000 139+#endif 140+ 141+#ifndef SOCK_TYPE_MASK 142+#define SOCK_TYPE_MASK 0xf 143+#endif 144+ 145+#ifndef SOCK_CLOEXEC 146+#define SOCK_CLOEXEC O_CLOEXEC 147+#endif 148+ 149+#ifndef SOCK_NONBLOCK 150+#define SOCK_NONBLOCK O_NONBLOCK 151+#endif 152+ 153+ 154 int lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen); 155+int lwip_accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags); 156 int lwip_bind(int s, const struct sockaddr *name, socklen_t namelen); 157 int lwip_shutdown(int s, int how); 158 int lwip_getpeername (int s, struct sockaddr *name, socklen_t *namelen); 159@@ -661,6 +680,8 @@ int lwip_inet_pton(int af, const char *src, void *dst); 160 /** @ingroup socket */ 161 #define accept(s,addr,addrlen) lwip_accept(s,addr,addrlen) 162 /** @ingroup socket */ 163+#define accept4(s,addr,addrlen,flags) lwip_accept4(s,addr,addrlen,flags) 164+/** @ingroup socket */ 165 #define bind(s,name,namelen) lwip_bind(s,name,namelen) 166 /** @ingroup socket */ 167 #define shutdown(s,how) lwip_shutdown(s,how) 168diff --git a/src/include/posix_api.h b/src/include/posix_api.h 169index c8f2cf9..e958ded 100644 170--- a/src/include/posix_api.h 171+++ b/src/include/posix_api.h 172@@ -66,6 +66,7 @@ typedef struct { 173 int (*fcntl64_fn)(int fd, int cmd, ...); 174 int (*pipe_fn)(int pipefd[2]); 175 int (*epoll_create_fn)(int size); 176+ int (*epoll_create1_fn)(int size); 177 int (*epoll_ctl_fn)(int epfd, int op, int fd, struct epoll_event *event); 178 int (*epoll_wait_fn)(int epfd, struct epoll_event *events, int maxevents, int timeout); 179 int (*epoll_close_fn)(int epfd); 180-- 1812.33.0 182 183