ruby-libstorj 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.gitmodules +3 -0
  4. data/.rspec +1 -0
  5. data/Gemfile +23 -0
  6. data/Gemfile.lock +111 -0
  7. data/Guardfile +21 -0
  8. data/LICENSE +502 -0
  9. data/README.md +262 -0
  10. data/Rakefile +76 -0
  11. data/ext/libstorj/.gitignore +47 -0
  12. data/ext/libstorj/.travis.yml +27 -0
  13. data/ext/libstorj/Doxyfile +2427 -0
  14. data/ext/libstorj/LICENSE +502 -0
  15. data/ext/libstorj/Makefile.am +6 -0
  16. data/ext/libstorj/README.md +198 -0
  17. data/ext/libstorj/autogen.sh +3 -0
  18. data/ext/libstorj/configure.ac +64 -0
  19. data/ext/libstorj/depends/Makefile +153 -0
  20. data/ext/libstorj/depends/config.guess +1462 -0
  21. data/ext/libstorj/depends/config.sub +1823 -0
  22. data/ext/libstorj/depends/extract-osx-sdk.sh +33 -0
  23. data/ext/libstorj/depends/packages/cctools.mk +7 -0
  24. data/ext/libstorj/depends/packages/clang.mk +7 -0
  25. data/ext/libstorj/depends/packages/gmp.mk +23 -0
  26. data/ext/libstorj/depends/packages/gnutls.mk +25 -0
  27. data/ext/libstorj/depends/packages/json-c.mk +7 -0
  28. data/ext/libstorj/depends/packages/libcurl.mk +39 -0
  29. data/ext/libstorj/depends/packages/libmicrohttpd.mk +7 -0
  30. data/ext/libstorj/depends/packages/libuv.mk +7 -0
  31. data/ext/libstorj/depends/packages/nettle.mk +30 -0
  32. data/ext/libstorj/libstorj.pc.in +11 -0
  33. data/ext/libstorj/src/Makefile.am +23 -0
  34. data/ext/libstorj/src/bip39.c +233 -0
  35. data/ext/libstorj/src/bip39.h +64 -0
  36. data/ext/libstorj/src/bip39_english.h +2074 -0
  37. data/ext/libstorj/src/cli.c +1494 -0
  38. data/ext/libstorj/src/crypto.c +525 -0
  39. data/ext/libstorj/src/crypto.h +178 -0
  40. data/ext/libstorj/src/downloader.c +1923 -0
  41. data/ext/libstorj/src/downloader.h +163 -0
  42. data/ext/libstorj/src/http.c +688 -0
  43. data/ext/libstorj/src/http.h +175 -0
  44. data/ext/libstorj/src/rs.c +962 -0
  45. data/ext/libstorj/src/rs.h +99 -0
  46. data/ext/libstorj/src/storj.c +1523 -0
  47. data/ext/libstorj/src/storj.h +1014 -0
  48. data/ext/libstorj/src/uploader.c +2736 -0
  49. data/ext/libstorj/src/uploader.h +181 -0
  50. data/ext/libstorj/src/utils.c +336 -0
  51. data/ext/libstorj/src/utils.h +65 -0
  52. data/ext/libstorj/test/Makefile.am +27 -0
  53. data/ext/libstorj/test/mockbridge.c +260 -0
  54. data/ext/libstorj/test/mockbridge.json +687 -0
  55. data/ext/libstorj/test/mockbridgeinfo.json +1836 -0
  56. data/ext/libstorj/test/mockfarmer.c +358 -0
  57. data/ext/libstorj/test/storjtests.h +41 -0
  58. data/ext/libstorj/test/tests.c +1617 -0
  59. data/ext/libstorj/test/tests_rs.c +869 -0
  60. data/ext/ruby-libstorj/extconf.rb +8 -0
  61. data/ext/ruby-libstorj/ruby-libstorj.cc +17 -0
  62. data/lib/ruby-libstorj.rb +1 -0
  63. data/lib/ruby-libstorj/arg_forwarding_task.rb +58 -0
  64. data/lib/ruby-libstorj/env.rb +178 -0
  65. data/lib/ruby-libstorj/ext/bucket.rb +71 -0
  66. data/lib/ruby-libstorj/ext/create_bucket_request.rb +53 -0
  67. data/lib/ruby-libstorj/ext/curl_code.rb +139 -0
  68. data/lib/ruby-libstorj/ext/ext.rb +71 -0
  69. data/lib/ruby-libstorj/ext/file.rb +84 -0
  70. data/lib/ruby-libstorj/ext/get_bucket_request.rb +45 -0
  71. data/lib/ruby-libstorj/ext/json_request.rb +51 -0
  72. data/lib/ruby-libstorj/ext/list_files_request.rb +63 -0
  73. data/lib/ruby-libstorj/ext/types.rb +226 -0
  74. data/lib/ruby-libstorj/ext/upload_options.rb +38 -0
  75. data/lib/ruby-libstorj/libstorj.rb +22 -0
  76. data/lib/ruby-libstorj/mixins/storj.rb +27 -0
  77. data/lib/ruby-libstorj/struct.rb +42 -0
  78. data/ruby-libstorj.gemspec +57 -0
  79. data/spec/helpers/options.yml.example +22 -0
  80. data/spec/helpers/shared_rake_examples.rb +132 -0
  81. data/spec/helpers/storj_options.rb +96 -0
  82. data/spec/helpers/upload.data +3 -0
  83. data/spec/helpers/upload.data.sha256 +1 -0
  84. data/spec/libstorj_spec.rb +0 -0
  85. data/spec/ruby-libstorj/arg_forwarding_task_spec.rb +311 -0
  86. data/spec/ruby-libstorj/env_spec.rb +353 -0
  87. data/spec/ruby-libstorj/ext_spec.rb +75 -0
  88. data/spec/ruby-libstorj/json_request_spec.rb +13 -0
  89. data/spec/ruby-libstorj/libstorj_spec.rb +81 -0
  90. data/spec/ruby-libstorj/struct_spec.rb +64 -0
  91. data/spec/spec_helper.rb +113 -0
  92. metadata +136 -0
