diff options
Diffstat (limited to 'toxcore/Messenger.h')
-rw-r--r-- | toxcore/Messenger.h | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 6c641a9a..a107d80f 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h | |||
@@ -36,6 +36,9 @@ | |||
36 | #define MAX_NAME_LENGTH 128 | 36 | #define MAX_NAME_LENGTH 128 |
37 | /* TODO: this must depend on other variable. */ | 37 | /* TODO: this must depend on other variable. */ |
38 | #define MAX_STATUSMESSAGE_LENGTH 1007 | 38 | #define MAX_STATUSMESSAGE_LENGTH 1007 |
39 | #define MAX_AVATAR_DATA_LENGTH 16384 | ||
40 | #define AVATAR_HASH_LENGTH 32 | ||
41 | |||
39 | 42 | ||
40 | #define FRIEND_ADDRESS_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(uint32_t) + sizeof(uint16_t)) | 43 | #define FRIEND_ADDRESS_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(uint32_t) + sizeof(uint16_t)) |
41 | 44 | ||
@@ -46,6 +49,11 @@ | |||
46 | #define PACKET_ID_STATUSMESSAGE 49 | 49 | #define PACKET_ID_STATUSMESSAGE 49 |
47 | #define PACKET_ID_USERSTATUS 50 | 50 | #define PACKET_ID_USERSTATUS 50 |
48 | #define PACKET_ID_TYPING 51 | 51 | #define PACKET_ID_TYPING 51 |
52 | #define PACKET_ID_AVATAR_INFO_REQ 52 | ||
53 | #define PACKET_ID_AVATAR_INFO 53 | ||
54 | #define PACKET_ID_AVATAR_DATA_CONTROL 54 | ||
55 | #define PACKET_ID_AVATAR_DATA_START 55 | ||
56 | #define PACKET_ID_AVATAR_DATA_PUSH 56 | ||
49 | #define PACKET_ID_RECEIPT 63 | 57 | #define PACKET_ID_RECEIPT 63 |
50 | #define PACKET_ID_MESSAGE 64 | 58 | #define PACKET_ID_MESSAGE 64 |
51 | #define PACKET_ID_ACTION 65 | 59 | #define PACKET_ID_ACTION 65 |
@@ -109,6 +117,13 @@ enum { | |||
109 | /* If no packets are received from friend in this time interval, kill the connection. */ | 117 | /* If no packets are received from friend in this time interval, kill the connection. */ |
110 | #define FRIEND_CONNECTION_TIMEOUT (FRIEND_PING_INTERVAL * 3) | 118 | #define FRIEND_CONNECTION_TIMEOUT (FRIEND_PING_INTERVAL * 3) |
111 | 119 | ||
120 | /* Must be < MAX_CRYPTO_DATA_SIZE */ | ||
121 | #define AVATAR_DATA_MAX_CHUNK_SIZE (MAX_CRYPTO_DATA_SIZE-1) | ||
122 | |||
123 | /* Per-friend data limit for avatar data requests */ | ||
124 | #define AVATAR_DATA_TRANSFER_LIMIT (10*MAX_AVATAR_DATA_LENGTH) | ||
125 | #define AVATAR_DATA_TRANSFER_TIMEOUT (60) /* 164kB every 60 seconds is not a lot */ | ||
126 | |||
112 | 127 | ||
113 | /* USERSTATUS - | 128 | /* USERSTATUS - |
114 | * Represents userstatuses someone can have. | 129 | * Represents userstatuses someone can have. |
@@ -122,6 +137,42 @@ typedef enum { | |||
122 | } | 137 | } |
123 | USERSTATUS; | 138 | USERSTATUS; |
124 | 139 | ||
140 | /* AVATARFORMAT - | ||
141 | * Data formats for user avatar images | ||
142 | */ | ||
143 | typedef enum { | ||
144 | AVATARFORMAT_NONE, | ||
145 | AVATARFORMAT_PNG | ||
146 | } | ||
147 | AVATARFORMAT; | ||
148 | |||
149 | /* AVATARDATACONTROL | ||
150 | * To control avatar data requests (PACKET_ID_AVATAR_DATA_CONTROL) | ||
151 | */ | ||
152 | typedef enum { | ||
153 | AVATARDATACONTROL_REQ, | ||
154 | AVATARDATACONTROL_ERROR | ||
155 | } | ||
156 | AVATARDATACONTROL; | ||
157 | |||
158 | typedef struct { | ||
159 | uint8_t started; | ||
160 | AVATARFORMAT format; | ||
161 | uint8_t hash[AVATAR_HASH_LENGTH]; | ||
162 | uint32_t total_length; | ||
163 | uint32_t bytes_received; | ||
164 | uint8_t data[MAX_AVATAR_DATA_LENGTH]; | ||
165 | } | ||
166 | AVATARRECEIVEDATA; | ||
167 | |||
168 | typedef struct { | ||
169 | /* Fields only used to limit the network usage from a given friend */ | ||
170 | uint32_t bytes_sent; /* Total bytes send to this user */ | ||
171 | uint64_t last_reset; /* Time the data counter was last reset */ | ||
172 | } | ||
173 | AVATARSENDDATA; | ||
174 | |||
175 | |||
125 | struct File_Transfers { | 176 | struct File_Transfers { |
126 | uint64_t size; | 177 | uint64_t size; |
127 | uint64_t transferred; | 178 | uint64_t transferred; |
@@ -163,6 +214,7 @@ typedef struct { | |||
163 | uint8_t statusmessage_sent; | 214 | uint8_t statusmessage_sent; |
164 | USERSTATUS userstatus; | 215 | USERSTATUS userstatus; |
165 | uint8_t userstatus_sent; | 216 | uint8_t userstatus_sent; |
217 | uint8_t avatar_info_sent; | ||
166 | uint8_t user_istyping; | 218 | uint8_t user_istyping; |
167 | uint8_t user_istyping_sent; | 219 | uint8_t user_istyping_sent; |
168 | uint8_t is_typing; | 220 | uint8_t is_typing; |
@@ -178,6 +230,9 @@ typedef struct { | |||
178 | int invited_groups[MAX_INVITED_GROUPS]; | 230 | int invited_groups[MAX_INVITED_GROUPS]; |
179 | uint16_t invited_groups_num; | 231 | uint16_t invited_groups_num; |
180 | 232 | ||
233 | AVATARSENDDATA avatar_send_data; | ||
234 | AVATARRECEIVEDATA *avatar_recv_data; // We are receiving avatar data from this friend. | ||
235 | |||
181 | struct { | 236 | struct { |
182 | int (*function)(void *object, const uint8_t *data, uint32_t len); | 237 | int (*function)(void *object, const uint8_t *data, uint32_t len); |
183 | void *object; | 238 | void *object; |
@@ -209,6 +264,11 @@ typedef struct Messenger { | |||
209 | 264 | ||
210 | USERSTATUS userstatus; | 265 | USERSTATUS userstatus; |
211 | 266 | ||
267 | AVATARFORMAT avatar_format; | ||
268 | uint8_t *avatar_data; | ||
269 | uint32_t avatar_data_length; | ||
270 | uint8_t avatar_hash[AVATAR_HASH_LENGTH]; | ||
271 | |||
212 | Friend *friendlist; | 272 | Friend *friendlist; |
213 | uint32_t numfriends; | 273 | uint32_t numfriends; |
214 | 274 | ||
@@ -243,6 +303,10 @@ typedef struct Messenger { | |||
243 | void *friend_connectionstatuschange_userdata; | 303 | void *friend_connectionstatuschange_userdata; |
244 | void (*friend_connectionstatuschange_internal)(struct Messenger *m, int32_t, uint8_t, void *); | 304 | void (*friend_connectionstatuschange_internal)(struct Messenger *m, int32_t, uint8_t, void *); |
245 | void *friend_connectionstatuschange_internal_userdata; | 305 | void *friend_connectionstatuschange_internal_userdata; |
306 | void *avatar_info_recv_userdata; | ||
307 | void (*avatar_info_recv)(struct Messenger *m, int32_t, uint8_t, uint8_t *, void *); | ||
308 | void *avatar_data_recv_userdata; | ||
309 | void (*avatar_data_recv)(struct Messenger *m, int32_t, uint8_t, uint8_t *, uint8_t *, uint32_t, void *); | ||
246 | 310 | ||
247 | void (*group_invite)(struct Messenger *m, int32_t, const uint8_t *, void *); | 311 | void (*group_invite)(struct Messenger *m, int32_t, const uint8_t *, void *); |
248 | void *group_invite_userdata; | 312 | void *group_invite_userdata; |
@@ -437,6 +501,94 @@ int m_copy_self_statusmessage(const Messenger *m, uint8_t *buf, uint32_t maxlen) | |||
437 | uint8_t m_get_userstatus(const Messenger *m, int32_t friendnumber); | 501 | uint8_t m_get_userstatus(const Messenger *m, int32_t friendnumber); |
438 | uint8_t m_get_self_userstatus(const Messenger *m); | 502 | uint8_t m_get_self_userstatus(const Messenger *m); |
439 | 503 | ||
504 | |||
505 | /* Set the user avatar image data. | ||
506 | * This should be made before connecting, so we will not announce that the user have no avatar | ||
507 | * before setting and announcing a new one, forcing the peers to re-download it. | ||
508 | * | ||
509 | * Notice that the library treats the image as raw data and does not interpret it by any way. | ||
510 | * | ||
511 | * Arguments: | ||
512 | * format - Avatar image format or NONE for user with no avatar (see AVATARFORMAT); | ||
513 | * data - pointer to the avatar data (may be NULL it the format is NONE); | ||
514 | * length - length of image data. Must be <= MAX_AVATAR_DATA_LENGTH. | ||
515 | * | ||
516 | * returns 0 on success | ||
517 | * returns -1 on failure. | ||
518 | */ | ||
519 | int m_set_avatar(Messenger *m, uint8_t format, const uint8_t *data, uint32_t length); | ||
520 | |||
521 | /* Get avatar data from the current user. | ||
522 | * Copies the current user avatar data to the destination buffer and sets the image format | ||
523 | * accordingly. | ||
524 | * | ||
525 | * If the avatar format is NONE, the buffer 'buf' isleft uninitialized, 'hash' is zeroed, and | ||
526 | * 'length' is set to zero. | ||
527 | * | ||
528 | * If any of the pointers format, buf, length, and hash are NULL, that particular field will be ignored. | ||
529 | * | ||
530 | * Arguments: | ||
531 | * format - destination pointer to the avatar image format (see AVATARFORMAT); | ||
532 | * buf - destination buffer to the image data. Must have at least 'maxlen' bytes; | ||
533 | * length - destination pointer to the image data length; | ||
534 | * maxlen - length of the destination buffer 'buf'; | ||
535 | * hash - destination pointer to the avatar hash (it must be exactly AVATAR_HASH_LENGTH bytes long). | ||
536 | * | ||
537 | * returns 0 on success; | ||
538 | * returns -1 on failure. | ||
539 | * | ||
540 | */ | ||
541 | int m_get_self_avatar(const Messenger *m, uint8_t *format, uint8_t *buf, uint32_t *length, uint32_t maxlen, | ||
542 | uint8_t *hash); | ||
543 | |||
544 | /* Generates a cryptographic hash of the given avatar data. | ||
545 | * This function is a wrapper to internal message-digest functions and specifically provided | ||
546 | * to generate hashes from user avatars that may be memcmp()ed with the values returned by the | ||
547 | * other avatar functions. It is specially important to validate cached avatars. | ||
548 | * | ||
549 | * Arguments: | ||
550 | * hash - destination buffer for the hash data, it must be exactly AVATAR_HASH_LENGTH bytes long. | ||
551 | * data - avatar image data; | ||
552 | * datalen - length of the avatar image data; it must be <= MAX_AVATAR_DATA_LENGTH. | ||
553 | * | ||
554 | * returns 0 on success | ||
555 | * returns -1 on failure. | ||
556 | */ | ||
557 | int m_avatar_hash(uint8_t *hash, const uint8_t *data, const uint32_t datalen); | ||
558 | |||
559 | /* Request avatar information from a friend. | ||
560 | * Asks a friend to provide their avatar information (image format and hash). The friend may | ||
561 | * or may not answer this request and, if answered, the information will be provided through | ||
562 | * the callback 'avatar_info'. | ||
563 | * | ||
564 | * returns 0 on success | ||
565 | * returns -1 on failure. | ||
566 | */ | ||
567 | int m_request_avatar_info(const Messenger *m, const int32_t friendnumber); | ||
568 | |||
569 | /* Send an unrequested avatar information to a friend. | ||
570 | * Sends our avatar format and hash to a friend; he/she can use this information to validate | ||
571 | * an avatar from the cache and may (or not) reply with an avatar data request. | ||
572 | * | ||
573 | * Notice: it is NOT necessary to send these notification after changing the avatar or | ||
574 | * connecting. The library already does this. | ||
575 | * | ||
576 | * returns 0 on success | ||
577 | * returns -1 on failure. | ||
578 | */ | ||
579 | int m_send_avatar_info(const Messenger *m, const int32_t friendnumber); | ||
580 | |||
581 | |||
582 | /* Request the avatar data from a friend. | ||
583 | * Ask a friend to send their avatar data. The friend may or may not answer this request and, | ||
584 | * if answered, the information will be provided in callback 'avatar_data'. | ||
585 | * | ||
586 | * returns 0 on sucess | ||
587 | * returns -1 on failure. | ||
588 | */ | ||
589 | int m_request_avatar_data(const Messenger *m, const int32_t friendnumber); | ||
590 | |||
591 | |||
440 | /* returns timestamp of last time friendnumber was seen online, or 0 if never seen. | 592 | /* returns timestamp of last time friendnumber was seen online, or 0 if never seen. |
441 | * returns -1 on error. | 593 | * returns -1 on error. |
442 | */ | 594 | */ |
@@ -533,6 +685,49 @@ void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, in | |||
533 | void m_callback_connectionstatus_internal_av(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, void *), | 685 | void m_callback_connectionstatus_internal_av(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, void *), |
534 | void *userdata); | 686 | void *userdata); |
535 | 687 | ||
688 | |||
689 | /* Set the callback function for avatar information. | ||
690 | * This callback will be called when avatar information are received from friends. These events | ||
691 | * can arrive at anytime, but are usually received uppon connection and in reply of avatar | ||
692 | * information requests. | ||
693 | * | ||
694 | * Function format is: | ||
695 | * function(Tox *tox, int32_t friendnumber, uint8_t format, uint8_t *hash, void *userdata) | ||
696 | * | ||
697 | * where 'format' is the avatar image format (see AVATARFORMAT) and 'hash' is the hash of | ||
698 | * the avatar data for caching purposes and it is exactly AVATAR_HASH_LENGTH long. If the | ||
699 | * image format is NONE, the hash is zeroed. | ||
700 | * | ||
701 | */ | ||
702 | void m_callback_avatar_info(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint8_t *, void *), | ||
703 | void *userdata); | ||
704 | |||
705 | |||
706 | /* Set the callback function for avatar data. | ||
707 | * This callback will be called when the complete avatar data was correctly received from a | ||
708 | * friend. This only happens in reply of a avatar data request (see tox_request_avatar_data); | ||
709 | * | ||
710 | * Function format is: | ||
711 | * function(Tox *tox, int32_t friendnumber, uint8_t format, uint8_t *hash, uint8_t *data, uint32_t datalen, void *userdata) | ||
712 | * | ||
713 | * where 'format' is the avatar image format (see AVATARFORMAT); 'hash' is the | ||
714 | * locally-calculated cryptographic hash of the avatar data and it is exactly | ||
715 | * AVATAR_HASH_LENGTH long; 'data' is the avatar image data and 'datalen' is the length | ||
716 | * of such data. | ||
717 | * | ||
718 | * If format is NONE, 'data' is NULL, 'datalen' is zero, and the hash is zeroed. The hash is | ||
719 | * always validated locally with the function tox_avatar_hash and ensured to match the image | ||
720 | * data, so this value can be safely used to compare with cached avatars. | ||
721 | * | ||
722 | * WARNING: users MUST treat all avatar image data received from another peer as untrusted and | ||
723 | * potentially malicious. The library only ensures that the data which arrived is the same the | ||
724 | * other user sent, and does not interpret or validate any image data. | ||
725 | */ | ||
726 | void m_callback_avatar_data(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint8_t *, uint8_t *, | ||
727 | uint32_t, void *), void *userdata); | ||
728 | |||
729 | |||
730 | |||
536 | /**********GROUP CHATS************/ | 731 | /**********GROUP CHATS************/ |
537 | 732 | ||
538 | /* Set the callback for group invites. | 733 | /* Set the callback for group invites. |