Conflicts:
	test/scripts/run_tests.sh
	test/testit.c
yamux
jmjatlanta 2017-10-09 09:12:14 -05:00
commit b99a78a4d3
59 changed files with 1381 additions and 215 deletions

6
.gitignore vendored
View File

@ -8,3 +8,9 @@
.settings/language.settings.xml
test/test_ipfs
main/ipfs
test/test1.txt
test/test2.txt
test/scripts/hello.bin
test/scripts/hello2.bin
test/scripts/testlog.txt
test/scripts/generate_file

View File

@ -175,9 +175,19 @@ int ipfs_block_free(struct Block* block) {
*/
struct Block* ipfs_block_copy(struct Block* original) {
struct Block* copy = ipfs_block_new();
copy->data_length = original->data_length;
copy->data = (unsigned char*) malloc(original->data_length);
memcpy(copy->data, original->data, original->data_length);
copy->cid = ipfs_cid_copy(original->cid);
if (copy != NULL) {
copy->data_length = original->data_length;
copy->data = (unsigned char*) malloc(original->data_length);
if (copy->data == NULL) {
ipfs_block_free(copy);
return NULL;
}
memcpy(copy->data, original->data, original->data_length);
copy->cid = ipfs_cid_copy(original->cid);
if (copy->cid == NULL) {
ipfs_block_free(copy);
return NULL;
}
}
return copy;
}

View File

@ -102,6 +102,8 @@ char* ipfs_blockstore_path_get(const struct FSRepo* fs_repo, const char* filenam
}
int complete_filename_size = strlen(filepath) + strlen(filename) + 2;
char* complete_filename = (char*)malloc(complete_filename_size);
if (complete_filename == NULL)
return NULL;
retVal = os_utils_filepath_join(filepath, filename, complete_filename, complete_filename_size);
return complete_filename;
}
@ -343,10 +345,20 @@ int ipfs_blockstore_get_node(const unsigned char* hash, size_t hash_length, stru
size_t bytes_read = fread(buffer, 1, file_size, file);
fclose(file);
int retVal = ipfs_hashtable_node_protobuf_decode(buffer, bytes_read, node);
// now we have the block, convert it to a node
struct Block* block;
if (!ipfs_blocks_block_protobuf_decode(buffer, bytes_read, &block)) {
free(key);
free(filename);
ipfs_block_free(block);
return 0;
}
int retVal = ipfs_hashtable_node_protobuf_decode(block->data, block->data_length, node);
free(key);
free(filename);
ipfs_block_free(block);
return retVal;
}

View File

@ -147,6 +147,10 @@ struct Cid* ipfs_cid_copy(const struct Cid* original) {
copy->version = original->version;
copy->hash_length = original->hash_length;
copy->hash = (unsigned char*) malloc(original->hash_length);
if (copy->hash == NULL) {
ipfs_cid_free(copy);
return NULL;
}
memcpy(copy->hash, original->hash, original->hash_length);
}
return copy;

View File

@ -1,5 +1,5 @@
CC = gcc
CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multiaddr/include -I../../c-protobuf -Wall -std=c99
CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multiaddr/include -I../../c-protobuf -Wall -std=c11
ifdef DEBUG
CFLAGS += -g3

View File

