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
|
|
|
|
* 2018-05-17 ChenYong First version
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
|
|
|
|
#include <lwip/sockets.h>
|
|
|
|
#include <lwip/netdb.h>
|
|
|
|
#include <lwip/api.h>
|
|
|
|
#include <lwip/init.h>
|
2019-04-16 18:24:55 +08:00
|
|
|
#include <lwip/netif.h>
|
2018-07-17 20:29:33 +08:00
|
|
|
|
|
|
|
#ifdef SAL_USING_POSIX
|
2021-09-11 23:48:14 +08:00
|
|
|
#include <poll.h>
|
2018-07-17 20:29:33 +08:00
|
|
|
#endif
|
|
|
|
|
2022-05-16 09:05:50 +08:00
|
|
|
#include <sal_low_lvl.h>
|
2018-08-24 20:39:10 +08:00
|
|
|
#include <af_inet.h>
|
2018-07-17 20:29:33 +08:00
|
|
|
|
2019-04-16 18:24:55 +08:00
|
|
|
#include <netdev.h>
|
|
|
|
|
2019-06-24 13:56:42 +08:00
|
|
|
#if (LWIP_VERSION < 0x2000000) && NETDEV_IPV6
|
|
|
|
#error "The lwIP version is not support IPV6, please disable netdev IPV6 configuration "
|
|
|
|
#elif (LWIP_VERSION > 0x2000000) && (NETDEV_IPV6 != LWIP_IPV6)
|
|
|
|
#error "IPV6 configuration error, Please check and synchronize netdev and lwip IPV6 configuration."
|
|
|
|
#endif
|
|
|
|
|
2018-07-17 20:29:33 +08:00
|
|
|
#if LWIP_VERSION < 0x2000000
|
|
|
|
#define SELWAIT_T int
|
|
|
|
#else
|
|
|
|
#ifndef SELWAIT_T
|
|
|
|
#define SELWAIT_T u8_t
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2018-10-16 10:31:06 +08:00
|
|
|
#ifdef SAL_USING_LWIP
|
|
|
|
|
2018-08-27 19:50:37 +08:00
|
|
|
#ifdef SAL_USING_POSIX
|
2018-11-01 18:20:03 +08:00
|
|
|
|
|
|
|
#if LWIP_VERSION >= 0x20100ff
|
|
|
|
#include <lwip/priv/sockets_priv.h>
|
|
|
|
#else /* LWIP_VERSION < 0x20100ff */
|
2018-07-17 20:29:33 +08:00
|
|
|
/*
|
|
|
|
* Re-define lwip socket
|
|
|
|
*
|
|
|
|
* NOTE: please make sure the definitions same in lwip::net_socket.c
|
|
|
|
*/
|
|
|
|
struct lwip_sock {
|
|
|
|
/** sockets currently are built on netconns, each socket has one netconn */
|
|
|
|
struct netconn *conn;
|
|
|
|
/** data that was left from the previous read */
|
|
|
|
void *lastdata;
|
|
|
|
/** offset in the data that was left from the previous read */
|
|
|
|
u16_t lastoffset;
|
|
|
|
/** number of times data was received, set by event_callback(),
|
|
|
|
tested by the receive and select functions */
|
|
|
|
s16_t rcvevent;
|
|
|
|
/** number of times data was ACKed (free send buffer), set by event_callback(),
|
|
|
|
tested by select */
|
|
|
|
u16_t sendevent;
|
|
|
|
/** error happened for this socket, set by event_callback(), tested by select */
|
|
|
|
u16_t errevent;
|
|
|
|
/** last error that occurred on this socket */
|
|
|
|
#if LWIP_VERSION < 0x2000000
|
|
|
|
int err;
|
|
|
|
#else
|
|
|
|
u8_t err;
|
|
|
|
#endif
|
|
|
|
/** counter of how many threads are waiting for this socket using select */
|
|
|
|
SELWAIT_T select_waiting;
|
|
|
|
|
|
|
|
rt_wqueue_t wait_head;
|
|
|
|
};
|
2018-11-01 18:20:03 +08:00
|
|
|
#endif /* LWIP_VERSION >= 0x20100ff */
|
2018-07-17 20:29:33 +08:00
|
|
|
|
|
|
|
extern struct lwip_sock *lwip_tryget_socket(int s);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-01 18:20:03 +08:00
|
|
|
#if LWIP_VERSION >= 0x20100ff
|
|
|
|
if ((void*)(sock->lastdata.pbuf) || (sock->rcvevent > 0))
|
|
|
|
#else
|
|
|
|
if ((void*)(sock->lastdata) || (sock->rcvevent > 0))
|
|
|
|
#endif
|
2018-07-17 20:29:33 +08:00
|
|
|
event |= POLLIN;
|
|
|
|
if (sock->sendevent)
|
|
|
|
event |= POLLOUT;
|
|
|
|
if (sock->errevent)
|
|
|
|
event |= POLLERR;
|
|
|
|
|
|
|
|
SYS_ARCH_UNPROTECT(lev);
|
|
|
|
|
|
|
|
if (event)
|
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_wqueue_wakeup(&sock->wait_head, (void*)(size_t)event);
|
2018-07-17 20:29:33 +08:00
|
|
|
}
|
|
|
|
}
|
2018-08-27 19:50:37 +08:00
|
|
|
#endif /* SAL_USING_POSIX */
|
2018-07-17 20:29:33 +08:00
|
|
|
|
|
|
|
static int inet_socket(int domain, int type, int protocol)
|
|
|
|
{
|
2018-08-27 19:50:37 +08:00
|
|
|
#ifdef SAL_USING_POSIX
|
2018-07-17 20:29:33 +08:00
|
|
|
int socket;
|
|
|
|
|
|
|
|
socket = lwip_socket(domain, type, protocol);
|
|
|
|
if (socket >= 0)
|
|
|
|
{
|
|
|
|
struct lwip_sock *lwsock;
|
|
|
|
|
|
|
|
lwsock = lwip_tryget_socket(socket);
|
|
|
|
lwsock->conn->callback = event_callback;
|
|
|
|
|
|
|
|
rt_wqueue_init(&lwsock->wait_head);
|
|
|
|
}
|
|
|
|
|
|
|
|
return socket;
|
2018-08-27 19:50:37 +08:00
|
|
|
#else
|
|
|
|
return lwip_socket(domain, type, protocol);
|
|
|
|
#endif /* SAL_USING_POSIX */
|
2018-07-17 20:29:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int inet_accept(int socket, struct sockaddr *addr, socklen_t *addrlen)
|
|
|
|
{
|
2018-08-27 19:50:37 +08:00
|
|
|
#ifdef SAL_USING_POSIX
|
2018-07-17 20:29:33 +08:00
|
|
|
int new_socket;
|
|
|
|
|
|
|
|
new_socket = lwip_accept(socket, addr, addrlen);
|
|
|
|
if (new_socket >= 0)
|
|
|
|
{
|
|
|
|
struct lwip_sock *lwsock;
|
|
|
|
|
|
|
|
lwsock = lwip_tryget_socket(new_socket);
|
|
|
|
|
|
|
|
rt_wqueue_init(&lwsock->wait_head);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_socket;
|
2018-08-27 19:50:37 +08:00
|
|
|
#else
|
|
|
|
return lwip_accept(socket, addr, addrlen);
|
|
|
|
#endif /* SAL_USING_POSIX */
|
2018-07-17 20:29:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int inet_getsockname(int socket, struct sockaddr *name, socklen_t *namelen)
|
|
|
|
{
|
|
|
|
#if LWIP_VERSION_MAJOR < 2U
|
|
|
|
rt_kprintf("ERROR: Your lwIP version is not supported. Please using lwIP 2.0.0+.\n");
|
|
|
|
RT_ASSERT(LWIP_VERSION_MAJOR >= 2U);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return lwip_getsockname(socket, name, namelen);
|
|
|
|
}
|
|
|
|
|
2019-05-15 14:45:31 +08:00
|
|
|
int inet_ioctlsocket(int socket, long cmd, void *arg)
|
|
|
|
{
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case F_GETFL:
|
|
|
|
case F_SETFL:
|
2022-12-03 12:07:44 +08:00
|
|
|
return lwip_fcntl(socket, cmd, (int)(size_t)arg);
|
2019-05-15 14:45:31 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
return lwip_ioctl(socket, cmd, arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-27 19:50:37 +08:00
|
|
|
#ifdef SAL_USING_POSIX
|
2018-07-17 20:29:33 +08:00
|
|
|
static int inet_poll(struct dfs_fd *file, struct rt_pollreq *req)
|
|
|
|
{
|
|
|
|
int mask = 0;
|
|
|
|
struct lwip_sock *sock;
|
|
|
|
struct sal_socket *sal_sock;
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
sal_sock = sal_get_socket((int)(size_t)file->vnode->data);
|
2018-07-17 20:29:33 +08:00
|
|
|
if(!sal_sock)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
sock = lwip_tryget_socket((int)(size_t)sal_sock->user_data);
|
2018-07-17 20:29:33 +08:00
|
|
|
if (sock != NULL)
|
|
|
|
{
|
|
|
|
rt_base_t level;
|
|
|
|
|
|
|
|
rt_poll_add(&sock->wait_head, req);
|
|
|
|
|
|
|
|
level = rt_hw_interrupt_disable();
|
2018-11-01 18:20:03 +08:00
|
|
|
|
|
|
|
#if LWIP_VERSION >= 0x20100ff
|
|
|
|
if ((void*)(sock->lastdata.pbuf) || sock->rcvevent)
|
|
|
|
#else
|
|
|
|
if ((void*)(sock->lastdata) || sock->rcvevent)
|
|
|
|
#endif
|
2018-07-17 20:29:33 +08:00
|
|
|
{
|
|
|
|
mask |= POLLIN;
|
|
|
|
}
|
|
|
|
if (sock->sendevent)
|
|
|
|
{
|
|
|
|
mask |= POLLOUT;
|
|
|
|
}
|
|
|
|
if (sock->errevent)
|
|
|
|
{
|
|
|
|
mask |= POLLERR;
|
2019-07-25 11:20:40 +08:00
|
|
|
/* clean error event */
|
|
|
|
sock->errevent = 0;
|
2018-07-17 20:29:33 +08:00
|
|
|
}
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
2018-08-27 19:50:37 +08:00
|
|
|
#endif
|
2018-07-17 20:29:33 +08:00
|
|
|
|
2018-11-15 14:39:31 +08:00
|
|
|
static const struct sal_socket_ops lwip_socket_ops =
|
|
|
|
{
|
2018-07-17 20:29:33 +08:00
|
|
|
inet_socket,
|
|
|
|
lwip_close,
|
|
|
|
lwip_bind,
|
|
|
|
lwip_listen,
|
|
|
|
lwip_connect,
|
|
|
|
inet_accept,
|
2018-11-02 10:23:29 +08:00
|
|
|
(int (*)(int, const void *, size_t, int, const struct sockaddr *, socklen_t))lwip_sendto,
|
|
|
|
(int (*)(int, void *, size_t, int, struct sockaddr *, socklen_t *))lwip_recvfrom,
|
2018-07-17 20:29:33 +08:00
|
|
|
lwip_getsockopt,
|
|
|
|
//TODO fix on 1.4.1
|
|
|
|
lwip_setsockopt,
|
|
|
|
lwip_shutdown,
|
|
|
|
lwip_getpeername,
|
|
|
|
inet_getsockname,
|
2019-05-15 14:45:31 +08:00
|
|
|
inet_ioctlsocket,
|
2018-08-27 19:50:37 +08:00
|
|
|
#ifdef SAL_USING_POSIX
|
2018-07-17 20:29:33 +08:00
|
|
|
inet_poll,
|
2018-08-27 19:50:37 +08:00
|
|
|
#endif
|
2018-07-17 20:29:33 +08:00
|
|
|
};
|
|
|
|
|
2019-04-16 18:24:55 +08:00
|
|
|
static const struct sal_netdb_ops lwip_netdb_ops =
|
2018-11-15 14:39:31 +08:00
|
|
|
{
|
2018-07-17 20:29:33 +08:00
|
|
|
lwip_gethostbyname,
|
|
|
|
lwip_gethostbyname_r,
|
|
|
|
lwip_getaddrinfo,
|
2018-11-15 14:39:31 +08:00
|
|
|
lwip_freeaddrinfo,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct sal_proto_family lwip_inet_family =
|
|
|
|
{
|
|
|
|
AF_INET,
|
2019-06-24 13:56:42 +08:00
|
|
|
#if LWIP_VERSION > 0x2000000
|
|
|
|
AF_INET6,
|
|
|
|
#else
|
2018-11-15 14:39:31 +08:00
|
|
|
AF_INET,
|
2021-03-08 18:19:04 +08:00
|
|
|
#endif
|
2019-04-16 18:24:55 +08:00
|
|
|
&lwip_socket_ops,
|
|
|
|
&lwip_netdb_ops,
|
2018-07-17 20:29:33 +08:00
|
|
|
};
|
|
|
|
|
2019-04-16 18:24:55 +08:00
|
|
|
/* Set lwIP network interface device protocol family information */
|
|
|
|
int sal_lwip_netdev_set_pf_info(struct netdev *netdev)
|
2018-07-17 20:29:33 +08:00
|
|
|
{
|
2019-04-16 18:24:55 +08:00
|
|
|
RT_ASSERT(netdev);
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2019-04-16 18:24:55 +08:00
|
|
|
netdev->sal_user_data = (void *) &lwip_inet_family;
|
2018-07-17 20:29:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2018-10-16 10:31:06 +08:00
|
|
|
|
|
|
|
#endif /* SAL_USING_LWIP */
|