2018-07-17 20:29:33 +08:00
|
|
|
/*
|
2021-03-08 18:19:04 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2018-07-17 20:29:33 +08:00
|
|
|
*
|
2018-10-15 01:23:33 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2018-07-17 20:29:33 +08:00
|
|
|
*
|
|
|
|
* 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>
|
2021-09-11 23:48:14 +08:00
|
|
|
#include <poll.h>
|
2018-07-17 20:29:33 +08:00
|
|
|
#include <dfs_net.h>
|
2021-05-22 02:50:35 +08:00
|
|
|
#include <sys/errno.h>
|
2018-07-17 20:29:33 +08:00
|
|
|
#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);
|
2018-09-01 12:17:19 +08:00
|
|
|
sal_closesocket(new_socket);
|
2018-07-17 20:29:33 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
d = fd_get(fd);
|
|
|
|
if(d)
|
|
|
|
{
|
|
|
|
/* this is a socket fd */
|
2022-12-03 12:07:44 +08:00
|
|
|
d->vnode = (struct dfs_fnode *)rt_malloc(sizeof(struct dfs_fnode));
|
|
|
|
if (!d->vnode)
|
|
|
|
{
|
|
|
|
/* release fd */
|
|
|
|
fd_release(fd);
|
|
|
|
rt_set_errno(-ENOMEM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
rt_memset(d->vnode, 0, sizeof(struct dfs_fnode));
|
|
|
|
rt_list_init(&d->vnode->list);
|
|
|
|
|
|
|
|
d->vnode->type = FT_SOCKET;
|
|
|
|
d->vnode->path = NULL;
|
|
|
|
d->vnode->fullpath = NULL;
|
|
|
|
d->vnode->ref_count = 1;
|
|
|
|
d->vnode->fops = dfs_net_get_fops();
|
2018-07-17 20:29:33 +08:00
|
|
|
d->flags = O_RDWR; /* set flags as read and write */
|
2022-12-03 12:07:44 +08:00
|
|
|
d->vnode->size = 0;
|
2018-07-17 20:29:33 +08:00
|
|
|
d->pos = 0;
|
|
|
|
|
|
|
|
/* set socket to the data of dfs_fd */
|
2022-12-03 12:07:44 +08:00
|
|
|
d->vnode->data = (void *)(size_t)new_socket;
|
2018-07-17 20:29:33 +08:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2018-09-01 12:17:19 +08:00
|
|
|
rt_set_errno(-ENOMEM);
|
|
|
|
sal_closesocket(new_socket);
|
2018-07-17 20:29:33 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(accept);
|
|
|
|
|
|
|
|
int bind(int s, const struct sockaddr *name, socklen_t namelen)
|
|
|
|
{
|
|
|
|
int socket = dfs_net_getsocket(s);
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
#ifdef SAL_USING_AF_UNIX
|
|
|
|
struct sockaddr_in server_addr = {0};
|
|
|
|
if (name->sa_family == AF_UNIX)
|
|
|
|
{
|
|
|
|
server_addr.sin_family = AF_INET;
|
|
|
|
server_addr.sin_port = htons(514);
|
|
|
|
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
|
|
|
return sal_bind(socket, (struct sockaddr *)&server_addr, namelen);
|
|
|
|
}
|
|
|
|
#endif /* SAL_USING_AF_UNIX */
|
|
|
|
|
2018-07-17 20:29:33 +08:00
|
|
|
return sal_bind(socket, name, namelen);
|
|
|
|
}
|
|
|
|
RTM_EXPORT(bind);
|
|
|
|
|
|
|
|
int shutdown(int s, int how)
|
|
|
|
{
|
2019-03-05 19:25:05 +08:00
|
|
|
int error = 0;
|
|
|
|
int socket = -1;
|
2018-07-17 20:29:33 +08:00
|
|
|
struct dfs_fd *d;
|
|
|
|
|
2019-03-05 19:25:05 +08:00
|
|
|
socket = dfs_net_getsocket(s);
|
|
|
|
if (socket < 0)
|
|
|
|
{
|
|
|
|
rt_set_errno(-ENOTSOCK);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-07-17 20:29:33 +08:00
|
|
|
d = fd_get(s);
|
|
|
|
if (d == NULL)
|
|
|
|
{
|
|
|
|
rt_set_errno(-EBADF);
|
|
|
|
return -1;
|
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2018-07-17 20:29:33 +08:00
|
|
|
if (sal_shutdown(socket, how) == 0)
|
|
|
|
{
|
2019-03-05 19:25:05 +08:00
|
|
|
error = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_set_errno(-ENOTSOCK);
|
|
|
|
error = -1;
|
2018-07-17 20:29:33 +08:00
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2019-03-05 19:25:05 +08:00
|
|
|
return error;
|
2018-07-17 20:29:33 +08:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
#ifdef SAL_USING_AF_UNIX
|
|
|
|
struct sockaddr_in server_addr = {0};
|
|
|
|
if (name->sa_family == AF_UNIX)
|
|
|
|
{
|
|
|
|
server_addr.sin_family = AF_INET;
|
|
|
|
server_addr.sin_port = htons(514);
|
|
|
|
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
|
|
|
return sal_connect(socket, (struct sockaddr *)&server_addr, namelen);
|
|
|
|
}
|
|
|
|
#endif /* SAL_USING_AF_UNIX */
|
|
|
|
|
2018-07-17 20:29:33 +08:00
|
|
|
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);
|
2022-12-03 12:07:44 +08:00
|
|
|
d->vnode = (struct dfs_fnode *)rt_malloc(sizeof(struct dfs_fnode));
|
|
|
|
if (!d->vnode)
|
|
|
|
{
|
|
|
|
/* release fd */
|
|
|
|
fd_release(fd);
|
|
|
|
rt_set_errno(-ENOMEM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SAL_USING_AF_UNIX
|
|
|
|
if (domain == AF_UNIX)
|
|
|
|
{
|
|
|
|
domain = AF_INET;
|
|
|
|
}
|
|
|
|
#endif /* SAL_USING_AF_UNIX */
|
2018-07-17 20:29:33 +08:00
|
|
|
|
|
|
|
/* create socket and then put it to the dfs_fd */
|
|
|
|
socket = sal_socket(domain, type, protocol);
|
|
|
|
if (socket >= 0)
|
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_memset(d->vnode, 0, sizeof(struct dfs_fnode));
|
|
|
|
rt_list_init(&d->vnode->list);
|
2018-07-17 20:29:33 +08:00
|
|
|
/* this is a socket fd */
|
2022-12-03 12:07:44 +08:00
|
|
|
d->vnode->type = FT_SOCKET;
|
|
|
|
d->vnode->path = NULL;
|
|
|
|
d->vnode->fullpath = NULL;
|
|
|
|
d->vnode->ref_count = 1;
|
|
|
|
d->vnode->fops = dfs_net_get_fops();
|
2018-07-17 20:29:33 +08:00
|
|
|
|
|
|
|
d->flags = O_RDWR; /* set flags as read and write */
|
2022-12-03 12:07:44 +08:00
|
|
|
d->vnode->size = 0;
|
2018-07-17 20:29:33 +08:00
|
|
|
d->pos = 0;
|
|
|
|
|
|
|
|
/* set socket to the data of dfs_fd */
|
2022-12-03 12:07:44 +08:00
|
|
|
d->vnode->data = (void *)(size_t)socket;
|
2018-07-17 20:29:33 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_free(d->vnode);
|
2018-07-17 20:29:33 +08:00
|
|
|
/* release fd */
|
2022-12-03 12:07:44 +08:00
|
|
|
fd_release(fd);
|
2018-07-17 20:29:33 +08:00
|
|
|
rt_set_errno(-ENOMEM);
|
2019-03-05 19:25:05 +08:00
|
|
|
return -1;
|
2018-07-17 20:29:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(socket);
|
|
|
|
|
|
|
|
int closesocket(int s)
|
|
|
|
{
|
2019-03-05 19:25:05 +08:00
|
|
|
int error = 0;
|
|
|
|
int socket = -1;
|
2018-07-17 20:29:33 +08:00
|
|
|
struct dfs_fd *d;
|
|
|
|
|
2019-03-05 19:25:05 +08:00
|
|
|
socket = dfs_net_getsocket(s);
|
|
|
|
if (socket < 0)
|
|
|
|
{
|
|
|
|
rt_set_errno(-ENOTSOCK);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-07-17 20:29:33 +08:00
|
|
|
d = fd_get(s);
|
2019-03-05 19:25:05 +08:00
|
|
|
if (d == RT_NULL)
|
2018-07-17 20:29:33 +08:00
|
|
|
{
|
2019-03-05 19:25:05 +08:00
|
|
|
rt_set_errno(-EBADF);
|
2018-07-17 20:29:33 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (!d->vnode)
|
|
|
|
{
|
|
|
|
rt_set_errno(-EBADF);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-07-17 20:29:33 +08:00
|
|
|
if (sal_closesocket(socket) == 0)
|
|
|
|
{
|
2019-03-05 19:25:05 +08:00
|
|
|
error = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_set_errno(-ENOTSOCK);
|
|
|
|
error = -1;
|
2018-07-17 20:29:33 +08:00
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_free(d->vnode);
|
2019-03-05 19:25:05 +08:00
|
|
|
/* socket has been closed, delete it from file system fd */
|
2022-12-03 12:07:44 +08:00
|
|
|
fd_release(s);
|
2019-03-05 19:25:05 +08:00
|
|
|
|
|
|
|
return error;
|
2018-07-17 20:29:33 +08:00
|
|
|
}
|
|
|
|
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);
|