c-ipfs/core/api.c

796 lines
21 KiB
C
Raw Normal View History

2017-07-21 03:40:43 +00:00
/**
* Methods for lightweight/specific HTTP for API communication.
*/
#define _GNU_SOURCE
#define __USE_GNU
2017-07-21 03:40:43 +00:00
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <arpa/inet.h>
2017-09-13 10:02:59 +00:00
#include <sys/uio.h>
2017-07-21 03:40:43 +00:00
#include <fcntl.h>
2017-07-21 03:40:43 +00:00
#include "libp2p/net/p2pnet.h"
2017-09-20 12:53:36 +00:00
#include "libp2p/os/memstream.h"
2017-07-21 03:40:43 +00:00
#include "libp2p/utils/logger.h"
2017-09-25 18:31:03 +00:00
#include "libp2p/utils/urlencode.h"
2017-07-21 03:40:43 +00:00
#include "ipfs/core/api.h"
2017-09-20 12:40:28 +00:00
#include "ipfs/importer/exporter.h"
2017-09-21 20:27:16 +00:00
#include "ipfs/core/http_request.h"
2017-07-21 03:40:43 +00:00
2017-09-25 13:55:42 +00:00
//pthread_mutex_t conns_lock;
//int conns_count;
2017-07-21 03:40:43 +00:00
2017-09-25 13:55:42 +00:00
//struct ApiContext api_list;
2017-07-21 03:40:43 +00:00
/**
* Write two strings on one write.
* @param fd file descriptor to write.
* @param str1 first string to write.
* @param str2 second string to write.
*/
size_t write_dual(int fd, char *str1, char *str2)
{
struct iovec iov[2];
iov[0].iov_base = str1;
iov[0].iov_len = strlen(str1);
iov[1].iov_base = str2;
iov[1].iov_len = strlen(str2);
return writev(fd, iov, 2);
}
2017-08-02 00:17:17 +00:00
int find_chunk(char *buf, const size_t buf_size, size_t *pos, size_t *size)
{
char *p = NULL;
*size = strtol(buf, &p, 16);
2017-09-13 10:02:59 +00:00
if (!p || p < buf || p > (buf + 10)) {
2017-08-02 00:17:17 +00:00
return 0;
}
*pos = (int)(p - buf);
if (p[0] == '\r' && p[1] == '\n') {
*pos += 2;
return 1;
}
return 0;
}
int read_chunked(int fd, struct s_request *req, char *already, size_t already_size)
{
char buf[MAX_READ], *p;
size_t pos, nsize, buf_size = 0, r;
2017-08-02 00:17:17 +00:00
if (already_size > 0) {
if (already_size <= sizeof(buf)) {
2017-08-02 00:17:17 +00:00
memcpy(buf, already, already_size);
buf_size += already_size;
already_size = 0;
} else {
memcpy(buf, already, sizeof(buf));
already += sizeof(buf);
buf_size += sizeof(buf);
already_size -= sizeof(buf);
2017-08-02 00:17:17 +00:00
}
}
while(buf_size) {
if (!find_chunk(buf, buf_size, &pos, &nsize)) {
libp2p_logger_error("api", "fail find_chunk.\n");
libp2p_logger_error("api", "nsize = %d.\n", nsize);
2017-08-02 00:17:17 +00:00
return 0;
}
if (nsize == 0) {
break;
}
p = realloc(req->buf, req->size + nsize);
if (!p) {
libp2p_logger_error("api", "fail realloc.\n");
2017-08-02 00:17:17 +00:00
return 0;
}
req->buf = p;
req->size += nsize;
CPCHUNK:
r = nsize;
buf_size -= pos;
if (r > buf_size) {
r = buf_size;
}
memcpy(req->buf + req->body + req->body_size, buf + pos, r);
req->body_size += r;
nsize -= r;
buf_size -= r;
if (buf_size > 0) {
memmove(buf, buf + pos + r, buf_size);
}
pos = 0;
if (already_size > 0) {
r = sizeof(buf) - buf_size;
if (already_size <= r) {
memcpy(buf, already, already_size);
buf_size += already_size;
already_size = 0;
} else {
memcpy(buf, already, r);
already += r;
buf_size += r;
already_size -= r;
}
}
if (socket_read_select4(fd, 5) > 0) {
r = sizeof(buf) - buf_size;
r = read(fd, buf+buf_size, r);
buf_size += r;
if (r == 0 && nsize == 0) {
2017-08-02 00:17:17 +00:00
break;
}
if (r <= 0) {
2017-08-02 00:17:17 +00:00
libp2p_logger_error("api", "read fail.\n");
return 0;
2017-08-02 00:17:17 +00:00
}
}
if (nsize > 0)
goto CPCHUNK; // still have data to transfer on current chunk.
if (memcmp (buf, "\r\n", 2)!=0) {
libp2p_logger_error("api", "fail CRLF.\n");
2017-08-02 00:17:17 +00:00
return 0;
}
}
return 1;
}
int read_all(int fd, struct s_request *req, char *already, size_t alread_size)
{
char buf[MAX_READ], *p;
size_t size = 0;
if (alread_size > 0) {
p = realloc(req->buf, req->size + alread_size);
if (!p) {
return 0;
}
req->buf = p;
req->size += alread_size;
memcpy(req->buf + req->body + req->body_size, already, alread_size);
req->body_size += alread_size;
}
for(;;) {
if (socket_read_select4(fd, 5) <= 0) {
break;
}
size = read(fd, buf, sizeof buf);
if (size <= 0) {
break;
}
p = realloc(req->buf, req->size + size);
if (!p) {
return 0;
}
req->buf = p;
req->size += size;
memcpy(req->buf + req->body + req->body_size, buf, size);
req->body_size += size;
}
return 1;
}
/**
* Find a token in a string array.
* @param string array and token string.
* @returns the pointer after where the token was found or NULL if it fails.
*/
char *str_tok(char *str, char *tok)
{
char *p = strstr(str, tok);
if (p) {
p += strlen(tok);
while(*p == ' ') p++;
}
return p;
}
/**
* Find a token in a binary array.
* @param array, size of array, token and size of token.
* @returns the pointer after where the token was found or NULL if it fails.
*/
char *bin_tok(char *bin, size_t limit, char *tok, size_t tok_size)
{
char *p = memmem(bin, limit, tok, tok_size);
if (p) {
p += tok_size;
}
return p;
}
/**
* Check if header contain a especific value.
* @param request structure, header name and value to check.
* @returns the pointer where the value was found or NULL if it fails.
*/
char *header_value_cmp(struct s_request *req, char *header, char *value)
{
char *p = str_tok(req->buf + req->header, header);
if (p) {
if (strstart(p, value)) {
return p;
}
}
return NULL;
}
/**
* Lookup for boundary at buffer string.
* @param body buffer string, boundary id, filename and content-type string.
* @returns the pointer where the multipart start.
*/
char *boundary_find(char *str, char *boundary, char **filename, char **contenttype)
{
char *p = str_tok(str, "--");
while (p) {
if (strstart(p, boundary)) {
// skip to the beginning, ignoring the header for now, if there is.
// TODO: return filename and content-type
p = strstr(p, "\r\n\r\n");
if (p) {
return p + 4; // ignore 4 bytes CRLF 2x
}
break;
}
p = str_tok(str, "--");
}
return NULL;
}
/**
* Return the size of boundary.
* @param boundary buffer, boundary id.
* @returns the size of boundary or 0 if fails.
*/
size_t boundary_size(char *str, char *boundary, size_t limit)
{
char *p = bin_tok(str, limit, "\r\n--", 4);
while (p) {
if (strstart(p, boundary)) {
if (cstrstart(p + strlen(boundary), "--\r\n")) {
p -= 4;
return (size_t)(p - str);
}
}
p = bin_tok(p, limit, "\r\n--", 4);
}
return 0;
}
2017-09-20 11:02:13 +00:00
/**
* function to find and read the object.
* @param path is the ipfs address, obj is a pointer to be allocated and will be the return of the data, size is a pointer to return the data length.
* @returns 1 when success is 0 when failure.
*/
2017-09-20 12:32:12 +00:00
int get_object(struct IpfsNode* local_node, char *path, unsigned char **obj, size_t *size)
2017-09-20 11:02:13 +00:00
{
2017-09-20 12:02:00 +00:00
FILE* memstream_file = NULL;
char* memstream_char = NULL;
size_t memstream_size = 0;
struct Cid* cid = NULL;
// convert hash to cid
if ( ipfs_cid_decode_hash_from_base58((unsigned char*)path, strlen(path), &cid) == 0) {
return 0;
}
// find block
struct HashtableNode* read_node = NULL;
if (!ipfs_exporter_get_node(local_node, cid->hash, cid->hash_length, &read_node)) {
ipfs_cid_free(cid);
return 0;
}
// open a memory stream
memstream_file = open_memstream(&memstream_char, &memstream_size);
if (memstream_file == NULL) {
libp2p_logger_error("api", "get_object: Unable to open a memory stream.\n");
ipfs_cid_free(cid);
return 0;
}
// throw everything (including links) into the memory stream
2017-09-20 12:53:36 +00:00
ipfs_exporter_cat_node(read_node, local_node, memstream_file);
2017-09-20 12:02:00 +00:00
fclose(memstream_file);
// no longer need these
ipfs_cid_free(cid);
2017-09-20 12:53:36 +00:00
ipfs_hashtable_node_free(read_node);
2017-09-20 12:02:00 +00:00
*size = memstream_size;
2017-09-20 12:53:36 +00:00
*obj = (unsigned char*)memstream_char;
2017-09-20 12:02:00 +00:00
return 1;
2017-09-20 11:02:13 +00:00
}
/**
* send object data as an http response.
* @param socket, object pointer and size.
* @returns 1 when success is 0 when failure.
*/
int send_object(int socket, unsigned char *obj, size_t size)
{
// TODO: implement.
return 0; // fail.
}
2017-09-20 12:32:12 +00:00
struct ApiConnectionParam {
int index;
struct IpfsNode* this_node;
};
2017-09-21 22:10:41 +00:00
/***
* Take an s_request and turn it into an HttpRequest
* @param req the incoming s_request
* @returns the resultant HttpRequest or NULL on error
*/
struct HttpRequest* api_build_http_request(struct s_request* req) {
struct HttpRequest* request = ipfs_core_http_request_new();
if (request != NULL) {
char *segs = malloc (strlen(req->buf + req->request) + 1);
if (segs) {
strcpy(segs, req->buf + req->request);
request->command = segs;
segs = strchr(segs, '/');
if (segs) {
*segs++ = '\0';
request->sub_command = segs; // sub_command can contain another level as filters/add
}
if (req->query) {
2017-09-25 18:31:03 +00:00
segs = libp2p_utils_url_decode(req->buf + req->query);
if (segs) {
while (segs) {
char *value, *name = segs;
segs = strchr(segs, '&');
if (segs) { // calc next to split before search for = on another parameter.
*segs++ = '\0';
}
value = strchr(name, '=');
2017-09-22 14:52:31 +00:00
if (value) {
*value++ = '\0';
}
if (value && (strcmp(name, "arg")==0)) {
2017-09-25 18:55:14 +00:00
libp2p_utils_vector_add(request->arguments, strdup(value));
2017-09-22 14:52:31 +00:00
} else {
struct HttpParam *hp = ipfs_core_http_param_new();
if (hp) {
2017-09-25 18:55:14 +00:00
hp->name = strdup(name);
hp->value = strdup(value); // maybe null ?
libp2p_utils_vector_add(request->params, hp);
}
}
}
2017-09-25 18:55:14 +00:00
free(segs);
}
}
}
2017-09-21 22:10:41 +00:00
}
return request;
}
2017-07-21 03:40:43 +00:00
/**
2017-07-23 14:49:40 +00:00
* Pthread to take care of each client connection.
2017-09-20 12:32:12 +00:00
* @param ptr an ApiConnectionParam
2017-07-21 03:40:43 +00:00
* @returns nothing
*/
void *api_connection_thread (void *ptr)
{
int timeout, s, r;
2017-09-20 12:32:12 +00:00
struct ApiConnectionParam* params = (struct ApiConnectionParam*)ptr;
char resp[MAX_READ+1], buf[MAX_READ+1], *p, *body;
2017-07-21 03:40:43 +00:00
char client[INET_ADDRSTRLEN];
struct s_request req;
int (*read_func)(int, struct s_request*, char*, size_t) = read_all;
req.buf = NULL; // sanity.
2017-07-21 03:40:43 +00:00
buf[MAX_READ] = '\0';
2017-09-25 13:55:42 +00:00
s = params->this_node->api_context->conns[params->index]->socket;
timeout = params->this_node->api_context->timeout;
2017-07-21 03:40:43 +00:00
if (socket_read_select4(s, timeout) <= 0) {
libp2p_logger_error("api", "Client connection timeout.\n");
goto quit;
}
r = read(s, buf, sizeof buf);
if (r <= 0) {
libp2p_logger_error("api", "Read from client fail.\n");
goto quit;
}
buf[r] = '\0';
p = strstr(buf, "\r\n\r\n");
if (p) {
body = p + 4;
req.size = p - buf + 1;
req.buf = malloc(req.size);
if (!req.buf) {
// memory allocation fail.
libp2p_logger_error("api", "malloc fail.\n");
write_cstr (s, HTTP_500);
goto quit;
}
memcpy(req.buf, buf, req.size - 1);
req.buf[req.size-1] = '\0';
req.method = 0;
p = strchr(req.buf + req.method, ' ');
if (!p) {
libp2p_logger_error("api", "fail looking for space on method '%s'.\n", req.buf + req.method);
write_cstr (s, HTTP_400);
goto quit;
}
*p++ = '\0'; // End of method.
req.path = p - req.buf;
if (strchr(p, '?')) {
p = strchr(p, '?');
*p++ = '\0';
req.query = p - req.buf;
} else {
req.query = 0;
}
p = strchr(p, ' ');
if (!p) {
libp2p_logger_error("api", "fail looking for space on path '%s'.\n", req.buf + req.path);
write_cstr (s, HTTP_400);
goto quit;
}
*p++ = '\0'; // End of path.
req.http_ver = p - req.buf;
p = strchr(req.buf + req.http_ver, '\r');
if (!p) {
libp2p_logger_error("api", "fail looking for CR on http_ver '%s'.\n", req.buf + req.http_ver);
write_cstr (s, HTTP_400);
goto quit;
}
*p++ = '\0'; // End of http version.
while (*p == '\r' || *p == '\n') p++;
req.header = p - req.buf;
req.body = req.size;
req.body_size = 0;
if (header_value_cmp(&req, "Transfer-Encoding:", "chunked")) {
read_func = read_chunked;
}
if (!read_func(s, &req, body, r - (body - buf))) {
libp2p_logger_error("api", "fail read_func.\n");
write_cstr (s, HTTP_500);
goto quit;
}
2017-09-21 22:10:41 +00:00
// once we leave the building of the req struct, do we need to do more? This flag will tell us.
int further_processing_necessary = 0;
2017-09-21 22:10:41 +00:00
2017-09-25 12:34:54 +00:00
if (strncmp(req.buf + req.method, "GET", 3)==0) {
if (strcmp (req.buf + req.path, "/")==0 ||
strcmp (req.buf + req.path, "/webui")==0 ||
2017-09-20 11:02:13 +00:00
strcmp (req.buf + req.path, "/webui/")==0) {
2017-09-21 14:50:15 +00:00
char *redir;
size_t size = sizeof(HTTP_301) + (sizeof(WEBUI_ADDR)*2);
redir = malloc(size);
if (redir) {
snprintf(redir, size, HTTP_301, WEBUI_ADDR, WEBUI_ADDR);
redir[size-1] = '\0'; // just in case
write_dual (s, req.buf + req.http_ver, strchr (redir, ' '));
free (redir);
} else {
write_cstr (s, HTTP_500);
}
} else if (cstrstart(req.buf + req.path, API_V0_START)) {
req.request = req.path + sizeof(API_V0_START) - 1;
further_processing_necessary = 1;
2017-09-20 11:02:13 +00:00
} else {
// TODO: handle download file here.
2017-09-21 22:10:41 +00:00
// move out of the if to do further processing
2017-09-20 11:02:13 +00:00
}
2017-09-21 22:10:41 +00:00
// end of GET
2017-09-25 12:34:54 +00:00
} else if (strncmp(req.buf + req.method, "POST", 4)==0) {
// TODO: Handle gzip/json POST requests.
p = header_value_cmp(&req, "Content-Type:", "multipart/form-data;");
if (p) {
p = str_tok(p, "boundary=");
if (p) {
char *boundary, *l;
int len;
if (*p == '"') {
p++;
l = strchr(p, '"');
} else {
l = p;
while (*l != '\r' && *l != '\0') l++;
}
len = l - p;
boundary = malloc (len+1);
if (boundary) {
memcpy(boundary, p, len);
boundary[len] = '\0';
p = boundary_find(req.buf + req.body, boundary, NULL, NULL);
if (p) {
req.boundary_size = boundary_size(p, boundary, req.size - (p - buf));
if (req.boundary_size > 0) {
req.boundary = p - req.buf;
}
}
free (boundary);
}
}
}
2017-09-21 22:10:41 +00:00
if (req.boundary > 0) {
libp2p_logger_error("api", "boundary index = %d, size = %d\n", req.boundary, req.boundary_size);
}
2017-09-21 22:10:41 +00:00
libp2p_logger_debug("api", "method = '%s'\n"
"path = '%s'\n"
"http_ver = '%s'\n"
"header {\n%s\n}\n"
"body_size = %d\n",
req.buf+req.method, req.buf+req.path, req.buf+req.http_ver,
req.buf+req.header, req.body_size);
2017-09-21 22:10:41 +00:00
// end of POST
2017-09-20 11:02:13 +00:00
} else {
// Unexpected???
libp2p_logger_error("api", "fail unexpected '%s'.\n", req.buf + req.method);
write_cstr (s, HTTP_500);
2017-09-21 22:10:41 +00:00
further_processing_necessary = 0;
}
if (further_processing_necessary) {
// now do something with the request we have built
struct HttpRequest* http_request = api_build_http_request(&req);
if (http_request != NULL) {
char* response_text = NULL;
if (!ipfs_core_http_request_process(params->this_node, http_request, &response_text)) {
libp2p_logger_error("api", "ipfs_core_http_request_process returned false.\n");
2017-09-25 22:56:10 +00:00
// 404
write_str(s, HTTP_404);
} else {
// now send the results
snprintf(resp, sizeof(resp), "%s 200 OK\r\n" \
"Content-Type: application/json\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, strlen(response_text), response_text);
if (response_text != NULL)
free(response_text);
write_str (s, resp);
libp2p_logger_debug("api", "resp = {\n%s\n}\n", resp);
2017-09-21 22:10:41 +00:00
}
ipfs_core_http_request_free(http_request);
} else {
// uh oh... something went wrong converting to the HttpRequest struct
libp2p_logger_error("api", "Unable to build HttpRequest struct.\n");
}
}
2017-07-21 03:40:43 +00:00
} else {
libp2p_logger_error("api", "fail looking for body.\n");
write_cstr (s, HTTP_400);
2017-07-21 03:40:43 +00:00
}
quit:
if (req.buf)
free(req.buf);
2017-09-25 13:55:42 +00:00
if (inet_ntop(AF_INET, &( params->this_node->api_context->conns[params->index]->ipv4), client, INET_ADDRSTRLEN) == NULL)
2017-07-21 03:40:43 +00:00
strcpy(client, "UNKNOW");
libp2p_logger_debug("api", "Closing client connection %s:%d (%d).\n", client, params->this_node->api_context->conns[params->index]->port, params->index+1);
2017-09-25 13:55:42 +00:00
pthread_mutex_lock(&params->this_node->api_context->conns_lock);
2017-07-21 03:40:43 +00:00
close(s);
2017-09-25 13:55:42 +00:00
free ( params->this_node->api_context->conns[params->index]);
params->this_node->api_context->conns[params->index] = NULL;
params->this_node->api_context->conns_count--;
pthread_mutex_unlock(&params->this_node->api_context->conns_lock);
2017-09-20 12:32:12 +00:00
free(params);
2017-07-21 03:40:43 +00:00
return NULL;
}
/**
* Close all connections stopping respectives pthreads and free allocated memory.
*/
2017-09-25 13:55:42 +00:00
void api_connections_cleanup (struct IpfsNode* local_node)
2017-07-21 03:40:43 +00:00
{
int i;
2017-09-25 13:55:42 +00:00
pthread_mutex_lock(&local_node->api_context->conns_lock);
if (local_node->api_context->conns_count > 0 && local_node->api_context->conns) {
for (i = 0 ; i < local_node->api_context->max_conns ; i++) {
if (local_node->api_context->conns[i]->pthread) {
pthread_cancel (local_node->api_context->conns[i]->pthread);
close (local_node->api_context->conns[i]->socket);
free (local_node->api_context->conns[i]);
local_node->api_context->conns[i] = NULL;
2017-07-21 03:40:43 +00:00
}
}
2017-09-25 13:55:42 +00:00
local_node->api_context->conns_count = 0;
2017-07-21 03:40:43 +00:00
}
2017-09-25 13:55:42 +00:00
if (local_node->api_context->conns) {
free (local_node->api_context->conns);
local_node->api_context->conns = NULL;
2017-07-21 03:40:43 +00:00
}
2017-09-25 13:55:42 +00:00
pthread_mutex_unlock(&local_node->api_context->conns_lock);
2017-07-21 03:40:43 +00:00
}
/**
* Pthread to keep in background dealing with client connections.
* @param ptr is not used.
* @returns nothing
*/
void *api_listen_thread (void *ptr)
{
int s;
INT_TYPE i;
uint32_t ipv4;
uint16_t port;
char client[INET_ADDRSTRLEN];
2017-09-20 12:32:12 +00:00
struct IpfsNode* local_node = (struct IpfsNode*)ptr;
2017-07-21 03:40:43 +00:00
2017-09-25 13:55:42 +00:00
local_node->api_context->conns_count = 0;
2017-07-21 03:40:43 +00:00
for (;;) {
2017-09-25 13:55:42 +00:00
s = socket_accept4(local_node->api_context->socket, &ipv4, &port);
2017-07-21 03:40:43 +00:00
if (s <= 0) {
break;
}
2017-09-25 13:55:42 +00:00
if (local_node->api_context->conns_count >= local_node->api_context->max_conns) { // limit reached.
libp2p_logger_error("api", "Limit of connections reached (%d).\n", local_node->api_context->max_conns);
2017-07-21 03:40:43 +00:00
close (s);
continue;
}
2017-09-25 13:55:42 +00:00
pthread_mutex_lock(&local_node->api_context->conns_lock);
for (i = 0 ; i < local_node->api_context->max_conns && local_node->api_context->conns[i] ; i++);
local_node->api_context->conns[i] = malloc (sizeof (struct s_conns));
if (!local_node->api_context->conns[i]) {
2017-07-21 03:40:43 +00:00
libp2p_logger_error("api", "Fail to allocate memory to accept connection.\n");
2017-09-25 13:55:42 +00:00
pthread_mutex_unlock(&local_node->api_context->conns_lock);
2017-07-21 03:40:43 +00:00
close (s);
continue;
}
if (inet_ntop(AF_INET, &ipv4, client, INET_ADDRSTRLEN) == NULL)
strcpy(client, "UNKNOW");
2017-09-25 13:55:42 +00:00
local_node->api_context->conns[i]->socket = s;
local_node->api_context->conns[i]->ipv4 = ipv4;
local_node->api_context->conns[i]->port = port;
2017-09-20 12:32:12 +00:00
// create a struct, which the thread is responsible to destroy
struct ApiConnectionParam* connection_param = (struct ApiConnectionParam*) malloc(sizeof(struct ApiConnectionParam));
if (connection_param == NULL) {
libp2p_logger_error("api", "api_listen_thread: Unable to allocate memory.\n");
2017-09-25 13:55:42 +00:00
pthread_mutex_unlock(&local_node->api_context->conns_lock);
2017-09-20 12:32:12 +00:00
close (s);
continue;
}
connection_param->index = i;
connection_param->this_node = local_node;
2017-09-25 13:55:42 +00:00
if (pthread_create(&(local_node->api_context->conns[i]->pthread), NULL, api_connection_thread, (void*)connection_param)) {
2017-07-21 03:40:43 +00:00
libp2p_logger_error("api", "Create pthread fail.\n");
2017-09-25 13:55:42 +00:00
free (local_node->api_context->conns[i]);
local_node->api_context->conns[i] = NULL;
local_node->api_context->conns_count--;
2017-07-21 03:40:43 +00:00
close(s);
2017-07-23 14:49:40 +00:00
} else {
2017-09-25 13:55:42 +00:00
local_node->api_context->conns_count++;
2017-07-21 03:40:43 +00:00
}
libp2p_logger_debug("api", "API for %s: Accept connection %s:%d (%d/%d), pthread %d.\n", local_node->identity->peer->id, client, port, local_node->api_context->conns_count, local_node->api_context->max_conns, i+1);
2017-09-25 13:55:42 +00:00
pthread_mutex_unlock(&local_node->api_context->conns_lock);
2017-07-21 03:40:43 +00:00
}
2017-09-25 13:55:42 +00:00
api_connections_cleanup (local_node);
2017-07-21 03:40:43 +00:00
return NULL;
}
2017-09-25 14:20:51 +00:00
struct ApiContext* api_context_new() {
struct ApiContext* context = (struct ApiContext*) malloc(sizeof(struct ApiContext));
if (context != NULL) {
context->conns = NULL;
context->conns_count = 0;
context->ipv4 = 0;
context->max_conns = 0;
context->port = 0;
context->socket = 0;
context->timeout = 0;
pthread_mutex_init(&context->conns_lock, NULL);
2017-09-25 14:20:51 +00:00
}
return context;
}
2017-07-21 03:40:43 +00:00
/**
* Start API interface daemon.
2017-09-20 12:32:12 +00:00
* @param local_node the context
2017-07-21 03:40:43 +00:00
* @param max_conns.
* @param timeout time out of client connection.
* @returns 0 when failure or 1 if success.
*/
2017-09-25 13:55:42 +00:00
int api_start (struct IpfsNode* local_node, int max_conns, int timeout)
2017-07-21 03:40:43 +00:00
{
int s;
size_t alloc_size = sizeof(void*) * max_conns;
2017-09-20 12:32:12 +00:00
struct MultiAddress* my_address = multiaddress_new_from_string(local_node->repo->config->addresses->api);
2017-09-20 12:40:28 +00:00
char* ip = NULL;
multiaddress_get_ip_address(my_address, &ip);
int port = multiaddress_get_ip_port(my_address);
2017-09-20 12:32:12 +00:00
2017-09-25 14:20:51 +00:00
local_node->api_context = api_context_new();
if (local_node->api_context == NULL) {
multiaddress_free(my_address);
return 0;
}
2017-09-25 13:55:42 +00:00
local_node->api_context->ipv4 = hostname_to_ip(ip); // api is listening only on loopback.
local_node->api_context->port = port;
2017-07-21 03:40:43 +00:00
2017-09-25 13:55:42 +00:00
if ((s = socket_listen(socket_tcp4(), &(local_node->api_context->ipv4), &(local_node->api_context->port))) <= 0) {
2017-07-21 03:40:43 +00:00
libp2p_logger_error("api", "Failed to init API. port: %d\n", port);
return 0;
}
2017-09-25 13:55:42 +00:00
local_node->api_context->socket = s;
local_node->api_context->max_conns = max_conns;
local_node->api_context->timeout = timeout;
2017-07-21 03:40:43 +00:00
2017-09-25 13:55:42 +00:00
local_node->api_context->conns = malloc (alloc_size);
if (!local_node->api_context->conns) {
2017-07-21 03:40:43 +00:00
close (s);
libp2p_logger_error("api", "Error allocating memory.\n");
return 0;
}
2017-09-25 13:55:42 +00:00
memset(local_node->api_context->conns, 0, alloc_size);
2017-07-21 03:40:43 +00:00
2017-09-25 13:55:42 +00:00
if (pthread_create(&local_node->api_context->api_thread, NULL, api_listen_thread, (void*)local_node)) {
2017-07-21 03:40:43 +00:00
close (s);
2017-09-25 13:55:42 +00:00
free (local_node->api_context->conns);
local_node->api_context->conns = NULL;
local_node->api_context->api_thread = 0;
2017-07-21 03:40:43 +00:00
libp2p_logger_error("api", "Error creating thread for API.\n");
return 0;
}
2017-09-25 13:55:42 +00:00
libp2p_logger_debug("api", "Started API on localhost port %d.\n", port);
2017-07-21 03:40:43 +00:00
return 1;
}
/**
* Stop API.
* @returns 0 when failure or 1 if success.
*/
2017-09-25 13:55:42 +00:00
int api_stop (struct IpfsNode *local_node)
2017-07-21 03:40:43 +00:00
{
2017-09-25 13:55:42 +00:00
if (local_node->api_context->api_thread == 0) return 0;
pthread_cancel(local_node->api_context->api_thread);
2017-07-21 03:40:43 +00:00
2017-09-25 13:55:42 +00:00
api_connections_cleanup (local_node);
2017-07-21 03:40:43 +00:00
2017-09-25 13:55:42 +00:00
local_node->api_context->api_thread = 0;
2017-07-21 03:40:43 +00:00
return 1;
}