summaryrefslogtreecommitdiff
path: root/toxcore/friend_requests.c
diff options
context:
space:
mode:
authorjin-eld <jin at mediatomb dot cc>2013-08-04 15:10:37 +0300
committerjin-eld <jin at mediatomb dot cc>2013-08-24 03:25:07 +0300
commite658892793c42b2d058eed0937025ef2ddaaa372 (patch)
tree2a022cab057f2c16ca95860ed980092880052f6e /toxcore/friend_requests.c
parente2aa8161adc85795fe4d63d4642f47e90937ddc2 (diff)
Rename core directory because of autoconf name clash
While doing the checks configure might generate "core" files and will then try to remove them. Having a "core" directory generates an error while runing the configure script. There's no workaround but to rename the core directory.
Diffstat (limited to 'toxcore/friend_requests.c')
-rw-r--r--toxcore/friend_requests.c141
1 files changed, 141 insertions, 0 deletions
diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c
new file mode 100644
index 00000000..d8c6858b
--- /dev/null
+++ b/toxcore/friend_requests.c
@@ -0,0 +1,141 @@
1/* friend_requests.c
2 *
3 * Handle friend requests.
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 *
22 */
23
24#include "friend_requests.h"
25
26/* Try to send a friendrequest to peer with public_key
27 data is the data in the request and length is the length.
28 return -1 if failure.
29 return 0 if it sent the friend request directly to the friend.
30 return the number of peers it was routed through if it did not send it directly.*/
31int send_friendrequest(DHT *dht, uint8_t *public_key, uint32_t nospam_num, uint8_t *data, uint32_t length)
32{
33 if (length + sizeof(nospam_num) > MAX_DATA_SIZE)
34 return -1;
35
36 uint8_t temp[MAX_DATA_SIZE];
37 memcpy(temp, &nospam_num, sizeof(nospam_num));
38 memcpy(temp + sizeof(nospam_num), data, length);
39 uint8_t packet[MAX_DATA_SIZE];
40 int len = create_request(dht->c->self_public_key, dht->c->self_secret_key, packet, public_key, temp,
41 length + sizeof(nospam_num),
42 CRYPTO_PACKET_FRIEND_REQ);
43
44 if (len == -1)
45 return -1;
46
47 IP_Port ip_port = DHT_getfriendip(dht, public_key);
48
49 if (ip_port.ip.i == 1)
50 return -1;
51
52 if (ip_port.ip.i != 0) {
53 if (sendpacket(dht->c->lossless_udp->net->sock, ip_port, packet, len) != -1)
54 return 0;
55
56 return -1;
57 }
58
59 int num = route_tofriend(dht, public_key, packet, len);
60
61 if (num == 0)
62 return -1;
63
64 return num;
65}
66
67
68/*
69 * Set and get the nospam variable used to prevent one type of friend request spam
70 */
71void set_nospam(Friend_Requests *fr, uint32_t num)
72{
73 fr->nospam = num;
74}
75
76uint32_t get_nospam(Friend_Requests *fr)
77{
78 return fr->nospam;
79}
80
81
82/* set the function that will be executed when a friend request is received. */
83void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *),
84 void *userdata)
85{
86 fr->handle_friendrequest = function;
87 fr->handle_friendrequest_isset = 1;
88 fr->handle_friendrequest_userdata = userdata;
89}
90
91/*Add to list of received friend requests*/
92static void addto_receivedlist(Friend_Requests *fr, uint8_t *client_id)
93{
94 if (fr->received_requests_index >= MAX_RECEIVED_STORED)
95 fr->received_requests_index = 0;
96
97 memcpy(fr->received_requests[fr->received_requests_index], client_id, crypto_box_PUBLICKEYBYTES);
98 ++fr->received_requests_index;
99}
100
101/* Check if a friend request was already received
102 return 0 if not, 1 if we did */
103static int request_received(Friend_Requests *fr, uint8_t *client_id)
104{
105 uint32_t i;
106
107 for (i = 0; i < MAX_RECEIVED_STORED; ++i) {
108 if (memcmp(fr->received_requests[i], client_id, crypto_box_PUBLICKEYBYTES) == 0)
109 return 1;
110 }
111
112 return 0;
113}
114
115
116static int friendreq_handlepacket(void *object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet,
117 uint32_t length)
118{
119 Friend_Requests *fr = object;
120
121 if (fr->handle_friendrequest_isset == 0)
122 return 1;
123
124 if (length <= sizeof(fr->nospam))
125 return 1;
126
127 if (request_received(fr, source_pubkey))
128 return 1;
129
130 if (memcmp(packet, &fr->nospam, sizeof(fr->nospam)) != 0)
131 return 1;
132
133 addto_receivedlist(fr, source_pubkey);
134 (*fr->handle_friendrequest)(source_pubkey, packet + 4, length - 4, fr->handle_friendrequest_userdata);
135 return 0;
136}
137
138void friendreq_init(Friend_Requests *fr, Net_Crypto *c)
139{
140 cryptopacket_registerhandler(c, CRYPTO_PACKET_FRIEND_REQ, &friendreq_handlepacket, fr);
141}