summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/friend_requests.c43
1 files changed, 14 insertions, 29 deletions
diff --git a/core/friend_requests.c b/core/friend_requests.c
index a524797f..d1b0da57 100644
--- a/core/friend_requests.c
+++ b/core/friend_requests.c
@@ -34,29 +34,27 @@ int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length)
34{ 34{
35 uint8_t packet[MAX_DATA_SIZE]; 35 uint8_t packet[MAX_DATA_SIZE];
36 int len = create_request(packet, public_key, data, length, 32); /* 32 is friend request packet id */ 36 int len = create_request(packet, public_key, data, length, 32); /* 32 is friend request packet id */
37
37 if(len == -1) 38 if(len == -1)
38 {
39 return -1; 39 return -1;
40 } 40
41 IP_Port ip_port = DHT_getfriendip(public_key); 41 IP_Port ip_port = DHT_getfriendip(public_key);
42
42 if(ip_port.ip.i == 1) 43 if(ip_port.ip.i == 1)
43 {
44 return -1; 44 return -1;
45 } 45
46 if(ip_port.ip.i != 0) 46 if(ip_port.ip.i != 0)
47 { 47 {
48 if(sendpacket(ip_port, packet, len) != -1) 48 if(sendpacket(ip_port, packet, len) != -1)
49 {
50 return 0; 49 return 0;
51 }
52 return -1; 50 return -1;
53 } 51 }
54 52
55 int num = route_tofriend(public_key, packet, len); 53 int num = route_tofriend(public_key, packet, len);
54
56 if(num == 0) 55 if(num == 0)
57 {
58 return -1; 56 return -1;
59 } 57
60 return num; 58 return num;
61} 59}
62 60
@@ -83,9 +81,7 @@ static uint16_t recieved_requests_index;
83static void addto_recievedlist(uint8_t * client_id) 81static void addto_recievedlist(uint8_t * client_id)
84{ 82{
85 if(recieved_requests_index >= MAX_RECIEVED_STORED) 83 if(recieved_requests_index >= MAX_RECIEVED_STORED)
86 {
87 recieved_requests_index = 0; 84 recieved_requests_index = 0;
88 }
89 85
90 memcpy(recieved_requests[recieved_requests_index], client_id, crypto_box_PUBLICKEYBYTES); 86 memcpy(recieved_requests[recieved_requests_index], client_id, crypto_box_PUBLICKEYBYTES);
91 ++recieved_requests_index; 87 ++recieved_requests_index;
@@ -96,13 +92,11 @@ static void addto_recievedlist(uint8_t * client_id)
96static int request_recieved(uint8_t * client_id) 92static int request_recieved(uint8_t * client_id)
97{ 93{
98 uint32_t i; 94 uint32_t i;
95
99 for(i = 0; i < MAX_RECIEVED_STORED; ++i) 96 for(i = 0; i < MAX_RECIEVED_STORED; ++i)
100 {
101 if(memcmp(recieved_requests[i], client_id, crypto_box_PUBLICKEYBYTES) == 0) 97 if(memcmp(recieved_requests[i], client_id, crypto_box_PUBLICKEYBYTES) == 0)
102 {
103 return 1; 98 return 1;
104 } 99
105 }
106 return 0; 100 return 0;
107} 101}
108 102
@@ -114,36 +108,27 @@ int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
114 { 108 {
115 if(length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && 109 if(length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
116 length > MAX_DATA_SIZE + ENCRYPTION_PADDING) 110 length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
117 {
118 return 1; 111 return 1;
119 } 112 if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) /* check if request is for us. */
120 if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)//check if request is for us.
121 { 113 {
122 if(handle_friendrequest_isset == 0) 114 if(handle_friendrequest_isset == 0)
123 {
124 return 1; 115 return 1;
125 } 116
126 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 117 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
127 uint8_t data[MAX_DATA_SIZE]; 118 uint8_t data[MAX_DATA_SIZE];
128 int len = handle_request(public_key, data, packet, length); 119 int len = handle_request(public_key, data, packet, length);
120
129 if(len == -1) 121 if(len == -1)
130 {
131 return 1; 122 return 1;
132 }
133 if(request_recieved(public_key)) 123 if(request_recieved(public_key))
134 {
135 return 1; 124 return 1;
136 } 125
137 addto_recievedlist(public_key); 126 addto_recievedlist(public_key);
138 (*handle_friendrequest)(public_key, data, len); 127 (*handle_friendrequest)(public_key, data, len);
139 } 128 }
140 else//if request is not for us, try routing it. 129 else /* if request is not for us, try routing it. */
141 {
142 if(route_packet(packet + 1, packet, length) == length) 130 if(route_packet(packet + 1, packet, length) == length)
143 {
144 return 0; 131 return 0;
145 }
146 }
147 } 132 }
148 return 1; 133 return 1;
149} \ No newline at end of file 134}