2012-09-09 04:20:09 +00:00
|
|
|
/*
|
|
|
|
Serval Mesh Software
|
|
|
|
Copyright (C) 2010-2012 Paul Gardner-Stephen
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2013-12-09 07:15:47 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2012-09-09 04:20:09 +00:00
|
|
|
#include "serval.h"
|
|
|
|
#include "rhizome.h"
|
2014-02-03 11:10:05 +00:00
|
|
|
#include "httpd.h"
|
2012-12-04 03:42:28 +00:00
|
|
|
#include "conf.h"
|
2012-09-09 04:20:09 +00:00
|
|
|
#include "str.h"
|
2012-09-28 08:23:20 +00:00
|
|
|
#include "strbuf.h"
|
|
|
|
#include "strbuf_helpers.h"
|
2013-12-09 07:15:47 +00:00
|
|
|
#include "socket.h"
|
|
|
|
|
2015-08-24 03:18:00 +00:00
|
|
|
DECLARE_HANDLER("/rhizome/import", rhizome_direct_import);
|
|
|
|
DECLARE_HANDLER("/rhizome/enquiry", rhizome_direct_enquiry);
|
|
|
|
DECLARE_HANDLER("/rhizome/", rhizome_direct_dispatch);
|
2012-09-09 04:20:09 +00:00
|
|
|
|
2015-08-31 06:01:58 +00:00
|
|
|
static char PART_MANIFEST[] = "manifest";
|
|
|
|
static char PART_PAYLOAD[] = "payload";
|
|
|
|
// TODO: "data" is deprecated in favour of "payload" to match the restful API
|
|
|
|
static char PART_DATA[] = "data";
|
|
|
|
|
2014-01-31 07:32:41 +00:00
|
|
|
static int _form_temporary_file_path(struct __sourceloc __whence, httpd_request *r, char *pathbuf, size_t bufsiz, const char *field)
|
2012-09-09 04:20:09 +00:00
|
|
|
{
|
2013-10-16 07:55:58 +00:00
|
|
|
// TODO: use a temporary directory
|
2015-08-31 05:38:13 +00:00
|
|
|
return formf_serval_tmp_path(pathbuf, bufsiz,
|
|
|
|
"rhizomedirect.%d.%s", r->http.alarm.poll.fd, field);
|
2012-09-09 04:20:09 +00:00
|
|
|
}
|
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
#define form_temporary_file_path(r,buf,field) _form_temporary_file_path(__WHENCE__, (r), (buf), sizeof(buf), (field))
|
2012-09-11 03:39:40 +00:00
|
|
|
|
2014-01-31 07:32:41 +00:00
|
|
|
static void rhizome_direct_clear_temporary_files(httpd_request *r)
|
2013-10-16 07:55:58 +00:00
|
|
|
{
|
2015-08-31 06:01:58 +00:00
|
|
|
const char *fields[] = { PART_MANIFEST, PART_PAYLOAD };
|
2013-10-16 07:55:58 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i != NELS(fields); ++i) {
|
|
|
|
char path[1024];
|
|
|
|
if (form_temporary_file_path(r, path, fields[i]) != -1)
|
|
|
|
if (unlink(path) == -1 && errno != ENOENT)
|
|
|
|
WARNF_perror("unlink(%s)", alloca_str_toprint(path));
|
|
|
|
}
|
|
|
|
}
|
2012-09-11 03:39:40 +00:00
|
|
|
|
2015-12-07 11:30:52 +00:00
|
|
|
static void http_request_rhizome_bundle_status_response(httpd_request *r, struct rhizome_bundle_result result, rhizome_manifest *m)
|
|
|
|
{
|
|
|
|
int http_status = 500;
|
|
|
|
switch (result.status) {
|
|
|
|
case RHIZOME_BUNDLE_STATUS_NEW:
|
|
|
|
http_status = 201; // Created
|
|
|
|
break;
|
|
|
|
case RHIZOME_BUNDLE_STATUS_DUPLICATE:
|
|
|
|
case RHIZOME_BUNDLE_STATUS_SAME:
|
|
|
|
http_status = 200; // OK
|
|
|
|
break;
|
|
|
|
case RHIZOME_BUNDLE_STATUS_OLD:
|
|
|
|
case RHIZOME_BUNDLE_STATUS_NO_ROOM:
|
|
|
|
http_status = 202; // Accepted
|
|
|
|
break;
|
|
|
|
case RHIZOME_BUNDLE_STATUS_INVALID:
|
|
|
|
case RHIZOME_BUNDLE_STATUS_INCONSISTENT:
|
|
|
|
case RHIZOME_BUNDLE_STATUS_MANIFEST_TOO_BIG:
|
|
|
|
http_status = 422; // Unprocessable Entity
|
|
|
|
break;
|
|
|
|
case RHIZOME_BUNDLE_STATUS_BUSY:
|
|
|
|
http_status = 423; // Locked
|
|
|
|
break;
|
|
|
|
case RHIZOME_BUNDLE_STATUS_READONLY:
|
|
|
|
case RHIZOME_BUNDLE_STATUS_FAKE:
|
|
|
|
http_status = 419; // Authentication Timeout
|
|
|
|
break;
|
|
|
|
case RHIZOME_BUNDLE_STATUS_ERROR:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (m)
|
|
|
|
http_request_response_static(&r->http, http_status, CONTENT_TYPE_TEXT, (const char *)m->manifestdata, m->manifest_all_bytes);
|
|
|
|
else
|
|
|
|
http_request_simple_response(&r->http, http_status, rhizome_bundle_result_message(result));
|
|
|
|
}
|
|
|
|
|
2013-10-28 11:57:27 +00:00
|
|
|
static int rhizome_direct_import_end(struct http_request *hr)
|
2013-10-16 07:55:58 +00:00
|
|
|
{
|
2014-01-31 07:32:41 +00:00
|
|
|
httpd_request *r = (httpd_request *) hr;
|
2013-12-30 07:39:56 +00:00
|
|
|
if (!r->u.direct_import.received_manifest) {
|
2013-10-16 07:55:58 +00:00
|
|
|
http_request_simple_response(&r->http, 400, "Missing 'manifest' part");
|
|
|
|
return 0;
|
|
|
|
}
|
2013-12-30 07:39:56 +00:00
|
|
|
if (!r->u.direct_import.received_data) {
|
2013-10-16 07:55:58 +00:00
|
|
|
http_request_simple_response(&r->http, 400, "Missing 'data' part");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Got a bundle to import */
|
|
|
|
char manifest_path[512];
|
|
|
|
char payload_path[512];
|
2015-08-31 06:01:58 +00:00
|
|
|
if ( form_temporary_file_path(r, manifest_path, PART_MANIFEST) == -1
|
|
|
|
|| form_temporary_file_path(r, payload_path, PART_PAYLOAD) == -1
|
2013-10-16 07:55:58 +00:00
|
|
|
) {
|
|
|
|
http_request_simple_response(&r->http, 500, "Internal Error: Buffer overrun");
|
|
|
|
return 0;
|
|
|
|
}
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(rhizome, "Call rhizome_bundle_import_files(%s, %s)",
|
|
|
|
alloca_str_toprint(manifest_path),
|
|
|
|
alloca_str_toprint(payload_path)
|
|
|
|
);
|
2013-10-16 07:55:58 +00:00
|
|
|
rhizome_manifest *m = rhizome_new_manifest();
|
2015-12-07 11:30:52 +00:00
|
|
|
if (!m) {
|
|
|
|
http_request_simple_response(&r->http, 429, "Manifest table full"); // Too Many Requests
|
2014-06-18 07:55:15 +00:00
|
|
|
return 0;
|
2013-10-16 07:55:58 +00:00
|
|
|
}
|
2015-12-07 11:30:52 +00:00
|
|
|
struct rhizome_bundle_result result = INVALID_RHIZOME_BUNDLE_RESULT;
|
2016-03-30 05:02:50 +00:00
|
|
|
result.status = rhizome_bundle_import_files(m, NULL, manifest_path, payload_path, 0);
|
2015-12-07 11:30:52 +00:00
|
|
|
rhizome_manifest_free(m);
|
|
|
|
rhizome_direct_clear_temporary_files(r);
|
|
|
|
http_request_rhizome_bundle_status_response(r, result, NULL);
|
|
|
|
rhizome_bundle_result_free(&result);
|
2013-10-16 07:55:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-10-02 23:25:04 +00:00
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
int rhizome_direct_enquiry_end(struct http_request *hr)
|
|
|
|
{
|
2014-01-31 07:32:41 +00:00
|
|
|
httpd_request *r = (httpd_request *) hr;
|
2013-12-30 07:39:56 +00:00
|
|
|
if (!r->u.direct_import.received_data) {
|
2013-10-16 07:55:58 +00:00
|
|
|
http_request_simple_response(&r->http, 400, "Missing 'data' part");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
char data_path[512];
|
2015-08-31 06:01:58 +00:00
|
|
|
if (form_temporary_file_path(r, data_path, PART_PAYLOAD) == -1) {
|
2013-10-16 07:55:58 +00:00
|
|
|
http_request_simple_response(&r->http, 500, "Internal Error: Buffer overrun");
|
|
|
|
return 0;
|
|
|
|
}
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(rhizome, "Call rhizome_direct_fill_response(%s)", alloca_str_toprint(data_path));
|
2013-10-16 07:55:58 +00:00
|
|
|
/* Read data buffer in, pass to rhizome direct for comparison with local
|
|
|
|
rhizome database, and send back responses. */
|
|
|
|
int fd = open(data_path, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
|
|
|
WHYF_perror("open(%s, O_RDONLY)", alloca_str_toprint(data_path));
|
|
|
|
/* Clean up after ourselves */
|
|
|
|
rhizome_direct_clear_temporary_files(r);
|
|
|
|
http_request_simple_response(&r->http, 500, "Internal Error: Couldn't read file");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
struct stat stat;
|
|
|
|
if (fstat(fd, &stat) == -1) {
|
|
|
|
WHYF_perror("stat(%d)", fd);
|
|
|
|
/* Clean up after ourselves */
|
|
|
|
close(fd);
|
|
|
|
rhizome_direct_clear_temporary_files(r);
|
|
|
|
http_request_simple_response(&r->http, 500, "Internal Error: Couldn't stat file");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
unsigned char *addr = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
|
|
|
|
if (addr==MAP_FAILED) {
|
|
|
|
WHYF_perror("mmap(NULL,%"PRId64",PROT_READ,MAP_SHARED,%d,0)", (int64_t) stat.st_size, fd);
|
|
|
|
/* Clean up after ourselves */
|
|
|
|
close(fd);
|
|
|
|
rhizome_direct_clear_temporary_files(r);
|
|
|
|
http_request_simple_response(&r->http, 500, "Internal Error: Couldn't mmap file");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Ask for a fill response. Regardless of the size of the set of BARs passed
|
|
|
|
to us, we will allow up to 64KB of response. */
|
|
|
|
rhizome_direct_bundle_cursor *c = rhizome_direct_get_fill_response(addr, stat.st_size, 65536);
|
|
|
|
munmap(addr,stat.st_size);
|
|
|
|
close(fd);
|
|
|
|
if (c) {
|
|
|
|
size_t bytes = c->buffer_offset_bytes + c->buffer_used;
|
|
|
|
if (http_request_set_response_bufsize(&r->http, bytes) == -1)
|
|
|
|
http_request_simple_response(&r->http, 500, "Internal Error: Out of memory");
|
|
|
|
else
|
2013-10-25 13:27:23 +00:00
|
|
|
http_request_response_static(&r->http, 200, "binary/octet-stream", (const char *)c->buffer, bytes);
|
2013-10-16 07:55:58 +00:00
|
|
|
rhizome_direct_bundle_iterator_free(&c);
|
|
|
|
} else
|
|
|
|
http_request_simple_response(&r->http, 500, "Internal Error: No response to enquiry");
|
|
|
|
rhizome_direct_clear_temporary_files(r);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-10-02 23:25:04 +00:00
|
|
|
|
2013-10-28 11:57:27 +00:00
|
|
|
static int rhizome_direct_addfile_end(struct http_request *hr)
|
2013-10-16 07:55:58 +00:00
|
|
|
{
|
2014-01-31 07:32:41 +00:00
|
|
|
httpd_request *r = (httpd_request *) hr;
|
2013-10-16 07:55:58 +00:00
|
|
|
// If given a file without a manifest, we should only accept if it we are configured to do so, and
|
|
|
|
// the connection is from localhost. Otherwise people could cause your servald to create
|
|
|
|
// arbitrary bundles, which would be bad.
|
2013-12-30 07:39:56 +00:00
|
|
|
if (!r->u.direct_import.received_manifest) {
|
2013-10-16 07:55:58 +00:00
|
|
|
char payload_path[512];
|
2015-08-31 06:01:58 +00:00
|
|
|
if (form_temporary_file_path(r, payload_path, PART_PAYLOAD) == -1) {
|
2013-10-16 07:55:58 +00:00
|
|
|
http_request_simple_response(&r->http, 500, "Internal Error: Buffer overrun");
|
|
|
|
return 0;
|
|
|
|
}
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(rhizome, "Call rhizome_store_payload_file(%s)", alloca_str_toprint(payload_path));
|
2013-10-16 07:55:58 +00:00
|
|
|
char manifestTemplate[1024];
|
|
|
|
manifestTemplate[0] = '\0';
|
|
|
|
if (config.rhizome.api.addfile.manifest_template_file[0]) {
|
2014-03-26 05:05:43 +00:00
|
|
|
if (!FORMF_SERVAL_ETC_PATH(manifestTemplate, "%s", config.rhizome.api.addfile.manifest_template_file)) {
|
2012-10-02 23:25:04 +00:00
|
|
|
rhizome_direct_clear_temporary_files(r);
|
2013-10-16 07:55:58 +00:00
|
|
|
http_request_simple_response(&r->http, 500, "Internal Error: Template path too long");
|
|
|
|
return 0;
|
2012-10-02 23:25:04 +00:00
|
|
|
}
|
2013-10-16 07:55:58 +00:00
|
|
|
if (access(manifestTemplate, R_OK) != 0) {
|
2012-10-02 23:25:04 +00:00
|
|
|
rhizome_direct_clear_temporary_files(r);
|
2013-10-16 07:55:58 +00:00
|
|
|
http_request_simple_response(&r->http, 500, "Internal Error: Cannot read template");
|
|
|
|
return 0;
|
2012-10-02 23:25:04 +00:00
|
|
|
}
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(rhizome, "Using manifest template %s", alloca_str_toprint(manifestTemplate));
|
2013-10-16 07:55:58 +00:00
|
|
|
}
|
|
|
|
rhizome_manifest *m = rhizome_new_manifest();
|
|
|
|
if (!m) {
|
|
|
|
WHY("Manifest struct could not be allocated -- not added to rhizome");
|
|
|
|
http_request_simple_response(&r->http, 500, "Internal Error: No free manifest slots");
|
|
|
|
rhizome_direct_clear_temporary_files(r);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-12-19 08:37:14 +00:00
|
|
|
if (manifestTemplate[0] && rhizome_read_manifest_from_file(m, manifestTemplate) == -1) {
|
2013-10-16 07:55:58 +00:00
|
|
|
WHY("Manifest template read failed");
|
|
|
|
rhizome_manifest_free(m);
|
|
|
|
rhizome_direct_clear_temporary_files(r);
|
|
|
|
http_request_simple_response(&r->http, 500, "Internal Error: Malformed manifest template");
|
|
|
|
return 0;
|
|
|
|
}
|
2013-12-27 18:56:22 +00:00
|
|
|
if (rhizome_stat_payload_file(m, payload_path) != RHIZOME_PAYLOAD_STATUS_NEW) {
|
2013-10-16 07:55:58 +00:00
|
|
|
WHY("Payload file stat failed");
|
|
|
|
rhizome_manifest_free(m);
|
|
|
|
rhizome_direct_clear_temporary_files(r);
|
|
|
|
http_request_simple_response(&r->http, 500, "Internal Error: Could not store file");
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-05 07:28:03 +00:00
|
|
|
if (!rhizome_is_bk_none(&config.rhizome.api.addfile.bundle_secret_key))
|
|
|
|
rhizome_apply_bundle_secret(m, &config.rhizome.api.addfile.bundle_secret_key);
|
2015-12-07 11:30:52 +00:00
|
|
|
// If manifest template did not specify a service field, then by default it is "file".
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (m->service == NULL)
|
|
|
|
rhizome_manifest_set_service(m, RHIZOME_SERVICE_FILE);
|
2016-05-31 03:20:32 +00:00
|
|
|
if (!is_sid_t_any(config.rhizome.api.addfile.default_author))
|
|
|
|
rhizome_manifest_set_author(m, &config.rhizome.api.addfile.default_author);
|
|
|
|
struct rhizome_bundle_result result = rhizome_fill_manifest(m, r->u.direct_import.data_file_name);
|
2015-12-07 11:30:52 +00:00
|
|
|
rhizome_manifest *mout = NULL;
|
|
|
|
if (result.status == RHIZOME_BUNDLE_STATUS_NEW) {
|
2015-11-30 13:53:15 +00:00
|
|
|
rhizome_bundle_result_free(&result);
|
2015-12-07 11:30:52 +00:00
|
|
|
rhizome_manifest_set_crypt(m, PAYLOAD_CLEAR);
|
|
|
|
// import file contents
|
|
|
|
// TODO, stream file into database
|
|
|
|
assert(m->filesize != RHIZOME_SIZE_UNSET);
|
|
|
|
if (m->filesize == 0 || rhizome_store_payload_file(m, payload_path) == RHIZOME_PAYLOAD_STATUS_NEW) {
|
|
|
|
result = rhizome_manifest_finalise(m, &mout, 1);
|
|
|
|
if (mout)
|
|
|
|
DEBUGF(rhizome, "Import sans-manifest appeared to succeed");
|
2012-10-02 23:25:04 +00:00
|
|
|
}
|
2013-10-16 07:55:58 +00:00
|
|
|
}
|
|
|
|
/* Respond with the manifest that was added. */
|
2015-12-07 11:30:52 +00:00
|
|
|
http_request_rhizome_bundle_status_response(r, result, mout);
|
|
|
|
/* Clean up after ourselves. */
|
|
|
|
rhizome_bundle_result_free(&result);
|
|
|
|
rhizome_direct_clear_temporary_files(r);
|
2013-10-16 07:55:58 +00:00
|
|
|
if (mout && mout != m)
|
|
|
|
rhizome_manifest_free(mout);
|
|
|
|
rhizome_manifest_free(m);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
http_request_simple_response(&r->http, 501, "Not Implemented: Rhizome add with manifest");
|
|
|
|
return 0;
|
|
|
|
}
|
2012-09-09 04:20:09 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 12:52:47 +00:00
|
|
|
static int rhizome_direct_process_mime_start(struct http_request *hr)
|
2012-09-09 04:20:09 +00:00
|
|
|
{
|
2014-01-31 07:32:41 +00:00
|
|
|
httpd_request *r = (httpd_request *) hr;
|
2013-12-30 07:39:56 +00:00
|
|
|
assert(r->u.direct_import.current_part == NULL);
|
|
|
|
assert(r->u.direct_import.part_fd == -1);
|
2013-12-21 12:52:47 +00:00
|
|
|
return 0;
|
2013-10-16 07:55:58 +00:00
|
|
|
}
|
2012-09-09 04:20:09 +00:00
|
|
|
|
2013-12-21 12:52:47 +00:00
|
|
|
static int rhizome_direct_process_mime_end(struct http_request *hr)
|
2013-10-16 07:55:58 +00:00
|
|
|
{
|
2014-01-31 07:32:41 +00:00
|
|
|
httpd_request *r = (httpd_request *) hr;
|
2013-12-30 07:39:56 +00:00
|
|
|
if (r->u.direct_import.part_fd != -1) {
|
|
|
|
if (close(r->u.direct_import.part_fd) == -1) {
|
|
|
|
WHYF_perror("close(%d)", r->u.direct_import.part_fd);
|
2013-10-16 07:55:58 +00:00
|
|
|
http_request_simple_response(&r->http, 500, "Internal Error: Close temporary file failed");
|
2013-12-21 12:52:47 +00:00
|
|
|
return 500;
|
2012-09-09 04:20:09 +00:00
|
|
|
}
|
2013-12-30 07:39:56 +00:00
|
|
|
r->u.direct_import.part_fd = -1;
|
2012-09-09 04:20:09 +00:00
|
|
|
}
|
2013-12-30 07:39:56 +00:00
|
|
|
if (r->u.direct_import.current_part == PART_MANIFEST)
|
|
|
|
r->u.direct_import.received_manifest = 1;
|
2015-09-21 09:10:31 +00:00
|
|
|
else if ( r->u.direct_import.current_part == PART_DATA
|
|
|
|
|| r->u.direct_import.current_part == PART_PAYLOAD)
|
2015-08-31 06:01:58 +00:00
|
|
|
r->u.direct_import.received_data = 1;
|
2013-12-30 07:39:56 +00:00
|
|
|
r->u.direct_import.current_part = NULL;
|
2013-12-21 12:52:47 +00:00
|
|
|
return 0;
|
2012-09-09 04:20:09 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 12:52:47 +00:00
|
|
|
static int rhizome_direct_process_mime_part_header(struct http_request *hr, const struct mime_part_headers *h)
|
2012-09-09 04:20:09 +00:00
|
|
|
{
|
2014-01-31 07:32:41 +00:00
|
|
|
httpd_request *r = (httpd_request *) hr;
|
2016-02-22 07:44:53 +00:00
|
|
|
if (!h->content_disposition.type[0])
|
|
|
|
return http_response_content_disposition(r, 415, "Missing", h->content_disposition.type);
|
|
|
|
if (strcmp(h->content_disposition.type, "form-data") != 0)
|
|
|
|
return http_response_content_disposition(r, 415, "Unsupported", h->content_disposition.type);
|
2015-09-21 09:10:31 +00:00
|
|
|
if ( strcmp(h->content_disposition.name, PART_PAYLOAD) == 0
|
|
|
|
|| strcmp(h->content_disposition.name, PART_DATA) == 0
|
|
|
|
) {
|
2015-08-31 06:01:58 +00:00
|
|
|
r->u.direct_import.current_part = PART_PAYLOAD;
|
2013-12-30 07:39:56 +00:00
|
|
|
strncpy(r->u.direct_import.data_file_name,
|
|
|
|
h->content_disposition.filename,
|
|
|
|
sizeof r->u.direct_import.data_file_name)
|
|
|
|
[sizeof r->u.direct_import.data_file_name - 1] = '\0';
|
2012-09-09 04:20:09 +00:00
|
|
|
}
|
2013-12-30 07:39:56 +00:00
|
|
|
else if (strcmp(h->content_disposition.name, PART_MANIFEST) == 0) {
|
|
|
|
r->u.direct_import.current_part = PART_MANIFEST;
|
2013-10-16 07:55:58 +00:00
|
|
|
} else
|
2013-12-21 12:52:47 +00:00
|
|
|
return 0;
|
2013-10-16 07:55:58 +00:00
|
|
|
char path[512];
|
2015-09-21 09:10:31 +00:00
|
|
|
if (form_temporary_file_path(r, path, r->u.direct_import.current_part) == -1) {
|
2013-10-16 07:55:58 +00:00
|
|
|
http_request_simple_response(&r->http, 500, "Internal Error: Buffer overrun");
|
2013-12-21 12:52:47 +00:00
|
|
|
return 0;
|
2013-10-16 07:55:58 +00:00
|
|
|
}
|
2013-12-30 07:39:56 +00:00
|
|
|
if ((r->u.direct_import.part_fd = open(path, O_WRONLY | O_CREAT, 0666)) == -1) {
|
2013-10-16 07:55:58 +00:00
|
|
|
WHYF_perror("open(%s,O_WRONLY|O_CREAT,0666)", alloca_str_toprint(path));
|
|
|
|
http_request_simple_response(&r->http, 500, "Internal Error: Create temporary file failed");
|
2013-12-21 12:52:47 +00:00
|
|
|
return 0;
|
2013-10-16 07:55:58 +00:00
|
|
|
}
|
2013-12-21 12:52:47 +00:00
|
|
|
return 0;
|
2013-10-16 07:55:58 +00:00
|
|
|
}
|
2012-09-09 04:20:09 +00:00
|
|
|
|
2013-12-30 07:39:56 +00:00
|
|
|
static int rhizome_direct_process_mime_body(struct http_request *hr, char *buf, size_t len)
|
2013-10-16 07:55:58 +00:00
|
|
|
{
|
2014-01-31 07:32:41 +00:00
|
|
|
httpd_request *r = (httpd_request *) hr;
|
2013-12-30 07:39:56 +00:00
|
|
|
if (r->u.direct_import.part_fd != -1) {
|
|
|
|
if (write_all(r->u.direct_import.part_fd, buf, len) == -1) {
|
2013-10-16 07:55:58 +00:00
|
|
|
http_request_simple_response(&r->http, 500, "Internal Error: Write temporary file failed");
|
2013-12-21 12:52:47 +00:00
|
|
|
return 500;
|
2012-09-09 04:20:09 +00:00
|
|
|
}
|
2013-10-16 07:55:58 +00:00
|
|
|
}
|
2013-12-21 12:52:47 +00:00
|
|
|
return 0;
|
2013-10-16 07:55:58 +00:00
|
|
|
}
|
2012-09-09 04:20:09 +00:00
|
|
|
|
2015-08-24 03:18:00 +00:00
|
|
|
static int rhizome_direct_import(httpd_request *r, const char *remainder)
|
2013-10-16 07:55:58 +00:00
|
|
|
{
|
2013-12-09 07:52:18 +00:00
|
|
|
if (*remainder)
|
2013-12-16 01:51:45 +00:00
|
|
|
return 404;
|
|
|
|
if (r->http.verb != HTTP_VERB_POST)
|
|
|
|
return 405;
|
2013-10-16 07:55:58 +00:00
|
|
|
r->http.form_data.handle_mime_part_start = rhizome_direct_process_mime_start;
|
|
|
|
r->http.form_data.handle_mime_part_end = rhizome_direct_process_mime_end;
|
2013-10-28 11:57:27 +00:00
|
|
|
r->http.form_data.handle_mime_part_header = rhizome_direct_process_mime_part_header;
|
2013-10-16 07:55:58 +00:00
|
|
|
r->http.form_data.handle_mime_body = rhizome_direct_process_mime_body;
|
|
|
|
r->http.handle_content_end = rhizome_direct_import_end;
|
2013-12-30 07:39:56 +00:00
|
|
|
r->u.direct_import.current_part = NULL;
|
|
|
|
r->u.direct_import.part_fd = -1;
|
|
|
|
r->u.direct_import.data_file_name[0] = '\0';
|
2013-12-16 01:51:45 +00:00
|
|
|
return 1;
|
2012-09-09 04:20:09 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 03:18:00 +00:00
|
|
|
static int rhizome_direct_enquiry(httpd_request *r, const char *remainder)
|
2012-09-09 04:20:09 +00:00
|
|
|
{
|
2013-12-09 07:52:18 +00:00
|
|
|
if (*remainder)
|
2013-12-16 01:51:45 +00:00
|
|
|
return 404;
|
|
|
|
if (r->http.verb != HTTP_VERB_POST)
|
|
|
|
return 405;
|
2013-10-16 07:55:58 +00:00
|
|
|
r->http.form_data.handle_mime_part_start = rhizome_direct_process_mime_start;
|
|
|
|
r->http.form_data.handle_mime_part_end = rhizome_direct_process_mime_end;
|
2013-10-28 11:57:27 +00:00
|
|
|
r->http.form_data.handle_mime_part_header = rhizome_direct_process_mime_part_header;
|
2013-10-16 07:55:58 +00:00
|
|
|
r->http.form_data.handle_mime_body = rhizome_direct_process_mime_body;
|
|
|
|
r->http.handle_content_end = rhizome_direct_enquiry_end;
|
2013-12-30 07:39:56 +00:00
|
|
|
r->u.direct_import.current_part = NULL;
|
|
|
|
r->u.direct_import.part_fd = -1;
|
|
|
|
r->u.direct_import.data_file_name[0] = '\0';
|
2013-12-16 01:51:45 +00:00
|
|
|
return 1;
|
2013-10-16 07:55:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Servald can be configured to accept files without manifests via HTTP from localhost, so that
|
|
|
|
* rhizome bundles can be created programatically. There are probably still some security
|
|
|
|
* loop-holes here, which is part of why we leave it disabled by default, but it will be sufficient
|
|
|
|
* for testing possible uses, including integration with OpenDataKit.
|
|
|
|
*/
|
2014-01-31 07:32:41 +00:00
|
|
|
int rhizome_direct_addfile(httpd_request *r, const char *remainder)
|
2013-10-16 07:55:58 +00:00
|
|
|
{
|
2013-12-09 07:52:18 +00:00
|
|
|
if (*remainder)
|
2013-12-16 01:51:45 +00:00
|
|
|
return 404;
|
|
|
|
if (r->http.verb != HTTP_VERB_POST)
|
|
|
|
return 405;
|
2013-10-28 01:38:57 +00:00
|
|
|
if ( r->http.client_sockaddr_in.sin_family != AF_INET
|
|
|
|
|| r->http.client_sockaddr_in.sin_addr.s_addr != config.rhizome.api.addfile.allow_host.s_addr
|
2012-10-04 05:05:09 +00:00
|
|
|
) {
|
2013-10-28 01:38:57 +00:00
|
|
|
INFOF("rhizome.api.addfile request received from %s, but is only allowed from AF_INET %s",
|
|
|
|
alloca_sockaddr(&r->http.client_sockaddr_in, sizeof r->http.client_sockaddr_in),
|
|
|
|
alloca_in_addr(&config.rhizome.api.addfile.allow_host)
|
2013-10-16 07:55:58 +00:00
|
|
|
);
|
|
|
|
rhizome_direct_clear_temporary_files(r);
|
2013-12-16 01:51:45 +00:00
|
|
|
return 403; // Forbidden
|
2012-09-09 04:20:09 +00:00
|
|
|
}
|
2013-10-16 07:55:58 +00:00
|
|
|
r->http.form_data.handle_mime_part_start = rhizome_direct_process_mime_start;
|
|
|
|
r->http.form_data.handle_mime_part_end = rhizome_direct_process_mime_end;
|
2013-10-28 11:57:27 +00:00
|
|
|
r->http.form_data.handle_mime_part_header = rhizome_direct_process_mime_part_header;
|
2013-10-16 07:55:58 +00:00
|
|
|
r->http.form_data.handle_mime_body = rhizome_direct_process_mime_body;
|
|
|
|
r->http.handle_content_end = rhizome_direct_addfile_end;
|
2013-12-30 07:39:56 +00:00
|
|
|
r->u.direct_import.current_part = NULL;
|
|
|
|
r->u.direct_import.part_fd = -1;
|
|
|
|
r->u.direct_import.data_file_name[0] = '\0';
|
2013-12-16 01:51:45 +00:00
|
|
|
return 1;
|
2012-09-09 04:20:09 +00:00
|
|
|
}
|
2012-09-09 23:46:27 +00:00
|
|
|
|
2015-08-24 03:18:00 +00:00
|
|
|
static int rhizome_direct_dispatch(httpd_request *r, const char *UNUSED(remainder))
|
2013-10-16 07:55:58 +00:00
|
|
|
{
|
|
|
|
if ( config.rhizome.api.addfile.uri_path[0]
|
|
|
|
&& strcmp(r->http.path, config.rhizome.api.addfile.uri_path) == 0
|
|
|
|
)
|
2013-12-09 07:52:18 +00:00
|
|
|
return rhizome_direct_addfile(r, "");
|
2013-12-16 01:51:45 +00:00
|
|
|
return 404;
|
2013-10-16 07:55:58 +00:00
|
|
|
}
|
|
|
|
|
2012-10-04 08:08:33 +00:00
|
|
|
static int receive_http_response(int sock, char *buffer, size_t buffer_len, struct http_response_parts *parts)
|
|
|
|
{
|
2013-12-10 06:22:53 +00:00
|
|
|
size_t len = 0;
|
|
|
|
ssize_t count;
|
2012-10-04 08:08:33 +00:00
|
|
|
do {
|
|
|
|
if ((count = read(sock, &buffer[len], buffer_len - len)) == -1)
|
2013-12-16 04:49:15 +00:00
|
|
|
return WHYF_perror("read(%d, %p, %d)", sock, &buffer[len], (int)(buffer_len - len));
|
2013-12-10 06:22:53 +00:00
|
|
|
len += (size_t)count;
|
2013-10-16 00:52:02 +00:00
|
|
|
} while (len < buffer_len && count != 0 && !is_http_header_complete(buffer, len, len));
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(rhizome_rx, "Received HTTP response %s", alloca_toprint(-1, buffer, len));
|
2012-10-04 08:08:33 +00:00
|
|
|
if (unpack_http_response(buffer, parts) == -1)
|
|
|
|
return -1;
|
|
|
|
if (parts->code != 200 && parts->code != 201) {
|
2013-07-15 00:29:24 +00:00
|
|
|
INFOF("Failed HTTP request: server returned %03u %s", parts->code, parts->reason);
|
2012-10-04 08:08:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2013-12-10 06:36:45 +00:00
|
|
|
if (parts->content_length == HTTP_RESPONSE_CONTENT_LENGTH_UNSET) {
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(rhizome_rx, "Invalid HTTP reply: missing Content-Length header");
|
2012-10-04 08:08:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(rhizome_rx, "content_length=%"PRIu64, parts->content_length);
|
2013-02-08 00:10:12 +00:00
|
|
|
return len - (parts->content_start - buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fill_buffer(int sock, unsigned char *buffer, int len, int buffer_size){
|
|
|
|
int count;
|
|
|
|
do {
|
|
|
|
if ((count = read(sock, &buffer[len], buffer_size - len)) == -1)
|
|
|
|
return WHYF_perror("read(%d, %p, %d)", sock, &buffer[len], buffer_size - len);
|
|
|
|
len += count;
|
|
|
|
} while (len < buffer_size);
|
2012-10-04 08:08:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-09 23:46:27 +00:00
|
|
|
void rhizome_direct_http_dispatch(rhizome_direct_sync_request *r)
|
|
|
|
{
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(rhizome_tx, "Dispatch size_high=%"PRId64,r->cursor->size_high);
|
2012-10-03 08:21:37 +00:00
|
|
|
rhizome_direct_transport_state_http *state = r->transport_specific_state;
|
2012-09-10 21:26:04 +00:00
|
|
|
|
|
|
|
int sock=socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (sock==-1) {
|
2013-10-16 07:55:58 +00:00
|
|
|
WHY_perror("socket");
|
2012-09-10 21:26:04 +00:00
|
|
|
goto end;
|
2013-10-16 07:55:58 +00:00
|
|
|
}
|
2012-09-10 21:26:04 +00:00
|
|
|
|
|
|
|
struct hostent *hostent;
|
|
|
|
hostent = gethostbyname(state->host);
|
|
|
|
if (!hostent) {
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(rhizome_tx, "could not resolve hostname");
|
2012-09-10 21:26:04 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2012-09-10 10:52:14 +00:00
|
|
|
|
2013-12-09 07:15:47 +00:00
|
|
|
struct socket_address addr;
|
|
|
|
bzero(&addr,sizeof(addr));
|
|
|
|
addr.addrlen = sizeof(addr.inet);
|
|
|
|
addr.inet.sin_family = AF_INET;
|
|
|
|
addr.inet.sin_port = htons(state->port);
|
|
|
|
addr.inet.sin_addr = *((struct in_addr *)hostent->h_addr);
|
2012-09-10 21:26:04 +00:00
|
|
|
|
2013-12-09 07:15:47 +00:00
|
|
|
if (connect(sock, &addr.addr, addr.addrlen) == -1) {
|
|
|
|
WHYF_perror("connect(%s)", alloca_socket_address(&addr));
|
2012-10-03 08:21:37 +00:00
|
|
|
close(sock);
|
|
|
|
goto end;
|
|
|
|
}
|
2013-10-16 07:55:58 +00:00
|
|
|
|
2012-10-03 08:21:37 +00:00
|
|
|
char boundary[20];
|
|
|
|
char buffer[8192];
|
|
|
|
|
2015-10-13 08:12:22 +00:00
|
|
|
strbuf bb = strbuf_local_buf(boundary);
|
2012-10-03 08:21:37 +00:00
|
|
|
strbuf_sprintf(bb, "%08lx%08lx", random(), random());
|
|
|
|
assert(!strbuf_overrun(bb));
|
|
|
|
strbuf content_preamble = strbuf_alloca(200);
|
|
|
|
strbuf content_postamble = strbuf_alloca(40);
|
|
|
|
strbuf_sprintf(content_preamble,
|
|
|
|
"--%s\r\n"
|
|
|
|
"Content-Disposition: form-data; name=\"data\"; filename=\"IHAVEs\"\r\n"
|
2014-02-07 05:58:40 +00:00
|
|
|
"Content-Type: %s\r\n"
|
2012-10-03 08:21:37 +00:00
|
|
|
"\r\n",
|
2014-02-07 05:58:40 +00:00
|
|
|
boundary, CONTENT_TYPE_BLOB
|
2012-10-03 08:21:37 +00:00
|
|
|
);
|
|
|
|
strbuf_sprintf(content_postamble, "\r\n--%s--\r\n", boundary);
|
|
|
|
assert(!strbuf_overrun(content_preamble));
|
|
|
|
assert(!strbuf_overrun(content_postamble));
|
|
|
|
int content_length = strbuf_len(content_preamble)
|
|
|
|
+ r->cursor->buffer_offset_bytes
|
|
|
|
+ r->cursor->buffer_used
|
|
|
|
+ strbuf_len(content_postamble);
|
2015-10-13 08:12:22 +00:00
|
|
|
strbuf request = strbuf_local_buf(buffer);
|
2012-10-03 08:21:37 +00:00
|
|
|
strbuf_sprintf(request,
|
|
|
|
"POST /rhizome/enquiry HTTP/1.0\r\n"
|
|
|
|
"Content-Length: %d\r\n"
|
|
|
|
"Content-Type: multipart/form-data; boundary=%s\r\n"
|
|
|
|
"\r\n%s",
|
|
|
|
content_length, boundary, strbuf_str(content_preamble)
|
|
|
|
);
|
|
|
|
assert(!strbuf_overrun(request));
|
2012-09-10 21:26:04 +00:00
|
|
|
|
2012-09-10 23:34:38 +00:00
|
|
|
/* TODO: Refactor this code so that it uses our asynchronous framework.
|
|
|
|
*/
|
2012-10-03 08:21:37 +00:00
|
|
|
int len = strbuf_len(request);
|
2012-09-10 21:26:04 +00:00
|
|
|
int sent=0;
|
|
|
|
while(sent<len) {
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(rhizome_tx, "write(%d, %s, %d)", sock, alloca_toprint(-1, &buffer[sent], len-sent), len-sent);
|
2012-09-10 21:26:04 +00:00
|
|
|
int count=write(sock,&buffer[sent],len-sent);
|
2012-10-03 08:21:37 +00:00
|
|
|
if (count == -1) {
|
2012-09-10 23:34:38 +00:00
|
|
|
if (errno==EPIPE) goto rx;
|
2012-10-03 08:21:37 +00:00
|
|
|
WHYF_perror("write(%d)", len - sent);
|
2012-09-10 21:26:04 +00:00
|
|
|
close(sock);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
sent+=count;
|
|
|
|
}
|
|
|
|
|
|
|
|
len=r->cursor->buffer_offset_bytes+r->cursor->buffer_used;
|
|
|
|
sent=0;
|
|
|
|
while(sent<len) {
|
|
|
|
int count=write(sock,&r->cursor->buffer[sent],len-sent);
|
2012-10-03 08:21:37 +00:00
|
|
|
if (count == -1) {
|
|
|
|
if (errno == EPIPE)
|
|
|
|
goto rx;
|
|
|
|
WHYF_perror("write(%d)", count);
|
2012-09-10 21:26:04 +00:00
|
|
|
close(sock);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
sent+=count;
|
|
|
|
}
|
|
|
|
|
2012-10-03 08:21:37 +00:00
|
|
|
strbuf_reset(request);
|
|
|
|
strbuf_puts(request, strbuf_str(content_postamble));
|
|
|
|
len = strbuf_len(request);
|
2012-09-10 21:26:04 +00:00
|
|
|
sent=0;
|
|
|
|
while(sent<len) {
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(rhizome_tx, "write(%d, %s, %d)", sock, alloca_toprint(-1, &buffer[sent], len-sent), len-sent);
|
2012-09-10 21:26:04 +00:00
|
|
|
int count=write(sock,&buffer[sent],len-sent);
|
2012-10-03 08:21:37 +00:00
|
|
|
if (count == -1) {
|
2012-09-10 23:34:38 +00:00
|
|
|
if (errno==EPIPE) goto rx;
|
2012-10-03 08:21:37 +00:00
|
|
|
WHYF_perror("write(%d)", len - sent);
|
2012-09-10 21:26:04 +00:00
|
|
|
close(sock);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
sent+=count;
|
|
|
|
}
|
|
|
|
|
2012-10-04 08:08:33 +00:00
|
|
|
struct http_response_parts parts;
|
2012-09-10 23:34:38 +00:00
|
|
|
rx:
|
|
|
|
/* request sent, now get response back. */
|
2013-02-08 00:10:12 +00:00
|
|
|
len=receive_http_response(sock, buffer, sizeof buffer, &parts);
|
|
|
|
if (len == -1) {
|
2012-10-04 08:08:33 +00:00
|
|
|
close(sock);
|
|
|
|
goto end;
|
2012-09-10 23:58:11 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 00:10:12 +00:00
|
|
|
/* Allocate a buffer to receive the entire action list */
|
|
|
|
content_length = parts.content_length;
|
2013-08-15 06:17:39 +00:00
|
|
|
unsigned char *actionlist=emalloc(content_length);
|
|
|
|
if (!actionlist){
|
|
|
|
close(sock);
|
|
|
|
goto end;
|
|
|
|
}
|
2013-02-08 00:10:12 +00:00
|
|
|
bcopy(parts.content_start, actionlist, len);
|
|
|
|
if (fill_buffer(sock, actionlist, len, content_length)==-1){
|
|
|
|
free(actionlist);
|
|
|
|
close(sock);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
close(sock);
|
2013-10-16 07:55:58 +00:00
|
|
|
|
2012-10-02 09:54:35 +00:00
|
|
|
/* We now have the list of (1+RHIZOME_BAR_PREFIX_BYTES)-byte records that indicate
|
|
|
|
the list of BAR prefixes that differ between the two nodes. We can now action
|
2013-10-16 07:55:58 +00:00
|
|
|
those which are relevant, i.e., based on whether we are pushing, pulling or
|
2012-10-02 09:54:35 +00:00
|
|
|
synchronising (both).
|
2012-09-21 12:31:14 +00:00
|
|
|
|
|
|
|
I am currently undecided as to whether it is cleaner to have some general
|
|
|
|
rhizome direct function for doing that, or whether it just adds unnecessary
|
|
|
|
complication, and the responses should just be handled in here.
|
|
|
|
|
|
|
|
For now, I am just going to implement it in here, and we can generalise later.
|
|
|
|
*/
|
2012-09-21 12:40:36 +00:00
|
|
|
int i;
|
2012-10-02 09:54:35 +00:00
|
|
|
for(i=10;i<content_length;i+=(1+RHIZOME_BAR_PREFIX_BYTES))
|
2012-09-21 12:40:36 +00:00
|
|
|
{
|
2012-10-03 15:55:55 +00:00
|
|
|
int type=actionlist[i];
|
2012-09-22 05:51:02 +00:00
|
|
|
if (type==2&&r->pullP) {
|
|
|
|
/* Need to fetch manifest. Once we have the manifest, then we can
|
2013-10-16 07:55:58 +00:00
|
|
|
use our normal bundle fetch routines from rhizome_fetch.c
|
2012-10-02 13:56:21 +00:00
|
|
|
|
|
|
|
Generate a request like: GET /rhizome/manifestbybar/<hex of bar>
|
|
|
|
and add it to our list of HTTP fetch requests, then watch
|
|
|
|
until the request is finished. That will give us the manifest.
|
|
|
|
Then as noted above, we can use that to pull the file down using
|
|
|
|
existing routines.
|
2012-09-22 05:51:02 +00:00
|
|
|
*/
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(rhizome_tx, "Fetching manifest %s* @ 0x%x",alloca_tohex(&actionlist[i], 1+RHIZOME_BAR_PREFIX_BYTES),i);
|
2014-02-05 04:56:56 +00:00
|
|
|
if (!rhizome_fetch_request_manifest_by_prefix(&addr, NULL, &actionlist[i+1], RHIZOME_BAR_PREFIX_BYTES))
|
2012-10-02 13:56:21 +00:00
|
|
|
{
|
2013-10-16 07:55:58 +00:00
|
|
|
/* Fetching the manifest, and then using it to see if we want to
|
2012-10-02 13:56:21 +00:00
|
|
|
fetch the file for import is all handled asynchronously, so just
|
|
|
|
wait for it to finish. */
|
2012-11-12 05:51:38 +00:00
|
|
|
while (rhizome_any_fetch_active() || rhizome_any_fetch_queued())
|
2012-10-18 07:00:22 +00:00
|
|
|
fd_poll();
|
2012-10-02 13:56:21 +00:00
|
|
|
}
|
2013-10-16 07:55:58 +00:00
|
|
|
|
2012-09-22 05:51:02 +00:00
|
|
|
} else if (type==1&&r->pushP) {
|
|
|
|
/* Form up the POST request to submit the appropriate bundle. */
|
|
|
|
|
|
|
|
/* Start by getting the manifest, which is the main thing we need, and also
|
|
|
|
gives us the information we need for sending any associated file. */
|
2013-10-03 19:20:37 +00:00
|
|
|
rhizome_manifest *m = rhizome_direct_get_manifest(&actionlist[i+1], RHIZOME_BAR_PREFIX_BYTES);
|
|
|
|
if (m == NULL) {
|
2012-10-03 08:21:37 +00:00
|
|
|
WHY("This should never happen. The manifest exists, but when I went looking for it, it doesn't appear to be there.");
|
2012-09-22 05:51:02 +00:00
|
|
|
goto next_item;
|
|
|
|
}
|
2012-09-26 06:31:27 +00:00
|
|
|
|
|
|
|
/* Get filehash and size from manifest if present */
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(rhizome_tx, "bundle id = %s", alloca_tohex_rhizome_bid_t(m->cryptoSignPublic));
|
|
|
|
DEBUGF(rhizome_tx, "bundle filehash = %s", alloca_tohex_rhizome_filehash_t(m->filehash));
|
|
|
|
DEBUGF(rhizome_tx, "file size = %"PRId64, m->filesize);
|
|
|
|
DEBUGF(rhizome_tx, "version = %"PRIu64, m->version);
|
2012-09-26 06:31:27 +00:00
|
|
|
|
|
|
|
/* We now have everything we need to compose the POST request and send it.
|
|
|
|
*/
|
|
|
|
char *template="POST /rhizome/import HTTP/1.0\r\n"
|
|
|
|
"Content-Length: %d\r\n"
|
|
|
|
"Content-Type: multipart/form-data; boundary=%s\r\n"
|
|
|
|
"\r\n";
|
|
|
|
char *template2="--%s\r\n"
|
|
|
|
"Content-Disposition: form-data; name=\"manifest\"; filename=\"m\"\r\n"
|
|
|
|
"Content-Type: application/octet-stream\r\n"
|
|
|
|
"\r\n";
|
|
|
|
char *template3=
|
2012-09-26 07:17:55 +00:00
|
|
|
"\r\n--%s\r\n"
|
2012-09-26 06:31:27 +00:00
|
|
|
"Content-Disposition: form-data; name=\"data\"; filename=\"d\"\r\n"
|
|
|
|
"Content-Type: application/octet-stream\r\n"
|
|
|
|
"\r\n";
|
|
|
|
/* Work out what the content length should be */
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(rhizome_tx, "manifest_all_bytes=%zu, manifest_body_bytes=%zu", m->manifest_all_bytes, m->manifest_body_bytes);
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
assert(m->filesize != RHIZOME_SIZE_UNSET);
|
2013-10-30 03:15:51 +00:00
|
|
|
size_t content_length =
|
|
|
|
strlen(template2) - 2 /* minus 2 for the "%s" that gets replaced */
|
|
|
|
+ strlen(boundary)
|
|
|
|
+ m->manifest_all_bytes
|
|
|
|
+ strlen(template3) - 2 /* minus 2 for the "%s" that gets replaced */
|
|
|
|
+ strlen(boundary)
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
+ m->filesize
|
2013-10-30 03:15:51 +00:00
|
|
|
+ strlen("\r\n--") + strlen(boundary) + strlen("--\r\n");
|
2012-09-26 06:31:27 +00:00
|
|
|
|
2012-10-03 08:21:37 +00:00
|
|
|
int len=snprintf(buffer,8192,template,content_length,boundary);
|
|
|
|
len+=snprintf(&buffer[len],8192-len,template2,boundary);
|
2012-09-26 06:31:27 +00:00
|
|
|
memcpy(&buffer[len],m->manifestdata,m->manifest_all_bytes);
|
|
|
|
len+=m->manifest_all_bytes;
|
2012-10-03 08:21:37 +00:00
|
|
|
len+=snprintf(&buffer[len],8192-len,template3,boundary);
|
2012-09-26 06:31:27 +00:00
|
|
|
|
2012-09-26 07:17:55 +00:00
|
|
|
sock=socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (sock==-1) {
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(rhizome_tx, "could not open socket");
|
2012-09-26 07:17:55 +00:00
|
|
|
goto closeit;
|
2013-10-16 07:55:58 +00:00
|
|
|
}
|
2013-12-09 07:15:47 +00:00
|
|
|
if (connect(sock,&addr.addr,addr.addrlen) == -1) {
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(rhizome_tx, "Could not connect to remote");
|
2013-10-16 07:55:58 +00:00
|
|
|
goto closeit;
|
|
|
|
}
|
2012-09-26 07:17:55 +00:00
|
|
|
|
|
|
|
int sent=0;
|
|
|
|
/* Send buffer now */
|
|
|
|
while(sent<len) {
|
|
|
|
int r=write(sock,&buffer[sent],len-sent);
|
|
|
|
if (r>0) sent+=r;
|
|
|
|
if (r<0) goto closeit;
|
2012-10-02 09:02:48 +00:00
|
|
|
}
|
2012-09-26 06:31:27 +00:00
|
|
|
|
2013-02-20 03:59:08 +00:00
|
|
|
/* send file contents */
|
|
|
|
{
|
2013-10-10 07:53:06 +00:00
|
|
|
rhizome_filehash_t filehash;
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (rhizome_database_filehash_from_id(&m->cryptoSignPublic, m->version, &filehash) == -1)
|
2013-02-20 03:59:08 +00:00
|
|
|
goto closeit;
|
2013-10-16 07:55:58 +00:00
|
|
|
|
2013-02-20 03:59:08 +00:00
|
|
|
struct rhizome_read read;
|
|
|
|
bzero(&read, sizeof read);
|
2014-04-03 00:06:38 +00:00
|
|
|
enum rhizome_payload_status pstatus = rhizome_open_read(&read, &filehash);
|
2013-12-27 18:56:22 +00:00
|
|
|
switch (pstatus) {
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_EMPTY:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_STORED:
|
2014-07-02 07:46:19 +00:00
|
|
|
goto pstatus_ok;
|
2013-12-27 18:56:22 +00:00
|
|
|
case RHIZOME_PAYLOAD_STATUS_NEW:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_ERROR:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_WRONG_SIZE:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_WRONG_HASH:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_CRYPTO_FAIL:
|
2014-07-02 07:46:19 +00:00
|
|
|
case RHIZOME_PAYLOAD_STATUS_TOO_BIG:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_EVICTED:
|
2013-12-27 18:56:22 +00:00
|
|
|
goto closeit;
|
2014-07-02 07:46:19 +00:00
|
|
|
// No "default" label, so the compiler will warn us if a case is not handled.
|
2013-12-27 18:56:22 +00:00
|
|
|
}
|
2014-07-02 07:46:19 +00:00
|
|
|
FATALF("pstatus = %d", pstatus);
|
|
|
|
pstatus_ok:
|
|
|
|
;
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
uint64_t read_ofs;
|
|
|
|
for(read_ofs=0;read_ofs<m->filesize;){
|
2012-09-26 07:42:48 +00:00
|
|
|
unsigned char buffer[4096];
|
2013-02-20 03:59:08 +00:00
|
|
|
read.offset=read_ofs;
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
ssize_t bytes_read = rhizome_read(&read, buffer, sizeof buffer);
|
|
|
|
if (bytes_read == -1) {
|
2013-02-20 03:59:08 +00:00
|
|
|
rhizome_read_close(&read);
|
|
|
|
goto closeit;
|
|
|
|
}
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
size_t write_ofs = 0;
|
|
|
|
while (write_ofs < (size_t) bytes_read){
|
|
|
|
ssize_t written = write(sock, buffer + write_ofs, (size_t) bytes_read - write_ofs);
|
|
|
|
if (written == -1){
|
2012-10-04 08:08:33 +00:00
|
|
|
WHY_perror("write");
|
2013-02-20 03:59:08 +00:00
|
|
|
rhizome_read_close(&read);
|
2012-10-04 08:08:33 +00:00
|
|
|
goto closeit;
|
|
|
|
}
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
write_ofs += (size_t) written;
|
2012-09-26 07:42:48 +00:00
|
|
|
}
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
read_ofs += (size_t) bytes_read;
|
2012-09-26 07:42:48 +00:00
|
|
|
}
|
2013-02-20 03:59:08 +00:00
|
|
|
rhizome_read_close(&read);
|
|
|
|
}
|
2012-09-26 07:17:55 +00:00
|
|
|
/* Send final mime boundary */
|
2012-10-03 08:21:37 +00:00
|
|
|
len=snprintf(buffer,8192,"\r\n--%s--\r\n",boundary);
|
2012-09-26 07:17:55 +00:00
|
|
|
sent=0;
|
|
|
|
while(sent<len) {
|
|
|
|
int r=write(sock,&buffer[sent],len-sent);
|
|
|
|
if (r>0) sent+=r;
|
|
|
|
if (r<0) goto closeit;
|
2013-10-16 07:55:58 +00:00
|
|
|
}
|
2012-09-26 07:17:55 +00:00
|
|
|
|
2012-10-04 08:08:33 +00:00
|
|
|
/* get response back. */
|
|
|
|
if (receive_http_response(sock, buffer, sizeof buffer, &parts) == -1)
|
|
|
|
goto closeit;
|
|
|
|
INFOF("Received HTTP response %03u %s", parts.code, parts.reason);
|
2012-09-26 07:17:55 +00:00
|
|
|
|
|
|
|
closeit:
|
|
|
|
close(sock);
|
2012-09-26 06:31:27 +00:00
|
|
|
|
2012-09-22 05:51:02 +00:00
|
|
|
if (m) rhizome_manifest_free(m);
|
|
|
|
}
|
|
|
|
next_item:
|
|
|
|
continue;
|
2012-09-21 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 00:10:12 +00:00
|
|
|
free(actionlist);
|
2013-10-16 07:55:58 +00:00
|
|
|
|
2012-09-21 13:45:10 +00:00
|
|
|
/* now update cursor according to what range was covered in the response.
|
|
|
|
We set our current position to just past the high limit of the returned
|
|
|
|
cursor.
|
|
|
|
|
|
|
|
XXX - This introduces potential problems with the returned cursor range.
|
|
|
|
If the far end returns an earlier cursor position than we are in, we could
|
|
|
|
end up in an infinite loop. We could also end up in a very long finite loop
|
|
|
|
if the cursor doesn't advance far. A simple solution is to not adjust the
|
|
|
|
cursor position, and simply re-attempt the sync until no actions result.
|
2013-10-16 07:55:58 +00:00
|
|
|
That will do for now.
|
2012-09-21 13:45:10 +00:00
|
|
|
*/
|
|
|
|
#ifdef FANCY_CURSOR_POSITION_HANDLING
|
|
|
|
rhizome_direct_bundle_cursor *c=rhizome_direct_bundle_iterator(10);
|
|
|
|
assert(c!=NULL);
|
2013-10-16 07:55:58 +00:00
|
|
|
if (rhizome_direct_bundle_iterator_unpickle_range(c,(unsigned char *)&p[0],10)) {
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(rhizome_tx, "Couldn't unpickle range. This should never happen. Assuming near and far cursor ranges match.");
|
2013-10-16 07:55:58 +00:00
|
|
|
}
|
2012-09-21 13:45:10 +00:00
|
|
|
else {
|
2015-07-06 08:19:49 +00:00
|
|
|
DEBUGF(rhizome_tx, "unpickled size_high=%"PRId64", limit_size_high=%"PRId64, c->size_high, c->limit_size_high);
|
|
|
|
DEBUGF(rhizome_tx, "c->buffer_size=%d",c->buffer_size);
|
2012-09-21 13:45:10 +00:00
|
|
|
r->cursor->size_low=c->limit_size_high;
|
|
|
|
/* Set tail of BID to all high, as we assume the far end has returned all
|
|
|
|
BIDs with the specified prefix. */
|
2013-10-03 13:46:45 +00:00
|
|
|
r->cursor->bid_low = RHIZOME_BID_MAX;
|
|
|
|
bcopy(c->limit_bid_high.binary, r->cursor->bid_low.binary, 4);
|
2012-09-21 13:45:10 +00:00
|
|
|
}
|
|
|
|
rhizome_direct_bundle_iterator_free(&c);
|
|
|
|
#endif
|
2012-09-21 12:31:14 +00:00
|
|
|
|
2012-09-10 21:26:04 +00:00
|
|
|
end:
|
2013-10-16 07:55:58 +00:00
|
|
|
/* Warning: tail recursion when done this way.
|
2012-09-09 23:46:27 +00:00
|
|
|
Should be triggered by an asynchronous event.
|
|
|
|
But this will do for now. */
|
|
|
|
rhizome_direct_continue_sync_request(r);
|
|
|
|
}
|