Pharos 0.7.23
Modern Web Framework & Web App Hosting Service.
Loading...
Searching...
No Matches
native_connection.c File Reference

Wraps accepted sockets with transport metadata and provides connection IO and lifecycle helpers. More...

#include "native_connection.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/un.h>
#include <unistd.h>

Go to the source code of this file.

Macros

#define native_connection_close_socket   close
#define native_connection_shutdown_socket(fd)
#define native_connection_recv(fd, buffer, len)
#define native_connection_send(fd, buffer, len)
#define NATIVE_CONNECTION_IO_TIMEOUT_SECONDS   15
#define native_connection_strdup   strdup

Functions

static char * native_connection_format_sockaddr (const struct sockaddr *addr, socklen_t addr_len)
static NativeTransportType native_connection_detect_transport (int fd)
static void native_connection_apply_timeouts (int fd)
static void native_connection_populate_endpoint_info (NativeConnection *connection)
void native_connection_init (NativeConnection *connection)
 Initializes a native connection record to an empty state.
void native_connection_reset (NativeConnection *connection)
 Releases owned transport metadata and resets a connection record.
int native_connection_accept_next (int server_fd, volatile sig_atomic_t *shutdown_requested, NativeConnection *connection)
 Accepts the next client connection and records its transport metadata.
int native_connection_read (NativeConnection *connection, void *buffer, size_t len)
 Reads bytes from a native connection.
int native_connection_write (NativeConnection *connection, const void *buffer, size_t len)
 Writes bytes to a native connection.
int native_connection_flush (NativeConnection *connection)
 Flushes any buffered connection output when the transport requires it.
int native_connection_close (NativeConnection *connection)
 Closes a native connection and releases its owned metadata.
int native_connection_shutdown (NativeConnection *connection)
 Shuts down a native connection without discarding the record itself.
const char * native_connection_peer_info (const NativeConnection *connection)
 Returns the printable remote endpoint description for a connection.
const char * native_connection_local_info (const NativeConnection *connection)
 Returns the printable local endpoint description for a connection.
NativeTransportType native_connection_transport_type (const NativeConnection *connection)
 Returns the resolved transport type for a connection.
const char * native_connection_transport_type_name (const NativeConnection *connection)
 Returns a human-readable transport type name for a connection.

Detailed Description

Wraps accepted sockets with transport metadata and provides connection IO and lifecycle helpers.

Definition in file native_connection.c.

Macro Definition Documentation

◆ native_connection_close_socket

#define native_connection_close_socket   close

Definition at line 26 of file native_connection.c.

Referenced by native_connection_accept_next(), and native_connection_close().

◆ NATIVE_CONNECTION_IO_TIMEOUT_SECONDS

#define NATIVE_CONNECTION_IO_TIMEOUT_SECONDS   15

Definition at line 32 of file native_connection.c.

Referenced by native_connection_apply_timeouts().

◆ native_connection_recv

#define native_connection_recv ( fd,
buffer,
len )
Value:
recv((fd), (buffer), (len), 0)
const char size_t len
Definition yyjson.h:8364

Definition at line 28 of file native_connection.c.

Referenced by native_connection_read().

◆ native_connection_send

#define native_connection_send ( fd,
buffer,
len )
Value:
send((fd), (buffer), (len), 0)

Definition at line 29 of file native_connection.c.

Referenced by native_connection_write().

◆ native_connection_shutdown_socket

#define native_connection_shutdown_socket ( fd)
Value:
shutdown((fd), SHUT_RDWR)

Definition at line 27 of file native_connection.c.

Referenced by native_connection_shutdown().

◆ native_connection_strdup

#define native_connection_strdup   strdup

Function Documentation

◆ native_connection_accept_next()

int native_connection_accept_next ( int server_fd,
volatile sig_atomic_t * shutdown_requested,
NativeConnection * connection )

Accepts the next client connection and records its transport metadata.

Parameters
server_fdListening socket descriptor.
shutdown_requestedShared shutdown flag checked while accepting.
connectionDestination connection record.
Returns
Positive when a client was accepted, 0 when shutdown was requested, or a negative value on error.

Definition at line 148 of file native_connection.c.

References NativeConnection::fd, native_connection_apply_timeouts(), native_connection_close_socket, native_connection_detect_transport(), native_connection_populate_endpoint_info(), native_connection_reset(), and NativeConnection::transport_type.

Referenced by accept_http_request_native().

◆ native_connection_apply_timeouts()

void native_connection_apply_timeouts ( int fd)
static

Definition at line 84 of file native_connection.c.

References NATIVE_CONNECTION_IO_TIMEOUT_SECONDS.

Referenced by native_connection_accept_next().

◆ native_connection_close()

int native_connection_close ( NativeConnection * connection)

Closes a native connection and releases its owned metadata.

Parameters
connectionConnection to close.
Returns
0 on success, or a negative value on error.

