From 1a5db09e38595f4090e5cb17d1349ccf998f236f Mon Sep 17 00:00:00 2001 From: maosiping Date: Tue, 15 Aug 2023 18:04:09 +0800 Subject: [PATCH] use lwip Signed-off-by: maosiping --- library/net_sockets.c | 44 +++++++++++++++++++++--- mbedtls.gni | 9 +++++ port/config/compat_lwip/socket_compat.h | 21 +++++++++++ port/config/compat_posix/socket_compat.h | 19 ++++++++++ 4 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 port/config/compat_lwip/socket_compat.h create mode 100644 port/config/compat_posix/socket_compat.h diff --git a/library/net_sockets.c b/library/net_sockets.c index 3c026fc..75d6551 100644 --- a/library/net_sockets.c +++ b/library/net_sockets.c @@ -111,10 +111,9 @@ static int wsa_init_done = 0; #include -#ifdef LITEOS_VERSION +#include "socket_compat.h" +#ifdef USE_LWIP #include "lwip/sockets.h" -#define close(fd) lwip_close(fd) -#define shutdown(fd, how) lwip_shutdown(fd, how) #endif /* @@ -220,7 +219,11 @@ int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, break; } +#ifdef USE_LWIP + lwip_close(ctx->fd); +#else close( ctx->fd ); +#endif ret = MBEDTLS_ERR_NET_CONNECT_FAILED; } @@ -267,14 +270,22 @@ int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char if( setsockopt( ctx->fd, SOL_SOCKET, SO_REUSEADDR, (const char *) &n, sizeof( n ) ) != 0 ) { +#ifdef USE_LWIP + lwip_close(ctx->fd); +#else close( ctx->fd ); +#endif ret = MBEDTLS_ERR_NET_SOCKET_FAILED; continue; } if( bind( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) != 0 ) { +#ifdef USE_LWIP + lwip_close(ctx->fd); +#else close( ctx->fd ); +#endif ret = MBEDTLS_ERR_NET_BIND_FAILED; continue; } @@ -284,7 +295,11 @@ int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char { if( listen( ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 ) { +#ifdef USE_LWIP + lwip_close(ctx->fd); +#else close( ctx->fd ); +#endif ret = MBEDTLS_ERR_NET_LISTEN_FAILED; continue; } @@ -326,8 +341,12 @@ static int net_would_block( const mbedtls_net_context *ctx ) /* * Never return 'WOULD BLOCK' on a blocking socket */ +#ifdef USE_LWIP + if ((lwip_fcntl(ctx->fd, F_GETFL, 0) & O_NONBLOCK) != O_NONBLOCK) { +#else if( ( fcntl( ctx->fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK ) { +#endif errno = err; return( 0 ); } @@ -474,8 +493,12 @@ int mbedtls_net_set_block( mbedtls_net_context *ctx ) !defined(EFI32) u_long n = 0; return( ioctlsocket( ctx->fd, FIONBIO, &n ) ); +#else +#ifdef USE_LWIP + return lwip_fcntl(ctx->fd, F_SETFL, lwip_fcntl(ctx->fd, F_GETFL, 0) & ~O_NONBLOCK); #else return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) & ~O_NONBLOCK ) ); +#endif // USE_LWIP #endif } @@ -485,8 +508,12 @@ int mbedtls_net_set_nonblock( mbedtls_net_context *ctx ) !defined(EFI32) u_long n = 1; return( ioctlsocket( ctx->fd, FIONBIO, &n ) ); +#else +#ifdef USE_LWIP + return lwip_fcntl(ctx->fd, F_SETFL, lwip_fcntl(ctx->fd, F_GETFL, 0) | O_NONBLOCK); #else return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) | O_NONBLOCK ) ); +#endif // USE_LWIP #endif } @@ -712,8 +739,11 @@ void mbedtls_net_close( mbedtls_net_context *ctx ) { if( ctx->fd == -1 ) return; - +#ifdef USE_LWIP + lwip_close(ctx->fd); +#else close( ctx->fd ); +#endif ctx->fd = -1; } @@ -725,9 +755,13 @@ void mbedtls_net_free( mbedtls_net_context *ctx ) { if( ctx->fd == -1 ) return; - +#ifdef USE_LWIP + lwip_shutdown(ctx->fd, 2); + lwip_close(ctx->fd); +#else shutdown( ctx->fd, 2 ); close( ctx->fd ); +#endif ctx->fd = -1; } diff --git a/mbedtls.gni b/mbedtls.gni index 322d1d7..5caaf85 100755 --- a/mbedtls.gni +++ b/mbedtls.gni @@ -139,3 +139,12 @@ if (defined(ohos_lite)) { MBEDTLS_INLCUDE_DIRS += [ "$MBEDTLSDIR/port/include" ] } + +if (product_name == "generic_m55_arm_32_bes_aurora_wear_mini_application") { + MBEDTLS_INLCUDE_DIRS += [ "$MBEDTLSDIR/port/config/compat_lwip" ] +} else if (product_name == + "generic_m55_arm_32_bes_phoinix_wear_mini_application") { + MBEDTLS_INLCUDE_DIRS += [ "$MBEDTLSDIR/port/config/compat_lwip" ] +} else { + MBEDTLS_INLCUDE_DIRS += [ "$MBEDTLSDIR/port/config/compat_posix" ] +} diff --git a/port/config/compat_lwip/socket_compat.h b/port/config/compat_lwip/socket_compat.h new file mode 100644 index 0000000..811e306 --- /dev/null +++ b/port/config/compat_lwip/socket_compat.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MBED_TLS_SOCKET_COMPAT_H +#define MBED_TLS_SOCKET_COMPAT_H + +#define USE_LWIP 1 + +#endif /* MBED_TLS_SOCKET_COMPAT_H */ diff --git a/port/config/compat_posix/socket_compat.h b/port/config/compat_posix/socket_compat.h new file mode 100644 index 0000000..da43953 --- /dev/null +++ b/port/config/compat_posix/socket_compat.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MBED_TLS_SOCKET_COMPAT_H +#define MBED_TLS_SOCKET_COMPAT_H + +#endif /* MBED_TLS_SOCKET_COMPAT_H */ -- GitLab