diff options
-rw-r--r-- | core/CMakeLists.txt | 3 | ||||
-rw-r--r-- | core/Messenger.h | 31 | ||||
-rw-r--r-- | core/network.c | 43 | ||||
-rw-r--r-- | core/network.h | 10 | ||||
-rw-r--r-- | core/tox.c | 374 | ||||
-rw-r--r-- | core/tox.h | 289 | ||||
-rw-r--r-- | other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c | 41 | ||||
-rw-r--r-- | testing/nTox.c | 100 | ||||
-rw-r--r-- | testing/nTox.h | 46 |
9 files changed, 819 insertions, 118 deletions
diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 02a42849..49ca7c83 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt | |||
@@ -10,7 +10,8 @@ set(core_sources | |||
10 | LAN_discovery.c | 10 | LAN_discovery.c |
11 | Messenger.c | 11 | Messenger.c |
12 | util.c | 12 | util.c |
13 | ping.c) | 13 | ping.c |
14 | tox.c) | ||
14 | 15 | ||
15 | set(core_headers | 16 | set(core_headers |
16 | DHT.h | 17 | DHT.h |
diff --git a/core/Messenger.h b/core/Messenger.h index 581c4ba9..e808529f 100644 --- a/core/Messenger.h +++ b/core/Messenger.h | |||
@@ -48,23 +48,28 @@ extern "C" { | |||
48 | #define PACKET_ID_MESSAGE 64 | 48 | #define PACKET_ID_MESSAGE 64 |
49 | #define PACKET_ID_ACTION 63 | 49 | #define PACKET_ID_ACTION 63 |
50 | 50 | ||
51 | |||
51 | /* status definitions */ | 52 | /* status definitions */ |
52 | #define FRIEND_ONLINE 4 | 53 | enum { |
53 | #define FRIEND_CONFIRMED 3 | 54 | NOFRIEND, |
54 | #define FRIEND_REQUESTED 2 | 55 | FRIEND_ADDED, |
55 | #define FRIEND_ADDED 1 | 56 | FRIEND_REQUESTED, |
56 | #define NOFRIEND 0 | 57 | FRIEND_CONFIRMED, |
58 | FRIEND_ONLINE, | ||
59 | }; | ||
57 | 60 | ||
58 | /* errors for m_addfriend | 61 | /* errors for m_addfriend |
59 | * FAERR - Friend Add Error */ | 62 | * FAERR - Friend Add Error */ |
60 | #define FAERR_TOOLONG -1 | 63 | enum { |
61 | #define FAERR_NOMESSAGE -2 | 64 | FAERR_TOOLONG = -1, |
62 | #define FAERR_OWNKEY -3 | 65 | FAERR_NOMESSAGE = -2, |
63 | #define FAERR_ALREADYSENT -4 | 66 | FAERR_OWNKEY = -3, |
64 | #define FAERR_UNKNOWN -5 | 67 | FAERR_ALREADYSENT = -4, |
65 | #define FAERR_BADCHECKSUM -6 | 68 | FAERR_UNKNOWN = -5, |
66 | #define FAERR_SETNEWNOSPAM -7 | 69 | FAERR_BADCHECKSUM = -6, |
67 | #define FAERR_NOMEM -8 | 70 | FAERR_SETNEWNOSPAM = -7, |
71 | FAERR_NOMEM = -8 | ||
72 | }; | ||
68 | 73 | ||
69 | /* don't assume MAX_STATUSMESSAGE_LENGTH will stay at 128, it may be increased | 74 | /* don't assume MAX_STATUSMESSAGE_LENGTH will stay at 128, it may be increased |
70 | to an absurdly large number later */ | 75 | to an absurdly large number later */ |
diff --git a/core/network.c b/core/network.c index 7880eae6..2bcf7d61 100644 --- a/core/network.c +++ b/core/network.c | |||
@@ -144,8 +144,9 @@ static void at_shutdown(void) | |||
144 | returns NULL if there are problems */ | 144 | returns NULL if there are problems */ |
145 | Networking_Core *new_networking(IP ip, uint16_t port) | 145 | Networking_Core *new_networking(IP ip, uint16_t port) |
146 | { | 146 | { |
147 | if(at_startup() != 0) | 147 | if (at_startup() != 0) |
148 | return NULL; | 148 | return NULL; |
149 | |||
149 | /* initialize our socket */ | 150 | /* initialize our socket */ |
150 | Networking_Core *temp = calloc(1, sizeof(Networking_Core)); | 151 | Networking_Core *temp = calloc(1, sizeof(Networking_Core)); |
151 | 152 | ||
@@ -215,43 +216,3 @@ void kill_networking(Networking_Core *net) | |||
215 | free(net); | 216 | free(net); |
216 | return; | 217 | return; |
217 | } | 218 | } |
218 | |||
219 | /* | ||
220 | resolve_addr(): | ||
221 | address should represent IPv4 or a hostname with A record | ||
222 | |||
223 | returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i | ||
224 | returns 0 on failure | ||
225 | |||
226 | TODO: Fix ipv6 support | ||
227 | */ | ||
228 | uint32_t resolve_addr(const char *address) | ||
229 | { | ||
230 | struct addrinfo *server = NULL; | ||
231 | struct addrinfo hints; | ||
232 | int rc; | ||
233 | uint32_t addr; | ||
234 | |||
235 | memset(&hints, 0, sizeof(hints)); | ||
236 | hints.ai_family = AF_INET; // IPv4 only right now. | ||
237 | hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. | ||
238 | |||
239 | rc = getaddrinfo(address, "echo", &hints, &server); | ||
240 | |||
241 | // Lookup failed. | ||
242 | if (rc != 0) { | ||
243 | return 0; | ||
244 | } | ||
245 | |||
246 | // IPv4 records only.. | ||
247 | if (server->ai_family != AF_INET) { | ||
248 | freeaddrinfo(server); | ||
249 | return 0; | ||
250 | } | ||
251 | |||
252 | |||
253 | addr = ((struct sockaddr_in *)server->ai_addr)->sin_addr.s_addr; | ||
254 | |||
255 | freeaddrinfo(server); | ||
256 | return addr; | ||
257 | } | ||
diff --git a/core/network.h b/core/network.h index 088bbb3b..3547f79b 100644 --- a/core/network.h +++ b/core/network.h | |||
@@ -151,16 +151,6 @@ Networking_Core *new_networking(IP ip, uint16_t port); | |||
151 | /* function to cleanup networking stuff(doesn't do much right now) */ | 151 | /* function to cleanup networking stuff(doesn't do much right now) */ |
152 | void kill_networking(Networking_Core *net); | 152 | void kill_networking(Networking_Core *net); |
153 | 153 | ||
154 | /* | ||
155 | resolve_addr(): | ||
156 | address should represent IPv4 or a hostname with A record | ||
157 | |||
158 | returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i | ||
159 | returns 0 on failure | ||
160 | |||
161 | TODO: Fix ipv6 support | ||
162 | */ | ||
163 | uint32_t resolve_addr(const char *address); | ||
164 | 154 | ||
165 | #ifdef __cplusplus | 155 | #ifdef __cplusplus |
166 | } | 156 | } |
diff --git a/core/tox.c b/core/tox.c new file mode 100644 index 00000000..a97e52bc --- /dev/null +++ b/core/tox.c | |||
@@ -0,0 +1,374 @@ | |||
1 | /* tox.c | ||
2 | * | ||
3 | * The Tox public API. | ||
4 | * | ||
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
6 | * | ||
7 | * This file is part of Tox. | ||
8 | * | ||
9 | * Tox is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * Tox is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #include "Messenger.h" | ||
25 | /* | ||
26 | * returns a FRIEND_ADDRESS_SIZE byte address to give to others. | ||
27 | * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] | ||
28 | * | ||
29 | */ | ||
30 | void tox_getaddress(void *tox, uint8_t *address) | ||
31 | { | ||
32 | Messenger *m = tox; | ||
33 | getaddress(m, address); | ||
34 | } | ||
35 | |||
36 | /* | ||
37 | * add a friend | ||
38 | * set the data that will be sent along with friend request | ||
39 | * address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum. | ||
40 | * data is the data and length is the length | ||
41 | * returns the friend number if success | ||
42 | * return FA_TOOLONG if message length is too long | ||
43 | * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte) | ||
44 | * return FAERR_OWNKEY if user's own key | ||
45 | * return FAERR_ALREADYSENT if friend request already sent or already a friend | ||
46 | * return FAERR_UNKNOWN for unknown error | ||
47 | * return FAERR_BADCHECKSUM if bad checksum in address | ||
48 | * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different | ||
49 | * (the nospam for that friend was set to the new one) | ||
50 | * return FAERR_NOMEM if increasing the friend list size fails | ||
51 | */ | ||
52 | int tox_addfriend(void *tox, uint8_t *address, uint8_t *data, uint16_t length) | ||
53 | { | ||
54 | Messenger *m = tox; | ||
55 | return m_addfriend(m, address, data, length); | ||
56 | } | ||
57 | |||
58 | /* add a friend without sending a friendrequest. | ||
59 | returns the friend number if success | ||
60 | return -1 if failure. */ | ||
61 | int tox_addfriend_norequest(void *tox, uint8_t *client_id) | ||
62 | { | ||
63 | Messenger *m = tox; | ||
64 | return m_addfriend_norequest(m, client_id); | ||
65 | } | ||
66 | |||
67 | /* return the friend id associated to that client id. | ||
68 | return -1 if no such friend */ | ||
69 | int tox_getfriend_id(void *tox, uint8_t *client_id) | ||
70 | { | ||
71 | Messenger *m = tox; | ||
72 | return getfriend_id(m, client_id); | ||
73 | } | ||
74 | |||
75 | /* copies the public key associated to that friend id into client_id buffer. | ||
76 | make sure that client_id is of size CLIENT_ID_SIZE. | ||
77 | return 0 if success | ||
78 | return -1 if failure */ | ||
79 | int tox_getclient_id(void *tox, int friend_id, uint8_t *client_id) | ||
80 | { | ||
81 | Messenger *m = tox; | ||
82 | return getclient_id(m, friend_id, client_id); | ||
83 | } | ||
84 | |||
85 | /* remove a friend */ | ||
86 | int tox_delfriend(void *tox, int friendnumber) | ||
87 | { | ||
88 | Messenger *m = tox; | ||
89 | return m_delfriend(m, friendnumber); | ||
90 | } | ||
91 | |||
92 | /* return 4 if friend is online | ||
93 | return 3 if friend is confirmed | ||
94 | return 2 if the friend request was sent | ||
95 | return 1 if the friend was added | ||
96 | return 0 if there is no friend with that number */ | ||
97 | int tox_friendstatus(void *tox, int friendnumber) | ||
98 | { | ||
99 | Messenger *m = tox; | ||
100 | return m_friendstatus(m, friendnumber); | ||
101 | } | ||
102 | |||
103 | /* send a text chat message to an online friend | ||
104 | returns the message id if packet was successfully put into the send queue | ||
105 | return 0 if it was not | ||
106 | you will want to retain the return value, it will be passed to your read receipt callback | ||
107 | if one is received. | ||
108 | m_sendmessage_withid will send a message with the id of your choosing, | ||
109 | however we can generate an id for you by calling plain m_sendmessage. */ | ||
110 | uint32_t tox_sendmessage(void *tox, int friendnumber, uint8_t *message, uint32_t length) | ||
111 | { | ||
112 | Messenger *m = tox; | ||
113 | return m_sendmessage(m, friendnumber, message, length); | ||
114 | } | ||
115 | |||
116 | uint32_t tox_sendmessage_withid(void *tox, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length) | ||
117 | { | ||
118 | Messenger *m = tox; | ||
119 | return m_sendmessage_withid(m, friendnumber, theid, message, length); | ||
120 | } | ||
121 | |||
122 | /* send an action to an online friend | ||
123 | returns 1 if packet was successfully put into the send queue | ||
124 | return 0 if it was not */ | ||
125 | int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length) | ||
126 | { | ||
127 | Messenger *m = tox; | ||
128 | return m_sendaction(m, friendnumber, action, length); | ||
129 | } | ||
130 | |||
131 | /* Set our nickname | ||
132 | name must be a string of maximum MAX_NAME_LENGTH length. | ||
133 | length must be at least 1 byte | ||
134 | length is the length of name with the NULL terminator | ||
135 | return 0 if success | ||
136 | return -1 if failure */ | ||
137 | int tox_setname(void *tox, uint8_t *name, uint16_t length) | ||
138 | { | ||
139 | Messenger *m = tox; | ||
140 | return setname(m, name, length); | ||
141 | } | ||
142 | |||
143 | /* | ||
144 | Get your nickname. | ||
145 | m The messanger context to use. | ||
146 | name Pointer to a string for the name. | ||
147 | nlen The length of the string buffer. | ||
148 | returns Return the length of the name, 0 on error. | ||
149 | */ | ||
150 | uint16_t tox_getselfname(void *tox, uint8_t *name, uint16_t nlen) | ||
151 | { | ||
152 | Messenger *m = tox; | ||
153 | return getself_name(m, name, nlen); | ||
154 | } | ||
155 | |||
156 | /* get name of friendnumber | ||
157 | put it in name | ||
158 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. | ||
159 | return 0 if success | ||
160 | return -1 if failure */ | ||
161 | int tox_getname(void *tox, int friendnumber, uint8_t *name) | ||
162 | { | ||
163 | Messenger *m = tox; | ||
164 | return getname(m, friendnumber, name); | ||
165 | } | ||
166 | |||
167 | /* set our user status | ||
168 | you are responsible for freeing status after | ||
169 | returns 0 on success, -1 on failure */ | ||
170 | int tox_set_statusmessage(void *tox, uint8_t *status, uint16_t length) | ||
171 | { | ||
172 | Messenger *m = tox; | ||
173 | return m_set_statusmessage(m, status, length); | ||
174 | } | ||
175 | |||
176 | int tox_set_userstatus(void *tox, USERSTATUS status) | ||
177 | { | ||
178 | Messenger *m = tox; | ||
179 | return m_set_userstatus(m, status); | ||
180 | } | ||
181 | |||
182 | /* return the length of friendnumber's status message, | ||
183 | including null | ||
184 | pass it into malloc */ | ||
185 | int tox_get_statusmessage_size(void *tox, int friendnumber) | ||
186 | { | ||
187 | Messenger *m = tox; | ||
188 | return m_get_statusmessage_size(m, friendnumber); | ||
189 | } | ||
190 | |||
191 | /* copy friendnumber's status message into buf, truncating if size is over maxlen | ||
192 | get the size you need to allocate from m_get_statusmessage_size | ||
193 | The self variant will copy our own status message. */ | ||
194 | int tox_copy_statusmessage(void *tox, int friendnumber, uint8_t *buf, uint32_t maxlen) | ||
195 | { | ||
196 | Messenger *m = tox; | ||
197 | return m_copy_statusmessage(m, friendnumber, buf, maxlen); | ||
198 | } | ||
199 | |||
200 | int tox_copy_self_statusmessage(void *tox, uint8_t *buf, uint32_t maxlen) | ||
201 | { | ||
202 | Messenger *m = tox; | ||
203 | return m_copy_self_statusmessage(m, buf, maxlen); | ||
204 | } | ||
205 | |||
206 | /* Return one of USERSTATUS values. | ||
207 | * Values unknown to your application should be represented as USERSTATUS_NONE. | ||
208 | * As above, the self variant will return our own USERSTATUS. | ||
209 | * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ | ||
210 | USERSTATUS tox_get_userstatus(void *tox, int friendnumber) | ||
211 | { | ||
212 | Messenger *m = tox; | ||
213 | return m_get_userstatus(m, friendnumber); | ||
214 | } | ||
215 | |||
216 | USERSTATUS tox_get_selfuserstatus(void *tox) | ||
217 | { | ||
218 | Messenger *m = tox; | ||
219 | return m_get_self_userstatus(m); | ||
220 | } | ||
221 | |||
222 | |||
223 | /* Sets whether we send read receipts for friendnumber. | ||
224 | * This function is not lazy, and it will fail if yesno is not (0 or 1).*/ | ||
225 | void tox_set_sends_receipts(void *tox, int friendnumber, int yesno) | ||
226 | { | ||
227 | Messenger *m = tox; | ||
228 | m_set_sends_receipts(m, friendnumber, yesno); | ||
229 | } | ||
230 | |||
231 | |||
232 | /* set the function that will be executed when a friend request is received. | ||
233 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ | ||
234 | void tox_callback_friendrequest(void *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata) | ||
235 | { | ||
236 | Messenger *m = tox; | ||
237 | m_callback_friendrequest(m, function, userdata); | ||
238 | } | ||
239 | |||
240 | |||
241 | /* set the function that will be executed when a message from a friend is received. | ||
242 | function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ | ||
243 | void tox_callback_friendmessage(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), | ||
244 | void *userdata) | ||
245 | { | ||
246 | Messenger *m = tox; | ||
247 | m_callback_friendmessage(m, function, userdata); | ||
248 | } | ||
249 | |||
250 | /* set the function that will be executed when an action from a friend is received. | ||
251 | function format is: function(int friendnumber, uint8_t * action, uint32_t length) */ | ||
252 | void tox_callback_action(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), void *userdata) | ||
253 | { | ||
254 | Messenger *m = tox; | ||
255 | m_callback_action(m, function, userdata); | ||
256 | } | ||
257 | |||
258 | /* set the callback for name changes | ||
259 | function(int friendnumber, uint8_t *newname, uint16_t length) | ||
260 | you are not responsible for freeing newname */ | ||
261 | void tox_callback_namechange(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), | ||
262 | void *userdata) | ||
263 | { | ||
264 | Messenger *m = tox; | ||
265 | m_callback_namechange(m, function, userdata); | ||
266 | } | ||
267 | |||
268 | /* set the callback for status message changes | ||
269 | function(int friendnumber, uint8_t *newstatus, uint16_t length) | ||
270 | you are not responsible for freeing newstatus */ | ||
271 | void tox_callback_statusmessage(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), | ||
272 | void *userdata) | ||
273 | { | ||
274 | Messenger *m = tox; | ||
275 | m_callback_statusmessage(m, function, userdata); | ||
276 | } | ||
277 | |||
278 | /* set the callback for status type changes | ||
279 | function(int friendnumber, USERSTATUS kind) */ | ||
280 | void tox_callback_userstatus(void *tox, void (*function)(Messenger *tox, int, USERSTATUS, void *), void *userdata) | ||
281 | { | ||
282 | Messenger *m = tox; | ||
283 | m_callback_userstatus(m, function, userdata); | ||
284 | } | ||
285 | |||
286 | /* set the callback for read receipts | ||
287 | function(int friendnumber, uint32_t receipt) | ||
288 | if you are keeping a record of returns from m_sendmessage, | ||
289 | receipt might be one of those values, and that means the message | ||
290 | has been received on the other side. since core doesn't | ||
291 | track ids for you, receipt may not correspond to any message | ||
292 | in that case, you should discard it. */ | ||
293 | void tox_callback_read_receipt(void *tox, void (*function)(Messenger *tox, int, uint32_t, void *), void *userdata) | ||
294 | { | ||
295 | Messenger *m = tox; | ||
296 | m_callback_read_receipt(m, function, userdata); | ||
297 | } | ||
298 | |||
299 | /* set the callback for connection status changes | ||
300 | function(int friendnumber, uint8_t status) | ||
301 | status: | ||
302 | 0 -- friend went offline after being previously online | ||
303 | 1 -- friend went online | ||
304 | note that this callback is not called when adding friends, thus the "after | ||
305 | being previously online" part. it's assumed that when adding friends, | ||
306 | their connection status is offline. */ | ||
307 | void tox_callback_connectionstatus(void *tox, void (*function)(Messenger *tox, int, uint8_t, void *), void *userdata) | ||
308 | { | ||
309 | Messenger *m = tox; | ||
310 | m_callback_connectionstatus(m, function, userdata); | ||
311 | } | ||
312 | |||
313 | /* Use this function to bootstrap the client | ||
314 | Sends a get nodes request to the given node with ip port and public_key */ | ||
315 | void tox_bootstrap(void *tox, IP_Port ip_port, uint8_t *public_key) | ||
316 | { | ||
317 | Messenger *m = tox; | ||
318 | DHT_bootstrap(m->dht, ip_port, public_key); | ||
319 | } | ||
320 | |||
321 | /* returns 0 if we are not connected to the DHT | ||
322 | returns 1 if we are */ | ||
323 | int tox_isconnected(void *tox) | ||
324 | { | ||
325 | Messenger *m = tox; | ||
326 | return DHT_isconnected(m->dht); | ||
327 | } | ||
328 | |||
329 | /* run this at startup | ||
330 | * returns allocated instance of tox on success | ||
331 | * returns 0 if there are problems */ | ||
332 | void *tox_new(void) | ||
333 | { | ||
334 | return initMessenger(); | ||
335 | } | ||
336 | |||
337 | /* run this before closing shop | ||
338 | * free all datastructures */ | ||
339 | void tox_kill(void *tox) | ||
340 | { | ||
341 | Messenger *m = tox; | ||
342 | cleanupMessenger(m); | ||
343 | } | ||
344 | |||
345 | /* the main loop that needs to be run at least 20 times per second */ | ||
346 | void tox_do(void *tox) | ||
347 | { | ||
348 | Messenger *m = tox; | ||
349 | doMessenger(m); | ||
350 | } | ||
351 | |||
352 | /* SAVING AND LOADING FUNCTIONS: */ | ||
353 | |||
354 | /* returns the size of the messenger data (for saving) */ | ||
355 | uint32_t tox_size(void *tox) | ||
356 | { | ||
357 | Messenger *m = tox; | ||
358 | return Messenger_size(m); | ||
359 | } | ||
360 | |||
361 | /* save the messenger in data (must be allocated memory of size Messenger_size()) */ | ||
362 | void tox_save(void *tox, uint8_t *data) | ||
363 | { | ||
364 | Messenger *m = tox; | ||
365 | Messenger_save(m, data); | ||
366 | } | ||
367 | |||
368 | /* load the messenger from data of size length */ | ||
369 | int tox_load(void *tox, uint8_t *data, uint32_t length) | ||
370 | { | ||
371 | Messenger *m = tox; | ||
372 | return Messenger_load(m, data, length); | ||
373 | } | ||
374 | |||
diff --git a/core/tox.h b/core/tox.h new file mode 100644 index 00000000..bdfac1d6 --- /dev/null +++ b/core/tox.h | |||
@@ -0,0 +1,289 @@ | |||
1 | /* tox.h | ||
2 | * | ||
3 | * The Tox public API. | ||
4 | * | ||
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
6 | * | ||
7 | * This file is part of Tox. | ||
8 | * | ||
9 | * Tox is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * Tox is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #ifndef TOX_H | ||
25 | #define TOX_H | ||
26 | |||
27 | #include <stdint.h> | ||
28 | |||
29 | #ifdef __cplusplus | ||
30 | extern "C" { | ||
31 | #endif | ||
32 | |||
33 | #define TOX_MAX_NAME_LENGTH 128 | ||
34 | #define TOX_MAX_STATUSMESSAGE_LENGTH 128 | ||
35 | #define TOX_CLIENT_ID_SIZE 32 | ||
36 | |||
37 | #define TOX_FRIEND_ADDRESS_SIZE (TOX_CLIENT_ID_SIZE + sizeof(uint32_t) + sizeof(uint16_t)) | ||
38 | |||
39 | |||
40 | typedef union { | ||
41 | uint8_t c[4]; | ||
42 | uint16_t s[2]; | ||
43 | uint32_t i; | ||
44 | } tox_IP; | ||
45 | |||
46 | typedef struct { | ||
47 | tox_IP ip; | ||
48 | uint16_t port; | ||
49 | /* not used for anything right now */ | ||
50 | uint16_t padding; | ||
51 | } tox_IP_Port; | ||
52 | |||
53 | /* status definitions */ | ||
54 | enum { | ||
55 | TOX_NOFRIEND, | ||
56 | TOX_FRIEND_ADDED, | ||
57 | TOX_FRIEND_REQUESTED, | ||
58 | TOX_FRIEND_CONFIRMED, | ||
59 | TOX_FRIEND_ONLINE, | ||
60 | }; | ||
61 | |||
62 | /* errors for m_addfriend | ||
63 | * FAERR - Friend Add Error */ | ||
64 | enum { | ||
65 | TOX_FAERR_TOOLONG = -1, | ||
66 | TOX_FAERR_NOMESSAGE = -2, | ||
67 | TOX_FAERR_OWNKEY = -3, | ||
68 | TOX_FAERR_ALREADYSENT = -4, | ||
69 | TOX_FAERR_UNKNOWN = -5, | ||
70 | TOX_FAERR_BADCHECKSUM = -6, | ||
71 | TOX_FAERR_SETNEWNOSPAM = -7, | ||
72 | TOX_FAERR_NOMEM = -8 | ||
73 | }; | ||
74 | /* USERSTATUS | ||
75 | * Represents userstatuses someone can have. */ | ||
76 | |||
77 | typedef enum { | ||
78 | TOX_USERSTATUS_NONE, | ||
79 | TOX_USERSTATUS_AWAY, | ||
80 | TOX_USERSTATUS_BUSY, | ||
81 | TOX_USERSTATUS_INVALID | ||
82 | } | ||
83 | TOX_USERSTATUS; | ||
84 | |||
85 | typedef void Tox; | ||
86 | |||
87 | /* | ||
88 | * returns a FRIEND_ADDRESS_SIZE byte address to give to others. | ||
89 | * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] | ||
90 | * | ||
91 | */ | ||
92 | void tox_getaddress(Tox *tox, uint8_t *address); | ||
93 | |||
94 | /* | ||
95 | * add a friend | ||
96 | * set the data that will be sent along with friend request | ||
97 | * address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum. | ||
98 | * data is the data and length is the length | ||
99 | * returns the friend number if success | ||
100 | * return TOX_FA_TOOLONG if message length is too long | ||
101 | * return TOX_FAERR_NOMESSAGE if no message (message length must be >= 1 byte) | ||
102 | * return TOX_FAERR_OWNKEY if user's own key | ||
103 | * return TOX_FAERR_ALREADYSENT if friend request already sent or already a friend | ||
104 | * return TOX_FAERR_UNKNOWN for unknown error | ||
105 | * return TOX_FAERR_BADCHECKSUM if bad checksum in address | ||
106 | * return TOX_FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different | ||
107 | * (the nospam for that friend was set to the new one) | ||
108 | * return TOX_FAERR_NOMEM if increasing the friend list size fails | ||
109 | */ | ||
110 | int tox_addfriend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length); | ||
111 | |||
112 | |||
113 | /* add a friend without sending a friendrequest. | ||
114 | returns the friend number if success | ||
115 | return -1 if failure. */ | ||
116 | int tox_addfriend_norequest(Tox *tox, uint8_t *client_id); | ||
117 | |||
118 | /* return the friend id associated to that client id. | ||
119 | return -1 if no such friend */ | ||
120 | int tox_getfriend_id(Tox *tox, uint8_t *client_id); | ||
121 | |||
122 | /* copies the public key associated to that friend id into client_id buffer. | ||
123 | make sure that client_id is of size CLIENT_ID_SIZE. | ||
124 | return 0 if success | ||
125 | return -1 if failure */ | ||
126 | int tox_getclient_id(Tox *tox, int friend_id, uint8_t *client_id); | ||
127 | |||
128 | /* remove a friend */ | ||
129 | int tox_delfriend(Tox *tox, int friendnumber); | ||
130 | |||
131 | /* return TOX_FRIEND_ONLINE if friend is online | ||
132 | return TOX_FRIEND_CONFIRMED if friend is confirmed | ||
133 | return TOX_FRIEND_REQUESTED if the friend request was sent | ||
134 | return TOX_FRIEND_ADDED if the friend was added | ||
135 | return TOX_NOFRIEND if there is no friend with that number */ | ||
136 | int tox_friendstatus(Tox *tox, int friendnumber); | ||
137 | |||
138 | /* send a text chat message to an online friend | ||
139 | returns the message id if packet was successfully put into the send queue | ||
140 | return 0 if it was not | ||
141 | you will want to retain the return value, it will be passed to your read receipt callback | ||
142 | if one is received. | ||
143 | m_sendmessage_withid will send a message with the id of your choosing, | ||
144 | however we can generate an id for you by calling plain m_sendmessage. */ | ||
145 | uint32_t tox_sendmessage(Tox *tox, int friendnumber, uint8_t *message, uint32_t length); | ||
146 | uint32_t tox_sendmessage_withid(Tox *tox, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length); | ||
147 | |||
148 | /* send an action to an online friend | ||
149 | returns 1 if packet was successfully put into the send queue | ||
150 | return 0 if it was not */ | ||
151 | int tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length); | ||
152 | |||
153 | /* Set our nickname | ||
154 | name must be a string of maximum MAX_NAME_LENGTH length. | ||
155 | length must be at least 1 byte | ||
156 | length is the length of name with the NULL terminator | ||
157 | return 0 if success | ||
158 | return -1 if failure */ | ||
159 | int tox_setname(Tox *tox, uint8_t *name, uint16_t length); | ||
160 | |||
161 | /* | ||
162 | Get your nickname. | ||
163 | m The messanger context to use. | ||
164 | name Pointer to a string for the name. | ||
165 | nlen The length of the string buffer. | ||
166 | returns Return the length of the name, 0 on error. | ||
167 | */ | ||
168 | uint16_t tox_getselfname(Tox *tox, uint8_t *name, uint16_t nlen); | ||
169 | |||
170 | /* get name of friendnumber | ||
171 | put it in name | ||
172 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. | ||
173 | return 0 if success | ||
174 | return -1 if failure */ | ||
175 | int tox_getname(Tox *tox, int friendnumber, uint8_t *name); | ||
176 | |||
177 | /* set our user status | ||
178 | you are responsible for freeing status after | ||
179 | returns 0 on success, -1 on failure */ | ||
180 | int tox_set_statusmessage(Tox *tox, uint8_t *status, uint16_t length); | ||
181 | int tox_set_userstatus(Tox *tox, TOX_USERSTATUS status); | ||
182 | |||
183 | /* return the length of friendnumber's status message, | ||
184 | including null | ||
185 | pass it into malloc */ | ||
186 | int tox_get_statusmessage_size(Tox *tox, int friendnumber); | ||
187 | |||
188 | /* copy friendnumber's status message into buf, truncating if size is over maxlen | ||
189 | get the size you need to allocate from m_get_statusmessage_size | ||
190 | The self variant will copy our own status message. */ | ||
191 | int tox_copy_statusmessage(Tox *tox, int friendnumber, uint8_t *buf, uint32_t maxlen); | ||
192 | int tox_copy_self_statusmessage(Tox *tox, uint8_t *buf, uint32_t maxlen); | ||
193 | |||
194 | /* Return one of USERSTATUS values. | ||
195 | * Values unknown to your application should be represented as USERSTATUS_NONE. | ||
196 | * As above, the self variant will return our own USERSTATUS. | ||
197 | * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ | ||
198 | TOX_USERSTATUS tox_get_userstatus(Tox *tox, int friendnumber); | ||
199 | TOX_USERSTATUS tox_get_selfuserstatus(Tox *tox); | ||
200 | |||
201 | /* Sets whether we send read receipts for friendnumber. | ||
202 | * This function is not lazy, and it will fail if yesno is not (0 or 1).*/ | ||
203 | void tox_set_sends_receipts(Tox *tox, int friendnumber, int yesno); | ||
204 | |||
205 | /* set the function that will be executed when a friend request is received. | ||
206 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ | ||
207 | void tox_callback_friendrequest(Tox *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata); | ||
208 | |||
209 | /* set the function that will be executed when a message from a friend is received. | ||
210 | function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ | ||
211 | void tox_callback_friendmessage(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), | ||
212 | void *userdata); | ||
213 | |||
214 | /* set the function that will be executed when an action from a friend is received. | ||
215 | function format is: function(int friendnumber, uint8_t * action, uint32_t length) */ | ||
216 | void tox_callback_action(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), void *userdata); | ||
217 | |||
218 | /* set the callback for name changes | ||
219 | function(int friendnumber, uint8_t *newname, uint16_t length) | ||
220 | you are not responsible for freeing newname */ | ||
221 | void tox_callback_namechange(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), | ||
222 | void *userdata); | ||
223 | |||
224 | /* set the callback for status message changes | ||
225 | function(int friendnumber, uint8_t *newstatus, uint16_t length) | ||
226 | you are not responsible for freeing newstatus */ | ||
227 | void tox_callback_statusmessage(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), | ||
228 | void *userdata); | ||
229 | |||
230 | /* set the callback for status type changes | ||
231 | function(int friendnumber, USERSTATUS kind) */ | ||
232 | void tox_callback_userstatus(Tox *tox, void (*function)(Tox *tox, int, TOX_USERSTATUS, void *), void *userdata); | ||
233 | |||
234 | /* set the callback for read receipts | ||
235 | function(int friendnumber, uint32_t receipt) | ||
236 | if you are keeping a record of returns from m_sendmessage, | ||
237 | receipt might be one of those values, and that means the message | ||
238 | has been received on the other side. since core doesn't | ||
239 | track ids for you, receipt may not correspond to any message | ||
240 | in that case, you should discard it. */ | ||
241 | void tox_callback_read_receipt(Tox *tox, void (*function)(Tox *tox, int, uint32_t, void *), void *userdata); | ||
242 | |||
243 | /* set the callback for connection status changes | ||
244 | function(int friendnumber, uint8_t status) | ||
245 | status: | ||
246 | 0 -- friend went offline after being previously online | ||
247 | 1 -- friend went online | ||
248 | note that this callback is not called when adding friends, thus the "after | ||
249 | being previously online" part. it's assumed that when adding friends, | ||
250 | their connection status is offline. */ | ||
251 | void tox_callback_connectionstatus(Tox *tox, void (*function)(Tox *tox, int, uint8_t, void *), void *userdata); | ||
252 | |||
253 | /* Use this function to bootstrap the client | ||
254 | Sends a get nodes request to the given node with ip port and public_key */ | ||
255 | void tox_bootstrap(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key); | ||
256 | |||
257 | /* returns 0 if we are not connected to the DHT | ||
258 | returns 1 if we are */ | ||
259 | int tox_isconnected(Tox *tox); | ||
260 | |||
261 | /* run this at startup | ||
262 | * returns allocated instance of tox on success | ||
263 | * returns 0 if there are problems */ | ||
264 | Tox *tox_new(void); | ||
265 | |||
266 | /* run this before closing shop | ||
267 | * free all datastructures */ | ||
268 | void tox_kill(Tox *tox); | ||
269 | |||
270 | /* the main loop that needs to be run at least 20 times per second */ | ||
271 | void tox_do(Tox *tox); | ||
272 | |||
273 | /* SAVING AND LOADING FUNCTIONS: */ | ||
274 | |||
275 | /* returns the size of the messenger data (for saving) */ | ||
276 | uint32_t tox_size(Tox *tox); | ||
277 | |||
278 | /* save the messenger in data (must be allocated memory of size Messenger_size()) */ | ||
279 | void tox_save(Tox *tox, uint8_t *data); | ||
280 | |||
281 | /* load the messenger from data of size length */ | ||
282 | int tox_load(Tox *tox, uint8_t *data, uint32_t length); | ||
283 | |||
284 | |||
285 | #ifdef __cplusplus | ||
286 | } | ||
287 | #endif | ||
288 | |||
289 | #endif | ||
diff --git a/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c b/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c index 7604b1e0..46409b76 100644 --- a/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c +++ b/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c | |||
@@ -72,6 +72,47 @@ int b16_to_key(char b16_string[], uint8_t *bs_pubkey) | |||
72 | return 0; | 72 | return 0; |
73 | } | 73 | } |
74 | 74 | ||
75 | /* | ||
76 | resolve_addr(): | ||
77 | address should represent IPv4 or a hostname with A record | ||
78 | |||
79 | returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i | ||
80 | returns 0 on failure | ||
81 | |||
82 | TODO: Fix ipv6 support | ||
83 | */ | ||
84 | |||
85 | uint32_t resolve_addr(const char *address) | ||
86 | { | ||
87 | struct addrinfo *server = NULL; | ||
88 | struct addrinfo hints; | ||
89 | int rc; | ||
90 | uint32_t addr; | ||
91 | |||
92 | memset(&hints, 0, sizeof(hints)); | ||
93 | hints.ai_family = AF_INET; // IPv4 only right now. | ||
94 | hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. | ||
95 | |||
96 | rc = getaddrinfo(address, "echo", &hints, &server); | ||
97 | |||
98 | // Lookup failed. | ||
99 | if (rc != 0) { | ||
100 | return 0; | ||
101 | } | ||
102 | |||
103 | // IPv4 records only.. | ||
104 | if (server->ai_family != AF_INET) { | ||
105 | freeaddrinfo(server); | ||
106 | return 0; | ||
107 | } | ||
108 | |||
109 | |||
110 | addr = ((struct sockaddr_in *)server->ai_addr)->sin_addr.s_addr; | ||
111 | |||
112 | freeaddrinfo(server); | ||
113 | return addr; | ||
114 | } | ||
115 | |||
75 | /* This unction connects to all specified servers | 116 | /* This unction connects to all specified servers |
76 | and connect to them. | 117 | and connect to them. |
77 | returns 1 if the connection to the DHT is up | 118 | returns 1 if the connection to the DHT is up |
diff --git a/testing/nTox.c b/testing/nTox.c index a476cc19..9df1e78b 100644 --- a/testing/nTox.c +++ b/testing/nTox.c | |||
@@ -43,22 +43,22 @@ char *help = "[i] commands:\n/f ID (to add friend)\n/m friendnumber message " | |||
43 | int x, y; | 43 | int x, y; |
44 | 44 | ||
45 | typedef struct { | 45 | typedef struct { |
46 | uint8_t id[CLIENT_ID_SIZE]; | 46 | uint8_t id[TOX_CLIENT_ID_SIZE]; |
47 | uint8_t accepted; | 47 | uint8_t accepted; |
48 | } Friend_request; | 48 | } Friend_request; |
49 | 49 | ||
50 | Friend_request pending_requests[256]; | 50 | Friend_request pending_requests[256]; |
51 | uint8_t num_requests = 0; | 51 | uint8_t num_requests = 0; |
52 | 52 | ||
53 | void get_id(Messenger *m, char *data) | 53 | void get_id(Tox *m, char *data) |
54 | { | 54 | { |
55 | sprintf(data, "[i] ID: "); | 55 | sprintf(data, "[i] ID: "); |
56 | int offset = strlen(data); | 56 | int offset = strlen(data); |
57 | int i = 0; | 57 | int i = 0; |
58 | uint8_t address[FRIEND_ADDRESS_SIZE]; | 58 | uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; |
59 | getaddress(m, address); | 59 | tox_getaddress(m, address); |
60 | 60 | ||
61 | for (; i < FRIEND_ADDRESS_SIZE; i++) { | 61 | for (; i < TOX_FRIEND_ADDRESS_SIZE; i++) { |
62 | sprintf(data + 2 * i + offset, "%02X ", address[i]); | 62 | sprintf(data + 2 * i + offset, "%02X ", address[i]); |
63 | } | 63 | } |
64 | } | 64 | } |
@@ -75,15 +75,15 @@ void new_lines(char *line) | |||
75 | } | 75 | } |
76 | 76 | ||
77 | 77 | ||
78 | void print_friendlist(Messenger *m) | 78 | void print_friendlist(Tox *m) |
79 | { | 79 | { |
80 | char name[MAX_NAME_LENGTH]; | 80 | char name[TOX_MAX_NAME_LENGTH]; |
81 | int i = 0; | 81 | int i = 0; |
82 | new_lines("[i] Friend List:"); | 82 | new_lines("[i] Friend List:"); |
83 | 83 | ||
84 | while (getname(m, i, (uint8_t *)name) != -1) { | 84 | while (tox_getname(m, i, (uint8_t *)name) != -1) { |
85 | /* account for the longest name and the longest "base" string */ | 85 | /* account for the longest name and the longest "base" string */ |
86 | char fstring[MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")]; | 86 | char fstring[TOX_MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")]; |
87 | 87 | ||
88 | if (strlen(name) <= 0) { | 88 | if (strlen(name) <= 0) { |
89 | sprintf(fstring, "[i] Friend: No Friend!\n\tid: %i", i); | 89 | sprintf(fstring, "[i] Friend: No Friend!\n\tid: %i", i); |
@@ -99,14 +99,14 @@ void print_friendlist(Messenger *m) | |||
99 | new_lines("\tno friends! D:"); | 99 | new_lines("\tno friends! D:"); |
100 | } | 100 | } |
101 | 101 | ||
102 | char *format_message(Messenger *m, char *message, int friendnum) | 102 | char *format_message(Tox *m, char *message, int friendnum) |
103 | { | 103 | { |
104 | char name[MAX_NAME_LENGTH]; | 104 | char name[TOX_MAX_NAME_LENGTH]; |
105 | 105 | ||
106 | if (friendnum != -1) { | 106 | if (friendnum != -1) { |
107 | getname(m, friendnum, (uint8_t *)name); | 107 | tox_getname(m, friendnum, (uint8_t *)name); |
108 | } else { | 108 | } else { |
109 | getself_name(m, (uint8_t *)name, sizeof(name)); | 109 | tox_getselfname(m, (uint8_t *)name, sizeof(name)); |
110 | } | 110 | } |
111 | 111 | ||
112 | char *msg = malloc(100 + strlen(message) + strlen(name) + 1); | 112 | char *msg = malloc(100 + strlen(message) + strlen(name) + 1); |
@@ -129,7 +129,7 @@ char *format_message(Messenger *m, char *message, int friendnum) | |||
129 | return msg; | 129 | return msg; |
130 | } | 130 | } |
131 | 131 | ||
132 | void line_eval(Messenger *m, char *line) | 132 | void line_eval(Tox *m, char *line) |
133 | { | 133 | { |
134 | if (line[0] == '/') { | 134 | if (line[0] == '/') { |
135 | char inpt_command = line[1]; | 135 | char inpt_command = line[1]; |
@@ -146,28 +146,28 @@ void line_eval(Messenger *m, char *line) | |||
146 | temp_id[i] = line[i + prompt_offset]; | 146 | temp_id[i] = line[i + prompt_offset]; |
147 | 147 | ||
148 | unsigned char *bin_string = hex_string_to_bin(temp_id); | 148 | unsigned char *bin_string = hex_string_to_bin(temp_id); |
149 | int num = m_addfriend(m, bin_string, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); | 149 | int num = tox_addfriend(m, bin_string, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); |
150 | free(bin_string); | 150 | free(bin_string); |
151 | char numstring[100]; | 151 | char numstring[100]; |
152 | 152 | ||
153 | switch (num) { | 153 | switch (num) { |
154 | case FAERR_TOOLONG: | 154 | case TOX_FAERR_TOOLONG: |
155 | sprintf(numstring, "[i] Message is too long."); | 155 | sprintf(numstring, "[i] Message is too long."); |
156 | break; | 156 | break; |
157 | 157 | ||
158 | case FAERR_NOMESSAGE: | 158 | case TOX_FAERR_NOMESSAGE: |
159 | sprintf(numstring, "[i] Please add a message to your request."); | 159 | sprintf(numstring, "[i] Please add a message to your request."); |
160 | break; | 160 | break; |
161 | 161 | ||
162 | case FAERR_OWNKEY: | 162 | case TOX_FAERR_OWNKEY: |
163 | sprintf(numstring, "[i] That appears to be your own ID."); | 163 | sprintf(numstring, "[i] That appears to be your own ID."); |
164 | break; | 164 | break; |
165 | 165 | ||
166 | case FAERR_ALREADYSENT: | 166 | case TOX_FAERR_ALREADYSENT: |
167 | sprintf(numstring, "[i] Friend request already sent."); | 167 | sprintf(numstring, "[i] Friend request already sent."); |
168 | break; | 168 | break; |
169 | 169 | ||
170 | case FAERR_UNKNOWN: | 170 | case TOX_FAERR_UNKNOWN: |
171 | sprintf(numstring, "[i] Undefined error when adding friend."); | 171 | sprintf(numstring, "[i] Undefined error when adding friend."); |
172 | break; | 172 | break; |
173 | 173 | ||
@@ -179,7 +179,7 @@ void line_eval(Messenger *m, char *line) | |||
179 | new_lines(numstring); | 179 | new_lines(numstring); |
180 | do_refresh(); | 180 | do_refresh(); |
181 | } else if (inpt_command == 'd') { | 181 | } else if (inpt_command == 'd') { |
182 | doMessenger(m); | 182 | tox_do(m); |
183 | } else if (inpt_command == 'm') { //message command: /m friendnumber messsage | 183 | } else if (inpt_command == 'm') { //message command: /m friendnumber messsage |
184 | size_t len = strlen(line); | 184 | size_t len = strlen(line); |
185 | 185 | ||
@@ -205,13 +205,13 @@ void line_eval(Messenger *m, char *line) | |||
205 | 205 | ||
206 | int num = atoi(numstring); | 206 | int num = atoi(numstring); |
207 | 207 | ||
208 | if (m_sendmessage(m, num, (uint8_t *) message, strlen(message) + 1) != 1) { | 208 | if (tox_sendmessage(m, num, (uint8_t *) message, strlen(message) + 1) != 1) { |
209 | new_lines("[i] could not send message"); | 209 | new_lines("[i] could not send message"); |
210 | } else { | 210 | } else { |
211 | new_lines(format_message(m, message, -1)); | 211 | new_lines(format_message(m, message, -1)); |
212 | } | 212 | } |
213 | } else if (inpt_command == 'n') { | 213 | } else if (inpt_command == 'n') { |
214 | uint8_t name[MAX_NAME_LENGTH]; | 214 | uint8_t name[TOX_MAX_NAME_LENGTH]; |
215 | int i = 0; | 215 | int i = 0; |
216 | size_t len = strlen(line); | 216 | size_t len = strlen(line); |
217 | 217 | ||
@@ -222,14 +222,14 @@ void line_eval(Messenger *m, char *line) | |||
222 | } | 222 | } |
223 | 223 | ||
224 | name[i - 3] = 0; | 224 | name[i - 3] = 0; |
225 | setname(m, name, i - 2); | 225 | tox_setname(m, name, i - 2); |
226 | char numstring[100]; | 226 | char numstring[100]; |
227 | sprintf(numstring, "[i] changed nick to %s", (char *)name); | 227 | sprintf(numstring, "[i] changed nick to %s", (char *)name); |
228 | new_lines(numstring); | 228 | new_lines(numstring); |
229 | } else if (inpt_command == 'l') { | 229 | } else if (inpt_command == 'l') { |
230 | print_friendlist(m); | 230 | print_friendlist(m); |
231 | } else if (inpt_command == 's') { | 231 | } else if (inpt_command == 's') { |
232 | uint8_t status[MAX_STATUSMESSAGE_LENGTH]; | 232 | uint8_t status[TOX_MAX_STATUSMESSAGE_LENGTH]; |
233 | int i = 0; | 233 | int i = 0; |
234 | size_t len = strlen(line); | 234 | size_t len = strlen(line); |
235 | 235 | ||
@@ -240,7 +240,7 @@ void line_eval(Messenger *m, char *line) | |||
240 | } | 240 | } |
241 | 241 | ||
242 | status[i - 3] = 0; | 242 | status[i - 3] = 0; |
243 | m_set_statusmessage(m, status, strlen((char *)status) + 1); | 243 | tox_set_statusmessage(m, status, strlen((char *)status) + 1); |
244 | char numstring[100]; | 244 | char numstring[100]; |
245 | sprintf(numstring, "[i] changed status to %s", (char *)status); | 245 | sprintf(numstring, "[i] changed status to %s", (char *)status); |
246 | new_lines(numstring); | 246 | new_lines(numstring); |
@@ -252,7 +252,7 @@ void line_eval(Messenger *m, char *line) | |||
252 | sprintf(numchar, "[i] you either didn't receive that request or you already accepted it"); | 252 | sprintf(numchar, "[i] you either didn't receive that request or you already accepted it"); |
253 | new_lines(numchar); | 253 | new_lines(numchar); |
254 | } else { | 254 | } else { |
255 | int num = m_addfriend_norequest(m, pending_requests[numf].id); | 255 | int num = tox_addfriend_norequest(m, pending_requests[numf].id); |
256 | 256 | ||
257 | if (num != -1) { | 257 | if (num != -1) { |
258 | pending_requests[numf].accepted = 1; | 258 | pending_requests[numf].accepted = 1; |
@@ -364,40 +364,40 @@ void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *us | |||
364 | char numchar[100]; | 364 | char numchar[100]; |
365 | sprintf(numchar, "[i] accept request with /a %u", num_requests); | 365 | sprintf(numchar, "[i] accept request with /a %u", num_requests); |
366 | new_lines(numchar); | 366 | new_lines(numchar); |
367 | memcpy(pending_requests[num_requests].id, public_key, CLIENT_ID_SIZE); | 367 | memcpy(pending_requests[num_requests].id, public_key, TOX_CLIENT_ID_SIZE); |
368 | pending_requests[num_requests].accepted = 0; | 368 | pending_requests[num_requests].accepted = 0; |
369 | ++num_requests; | 369 | ++num_requests; |
370 | do_refresh(); | 370 | do_refresh(); |
371 | } | 371 | } |
372 | 372 | ||
373 | void print_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) | 373 | void print_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) |
374 | { | 374 | { |
375 | new_lines(format_message(m, (char *)string, friendnumber)); | 375 | new_lines(format_message(m, (char *)string, friendnumber)); |
376 | } | 376 | } |
377 | 377 | ||
378 | void print_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) | 378 | void print_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) |
379 | { | 379 | { |
380 | char name[MAX_NAME_LENGTH]; | 380 | char name[TOX_MAX_NAME_LENGTH]; |
381 | 381 | ||
382 | if (getname(m, friendnumber, (uint8_t *)name) != -1) { | 382 | if (tox_getname(m, friendnumber, (uint8_t *)name) != -1) { |
383 | char msg[100 + length]; | 383 | char msg[100 + length]; |
384 | sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); | 384 | sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); |
385 | new_lines(msg); | 385 | new_lines(msg); |
386 | } | 386 | } |
387 | } | 387 | } |
388 | 388 | ||
389 | void print_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) | 389 | void print_statuschange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) |
390 | { | 390 | { |
391 | char name[MAX_NAME_LENGTH]; | 391 | char name[TOX_MAX_NAME_LENGTH]; |
392 | 392 | ||
393 | if (getname(m, friendnumber, (uint8_t *)name) != -1) { | 393 | if (tox_getname(m, friendnumber, (uint8_t *)name) != -1) { |
394 | char msg[100 + length + strlen(name) + 1]; | 394 | char msg[100 + length + strlen(name) + 1]; |
395 | sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); | 395 | sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); |
396 | new_lines(msg); | 396 | new_lines(msg); |
397 | } | 397 | } |
398 | } | 398 | } |
399 | 399 | ||
400 | void load_key(Messenger *m, char *path) | 400 | void load_key(Tox *m, char *path) |
401 | { | 401 | { |
402 | FILE *data_file = fopen(path, "r"); | 402 | FILE *data_file = fopen(path, "r"); |
403 | int size = 0; | 403 | int size = 0; |
@@ -415,13 +415,13 @@ void load_key(Messenger *m, char *path) | |||
415 | goto FILE_ERROR; | 415 | goto FILE_ERROR; |
416 | } | 416 | } |
417 | 417 | ||
418 | Messenger_load(m, data, size); | 418 | tox_load(m, data, size); |
419 | 419 | ||
420 | } else { | 420 | } else { |
421 | //else save new keys | 421 | //else save new keys |
422 | int size = Messenger_size(m); | 422 | int size = tox_size(m); |
423 | uint8_t data[size]; | 423 | uint8_t data[size]; |
424 | Messenger_save(m, data); | 424 | tox_save(m, data); |
425 | data_file = fopen(path, "w"); | 425 | data_file = fopen(path, "w"); |
426 | 426 | ||
427 | if (!data_file) { | 427 | if (!data_file) { |
@@ -463,7 +463,7 @@ int main(int argc, char *argv[]) | |||
463 | int i = 0; | 463 | int i = 0; |
464 | char *filename = "data"; | 464 | char *filename = "data"; |
465 | char idstring[200] = {0}; | 465 | char idstring[200] = {0}; |
466 | Messenger *m; | 466 | Tox *m; |
467 | 467 | ||
468 | if (argc < 4) { | 468 | if (argc < 4) { |
469 | printf("[!] Usage: %s [IP] [port] [public_key] <keyfile>\n", argv[0]); | 469 | printf("[!] Usage: %s [IP] [port] [public_key] <keyfile>\n", argv[0]); |
@@ -487,7 +487,7 @@ int main(int argc, char *argv[]) | |||
487 | } | 487 | } |
488 | } | 488 | } |
489 | 489 | ||
490 | m = initMessenger(); | 490 | m = tox_new(); |
491 | 491 | ||
492 | if ( !m ) { | 492 | if ( !m ) { |
493 | fputs("Failed to allocate Messenger datastructure", stderr); | 493 | fputs("Failed to allocate Messenger datastructure", stderr); |
@@ -496,10 +496,10 @@ int main(int argc, char *argv[]) | |||
496 | 496 | ||
497 | load_key(m, filename); | 497 | load_key(m, filename); |
498 | 498 | ||
499 | m_callback_friendrequest(m, print_request, NULL); | 499 | tox_callback_friendrequest(m, print_request, NULL); |
500 | m_callback_friendmessage(m, print_message, NULL); | 500 | tox_callback_friendmessage(m, print_message, NULL); |
501 | m_callback_namechange(m, print_nickchange, NULL); | 501 | tox_callback_namechange(m, print_nickchange, NULL); |
502 | m_callback_statusmessage(m, print_statuschange, NULL); | 502 | tox_callback_statusmessage(m, print_statuschange, NULL); |
503 | 503 | ||
504 | initscr(); | 504 | initscr(); |
505 | noecho(); | 505 | noecho(); |
@@ -511,7 +511,7 @@ int main(int argc, char *argv[]) | |||
511 | new_lines(idstring); | 511 | new_lines(idstring); |
512 | strcpy(line, ""); | 512 | strcpy(line, ""); |
513 | 513 | ||
514 | IP_Port bootstrap_ip_port; | 514 | tox_IP_Port bootstrap_ip_port; |
515 | bootstrap_ip_port.port = htons(atoi(argv[2])); | 515 | bootstrap_ip_port.port = htons(atoi(argv[2])); |
516 | int resolved_address = resolve_addr(argv[1]); | 516 | int resolved_address = resolve_addr(argv[1]); |
517 | 517 | ||
@@ -521,17 +521,17 @@ int main(int argc, char *argv[]) | |||
521 | exit(1); | 521 | exit(1); |
522 | 522 | ||
523 | unsigned char *binary_string = hex_string_to_bin(argv[3]); | 523 | unsigned char *binary_string = hex_string_to_bin(argv[3]); |
524 | DHT_bootstrap(m->dht, bootstrap_ip_port, binary_string); | 524 | tox_bootstrap(m, bootstrap_ip_port, binary_string); |
525 | free(binary_string); | 525 | free(binary_string); |
526 | nodelay(stdscr, TRUE); | 526 | nodelay(stdscr, TRUE); |
527 | 527 | ||
528 | while (true) { | 528 | while (true) { |
529 | if (on == 0 && DHT_isconnected(m->dht)) { | 529 | if (on == 0 && tox_isconnected(m)) { |
530 | new_lines("[i] connected to DHT\n[i] define username with /n"); | 530 | new_lines("[i] connected to DHT\n[i] define username with /n"); |
531 | on = 1; | 531 | on = 1; |
532 | } | 532 | } |
533 | 533 | ||
534 | doMessenger(m); | 534 | tox_do(m); |
535 | c_sleep(1); | 535 | c_sleep(1); |
536 | do_refresh(); | 536 | do_refresh(); |
537 | 537 | ||
@@ -552,7 +552,7 @@ int main(int argc, char *argv[]) | |||
552 | } | 552 | } |
553 | } | 553 | } |
554 | 554 | ||
555 | cleanupMessenger(m); | 555 | tox_kill(m); |
556 | endwin(); | 556 | endwin(); |
557 | return 0; | 557 | return 0; |
558 | } | 558 | } |
diff --git a/testing/nTox.h b/testing/nTox.h index fdd88fb4..b27a956f 100644 --- a/testing/nTox.h +++ b/testing/nTox.h | |||
@@ -35,15 +35,55 @@ | |||
35 | #include <arpa/inet.h> | 35 | #include <arpa/inet.h> |
36 | #include <sys/types.h> | 36 | #include <sys/types.h> |
37 | #include <netdb.h> | 37 | #include <netdb.h> |
38 | #include "../core/Messenger.h" | 38 | #include "../core/tox.h" |
39 | #include "../core/network.h" | ||
40 | 39 | ||
41 | #define STRING_LENGTH 256 | 40 | #define STRING_LENGTH 256 |
42 | #define HISTORY 50 | 41 | #define HISTORY 50 |
43 | #define PUB_KEY_BYTES 32 | 42 | #define PUB_KEY_BYTES 32 |
44 | 43 | ||
44 | /* | ||
45 | resolve_addr(): | ||
46 | address should represent IPv4 or a hostname with A record | ||
47 | |||
48 | returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i | ||
49 | returns 0 on failure | ||
50 | |||
51 | TODO: Fix ipv6 support | ||
52 | */ | ||
53 | |||
54 | uint32_t resolve_addr(const char *address) | ||
55 | { | ||
56 | struct addrinfo *server = NULL; | ||
57 | struct addrinfo hints; | ||
58 | int rc; | ||
59 | uint32_t addr; | ||
60 | |||
61 | memset(&hints, 0, sizeof(hints)); | ||
62 | hints.ai_family = AF_INET; // IPv4 only right now. | ||
63 | hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. | ||
64 | |||
65 | rc = getaddrinfo(address, "echo", &hints, &server); | ||
66 | |||
67 | // Lookup failed. | ||
68 | if (rc != 0) { | ||
69 | return 0; | ||
70 | } | ||
71 | |||
72 | // IPv4 records only.. | ||
73 | if (server->ai_family != AF_INET) { | ||
74 | freeaddrinfo(server); | ||
75 | return 0; | ||
76 | } | ||
77 | |||
78 | |||
79 | addr = ((struct sockaddr_in *)server->ai_addr)->sin_addr.s_addr; | ||
80 | |||
81 | freeaddrinfo(server); | ||
82 | return addr; | ||
83 | } | ||
84 | |||
45 | void new_lines(char *line); | 85 | void new_lines(char *line); |
46 | void line_eval(Messenger *m, char *line); | 86 | void line_eval(Tox *m, char *line); |
47 | void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) ; | 87 | void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) ; |
48 | int count_lines(char *string) ; | 88 | int count_lines(char *string) ; |
49 | char *appender(char *str, const char c); | 89 | char *appender(char *str, const char c); |