2014-09-15 20:22:02 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
|
2014-09-17 11:20:23 +02:00
|
|
|
* 2014 Johannes Schauer <j.schauer@email.de>
|
2014-09-15 20:22:02 +02:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
|
* under the terms of the GNU General Public License version 2, as published by
|
|
|
|
|
* the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2014-10-02 08:20:08 +02:00
|
|
|
#define _POSIX_C_SOURCE 200809L // for strdup
|
2015-01-21 23:43:45 +00:00
|
|
|
#define _DEFAULT_SOURCE // for strdup on old systems
|
2014-10-02 08:20:08 +02:00
|
|
|
|
2014-09-15 20:00:05 +02:00
|
|
|
#include <openssl/md5.h>
|
2014-09-20 09:40:59 +02:00
|
|
|
#include <openssl/sha.h>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2014-12-29 13:57:53 +01:00
|
|
|
#include <unistd.h>
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-18 09:11:00 +02:00
|
|
|
#include "../utils/strings.h"
|
|
|
|
|
#include "apicalls.h"
|
2014-09-20 09:40:59 +02:00
|
|
|
#include "mfconn.h"
|
2014-09-18 09:11:00 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
struct mfconn {
|
|
|
|
|
char *server;
|
|
|
|
|
uint32_t secret_key;
|
|
|
|
|
char *secret_time;
|
|
|
|
|
char *session_token;
|
2014-12-29 15:11:53 +01:00
|
|
|
char *ekey;
|
2014-12-06 11:04:04 +01:00
|
|
|
char *username;
|
|
|
|
|
char *password;
|
|
|
|
|
int app_id;
|
|
|
|
|
char *app_key;
|
|
|
|
|
int max_num_retries;
|
2014-09-18 09:11:00 +02:00
|
|
|
};
|
|
|
|
|
|
2014-09-21 15:59:52 +02:00
|
|
|
mfconn *mfconn_create(const char *server, const char *username,
|
|
|
|
|
const char *password, int app_id,
|
2014-12-06 11:04:04 +01:00
|
|
|
const char *app_key, int max_num_retries)
|
2014-09-18 09:11:00 +02:00
|
|
|
{
|
2014-09-20 10:59:54 +02:00
|
|
|
mfconn *conn;
|
|
|
|
|
int retval;
|
2014-09-18 09:11:00 +02:00
|
|
|
|
2014-09-23 19:30:50 +02:00
|
|
|
if (server == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
if (username == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
if (password == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
if (app_id < 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
conn = (mfconn *) calloc(1, sizeof(mfconn));
|
2014-09-18 09:11:00 +02:00
|
|
|
|
2014-09-19 23:13:29 +02:00
|
|
|
conn->server = strdup(server);
|
2014-12-06 11:04:04 +01:00
|
|
|
conn->username = strdup(username);
|
|
|
|
|
conn->password = strdup(password);
|
|
|
|
|
conn->app_id = app_id;
|
|
|
|
|
if (app_key != NULL)
|
|
|
|
|
conn->app_key = strdup(app_key);
|
|
|
|
|
else
|
|
|
|
|
conn->app_key = NULL;
|
|
|
|
|
conn->max_num_retries = max_num_retries;
|
|
|
|
|
conn->secret_time = NULL;
|
|
|
|
|
conn->session_token = NULL;
|
2014-12-29 15:11:53 +01:00
|
|
|
conn->ekey = NULL;
|
2014-09-19 23:13:29 +02:00
|
|
|
retval = mfconn_api_user_get_session_token(conn, conn->server,
|
2014-12-06 11:04:04 +01:00
|
|
|
conn->username, conn->password,
|
|
|
|
|
conn->app_id, conn->app_key,
|
2014-09-20 10:59:54 +02:00
|
|
|
&(conn->secret_key),
|
|
|
|
|
&(conn->secret_time),
|
2014-12-29 15:11:53 +01:00
|
|
|
&(conn->session_token),
|
|
|
|
|
&(conn->ekey));
|
2014-09-18 09:11:00 +02:00
|
|
|
|
2014-12-06 11:04:04 +01:00
|
|
|
if (retval != 0) {
|
2014-09-23 20:52:30 +02:00
|
|
|
fprintf(stderr, "error: mfconn_api_user_get_session_token\n");
|
2014-09-18 09:11:00 +02:00
|
|
|
return NULL;
|
2014-09-23 20:52:30 +02:00
|
|
|
}
|
2014-12-06 11:04:04 +01:00
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int mfconn_refresh_token(mfconn * conn)
|
|
|
|
|
{
|
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
|
|
free(conn->secret_time);
|
|
|
|
|
conn->secret_time = NULL;
|
|
|
|
|
free(conn->session_token);
|
|
|
|
|
conn->session_token = NULL;
|
2014-12-29 15:11:53 +01:00
|
|
|
free(conn->ekey);
|
|
|
|
|
conn->ekey = NULL;
|
2014-12-06 11:04:04 +01:00
|
|
|
retval = mfconn_api_user_get_session_token(conn, conn->server,
|
|
|
|
|
conn->username, conn->password,
|
|
|
|
|
conn->app_id, conn->app_key,
|
|
|
|
|
&(conn->secret_key),
|
|
|
|
|
&(conn->secret_time),
|
2014-12-29 15:11:53 +01:00
|
|
|
&(conn->session_token),
|
|
|
|
|
&(conn->ekey));
|
2014-12-06 11:04:04 +01:00
|
|
|
if (retval != 0) {
|
|
|
|
|
fprintf(stderr, "user/get_session_token failed\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2014-09-18 09:11:00 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
void mfconn_destroy(mfconn * conn)
|
2014-09-18 09:11:00 +02:00
|
|
|
{
|
2014-09-19 23:13:29 +02:00
|
|
|
free(conn->server);
|
2014-12-06 11:04:04 +01:00
|
|
|
free(conn->username);
|
|
|
|
|
free(conn->password);
|
|
|
|
|
if (conn->app_key != NULL)
|
|
|
|
|
free(conn->app_key);
|
2014-09-19 23:13:29 +02:00
|
|
|
free(conn->secret_time);
|
|
|
|
|
free(conn->session_token);
|
2014-12-29 15:11:53 +01:00
|
|
|
free(conn->ekey);
|
2014-09-19 23:13:29 +02:00
|
|
|
free(conn);
|
2014-09-18 09:11:00 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
void mfconn_update_secret_key(mfconn * conn)
|
2014-09-18 09:11:00 +02:00
|
|
|
{
|
2014-09-20 10:59:54 +02:00
|
|
|
uint64_t new_val;
|
2014-09-18 09:11:00 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
if (conn == NULL)
|
|
|
|
|
return;
|
2014-09-18 09:11:00 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
new_val = ((uint64_t) conn->secret_key) * 16807;
|
2014-09-18 09:11:00 +02:00
|
|
|
new_val %= 0x7FFFFFFF;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-19 23:13:29 +02:00
|
|
|
conn->secret_key = new_val;
|
2014-09-18 09:11:00 +02:00
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-21 15:59:52 +02:00
|
|
|
const char *mfconn_create_user_signature(mfconn * conn,
|
|
|
|
|
const char *username,
|
|
|
|
|
const char *password, int app_id,
|
|
|
|
|
const char *app_key)
|
2014-09-15 20:00:05 +02:00
|
|
|
{
|
2014-09-20 10:59:54 +02:00
|
|
|
char *signature_raw;
|
|
|
|
|
unsigned char signature_enc[20]; // sha1 is 160 bits
|
2014-09-19 09:21:28 +02:00
|
|
|
char signature_hex[41];
|
2014-09-15 20:00:05 +02:00
|
|
|
int i;
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
if (conn == NULL)
|
|
|
|
|
return NULL;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-23 18:53:16 +02:00
|
|
|
if (app_key == NULL) {
|
2014-09-24 11:28:35 +02:00
|
|
|
signature_raw = strdup_printf("%s%s%d", username, password, app_id);
|
2014-09-23 18:53:16 +02:00
|
|
|
} else {
|
|
|
|
|
signature_raw = strdup_printf("%s%s%d%s",
|
|
|
|
|
username, password, app_id, app_key);
|
|
|
|
|
}
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
SHA1((const unsigned char *)signature_raw,
|
2014-09-20 10:59:54 +02:00
|
|
|
strlen(signature_raw), signature_enc);
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
free(signature_raw);
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
for (i = 0; i < 20; i++) {
|
|
|
|
|
sprintf(&signature_hex[i * 2], "%02x", signature_enc[i]);
|
2014-09-15 20:00:05 +02:00
|
|
|
}
|
|
|
|
|
signature_hex[40] = '\0';
|
|
|
|
|
|
|
|
|
|
return strdup((const char *)signature_hex);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-21 15:59:52 +02:00
|
|
|
const char *mfconn_create_call_signature(mfconn * conn, const char *url,
|
|
|
|
|
const char *args)
|
2014-09-15 20:00:05 +02:00
|
|
|
{
|
2014-09-20 10:59:54 +02:00
|
|
|
char *signature_raw;
|
|
|
|
|
unsigned char signature_enc[16]; // md5 is 128 bits
|
2014-09-19 09:21:28 +02:00
|
|
|
char signature_hex[33];
|
2014-09-20 10:59:54 +02:00
|
|
|
char *api;
|
2014-09-15 20:00:05 +02:00
|
|
|
int i;
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
if (conn == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
if (url == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
if (args == NULL)
|
|
|
|
|
return NULL;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
// printf("url: %s\n\rargs: %s\n\r",url,args);
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
api = strstr(url, "/api/");
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
if (api == NULL)
|
|
|
|
|
return NULL;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
signature_raw = strdup_printf("%d%s%s%s",
|
2014-09-20 10:59:54 +02:00
|
|
|
(conn->secret_key % 256),
|
|
|
|
|
conn->secret_time, api, args);
|
2014-10-19 08:58:56 +02:00
|
|
|
if (signature_raw == NULL) {
|
|
|
|
|
fprintf(stderr, "strdup_printf failed\n");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
MD5((const unsigned char *)signature_raw,
|
2014-09-20 10:59:54 +02:00
|
|
|
strlen(signature_raw), signature_enc);
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
free(signature_raw);
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
|
sprintf(&signature_hex[i * 2], "%02x", signature_enc[i]);
|
2014-09-15 20:00:05 +02:00
|
|
|
}
|
|
|
|
|
signature_hex[32] = '\0';
|
|
|
|
|
|
|
|
|
|
return strdup((const char *)signature_hex);
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-04 10:34:54 +01:00
|
|
|
const char *mfconn_create_unsigned_get(mfconn * conn, int ssl,
|
|
|
|
|
const char *api, const char *fmt,
|
|
|
|
|
...)
|
|
|
|
|
{
|
|
|
|
|
char *api_request = NULL;
|
|
|
|
|
char *api_args = NULL;
|
|
|
|
|
int bytes_to_alloc;
|
|
|
|
|
int api_args_len;
|
|
|
|
|
int api_len;
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
2014-12-19 07:57:01 +01:00
|
|
|
if (conn == NULL) {
|
|
|
|
|
fprintf(stderr, "conn cannot be NULL\n");
|
2014-12-04 10:34:54 +01:00
|
|
|
return NULL;
|
2014-12-19 07:57:01 +01:00
|
|
|
}
|
|
|
|
|
if (conn->server == NULL) {
|
|
|
|
|
fprintf(stderr, "server cannot be NULL\n");
|
2014-12-04 10:34:54 +01:00
|
|
|
return NULL;
|
2014-12-19 07:57:01 +01:00
|
|
|
}
|
2014-12-04 10:34:54 +01:00
|
|
|
// make sure the api (ex: user/get_info.php) is sane
|
2014-12-19 07:57:01 +01:00
|
|
|
if (api == NULL) {
|
|
|
|
|
fprintf(stderr, "api call cannot be NULL\n");
|
2014-12-04 10:34:54 +01:00
|
|
|
return NULL;
|
2014-12-19 07:57:01 +01:00
|
|
|
}
|
2014-12-04 10:34:54 +01:00
|
|
|
api_len = strlen(api);
|
2014-12-19 07:57:01 +01:00
|
|
|
if (api_len < 3) {
|
|
|
|
|
fprintf(stderr, "api call length cannot be less than 3\n");
|
2014-12-04 10:34:54 +01:00
|
|
|
return NULL;
|
2014-12-19 07:57:01 +01:00
|
|
|
}
|
2014-12-04 10:34:54 +01:00
|
|
|
// calculate how big of a buffer we need
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
api_args_len = (vsnprintf(NULL, 0, fmt, ap) + 1); // + 1 for NULL
|
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
|
|
// create the correctly sized buffer and process the args
|
|
|
|
|
api_args = (char *)calloc(api_args_len, sizeof(char));
|
|
|
|
|
|
|
|
|
|
// printf("\n\r%d\n\r",api_args_len);
|
|
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
vsnprintf(api_args, api_args_len, fmt, ap);
|
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
|
|
// correct user error of trailing slash
|
2014-12-19 07:57:01 +01:00
|
|
|
if (api[api_len - 1] == '/') {
|
|
|
|
|
fprintf(stderr, "api call must not end with slash\n");
|
2014-12-04 10:34:54 +01:00
|
|
|
return NULL;
|
2014-12-19 07:57:01 +01:00
|
|
|
}
|
2014-12-04 10:34:54 +01:00
|
|
|
|
2014-12-26 20:28:23 -06:00
|
|
|
api_request = strdup_printf("%s//%s/api/%s/%s",
|
|
|
|
|
(ssl ? "https:" : "http:"),
|
2014-12-28 09:40:52 +01:00
|
|
|
conn->server, MFAPI_VERSION, api);
|
2014-12-04 10:34:54 +01:00
|
|
|
|
|
|
|
|
// compute the amount of space requred to realloc() the request
|
|
|
|
|
bytes_to_alloc = api_args_len;
|
|
|
|
|
bytes_to_alloc += strlen(api_request);
|
|
|
|
|
bytes_to_alloc += 1; // null termination
|
|
|
|
|
|
|
|
|
|
// append api GET args to api request
|
|
|
|
|
api_request = (char *)realloc(api_request, bytes_to_alloc);
|
|
|
|
|
if (api_request == NULL) {
|
|
|
|
|
fprintf(stderr, "cannot allocate memory\n");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strncat(api_request, api_args, api_args_len);
|
|
|
|
|
|
|
|
|
|
free(api_args);
|
|
|
|
|
|
|
|
|
|
return api_request;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-21 15:59:52 +02:00
|
|
|
const char *mfconn_create_signed_get(mfconn * conn, int ssl,
|
2014-09-24 11:28:35 +02:00
|
|
|
const char *api, const char *fmt, ...)
|
2014-09-15 20:00:05 +02:00
|
|
|
{
|
2014-09-20 10:59:54 +02:00
|
|
|
char *api_request = NULL;
|
|
|
|
|
char *api_args = NULL;
|
|
|
|
|
char *signature;
|
2014-09-21 15:59:52 +02:00
|
|
|
const char *call_hash;
|
2014-09-20 10:59:54 +02:00
|
|
|
char *session_token;
|
|
|
|
|
int bytes_to_alloc;
|
|
|
|
|
int api_args_len;
|
|
|
|
|
int api_len;
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
2014-12-19 07:57:01 +01:00
|
|
|
if (conn == NULL) {
|
|
|
|
|
fprintf(stderr, "conn cannot be NULL\n");
|
2014-09-20 10:59:54 +02:00
|
|
|
return NULL;
|
2014-12-19 07:57:01 +01:00
|
|
|
}
|
|
|
|
|
if (conn->server == NULL) {
|
|
|
|
|
fprintf(stderr, "server cannot be NULL\n");
|
2014-09-20 10:59:54 +02:00
|
|
|
return NULL;
|
2014-12-19 07:57:01 +01:00
|
|
|
}
|
|
|
|
|
if (conn->secret_time == NULL) {
|
|
|
|
|
fprintf(stderr, "secret_time cannot be NULL\n");
|
2014-09-20 10:59:54 +02:00
|
|
|
return NULL;
|
2014-12-19 07:57:01 +01:00
|
|
|
}
|
|
|
|
|
if (conn->session_token == NULL) {
|
|
|
|
|
fprintf(stderr, "session_token cannot be NULL\n");
|
2014-09-20 10:59:54 +02:00
|
|
|
return NULL;
|
2014-12-19 07:57:01 +01:00
|
|
|
}
|
2014-09-15 20:00:05 +02:00
|
|
|
// make sure the api (ex: user/get_info.php) is sane
|
2014-12-19 07:57:01 +01:00
|
|
|
if (api == NULL) {
|
|
|
|
|
fprintf(stderr, "api name cannot be NULL\n");
|
2014-09-20 10:59:54 +02:00
|
|
|
return NULL;
|
2014-12-19 07:57:01 +01:00
|
|
|
}
|
2014-09-15 20:00:05 +02:00
|
|
|
api_len = strlen(api);
|
2014-12-19 07:57:01 +01:00
|
|
|
if (api_len < 3) {
|
|
|
|
|
fprintf(stderr, "api name length cannot be less than 3\n");
|
2014-09-20 10:59:54 +02:00
|
|
|
return NULL;
|
2014-12-19 07:57:01 +01:00
|
|
|
}
|
2014-09-15 20:00:05 +02:00
|
|
|
// calculate how big of a buffer we need
|
|
|
|
|
va_start(ap, fmt);
|
2014-09-20 10:59:54 +02:00
|
|
|
api_args_len = (vsnprintf(NULL, 0, fmt, ap) + 1); // + 1 for NULL
|
2014-09-15 20:00:05 +02:00
|
|
|
va_end(ap);
|
|
|
|
|
|
2014-09-19 23:13:29 +02:00
|
|
|
session_token = strdup_printf("&session_token=%s", conn->session_token);
|
2014-09-18 09:11:00 +02:00
|
|
|
|
|
|
|
|
api_args_len += strlen(session_token);
|
|
|
|
|
|
2014-09-15 20:00:05 +02:00
|
|
|
// create the correctly sized buffer and process the args
|
2014-09-20 10:59:54 +02:00
|
|
|
api_args = (char *)calloc(api_args_len, sizeof(char));
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
// printf("\n\r%d\n\r",api_args_len);
|
|
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
vsnprintf(api_args, api_args_len, fmt, ap);
|
|
|
|
|
va_end(ap);
|
|
|
|
|
|
2014-09-18 09:11:00 +02:00
|
|
|
strcat(api_args, session_token);
|
|
|
|
|
free(session_token);
|
|
|
|
|
|
2014-09-15 20:00:05 +02:00
|
|
|
// correct user error of trailing slash
|
2014-12-19 07:57:01 +01:00
|
|
|
if (api[api_len - 1] == '/') {
|
|
|
|
|
fprintf(stderr, "api name cannot end with slash\n");
|
2014-09-21 15:59:52 +02:00
|
|
|
return NULL;
|
2014-12-19 07:57:01 +01:00
|
|
|
}
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-12-26 20:28:23 -06:00
|
|
|
api_request = strdup_printf("%s//%s/api/%s/%s",
|
|
|
|
|
(ssl ? "https:" : "http:"),
|
2014-12-28 09:40:52 +01:00
|
|
|
conn->server, MFAPI_VERSION, api);
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
call_hash = mfconn_create_call_signature(conn, api_request, api_args);
|
|
|
|
|
signature = strdup_printf("&signature=%s", call_hash);
|
2014-09-21 15:59:52 +02:00
|
|
|
free((void *)call_hash);
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
// compute the amount of space requred to realloc() the request
|
|
|
|
|
bytes_to_alloc = api_args_len;
|
|
|
|
|
bytes_to_alloc += strlen(api_request);
|
|
|
|
|
bytes_to_alloc += strlen(signature);
|
2014-09-20 10:59:54 +02:00
|
|
|
bytes_to_alloc += 1; // null termination
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
// append api GET args to api request
|
2014-09-20 10:59:54 +02:00
|
|
|
api_request = (char *)realloc(api_request, bytes_to_alloc);
|
2014-10-19 08:58:56 +02:00
|
|
|
if (api_request == NULL) {
|
|
|
|
|
fprintf(stderr, "cannot allocate memory\n");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
strncat(api_request, api_args, api_args_len);
|
|
|
|
|
strcat(api_request, signature);
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
free(signature);
|
|
|
|
|
free(api_args);
|
|
|
|
|
|
|
|
|
|
return api_request;
|
|
|
|
|
}
|
2014-09-18 09:11:00 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
const char *mfconn_get_session_token(mfconn * conn)
|
2014-09-18 09:11:00 +02:00
|
|
|
{
|
2014-09-19 23:13:29 +02:00
|
|
|
return conn->session_token;
|
2014-09-18 09:11:00 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
const char *mfconn_get_secret_time(mfconn * conn)
|
2014-09-18 09:11:00 +02:00
|
|
|
{
|
2014-09-19 23:13:29 +02:00
|
|
|
return conn->secret_time;
|
2014-09-18 09:11:00 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
uint32_t mfconn_get_secret_key(mfconn * conn)
|
2014-09-18 09:11:00 +02:00
|
|
|
{
|
2014-09-19 23:13:29 +02:00
|
|
|
return conn->secret_key;
|
2014-09-18 09:11:00 +02:00
|
|
|
}
|
2014-12-06 11:04:04 +01:00
|
|
|
|
|
|
|
|
int mfconn_get_max_num_retries(mfconn * conn)
|
|
|
|
|
{
|
|
|
|
|
return conn->max_num_retries;
|
|
|
|
|
}
|
2014-12-29 13:57:53 +01:00
|
|
|
|
2014-12-29 15:11:53 +01:00
|
|
|
const char *mfconn_get_ekey(mfconn * conn)
|
|
|
|
|
{
|
|
|
|
|
return conn->ekey;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-29 13:57:53 +01:00
|
|
|
int mfconn_upload_poll_for_completion(mfconn * conn, const char *upload_key)
|
|
|
|
|
{
|
|
|
|
|
int status;
|
|
|
|
|
int fileerror;
|
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
// no need to update the secret key after this
|
|
|
|
|
retval = mfconn_api_upload_poll_upload(conn, upload_key, &status,
|
|
|
|
|
&fileerror);
|
|
|
|
|
if (retval != 0) {
|
|
|
|
|
fprintf(stderr, "mfconn_api_upload_poll_upload failed\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
fprintf(stderr, "status: %d, filerror: %d\n", status, fileerror);
|
|
|
|
|
|
|
|
|
|
// values 98 and 99 are terminal states for a completed upload
|
|
|
|
|
if (status == 99 || status == 98) {
|
|
|
|
|
fprintf(stderr, "done\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
sleep(1);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|