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/src | |
| parent | 4a43d3cdae1b731dcf51d3531048c51fd4142516 (diff) | |
| parent | d4469fe7d83d08913d096dc6d75a750dd854c0cc (diff) | |
Merge pull request #1 from RT-Thread/master
test
Diffstat (limited to 'components/net/sal_socket/src')
| -rw-r--r-- | components/net/sal_socket/src/sal_ipaddr.c | 18 | ||||
| -rw-r--r-- | components/net/sal_socket/src/sal_socket.c | 418 |
2 files changed, 338 insertions, 98 deletions
diff --git a/components/net/sal_socket/src/sal_ipaddr.c b/components/net/sal_socket/src/sal_ipaddr.c index 0060ccec45..6477a8ad64 100644 --- a/components/net/sal_socket/src/sal_ipaddr.c +++ b/components/net/sal_socket/src/sal_ipaddr.c @@ -1,21 +1,7 @@ /* - * File : sal_ipaddr.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 diff --git a/components/net/sal_socket/src/sal_socket.c b/components/net/sal_socket/src/sal_socket.c index 71aaa2c658..dba8e66254 100644 --- a/components/net/sal_socket/src/sal_socket.c +++ b/components/net/sal_socket/src/sal_socket.c @@ -1,25 +1,12 @@ /* - * File : sal_socket.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-05-23 ChenYong First version + * 2018-11-12 ChenYong Add TLS support */ #include <rtthread.h> @@ -27,14 +14,19 @@ #include <sal_socket.h> #include <sal_netdb.h> +#ifdef SAL_USING_TLS +#include <sal_tls.h> +#endif #include <sal.h> #define DBG_ENABLE -#define DBG_SECTION_NAME "SAL_SOC" -#define DBG_LEVEL DBG_INFO +#define DBG_SECTION_NAME "SAL_SOC" +#define DBG_LEVEL DBG_INFO #define DBG_COLOR #include <rtdbg.h> +#define SOCKET_TABLE_STEP_LEN 4 + /* the socket table used to dynamic allocate sockets */ struct sal_socket_table { @@ -42,29 +34,58 @@ struct sal_socket_table struct sal_socket **sockets; }; +#ifdef SAL_USING_TLS +/* The global TLS protocol options */ +static struct sal_proto_tls *proto_tls; +#endif + /* The global array of available protocol families */ -static struct proto_family proto_families[SAL_PROTO_FAMILIES_NUM]; +static struct sal_proto_family proto_families[SAL_PROTO_FAMILIES_NUM]; /* 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; +#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) \ + + /** - * SAL (Socket Abstraction Layer) initialization. + * SAL (Socket Abstraction Layer) initialize. * - * @return result - * >= 0: initialize success + * @return result 0: initialize success + * -1: initialize failed */ int sal_init(void) { - if(init_ok) + int cn; + + if (init_ok) { LOG_D("Socket Abstraction Layer is already initialized."); return 0; } - /* clean sal socket table */ - memset(&socket_table, 0, sizeof(socket_table)); + /* 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; + } + /* create sal socket lock */ rt_mutex_init(&sal_core_lock, "sal_lock", RT_IPC_FLAG_FIFO); @@ -76,14 +97,31 @@ int sal_init(void) INIT_COMPONENT_EXPORT(sal_init); /** - * this function will register the current protocol family to the global array of protocol families. + * This function will register TLS protocol to the global TLS protocol. * - * @param pf protocol families structure + * @param pt TLS protocol object * - * @return 0 : protocol families structure index - * -1 : the global array of available protocol families is full + * @return 0: TLS protocol object register success */ -int sal_proto_family_register(const struct proto_family *pf) +#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 register protocol family to the global array of protocol families. + * + * @param pf protocol family object + * + * @return 0: protocol family object register success + * -1: the global array of available protocol families is full + */ +int sal_proto_family_register(const struct sal_proto_family *pf) { rt_base_t level; int idx; @@ -91,15 +129,26 @@ int sal_proto_family_register(const struct proto_family *pf) /* disable interrupt */ level = rt_hw_interrupt_disable(); + /* check protocol family is already registered */ + for(idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++) + { + if (proto_families[idx].family == pf->family && proto_families[idx].create) + { + /* enable interrupt */ + rt_hw_interrupt_enable(level); + LOG_E("%s protocol family is already registered!", pf->family); + return -1; + } + } + /* find an empty protocol family entry */ for(idx = 0; idx < SAL_PROTO_FAMILIES_NUM && proto_families[idx].create; idx++); /* can't find an empty protocol family entry */ - if(idx == SAL_PROTO_FAMILIES_NUM) + if (idx == SAL_PROTO_FAMILIES_NUM) { /* enable interrupt */ rt_hw_interrupt_enable(level); - return -1; } @@ -107,10 +156,11 @@ int sal_proto_family_register(const struct proto_family *pf) proto_families[idx].sec_family = pf->sec_family; proto_families[idx].create = pf->create; - proto_families[idx].gethostbyname = pf->gethostbyname; - proto_families[idx].gethostbyname_r = pf->gethostbyname_r; - proto_families[idx].freeaddrinfo = pf->freeaddrinfo; - proto_families[idx].getaddrinfo = pf->getaddrinfo; + proto_families[idx].ops = pf->ops; + proto_families[idx].ops->gethostbyname = pf->ops->gethostbyname; + proto_families[idx].ops->gethostbyname_r = pf->ops->gethostbyname_r; + proto_families[idx].ops->freeaddrinfo = pf->ops->freeaddrinfo; + proto_families[idx].ops->getaddrinfo = pf->ops->getaddrinfo; /* enable interrupt */ rt_hw_interrupt_enable(level); @@ -119,11 +169,87 @@ int sal_proto_family_register(const struct proto_family *pf) } /** - * this function will get socket structure by sal socket descriptor + * This function removes a previously registered protocol family object. + * + * @param pf protocol family object + * + * @return >=0 : unregister protocol family index + * -1 : unregister failed + */ +int sal_proto_family_unregister(int family) +{ + int idx = 0; + + RT_ASSERT(family > 0 && family < AF_MAX); + + for(idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++) + { + if (proto_families[idx].family == family && proto_families[idx].create) + { + rt_memset(&proto_families[idx], 0x00, sizeof(struct sal_proto_family)); + + return idx; + } + } + + return -1; +} + +/** + * This function will judge whether protocol family is registered + * + * @param family protocol family number + * + * @return 1: protocol family is registered + * 0: protocol family is not registered + */ +rt_bool_t sal_proto_family_is_registered(int family) +{ + int idx = 0; + + RT_ASSERT(family > 0 && family < AF_MAX); + + for (idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++) + { + if (proto_families[idx].family == family && proto_families[idx].create) + { + return RT_TRUE; + } + } + + return RT_FALSE; +} + +/** + * This function will get protocol family object by family number. + * + * @param family protocol family number + * + * @return protocol family object + */ +struct sal_proto_family *sal_proto_family_find(int family) +{ + int idx = 0; + + RT_ASSERT(family > 0 && family < AF_MAX); + + for (idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++) + { + if (proto_families[idx].family == family && proto_families[idx].create) + { + return &proto_families[idx]; + } + } + + return RT_NULL; +} + +/** + * This function will get sal socket object by sal socket descriptor. * * @param socket sal socket index * - * @return socket structure of the current sal socket index + * @return sal socket object of the current sal socket index */ struct sal_socket *sal_get_socket(int socket) { @@ -145,7 +271,7 @@ struct sal_socket *sal_get_socket(int socket) } /** - * this function will lock sal socket. + * This function will lock sal socket. * * @note please don't invoke it on ISR. */ @@ -161,7 +287,7 @@ static void sal_lock(void) } /** - * this function will lock sal socket. + * This function will lock sal socket. * * @note please don't invoke it on ISR. */ @@ -171,13 +297,13 @@ static void sal_unlock(void) } /** - * this function will get protocol family structure by family type + * This function will get protocol family structure by family type * * @param family protocol family * * @return protocol family structure address */ -static struct proto_family *get_proto_family(int family) +static struct sal_proto_family *get_proto_family(int family) { int idx; @@ -201,23 +327,23 @@ static struct proto_family *get_proto_family(int family) } /** - * this function will initialize socket structure and set socket options + * 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 socket structure address + * @param res sal socket object address * * @return 0 : socket initialize success * -1 : input the wrong family * -2 : input the wrong socket type - * -3 : get protocol family structure failed + * -3 : get protocol family object failed * -4 : set socket options failed */ static int socket_init(int family, int type, int protocol, struct sal_socket **res) { struct sal_socket *sock; - struct proto_family *pf; + struct sal_proto_family *pf; if (family < 0 || family > AF_MAX) { @@ -234,7 +360,7 @@ static int socket_init(int family, int type, int protocol, struct sal_socket **r sock->type = type; sock->protocol = protocol; - /* get socket protocol family structure */ + /* get socket protocol family object */ if ((pf = get_proto_family(family)) == RT_NULL) { return -3; @@ -268,8 +394,8 @@ static int socket_alloc(struct sal_socket_table *st, int f_socket) int cnt, index; struct sal_socket **sockets; - /* increase the number of FD with 4 step length */ - cnt = st->max_socket + 4; + /* 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 *)); @@ -289,7 +415,7 @@ static int socket_alloc(struct sal_socket_table *st, int f_socket) /* allocate 'struct sal_socket' */ if (idx < (int) st->max_socket && st->sockets[idx] == RT_NULL) { - st->sockets[idx] = rt_malloc(sizeof(struct sal_socket)); + st->sockets[idx] = rt_calloc(1, sizeof(struct sal_socket)); if (st->sockets[idx] == RT_NULL) { idx = st->max_socket; @@ -301,11 +427,6 @@ __result: } -/** - * this function will return a empty sal socket structure address - * - * @return sal socket structure address - */ static int socket_new(void) { struct sal_socket *sock; @@ -327,6 +448,11 @@ static int socket_new(void) sock = st->sockets[idx]; sock->socket = idx + SAL_SOCKET_OFFSET; sock->magic = SAL_SOCKET_MAGIC; + sock->ops = RT_NULL; + sock->user_data = RT_NULL; +#ifdef SAL_USING_TLS + sock->user_data_tls = RT_NULL; +#endif __result: sal_unlock(); @@ -353,20 +479,23 @@ int sal_accept(int socket, struct sockaddr *addr, socklen_t *addrlen) if (new_socket != -1) { int retval; - int new_socket; + int new_sal_socket; struct sal_socket *new_sock; /* allocate a new socket structure and registered socket options */ - new_socket = socket_new(); - if (new_socket < 0) + new_sal_socket = socket_new(); + if (new_sal_socket < 0) { + sock->ops->closesocket(new_socket); return -1; } - new_sock = sal_get_socket(new_socket); + new_sock = sal_get_socket(new_sal_socket); retval = socket_init(sock->domain, sock->type, sock->protocol, &new_sock); if (retval < 0) { + sock->ops->closesocket(new_socket); + rt_memset(new_sock, 0x00, sizeof(struct sal_socket)); LOG_E("New socket registered failed, return error %d.", retval); return -1; } @@ -374,7 +503,7 @@ int sal_accept(int socket, struct sockaddr *addr, socklen_t *addrlen) /* socket struct user_data used to store the acquired new socket */ new_sock->user_data = (void *) new_socket; - return new_sock->socket; + return new_sal_socket; } return -1; @@ -415,7 +544,17 @@ int sal_shutdown(int socket, int how) if (sock->ops->shutdown((int) sock->user_data, how) == 0) { - memset(sock, 0x00, sizeof(struct sal_socket)); +#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 + rt_free(sock); + socket_table.sockets[socket] = RT_NULL; return 0; } @@ -491,12 +630,46 @@ int sal_setsockopt(int socket, int level, int optname, const void *optval, sockl return -RT_ENOSYS; } +#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 sock->ops->setsockopt((int) sock->user_data, level, optname, optval, optlen); + } +#else return sock->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; + int ret; sock = sal_get_socket(socket); if (!sock) @@ -509,7 +682,20 @@ int sal_connect(int socket, const struct sockaddr *name, socklen_t namelen) return -RT_ENOSYS; } - return sock->ops->connect((int) sock->user_data, name, namelen); + ret = sock->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) @@ -546,7 +732,24 @@ int sal_recvfrom(int socket, void *mem, size_t len, int flags, return -RT_ENOSYS; } +#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 sock->ops->recvfrom((int) sock->user_data, mem, len, flags, from, fromlen); + } +#else return sock->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, @@ -565,7 +768,24 @@ int sal_sendto(int socket, const void *dataptr, size_t size, int flags, return -RT_ENOSYS; } +#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 sock->ops->sendto((int) sock->user_data, dataptr, size, flags, to, tolen); + } +#else return sock->ops->sendto((int) sock->user_data, dataptr, size, flags, to, tolen); +#endif } int sal_socket(int domain, int type, int protocol) @@ -597,8 +817,17 @@ int sal_socket(int domain, int type, int protocol) proto_socket = sock->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(proto_socket); + if (sock->user_data_tls == RT_NULL) + { + return -1; + } + } +#endif sock->user_data = (void *) proto_socket; - return sock->socket; } @@ -622,7 +851,17 @@ int sal_closesocket(int socket) if (sock->ops->closesocket((int) sock->user_data) == 0) { - memset(sock, 0x00, sizeof(struct sal_socket)); +#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 + rt_free(sock); + socket_table.sockets[socket] = RT_NULL; return 0; } @@ -647,6 +886,7 @@ int sal_ioctlsocket(int socket, long cmd, void *arg) return sock->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; @@ -665,16 +905,22 @@ int sal_poll(struct dfs_fd *file, struct rt_pollreq *req) return sock->ops->poll(file, req); } +#endif struct hostent *sal_gethostbyname(const char *name) { int i; + struct hostent *hst; for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i) { - if (proto_families[i].gethostbyname) + if (proto_families[i].ops && proto_families[i].ops->gethostbyname) { - return proto_families[i].gethostbyname(name); + hst = proto_families[i].ops->gethostbyname(name); + if (hst != RT_NULL) + { + return hst; + } } } @@ -684,47 +930,55 @@ 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) { - int i; + int i, res; for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i) { - if (proto_families[i].gethostbyname_r) + if (proto_families[i].ops && proto_families[i].ops->gethostbyname_r) { - return proto_families[i].gethostbyname_r(name, ret, buf, buflen, result, h_errnop); + res = proto_families[i].ops->gethostbyname_r(name, ret, buf, buflen, result, h_errnop); + if (res == 0) + { + return res; + } } } return -1; } -void sal_freeaddrinfo(struct addrinfo *ai) +int sal_getaddrinfo(const char *nodename, + const char *servname, + const struct addrinfo *hints, + struct addrinfo **res) { - int i; + int i, ret; for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i) { - if (proto_families[i].freeaddrinfo) + if (proto_families[i].ops && proto_families[i].ops->getaddrinfo) { - proto_families[i].freeaddrinfo(ai); - return; + ret = proto_families[i].ops->getaddrinfo(nodename, servname, hints, res); + if (ret == 0) + { + return ret; + } } } + + return -1; } -int sal_getaddrinfo(const char *nodename, - const char *servname, - const struct addrinfo *hints, - struct addrinfo **res) +void sal_freeaddrinfo(struct addrinfo *ai) { int i; for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i) { - if (proto_families[i].getaddrinfo) + if (proto_families[i].ops && proto_families[i].ops->freeaddrinfo) { - return proto_families[i].getaddrinfo(nodename, servname, hints, res); + proto_families[i].ops->freeaddrinfo(ai); + return; } } - - return -1; } |
