Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

us_update_socket_context() and ability to specify inline key/cert #231

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions examples/http3_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void on_stream_headers(us_quic_stream_t *s) {
//print_current_headers();

/* Make a new stream */
us_quic_socket_create_stream(us_quic_stream_socket(s));
us_quic_socket_create_stream(us_quic_stream_socket(s), 0);
}

/* And this would be the body of the request */
Expand All @@ -109,7 +109,7 @@ void on_start(struct us_timer_t *t) {


if (num_sockets < 10) {
us_quic_socket_t *connect_socket = us_quic_socket_context_connect(context, "::1", 9004);
us_quic_socket_t *connect_socket = us_quic_socket_context_connect(context, "::1", 9004, 0);
} else {
if (!ignore) {

Expand All @@ -120,7 +120,7 @@ void on_start(struct us_timer_t *t) {
printf("Starting now\n");
for (int i = 0; i < num_sockets; i++) {
for (int j = 0; j < 32; j++) {
us_quic_socket_create_stream(sockets[i]);
us_quic_socket_create_stream(sockets[i], 0);
}
}
}
Expand Down Expand Up @@ -171,7 +171,7 @@ int main() {
};

/* Create quic socket context (assumes h3 for now) */
context = us_create_quic_socket_context(loop, options);
context = us_create_quic_socket_context(loop, options, 0);

/* Specify application callbacks */
us_quic_socket_context_on_stream_data(context, on_stream_data);
Expand All @@ -187,7 +187,7 @@ int main() {

/* We also establish a client connection that sends requests */
//for (int i = 0; i < 4; i++) {
//us_quic_socket_t *connect_socket = us_quic_socket_context_connect(context, "::1", 9004);
//us_quic_socket_t *connect_socket = us_quic_socket_context_connect(context, "::1", 9004, 0);
//}

/* Run the event loop */
Expand Down
16 changes: 10 additions & 6 deletions examples/http3_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,21 @@ void on_stream_headers(us_quic_stream_t *s) {
//us_quic_socket_context_set_header(context, 2, "content-type", 12, "text/html", 9);
us_quic_socket_context_send_headers(context, s, 1, 1);

/* Write body and shutdown (unknown if content-length must be present?) */
/* Write body (unknown if content-length must be present?) */
us_quic_stream_write(s, "Hello quic!", 11);

/* Every request has its own stream, so we conceptually serve requests like in HTTP 1.0 */
us_quic_stream_shutdown(s);
}

/* And this would be the body of the request */
void on_stream_data(us_quic_stream_t *s, char *data, int length) {
printf("Body length is: %d\n", length);
}

/* Request finished */
void on_stream_end(us_quic_stream_t *s) {
/* Every request has its own stream, so we conceptually serve requests like in HTTP 1.0 */
us_quic_stream_shutdown(s);
}

void on_stream_writable(us_quic_stream_t *s) {

}
Expand Down Expand Up @@ -89,10 +92,11 @@ int main() {
};

/* Create quic socket context (assumes h3 for now) */
context = us_create_quic_socket_context(loop, options);
context = us_create_quic_socket_context(loop, options, 0);

/* Specify application callbacks */
us_quic_socket_context_on_stream_data(context, on_stream_data);
us_quic_socket_context_on_stream_end(context, on_stream_end);
us_quic_socket_context_on_stream_open(context, on_stream_open);
us_quic_socket_context_on_stream_close(context, on_stream_close);
us_quic_socket_context_on_stream_writable(context, on_stream_writable);
Expand All @@ -101,7 +105,7 @@ int main() {
us_quic_socket_context_on_close(context, on_close);

/* The listening socket is the actual UDP socket used */
us_quic_listen_socket_t *listen_socket = us_quic_socket_context_listen(context, "::1", 9004);
us_quic_listen_socket_t *listen_socket = us_quic_socket_context_listen(context, "::1", 9004, 0);

/* Run the event loop */
us_loop_run(loop);
Expand Down
Empty file modified misc/gen_test_certs.sh
100755 → 100644
Empty file.
27 changes: 21 additions & 6 deletions misc/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,24 @@ WIN32_EXPORT long long us_loop_iteration_number(struct us_loop_t *loop);
# us_socket_context_t - The per-behavior group of networking sockets
```c
struct us_socket_context_options_t {
const char *key_file_name;
const char *cert_file_name;
union{ const char *key_file_name, *key_file; };
union{ const char *cert_file_name, *cert_file; };
const char *passphrase;
const char *dh_params_file_name;
const char *ca_file_name;
union{ const char *dh_params_file_name, *dh_params_file; };
union{ const char *ca_file_name, *ca_file; };
const char *ssl_ciphers;
int ssl_prefer_low_memory_usage;
char ssl_prefer_low_memory_usage;
char key_data_inline;
char cert_data_inline;
char dh_params_data_inline;
};

/* A socket context holds shared callbacks and user data extension for associated sockets */
WIN32_EXPORT struct us_socket_context_t *us_create_socket_context(int ssl, struct us_loop_t *loop, int ext_size, struct us_socket_context_options_t options);

/* Update socket context options, for example, to load a new certificate without creating a new socket */
WIN32_EXPORT int us_update_socket_context(int ssl, struct us_socket_context_t* ctx, const struct us_socket_context_options_t* options);

/* Delete resources allocated at creation time. */
WIN32_EXPORT void us_socket_context_free(int ssl, struct us_socket_context_t *context);

Expand All @@ -71,8 +77,17 @@ WIN32_EXPORT struct us_listen_socket_t *us_socket_context_listen(int ssl, struct
/* listen_socket.c/.h */
WIN32_EXPORT void us_listen_socket_close(int ssl, struct us_listen_socket_t *ls);

/* DNS lookup */
WIN32_EXPORT struct addrinfo *us_get_addr(const char* host, int port);

/* free data returned by us_get_addr() */
WIN32_EXPORT void us_free_addr(struct addrinfo *addr);

/* Land in on_open or on_close or return null or return socket */
WIN32_EXPORT struct us_socket_t *us_socket_context_connect(int ssl, struct us_socket_context_t *context, const char *host, int port, int options, int socket_ext_size);
WIN32_EXPORT struct us_socket_t *us_socket_context_connect(int ssl, struct us_socket_context_t *context, const char *host, int port, const char *source_host, int options, int socket_ext_size);

/* Same as above but use addrinfo object */
WIN32_EXPORT struct us_socket_t *us_socket_context_connect_addr(int ssl, struct us_socket_context_t *context, const struct addrinfo *host, const const char *source_host, int options, int socket_ext_size);

/* Returns the loop for this socket context. */
WIN32_EXPORT struct us_loop_t *us_socket_context_loop(int ssl, struct us_socket_context_t *context);
Expand Down
43 changes: 43 additions & 0 deletions src/bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,25 @@ int bsd_would_block() {
#endif
}

struct addrinfo *us_get_addr(const char* host, int port){
struct addrinfo hints, *result;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

char port_string[16];
snprintf(port_string, 16, "%d", port);

if (getaddrinfo(host, port_string, &hints, &result) != 0) {
return NULL;
}
return result;
}

void us_free_addr(struct addrinfo *addr){
freeaddrinfo(addr);
}

// return LIBUS_SOCKET_ERROR or the fd that represents listen socket
// listen both on ipv6 and ipv4
LIBUS_SOCKET_DESCRIPTOR bsd_create_listen_socket(const char *host, int port, int options) {
Expand Down Expand Up @@ -709,6 +728,30 @@ int bsd_udp_packet_buffer_ecn(void *msgvec, int index) {
return 0; // no ecn defaults to 0
}

LIBUS_SOCKET_DESCRIPTOR bsd_create_connect_socket_addr(const struct addrinfo *host, const char *source_host, int options) {

LIBUS_SOCKET_DESCRIPTOR fd = bsd_create_socket(host->ai_family, host->ai_socktype, host->ai_protocol);
if (fd == LIBUS_SOCKET_ERROR) {
return LIBUS_SOCKET_ERROR;
}

if (source_host) {
struct addrinfo *interface_result;
if (!getaddrinfo(source_host, NULL, NULL, &interface_result)) {
int ret = bind(fd, interface_result->ai_addr, (socklen_t) interface_result->ai_addrlen);
freeaddrinfo(interface_result);
if (ret == LIBUS_SOCKET_ERROR) {
bsd_close_socket(fd);
return LIBUS_SOCKET_ERROR;
}
}
}

connect(fd, host->ai_addr, (socklen_t) host->ai_addrlen);

return fd;
}

LIBUS_SOCKET_DESCRIPTOR bsd_create_connect_socket(const char *host, int port, const char *source_host, int options) {
struct addrinfo hints, *result;
memset(&hints, 0, sizeof(struct addrinfo));
Expand Down
38 changes: 38 additions & 0 deletions src/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,15 @@ struct us_socket_context_t *us_create_socket_context(int ssl, struct us_loop_t *
return context;
}

int us_update_socket_context(int ssl, struct us_socket_context_t* ctx, const struct us_socket_context_options_t* options) {
#ifndef LIBUS_NO_SSL
if(ssl){
return us_internal_update_ssl_socket_context((struct us_internal_ssl_socket_context_t*) ctx, options);
}
#endif
return 1;
}

void us_socket_context_free(int ssl, struct us_socket_context_t *context) {
#ifndef LIBUS_NO_SSL
if (ssl) {
Expand Down Expand Up @@ -312,6 +321,35 @@ struct us_listen_socket_t *us_socket_context_listen_unix(int ssl, struct us_sock
return ls;
}

struct us_socket_t *us_socket_context_connect_addr(int ssl, struct us_socket_context_t *context, const struct addrinfo *host, const char *source_host, int options, int socket_ext_size) {
#ifndef LIBUS_NO_SSL
if (ssl) {
return (struct us_socket_t *) us_internal_ssl_socket_context_connect_addr((struct us_internal_ssl_socket_context_t *) context, host, source_host, options, socket_ext_size);
}
#endif

LIBUS_SOCKET_DESCRIPTOR connect_socket_fd = bsd_create_connect_socket_addr(host, source_host, options);
if (connect_socket_fd == LIBUS_SOCKET_ERROR) {
return 0;
}

/* Connect sockets are semi-sockets just like listen sockets */
struct us_poll_t *p = us_create_poll(context->loop, 0, sizeof(struct us_socket_t) + socket_ext_size);
us_poll_init(p, connect_socket_fd, POLL_TYPE_SEMI_SOCKET);
us_poll_start(p, context->loop, LIBUS_SOCKET_WRITABLE);

struct us_socket_t *connect_socket = (struct us_socket_t *) p;

/* Link it into context so that timeout fires properly */
connect_socket->context = context;
connect_socket->timeout = 255;
connect_socket->long_timeout = 255;
connect_socket->low_prio_state = 0;
us_internal_socket_context_link_socket(context, connect_socket);

return connect_socket;
}

struct us_socket_t *us_socket_context_connect(int ssl, struct us_socket_context_t *context, const char *host, int port, const char *source_host, int options, int socket_ext_size) {
#ifndef LIBUS_NO_SSL
if (ssl) {
Expand Down
Loading