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.c32
1 files changed, 28 insertions, 4 deletions
diff --git a/core/friend_requests.c b/core/friend_requests.c
index 5e9b447c..8276db29 100644
--- a/core/friend_requests.c
+++ b/core/friend_requests.c
@@ -25,15 +25,22 @@
25 25
26uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; 26uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
27 27
28
28/* Try to send a friendrequest to peer with public_key 29/* Try to send a friendrequest to peer with public_key
29 data is the data in the request and length is the length. 30 data is the data in the request and length is the length.
30 return -1 if failure. 31 return -1 if failure.
31 return 0 if it sent the friend request directly to the friend. 32 return 0 if it sent the friend request directly to the friend.
32 return the number of peers it was routed through if it did not send it directly.*/ 33 return the number of peers it was routed through if it did not send it directly.*/
33int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length) 34int send_friendrequest(uint8_t * public_key, uint32_t nospam_num, uint8_t * data, uint32_t length)
34{ 35{
36 if(length - sizeof(nospam_num) > MAX_DATA_SIZE)
37 return -1;
38
39 uint8_t temp[MAX_DATA_SIZE];
40 memcpy(temp, &nospam_num, sizeof(nospam_num));
41 memcpy(temp + sizeof(nospam_num), data, length);
35 uint8_t packet[MAX_DATA_SIZE]; 42 uint8_t packet[MAX_DATA_SIZE];
36 int len = create_request(packet, public_key, data, length, 32); /* 32 is friend request packet id */ 43 int len = create_request(packet, public_key, temp, length + sizeof(nospam_num), 32); /* 32 is friend request packet id */
37 44
38 if (len == -1) 45 if (len == -1)
39 return -1; 46 return -1;
@@ -57,6 +64,20 @@ int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length)
57 return num; 64 return num;
58} 65}
59 66
67static uint32_t nospam;
68/*
69 * Set and get the nospam variable used to prevent one type of friend request spam
70 */
71void set_nospam(uint32_t num)
72{
73 nospam = num;
74}
75
76uint32_t get_nospam()
77{
78 return nospam;
79}
80
60static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t, void*); 81static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t, void*);
61static uint8_t handle_friendrequest_isset = 0; 82static uint8_t handle_friendrequest_isset = 0;
62static void* handle_friendrequest_userdata; 83static void* handle_friendrequest_userdata;
@@ -115,14 +136,17 @@ static int friendreq_handlepacket(IP_Port source, uint8_t * packet, uint32_t len
115 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 136 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
116 uint8_t data[MAX_DATA_SIZE]; 137 uint8_t data[MAX_DATA_SIZE];
117 int len = handle_request(public_key, data, packet, length); 138 int len = handle_request(public_key, data, packet, length);
118
119 if (len == -1) 139 if (len == -1)
120 return 1; 140 return 1;
141 if (len <= sizeof(nospam))
142 return 1;
121 if (request_received(public_key)) 143 if (request_received(public_key))
122 return 1; 144 return 1;
145 if (memcmp(data, &nospam, sizeof(nospam)) != 0)
146 return 1;
123 147
124 addto_receivedlist(public_key); 148 addto_receivedlist(public_key);
125 (*handle_friendrequest)(public_key, data, len, handle_friendrequest_userdata); 149 (*handle_friendrequest)(public_key, data + 4, len - 4, handle_friendrequest_userdata);
126 } else { /* if request is not for us, try routing it. */ 150 } else { /* if request is not for us, try routing it. */
127 if(route_packet(packet + 1, packet, length) == length) 151 if(route_packet(packet + 1, packet, length) == length)
128 return 0; 152 return 0;