A bit of new api work done.

pull/1257/head
irungentoo 2015-02-14 23:00:12 -05:00
parent eac0d435f3
commit 3c7888d752
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
5 changed files with 107 additions and 11 deletions

View File

@ -20,7 +20,11 @@
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
TODO:
-replace bool with uint8_t
-remove enums (typedef enum in api to uint8_t)
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
@ -29,6 +33,8 @@
#include "group.h"
#include "logger.h"
#include "../toxencryptsave/defines.h"
#define TOX_DEFINED
typedef struct Messenger Tox;
@ -62,6 +68,9 @@ void tox_options_default(struct Tox_Options *options)
{
if (options) {
memset(options, 0, sizeof(struct Tox_Options));
options->ipv6_enabled = 1;
options->udp_enabled = 1;
options->proxy_type = TOX_PROXY_TYPE_NONE;
}
}
@ -85,22 +94,102 @@ void tox_options_free(struct Tox_Options *options)
Tox *tox_new(struct Tox_Options const *options, uint8_t const *data, size_t length, TOX_ERR_NEW *error)
{
if (!logger_get_global())
logger_set_global(logger_new(LOGGER_OUTPUT_FILE, LOGGER_LEVEL, "toxcore"));
if (data) {
if (memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_ENCRYPTED);
return NULL;
}
}
Messenger_Options m_options = {0};
if (options == NULL) {
m_options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT;
} else {
m_options.ipv6enabled = options->ipv6_enabled;
m_options.udp_disabled = !options->udp_enabled;
switch (options->proxy_type) {
case TOX_PROXY_TYPE_HTTP:
m_options.proxy_info.proxy_type = TCP_PROXY_HTTP;
break;
case TOX_PROXY_TYPE_SOCKS5:
m_options.proxy_info.proxy_type = TCP_PROXY_SOCKS5;
break;
case TOX_PROXY_TYPE_NONE:
m_options.proxy_info.proxy_type = TCP_PROXY_NONE;
break;
default:
SET_ERROR_PARAMETER(error, TOX_ERR_PROXY_TYPE);
return NULL;
}
if (m_options.proxy_info.proxy_type != TCP_PROXY_NONE) {
if (options->proxy_port == 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_PORT);
return NULL;
}
ip_init(&m_options.proxy_info.ip_port.ip, m_options.ipv6enabled);
if (m_options.ipv6enabled)
m_options.proxy_info.ip_port.ip.family = AF_UNSPEC;
if (!addr_resolve_or_parse_ip(options->proxy_address, &m_options.proxy_info.ip_port.ip, NULL)) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_HOST);
//TODO: TOX_ERR_NEW_PROXY_NOT_FOUND if domain.
return NULL;
}
m_options.proxy_info.ip_port.port = htons(options->proxy_port);
}
}
Messenger *m = new_messenger(&m_options);
//TODO: TOX_ERR_NEW_MALLOC
//TODO: TOX_ERR_NEW_PORT_ALLOC
if (!new_groupchats(m)) {
kill_messenger(m);
return NULL;
}
if (messenger_load(m, data, length) == -1) {
/* TODO: uncomment this when tox is stable.
tox_kill(m);
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT);
return NULL;
*/
}
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK);
return m;
}
void tox_kill(Tox *tox)
{
Messenger *m = tox;
kill_groupchats(m->group_chat_object);
kill_messenger(m);
logger_kill_global();
}
size_t tox_save_size(Tox const *tox)
{
const Messenger *m = tox;
return messenger_size(m);
}
void tox_save(Tox const *tox, uint8_t *data)
{
const Messenger *m = tox;
messenger_save(m, data);
}

View File

@ -418,12 +418,16 @@ typedef enum TOX_ERR_NEW {
*/
TOX_ERR_NEW_PORT_ALLOC,
/**
* proxy_enabled was true, but the proxy_address passed had an invalid format
* proxy_type was invalid.
*/
TOX_ERR_PROXY_TYPE,
/**
* proxy_type was valid but the proxy_address passed had an invalid format
* or was NULL.
*/
TOX_ERR_NEW_PROXY_BAD_HOST,
/**
* proxy_enabled was true, but the proxy_port was invalid.
* proxy_type was valid, but the proxy_port was invalid.
*/
TOX_ERR_NEW_PROXY_BAD_PORT,
/**

2
toxencryptsave/defines.h Normal file
View File

@ -0,0 +1,2 @@
#define TOX_ENC_SAVE_MAGIC_NUMBER "toxEsave"
#define TOX_ENC_SAVE_MAGIC_LENGTH 8

View File

@ -26,6 +26,7 @@
#endif
#include "toxencryptsave.h"
#include "defines.h"
#include "../toxcore/crypto_core.h"
#include "../toxcore/tox.h"
@ -64,7 +65,7 @@ int tox_pass_salt_length()
/* return size of the messenger data (for encrypted saving). */
uint32_t tox_encrypted_size(const Tox *tox)
{
return tox_size(tox) + TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
return tox_save_size(tox) + TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
}
/* This retrieves the salt used to encrypt the given data, which can then be passed to
@ -203,7 +204,7 @@ int tox_pass_encrypt(const uint8_t *data, uint32_t data_len, uint8_t *passphrase
int tox_encrypted_save(const Tox *tox, uint8_t *data, uint8_t *passphrase, uint32_t pplength)
{
/* first get plain save data */
uint32_t temp_size = tox_size(tox);
uint32_t temp_size = tox_save_size(tox);
uint8_t temp_data[temp_size];
tox_save(tox, temp_data);
@ -220,7 +221,7 @@ int tox_encrypted_save(const Tox *tox, uint8_t *data, uint8_t *passphrase, uint3
int tox_encrypted_key_save(const Tox *tox, uint8_t *data, uint8_t *key)
{
/* first get plain save data */
uint32_t temp_size = tox_size(tox);
uint32_t temp_size = tox_save_size(tox);
uint8_t temp_data[temp_size];
tox_save(tox, temp_data);

View File

@ -30,8 +30,8 @@ extern "C" {
#include <stdint.h>
#ifndef __TOX_DEFINED__
#define __TOX_DEFINED__
#ifndef TOX_DEFINED
#define TOX_DEFINED
typedef struct Tox Tox;
#endif