diff options
-rw-r--r-- | core/DHT.h | 138 | ||||
-rw-r--r-- | core/LAN_discovery.h | 12 | ||||
-rw-r--r-- | core/Lossless_UDP.h | 102 | ||||
-rw-r--r-- | core/Messenger.h | 210 | ||||
-rw-r--r-- | core/friend_requests.h | 24 | ||||
-rw-r--r-- | core/net_crypto.h | 186 | ||||
-rw-r--r-- | core/network.h | 110 |
7 files changed, 391 insertions, 391 deletions
@@ -30,79 +30,79 @@ | |||
30 | extern "C" { | 30 | extern "C" { |
31 | #endif | 31 | #endif |
32 | 32 | ||
33 | /* Current time, unix format */ | 33 | /* Current time, unix format */ |
34 | #define unix_time() ((uint32_t)time(NULL)) | 34 | #define unix_time() ((uint32_t)time(NULL)) |
35 | 35 | ||
36 | /* size of the client_id in bytes */ | 36 | /* size of the client_id in bytes */ |
37 | #define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES | 37 | #define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES |
38 | 38 | ||
39 | /* Add a new friend to the friends list | 39 | /* Add a new friend to the friends list |
40 | client_id must be CLIENT_ID_SIZE bytes long. | 40 | client_id must be CLIENT_ID_SIZE bytes long. |
41 | returns 0 if success | 41 | returns 0 if success |
42 | returns 1 if failure (friends list is full) */ | 42 | returns 1 if failure (friends list is full) */ |
43 | int DHT_addfriend(uint8_t *client_id); | 43 | int DHT_addfriend(uint8_t *client_id); |
44 | 44 | ||
45 | /* Delete a friend from the friends list | 45 | /* Delete a friend from the friends list |
46 | client_id must be CLIENT_ID_SIZE bytes long. | 46 | client_id must be CLIENT_ID_SIZE bytes long. |
47 | returns 0 if success | 47 | returns 0 if success |
48 | returns 1 if failure (client_id not in friends list) */ | 48 | returns 1 if failure (client_id not in friends list) */ |
49 | int DHT_delfriend(uint8_t *client_id); | 49 | int DHT_delfriend(uint8_t *client_id); |
50 | 50 | ||
51 | /* Get ip of friend | 51 | /* Get ip of friend |
52 | client_id must be CLIENT_ID_SIZE bytes long. | 52 | client_id must be CLIENT_ID_SIZE bytes long. |
53 | ip must be 4 bytes long. | 53 | ip must be 4 bytes long. |
54 | port must be 2 bytes long. | 54 | port must be 2 bytes long. |
55 | returns ip if success | 55 | returns ip if success |
56 | returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.) | 56 | returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.) |
57 | returns ip of 1 if friend is not in list. */ | 57 | returns ip of 1 if friend is not in list. */ |
58 | IP_Port DHT_getfriendip(uint8_t *client_id); | 58 | IP_Port DHT_getfriendip(uint8_t *client_id); |
59 | 59 | ||
60 | /* Run this function at least a couple times per second (It's the main loop) */ | 60 | /* Run this function at least a couple times per second (It's the main loop) */ |
61 | void doDHT(); | 61 | void doDHT(); |
62 | 62 | ||
63 | /* if we receive a DHT packet we call this function so it can be handled. | 63 | /* if we receive a DHT packet we call this function so it can be handled. |
64 | return 0 if packet is handled correctly. | 64 | return 0 if packet is handled correctly. |
65 | return 1 if it didn't handle the packet or if the packet was shit. */ | 65 | return 1 if it didn't handle the packet or if the packet was shit. */ |
66 | int DHT_handlepacket(uint8_t *packet, uint32_t length, IP_Port source); | 66 | int DHT_handlepacket(uint8_t *packet, uint32_t length, IP_Port source); |
67 | 67 | ||
68 | /* Use this function to bootstrap the client | 68 | /* Use this function to bootstrap the client |
69 | Sends a get nodes request to the given node with ip port and public_key */ | 69 | Sends a get nodes request to the given node with ip port and public_key */ |
70 | void DHT_bootstrap(IP_Port ip_port, uint8_t *public_key); | 70 | void DHT_bootstrap(IP_Port ip_port, uint8_t *public_key); |
71 | 71 | ||
72 | /* ROUTING FUNCTIONS */ | 72 | /* ROUTING FUNCTIONS */ |
73 | 73 | ||
74 | /* send the given packet to node with client_id | 74 | /* send the given packet to node with client_id |
75 | returns -1 if failure */ | 75 | returns -1 if failure */ |
76 | int route_packet(uint8_t *client_id, uint8_t *packet, uint32_t length); | 76 | int route_packet(uint8_t *client_id, uint8_t *packet, uint32_t length); |
77 | 77 | ||
78 | /* Send the following packet to everyone who tells us they are connected to friend_id | 78 | /* Send the following packet to everyone who tells us they are connected to friend_id |
79 | returns the number of nodes it sent the packet to */ | 79 | returns the number of nodes it sent the packet to */ |
80 | int route_tofriend(uint8_t *friend_id, uint8_t *packet, uint32_t length); | 80 | int route_tofriend(uint8_t *friend_id, uint8_t *packet, uint32_t length); |
81 | 81 | ||
82 | /* NAT PUNCHING FUNCTIONS */ | 82 | /* NAT PUNCHING FUNCTIONS */ |
83 | 83 | ||
84 | /* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist | 84 | /* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist |
85 | ip_portlist must be at least MAX_FRIEND_CLIENTS big | 85 | ip_portlist must be at least MAX_FRIEND_CLIENTS big |
86 | returns the number of ips returned | 86 | returns the number of ips returned |
87 | returns -1 if no such friend*/ | 87 | returns -1 if no such friend*/ |
88 | int friend_ips(IP_Port *ip_portlist, uint8_t *friend_id); | 88 | int friend_ips(IP_Port *ip_portlist, uint8_t *friend_id); |
89 | 89 | ||
90 | /* SAVE/LOAD functions */ | 90 | /* SAVE/LOAD functions */ |
91 | 91 | ||
92 | /* get the size of the DHT (for saving) */ | 92 | /* get the size of the DHT (for saving) */ |
93 | uint32_t DHT_size(); | 93 | uint32_t DHT_size(); |
94 | 94 | ||
95 | /* save the DHT in data where data is an array of size DHT_size() */ | 95 | /* save the DHT in data where data is an array of size DHT_size() */ |
96 | void DHT_save(uint8_t *data); | 96 | void DHT_save(uint8_t *data); |
97 | 97 | ||
98 | /* load the DHT from data of size size; | 98 | /* load the DHT from data of size size; |
99 | return -1 if failure | 99 | return -1 if failure |
100 | return 0 if success */ | 100 | return 0 if success */ |
101 | int DHT_load(uint8_t *data, uint32_t size); | 101 | int DHT_load(uint8_t *data, uint32_t size); |
102 | 102 | ||
103 | /* returns 0 if we are not connected to the DHT | 103 | /* returns 0 if we are not connected to the DHT |
104 | returns 1 if we are */ | 104 | returns 1 if we are */ |
105 | int DHT_isconnected(); | 105 | int DHT_isconnected(); |
106 | 106 | ||
107 | #ifdef __cplusplus | 107 | #ifdef __cplusplus |
108 | } | 108 | } |
diff --git a/core/LAN_discovery.h b/core/LAN_discovery.h index 3c056ba6..4ca65c03 100644 --- a/core/LAN_discovery.h +++ b/core/LAN_discovery.h | |||
@@ -32,14 +32,14 @@ | |||
32 | extern "C" { | 32 | extern "C" { |
33 | #endif | 33 | #endif |
34 | 34 | ||
35 | /*Send a LAN discovery pcaket to the broadcast address with port port*/ | 35 | /*Send a LAN discovery pcaket to the broadcast address with port port*/ |
36 | int send_LANdiscovery(uint16_t port); | 36 | int send_LANdiscovery(uint16_t port); |
37 | 37 | ||
38 | 38 | ||
39 | /* if we receive a packet we call this function so it can be handled. | 39 | /* if we receive a packet we call this function so it can be handled. |
40 | return 0 if packet is handled correctly. | 40 | return 0 if packet is handled correctly. |
41 | return 1 if it didn't handle the packet or if the packet was shit. */ | 41 | return 1 if it didn't handle the packet or if the packet was shit. */ |
42 | int LANdiscovery_handlepacket(uint8_t *packet, uint32_t length, IP_Port source); | 42 | int LANdiscovery_handlepacket(uint8_t *packet, uint32_t length, IP_Port source); |
43 | 43 | ||
44 | 44 | ||
45 | 45 | ||
diff --git a/core/Lossless_UDP.h b/core/Lossless_UDP.h index 72214370..033bc480 100644 --- a/core/Lossless_UDP.h +++ b/core/Lossless_UDP.h | |||
@@ -30,74 +30,74 @@ | |||
30 | extern "C" { | 30 | extern "C" { |
31 | #endif | 31 | #endif |
32 | 32 | ||
33 | /* maximum length of the data in the data packets */ | 33 | /* maximum length of the data in the data packets */ |
34 | #define MAX_DATA_SIZE 1024 | 34 | #define MAX_DATA_SIZE 1024 |
35 | 35 | ||
36 | /* Functions */ | 36 | /* Functions */ |
37 | 37 | ||
38 | /* initialize a new connection to ip_port | 38 | /* initialize a new connection to ip_port |
39 | returns an integer corresponding to the connection id. | 39 | returns an integer corresponding to the connection id. |
40 | return -1 if it could not initialize the connection. | 40 | return -1 if it could not initialize the connection. |
41 | if there already was an existing connection to that ip_port return its number. */ | 41 | if there already was an existing connection to that ip_port return its number. */ |
42 | int new_connection(IP_Port ip_port); | 42 | int new_connection(IP_Port ip_port); |
43 | 43 | ||
44 | /* get connection id from IP_Port | 44 | /* get connection id from IP_Port |
45 | return -1 if there are no connections like we are looking for | 45 | return -1 if there are no connections like we are looking for |
46 | return id if it found it */ | 46 | return id if it found it */ |
47 | int getconnection_id(IP_Port ip_port); | 47 | int getconnection_id(IP_Port ip_port); |
48 | 48 | ||
49 | /* returns an integer corresponding to the next connection in our imcoming connection list | 49 | /* returns an integer corresponding to the next connection in our imcoming connection list |
50 | return -1 if there are no new incoming connections in the list. */ | 50 | return -1 if there are no new incoming connections in the list. */ |
51 | int incoming_connection(); | 51 | int incoming_connection(); |
52 | 52 | ||
53 | /* return -1 if it could not kill the connection. | 53 | /* return -1 if it could not kill the connection. |
54 | return 0 if killed successfully */ | 54 | return 0 if killed successfully */ |
55 | int kill_connection(int connection_id); | 55 | int kill_connection(int connection_id); |
56 | 56 | ||
57 | /* kill connection in seconds seconds. | 57 | /* kill connection in seconds seconds. |
58 | return -1 if it can not kill the connection. | 58 | return -1 if it can not kill the connection. |
59 | return 0 if it will kill it */ | 59 | return 0 if it will kill it */ |
60 | int kill_connection_in(int connection_id, uint32_t seconds); | 60 | int kill_connection_in(int connection_id, uint32_t seconds); |
61 | 61 | ||
62 | /* returns the ip_port of the corresponding connection. | 62 | /* returns the ip_port of the corresponding connection. |
63 | return 0 if there is no such connection. */ | 63 | return 0 if there is no such connection. */ |
64 | IP_Port connection_ip(int connection_id); | 64 | IP_Port connection_ip(int connection_id); |
65 | 65 | ||
66 | /* returns the id of the next packet in the queue | 66 | /* returns the id of the next packet in the queue |
67 | return -1 if no packet in queue */ | 67 | return -1 if no packet in queue */ |
68 | char id_packet(int connection_id); | 68 | char id_packet(int connection_id); |
69 | 69 | ||
70 | /* return 0 if there is no received data in the buffer. | 70 | /* return 0 if there is no received data in the buffer. |
71 | return length of received packet if successful */ | 71 | return length of received packet if successful */ |
72 | int read_packet(int connection_id, uint8_t *data); | 72 | int read_packet(int connection_id, uint8_t *data); |
73 | 73 | ||
74 | /* return 0 if data could not be put in packet queue | 74 | /* return 0 if data could not be put in packet queue |
75 | return 1 if data was put into the queue */ | 75 | return 1 if data was put into the queue */ |
76 | int write_packet(int connection_id, uint8_t *data, uint32_t length); | 76 | int write_packet(int connection_id, uint8_t *data, uint32_t length); |
77 | 77 | ||
78 | /* returns the number of packets in the queue waiting to be successfully sent. */ | 78 | /* returns the number of packets in the queue waiting to be successfully sent. */ |
79 | uint32_t sendqueue(int connection_id); | 79 | uint32_t sendqueue(int connection_id); |
80 | 80 | ||
81 | /* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ | 81 | /* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ |
82 | uint32_t recvqueue(int connection_id); | 82 | uint32_t recvqueue(int connection_id); |
83 | 83 | ||
84 | /* check if connection is connected | 84 | /* check if connection is connected |
85 | return 0 no. | 85 | return 0 no. |
86 | return 1 if attempting handshake | 86 | return 1 if attempting handshake |
87 | return 2 if handshake is done | 87 | return 2 if handshake is done |
88 | return 3 if fully connected | 88 | return 3 if fully connected |
89 | return 4 if timed out and wating to be killed */ | 89 | return 4 if timed out and wating to be killed */ |
90 | int is_connected(int connection_id); | 90 | int is_connected(int connection_id); |
91 | 91 | ||
92 | /* Call this function a couple times per second | 92 | /* Call this function a couple times per second |
93 | It's the main loop. */ | 93 | It's the main loop. */ |
94 | void doLossless_UDP(); | 94 | void doLossless_UDP(); |
95 | 95 | ||
96 | 96 | ||
97 | /* if we receive a Lossless_UDP packet we call this function so it can be handled. | 97 | /* if we receive a Lossless_UDP packet we call this function so it can be handled. |
98 | return 0 if packet is handled correctly. | 98 | return 0 if packet is handled correctly. |
99 | return 1 if it didn't handle the packet or if the packet was shit. */ | 99 | return 1 if it didn't handle the packet or if the packet was shit. */ |
100 | int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source); | 100 | int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source); |
101 | 101 | ||
102 | #ifdef __cplusplus | 102 | #ifdef __cplusplus |
103 | } | 103 | } |
diff --git a/core/Messenger.h b/core/Messenger.h index 3cdeeb93..564e29f2 100644 --- a/core/Messenger.h +++ b/core/Messenger.h | |||
@@ -42,111 +42,111 @@ extern "C" { | |||
42 | #define PACKET_ID_USERSTATUS 49 | 42 | #define PACKET_ID_USERSTATUS 49 |
43 | #define PACKET_ID_MESSAGE 64 | 43 | #define PACKET_ID_MESSAGE 64 |
44 | 44 | ||
45 | /* don't assume MAX_USERSTATUS_LENGTH will stay at 128, it may be increased | 45 | /* don't assume MAX_USERSTATUS_LENGTH will stay at 128, it may be increased |
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 failure. */ |
54 | int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length); | 54 | int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length); |
55 | 55 | ||
56 | 56 | ||
57 | /* add a friend without sending a friendrequest. | 57 | /* add a friend without sending a friendrequest. |
58 | returns the friend number if success | 58 | returns the friend number if success |
59 | return -1 if failure. */ | 59 | return -1 if failure. */ |
60 | int m_addfriend_norequest(uint8_t *client_id); | 60 | int m_addfriend_norequest(uint8_t *client_id); |
61 | 61 | ||
62 | /* return the friend id associated to that client id. | 62 | /* return the friend id associated to that client id. |
63 | return -1 if no such friend */ | 63 | return -1 if no such friend */ |
64 | int getfriend_id(uint8_t *client_id); | 64 | int getfriend_id(uint8_t *client_id); |
65 | 65 | ||
66 | /* copies the public key associated to that friend id into client_id buffer. | 66 | /* copies the public key associated to that friend id into client_id buffer. |
67 | make sure that client_id is of size CLIENT_ID_SIZE. | 67 | make sure that client_id is of size CLIENT_ID_SIZE. |
68 | return 0 if success | 68 | return 0 if success |
69 | return -1 if failure */ | 69 | return -1 if failure */ |
70 | int getclient_id(int friend_id, uint8_t *client_id); | 70 | int getclient_id(int friend_id, uint8_t *client_id); |
71 | 71 | ||
72 | /* remove a friend */ | 72 | /* remove a friend */ |
73 | int m_delfriend(int friendnumber); | 73 | int m_delfriend(int friendnumber); |
74 | 74 | ||
75 | /* return 4 if friend is online | 75 | /* return 4 if friend is online |
76 | return 3 if friend is confirmed | 76 | return 3 if friend is confirmed |
77 | return 2 if the friend request was sent | 77 | return 2 if the friend request was sent |
78 | return 1 if the friend was added | 78 | return 1 if the friend was added |
79 | return 0 if there is no friend with that number */ | 79 | return 0 if there is no friend with that number */ |
80 | int m_friendstatus(int friendnumber); | 80 | int m_friendstatus(int friendnumber); |
81 | 81 | ||
82 | /* send a text chat message to an online friend | 82 | /* send a text chat message to an online friend |
83 | returns 1 if packet was successfully put into the send queue | 83 | returns 1 if packet was successfully put into the send queue |
84 | return 0 if it was not */ | 84 | return 0 if it was not */ |
85 | int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length); | 85 | int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length); |
86 | 86 | ||
87 | /* Set our nickname | 87 | /* Set our nickname |
88 | name must be a string of maximum MAX_NAME_LENGTH length. | 88 | name must be a string of maximum MAX_NAME_LENGTH length. |
89 | return 0 if success | 89 | return 0 if success |
90 | return -1 if failure */ | 90 | return -1 if failure */ |
91 | int setname(uint8_t *name, uint16_t length); | 91 | int setname(uint8_t *name, uint16_t length); |
92 | 92 | ||
93 | /* get name of friendnumber | 93 | /* get name of friendnumber |
94 | put it in name | 94 | put it in name |
95 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. | 95 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. |
96 | return 0 if success | 96 | return 0 if success |
97 | return -1 if failure */ | 97 | return -1 if failure */ |
98 | int getname(int friendnumber, uint8_t *name); | 98 | int getname(int friendnumber, uint8_t *name); |
99 | 99 | ||
100 | /* set our user status | 100 | /* set our user status |
101 | you are responsible for freeing status after | 101 | you are responsible for freeing status after |
102 | returns 0 on success, -1 on failure */ | 102 | returns 0 on success, -1 on failure */ |
103 | int m_set_userstatus(uint8_t *status, uint16_t length); | 103 | int m_set_userstatus(uint8_t *status, uint16_t length); |
104 | 104 | ||
105 | /* return the length of friendnumber's user status, | 105 | /* return the length of friendnumber's user status, |
106 | including null | 106 | including null |
107 | pass it into malloc */ | 107 | pass it into malloc */ |
108 | int m_get_userstatus_size(int friendnumber); | 108 | int m_get_userstatus_size(int friendnumber); |
109 | 109 | ||
110 | /* copy friendnumber's userstatus into buf, truncating if size is over maxlen | 110 | /* copy friendnumber's userstatus into buf, truncating if size is over maxlen |
111 | get the size you need to allocate from m_get_userstatus_size */ | 111 | get the size you need to allocate from m_get_userstatus_size */ |
112 | int m_copy_userstatus(int friendnumber, uint8_t *buf, uint32_t maxlen); | 112 | int m_copy_userstatus(int friendnumber, uint8_t *buf, uint32_t maxlen); |
113 | 113 | ||
114 | /* set the function that will be executed when a friend request is received. | 114 | /* set the function that will be executed when a friend request is received. |
115 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ | 115 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ |
116 | void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); | 116 | void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); |
117 | 117 | ||
118 | /* set the function that will be executed when a message from a friend is received. | 118 | /* set the function that will be executed when a message from a friend is received. |
119 | function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ | 119 | function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ |
120 | void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)); | 120 | void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)); |
121 | 121 | ||
122 | /* set the callback for name changes | 122 | /* set the callback for name changes |
123 | function(int friendnumber, uint8_t *newname, uint16_t length) | 123 | function(int friendnumber, uint8_t *newname, uint16_t length) |
124 | you are not responsible for freeing newname */ | 124 | you are not responsible for freeing newname */ |
125 | void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t)); | 125 | void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t)); |
126 | 126 | ||
127 | /* set the callback for user status changes | 127 | /* set the callback for user status changes |
128 | function(int friendnumber, uint8_t *newstatus, uint16_t length) | 128 | function(int friendnumber, uint8_t *newstatus, uint16_t length) |
129 | you are not responsible for freeing newstatus */ | 129 | you are not responsible for freeing newstatus */ |
130 | void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t)); | 130 | void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t)); |
131 | 131 | ||
132 | /* run this at startup | 132 | /* run this at startup |
133 | returns 0 if no connection problems | 133 | returns 0 if no connection problems |
134 | returns -1 if there are problems */ | 134 | returns -1 if there are problems */ |
135 | int initMessenger(); | 135 | int initMessenger(); |
136 | 136 | ||
137 | /* the main loop that needs to be run at least 200 times per second */ | 137 | /* the main loop that needs to be run at least 200 times per second */ |
138 | void doMessenger(); | 138 | void doMessenger(); |
139 | 139 | ||
140 | /* SAVING AND LOADING FUNCTIONS: */ | 140 | /* SAVING AND LOADING FUNCTIONS: */ |
141 | 141 | ||
142 | /* returns the size of the messenger data (for saving) */ | 142 | /* returns the size of the messenger data (for saving) */ |
143 | uint32_t Messenger_size(); | 143 | uint32_t Messenger_size(); |
144 | 144 | ||
145 | /* save the messenger in data (must be allocated memory of size Messenger_size()) */ | 145 | /* save the messenger in data (must be allocated memory of size Messenger_size()) */ |
146 | void Messenger_save(uint8_t *data); | 146 | void Messenger_save(uint8_t *data); |
147 | 147 | ||
148 | /* load the messenger from data of size length */ | 148 | /* load the messenger from data of size length */ |
149 | int Messenger_load(uint8_t *data, uint32_t length); | 149 | int Messenger_load(uint8_t *data, uint32_t length); |
150 | 150 | ||
151 | #ifdef __cplusplus | 151 | #ifdef __cplusplus |
152 | } | 152 | } |
diff --git a/core/friend_requests.h b/core/friend_requests.h index e21c2a90..e38f7edc 100644 --- a/core/friend_requests.h +++ b/core/friend_requests.h | |||
@@ -31,18 +31,18 @@ | |||
31 | extern "C" { | 31 | extern "C" { |
32 | #endif | 32 | #endif |
33 | 33 | ||
34 | /* Try to send a friendrequest to peer with public_key | 34 | /* Try to send a friendrequest to peer with public_key |
35 | data is the data in the request and length is the length. */ | 35 | data is the data in the request and length is the length. */ |
36 | int send_friendrequest(uint8_t *public_key, uint8_t *data, uint32_t length); | 36 | int send_friendrequest(uint8_t *public_key, uint8_t *data, uint32_t length); |
37 | 37 | ||
38 | /* set the function that will be executed when a friend request for us is received. | 38 | /* set the function that will be executed when a friend request for us is received. |
39 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ | 39 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ |
40 | void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); | 40 | void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); |
41 | 41 | ||
42 | /* if we receive a packet we call this function so it can be handled. | 42 | /* if we receive a packet we call this function so it can be handled. |
43 | return 0 if packet is handled correctly. | 43 | return 0 if packet is handled correctly. |
44 | return 1 if it didn't handle the packet or if the packet was shit. */ | 44 | return 1 if it didn't handle the packet or if the packet was shit. */ |
45 | int friendreq_handlepacket(uint8_t *packet, uint32_t length, IP_Port source); | 45 | int friendreq_handlepacket(uint8_t *packet, uint32_t length, IP_Port source); |
46 | 46 | ||
47 | #ifdef __cplusplus | 47 | #ifdef __cplusplus |
48 | } | 48 | } |
diff --git a/core/net_crypto.h b/core/net_crypto.h index 0eb2ad6d..0e7284c9 100644 --- a/core/net_crypto.h +++ b/core/net_crypto.h | |||
@@ -30,102 +30,102 @@ | |||
30 | extern "C" { | 30 | extern "C" { |
31 | #endif | 31 | #endif |
32 | 32 | ||
33 | /* Our public key. */ | 33 | /* Our public key. */ |
34 | extern uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | 34 | extern uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; |
35 | extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | 35 | extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; |
36 | 36 | ||
37 | #define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) | 37 | #define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) |
38 | 38 | ||
39 | /* encrypts plain of length length to encrypted of length + 16 using the | 39 | /* encrypts plain of length length to encrypted of length + 16 using the |
40 | public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce | 40 | public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce |
41 | return -1 if there was a problem. | 41 | return -1 if there was a problem. |
42 | return length of encrypted data if everything was fine. */ | 42 | return length of encrypted data if everything was fine. */ |
43 | int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, | 43 | int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, |
44 | uint8_t *plain, uint32_t length, uint8_t *encrypted); | 44 | uint8_t *plain, uint32_t length, uint8_t *encrypted); |
45 | 45 | ||
46 | 46 | ||
47 | /* decrypts encrypted of length length to plain of length length - 16 using the | 47 | /* decrypts encrypted of length length to plain of length length - 16 using the |
48 | public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce | 48 | public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce |
49 | return -1 if there was a problem(decryption failed) | 49 | return -1 if there was a problem(decryption failed) |
50 | return length of plain data if everything was fine. */ | 50 | return length of plain data if everything was fine. */ |
51 | int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, | 51 | int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, |
52 | uint8_t *encrypted, uint32_t length, uint8_t *plain); | 52 | uint8_t *encrypted, uint32_t length, uint8_t *plain); |
53 | 53 | ||
54 | 54 | ||
55 | /* fill the given nonce with random bytes. */ | 55 | /* fill the given nonce with random bytes. */ |
56 | void random_nonce(uint8_t *nonce); | 56 | void random_nonce(uint8_t *nonce); |
57 | 57 | ||
58 | /* return 0 if there is no received data in the buffer | 58 | /* return 0 if there is no received data in the buffer |
59 | return -1 if the packet was discarded. | 59 | return -1 if the packet was discarded. |
60 | return length of received data if successful */ | 60 | return length of received data if successful */ |
61 | int read_cryptpacket(int crypt_connection_id, uint8_t *data); | 61 | int read_cryptpacket(int crypt_connection_id, uint8_t *data); |
62 | 62 | ||
63 | /* return 0 if data could not be put in packet queue | 63 | /* return 0 if data could not be put in packet queue |
64 | return 1 if data was put into the queue */ | 64 | return 1 if data was put into the queue */ |
65 | int write_cryptpacket(int crypt_connection_id, uint8_t *data, uint32_t length); | 65 | int write_cryptpacket(int crypt_connection_id, uint8_t *data, uint32_t length); |
66 | 66 | ||
67 | /* create a request to peer with public_key. | 67 | /* create a request to peer with public_key. |
68 | packet must be an array of MAX_DATA_SIZE big. | 68 | packet must be an array of MAX_DATA_SIZE big. |
69 | Data represents the data we send with the request with length being the length of the data. | 69 | Data represents the data we send with the request with length being the length of the data. |
70 | request_id is the id of the request (32 = friend request, 254 = ping request) | 70 | request_id is the id of the request (32 = friend request, 254 = ping request) |
71 | returns -1 on failure | 71 | returns -1 on failure |
72 | returns the length of the created packet on success */ | 72 | returns the length of the created packet on success */ |
73 | int create_request(uint8_t *packet, uint8_t * public_key, uint8_t *data, uint32_t length, uint8_t request_id); | 73 | int create_request(uint8_t *packet, uint8_t * public_key, uint8_t *data, uint32_t length, uint8_t request_id); |
74 | 74 | ||
75 | /* puts the senders public key in the request in public_key, the data from the request | 75 | /* puts the senders public key in the request in public_key, the data from the request |
76 | in data if a friend or ping request was sent to us and returns the length of the data. | 76 | in data if a friend or ping request was sent to us and returns the length of the data. |
77 | packet is the request packet and length is its length | 77 | packet is the request packet and length is its length |
78 | return -1 if not valid request. */ | 78 | return -1 if not valid request. */ |
79 | int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t length); | 79 | int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t length); |
80 | 80 | ||
81 | /* Start a secure connection with other peer who has public_key and ip_port | 81 | /* Start a secure connection with other peer who has public_key and ip_port |
82 | returns -1 if failure | 82 | returns -1 if failure |
83 | returns crypt_connection_id of the initialized connection if everything went well. */ | 83 | returns crypt_connection_id of the initialized connection if everything went well. */ |
84 | int crypto_connect(uint8_t *public_key, IP_Port ip_port); | 84 | int crypto_connect(uint8_t *public_key, IP_Port ip_port); |
85 | 85 | ||
86 | /* kill a crypto connection | 86 | /* kill a crypto connection |
87 | return 0 if killed successfully | 87 | return 0 if killed successfully |
88 | return 1 if there was a problem. */ | 88 | return 1 if there was a problem. */ |
89 | int crypto_kill(int crypt_connection_id); | 89 | int crypto_kill(int crypt_connection_id); |
90 | 90 | ||
91 | /* handle an incoming connection | 91 | /* handle an incoming connection |
92 | return -1 if no crypto inbound connection | 92 | return -1 if no crypto inbound connection |
93 | return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection | 93 | return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection |
94 | Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce | 94 | Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce |
95 | and the session public key for the connection in session_key | 95 | and the session public key for the connection in session_key |
96 | to accept it see: accept_crypto_inbound(...) | 96 | to accept it see: accept_crypto_inbound(...) |
97 | to refuse it just call kill_connection(...) on the connection id */ | 97 | to refuse it just call kill_connection(...) on the connection id */ |
98 | int crypto_inbound(uint8_t *public_key, uint8_t * secret_nonce, uint8_t *session_key); | 98 | int crypto_inbound(uint8_t *public_key, uint8_t * secret_nonce, uint8_t *session_key); |
99 | 99 | ||
100 | /* accept an incoming connection using the parameters provided by crypto_inbound | 100 | /* accept an incoming connection using the parameters provided by crypto_inbound |
101 | return -1 if not successful | 101 | return -1 if not successful |
102 | returns the crypt_connection_id if successful */ | 102 | returns the crypt_connection_id if successful */ |
103 | int accept_crypto_inbound(int connection_id, uint8_t *public_key, uint8_t * secret_nonce, uint8_t *session_key); | 103 | int accept_crypto_inbound(int connection_id, uint8_t *public_key, uint8_t * secret_nonce, uint8_t *session_key); |
104 | 104 | ||
105 | /* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet | 105 | /* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet |
106 | (we have received a handshake but no empty data packet), 3 if the connection is established. | 106 | (we have received a handshake but no empty data packet), 3 if the connection is established. |
107 | 4 if the connection is timed out and waiting to be killed */ | 107 | 4 if the connection is timed out and waiting to be killed */ |
108 | int is_cryptoconnected(int crypt_connection_id); | 108 | int is_cryptoconnected(int crypt_connection_id); |
109 | 109 | ||
110 | 110 | ||
111 | /* Generate our public and private keys | 111 | /* Generate our public and private keys |
112 | Only call this function the first time the program starts. */ | 112 | Only call this function the first time the program starts. */ |
113 | void new_keys(); | 113 | void new_keys(); |
114 | 114 | ||
115 | /* save the public and private keys to the keys array | 115 | /* save the public and private keys to the keys array |
116 | Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ | 116 | Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ |
117 | void save_keys(uint8_t * keys); | 117 | void save_keys(uint8_t * keys); |
118 | 118 | ||
119 | /* load the public and private keys from the keys array | 119 | /* load the public and private keys from the keys array |
120 | Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ | 120 | Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ |
121 | void load_keys(uint8_t * keys); | 121 | void load_keys(uint8_t * keys); |
122 | 122 | ||
123 | /* run this to (re)initialize net_crypto | 123 | /* run this to (re)initialize net_crypto |
124 | sets all the global connection variables to their default values. */ | 124 | sets all the global connection variables to their default values. */ |
125 | void initNetCrypto(); | 125 | void initNetCrypto(); |
126 | 126 | ||
127 | /* main loop */ | 127 | /* main loop */ |
128 | void doNetCrypto(); | 128 | void doNetCrypto(); |
129 | 129 | ||
130 | #ifdef __cplusplus | 130 | #ifdef __cplusplus |
131 | } | 131 | } |
diff --git a/core/network.h b/core/network.h index c8b8e355..aaaaa409 100644 --- a/core/network.h +++ b/core/network.h | |||
@@ -69,63 +69,63 @@ extern "C" { | |||
69 | 69 | ||
70 | #define MAX_UDP_PACKET_SIZE 65507 | 70 | #define MAX_UDP_PACKET_SIZE 65507 |
71 | 71 | ||
72 | typedef union { | 72 | typedef union { |
73 | uint8_t c[4]; | 73 | uint8_t c[4]; |
74 | uint16_t s[2]; | 74 | uint16_t s[2]; |
75 | uint32_t i; | 75 | uint32_t i; |
76 | } IP; | 76 | } IP; |
77 | 77 | ||
78 | typedef struct { | 78 | typedef struct { |
79 | IP ip; | 79 | IP ip; |
80 | uint16_t port; | 80 | uint16_t port; |
81 | /* not used for anything right now */ | 81 | /* not used for anything right now */ |
82 | uint16_t padding; | 82 | uint16_t padding; |
83 | } IP_Port; | 83 | } IP_Port; |
84 | 84 | ||
85 | typedef struct { | 85 | typedef struct { |
86 | int16_t family; | 86 | int16_t family; |
87 | uint16_t port; | 87 | uint16_t port; |
88 | IP ip; | 88 | IP ip; |
89 | uint8_t zeroes[8]; | 89 | uint8_t zeroes[8]; |
90 | #ifdef ENABLE_IPV6 | 90 | #ifdef ENABLE_IPV6 |
91 | uint8_t zeroes2[12]; | 91 | uint8_t zeroes2[12]; |
92 | #endif | 92 | #endif |
93 | } ADDR; | 93 | } ADDR; |
94 | 94 | ||
95 | /* returns current time in milleseconds since the epoch. */ | 95 | /* returns current time in milleseconds since the epoch. */ |
96 | uint64_t current_time(); | 96 | uint64_t current_time(); |
97 | 97 | ||
98 | /* return a random number | 98 | /* return a random number |
99 | NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary */ | 99 | NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary */ |
100 | uint32_t random_int(); | 100 | uint32_t random_int(); |
101 | 101 | ||
102 | /* Basic network functions: */ | 102 | /* Basic network functions: */ |
103 | 103 | ||
104 | /* Function to send packet(data) of length length to ip_port */ | 104 | /* Function to send packet(data) of length length to ip_port */ |
105 | int sendpacket(IP_Port ip_port, uint8_t *data, uint32_t length); | 105 | int sendpacket(IP_Port ip_port, uint8_t *data, uint32_t length); |
106 | 106 | ||
107 | /* Function to receive data, ip and port of sender is put into ip_port | 107 | /* Function to receive data, ip and port of sender is put into ip_port |
108 | the packet data into data | 108 | the packet data into data |
109 | the packet length into length. */ | 109 | the packet length into length. */ |
110 | int receivepacket(IP_Port *ip_port, uint8_t *data, uint32_t *length); | 110 | int receivepacket(IP_Port *ip_port, uint8_t *data, uint32_t *length); |
111 | 111 | ||
112 | /* initialize networking | 112 | /* initialize networking |
113 | bind to ip and port | 113 | bind to ip and port |
114 | ip must be in network order EX: 127.0.0.1 = (7F000001) | 114 | ip must be in network order EX: 127.0.0.1 = (7F000001) |
115 | port is in host byte order (this means don't worry about it) | 115 | port is in host byte order (this means don't worry about it) |
116 | returns 0 if no problems | 116 | returns 0 if no problems |
117 | returns -1 if there were problems */ | 117 | returns -1 if there were problems */ |
118 | int init_networking(IP ip, uint16_t port); | 118 | int init_networking(IP ip, uint16_t port); |
119 | 119 | ||
120 | /* function to cleanup networking stuff(doesn't do much right now) */ | 120 | /* function to cleanup networking stuff(doesn't do much right now) */ |
121 | void shutdown_networking(); | 121 | void shutdown_networking(); |
122 | 122 | ||
123 | /* resolves provided address to a binary data in network byte order | 123 | /* resolves provided address to a binary data in network byte order |
124 | address is ASCII null terminated string | 124 | address is ASCII null terminated string |
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(char *address); |
129 | 129 | ||
130 | #ifdef __cplusplus | 130 | #ifdef __cplusplus |
131 | } | 131 | } |