Sockets BSD-Like API module.
#include "lwip/opt.h"
#include "lwip/sockets.h"
#include "lwip/api.h"
#include "lwip/sys.h"
#include "lwip/igmp.h"
#include "lwip/inet.h"
#include "lwip/tcp.h"
#include "lwip/raw.h"
#include "lwip/udp.h"
#include "lwip/tcpip.h"
#include "lwip/pbuf.h"
#include <string.h>
Data Structures | |
struct | lwip_select_cb |
Description for a task waiting in select. More... | |
struct | lwip_setgetsockopt_data |
This struct is used to pass data to the set/getsockopt_internal functions running in tcpip_thread context (only a void* is allowed) More... | |
struct | lwip_sock |
Contains all internal pointers and states used for a socket. More... | |
Macros | |
#define | err_to_errno(err) |
#define | ERR_TO_ERRNO_TABLE_SIZE (sizeof(err_to_errno_table)/sizeof(err_to_errno_table[0])) |
#define | NUM_SOCKETS MEMP_NUM_NETCONN |
#define | set_errno(err) |
#define | sock_set_errno(sk, e) |
Functions | |
static int | alloc_socket (struct netconn *newconn, int accepted) |
Allocate a new socket for a given netconn. More... | |
static void | event_callback (struct netconn *conn, enum netconn_evt evt, u16_t len) |
Callback registered in the netconn layer for each socket-netconn. More... | |
static void | free_socket (struct lwip_sock *sock, int is_tcp) |
Free a socket. More... | |
static struct lwip_sock * | get_socket (int s) |
Map a externally used socket index to the internal socket representation. More... | |
int | lwip_accept (int s, struct sockaddr *addr, socklen_t *addrlen) |
int | lwip_bind (int s, const struct sockaddr *name, socklen_t namelen) |
int | lwip_close (int s) |
int | lwip_connect (int s, const struct sockaddr *name, socklen_t namelen) |
int | lwip_fcntl (int s, int cmd, int val) |
A minimal implementation of fcntl. More... | |
static int | lwip_getaddrname (int s, struct sockaddr *name, socklen_t *namelen, u8_t local) |
int | lwip_getpeername (int s, struct sockaddr *name, socklen_t *namelen) |
int | lwip_getsockname (int s, struct sockaddr *name, socklen_t *namelen) |
int | lwip_getsockopt (int s, int level, int optname, void *optval, socklen_t *optlen) |
static void | lwip_getsockopt_internal (void *arg) |
int | lwip_ioctl (int s, long cmd, void *argp) |
int | lwip_listen (int s, int backlog) |
Set a socket into listen mode. More... | |
int | lwip_read (int s, void *mem, size_t len) |
int | lwip_recv (int s, void *mem, size_t len, int flags) |
int | lwip_recvfrom (int s, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen) |
int | lwip_select (int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, struct timeval *timeout) |
Processing exceptset is not yet implemented. More... | |
static int | lwip_selscan (int maxfdp1, fd_set *readset_in, fd_set *writeset_in, fd_set *exceptset_in, fd_set *readset_out, fd_set *writeset_out, fd_set *exceptset_out) |
Go through the readset and writeset lists and see which socket of the sockets set in the sets has events. More... | |
int | lwip_send (int s, const void *data, size_t size, int flags) |
int | lwip_sendto (int s, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen) |
int | lwip_setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen) |
static void | lwip_setsockopt_internal (void *arg) |
int | lwip_shutdown (int s, int how) |
Unimplemented: Close one end of a full-duplex connection. More... | |
int | lwip_socket (int domain, int type, int protocol) |
void | lwip_socket_init (void) |
Initialize this module. More... | |
int | lwip_write (int s, const void *data, size_t size) |
static struct lwip_sock * | tryget_socket (int s) |
Same as get_socket but doesn't set errno. More... | |
Variables | |
static const int | err_to_errno_table [] |
Table to quickly map an lwIP error (err_t) to a socket error by using -err as an index. More... | |
static volatile int | select_cb_ctr |
This counter is increased from lwip_select when the list is chagned and checked in event_callback to see if it has changed. More... | |
static struct lwip_select_cb * | select_cb_list |
The global list of tasks waiting for select. More... | |
static struct lwip_sock | sockets [NUM_SOCKETS] |
The global array of available sockets. More... | |
#define err_to_errno | ( | err | ) |
Referenced by lwip_accept(), lwip_bind(), lwip_connect(), lwip_getsockopt_internal(), lwip_listen(), lwip_recvfrom(), lwip_send(), lwip_sendto(), and lwip_shutdown().
#define ERR_TO_ERRNO_TABLE_SIZE (sizeof(err_to_errno_table)/sizeof(err_to_errno_table[0])) |
#define NUM_SOCKETS MEMP_NUM_NETCONN |
Referenced by alloc_socket(), get_socket(), lwip_accept(), and tryget_socket().
#define set_errno | ( | err | ) |
Referenced by get_socket(), lwip_close(), lwip_select(), and lwip_socket().
#define sock_set_errno | ( | sk, | |
e | |||
) |
Referenced by lwip_accept(), lwip_bind(), lwip_connect(), lwip_getaddrname(), lwip_getsockopt(), lwip_getsockopt_internal(), lwip_ioctl(), lwip_listen(), lwip_recvfrom(), lwip_send(), lwip_sendto(), lwip_setsockopt(), and lwip_shutdown().
|
static |
Allocate a new socket for a given netconn.
newconn | the netconn for which to allocate a socket |
accepted | 1 if socket has been created by accept(), 0 if socket has been created by socket() |
References lwip_sock::conn, lwip_sock::err, lwip_sock::errevent, lwip_sock::lastdata, lwip_sock::lastoffset, NETCONN_TCP, NULL, NUM_SOCKETS, lwip_sock::rcvevent, lwip_sock::select_waiting, lwip_sock::sendevent, sockets, SYS_ARCH_DECL_PROTECT(), SYS_ARCH_PROTECT(), SYS_ARCH_UNPROTECT(), and netconn::type.
Referenced by lwip_accept(), and lwip_socket().
|
static |
Callback registered in the netconn layer for each socket-netconn.
Processes recvevent (data available) and wakes up tasks waiting for select.
References lwip_sock::errevent, lwip_select_cb::exceptset, FD_ISSET, get_socket(), LWIP_ASSERT, LWIP_UNUSED_ARG, NETCONN_EVT_ERROR, NETCONN_EVT_RCVMINUS, NETCONN_EVT_RCVPLUS, NETCONN_EVT_SENDMINUS, NETCONN_EVT_SENDPLUS, lwip_select_cb::next, NULL, lwip_sock::rcvevent, lwip_select_cb::readset, select_cb_ctr, lwip_sock::select_waiting, lwip_select_cb::sem, lwip_select_cb::sem_signalled, lwip_sock::sendevent, netconn::socket, SYS_ARCH_DECL_PROTECT(), SYS_ARCH_PROTECT(), SYS_ARCH_UNPROTECT(), sys_sem_signal(), and lwip_select_cb::writeset.
Referenced by lwip_accept(), and lwip_socket().
Free a socket.
The socket's netconn must have been delete before!
sock | the socket to free |
is_tcp | != 0 for TCP sockets, used to free lastdata |
References lwip_sock::conn, lwip_sock::err, lwip_sock::lastdata, lwip_sock::lastoffset, netbuf_delete(), NULL, pbuf_free(), SYS_ARCH_DECL_PROTECT(), SYS_ARCH_PROTECT(), and SYS_ARCH_UNPROTECT().
Referenced by lwip_close().
|
static |
Map a externally used socket index to the internal socket representation.
s | externally used socket index |
References lwip_sock::conn, EBADF, LWIP_DEBUGF, NULL, NUM_SOCKETS, set_errno, sockets, and SOCKETS_DEBUG.
Referenced by event_callback(), lwip_accept(), lwip_bind(), lwip_close(), lwip_connect(), lwip_fcntl(), lwip_getaddrname(), lwip_getsockopt(), lwip_ioctl(), lwip_listen(), lwip_recvfrom(), lwip_send(), lwip_sendto(), lwip_setsockopt(), and lwip_shutdown().
References AF_INET, alloc_socket(), netconn::callback, lwip_sock::conn, ENFILE, ERR_OK, err_to_errno, event_callback(), EWOULDBLOCK, get_socket(), htons, inet_addr_from_ipaddr, ip_addr_debug_print, LWIP_ASSERT, LWIP_DEBUGF, MEMCPY, netconn_accept(), netconn_delete(), netconn_is_nonblocking, netconn_peer, netconn_set_noautorecved, NULL, NUM_SOCKETS, lwip_sock::rcvevent, sockaddr_in::sin_addr, sockaddr_in::sin_family, sockaddr_in::sin_len, sockaddr_in::sin_port, sock_set_errno, netconn::socket, sockets, SOCKETS_DEBUG, SYS_ARCH_DECL_PROTECT(), SYS_ARCH_PROTECT(), SYS_ARCH_UNPROTECT(), and U16_F.
int lwip_close | ( | int | s | ) |
References lwip_sock::conn, free_socket(), get_socket(), lwip_sock::lastdata, LWIP_ASSERT, LWIP_DEBUGF, netconn_delete(), NETCONN_TCP, NULL, set_errno, and SOCKETS_DEBUG.
int lwip_fcntl | ( | int | s, |
int | cmd, | ||
int | val | ||
) |
A minimal implementation of fcntl.
Currently only the commands F_GETFL and F_SETFL are implemented. Only the flag O_NONBLOCK is implemented.
References lwip_sock::conn, F_GETFL, F_SETFL, get_socket(), LWIP_DEBUGF, netconn_is_nonblocking, netconn_set_nonblocking, O_NONBLOCK, and SOCKETS_DEBUG.
|
static |
References AF_INET, lwip_sock::conn, get_socket(), htons, inet_addr_from_ipaddr, ip_addr_debug_print, LWIP_DEBUGF, MEMCPY, netconn_getaddr(), sockaddr_in::sin_addr, sockaddr_in::sin_family, sockaddr_in::sin_len, sockaddr_in::sin_port, sock_set_errno, SOCKETS_DEBUG, and U16_F.
Referenced by lwip_getpeername(), and lwip_getsockname().
References lwip_getaddrname().
References lwip_getaddrname().
References lwip_sock::conn, EAFNOSUPPORT, EFAULT, EINVAL, ENOPROTOOPT, lwip_setgetsockopt_data::err, ERR_OK, get_socket(), IP_TOS, IP_TTL, IPPROTO_IP, IPPROTO_TCP, IPPROTO_UDPLITE, lwip_setgetsockopt_data::level, LWIP_DEBUGF, lwip_getsockopt_internal(), NETCONN_TCP, NETCONN_UDP, NETCONN_UDPLITE, NETCONNTYPE_GROUP, NULL, netconn::op_completed, lwip_setgetsockopt_data::optlen, lwip_setgetsockopt_data::optname, lwip_setgetsockopt_data::optval, netconn::pcb, SO_ACCEPTCONN, SO_BROADCAST, SO_ERROR, SO_KEEPALIVE, SO_NO_CHECK, SO_RCVBUF, SO_RCVTIMEO, SO_REUSEADDR, SO_REUSEPORT, SO_TYPE, lwip_setgetsockopt_data::sock, sock_set_errno, SOCKETS_DEBUG, SOL_SOCKET, sys_arch_sem_wait(), TCP_KEEPALIVE, TCP_KEEPCNT, TCP_KEEPIDLE, TCP_KEEPINTVL, TCP_NODELAY, tcpip_callback, netconn::type, and netconn::udp.
References lwip_sock::conn, EINPROGRESS, lwip_sock::err, err_to_errno, inet_addr_from_ipaddr, netconn::ip, IP_TOS, IP_TTL, IPPROTO_IP, IPPROTO_TCP, IPPROTO_UDPLITE, tcp_pcb::keep_idle, netconn::last_err, lwip_setgetsockopt_data::level, LWIP_ASSERT, LWIP_DEBUGF, NETCONN_RAW, NETCONN_TCP, NETCONN_UDP, NETCONNTYPE_GROUP, NULL, netconn::op_completed, lwip_setgetsockopt_data::optname, lwip_setgetsockopt_data::optval, netconn::pcb, SO_ACCEPTCONN, SO_BROADCAST, SO_ERROR, SO_KEEPALIVE, SO_NO_CHECK, SO_RCVBUF, SO_RCVTIMEO, SO_REUSEADDR, SO_REUSEPORT, SO_TYPE, lwip_setgetsockopt_data::sock, SOCK_DGRAM, SOCK_RAW, sock_set_errno, SOCK_STREAM, SOCKETS_DEBUG, SOL_SOCKET, sys_sem_signal(), netconn::tcp, TCP_KEEPALIVE, TCP_KEEPCNT, TCP_KEEPIDLE, TCP_KEEPINTVL, tcp_nagle_disabled, TCP_NODELAY, netconn::type, netconn::udp, and X32_F.
Referenced by lwip_getsockopt().
int lwip_ioctl | ( | int | s, |
long | cmd, | ||
void * | argp | ||
) |
int lwip_listen | ( | int | s, |
int | backlog | ||
) |
Set a socket into listen mode.
The socket may not have been used for another connection previously.
s | the socket to set to listening mode |
backlog | (ATTENTION: needs TCP_LISTEN_BACKLOG=1) |
References lwip_sock::conn, lwip_sock::err, ERR_OK, err_to_errno, get_socket(), LWIP_DEBUGF, LWIP_MAX, LWIP_MIN, netconn_listen_with_backlog(), sock_set_errno, and SOCKETS_DEBUG.
int lwip_read | ( | int | s, |
void * | mem, | ||
size_t | len | ||
) |
References lwip_recvfrom(), and NULL.
int lwip_recv | ( | int | s, |
void * | mem, | ||
size_t | len, | ||
int | flags | ||
) |
References lwip_recvfrom(), and NULL.
int lwip_recvfrom | ( | int | s, |
void * | mem, | ||
size_t | len, | ||
int | flags, | ||
struct sockaddr * | from, | ||
socklen_t * | fromlen | ||
) |
References AF_INET, lwip_sock::conn, ERR_CLSD, ERR_OK, err_to_errno, EWOULDBLOCK, pbuf::flags, get_socket(), htons, inet_addr_from_ipaddr, ip_addr_debug_print, lwip_sock::lastdata, lwip_sock::lastoffset, LWIP_ASSERT, LWIP_DEBUGF, lwip_strerr, MEMCPY, MSG_DONTWAIT, MSG_PEEK, netbuf_delete(), netbuf_fromaddr, netbuf_fromport, netconn_getaddr(), netconn_is_nonblocking, netconn_recv(), netconn_recv_tcp_pbuf(), netconn_recved(), NETCONN_TCP, NULL, pbuf_copy_partial(), PBUF_FLAG_PUSH, pbuf_free(), lwip_sock::rcvevent, sockaddr_in::sin_addr, sockaddr_in::sin_family, sockaddr_in::sin_len, sockaddr_in::sin_port, sock_set_errno, SOCKETS_DEBUG, SZT_F, pbuf::tot_len, and U16_F.
Referenced by lwip_read(), and lwip_recv().
int lwip_select | ( | int | maxfdp1, |
fd_set * | readset, | ||
fd_set * | writeset, | ||
fd_set * | exceptset, | ||
struct timeval * | timeout | ||
) |
Processing exceptset is not yet implemented.
References ENOMEM, ERR_OK, lwip_select_cb::exceptset, FD_ISSET, LWIP_ASSERT, LWIP_DEBUGF, lwip_selscan(), lwip_select_cb::next, NULL, lwip_select_cb::prev, lwip_select_cb::readset, S32_F, select_cb_ctr, select_cb_list, lwip_sock::select_waiting, lwip_select_cb::sem, lwip_select_cb::sem_signalled, set_errno, SOCKETS_DEBUG, SYS_ARCH_DECL_PROTECT(), SYS_ARCH_PROTECT(), sys_arch_sem_wait(), SYS_ARCH_TIMEOUT, SYS_ARCH_UNPROTECT(), sys_sem_free(), sys_sem_new(), tryget_socket(), and lwip_select_cb::writeset.
|
static |
Go through the readset and writeset lists and see which socket of the sockets set in the sets has events.
On return, readset, writeset and exceptset have the sockets enabled that had events.
exceptset is not used for now!!!
maxfdp1 | the highest socket index in the sets |
readset_in,: | set of sockets to check for read events |
writeset_in,: | set of sockets to check for write events |
exceptset_in,: | set of sockets to check for error events |
readset_out,: | set of sockets that had read events |
writeset_out,: | set of sockets that had write events |
exceptset_out,: | set os sockets that had error events |
References lwip_sock::errevent, FD_ISSET, FD_SET, FD_ZERO, lwip_sock::lastdata, LWIP_ASSERT, LWIP_DEBUGF, NULL, lwip_sock::rcvevent, lwip_sock::sendevent, SOCKETS_DEBUG, SYS_ARCH_DECL_PROTECT(), SYS_ARCH_PROTECT(), SYS_ARCH_UNPROTECT(), and tryget_socket().
Referenced by lwip_select().
int lwip_send | ( | int | s, |
const void * | data, | ||
size_t | size, | ||
int | flags | ||
) |
References lwip_sock::conn, EMSGSIZE, lwip_sock::err, ERR_ARG, ERR_OK, err_to_errno, get_socket(), LWIP_DEBUGF, lwip_sendto(), MSG_DONTWAIT, MSG_MORE, NETCONN_COPY, NETCONN_DONTBLOCK, netconn_is_nonblocking, NETCONN_MORE, NETCONN_TCP, netconn_write(), NULL, sock_set_errno, SOCKETS_DEBUG, SZT_F, TCP_MSS, TCP_SND_BUF, TCP_SND_QUEUELEN, and netconn::type.
Referenced by lwip_sendto(), and lwip_write().
int lwip_sendto | ( | int | s, |
const void * | data, | ||
size_t | size, | ||
int | flags, | ||
const struct sockaddr * | to, | ||
socklen_t | tolen | ||
) |
References netbuf::addr, AF_INET, lwip_sock::err, ERR_ARG, ERR_MEM, ERR_OK, err_to_errno, get_socket(), inet_addr_to_ipaddr, inet_addr_to_ipaddr_p, ip_addr_debug_print, ip_addr_set_any, LOCK_TCPIP_CORE, LWIP_ASSERT, LWIP_DEBUGF, LWIP_ERROR(), lwip_send(), LWIP_UNUSED_ARG, MEMCPY, netbuf_alloc(), netbuf_free(), netbuf_fromport, netbuf_ref(), netbuf_take, NETCONN_RAW, netconn_send(), NETCONN_TCP, ntohs, NULL, netbuf::p, pbuf::payload, pbuf_alloc(), pbuf_free(), PBUF_RAM, PBUF_REF, PBUF_TRANSPORT, netbuf::ptr, sockaddr::sa_family, sock_set_errno, SOCKETS_DEBUG, U16_F, and UNLOCK_TCPIP_CORE.
Referenced by lwip_send().
References lwip_sock::conn, EAFNOSUPPORT, EFAULT, EINVAL, ENOPROTOOPT, lwip_setgetsockopt_data::err, ERR_OK, get_socket(), IP_TOS, IP_TTL, IPPROTO_IP, IPPROTO_TCP, IPPROTO_UDPLITE, lwip_setgetsockopt_data::level, LWIP_DEBUGF, lwip_setsockopt_internal(), NETCONN_TCP, NETCONN_UDP, NETCONN_UDPLITE, NETCONNTYPE_GROUP, NULL, netconn::op_completed, lwip_setgetsockopt_data::optlen, lwip_setgetsockopt_data::optname, lwip_setgetsockopt_data::optval, netconn::pcb, SO_BROADCAST, SO_KEEPALIVE, SO_NO_CHECK, SO_RCVBUF, SO_RCVTIMEO, SO_REUSEADDR, SO_REUSEPORT, lwip_setgetsockopt_data::sock, sock_set_errno, SOCKETS_DEBUG, SOL_SOCKET, sys_arch_sem_wait(), TCP_KEEPALIVE, TCP_KEEPCNT, TCP_KEEPIDLE, TCP_KEEPINTVL, TCP_NODELAY, tcpip_callback, netconn::type, and netconn::udp.
References lwip_sock::conn, EADDRNOTAVAIL, lwip_setgetsockopt_data::err, ERR_OK, inet_addr_to_ipaddr, netconn::ip, IP_TOS, IP_TTL, IPPROTO_IP, IPPROTO_TCP, IPPROTO_UDPLITE, tcp_pcb::keep_idle, lwip_setgetsockopt_data::level, LWIP_ASSERT, LWIP_DEBUGF, NULL, netconn::op_completed, lwip_setgetsockopt_data::optname, lwip_setgetsockopt_data::optval, netconn::pcb, SO_BROADCAST, SO_KEEPALIVE, SO_NO_CHECK, SO_RCVBUF, SO_RCVTIMEO, SO_REUSEADDR, SO_REUSEPORT, lwip_setgetsockopt_data::sock, SOCKETS_DEBUG, SOL_SOCKET, sys_sem_signal(), netconn::tcp, TCP_KEEPALIVE, TCP_KEEPCNT, TCP_KEEPIDLE, TCP_KEEPINTVL, tcp_nagle_disable, tcp_nagle_enable, TCP_NODELAY, U32_F, and netconn::udp.
Referenced by lwip_setsockopt().
int lwip_shutdown | ( | int | s, |
int | how | ||
) |
Unimplemented: Close one end of a full-duplex connection.
Currently, the full connection is closed.
References lwip_sock::conn, EINVAL, ENOTCONN, EOPNOTSUPP, lwip_sock::err, ERR_OK, err_to_errno, get_socket(), LWIP_DEBUGF, netconn_shutdown(), NETCONN_TCP, NULL, SHUT_RD, SHUT_RDWR, SHUT_WR, sock_set_errno, and SOCKETS_DEBUG.
int lwip_socket | ( | int | domain, |
int | type, | ||
int | protocol | ||
) |
References alloc_socket(), EINVAL, ENFILE, ENOBUFS, event_callback(), IPPROTO_UDPLITE, LWIP_DEBUGF, LWIP_UNUSED_ARG, netconn_delete(), netconn_new_with_callback, netconn_new_with_proto_and_callback(), NETCONN_RAW, netconn_set_noautorecved, NETCONN_TCP, NETCONN_UDP, NETCONN_UDPLITE, NULL, PF_INET, set_errno, SOCK_DGRAM, SOCK_RAW, SOCK_STREAM, netconn::socket, and SOCKETS_DEBUG.
Initialize this module.
LWIP_TIMEVAL_PRIVATE: if you want to use the struct timeval provided by your system, set this to 0 and include <sys/time.h> in cc.h.
This function has to be called before any other functions in this module!
Referenced by lwip_init().
int lwip_write | ( | int | s, |
const void * | data, | ||
size_t | size | ||
) |
References lwip_send().
|
static |
Same as get_socket but doesn't set errno.
s | externally used socket index |
References lwip_sock::conn, NULL, NUM_SOCKETS, and sockets.
Referenced by lwip_select(), and lwip_selscan().
|
static |
Table to quickly map an lwIP error (err_t) to a socket error by using -err as an index.
|
static |
This counter is increased from lwip_select when the list is chagned and checked in event_callback to see if it has changed.
Referenced by event_callback(), and lwip_select().
|
static |
The global list of tasks waiting for select.
Referenced by lwip_select().
|
static |
The global array of available sockets.
Referenced by alloc_socket(), get_socket(), lwip_accept(), and tryget_socket().