mongoose/src/net.h

104 lines
4.8 KiB
C
Raw Normal View History

2020-12-05 11:26:32 +00:00
#pragma once
#include "arch.h"
#include "config.h"
2020-12-05 11:26:32 +00:00
#include "event.h"
#include "iobuf.h"
2020-12-23 10:15:09 +00:00
#include "str.h"
#include "timer.h"
2020-12-05 11:26:32 +00:00
2020-12-22 09:44:59 +00:00
struct mg_dns {
const char *url; // DNS server URL
struct mg_connection *c; // DNS server connection
2020-12-05 11:26:32 +00:00
};
struct mg_addr {
uint8_t ip[16]; // Holds IPv4 or IPv6 address, in network byte order
uint16_t port; // TCP or UDP port in network byte order
uint8_t scope_id; // IPv6 scope ID
bool is_ip6; // True when address is IPv6 address
2020-12-05 11:26:32 +00:00
};
2020-12-22 09:44:59 +00:00
struct mg_mgr {
struct mg_connection *conns; // List of active connections
struct mg_dns dns4; // DNS for IPv4
struct mg_dns dns6; // DNS for IPv6
int dnstimeout; // DNS resolve timeout in milliseconds
2022-04-22 20:44:53 +01:00
bool use_dns6; // Use DNS6 server by default, see #1532
2020-12-22 09:44:59 +00:00
unsigned long nextid; // Next connection ID
unsigned long timerid; // Next timer ID
2021-02-28 16:40:27 +00:00
void *userdata; // Arbitrary user data pointer
2023-09-21 18:43:33 +01:00
void *tls_ctx; // TLS context shared by all TLS sessions
uint16_t mqtt_id; // MQTT IDs for pub/sub
void *active_dns_requests; // DNS requests in progress
struct mg_timer *timers; // Active timers
2022-08-03 15:07:16 +01:00
int epoll_fd; // Used when MG_EPOLL_ENABLE=1
void *priv; // Used by the MIP stack
size_t extraconnsize; // Used by the MIP stack
#if MG_ENABLE_FREERTOS_TCP
2020-12-22 09:44:59 +00:00
SocketSet_t ss; // NOTE(lsm): referenced from socket struct
#endif
};
2020-12-05 11:26:32 +00:00
struct mg_connection {
struct mg_connection *next; // Linkage in struct mg_mgr :: connections
struct mg_mgr *mgr; // Our container
2022-02-22 22:00:55 +00:00
struct mg_addr loc; // Local address
struct mg_addr rem; // Remote address
2020-12-05 11:26:32 +00:00
void *fd; // Connected socket, or LWIP data
2020-12-21 12:26:44 +00:00
unsigned long id; // Auto-incrementing unique connection ID
2020-12-05 11:26:32 +00:00
struct mg_iobuf recv; // Incoming data
struct mg_iobuf send; // Outgoing data
2023-08-22 10:13:04 +01:00
struct mg_iobuf prof; // Profile data enabled by MG_ENABLE_PROFILE
struct mg_iobuf rtls; // TLS only. Incoming encrypted data
2020-12-05 11:26:32 +00:00
mg_event_handler_t fn; // User-specified event handler function
2021-03-11 13:15:53 +00:00
void *fn_data; // User-specified function parameter
2020-12-05 11:26:32 +00:00
mg_event_handler_t pfn; // Protocol-specific handler function
void *pfn_data; // Protocol-specific function parameter
char data[MG_DATA_SIZE]; // Arbitrary connection data
2020-12-05 11:26:32 +00:00
void *tls; // TLS specific data
unsigned is_listening : 1; // Listening connection
unsigned is_client : 1; // Outbound (client) connection
unsigned is_accepted : 1; // Accepted (server) connection
2021-03-11 13:15:53 +00:00
unsigned is_resolving : 1; // Non-blocking DNS resolution is in progress
2023-02-07 02:10:30 +00:00
unsigned is_arplooking : 1; // Non-blocking ARP resolution is in progress
2020-12-05 11:26:32 +00:00
unsigned is_connecting : 1; // Non-blocking connect is in progress
unsigned is_tls : 1; // TLS-enabled connection
unsigned is_tls_hs : 1; // TLS handshake is in progress
unsigned is_udp : 1; // UDP connection
unsigned is_websocket : 1; // WebSocket connection
unsigned is_mqtt5 : 1; // For MQTT connection, v5 indicator
2020-12-05 11:26:32 +00:00
unsigned is_hexdumping : 1; // Hexdump in/out traffic
2020-12-06 21:12:05 +00:00
unsigned is_draining : 1; // Send remaining data, then close and free
unsigned is_closing : 1; // Close and free the connection immediately
unsigned is_full : 1; // Stop reads, until cleared
unsigned is_resp : 1; // Response is still being generated
2020-12-05 11:26:32 +00:00
unsigned is_readable : 1; // Connection is ready to read
unsigned is_writable : 1; // Connection is ready to write
};
void mg_mgr_poll(struct mg_mgr *, int ms);
void mg_mgr_init(struct mg_mgr *);
void mg_mgr_free(struct mg_mgr *);
struct mg_connection *mg_listen(struct mg_mgr *, const char *url,
mg_event_handler_t fn, void *fn_data);
struct mg_connection *mg_connect(struct mg_mgr *, const char *url,
mg_event_handler_t fn, void *fn_data);
2022-04-22 14:42:07 +01:00
struct mg_connection *mg_wrapfd(struct mg_mgr *mgr, int fd,
mg_event_handler_t fn, void *fn_data);
2021-10-22 14:00:31 +01:00
void mg_connect_resolved(struct mg_connection *);
bool mg_send(struct mg_connection *, const void *, size_t);
2022-02-10 17:11:03 +00:00
size_t mg_printf(struct mg_connection *, const char *fmt, ...);
2022-09-23 08:59:02 +01:00
size_t mg_vprintf(struct mg_connection *, const char *fmt, va_list *ap);
2020-12-20 16:55:33 +00:00
bool mg_aton(struct mg_str str, struct mg_addr *addr);
2022-02-22 21:14:29 +00:00
// These functions are used to integrate with custom network stacks
2022-02-23 10:51:01 +00:00
struct mg_connection *mg_alloc_conn(struct mg_mgr *);
2022-02-23 03:06:02 +00:00
void mg_close_conn(struct mg_connection *c);
bool mg_open_listener(struct mg_connection *c, const char *url);
// Utility functions
struct mg_timer *mg_timer_add(struct mg_mgr *mgr, uint64_t milliseconds,
unsigned flags, void (*fn)(void *), void *arg);