@ -326,6 +326,47 @@ struct HttpRequest* api_build_http_request(struct s_request* req) {
return request;
}
/**
* Write bytes into chunks.
* @param socket, buffer array, length
* @returns 1 when success or 0 if it fails.
*/
int api_send_resp_chunks(int fd, void *buf, size_t size)
{
char head[20];
size_t s;
int l;
struct iovec iov[3];
// will be reused in each write, so defined only once.
iov[2].iov_base = "\r\n";
iov[2].iov_len = 2;
while (size > 0) {
s = size > MAX_CHUNK ? MAX_CHUNK : size; // write only MAX_CHUNK at once
l = snprintf(head, sizeof head, "%x\r\n", (unsigned int)s);
if (l <= 0)
return 0; // fail at snprintf
iov[0].iov_base = head;
iov[0].iov_len = l; // head length.
iov[1].iov_base = buf;
iov[1].iov_len = s;
buf += s;
size -= s;
if (size == 0) { // last chunk
iov[2].iov_base = "\r\n0\r\n\r\n";
iov[2].iov_len = 7;
}
libp2p_logger_debug("api", "writing chunk block of %d bytes\n", s);
if (writev(fd, iov, 3) == -1)
return 0; // fail writing.
}
return 1;
}
/**
* Pthread to take care of each client connection.
* @param ptr an ApiConnectionParam
@ -353,7 +394,8 @@ void *api_connection_thread (void *ptr)
}
r = read(s, buf, sizeof buf);
if (r <= 0) {
libp2p_logger_error("api", "Read from client fail.\n");
// this is a common occurrence, so moved from error to debug
libp2p_logger_debug("api", "Read from client fail.\n");
goto quit;
}
buf[r] = '\0';
@ -511,22 +553,20 @@ void *api_connection_thread (void *ptr)
// 404
write_str(s, HTTP_404);
} else {
snprintf(resp, sizeof(resp), "%s 200 OK\r\n" \
snprintf(resp, MAX_READ+1, "%s 200 OK\r\n" \
"Content-Type: %s\r\n"
"Server: c-ipfs/0.0.0-dev\r\n"
"X-Chunked-Output: 1\r\n"
"Connection: close\r\n"
"Transfer-Encoding: chunked\r\n"
"\r\n"
"%x\r\n"
"%s\r\n"
"0\r\n\r\n"
,req.buf + req.http_ver, http_response->content_type, (unsigned int)http_response->bytes_size, http_response->bytes);
ipfs_core_http_response_free(http_response);
,req.buf + req.http_ver, http_response->content_type);
write_str (s, resp);
api_send_resp_chunks(s, http_response->bytes, http_response->bytes_size);
libp2p_logger_debug("api", "resp = {\n%s\n}\n", resp);
}
ipfs_core_http_request_free(http_request);
ipfs_core_http_response_free(http_response);
} else {
// uh oh... something went wrong converting to the HttpRequest struct
libp2p_logger_error("api", "Unable to build HttpRequest struct.\n");
@ -686,6 +726,8 @@ int api_start (struct IpfsNode* local_node, int max_conns, int timeout)
}
local_node->api_context->ipv4 = hostname_to_ip(ip); // api is listening only on loopback.
if (ip != NULL)
free(ip);
local_node->api_context->port = port;
if ((s = socket_listen(socket_tcp4(), &(local_node->api_context->ipv4), &(local_node->api_context->port))) <= 0) {

View File

@ -28,16 +28,23 @@ int api_running(struct IpfsNode* local_node) {
portno = multiaddress_get_ip_port(my_multiaddress);
multiaddress_get_ip_address(my_multiaddress, &ip);
multiaddress_free(my_multiaddress);
if (ip == NULL)
return 0;
int sockfd;
struct sockaddr_in serv_addr;
struct hostent *server;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
free(ip);
return 0;
}
server = gethostbyname(ip);
free(ip);
if (server == NULL) {
return 0;

View File

@ -48,7 +48,6 @@ void ipfs_core_http_request_free(struct HttpRequest* request) {
libp2p_utils_vector_free(request->params);
}
if (request->arguments != NULL) {
// arguments should not be dynamically allocated
//for(int i = 0; i < request->arguments->total; i++) {
// free((char*)libp2p_utils_vector_get(request->arguments, i));
//}
@ -127,6 +126,10 @@ int ipfs_core_http_process_name(struct IpfsNode* local_node, struct HttpRequest*
struct HttpResponse* res = *response;
res->content_type = "application/json";
res->bytes = (uint8_t*) malloc(strlen(local_node->identity->peer->id) + strlen(path) + 30);
if (res->bytes == NULL) {
free(result);
return 0;
}
sprintf((char*)res->bytes, "{ \"Path\": \"%s\" }", result);
res->bytes_size = strlen((char*)res->bytes);
}
@ -138,6 +141,8 @@ int ipfs_core_http_process_name(struct IpfsNode* local_node, struct HttpRequest*
struct HttpResponse* res = *response;
res->content_type = "application/json";
res->bytes = (uint8_t*) malloc(strlen(local_node->identity->peer->id) + strlen(path) + 30);
if (res->bytes == NULL)
return 0;
sprintf((char*)res->bytes, "{ \"Name\": \"%s\"\n \"Value\": \"%s\" }", local_node->identity->peer->id, path);
res->bytes_size = strlen((char*)res->bytes);
}
@ -178,52 +183,61 @@ int ipfs_core_http_process_dht_provide(struct IpfsNode* local_node, struct HttpR
char* hash = (char*)libp2p_utils_vector_get(request->arguments, i);
struct Cid* cid;
if (!ipfs_cid_decode_hash_from_base58((unsigned char*)hash, strlen(hash), &cid)) {
ipfs_cid_free(cid);
cid = NULL;
failedCount++;
continue;
}
if (!local_node->routing->Provide(local_node->routing, cid->hash, cid->hash_length)) {
ipfs_cid_free(cid);
cid = NULL;
failedCount++;
continue;
}
ipfs_cid_free(cid);
}
*response = ipfs_core_http_response_new();
struct HttpResponse* res = *response;
res->content_type = "application/json";
res->bytes = (uint8_t*) malloc(1024);
if (!failedCount) {
// complete success
// TODO: do the right thing
snprintf((char*)res->bytes, 1024, "{\n\t\"ID\": \"<string>\"\n" \
"\t\"Type\": \"<int>\"\n"
"\t\"Responses\": [\n"
"\t\t{\n"
"\t\t\t\"ID\": \"<string>\"\n"
"\t\t\t\"Addrs\": [\n"
"\t\t\t\t\"<object>\"\n"
"\t\t\t]\n"
"\t\t}\n"
"\t]\n"
"\t\"Extra\": \"<string>\"\n"
"}\n"
);
if (res->bytes == NULL) {
res->bytes_size = 0;
} else {
// at least some failed
// TODO: do the right thing
snprintf((char*)res->bytes, 1024, "{\n\t\"ID\": \"<string>\",\n" \
"\t\"Type\": \"<int>\",\n"
"\t\"Responses\": [\n"
"\t\t{\n"
"\t\t\t\"ID\": \"<string>\",\n"
"\t\t\t\"Addrs\": [\n"
"\t\t\t\t\"<object>\"\n"
"\t\t\t]\n"
"\t\t}\n"
"\t],\n"
"\t\"Extra\": \"<string>\"\n"
"}\n"
);
if (!failedCount) {
// complete success
// TODO: do the right thing
snprintf((char*)res->bytes, 1024, "{\n\t\"ID\": \"<string>\"\n" \
"\t\"Type\": \"<int>\"\n"
"\t\"Responses\": [\n"
"\t\t{\n"
"\t\t\t\"ID\": \"<string>\"\n"
"\t\t\t\"Addrs\": [\n"
"\t\t\t\t\"<object>\"\n"
"\t\t\t]\n"
"\t\t}\n"
"\t]\n"
"\t\"Extra\": \"<string>\"\n"
"}\n"
);
} else {
// at least some failed
// TODO: do the right thing
snprintf((char*)res->bytes, 1024, "{\n\t\"ID\": \"<string>\",\n" \
"\t\"Type\": \"<int>\",\n"
"\t\"Responses\": [\n"
"\t\t{\n"
"\t\t\t\"ID\": \"<string>\",\n"
"\t\t\t\"Addrs\": [\n"
"\t\t\t\t\"<object>\"\n"
"\t\t\t]\n"
"\t\t}\n"
"\t],\n"
"\t\"Extra\": \"<string>\"\n"
"}\n"
);
}
res->bytes_size = strlen((char*)res->bytes);
}
res->bytes_size = strlen((char*)res->bytes);
return failedCount < request->arguments->total;
}
@ -241,13 +255,18 @@ int ipfs_core_http_process_dht_get(struct IpfsNode* local_node, struct HttpReque
char* hash = (char*)libp2p_utils_vector_get(request->arguments, i);
struct Cid* cid;
if (!ipfs_cid_decode_hash_from_base58((unsigned char*)hash, strlen(hash), &cid)) {
ipfs_cid_free(cid);
cid = NULL;
failedCount++;
continue;
}
if (!local_node->routing->GetValue(local_node->routing, cid->hash, cid->hash_length, (void**)&res->bytes, &res->bytes_size)) {
ipfs_cid_free(cid);
cid = NULL;
failedCount++;
continue;
}
ipfs_cid_free(cid);
//TODO: we need to handle multiple arguments
}
return failedCount < request->arguments->total;
@ -313,7 +332,9 @@ char* ipfs_core_http_request_build_url_start(struct IpfsNode* local_node) {
sprintf(port, "%d", portInt);
int len = 18 + strlen(host) + strlen(port);
char* retVal = malloc(len);
sprintf(retVal, "http://%s:%s/api/v0", host, port);
if (retVal != NULL) {
sprintf(retVal, "http://%s:%s/api/v0", host, port);
}
free(host);
multiaddress_free(ma);
return retVal;
@ -329,18 +350,20 @@ int ipfs_core_http_request_add_commands(struct HttpRequest* request, char** url)
// command
int addl_length = strlen(request->command) + 2;
char* string1 = (char*) malloc(strlen(*url) + addl_length);
sprintf(string1, "%s/%s", *url, request->command);
free(*url);
*url = string1;
// sub_command
if (request->sub_command != NULL) {
addl_length = strlen(request->sub_command) + 2;
string1 = (char*) malloc(strlen(*url) + addl_length);
sprintf(string1, "%s/%s", *url, request->sub_command);
if (string1 != NULL) {
sprintf(string1, "%s/%s", *url, request->command);
free(*url);
*url = string1;
// sub_command
if (request->sub_command != NULL) {
addl_length = strlen(request->sub_command) + 2;
string1 = (char*) malloc(strlen(*url) + addl_length);
sprintf(string1, "%s/%s", *url, request->sub_command);
free(*url);
*url = string1;
}
}
return 1;
return string1 != NULL;
}
/***
@ -404,9 +427,10 @@ size_t curl_cb(void* ptr, size_t size, size_t nmemb, struct curl_string* str) {
* @param local_node the context
* @param request the request
* @param result the results
* @param result_size the size of the results
* @returns true(1) on success, false(0) on error
*/
int ipfs_core_http_request_get(struct IpfsNode* local_node, struct HttpRequest* request, char** result) {
int ipfs_core_http_request_get(struct IpfsNode* local_node, struct HttpRequest* request, char** result, size_t *result_size) {
if (request == NULL || request->command == NULL)
return 0;
@ -442,13 +466,97 @@ int ipfs_core_http_request_get(struct IpfsNode* local_node, struct HttpRequest*
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res == CURLE_OK) {
if (strcmp(s.ptr, "404 page not found") != 0)
if (strcmp(s.ptr, "404 page not found") != 0) {
*result = s.ptr;
*result_size = s.len;
}
else
res = -1;
} else {
libp2p_logger_error("http_request", "Results of [%s] returned failure. Return value: %d.\n", url, res);
if (s.ptr != NULL)
free(s.ptr);
}
return res == CURLE_OK;
}
/**
* Do an HTTP Post to the local API
* @param local_node the context
* @param request the request
* @param result the results
* @param result_size the size of the results
* @param data the array with post data
* @param data_size the data length
* @returns true(1) on success, false(0) on error
*/
int ipfs_core_http_request_post(struct IpfsNode* local_node, struct HttpRequest* request, char** result, size_t* result_size, char *data, size_t data_size) {
if (request == NULL || request->command == NULL || data == NULL)
return 0;
char* url = ipfs_core_http_request_build_url_start(local_node);
if (url == NULL)
return 0;
if (!ipfs_core_http_request_add_commands(request, &url)) {
free(url);
return 0;
}
if (!ipfs_core_http_request_add_parameters(request, &url)) {
free(url);
return 0;
}
// do the POST using libcurl
CURL *curl;
CURLcode res;
struct curl_string s;
s.len = 0;
s.ptr = malloc(1);
s.ptr[0] = '\0';
struct curl_httppost *post = NULL, *last = NULL;
CURLFORMcode curl_form_ret = curl_formadd(&post, &last,
CURLFORM_COPYNAME, "filename",
CURLFORM_PTRCONTENTS, data,
CURLFORM_CONTENTTYPE, "application/octet-stream",
CURLFORM_FILENAME, "",
CURLFORM_CONTENTSLENGTH, data_size,
CURLFORM_END);
if (CURL_FORMADD_OK != curl_form_ret) {
// i'm always getting curl_form_ret == 4 here
// it means CURL_FORMADD_UNKNOWN_OPTION
// what i'm doing wrong?
fprintf(stderr, "curl_form_ret = %d\n", (int)curl_form_ret);
return 0;
}
curl = curl_easy_init();
if (!curl) {
return 0;
}
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res == CURLE_OK) {
if (strcmp(s.ptr, "404 page not found") != 0) {
*result = s.ptr;
*result_size = s.len;
}
else
res = -1;
} else {
//libp2p_logger_error("http_request", "Results of [%s] returned failure. Return value: %d.\n", url, res);
fprintf(stderr, "Results of [%s] returned failure. Return value: %d.\n", url, res);
if (s.ptr != NULL)
free(s.ptr);
}
return res == CURLE_OK;
}

