Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

lwip_sockets.c File Reference

lwip_sockets.c File Reference

Sockets BSD-Like API module. More...

Go to the source code of this file.

Enumerations

enum  lwip_pollscan_opts { LWIP_POLLSCAN_CLEAR = 1, LWIP_POLLSCAN_INC_WAIT = 2, LWIP_POLLSCAN_DEC_WAIT = 4 }
 

Options for the lwip_pollscan function.

More...

Functions

static int lwip_socket_register_membership (int s, const ip4_addr_t *if_addr, const ip4_addr_t *multi_addr)
 Register a new IGMP membership.
static void lwip_socket_unregister_membership (int s, const ip4_addr_t *if_addr, const ip4_addr_t *multi_addr)
 Unregister a previously registered membership.
static void lwip_socket_drop_registered_memberships (int s)
 Drop all memberships of a socket that were not dropped explicitly via setsockopt.
static int lwip_socket_register_mld6_membership (int s, unsigned int if_idx, const ip6_addr_t *multi_addr)
 Register a new MLD6 membership.
static void lwip_socket_unregister_mld6_membership (int s, unsigned int if_idx, const ip6_addr_t *multi_addr)
 Unregister a previously registered MLD6 membership.
static void lwip_socket_drop_registered_mld6_memberships (int s)
 Drop all MLD6 memberships of a socket that were not dropped explicitly via setsockopt.
static void event_callback (struct netconn *conn, enum netconn_evt evt, u16_t len)
 Callback registered in the netconn layer for each socket-netconn.
static void select_check_waiters (int s, int has_recvevent, int has_sendevent, int has_errevent)
 Check if any select waiters are waiting on this socket and its events.
static void lwip_getsockopt_callback (void *arg)
 lwip_getsockopt_callback: only used without CORE_LOCKING to get into the tcpip_thread
static void lwip_setsockopt_callback (void *arg)
 lwip_setsockopt_callback: only used without CORE_LOCKING to get into the tcpip_thread
static int lwip_getsockopt_impl (int s, int level, int optname, void *optval, socklen_t *optlen)
 lwip_getsockopt_impl: the actual implementation of getsockopt: same argument as lwip_getsockopt, either called directly or through callback
static int lwip_setsockopt_impl (int s, int level, int optname, const void *optval, socklen_t optlen)
 lwip_setsockopt_impl: the actual implementation of setsockopt: same argument as lwip_setsockopt, either called directly or through callback
static int free_socket_locked (struct lwip_sock *sock, int is_tcp, struct netconn **conn, union lwip_sock_lastdata *lastdata)
 Free a socket (under lock)
static void free_socket_free_elements (int is_tcp, struct netconn *conn, union lwip_sock_lastdata *lastdata)
 Free a socket's leftover members.
void lwip_socket_thread_init (void)
 LWIP_NETCONN_SEM_PER_THREAD==1: initialize thread-local semaphore.
void lwip_socket_thread_cleanup (void)
 LWIP_NETCONN_SEM_PER_THREAD==1: destroy thread-local semaphore.
static struct lwip_socktryget_socket (int fd)
 Same as get_socket but doesn't set errno.
static struct lwip_sockget_socket (int fd)
 Map a externally used socket index to the internal socket representation.
static int alloc_socket (struct netconn *newconn, int accepted)
 Allocate a new socket for a given netconn.
static void free_socket (struct lwip_sock *sock, int is_tcp)
 Free a socket.
int lwip_listen (int s, int backlog)
 Set a socket into listen mode.
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.
static int lwip_pollscan (struct pollfd *fds, nfds_t nfds, enum lwip_pollscan_opts opts)
 Update revents in each struct pollfd.
static int lwip_poll_should_wake (const struct lwip_select_cb *scb, int fd, int has_recvevent, int has_sendevent, int has_errevent)
 Check whether event_callback should wake up a thread waiting in lwip_poll.
int lwip_shutdown (int s, int how)
 Close one end of a full-duplex connection.
int lwip_fcntl (int s, int cmd, int val)
 A minimal implementation of fcntl.

Variables

static struct lwip_sock sockets [NUM_SOCKETS]
 The global array of available sockets.
static volatile int select_cb_ctr
 This counter is increased from lwip_select when the list is changed and checked in select_check_waiters to see if it has changed.
static struct lwip_select_cbselect_cb_list
 The global list of tasks waiting for select.

Detailed Description

Sockets BSD-Like API module.

Definition in file lwip_sockets.c.


Enumeration Type Documentation

Options for the lwip_pollscan function.

Enumerator:
LWIP_POLLSCAN_CLEAR 

Clear revents in each struct pollfd.

LWIP_POLLSCAN_INC_WAIT 

