summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/CMakeLists.txt16
-rw-r--r--core/Lossless_UDP.c6
-rw-r--r--core/Messenger.c31
-rw-r--r--core/Messenger.h18
-rw-r--r--core/friend_requests.h2
-rw-r--r--core/network.c4
-rw-r--r--core/network.h8
7 files changed, 55 insertions, 30 deletions
diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt
index 6ddd5b9b..44ae980c 100644
--- a/core/CMakeLists.txt
+++ b/core/CMakeLists.txt
@@ -3,15 +3,17 @@ project(core C)
3 3
4if(WIN32) 4if(WIN32)
5 include_directories(${CMAKE_HOME_DIRECTORY}/sodium/include/) 5 include_directories(${CMAKE_HOME_DIRECTORY}/sodium/include/)
6else(WIN32)
7 include_directories(${SODIUM_INCLUDE_DIR})
6endif() 8endif()
7 9
8set(core_sources 10set(core_sources
9 DHT.c 11 DHT.c
10 network.c 12 network.c
11 Lossless_UDP.c 13 Lossless_UDP.c
12 net_crypto.c 14 net_crypto.c
13 friend_requests.c 15 friend_requests.c
14 LAN_discovery.c 16 LAN_discovery.c
15 Messenger.c) 17 Messenger.c)
16 18
17add_library(core ${core_sources}) 19add_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. */
309uint32_t sendqueue(int connection_id) 309uint32_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(...) */
315uint32_t recvqueue(int connection_id) 317uint32_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 */
322char id_packet(int connection_id) 326char 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 d7770dfe..eb59b81a 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*/
103int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) 106int 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
132int m_addfriend_norequest(uint8_t * client_id) 135int 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;
@@ -165,7 +168,7 @@ int m_delfriend(int friendnumber)
165 uint32_t i; 168 uint32_t i;
166 169
167 for (i = numfriends; i != 0; --i) { 170 for (i = numfriends; i != 0; --i) {
168 if (friendlist[i].status != 0) 171 if (friendlist[i-1].status != 0)
169 break; 172 break;
170 } 173 }
171 numfriends = i; 174 numfriends = i;
@@ -180,7 +183,7 @@ int m_delfriend(int friendnumber)
180 return 0 if there is no friend with that number */ 183 return 0 if there is no friend with that number */
181int m_friendstatus(int friendnumber) 184int m_friendstatus(int friendnumber)
182{ 185{
183 if (friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) 186 if (friendnumber < 0 || friendnumber >= numfriends)
184 return 0; 187 return 0;
185 return friendlist[friendnumber].status; 188 return friendlist[friendnumber].status;
186} 189}
@@ -190,7 +193,7 @@ int m_friendstatus(int friendnumber)
190 return 0 if it was not */ 193 return 0 if it was not */
191int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length) 194int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length)
192{ 195{
193 if (friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) 196 if (friendnumber < 0 || friendnumber >= numfriends)
194 return 0; 197 return 0;
195 if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) 198 if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4)
196 /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */ 199 /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */
@@ -242,6 +245,16 @@ int setname(uint8_t * name, uint16_t length)
242 return 0; 245 return 0;
243} 246}
244 247
248/* get our nickname
249 put it in name
250 name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
251 return the length of the name */
252uint16_t getself_name(uint8_t *name)
253{
254 memcpy(name, self_name, self_name_length);
255 return self_name_length;
256}
257
245/* get name of friendnumber 258/* get name of friendnumber
246 put it in name 259 put it in name
247 name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. 260 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*/
54int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length); 57int 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 */
93int setname(uint8_t *name, uint16_t length); 96int 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*/
101uint16_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/friend_requests.h b/core/friend_requests.h
index e38f7edc..4dfc5130 100644
--- a/core/friend_requests.h
+++ b/core/friend_requests.h
@@ -48,4 +48,4 @@ int friendreq_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
48} 48}
49#endif 49#endif
50 50
51#endif \ No newline at end of file 51#endif
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 */
172int resolve_addr(char *address) 172int 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..3277070c 100644
--- a/core/network.h
+++ b/core/network.h
@@ -56,11 +56,7 @@
56/* we use libsodium by default */ 56/* we use libsodium by default */
57#include <sodium.h> 57#include <sodium.h>
58#else 58#else
59 59#include <crypto_box.h>
60/* TODO: Including stuff like this is bad. This needs fixing.
61 We keep support for the original NaCl for now. */
62#include "../nacl/build/Linux/include/amd64/crypto_box.h"
63
64#endif 60#endif
65 61
66#ifdef __cplusplus 62#ifdef __cplusplus
@@ -125,7 +121,7 @@ void shutdown_networking();
125 address should represent IPv4, IPv6 or a hostname 121 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 122 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 */ 123 on failure returns -1 */
128int resolve_addr(char *address); 124int resolve_addr(const char *address);
129 125
130#ifdef __cplusplus 126#ifdef __cplusplus
131} 127}