2020-06-11 07:10:14 +00:00
|
|
|
#include "webfuse/adapter/impl/client.h"
|
2020-06-11 16:07:42 +00:00
|
|
|
#include "webfuse/adapter/impl/client_protocol.h"
|
|
|
|
#include "webfuse/adapter/impl/client_tlsconfig.h"
|
|
|
|
#include "webfuse/core/lws_log.h"
|
|
|
|
|
|
|
|
#include <libwebsockets.h>
|
2020-06-11 07:10:14 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2020-06-11 16:07:42 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define WF_CLIENT_PROTOCOL_COUNT 2
|
2020-06-11 07:10:14 +00:00
|
|
|
|
|
|
|
struct wf_client
|
|
|
|
{
|
2020-06-11 16:07:42 +00:00
|
|
|
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;
|
2020-06-11 07:10:14 +00:00
|
|
|
void * user_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct wf_client *
|
|
|
|
wf_impl_client_create(
|
|
|
|
wf_client_callback_fn * callback,
|
|
|
|
void * user_data)
|
|
|
|
{
|
2020-06-11 16:07:42 +00:00
|
|
|
wf_lwslog_disable();
|
|
|
|
|
2020-06-11 07:10:14 +00:00
|
|
|
struct wf_client * client = malloc(sizeof(struct wf_client));
|
2020-06-11 16:07:42 +00:00
|
|
|
wf_impl_client_tlsconfig_init(&client->tls);
|
2020-06-11 07:10:14 +00:00
|
|
|
client->user_data = user_data;
|
2020-06-11 16:07:42 +00:00
|
|
|
wf_impl_client_protocol_init(&client->protocol,
|
|
|
|
(wf_client_callback_fn*) callback, (void*) client);
|
|
|
|
|
|
|
|
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);
|
2020-06-11 07:10:14 +00:00
|
|
|
|
2020-06-11 16:07:42 +00:00
|
|
|
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);
|
|
|
|
}
|
2020-06-11 07:10:14 +00:00
|
|
|
|
2020-06-11 16:07:42 +00:00
|
|
|
wf_impl_client_protocol_callback(&client->protocol, WF_CLIENT_CREATED ,NULL);
|
2020-06-11 07:10:14 +00:00
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wf_impl_client_dispose(
|
|
|
|
struct wf_client * client)
|
|
|
|
{
|
2020-06-11 16:07:42 +00:00
|
|
|
lws_context_destroy(client->context);
|
|
|
|
wf_impl_client_protocol_cleanup(&client->protocol);
|
|
|
|
wf_impl_client_tlsconfig_cleanup(&client->tls);
|
2020-06-11 07:10:14 +00:00
|
|
|
free(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
wf_impl_client_get_userdata(
|
|
|
|
struct wf_client * client)
|
|
|
|
{
|
|
|
|
return client->user_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wf_impl_client_service(
|
|
|
|
struct wf_client * client)
|
|
|
|
{
|
2020-06-11 17:12:07 +00:00
|
|
|
lws_service(client->context, 0);
|
2020-06-11 07:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wf_impl_client_interrupt(
|
|
|
|
struct wf_client * client)
|
|
|
|
{
|
2020-06-11 17:12:07 +00:00
|
|
|
lws_cancel_service(client->context);
|
2020-06-11 07:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wf_impl_client_connect(
|
|
|
|
struct wf_client * client,
|
|
|
|
char const * url)
|
|
|
|
{
|
2020-06-11 20:57:56 +00:00
|
|
|
wf_impl_client_protocol_connect(&client->protocol, client->context, url);
|
2020-06-11 07:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wf_impl_client_disconnect(
|
|
|
|
struct wf_client * client)
|
|
|
|
{
|
2020-06-11 20:57:56 +00:00
|
|
|
wf_impl_client_protocol_disconnect(&client->protocol);
|
2020-06-11 07:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wf_impl_client_authenticate(
|
|
|
|
struct wf_client * client)
|
|
|
|
{
|
2020-06-11 16:07:42 +00:00
|
|
|
wf_impl_client_protocol_callback(&client->protocol, WF_CLIENT_AUTHENTICATION_FAILED, NULL);
|
2020-06-11 07:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wf_impl_client_add_filesystem(
|
|
|
|
struct wf_client * client,
|
|
|
|
char const * local_path,
|
|
|
|
char const * name)
|
|
|
|
{
|
|
|
|
(void) local_path;
|
|
|
|
(void) name;
|
|
|
|
|
2020-06-11 16:07:42 +00:00
|
|
|
wf_impl_client_protocol_callback(&client->protocol, WF_CLIENT_FILESYSTEM_ADD_FAILED, NULL);
|
2020-06-11 07:10:14 +00:00
|
|
|
}
|