diff options
| author | yangjie11 <[email protected]> | 2018-11-28 19:02:11 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2018-11-28 19:02:11 +0800 |
| commit | 1a7f95f92b84b0a3acd6243cb2a658b4f9b6f1ac (patch) | |
| tree | 3b90c3e4b7ced83ce239ae8df020926eaf74d7de /components/net/sal_socket/impl | |
| parent | 4a43d3cdae1b731dcf51d3531048c51fd4142516 (diff) | |
| parent | d4469fe7d83d08913d096dc6d75a750dd854c0cc (diff) | |
Merge pull request #1 from RT-Thread/master
test
Diffstat (limited to 'components/net/sal_socket/impl')
| -rw-r--r-- | components/net/sal_socket/impl/af_inet.h | 34 | ||||
| -rw-r--r-- | components/net/sal_socket/impl/af_inet_at.c | 49 | ||||
| -rw-r--r-- | components/net/sal_socket/impl/af_inet_lwip.c | 91 | ||||
| -rw-r--r-- | components/net/sal_socket/impl/proto_mbedtls.c | 239 |
4 files changed, 350 insertions, 63 deletions
diff --git a/components/net/sal_socket/impl/af_inet.h b/components/net/sal_socket/impl/af_inet.h new file mode 100644 index 0000000000..8b7bf9d2a6 --- /dev/null +++ b/components/net/sal_socket/impl/af_inet.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-08-25 ChenYong First version + */ + +#ifndef __AF_INET_H__ +#define __AF_INET_H__ + +#include <rtthread.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef SAL_USING_LWIP +/* lwIP protocol family register */ +int lwip_inet_init(void); +#endif + +#ifdef SAL_USING_AT +/* AT protocol family register */ +int at_inet_init(void); +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* __AF_INET_H__ */ diff --git a/components/net/sal_socket/impl/af_inet_at.c b/components/net/sal_socket/impl/af_inet_at.c index 8ddb236e84..90d1537967 100644 --- a/components/net/sal_socket/impl/af_inet_at.c +++ b/components/net/sal_socket/impl/af_inet_at.c @@ -1,36 +1,27 @@ /* - * File : af_inet_at.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2018-06-06 ChenYong First version */ +#include <rtthread.h> + #include <netdb.h> #include <sal.h> #include <at_socket.h> +#include <af_inet.h> #ifdef SAL_USING_POSIX #include <dfs_poll.h> #endif +#ifdef SAL_USING_AT + #ifdef SAL_USING_POSIX static int at_poll(struct dfs_fd *file, struct rt_pollreq *req) { @@ -71,7 +62,7 @@ static int at_poll(struct dfs_fd *file, struct rt_pollreq *req) } #endif -static const struct proto_ops at_inet_stream_ops = +static const struct sal_socket_ops at_socket_ops = { at_socket, at_closesocket, @@ -90,8 +81,6 @@ static const struct proto_ops at_inet_stream_ops = #ifdef SAL_USING_POSIX at_poll, -#else - NULL, #endif /* SAL_USING_POSIX */ }; @@ -101,25 +90,33 @@ static int at_create(struct sal_socket *socket, int type, int protocol) //TODO Check type & protocol - socket->ops = &at_inet_stream_ops; + socket->ops = &at_socket_ops; return 0; } -static const struct proto_family at_inet_family_ops = { - AF_AT, - AF_INET, - at_create, +static struct sal_proto_ops at_proto_ops = +{ at_gethostbyname, NULL, - at_freeaddrinfo, at_getaddrinfo, + at_freeaddrinfo, +}; + +static const struct sal_proto_family at_inet_family = +{ + AF_AT, + AF_INET, + at_create, + &at_proto_ops, }; int at_inet_init(void) { - sal_proto_family_register(&at_inet_family_ops); + sal_proto_family_register(&at_inet_family); return 0; } INIT_COMPONENT_EXPORT(at_inet_init); + +#endif /* SAL_USING_AT */ diff --git a/components/net/sal_socket/impl/af_inet_lwip.c b/components/net/sal_socket/impl/af_inet_lwip.c index deb9239bf4..0826b052ab 100644 --- a/components/net/sal_socket/impl/af_inet_lwip.c +++ b/components/net/sal_socket/impl/af_inet_lwip.c @@ -1,21 +1,7 @@ /* - * File : af_inet_lwip.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -34,6 +20,7 @@ #endif #include <sal.h> +#include <af_inet.h> #if LWIP_VERSION < 0x2000000 #define SELWAIT_T int @@ -43,6 +30,13 @@ #endif #endif +#ifdef SAL_USING_LWIP + +#ifdef SAL_USING_POSIX + +#if LWIP_VERSION >= 0x20100ff +#include <lwip/priv/sockets_priv.h> +#else /* LWIP_VERSION < 0x20100ff */ /* * Re-define lwip socket * @@ -72,10 +66,9 @@ struct lwip_sock { /** counter of how many threads are waiting for this socket using select */ SELWAIT_T select_waiting; -#ifdef SAL_USING_POSIX rt_wqueue_t wait_head; -#endif }; +#endif /* LWIP_VERSION >= 0x20100ff */ extern struct lwip_sock *lwip_tryget_socket(int s); @@ -148,7 +141,11 @@ static void event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len break; } - if (sock->lastdata || sock->rcvevent > 0) +#if LWIP_VERSION >= 0x20100ff + if ((void*)(sock->lastdata.pbuf) || (sock->rcvevent > 0)) +#else + if ((void*)(sock->lastdata) || (sock->rcvevent > 0)) +#endif event |= POLLIN; if (sock->sendevent) event |= POLLOUT; @@ -159,14 +156,14 @@ static void event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len if (event) { -#ifdef SAL_USING_POSIX rt_wqueue_wakeup(&sock->wait_head, (void*) event); -#endif } } +#endif /* SAL_USING_POSIX */ static int inet_socket(int domain, int type, int protocol) { +#ifdef SAL_USING_POSIX int socket; socket = lwip_socket(domain, type, protocol); @@ -177,17 +174,18 @@ static int inet_socket(int domain, int type, int protocol) lwsock = lwip_tryget_socket(socket); lwsock->conn->callback = event_callback; -#ifdef SAL_USING_POSIX rt_wqueue_init(&lwsock->wait_head); -#endif - } return socket; +#else + return lwip_socket(domain, type, protocol); +#endif /* SAL_USING_POSIX */ } static int inet_accept(int socket, struct sockaddr *addr, socklen_t *addrlen) { +#ifdef SAL_USING_POSIX int new_socket; new_socket = lwip_accept(socket, addr, addrlen); @@ -197,12 +195,13 @@ static int inet_accept(int socket, struct sockaddr *addr, socklen_t *addrlen) lwsock = lwip_tryget_socket(new_socket); -#ifdef SAL_USING_POSIX rt_wqueue_init(&lwsock->wait_head); -#endif } return new_socket; +#else + return lwip_accept(socket, addr, addrlen); +#endif /* SAL_USING_POSIX */ } static int inet_getsockname(int socket, struct sockaddr *name, socklen_t *namelen) @@ -215,6 +214,7 @@ static int inet_getsockname(int socket, struct sockaddr *name, socklen_t *namele return lwip_getsockname(socket, name, namelen); } +#ifdef SAL_USING_POSIX static int inet_poll(struct dfs_fd *file, struct rt_pollreq *req) { int mask = 0; @@ -235,7 +235,12 @@ static int inet_poll(struct dfs_fd *file, struct rt_pollreq *req) rt_poll_add(&sock->wait_head, req); level = rt_hw_interrupt_disable(); - if (sock->lastdata || sock->rcvevent) + +#if LWIP_VERSION >= 0x20100ff + if ((void*)(sock->lastdata.pbuf) || sock->rcvevent) +#else + if ((void*)(sock->lastdata) || sock->rcvevent) +#endif { mask |= POLLIN; } @@ -252,16 +257,18 @@ static int inet_poll(struct dfs_fd *file, struct rt_pollreq *req) return mask; } +#endif -static const struct proto_ops lwip_inet_stream_ops = { +static const struct sal_socket_ops lwip_socket_ops = +{ inet_socket, lwip_close, lwip_bind, lwip_listen, lwip_connect, inet_accept, - lwip_sendto, - lwip_recvfrom, + (int (*)(int, const void *, size_t, int, const struct sockaddr *, socklen_t))lwip_sendto, + (int (*)(int, void *, size_t, int, struct sockaddr *, socklen_t *))lwip_recvfrom, lwip_getsockopt, //TODO fix on 1.4.1 lwip_setsockopt, @@ -269,7 +276,9 @@ static const struct proto_ops lwip_inet_stream_ops = { lwip_getpeername, inet_getsockname, lwip_ioctl, +#ifdef SAL_USING_POSIX inet_poll, +#endif }; static int inet_create(struct sal_socket *socket, int type, int protocol) @@ -278,25 +287,33 @@ static int inet_create(struct sal_socket *socket, int type, int protocol) //TODO Check type & protocol - socket->ops = &lwip_inet_stream_ops; + socket->ops = &lwip_socket_ops; return 0; } -static const struct proto_family lwip_inet_family_ops = { - AF_INET, - AF_INET, - inet_create, +static struct sal_proto_ops lwip_proto_ops = +{ lwip_gethostbyname, lwip_gethostbyname_r, - lwip_freeaddrinfo, lwip_getaddrinfo, + lwip_freeaddrinfo, +}; + +static const struct sal_proto_family lwip_inet_family = +{ + AF_INET, + AF_INET, + inet_create, + &lwip_proto_ops, }; int lwip_inet_init(void) { - sal_proto_family_register(&lwip_inet_family_ops); + sal_proto_family_register(&lwip_inet_family); return 0; } INIT_COMPONENT_EXPORT(lwip_inet_init); + +#endif /* SAL_USING_LWIP */ diff --git a/components/net/sal_socket/impl/proto_mbedtls.c b/components/net/sal_socket/impl/proto_mbedtls.c new file mode 100644 index 0000000000..8ed9f67ff1 --- /dev/null +++ b/components/net/sal_socket/impl/proto_mbedtls.c @@ -0,0 +1,239 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-11-12 ChenYong First version + */ + +#include <rtthread.h> + +#ifdef RT_USING_DFS +#include <dfs_posix.h> +#endif + +#ifdef SAL_USING_TLS +#include <sal_tls.h> +#endif +#include <netdb.h> +#include <sal.h> + +#ifdef SAL_USING_TLS + +#if !defined(MBEDTLS_CONFIG_FILE) +#include <mbedtls/config.h> +#else +#include MBEDTLS_CONFIG_FILE +#endif + +#include <tls_certificate.h> +#include <tls_client.h> + +#ifndef SAL_MEBDTLS_BUFFER_LEN +#define SAL_MEBDTLS_BUFFER_LEN 1024 +#endif + +static void *mebdtls_socket(int socket) +{ + MbedTLSSession *session = RT_NULL; + char *pers = "mbedtls"; + + if (socket < 0) + { + return RT_NULL; + } + + session = (MbedTLSSession *) tls_calloc(1, sizeof(MbedTLSSession)); + if (session == RT_NULL) + { + return RT_NULL; + } + + session->buffer_len = SAL_MEBDTLS_BUFFER_LEN; + session->buffer = tls_calloc(1, session->buffer_len); + if (session->buffer == RT_NULL) + { + tls_free(session); + session = RT_NULL; + + return RT_NULL; + } + + /* initialize TLS Client sesison */ + if (mbedtls_client_init(session, (void *) pers, rt_strlen(pers)) != RT_EOK) + { + mbedtls_client_close(session); + return RT_NULL; + } + session->server_fd.fd = socket; + + return (void *)session; +} + +int mbedtls_net_send_cb(void *ctx, const unsigned char *buf, size_t len) +{ + struct sal_socket *sock; + int socket, ret; + + RT_ASSERT(ctx); + RT_ASSERT(buf); + + socket = ((mbedtls_net_context *) ctx)->fd; + sock = sal_get_socket(socket); + if (sock == RT_NULL) + { + return -1; + } + + /* Register scoket sendto option to TLS send data callback */ + ret = sock->ops->sendto((int) sock->user_data, (void *)buf, len, 0, RT_NULL, RT_NULL); + if (ret < 0) + { +#ifdef RT_USING_DFS + if ((fcntl(socket, F_GETFL) & O_NONBLOCK) == O_NONBLOCK) + return MBEDTLS_ERR_SSL_WANT_WRITE; +#endif + if (errno == ECONNRESET) + return MBEDTLS_ERR_NET_CONN_RESET; + if ( errno == EINTR) + return MBEDTLS_ERR_SSL_WANT_READ; + + return MBEDTLS_ERR_NET_SEND_FAILED ; + } + + return ret; +} + +int mbedtls_net_recv_cb( void *ctx, unsigned char *buf, size_t len) +{ + struct sal_socket *sock; + int socket, ret; + + RT_ASSERT(ctx); + RT_ASSERT(buf); + + socket = ((mbedtls_net_context *) ctx)->fd; + sock = sal_get_socket(socket); + if (sock == RT_NULL) + { + return -1; + } + + /* Register scoket recvfrom option to TLS recv data callback */ + ret = sock->ops->recvfrom((int) sock->user_data, (void *)buf, len, 0, RT_NULL, RT_NULL); + if (ret < 0) + { +#ifdef RT_USING_DFS + if ((fcntl(socket, F_GETFL) & O_NONBLOCK) == O_NONBLOCK) + return MBEDTLS_ERR_SSL_WANT_WRITE; +#endif + if (errno == ECONNRESET) + return MBEDTLS_ERR_NET_CONN_RESET; + if ( errno == EINTR) + return MBEDTLS_ERR_SSL_WANT_READ; + + return MBEDTLS_ERR_NET_RECV_FAILED ; + } + + return ret; +} + +static int mbedtls_connect(void *sock) +{ + MbedTLSSession *session = RT_NULL; + int ret = 0; + + RT_ASSERT(sock); + + session = (MbedTLSSession *) sock; + + /* Set the SSL Configure infromation */ + ret = mbedtls_client_context(session); + if (ret < 0) + { + goto __exit; + } + + /* Set the underlying BIO callbacks for write, read and read-with-timeout. */ + mbedtls_ssl_set_bio(&session->ssl, &session->server_fd, mbedtls_net_send_cb, mbedtls_net_recv_cb, RT_NULL); + + while ((ret = mbedtls_ssl_handshake(&session->ssl)) != 0) + { + if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) + { + goto __exit; + } + } + + /* Return the result of the certificate verification */ + ret = mbedtls_ssl_get_verify_result(&session->ssl); + if (ret != 0) + { + rt_memset(session->buffer, 0x00, session->buffer_len); + mbedtls_x509_crt_verify_info((char *)session->buffer, session->buffer_len, " ! ", ret); + goto __exit; + } + + return ret; + +__exit: + if (session) + { + mbedtls_client_close(session); + } + + return ret; +} + +static int mbedtls_closesocket(void *sock) +{ + struct sal_socket *ssock; + int socket; + + if (sock == RT_NULL) + { + return 0; + } + + socket = ((MbedTLSSession *) sock)->server_fd.fd; + ssock = sal_get_socket(socket); + if (ssock == RT_NULL) + { + return -1; + } + + /* Close TLS client session, and clean user-data in SAL socket */ + mbedtls_client_close((MbedTLSSession *) sock); + ssock->user_data_tls = RT_NULL; + + return 0; +} + +static const struct sal_proto_tls_ops mbedtls_proto_ops= +{ + RT_NULL, + mebdtls_socket, + mbedtls_connect, + (int (*)(void *sock, const void *data, size_t size)) mbedtls_client_write, + (int (*)(void *sock, void *mem, size_t len)) mbedtls_client_read, + mbedtls_closesocket, +}; + +static const struct sal_proto_tls mbedtls_proto = +{ + "mbedtls", + &mbedtls_proto_ops, +}; + +int sal_mbedtls_proto_init(void) +{ + /* register MbedTLS protocol options to SAL */ + sal_proto_tls_register(&mbedtls_proto); + + return 0; +} +INIT_COMPONENT_EXPORT(sal_mbedtls_proto_init); + +#endif /* SAL_USING_TLS */ |
