|
M_io_error_t | M_io_net_server_create (M_io_t **io_out, unsigned short port, const char *bind_ip, M_io_net_type_t type) |
|
M_io_error_t | M_io_net_client_create (M_io_t **io_out, M_dns_t *dns, const char *host, unsigned short port, M_io_net_type_t type) |
|
M_bool | M_io_net_set_keepalives (M_io_t *io, M_uint64 idle_time_s, M_uint64 retry_time_s, M_uint64 retry_cnt) |
|
M_bool | M_io_net_set_nagle (M_io_t *io, M_bool nagle_enabled) |
|
M_bool | M_io_net_set_connect_timeout_ms (M_io_t *io, M_uint64 timeout_ms) |
|
char * | M_io_net_get_fqdn (void) |
|
const char * | M_io_net_get_host (M_io_t *io) |
|
const char * | M_io_net_get_ipaddr (M_io_t *io) |
|
const char * | M_io_net_get_server_ipaddr (M_io_t *io) |
|
unsigned short | M_io_net_get_port (M_io_t *io) |
|
unsigned short | M_io_net_get_ephemeral_port (M_io_t *io) |
|
enum M_io_net_type | M_io_net_get_type (M_io_t *io) |
|
M_uint64 | M_io_net_get_connect_timeout_ms (M_io_t *io) |
|
M_uint64 | M_io_net_time_dns_ms (M_io_t *io) |
|
M_uint64 | M_io_net_time_connect_ms (M_io_t *io) |
|
M_bool | M_io_net_ipaddr_to_bin (unsigned char *ipaddr_bin, size_t ipaddr_bin_size, const char *ipaddr_str, size_t *ipaddr_bin_len) |
|
M_bool | M_io_net_bin_to_ipaddr (char *ipaddr_str, size_t ipaddr_str_size, const unsigned char *ipaddr_bin, size_t ipaddr_bin_len) |
|
Capable of functioning as a network server and client.
Example network client which downloads google.com's home page. This uses a network client connection wrapped in TLS. A trace layer is provided and is commented out.
#include <mstdlib/mstdlib.h>
#include <mstdlib/mstdlib_io.h>
#include <mstdlib/mstdlib_tls.h>
{
char *out;
switch (type) {
break;
break;
default:
return;
}
}
{
char buf[128] = { 0 };
size_t len_written = 0;
switch (etype) {
M_printf(
"CONNECTED: %s%s%s:%d (%s: %s - session%sreused)\n",
break;
M_io_read(io, (
unsigned char *)buf,
sizeof(buf), &len_written);
if (len_written > 0) {
M_printf(
"%.*s", (
int)len_written, buf);
}
break;
break;
break;
break;
}
}
int main(int argc, char **argv)
{
size_t layer_id;
return 0;
}
struct M_buf M_buf_t
Definition: m_buf.h:77
M_buf_t * M_buf_create(void) M_WARN_UNUSED_RESULT M_MALLOC
void M_buf_add_str(M_buf_t *buf, const char *str)
void M_buf_cancel(M_buf_t *buf) M_FREE(1)
M_bool M_dns_destroy(M_dns_t *dns)
struct M_dns M_dns_t
Definition: m_dns.h:43
M_dns_t * M_dns_create(M_event_t *event)
void M_event_destroy(M_event_t *event)
enum M_event_type M_event_type_t
Definition: m_event.h:189
struct M_event M_event_t
Definition: m_event.h:210
M_bool M_event_add(M_event_t *event, M_io_t *io, M_event_callback_t callback, void *cb_data)
void M_event_done(M_event_t *event)
M_event_err_t M_event_loop(M_event_t *event, M_uint64 timeout_ms)
M_event_t * M_event_create(M_uint32 flags)
void M_event_done_with_disconnect(M_event_t *event, M_uint64 timeout_before_disconnect_ms, M_uint64 disconnect_timeout_ms)
@ M_EVENT_TYPE_WRITE
Definition: m_event.h:184
@ M_EVENT_TYPE_ACCEPT
Definition: m_event.h:174
@ M_EVENT_TYPE_DISCONNECTED
Definition: m_event.h:176
@ M_EVENT_TYPE_OTHER
Definition: m_event.h:185
@ M_EVENT_TYPE_READ
Definition: m_event.h:175
@ M_EVENT_TYPE_CONNECTED
Definition: m_event.h:173
@ M_EVENT_TYPE_ERROR
Definition: m_event.h:181
@ M_EVENT_FLAG_NONE
Definition: m_event.h:232
ssize_t M_printf(const char *fmt,...)
unsigned short M_io_net_get_port(M_io_t *io)
enum M_io_net_type M_io_net_get_type(M_io_t *io)
M_io_error_t M_io_net_client_create(M_io_t **io_out, M_dns_t *dns, const char *host, unsigned short port, M_io_net_type_t type)
const char * M_io_net_get_ipaddr(M_io_t *io)
@ M_IO_NET_ANY
Definition: m_io_net.h:336
@ M_IO_NET_IPV6
Definition: m_io_net.h:338
enum M_io_trace_type M_io_trace_type_t
Definition: m_io_trace.h:54
@ M_IO_TRACE_TYPE_READ
Definition: m_io_trace.h:50
@ M_IO_TRACE_TYPE_WRITE
Definition: m_io_trace.h:51
void M_io_destroy(M_io_t *comm)
struct M_io M_io_t
Definition: m_io.h:59
M_io_error_t M_io_write_from_buf(M_io_t *comm, M_buf_t *buf)
M_io_error_t M_io_read(M_io_t *comm, unsigned char *buf, size_t buf_len, size_t *len_read)
#define M_IO_LAYER_FIND_FIRST_ID
Definition: m_io.h:110
void M_free(void *ptr) M_FREE(1)
char * M_str_hexdump(int flags, size_t bytes_per_line, const char *line_prefix, const unsigned char *data, size_t data_len)
@ M_STR_HEXDUMP_HEADER
Definition: m_str.h:1875
M_bool M_tls_clientctx_set_default_trust(M_tls_clientctx_t *ctx)
const char * M_tls_get_cipher(M_io_t *io, size_t id)
const char * M_tls_protocols_to_str(M_tls_protocols_t protocol)
M_bool M_tls_get_sessionreused(M_io_t *io, size_t id)
M_tls_clientctx_t * M_tls_clientctx_create(void)
struct M_tls_clientctx M_tls_clientctx_t
Definition: m_tls.h:45
M_tls_protocols_t M_tls_get_protocol(M_io_t *io, size_t id)
M_io_error_t M_io_tls_client_add(M_io_t *io, M_tls_clientctx_t *ctx, const char *hostname, size_t *layer_id)
void M_tls_clientctx_destroy(M_tls_clientctx_t *ctx)
Example network sever. This is an echo server which uses a state machine to determine what operation it should perform.
#include <mstdlib/mstdlib.h>
#include <mstdlib/mstdlib_io.h>
typedef enum {
STATE_CHECK = 1,
STATE_ECHO,
STATE_EXIT
} states_t;
typedef struct {
} ldata_t;
{
ldata_t *ldata = data;
(void)next;
}
}
{
ldata_t *ldata = data;
char *out;
*next = STATE_CHECK;
}
{
ldata_t *ldata = data;
(void)next;
}
{
ldata_t *ldata = thunk;
char error[256] = { 0 };
M_bool clean = M_FALSE;
switch (etype) {
M_printf(
"CLIENT CONNECTED: %s%s%s:%d\n",
break;
}
}
break;
}
break;
clean = M_TRUE;
if (!clean) {
}
M_printf(
"CLIENT DISCONNECTED: %s%s%s:%d (%s%s%s)\n",
clean?"clean":"unclean", clean?"":" - ", clean?"":error);
break;
}
}
{
ldata_t *ldata;
char error[256] = { 0 };
(void)thunk;
switch (etype) {
return;
M_printf(
"ACCEPT FAILURE: %s\n", error);
}
ldata->el = el;
ldata->io = io_out;
break;
break;
break;
}
}
int main(int argc, char *argv)
{
return 0;
}
return 0;
}
size_t M_buf_len(const M_buf_t *buf)
M_io_error_t M_io_net_server_create(M_io_t **io_out, unsigned short port, const char *bind_ip, M_io_net_type_t type)
@ M_IO_NET_IPV4
Definition: m_io_net.h:337
void M_io_disconnect(M_io_t *comm)
M_io_error_t M_io_read_into_parser(M_io_t *comm, M_parser_t *parser)
enum M_io_error M_io_error_t
Definition: m_io.h:93
void M_io_get_error_string(M_io_t *io, char *error, size_t err_len)
M_io_error_t M_io_accept(M_io_t **io_out, M_io_t *server_io)
const char * M_io_error_string(M_io_error_t error)
@ M_IO_ERROR_SUCCESS
Definition: m_io.h:69
@ M_IO_ERROR_WOULDBLOCK
Definition: m_io.h:70
void * M_malloc_zero(size_t size) M_ALLOC_SIZE(1) M_WARN_UNUSED_RESULT M_MALLOC
size_t M_parser_len(const M_parser_t *parser)
void M_parser_destroy(M_parser_t *parser)
struct M_parser M_parser_t
Definition: m_parser.h:52
char * M_parser_read_strdup_mark(M_parser_t *parser)
M_parser_t * M_parser_create(M_uint32 flags)
size_t M_parser_mark_rewind(M_parser_t *parser)
void M_parser_mark(M_parser_t *parser)
size_t M_parser_consume_until(M_parser_t *parser, const unsigned char *pat, size_t len, M_bool eat_pat)
@ M_PARSER_FLAG_NONE
Definition: m_parser.h:60
M_state_machine_status_t M_state_machine_run(M_state_machine_t *m, void *data)
M_bool M_state_machine_insert_state(M_state_machine_t *m, M_uint64 id, M_uint64 ndescr, const char *descr, M_state_machine_state_cb func, M_state_machine_cleanup_t *cleanup, M_list_u64_t *next_ids)
M_state_machine_t * M_state_machine_create(M_uint64 ndescr, const char *descr, M_uint32 flags)
M_state_machine_status_t
Definition: m_state_machine.h:565
struct M_state_machine M_state_machine_t
Definition: m_state_machine.h:556
void M_state_machine_destroy(M_state_machine_t *m)
@ M_STATE_MACHINE_NONE
Definition: m_state_machine.h:633
@ M_STATE_MACHINE_STATUS_WAIT
Definition: m_state_machine.h:598
@ M_STATE_MACHINE_STATUS_NEXT
Definition: m_state_machine.h:568
M_bool M_str_eq(const char *s1, const char *s2)