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>
|
|
|
|
#include <dfs_def.h>
|
2015-05-02 22:53:08 +08:00
|
|
|
#include <dfs_posix.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
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* allocate a fd */
|
|
|
|
fd = fd_new();
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
rt_set_errno(-DFS_STATUS_ENOMEM);
|
|
|
|
lwip_close(sock);
|
|
|
|
|
|
|
|
rt_kprintf("no fd yet!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
d = fd_get(fd);
|
|
|
|
|
|
|
|
/* this is a socket fd */
|
|
|
|
d->type = FT_SOCKET;
|
|
|
|
d->path = RT_NULL;
|
|
|
|
|
2016-05-10 09:22:01 +08:00
|
|
|
d->fs = dfs_net_get_fs();
|
2015-10-11 15:38:35 +08:00
|
|
|
|
|
|
|
d->flags = DFS_O_RDWR; /* set flags as read and write */
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
rt_set_errno(-DFS_STATUS_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
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* allocate a fd */
|
|
|
|
fd = fd_new();
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
rt_set_errno(-DFS_STATUS_ENOMEM);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
d = fd_get(fd);
|
|
|
|
|
|
|
|
/* create socket in lwip and then put it to the dfs_fd */
|
|
|
|
sock = lwip_socket(domain, type, protocol);
|
|
|
|
if (sock > 0)
|
|
|
|
{
|
|
|
|
/* this is a socket fd */
|
|
|
|
d->type = FT_SOCKET;
|
|
|
|
d->path = RT_NULL;
|
|
|
|
|
|
|
|
d->fs = dfs_lwip_get_fs();
|
2015-10-11 15:38:35 +08:00
|
|
|
|
2015-05-01 16:19:52 +08:00
|
|
|
d->flags = DFS_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;
|
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);
|