From 77b675543a0f1d0ce9a5e7d09cfd142f161072e3 Mon Sep 17 00:00:00 2001 From: maosiping Date: Thu, 3 Aug 2023 14:49:41 +0800 Subject: [PATCH] liteos only ipv4 Signed-off-by: maosiping --- BUILD.gn | 13 +++++++++++++ library/net_sockets.c | 16 ++++++++++++++++ mbedtls.gni | 12 +++++++++++- port/BUILD.gn | 3 ++- port/include/mbedtls_log.h | 1 - port/include/tls_client.h | 5 ++++- port/src/tls_client.c | 5 ++--- 7 files changed, 48 insertions(+), 7 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index 076c974..6482fea 100755 --- a/BUILD.gn +++ b/BUILD.gn @@ -27,10 +27,23 @@ MBEDTLS_SOURCES += [ "library/x509write_csr.c", ] +if (defined(ohos_lite)) { + MBEDTLS_SOURCES -= [ "library/ssl_srv.c" ] +} + if (defined(ohos_lite)) { import("//build/lite/config/component/lite_component.gni") import("//build/lite/ndk/ndk.gni") + if (ohos_kernel_type != "liteos_m") { + MBEDTLS_SOURCES += [ "library/ssl_srv.c" ] + MBEDTLS_SOURCES -= [ + "$MBEDTLSDIR/library/ssl_srv.c", + "$MBEDTLSDIR/port/src/tls_client.c", + "$MBEDTLSDIR/port/src/tls_certificate.c", + ] + } + config("mbedtls_config") { include_dirs = MBEDTLS_INLCUDE_DIRS if (ohos_kernel_type == "liteos_m") { diff --git a/library/net_sockets.c b/library/net_sockets.c index 17a9e4a..ebe3b5f 100644 --- a/library/net_sockets.c +++ b/library/net_sockets.c @@ -193,8 +193,15 @@ int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, ret = MBEDTLS_ERR_NET_UNKNOWN_HOST; for( cur = addr_list; cur != NULL; cur = cur->ai_next ) { +#ifdef LITEOS_VERSION + if (cur->ai_family != AF_INET || cur->ai_socktype != SOCK_STREAM) { + continue; + } + ctx->fd = (int) socket(AF_INET, SOCK_STREAM, 0); +#else ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype, cur->ai_protocol ); +#endif if( ctx->fd < 0 ) { ret = MBEDTLS_ERR_NET_SOCKET_FAILED; @@ -575,8 +582,12 @@ int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len ) ret = check_fd( fd, 0 ); if( ret != 0 ) return( ret ); +#ifdef LITEOS_VERSION + ret = (int) recv( fd, buf, len, 0); +#else ret = (int) read( fd, buf, len ); +#endif if( ret < 0 ) { @@ -658,7 +669,12 @@ int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len ) if( ret != 0 ) return( ret ); +#ifdef LITEOS_VERSION + ret = (int) send( fd, buf, len, 0); +#else + ret = (int) write( fd, buf, len ); +#endif if( ret < 0 ) { diff --git a/mbedtls.gni b/mbedtls.gni index 42c3681..322d1d7 100755 --- a/mbedtls.gni +++ b/mbedtls.gni @@ -24,7 +24,7 @@ # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. MBEDTLSDIR = "//third_party/mbedtls/" KERNELDIR = "//kernel/liteos_m/" @@ -129,3 +129,13 @@ MBEDTLS_INLCUDE_DIRS = [ "$MBEDTLSDIR/include/mbedtls", "$MBEDTLSDIR/tests/include", ] + +if (defined(ohos_lite)) { + MBEDTLS_SOURCES += [ + "$MBEDTLSDIR/library/ssl_srv.c", + "$MBEDTLSDIR/port/src/tls_client.c", + "$MBEDTLSDIR/port/src/tls_certificate.c", + ] + + MBEDTLS_INLCUDE_DIRS += [ "$MBEDTLSDIR/port/include" ] +} diff --git a/port/BUILD.gn b/port/BUILD.gn index 60ee16b..016e59c 100755 --- a/port/BUILD.gn +++ b/port/BUILD.gn @@ -11,7 +11,7 @@ # 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. -# +# import("//build/lite/config/component/lite_component.gni") @@ -22,6 +22,7 @@ if (ohos_build_type == "debug") { "//third_party/mbedtls/include", "//commonlibrary/utils_lite/include", "//third_party/bounds_checking_function/include", + "//third_party/mbedtls/port/include", ] } diff --git a/port/include/mbedtls_log.h b/port/include/mbedtls_log.h index 1210dad..96767c3 100755 --- a/port/include/mbedtls_log.h +++ b/port/include/mbedtls_log.h @@ -30,7 +30,6 @@ #ifndef OHOS_DEBUG #define DECORATOR_HILOG(op, fmt, args...) \ do { \ - op(LOG_CORE, fmt, ##args); \ } while (0) #else #define DECORATOR_HILOG(op, fmt, args...) \ diff --git a/port/include/tls_client.h b/port/include/tls_client.h index 320ce47..65f6b4a 100755 --- a/port/include/tls_client.h +++ b/port/include/tls_client.h @@ -17,7 +17,7 @@ #define MBEDTLS_CLIENT_H #include "mbedtls/platform.h" -#include "mbedtls/net_sockets" +#include "mbedtls/net_sockets.h" #include "mbedtls/ssl.h" #include "mbedtls/entropy.h" #include "mbedtls/ctr_drbg.h" @@ -25,6 +25,9 @@ #define RET_ERROR -1; #define RET_EOK 0 +#ifndef LOG_CORE +#define LOG_CORE 3 +#endif typedef struct MbedTLSSession { char *host; diff --git a/port/src/tls_client.c b/port/src/tls_client.c index a23d288..c25dce9 100755 --- a/port/src/tls_client.c +++ b/port/src/tls_client.c @@ -90,7 +90,7 @@ int MbedtlsClientContext(MbedTLSSession *session) int ret = mbedtls_x509_crt_parse(&session->cacert, (const unsigned char *)G_MBEDTLS_ROOT_CERTIFICATE, G_MBEDTLS_ROOT_CERTIFICATE_LEN); if (ret < 0) { - LOGE("mbedtls_x509_crt_parse error, return -0x%x.", -ret); + LOGD("mbedtls_x509_crt_parse error, return -0x%x.", -ret); return ret; } @@ -135,7 +135,7 @@ int MbedtlsClientConnect(MbedTLSSession *session) if (session == NULL) { return -RET_ERROR; } - LOGI("connect: host:%s, port: %s", session->host, session->port); + LOGD("connect: host:%s, port: %s", session->host, session->port); int ret = mbedtls_net_connect(&session->server_fd, session->host, session->port, MBEDTLS_NET_PROTO_TCP); if (ret != 0) { @@ -145,7 +145,6 @@ int MbedtlsClientConnect(MbedTLSSession *session) LOGD("Connected %s:%s fd:%d, success...", session->host, session->port, session->server_fd.fd); mbedtls_ssl_set_bio(&session->ssl, &session->server_fd, mbedtls_net_send, mbedtls_net_recv, NULL); - LOGD("ssl state=%d", session->ssl.state); while ((ret = mbedtls_ssl_handshake(&session->ssl)) != 0) { LOGD("mbedtls_ssl_handshake ret=0x%x.", -ret); -- GitLab