/work/toxcore/friend_requests.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* SPDX-License-Identifier: GPL-3.0-or-later |
2 | | * Copyright © 2016-2018 The TokTok team. |
3 | | * Copyright © 2013 Tox project. |
4 | | */ |
5 | | |
6 | | /** |
7 | | * Handle friend requests. |
8 | | */ |
9 | | #include "friend_requests.h" |
10 | | |
11 | | #include <stdlib.h> |
12 | | #include <string.h> |
13 | | |
14 | | #include "ccompat.h" |
15 | | #include "crypto_core.h" |
16 | | #include "friend_connection.h" |
17 | | #include "onion_client.h" |
18 | | |
19 | | /** |
20 | | * NOTE: The following is just a temporary fix for the multiple friend requests received at the same time problem. |
21 | | * TODO(irungentoo): Make this better (This will most likely tie in with the way we will handle spam). |
22 | | */ |
23 | 13.5k | #define MAX_RECEIVED_STORED 32 |
24 | | |
25 | | struct Received_Requests { |
26 | | uint8_t requests[MAX_RECEIVED_STORED][CRYPTO_PUBLIC_KEY_SIZE]; |
27 | | uint16_t requests_index; |
28 | | }; |
29 | | |
30 | | struct Friend_Requests { |
31 | | uint32_t nospam; |
32 | | fr_friend_request_cb *handle_friendrequest; |
33 | | uint8_t handle_friendrequest_isset; |
34 | | void *handle_friendrequest_object; |
35 | | |
36 | | filter_function_cb *filter_function; |
37 | | void *filter_function_userdata; |
38 | | |
39 | | struct Received_Requests received; |
40 | | }; |
41 | | |
42 | | /** Set and get the nospam variable used to prevent one type of friend request spam. */ |
43 | | void set_nospam(Friend_Requests *fr, uint32_t num) |
44 | 4.12k | { |
45 | 4.12k | fr->nospam = num; |
46 | 4.12k | } |
47 | | |
48 | | uint32_t get_nospam(const Friend_Requests *fr) |
49 | 1.07k | { |
50 | 1.07k | return fr->nospam; |
51 | 1.07k | } |
52 | | |
53 | | |
54 | | /** Set the function that will be executed when a friend request for us is received. */ |
55 | | void callback_friendrequest(Friend_Requests *fr, fr_friend_request_cb *function, void *object) |
56 | 3.66k | { |
57 | 3.66k | fr->handle_friendrequest = function; |
58 | 3.66k | fr->handle_friendrequest_isset = 1; |
59 | 3.66k | fr->handle_friendrequest_object = object; |
60 | 3.66k | } |
61 | | |
62 | | /** @brief Set the function used to check if a friend request should be displayed to the user or not. |
63 | | * It must return 0 if the request is ok (anything else if it is bad). |
64 | | */ |
65 | | void set_filter_function(Friend_Requests *fr, filter_function_cb *function, void *userdata) |
66 | 3.66k | { |
67 | 3.66k | fr->filter_function = function; |
68 | 3.66k | fr->filter_function_userdata = userdata; |
69 | 3.66k | } |
70 | | |
71 | | /** Add to list of received friend requests. */ |
72 | | non_null() |
73 | | static void addto_receivedlist(Friend_Requests *fr, const uint8_t *real_pk) |
74 | 179 | { |
75 | 179 | if (fr->received.requests_index >= MAX_RECEIVED_STORED) { |
76 | 0 | fr->received.requests_index = 0; |
77 | 0 | } |
78 | | |
79 | 179 | pk_copy(fr->received.requests[fr->received.requests_index], real_pk); |
80 | 179 | ++fr->received.requests_index; |
81 | 179 | } |
82 | | |
83 | | /** @brief Check if a friend request was already received. |
84 | | * |
85 | | * @retval false if it did not. |
86 | | * @retval true if it did. |
87 | | */ |
88 | | non_null() |
89 | | static bool request_received(const Friend_Requests *fr, const uint8_t *real_pk) |
90 | 351 | { |
91 | 8.07k | for (uint32_t i = 0; i < MAX_RECEIVED_STORED; ++i) { |
92 | 7.87k | if (pk_equal(fr->received.requests[i], real_pk)) { |
93 | 152 | return true; |
94 | 152 | } |
95 | 7.87k | } |
96 | | |
97 | 199 | return false; |
98 | 351 | } |
99 | | |
100 | | /** @brief Remove real_pk from received_requests list. |
101 | | * |
102 | | * @retval 0 if it removed it successfully. |
103 | | * @retval -1 if it didn't find it. |
104 | | */ |
105 | | int remove_request_received(Friend_Requests *fr, const uint8_t *real_pk) |
106 | 159 | { |
107 | 5.24k | for (uint32_t i = 0; i < MAX_RECEIVED_STORED; ++i) { |
108 | 5.08k | if (pk_equal(fr->received.requests[i], real_pk)) { |
109 | 0 | crypto_memzero(fr->received.requests[i], CRYPTO_PUBLIC_KEY_SIZE); |
110 | 0 | return 0; |
111 | 0 | } |
112 | 5.08k | } |
113 | | |
114 | 159 | return -1; |
115 | 159 | } |
116 | | |
117 | | |
118 | | non_null() |
119 | | static int friendreq_handlepacket(void *object, const uint8_t *source_pubkey, const uint8_t *data, uint16_t length, |
120 | | void *userdata) |
121 | 352 | { |
122 | 352 | Friend_Requests *const fr = (Friend_Requests *)object; |
123 | | |
124 | 352 | if (length <= 1 + sizeof(fr->nospam) || length > ONION_CLIENT_MAX_DATA_SIZE) { |
125 | 1 | return 1; |
126 | 1 | } |
127 | | |
128 | 351 | ++data; |
129 | 351 | --length; |
130 | | |
131 | 351 | if (fr->handle_friendrequest_isset == 0) { |
132 | 0 | return 1; |
133 | 0 | } |
134 | | |
135 | 351 | if (request_received(fr, source_pubkey)) { |
136 | 152 | return 1; |
137 | 152 | } |
138 | | |
139 | 199 | if (memcmp(data, &fr->nospam, sizeof(fr->nospam)) != 0) { |
140 | 20 | return 1; |
141 | 20 | } |
142 | | |
143 | 179 | if (fr->filter_function != nullptr) { |
144 | 179 | if (fr->filter_function(fr->filter_function_userdata, source_pubkey) != 0) { |
145 | 0 | return 1; |
146 | 0 | } |
147 | 179 | } |
148 | | |
149 | 179 | addto_receivedlist(fr, source_pubkey); |
150 | | |
151 | 179 | const uint16_t message_len = length - sizeof(fr->nospam); |
152 | 179 | VLA(uint8_t, message, message_len + 1); |
153 | 179 | memcpy(message, data + sizeof(fr->nospam), message_len); |
154 | 179 | message[message_len] = 0; /* Be sure the message is null terminated. TODO(iphydf): But why? */ |
155 | | |
156 | 179 | fr->handle_friendrequest(fr->handle_friendrequest_object, source_pubkey, message, message_len, userdata); |
157 | 179 | return 0; |
158 | 179 | } |
159 | | |
160 | | void friendreq_init(Friend_Requests *fr, Friend_Connections *fr_c) |
161 | 3.66k | { |
162 | 3.66k | set_friend_request_callback(fr_c, &friendreq_handlepacket, fr); |
163 | 3.66k | } |
164 | | |
165 | | Friend_Requests *friendreq_new(void) |
166 | 4.17k | { |
167 | 4.17k | return (Friend_Requests *)calloc(1, sizeof(Friend_Requests)); |
168 | 4.17k | } |
169 | | |
170 | | void friendreq_kill(Friend_Requests *fr) |
171 | 3.11k | { |
172 | 3.11k | free(fr); |
173 | 3.11k | } |