diff options
| author | Man, Jianting (Meco) <[email protected]> | 2022-02-19 11:07:45 -0500 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-02-20 00:07:45 +0800 |
| commit | 6895da53071358cdd4da8f15f32c977dfedfbe89 (patch) | |
| tree | c65401f29a630222c0928487415b1d0346b0488b /components/net/sal_socket | |
| parent | 5a61304a09bb2341565407d8fdc0a14f03c50aa3 (diff) | |
[net] adjust folder structure (#5587)
* [net] adjust folder structure
- 将lwip相关的文件夹,移入lwip文件夹
- Kconfig 分散到at netdev lwip sal文件夹中,使用menuconfig代替menu
Diffstat (limited to 'components/net/sal_socket')
19 files changed, 0 insertions, 3043 deletions
diff --git a/components/net/sal_socket/SConscript b/components/net/sal_socket/SConscript deleted file mode 100644 index 3903dce89a..0000000000 --- a/components/net/sal_socket/SConscript +++ /dev/null @@ -1,35 +0,0 @@ -# RT-Thread building script for component - -from building import * - -cwd = GetCurrentDir() - -src = Glob('src/*.c') -src += Glob('socket/net_netdb.c') - -CPPPATH = [cwd + '/include'] -CPPPATH += [cwd + '/include/socket'] - -if GetDepend('SAL_USING_LWIP'): - src += Glob('impl/af_inet_lwip.c') - -if GetDepend('SAL_USING_AT'): - src += Glob('impl/af_inet_at.c') - -if GetDepend('SAL_USING_LWIP') or GetDepend('SAL_USING_AT'): - CPPPATH += [cwd + '/impl'] - -if GetDepend('SAL_USING_TLS'): - src += Glob('impl/proto_mbedtls.c') - -if GetDepend('SAL_USING_POSIX'): - CPPPATH += [cwd + '/include/dfs_net'] - src += Glob('socket/net_sockets.c') - src += Glob('dfs_net/*.c') - -if not GetDepend('HAVE_SYS_SOCKET_H'): - CPPPATH += [cwd + '/include/socket/sys_socket'] - -group = DefineGroup('SAL', src, depend = ['RT_USING_SAL'], CPPPATH = CPPPATH) - -Return('group') diff --git a/components/net/sal_socket/dfs_net/dfs_net.c b/components/net/sal_socket/dfs_net/dfs_net.c deleted file mode 100644 index 67b652c320..0000000000 --- a/components/net/sal_socket/dfs_net/dfs_net.c +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2015-02-17 Bernard First version - * 2016-05-07 Bernard Rename dfs_lwip to dfs_net - * 2018-03-09 Bernard Fix the last data issue in poll. - * 2018-05-24 ChenYong Add socket abstraction layer - */ - -#include <rtthread.h> - -#include <dfs.h> -#include <dfs_net.h> - -#include <sys/socket.h> - -int dfs_net_getsocket(int fd) -{ - int socket; - struct dfs_fd *_dfs_fd; - - _dfs_fd = fd_get(fd); - if (_dfs_fd == NULL) return -1; - - if (_dfs_fd->type != FT_SOCKET) socket = -1; - else socket = (int)_dfs_fd->data; - - fd_put(_dfs_fd); /* put this dfs fd */ - return socket; -} - -static int dfs_net_ioctl(struct dfs_fd* file, int cmd, void* args) -{ - int socket = (int) file->data; - - return sal_ioctlsocket(socket, cmd, args); -} - -static int dfs_net_read(struct dfs_fd* file, void *buf, size_t count) -{ - int socket = (int) file->data; - - return sal_recvfrom(socket, buf, count, 0, NULL, NULL); -} - -static int dfs_net_write(struct dfs_fd *file, const void *buf, size_t count) -{ - int socket = (int) file->data; - - return sal_sendto(socket, buf, count, 0, NULL, 0); -} - -static int dfs_net_close(struct dfs_fd* file) -{ - int socket = (int) file->data; - - return sal_closesocket(socket); -} - -static int dfs_net_poll(struct dfs_fd *file, struct rt_pollreq *req) -{ - extern int sal_poll(struct dfs_fd *file, struct rt_pollreq *req); - - return sal_poll(file, req); -} - -const struct dfs_file_ops _net_fops = -{ - NULL, /* open */ - dfs_net_close, - dfs_net_ioctl, - dfs_net_read, - dfs_net_write, - NULL, - NULL, /* lseek */ - NULL, /* getdents */ - dfs_net_poll, -}; - -const struct dfs_file_ops *dfs_net_get_fops(void) -{ - return &_net_fops; -} diff --git a/components/net/sal_socket/impl/af_inet.h b/components/net/sal_socket/impl/af_inet.h deleted file mode 100644 index 421d15bfae..0000000000 --- a/components/net/sal_socket/impl/af_inet.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2006-2021, 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 - -/* Set lwIP network interface device protocol family information */ -int sal_lwip_netdev_set_pf_info(struct netdev *netdev); - -#endif /* SAL_USING_LWIP */ - -#ifdef SAL_USING_AT - -/* Set AT network interface device protocol family information */ -int sal_at_netdev_set_pf_info(struct netdev *netdev); - -#endif /* SAL_USING_AT */ - -#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 deleted file mode 100644 index 07dff0018c..0000000000 --- a/components/net/sal_socket/impl/af_inet_at.c +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * 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> - -#include <netdev.h> - -#ifdef SAL_USING_POSIX -#include <poll.h> -#endif - -#ifdef SAL_USING_AT - -#ifdef SAL_USING_POSIX -static int at_poll(struct dfs_fd *file, struct rt_pollreq *req) -{ - int mask = 0; - struct at_socket *sock; - struct sal_socket *sal_sock; - - sal_sock = sal_get_socket((int) file->data); - if(!sal_sock) - { - return -1; - } - - sock = at_get_socket((int)sal_sock->user_data); - if (sock != NULL) - { - rt_base_t level; - - rt_poll_add(&sock->wait_head, req); - - level = rt_hw_interrupt_disable(); - if (sock->rcvevent) - { - mask |= POLLIN; - } - if (sock->sendevent) - { - mask |= POLLOUT; - } - if (sock->errevent) - { - mask |= POLLERR; - } - rt_hw_interrupt_enable(level); - } - - return mask; -} -#endif - -static const struct sal_socket_ops at_socket_ops = -{ - at_socket, - at_closesocket, - at_bind, - NULL, - at_connect, - NULL, - at_sendto, - at_recvfrom, - at_getsockopt, - at_setsockopt, - at_shutdown, - NULL, - NULL, - NULL, -#ifdef SAL_USING_POSIX - at_poll, -#endif /* SAL_USING_POSIX */ -}; - -static const struct sal_netdb_ops at_netdb_ops = -{ - at_gethostbyname, - NULL, - at_getaddrinfo, - at_freeaddrinfo, -}; - -static const struct sal_proto_family at_inet_family = -{ - AF_AT, - AF_INET, - &at_socket_ops, - &at_netdb_ops, -}; - - -/* Set AT network interface device protocol family information */ -int sal_at_netdev_set_pf_info(struct netdev *netdev) -{ - RT_ASSERT(netdev); - - netdev->sal_user_data = (void *) &at_inet_family; - return 0; -} - -#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 deleted file mode 100644 index 2cf0958ee4..0000000000 --- a/components/net/sal_socket/impl/af_inet_lwip.c +++ /dev/null @@ -1,337 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2018-05-17 ChenYong First version - */ - -#include <rtthread.h> - -#include <lwip/sockets.h> -#include <lwip/netdb.h> -#include <lwip/api.h> -#include <lwip/init.h> -#include <lwip/netif.h> - -#ifdef SAL_USING_POSIX -#include <poll.h> -#endif - -#include <sal.h> -#include <af_inet.h> - -#include <netdev.h> - -#if (LWIP_VERSION < 0x2000000) && NETDEV_IPV6 -#error "The lwIP version is not support IPV6, please disable netdev IPV6 configuration " -#elif (LWIP_VERSION > 0x2000000) && (NETDEV_IPV6 != LWIP_IPV6) -#error "IPV6 configuration error, Please check and synchronize netdev and lwip IPV6 configuration." -#endif - -#if LWIP_VERSION < 0x2000000 -#define SELWAIT_T int -#else -#ifndef SELWAIT_T -#define SELWAIT_T u8_t -#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 - * - * NOTE: please make sure the definitions same in lwip::net_socket.c - */ -struct lwip_sock { - /** sockets currently are built on netconns, each socket has one netconn */ - struct netconn *conn; - /** data that was left from the previous read */ - void *lastdata; - /** offset in the data that was left from the previous read */ - u16_t lastoffset; - /** number of times data was received, set by event_callback(), - tested by the receive and select functions */ - s16_t rcvevent; - /** number of times data was ACKed (free send buffer), set by event_callback(), - tested by select */ - u16_t sendevent; - /** error happened for this socket, set by event_callback(), tested by select */ - u16_t errevent; - /** last error that occurred on this socket */ -#if LWIP_VERSION < 0x2000000 - int err; -#else - u8_t err; -#endif - /** counter of how many threads are waiting for this socket using select */ - SELWAIT_T select_waiting; - - rt_wqueue_t wait_head; -}; -#endif /* LWIP_VERSION >= 0x20100ff */ - -extern struct lwip_sock *lwip_tryget_socket(int s); - -static void event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len) -{ - int s; - struct lwip_sock *sock; - uint32_t event = 0; - SYS_ARCH_DECL_PROTECT(lev); - - LWIP_UNUSED_ARG(len); - - /* Get socket */ - if (conn) - { - s = conn->socket; - if (s < 0) - { - /* Data comes in right away after an accept, even though - * the server task might not have created a new socket yet. - * Just count down (or up) if that's the case and we - * will use the data later. Note that only receive events - * can happen before the new socket is set up. */ - SYS_ARCH_PROTECT(lev); - if (conn->socket < 0) - { - if (evt == NETCONN_EVT_RCVPLUS) - { - conn->socket--; - } - SYS_ARCH_UNPROTECT(lev); - return; - } - s = conn->socket; - SYS_ARCH_UNPROTECT(lev); - } - - sock = lwip_tryget_socket(s); - if (!sock) - { - return; - } - } - else - { - return; - } - - SYS_ARCH_PROTECT(lev); - /* Set event as required */ - switch (evt) - { - case NETCONN_EVT_RCVPLUS: - sock->rcvevent++; - break; - case NETCONN_EVT_RCVMINUS: - sock->rcvevent--; - break; - case NETCONN_EVT_SENDPLUS: - sock->sendevent = 1; - break; - case NETCONN_EVT_SENDMINUS: - sock->sendevent = 0; - break; - case NETCONN_EVT_ERROR: - sock->errevent = 1; - break; - default: - LWIP_ASSERT("unknown event", 0); - break; - } - -#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; - if (sock->errevent) - event |= POLLERR; - - SYS_ARCH_UNPROTECT(lev); - - if (event) - { - rt_wqueue_wakeup(&sock->wait_head, (void*) event); - } -} -#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); - if (socket >= 0) - { - struct lwip_sock *lwsock; - - lwsock = lwip_tryget_socket(socket); - lwsock->conn->callback = event_callback; - - rt_wqueue_init(&lwsock->wait_head); - } - - 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); - if (new_socket >= 0) - { - struct lwip_sock *lwsock; - - lwsock = lwip_tryget_socket(new_socket); - - rt_wqueue_init(&lwsock->wait_head); - } - - 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) -{ -#if LWIP_VERSION_MAJOR < 2U - rt_kprintf("ERROR: Your lwIP version is not supported. Please using lwIP 2.0.0+.\n"); - RT_ASSERT(LWIP_VERSION_MAJOR >= 2U); -#endif - - return lwip_getsockname(socket, name, namelen); -} - -int inet_ioctlsocket(int socket, long cmd, void *arg) -{ - switch (cmd) - { - case F_GETFL: - case F_SETFL: - return lwip_fcntl(socket, cmd, (int) arg); - - default: - return lwip_ioctl(socket, cmd, arg); - } -} - -#ifdef SAL_USING_POSIX -static int inet_poll(struct dfs_fd *file, struct rt_pollreq *req) -{ - int mask = 0; - struct lwip_sock *sock; - struct sal_socket *sal_sock; - - sal_sock = sal_get_socket((int) file->data); - if(!sal_sock) - { - return -1; - } - - sock = lwip_tryget_socket((int)sal_sock->user_data); - if (sock != NULL) - { - rt_base_t level; - - rt_poll_add(&sock->wait_head, req); - - level = rt_hw_interrupt_disable(); - -#if LWIP_VERSION >= 0x20100ff - if ((void*)(sock->lastdata.pbuf) || sock->rcvevent) -#else - if ((void*)(sock->lastdata) || sock->rcvevent) -#endif - { - mask |= POLLIN; - } - if (sock->sendevent) - { - mask |= POLLOUT; - } - if (sock->errevent) - { - mask |= POLLERR; - /* clean error event */ - sock->errevent = 0; - } - rt_hw_interrupt_enable(level); - } - - return mask; -} -#endif - -static const struct sal_socket_ops lwip_socket_ops = -{ - inet_socket, - lwip_close, - lwip_bind, - lwip_listen, - lwip_connect, - inet_accept, - (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, - lwip_shutdown, - lwip_getpeername, - inet_getsockname, - inet_ioctlsocket, -#ifdef SAL_USING_POSIX - inet_poll, -#endif -}; - -static const struct sal_netdb_ops lwip_netdb_ops = -{ - lwip_gethostbyname, - lwip_gethostbyname_r, - lwip_getaddrinfo, - lwip_freeaddrinfo, -}; - -static const struct sal_proto_family lwip_inet_family = -{ - AF_INET, -#if LWIP_VERSION > 0x2000000 - AF_INET6, -#else - AF_INET, -#endif - &lwip_socket_ops, - &lwip_netdb_ops, -}; - -/* Set lwIP network interface device protocol family information */ -int sal_lwip_netdev_set_pf_info(struct netdev *netdev) -{ - RT_ASSERT(netdev); - - netdev->sal_user_data = (void *) &lwip_inet_family; - return 0; -} - -#endif /* SAL_USING_LWIP */ diff --git a/components/net/sal_socket/impl/proto_mbedtls.c b/components/net/sal_socket/impl/proto_mbedtls.c deleted file mode 100644 index 338792c948..0000000000 --- a/components/net/sal_socket/impl/proto_mbedtls.c +++ /dev/null @@ -1,249 +0,0 @@ -/* - * Copyright (c) 2006-2021, 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 <unistd.h> -#include <sys/stat.h> -#include <sys/statfs.h> -#endif - -#ifdef SAL_USING_TLS -#include <sal_tls.h> -#endif -#include <netdb.h> -#include <sal.h> - -#include <netdev.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; - struct sal_proto_family *pf; - - RT_ASSERT(ctx); - RT_ASSERT(buf); - - socket = ((mbedtls_net_context *) ctx)->fd; - sock = sal_get_socket(socket); - if (sock == RT_NULL) - { - return -1; - } - - pf = (struct sal_proto_family *)sock->netdev->sal_user_data; - - /* Register scoket sendto option to TLS send data callback */ - ret = pf->skt_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; - struct sal_proto_family *pf; - 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; - } - - pf = (struct sal_proto_family *)sock->netdev->sal_user_data; - - /* Register scoket recvfrom option to TLS recv data callback */ - ret = pf->skt_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 */ diff --git a/components/net/sal_socket/include/dfs_net/dfs_net.h b/components/net/sal_socket/include/dfs_net/dfs_net.h deleted file mode 100644 index 82b7526603..0000000000 --- a/components/net/sal_socket/include/dfs_net/dfs_net.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2015-02-17 Bernard First version - * 2016-05-05 Bernard rename dfs_lwip to dfs_net. - */ - -#ifndef DFS_NET_H__ -#define DFS_NET_H__ - -#include <dfs_file.h> - -#ifdef __cplusplus -extern "C" { -#endif - -const struct dfs_file_ops* dfs_net_get_fops(void); -int dfs_net_getsocket(int fd); - -#ifdef __cplusplus -} -#endif - -#endif - diff --git a/components/net/sal_socket/include/sal.h b/components/net/sal_socket/include/sal.h deleted file mode 100644 index dd250e37b6..0000000000 --- a/components/net/sal_socket/include/sal.h +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2018-05-17 ChenYong First version - */ - -#ifndef SAL_H__ -#define SAL_H__ - -#include <rtdevice.h> - -#ifdef SAL_USING_POSIX -#include <dfs_file.h> -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -#if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED) -typedef uint32_t socklen_t; -#endif - -/* SAL socket magic word */ -#define SAL_SOCKET_MAGIC 0x5A10 - -/* The maximum number of sockets structure */ -#ifndef SAL_SOCKETS_NUM -#define SAL_SOCKETS_NUM DFS_FD_MAX -#endif - -/* The maximum number of protocol families */ -#ifndef SAL_PROTO_FAMILIES_NUM -#define SAL_PROTO_FAMILIES_NUM 4 -#endif - -/* SAL socket offset */ -#ifndef SAL_SOCKET_OFFSET -#define SAL_SOCKET_OFFSET 0 -#endif - -struct sal_socket -{ - uint32_t magic; /* SAL socket magic word */ - - int socket; /* SAL socket descriptor */ - int domain; - int type; - int protocol; - - struct netdev *netdev; /* SAL network interface device */ - - void *user_data; /* user-specific data */ -#ifdef SAL_USING_TLS - void *user_data_tls; /* user-specific TLS data */ -#endif -}; - -/* network interface socket opreations */ -struct sal_socket_ops -{ - int (*socket) (int domain, int type, int protocol); - int (*closesocket)(int s); - int (*bind) (int s, const struct sockaddr *name, socklen_t namelen); - int (*listen) (int s, int backlog); - int (*connect) (int s, const struct sockaddr *name, socklen_t namelen); - int (*accept) (int s, struct sockaddr *addr, socklen_t *addrlen); - int (*sendto) (int s, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen); - int (*recvfrom) (int s, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen); - int (*getsockopt) (int s, int level, int optname, void *optval, socklen_t *optlen); - int (*setsockopt) (int s, int level, int optname, const void *optval, socklen_t optlen); - int (*shutdown) (int s, int how); - int (*getpeername)(int s, struct sockaddr *name, socklen_t *namelen); - int (*getsockname)(int s, struct sockaddr *name, socklen_t *namelen); - int (*ioctlsocket)(int s, long cmd, void *arg); -#ifdef SAL_USING_POSIX - int (*poll) (struct dfs_fd *file, struct rt_pollreq *req); -#endif -}; - -/* sal network database name resolving */ -struct sal_netdb_ops -{ - struct hostent* (*gethostbyname) (const char *name); - int (*gethostbyname_r)(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop); - int (*getaddrinfo) (const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res); - void (*freeaddrinfo) (struct addrinfo *ai); -}; - -struct sal_proto_family -{ - int family; /* primary protocol families type */ - int sec_family; /* secondary protocol families type */ - const struct sal_socket_ops *skt_ops; /* socket opreations */ - const struct sal_netdb_ops *netdb_ops; /* network database opreations */ -}; - -/* SAL(Socket Abstraction Layer) initialize */ -int sal_init(void); -/* Get SAL socket object by socket descriptor */ -struct sal_socket *sal_get_socket(int sock); - -/* check SAL socket netweork interface device internet status */ -int sal_check_netdev_internet_up(struct netdev *netdev); - -#ifdef __cplusplus -} -#endif - -#endif /* SAL_H__ */ diff --git a/components/net/sal_socket/include/sal_netdb.h b/components/net/sal_socket/include/sal_netdb.h deleted file mode 100644 index 9b1421270f..0000000000 --- a/components/net/sal_socket/include/sal_netdb.h +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2018-05-24 ChenYong First version - */ -#ifndef SAL_NETDB_H__ -#define SAL_NETDB_H__ - -#include <stddef.h> -#include "sal_socket.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define EAI_NONAME 200 -#define EAI_SERVICE 201 -#define EAI_FAIL 202 -#define EAI_MEMORY 203 -#define EAI_FAMILY 204 - -#define HOST_NOT_FOUND 210 -#define NO_DATA 211 -#define NO_RECOVERY 212 -#define TRY_AGAIN 213 - -#define AI_PASSIVE 0x01 -#define AI_CANONNAME 0x02 -#define AI_NUMERICHOST 0x04 -#define AI_NUMERICSERV 0x08 -#define AI_V4MAPPED 0x10 -#define AI_ALL 0x20 -#define AI_ADDRCONFIG 0x40 - -/* input flags for structure addrinfo */ -#define AI_PASSIVE 0x01 -#define AI_CANONNAME 0x02 -#define AI_NUMERICHOST 0x04 -#define AI_NUMERICSERV 0x08 -#define AI_V4MAPPED 0x10 -#define AI_ALL 0x20 -#define AI_ADDRCONFIG 0x40 - -#define DNS_MAX_NAME_LENGTH 256 - -struct hostent { - char *h_name; /* Official name of the host. */ - char **h_aliases; /* A pointer to an array of pointers to alternative host names, - terminated by a null pointer. */ - int h_addrtype; /* Address type. */ - int h_length; /* The length, in bytes, of the address. */ - char **h_addr_list; /* A pointer to an array of pointers to network addresses (in - network byte order) for the host, terminated by a null pointer. */ -#define h_addr h_addr_list[0] /* for backward compatibility */ -}; - -struct addrinfo { - int ai_flags; /* Input flags. */ - int ai_family; /* Address family of socket. */ - int ai_socktype; /* Socket type. */ - int ai_protocol; /* Protocol of socket. */ - socklen_t ai_addrlen; /* Length of socket address. */ - struct sockaddr *ai_addr; /* Socket address of socket. */ - char *ai_canonname; /* Canonical name of service location. */ - struct addrinfo *ai_next; /* Pointer to next in list. */ -}; - -struct hostent *sal_gethostbyname(const char *name); - -int sal_gethostbyname_r(const char *name, struct hostent *ret, char *buf, - size_t buflen, struct hostent **result, int *h_errnop); -void sal_freeaddrinfo(struct addrinfo *ai); -int sal_getaddrinfo(const char *nodename, - const char *servname, - const struct addrinfo *hints, - struct addrinfo **res); - -#ifdef __cplusplus -} -#endif - -#endif /* SAL_NETDB_H__ */ diff --git a/components/net/sal_socket/include/sal_socket.h b/components/net/sal_socket/include/sal_socket.h deleted file mode 100644 index c479d21c62..0000000000 --- a/components/net/sal_socket/include/sal_socket.h +++ /dev/null @@ -1,218 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2018-05-24 ChenYong First version - */ - -#ifndef SAL_SOCKET_H__ -#define SAL_SOCKET_H__ - -#include <stddef.h> -#include <arpa/inet.h> - -#ifdef __cplusplus -extern "C" { -#endif - -#if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED) -typedef uint32_t socklen_t; -#endif - -#if !defined(sa_family_t) && !defined(SA_FAMILY_T_DEFINED) -typedef uint8_t sa_family_t; -#endif - -/* If your port already typedef's in_port_t, define IN_PORT_T_DEFINED - to prevent this code from redefining it. */ -#if !defined(in_port_t) && !defined(IN_PORT_T_DEFINED) -typedef uint16_t in_port_t; -#endif - -/* Socket protocol types (TCP/UDP/RAW) */ -#define SOCK_STREAM 1 -#define SOCK_DGRAM 2 -#define SOCK_RAW 3 - -#define SOCK_MAX (SOCK_RAW + 1) - -/* Option flags per-socket. These must match the SOF_ flags in ip.h (checked in init.c) */ -#define SO_REUSEADDR 0x0004 /* Allow local address reuse */ -#define SO_KEEPALIVE 0x0008 /* keep connections alive */ -#define SO_BROADCAST 0x0020 /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */ - -/* Additional options, not kept in so_options */ -#define SO_DEBUG 0x0001 /* Unimplemented: turn on debugging info recording */ -#define SO_ACCEPTCONN 0x0002 /* socket has had listen() */ -#define SO_DONTROUTE 0x0010 /* Unimplemented: just use interface addresses */ -#define SO_USELOOPBACK 0x0040 /* Unimplemented: bypass hardware when possible */ -#define SO_LINGER 0x0080 /* linger on close if data present */ -#define SO_DONTLINGER ((int)(~SO_LINGER)) -#define SO_OOBINLINE 0x0100 /* Unimplemented: leave received OOB data in line */ -#define SO_REUSEPORT 0x0200 /* Unimplemented: allow local address & port reuse */ -#define SO_SNDBUF 0x1001 /* Unimplemented: send buffer size */ -#define SO_RCVBUF 0x1002 /* receive buffer size */ -#define SO_SNDLOWAT 0x1003 /* Unimplemented: send low-water mark */ -#define SO_RCVLOWAT 0x1004 /* Unimplemented: receive low-water mark */ -#define SO_SNDTIMEO 0x1005 /* send timeout */ -#define SO_RCVTIMEO 0x1006 /* receive timeout */ -#define SO_ERROR 0x1007 /* get error status and clear */ -#define SO_TYPE 0x1008 /* get socket type */ -#define SO_CONTIMEO 0x1009 /* Unimplemented: connect timeout */ -#define SO_NO_CHECK 0x100a /* don't create UDP checksum */ - -/* Level number for (get/set)sockopt() to apply to socket itself */ -#define SOL_SOCKET 0xfff /* options for socket level */ - -#define AF_UNSPEC 0 -#define AF_INET 2 -#define AF_INET6 10 -#define AF_CAN 29 /* Controller Area Network */ -#define AF_AT 45 /* AT socket */ -#define AF_WIZ 46 /* WIZnet socket */ -#define PF_INET AF_INET -#define PF_INET6 AF_INET6 -#define PF_UNSPEC AF_UNSPEC -#define PF_CAN AF_CAN -#define PF_AT AF_AT -#define PF_WIZ AF_WIZ - -#define AF_MAX (AF_WIZ + 1) /* For now.. */ - -#define IPPROTO_IP 0 -#define IPPROTO_ICMP 1 -#define IPPROTO_TCP 6 -#define IPPROTO_UDP 17 -#define IPPROTO_IPV6 41 -#define IPPROTO_ICMPV6 58 -#define IPPROTO_UDPLITE 136 -#define IPPROTO_RAW 255 - -/* Flags we can use with send and recv */ -#define MSG_PEEK 0x01 /* Peeks at an incoming message */ -#define MSG_WAITALL 0x02 /* Unimplemented: Requests that the function block until the full amount of data requested can be returned */ -#define MSG_OOB 0x04 /* Unimplemented: Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific */ -#define MSG_DONTWAIT 0x08 /* Nonblocking i/o for this operation only */ -#define MSG_MORE 0x10 /* Sender will send more */ - -/* Options for level IPPROTO_IP */ -#define IP_TOS 1 -#define IP_TTL 2 - -/* Options for level IPPROTO_TCP */ -#define TCP_NODELAY 0x01 /* don't delay send to coalesce packets */ -#define TCP_KEEPALIVE 0x02 /* send KEEPALIVE probes when idle for pcb->keep_idle milliseconds */ -#define TCP_KEEPIDLE 0x03 /* set pcb->keep_idle - Same as TCP_KEEPALIVE, but use seconds for get/setsockopt */ -#define TCP_KEEPINTVL 0x04 /* set pcb->keep_intvl - Use seconds for get/setsockopt */ -#define TCP_KEEPCNT 0x05 /* set pcb->keep_cnt - Use number of probes sent for get/setsockopt */ - -/* Options and types related to multicast membership */ -#define IP_ADD_MEMBERSHIP 3 -#define IP_DROP_MEMBERSHIP 4 -/* Options and types for UDP multicast traffic handling */ -#define IP_MULTICAST_TTL 5 -#define IP_MULTICAST_IF 6 -#define IP_MULTICAST_LOOP 7 - -typedef struct ip_mreq -{ - struct in_addr imr_multiaddr; /* IP multicast address of group */ - struct in_addr imr_interface; /* local IP address of interface */ -} ip_mreq; - -/* The Type of Service provides an indication of the abstract parameters of the quality of service desired */ -#define IPTOS_TOS_MASK 0x1E -#define IPTOS_TOS(tos) ((tos) & IPTOS_TOS_MASK) -#define IPTOS_LOWDELAY 0x10 -#define IPTOS_THROUGHPUT 0x08 -#define IPTOS_RELIABILITY 0x04 -#define IPTOS_LOWCOST 0x02 -#define IPTOS_MINCOST IPTOS_LOWCOST - -/* The Network Control precedence designation is intended to be used within a network only */ -#define IPTOS_PREC_MASK 0xe0 -#define IPTOS_PREC(tos) ((tos) & IPTOS_PREC_MASK) -#define IPTOS_PREC_NETCONTROL 0xe0 -#define IPTOS_PREC_INTERNETCONTROL 0xc0 -#define IPTOS_PREC_CRITIC_ECP 0xa0 -#define IPTOS_PREC_FLASHOVERRIDE 0x80 -#define IPTOS_PREC_FLASH 0x60 -#define IPTOS_PREC_IMMEDIATE 0x40 -#define IPTOS_PREC_PRIORITY 0x20 -#define IPTOS_PREC_ROUTINE 0x00 - -/* Options for shatdown type */ -#ifndef SHUT_RD - #define SHUT_RD 0 - #define SHUT_WR 1 - #define SHUT_RDWR 2 -#endif - -struct sockaddr -{ - uint8_t sa_len; - sa_family_t sa_family; - char sa_data[14]; -}; - -#if NETDEV_IPV4 -/* members are in network byte order */ -struct sockaddr_in -{ - uint8_t sin_len; - sa_family_t sin_family; - in_port_t sin_port; - struct in_addr sin_addr; -#define SIN_ZERO_LEN 8 - char sin_zero[SIN_ZERO_LEN]; -}; -#endif /* NETDEV_IPV4 */ - -#if NETDEV_IPV6 -struct sockaddr_in6 -{ - uint8_t sin6_len; /* length of this structure */ - sa_family_t sin6_family; /* AF_INET6 */ - in_port_t sin6_port; /* Transport layer port # */ - uint32_t sin6_flowinfo; /* IPv6 flow information */ - struct in6_addr sin6_addr; /* IPv6 address */ - uint32_t sin6_scope_id; /* Set of interfaces for scope */ -}; -#endif /* NETDEV_IPV6 */ - -struct sockaddr_storage -{ - uint8_t s2_len; - sa_family_t ss_family; - char s2_data1[2]; - uint32_t s2_data2[3]; -#if NETDEV_IPV6 - uint32_t s2_data3[3]; -#endif /* NETDEV_IPV6 */ -}; - -int sal_accept(int socket, struct sockaddr *addr, socklen_t *addrlen); -int sal_bind(int socket, const struct sockaddr *name, socklen_t namelen); -int sal_shutdown(int socket, int how); -int sal_getpeername (int socket, struct sockaddr *name, socklen_t *namelen); -int sal_getsockname (int socket, struct sockaddr *name, socklen_t *namelen); -int sal_getsockopt (int socket, int level, int optname, void *optval, socklen_t *optlen); -int sal_setsockopt (int socket, int level, int optname, const void *optval, socklen_t optlen); -int sal_connect(int socket, const struct sockaddr *name, socklen_t namelen); -int sal_listen(int socket, int backlog); -int sal_recvfrom(int socket, void *mem, size_t len, int flags, - struct sockaddr *from, socklen_t *fromlen); -int sal_sendto(int socket, const void *dataptr, size_t size, int flags, - const struct sockaddr *to, socklen_t tolen); -int sal_socket(int domain, int type, int protocol); -int sal_closesocket(int socket); -int sal_ioctlsocket(int socket, long cmd, void *arg); - -#ifdef __cplusplus -} -#endif - -#endif /* SAL_SOCKET_H__ */ diff --git a/components/net/sal_socket/include/sal_tls.h b/components/net/sal_socket/include/sal_tls.h deleted file mode 100644 index f211a3c1f8..0000000000 --- a/components/net/sal_socket/include/sal_tls.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2018-11-10 ChenYong First version - */ -#ifndef __SAL_TLS_H__ -#define __SAL_TLS_H__ - -#ifdef __cplusplus -extern "C" { -#endif - -#include <rtthread.h> - -/* Protocol level for TLS. - * Here, the same socket protocol level for TLS as in Linux was used. - */ -#define SOL_TLS 282 - -/* Socket options for TLS */ - -/* Socket option to select TLS credentials to use. */ -#define TLS_CRET_LIST 1 -/* Socket option to set select ciphersuites to use. */ -#define TLS_CIPHERSUITE_LIST 2 -/* Socket option to set peer verification level for TLS connection. */ -#define TLS_PEER_VERIFY 3 -/* Socket option to set role for DTLS connection. */ -#define TLS_DTLS_ROLE 4 - -/* Protocol numbers for TLS protocols */ -#define PROTOCOL_TLS 256 -#define PROTOCOL_DTLS 257 - - -struct sal_proto_tls_ops -{ - int (*init)(void); - void* (*socket)(int socket); - int (*connect)(void *sock); - int (*send)(void *sock, const void *data, size_t size); - int (*recv)(void *sock, void *mem, size_t len); - int (*closesocket)(void *sock); - - int (*set_cret_list)(void *sock, const void *cert, size_t size); /* Set TLS credentials */ - int (*set_ciphersurite)(void *sock, const void* ciphersurite, size_t size); /* Set select ciphersuites */ - int (*set_peer_verify)(void *sock, const void* peer_verify, size_t size); /* Set peer verification */ - int (*set_dtls_role)(void *sock, const void *dtls_role, size_t size); /* Set role for DTLS */ -}; - -struct sal_proto_tls -{ - char name[RT_NAME_MAX]; /* TLS protocol name */ - const struct sal_proto_tls_ops *ops; /* SAL TLS protocol options */ -}; - -/* SAL TLS protocol register */ -int sal_proto_tls_register(const struct sal_proto_tls *pt); - -#ifdef __cplusplus -} -#endif - -#endif /* __SAL_TLS_H__ */ diff --git a/components/net/sal_socket/include/socket/netdb.h b/components/net/sal_socket/include/socket/netdb.h deleted file mode 100644 index 8394fddea5..0000000000 --- a/components/net/sal_socket/include/socket/netdb.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2015-02-17 Bernard First version - * 2018-05-24 ChenYong Add socket abstraction layer - */ - -#ifndef NETDB_H__ -#define NETDB_H__ - -#ifdef __cplusplus -extern "C" { -#endif - -#include <stddef.h> -#include <sal_netdb.h> - -struct hostent *gethostbyname(const char *name); - -int gethostbyname_r(const char *name, struct hostent *ret, char *buf, - size_t buflen, struct hostent **result, int *h_errnop); -void freeaddrinfo(struct addrinfo *ai); -int getaddrinfo(const char *nodename, - const char *servname, - const struct addrinfo *hints, - struct addrinfo **res); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/components/net/sal_socket/include/socket/netinet/in.h b/components/net/sal_socket/include/socket/netinet/in.h deleted file mode 100644 index 37240547b4..0000000000 --- a/components/net/sal_socket/include/socket/netinet/in.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2015-02-17 Bernard First version - */ - -#ifndef IN_H__ -#define IN_H__ - -#include <sys/socket.h> - -#ifndef IN6_IS_ADDR_MULTICAST -#define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff) -#endif - -#endif diff --git a/components/net/sal_socket/include/socket/netinet/tcp.h b/components/net/sal_socket/include/socket/netinet/tcp.h deleted file mode 100644 index a3b530186a..0000000000 --- a/components/net/sal_socket/include/socket/netinet/tcp.h +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2015-02-17 Bernard First version - */ - -#ifndef TCP_H__ -#define TCP_H__ - -#include <sys/socket.h> - -#endif diff --git a/components/net/sal_socket/include/socket/netinet/udp.h b/components/net/sal_socket/include/socket/netinet/udp.h deleted file mode 100644 index 21f50d55e5..0000000000 --- a/components/net/sal_socket/include/socket/netinet/udp.h +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2015-02-17 Bernard First version - */ - -#ifndef UDP_H__ -#define UDP_H__ - -#include <sys/socket.h> - -#endif diff --git a/components/net/sal_socket/include/socket/sys_socket/sys/socket.h b/components/net/sal_socket/include/socket/sys_socket/sys/socket.h deleted file mode 100644 index a86240e2ff..0000000000 --- a/components/net/sal_socket/include/socket/sys_socket/sys/socket.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2015-02-17 Bernard First version - * 2018-05-17 ChenYong Add socket abstraction layer - */ - -#ifndef SYS_SOCKET_H_ -#define SYS_SOCKET_H_ - -#include <rtthread.h> -#include <sal_socket.h> -#ifdef SAL_USING_TLS -#include <sal_tls.h> -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef SAL_USING_POSIX -int accept(int s, struct sockaddr *addr, socklen_t *addrlen); -int bind(int s, const struct sockaddr *name, socklen_t namelen); -int shutdown(int s, int how); -int getpeername(int s, struct sockaddr *name, socklen_t *namelen); -int getsockname(int s, struct sockaddr *name, socklen_t *namelen); -int getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen); -int setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen); -int connect(int s, const struct sockaddr *name, socklen_t namelen); -int listen(int s, int backlog); -int recv(int s, void *mem, size_t len, int flags); -int recvfrom(int s, void *mem, size_t len, int flags, - struct sockaddr *from, socklen_t *fromlen); -int send(int s, const void *dataptr, size_t size, int flags); -int sendto(int s, const void *dataptr, size_t size, int flags, - const struct sockaddr *to, socklen_t tolen); -int socket(int domain, int type, int protocol); -int closesocket(int s); -int ioctlsocket(int s, long cmd, void *arg); -#else -#define accept(s, addr, addrlen) sal_accept(s, addr, addrlen) -#define bind(s, name, namelen) sal_bind(s, name, namelen) -#define shutdown(s, how) sal_shutdown(s, how) -#define getpeername(s, name, namelen) sal_getpeername(s, name, namelen) -#define getsockname(s, name, namelen) sal_getsockname(s, name, namelen) -#define getsockopt(s, level, optname, optval, optlen) sal_getsockopt(s, level, optname, optval, optlen) -#define setsockopt(s, level, optname, optval, optlen) sal_setsockopt(s, level, optname, optval, optlen) -#define connect(s, name, namelen) sal_connect(s, name, namelen) -#define listen(s, backlog) sal_listen(s, backlog) -#define recv(s, mem, len, flags) sal_recvfrom(s, mem, len, flags, NULL, NULL) -#define recvfrom(s, mem, len, flags, from, fromlen) sal_recvfrom(s, mem, len, flags, from, fromlen) -#define send(s, dataptr, size, flags) sal_sendto(s, dataptr, size, flags, NULL, NULL) -#define sendto(s, dataptr, size, flags, to, tolen) sal_sendto(s, dataptr, size, flags, to, tolen) -#define socket(domain, type, protocol) sal_socket(domain, type, protocol) -#define closesocket(s) sal_closesocket(s) -#define ioctlsocket(s, cmd, arg) sal_ioctlsocket(s, cmd, arg) -#endif /* SAL_USING_POSIX */ - -#ifdef __cplusplus -} -#endif - -#endif /* _SYS_SOCKET_H_ */ diff --git a/components/net/sal_socket/socket/net_netdb.c b/components/net/sal_socket/socket/net_netdb.c deleted file mode 100644 index 1eb84e1c32..0000000000 --- a/components/net/sal_socket/socket/net_netdb.c +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2015-02-17 Bernard First version - * 2108-05-24 ChenYong Add socket abstraction layer - */ - -#include <stddef.h> -#include <rtthread.h> - -#include <netdb.h> - -struct hostent *gethostbyname(const char *name) -{ - return sal_gethostbyname(name); -} -RTM_EXPORT(gethostbyname); - -int gethostbyname_r(const char *name, struct hostent *ret, char *buf, - size_t buflen, struct hostent **result, int *h_errnop) -{ - return sal_gethostbyname_r(name, ret, buf, buflen, result, h_errnop); -} -RTM_EXPORT(gethostbyname_r); - -void freeaddrinfo(struct addrinfo *ai) -{ - sal_freeaddrinfo(ai); -} -RTM_EXPORT(freeaddrinfo); - -int getaddrinfo(const char *nodename, - const char *servname, - const struct addrinfo *hints, - struct addrinfo **res) -{ - return sal_getaddrinfo(nodename, servname, hints, res); -} -RTM_EXPORT(getaddrinfo); diff --git a/components/net/sal_socket/socket/net_sockets.c b/components/net/sal_socket/socket/net_sockets.c deleted file mode 100644 index 1b6db50e6d..0000000000 --- a/components/net/sal_socket/socket/net_sockets.c +++ /dev/null @@ -1,293 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2015-02-17 Bernard First version - * 2018-05-17 ChenYong Add socket abstraction layer - */ - -#include <dfs.h> -#include <dfs_file.h> -#include <poll.h> -#include <dfs_net.h> -#include <sys/errno.h> -#include <sys/socket.h> - -int accept(int s, struct sockaddr *addr, socklen_t *addrlen) -{ - int new_socket = -1; - int socket = dfs_net_getsocket(s); - - new_socket = sal_accept(socket, addr, addrlen); - if (new_socket != -1) - { - /* this is a new socket, create it in file system fd */ - int fd; - struct dfs_fd *d; - - /* allocate a fd */ - fd = fd_new(); - if (fd < 0) - { - rt_set_errno(-ENOMEM); - sal_closesocket(new_socket); - return -1; - } - - d = fd_get(fd); - if(d) - { - /* this is a socket fd */ - d->type = FT_SOCKET; - d->path = NULL; - - d->fops = dfs_net_get_fops(); - - d->flags = O_RDWR; /* set flags as read and write */ - d->size = 0; - d->pos = 0; - - /* set socket to the data of dfs_fd */ - d->data = (void *) new_socket; - - /* release the ref-count of fd */ - fd_put(d); - - return fd; - } - - rt_set_errno(-ENOMEM); - sal_closesocket(new_socket); - return -1; - } - - return -1; -} -RTM_EXPORT(accept); - -int bind(int s, const struct sockaddr *name, socklen_t namelen) -{ - int socket = dfs_net_getsocket(s); - - return sal_bind(socket, name, namelen); -} -RTM_EXPORT(bind); - -int shutdown(int s, int how) -{ - int error = 0; - int socket = -1; - struct dfs_fd *d; - - socket = dfs_net_getsocket(s); - if (socket < 0) - { - rt_set_errno(-ENOTSOCK); - return -1; - } - - d = fd_get(s); - if (d == NULL) - { - rt_set_errno(-EBADF); - return -1; - } - - if (sal_shutdown(socket, how) == 0) - { - error = 0; - } - else - { - rt_set_errno(-ENOTSOCK); - error = -1; - } - - fd_put(d); - - return error; -} -RTM_EXPORT(shutdown); - -int getpeername(int s, struct sockaddr *name, socklen_t *namelen) -{ - int socket = dfs_net_getsocket(s); - - return sal_getpeername(socket, name, namelen); -} -RTM_EXPORT(getpeername); - -int getsockname(int s, struct sockaddr *name, socklen_t *namelen) -{ - int socket = dfs_net_getsocket(s); - - return sal_getsockname(socket, name, namelen); -} -RTM_EXPORT(getsockname); - -int getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen) -{ - int socket = dfs_net_getsocket(s); - - return sal_getsockopt(socket, level, optname, optval, optlen); -} -RTM_EXPORT(getsockopt); - -int setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen) -{ - int socket = dfs_net_getsocket(s); - - return sal_setsockopt(socket, level, optname, optval, optlen); -} -RTM_EXPORT(setsockopt); - -int connect(int s, const struct sockaddr *name, socklen_t namelen) -{ - int socket = dfs_net_getsocket(s); - - return sal_connect(socket, name, namelen); -} -RTM_EXPORT(connect); - -int listen(int s, int backlog) -{ - int socket = dfs_net_getsocket(s); - - return sal_listen(socket, backlog); -} -RTM_EXPORT(listen); - -int recv(int s, void *mem, size_t len, int flags) -{ - int socket = dfs_net_getsocket(s); - - return sal_recvfrom(socket, mem, len, flags, NULL, NULL); -} -RTM_EXPORT(recv); - -int recvfrom(int s, void *mem, size_t len, int flags, - struct sockaddr *from, socklen_t *fromlen) -{ - int socket = dfs_net_getsocket(s); - - return sal_recvfrom(socket, mem, len, flags, from, fromlen); -} -RTM_EXPORT(recvfrom); - -int send(int s, const void *dataptr, size_t size, int flags) -{ - int socket = dfs_net_getsocket(s); - - return sal_sendto(socket, dataptr, size, flags, NULL, 0); -} -RTM_EXPORT(send); - -int sendto(int s, const void *dataptr, size_t size, int flags, - const struct sockaddr *to, socklen_t tolen) -{ - int socket = dfs_net_getsocket(s); - - return sal_sendto(socket, dataptr, size, flags, to, tolen); -} -RTM_EXPORT(sendto); - -int socket(int domain, int type, int protocol) -{ - /* create a BSD socket */ - int fd; - int socket; - struct dfs_fd *d; - - /* allocate a fd */ - fd = fd_new(); - if (fd < 0) - { - rt_set_errno(-ENOMEM); - - return -1; - } - d = fd_get(fd); - - /* create socket and then put it to the dfs_fd */ - socket = sal_socket(domain, type, protocol); - if (socket >= 0) - { - /* this is a socket fd */ - d->type = FT_SOCKET; - d->path = NULL; - - d->fops = dfs_net_get_fops(); - - d->flags = O_RDWR; /* set flags as read and write */ - d->size = 0; - d->pos = 0; - - /* set socket to the data of dfs_fd */ - d->data = (void *) socket; - } - else - { - /* release fd */ - fd_put(d); - fd_put(d); - - rt_set_errno(-ENOMEM); - - return -1; - } - - /* release the ref-count of fd */ - fd_put(d); - - return fd; -} -RTM_EXPORT(socket); - -int closesocket(int s) -{ - int error = 0; - int socket = -1; - struct dfs_fd *d; - - socket = dfs_net_getsocket(s); - if (socket < 0) - { - rt_set_errno(-ENOTSOCK); - return -1; - } - - d = fd_get(s); - if (d == RT_NULL) - { - rt_set_errno(-EBADF); - return -1; - } - - if (sal_closesocket(socket) == 0) - { - error = 0; - } - else - { - rt_set_errno(-ENOTSOCK); - error = -1; - } - - /* socket has been closed, delete it from file system fd */ - fd_put(d); - fd_put(d); - - return error; -} -RTM_EXPORT(closesocket); - -int ioctlsocket(int s, long cmd, void *arg) -{ - int socket = dfs_net_getsocket(s); - - return sal_ioctlsocket(socket, cmd, arg); -} -RTM_EXPORT(ioctlsocket); diff --git a/components/net/sal_socket/src/sal_socket.c b/components/net/sal_socket/src/sal_socket.c deleted file mode 100644 index 8f4ac915eb..0000000000 --- a/components/net/sal_socket/src/sal_socket.c +++ /dev/null @@ -1,1177 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2018-05-23 ChenYong First version - * 2018-11-12 ChenYong Add TLS support - */ - -#include <rtthread.h> -#include <rthw.h> -#include <sys/time.h> - -#include <sal_socket.h> -#include <sal_netdb.h> -#ifdef SAL_USING_TLS -#include <sal_tls.h> -#endif -#include <sal.h> -#include <netdev.h> - -#ifdef SAL_INTERNET_CHECK -#include <ipc/workqueue.h> -#endif - -/* check system workqueue stack size */ -#if RT_SYSTEM_WORKQUEUE_STACKSIZE < 1536 -#error "The system workqueue stack size must more than 1536 bytes" -#endif - -#define DBG_TAG "sal.skt" -#define DBG_LVL DBG_INFO -#include <rtdbg.h> - -#define SOCKET_TABLE_STEP_LEN 4 - -/* the socket table used to dynamic allocate sockets */ -struct sal_socket_table -{ - uint32_t max_socket; - struct sal_socket **sockets; -}; - -/* record the netdev and res table*/ -struct sal_netdev_res_table -{ - struct addrinfo *res; - struct netdev *netdev; -}; - -#ifdef SAL_USING_TLS -/* The global TLS protocol options */ -static struct sal_proto_tls *proto_tls; -#endif - -/* The global socket table */ -static struct sal_socket_table socket_table; -static struct rt_mutex sal_core_lock; -static rt_bool_t init_ok = RT_FALSE; -static struct sal_netdev_res_table sal_dev_res_tbl[SAL_SOCKETS_NUM]; - -#define IS_SOCKET_PROTO_TLS(sock) (((sock)->protocol == PROTOCOL_TLS) || \ - ((sock)->protocol == PROTOCOL_DTLS)) -#define SAL_SOCKOPS_PROTO_TLS_VALID(sock, name) (proto_tls && (proto_tls->ops->name) && IS_SOCKET_PROTO_TLS(sock)) - -#define SAL_SOCKOPT_PROTO_TLS_EXEC(sock, name, optval, optlen) \ -do { \ - if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, name)){ \ - return proto_tls->ops->name((sock)->user_data_tls, (optval), (optlen)); \ - } \ -}while(0) \ - -#define SAL_SOCKET_OBJ_GET(sock, socket) \ -do { \ - (sock) = sal_get_socket(socket); \ - if ((sock) == RT_NULL) { \ - return -1; \ - } \ -}while(0) \ - -#define SAL_NETDEV_IS_UP(netdev) \ -do { \ - if (!netdev_is_up(netdev)) { \ - return -1; \ - } \ -}while(0) \ - -#define SAL_NETDEV_SOCKETOPS_VALID(netdev, pf, ops) \ -do { \ - (pf) = (struct sal_proto_family *) netdev->sal_user_data; \ - if ((pf)->skt_ops->ops == RT_NULL){ \ - return -1; \ - } \ -}while(0) \ - -#define SAL_NETDEV_NETDBOPS_VALID(netdev, pf, ops) \ - ((netdev) && netdev_is_up(netdev) && \ - ((pf) = (struct sal_proto_family *) (netdev)->sal_user_data) != RT_NULL && \ - (pf)->netdb_ops->ops) \ - -#define SAL_NETDBOPS_VALID(netdev, pf, ops) \ - ((netdev) && \ - ((pf) = (struct sal_proto_family *) (netdev)->sal_user_data) != RT_NULL && \ - (pf)->netdb_ops->ops) \ - -/** - * SAL (Socket Abstraction Layer) initialize. - * - * @return result 0: initialize success - * -1: initialize failed - */ -int sal_init(void) -{ - int cn; - - if (init_ok) - { - LOG_D("Socket Abstraction Layer is already initialized."); - return 0; - } - - /* init sal socket table */ - cn = SOCKET_TABLE_STEP_LEN < SAL_SOCKETS_NUM ? SOCKET_TABLE_STEP_LEN : SAL_SOCKETS_NUM; - socket_table.max_socket = cn; - socket_table.sockets = rt_calloc(1, cn * sizeof(struct sal_socket *)); - if (socket_table.sockets == RT_NULL) - { - LOG_E("No memory for socket table.\n"); - return -1; - } - - /*init the dev_res table */ - rt_memset(sal_dev_res_tbl, 0, sizeof(sal_dev_res_tbl)); - - /* create sal socket lock */ - rt_mutex_init(&sal_core_lock, "sal_lock", RT_IPC_FLAG_PRIO); - - LOG_I("Socket Abstraction Layer initialize success."); - init_ok = RT_TRUE; - - return 0; -} -INIT_COMPONENT_EXPORT(sal_init); - -#ifdef SAL_INTERNET_CHECK -/* check SAL network interface device internet status */ -static void check_netdev_internet_up_work(struct rt_work *work, void *work_data) -{ -#define SAL_INTERNET_VERSION 0x00 -#define SAL_INTERNET_BUFF_LEN 12 -#define SAL_INTERNET_TIMEOUT (2) - -#define SAL_INTERNET_HOST "link.rt-thread.org" -#define SAL_INTERNET_PORT 8101 - -#define SAL_INTERNET_MONTH_LEN 4 -#define SAL_INTERNET_DATE_LEN 16 - - int index, sockfd = -1, result = 0; - struct sockaddr_in server_addr; - struct hostent *host; - struct timeval timeout; - struct netdev *netdev = (struct netdev *)work_data; - socklen_t addr_len = sizeof(struct sockaddr_in); - char send_data[SAL_INTERNET_BUFF_LEN], recv_data = 0; - - const char month[][SAL_INTERNET_MONTH_LEN] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; - char date[SAL_INTERNET_DATE_LEN]; - int moth_num = 0; - - struct sal_proto_family *pf = (struct sal_proto_family *) netdev->sal_user_data; - const struct sal_socket_ops *skt_ops; - - if (work) - { - rt_free(work); - } - - /* get network interface socket operations */ - if (pf == RT_NULL || pf->skt_ops == RT_NULL) - { - result = -RT_ERROR; - goto __exit; - } - - host = (struct hostent *) pf->netdb_ops->gethostbyname(SAL_INTERNET_HOST); - if (host == RT_NULL) - { - result = -RT_ERROR; - goto __exit; - } - - skt_ops = pf->skt_ops; - if ((sockfd = skt_ops->socket(AF_INET, SOCK_DGRAM, 0)) < 0) - { - result = -RT_ERROR; - goto __exit; - } - - server_addr.sin_family = AF_INET; - server_addr.sin_port = htons(SAL_INTERNET_PORT); - server_addr.sin_addr = *((struct in_addr *)host->h_addr); - rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero)); - - timeout.tv_sec = SAL_INTERNET_TIMEOUT; - timeout.tv_usec = 0; - - /* set receive and send timeout */ - skt_ops->setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (void *) &timeout, sizeof(timeout)); - skt_ops->setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (void *) &timeout, sizeof(timeout)); - - /* get build moth value*/ - rt_memset(date, 0x00, SAL_INTERNET_DATE_LEN); - rt_snprintf(date, SAL_INTERNET_DATE_LEN, "%s", __DATE__); - - for (index = 0; index < sizeof(month) / SAL_INTERNET_MONTH_LEN; index++) - { - if (rt_memcmp(date, month[index], SAL_INTERNET_MONTH_LEN - 1) == 0) - { - moth_num = index + 1; - break; - } - } - - /* not find build month */ - if (moth_num == 0 || moth_num > sizeof(month) / SAL_INTERNET_MONTH_LEN) - { - result = -RT_ERROR; - goto __exit; - } - - rt_memset(send_data, 0x00, SAL_INTERNET_BUFF_LEN); - send_data[0] = SAL_INTERNET_VERSION; - for (index = 0; index < netdev->hwaddr_len; index++) - { - send_data[index + 1] = netdev->hwaddr[index] + moth_num; - } - send_data[9] = RT_VERSION; - send_data[10] = RT_SUBVERSION; - send_data[11] = RT_REVISION; - - skt_ops->sendto(sockfd, send_data, SAL_INTERNET_BUFF_LEN, 0, - (struct sockaddr *)&server_addr, sizeof(struct sockaddr)); - - result = skt_ops->recvfrom(sockfd, &recv_data, sizeof(recv_data), 0, (struct sockaddr *)&server_addr, &addr_len); - if (result < 0) - { - goto __exit; - } - - if (recv_data == RT_FALSE) - { - result = -RT_ERROR; - goto __exit; - } - -__exit: - if (result > 0) - { - LOG_D("Set network interface device(%s) internet status up.", netdev->name); - netdev_low_level_set_internet_status(netdev, RT_TRUE); - } - else - { - LOG_D("Set network interface device(%s) internet status down.", netdev->name); - netdev_low_level_set_internet_status(netdev, RT_FALSE); - } - - if (sockfd >= 0) - { - skt_ops->closesocket(sockfd); - } -} -#endif /* SAL_INTERNET_CHECK */ - -/** - * This function will check SAL network interface device internet status. - * - * @param netdev the network interface device to check - */ -int sal_check_netdev_internet_up(struct netdev *netdev) -{ - RT_ASSERT(netdev); - -#ifdef SAL_INTERNET_CHECK - /* workqueue for network connect */ - struct rt_work *net_work = RT_NULL; - - - net_work = (struct rt_work *)rt_calloc(1, sizeof(struct rt_work)); - if (net_work == RT_NULL) - { - LOG_W("No memory for network interface device(%s) delay work.", netdev->name); - return -1; - } - - rt_work_init(net_work, check_netdev_internet_up_work, (void *)netdev); - rt_work_submit(net_work, RT_TICK_PER_SECOND); -#endif /* SAL_INTERNET_CHECK */ - return 0; -} - -/** - * This function will register TLS protocol to the global TLS protocol. - * - * @param pt TLS protocol object - * - * @return 0: TLS protocol object register success - */ -#ifdef SAL_USING_TLS -int sal_proto_tls_register(const struct sal_proto_tls *pt) -{ - RT_ASSERT(pt); - proto_tls = (struct sal_proto_tls *) pt; - - return 0; -} -#endif - -/** - * This function will get sal socket object by sal socket descriptor. - * - * @param socket sal socket index - * - * @return sal socket object of the current sal socket index - */ -struct sal_socket *sal_get_socket(int socket) -{ - struct sal_socket_table *st = &socket_table; - - socket = socket - SAL_SOCKET_OFFSET; - - if (socket < 0 || socket >= (int) st->max_socket) - { - return RT_NULL; - } - - /* check socket structure valid or not */ - RT_ASSERT(st->sockets[socket]->magic == SAL_SOCKET_MAGIC); - - return st->sockets[socket]; -} - -/** - * This function will lock sal socket. - * - * @note please don't invoke it on ISR. - */ -static void sal_lock(void) -{ - rt_err_t result; - - result = rt_mutex_take(&sal_core_lock, RT_WAITING_FOREVER); - if (result != RT_EOK) - { - RT_ASSERT(0); - } -} - -/** - * This function will lock sal socket. - * - * @note please don't invoke it on ISR. - */ -static void sal_unlock(void) -{ - rt_mutex_release(&sal_core_lock); -} - -/** - * This function will clean the netdev. - * - * @note please don't invoke it on ISR. - */ -int sal_netdev_cleanup(struct netdev *netdev) -{ - int idx = 0, find_dev; - - do - { - find_dev = 0; - sal_lock(); - for (idx = 0; idx < socket_table.max_socket; idx++) - { - if (socket_table.sockets[idx] && socket_table.sockets[idx]->netdev == netdev) - { - find_dev = 1; - break; - } - } - sal_unlock(); - if (find_dev) - { - rt_thread_mdelay(100); - } - } - while (find_dev); - - return 0; -} - -/** - * This function will initialize sal socket object and set socket options - * - * @param family protocol family - * @param type socket type - * @param protocol transfer Protocol - * @param res sal socket object address - * - * @return 0 : socket initialize success - * -1 : input the wrong family - * -2 : input the wrong socket type - * -3 : get network interface failed - */ -static int socket_init(int family, int type, int protocol, struct sal_socket **res) -{ - - struct sal_socket *sock; - struct sal_proto_family *pf; - struct netdev *netdv_def = netdev_default; - struct netdev *netdev = RT_NULL; - rt_bool_t flag = RT_FALSE; - - if (family < 0 || family > AF_MAX) - { - return -1; - } - - if (type < 0 || type > SOCK_MAX) - { - return -2; - } - - sock = *res; - sock->domain = family; - sock->type = type; - sock->protocol = protocol; - - if (netdv_def && netdev_is_up(netdv_def)) - { - /* check default network interface device protocol family */ - pf = (struct sal_proto_family *) netdv_def->sal_user_data; - if (pf != RT_NULL && pf->skt_ops && (pf->family == family || pf->sec_family == family)) - { - sock->netdev = netdv_def; - flag = RT_TRUE; - } - } - - if (flag == RT_FALSE) - { - /* get network interface device by protocol family */ - netdev = netdev_get_by_family(family); - if (netdev == RT_NULL) - { - LOG_E("not find network interface device by protocol family(%d).", family); - return -3; - } - - sock->netdev = netdev; - } - - return 0; -} - -static int socket_alloc(struct sal_socket_table *st, int f_socket) -{ - int idx; - - /* find an empty socket entry */ - for (idx = f_socket; idx < (int) st->max_socket; idx++) - { - if (st->sockets[idx] == RT_NULL) - { - break; - } - } - - /* allocate a larger sockte container */ - if (idx == (int) st->max_socket && st->max_socket < SAL_SOCKETS_NUM) - { - int cnt, index; - struct sal_socket **sockets; - - /* increase the number of socket with 4 step length */ - cnt = st->max_socket + SOCKET_TABLE_STEP_LEN; - cnt = cnt > SAL_SOCKETS_NUM ? SAL_SOCKETS_NUM : cnt; - - sockets = rt_realloc(st->sockets, cnt * sizeof(struct sal_socket *)); - if (sockets == RT_NULL) - goto __result; /* return st->max_socket */ - - /* clean the new allocated fds */ - for (index = st->max_socket; index < cnt; index++) - { - sockets[index] = RT_NULL; - } - - st->sockets = sockets; - st->max_socket = cnt; - } - - /* allocate 'struct sal_socket' */ - if (idx < (int) st->max_socket && st->sockets[idx] == RT_NULL) - { - st->sockets[idx] = rt_calloc(1, sizeof(struct sal_socket)); - if (st->sockets[idx] == RT_NULL) - { - idx = st->max_socket; - } - } - -__result: - return idx; -} - -static void socket_free(struct sal_socket_table *st, int idx) -{ - struct sal_socket *sock; - - sock = st->sockets[idx]; - st->sockets[idx] = RT_NULL; - rt_free(sock); -} - -static int socket_new(void) -{ - struct sal_socket *sock; - struct sal_socket_table *st = &socket_table; - int idx; - - sal_lock(); - - /* find an empty sal socket entry */ - idx = socket_alloc(st, 0); - - /* can't find an empty sal socket entry */ - if (idx == (int) st->max_socket) - { - idx = -(1 + SAL_SOCKET_OFFSET); - goto __result; - } - - sock = st->sockets[idx]; - sock->socket = idx + SAL_SOCKET_OFFSET; - sock->magic = SAL_SOCKET_MAGIC; - sock->netdev = RT_NULL; - sock->user_data = RT_NULL; -#ifdef SAL_USING_TLS - sock->user_data_tls = RT_NULL; -#endif - -__result: - sal_unlock(); - return idx + SAL_SOCKET_OFFSET; -} - -static void socket_delete(int socket) -{ - struct sal_socket *sock; - struct sal_socket_table *st = &socket_table; - int idx; - - idx = socket - SAL_SOCKET_OFFSET; - if (idx < 0 || idx >= (int) st->max_socket) - { - return; - } - sal_lock(); - sock = sal_get_socket(socket); - RT_ASSERT(sock != RT_NULL); - sock->magic = 0; - sock->netdev = RT_NULL; - socket_free(st, idx); - sal_unlock(); -} - -int sal_accept(int socket, struct sockaddr *addr, socklen_t *addrlen) -{ - int new_socket; - struct sal_socket *sock; - struct sal_proto_family *pf; - - /* get the socket object by socket descriptor */ - SAL_SOCKET_OBJ_GET(sock, socket); - - /* check the network interface is up status */ - SAL_NETDEV_IS_UP(sock->netdev); - - /* check the network interface socket operations */ - SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, accept); - - new_socket = pf->skt_ops->accept((int) sock->user_data, addr, addrlen); - if (new_socket != -1) - { - int retval; - int new_sal_socket; - struct sal_socket *new_sock; - - /* allocate a new socket structure and registered socket options */ - new_sal_socket = socket_new(); - new_sock = sal_get_socket(new_sal_socket); - if (new_sock == RT_NULL) - { - pf->skt_ops->closesocket(new_socket); - return -1; - } - - retval = socket_init(sock->domain, sock->type, sock->protocol, &new_sock); - if (retval < 0) - { - pf->skt_ops->closesocket(new_socket); - rt_memset(new_sock, 0x00, sizeof(struct sal_socket)); - /* socket init failed, delete socket */ - socket_delete(new_sal_socket); - LOG_E("New socket registered failed, return error %d.", retval); - return -1; - } - - /* new socket create by accept should have the same netdev with server*/ - new_sock->netdev = sock->netdev; - /* socket structure user_data used to store the acquired new socket */ - new_sock->user_data = (void *) new_socket; - - return new_sal_socket; - } - - return -1; -} - -static void sal_sockaddr_to_ipaddr(const struct sockaddr *name, ip_addr_t *local_ipaddr) -{ - const struct sockaddr_in *svr_addr = (const struct sockaddr_in *) name; - -#if NETDEV_IPV4 && NETDEV_IPV6 - local_ipaddr->u_addr.ip4.addr = svr_addr->sin_addr.s_addr; - local_ipaddr->type = IPADDR_TYPE_V4; -#elif NETDEV_IPV4 - local_ipaddr->addr = svr_addr->sin_addr.s_addr; -#elif NETDEV_IPV6 -#error "not only support IPV6" -#endif /* NETDEV_IPV4 && NETDEV_IPV6*/ -} - -int sal_bind(int socket, const struct sockaddr *name, socklen_t namelen) -{ - struct sal_socket *sock; - struct sal_proto_family *pf; - ip_addr_t input_ipaddr; - - RT_ASSERT(name); - - /* get the socket object by socket descriptor */ - SAL_SOCKET_OBJ_GET(sock, socket); - - /* bind network interface by ip address */ - sal_sockaddr_to_ipaddr(name, &input_ipaddr); - - /* check input ipaddr is default netdev ipaddr */ - if (!ip_addr_isany_val(input_ipaddr)) - { - struct sal_proto_family *input_pf = RT_NULL, *local_pf = RT_NULL; - struct netdev *new_netdev = RT_NULL; - - new_netdev = netdev_get_by_ipaddr(&input_ipaddr); - if (new_netdev == RT_NULL) - { - return -1; - } - - /* get input and local ip address proto_family */ - SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, local_pf, bind); - SAL_NETDEV_SOCKETOPS_VALID(new_netdev, input_pf, bind); - - /* check the network interface protocol family type */ - if (input_pf->family != local_pf->family) - { - int new_socket = -1; - - /* protocol family is different, close old socket and create new socket by input ip address */ - local_pf->skt_ops->closesocket(socket); - - new_socket = input_pf->skt_ops->socket(input_pf->family, sock->type, sock->protocol); - if (new_socket < 0) - { - return -1; - } - sock->netdev = new_netdev; - sock->user_data = (void *) new_socket; - } - } - - /* check and get protocol families by the network interface device */ - SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, bind); - return pf->skt_ops->bind((int) sock->user_data, name, namelen); -} - -int sal_shutdown(int socket, int how) -{ - struct sal_socket *sock; - struct sal_proto_family *pf; - int error = 0; - - /* get the socket object by socket descriptor */ - SAL_SOCKET_OBJ_GET(sock, socket); - - /* shutdown operation not need to check network interface status */ - /* check the network interface socket opreation */ - SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, shutdown); - - if (pf->skt_ops->shutdown((int) sock->user_data, how) == 0) - { -#ifdef SAL_USING_TLS - if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, closesocket)) - { - if (proto_tls->ops->closesocket(sock->user_data_tls) < 0) - { - return -1; - } - } -#endif - error = 0; - } - else - { - error = -1; - } - - - return error; -} - -int sal_getpeername(int socket, struct sockaddr *name, socklen_t *namelen) -{ - struct sal_socket *sock; - struct sal_proto_family *pf; - - /* get the socket object by socket descriptor */ - SAL_SOCKET_OBJ_GET(sock, socket); - - /* check the network interface socket opreation */ - SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, getpeername); - - return pf->skt_ops->getpeername((int) sock->user_data, name, namelen); -} - -int sal_getsockname(int socket, struct sockaddr *name, socklen_t *namelen) -{ - struct sal_socket *sock; - struct sal_proto_family *pf; - - /* get socket object by socket descriptor */ - SAL_SOCKET_OBJ_GET(sock, socket); - - /* check the network interface socket opreation */ - SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, getsockname); - - return pf->skt_ops->getsockname((int) sock->user_data, name, namelen); -} - -int sal_getsockopt(int socket, int level, int optname, void *optval, socklen_t *optlen) -{ - struct sal_socket *sock; - struct sal_proto_family *pf; - - /* get the socket object by socket descriptor */ - SAL_SOCKET_OBJ_GET(sock, socket); - - /* check the network interface socket opreation */ - SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, getsockopt); - - return pf->skt_ops->getsockopt((int) sock->user_data, level, optname, optval, optlen); -} - -int sal_setsockopt(int socket, int level, int optname, const void *optval, socklen_t optlen) -{ - struct sal_socket *sock; - struct sal_proto_family *pf; - - /* get the socket object by socket descriptor */ - SAL_SOCKET_OBJ_GET(sock, socket); - - /* check the network interface socket opreation */ - SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, setsockopt); - -#ifdef SAL_USING_TLS - if (level == SOL_TLS) - { - switch (optname) - { - case TLS_CRET_LIST: - SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_cret_list, optval, optlen); - break; - - case TLS_CIPHERSUITE_LIST: - SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_ciphersurite, optval, optlen); - break; - - case TLS_PEER_VERIFY: - SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_peer_verify, optval, optlen); - break; - - case TLS_DTLS_ROLE: - SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_dtls_role, optval, optlen); - break; - - default: - return -1; - } - - return 0; - } - else - { - return pf->skt_ops->setsockopt((int) sock->user_data, level, optname, optval, optlen); - } -#else - return pf->skt_ops->setsockopt((int) sock->user_data, level, optname, optval, optlen); -#endif /* SAL_USING_TLS */ -} - -int sal_connect(int socket, const struct sockaddr *name, socklen_t namelen) -{ - struct sal_socket *sock; - struct sal_proto_family *pf; - int ret; - - /* get the socket object by socket descriptor */ - SAL_SOCKET_OBJ_GET(sock, socket); - - /* check the network interface is up status */ - SAL_NETDEV_IS_UP(sock->netdev); - /* check the network interface socket opreation */ - SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, connect); - - ret = pf->skt_ops->connect((int) sock->user_data, name, namelen); -#ifdef SAL_USING_TLS - if (ret >= 0 && SAL_SOCKOPS_PROTO_TLS_VALID(sock, connect)) - { - if (proto_tls->ops->connect(sock->user_data_tls) < 0) - { - return -1; - } - - return ret; - } -#endif - - return ret; -} - -int sal_listen(int socket, int backlog) -{ - struct sal_socket *sock; - struct sal_proto_family *pf; - - /* get the socket object by socket descriptor */ - SAL_SOCKET_OBJ_GET(sock, socket); - - /* check the network interface socket opreation */ - SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, listen); - - return pf->skt_ops->listen((int) sock->user_data, backlog); -} - -int sal_recvfrom(int socket, void *mem, size_t len, int flags, - struct sockaddr *from, socklen_t *fromlen) -{ - struct sal_socket *sock; - struct sal_proto_family *pf; - - /* get the socket object by socket descriptor */ - SAL_SOCKET_OBJ_GET(sock, socket); - - /* check the network interface is up status */ - SAL_NETDEV_IS_UP(sock->netdev); - /* check the network interface socket opreation */ - SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, recvfrom); - -#ifdef SAL_USING_TLS - if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, recv)) - { - int ret; - - if ((ret = proto_tls->ops->recv(sock->user_data_tls, mem, len)) < 0) - { - return -1; - } - return ret; - } - else - { - return pf->skt_ops->recvfrom((int) sock->user_data, mem, len, flags, from, fromlen); - } -#else - return pf->skt_ops->recvfrom((int) sock->user_data, mem, len, flags, from, fromlen); -#endif -} - -int sal_sendto(int socket, const void *dataptr, size_t size, int flags, - const struct sockaddr *to, socklen_t tolen) -{ - struct sal_socket *sock; - struct sal_proto_family *pf; - - /* get the socket object by socket descriptor */ - SAL_SOCKET_OBJ_GET(sock, socket); - - /* check the network interface is up status */ - SAL_NETDEV_IS_UP(sock->netdev); - /* check the network interface socket opreation */ - SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, sendto); - -#ifdef SAL_USING_TLS - if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, send)) - { - int ret; - - if ((ret = proto_tls->ops->send(sock->user_data_tls, dataptr, size)) < 0) - { - return -1; - } - return ret; - } - else - { - return pf->skt_ops->sendto((int) sock->user_data, dataptr, size, flags, to, tolen); - } -#else - return pf->skt_ops->sendto((int) sock->user_data, dataptr, size, flags, to, tolen); -#endif -} - -int sal_socket(int domain, int type, int protocol) -{ - int retval; - int socket, proto_socket; - struct sal_socket *sock; - struct sal_proto_family *pf; - - /* allocate a new socket and registered socket options */ - socket = socket_new(); - if (socket < 0) - { - return -1; - } - - /* get sal socket object by socket descriptor */ - sock = sal_get_socket(socket); - if (sock == RT_NULL) - { - socket_delete(socket); - return -1; - } - - /* Initialize sal socket object */ - retval = socket_init(domain, type, protocol, &sock); - if (retval < 0) - { - LOG_E("SAL socket protocol family input failed, return error %d.", retval); - socket_delete(socket); - return -1; - } - - /* valid the network interface socket opreation */ - SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, socket); - - proto_socket = pf->skt_ops->socket(domain, type, protocol); - if (proto_socket >= 0) - { -#ifdef SAL_USING_TLS - if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, socket)) - { - sock->user_data_tls = proto_tls->ops->socket(socket); - if (sock->user_data_tls == RT_NULL) - { - socket_delete(socket); - return -1; - } - } -#endif - sock->user_data = (void *) proto_socket; - return sock->socket; - } - socket_delete(socket); - return -1; -} - -int sal_closesocket(int socket) -{ - struct sal_socket *sock; - struct sal_proto_family *pf; - int error = 0; - - /* get the socket object by socket descriptor */ - SAL_SOCKET_OBJ_GET(sock, socket); - - /* clsoesocket operation not need to vaild network interface status */ - /* valid the network interface socket opreation */ - SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, closesocket); - - if (pf->skt_ops->closesocket((int) sock->user_data) == 0) - { -#ifdef SAL_USING_TLS - if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, closesocket)) - { - if (proto_tls->ops->closesocket(sock->user_data_tls) < 0) - { - return -1; - } - } -#endif - error = 0; - } - else - { - error = -1; - } - - /* delete socket */ - socket_delete(socket); - - return error; -} - -int sal_ioctlsocket(int socket, long cmd, void *arg) -{ - struct sal_socket *sock; - struct sal_proto_family *pf; - - /* get the socket object by socket descriptor */ - SAL_SOCKET_OBJ_GET(sock, socket); - - /* check the network interface socket opreation */ - SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, ioctlsocket); - - return pf->skt_ops->ioctlsocket((int) sock->user_data, cmd, arg); -} - -#ifdef SAL_USING_POSIX -int sal_poll(struct dfs_fd *file, struct rt_pollreq *req) -{ - struct sal_socket *sock; - struct sal_proto_family *pf; - int socket = (int) file->data; - - /* get the socket object by socket descriptor */ - SAL_SOCKET_OBJ_GET(sock, socket); - - /* check the network interface is up status */ - SAL_NETDEV_IS_UP(sock->netdev); - /* check the network interface socket opreation */ - SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, poll); - - return pf->skt_ops->poll(file, req); -} -#endif - -struct hostent *sal_gethostbyname(const char *name) -{ - struct netdev *netdev = netdev_default; - struct sal_proto_family *pf; - - if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname)) - { - return pf->netdb_ops->gethostbyname(name); - } - else - { - /* get the first network interface device with up status */ - netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP); - if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname)) - { - return pf->netdb_ops->gethostbyname(name); - } - } - - return RT_NULL; -} - -int sal_gethostbyname_r(const char *name, struct hostent *ret, char *buf, - size_t buflen, struct hostent **result, int *h_errnop) -{ - struct netdev *netdev = netdev_default; - struct sal_proto_family *pf; - - if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname_r)) - { - return pf->netdb_ops->gethostbyname_r(name, ret, buf, buflen, result, h_errnop); - } - else - { - /* get the first network interface device with up status */ - netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP); - if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname_r)) - { - return pf->netdb_ops->gethostbyname_r(name, ret, buf, buflen, result, h_errnop); - } - } - - return -1; -} - -int sal_getaddrinfo(const char *nodename, - const char *servname, - const struct addrinfo *hints, - struct addrinfo **res) -{ - struct netdev *netdev = netdev_default; - struct sal_proto_family *pf; - int ret = 0; - rt_uint32_t i = 0; - - if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, getaddrinfo)) - { - ret = pf->netdb_ops->getaddrinfo(nodename, servname, hints, res); - } - else - { - /* get the first network interface device with up status */ - netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP); - if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, getaddrinfo)) - { - ret = pf->netdb_ops->getaddrinfo(nodename, servname, hints, res); - } - else - { - ret = -1; - } - } - - if(ret == RT_EOK) - { - /*record the netdev and res*/ - for(i = 0; i < SAL_SOCKETS_NUM; i++) - { - if(sal_dev_res_tbl[i].res == RT_NULL) - { - sal_dev_res_tbl[i].res = *res; - sal_dev_res_tbl[i].netdev = netdev; - break; - } - } - - RT_ASSERT((i < SAL_SOCKETS_NUM)); - - } - - return ret; -} - -void sal_freeaddrinfo(struct addrinfo *ai) -{ - struct netdev *netdev = RT_NULL; - struct sal_proto_family *pf = RT_NULL; - rt_uint32_t i = 0; - - /*when use the multi netdev, it must free the ai use the getaddrinfo netdev */ - for(i = 0; i < SAL_SOCKETS_NUM; i++) - { - if(sal_dev_res_tbl[i].res == ai) - { - netdev = sal_dev_res_tbl[i].netdev; - sal_dev_res_tbl[i].res = RT_NULL; - sal_dev_res_tbl[i].netdev = RT_NULL; - break; - } - } - RT_ASSERT((i < SAL_SOCKETS_NUM)); - - if (SAL_NETDBOPS_VALID(netdev, pf, freeaddrinfo)) - { - pf->netdb_ops->freeaddrinfo(ai); - } -} |
