summaryrefslogtreecommitdiff
path: root/core/friend_requests.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/friend_requests.c')
-rw-r--r--core/friend_requests.c87
1 files changed, 64 insertions, 23 deletions
diff --git a/core/friend_requests.c b/core/friend_requests.c
index 18f0866b..d1b0da57 100644
--- a/core/friend_requests.c
+++ b/core/friend_requests.c
@@ -2,13 +2,29 @@
2 * 2 *
3 * Handle friend requests. 3 * Handle friend requests.
4 * 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 *
5 */ 22 */
6 23
7#include "friend_requests.h" 24#include "friend_requests.h"
8 25
9uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; 26uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
10 27
11
12/* Try to send a friendrequest to peer with public_key 28/* Try to send a friendrequest to peer with public_key
13 data is the data in the request and length is the length. 29 data is the data in the request and length is the length.
14 return -1 if failure. 30 return -1 if failure.
@@ -18,33 +34,30 @@ int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length)
18{ 34{
19 uint8_t packet[MAX_DATA_SIZE]; 35 uint8_t packet[MAX_DATA_SIZE];
20 int len = create_request(packet, public_key, data, length, 32); /* 32 is friend request packet id */ 36 int len = create_request(packet, public_key, data, length, 32); /* 32 is friend request packet id */
37
21 if(len == -1) 38 if(len == -1)
22 {
23 return -1; 39 return -1;
24 } 40
25 IP_Port ip_port = DHT_getfriendip(public_key); 41 IP_Port ip_port = DHT_getfriendip(public_key);
42
26 if(ip_port.ip.i == 1) 43 if(ip_port.ip.i == 1)
27 {
28 return -1; 44 return -1;
29 } 45
30 if(ip_port.ip.i != 0) 46 if(ip_port.ip.i != 0)
31 { 47 {
32 if(sendpacket(ip_port, packet, len) != -1) 48 if(sendpacket(ip_port, packet, len) != -1)
33 {
34 return 0; 49 return 0;
35 }
36 return -1; 50 return -1;
37 } 51 }
38 52
39 int num = route_tofriend(public_key, packet, len); 53 int num = route_tofriend(public_key, packet, len);
54
40 if(num == 0) 55 if(num == 0)
41 {
42 return -1; 56 return -1;
43 } 57
44 return num; 58 return num;
45} 59}
46 60
47
48static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t); 61static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t);
49static uint8_t handle_friendrequest_isset = 0; 62static uint8_t handle_friendrequest_isset = 0;
50 63
@@ -56,6 +69,38 @@ void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
56} 69}
57 70
58 71
72/*NOTE: the following is just a temporary fix for the multiple friend requests recieved at the same time problem
73 TODO: Make this better (This will most likely tie in with the way we will handle spam.)*/
74
75#define MAX_RECIEVED_STORED 32
76
77static uint8_t recieved_requests[MAX_RECIEVED_STORED][crypto_box_PUBLICKEYBYTES];
78static uint16_t recieved_requests_index;
79
80/*Add to list of recieved friend requests*/
81static void addto_recievedlist(uint8_t * client_id)
82{
83 if(recieved_requests_index >= MAX_RECIEVED_STORED)
84 recieved_requests_index = 0;
85
86 memcpy(recieved_requests[recieved_requests_index], client_id, crypto_box_PUBLICKEYBYTES);
87 ++recieved_requests_index;
88}
89
90/* Check if a friend request was already recieved
91 return 0 if not, 1 if we did */
92static int request_recieved(uint8_t * client_id)
93{
94 uint32_t i;
95
96 for(i = 0; i < MAX_RECIEVED_STORED; ++i)
97 if(memcmp(recieved_requests[i], client_id, crypto_box_PUBLICKEYBYTES) == 0)
98 return 1;
99
100 return 0;
101}
102
103
59int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) 104int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
60{ 105{
61 106
@@ -63,31 +108,27 @@ int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
63 { 108 {
64 if(length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && 109 if(length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
65 length > MAX_DATA_SIZE + ENCRYPTION_PADDING) 110 length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
66 {
67 return 1; 111 return 1;
68 } 112 if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) /* check if request is for us. */
69 if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)//check if request is for us.
70 { 113 {
71 if(handle_friendrequest_isset == 0) 114 if(handle_friendrequest_isset == 0)
72 {
73 return 1; 115 return 1;
74 } 116
75 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 117 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
76 uint8_t data[MAX_DATA_SIZE]; 118 uint8_t data[MAX_DATA_SIZE];
77 int len = handle_request(public_key, data, packet, length); 119 int len = handle_request(public_key, data, packet, length);
120
78 if(len == -1) 121 if(len == -1)
79 {
80 return 1; 122 return 1;
81 } 123 if(request_recieved(public_key))
124 return 1;
125
126 addto_recievedlist(public_key);
82 (*handle_friendrequest)(public_key, data, len); 127 (*handle_friendrequest)(public_key, data, len);
83 } 128 }
84 else//if request is not for us, try routing it. 129 else /* if request is not for us, try routing it. */
85 {
86 if(route_packet(packet + 1, packet, length) == length) 130 if(route_packet(packet + 1, packet, length) == length)
87 {
88 return 0; 131 return 0;
89 }
90 }
91 } 132 }
92 return 1; 133 return 1;
93} \ No newline at end of file 134}