1 /*
2  * zlib License
3  * 
4  * (C) 2032 XXIV
5  * 
6  * This software is provided *as-is*, without any express or implied
7  * warranty.  In no event will the authors be held liable for any damages
8  * arising from the use of this software.
9  * 
10  * Permission is granted to anyone to use this software for any purpose,
11  * including commercial applications, and to alter it and redistribute it
12  * freely, subject to the following restrictions:
13  * 
14  * 1. The origin of this software must not be misrepresented; you must not
15  *    claim that you wrote the original software. If you use this software
16  *    in a product, an acknowledgment in the product documentation would be
17  *    appreciated but is not required.
18  * 2. Altered source versions must be plainly marked as such, and must not be
19  *    misrepresented as being the original software.
20  * 3. This notice may not be removed or altered from any source distribution.
21  */
22 module cutenet;
23 
24 extern (C):
25 
26 struct cn_client_t;
27 struct cn_server_t;
28 
29 struct cn_crypto_key_t
30 {
31     ubyte[32] key;
32 }
33 
34 struct cn_crypto_sign_public_t
35 {
36     ubyte[32] key;
37 }
38 
39 struct cn_crypto_sign_secret_t
40 {
41     ubyte[64] key;
42 }
43 
44 struct cn_crypto_signature_t
45 {
46     ubyte[64] bytes;
47 }
48 
49 enum cn_address_type_t
50 {
51     NONE = 0,
52     IPV4 = 1,
53     IPV6 = 2
54 }
55 
56 struct cn_endpoint_t
57 {
58     cn_address_type_t type;
59     ushort port;
60 
61     union cn_endpoint_u_t
62     {
63         ubyte[4] ipv4;
64         ushort[8] ipv6;
65     }
66 
67     cn_endpoint_u_t u;
68 }
69 
70 enum cn_client_state_t
71 {
72     CONNECT_TOKEN_EXPIRED = -6,
73     INVALID_CONNECT_TOKEN = -5,
74     CONNECTION_TIMED_OUT = -4,
75     CHALLENGE_RESPONSE_TIMED_OUT = -3,
76     CONNECTION_REQUEST_TIMED_OUT = -2,
77     CONNECTION_DENIED = -1,
78     DISCONNECTED = 0,
79     SENDING_CONNECTION_REQUEST = 1,
80     SENDING_CHALLENGE_RESPONSE = 2,
81     CONNECTED = 3
82 }
83 
84 struct cn_server_config_t
85 {
86     ulong application_id;
87     int max_incoming_bytes_per_second;
88     int max_outgoing_bytes_per_second;
89     int connection_timeout;
90     double resend_rate;
91     cn_crypto_sign_public_t public_key;
92     cn_crypto_sign_secret_t secret_key;
93     void* user_allocator_context;
94 }
95 
96 enum cn_server_event_type_t
97 {
98     NEW_CONNECTION = 0,
99     DISCONNECTED = 1,
100     PAYLOAD_PACKET = 2
101 }
102 
103 struct cn_server_event_t
104 {
105     cn_server_event_type_t type;
106     union cn_server_event_u_t
107     {
108         struct cn_server_event_new_connection_t
109         {
110             int client_index;
111             ulong client_id;
112             cn_endpoint_t endpoint;
113         }
114 
115         cn_server_event_new_connection_t new_connection;
116 
117         struct cn_server_event_disconnected_t
118         {
119             int client_index;
120         }
121 
122         cn_server_event_disconnected_t disconnected;
123 
124         struct cn_server_event_payload_packet_t
125         {
126             int client_index;
127             void* data;
128             int size;
129         }
130 
131         cn_server_event_payload_packet_t payload_packet;
132     }
133 
134     cn_server_event_u_t u;
135 }
136 
137 struct cn_result_t
138 {
139     int code;
140     const(char)* details;
141 }
142 
143 int cn_endpoint_init (cn_endpoint_t* endpoint, const(char)* address_and_port_string);
144 void cn_endpoint_to_string (cn_endpoint_t endpoint, char* buffer, int buffer_size);
145 int cn_endpoint_equals (cn_endpoint_t a, cn_endpoint_t b);
146 cn_crypto_key_t cn_crypto_generate_key ();
147 void cn_crypto_random_bytes (void* data, int byte_count);
148 void cn_crypto_sign_keygen (cn_crypto_sign_public_t* public_key, cn_crypto_sign_secret_t* secret_key);
149 cn_result_t cn_generate_connect_token (
150     ulong application_id,
151     ulong creation_timestamp,
152     const(cn_crypto_key_t)* client_to_server_key,
153     const(cn_crypto_key_t)* server_to_client_key,
154     ulong expiration_timestamp,
155     uint handshake_timeout,
156     int address_count,
157     const(char*)* address_list,
158     ulong client_id,
159     const(ubyte)* user_data,
160     const(cn_crypto_sign_secret_t)* shared_secret_key,
161     ubyte* token_ptr_out);
162 cn_client_t* cn_client_create (
163     ushort port,
164     ulong application_id,
165     bool use_ipv6,
166     void* user_allocator_context);
167 void cn_client_destroy (cn_client_t* client);
168 cn_result_t cn_client_connect (cn_client_t* client, const(ubyte)* connect_token);
169 void cn_client_disconnect (cn_client_t* client);
170 void cn_client_update (cn_client_t* client, double dt, ulong current_time);
171 bool cn_client_pop_packet (cn_client_t* client, void** packet, int* size, bool* was_sent_reliably);
172 void cn_client_free_packet (cn_client_t* client, void* packet);
173 cn_result_t cn_client_send (cn_client_t* client, const(void)* packet, int size, bool send_reliably);
174 cn_client_state_t cn_client_state_get (const(cn_client_t)* client);
175 const(char)* cn_client_state_string (cn_client_state_t state);
176 void cn_client_enable_network_simulator (cn_client_t* client, double latency, double jitter, double drop_chance, double duplicate_chance);
177 float cn_client_get_packet_loss_estimate (cn_client_t* client);
178 float cn_client_get_rtt_estimate (cn_client_t* client);
179 float cn_client_get_incoming_kbps_estimate (cn_client_t* client);
180 float cn_client_get_outgoing_kbps_estimate (cn_client_t* client);
181 cn_server_config_t cn_server_config_defaults ();
182 cn_server_t* cn_server_create (cn_server_config_t config);
183 void cn_server_destroy (cn_server_t* server);
184 cn_result_t cn_server_start (cn_server_t* server, const(char)* address_and_port);
185 void cn_server_stop (cn_server_t* server);
186 bool cn_server_pop_event (cn_server_t* server, cn_server_event_t* event);
187 void cn_server_free_packet (cn_server_t* server, int client_index, void* data);
188 void cn_server_update (cn_server_t* server, double dt, ulong current_time);
189 void cn_server_disconnect_client (cn_server_t* server, int client_index, bool notify_client);
190 cn_result_t cn_server_send (cn_server_t* server, const(void)* packet, int size, int client_index, bool send_reliably);
191 bool cn_server_is_client_connected (cn_server_t* server, int client_index);
192 void cn_server_set_public_ip (cn_server_t* server, const(char)* address_and_port);
193 void cn_server_enable_network_simulator (cn_server_t* server, double latency, double jitter, double drop_chance, double duplicate_chance);
194 float cn_server_get_packet_loss_estimate (cn_server_t* server, int client_index);
195 float cn_server_get_rtt_estimate (cn_server_t* server, int client_index);
196 float cn_server_get_incoming_kbps_estimate (cn_server_t* server, int client_index);
197 float cn_server_get_outgoing_kbps_estimate (cn_server_t* server, int client_index);
198 bool cn_is_error (cn_result_t result);
199 cn_result_t cn_error_failure (const(char)* details);
200 cn_result_t cn_error_success ();