diff options
| author | tmmdh <[email protected]> | 2021-01-20 18:45:20 +0800 |
|---|---|---|
| committer | tmmdh <[email protected]> | 2021-01-20 18:45:20 +0800 |
| commit | 9db4d446382426d215ffe79f60920c98a8b1be53 (patch) | |
| tree | a3bd61199194d5f8251bf7ee889e22baedf5dc31 /components/net/sal_socket/src | |
| parent | 55c0a9f27900d19556d52110be6397f3ee15a6c3 (diff) | |
| parent | afd575105d2fdd29fb003fb49c6e4f5446f824ce (diff) | |
Merge branch 'master' of https://github.com/RT-Thread/rt-thread
Diffstat (limited to 'components/net/sal_socket/src')
| -rw-r--r-- | components/net/sal_socket/src/sal_socket.c | 87 |
1 files changed, 69 insertions, 18 deletions
diff --git a/components/net/sal_socket/src/sal_socket.c b/components/net/sal_socket/src/sal_socket.c index ec0519df02..27dea260fb 100644 --- a/components/net/sal_socket/src/sal_socket.c +++ b/components/net/sal_socket/src/sal_socket.c @@ -21,7 +21,9 @@ #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 @@ -41,6 +43,13 @@ struct sal_socket_table 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; @@ -50,6 +59,7 @@ static struct sal_proto_tls *proto_tls; 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)) @@ -90,6 +100,11 @@ do { ((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. * @@ -116,6 +131,9 @@ int sal_init(void) 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_FIFO); @@ -126,6 +144,7 @@ int sal_init(void) } 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) { @@ -255,6 +274,7 @@ __exit: skt_ops->closesocket(sockfd); } } +#endif /* SAL_INTERNET_CHECK */ /** * This function will check SAL network interface device internet status. @@ -263,10 +283,12 @@ __exit: */ int sal_check_netdev_internet_up(struct netdev *netdev) { + RT_ASSERT(netdev); + +#ifdef SAL_INTERNET_CHECK /* workqueue for network connect */ struct rt_delayed_work *net_work = RT_NULL; - RT_ASSERT(netdev); net_work = (struct rt_delayed_work *)rt_calloc(1, sizeof(struct rt_delayed_work)); if (net_work == RT_NULL) @@ -277,7 +299,7 @@ int sal_check_netdev_internet_up(struct netdev *netdev) rt_delayed_work_init(net_work, check_netdev_internet_up_work, (void *)netdev); rt_work_submit(&(net_work->work), RT_TICK_PER_SECOND); - +#endif /* SAL_INTERNET_CHECK */ return 0; } @@ -309,12 +331,13 @@ 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; } - socket = socket - SAL_SOCKET_OFFSET; /* check socket structure valid or not */ RT_ASSERT(st->sockets[socket]->magic == SAL_SOCKET_MAGIC); @@ -975,7 +998,7 @@ int sal_closesocket(int socket) /* clsoesocket operation not need to vaild network interface status */ /* valid the network interface socket opreation */ - SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, socket); + SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, closesocket); if (pf->skt_ops->closesocket((int) sock->user_data) == 0) { @@ -1086,10 +1109,12 @@ int sal_getaddrinfo(const char *nodename, { 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)) { - return pf->netdb_ops->getaddrinfo(nodename, servname, hints, res); + ret = pf->netdb_ops->getaddrinfo(nodename, servname, hints, res); } else { @@ -1097,30 +1122,56 @@ int sal_getaddrinfo(const char *nodename, netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP); if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, getaddrinfo)) { - return pf->netdb_ops->getaddrinfo(nodename, servname, hints, res); + ret = pf->netdb_ops->getaddrinfo(nodename, servname, hints, res); + } + else + { + ret = -1; } } - return -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 = netdev_default; - struct sal_proto_family *pf; + struct netdev *netdev = RT_NULL; + struct sal_proto_family *pf = RT_NULL; + rt_uint32_t i = 0; - if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, freeaddrinfo)) + /*when use the multi netdev, it must free the ai use the getaddrinfo netdev */ + for(i = 0; i < SAL_SOCKETS_NUM; i++) { - pf->netdb_ops->freeaddrinfo(ai); - } - 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, freeaddrinfo)) + if(sal_dev_res_tbl[i].res == ai) { - pf->netdb_ops->freeaddrinfo(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); + } } |
