From 3c7888d7523f876740084c7315e31f973920212b Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sat, 14 Feb 2015 23:00:12 -0500 Subject: A bit of new api work done. --- toxcore/tox.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 93 insertions(+), 4 deletions(-) (limited to 'toxcore/tox.c') diff --git a/toxcore/tox.c b/toxcore/tox.c index 124d7119..b509fc3b 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -20,7 +20,11 @@ * along with Tox. If not, see . * */ - +/* +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); } -- cgit v1.2.3