diff options
Diffstat (limited to 'core/Messenger.c')
-rw-r--r-- | core/Messenger.c | 174 |
1 files changed, 154 insertions, 20 deletions
diff --git a/core/Messenger.c b/core/Messenger.c index 7cfbb8ca..1107b60f 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -10,71 +10,203 @@ | |||
10 | typedef struct | 10 | typedef struct |
11 | { | 11 | { |
12 | uint8_t client_id[CLIENT_ID_SIZE]; | 12 | uint8_t client_id[CLIENT_ID_SIZE]; |
13 | 13 | int crypt_connection_id; | |
14 | int friend_request_id; //id of the friend request corresponding to the current friend request to the current friend. | ||
15 | uint8_t status;//0 if no friend, 1 if added, 2 if friend request successfully sent, 3 if confirmed friend, 4 if online. | ||
14 | 16 | ||
15 | }Friend; | 17 | }Friend; |
16 | 18 | ||
19 | |||
20 | uint8_t info[MAX_DATA_SIZE]; //the data that is sent during the friend requests we do | ||
21 | |||
22 | uint16_t info_size; //length of the info | ||
23 | |||
17 | #define MAX_NUM_FRIENDS 256 | 24 | #define MAX_NUM_FRIENDS 256 |
18 | 25 | ||
19 | Friend friendlist[MAX_NUM_FRIENDS]; | 26 | Friend friendlist[MAX_NUM_FRIENDS]; |
27 | |||
28 | #define MAX_MESSAGE_LENGTH 256 | ||
29 | |||
30 | uint32_t numfriends; | ||
20 | 31 | ||
21 | //add a friend | 32 | //add a friend |
33 | //client_id is the client i of the friend | ||
22 | //returns the friend number if success | 34 | //returns the friend number if success |
23 | //return -1 if failure. | 35 | //return -1 if failure. |
24 | int m_addfriend(uint8_t * client_id) | 36 | int m_addfriend(uint8_t * client_id) |
25 | { | 37 | { |
26 | 38 | ||
27 | //add friend to the DHT | 39 | DHT_addfriend(client_id); |
28 | addfriend(uint8_t * client_id); | 40 | friendlist[numfriends].status = 1; |
29 | 41 | friendlist[numfriends].friend_request_id = -1; | |
30 | send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, uint32_t length); | 42 | memcpy(friendlist[numfriends].client_id, client_id, CLIENT_ID_SIZE); |
43 | numfriends++; | ||
31 | 44 | ||
45 | return numfriends - 1; | ||
32 | } | 46 | } |
33 | 47 | ||
34 | //remove a friend | 48 | //remove a friend |
35 | int m_delfriend(int friendnumber) | 49 | int m_delfriend(int friendnumber) |
50 | {/* | ||
51 | TODO | ||
52 | DHT_delfriend(friendlist[friendnumber].client_id); | ||
53 | */ | ||
54 | } | ||
55 | |||
56 | |||
57 | //return 4 if friend is online | ||
58 | //return 3 if friend is confirmed | ||
59 | //return 2 if the friend request was sent successfully | ||
60 | //return 1 if the friend was added | ||
61 | //return 0 if there is no friend with that number. | ||
62 | int m_friendstatus(int friendnumber) | ||
36 | { | 63 | { |
37 | //delete friend from DHT | 64 | if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) |
38 | delfriend(uint8_t * client_id); | 65 | { |
39 | 66 | return 0; | |
67 | } | ||
68 | return friendlist[friendnumber].status; | ||
40 | } | 69 | } |
41 | 70 | ||
42 | 71 | ||
43 | //return 1 if friend is online | 72 | //send a text chat message to an online friend. |
44 | //return 0 if he is not | 73 | //returns 1 if packet was successfully put into the send queue |
45 | int m_friendonline(int friendnumber) | 74 | //return 0 if it was not. |
75 | int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length) | ||
46 | { | 76 | { |
47 | 77 | if(length >= MAX_DATA_SIZE) | |
78 | //this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. | ||
79 | { | ||
80 | return 0; | ||
81 | } | ||
82 | uint8_t temp[MAX_DATA_SIZE]; | ||
83 | temp[0] = 64; | ||
84 | memcpy(temp + 1, message, length); | ||
85 | return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1); | ||
48 | 86 | ||
49 | } | 87 | } |
50 | 88 | ||
89 | //set the data that will be sent along with friend requests | ||
90 | //return -1 if failure | ||
91 | //return 0 if success | ||
92 | int m_setinfo(uint8_t * data, uint16_t length) | ||
93 | { | ||
94 | if(length == 0 || length > MAX_DATA_SIZE - 1 - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES) | ||
95 | { | ||
96 | return -1; | ||
97 | } | ||
98 | memcpy(info, data, length); | ||
99 | info_size = length; | ||
100 | return 0; | ||
101 | } | ||
51 | 102 | ||
52 | //send a text chat message to a friend. | 103 | void (*friend_request)(uint8_t *, uint8_t *, uint16_t); |
53 | int m_sendmessage(int friendnumber) | 104 | |
105 | //set the function that will be executed when a friend request is received. | ||
106 | int m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) | ||
54 | { | 107 | { |
55 | write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length); | 108 | friend_request = function; |
56 | 109 | } | |
110 | |||
111 | |||
112 | void (*friend_message)(int, uint8_t *, uint16_t); | ||
113 | |||
114 | //set the function that will be executed when a message from a friend is received. | ||
115 | int m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)) | ||
116 | { | ||
117 | friend_message = function; | ||
57 | } | 118 | } |
58 | 119 | ||
59 | 120 | ||
60 | #define PORT 33445 | 121 | #define PORT 33445 |
61 | //run this at startup | 122 | //run this at startup |
62 | void initMessenger(); | 123 | void initMessenger() |
63 | { | 124 | { |
64 | new_keys(); | 125 | new_keys(); |
126 | initNetCrypto(); | ||
65 | IP ip; | 127 | IP ip; |
66 | ip.i = 0; | 128 | ip.i = 0; |
67 | init_networking(ip, PORT); | 129 | init_networking(ip, PORT); |
130 | memcpy(self_client_id, self_public_key, crypto_box_PUBLICKEYBYTES); | ||
68 | 131 | ||
69 | } | 132 | } |
70 | 133 | ||
134 | void doFriends() | ||
135 | {//TODO: add incoming connections and some other stuff. | ||
136 | uint32_t i; | ||
137 | int len; | ||
138 | uint8_t temp[MAX_DATA_SIZE]; | ||
139 | for(i = 0; i < numfriends; i++) | ||
140 | { | ||
141 | if(friendlist[i].status == 1) | ||
142 | { | ||
143 | IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); | ||
144 | int request = check_friendrequest(friendlist[i].friend_request_id); | ||
145 | //printf("\n%u %u %u\n", friendip.ip.i, request, friendlist[i].friend_request_id); | ||
146 | if(friendip.ip.i > 1 && request == -1) | ||
147 | { | ||
148 | friendlist[i].friend_request_id = send_friendrequest(friendlist[i].client_id, friendip, info, info_size); | ||
149 | } | ||
150 | if(request == 1) | ||
151 | { | ||
152 | friendlist[i].status = 2; | ||
153 | } | ||
154 | } | ||
155 | if(friendlist[i].status == 2 || friendlist[i].status == 3) | ||
156 | { | ||
157 | IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); | ||
158 | if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 0 && friendip.ip.i > 1) | ||
159 | { | ||
160 | friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); | ||
161 | } | ||
162 | if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 3)//if connection is established. | ||
163 | { | ||
164 | friendlist[i].status = 4; | ||
165 | } | ||
166 | } | ||
167 | while(friendlist[i].status == 4) | ||
168 | { | ||
169 | len = read_cryptpacket(friendlist[i].crypt_connection_id, temp); | ||
170 | if(len > 0) | ||
171 | { | ||
172 | if(temp[0] == 64) | ||
173 | { | ||
174 | (*friend_message)(i, temp, len); | ||
175 | } | ||
176 | } | ||
177 | else | ||
178 | { | ||
179 | if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 4)//if the connection timed out, kill it | ||
180 | { | ||
181 | crypto_kill(friendlist[i].crypt_connection_id); | ||
182 | friendlist[i].status = 3; | ||
183 | } | ||
184 | break; | ||
185 | } | ||
186 | } | ||
187 | } | ||
188 | } | ||
189 | |||
190 | void doFriendRequest() | ||
191 | { | ||
192 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | ||
193 | uint8_t temp[MAX_DATA_SIZE]; | ||
194 | |||
195 | int len = handle_friendrequest(public_key, temp); | ||
196 | if(len >= 0) | ||
197 | { | ||
198 | (*friend_request)(public_key, temp, len); | ||
199 | |||
200 | } | ||
201 | |||
202 | } | ||
71 | //the main loop that needs to be run at least 200 times per second. | 203 | //the main loop that needs to be run at least 200 times per second. |
72 | void doMessenger(); | 204 | void doMessenger() |
73 | { | 205 | { |
74 | IP_Port ip_port; | 206 | IP_Port ip_port; |
75 | uint8_t data[MAX_UDP_PACKET_SIZE]; | 207 | uint8_t data[MAX_UDP_PACKET_SIZE]; |
76 | uint32_t length; | 208 | uint32_t length; |
77 | while(recievepacket(&ip_port, data, &length) != -1) | 209 | while(receivepacket(&ip_port, data, &length) != -1) |
78 | { | 210 | { |
79 | //if(rand() % 3 != 1)//simulate packet loss | 211 | //if(rand() % 3 != 1)//simulate packet loss |
80 | //{ | 212 | //{ |
@@ -92,4 +224,6 @@ void doMessenger(); | |||
92 | doDHT(); | 224 | doDHT(); |
93 | doLossless_UDP(); | 225 | doLossless_UDP(); |
94 | doNetCrypto(); | 226 | doNetCrypto(); |
95 | } \ No newline at end of file | 227 | doFriendRequest(); |
228 | doFriends(); | ||
229 | } | ||