diff options
Diffstat (limited to 'core/friend_requests.c')
-rw-r--r-- | core/friend_requests.c | 81 |
1 files changed, 36 insertions, 45 deletions
diff --git a/core/friend_requests.c b/core/friend_requests.c index 3708f154..ee2da633 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 | ||
26 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | ||
27 | |||
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.*/ |
34 | int send_friendrequest(uint8_t *public_key, uint32_t nospam_num, uint8_t *data, uint32_t length) | 31 | int 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,26 @@ 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(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, |
41 | length + sizeof(nospam_num), | ||
44 | 32); /* 32 is friend request packet id */ | 42 | 32); /* 32 is friend request packet id */ |
45 | 43 | ||
46 | if (len == -1) | 44 | if (len == -1) |
47 | return -1; | 45 | return -1; |
48 | 46 | ||
49 | IP_Port ip_port = DHT_getfriendip(public_key); | 47 | IP_Port ip_port = DHT_getfriendip(dht, public_key); |
50 | 48 | ||
51 | if (ip_port.ip.i == 1) | 49 | if (ip_port.ip.i == 1) |
52 | return -1; | 50 | return -1; |
53 | 51 | ||
54 | if (ip_port.ip.i != 0) { | 52 | if (ip_port.ip.i != 0) { |
55 | if (sendpacket(ip_port, packet, len) != -1) | 53 | if (sendpacket(dht->c->lossless_udp->net->sock, ip_port, packet, len) != -1) |
56 | return 0; | 54 | return 0; |
57 | 55 | ||
58 | return -1; | 56 | return -1; |
59 | } | 57 | } |
60 | 58 | ||
61 | int num = route_tofriend(public_key, packet, len); | 59 | int num = route_tofriend(dht, public_key, packet, len); |
62 | 60 | ||
63 | if (num == 0) | 61 | if (num == 0) |
64 | return -1; | 62 | return -1; |
@@ -66,58 +64,48 @@ int send_friendrequest(uint8_t *public_key, uint32_t nospam_num, uint8_t *data, | |||
66 | return num; | 64 | return num; |
67 | } | 65 | } |
68 | 66 | ||
69 | static uint32_t nospam; | 67 | |
70 | /* | 68 | /* |
71 | * Set and get the nospam variable used to prevent one type of friend request spam | 69 | * Set and get the nospam variable used to prevent one type of friend request spam |
72 | */ | 70 | */ |
73 | void set_nospam(uint32_t num) | 71 | void set_nospam(Friend_Requests *fr, uint32_t num) |
74 | { | 72 | { |
75 | nospam = num; | 73 | fr->nospam = num; |
76 | } | 74 | } |
77 | 75 | ||
78 | uint32_t get_nospam() | 76 | uint32_t get_nospam(Friend_Requests *fr) |
79 | { | 77 | { |
80 | return nospam; | 78 | return fr->nospam; |
81 | } | 79 | } |
82 | 80 | ||
83 | static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t, void *); | 81 | |
84 | static uint8_t handle_friendrequest_isset = 0; | ||
85 | static void *handle_friendrequest_userdata; | ||
86 | /* set the function that will be executed when a friend request is received. */ | 82 | /* set the function that will be executed when a friend request is received. */ |
87 | void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata) | 83 | void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), |
84 | void *userdata) | ||
88 | { | 85 | { |
89 | handle_friendrequest = function; | 86 | fr->handle_friendrequest = function; |
90 | handle_friendrequest_isset = 1; | 87 | fr->handle_friendrequest_isset = 1; |
91 | handle_friendrequest_userdata = userdata; | 88 | fr->handle_friendrequest_userdata = userdata; |
92 | } | 89 | } |
93 | 90 | ||
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 | |||
100 | static uint8_t received_requests[MAX_RECEIVED_STORED][crypto_box_PUBLICKEYBYTES]; | ||
101 | static uint16_t received_requests_index; | ||
102 | |||
103 | /*Add to list of received friend requests*/ | 91 | /*Add to list of received friend requests*/ |
104 | static void addto_receivedlist(uint8_t *client_id) | 92 | static void addto_receivedlist(Friend_Requests *fr, uint8_t *client_id) |
105 | { | 93 | { |
106 | if (received_requests_index >= MAX_RECEIVED_STORED) | 94 | if (fr->received_requests_index >= MAX_RECEIVED_STORED) |
107 | received_requests_index = 0; | 95 | fr->received_requests_index = 0; |
108 | 96 | ||
109 | memcpy(received_requests[received_requests_index], client_id, crypto_box_PUBLICKEYBYTES); | 97 | memcpy(fr->received_requests[fr->received_requests_index], client_id, crypto_box_PUBLICKEYBYTES); |
110 | ++received_requests_index; | 98 | ++fr->received_requests_index; |
111 | } | 99 | } |
112 | 100 | ||
113 | /* Check if a friend request was already received | 101 | /* Check if a friend request was already received |
114 | return 0 if not, 1 if we did */ | 102 | return 0 if not, 1 if we did */ |
115 | static int request_received(uint8_t *client_id) | 103 | static int request_received(Friend_Requests *fr, uint8_t *client_id) |
116 | { | 104 | { |
117 | uint32_t i; | 105 | uint32_t i; |
118 | 106 | ||
119 | for (i = 0; i < MAX_RECEIVED_STORED; ++i) { | 107 | for (i = 0; i < MAX_RECEIVED_STORED; ++i) { |
120 | if (memcmp(received_requests[i], client_id, crypto_box_PUBLICKEYBYTES) == 0) | 108 | if (memcmp(fr->received_requests[i], client_id, crypto_box_PUBLICKEYBYTES) == 0) |
121 | return 1; | 109 | return 1; |
122 | } | 110 | } |
123 | 111 | ||
@@ -125,26 +113,29 @@ static int request_received(uint8_t *client_id) | |||
125 | } | 113 | } |
126 | 114 | ||
127 | 115 | ||
128 | static int friendreq_handlepacket(IP_Port source, uint8_t *source_pubkey, uint8_t *packet, uint32_t length) | 116 | static int friendreq_handlepacket(void *object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet, |
117 | uint32_t length) | ||
129 | { | 118 | { |
130 | if (handle_friendrequest_isset == 0) | 119 | Friend_Requests *fr = object; |
120 | |||
121 | if (fr->handle_friendrequest_isset == 0) | ||
131 | return 1; | 122 | return 1; |
132 | 123 | ||
133 | if (length <= sizeof(nospam)) | 124 | if (length <= sizeof(fr->nospam)) |
134 | return 1; | 125 | return 1; |
135 | 126 | ||
136 | if (request_received(source_pubkey)) | 127 | if (request_received(fr, source_pubkey)) |
137 | return 1; | 128 | return 1; |
138 | 129 | ||
139 | if (memcmp(packet, &nospam, sizeof(nospam)) != 0) | 130 | if (memcmp(packet, &fr->nospam, sizeof(fr->nospam)) != 0) |
140 | return 1; | 131 | return 1; |
141 | 132 | ||
142 | addto_receivedlist(source_pubkey); | 133 | addto_receivedlist(fr, source_pubkey); |
143 | (*handle_friendrequest)(source_pubkey, packet + 4, length - 4, handle_friendrequest_userdata); | 134 | (*fr->handle_friendrequest)(source_pubkey, packet + 4, length - 4, fr->handle_friendrequest_userdata); |
144 | return 0; | 135 | return 0; |
145 | } | 136 | } |
146 | 137 | ||
147 | void friendreq_init(void) | 138 | void friendreq_init(Friend_Requests *fr, Net_Crypto *c) |
148 | { | 139 | { |
149 | cryptopacket_registerhandler(32, &friendreq_handlepacket); | 140 | cryptopacket_registerhandler(c, 32, &friendreq_handlepacket, fr); |
150 | } | 141 | } |