Increment select_waiting in each struct lwip_sock.

LWIP_POLLSCAN_DEC_WAIT 

Decrement select_waiting in each struct lwip_sock.

Definition at line 2171 of file lwip_sockets.c.


Function Documentation

static int alloc_socket ( struct netconn newconn,
int  accepted 
) [static]

Allocate a new socket for a given netconn.

Parameters:
newconnthe netconn for which to allocate a socket
accepted1 if socket has been created by accept(), 0 if socket has been created by socket()
Returns:
the index of the new socket; -1 on error

Definition at line 501 of file lwip_sockets.c.

static void event_callback ( struct netconn conn,
enum netconn_evt  evt,
u16_t  len 
) [static]

Callback registered in the netconn layer for each socket-netconn.

Processes recvevent (data available) and wakes up tasks waiting for select.

Note:
for LWIP_TCPIP_CORE_LOCKING any caller of this function must have the core lock held when signaling the following events as they might cause select_list_cb to be checked: NETCONN_EVT_RCVPLUS NETCONN_EVT_SENDPLUS NETCONN_EVT_ERROR This requirement will be asserted in select_check_waiters()

Definition at line 2477 of file lwip_sockets.c.

static void free_socket ( struct lwip_sock sock,
int  is_tcp 
) [static]

Free a socket.

The socket's netconn must have been delete before!

Parameters:
sockthe socket to free
is_tcp!= 0 for TCP sockets, used to free lastdata

Definition at line 594 of file lwip_sockets.c.

static void free_socket_free_elements ( int  is_tcp,
struct netconn conn,
union lwip_sock_lastdata *  lastdata 
) [static]

Free a socket's leftover members.

Definition at line 572 of file lwip_sockets.c.

static int free_socket_locked ( struct lwip_sock sock,
int  is_tcp,
struct netconn **  conn,
union lwip_sock_lastdata *  lastdata 
) [static]

Free a socket (under lock)

Parameters:
sockthe socket to free
is_tcp!= 0 for TCP sockets, used to free lastdata
connthe socekt's netconn is stored here, must be freed externally
lastdatalastdata is stored here, must be freed externally

Definition at line 548 of file lwip_sockets.c.

static struct lwip_sock* get_socket ( int  fd ) [static, read]

Map a externally used socket index to the internal socket representation.

Parameters:
fdexternally used socket index
Returns:
struct lwip_sock for the socket or NULL if not found

Definition at line 479 of file lwip_sockets.c.

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. The flag O_NONBLOCK and access modes are supported for F_GETFL, only the flag O_NONBLOCK is implemented for F_SETFL.

Definition at line 3836 of file lwip_sockets.c.

static void lwip_getsockopt_callback ( void *  arg ) [static]

lwip_getsockopt_callback: only used without CORE_LOCKING to get into the tcpip_thread

Definition at line 2840 of file lwip_sockets.c.

static int lwip_getsockopt_impl ( int  s,
int  level,
int  optname,
void *  optval,
socklen_t *  optlen 
) [static]

lwip_getsockopt_impl: the actual implementation of getsockopt: same argument as lwip_getsockopt, either called directly or through callback

Definition at line 2882 of file lwip_sockets.c.

int lwip_listen ( int  s,
int  backlog 
)

Set a socket into listen mode.

The socket may not have been used for another connection previously.

Parameters:
sthe socket to set to listening mode
backlog(ATTENTION: needs TCP_LISTEN_BACKLOG=1)
Returns:
0 on success, non-zero on failure

Definition at line 881 of file lwip_sockets.c.

static int lwip_poll_should_wake ( const struct lwip_select_cb scb,
int  fd,
int  has_recvevent,
int  has_sendevent,
int  has_errevent 
) [static]

Check whether event_callback should wake up a thread waiting in lwip_poll.

Definition at line 2438 of file lwip_sockets.c.

static int lwip_pollscan ( struct pollfd *  fds,
nfds_t  nfds,
enum lwip_pollscan_opts  opts 
) [static]

Update revents in each struct pollfd.

Optionally update select_waiting in struct lwip_sock.

Parameters:
fdsarray of structures to update
nfdsnumber of structures in fds
optswhat to update and how
Returns:
number of structures that have revents != 0