View File

@ -174,6 +174,11 @@ void* ipfs_null_listen (void *ptr)
connection_param->local_node = listen_param->local_node;
connection_param->port = listen_param->port;
connection_param->ip = malloc(INET_ADDRSTRLEN);
if (connection_param->ip == NULL) {
// we are out of memory
free(connection_param);
continue;
}
if (inet_ntop(AF_INET, &(listen_param->ipv4), connection_param->ip, INET_ADDRSTRLEN) == NULL) {
free(connection_param->ip);
connection_param->ip = NULL;

View File

@ -66,6 +66,10 @@ int ipfs_ping (int argc, char **argv)
// perhaps they passed an IP and port
if (argc >= 3) {
char* str = malloc(strlen(argv[2]) + strlen(argv[3]) + 100);
if (str == NULL) {
// memory issue
goto exit;
}
sprintf(str, "/ip4/%s/tcp/%s", argv[2], argv[3]);
peer_to_ping = libp2p_peer_new();
if (peer_to_ping) {

View File

@ -519,6 +519,10 @@ int ipfs_bitswap_message_protobuf_encode(const struct BitswapMessage* message, u
// protobuf it
size_t temp_size = ipfs_blocks_block_protobuf_encode_size(entry);
uint8_t* temp = (uint8_t*) malloc(temp_size);
if (temp == NULL) {
// memory issues
return 0;
}
if (!ipfs_blocks_block_protobuf_encode(entry, temp, temp_size, &temp_size)) {
free(temp);
return 0;
@ -536,6 +540,9 @@ int ipfs_bitswap_message_protobuf_encode(const struct BitswapMessage* message, u
if (message->wantlist != NULL) {
size_t temp_size = ipfs_bitswap_wantlist_protobuf_encode_size(message->wantlist);
uint8_t* temp = (uint8_t*) malloc(temp_size);
if (temp == NULL) {
return 0;
}
if (!ipfs_bitswap_wantlist_protobuf_encode(message->wantlist, temp, temp_size, &temp_size)) {
free(temp);
return 0;
@ -665,6 +672,9 @@ int ipfs_bitswap_message_add_wantlist_items(struct BitswapMessage* message, stru
struct WantlistEntry* entry = ipfs_bitswap_wantlist_entry_new();
entry->block_size = ipfs_cid_protobuf_encode_size(cidEntry->cid);
entry->block = (unsigned char*) malloc(entry->block_size);
if (entry->block == NULL) {
return 0;
}
if (!ipfs_cid_protobuf_encode(cidEntry->cid, entry->block, entry->block_size, &entry->block_size)) {
// TODO: we should do more than return a half-baked list
return 0;

View File

@ -1,5 +1,5 @@
CC = gcc
CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/include -I../../c-protobuf -Wall -std=c99
CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/include -I../../c-protobuf -Wall -std=c11
ifdef DEBUG
CFLAGS += -g3

View File

@ -36,16 +36,20 @@ int ipfs_exporter_get_node(struct IpfsNode* local_node, const unsigned char* has
goto exit;
}
libp2p_logger_debug("exporter", "get_node got a value. Converting it to a HashtableNode\n");
// unprotobuf
if (!ipfs_hashtable_node_protobuf_decode(buffer, buffer_size, result)) {
libp2p_logger_debug("exporter", "Conversion to HashtableNode not successful\n");
libp2p_logger_error("exporter", "Conversion to HashtableNode not successful\n");
goto exit;
}
// copy in the hash
(*result)->hash_size = hash_size;
(*result)->hash = malloc(hash_size);
if ( (*result)->hash == NULL) {
// memory issue
libp2p_logger_error("exporter", "get_node: Unable to allocate memory.\n");
goto exit;
}
memcpy((*result)->hash, hash, hash_size);
retVal = 1;
@ -267,7 +271,7 @@ int ipfs_exporter_object_cat_to_file(struct IpfsNode *local_node, unsigned char*
* @param argv arguments
* @returns true(1) on success
*/
int ipfs_exporter_object_cat(struct CliArguments* args) {
int ipfs_exporter_object_cat(struct CliArguments* args, FILE* output_file) {
struct IpfsNode *local_node = NULL;
char* repo_dir = NULL;
@ -290,9 +294,10 @@ int ipfs_exporter_object_cat(struct CliArguments* args) {
request->sub_command = "get";
request->arguments = libp2p_utils_vector_new(1);
libp2p_utils_vector_add(request->arguments, hash);
int retVal = ipfs_core_http_request_get(local_node, request, &response);
if (response != NULL && strlen(response) > 0) {
fprintf(stdout, "%s", response);
size_t response_size = 0;
int retVal = ipfs_core_http_request_get(local_node, request, &response, &response_size);
if (response != NULL && response_size > 0) {
fwrite(response, 1, response_size, output_file);
free(response);
} else {
retVal = 0;
@ -307,7 +312,7 @@ int ipfs_exporter_object_cat(struct CliArguments* args) {
return 0;
}
int retVal = ipfs_exporter_object_cat_to_file(local_node, cid->hash, cid->hash_length, stdout);
int retVal = ipfs_exporter_object_cat_to_file(local_node, cid->hash, cid->hash_length, output_file);
ipfs_cid_free(cid);
return retVal;

View File

@ -1,3 +1,6 @@
// these two for strdup
#define _GNU_SOURCE
#define __USE_GNU
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -8,6 +11,7 @@
#include "libp2p/os/utils.h"
#include "ipfs/cmd/cli.h"
#include "ipfs/core/ipfs_node.h"
#include "ipfs/core/http_request.h"
#include "ipfs/repo/fsrepo/fs_repo.h"
#include "ipfs/repo/init.h"
#include "ipfs/unixfs/unixfs.h"
@ -216,6 +220,12 @@ int ipfs_import_file(const char* root_dir, const char* fileName, struct Hashtabl
} else {
free(path);
path = malloc(strlen(root_dir) + strlen(file) + 2);
if (path == NULL) {
// memory issue
if (file != NULL)
free(file);
return 0;
}
os_utils_filepath_join(root_dir, file, path, strlen(root_dir) + strlen(file) + 2);
new_root_dir = path;
}
@ -324,6 +334,9 @@ struct FileList* ipfs_import_get_filelist(struct CliArguments* args) {
continue;
}
struct FileList* current = (struct FileList*)malloc(sizeof(struct FileList));
if (current == NULL) {
return NULL;
}
current->next = NULL;
current->file_name = args->argv[i];
// now wire it in
@ -386,32 +399,55 @@ int ipfs_import_files(struct CliArguments* args) {
}
ipfs_node_offline_new(repo_path, &local_node);
// import the file(s)
current = first;
while (current != NULL) {
if (current->file_name[0] != '-') { // not a switch
os_utils_split_filename(current->file_name, &path, &filename);
size_t bytes_written = 0;
if (!ipfs_import_file(NULL, current->file_name, &directory_entry, local_node, &bytes_written, recursive))
goto exit;
ipfs_import_print_node_results(directory_entry, filename);
// cleanup
if (path != NULL) {
free(path);
path = NULL;
}
if (filename != NULL) {
free(filename);
filename = NULL;
}
if (directory_entry != NULL) {
ipfs_hashtable_node_free(directory_entry);
directory_entry = NULL;
}
/** disabling for the time being
if (local_node->mode == MODE_API_AVAILABLE) {
// do this through the API
struct HttpRequest* request = ipfs_core_http_request_new();
request->command = "add";
struct HttpParam* recursive_param = ipfs_core_http_param_new();
recursive_param->name = strdup("recursive");
recursive_param->value = strdup((recursive ? "true" : "false"));
libp2p_utils_vector_add(request->params, recursive_param);
current = first;
while (current != NULL) {
libp2p_utils_vector_add(request->arguments, current->file_name);
current = current->next;
}
current = current->next;
}
uint8_t* result = NULL;
size_t result_size = 0;
if (!ipfs_core_http_request_post(local_node, request, &result, &result_size, data, data_size)) {
}
} else {
*/
// No daemon is running. Do this without using the API
// import the file(s)
current = first;
while (current != NULL) {
if (current->file_name[0] != '-') { // not a switch
os_utils_split_filename(current->file_name, &path, &filename);
size_t bytes_written = 0;
if (!ipfs_import_file(NULL, current->file_name, &directory_entry, local_node, &bytes_written, recursive))
goto exit;
ipfs_import_print_node_results(directory_entry, filename);
// cleanup
if (path != NULL) {
free(path);
path = NULL;
}
if (filename != NULL) {
free(filename);
filename = NULL;
}
if (directory_entry != NULL) {
ipfs_hashtable_node_free(directory_entry);
directory_entry = NULL;
}
}
current = current->next;
}
// } uncomment this line when the api is up and running with file transfer
retVal = 1;
exit:

View File

@ -28,9 +28,16 @@ int ipfs_resolver_next_path(const char* path, char** next_part) {
char* pos = strchr(&path[i+1], '/');
if (pos == NULL) {
*next_part = (char*)malloc(strlen(path) + 1);
if ( *next_part == NULL) {
// memory issue
return 0;
}
strcpy(*next_part, path);
} else {
*next_part = (char*)malloc(pos - &path[i] + 1);
if (*next_part == NULL) {
return 0;
}
strncpy(*next_part, &path[i], pos-&path[i]);
(*next_part)[pos-&path[i]] = 0;
}
@ -145,9 +152,11 @@ struct HashtableNode* ipfs_resolver_remote_get(const char* path, struct Hashtabl
message->key_size = strlen(key);
size_t b58size = 100;
uint8_t *b58key = (uint8_t *) malloc(b58size);
libp2p_crypto_encoding_base58_encode((unsigned char*)message->key, message->key_size, (unsigned char**) &b58key, &b58size);
libp2p_logger_debug("resolver", "Attempting to use kademlia to get key %s.\n", b58key);
free(b58key);
if (b58key == NULL) {
libp2p_crypto_encoding_base58_encode((unsigned char*)message->key, message->key_size, (unsigned char**) &b58key, &b58size);
libp2p_logger_debug("resolver", "Attempting to use kademlia to get key %s.\n", b58key);
free(b58key);
}
size_t message_protobuf_size = libp2p_message_protobuf_encode_size(message);
unsigned char message_protobuf[message_protobuf_size];
libp2p_message_protobuf_encode(message, message_protobuf, message_protobuf_size, &message_protobuf_size);

View File

@ -10,6 +10,7 @@
#endif
#define MAX_READ (32*1024) // 32k
#define MAX_CHUNK (32*1024) // 32k
struct ApiContext {
int socket;
@ -86,6 +87,7 @@ struct s_request {
#define cstrstart(a,b) (memcmp(a,b,sizeof(b)-1)==0)
#define strstart(a,b) (memcmp(a,b,strlen(b))==0)
int api_send_resp_chunks(int fd, void *buf, size_t size);
void *api_connection_thread (void *ptr);
void api_connections_cleanup (struct IpfsNode* node);
void *api_listen_thread (void *ptr);

View File

@ -71,6 +71,19 @@ int ipfs_core_http_request_process(struct IpfsNode* local_node, struct HttpReque
* @param local_node the context
* @param request the request
* @param result the results
* @param result_size the size of the results
* @returns true(1) on success, false(0) on error
*/
int ipfs_core_http_request_get(struct IpfsNode* local_node, struct HttpRequest* request, char** result);
int ipfs_core_http_request_get(struct IpfsNode* local_node, struct HttpRequest* request, char** result, size_t* result_size);
/**
* Do an HTTP Post to the local API
* @param local_node the context
* @param request the request
* @param result the results
* @param result_size the size of the results
* @param data the array with post data
* @param data_size the data length
* @returns true(1) on success, false(0) on error
*/
int ipfs_core_http_request_post(struct IpfsNode* local_node, struct HttpRequest* request, char** result, size_t* result_size, char *data, size_t data_size);

View File

@ -31,9 +31,10 @@ int ipfs_exporter_object_get(int argc, char** argv);
* Called from the command line with ipfs cat [hash]. Retrieves the object pointed to by hash, and displays its block data (links and data elements)
* @param argc number of arguments
* @param argv arguments
* @param output_file where to stream the results
* @returns true(1) on success
*/
int ipfs_exporter_object_cat(struct CliArguments* args);
int ipfs_exporter_object_cat(struct CliArguments* args, FILE* output_file);
/**
* Retrieves the object pointed to by hash and displays the raw data

View File

@ -21,10 +21,12 @@ struct namesys_pb {
// setting an EOL says "this record is valid until..."
const static IpnsEntry_ValidityType IpnsEntry_EOL = 0;
/*
static char *IpnsEntry_ValidityType_name[] = {
"EOL",
NULL
};
*/
int IpnsEntry_ValidityType_value (char *s);
struct ipns_entry* ipfs_namesys_pb_new_ipns_entry ();

View File

@ -41,6 +41,9 @@ void stripit(int argc, char** argv) {
char* old_arg = argv[argc];
int full_length = strlen(old_arg);
char *tmp = (char*) malloc(full_length + 1);
if (tmp == NULL) {
return;
}
char* ptr1 = &old_arg[1];
strcpy(tmp, ptr1);
tmp[strlen(tmp)-1] = 0;
@ -171,7 +174,7 @@ int main(int argc, char** argv) {
//ipfs_exporter_get(argc, argv);
//break;
case (CAT):
retVal = ipfs_exporter_object_cat(args);
retVal = ipfs_exporter_object_cat(args, stdout);
break;
case (DNS):
retVal = ipfs_dns(argc, argv);

View File

@ -92,7 +92,7 @@ int ipfs_merkledag_add(struct HashtableNode* node, struct FSRepo* fs_repo, size_
ipfs_block_free(block);
return 0;
}
ipfs_block_free(block);
// TODO: call HasBlock (unsure why as yet)
return 1;
}

View File

@ -29,6 +29,7 @@ enum WireType ipfs_node_link_message_fields[] = { WIRETYPE_LENGTH_DELIMITED, WIR
* @Param name: The name of the link (char *)
* @Param size: Size of the link (size_t)
* @Param ahash: An Qmhash
* @returns true(1) on success, false(0) otherwise
*/
int ipfs_node_link_create(char * name, unsigned char * ahash, size_t hash_size, struct NodeLink** node_link)
{
@ -40,12 +41,18 @@ int ipfs_node_link_create(char * name, unsigned char * ahash, size_t hash_size,
// hash
link->hash_size = hash_size;
link->hash = (unsigned char*)malloc(hash_size);
if (link->hash == NULL) {
ipfs_node_link_free(link);
*node_link = NULL;
return 0;
}
memcpy(link->hash, ahash, hash_size);
// name
if (name != NULL && strlen(name) > 0) {
link->name = malloc(strlen(name) + 1);
if ( link->name == NULL) {
free(link);
ipfs_node_link_free(link);
*node_link = NULL;
return 0;
}
strcpy(link->name, name);
@ -192,6 +199,9 @@ int ipfs_node_link_protobuf_decode(unsigned char* buffer, size_t buffer_length,
}
link->hash_size = hash_size - 2;
link->hash = (unsigned char*)malloc(link->hash_size);
if (link->hash == NULL) {
goto exit;
}
memcpy((char*)link->hash, (char*)&hash[2], link->hash_size);
free(hash);
pos += bytes_read;
@ -729,13 +739,18 @@ int Node_Resolve(char ** result, char * input1)
char * tr;
char * end;
tr=strtok_r(input,"/",&end);
int retVal = 1;
for(int i = 0;tr;i++)
{
result[i] = (char *) malloc(strlen(tr)+1);
strcpy(result[i], tr);
if (result[i] != NULL) {
strcpy(result[i], tr);
} else {
retVal = 0;
}
tr=strtok_r(NULL,"/",&end);
}
return 1;
return retVal;
}
/*Node_Resolve_Links
@ -751,6 +766,9 @@ struct Link_Proc * Node_Resolve_Links(struct HashtableNode * N, char * path)
}
int expected_link_ammount = Node_Resolve_Max_Size(path);
struct Link_Proc * LProc = (struct Link_Proc *) malloc(sizeof(struct Link_Proc) + sizeof(struct NodeLink) * expected_link_ammount);
if (LProc == NULL) {
return NULL;
}
LProc->ammount = 0;
char * linknames[expected_link_ammount];
Node_Resolve(linknames, path);
@ -761,8 +779,10 @@ struct Link_Proc * Node_Resolve_Links(struct HashtableNode * N, char * path)
if(proclink)
{
LProc->links[i] = (struct NodeLink *)malloc(sizeof(struct NodeLink));
memcpy(LProc->links[i], proclink, sizeof(struct NodeLink));
LProc->ammount++;
if (LProc->links[i] == NULL) { // TODO: What should we do if memory wasn't allocated here?
memcpy(LProc->links[i], proclink, sizeof(struct NodeLink));
LProc->ammount++;
}
free(proclink);
}
}

View File

@ -20,9 +20,10 @@ int ipfs_name_publish(struct IpfsNode* local_node, char* name) {
request->command = "name";
request->sub_command = "publish";
libp2p_utils_vector_add(request->arguments, name);
int retVal = ipfs_core_http_request_get(local_node, request, &response);
if (response != NULL) {
fprintf(stdout, "%s", response);
size_t response_size = 0;
int retVal = ipfs_core_http_request_get(local_node, request, &response, &response_size);
if (response != NULL && response_size > 0) {
fwrite(response, 1, response_size, stdout);
free(response);
}
ipfs_core_http_request_free(request);
@ -38,9 +39,10 @@ int ipfs_name_resolve(struct IpfsNode* local_node, char* name) {
request->command = "name";
request->sub_command = "resolve";
libp2p_utils_vector_add(request->arguments, name);
int retVal = ipfs_core_http_request_get(local_node, request, &response);
if (response != NULL) {
fprintf(stdout, "%s", response);
size_t response_size = 0;
int retVal = ipfs_core_http_request_get(local_node, request, &response, &response_size);
if (response != NULL && response_size > 0) {
fwrite(response, 1, response_size, stdout);
free(response);
}
ipfs_core_http_request_free(request);

View File

@ -3,6 +3,7 @@
#include "ipfs/namesys/routing.h"
#include "ipfs/namesys/pb.h"
/*
int IpnsEntry_ValidityType_value (char *s)
{
int r;
@ -19,6 +20,7 @@ int IpnsEntry_ValidityType_value (char *s)
return -1; // not found.
}
*/
struct ipns_entry* ipfs_namesys_pb_new_ipns_entry ()
{

View File

@ -41,6 +41,10 @@ int ipfs_namesys_resolver_resolve_once(struct IpfsNode* local_node, const char*
if (local_node->repo->config->datastore->datastore_get(cid->hash, cid->hash_length, &record, local_node->repo->config->datastore)) {
// we are able to handle this locally... return the results
*results = (char*) malloc(record->value_size + 1);
if (*results == NULL) {
ipfs_cid_free(cid);
return 0;
}
memset(*results, 0, record->value_size + 1);
memcpy(*results, record->value, record->value_size);
ipfs_cid_free(cid);
@ -64,6 +68,9 @@ int ipfs_namesys_resolver_resolve_once(struct IpfsNode* local_node, const char*
int ipfs_namesys_resolver_resolve(struct IpfsNode* local_node, const char* path, int recursive, char** results) {
char* result = NULL;
char* current_path = (char*) malloc(strlen(path) + 1);
if (current_path == NULL) {
return 0;
}
strcpy(current_path, path);
// if we go more than 10 deep, bail
@ -84,7 +91,8 @@ int ipfs_namesys_resolver_resolve(struct IpfsNode* local_node, const char* path,
// result will not be NULL
free(current_path);
current_path = (char*) malloc(strlen(result)+1);
strcpy(current_path, result);
if (current_path != NULL)
strcpy(current_path, result);
free(result);
counter++;
} while(recursive && is_ipns_string(current_path));

View File

@ -131,6 +131,8 @@ int ipfs_namesys_hex_string_to_bytes(const unsigned char* hex, unsigned char** b
// allocate memory
*buffer = (unsigned char*)malloc( hex_size / 2 );
unsigned char* ptr = *buffer;
if (ptr == NULL)
return ErrAllocFailed;
// convert string
for(size_t i = 0; i < hex_size; i++) {

View File

@ -7,8 +7,10 @@
char* alloc_and_copy(char* source) {
unsigned long strLen = strlen(source);
char* result = malloc(sizeof(char) * (strLen + 1));
strncpy(result, source, strLen);
result[strLen] = 0;
if (result != NULL) {
strncpy(result, source, strLen);
result[strLen] = 0;
}
return result;
}

View File

@ -139,25 +139,35 @@ int ipfs_repo_config_init(struct RepoConfig* config, unsigned int num_bits_for_k
return 0;
// swarm addresses
char* addr1 = malloc(27);
sprintf(addr1, "/ip4/0.0.0.0/tcp/%d", swarm_port);
config->addresses->swarm_head = libp2p_utils_linked_list_new();
config->addresses->swarm_head->item = malloc(strlen(addr1) + 1);
strcpy(config->addresses->swarm_head->item, addr1);
char* addr1 = malloc(64);
if (addr1 != NULL) {
sprintf(addr1, "/ip4/0.0.0.0/tcp/%d", swarm_port);
config->addresses->swarm_head = libp2p_utils_linked_list_new();
if (config->addresses->swarm_head != NULL) {
config->addresses->swarm_head->item = malloc(strlen(addr1) + 1);
if (config->addresses->swarm_head->item != NULL) {
strcpy(config->addresses->swarm_head->item, addr1);
}
sprintf(addr1, "/ip6/::/tcp/%d", swarm_port);
config->addresses->swarm_head->next = libp2p_utils_linked_list_new();
config->addresses->swarm_head->next->item = malloc(strlen(addr1) + 1);
strcpy(config->addresses->swarm_head->next->item, addr1);
int port_adder = swarm_port - 4001;
sprintf(addr1, "/ip4/127.0.0.1/tcp/%d", 5001 + port_adder);
config->addresses->api = malloc(strlen(addr1)+1);
strcpy(config->addresses->api, addr1);
sprintf(addr1, "/ip4/127.0.0.1/tcp/%d", 8080 + port_adder);
config->addresses->gateway = malloc(strlen(addr1+1));
strcpy(config->addresses->gateway, addr1);
free(addr1);
sprintf(addr1, "/ip6/::/tcp/%d", swarm_port);
config->addresses->swarm_head->next = libp2p_utils_linked_list_new();
if (config->addresses->swarm_head->next != NULL) {
config->addresses->swarm_head->next->item = malloc(strlen(addr1) + 1);
if (config->addresses->swarm_head->next->item != NULL)
strcpy(config->addresses->swarm_head->next->item, addr1);
}
}
int port_adder = swarm_port - 4001;
sprintf(addr1, "/ip4/127.0.0.1/tcp/%d", 5001 + port_adder);
config->addresses->api = malloc(strlen(addr1)+1);
if (config->addresses->api != NULL)
strcpy(config->addresses->api, addr1);
sprintf(addr1, "/ip4/127.0.0.1/tcp/%d", 8080 + port_adder);
config->addresses->gateway = malloc(strlen(addr1)+1);
if (config->addresses->gateway != NULL)
strcpy(config->addresses->gateway, addr1);
free(addr1);
}
config->discovery.mdns.enabled = 1;
config->discovery.mdns.interval = 10;

View File

@ -6,8 +6,10 @@
char* alloc_and_fill(char* source) {
char* newString = malloc(sizeof(char) * (strlen(source) + 1));
strncpy(newString, source, strlen(source));
newString[strlen(source)] = 0;
if (newString != NULL) {
strncpy(newString, source, strlen(source));
newString[strlen(source)] = 0;
}
return newString;
}

View File

@ -150,7 +150,8 @@ int ipfs_repo_fsrepo_new(const char* repo_path, struct RepoConfig* config, struc
} else {
int len = strlen(repo_path) + 1;
(*repo)->path = (char*)malloc(len);
strncpy((*repo)->path, repo_path, len);
if ( (*repo)->path != NULL)
strncpy((*repo)->path, repo_path, len);
}
// allocate other structures
if (config != NULL)
@ -574,10 +575,13 @@ int ipfs_repo_fsrepo_node_get(const unsigned char* hash, size_t hash_length, voi
if (retVal == 1) {
*node_size = ipfs_hashtable_node_protobuf_encode_size(node);
*node_obj = malloc(*node_size);
if (*node_obj == NULL) {
ipfs_hashtable_node_free(node);
return 0;
}
retVal = ipfs_hashtable_node_protobuf_encode(node, *node_obj, *node_size, node_size);
}
if (node != NULL)
ipfs_hashtable_node_free(node);
ipfs_hashtable_node_free(node);
return retVal;
}

View File

@ -220,6 +220,10 @@ int repo_fsrepo_lmdb_put(struct DatastoreRecord* datastore_record, const struct
journalstore_record = lmdb_journal_record_new();