diff options
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/tox.c | 97 | ||||
-rw-r--r-- | toxcore/tox.h | 8 |
2 files changed, 99 insertions, 6 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 | /* | |
24 | TODO: | ||
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 |
33 | typedef struct Messenger Tox; | 39 | typedef 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 | ||
86 | Tox *tox_new(struct Tox_Options const *options, uint8_t const *data, size_t length, TOX_ERR_NEW *error) | 95 | Tox *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 | ||
91 | void tox_kill(Tox *tox) | 175 | void 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 | ||
97 | size_t tox_save_size(Tox const *tox) | 184 | size_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 | ||
103 | void tox_save(Tox const *tox, uint8_t *data) | 191 | void tox_save(Tox const *tox, uint8_t *data) |
104 | { | 192 | { |
105 | 193 | const Messenger *m = tox; | |
194 | messenger_save(m, data); | ||
106 | } | 195 | } |
diff --git a/toxcore/tox.h b/toxcore/tox.h index 8dab7002..61f40539 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h | |||
@@ -418,12 +418,16 @@ typedef enum TOX_ERR_NEW { | |||
418 | */ | 418 | */ |
419 | TOX_ERR_NEW_PORT_ALLOC, | 419 | TOX_ERR_NEW_PORT_ALLOC, |
420 | /** | 420 | /** |
421 | * proxy_enabled was true, but the proxy_address passed had an invalid format | 421 | * proxy_type was invalid. |
422 | */ | ||
423 | TOX_ERR_PROXY_TYPE, | ||
424 | /** | ||
425 | * proxy_type was valid but the proxy_address passed had an invalid format | ||
422 | * or was NULL. | 426 | * or was NULL. |
423 | */ | 427 | */ |
424 | TOX_ERR_NEW_PROXY_BAD_HOST, | 428 | TOX_ERR_NEW_PROXY_BAD_HOST, |
425 | /** | 429 | /** |
426 | * proxy_enabled was true, but the proxy_port was invalid. | 430 | * proxy_type was valid, but the proxy_port was invalid. |
427 | */ | 431 | */ |
428 | TOX_ERR_NEW_PROXY_BAD_PORT, | 432 | TOX_ERR_NEW_PROXY_BAD_PORT, |
429 | /** | 433 | /** |