This package includes the SharkSSL lite library and header files.

Dependents:   WebSocket-Client-Example SharkMQ-LED-Demo

Embed: (wiki syntax)

« Back to documentation index

SharkSSL Socket Example Lib

SharkSSL Socket Example Lib
[SharkExamples]

The SharkSSL Socket Example Lib (selib.h/selib.c) is a basic module that wraps the transport agnostic SharkSSL functions for encoding and decoding into functions that interface to TCP/IP socket calls. More...

Functions

int seSec_handshake (SharkSslCon *s, SOCKET *sock, U32 timeout, const char *commonName)
 Performs the initial SSL handshaking using an asymmetric cipher in order to establish cipher settings and a shared key for the session.
int seSec_read (SharkSslCon *s, SOCKET *sock, U8 **buf, U32 timeout)
 Read data from socket stream and decode the encrypted data.
int seSec_write (SharkSslCon *s, SOCKET *sock, U8 *buf, int maxLen)
 Encrypts and sends encrypted data to the connected peer side.
int se_connect (SOCKET *sock, const char *address, U16 port)
 Initializes a SOCKET object connected to a remote host/address at a given port.
int se_bind (SOCKET *sock, U16 port)
 Initializes a SOCKET object bound to a local port, ready to accept client connections.
int se_accept (SOCKET **listenSock, U32 timeout, SOCKET **outSock)
 Waits for remote connections on the server SOCKET object 'listenSock', initialized by function se_bind, and initializes socket object 'outSock' to represent the new connection.
void se_close (SOCKET *sock)
 Close a connected socket connection.
int se_sockValid (SOCKET *sock)
 Returns TRUE if socket is valid (connected).
S32 se_send (SOCKET *sock, const void *buf, U32 len)
 Sends data to the connected peer.
S32 se_recv (SOCKET *sock, void *buf, U32 len, U32 timeout)
 Waits for data sent by peer.
void _xprintf (const char *fmt,...)
 The example code and macro xprintf requires this function when the code is compiled with macro XPRINTF set to 1.
void mainTask (SeCtx *ctx)
 Main entry for all example programs.

Detailed Description

The SharkSSL Socket Example Lib (selib.h/selib.c) is a basic module that wraps the transport agnostic SharkSSL functions for encoding and decoding into functions that interface to TCP/IP socket calls.

![Socket Example Lib](SharkSSL-selibjpg)

In addition, the file selib.c includes wrappers for standard BSD socket calls at the end of the file. TCP/IP stacks not using the BSD API must define NO_BSD_SOCK and implement the following functions in a separate file: se_bind, se_accept, se_connect, se_close, se_sockValid, se_send, and se_recv.


Function Documentation

void _xprintf ( const char *  fmt,
  ... 
)

The example code and macro xprintf requires this function when the code is compiled with macro XPRINTF set to 1.

Parameters:
fmtthe format string.
...variable arguments.
void mainTask ( SeCtx *  ctx )

Main entry for all example programs.

int se_accept ( SOCKET **  listenSock,
U32  timeout,
SOCKET **  outSock 
)

Waits for remote connections on the server SOCKET object 'listenSock', initialized by function se_bind, and initializes socket object 'outSock' to represent the new connection.

Returns:
  • 1 Success
  • 0 timeout
  • -1 error

Definition at line 61 of file seLwIP.c.

int se_bind ( SOCKET *  sock,
U16  port 
)

Initializes a SOCKET object bound to a local port, ready to accept client connections.

Returns:
Zero on success. Error codes returned:
  • -1 Cannot create socket: Fatal
  • -2 Cannot listen: Fatal
  • -3 Cannot bind: socket in use

Definition at line 82 of file seLwIP.c.

void se_close ( SOCKET *  sock )

Close a connected socket connection.

Definition at line 132 of file seLwIP.c.

int se_connect ( SOCKET *  sock,
const char *  address,
U16  port 
)

Initializes a SOCKET object connected to a remote host/address at a given port.

Returns:
Zero on success. Error codes returned:
  • -1 Cannot create socket: Fatal
  • -2 Cannot resolve 'address'
  • -3 Cannot connect

Definition at line 110 of file seLwIP.c.

S32 se_recv ( SOCKET *  sock,
void *  buf,
U32  len,
U32  timeout 
)

Waits for data sent by peer.

Parameters:
sockthe SOCKET object.
bufis the data to send.
lenis the 'buf' length.
timeoutin milliseconds. The timeout can be set to INFINITE_TMO.
Returns:
the length of the data read, zero on timeout, or a negative value on error.

Definition at line 156 of file seLwIP.c.

S32 se_send ( SOCKET *  sock,
const void *  buf,
U32  len 
)

Sends data to the connected peer.

Definition at line 143 of file seLwIP.c.

int se_sockValid ( SOCKET *  sock )

Returns TRUE if socket is valid (connected).

Definition at line 193 of file seLwIP.c.

int seSec_handshake ( SharkSslCon s,
SOCKET *  sock,
U32  timeout,
const char *  commonName 
)

Performs the initial SSL handshaking using an asymmetric cipher in order to establish cipher settings and a shared key for the session.

The function also validates the server's certificate and compares the commonName provided in argument 3 with the common name in the certificate, if commonName is provided (not NULL). The domain name comparison works with and without the clone certificate API (SHARKSSL_ENABLE_CLONE_CERTINFO).

Note: the function only performs basic certificate domain name comparison and can only be used with server certificates that does not include Subject Alternative Names (SAN Certificate). Use the more advanced function SharkSslCon_trusted for certificate trust management if the server uses a SAN Certificate.

Parameters:
sthe SharkSslCon object.
sockthe SOCKET object.
timeoutin milliseconds. The timeout can be set to INFINITE_TMO.
commonNameis optional and is used for certificate domain name verification.
Returns:
  • A negative value on error. The negative value is an inverted SharkSslCon_RetVal value.
  • Zero on timeout.
  • The return value is SharkSslConTrust for any return value greater than zero.
int seSec_read ( SharkSslCon s,
SOCKET *  sock,
U8 **  buf,
U32  timeout 
)

Read data from socket stream and decode the encrypted data.

The buffer is managed by SharkSSL and the data returned is valid until the next SharkSSL call. This function blocks until data is available or until 'timeout' milliseconds.

Parameters:
sthe SharkSslCon object.
sockthe SOCKET object.
bufis a pointer set to the SharkSSL receive buffer offset to the WebSocket payload data.
timeoutin milliseconds. The timeout can be set to INFINITE_TMO.
Returns:
The function returns the number of bytes available in 'buf' on success. The function returns 0 on timeout and a negative value on error.
int seSec_write ( SharkSslCon s,
SOCKET *  sock,
U8 *  buf,
int  maxLen 
)

Encrypts and sends encrypted data to the connected peer side.

Returns:
Zero on success or a negative value on error.