Microchip® Advanced Software Framework

sockets.c File Reference

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_sockget_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_socktryget_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_cbselect_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)
Value:
((unsigned)(-(err)) < ERR_TO_ERRNO_TABLE_SIZE ? \
#define EIO
Definition: arch.h:83
#define ERR_TO_ERRNO_TABLE_SIZE
Definition: sockets.c:154
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.
Definition: sockets.c:135

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
#define set_errno (   err)
#define sock_set_errno (   sk,
 
)
Value:
do { \
sk->err = (e); \
set_errno(sk->err); \
} while (0)
#define set_errno(err)
Definition: sockets.c:166

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 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

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 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

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 struct lwip_sock* get_socket ( int  s)
static

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

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

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

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.

int lwip_getpeername ( int  s,
struct sockaddr name,
socklen_t namelen 
)

References lwip_getaddrname().

int lwip_getsockname ( int  s,
struct sockaddr name,
socklen_t namelen 
)

References lwip_getaddrname().

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

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.

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.

exceptset is not used for now!!!

Parameters
maxfdp1the 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
Returns
number of sockets that had events (read/write/exception) (>= 0)

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_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.

void lwip_socket_init ( void  )

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 struct lwip_sock* tryget_socket ( int  s)
static

Same as get_socket but doesn't set errno.

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

References lwip_sock::conn, NULL, NUM_SOCKETS, and sockets.

Referenced by lwip_select(), and lwip_selscan().

const int err_to_errno_table[]
static
Initial value:
= {
0,
EIO,
-1,
}
#define EINVAL
Definition: arch.h:100
#define EIO
Definition: arch.h:83
#define ECONNABORTED
Definition: arch.h:183
#define ENOTCONN
Definition: arch.h:187
#define ENOBUFS
Definition: arch.h:185
#define EWOULDBLOCK
Definition: arch.h:119
#define EALREADY
Definition: arch.h:194
#define ECONNRESET
Definition: arch.h:184
#define ENOMEM
Definition: arch.h:90
#define EHOSTUNREACH
Definition: arch.h:193
#define EINPROGRESS
Definition: arch.h:195
#define EADDRINUSE
Definition: arch.h:178

Table to quickly map an lwIP error (err_t) to a socket error by using -err as an index.

volatile int select_cb_ctr
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().

struct lwip_select_cb* select_cb_list
static

The global list of tasks waiting for select.

Referenced by lwip_select().

struct lwip_sock sockets[NUM_SOCKETS]
static

The global array of available sockets.

Referenced by alloc_socket(), get_socket(), lwip_accept(), and tryget_socket().