2015-05-01 16:19:52 +08:00
|
|
|
/*
|
|
|
|
* File : lwip_sockets.c
|
|
|
|
* This file is part of RT-Thread RTOS
|
|
|
|
* COPYRIGHT (C) 2015, 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.
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2015-02-17 Bernard First version
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dfs.h>
|
2015-05-02 22:53:08 +08:00
|
|
|
#include <dfs_posix.h>
|
2017-10-17 22:27:06 +08:00
|
|
|
#include <dfs_poll.h>
|
2015-05-01 16:19:52 +08:00
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2016-05-10 09:22:01 +08:00
|
|
|
#include "dfs_net.h"
|
2015-05-01 16:19:52 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
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 (sock->lastdata || sock->rcvevent > 0) 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-01 16:19:52 +08:00
|
|
|
int accept(int s, struct sockaddr *addr, socklen_t *addrlen)
|
|
|
|
{
|
2015-10-11 15:38:35 +08:00
|
|
|
int new_client = -1;
|
2016-05-10 09:22:01 +08:00
|
|
|
int sock = dfs_net_getsocket(s);
|
2015-05-01 16:19:52 +08:00
|
|
|
|
2015-05-11 12:32:53 +08:00
|
|
|
new_client = lwip_accept(sock, addr, addrlen);
|
2015-10-11 15:38:35 +08:00
|
|
|
if (new_client != -1)
|
|
|
|
{
|
|
|
|
/* this is a new socket, create it in file system fd */
|
|
|
|
int fd;
|
|
|
|
struct dfs_fd *d;
|
2017-10-15 22:44:53 +08:00
|
|
|
struct lwip_sock *lwsock;
|
|
|
|
|
2015-10-11 15:38:35 +08:00
|
|
|
/* allocate a fd */
|
|
|
|
fd = fd_new();
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
rt_set_errno(-ENOMEM);
|
2015-10-11 15:38:35 +08:00
|
|
|
lwip_close(sock);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
d = fd_get(fd);
|
|
|
|
|
|
|
|
/* this is a socket fd */
|
|
|
|
d->type = FT_SOCKET;
|
|
|
|
d->path = RT_NULL;
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
d->fops = dfs_net_get_fops();
|
|
|
|
/* initialize wait head */
|
|
|
|
lwsock = lwip_tryget_socket(new_client);
|
2018-06-26 11:19:38 +08:00
|
|
|
rt_wqueue_init(&(lwsock->wait_head));
|
2015-10-11 15:38:35 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
d->flags = O_RDWR; /* set flags as read and write */
|
2015-10-11 15:38:35 +08:00
|
|
|
d->size = 0;
|
|
|
|
d->pos = 0;
|
|
|
|
|
|
|
|
/* set socket to the data of dfs_fd */
|
|
|
|
d->data = (void *) new_client;
|
|
|
|
|
|
|
|
/* release the ref-count of fd */
|
|
|
|
fd_put(d);
|
|
|
|
|
2015-05-11 12:32:53 +08:00
|
|
|
return fd;
|
2015-10-11 15:38:35 +08:00
|
|
|
}
|
2015-05-11 12:32:53 +08:00
|
|
|
|
2015-10-11 15:38:35 +08:00
|
|
|
return new_client;
|
2015-05-01 16:19:52 +08:00
|
|
|
}
|
2015-10-11 15:38:35 +08:00
|
|
|
RTM_EXPORT(accept);
|
2015-05-01 16:19:52 +08:00
|
|
|
|
|
|
|
int bind(int s, const struct sockaddr *name, socklen_t namelen)
|
|
|
|
{
|
2016-05-10 09:22:01 +08:00
|
|
|
int sock = dfs_net_getsocket(s);
|
2015-10-11 15:38:35 +08:00
|
|
|
|
2015-05-01 16:19:52 +08:00
|
|
|
return lwip_bind(sock, name, namelen);
|
|
|
|
}
|
2015-10-11 15:38:35 +08:00
|
|
|
RTM_EXPORT(bind);
|
2015-05-01 16:19:52 +08:00
|
|
|
|
|
|
|
int shutdown(int s, int how)
|
|
|
|
{
|
2015-05-11 12:32:53 +08:00
|
|
|
int sock;
|
2015-10-11 15:38:35 +08:00
|
|
|
struct dfs_fd *d;
|
2015-05-11 12:32:53 +08:00
|
|
|
|
2015-10-11 15:38:35 +08:00
|
|
|
d = fd_get(s);
|
|
|
|
if (d == RT_NULL)
|
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
rt_set_errno(-EBADF);
|
2015-05-11 12:32:53 +08:00
|
|
|
|
2015-10-11 15:38:35 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-10 09:22:01 +08:00
|
|
|
sock = dfs_net_getsocket(s);
|
2015-05-11 12:32:53 +08:00
|
|
|
if (lwip_shutdown(sock, how) == 0)
|
|
|
|
{
|
2015-10-11 15:38:35 +08:00
|
|
|
/* socket has been closed, delete it from file system fd */
|
|
|
|
fd_put(d);
|
|
|
|
fd_put(d);
|
|
|
|
|
|
|
|
return 0;
|
2015-05-11 12:32:53 +08:00
|
|
|
}
|
2015-05-01 16:19:52 +08:00
|
|
|
|
2015-10-11 15:38:35 +08:00
|
|
|
return -1;
|
2015-05-01 16:19:52 +08:00
|
|
|
}
|
2015-10-11 15:38:35 +08:00
|
|
|
RTM_EXPORT(shutdown);
|
2015-05-01 16:19:52 +08:00
|
|
|
|
2015-10-11 15:38:35 +08:00
|
|
|
int getpeername(int s, struct sockaddr *name, socklen_t *namelen)
|
2015-05-01 16:19:52 +08:00
|
|
|
{
|
2016-05-10 09:22:01 +08:00
|
|
|
int sock = dfs_net_getsocket(s);
|
2015-05-01 16:19:52 +08:00
|
|
|
|
|
|
|
return lwip_getpeername(sock, name, namelen);
|
|
|
|
}
|
2015-10-11 15:38:35 +08:00
|
|
|
RTM_EXPORT(getpeername);
|
2015-05-01 16:19:52 +08:00
|
|
|
|
2015-10-11 15:38:35 +08:00
|
|
|
int getsockname(int s, struct sockaddr *name, socklen_t *namelen)
|
2015-05-01 16:19:52 +08:00
|
|
|
{
|
2016-05-10 09:22:01 +08:00
|
|
|
int sock = dfs_net_getsocket(s);
|
2015-10-11 15:38:35 +08:00
|
|
|
|
2015-05-01 16:19:52 +08:00
|
|
|
return lwip_getsockname(sock, name, namelen);
|
|
|
|
}
|
2015-10-11 15:38:35 +08:00
|
|
|
RTM_EXPORT(getsockname);
|
2015-05-01 16:19:52 +08:00
|
|
|
|
2015-10-11 15:38:35 +08:00
|
|
|
int getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen)
|
2015-05-01 16:19:52 +08:00
|
|
|
{
|
2016-05-10 09:22:01 +08:00
|
|
|
int sock = dfs_net_getsocket(s);
|
2015-10-11 15:38:35 +08:00
|
|
|
|
2015-05-01 16:19:52 +08:00
|
|
|
return lwip_getsockopt(sock, level, optname, optval, optlen);
|
|
|
|
}
|
2015-10-11 15:38:35 +08:00
|
|
|
RTM_EXPORT(getsockopt);
|
2015-05-01 16:19:52 +08:00
|
|
|
|
2015-10-11 15:38:35 +08:00
|
|
|
int setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen)
|
2015-05-01 16:19:52 +08:00
|
|
|
{
|
2016-05-10 09:22:01 +08:00
|
|
|
int sock = dfs_net_getsocket(s);
|
2015-10-11 15:38:35 +08:00
|
|
|
|
2018-05-27 21:30:04 +08:00
|
|
|
#if LWIP_VERSION_MAJOR < 2U
|
|
|
|
#error "Your lwIP version is not supported. Please using lwIP 2.0.0+."
|
|
|
|
#endif
|
|
|
|
|
2015-05-01 16:19:52 +08:00
|
|
|
return lwip_setsockopt(sock, level, optname, optval, optlen);
|
|
|
|
}
|
2015-10-11 15:38:35 +08:00
|
|
|
RTM_EXPORT(setsockopt);
|
2015-05-01 16:19:52 +08:00
|
|
|
|
|
|
|
int connect(int s, const struct sockaddr *name, socklen_t namelen)
|
|
|
|
{
|
2016-05-10 09:22:01 +08:00
|
|
|
int sock = dfs_net_getsocket(s);
|
2015-10-11 15:38:35 +08:00
|
|
|
|
2015-05-01 16:19:52 +08:00
|
|
|
return lwip_connect(sock, name, namelen);
|
|
|
|
}
|
2015-10-11 15:38:35 +08:00
|
|
|
RTM_EXPORT(connect);
|
2015-05-01 16:19:52 +08:00
|
|
|
|
|
|
|
int listen(int s, int backlog)
|
|
|
|
{
|
2016-05-10 09:22:01 +08:00
|
|
|
int sock = dfs_net_getsocket(s);
|
2015-10-11 15:38:35 +08:00
|
|
|
|
2015-05-01 16:19:52 +08:00
|
|
|
return lwip_listen(sock, backlog);
|
|
|
|
}
|
2015-10-11 15:38:35 +08:00
|
|
|
RTM_EXPORT(listen);
|
2015-05-01 16:19:52 +08:00
|
|
|
|
|
|
|
int recv(int s, void *mem, size_t len, int flags)
|
|
|
|
{
|
2016-05-10 09:22:01 +08:00
|
|
|
int sock = dfs_net_getsocket(s);
|
2015-10-11 15:38:35 +08:00
|
|
|
|
2015-05-01 16:19:52 +08:00
|
|
|
return lwip_recv(sock, mem, len, flags);
|
|
|
|
}
|
2015-10-11 15:38:35 +08:00
|
|
|
RTM_EXPORT(recv);
|
2015-05-01 16:19:52 +08:00
|
|
|
|
|
|
|
int recvfrom(int s, void *mem, size_t len, int flags,
|
2015-10-11 15:38:35 +08:00
|
|
|
struct sockaddr *from, socklen_t *fromlen)
|
2015-05-01 16:19:52 +08:00
|
|
|
{
|
2016-05-10 09:22:01 +08:00
|
|
|
int sock = dfs_net_getsocket(s);
|
2015-10-11 15:38:35 +08:00
|
|
|
|
2015-05-01 16:19:52 +08:00
|
|
|
return lwip_recvfrom(sock, mem, len, flags, from, fromlen);
|
|
|
|
}
|
2015-10-11 15:38:35 +08:00
|
|
|
RTM_EXPORT(recvfrom);
|
2015-05-01 16:19:52 +08:00
|
|
|
|
|
|
|
int send(int s, const void *dataptr, size_t size, int flags)
|
|
|
|
{
|
2016-05-10 09:22:01 +08:00
|
|
|
int sock = dfs_net_getsocket(s);
|
2015-10-11 15:38:35 +08:00
|
|
|
|
2015-05-01 16:19:52 +08:00
|
|
|
return lwip_send(sock, dataptr, size, flags);
|
|
|
|
}
|
2015-10-11 15:38:35 +08:00
|
|
|
RTM_EXPORT(send);
|
2015-05-01 16:19:52 +08:00
|
|
|
|
|
|
|
int sendto(int s, const void *dataptr, size_t size, int flags,
|
2015-10-11 15:38:35 +08:00
|
|
|
const struct sockaddr *to, socklen_t tolen)
|
2015-05-01 16:19:52 +08:00
|
|
|
{
|
2016-05-10 09:22:01 +08:00
|
|
|
int sock = dfs_net_getsocket(s);
|
2015-10-11 15:38:35 +08:00
|
|
|
|
2015-05-01 16:19:52 +08:00
|
|
|
return lwip_sendto(sock, dataptr, size, flags, to, tolen);
|
|
|
|
}
|
2015-10-11 15:38:35 +08:00
|
|
|
RTM_EXPORT(sendto);
|
2015-05-01 16:19:52 +08:00
|
|
|
|
|
|
|
int socket(int domain, int type, int protocol)
|
|
|
|
{
|
|
|
|
/* create a BSD socket */
|
|
|
|
int fd;
|
|
|
|
int sock;
|
|
|
|
struct dfs_fd *d;
|
2017-10-15 22:44:53 +08:00
|
|
|
struct lwip_sock *lwsock;
|
2015-05-01 16:19:52 +08:00
|
|
|
|
|
|
|
/* allocate a fd */
|
|
|
|
fd = fd_new();
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
rt_set_errno(-ENOMEM);
|
2015-05-01 16:19:52 +08:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
d = fd_get(fd);
|
|
|
|
|
|
|
|
/* create socket in lwip and then put it to the dfs_fd */
|
|
|
|
sock = lwip_socket(domain, type, protocol);
|
2017-10-15 22:44:53 +08:00
|
|
|
if (sock >= 0)
|
2015-05-01 16:19:52 +08:00
|
|
|
{
|
|
|
|
/* this is a socket fd */
|
2017-10-15 22:44:53 +08:00
|
|
|
d->type = FT_SOCKET;
|
|
|
|
d->path = NULL;
|
2015-05-01 16:19:52 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
d->fops = dfs_net_get_fops();
|
2015-10-11 15:38:35 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
d->flags = O_RDWR; /* set flags as read and write */
|
|
|
|
d->size = 0;
|
|
|
|
d->pos = 0;
|
2015-10-11 15:38:35 +08:00
|
|
|
|
2015-05-01 16:19:52 +08:00
|
|
|
/* set socket to the data of dfs_fd */
|
2015-10-11 15:38:35 +08:00
|
|
|
d->data = (void *) sock;
|
2017-10-15 22:44:53 +08:00
|
|
|
|
|
|
|
lwsock = lwip_tryget_socket(sock);
|
2018-06-26 11:19:38 +08:00
|
|
|
rt_wqueue_init(&(lwsock->wait_head));
|
2017-10-15 22:44:53 +08:00
|
|
|
lwsock->conn->callback = event_callback;
|
2015-05-01 16:19:52 +08:00
|
|
|
}
|
2018-05-27 21:37:08 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* release fd */
|
|
|
|
fd_put(d);
|
|
|
|
fd_put(d);
|
|
|
|
|
|
|
|
rt_set_errno(-ENOMEM);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2015-05-01 16:19:52 +08:00
|
|
|
|
|
|
|
/* release the ref-count of fd */
|
|
|
|
fd_put(d);
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
2015-10-11 15:38:35 +08:00
|
|
|
RTM_EXPORT(socket);
|
2017-10-15 22:44:53 +08:00
|
|
|
|
|
|
|
int closesocket(int s)
|
|
|
|
{
|
|
|
|
int sock = dfs_net_getsocket(s);
|
2017-12-11 17:25:25 +08:00
|
|
|
struct dfs_fd *d;
|
|
|
|
|
|
|
|
d = fd_get(s);
|
|
|
|
|
|
|
|
/* socket has been closed, delete it from file system fd */
|
|
|
|
fd_put(d);
|
|
|
|
fd_put(d);
|
2017-10-15 22:44:53 +08:00
|
|
|
|
|
|
|
return lwip_close(sock);
|
|
|
|
}
|
|
|
|
RTM_EXPORT(closesocket);
|
|
|
|
|
|
|
|
int ioctlsocket(int s, long cmd, void *arg)
|
|
|
|
{
|
|
|
|
int sock = dfs_net_getsocket(s);
|
|
|
|
|
|
|
|
return lwip_ioctl(sock, cmd, arg);
|
|
|
|
}
|
|
|
|
RTM_EXPORT(ioctlsocket);
|