Skip to content

Commit 269e46b

Browse files
fix(net): harden sal socket lifetime and close handling
1 parent ecf2409 commit 269e46b

6 files changed

Lines changed: 564 additions & 149 deletions

File tree

components/net/sal/impl/af_inet_at.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static int at_poll(struct dfs_file *file, struct rt_pollreq *req)
3838
}
3939

4040
sock = at_get_socket((int)sal_sock->user_data);
41+
sal_socket_put(sal_sock);
4142
if (sock != NULL)
4243
{
4344
rt_base_t level;

components/net/sal/impl/af_inet_lwip.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ static int inet_poll(struct dfs_file *file, struct rt_pollreq *req)
258258
}
259259

260260
sock = lwip_tryget_socket((int)(size_t)sal_sock->user_data);
261+
sal_socket_put(sal_sock);
261262
if (sock != NULL)
262263
{
263264
rt_base_t level;

components/net/sal/impl/proto_mbedtls.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ int mbedtls_net_send_cb(void *ctx, const unsigned char *buf, size_t len)
9696

9797
/* Register scoket sendto option to TLS send data callback */
9898
ret = pf->skt_ops->sendto((int) sock->user_data, (void *)buf, len, 0, RT_NULL, RT_NULL);
99+
sal_socket_put(sock);
99100
if (ret < 0)
100101
{
101102
#ifdef RT_USING_DFS
@@ -133,6 +134,7 @@ int mbedtls_net_recv_cb( void *ctx, unsigned char *buf, size_t len)
133134

134135
/* Register scoket recvfrom option to TLS recv data callback */
135136
ret = pf->skt_ops->recvfrom((int) sock->user_data, (void *)buf, len, 0, RT_NULL, RT_NULL);
137+
sal_socket_put(sock);
136138
if (ret < 0)
137139
{
138140
#ifdef RT_USING_DFS
@@ -199,24 +201,13 @@ static int mbedtls_connect(void *sock)
199201

200202
static int mbedtls_closesocket(void *sock)
201203
{
202-
struct sal_socket *ssock;
203-
int socket;
204-
205204
if (sock == RT_NULL)
206205
{
207206
return 0;
208207
}
209208

210-
socket = ((MbedTLSSession *) sock)->server_fd.fd;
211-
ssock = sal_get_socket(socket);
212-
if (ssock == RT_NULL)
213-
{
214-
return -1;
215-
}
216-
217209
/* Close TLS client session, and clean user-data in SAL socket */
218210
mbedtls_client_close((MbedTLSSession *) sock);
219-
ssock->user_data_tls = RT_NULL;
220211

221212
return 0;
222213
}

components/net/sal/include/sal_low_lvl.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define SAL_LOW_LEVEL_H__
1515

1616
#include <rtdevice.h>
17+
#include <ipc/completion.h>
1718

1819
#ifdef SAL_USING_POSIX
1920
#include <dfs_file.h>
@@ -30,6 +31,14 @@ typedef uint32_t socklen_t;
3031
/* SAL socket magic word */
3132
#define SAL_SOCKET_MAGIC 0x5A10
3233

34+
enum sal_socket_state
35+
{
36+
SAL_SOCKET_STATE_INIT = 0,
37+
SAL_SOCKET_STATE_OPEN, /* visible in socket table */
38+
SAL_SOCKET_STATE_CLOSING, /* detached, waiting refcnt drop */
39+
SAL_SOCKET_STATE_CLOSED, /* ready for cache reuse */
40+
};
41+
3342
/* The maximum number of sockets structure */
3443
#ifndef SAL_SOCKETS_NUM
3544
#define SAL_SOCKETS_NUM DFS_FD_MAX
@@ -63,6 +72,10 @@ struct sal_socket
6372
#ifdef SAL_USING_TLS
6473
void *user_data_tls; /* user-specific TLS data */
6574
#endif
75+
rt_atomic_t refcnt; /* in-flight SAL references */
76+
rt_uint8_t state; /* socket lifecycle state */
77+
struct rt_completion close_completion; /* wake close waiter */
78+
struct sal_socket *next_free; /* internal cache link */
6679
};
6780

6881
/* network interface socket opreations */
@@ -109,8 +122,8 @@ struct sal_proto_family
109122

110123
/* SAL(Socket Abstraction Layer) initialize */
111124
int sal_init(void);
112-
/* Get SAL socket object by socket descriptor */
113125
struct sal_socket *sal_get_socket(int sock);
126+
void sal_socket_put(struct sal_socket *sock);
114127

115128
/* check SAL socket netweork interface device internet status */
116129
int sal_check_netdev_internet_up(struct netdev *netdev);

0 commit comments

Comments
 (0)