cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
lwip.c File Reference

CWIST over lwIP's raw TCP API; see <cwist/sys/port/lwip.h>. More...

#include <cwist/sys/port/lwip.h>
#include <cwist/core/mem/alloc.h>
#include <cwist/net/http/http.h>
#include "lwip/opt.h"
#include "lwip/sys.h"
#include "lwip/tcp.h"
#include <stdio.h>
#include <string.h>
#include <strings.h>
Include dependency graph for lwip.c:

Go to the source code of this file.

Macros

#define CWIST_LWIP_POLL_TICKS   2
 

Typedefs

typedef struct cwist_lwip_conn cwist_lwip_conn
 

Enumerations

enum  frame_result { FRAME_NEED_MORE , FRAME_COMPLETE , FRAME_ERROR }
 

Functions

void cwist_lwip_config_init (cwist_lwip_config *cfg)
 Fill cfg with the defaults documented on cwist_lwip_config.
 
cwist_lwip_server * cwist_lwip_listen (cwist_app *app, const cwist_lwip_config *cfg)
 Bind a listening tcp_pcb on cfg->port and serve app on it.
 
void cwist_lwip_close (cwist_lwip_server *srv)
 Stop listening, abort every open connection, and free srv.
 
size_t cwist_lwip_active_connections (const cwist_lwip_server *srv)
 Number of connections the server currently holds open.
 
cwist_lwip_tls * cwist_lwip_tls_create (const unsigned char *cert, size_t cert_len, const unsigned char *key, size_t key_len)
 Create a TLS server context from a certificate chain and private key.
 
void cwist_lwip_tls_destroy (cwist_lwip_tls *tls)
 Free a TLS context; close every server using it first.
 

Detailed Description

CWIST over lwIP's raw TCP API; see <cwist/sys/port/lwip.h>.

Not part of libcwist: the firmware compiles this file next to lwIP so it picks up the firmware's lwipopts.h. Uses only public CWIST API (parse, dispatch, serialize), which keeps it independent of the socket runtime.

One connection runs one exchange at a time:

recv -> rx buffer -> frame (Content-Length) -> parse -> dispatch -> serialize into tx -> tcp_write as tcp_sndbuf allows -> tcp_sent acks drain tx -> next pipelined request or close

tx is written without TCP_WRITE_FLAG_COPY, so lwIP references it until the peer acknowledges; it is freed only once tx_acked reaches tx_len.

With CWIST_LWIP_TLS and cfg.tls set, mbedTLS sits between the pcb and the same rx/tx buffers: received ciphertext queues in tin and is decrypted into rx by the pump; tx goes through mbedtls_ssl_write, whose records are copied into lwIP (TCP_WRITE_FLAG_COPY) by the BIO, so tx can be freed as soon as mbedTLS has taken all of it. The next pipelined request is then picked up from tcp_sent, as on the plaintext path.

Definition in file lwip.c.

Macro Definition Documentation

◆ CWIST_LWIP_POLL_TICKS

#define CWIST_LWIP_POLL_TICKS   2

Definition at line 58 of file lwip.c.

Typedef Documentation

◆ cwist_lwip_conn

typedef struct cwist_lwip_conn cwist_lwip_conn

Definition at line 62 of file lwip.c.

Enumeration Type Documentation

◆ frame_result

Enumerator
FRAME_NEED_MORE 
FRAME_COMPLETE 
FRAME_ERROR 

Definition at line 113 of file lwip.c.

Function Documentation

◆ cwist_lwip_active_connections()

size_t cwist_lwip_active_connections ( const cwist_lwip_server *  srv)

Number of connections the server currently holds open.

Parameters
srvServer handle.
Returns
Open connection count (0 for NULL).

Definition at line 791 of file lwip.c.

◆ cwist_lwip_close()

void cwist_lwip_close ( cwist_lwip_server *  srv)

Stop listening, abort every open connection, and free srv.

Stop listening and abort every open connection, then free srv.

Parameters
srvServer returned by cwist_lwip_listen(); NULL is a no-op.

Definition at line 774 of file lwip.c.

References cap, and cwist_free().

◆ cwist_lwip_config_init()

void cwist_lwip_config_init ( cwist_lwip_config *  cfg)

◆ cwist_lwip_listen()

cwist_lwip_server * cwist_lwip_listen ( cwist_app *  app,
const cwist_lwip_config *  cfg 
)

Bind a listening tcp_pcb on cfg->port and serve app on it.

Bind a listening tcp_pcb on cfg->port (any address) and serve app on it.

Parameters
appApplication to dispatch to; must outlive the server.
cfgSettings, copied; NULL means the defaults.
Returns
Server handle, or NULL on pcb or allocation failure.

Definition at line 729 of file lwip.c.

References cap, cwist_alloc(), cwist_free(), and cwist_lwip_config_init().

◆ cwist_lwip_tls_create()

cwist_lwip_tls * cwist_lwip_tls_create ( const unsigned char *  cert,
size_t  cert_len,
const unsigned char *  key,
size_t  key_len 
)

Create a TLS server context from a certificate chain and private key.

Parameters
certCertificate chain, leaf first (PEM with its NUL counted, or DER).
cert_lenLength of cert.
keyMatching private key (PEM with its NUL counted, or DER).
key_lenLength of key.
Returns
Context, or NULL on parse/pairing/seeding/allocation failure or when built without CWIST_LWIP_TLS.

Definition at line 804 of file lwip.c.

References cap, cwist_alloc(), and cwist_lwip_tls_destroy().

◆ cwist_lwip_tls_destroy()

void cwist_lwip_tls_destroy ( cwist_lwip_tls *  tls)

Free a TLS context; close every server using it first.

Free a TLS context. Close every server using it first.

Parameters
tlsContext from cwist_lwip_tls_create(); NULL is a no-op.

Definition at line 841 of file lwip.c.

References cap, and cwist_free().

Referenced by cwist_lwip_tls_create().