summaryrefslogtreecommitdiff
path: root/toxcore/tox.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2015-02-14 23:00:12 -0500
committerirungentoo <irungentoo@gmail.com>2015-02-14 23:00:12 -0500
commit3c7888d7523f876740084c7315e31f973920212b (patch)
treeb00246db826e52ae20ba5542fe96b6dd56512a18 /toxcore/tox.c
parenteac0d435f35db468435fa596a3b00b593d6bf3e9 (diff)
A bit of new api work done.
Diffstat (limited to 'toxcore/tox.c')
-rw-r--r--toxcore/tox.c97
1 files changed, 93 insertions, 4 deletions
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 @@
20 * along with Tox. If not, see <http://www.gnu.org/licenses/>. 20 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
21 * 21 *
22 */ 22 */
23 23/*
24TODO:
25-replace bool with uint8_t
26-remove enums (typedef enum in api to uint8_t)
27*/
24#ifdef HAVE_CONFIG_H 28#ifdef HAVE_CONFIG_H
25#include "config.h" 29#include "config.h"
26#endif 30#endif
@@ -29,6 +33,8 @@
29#include "group.h" 33#include "group.h"
30#include "logger.h" 34#include "logger.h"
31 35
36#include "../toxencryptsave/defines.h"
37
32#define TOX_DEFINED 38#define TOX_DEFINED
33typedef struct Messenger Tox; 39typedef struct Messenger Tox;
34 40
@@ -62,6 +68,9 @@ void tox_options_default(struct Tox_Options *options)
62{ 68{
63 if (options) { 69 if (options) {
64 memset(options, 0, sizeof(struct Tox_Options)); 70 memset(options, 0, sizeof(struct Tox_Options));
71 options->ipv6_enabled = 1;
72 options->udp_enabled = 1;
73 options->proxy_type = TOX_PROXY_TYPE_NONE;
65 } 74 }
66} 75}
67 76
@@ -85,22 +94,102 @@ void tox_options_free(struct Tox_Options *options)
85 94
86Tox *tox_new(struct Tox_Options const *options, uint8_t const *data, size_t length, TOX_ERR_NEW *error) 95Tox *tox_new(struct Tox_Options const *options, uint8_t const *data, size_t length, TOX_ERR_NEW *error)
87{ 96{
97 if (!logger_get_global())
98 logger_set_global(logger_new(LOGGER_OUTPUT_FILE, LOGGER_LEVEL, "toxcore"));
99
100 if (data) {
101 if (memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0) {
102 SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_ENCRYPTED);
103 return NULL;
104 }
105 }
106
107 Messenger_Options m_options = {0};
108
109 if (options == NULL) {
110 m_options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT;
111 } else {
112 m_options.ipv6enabled = options->ipv6_enabled;
113 m_options.udp_disabled = !options->udp_enabled;
114
115 switch (options->proxy_type) {
116 case TOX_PROXY_TYPE_HTTP:
117 m_options.proxy_info.proxy_type = TCP_PROXY_HTTP;
118 break;
119
120 case TOX_PROXY_TYPE_SOCKS5:
121 m_options.proxy_info.proxy_type = TCP_PROXY_SOCKS5;
122 break;
123
124 case TOX_PROXY_TYPE_NONE:
125 m_options.proxy_info.proxy_type = TCP_PROXY_NONE;
126 break;
127
128 default:
129 SET_ERROR_PARAMETER(error, TOX_ERR_PROXY_TYPE);
130 return NULL;
131 }
132
133 if (m_options.proxy_info.proxy_type != TCP_PROXY_NONE) {
134 if (options->proxy_port == 0) {
135 SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_PORT);
136 return NULL;
137 }
138
139 ip_init(&m_options.proxy_info.ip_port.ip, m_options.ipv6enabled);
140
141 if (m_options.ipv6enabled)
142 m_options.proxy_info.ip_port.ip.family = AF_UNSPEC;
88 143
144 if (!addr_resolve_or_parse_ip(options->proxy_address, &m_options.proxy_info.ip_port.ip, NULL)) {
145 SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_HOST);
146 //TODO: TOX_ERR_NEW_PROXY_NOT_FOUND if domain.
147 return NULL;
148 }
149
150 m_options.proxy_info.ip_port.port = htons(options->proxy_port);
151 }
152 }
153
154 Messenger *m = new_messenger(&m_options);
155 //TODO: TOX_ERR_NEW_MALLOC
156 //TODO: TOX_ERR_NEW_PORT_ALLOC
157
158 if (!new_groupchats(m)) {
159 kill_messenger(m);
160 return NULL;
161 }
162
163 if (messenger_load(m, data, length) == -1) {
164 /* TODO: uncomment this when tox is stable.
165 tox_kill(m);
166 SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT);
167 return NULL;
168 */
169 }
170
171 SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK);
172 return m;
89} 173}
90 174
91void tox_kill(Tox *tox) 175void tox_kill(Tox *tox)
92{ 176{
93 177 Messenger *m = tox;
178 kill_groupchats(m->group_chat_object);
179 kill_messenger(m);
180 logger_kill_global();
94} 181}
95 182
96 183
97size_t tox_save_size(Tox const *tox) 184size_t tox_save_size(Tox const *tox)
98{ 185{
99 186 const Messenger *m = tox;
187 return messenger_size(m);
100} 188}
101 189
102 190
103void tox_save(Tox const *tox, uint8_t *data) 191void tox_save(Tox const *tox, uint8_t *data)
104{ 192{
105 193 const Messenger *m = tox;
194 messenger_save(m, data);
106} 195}