diff options
author | irungentoo <irungentoo@gmail.com> | 2013-07-27 08:26:00 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-07-27 08:26:00 -0400 |
commit | 1a6446266c9727e5c95e18d5e44157fd5a60900f (patch) | |
tree | 6dafbf52b7952f68a95ac9317871e923db6cc565 /core/friend_requests.c | |
parent | 45b5ef0b79239167d9bc8fe52b25b36b684d2687 (diff) | |
parent | 241aca98bdc8106221ee7d7dbcea9f2fa17f24bc (diff) |
Merge branch 'master' of https://github.com/KostyaKow/ProjectTox-Core into KostyaKow-master
Conflicts:
core/DHT.c
Diffstat (limited to 'core/friend_requests.c')
-rw-r--r-- | core/friend_requests.c | 50 |
1 files changed, 22 insertions, 28 deletions
diff --git a/core/friend_requests.c b/core/friend_requests.c index d1b0da57..7a2cdc24 100644 --- a/core/friend_requests.c +++ b/core/friend_requests.c | |||
@@ -35,24 +35,23 @@ int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length) | |||
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 | ||
38 | if(len == -1) | 38 | if (len == -1) |
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 | ||
43 | if(ip_port.ip.i == 1) | 43 | if (ip_port.ip.i == 1) |
44 | return -1; | 44 | return -1; |
45 | 45 | ||
46 | if(ip_port.ip.i != 0) | 46 | if (ip_port.ip.i != 0) { |
47 | { | 47 | if (sendpacket(ip_port, packet, len) != -1) |
48 | if(sendpacket(ip_port, packet, len) != -1) | ||
49 | return 0; | 48 | return 0; |
50 | return -1; | 49 | return -1; |
51 | } | 50 | } |
52 | 51 | ||
53 | int num = route_tofriend(public_key, packet, len); | 52 | int num = route_tofriend(public_key, packet, len); |
54 | 53 | ||
55 | if(num == 0) | 54 | if (num == 0) |
56 | return -1; | 55 | return -1; |
57 | 56 | ||
58 | return num; | 57 | return num; |
@@ -62,8 +61,7 @@ static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t); | |||
62 | static uint8_t handle_friendrequest_isset = 0; | 61 | static uint8_t handle_friendrequest_isset = 0; |
63 | 62 | ||
64 | /* set the function that will be executed when a friend request is received. */ | 63 | /* set the function that will be executed when a friend request is received. */ |
65 | void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) | 64 | void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) { |
66 | { | ||
67 | handle_friendrequest = function; | 65 | handle_friendrequest = function; |
68 | handle_friendrequest_isset = 1; | 66 | handle_friendrequest_isset = 1; |
69 | } | 67 | } |
@@ -78,9 +76,8 @@ static uint8_t recieved_requests[MAX_RECIEVED_STORED][crypto_box_PUBLICKEYBYTES] | |||
78 | static uint16_t recieved_requests_index; | 76 | static uint16_t recieved_requests_index; |
79 | 77 | ||
80 | /*Add to list of recieved friend requests*/ | 78 | /*Add to list of recieved friend requests*/ |
81 | static void addto_recievedlist(uint8_t * client_id) | 79 | static void addto_recievedlist(uint8_t * client_id) { |
82 | { | 80 | if (recieved_requests_index >= MAX_RECIEVED_STORED) |
83 | if(recieved_requests_index >= MAX_RECIEVED_STORED) | ||
84 | recieved_requests_index = 0; | 81 | recieved_requests_index = 0; |
85 | 82 | ||
86 | memcpy(recieved_requests[recieved_requests_index], client_id, crypto_box_PUBLICKEYBYTES); | 83 | memcpy(recieved_requests[recieved_requests_index], client_id, crypto_box_PUBLICKEYBYTES); |
@@ -89,46 +86,43 @@ static void addto_recievedlist(uint8_t * client_id) | |||
89 | 86 | ||
90 | /* Check if a friend request was already recieved | 87 | /* Check if a friend request was already recieved |
91 | return 0 if not, 1 if we did */ | 88 | return 0 if not, 1 if we did */ |
92 | static int request_recieved(uint8_t * client_id) | 89 | static int request_recieved(uint8_t * client_id) { |
93 | { | ||
94 | uint32_t i; | 90 | uint32_t i; |
95 | 91 | ||
96 | for(i = 0; i < MAX_RECIEVED_STORED; ++i) | 92 | for (i = 0; i < MAX_RECIEVED_STORED; ++i) { |
97 | if(memcmp(recieved_requests[i], client_id, crypto_box_PUBLICKEYBYTES) == 0) | 93 | if (memcmp(recieved_requests[i], client_id, crypto_box_PUBLICKEYBYTES) == 0) |
98 | return 1; | 94 | return 1; |
95 | } | ||
99 | 96 | ||
100 | return 0; | 97 | return 0; |
101 | } | 98 | } |
102 | 99 | ||
103 | 100 | ||
104 | int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | 101 | int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) { |
105 | { | 102 | if (packet[0] == 32) { |
106 | 103 | if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && | |
107 | if(packet[0] == 32) | 104 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) |
108 | { | ||
109 | if(length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && | ||
110 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) | ||
111 | return 1; | 105 | return 1; |
112 | if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) /* check if request is for us. */ | 106 | if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {// check if request is for us. |
113 | { | 107 | if (handle_friendrequest_isset == 0) |
114 | if(handle_friendrequest_isset == 0) | ||
115 | return 1; | 108 | return 1; |
116 | 109 | ||
117 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 110 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
118 | uint8_t data[MAX_DATA_SIZE]; | 111 | uint8_t data[MAX_DATA_SIZE]; |
119 | int len = handle_request(public_key, data, packet, length); | 112 | int len = handle_request(public_key, data, packet, length); |
120 | 113 | ||
121 | if(len == -1) | 114 | if (len == -1) |
122 | return 1; | 115 | return 1; |
123 | if(request_recieved(public_key)) | 116 | if (request_recieved(public_key)) |
124 | return 1; | 117 | return 1; |
125 | 118 | ||
126 | addto_recievedlist(public_key); | 119 | addto_recievedlist(public_key); |
127 | (*handle_friendrequest)(public_key, data, len); | 120 | (*handle_friendrequest)(public_key, data, len); |
128 | } | 121 | } |
129 | else /* if request is not for us, try routing it. */ | 122 | else {/* if request is not for us, try routing it. */ |
130 | if(route_packet(packet + 1, packet, length) == length) | 123 | if(route_packet(packet + 1, packet, length) == length) |
131 | return 0; | 124 | return 0; |
125 | } | ||
132 | } | 126 | } |
133 | return 1; | 127 | return 1; |
134 | } | 128 | } |