summaryrefslogtreecommitdiff
path: root/toxcore/friend_connection.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-10-04 16:49:17 -0400
committerirungentoo <irungentoo@gmail.com>2014-10-04 16:49:17 -0400
commit5555f01e279a1e514584a62e709c4ef1b8d57ecd (patch)
tree4f682b902ce248e8652db372e4a5b8001aef4954 /toxcore/friend_connection.c
parentcafbdd5d9443aa61094af99e609f369671466447 (diff)
Send friend request with connection if for some reason (groupchats) we
are already connected to the friend but they have not added us yet.
Diffstat (limited to 'toxcore/friend_connection.c')
-rw-r--r--toxcore/friend_connection.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c
index 7be4251a..7eb8e8c0 100644
--- a/toxcore/friend_connection.c
+++ b/toxcore/friend_connection.c
@@ -253,6 +253,13 @@ static int handle_packet(void *object, int number, uint8_t *data, uint16_t lengt
253 Friend_Connections *fr_c = object; 253 Friend_Connections *fr_c = object;
254 Friend_Conn *friend_con = get_conn(fr_c, number); 254 Friend_Conn *friend_con = get_conn(fr_c, number);
255 255
256 if (data[0] == PACKET_ID_FRIEND_REQUESTS) {
257 if (fr_c->fr_request_callback)
258 fr_c->fr_request_callback(fr_c->fr_request_object, friend_con->real_public_key, data, length);
259
260 return 0;
261 }
262
256 if (!friend_con) 263 if (!friend_con)
257 return -1; 264 return -1;
258 265
@@ -538,6 +545,53 @@ int kill_friend_connection(Friend_Connections *fr_c, int friendcon_id)
538} 545}
539 546
540 547
548/* Set friend request callback.
549 *
550 * This function will be called every time a friend request packet is received.
551 */
552void set_friend_request_callback(Friend_Connections *fr_c, int (*fr_request_callback)(void *, const uint8_t *,
553 const uint8_t *, uint32_t), void *object)
554{
555 fr_c->fr_request_callback = fr_request_callback;
556 fr_c->fr_request_object = object;
557 oniondata_registerhandler(fr_c->onion_c, CRYPTO_PACKET_FRIEND_REQ, fr_request_callback, object);
558}
559
560/* Send a Friend request packet.
561 *
562 * return -1 if failure.
563 * return 0 if it sent the friend request directly to the friend.
564 * return the number of peers it was routed through if it did not send it directly.
565 */
566int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint32_t nospam_num, const uint8_t *data,
567 uint16_t length)
568{
569 if (1 + sizeof(nospam_num) + length > ONION_CLIENT_MAX_DATA_SIZE || length == 0)
570 return -1;
571
572 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id);
573
574 if (!friend_con)
575 return -1;
576
577 uint8_t packet[1 + sizeof(nospam_num) + length];
578 memcpy(packet + 1, &nospam_num, sizeof(nospam_num));
579 memcpy(packet + 1 + sizeof(nospam_num), data, length);
580
581 if (friend_con->status == FRIENDCONN_STATUS_CONNECTED) {
582 packet[0] = PACKET_ID_FRIEND_REQUESTS;
583 return write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, packet, sizeof(packet), 0) != -1;
584 } else {
585 packet[0] = CRYPTO_PACKET_FRIEND_REQ;
586 int num = send_onion_data(fr_c->onion_c, friend_con->onion_friendnum, packet, sizeof(packet));
587
588 if (num <= 0)
589 return -1;
590
591 return num;
592 }
593}
594
541/* Create new friend_connections instance. */ 595/* Create new friend_connections instance. */
542Friend_Connections *new_friend_connections(Onion_Client *onion_c) 596Friend_Connections *new_friend_connections(Onion_Client *onion_c)
543{ 597{