diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/CMakeLists.txt | 14 | ||||
-rw-r--r-- | core/Lossless_UDP.c | 6 | ||||
-rw-r--r-- | core/Messenger.c | 31 | ||||
-rw-r--r-- | core/Messenger.h | 18 | ||||
-rw-r--r-- | core/network.c | 4 | ||||
-rw-r--r-- | core/network.h | 2 |
6 files changed, 51 insertions, 24 deletions
diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 6ddd5b9b..420308d8 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt | |||
@@ -6,12 +6,12 @@ if(WIN32) | |||
6 | endif() | 6 | endif() |
7 | 7 | ||
8 | set(core_sources | 8 | set(core_sources |
9 | DHT.c | 9 | DHT.c |
10 | network.c | 10 | network.c |
11 | Lossless_UDP.c | 11 | Lossless_UDP.c |
12 | net_crypto.c | 12 | net_crypto.c |
13 | friend_requests.c | 13 | friend_requests.c |
14 | LAN_discovery.c | 14 | LAN_discovery.c |
15 | Messenger.c) | 15 | Messenger.c) |
16 | 16 | ||
17 | add_library(core ${core_sources}) | 17 | add_library(core ${core_sources}) |
diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c index eb1314d1..6be8328f 100644 --- a/core/Lossless_UDP.c +++ b/core/Lossless_UDP.c | |||
@@ -308,12 +308,16 @@ IP_Port connection_ip(int connection_id) | |||
308 | /* returns the number of packets in the queue waiting to be successfully sent. */ | 308 | /* returns the number of packets in the queue waiting to be successfully sent. */ |
309 | uint32_t sendqueue(int connection_id) | 309 | uint32_t sendqueue(int connection_id) |
310 | { | 310 | { |
311 | if (connection_id < 0 || connection_id >= MAX_CONNECTIONS) | ||
312 | return 0; | ||
311 | return connections[connection_id].sendbuff_packetnum - connections[connection_id].successful_sent; | 313 | return connections[connection_id].sendbuff_packetnum - connections[connection_id].successful_sent; |
312 | } | 314 | } |
313 | 315 | ||
314 | /* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ | 316 | /* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ |
315 | uint32_t recvqueue(int connection_id) | 317 | uint32_t recvqueue(int connection_id) |
316 | { | 318 | { |
319 | if (connection_id < 0 || connection_id >= MAX_CONNECTIONS) | ||
320 | return 0; | ||
317 | return connections[connection_id].recv_packetnum - connections[connection_id].successful_read; | 321 | return connections[connection_id].recv_packetnum - connections[connection_id].successful_read; |
318 | } | 322 | } |
319 | 323 | ||
@@ -321,6 +325,8 @@ uint32_t recvqueue(int connection_id) | |||
321 | return -1 if no packet in queue */ | 325 | return -1 if no packet in queue */ |
322 | char id_packet(int connection_id) | 326 | char id_packet(int connection_id) |
323 | { | 327 | { |
328 | if (connection_id < 0 || connection_id >= MAX_CONNECTIONS) | ||
329 | return -1; | ||
324 | if (recvqueue(connection_id) != 0 && connections[connection_id].status != 0) | 330 | if (recvqueue(connection_id) != 0 && connections[connection_id].status != 0) |
325 | return connections[connection_id].recvbuffer[connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0]; | 331 | return connections[connection_id].recvbuffer[connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0]; |
326 | return -1; | 332 | return -1; |
diff --git a/core/Messenger.c b/core/Messenger.c index 4c76dd30..872d7407 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -99,18 +99,21 @@ int getclient_id(int friend_id, uint8_t *client_id) | |||
99 | client_id is the client id of the friend | 99 | client_id is the client id of the friend |
100 | data is the data and length is the length | 100 | data is the data and length is the length |
101 | returns the friend number if success | 101 | returns the friend number if success |
102 | return -1 if failure. */ | 102 | return -1 if key length is wrong. |
103 | return -2 if user's own key | ||
104 | return -3 if already a friend | ||
105 | return -4 for other*/ | ||
103 | int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) | 106 | int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) |
104 | { | 107 | { |
105 | if (length == 0 || length >= | 108 | if (length == 0 || length >= |
106 | (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES)) | 109 | (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES)) |
107 | return -1; | 110 | return -1; |
108 | if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) | 111 | if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) |
109 | return -1; | 112 | return -2; |
110 | if (getfriend_id(client_id) != -1) | 113 | if (getfriend_id(client_id) != -1) |
111 | return -1; | 114 | return -3; |
112 | uint32_t i; | 115 | uint32_t i; |
113 | for (i = 0; i <= numfriends; ++i) { | 116 | for (i = 0; i <= numfriends; ++i) { /*TODO: dynamic memory allocation, this will segfault if there are more than MAX_NUM_FRIENDS*/ |
114 | if(friendlist[i].status == 0) { | 117 | if(friendlist[i].status == 0) { |
115 | DHT_addfriend(client_id); | 118 | DHT_addfriend(client_id); |
116 | friendlist[i].status = 1; | 119 | friendlist[i].status = 1; |
@@ -126,7 +129,7 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) | |||
126 | return i; | 129 | return i; |
127 | } | 130 | } |
128 | } | 131 | } |
129 | return -1; | 132 | return -4; |
130 | } | 133 | } |
131 | 134 | ||
132 | int m_addfriend_norequest(uint8_t * client_id) | 135 | int m_addfriend_norequest(uint8_t * client_id) |
@@ -134,7 +137,7 @@ int m_addfriend_norequest(uint8_t * client_id) | |||
134 | if (getfriend_id(client_id) != -1) | 137 | if (getfriend_id(client_id) != -1) |
135 | return -1; | 138 | return -1; |
136 | uint32_t i; | 139 | uint32_t i; |
137 | for (i = 0; i <= numfriends; ++i) { | 140 | for (i = 0; i <= numfriends; ++i) {/*TODO: dynamic memory allocation, this will segfault if there are more than MAX_NUM_FRIENDS*/ |
138 | if(friendlist[i].status == 0) { | 141 | if(friendlist[i].status == 0) { |
139 | DHT_addfriend(client_id); | 142 | DHT_addfriend(client_id); |
140 | friendlist[i].status = 2; | 143 | friendlist[i].status = 2; |
@@ -164,7 +167,7 @@ int m_delfriend(int friendnumber) | |||
164 | memset(&friendlist[friendnumber], 0, sizeof(Friend)); | 167 | memset(&friendlist[friendnumber], 0, sizeof(Friend)); |
165 | uint32_t i; | 168 | uint32_t i; |
166 | for (i = numfriends; i != 0; --i) { | 169 | for (i = numfriends; i != 0; --i) { |
167 | if (friendlist[i].status != 0) | 170 | if (friendlist[i-1].status != 0) |
168 | break; | 171 | break; |
169 | } | 172 | } |
170 | numfriends = i; | 173 | numfriends = i; |
@@ -178,7 +181,7 @@ int m_delfriend(int friendnumber) | |||
178 | return 0 if there is no friend with that number */ | 181 | return 0 if there is no friend with that number */ |
179 | int m_friendstatus(int friendnumber) | 182 | int m_friendstatus(int friendnumber) |
180 | { | 183 | { |
181 | if (friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) | 184 | if (friendnumber < 0 || friendnumber >= numfriends) |
182 | return 0; | 185 | return 0; |
183 | return friendlist[friendnumber].status; | 186 | return friendlist[friendnumber].status; |
184 | } | 187 | } |
@@ -188,7 +191,7 @@ int m_friendstatus(int friendnumber) | |||
188 | return 0 if it was not */ | 191 | return 0 if it was not */ |
189 | int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length) | 192 | int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length) |
190 | { | 193 | { |
191 | if (friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) | 194 | if (friendnumber < 0 || friendnumber >= numfriends) |
192 | return 0; | 195 | return 0; |
193 | if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) | 196 | if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) |
194 | /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */ | 197 | /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */ |
@@ -240,6 +243,16 @@ int setname(uint8_t * name, uint16_t length) | |||
240 | return 0; | 243 | return 0; |
241 | } | 244 | } |
242 | 245 | ||
246 | /* get our nickname | ||
247 | put it in name | ||
248 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. | ||
249 | return the length of the name */ | ||
250 | uint16_t getself_name(uint8_t *name) | ||
251 | { | ||
252 | memcpy(name, self_name, self_name_length); | ||
253 | return self_name_length; | ||
254 | } | ||
255 | |||
243 | /* get name of friendnumber | 256 | /* get name of friendnumber |
244 | put it in name | 257 | put it in name |
245 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. | 258 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. |
diff --git a/core/Messenger.h b/core/Messenger.h index 7263901c..9ce96fb4 100644 --- a/core/Messenger.h +++ b/core/Messenger.h | |||
@@ -46,11 +46,14 @@ extern "C" { | |||
46 | to an absurdly large number later */ | 46 | to an absurdly large number later */ |
47 | 47 | ||
48 | /* add a friend | 48 | /* add a friend |
49 | set the data that will be sent along with friend request | 49 | set the data that will be sent along with friend request |
50 | client_id is the client id of the friend | 50 | client_id is the client id of the friend |
51 | data is the data and length is the length | 51 | data is the data and length is the length |
52 | returns the friend number if success | 52 | returns the friend number if success |
53 | return -1 if failure. */ | 53 | return -1 if key length is wrong. |
54 | return -2 if user's own key | ||
55 | return -3 if already a friend | ||
56 | return -4 for other*/ | ||
54 | int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length); | 57 | int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length); |
55 | 58 | ||
56 | 59 | ||
@@ -92,6 +95,11 @@ int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length); | |||
92 | return -1 if failure */ | 95 | return -1 if failure */ |
93 | int setname(uint8_t *name, uint16_t length); | 96 | int setname(uint8_t *name, uint16_t length); |
94 | 97 | ||
98 | /* get our nickname | ||
99 | put it in name | ||
100 | return the length of the name*/ | ||
101 | uint16_t getself_name(uint8_t *name); | ||
102 | |||
95 | /* get name of friendnumber | 103 | /* get name of friendnumber |
96 | put it in name | 104 | put it in name |
97 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. | 105 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. |
diff --git a/core/network.c b/core/network.c index aa16bda9..a7a4efcd 100644 --- a/core/network.c +++ b/core/network.c | |||
@@ -169,7 +169,7 @@ void shutdown_networking() | |||
169 | address should represent IPv4, IPv6 or a hostname | 169 | address should represent IPv4, IPv6 or a hostname |
170 | on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i | 170 | on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i |
171 | on failure returns -1 */ | 171 | on failure returns -1 */ |
172 | int resolve_addr(char *address) | 172 | int resolve_addr(const char *address) |
173 | { | 173 | { |
174 | struct addrinfo hints; | 174 | struct addrinfo hints; |
175 | memset(&hints, 0, sizeof(hints)); | 175 | memset(&hints, 0, sizeof(hints)); |
@@ -178,7 +178,7 @@ int resolve_addr(char *address) | |||
178 | 178 | ||
179 | struct addrinfo *server = NULL; | 179 | struct addrinfo *server = NULL; |
180 | 180 | ||
181 | int success = getaddrinfo(address, "7", &hints, &server); | 181 | int success = getaddrinfo(address, "echo", &hints, &server); |
182 | if(success != 0) | 182 | if(success != 0) |
183 | return -1; | 183 | return -1; |
184 | 184 | ||
diff --git a/core/network.h b/core/network.h index aaaaa409..8af4b32f 100644 --- a/core/network.h +++ b/core/network.h | |||
@@ -125,7 +125,7 @@ void shutdown_networking(); | |||
125 | address should represent IPv4, IPv6 or a hostname | 125 | address should represent IPv4, IPv6 or a hostname |
126 | on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i | 126 | on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i |
127 | on failure returns -1 */ | 127 | on failure returns -1 */ |
128 | int resolve_addr(char *address); | 128 | int resolve_addr(const char *address); |
129 | 129 | ||
130 | #ifdef __cplusplus | 130 | #ifdef __cplusplus |
131 | } | 131 | } |