-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_select_common.c
332 lines (276 loc) · 10.5 KB
/
main_select_common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
* \file main_select.c
* \author Kirill Scherba <kirill@scherba.ru>
*
* \example main_select.c
*
* This is example of Teocli library which use select to check that data is
* available ready to read. This application connect to network
* L0 server, initialize (login) at the L0 server, and send and receive data to
* from network peer.
*
* See server example parameters at:
* https://gitlab.ksproject.org/teonet/teocli/blob/master/README.md#basic-teocli-example
*
* ### This application parameters:
*
* **Usage:** ./teocli_s <client_name> <server_address> <server_port> <peer_name> [message]
*
* **Example:** ./teocli_s C3 127.0.0.1 9000 teostream "Story about this world!"
*
* ### This application algorithm:
*
* * Connect to L0 server with server_address and server_port parameters
* * Send ClientLogin request with client_name parameter
* * Send CMD_L_PEERS request to peer_name server
* * Receive peers data from peer_name server
* * Send CMD_L_ECHO request to peer_name server
* * Receive peers data from peer_name server
* * Close connection
*
* Created on October 19, 2015, 3:51 PM
*/
#if defined(_WIN32)
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if !defined(_WIN32)
#include <unistd.h>
#endif
#include <errno.h>
#include <signal.h>
#include "libteol0/teonet_l0_client.h"
#include "libteol0/teonet_l0_client_options.h"
#include "libteol0/teonet_l0_client_crypt.h"
#define TL0CNS_VERSION "0.0.1"
/**
* Application parameters structure
*/
struct app_parameters {
const char *host_name;
const char *addr;
int port;
const char *peer_name;
const char *msg;
};
// global app flags
volatile int quit_flag;
volatile int reconnect_flag;
/**
* Teonet L0 client event callback
*
* @param con
* @param event
* @param data
* @param data_len
* @param user_data
*/
void event_cb(void *con, teoLNullEvents event, void *data,
size_t data_len, void *user_data) {
const struct app_parameters *param = user_data;
// TODO: save crypto key here
switch(event) {
case EV_L_CONNECTED: {
if (data && data_len == sizeof(teoLNullConnectionStatus)) {
teoLNullConnectionStatus status = *(teoLNullConnectionStatus *)data;
if (status == CON_STATUS_CONNECTED) {
printf("Successfully connect to server\n");
// Send (1) Initialization packet to L0 server
ssize_t snd = teoLNullLogin(con, param->host_name);
if (snd == -1) perror(strerror(errno));
printf("Send %d bytes packet to L0 server, Initialization packet\n", (int)snd);
// Send (2) peer list request to peer, command CMD_L_PEERS
snd = teoLNullSend(con, CMD_L_PEERS, param->peer_name, NULL, 0);
printf( "Send %d bytes packet to L0 server to peer %s, "
"cmd = %d (CMD_L_PEERS)\n",
(int)snd, param->peer_name, CMD_L_PEERS);
// Send (3) echo request to peer, command CMD_L_ECHO
//
// Add current time to the end of message (it should be return
// back by server)
// snd = teoLNullSendEcho(con, param->peer_name, param->msg);
// if(snd == -1) perror(strerror(errno));
// printf("Send %d bytes packet to L0 server to peer %s, "
// "cmd = %d (CMD_L_ECHO), "
// "data: %s\n",
// (int)snd, param->peer_name, CMD_L_ECHO, param->msg);
// Show empty line
printf("\n");
} else {
printf("Can't connect to server, status %s\n", STRING_teoLNullConnectionStatus(status));
}
} else {
printf("Invalid EV_L_CONNECTED event, data=%p, data_len=%lu\n", data, data_len);
}
} break;
case EV_L_DISCONNECTED:
printf("Disconnected ...\n");
reconnect_flag = 1;
break;
case EV_L_RECEIVED_UNRELIABLE: // fallthrough
case EV_L_RECEIVED:
{
// Receive answer from server
const size_t rc = data_len;
teoLNullCPacket *cp = (teoLNullCPacket*) data;
printf("Receive %d bytes: %hu bytes data from L0 server, "
"from peer %s, cmd = %hhu\n",
(int)rc, cp->data_length, cp->peer_name, cp->cmd);
// Process commands
switch(cp->cmd) {
case CMD_L_PEERS_ANSWER:
{
// Show peer list
if(cp->data_length > 1) {
ksnet_arp_data_ext_ar *arp_data_ar = (ksnet_arp_data_ext_ar *)
(cp->peer_name + cp->peer_name_length);
const char *ln =
"--------------------------------------------"
"---------\n";
printf("%sPeers (%u): \n%s", ln, arp_data_ar->length, ln);
int i;
for(i = 0; i < (int)arp_data_ar->length; i++) {
printf("%-20s %-20s (%2d) %-15s %d %8.3f ms\n",
arp_data_ar->arp_data[i].name,
arp_data_ar->arp_data[i].data.type,
arp_data_ar->arp_data[i].data.data.mode,
arp_data_ar->arp_data[i].data.data.addr,
arp_data_ar->arp_data[i].data.data.port,
arp_data_ar->arp_data[i].data.data.last_triptime);
}
printf("%s", ln);
}
} break;
case CMD_L_ECHO_ANSWER:
{
printf("Got echo answer command\n");
data = cp->peer_name + cp->peer_name_length;
int trip_time = teoLNullProccessEchoAnswer(data);
// Show data
printf("Data: %s\n", (char*)data);
// Show trip time
printf("Trip time: %d ms\n\n", trip_time);
} break;
case CMD_L_L0_CRYPTO_KEY: {
// \TODO: we receive key answer here
} break;
case CMD_L_AUTH_LOGIN_ANSWER: {
printf("Got answer from authentication server\n");
const char *auth_data = (const char *)
(cp->peer_name + cp->peer_name_length);
// Show data
printf("Data: %s\n\n", auth_data);
}
break;
case CMD_L_ECHO:
{
printf("Got echo command\n");
} break;
default:
printf("Got unknown command\n");
break;
}
} break;
default:
break;
}
}
void INThandler(int sig)
{
printf("Catch CTRL-C SIGNAL !!!\n");
quit_flag = 1;
}
/**
* Main L0 Native client example function
*
* @param argc Number of arguments
* @param argv Arguments array
*
* @return
*/
int main(int argc, char** argv) {
signal(SIGINT, INThandler);
// Welcome message
printf("Teonet L0 client with Select and Event Loop Callback example version " TL0CN_VERSION " (Native TCP Client)\n\n");
// Check application parameters
if(argc < 5) {
printf("Usage: "
"%s <client_name> <server_address> <server_port> <peer_name> "
"[message]\n", argv[0]);
exit(EXIT_SUCCESS);
}
const int send_size = 10;
char *send_msg = malloc(send_size);
int i = 0;
for (i = 0; i<send_size; ++i)
send_msg[i] = 'Q';
// Teonet L0 server parameters
struct app_parameters param;
param.host_name = argv[1]; //"C3";
param.addr = argv[2]; //"127.0.0.1"; //"10.12.35.53"; //
param.port = atoi(argv[3]); //9000;
param.peer_name = argv[4]; //"teostream";
if(argc > 5) param.msg = argv[5];
else param.msg = send_msg;
PROTOCOL transport_proto = getenv("FORCE_TCP") ? TCP : TRUDP;
const char* cypher = getenv("TEO_CYPHER");
if (cypher != NULL && cypher[0] != 0) {
teoLNullEncryptionProtocol cypher_code = ENC_PROTO_DISABLED;
if (strcmp("ECDH_AES_128_V1", cypher) == 0) {
cypher_code = ENC_PROTO_ECDH_AES_128_V1;
}
teoLNUllSetOption_EncryptionProtocol(cypher_code);
}
// Initialize L0 Client library
teoLNullInit();
quit_flag = 0;
while(!quit_flag) {
// Connect to L0 server
teoLNullConnectData *con = teoLNullConnectE(param.addr, param.port,
event_cb, ¶m, transport_proto);
reconnect_flag = 0;
if(con->status >= 0) {
const int timeout = 1000;
uint64_t nextPing = teoGetTimestampFull();
// Event loop
while(teoLNullReadEventLoop(con, timeout)) {
if (quit_flag || reconnect_flag) {
break;
}
switch (con->status) {
case CON_STATUS_NOT_CONNECTED: {
// wait to be connected
} continue;
case CON_STATUS_CONNECTED:{
// run
// Send Echo command every second
uint64_t now = teoGetTimestampFull();
if (now > nextPing) {
teoLNullSendEcho(con, param.peer_name, param.msg);
// teoLNullSendUnreliable(con, CMD_L_PEERS, param.peer_name, NULL, 0);
nextPing = now + 1000000;
}
} continue;
default:{
// Some error condition, go to reconnect
} break;
}
// check if reconnecting
break;
}
}
printf("con->status=%s\n", STRING_teoLNullConnectionStatus(con->status));
if (!quit_flag) {
printf("reconnect cooldown\n");
teoLNullSleep(2000);
}
// Close connection
teoLNullDisconnect(con);
}
// Cleanup L0 Client library
teoLNullCleanup();
free(send_msg);
return (EXIT_SUCCESS);
}