1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2026-03-02 04:09:18 +00:00

added wf_client_tlsconfig

This commit is contained in:
Falk Werner
2020-06-11 18:07:42 +02:00
parent dcbe4f075a
commit 06a24e09da
12 changed files with 412 additions and 9 deletions

View File

@@ -9,6 +9,7 @@
#include "webfuse/core/util.h"
#include "webfuse/adapter/impl/client.h"
#include "webfuse/adapter/impl/client_tlsconfig.h"
// server
@@ -265,3 +266,28 @@ wf_client_add_filesystem(
wf_impl_client_add_filesystem(client, local_path, name);
}
// client_tlsconfig
void
wf_client_tlsconfig_set_keypath(
struct wf_client_tlsconfig * config,
char const * key_path)
{
wf_impl_client_tlsconfig_set_keypath(config, key_path);
}
void
wf_client_tlsconfig_set_certpath(
struct wf_client_tlsconfig * config,
char const * cert_path)
{
wf_impl_client_tlsconfig_set_certpath(config, cert_path);
}
void
wf_client_tlsconfig_set_cafilepath(
struct wf_client_tlsconfig * config,
char const * cafile_path)
{
wf_impl_client_tlsconfig_set_cafilepath(config, cafile_path);
}

View File

@@ -1,10 +1,22 @@
#include "webfuse/adapter/impl/client.h"
#include "webfuse/adapter/impl/client_protocol.h"
#include "webfuse/adapter/impl/client_tlsconfig.h"
#include "webfuse/core/lws_log.h"
#include <libwebsockets.h>
#include <stdlib.h>
#include <string.h>
#define WF_CLIENT_PROTOCOL_COUNT 2
struct wf_client
{
wf_client_callback_fn * callback;
struct wf_client_protocol protocol;
struct lws_context_creation_info info;
struct lws_protocols protocols[WF_CLIENT_PROTOCOL_COUNT];
struct wf_client_tlsconfig tls;
struct lws_context * context;
void * user_data;
};
@@ -13,12 +25,42 @@ wf_impl_client_create(
wf_client_callback_fn * callback,
void * user_data)
{
wf_lwslog_disable();
struct wf_client * client = malloc(sizeof(struct wf_client));
client->callback = callback;
wf_impl_client_tlsconfig_init(&client->tls);
client->user_data = user_data;
wf_impl_client_protocol_init(&client->protocol,
(wf_client_callback_fn*) callback, (void*) client);
client->callback(client, WF_CLIENT_CREATED, NULL);
memset(client->protocols, 0, sizeof(struct lws_protocols) * WF_CLIENT_PROTOCOL_COUNT);
wf_impl_client_protocol_init_lws(&client->protocol, &client->protocols[0]);
memset(&client->info, 0, sizeof(struct lws_context_creation_info));
client->info.port = CONTEXT_PORT_NO_LISTEN;
client->info.protocols = client->protocols;
client->info.uid = -1;
client->info.gid = -1;
wf_impl_client_protocol_callback(&client->protocol, WF_CLIENT_GET_TLS_CONFIG, &client->tls);
if (wf_impl_client_tlsconfig_isset(&client->tls))
{
client->info.options |= LWS_SERVER_OPTION_EXPLICIT_VHOSTS;
}
client->context = lws_create_context(&client->info);
if (wf_impl_client_tlsconfig_isset(&client->tls))
{
struct lws_vhost * vhost = lws_create_vhost(client->context, &client->info);
client->info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
client->info.client_ssl_cert_filepath = client->tls.cert_path;
client->info.client_ssl_private_key_filepath = client->tls.key_path;
client->info.client_ssl_ca_filepath = client->tls.cafile_path;
lws_init_vhost_client_ssl(&client->info, vhost);
}
wf_impl_client_protocol_callback(&client->protocol, WF_CLIENT_CREATED ,NULL);
return client;
}
@@ -26,7 +68,9 @@ void
wf_impl_client_dispose(
struct wf_client * client)
{
client->callback(client, WF_CLIENT_DISPOSING, NULL);
lws_context_destroy(client->context);
wf_impl_client_protocol_cleanup(&client->protocol);
wf_impl_client_tlsconfig_cleanup(&client->tls);
free(client);
}
@@ -57,7 +101,7 @@ wf_impl_client_connect(
char const * url)
{
(void) url;
client->callback(client, WF_CLIENT_DISCONNECTED, NULL);
wf_impl_client_protocol_callback(&client->protocol, WF_CLIENT_DISCONNECTED, NULL);
}
void
@@ -71,7 +115,7 @@ void
wf_impl_client_authenticate(
struct wf_client * client)
{
client->callback(client, WF_CLIENT_AUTHENTICATION_FAILED, NULL);
wf_impl_client_protocol_callback(&client->protocol, WF_CLIENT_AUTHENTICATION_FAILED, NULL);
}
void
@@ -83,5 +127,5 @@ wf_impl_client_add_filesystem(
(void) local_path;
(void) name;
client->callback(client, WF_CLIENT_FILESYSTEM_ADD_FAILED, NULL);
wf_impl_client_protocol_callback(&client->protocol, WF_CLIENT_FILESYSTEM_ADD_FAILED, NULL);
}

