diff --git a/CMakeLists.txt b/CMakeLists.txt index 36637ed..c017d3e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -108,6 +108,7 @@ install(FILES "${PROJECT_BINARY_DIR}/libwsfs-adapter.pc" DESTINATION lib${LIB_SU set(WSFS_PROVIDER_SOURCES lib/wsfs/provider/url.c lib/wsfs/provider/client.c + lib/wsfs/provider/client_config.c lib/wsfs/provider/client_protocol.c lib/wsfs/provider/provider.c lib/wsfs/provider/request.c diff --git a/example/provider/main.c b/example/provider/main.c index dcdfecd..07c62fb 100644 --- a/example/provider/main.c +++ b/example/provider/main.c @@ -6,9 +6,18 @@ #include #include +#include +#include #include "wsfs_provider.h" +struct config +{ + char * url; + struct wsfsp_client_config * client_config; + bool show_help; +}; + enum fs_entry_type { FS_FILE, @@ -31,6 +40,88 @@ struct fs struct fs_entry const * entries; }; +static void show_help() +{ + printf( + "wsfs-provider, Copyright (c) 2019 fuse-wsfs authors \n" + "Example for websocket file system provider\n" + "\n" + "Usage: wsfs-provider -u [-k ] [-c ]\n" + "\n" + "Options:\n" + "\t-u, --url URL of WSFS server (required)\n" + "\t-k, --key_path Path to private key of provider (default: not set, TLS disabled)\n" + "\t-c, --cert_path Path to certificate of provider (defautl: not set, TLS disabled)\n" + "\t-h, --help prints this message\n" + "\n" + "Example:\n" + "\twsfs-provider -u ws://localhost:8080/\n" + "\n" + ); +} + +static int parse_arguments( + int argc, + char* argv[], + struct config * config) +{ + static struct option const options[] = + { + {"url", required_argument, NULL, 'u'}, + {"key_path", required_argument, NULL, 'k'}, + {"cert_path", required_argument, NULL, 'c'}, + {"help", no_argument, NULL, 'h'}, + {NULL, 0, NULL, 0} + }; + + int result = EXIT_SUCCESS; + bool finished = false; + while (!finished) + { + int option_index = 0; + int const c = getopt_long(argc, argv, "u:k:c:h", options, &option_index); + + switch (c) + { + case -1: + finished = true; + break; + case 'h': + config->show_help = true; + finished = true; + break; + case 'u': + free(config->url); + config->url = strdup(optarg); + break; + case 'k': + wsfsp_client_config_set_keypath(config->client_config, optarg); + break; + case 'c': + wsfsp_client_config_set_certpath(config->client_config, optarg); + break; + default: + fprintf(stderr, "error: unknown argument\n"); + finished = true; + result = EXIT_FAILURE; + break; + } + + if (NULL == config->url) + { + fprintf(stderr, "error: missing required argument \"-u\"\n"); + result = EXIT_FAILURE; + } + + if (result != EXIT_SUCCESS) + { + config->show_help = true; + } + } + + return result; +} + static struct fs_entry const * fs_getentry( struct fs * fs, ino_t inode) @@ -223,15 +314,6 @@ static void fs_read( } } -static struct wsfsp_provider fs_provider = -{ - .lookup = &fs_lookup, - .getattr = &fs_getattr, - .readdir = &fs_readdir, - .open = &fs_open, - .read = &fs_read -}; - static struct wsfsp_client * client; static void on_interrupt(int signal_id) @@ -243,36 +325,57 @@ static void on_interrupt(int signal_id) int main(int argc, char* argv[]) { - (void) argc; - (void) argv; + struct config config; + config.url = NULL; + config.show_help = false; + config.client_config = wsfsp_client_config_create(); + int result = parse_arguments(argc, argv, &config); - static struct fs_entry const entries[]= + if (EXIT_SUCCESS == result) { - {.parent = 0, .inode = 1, .name = "", .mode = 0555, .type = FS_DIR}, + static struct fs_entry const entries[]= { - .parent = 1, - .inode = 2, - .name = "hello.txt", - .mode = 0555, - .type = FS_FILE, - .content="hello, world!", - .content_length = 13, - }, - {.parent = 0, .inode = 0, .name = NULL} - }; + {.parent = 0, .inode = 1, .name = "", .mode = 0555, .type = FS_DIR}, + { + .parent = 1, + .inode = 2, + .name = "hello.txt", + .mode = 0555, + .type = FS_FILE, + .content="hello, world!", + .content_length = 13, + }, + {.parent = 0, .inode = 0, .name = NULL} + }; + + struct fs fs = + { + .entries = entries + }; - struct fs fs = - { - .entries = entries - }; + signal(SIGINT, &on_interrupt); - signal(SIGINT, &on_interrupt); + wsfsp_client_config_set_userdata(config.client_config, &fs); + wsfsp_client_config_set_onlookup(config.client_config, &fs_lookup); + wsfsp_client_config_set_ongetattr(config.client_config, &fs_getattr); + wsfsp_client_config_set_onreaddir(config.client_config, &fs_readdir); + wsfsp_client_config_set_onopen(config.client_config, &fs_open); + wsfsp_client_config_set_onread(config.client_config, &fs_read); - client = wsfsp_client_create(&fs_provider, &fs); - wsfsp_client_connect(client, "ws://localhost:8080/"); + client = wsfsp_client_create(config.client_config); + wsfsp_client_connect(client, config.url); - wsfsp_client_run(client); + wsfsp_client_run(client); + + wsfsp_client_dispose(client); + } + + if (config.show_help) + { + show_help(); + } - wsfsp_client_dispose(client); - return EXIT_SUCCESS; + free(config.url); + wsfsp_client_config_dispose(config.client_config); + return result; } \ No newline at end of file diff --git a/include/wsfs/provider/client.h b/include/wsfs/provider/client.h index 570679d..d62a855 100644 --- a/include/wsfs/provider/client.h +++ b/include/wsfs/provider/client.h @@ -3,8 +3,8 @@ #include "wsfs/provider/api.h" -struct wsfsp_provider; struct wsfsp_client; +struct wsfsp_client_config; #ifdef __cplusplus extern "C" @@ -12,8 +12,15 @@ extern "C" #endif extern WSFSP_API struct wsfsp_client * wsfsp_client_create( - struct wsfsp_provider * provider, - void * user_data); + struct wsfsp_client_config * config); + +extern WSFSP_API void wsfsp_client_set_keypath( + struct wsfsp_client * client, + char * key_path); + +extern WSFSP_API void wsfsp_client_set_certpath( + struct wsfsp_client * client, + char * cert_path); extern WSFSP_API void wsfsp_client_connect( struct wsfsp_client * client, diff --git a/include/wsfs/provider/provider.h b/include/wsfs/provider/provider.h deleted file mode 100644 index 25d624d..0000000 --- a/include/wsfs/provider/provider.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef WSFS_PROVIDER_PROVIDER_H -#define WSFS_PROVIDER_PROVIDER_H - -#include -#include -#include -#include -#include -#include - -typedef void wsfsp_connected_fn( - void * user_data); - -typedef void wsfsp_disconnected_fn( -void * user_data); - -typedef void wsfsp_ontimer_fn( - void * user_data); - -struct wsfsp_provider -{ - wsfsp_connected_fn * connected; - wsfsp_disconnected_fn * disconnected; - wsfsp_ontimer_fn * ontimer; - wsfsp_lookup_fn * lookup; - wsfsp_getattr_fn * getattr; - wsfsp_readdir_fn * readdir; - wsfsp_open_fn * open; - wsfsp_close_fn * close; - wsfsp_read_fn * read; -}; - - -#endif diff --git a/include/wsfs_provider.h b/include/wsfs_provider.h index 9c033ec..63e09fc 100644 --- a/include/wsfs_provider.h +++ b/include/wsfs_provider.h @@ -5,8 +5,8 @@ #include #include +#include #include -#include #include #include diff --git a/lib/wsfs/provider/client.c b/lib/wsfs/provider/client.c index 7d0676c..886880c 100644 --- a/lib/wsfs/provider/client.c +++ b/lib/wsfs/provider/client.c @@ -8,6 +8,7 @@ #include "wsfs/provider/provider.h" #include "wsfs/provider/client_protocol_intern.h" +#include "wsfs/provider/client_config_intern.h" #include "wsfs/provider/url.h" #define WSFSP_PROTOCOL ("fs") @@ -18,17 +19,17 @@ struct wsfsp_client { volatile bool is_running; - struct wsfsp_provider provider; struct wsfsp_client_protocol protocol; struct lws_context_creation_info info; struct lws_protocols protocols[WSFSP_CLIENT_PROTOCOL_COUNT]; struct lws_context * context; + char * key_path; + char * cert_path; }; struct wsfsp_client * wsfsp_client_create( - struct wsfsp_provider * provider, - void * user_data) + struct wsfsp_client_config * config) { lws_set_log_level(WSFSP_DISABLE_LWS_LOG, NULL); @@ -36,7 +37,7 @@ struct wsfsp_client * wsfsp_client_create( if (NULL != client) { client->is_running = true; - wsfsp_client_protocol_init(&client->protocol, provider, user_data); + wsfsp_client_protocol_init(&client->protocol, &config->provider, config->user_data); memset(client->protocols, 0, sizeof(struct lws_protocols) * WSFSP_CLIENT_PROTOCOL_COUNT); client->protocols[0].name = "fs"; @@ -48,6 +49,11 @@ struct wsfsp_client * wsfsp_client_create( client->info.uid = -1; client->info.gid = -1; + if ((NULL != config->cert_path) && (NULL != config->key_path)) + { + + } + client->context = lws_create_context(&client->info); } @@ -96,17 +102,6 @@ void wsfsp_client_disconnect( // ToDo: implement me } -void wsfsp_client_settimeout( - struct wsfsp_client * client, - unsigned int timepoint) -{ - (void) client; - (void) timepoint; - - // ToDo: implement me -} - - void wsfsp_client_run( struct wsfsp_client * client) { diff --git a/lib/wsfs/provider/client_protocol.c b/lib/wsfs/provider/client_protocol.c index 21685fa..f3c026e 100644 --- a/lib/wsfs/provider/client_protocol.c +++ b/lib/wsfs/provider/client_protocol.c @@ -7,14 +7,7 @@ #include -#include "wsfs/provider/provider_intern.h" -#include "wsfs/provider/operation/lookup_intern.h" -#include "wsfs/provider/operation/getattr_intern.h" -#include "wsfs/provider/operation/readdir_intern.h" -#include "wsfs/provider/operation/open_intern.h" -#include "wsfs/provider/operation/close_intern.h" -#include "wsfs/provider/operation/read_intern.h" - +#include "wsfs/provider/provider.h" #include "wsfs/util.h" #include "wsfs/message.h" @@ -117,15 +110,7 @@ void wsfsp_client_protocol_init( protocol->request.user_data = protocol; protocol->user_data = user_data; - protocol->provider.lookup = (NULL != provider->lookup) ? provider->lookup : &wsfsp_lookup_default; - protocol->provider.getattr = (NULL != provider->getattr) ? provider->getattr : &wsfsp_getattr_default; - protocol->provider.readdir = (NULL != provider->readdir) ? provider->readdir : &wsfsp_readdir_default; - protocol->provider.open = (NULL != provider->open) ? provider->open : &wsfsp_open_default; - protocol->provider.close = (NULL != provider->close) ? provider->close : &wsfsp_close_default; - protocol->provider.read = (NULL != provider->read) ? provider->read : &wsfsp_read_default; - protocol->provider.connected = (NULL != provider->connected) ? provider->connected : &wsfsp_connected_default; - protocol->provider.disconnected = (NULL != provider->disconnected) ? provider->disconnected : &wsfsp_disconnected_default; - protocol->provider.ontimer = (NULL != provider->ontimer) ? provider->ontimer : &wsfsp_ontimer_default; + wsfsp_provider_init_from_prototype(&protocol->provider, provider); } void wsfsp_client_protocol_cleanup( diff --git a/lib/wsfs/provider/operation/close_intern.h b/lib/wsfs/provider/operation/close_intern.h index abb7968..ecc55e2 100644 --- a/lib/wsfs/provider/operation/close_intern.h +++ b/lib/wsfs/provider/operation/close_intern.h @@ -2,7 +2,7 @@ #define WSFS_PROVIDER_OPERATION_CLOSE_INTERN_H #include "wsfs/provider/operation/close.h" -#include "wsfs/provider/provider_intern.h" +#include "wsfs/provider/provider.h" #ifdef __cplusplus extern "C" diff --git a/lib/wsfs/provider/operation/getattr_intern.h b/lib/wsfs/provider/operation/getattr_intern.h index 784e013..2a92dc4 100644 --- a/lib/wsfs/provider/operation/getattr_intern.h +++ b/lib/wsfs/provider/operation/getattr_intern.h @@ -2,7 +2,7 @@ #define WSFS_PROVIDER_OPERATION_GETATTR_INTERN_H #include "wsfs/provider/operation/getattr.h" -#include "wsfs/provider/provider_intern.h" +#include "wsfs/provider/provider.h" #ifdef __cplusplus extern "C" diff --git a/lib/wsfs/provider/operation/lookup_intern.h b/lib/wsfs/provider/operation/lookup_intern.h index 061610b..86bccec 100644 --- a/lib/wsfs/provider/operation/lookup_intern.h +++ b/lib/wsfs/provider/operation/lookup_intern.h @@ -2,7 +2,7 @@ #define WSFS_PROVIDER_OPERATION_LOOKUP_INTERN_H #include "wsfs/provider/operation/lookup.h" -#include "wsfs/provider/provider_intern.h" +#include "wsfs/provider/provider.h" #ifdef __cplusplus extern "C" diff --git a/lib/wsfs/provider/operation/open_intern.h b/lib/wsfs/provider/operation/open_intern.h index bd03d3f..753ee46 100644 --- a/lib/wsfs/provider/operation/open_intern.h +++ b/lib/wsfs/provider/operation/open_intern.h @@ -2,7 +2,7 @@ #define WSFS_PROVIDER_OPERATION_OPEN_INTERN_H #include "wsfs/provider/operation/open.h" -#include "wsfs/provider/provider_intern.h" +#include "wsfs/provider/provider.h" #ifdef __cplusplus extern "C" diff --git a/lib/wsfs/provider/operation/read_intern.h b/lib/wsfs/provider/operation/read_intern.h index d8adcab..793572d 100644 --- a/lib/wsfs/provider/operation/read_intern.h +++ b/lib/wsfs/provider/operation/read_intern.h @@ -2,7 +2,7 @@ #define WSFS_PROVIDER_OPERATION_READ_INTERN_H #include "wsfs/provider/operation/read.h" -#include "wsfs/provider/provider_intern.h" +#include "wsfs/provider/provider.h" #ifdef __cplusplus extern "C" diff --git a/lib/wsfs/provider/operation/readdir_intern.h b/lib/wsfs/provider/operation/readdir_intern.h index 8266aa9..b8cc225 100644 --- a/lib/wsfs/provider/operation/readdir_intern.h +++ b/lib/wsfs/provider/operation/readdir_intern.h @@ -2,7 +2,7 @@ #define WSFS_PROVIDER_OPERATION_READDIR_INTERN_H #include "wsfs/provider/operation/readdir.h" -#include "wsfs/provider/provider_intern.h" +#include "wsfs/provider/provider.h" #ifdef __cplusplus extern "C" diff --git a/lib/wsfs/provider/provider.c b/lib/wsfs/provider/provider.c index f64dabd..5cb548a 100644 --- a/lib/wsfs/provider/provider.c +++ b/lib/wsfs/provider/provider.c @@ -1,4 +1,4 @@ -#include "wsfs/provider/provider_intern.h" +#include "wsfs/provider/provider.h" #include #include @@ -55,6 +55,35 @@ static void wsfsp_provider_invoke_method( } } +void wsfsp_provider_init( + struct wsfsp_provider * provider) +{ + provider->lookup = &wsfsp_lookup_default; + provider->getattr = &wsfsp_getattr_default; + provider->readdir = &wsfsp_readdir_default; + provider->open = &wsfsp_open_default; + provider->close = &wsfsp_close_default; + provider->read = &wsfsp_read_default; + provider->connected = &wsfsp_connected_default; + provider->disconnected = &wsfsp_disconnected_default; + provider->ontimer = &wsfsp_ontimer_default; +} + +void wsfsp_provider_init_from_prototype( + struct wsfsp_provider * provider, + struct wsfsp_provider const * prototype) +{ + provider->lookup = prototype->lookup; + provider->getattr = prototype->getattr; + provider->readdir = prototype->readdir; + provider->open = prototype->open; + provider->close = prototype->close; + provider->read = prototype->read; + provider->connected = prototype->connected; + provider->disconnected = prototype->disconnected; + provider->ontimer = prototype->ontimer; +} + void wsfsp_provider_invoke( struct wsfsp_invokation_context * context, json_t * request) diff --git a/lib/wsfs/provider/provider_intern.h b/lib/wsfs/provider/provider_intern.h deleted file mode 100644 index 11c5ef0..0000000 --- a/lib/wsfs/provider/provider_intern.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef WSFS_PROVIDER_PROVIDER_INTERN_H -#define WSFS_PROVIDER_PROVIDER_INTERN_H - -#include "wsfs/provider/provider.h" -#include "wsfs/provider/request.h" - -#include - -struct wsfsp_invokation_context -{ - struct wsfsp_provider * provider; - void * user_data; - struct wsfsp_request * request; -}; - -#ifdef __cplusplus -extern "C" -{ -#endif - -extern void wsfsp_provider_invoke( - struct wsfsp_invokation_context * context, - json_t * request); - -extern void wsfsp_connected_default( - void * user_data); - -extern void wsfsp_disconnected_default( - void * user_data); - -extern void wsfsp_ontimer_default( - void * user_data); - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file