Definition at line 2193 of file lwip_sockets.c.

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 
) [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.

Parameters:
maxfdp1the highest socket index in the sets
readset_inset of sockets to check for read events
writeset_inset of sockets to check for write events
exceptset_inset of sockets to check for error events
readset_outset of sockets that had read events
writeset_outset of sockets that had write events
exceptset_outset os sockets that had error events
Returns:
number of sockets that had events (read/write/exception) (>= 0)

Definition at line 1835 of file lwip_sockets.c.

static void lwip_setsockopt_callback ( void *  arg ) [static]

lwip_setsockopt_callback: only used without CORE_LOCKING to get into the tcpip_thread

Definition at line 3281 of file lwip_sockets.c.

static int lwip_setsockopt_impl ( int  s,
int  level,
int  optname,
const void *  optval,
socklen_t  optlen 
) [static]

lwip_setsockopt_impl: the actual implementation of setsockopt: same argument as lwip_setsockopt, either called directly or through callback

Definition at line 3303 of file lwip_sockets.c.

int lwip_shutdown ( int  s,
int  how 
)

Close one end of a full-duplex connection.

Definition at line 2655 of file lwip_sockets.c.

static void lwip_socket_drop_registered_memberships ( int  s ) [static]

Drop all memberships of a socket that were not dropped explicitly via setsockopt.

ATTENTION: this function is NOT called from tcpip_thread (or under CORE_LOCK).

Definition at line 4044 of file lwip_sockets.c.

static void lwip_socket_drop_registered_mld6_memberships ( int  s ) [static]

Drop all MLD6 memberships of a socket that were not dropped explicitly via setsockopt.

ATTENTION: this function is NOT called from tcpip_thread (or under CORE_LOCK).

Definition at line 4132 of file lwip_sockets.c.

static int lwip_socket_register_membership ( int  s,
const ip4_addr_t if_addr,
const ip4_addr_t multi_addr 
) [static]

Register a new IGMP membership.

On socket close, the membership is dropped automatically.

ATTENTION: this function is called from tcpip_thread (or under CORE_LOCK).

Returns:
1 on success, 0 on failure

Definition at line 3989 of file lwip_sockets.c.

static int lwip_socket_register_mld6_membership ( int  s,
unsigned int  if_idx,
const ip6_addr_t multi_addr 
) [static]

Register a new MLD6 membership.

On socket close, the membership is dropped automatically.

ATTENTION: this function is called from tcpip_thread (or under CORE_LOCK).

Returns:
1 on success, 0 on failure

Definition at line 4077 of file lwip_sockets.c.

void lwip_socket_thread_cleanup ( void   )

LWIP_NETCONN_SEM_PER_THREAD==1: destroy thread-local semaphore.

Definition at line 327 of file lwip_sockets.c.

void lwip_socket_thread_init ( void   )

LWIP_NETCONN_SEM_PER_THREAD==1: initialize thread-local semaphore.

Definition at line 320 of file lwip_sockets.c.

static void lwip_socket_unregister_membership ( int  s,
const ip4_addr_t if_addr,
const ip4_addr_t multi_addr 
) [static]

Unregister a previously registered membership.

This prevents dropping the membership on socket close.

ATTENTION: this function is called from tcpip_thread (or under CORE_LOCK).

Definition at line 4017 of file lwip_sockets.c.

static void lwip_socket_unregister_mld6_membership ( int  s,
unsigned int  if_idx,
const ip6_addr_t multi_addr 
) [static]

Unregister a previously registered MLD6 membership.

This prevents dropping the membership on socket close.

ATTENTION: this function is called from tcpip_thread (or under CORE_LOCK).

Definition at line 4105 of file lwip_sockets.c.

static void select_check_waiters ( int  s,
int  has_recvevent,
int  has_sendevent,
int  has_errevent 
) [static]

Check if any select waiters are waiting on this socket and its events.

Note:
on synchronization of select_cb_list: LWIP_TCPIP_CORE_LOCKING: the select_cb_list must only be accessed while holding the core lock. We do a single pass through the list and signal any waiters. Core lock should already be held when calling here!!!!

!LWIP_TCPIP_CORE_LOCKING: we use SYS_ARCH_PROTECT but unlock on each iteration of the loop, thus creating a possibility where a thread could modify the select_cb_list during our UNPROTECT/PROTECT. We use a generational counter to detect this change and restart the list walk. The list is expected to be small

Definition at line 2576 of file lwip_sockets.c.

static struct lwip_sock* tryget_socket ( int  fd ) [static, read]

Same as get_socket but doesn't set errno.

Parameters:
fdexternally used socket index
Returns:
struct lwip_sock for the socket or NULL if not found

Definition at line 460 of file lwip_sockets.c.


Variable Documentation

volatile int select_cb_ctr [static]

This counter is increased from lwip_select when the list is changed and checked in select_check_waiters to see if it has changed.

Definition at line 275 of file lwip_sockets.c.

struct lwip_select_cb* select_cb_list [static]

The global list of tasks waiting for select.

Definition at line 278 of file lwip_sockets.c.

struct lwip_sock sockets[NUM_SOCKETS] [static]

The global array of available sockets.

Definition at line 260 of file lwip_sockets.c.