diff options
author | irungentoo <irungentoo@gmail.com> | 2014-10-04 16:49:17 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2014-10-04 16:49:17 -0400 |
commit | 5555f01e279a1e514584a62e709c4ef1b8d57ecd (patch) | |
tree | 4f682b902ce248e8652db372e4a5b8001aef4954 | |
parent | cafbdd5d9443aa61094af99e609f369671466447 (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.
-rw-r--r-- | toxcore/Messenger.c | 8 | ||||
-rw-r--r-- | toxcore/friend_connection.c | 54 | ||||
-rw-r--r-- | toxcore/friend_connection.h | 19 | ||||
-rw-r--r-- | toxcore/friend_requests.c | 35 | ||||
-rw-r--r-- | toxcore/friend_requests.h | 10 | ||||
-rw-r--r-- | toxcore/group.c | 2 |
6 files changed, 82 insertions, 46 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index db5a3141..04d1a2f7 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -1625,7 +1625,7 @@ Messenger *new_messenger(Messenger_Options *options) | |||
1625 | } | 1625 | } |
1626 | 1626 | ||
1627 | m->options = *options; | 1627 | m->options = *options; |
1628 | friendreq_init(&(m->fr), m->onion_c); | 1628 | friendreq_init(&(m->fr), m->fr_c); |
1629 | LANdiscovery_init(m->dht); | 1629 | LANdiscovery_init(m->dht); |
1630 | set_nospam(&(m->fr), random_int()); | 1630 | set_nospam(&(m->fr), random_int()); |
1631 | set_filter_function(&(m->fr), &friend_already_added, m); | 1631 | set_filter_function(&(m->fr), &friend_already_added, m); |
@@ -2286,9 +2286,9 @@ void do_friends(Messenger *m) | |||
2286 | 2286 | ||
2287 | for (i = 0; i < m->numfriends; ++i) { | 2287 | for (i = 0; i < m->numfriends; ++i) { |
2288 | if (m->friendlist[i].status == FRIEND_ADDED) { | 2288 | if (m->friendlist[i].status == FRIEND_ADDED) { |
2289 | int fr = send_friendrequest(m->onion_c, m->friendlist[i].client_id, m->friendlist[i].friendrequest_nospam, | 2289 | int fr = send_friend_request_packet(m->fr_c, m->friendlist[i].friendcon_id, m->friendlist[i].friendrequest_nospam, |
2290 | m->friendlist[i].info, | 2290 | m->friendlist[i].info, |
2291 | m->friendlist[i].info_size); | 2291 | m->friendlist[i].info_size); |
2292 | 2292 | ||
2293 | if (fr >= 0) { | 2293 | if (fr >= 0) { |
2294 | set_friend_status(m, i, FRIEND_REQUESTED); | 2294 | set_friend_status(m, i, FRIEND_REQUESTED); |
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 | */ | ||
552 | void 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 | */ | ||
566 | int 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. */ |
542 | Friend_Connections *new_friend_connections(Onion_Client *onion_c) | 596 | Friend_Connections *new_friend_connections(Onion_Client *onion_c) |
543 | { | 597 | { |
diff --git a/toxcore/friend_connection.h b/toxcore/friend_connection.h index 64280acb..4f8cbf41 100644 --- a/toxcore/friend_connection.h +++ b/toxcore/friend_connection.h | |||
@@ -36,6 +36,7 @@ | |||
36 | #define GROUPCHAT_CALLBACK_INDEX 1 | 36 | #define GROUPCHAT_CALLBACK_INDEX 1 |
37 | 37 | ||
38 | #define PACKET_ID_ALIVE 16 | 38 | #define PACKET_ID_ALIVE 16 |
39 | #define PACKET_ID_FRIEND_REQUESTS 18 | ||
39 | 40 | ||
40 | /* Interval between the sending of ping packets. */ | 41 | /* Interval between the sending of ping packets. */ |
41 | #define FRIEND_PING_INTERVAL 6 | 42 | #define FRIEND_PING_INTERVAL 6 |
@@ -93,6 +94,8 @@ typedef struct { | |||
93 | Friend_Conn *conns; | 94 | Friend_Conn *conns; |
94 | uint32_t num_cons; | 95 | uint32_t num_cons; |
95 | 96 | ||
97 | int (*fr_request_callback)(void *object, const uint8_t *source_pubkey, const uint8_t *data, uint32_t len); | ||
98 | void *fr_request_object; | ||
96 | } Friend_Connections; | 99 | } Friend_Connections; |
97 | 100 | ||
98 | /* return friendcon_id corresponding to the real public key on success. | 101 | /* return friendcon_id corresponding to the real public key on success. |
@@ -157,6 +160,22 @@ int new_friend_connection(Friend_Connections *fr_c, const uint8_t *real_public_k | |||
157 | */ | 160 | */ |
158 | int kill_friend_connection(Friend_Connections *fr_c, int friendcon_id); | 161 | int kill_friend_connection(Friend_Connections *fr_c, int friendcon_id); |
159 | 162 | ||
163 | /* Send a Friend request packet. | ||
164 | * | ||
165 | * return -1 if failure. | ||
166 | * return 0 if it sent the friend request directly to the friend. | ||
167 | * return the number of peers it was routed through if it did not send it directly. | ||
168 | */ | ||
169 | int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint32_t nospam_num, const uint8_t *data, | ||
170 | uint16_t length); | ||
171 | |||
172 | /* Set friend request callback. | ||
173 | * | ||
174 | * This function will be called every time a friend request is received. | ||
175 | */ | ||
176 | void set_friend_request_callback(Friend_Connections *fr_c, int (*fr_request_callback)(void *, const uint8_t *, | ||
177 | const uint8_t *, uint32_t), void *object); | ||
178 | |||
160 | /* Create new friend_connections instance. */ | 179 | /* Create new friend_connections instance. */ |
161 | Friend_Connections *new_friend_connections(Onion_Client *onion_c); | 180 | Friend_Connections *new_friend_connections(Onion_Client *onion_c); |
162 | 181 | ||
diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c index a662b629..a7a1bcba 100644 --- a/toxcore/friend_requests.c +++ b/toxcore/friend_requests.c | |||
@@ -28,37 +28,6 @@ | |||
28 | #include "friend_requests.h" | 28 | #include "friend_requests.h" |
29 | #include "util.h" | 29 | #include "util.h" |
30 | 30 | ||
31 | /* Try to send a friend request to peer with public_key. | ||
32 | * data is the data in the request and length is the length. | ||
33 | * | ||
34 | * return -1 if failure. | ||
35 | * return 0 if it sent the friend request directly to the friend. | ||
36 | * return the number of peers it was routed through if it did not send it directly. | ||
37 | */ | ||
38 | int send_friendrequest(const Onion_Client *onion_c, const uint8_t *public_key, uint32_t nospam_num, const uint8_t *data, | ||
39 | uint32_t length) | ||
40 | { | ||
41 | if (1 + sizeof(nospam_num) + length > ONION_CLIENT_MAX_DATA_SIZE || length == 0) | ||
42 | return -1; | ||
43 | |||
44 | uint8_t temp[1 + sizeof(nospam_num) + length]; | ||
45 | temp[0] = CRYPTO_PACKET_FRIEND_REQ; | ||
46 | memcpy(temp + 1, &nospam_num, sizeof(nospam_num)); | ||
47 | memcpy(temp + 1 + sizeof(nospam_num), data, length); | ||
48 | |||
49 | int friend_num = onion_friend_num(onion_c, public_key); | ||
50 | |||
51 | if (friend_num == -1) | ||
52 | return -1; | ||
53 | |||
54 | int num = send_onion_data(onion_c, friend_num, temp, sizeof(temp)); | ||
55 | |||
56 | if (num <= 0) | ||
57 | return -1; | ||
58 | |||
59 | return num; | ||
60 | } | ||
61 | |||
62 | 31 | ||
63 | /* Set and get the nospam variable used to prevent one type of friend request spam. */ | 32 | /* Set and get the nospam variable used to prevent one type of friend request spam. */ |
64 | void set_nospam(Friend_Requests *fr, uint32_t num) | 33 | void set_nospam(Friend_Requests *fr, uint32_t num) |
@@ -169,7 +138,7 @@ static int friendreq_handlepacket(void *object, const uint8_t *source_pubkey, co | |||
169 | return 0; | 138 | return 0; |
170 | } | 139 | } |
171 | 140 | ||
172 | void friendreq_init(Friend_Requests *fr, Onion_Client *onion_c) | 141 | void friendreq_init(Friend_Requests *fr, Friend_Connections *fr_c) |
173 | { | 142 | { |
174 | oniondata_registerhandler(onion_c, CRYPTO_PACKET_FRIEND_REQ, &friendreq_handlepacket, fr); | 143 | set_friend_request_callback(fr_c, &friendreq_handlepacket, fr); |
175 | } | 144 | } |
diff --git a/toxcore/friend_requests.h b/toxcore/friend_requests.h index c3e31f36..b7e07af4 100644 --- a/toxcore/friend_requests.h +++ b/toxcore/friend_requests.h | |||
@@ -24,7 +24,7 @@ | |||
24 | #ifndef FRIEND_REQUESTS_H | 24 | #ifndef FRIEND_REQUESTS_H |
25 | #define FRIEND_REQUESTS_H | 25 | #define FRIEND_REQUESTS_H |
26 | 26 | ||
27 | #include "onion_client.h" | 27 | #include "friend_connection.h" |
28 | 28 | ||
29 | #define MAX_FRIEND_REQUEST_DATA_SIZE (ONION_CLIENT_MAX_DATA_SIZE - (1 + sizeof(uint32_t))) | 29 | #define MAX_FRIEND_REQUEST_DATA_SIZE (ONION_CLIENT_MAX_DATA_SIZE - (1 + sizeof(uint32_t))) |
30 | 30 | ||
@@ -47,12 +47,6 @@ typedef struct { | |||
47 | uint16_t received_requests_index; | 47 | uint16_t received_requests_index; |
48 | } Friend_Requests; | 48 | } Friend_Requests; |
49 | 49 | ||
50 | /* Try to send a friendrequest to peer with public_key. | ||
51 | * data is the data in the request and length is the length. | ||
52 | * Maximum length of data is MAX_FRIEND_REQUEST_DATA_SIZE. | ||
53 | */ | ||
54 | int send_friendrequest(const Onion_Client *onion_c, const uint8_t *public_key, uint32_t nospam_num, const uint8_t *data, | ||
55 | uint32_t length); | ||
56 | /* Set and get the nospam variable used to prevent one type of friend request spam. */ | 50 | /* Set and get the nospam variable used to prevent one type of friend request spam. */ |
57 | void set_nospam(Friend_Requests *fr, uint32_t num); | 51 | void set_nospam(Friend_Requests *fr, uint32_t num); |
58 | uint32_t get_nospam(const Friend_Requests *fr); | 52 | uint32_t get_nospam(const Friend_Requests *fr); |
@@ -77,7 +71,7 @@ void callback_friendrequest(Friend_Requests *fr, void (*function)(void *, const | |||
77 | void set_filter_function(Friend_Requests *fr, int (*function)(const uint8_t *, void *), void *userdata); | 71 | void set_filter_function(Friend_Requests *fr, int (*function)(const uint8_t *, void *), void *userdata); |
78 | 72 | ||
79 | /* Sets up friendreq packet handlers. */ | 73 | /* Sets up friendreq packet handlers. */ |
80 | void friendreq_init(Friend_Requests *fr, Onion_Client *onion_c); | 74 | void friendreq_init(Friend_Requests *fr, Friend_Connections *fr_c); |
81 | 75 | ||
82 | 76 | ||
83 | #endif | 77 | #endif |
diff --git a/toxcore/group.c b/toxcore/group.c index f83cd5a2..352c40f5 100644 --- a/toxcore/group.c +++ b/toxcore/group.c | |||
@@ -979,7 +979,7 @@ static void handle_friend_invite_packet(Messenger *m, int32_t friendnumber, cons | |||
979 | return; | 979 | return; |
980 | 980 | ||
981 | uint16_t peer_number = rand(); /* TODO: what if two people enter the group at the same time and | 981 | uint16_t peer_number = rand(); /* TODO: what if two people enter the group at the same time and |
982 | are given the same peer_number by different nodes? */ | 982 | are given the same peer_number by different nodes? */ |
983 | unsigned int tries = 0; | 983 | unsigned int tries = 0; |
984 | 984 | ||
985 | while (get_peer_index(g, peer_number) != -1) { | 985 | while (get_peer_index(g, peer_number) != -1) { |