diff options
Diffstat (limited to 'core/friend_requests.c')
-rw-r--r-- | core/friend_requests.c | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/core/friend_requests.c b/core/friend_requests.c index 00beaa14..3708f154 100644 --- a/core/friend_requests.c +++ b/core/friend_requests.c | |||
@@ -31,16 +31,17 @@ uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | |||
31 | return -1 if failure. | 31 | return -1 if failure. |
32 | return 0 if it sent the friend request directly to the friend. | 32 | 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.*/ | 33 | 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) | 34 | int send_friendrequest(uint8_t *public_key, uint32_t nospam_num, uint8_t *data, uint32_t length) |
35 | { | 35 | { |
36 | if(length + sizeof(nospam_num) > MAX_DATA_SIZE) | 36 | if (length + sizeof(nospam_num) > MAX_DATA_SIZE) |
37 | return -1; | 37 | return -1; |
38 | 38 | ||
39 | uint8_t temp[MAX_DATA_SIZE]; | 39 | uint8_t temp[MAX_DATA_SIZE]; |
40 | memcpy(temp, &nospam_num, sizeof(nospam_num)); | 40 | memcpy(temp, &nospam_num, sizeof(nospam_num)); |
41 | memcpy(temp + sizeof(nospam_num), data, length); | 41 | memcpy(temp + sizeof(nospam_num), data, length); |
42 | uint8_t packet[MAX_DATA_SIZE]; | 42 | uint8_t packet[MAX_DATA_SIZE]; |
43 | int len = create_request(packet, public_key, temp, length + sizeof(nospam_num), 32); /* 32 is friend request packet id */ | 43 | int len = create_request(packet, public_key, temp, length + sizeof(nospam_num), |
44 | 32); /* 32 is friend request packet id */ | ||
44 | 45 | ||
45 | if (len == -1) | 46 | if (len == -1) |
46 | return -1; | 47 | return -1; |
@@ -53,6 +54,7 @@ int send_friendrequest(uint8_t * public_key, uint32_t nospam_num, uint8_t * data | |||
53 | if (ip_port.ip.i != 0) { | 54 | if (ip_port.ip.i != 0) { |
54 | if (sendpacket(ip_port, packet, len) != -1) | 55 | if (sendpacket(ip_port, packet, len) != -1) |
55 | return 0; | 56 | return 0; |
57 | |||
56 | return -1; | 58 | return -1; |
57 | } | 59 | } |
58 | 60 | ||
@@ -78,11 +80,11 @@ uint32_t get_nospam() | |||
78 | return nospam; | 80 | return nospam; |
79 | } | 81 | } |
80 | 82 | ||
81 | static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t, void*); | 83 | static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t, void *); |
82 | static uint8_t handle_friendrequest_isset = 0; | 84 | static uint8_t handle_friendrequest_isset = 0; |
83 | static void* handle_friendrequest_userdata; | 85 | static void *handle_friendrequest_userdata; |
84 | /* set the function that will be executed when a friend request is received. */ | 86 | /* set the function that will be executed when a friend request is received. */ |
85 | void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t, void*), void* userdata) | 87 | void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata) |
86 | { | 88 | { |
87 | handle_friendrequest = function; | 89 | handle_friendrequest = function; |
88 | handle_friendrequest_isset = 1; | 90 | handle_friendrequest_isset = 1; |
@@ -99,7 +101,7 @@ static uint8_t received_requests[MAX_RECEIVED_STORED][crypto_box_PUBLICKEYBYTES] | |||
99 | static uint16_t received_requests_index; | 101 | static uint16_t received_requests_index; |
100 | 102 | ||
101 | /*Add to list of received friend requests*/ | 103 | /*Add to list of received friend requests*/ |
102 | static void addto_receivedlist(uint8_t * client_id) | 104 | static void addto_receivedlist(uint8_t *client_id) |
103 | { | 105 | { |
104 | if (received_requests_index >= MAX_RECEIVED_STORED) | 106 | if (received_requests_index >= MAX_RECEIVED_STORED) |
105 | received_requests_index = 0; | 107 | received_requests_index = 0; |
@@ -110,7 +112,7 @@ static void addto_receivedlist(uint8_t * client_id) | |||
110 | 112 | ||
111 | /* Check if a friend request was already received | 113 | /* Check if a friend request was already received |
112 | return 0 if not, 1 if we did */ | 114 | return 0 if not, 1 if we did */ |
113 | static int request_received(uint8_t * client_id) | 115 | static int request_received(uint8_t *client_id) |
114 | { | 116 | { |
115 | uint32_t i; | 117 | uint32_t i; |
116 | 118 | ||
@@ -123,14 +125,17 @@ static int request_received(uint8_t * client_id) | |||
123 | } | 125 | } |
124 | 126 | ||
125 | 127 | ||
126 | static int friendreq_handlepacket(IP_Port source, uint8_t * source_pubkey, uint8_t * packet, uint32_t length) | 128 | static int friendreq_handlepacket(IP_Port source, uint8_t *source_pubkey, uint8_t *packet, uint32_t length) |
127 | { | 129 | { |
128 | if (handle_friendrequest_isset == 0) | 130 | if (handle_friendrequest_isset == 0) |
129 | return 1; | 131 | return 1; |
132 | |||
130 | if (length <= sizeof(nospam)) | 133 | if (length <= sizeof(nospam)) |
131 | return 1; | 134 | return 1; |
135 | |||
132 | if (request_received(source_pubkey)) | 136 | if (request_received(source_pubkey)) |
133 | return 1; | 137 | return 1; |
138 | |||
134 | if (memcmp(packet, &nospam, sizeof(nospam)) != 0) | 139 | if (memcmp(packet, &nospam, sizeof(nospam)) != 0) |
135 | return 1; | 140 | return 1; |
136 | 141 | ||