summaryrefslogtreecommitdiff
path: root/core/friend_requests.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-07-22 14:52:42 -0400
committerirungentoo <irungentoo@gmail.com>2013-07-22 14:52:42 -0400
commit55361eac6f12fb2b2aaec427129aae65f5eb5e6b (patch)
tree41107d3aaa5edd4bc7e24951390ed676f00e8b2c /core/friend_requests.c
parentb368a6b4b898e2d2fa558931f724f2d204de6335 (diff)
Modified the way friend requests worked.
Added routing of friend requests.
Diffstat (limited to 'core/friend_requests.c')
-rw-r--r--core/friend_requests.c93
1 files changed, 93 insertions, 0 deletions
diff --git a/core/friend_requests.c b/core/friend_requests.c
new file mode 100644
index 00000000..18f0866b
--- /dev/null
+++ b/core/friend_requests.c
@@ -0,0 +1,93 @@
1/* friend_requests.c
2 *
3 * Handle friend requests.
4 *
5 */
6
7#include "friend_requests.h"
8
9uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
10
11
12/* Try to send a friendrequest to peer with public_key
13 data is the data in the request and length is the length.
14 return -1 if failure.
15 return 0 if it sent the friend request directly to the friend.
16 return the number of peers it was routed through if it did not send it directly.*/
17int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length)
18{
19 uint8_t packet[MAX_DATA_SIZE];
20 int len = create_request(packet, public_key, data, length, 32); /* 32 is friend request packet id */
21 if(len == -1)
22 {
23 return -1;
24 }
25 IP_Port ip_port = DHT_getfriendip(public_key);
26 if(ip_port.ip.i == 1)
27 {
28 return -1;
29 }
30 if(ip_port.ip.i != 0)
31 {
32 if(sendpacket(ip_port, packet, len) != -1)
33 {
34 return 0;
35 }
36 return -1;
37 }
38
39 int num = route_tofriend(public_key, packet, len);
40 if(num == 0)
41 {
42 return -1;
43 }
44 return num;
45}
46
47
48static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t);
49static uint8_t handle_friendrequest_isset = 0;
50
51/* set the function that will be executed when a friend request is received. */
52void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
53{
54 handle_friendrequest = function;
55 handle_friendrequest_isset = 1;
56}
57
58
59int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
60{
61
62 if(packet[0] == 32)
63 {
64 if(length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
65 length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
66 {
67 return 1;
68 }
69 if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)//check if request is for us.
70 {
71 if(handle_friendrequest_isset == 0)
72 {
73 return 1;
74 }
75 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
76 uint8_t data[MAX_DATA_SIZE];
77 int len = handle_request(public_key, data, packet, length);
78 if(len == -1)
79 {
80 return 1;
81 }
82 (*handle_friendrequest)(public_key, data, len);
83 }
84 else//if request is not for us, try routing it.
85 {
86 if(route_packet(packet + 1, packet, length) == length)
87 {
88 return 0;
89 }
90 }
91 }
92 return 1;
93} \ No newline at end of file