2020-06-16 21:39:45 +00:00
|
|
|
#include "webfuse_provider/impl/client_protocol.h"
|
2019-03-26 22:04:53 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <libwebsockets.h>
|
|
|
|
#include <jansson.h>
|
|
|
|
|
2020-06-16 21:39:45 +00:00
|
|
|
#include "webfuse_provider/impl/client_config.h"
|
|
|
|
#include "webfuse_provider/impl/provider.h"
|
|
|
|
#include "webfuse_provider/impl/credentials.h"
|
2020-06-21 19:18:43 +00:00
|
|
|
#include "webfuse_provider/impl/util/util.h"
|
2020-06-16 21:39:45 +00:00
|
|
|
#include "webfuse_provider/impl/message.h"
|
|
|
|
#include "webfuse_provider/impl/message_queue.h"
|
2020-06-21 19:18:43 +00:00
|
|
|
#include "webfuse_provider/impl/util/container_of.h"
|
|
|
|
#include "webfuse_provider/impl/util/url.h"
|
2020-06-16 21:39:45 +00:00
|
|
|
#include "webfuse_provider/protocol_names.h"
|
|
|
|
|
|
|
|
#include "webfuse_provider/impl/timer/manager.h"
|
|
|
|
|
|
|
|
#include "webfuse_provider/impl/jsonrpc/response.h"
|
|
|
|
#include "webfuse_provider/impl/jsonrpc/request.h"
|
|
|
|
#include "webfuse_provider/impl/jsonrpc/proxy.h"
|
2020-03-01 00:13:24 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
#define WFP_DEFAULT_TIMEOUT (10 * 1000)
|
2020-03-01 00:13:24 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
static void wfp_impl_client_protocol_respond(
|
2020-07-08 19:29:18 +00:00
|
|
|
struct wfp_message * message,
|
2019-03-26 22:04:53 +00:00
|
|
|
void * user_data)
|
|
|
|
{
|
|
|
|
struct wfp_client_protocol * protocol = (struct wfp_client_protocol *) user_data;
|
|
|
|
|
2020-06-25 20:08:48 +00:00
|
|
|
wfp_slist_append(&protocol->messages, &message->item);
|
|
|
|
lws_callback_on_writable(protocol->wsi);
|
2019-03-26 22:04:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 00:13:24 +00:00
|
|
|
static void wfp_impl_client_protocol_process(
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wfp_client_protocol * protocol,
|
2020-03-01 00:13:24 +00:00
|
|
|
char const * data,
|
2019-03-26 22:04:53 +00:00
|
|
|
size_t length)
|
|
|
|
{
|
2020-03-01 00:13:24 +00:00
|
|
|
json_t * message = json_loadb(data, length, 0, NULL);
|
|
|
|
if (NULL != message)
|
2019-03-26 22:04:53 +00:00
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
if (wfp_jsonrpc_is_response(message))
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_onresult(protocol->proxy, message);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
if (wfp_jsonrpc_is_request(message))
|
2019-03-26 22:04:53 +00:00
|
|
|
{
|
2020-03-01 00:13:24 +00:00
|
|
|
struct wfp_impl_invokation_context context =
|
|
|
|
{
|
|
|
|
.provider = &protocol->provider,
|
|
|
|
.user_data = protocol->user_data,
|
|
|
|
.request = &protocol->request
|
|
|
|
};
|
|
|
|
|
|
|
|
wfp_impl_provider_invoke(&context, message);
|
|
|
|
}
|
2019-03-26 22:04:53 +00:00
|
|
|
|
2020-03-01 00:13:24 +00:00
|
|
|
json_decref(message);
|
2019-03-26 22:04:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-01 00:13:24 +00:00
|
|
|
static void
|
2020-03-01 12:42:46 +00:00
|
|
|
wfp_impl_client_protocol_on_add_filesystem_finished(
|
2020-03-01 00:13:24 +00:00
|
|
|
void * user_data,
|
|
|
|
json_t const * result,
|
2020-06-16 21:57:41 +00:00
|
|
|
json_t const * WFP_UNUSED_PARAM(error))
|
2019-04-17 20:51:16 +00:00
|
|
|
{
|
2020-03-01 00:13:24 +00:00
|
|
|
struct wfp_client_protocol * protocol = user_data;
|
2020-03-01 10:06:09 +00:00
|
|
|
if (NULL == protocol->wsi) { return; }
|
2020-03-01 12:42:46 +00:00
|
|
|
|
2020-03-01 00:13:24 +00:00
|
|
|
if (NULL != result)
|
2019-04-17 20:51:16 +00:00
|
|
|
{
|
2020-03-01 00:13:24 +00:00
|
|
|
protocol->is_connected = true;
|
|
|
|
protocol->provider.connected(protocol->user_data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-01 10:06:09 +00:00
|
|
|
protocol->is_shutdown_requested = true;
|
|
|
|
lws_callback_on_writable(protocol->wsi);
|
|
|
|
}
|
2020-03-01 00:13:24 +00:00
|
|
|
}
|
2019-04-17 20:51:16 +00:00
|
|
|
|
2020-03-01 00:13:24 +00:00
|
|
|
static void wfp_impl_client_protocol_add_filesystem(
|
|
|
|
struct wfp_client_protocol * protocol)
|
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_invoke(
|
2020-03-01 00:13:24 +00:00
|
|
|
protocol->proxy,
|
2020-03-01 12:42:46 +00:00
|
|
|
&wfp_impl_client_protocol_on_add_filesystem_finished,
|
2020-03-01 00:13:24 +00:00
|
|
|
protocol,
|
|
|
|
"add_filesystem",
|
|
|
|
"s",
|
|
|
|
"cprovider");
|
2019-04-17 20:51:16 +00:00
|
|
|
}
|
2019-03-26 22:04:53 +00:00
|
|
|
|
2020-03-01 12:42:46 +00:00
|
|
|
static void
|
|
|
|
wfp_impl_client_protocol_on_authenticate_finished(
|
|
|
|
void * user_data,
|
|
|
|
json_t const * result,
|
2020-06-16 21:57:41 +00:00
|
|
|
json_t const * WFP_UNUSED_PARAM(error))
|
2020-03-01 12:42:46 +00:00
|
|
|
{
|
|
|
|
struct wfp_client_protocol * protocol = user_data;
|
|
|
|
if (NULL == protocol->wsi) { return; }
|
|
|
|
|
|
|
|
if (NULL != result)
|
|
|
|
{
|
|
|
|
wfp_impl_client_protocol_add_filesystem(protocol);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
protocol->is_shutdown_requested = true;
|
|
|
|
lws_callback_on_writable(protocol->wsi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wfp_impl_client_protocol_authenticate(
|
|
|
|
struct wfp_client_protocol * protocol)
|
|
|
|
{
|
|
|
|
struct wfp_credentials credentials;
|
|
|
|
wfp_impl_credentials_init(&credentials);
|
|
|
|
|
|
|
|
protocol->provider.get_credentials(&credentials, protocol->user_data);
|
|
|
|
|
|
|
|
char const * cred_type = wfp_impl_credentials_get_type(&credentials);
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_invoke(
|
2020-03-01 12:42:46 +00:00
|
|
|
protocol->proxy,
|
|
|
|
&wfp_impl_client_protocol_on_authenticate_finished,
|
|
|
|
protocol,
|
|
|
|
"authenticate",
|
|
|
|
"sj",
|
2020-07-10 21:12:35 +00:00
|
|
|
cred_type, &wfp_impl_credentials_write, &credentials);
|
2020-03-01 12:42:46 +00:00
|
|
|
|
|
|
|
wfp_impl_credentials_cleanup(&credentials);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wfp_impl_client_protocol_handshake(
|
|
|
|
struct wfp_client_protocol * protocol)
|
|
|
|
{
|
|
|
|
if (wfp_impl_provider_is_authentication_enabled(&protocol->provider))
|
|
|
|
{
|
|
|
|
wfp_impl_client_protocol_authenticate(protocol);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wfp_impl_client_protocol_add_filesystem(protocol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
static int wfp_impl_client_protocol_callback(
|
|
|
|
struct lws * wsi,
|
|
|
|
enum lws_callback_reasons reason,
|
2020-06-16 21:57:41 +00:00
|
|
|
void * WFP_UNUSED_PARAM(user),
|
2019-03-26 22:04:53 +00:00
|
|
|
void * in,
|
|
|
|
size_t len)
|
|
|
|
{
|
2020-03-01 10:06:09 +00:00
|
|
|
int result = 0;
|
2019-03-26 22:04:53 +00:00
|
|
|
struct lws_protocols const * ws_protocol = lws_get_protocol(wsi);
|
|
|
|
struct wfp_client_protocol * protocol = (NULL != ws_protocol) ? ws_protocol->user: NULL;
|
|
|
|
|
|
|
|
if (NULL != protocol)
|
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_manager_check(protocol->timer_manager);
|
2020-03-01 00:13:24 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
switch (reason)
|
|
|
|
{
|
|
|
|
case LWS_CALLBACK_CLIENT_ESTABLISHED:
|
2020-03-01 12:42:46 +00:00
|
|
|
wfp_impl_client_protocol_handshake(protocol);
|
2019-03-26 22:04:53 +00:00
|
|
|
break;
|
|
|
|
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
|
2019-05-19 12:33:42 +00:00
|
|
|
protocol->is_connected = false;
|
2019-03-26 22:04:53 +00:00
|
|
|
protocol->provider.disconnected(protocol->user_data);
|
|
|
|
break;
|
|
|
|
case LWS_CALLBACK_CLIENT_CLOSED:
|
2019-05-19 12:33:42 +00:00
|
|
|
protocol->is_connected = false;
|
2020-03-01 10:06:09 +00:00
|
|
|
protocol->provider.disconnected(protocol->user_data);
|
|
|
|
protocol->wsi = NULL;
|
2019-03-26 22:04:53 +00:00
|
|
|
break;
|
|
|
|
case LWS_CALLBACK_CLIENT_RECEIVE:
|
2020-03-01 00:13:24 +00:00
|
|
|
wfp_impl_client_protocol_process(protocol, in, len);
|
2019-03-26 22:04:53 +00:00
|
|
|
break;
|
|
|
|
case LWS_CALLBACK_SERVER_WRITEABLE:
|
|
|
|
// fall-through
|
|
|
|
case LWS_CALLBACK_CLIENT_WRITEABLE:
|
2020-03-01 10:06:09 +00:00
|
|
|
if (wsi == protocol->wsi)
|
2020-02-23 20:02:01 +00:00
|
|
|
{
|
2020-03-01 10:06:09 +00:00
|
|
|
if (protocol->is_shutdown_requested)
|
2019-03-26 22:04:53 +00:00
|
|
|
{
|
2020-03-01 10:06:09 +00:00
|
|
|
result = 1;
|
2019-03-26 22:04:53 +00:00
|
|
|
}
|
2020-06-16 21:57:41 +00:00
|
|
|
else if (!wfp_slist_empty(&protocol->messages))
|
2020-03-01 10:06:09 +00:00
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_slist_item * item = wfp_slist_remove_first(&protocol->messages);
|
|
|
|
struct wfp_message * message = wfp_container_of(item, struct wfp_message, item);
|
2020-03-01 10:06:09 +00:00
|
|
|
lws_write(wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT);
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_message_dispose(message);
|
2020-03-01 10:06:09 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
if (!wfp_slist_empty(&protocol->messages))
|
2020-03-01 10:06:09 +00:00
|
|
|
{
|
|
|
|
lws_callback_on_writable(wsi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 22:04:53 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-01 10:06:09 +00:00
|
|
|
return result;
|
2019-03-26 22:04:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 19:41:10 +00:00
|
|
|
static void wfp_impl_client_protocol_send(
|
2020-07-10 21:12:35 +00:00
|
|
|
char * data,
|
|
|
|
size_t length,
|
2020-03-01 00:13:24 +00:00
|
|
|
void * user_data)
|
|
|
|
{
|
|
|
|
struct wfp_client_protocol * protocol = user_data;
|
|
|
|
|
2020-07-10 21:12:35 +00:00
|
|
|
struct wfp_message * message = wfp_message_create(data, length);
|
|
|
|
wfp_slist_append(&protocol->messages, &message->item);
|
|
|
|
lws_callback_on_writable(protocol->wsi);
|
2020-03-01 00:13:24 +00:00
|
|
|
}
|
2019-03-26 22:04:53 +00:00
|
|
|
|
|
|
|
void wfp_impl_client_protocol_init(
|
|
|
|
struct wfp_client_protocol * protocol,
|
|
|
|
struct wfp_provider const * provider,
|
|
|
|
void * user_data)
|
|
|
|
{
|
2019-05-19 12:33:42 +00:00
|
|
|
protocol->is_connected = false;
|
2020-03-01 10:06:09 +00:00
|
|
|
protocol->is_shutdown_requested = false;
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_slist_init(&protocol->messages);
|
2019-03-26 22:04:53 +00:00
|
|
|
|
|
|
|
protocol->wsi = NULL;
|
|
|
|
|
|
|
|
protocol->request.respond = &wfp_impl_client_protocol_respond;
|
|
|
|
protocol->request.user_data = protocol;
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
protocol->timer_manager = wfp_timer_manager_create();
|
|
|
|
protocol->proxy = wfp_jsonrpc_proxy_create(protocol->timer_manager, WFP_DEFAULT_TIMEOUT, &wfp_impl_client_protocol_send, protocol);
|
2020-03-01 00:13:24 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
protocol->user_data = user_data;
|
|
|
|
wfp_impl_provider_init_from_prototype(&protocol->provider, provider);
|
|
|
|
}
|
|
|
|
|
|
|
|
void wfp_impl_client_protocol_cleanup(
|
|
|
|
struct wfp_client_protocol * protocol)
|
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_dispose(protocol->proxy);
|
|
|
|
wfp_timer_manager_dispose(protocol->timer_manager);
|
|
|
|
wfp_message_queue_cleanup(&protocol->messages);
|
2019-03-26 22:04:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct wfp_client_protocol * wfp_impl_client_protocol_create(
|
2020-02-19 21:44:56 +00:00
|
|
|
struct wfp_client_config const * config)
|
2019-03-26 22:04:53 +00:00
|
|
|
{
|
|
|
|
struct wfp_client_protocol * protocol = malloc(sizeof(struct wfp_client_protocol));
|
2020-03-21 20:22:22 +00:00
|
|
|
wfp_impl_client_protocol_init(protocol, &config->provider, config->user_data);
|
2019-03-26 22:04:53 +00:00
|
|
|
|
|
|
|
return protocol;
|
|
|
|
}
|
|
|
|
|
|
|
|
void wfp_impl_client_protocol_dispose(
|
|
|
|
struct wfp_client_protocol * protocol)
|
|
|
|
{
|
|
|
|
wfp_impl_client_protocol_cleanup(protocol);
|
|
|
|
free(protocol);
|
|
|
|
}
|
|
|
|
|
|
|
|
void wfp_impl_client_protocol_init_lws(
|
|
|
|
struct wfp_client_protocol * protocol,
|
|
|
|
struct lws_protocols * lws_protocol)
|
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
lws_protocol->name = WFP_PROTOCOL_NAME_PROVIDER_CLIENT;
|
2019-03-26 22:04:53 +00:00
|
|
|
lws_protocol->callback = &wfp_impl_client_protocol_callback;
|
|
|
|
lws_protocol->per_session_data_size = 0;
|
|
|
|
lws_protocol->user = protocol;
|
|
|
|
}
|
2020-02-19 21:44:56 +00:00
|
|
|
|
|
|
|
void wfp_impl_client_protocol_connect(
|
|
|
|
struct wfp_client_protocol * protocol,
|
|
|
|
struct lws_context * context,
|
|
|
|
char const * url)
|
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_url url_data;
|
|
|
|
bool const success = wfp_url_init(&url_data, url);
|
2020-02-19 21:44:56 +00:00
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
struct lws_client_connect_info info;
|
|
|
|
memset(&info, 0, sizeof(struct lws_client_connect_info));
|
|
|
|
info.context = context;
|
|
|
|
info.port = url_data.port;
|
|
|
|
info.address = url_data.host;
|
|
|
|
info.path = url_data.path;
|
|
|
|
info.host = info.address;
|
|
|
|
info.origin = info.address;
|
|
|
|
info.ssl_connection = (url_data.use_tls) ? LCCSCF_USE_SSL : 0;
|
2020-06-16 21:57:41 +00:00
|
|
|
info.protocol = WFP_PROTOCOL_NAME_ADAPTER_SERVER;
|
|
|
|
info.local_protocol_name = WFP_PROTOCOL_NAME_PROVIDER_CLIENT;
|
2020-02-19 21:44:56 +00:00
|
|
|
info.pwsi = &protocol->wsi;
|
|
|
|
|
|
|
|
lws_client_connect_via_info(&info);
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_url_cleanup(&url_data);
|
2020-02-19 21:44:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
protocol->provider.disconnected(protocol->user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-04-07 18:37:50 +00:00
|
|
|
|
|
|
|
void wfp_impl_client_protocol_disconnect(
|
|
|
|
struct wfp_client_protocol * protocol)
|
|
|
|
{
|
|
|
|
if (protocol->is_connected)
|
|
|
|
{
|
|
|
|
protocol->is_shutdown_requested = true;
|
|
|
|
lws_callback_on_writable(protocol->wsi);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
protocol->provider.disconnected(protocol->user_data);
|
|
|
|
}
|
|
|
|
}
|