Definition at line 207 of file native_connection.c.

References NativeConnection::fd, native_connection_close_socket, and native_connection_reset().

Referenced by accept_http_request_native(), build_worker_context_native(), free_worker_context_native(), serve_runtime_stack_native(), serve_single_app(), and serve_static_root().

◆ native_connection_detect_transport()

NativeTransportType native_connection_detect_transport ( int fd)
static

◆ native_connection_flush()

int native_connection_flush ( NativeConnection * connection)

Flushes any buffered connection output when the transport requires it.

Parameters
connectionConnection to flush.
Returns
0 on success, or a negative value on error.

Definition at line 202 of file native_connection.c.

◆ native_connection_format_sockaddr()

char * native_connection_format_sockaddr ( const struct sockaddr * addr,
socklen_t addr_len )
static

Definition at line 40 of file native_connection.c.

References native_connection_strdup.

Referenced by native_connection_populate_endpoint_info().

◆ native_connection_init()

void native_connection_init ( NativeConnection * connection)

Initializes a native connection record to an empty state.

Parameters
connectionConnection record to initialize.

Definition at line 126 of file native_connection.c.

References NativeConnection::fd, NativeConnection::local_info, NATIVE_TRANSPORT_UNKNOWN, NativeConnection::peer_info, and NativeConnection::transport_type.

Referenced by build_worker_context_native(), serve_runtime_stack_native(), serve_single_app(), and serve_static_root().

◆ native_connection_local_info()

const char * native_connection_local_info ( const NativeConnection * connection)

Returns the printable local endpoint description for a connection.

Parameters
connectionConnection to inspect.
Returns
Pointer to owned local-info text, or NULL when unavailable.

Definition at line 230 of file native_connection.c.

References NativeConnection::local_info.

◆ native_connection_peer_info()

const char * native_connection_peer_info ( const NativeConnection * connection)

Returns the printable remote endpoint description for a connection.

Parameters
connectionConnection to inspect.
Returns
Pointer to owned peer-info text, or NULL when unavailable.

Definition at line 226 of file native_connection.c.

References NativeConnection::peer_info.

◆ native_connection_populate_endpoint_info()

void native_connection_populate_endpoint_info ( NativeConnection * connection)
static

◆ native_connection_read()

int native_connection_read ( NativeConnection * connection,
void * buffer,
size_t len )

Reads bytes from a native connection.

Parameters
connectionOpen connection to read from.
bufferDestination buffer.
lenMaximum number of bytes to read.
Returns
Number of bytes read, or a negative value on error.

Definition at line 180 of file native_connection.c.

References NativeConnection::fd, len, and native_connection_recv.

Referenced by native_spool_request_body_to_temp_file(), and parse_http_request_native().

◆ native_connection_reset()

void native_connection_reset ( NativeConnection * connection)

Releases owned transport metadata and resets a connection record.

Parameters
connectionConnection record to clear.

Definition at line 136 of file native_connection.c.

References NativeConnection::fd, NativeConnection::local_info, NATIVE_TRANSPORT_UNKNOWN, NativeConnection::peer_info, and NativeConnection::transport_type.

Referenced by native_connection_accept_next(), and native_connection_close().

◆ native_connection_shutdown()

int native_connection_shutdown ( NativeConnection * connection)

Shuts down a native connection without discarding the record itself.

Parameters
connectionConnection to shut down.
Returns
0 on success, or a negative value on error.

Definition at line 219 of file native_connection.c.

References NativeConnection::fd, and native_connection_shutdown_socket.

◆ native_connection_transport_type()

NativeTransportType native_connection_transport_type ( const NativeConnection * connection)

Returns the resolved transport type for a connection.

Parameters
connectionConnection to inspect.
Returns
Transport type recorded for the connection.

Definition at line 234 of file native_connection.c.

References NATIVE_TRANSPORT_UNKNOWN, and NativeConnection::transport_type.

Referenced by native_connection_transport_type_name().

◆ native_connection_transport_type_name()

const char * native_connection_transport_type_name ( const NativeConnection * connection)

Returns a human-readable transport type name for a connection.

Parameters
connectionConnection to inspect.
Returns
Static transport type name string.

Definition at line 238 of file native_connection.c.

References native_connection_transport_type(), NATIVE_TRANSPORT_TCP, and NATIVE_TRANSPORT_UNIX.

◆ native_connection_write()

int native_connection_write ( NativeConnection * connection,
const void * buffer,
size_t len )

Writes bytes to a native connection.

Parameters
connectionOpen connection to write to.
bufferSource bytes to send.
lenNumber of bytes to send.
Returns
Number of bytes written, or a negative value on error.

Definition at line 187 of file native_connection.c.

References NativeConnection::fd, len, and native_connection_send.

Referenced by handle_app_or_runtime_request(), native_connection_printf(), native_dynamic_media_serve_asset(), and native_serve_static_file().