@@ -0,0 +1,1014 @@
1
+ /**
2
+ * @file storj.h
3
+ * @brief Storj library.
4
+ *
5
+ * Implements functionality to upload and download files from the Storj
6
+ * distributed network.
7
+ */
8
+
9
+ #ifndef STORJ_H
10
+ #define STORJ_H
11
+
12
+ #ifdef __cplusplus
13
+ extern "C" {
14
+ #endif
15
+
16
+ #if defined(_WIN32) && defined(STORJDLL)
17
+ #if defined(DLL_EXPORT)
18
+ #define STORJ_API __declspec(dllexport)
19
+ #else
20
+ #define STORJ_API __declspec(dllimport)
21
+ #endif
22
+ #else
23
+ #define STORJ_API
24
+ #endif
25
+
26
+ #include <assert.h>
27
+ #include <json-c/json.h>
28
+ #include <stdlib.h>
29
+ #include <stdio.h>
30
+ #include <stdbool.h>
31
+ #include <stdarg.h>
32
+ #include <string.h>
33
+ #include <uv.h>
34
+ #include <curl/curl.h>
35
+
36
+ #include <inttypes.h>
37
+
38
+ #ifdef _WIN32
39
+ #include <time.h>
40
+ #endif
41
+
42
+ #ifndef _WIN32
43
+ #include <sys/mman.h>
44
+ #include <unistd.h>
45
+ #endif
46
+
47
+ // File transfer success
48
+ #define STORJ_TRANSFER_OK 0
49
+ #define STORJ_TRANSFER_CANCELED 1
50
+
51
+ // Bridge related errors 1000 to 1999
52
+ #define STORJ_BRIDGE_REQUEST_ERROR 1000
53
+ #define STORJ_BRIDGE_AUTH_ERROR 1001
54
+ #define STORJ_BRIDGE_TOKEN_ERROR 1002
55
+ #define STORJ_BRIDGE_TIMEOUT_ERROR 1003
56
+ #define STORJ_BRIDGE_INTERNAL_ERROR 1004
57
+ #define STORJ_BRIDGE_RATE_ERROR 1005
58
+ #define STORJ_BRIDGE_BUCKET_NOTFOUND_ERROR 1006
59
+ #define STORJ_BRIDGE_FILE_NOTFOUND_ERROR 1007
60
+ #define STORJ_BRIDGE_JSON_ERROR 1008
61
+ #define STORJ_BRIDGE_FRAME_ERROR 1009
62
+ #define STORJ_BRIDGE_POINTER_ERROR 1010
63
+ #define STORJ_BRIDGE_REPOINTER_ERROR 1011
64
+ #define STORJ_BRIDGE_FILEINFO_ERROR 1012
65
+ #define STORJ_BRIDGE_BUCKET_FILE_EXISTS 1013
66
+ #define STORJ_BRIDGE_OFFER_ERROR 1014
67
+
68
+ // Farmer related errors 2000 to 2999
69
+ #define STORJ_FARMER_REQUEST_ERROR 2000
70
+ #define STORJ_FARMER_TIMEOUT_ERROR 2001
71
+ #define STORJ_FARMER_AUTH_ERROR 2002
72
+ #define STORJ_FARMER_EXHAUSTED_ERROR 2003
73
+ #define STORJ_FARMER_INTEGRITY_ERROR 2004
74
+
75
+ // File related errors 3000 to 3999
76
+ #define STORJ_FILE_INTEGRITY_ERROR 3000
77
+ #define STORJ_FILE_WRITE_ERROR 3001
78
+ #define STORJ_FILE_ENCRYPTION_ERROR 3002
79
+ #define STORJ_FILE_SIZE_ERROR 3003
80
+ #define STORJ_FILE_DECRYPTION_ERROR 3004
81
+ #define STORJ_FILE_GENERATE_HMAC_ERROR 3005
82
+ #define STORJ_FILE_READ_ERROR 3006
83
+ #define STORJ_FILE_SHARD_MISSING_ERROR 3007
84
+ #define STORJ_FILE_RECOVER_ERROR 3008
85
+ #define STORJ_FILE_RESIZE_ERROR 3009
86
+ #define STORJ_FILE_UNSUPPORTED_ERASURE 3010
87
+ #define STORJ_FILE_PARITY_ERROR 3011
88
+
89
+ // Memory related errors
90
+ #define STORJ_MEMORY_ERROR 4000
91
+ #define STORJ_MAPPING_ERROR 4001
92
+ #define STORJ_UNMAPPING_ERROR 4002
93
+
94
+ // Queue related errors
95
+ #define STORJ_QUEUE_ERROR 5000
96
+
97
+ // Meta related errors 6000 to 6999
98
+ #define STORJ_META_ENCRYPTION_ERROR 6000
99
+ #define STORJ_META_DECRYPTION_ERROR 6001
100
+
101
+ // Miscellaneous errors
102
+ #define STORJ_HEX_DECODE_ERROR 7000
103
+
104
+ // Exchange report codes
105
+ #define STORJ_REPORT_SUCCESS 1000
106
+ #define STORJ_REPORT_FAILURE 1100
107
+
108
+ // Exchange report messages
109
+ #define STORJ_REPORT_FAILED_INTEGRITY "FAILED_INTEGRITY"
110
+ #define STORJ_REPORT_SHARD_DOWNLOADED "SHARD_DOWNLOADED"
111
+ #define STORJ_REPORT_SHARD_UPLOADED "SHARD_UPLOADED"
112
+ #define STORJ_REPORT_DOWNLOAD_ERROR "DOWNLOAD_ERROR"
113
+ #define STORJ_REPORT_UPLOAD_ERROR "TRANSFER_FAILED"
114
+
115
+ #define STORJ_SHARD_CHALLENGES 4
116
+ #define STORJ_LOW_SPEED_LIMIT 30720L
117
+ #define STORJ_LOW_SPEED_TIME 20L
118
+ #define STORJ_HTTP_TIMEOUT 60L
119
+
120
+ typedef struct {
121
+ uint8_t *encryption_ctr;
122
+ uint8_t *encryption_key;
123
+ struct aes256_ctx *ctx;
124
+ } storj_encryption_ctx_t ;
125
+
126
+ typedef enum {
127
+ STORJ_REPORT_NOT_PREPARED = 0,
128
+ STORJ_REPORT_AWAITING_SEND = 1,
129
+ STORJ_REPORT_SENDING = 2,
130
+ STORJ_REPORT_SENT = 3
131
+ } exchange_report_status_t;
132
+
133
+ /** @brief Bridge configuration options
134
+ *
135
+ * Proto can be "http" or "https", and the user/pass are used for
136
+ * basic authentication to a Storj bridge.
137
+ */
138
+ typedef struct {
139
+ const char *proto;
140
+ const char *host;
141
+ int port;
142
+ const char *user;
143
+ const char *pass;
144
+ } storj_bridge_options_t;
145
+
146
+ /** @brief File encryption options
147
+ *
148
+ * The mnemonic is a BIP39 secret code used for generating keys for file
149
+ * encryption and decryption.
150
+ */
151
+ typedef struct storj_encrypt_options {
152
+ const char *mnemonic;
153
+ } storj_encrypt_options_t;
154
+
155
+
156
+
157
+ /** @brief HTTP configuration options
158
+ *
159
+ * Settings for making HTTP requests
160
+ */
161
+ typedef struct storj_http_options {
162
+ const char *user_agent;
163
+ const char *proxy_url;
164
+ const char *cainfo_path;
165
+ uint64_t low_speed_limit;
166
+ uint64_t low_speed_time;
167
+ uint64_t timeout;
168
+ } storj_http_options_t;
169
+
170
+ /** @brief A function signature for logging
171
+ */
172
+ typedef void (*storj_logger_fn)(const char *message, int level, void *handle);
173
+
174
+ /** @brief Logging configuration options
175
+ *
176
+ * Settings for logging
177
+ */
178
+ typedef struct storj_log_options {
179
+ storj_logger_fn logger;
180
+ int level;
181
+ } storj_log_options_t;
182
+
183
+ /** @brief A function signature for logging
184
+ */
185
+ typedef void (*storj_logger_format_fn)(storj_log_options_t *options,
186
+ void *handle,
187
+ const char *format, ...);
188
+
189
+ /** @brief Functions for all logging levels
190
+ */
191
+ typedef struct storj_log_levels {
192
+ storj_logger_format_fn debug;
193
+ storj_logger_format_fn info;
194
+ storj_logger_format_fn warn;
195
+ storj_logger_format_fn error;
196
+ } storj_log_levels_t;
197
+
198
+ /** @brief A structure for a Storj user environment.
199
+ *
200
+ * This is the highest level structure and holds many commonly used options
201
+ * and the event loop for queuing work.
202
+ */
203
+ typedef struct storj_env {
204
+ storj_bridge_options_t *bridge_options;
205
+ storj_encrypt_options_t *encrypt_options;
206
+ storj_http_options_t *http_options;
207
+ storj_log_options_t *log_options;
208
+ const char *tmp_path;
209
+ uv_loop_t *loop;
210
+ storj_log_levels_t *log;
211
+ } storj_env_t;
212
+
213
+ /** @brief A structure for queueing json request work
214
+ */
215
+ typedef struct {
216
+ storj_http_options_t *http_options;
217
+ storj_bridge_options_t *options;
218
+ char *method;
219
+ char *path;
220
+ bool auth;
221
+ struct json_object *body;
222
+ struct json_object *response;
223
+ int error_code;
224
+ int status_code;
225
+ void *handle;
226
+ } json_request_t;
227
+
228
+ /** @brief A structure for that describes a bucket
229
+ */
230
+ typedef struct {
231
+ const char *created;
232
+ const char *name;
233
+ const char *id;
234
+ bool decrypted;
235
+ } storj_bucket_meta_t;
236
+
237
+ /** @brief A structure for queueing create bucket request work
238
+ */
239
+ typedef struct {
240
+ storj_http_options_t *http_options;
241
+ storj_encrypt_options_t *encrypt_options;
242
+ storj_bridge_options_t *bridge_options;
243
+ const char *bucket_name;
244
+ const char *encrypted_bucket_name;
245
+ struct json_object *response;
246
+ storj_bucket_meta_t *bucket;
247
+ int error_code;
248
+ int status_code;
249
+ void *handle;
250
+ } create_bucket_request_t;
251
+
252
+ /** @brief A structure for queueing list buckets request work
253
+ */
254
+ typedef struct {
255
+ storj_http_options_t *http_options;
256
+ storj_encrypt_options_t *encrypt_options;
257
+ storj_bridge_options_t *options;
258
+ char *method;
259
+ char *path;
260
+ bool auth;
261
+ struct json_object *body;
262
+ struct json_object *response;
263
+ storj_bucket_meta_t *buckets;
264
+ uint32_t total_buckets;
265
+ int error_code;
266
+ int status_code;
267
+ void *handle;
268
+ } get_buckets_request_t;
269
+
270
+ /** @brief A structure for queueing get bucket request work
271
+ */
272
+ typedef struct {
273
+ storj_http_options_t *http_options;
274
+ storj_encrypt_options_t *encrypt_options;
275
+ storj_bridge_options_t *options;
276
+ char *method;
277
+ char *path;
278
+ bool auth;
279
+ struct json_object *body;
280
+ struct json_object *response;
281
+ storj_bucket_meta_t *bucket;
282
+ int error_code;
283
+ int status_code;
284
+ void *handle;
285
+ } get_bucket_request_t;
286
+
287
+ /** @brief A structure for that describes a bucket entry/file
288
+ */
289
+ typedef struct {
290
+ const char *created;
291
+ const char *filename;
292
+ const char *mimetype;
293
+ const char *erasure;
294
+ uint64_t size;
295
+ const char *hmac;
296
+ const char *id;
297
+ bool decrypted;
298
+ const char *index;
299
+ } storj_file_meta_t;
300
+
301
+ /** @brief A structure for queueing list files request work
302
+ */
303
+ typedef struct {
304
+ storj_http_options_t *http_options;
305
+ storj_encrypt_options_t *encrypt_options;
306
+ storj_bridge_options_t *options;
307
+ const char *bucket_id;
308
+ char *method;
309
+ char *path;
310
+ bool auth;
311
+ struct json_object *body;
312
+ struct json_object *response;
313
+ storj_file_meta_t *files;
314
+ uint32_t total_files;
315
+ int error_code;
316
+ int status_code;
317
+ void *handle;
318
+ } list_files_request_t;
319
+
320
+ typedef enum {
321
+ BUCKET_PUSH,
322
+ BUCKET_PULL
323
+ } storj_bucket_op_t;
324
+
325
+ static const char *BUCKET_OP[] = { "PUSH", "PULL" };
326
+
327
+ /** @brief A data structure that represents an exchange report
328
+ *
329
+ * These are sent at the end of an exchange with a farmer to report the
330
+ * performance and reliability of farmers.
331
+ */
332
+ typedef struct {
333
+ char *data_hash;
334
+ char *reporter_id;
335
+ char *farmer_id;
336
+ char *client_id;
337
+ uint64_t start;
338
+ uint64_t end;
339
+ unsigned int code;
340
+ char *message;
341
+ unsigned int send_status;
342
+ unsigned int send_count;
343
+ uint32_t pointer_index;
344
+ } storj_exchange_report_t;
345
+
346
+ /** @brief A function signature for download/upload progress callback
347
+ */
348
+ typedef void (*storj_progress_cb)(double progress,
349
+ uint64_t bytes,
350
+ uint64_t total_bytes,
351
+ void *handle);
352
+
353
+ /** @brief A function signature for a download complete callback
354
+ */
355
+ typedef void (*storj_finished_download_cb)(int status, FILE *fd, void *handle);
356
+
357
+ /** @brief A function signature for an upload complete callback
358
+ */
359
+ typedef void (*storj_finished_upload_cb)(int error_status, char *file_id, void *handle);
360
+
361
+ /** @brief A structure that represents a pointer to a shard
362
+ *
363
+ * A shard is an encrypted piece of a file, a pointer holds all necessary
364
+ * information to retrieve a shard from a farmer, including the IP address
365
+ * and port of the farmer, as well as a token indicating a transfer has been
366
+ * authorized. Other necessary information such as the expected hash of the
367
+ * data, and the index position in the file is also included.
368
+ *
369
+ * The data can be replaced with new farmer contact, in case of failure, and the
370
+ * total number of replacements can be tracked.
371
+ */
372
+ typedef struct {
373
+ unsigned int replace_count;
374
+ char *token;
375
+ char *shard_hash;
376
+ uint32_t index;
377
+ int status;
378
+ uint64_t size;
379
+ bool parity;
380
+ uint64_t downloaded_size;
381
+ char *farmer_id;
382
+ char *farmer_address;
383
+ int farmer_port;
384
+ storj_exchange_report_t *report;
385
+ uv_work_t *work;
386
+ } storj_pointer_t;
387
+
388
+ /** @brief A structure for file upload options
389
+ */
390
+ typedef struct {
391
+ int prepare_frame_limit;
392
+ int push_frame_limit;
393
+ int push_shard_limit;
394
+ bool rs;
395
+ const char *index;
396
+ const char *bucket_id;
397
+ const char *file_name;
398
+ FILE *fd;
399
+ } storj_upload_opts_t;
400
+
401
+ /** @brief A structure that keeps state between multiple worker threads,
402
+ * and for referencing a download to apply actions to an in-progress download.
403
+ *
404
+ * After work has been completed in a thread, its after work callback will
405
+ * update and modify the state and then queue the next set of work based on the
406
+ * changes, and added to the event loop. The state is all managed within one
407
+ * thread, the event loop thread, and any work that is performed in another
408
+ * thread should not modify this structure directly, but should pass a
409
+ * reference to it, so that once the work is complete the state can be updated.
410
+ */
411
+ typedef struct {
412
+ uint64_t total_bytes;
413
+ storj_file_meta_t *info;
414
+ bool requesting_info;
415
+ uint32_t info_fail_count;
416
+ storj_env_t *env;
417
+ const char *file_id;
418
+ const char *bucket_id;
419
+ FILE *destination;
420
+ storj_progress_cb progress_cb;
421
+ storj_finished_download_cb finished_cb;
422
+ bool finished;
423
+ bool canceled;
424
+ uint64_t shard_size;
425
+ uint32_t total_shards;
426
+ int download_max_concurrency;
427
+ uint32_t completed_shards;
428
+ uint32_t resolving_shards;
429
+ storj_pointer_t *pointers;
430
+ char *excluded_farmer_ids;
431
+ uint32_t total_pointers;
432
+ uint32_t total_parity_pointers;
433
+ bool rs;
434
+ bool recovering_shards;
435
+ bool truncated;
436
+ bool pointers_completed;
437
+ uint32_t pointer_fail_count;
438
+ bool requesting_pointers;
439
+ int error_status;
440
+ bool writing;
441
+ uint8_t *decrypt_key;
442
+ uint8_t *decrypt_ctr;
443
+ const char *hmac;
444
+ uint32_t pending_work_count;
445
+ storj_log_levels_t *log;
446
+ void *handle;
447
+ } storj_download_state_t;
448
+
449
+
450
+ typedef struct {
451
+ char *hash;
452
+ uint8_t *challenges[STORJ_SHARD_CHALLENGES][32];
453
+ char *challenges_as_str[STORJ_SHARD_CHALLENGES][64 + 1];
454
+ // Merkle Tree leaves. Each leaf is size of RIPEMD160 hash
455
+ char *tree[2 * STORJ_SHARD_CHALLENGES - 1][20 * 2 + 1];
456
+ int index;
457
+ bool is_parity;
458
+ uint64_t size;
459
+ } shard_meta_t;
460
+
461
+ typedef struct {
462
+ char *token;
463
+ char *farmer_user_agent;
464
+ char *farmer_protocol;
465
+ char *farmer_address;
466
+ char *farmer_port;
467
+ char *farmer_node_id;
468
+ } farmer_pointer_t;
469
+
470
+ typedef struct {
471
+ int progress;
472
+ int push_frame_request_count;
473
+ int push_shard_request_count;
474
+ int index;
475
+ farmer_pointer_t *pointer;
476
+ shard_meta_t *meta;
477
+ storj_exchange_report_t *report;
478
+ uint64_t uploaded_size;
479
+ uv_work_t *work;
480
+ } shard_tracker_t;
481
+
482
+ typedef struct {
483
+ storj_env_t *env;
484
+ uint32_t shard_concurrency;
485
+ const char *index;
486
+ const char *file_name;
487
+ char *file_id;
488
+ const char *encrypted_file_name;
489
+ FILE *original_file;
490
+ uint64_t file_size;
491
+ const char *bucket_id;
492
+ char *bucket_key;
493
+ uint32_t completed_shards;
494
+ uint32_t total_shards;
495
+ uint32_t total_data_shards;
496
+ uint32_t total_parity_shards;
497
+ uint64_t shard_size;
498
+ uint64_t total_bytes;
499
+ uint64_t uploaded_bytes;
500
+ char *exclude;
501
+ char *frame_id;
502
+ char *hmac_id;
503
+ uint8_t *encryption_key;
504
+ uint8_t *encryption_ctr;
505
+
506
+ // TODO: change this to opts or env
507
+ bool rs;
508
+ bool awaiting_parity_shards;
509
+ char *parity_file_path;
510
+ FILE *parity_file;
511
+ char *encrypted_file_path;
512
+ FILE *encrypted_file;
513
+ bool creating_encrypted_file;
514
+
515
+ bool requesting_frame;
516
+ bool completed_upload;
517
+ bool creating_bucket_entry;
518
+ bool received_all_pointers;
519
+ bool final_callback_called;
520
+ bool canceled;
521
+ bool bucket_verified;
522
+ bool file_verified;
523
+
524
+ bool progress_finished;
525
+
526
+ int push_shard_limit;
527
+ int push_frame_limit;
528
+ int prepare_frame_limit;
529
+
530
+ int frame_request_count;
531
+ int add_bucket_entry_count;
532
+ int bucket_verify_count;
533
+ int file_verify_count;
534
+ int create_encrypted_file_count;
535
+
536
+ storj_progress_cb progress_cb;
537
+ storj_finished_upload_cb finished_cb;
538
+ int error_status;
539
+ storj_log_levels_t *log;
540
+ void *handle;
541
+ shard_tracker_t *shard;
542
+ int pending_work_count;
543
+ } storj_upload_state_t;
544
+
545
+ /**
546
+ * @brief Initialize a Storj environment
547
+ *
548
+ * This will setup an event loop for queueing further actions, as well
549
+ * as define necessary configuration options for communicating with Storj
550
+ * bridge, and for encrypting/decrypting files.
551
+ *
552
+ * @param[in] options - Storj Bridge API options
553
+ * @param[in] encrypt_options - File encryption options
554
+ * @param[in] http_options - HTTP settings
555
+ * @param[in] log_options - Logging settings
556
+ * @return A null value on error, otherwise a storj_env pointer.
557
+ */
558
+ STORJ_API storj_env_t *storj_init_env(storj_bridge_options_t *options,
559
+ storj_encrypt_options_t *encrypt_options,
560
+ storj_http_options_t *http_options,
561
+ storj_log_options_t *log_options);
562
+
563
+
564
+ /**
565
+ * @brief Destroy a Storj environment
566
+ *
567
+ * This will free all memory for the Storj environment and zero out any memory
568
+ * with sensitive information, such as passwords and encryption keys.
569
+ *
570
+ * The event loop must be closed before this method should be used.
571
+ *
572
+ * @param [in] env
573
+ */
574
+ STORJ_API int storj_destroy_env(storj_env_t *env);
575
+
576
+ /**
577
+ * @brief Will encrypt and write options to disk
578
+ *
579
+ * This will encrypt bridge and encryption options to disk using a key
580
+ * derivation function on a passphrase.
581
+ *
582
+ * @param[in] filepath - The file path to save the options
583
+ * @param[in] passphrase - Used to encrypt options to disk
584
+ * @param[in] bridge_user - The bridge username
585
+ * @param[in] bridge_pass - The bridge password
586
+ * @param[in] mnemonic - The file encryption mnemonic
587
+ * @return A non-zero value on error, zero on success.
588
+ */
589
+ STORJ_API int storj_encrypt_write_auth(const char *filepath,
590
+ const char *passhrase,
591
+ const char *bridge_user,
592
+ const char *bridge_pass,
593
+ const char *mnemonic);
594
+
595
+
596
+ /**
597
+ * @brief Will encrypt options to disk
598
+ *
599
+ * This will encrypt bridge and encryption using a key
600
+ * derivation function on a passphrase.
601
+ *
602
+ * @param[in] passphrase - Used to encrypt options to disk
603
+ * @param[in] bridge_user - The bridge username
604
+ * @param[in] bridge_pass - The bridge password
605
+ * @param[in] mnemonic - The file encryption mnemonic
606
+ * @param[out] buffer - The destination buffer
607
+ * @return A non-zero value on error, zero on success.
608
+ */
609
+ STORJ_API int storj_encrypt_auth(const char *passhrase,
610
+ const char *bridge_user,
611
+ const char *bridge_pass,
612
+ const char *mnemonic,
613
+ char **buffer);
614
+
615
+ /**
616
+ * @brief Will read and decrypt options from disk
617
+ *
618
+ * This will decrypt bridge and encryption options from disk from
619
+ * the passphrase.
620
+ *
621
+ * @param[in] filepath - The file path to read the options
622
+ * @param[in] passphrase - Used to encrypt options to disk
623
+ * @param[out] bridge_user - The bridge username
624
+ * @param[out] bridge_pass - The bridge password
625
+ * @param[out] mnemonic - The file encryption mnemonic
626
+ * @return A non-zero value on error, zero on success.
627
+ */
628
+ STORJ_API int storj_decrypt_read_auth(const char *filepath,
629
+ const char *passphrase,
630
+ char **bridge_user,
631
+ char **bridge_pass,
632
+ char **mnemonic);
633
+
634
+ /**
635
+ * @brief Will decrypt options
636
+ *
637
+ * This will decrypt bridge and encryption options using key derived
638
+ * from a passphrase.
639
+ *
640
+ * @param[in] buffer - The encrypted buffer
641
+ * @param[in] passphrase - Used to encrypt options to disk
642
+ * @param[out] bridge_user - The bridge username
643
+ * @param[out] bridge_pass - The bridge password
644
+ * @param[out] mnemonic - The file encryption mnemonic
645
+ * @return A non-zero value on error, zero on success.
646
+ */
647
+ STORJ_API int storj_decrypt_auth(const char *buffer,
648
+ const char *passphrase,
649
+ char **bridge_user,
650
+ char **bridge_pass,
651
+ char **mnemonic);
652
+
653
+ /**
654
+ * @brief Will get the current unix timestamp in milliseconds
655
+ *
656
+ * @return A unix timestamp
657
+ */
658
+ STORJ_API uint64_t storj_util_timestamp();
659
+
660
+ /**
661
+ * @brief Will generate a new random mnemonic
662
+ *
663
+ * This will generate a new random mnemonic with 128 to 256 bits
664
+ * of entropy.
665
+ *
666
+ * @param[in] strength - The bits of entropy
667
+ * @param[out] buffer - The destination of the mnemonic
668
+ * @return A non-zero value on error, zero on success.
669
+ */
670
+ STORJ_API int storj_mnemonic_generate(int strength, char **buffer);
671
+
672
+ /**
673
+ * @brief Will check that a mnemonic is valid
674
+ *
675
+ * This will check that a mnemonic has been entered correctly by verifying
676
+ * the checksum, and that words are a part of the list.
677
+ *
678
+ * @param[in] strength - The bits of entropy
679
+ * @return Will return true on success and false failure
680
+ */
681
+ STORJ_API bool storj_mnemonic_check(const char *mnemonic);
682
+
683
+ /**
684
+ * @brief Get the error message for an error code
685
+ *
686
+ * This function will return a error message associated with a storj
687
+ * error code.
688
+ *
689
+ * @param[in] error_code The storj error code integer
690
+ * @return A char pointer with error message
691
+ */
692
+ STORJ_API char *storj_strerror(int error_code);
693
+
694
+ /**
695
+ * @brief Get Storj bridge API information.
696
+ *
697
+ * This function will get general information about the storj bridge api.
698
+ * The network i/o is performed in a thread pool with a libuv loop, and the
699
+ * response is available in the first argument to the callback function.
700
+ *
701
+ * @param[in] env The storj environment struct
702
+ * @param[in] handle A pointer that will be available in the callback
703
+ * @param[in] cb A function called with response when complete
704
+ * @return A non-zero error value on failure and 0 on success.
705
+ */
706
+ STORJ_API int storj_bridge_get_info(storj_env_t *env,
707
+ void *handle,
708
+ uv_after_work_cb cb);
709
+
710
+ /**
711
+ * @brief List available buckets for a user.
712
+ *
713
+ * @param[in] env The storj environment struct
714
+ * @param[in] handle A pointer that will be available in the callback
715
+ * @param[in] cb A function called with response when complete
716
+ * @return A non-zero error value on failure and 0 on success.
717
+ */
718
+ STORJ_API int storj_bridge_get_buckets(storj_env_t *env,
719
+ void *handle,
720
+ uv_after_work_cb cb);
721
+
722
+ /**
723
+ * @brief Will free all structs for get buckets request
724
+ *
725
+ * @param[in] req - The work request from storj_bridge_get_buckets callback
726
+ */
727
+ STORJ_API void storj_free_get_buckets_request(get_buckets_request_t *req);
728
+
729
+ /**
730
+ * @brief Create a bucket.
731
+ *
732
+ * @param[in] env The storj environment struct
733
+ * @param[in] name The name of the bucket
734
+ * @param[in] handle A pointer that will be available in the callback
735
+ * @param[in] cb A function called with response when complete
736
+ * @return A non-zero error value on failure and 0 on success.
737
+ */
738
+ STORJ_API int storj_bridge_create_bucket(storj_env_t *env,
739
+ const char *name,
740
+ void *handle,
741
+ uv_after_work_cb cb);
742
+
743
+ /**
744
+ * @brief Delete a bucket.
745
+ *
746
+ * @param[in] env The storj environment struct
747
+ * @param[in] id The bucket id
748
+ * @param[in] handle A pointer that will be available in the callback
749
+ * @param[in] cb A function called with response when complete
750
+ * @return A non-zero error value on failure and 0 on success.
751
+ */
752
+ STORJ_API int storj_bridge_delete_bucket(storj_env_t *env,
753
+ const char *id,
754
+ void *handle,
755
+ uv_after_work_cb cb);
756
+
757
+ /**
758
+ * @brief Get a info of specific bucket.
759
+ *
760
+ * @param[in] env The storj environment struct
761
+ * @param[in] bucket_id The bucket id
762
+ * @param[in] handle A pointer that will be available in the callback
763
+ * @param[in] cb A function called with response when complete
764
+ * @return A non-zero error value on failure and 0 on success.
765
+ */
766
+ STORJ_API int storj_bridge_get_bucket(storj_env_t *env,
767
+ const char *id,
768
+ void *handle,
769
+ uv_after_work_cb cb);
770
+
771
+ /**
772
+ * @brief Will free all structs for get bucket request
773
+ *
774
+ * @param[in] req - The work request from storj_bridge_get_bucket callback
775
+ */
776
+ STORJ_API void storj_free_get_bucket_request(get_bucket_request_t *req);
777
+
778
+ /**
779
+ * @brief Get a list of all files in a bucket.
780
+ *
781
+ * @param[in] env The storj environment struct
782
+ * @param[in] id The bucket id
783
+ * @param[in] handle A pointer that will be available in the callback
784
+ * @param[in] cb A function called with response when complete
785
+ * @return A non-zero error value on failure and 0 on success.
786
+ */
787
+ STORJ_API int storj_bridge_list_files(storj_env_t *env,
788
+ const char *id,
789
+ void *handle,
790
+ uv_after_work_cb cb);
791
+
792
+ /**
793
+ * @brief Will free all structs for list files request
794
+ *
795
+ * @param[in] req - The work request from storj_bridge_list_files callback
796
+ */
797
+ STORJ_API void storj_free_list_files_request(list_files_request_t *req);
798
+
799
+ /**
800
+ * @brief Create a PUSH or PULL bucket token.
801
+ *
802
+ * @param[in] env The storj environment struct
803
+ * @param[in] bucket_id The bucket id
804
+ * @param[in] operation The type of operation PUSH or PULL
805
+ * @param[in] handle A pointer that will be available in the callback
806
+ * @param[in] cb A function called with response when complete
807
+ * @return A non-zero error value on failure and 0 on success.
808
+ */
809
+ STORJ_API int storj_bridge_create_bucket_token(storj_env_t *env,
810
+ const char *bucket_id,
811
+ storj_bucket_op_t operation,
812
+ void *handle,
813
+ uv_after_work_cb cb);
814
+
815
+ /**
816
+ * @brief Get pointers with locations to file shards.
817
+ *
818
+ * @param[in] env The storj environment struct
819
+ * @param[in] bucket_id The bucket id
820
+ * @param[in] file_id The bucket id
821
+ * @param[in] handle A pointer that will be available in the callback
822
+ * @param[in] cb A function called with response when complete
823
+ * @return A non-zero error value on failure and 0 on success.
824
+ */
825
+ STORJ_API int storj_bridge_get_file_pointers(storj_env_t *env,
826
+ const char *bucket_id,
827
+ const char *file_id,
828
+ void *handle,
829
+ uv_after_work_cb cb);
830
+
831
+ /**
832
+ * @brief Delete a file in a bucket.
833
+ *
834
+ * @param[in] env The storj environment struct
835
+ * @param[in] bucket_id The bucket id
836
+ * @param[in] file_id The bucket id
837
+ * @param[in] handle A pointer that will be available in the callback
838
+ * @param[in] cb A function called with response when complete
839
+ * @return A non-zero error value on failure and 0 on success.
840
+ */
841
+ STORJ_API int storj_bridge_delete_file(storj_env_t *env,
842
+ const char *bucket_id,
843
+ const char *file_id,
844
+ void *handle,
845
+ uv_after_work_cb cb);
846
+
847
+ /**
848
+ * @brief Create a file frame
849
+ *
850
+ * @param[in] env The storj environment struct
851
+ * @param[in] handle A pointer that will be available in the callback
852
+ * @param[in] cb A function called with response when complete
853
+ * @return A non-zero error value on failure and 0 on success.
854
+ */
855
+ STORJ_API int storj_bridge_create_frame(storj_env_t *env,
856
+ void *handle,
857
+ uv_after_work_cb cb);
858
+
859
+ /**
860
+ * @brief List available file frames
861
+ *
862
+ * @param[in] env The storj environment struct
863
+ * @param[in] handle A pointer that will be available in the callback
864
+ * @param[in] cb A function called with response when complete
865
+ * @return A non-zero error value on failure and 0 on success.
866
+ */
867
+ STORJ_API int storj_bridge_get_frames(storj_env_t *env,
868
+ void *handle,
869
+ uv_after_work_cb cb);
870
+
871
+ /**
872
+ * @brief Get information for a file frame
873
+ *
874
+ * @param[in] env The storj environment struct
875
+ * @param[in] frame_id The frame id
876
+ * @param[in] handle A pointer that will be available in the callback
877
+ * @param[in] cb A function called with response when complete
878
+ * @return A non-zero error value on failure and 0 on success.
879
+ */
880
+ STORJ_API int storj_bridge_get_frame(storj_env_t *env,
881
+ const char *frame_id,
882
+ void *handle,
883
+ uv_after_work_cb cb);
884
+
885
+ /**
886
+ * @brief Delete a file frame
887
+ *
888
+ * @param[in] env The storj environment struct
889
+ * @param[in] frame_id The frame id
890
+ * @param[in] handle A pointer that will be available in the callback
891
+ * @param[in] cb A function called with response when complete
892
+ * @return A non-zero error value on failure and 0 on success.
893
+ */
894
+ STORJ_API int storj_bridge_delete_frame(storj_env_t *env,
895
+ const char *frame_id,
896
+ void *handle,
897
+ uv_after_work_cb cb);
898
+
899
+ /**
900
+ * @brief Get metadata for a file
901
+ *
902
+ * @param[in] env The storj environment struct
903
+ * @param[in] bucket_id The bucket id
904
+ * @param[in] file_id The bucket id
905
+ * @param[in] handle A pointer that will be available in the callback
906
+ * @param[in] cb A function called with response when complete
907
+ * @return A non-zero error value on failure and 0 on success.
908
+ */
909
+ STORJ_API int storj_bridge_get_file_info(storj_env_t *env,
910
+ const char *bucket_id,
911
+ const char *file_id,
912
+ void *handle,
913
+ uv_after_work_cb cb);
914
+
915
+ /**
916
+ * @brief Get mirror data for a file
917
+ *
918
+ * @param[in] env The storj environment struct
919
+ * @param[in] bucket_id The bucket id
920
+ * @param[in] file_id The bucket id
921
+ * @param[in] handle A pointer that will be available in the callback
922
+ * @param[in] cb A function called with response when complete
923
+ * @return A non-zero error value on failure and 0 on success.
924
+ */
925
+ STORJ_API int storj_bridge_list_mirrors(storj_env_t *env,
926
+ const char *bucket_id,
927
+ const char *file_id,
928
+ void *handle,
929
+ uv_after_work_cb cb);
930
+
931
+ /**
932
+ * @brief Will cancel an upload
933
+ *
934
+ * @param[in] state A pointer to the the upload state
935
+ * @return A non-zero error value on failure and 0 on success.
936
+ */
937
+ STORJ_API int storj_bridge_store_file_cancel(storj_upload_state_t *state);
938
+
939
+ /**
940
+ * @brief Upload a file
941
+ *
942
+ * @param[in] env A pointer to environment
943
+ * @param[in] state A pointer to the the upload state
944
+ * @param[in] opts The options for the upload
945
+ * @param[in] handle A pointer that will be available in the callback
946
+ * @param[in] progress_cb Function called with progress updates
947
+ * @param[in] finished_cb Function called when download finished
948
+ * @return A non-zero error value on failure and 0 on success.
949
+ */
950
+ STORJ_API storj_upload_state_t *storj_bridge_store_file(storj_env_t *env,
951
+ storj_upload_opts_t *opts,
952
+ void *handle,
953
+ storj_progress_cb progress_cb,
954
+ storj_finished_upload_cb finished_cb);
955
+
956
+ /**
957
+ * @brief Will cancel a download
958
+ *
959
+ * @param[in] state A pointer to the the download state
960
+ * @return A non-zero error value on failure and 0 on success.
961
+ */
962
+ STORJ_API int storj_bridge_resolve_file_cancel(storj_download_state_t *state);
963
+
964
+ /**
965
+ * @brief Download a file
966
+ *
967
+ * @param[in] env A pointer to environment
968
+ * @param[in] state A pointer to the the download state
969
+ * @param[in] bucket_id Character array of bucket id
970
+ * @param[in] file_id Character array of file id
971
+ * @param[in] destination File descriptor of the destination
972
+ * @param[in] handle A pointer that will be available in the callback
973
+ * @param[in] progress_cb Function called with progress updates
974
+ * @param[in] finished_cb Function called when download finished
975
+ * @return A non-zero error value on failure and 0 on success.
976
+ */
977
+ STORJ_API storj_download_state_t *storj_bridge_resolve_file(storj_env_t *env,
978
+ const char *bucket_id,
979
+ const char *file_id,
980
+ FILE *destination,
981
+ void *handle,
982
+ storj_progress_cb progress_cb,
983
+ storj_finished_download_cb finished_cb);
984
+
985
+ /**
986
+ * @brief Register a user
987
+ *
988
+ * @param[in] env The storj environment struct
989
+ * @param[in] email the user's email
990
+ * @param[in] password the user's password
991
+ * @param[in] handle A pointer that will be available in the callback
992
+ * @param[in] cb A function called with response when complete
993
+ * @return A non-zero error value on failure and 0 on success.
994
+ */
995
+ STORJ_API int storj_bridge_register(storj_env_t *env,
996
+ const char *email,
997
+ const char *password,
998
+ void *handle,
999
+ uv_after_work_cb cb);
1000
+
1001
+ static inline char separator()
1002
+ {
1003
+ #ifdef _WIN32
1004
+ return '\\';
1005
+ #else
1006
+ return '/';
1007
+ #endif
1008
+ }
1009
+
1010
+ #ifdef __cplusplus
1011
+ }
1012
+ #endif
1013
+
1014
+ #endif /* STORJ_H */