View File

@@ -0,0 +1,55 @@
#include "webfuse/adapter/impl/client_protocol.h"
#include "webfuse/adapter/client_callback.h"
#include "webfuse/core/protocol_names.h"
#include "webfuse/core/util.h"
#include <stddef.h>
#include <libwebsockets.h>
static int wf_impl_client_protocol_lws_callback(
struct lws * WF_UNUSED_PARAM(wsi),
enum lws_callback_reasons WF_UNUSED_PARAM(reason),
void * WF_UNUSED_PARAM(user),
void * WF_UNUSED_PARAM(in),
size_t WF_UNUSED_PARAM(len))
{
return 0;
}
void
wf_impl_client_protocol_init(
struct wf_client_protocol * protocol,
wf_client_callback_fn * callback,
void * user_data)
{
protocol->callback = callback;
protocol->user_data = user_data;
protocol->callback(protocol->user_data, WF_CLIENT_INIT, NULL);
}
void
wf_impl_client_protocol_cleanup(
struct wf_client_protocol * protocol)
{
protocol->callback(protocol->user_data, WF_CLIENT_CLEANUP, NULL);
}
void
wf_impl_client_protocol_callback(
struct wf_client_protocol * protocol,
int reason,
void * arg)
{
protocol->callback(protocol->user_data, reason, arg);
}
void
wf_impl_client_protocol_init_lws(
struct wf_client_protocol * protocol,
struct lws_protocols * lws_protocol)
{
lws_protocol->name = WF_PROTOCOL_NAME_ADAPTER_CLIENT;
lws_protocol->callback = &wf_impl_client_protocol_lws_callback;
lws_protocol->per_session_data_size = 0;
lws_protocol->user = protocol;
}

View File

@@ -0,0 +1,51 @@
#ifndef WF_ADAPTER_IMPL_CLIENT_PROTOCOL_H
#define WF_ADAPTER_IMPL_CLIENT_PROTOCOL_H
#include "webfuse/adapter/client_callback.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct lws_protocols;
typedef void
wf_client_protocol_callback_fn(
void * user_data,
int reason,
void * arg);
struct wf_client_protocol
{
wf_client_callback_fn * callback;
void * user_data;
};
extern void
wf_impl_client_protocol_init(
struct wf_client_protocol * protocol,
wf_client_callback_fn * callback,
void * user_data);
extern void
wf_impl_client_protocol_cleanup(
struct wf_client_protocol * protocol);
extern void
wf_impl_client_protocol_callback(
struct wf_client_protocol * protocol,
int reason,
void * arg);
extern void
wf_impl_client_protocol_init_lws(
struct wf_client_protocol * protocol,
struct lws_protocols * lws_protocol);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,55 @@
#include "webfuse/adapter/impl/client_tlsconfig.h"
#include <stdlib.h>
#include <string.h>
void
wf_impl_client_tlsconfig_init(
struct wf_client_tlsconfig * config)
{
config->key_path = NULL;
config->cert_path = NULL;
config->cafile_path = NULL;
}
void
wf_impl_client_tlsconfig_cleanup(
struct wf_client_tlsconfig * config)
{
free(config->key_path);
free(config->cert_path);
free(config->cafile_path);
}
void
wf_impl_client_tlsconfig_set_keypath(
struct wf_client_tlsconfig * config,
char const * key_path)
{
free(config->key_path);
config->key_path = strdup(key_path);
}
void
wf_impl_client_tlsconfig_set_certpath(
struct wf_client_tlsconfig * config,
char const * cert_path)
{
free(config->cert_path);
config->cert_path = strdup(cert_path);
}
void
wf_impl_client_tlsconfig_set_cafilepath(
struct wf_client_tlsconfig * config,
char const * cafile_path)
{
free(config->cafile_path);
config->cafile_path = strdup(cafile_path);
}
bool
wf_impl_client_tlsconfig_isset(
struct wf_client_tlsconfig const * config)
{
return (NULL != config->cert_path) && (NULL != config->key_path);
}

View File

@@ -0,0 +1,52 @@
#ifndef WF_ADAPTER_IMPL_CLIENT_TLSCONFIG_H
#define WF_ADAPTER_IMPL_CLIENT_TLSCONFIG_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#ifdef __cplusplus
extern "C"
{
#endif
struct wf_client_tlsconfig
{
char * key_path;
char * cert_path;
char * cafile_path;
};
extern void
wf_impl_client_tlsconfig_init(
struct wf_client_tlsconfig * config);
extern void
wf_impl_client_tlsconfig_cleanup(
struct wf_client_tlsconfig * config);
extern void
wf_impl_client_tlsconfig_set_keypath(
struct wf_client_tlsconfig * config,
char const * key_path);
extern void
wf_impl_client_tlsconfig_set_certpath(
struct wf_client_tlsconfig * config,
char const * cert_path);
extern void
wf_impl_client_tlsconfig_set_cafilepath(
struct wf_client_tlsconfig * config,
char const * cafile_path);
extern bool
wf_impl_client_tlsconfig_isset(
struct wf_client_tlsconfig const * config);
#ifdef __cplusplus
}
#endif
#endif