summaryrefslogtreecommitdiff
path: root/core/friend_requests.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-08-20 12:08:55 -0400
committerirungentoo <irungentoo@gmail.com>2013-08-20 12:08:55 -0400
commita1c40d753ee8faf15aa0dd314bd4249aa3750b2e (patch)
treefb52c5cffcb897afa709e2d4a9882e89e9c90b5d /core/friend_requests.c
parent9f0efe920170472a4f757f7d8dc84f41e794f945 (diff)
More refactoring done.
Diffstat (limited to 'core/friend_requests.c')
-rw-r--r--core/friend_requests.c77
1 files changed, 32 insertions, 45 deletions
diff --git a/core/friend_requests.c b/core/friend_requests.c
index 7be7a4bd..b723de36 100644
--- a/core/friend_requests.c
+++ b/core/friend_requests.c
@@ -23,15 +23,12 @@
23 23
24#include "friend_requests.h" 24#include "friend_requests.h"
25 25
26uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
27uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
28
29/* Try to send a friendrequest to peer with public_key 26/* Try to send a friendrequest to peer with public_key
30 data is the data in the request and length is the length. 27 data is the data in the request and length is the length.
31 return -1 if failure. 28 return -1 if failure.
32 return 0 if it sent the friend request directly to the friend. 29 return 0 if it sent the friend request directly to the friend.
33 return the number of peers it was routed through if it did not send it directly.*/ 30 return the number of peers it was routed through if it did not send it directly.*/
34int send_friendrequest(uint8_t *public_key, uint32_t nospam_num, uint8_t *data, uint32_t length) 31int send_friendrequest(DHT *dht, uint8_t *public_key, uint32_t nospam_num, uint8_t *data, uint32_t length)
35{ 32{
36 if (length + sizeof(nospam_num) > MAX_DATA_SIZE) 33 if (length + sizeof(nospam_num) > MAX_DATA_SIZE)
37 return -1; 34 return -1;
@@ -40,25 +37,25 @@ int send_friendrequest(uint8_t *public_key, uint32_t nospam_num, uint8_t *data,
40 memcpy(temp, &nospam_num, sizeof(nospam_num)); 37 memcpy(temp, &nospam_num, sizeof(nospam_num));
41 memcpy(temp + sizeof(nospam_num), data, length); 38 memcpy(temp + sizeof(nospam_num), data, length);
42 uint8_t packet[MAX_DATA_SIZE]; 39 uint8_t packet[MAX_DATA_SIZE];
43 int len = create_request(self_public_key, self_secret_key, packet, public_key, temp, length + sizeof(nospam_num), 40 int len = create_request(dht->c->self_public_key, dht->c->self_secret_key, packet, public_key, temp, length + sizeof(nospam_num),
44 32); /* 32 is friend request packet id */ 41 32); /* 32 is friend request packet id */
45 42
46 if (len == -1) 43 if (len == -1)
47 return -1; 44 return -1;
48 45
49 IP_Port ip_port = DHT_getfriendip(public_key); 46 IP_Port ip_port = DHT_getfriendip(dht, public_key);
50 47
51 if (ip_port.ip.i == 1) 48 if (ip_port.ip.i == 1)
52 return -1; 49 return -1;
53 50
54 if (ip_port.ip.i != 0) { 51 if (ip_port.ip.i != 0) {
55 if (sendpacket(temp_net->sock, ip_port, packet, len) != -1) 52 if (sendpacket(dht->c->lossless_udp->net->sock, ip_port, packet, len) != -1)
56 return 0; 53 return 0;
57 54
58 return -1; 55 return -1;
59 } 56 }
60 57
61 int num = route_tofriend(public_key, packet, len); 58 int num = route_tofriend(dht, public_key, packet, len);
62 59
63 if (num == 0) 60 if (num == 0)
64 return -1; 61 return -1;
@@ -66,58 +63,47 @@ int send_friendrequest(uint8_t *public_key, uint32_t nospam_num, uint8_t *data,
66 return num; 63 return num;
67} 64}
68 65
69static uint32_t nospam; 66
70/* 67/*
71 * Set and get the nospam variable used to prevent one type of friend request spam 68 * Set and get the nospam variable used to prevent one type of friend request spam
72 */ 69 */
73void set_nospam(uint32_t num) 70void set_nospam(Friend_Requests *fr, uint32_t num)
74{ 71{
75 nospam = num; 72 fr->nospam = num;
76} 73}
77 74
78uint32_t get_nospam() 75uint32_t get_nospam(Friend_Requests *fr)
79{ 76{
80 return nospam; 77 return fr->nospam;
81} 78}
82 79
83static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t, void *); 80
84static uint8_t handle_friendrequest_isset = 0;
85static void *handle_friendrequest_userdata;
86/* set the function that will be executed when a friend request is received. */ 81/* set the function that will be executed when a friend request is received. */
87void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata) 82void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata)
88{ 83{
89 handle_friendrequest = function; 84 fr->handle_friendrequest = function;
90 handle_friendrequest_isset = 1; 85 fr->handle_friendrequest_isset = 1;
91 handle_friendrequest_userdata = userdata; 86 fr->handle_friendrequest_userdata = userdata;
92} 87}
93 88
94
95/*NOTE: the following is just a temporary fix for the multiple friend requests received at the same time problem
96 TODO: Make this better (This will most likely tie in with the way we will handle spam.)*/
97
98#define MAX_RECEIVED_STORED 32
99
100static uint8_t received_requests[MAX_RECEIVED_STORED][crypto_box_PUBLICKEYBYTES];
101static uint16_t received_requests_index;
102
103/*Add to list of received friend requests*/ 89/*Add to list of received friend requests*/
104static void addto_receivedlist(uint8_t *client_id) 90static void addto_receivedlist(Friend_Requests *fr, uint8_t *client_id)
105{ 91{
106 if (received_requests_index >= MAX_RECEIVED_STORED) 92 if (fr->received_requests_index >= MAX_RECEIVED_STORED)
107 received_requests_index = 0; 93 fr->received_requests_index = 0;
108 94
109 memcpy(received_requests[received_requests_index], client_id, crypto_box_PUBLICKEYBYTES); 95 memcpy(fr->received_requests[fr->received_requests_index], client_id, crypto_box_PUBLICKEYBYTES);
110 ++received_requests_index; 96 ++fr->received_requests_index;
111} 97}
112 98
113/* Check if a friend request was already received 99/* Check if a friend request was already received
114 return 0 if not, 1 if we did */ 100 return 0 if not, 1 if we did */
115static int request_received(uint8_t *client_id) 101static int request_received(Friend_Requests *fr, uint8_t *client_id)
116{ 102{
117 uint32_t i; 103 uint32_t i;
118 104
119 for (i = 0; i < MAX_RECEIVED_STORED; ++i) { 105 for (i = 0; i < MAX_RECEIVED_STORED; ++i) {
120 if (memcmp(received_requests[i], client_id, crypto_box_PUBLICKEYBYTES) == 0) 106 if (memcmp(fr->received_requests[i], client_id, crypto_box_PUBLICKEYBYTES) == 0)
121 return 1; 107 return 1;
122 } 108 }
123 109
@@ -125,26 +111,27 @@ static int request_received(uint8_t *client_id)
125} 111}
126 112
127 113
128static int friendreq_handlepacket(IP_Port source, uint8_t *source_pubkey, uint8_t *packet, uint32_t length) 114static int friendreq_handlepacket(void *object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet, uint32_t length)
129{ 115{
130 if (handle_friendrequest_isset == 0) 116 Friend_Requests *fr = object;
117 if (fr->handle_friendrequest_isset == 0)
131 return 1; 118 return 1;
132 119
133 if (length <= sizeof(nospam)) 120 if (length <= sizeof(fr->nospam))
134 return 1; 121 return 1;
135 122
136 if (request_received(source_pubkey)) 123 if (request_received(fr, source_pubkey))
137 return 1; 124 return 1;
138 125
139 if (memcmp(packet, &nospam, sizeof(nospam)) != 0) 126 if (memcmp(packet, &fr->nospam, sizeof(fr->nospam)) != 0)
140 return 1; 127 return 1;
141 128
142 addto_receivedlist(source_pubkey); 129 addto_receivedlist(fr, source_pubkey);
143 (*handle_friendrequest)(source_pubkey, packet + 4, length - 4, handle_friendrequest_userdata); 130 (*fr->handle_friendrequest)(source_pubkey, packet + 4, length - 4, fr->handle_friendrequest_userdata);
144 return 0; 131 return 0;
145} 132}
146 133
147void friendreq_init(void) 134void friendreq_init(Friend_Requests *fr, Net_Crypto *c)
148{ 135{
149 cryptopacket_registerhandler(temp_net_crypto, 32, &friendreq_handlepacket); 136 cryptopacket_registerhandler(c, 32, &friendreq_handlepacket, fr);
150} 137}