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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
|
/*
**
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "v_cmd_gen.h"
#if !defined V_GENERATE_FUNC_MODE
#include "verse.h"
#include "v_cmd_buf.h"
#include "v_network_out_que.h"
#include "v_network.h"
#include "v_connection.h"
#include "v_encryption.h"
#include "v_util.h"
extern void verse_send_packet_ack(uint32 packet_id);
static void v_send_hidden_connect_contact(void) /* Stage 0: Clinets inital call to connect to host */
{
uint8 buf[V_ENCRYPTION_LOGIN_KEY_HALF_SIZE + 4 + 1 + 1], *key;
unsigned int i, buffer_pos = 0;
buffer_pos += vnp_raw_pack_uint32(&buf[buffer_pos], 0);/* Packing the packet id */
buffer_pos += vnp_raw_pack_uint8(&buf[buffer_pos], 0);/* Packing the command */
buffer_pos += vnp_raw_pack_uint8(&buf[buffer_pos], 0);/* Stage 0 */
key = v_con_get_my_key();
for(i = 0; i < V_ENCRYPTION_LOGIN_KEY_SIZE; i++)
buffer_pos += vnp_raw_pack_uint8(&buf[buffer_pos], key[V_ENCRYPTION_LOGIN_PUBLIC_START + i]);/* Packing the command */
for(i = 0; i < V_ENCRYPTION_LOGIN_KEY_SIZE; i++)
buffer_pos += vnp_raw_pack_uint8(&buf[buffer_pos], key[V_ENCRYPTION_LOGIN_N_START + i]);/* Packing the command */
v_n_send_data(v_con_get_network_address(), buf, buffer_pos);
}
static void v_send_hidden_connect_send_key(void) /* Stage 1: Hosts reply to any atempt to connect */
{
uint8 buf[V_ENCRYPTION_LOGIN_KEY_SIZE * 3 + 4 + 1 + 1 + 1 + 4 + 4], *host_id;
unsigned int i, buffer_pos = 0, s, f;
buffer_pos += vnp_raw_pack_uint32(&buf[buffer_pos], 0);/* Packing the packet id */
buffer_pos += vnp_raw_pack_uint8(&buf[buffer_pos], 0);/* Packing the command */
buffer_pos += vnp_raw_pack_uint8(&buf[buffer_pos], 1);/* Stage 1 */
buffer_pos += vnp_raw_pack_uint8(&buf[buffer_pos], V_RELEASE_NUMBER);/* version */
v_n_get_current_time(&s, &f);
buffer_pos += vnp_raw_pack_uint32(&buf[buffer_pos], s);/* time, seconds */
buffer_pos += vnp_raw_pack_uint32(&buf[buffer_pos], f);/* time, fraction */
host_id = v_con_get_host_id();
for(i = 0; i < V_ENCRYPTION_LOGIN_KEY_SIZE; i++)
buffer_pos += vnp_raw_pack_uint8(&buf[buffer_pos], host_id[V_ENCRYPTION_LOGIN_PUBLIC_START + i]);
for(i = 0; i < V_ENCRYPTION_LOGIN_KEY_SIZE; i++)
buffer_pos += vnp_raw_pack_uint8(&buf[buffer_pos], host_id[V_ENCRYPTION_LOGIN_N_START + i]);
v_n_send_data(v_con_get_network_address(), buf, buffer_pos);
}
static void v_send_hidden_connect_login(void) /* Stage 2: clients sends encrypted name and password */
{
uint8 buf[1500], *key, name_pass[V_ENCRYPTION_LOGIN_KEY_SIZE], encrypted_key[V_ENCRYPTION_LOGIN_KEY_SIZE];
const char *name, *pass;
unsigned int buffer_pos = 0, i;
buffer_pos += vnp_raw_pack_uint32(&buf[buffer_pos], 1);/* Packing the packet id */
buffer_pos += vnp_raw_pack_uint8(&buf[buffer_pos], 0);/* Packing the command */
buffer_pos += vnp_raw_pack_uint8(&buf[buffer_pos], 2);/* Stage 2 */
name = v_con_get_name();
/* Pad data area with randomness. */
for(i = 0; i < sizeof name_pass; i++)
name_pass[i] = rand() >> 13;
v_strlcpy(name_pass, name, V_ENCRYPTION_LOGIN_KEY_SIZE / 2);
pass = v_con_get_pass();
v_strlcpy(name_pass + V_ENCRYPTION_LOGIN_KEY_SIZE / 2, pass, V_ENCRYPTION_LOGIN_KEY_SIZE / 2);
/* Make sure last (MSB) byte is clear, to guarantee that data < key for RSA math. */
name_pass[sizeof name_pass - 1] = 0;
key = v_con_get_other_key();
v_e_connect_encrypt(encrypted_key, name_pass, &key[V_ENCRYPTION_LOGIN_PUBLIC_START], &key[V_ENCRYPTION_LOGIN_N_START]);
for(i = 0; i < V_ENCRYPTION_LOGIN_KEY_SIZE; i++)
buffer_pos += vnp_raw_pack_uint8(&buf[buffer_pos], encrypted_key[i]);
v_n_send_data(v_con_get_network_address(), buf, buffer_pos);
}
static void v_send_hidden_connect_accept(void) /* Host accepts Clients connectionatempt and sends over data encryption key */
{
uint8 buf[1500], *client_key, encrypted[V_ENCRYPTION_DATA_KEY_SIZE];
unsigned int i, buffer_pos = 0;
buffer_pos += vnp_raw_pack_uint32(&buf[buffer_pos], 1);/* Packing the packet id */
buffer_pos += vnp_raw_pack_uint8(&buf[buffer_pos], 1);/* Packing the command */
buffer_pos += vnp_raw_pack_uint32(&buf[buffer_pos], verse_session_get_avatar());
client_key = v_con_get_other_key();
v_e_connect_encrypt(encrypted, v_con_get_data_key(), &client_key[V_ENCRYPTION_LOGIN_PUBLIC_START], &client_key[V_ENCRYPTION_LOGIN_N_START]);
for(i = 0; i < V_ENCRYPTION_DATA_KEY_SIZE; i++)
buffer_pos += vnp_raw_pack_uint8(&buf[buffer_pos], encrypted[i]);
v_n_send_data(v_con_get_network_address(), buf, buffer_pos);
}
static void v_send_hidden_connect_terminate(VNetworkAddress *address, unsigned int packet_id, const char *bye) /* Host accepts Clients connectionatempt and sends over data encryption key */
{
uint8 buf[1500];
unsigned int buffer_pos = 0;
buffer_pos += vnp_raw_pack_uint32(&buf[buffer_pos], packet_id);/* Packing the packet id */
buffer_pos += vnp_raw_pack_uint8(&buf[buffer_pos], 2);/* Packing the command */
buffer_pos += vnp_raw_pack_string(&buf[buffer_pos], bye, 512); /* pack message */
v_e_data_encrypt_command(buf, sizeof (uint32), buf + sizeof (uint32), buffer_pos, v_con_get_data_key());
v_n_send_data(address, buf, buffer_pos);
}
VSession verse_send_connect(const char *name, const char *pass, const char *address, const uint8 *expected_key)
{
uint8 *my_key, *key;
unsigned int i;
VNetworkAddress a;
VSession *session;
if(v_n_set_network_address(&a, address))
{
#if defined(V_PRINT_SEND_COMMANDS)
char ip_string[32];
#endif
session = v_con_connect(a.ip, a.port, V_CS_CONTACT);
#if defined(V_PRINT_SEND_COMMANDS)
v_n_get_address_string(&a, ip_string);
printf("send: %p = verse_send_connect(name = %s, pass = %s, address = %s (%s), expected_key = %p)\n", session, name, pass, address, ip_string, expected_key);
#endif
v_con_set_name_pass(name, pass);
if(expected_key != NULL)
{
key = malloc((sizeof *key) * V_ENCRYPTION_LOGIN_KEY_HALF_SIZE);
for(i = 0; i < V_ENCRYPTION_LOGIN_KEY_HALF_SIZE; i++)
key[i] = expected_key[i];
*v_con_get_expected_key() = key;
}
my_key = v_con_get_my_key();
v_e_connect_create_key(&my_key[V_ENCRYPTION_LOGIN_PRIVATE_START], &my_key[V_ENCRYPTION_LOGIN_PUBLIC_START], &my_key[V_ENCRYPTION_LOGIN_N_START]);
v_send_hidden_connect_contact();
v_con_inqueue_timer_update(); /* Reset timer in connection's in queue, above takes a while. */
return session;
}
else
{
#if defined(V_PRINT_SEND_COMMANDS)
printf("send: NULL = verse_send_connect(name = %s, pass = %s, address = %s (Unressolved DNS), key = %p);\n", name, pass, address, key);
#endif
return NULL;
}
}
void v_update_connection_pending(boolean resend)
{
VSession (* func_connect)(void *user_data, const char *name, const char *pass, const char *address, const uint8 *key);
VSession (* func_connect_accept)(void *user_data, VNodeID avatar, char *address, uint8 *host_id);
void (* func_connect_termanate)(void *user_data, char *address, const char *bye);
char address_string[32];
switch(v_con_get_connect_stage())
{
case V_CS_CONTACT : /* client tries to contact host */
if(resend)
v_send_hidden_connect_contact();
break;
case V_CS_CONTACTED : /* Host replies with challange */
if(resend)
v_send_hidden_connect_send_key();
break;
case V_CS_PENDING_ACCEPT : /* Client sends login */
if(resend)
v_send_hidden_connect_login();
break;
case V_CS_PENDING_HOST_CALLBACK : /* Host got login waits for accept connect callback */
v_con_set_connect_stage(V_CS_PENDING_DECISION);
func_connect = v_fs_get_user_func(0);
v_n_get_address_string(v_con_get_network_address(), address_string);
#if defined(V_PRINT_RECEIVE_COMMANDS)
printf("receive: verse_send_connect(address = %s, name = %s, pass = %s, key = NULL); callback = %p\n", address_string, v_con_get_name(), v_con_get_pass(), func_connect);
#endif
if(func_connect != 0)
func_connect(v_fs_get_user_data(0), v_con_get_name(), v_con_get_pass(), address_string, NULL);
break;
case V_CS_PENDING_CLIENT_CALLBACK_ACCEPT : /* Host got login waits for accept connect callback */
v_con_set_connect_stage(V_CS_CONNECTED);
func_connect_accept = v_fs_get_user_func(1);
v_n_get_address_string(v_con_get_network_address(), address_string);
#if defined(V_PRINT_RECEIVE_COMMANDS)
printf("receive: func_connect_accept(avatar = %u, address = %s, name = %s, pass = %s, key = NULL); callback = %p\n",
verse_session_get_avatar(), address_string, v_con_get_name(), v_con_get_pass(), func_connect);
#endif
if(func_connect_accept != 0)
func_connect_accept(v_fs_get_user_data(1), verse_session_get_avatar(), address_string, NULL);
break;
case V_CS_PENDING_CLIENT_CALLBACK_TERMINATE : /* Host got login waits for accept connect callback */
v_con_set_connect_stage(V_CS_CONNECTED);
func_connect_termanate = v_fs_get_user_func(2);
v_n_get_address_string(v_con_get_network_address(), address_string);
#if defined(V_PRINT_RECEIVE_COMMANDS)
printf("receive: func_connect_termanate(address = %s, bye = %s); callback = %p\n", address_string, "no message", func_connect);
#endif
if(func_connect_termanate != 0)
func_connect_termanate(v_fs_get_user_data(2), address_string, "no message");
break;
default:
;
}
}
void v_unpack_connection(const char *buf, unsigned int buffer_length) /* un packing all stages of connect command */
{
unsigned int buffer_pos, i, pack_id;
uint32 seconds, fractions, pre;
uint8 /*key[V_ENCRYPTION_LOGIN_KEY_SIZE], */stage, cmd_id, version;
if(buffer_length < 5)
return;
buffer_pos = vnp_raw_unpack_uint32(buf, &pack_id);
buffer_pos += vnp_raw_unpack_uint8(&buf[buffer_pos], &cmd_id);
pre = v_con_get_connect_stage();
if(cmd_id == 0)
{
buffer_pos += vnp_raw_unpack_uint8(&buf[buffer_pos], &stage);
printf(" Handling connection, stage %u\n", stage);
if(stage == V_CS_IDLE && V_CS_IDLE == v_con_get_connect_stage()) /* reseved by host */
{
uint8 *other_key, *my_key;
verse_send_packet_ack(pack_id);
my_key = v_con_get_my_key();
v_con_set_data_key(v_e_data_create_key());
other_key = v_con_get_other_key();
for(i = 0; i < V_ENCRYPTION_LOGIN_KEY_SIZE; i++)
buffer_pos += vnp_raw_unpack_uint8(&buf[buffer_pos], &other_key[V_ENCRYPTION_LOGIN_PUBLIC_START + i]);/* Packing the command */
for(i = 0; i < V_ENCRYPTION_LOGIN_KEY_SIZE; i++)
buffer_pos += vnp_raw_unpack_uint8(&buf[buffer_pos], &other_key[V_ENCRYPTION_LOGIN_N_START + i]);/* Packing the command */
v_con_set_connect_stage(V_CS_CONTACTED);
v_send_hidden_connect_send_key();
return;
}
if(stage == V_CS_CONTACT && V_CS_CONTACT == v_con_get_connect_stage())
{
uint8 *other_key; /* *host_id, *my_key, a[V_ENCRYPTION_LOGIN_KEY_SIZE], b[V_ENCRYPTION_LOGIN_KEY_SIZE];*/
verse_send_packet_ack(pack_id);
buffer_pos += vnp_raw_unpack_uint8(&buf[buffer_pos], &version);
if(version != V_RELEASE_NUMBER)
{
/* char error_message[128];
func_connect_deny = v_fs_get_user_func(2);
#if defined(V_PRINT_RECEIVE_COMMANDS)
printf("receive: verse_send_connect_deny(Host is running version %u you are running version %u); callback = %p\n", (uint32)version, (uint32)V_RELEASE_NUMBER func_connect_deny);
#endif
if(func_connect_deny != NULL)
{
sprintf(error_message, "Host is running version %u you are running version %u", (uint32)version, (uint32)V_RELEASE_NUMBER);
func_connect_deny(v_fs_get_user_data(2), error_message);
}*/
return;
}
buffer_pos += vnp_raw_unpack_uint32(&buf[buffer_pos], &seconds);
buffer_pos += vnp_raw_unpack_uint32(&buf[buffer_pos], &fractions);
v_con_set_time(seconds, fractions);
other_key = v_con_get_other_key();
for(i = 0; i < V_ENCRYPTION_LOGIN_KEY_SIZE; i++)
buffer_pos += vnp_raw_unpack_uint8(&buf[buffer_pos], &other_key[V_ENCRYPTION_LOGIN_PUBLIC_START + i]);
for(i = 0; i < V_ENCRYPTION_DATA_KEY_SIZE; i++)
buffer_pos += vnp_raw_unpack_uint8(&buf[buffer_pos], &other_key[V_ENCRYPTION_LOGIN_N_START + i]);
v_con_set_connect_stage(V_CS_PENDING_ACCEPT);
v_send_hidden_connect_login();
return;
}
#if 0
for(i = 0; i < V_ENCRYPTION_LOGIN_KEY_HALF_SIZE && encrypted_key[i] == 0; i++);
if(i < 0)
{
other_key = v_con_get_my_key();
v_e_connect_encrypt(decrypted_key, encrypted_key, &other_key[V_ENCRYPTION_LOGIN_PUBLIC_START + i], &other_key[V_ENCRYPTION_LOGIN_N_START + i]);
for(i = 0; i < V_ENCRYPTION_LOGIN_KEY_HALF_SIZE && my_key[V_ENCRYPTION_LOGIN_PUBLIC_START + i] == decrypted_key[i]; i++);
if(i < 0) /* Host is not who it appers top be */
{
func_connect_deny = v_fs_get_user_func(2);
#if defined(V_PRINT_RECEIVE_COMMANDS)
printf("receive: verse_send_connect_deny(Host failed identity check); callback = %p\n", func_connect_deny);
#endif
if(func_connect_deny != NULL)
func_connect_deny(v_fs_get_user_data(2), "Host failed identity check");
return;
}
}
#endif
if(stage == V_CS_CONTACTED && V_CS_CONTACTED == v_con_get_connect_stage()) /* reseved by host */
{
char *host_id, unpack[V_ENCRYPTION_LOGIN_KEY_SIZE], data[V_ENCRYPTION_LOGIN_KEY_SIZE];
VNetworkAddress *address;
verse_send_packet_ack(pack_id);
address = v_con_get_network_address();
for(i = 0; i < V_ENCRYPTION_LOGIN_KEY_SIZE; i++)
buffer_pos += vnp_raw_unpack_uint8(&buf[buffer_pos], &data[i]);
host_id = v_con_get_host_id();
v_e_connect_encrypt(unpack, data, &host_id[V_ENCRYPTION_LOGIN_PRIVATE_START], &host_id[V_ENCRYPTION_LOGIN_N_START]);
v_con_set_name_pass(unpack, &unpack[V_ENCRYPTION_LOGIN_KEY_SIZE / 2]);
v_con_set_connect_stage(V_CS_PENDING_HOST_CALLBACK);
return;
}
}
if(cmd_id == 1 && V_CS_PENDING_ACCEPT == v_con_get_connect_stage()) /* reseved by client */
{
uint8 *my_key, key[V_ENCRYPTION_DATA_KEY_SIZE], decrypted[V_ENCRYPTION_DATA_KEY_SIZE];
uint32 avatar;
verse_send_packet_ack(pack_id);
buffer_pos += vnp_raw_unpack_uint32(&buf[buffer_pos], &avatar);
v_con_set_avatar(avatar);
for(i = 0; i < V_ENCRYPTION_DATA_KEY_SIZE; i++)
buffer_pos += vnp_raw_unpack_uint8(&buf[buffer_pos], &key[i]);
my_key = v_con_get_my_key();
v_e_connect_encrypt(decrypted, key, &my_key[V_ENCRYPTION_LOGIN_PRIVATE_START], &my_key[V_ENCRYPTION_LOGIN_N_START]);
v_con_set_data_key(decrypted);
v_con_set_connect_stage(V_CS_PENDING_CLIENT_CALLBACK_ACCEPT);
v_send_hidden_connect_send_key();
return;
}
if(cmd_id == 2 && V_CS_PENDING_ACCEPT == v_con_get_connect_stage()) /* reseved by client */
{
verse_send_packet_ack(pack_id);
/* buffer_pos += vnp_raw_unpack_string(&buf[buffer_pos], name, 512, buffer_length - buffer_pos);
*/ v_con_set_connect_stage(V_CS_PENDING_CLIENT_CALLBACK_TERMINATE);
return;
}
}
VSession verse_send_connect_accept(VNodeID avatar, const char *address, uint8 *host_id)
{
VNetworkAddress a;
#if defined(V_PRINT_SEND_COMMANDS)
printf("send: verse_send_connect_accept(avatar = %u, address = %s, host_id = NULL);\n", avatar, address);
#endif
if(!v_n_set_network_address(&a, address))
return NULL;
if(v_co_switch_connection(a.ip, a.port))
{
if(v_con_get_connect_stage() != V_CS_PENDING_DECISION)
return NULL;
v_con_set_avatar(avatar);
v_con_set_connect_stage(V_CS_CONNECTED);
v_send_hidden_connect_accept();
return v_con_get_network_queue();
}
return NULL;
}
void v_callback_connect_terminate(const char *bye)
{
void (* func_connect_terminate)(void *user_data, char *address, const char *bye);
char address_string[32];
printf("terminate (%s)\n", bye);
func_connect_terminate = v_fs_get_user_func(2);
v_n_get_address_string(v_con_get_network_address(), address_string);
#if defined(V_PRINT_RECEIVE_COMMANDS)
printf("receive: verse_send_connect_terminate(address = %s, bye = %s); callback = %p\n", address_string, bye, func_connect_terminate);
#endif
if(func_connect_terminate != 0)
func_connect_terminate(v_fs_get_user_data(2), address_string, bye);
}
void verse_send_connect_terminate(const char *address, const char *bye)
{
VNetworkAddress a;
#if defined(V_PRINT_RECEIVE_COMMANDS)
printf("send: verse_send_connect_terminate(address = %s, bye = %s);\n", address, bye);
#endif
if(address == NULL)
v_send_hidden_connect_terminate(v_con_get_network_address(), v_noq_get_next_out_packet_id(v_con_get_network_queue()), bye);
else if(!v_n_set_network_address(&a, address))
return;
else if(v_co_switch_connection(a.ip, a.port))
v_send_hidden_connect_terminate(v_con_get_network_address(), v_noq_get_next_out_packet_id(v_con_get_network_queue()), bye);
if(v_con_get_connect_stage() != V_CS_PENDING_DECISION)
verse_session_destroy(v_con_get_network_queue());
}
void verse_send_ping(const char *address, const char *message)
{
VNetworkAddress a;
if(v_n_set_network_address(&a, address))
{
unsigned int buffer_pos = 0;
uint8 buf[1500];
buffer_pos += vnp_raw_pack_uint32(&buf[buffer_pos], 0);/* Packing the Packet id */
buffer_pos += vnp_raw_pack_uint8(&buf[buffer_pos], 5);/* Packing the command */
#if defined V_PRINT_SEND_COMMANDS
printf("send: verse_send_ping(address = %s text = %s);\n", address, message);
#endif
buffer_pos += vnp_raw_pack_string(&buf[buffer_pos], message, 1400);
v_n_send_data(&a, buf, buffer_pos);
}
#if defined V_PRINT_SEND_COMMANDS
else
printf("send: verse_send_ping(address = %s (FAULTY) message = %s);\n", address, message);
#endif
}
unsigned int v_unpack_ping(const char *buf, size_t buffer_length)
{
unsigned int buffer_pos = 0;
void (* func_ping)(void *user_data, const char *address, const char *text);
char address[64];
char message[1400];
func_ping = v_fs_get_user_func(5);
v_n_get_address_string(v_con_get_network_address(), address);
buffer_pos += vnp_raw_unpack_string(&buf[buffer_pos], message, 1400, buffer_length - buffer_pos);
#if defined V_PRINT_RECEIVE_COMMANDS
printf("receive: verse_send_ping(address = %s message = %s ); callback = %p\n", address, message, v_fs_get_user_func(5));
#endif
if(func_ping != NULL)
func_ping(v_fs_get_user_data(5), address, message);
return buffer_pos;
}
typedef struct {
uint32 ip;
uint16 port;
char message[1400];
void *next;
} VPingCommand;
static VPingCommand *v_ping_commands = NULL;
boolean v_connect_unpack_ping(const char *buf, size_t buffer_length, uint32 ip, uint16 port)
{
if(buffer_length > 5)
{
unsigned int buffer_pos = 0;
uint8 cmd_id;
uint32 pack_id;
buffer_pos = vnp_raw_unpack_uint32(&buf[buffer_pos], &pack_id);
buffer_pos += vnp_raw_unpack_uint8(&buf[buffer_pos], &cmd_id);
if(cmd_id == 5)
{
if(NULL != v_fs_get_user_func(5))
{
VPingCommand *pc;
pc = malloc(sizeof *pc);
pc->ip = ip;
pc->port = port;
vnp_raw_unpack_string(&buf[buffer_pos], pc->message,
sizeof pc->message, buffer_length - buffer_pos);
pc->next = v_ping_commands;
v_ping_commands = pc;
}
return TRUE;
}
}
return FALSE;
}
void v_ping_update(void)
{
VPingCommand *cp;
void (* func_ping)(void *user_data, const char *address, const char *text);
VNetworkAddress a;
char address[64];
func_ping = v_fs_get_user_func(5);
while(v_ping_commands != NULL)
{
cp = v_ping_commands->next;
a.ip = v_ping_commands->ip;
a.port = v_ping_commands->port;
v_n_get_address_string(&a, address);
#if defined V_PRINT_RECEIVE_COMMANDS
printf("receive: verse_send_ping(address = %s message = %s ); callback = %p\n", address, v_ping_commands->message, v_fs_get_user_func(5));
#endif
if(func_ping != NULL)
func_ping(v_fs_get_user_data(5), address, v_ping_commands->message);
free(v_ping_commands);
v_ping_commands = cp;
}
}
#endif
|