-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect_handler.c
690 lines (537 loc) · 20.5 KB
/
connect_handler.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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/*===========================================================================================
# Copyright (C) 2018 Nafiu Shaibu.
# Purpose:
#-------------------------------------------------------------------------------------------
# This is a free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your option)
# any later version.
# This is distributed in the hopes that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#===========================================================================================
*/
#include "connect_handler.h"
static inline void send_msg_dontwait(struct thread_block *blk, const char *msg)
{
send(blk->socket, msg, strlen(msg), MSG_DONTWAIT);
}
static inline void init_connection_handler(struct thread_block *blk)
{
list_lock_acquire(blk->tid_list);
if ( blk->tid_list->list_len > MAX_CONN ) {
blk->user_auth = USER_TO_EXIT;
close(blk->socket);
list_lock_release(blk->tid_list);
pthread_cancel( pthread_self() );
pthread_testcancel();
} else {
append_list_node(blk->tid_list, blk->tid);
list_lock_release(blk->tid_list);
}
}
static int registrator(struct thread_block *blk, const char *name, const char *passwd)
{
char db_query[MYSQL_QUERY_BUFF];
MYSQL_RES *mysql_res = NULL;
sprintf( db_query,
"SELECT userid FROM %s.users WHERE username = '%s' AND userpasswd = '%s'",
my_info.database_name, name, passwd);
if ( mysql_query(blk->con, db_query) !=0 ) {
log_errors( blk->tid,
MYSQL_ERRORS,
DO_EXIT,
NO_WRITE,
LOGS_FATAL_ERRORS,
MYSQL_QUERY_FAILED,
blk,
(void (*)(void*))destroy_thread_node );
}
mysql_res = mysql_store_result(blk->con);
int test = mysql_num_rows(mysql_res); //test whether user is unique
if ( test == 0 ) {
sprintf( db_query,
"INSERT INTO %s.users(username, userpasswd) VALUES ( '%s', '%s')",
my_info.database_name, name, passwd);
if ( mysql_query(blk->con, db_query) !=0 ) {
log_errors( blk->tid,
MYSQL_ERRORS,
DO_EXIT,
NO_WRITE,
LOGS_FATAL_ERRORS,
MYSQL_QUERY_FAILED,
blk,
(void (*)(void*))destroy_thread_node );
} else
return 0;
}
return -1;
}
static int authenticator(struct thread_block *blk, const char name[], const char passwd[])
{
char db_query[MYSQL_QUERY_BUFF];
MYSQL_RES *mysql_res = NULL;
MYSQL_ROW mysql_row;
sprintf( db_query,
"SELECT userid, username FROM %s.users WHERE username = '%s' AND userpasswd = '%s'",
my_info.database_name, name, passwd);
if ( mysql_query(blk->con, db_query) !=0 ) {
log_errors( blk->tid,
MYSQL_ERRORS,
DO_EXIT,
NO_WRITE,
LOGS_FATAL_ERRORS,
MYSQL_QUERY_FAILED,
blk,
(void (*)(void*))destroy_thread_node );
}
mysql_res = mysql_use_result(blk->con);
if (mysql_res == NULL) {
mysql_free_result(mysql_res);
log_errors( blk->tid,
MYSQL_ERRORS,
DO_EXIT,
NO_WRITE,
LOGS_FATAL_ERRORS,
MYSQL_RESULT_CANT_READ,
blk,
(void (*)(void*))destroy_thread_node );
}
mysql_row = mysql_fetch_row(mysql_res); //get username and id in array
if ( mysql_row == NULL ) {
mysql_free_result(mysql_res);
log_errors( blk->tid,
MYSQL_ERRORS,
DONT_EXIT,
NO_WRITE,
LOGS_INFO,
MYSQL_NO_USER,
NULL,
error_ignore );
return -1; //no such user
}
char tmp[10];
unsigned long *len = mysql_fetch_lengths(mysql_res);
bcopy( mysql_row[0], tmp, len[0] ); //set thread block user id
blk->userid = atoi(tmp);
struct thread_block *node = getAddrFromaddrTable(blk->userid);
if ( node != NULL ) { //if user has already log in return
mysql_free_result(mysql_res);
return -1;
}
bcopy( mysql_row[1], &(blk->username), len[1] ); //set thread block user name
mysql_free_result(mysql_res);
pthread_mutex_unlock(&addrTable_mutex);
number_of_users = number_of_users + 1;
insertInAddrTable(blk->userid, (void*)blk); //put userid and thread_block address in address table
blk->user_auth = USER_AUTH; //set that user has been authenticated
return 0;
}
static void authenticate_user(struct thread_block *blk, struct packet *pk)
{
char passwd[PASSWD_BUF_SIZE], name[NAME_BUF_SIZE], str[MAX_PACKET_SIZE];
char *saveptr, *pword = NULL, *nword = NULL;
strcpy(str, pk->msg);
destroy_packet(pk);
nword = strtok_r(str, ":", &saveptr);
pword = strtok_r(NULL, ":", &saveptr);
if ( nword != NULL && pword != NULL ) {
strcpy(name, nword);
strcpy(passwd, pword);
int ret = authenticator(blk, name, passwd);
if ( ret == 0 ) {
sprintf(str, "|%d|%d|-1|-1|ACK|", ACK_PACKET, blk->userid);
send_msg_dontwait(blk, str);
} else { //authentication failed
sprintf(str, "|%d|-1|-1|-1|FIN|", FIN_PACKET);
send_msg_dontwait(blk, str);
//destroy_thread_node(blk);
blk->user_auth = USER_TO_EXIT;
pthread_cancel( pthread_self() );
}
} else { //if any word is null exit
sprintf(str, "|%d|-1|-1|-1|FIN|", FIN_PACKET); /*send fin packet and close*/
send_msg_dontwait(blk, str); /*connects*/
//destroy_thread_node(blk);
blk->user_auth = USER_TO_EXIT;
pthread_cancel( pthread_self() );
}
}
static void register_new_user(struct thread_block *blk, struct packet *pk)
{
char passwd[PASSWD_BUF_SIZE], name[NAME_BUF_SIZE], str[MAX_PACKET_SIZE];
char *saveptr, *pword = NULL, *nword = NULL;
//May have email address
strcpy(str, pk->msg);
destroy_packet(pk);
nword = strtok_r(str, ":", &saveptr);
pword = strtok_r(NULL, ":", &saveptr);
if ( nword != NULL && pword != NULL ) {
strncpy(name, nword, NAME_BUF_SIZE);
strncpy(passwd, pword, PASSWD_BUF_SIZE);
int ret = registrator(blk, name, passwd);
if ( ret == 0 ) {
sprintf(str, "|%d|-1|-1|-1|ACK|", ACK_PACKET );
send_msg_dontwait(blk, str);
} else { //authentication failed
sprintf(str, "|%d|-1|-1|-1|FIN|", FIN_PACKET);
send_msg_dontwait(blk, str);
//destroy_thread_node(blk);
//pthread_exit(NULL);
}
} else { //if any word is null exit
sprintf(str, "|%d|-1|-1|-1|FIN|", FIN_PACKET); /*send fin packet and close*/
send_msg_dontwait(blk, str); /*connects*/
//destroy_thread_node(blk);
//pthread_exit(NULL);
}
}
static void NOT_YET_AUTHENTICATED_EXIT(struct thread_block *block, struct packet *pk)
{
if ( block->user_auth == USER_NOT_AUTH ) {
//destroy_thread_node(block);
destroy_packet(pk);
pthread_cancel(pthread_self());
}
}
static void interpret_packets(struct thread_block *blk, struct packet *pk)
{
char db_query[MYSQL_QUERY_BUFF+1], str[MAX_PACKET_SIZE+1];
char *saveptr, *latti_word=NULL, *longi_word=NULL; //tmp storage for strtok_r calls
char *mem=NULL; //This is used when dynamic memory is required and it must be freed in the scope used
struct thread_block *node = NULL; //for addrTable
MYSQL_RES *mysql_res = NULL;
MYSQL_ROW mysql_row;
switch(pk->ptype) {
case REG_PACKET: register_new_user(blk, pk); break;
case AUTH_PACKET: authenticate_user(blk, pk); break;
case LOGOUT_PACKET:
case CLOSE_PACKET:
NOT_YET_AUTHENTICATED_EXIT(blk, pk);
if ( blk->userid == pk->sender_id ) {
blk->user_auth = USER_TO_EXIT;
pthread_cancel( pthread_self() );
}
//destroy_packet(pk);
break;
case GET_ALL_USERS_PACKET:
NOT_YET_AUTHENTICATED_EXIT(blk, pk);
set_revision_num(blk);
int cli_revision_num = atoi((const char*)pk->msg);
if ( blk->revision_number > cli_revision_num) {
snprintf( db_query, MYSQL_QUERY_BUFF,
"SELECT userid, username FROM %s.users WHERE userid = %d ORDER BY userid ",
my_info.database_name, cli_revision_num + 1 );
if ( mysql_query(blk->con, db_query) !=0 ) {
log_errors( blk->tid,
MYSQL_ERRORS,
DONT_EXIT,
NO_WRITE,
LOGS_FATAL_ERRORS,
MYSQL_QUERY_FAILED,
NULL,
error_ignore );
}
mysql_res = mysql_store_result(blk->con);
if (mysql_res == NULL) {
mysql_free_result(mysql_res);
log_errors( blk->tid,
MYSQL_ERRORS,
DONT_EXIT,
NO_WRITE,
LOGS_FATAL_ERRORS,
MYSQL_RESULT_CANT_READ,
NULL,
error_ignore );
}
mysql_row = mysql_fetch_row(mysql_res);
memset(str, '\0', MAX_PACKET_SIZE);
snprintf(str, MAX_PACKET_SIZE,
"|%d|%d|%d|%d|%s:%s|",
USERS_PACKET,
blk->userid, blk->userid,
COORD_DATA, mysql_row[0], mysql_row[1]);
server_debug("Sending user: %s", str);
send_msg_dontwait(blk, str);
} else {
memset(str, '\0', MAX_PACKET_SIZE);
snprintf(str, MAX_PACKET_SIZE,
"|%d|%d|%d|0|0|",
FIN_PACKET,
blk->userid, blk->userid
);
server_debug("Sending user: %s", str);
send_msg_dontwait(blk, str);
}
break;
case MSG_PACKET:
NOT_YET_AUTHENTICATED_EXIT(blk, pk);
if ( pk->receiver_id == blk->userid ) { //if I am the receiver
snprintf( str, MAX_PACKET_SIZE,
"|%d|%d|%d|%d|%s|", /*format of packet /30/3/1/0/DATA*/
MSG_PACKET,
pk->sender_id,
pk->receiver_id,
pk->tmsg,
(char*)pk->msg
);
send_msg_dontwait(blk, str);
} else { //i am not the receiver, i am the sender
pthread_mutex_lock( &addrTable_mutex );
node = getAddrFromaddrTable(pk->receiver_id);
pthread_mutex_unlock( &addrTable_mutex );
if ( node != NULL ) { //if receiver is online
//append packet to reciver msg queue
pthread_mutex_lock( &(node->in_queue->queue_lock) ); //get queue_lock to append msg
enqueue(node->in_queue, pk); //append msg
pthread_mutex_unlock( &(node->in_queue->queue_lock) );
} else {
//write to database for messages
snprintf( db_query, MYSQL_QUERY_BUFF,
"INSERT INTO %s.messages(sender_id, receiver_id, time_sent, time_received, msg_type, message) \
VALUES(%d, '%d', '%ld', '%ld', '%d', '%s' )",
my_info.database_name, /*The database name*/
pk->sender_id, /*The sender id*/
pk->receiver_id, /*The receiver id*/
time(NULL), /*[sent time]Number of seconds since unix epoch*/
(time_t)-1, /*[receiver time]Number of seconds since unix epoch*/
pk->tmsg, /*The msg type*/
(char*)pk->msg /*The msg*/
);
if ( mysql_query(blk->con, db_query) !=0 ) {
log_errors( blk->tid,
MYSQL_ERRORS,
DONT_EXIT,
NO_WRITE,
LOGS_FATAL_ERRORS,
MYSQL_QUERY_FAILED,
NULL,
error_ignore );
}
}
}
break;
case GET_MSG_PACKET:
NOT_YET_AUTHENTICATED_EXIT(blk, pk);
if ( blk->userid == pk->sender_id ) {
sprintf(db_query,
"SELECT sender_id, receiver_id, msg_type, message FROM %s.messages WHERE \
receiver_id = %d AND time_received = '-1' ORDER BY time_sent ASC",
my_info.database_name, blk->userid );
if ( mysql_query(blk->con, db_query) != 0 ) {
log_errors( blk->tid,
MYSQL_ERRORS,
DONT_EXIT,
NO_WRITE,
LOGS_FATAL_ERRORS,
MYSQL_QUERY_FAILED,
NULL,
error_ignore );
}
mysql_res = mysql_store_result(blk->con);
if (mysql_res == NULL) {
mysql_free_result(mysql_res);
log_errors( blk->tid,
MYSQL_ERRORS,
DONT_EXIT,
NO_WRITE,
LOGS_FATAL_ERRORS,
MYSQL_RESULT_CANT_READ,
NULL,
error_ignore );
}
while ( (mysql_row = mysql_fetch_row(mysql_res)) ) {
memset(str, '\0', MAX_PACKET_SIZE);
snprintf( str, MAX_PACKET_SIZE,
"|%d|%s|%s|%s|%s|",
MSG_PACKET,
mysql_row[0], mysql_row[1], mysql_row[2], mysql_row[3] );
send_msg_dontwait(blk, str);
}
mysql_free_result(mysql_res);
memset(db_query, '\0', MYSQL_QUERY_BUFF);
sprintf( db_query,
"UPDATE %s.messages SET time_received = '%ld' WHERE receiver_id = %d AND time_received = '-1'",
my_info.database_name, blk->curr_time() , blk->userid);
if ( mysql_query(blk->con, db_query) != 0 ) {
log_errors( blk->tid,
MYSQL_ERRORS,
DONT_EXIT,
NO_WRITE,
LOGS_FATAL_ERRORS,
MYSQL_QUERY_FAILED,
NULL,
error_ignore );
}
}
destroy_packet(pk);
break;
case GET_GEO_PACKET:
NOT_YET_AUTHENTICATED_EXIT(blk, pk);
if ( pk->receiver_id == pk->sender_id ) { /*if I am both the sender and the receiver, then*/
snprintf( str, MAX_PACKET_SIZE, /*I am asking for my location*/
"|%d|%d|%d|%d|%f:%f|", /*format of /30/3/1/10/125541:624561/*/
GEO_PACKET,
pk->sender_id, pk->receiver_id,
COORD_DATA, blk->loc.lattitude, blk->loc.longitude);
send_msg_dontwait(blk, str);
}
if (blk->userid == pk->receiver_id && pk->sender_id != pk->receiver_id) { /*if i am the receiver then some is asking */
/*for my location */
pthread_mutex_lock( &addrTable_mutex );
node = getAddrFromaddrTable( pk->sender_id );
pthread_mutex_unlock( &addrTable_mutex );
if ( node != NULL ) {
memset(str, '\0', MAX_PACKET_SIZE);
snprintf( str, MAX_PACKET_SIZE,
"|%d|%d|%d|%d|%f:%f|",
GEO_PACKET,
pk->receiver_id,
pk->sender_id,
COORD_DATA,
blk->loc.lattitude, blk->loc.longitude
);
struct packet *pckt = string_to_packet(str);
if ( pckt != NULL ) {
server_debug("Enqueuing packet: |%d|%d|%d|%d|%s|", pckt->ptype, pckt->sender_id, pckt->receiver_id, pckt->tmsg, (char*)pckt->msg);
pthread_mutex_lock( &(node->in_queue->queue_lock) );
enqueue(node->in_queue, pckt);
pthread_mutex_unlock( &(node->in_queue->queue_lock) );
} else {
mem = packet_to_string(pk);
sprintf(str, "This packet was dropped: %s", mem);
if (mem != NULL) free(mem);
log_errors( blk->tid,
STD_ERRORS,
DONT_EXIT,
NO_WRITE,
LOGS_WARNING,
str,
NULL,
error_ignore);
}
} else {
/*Receiver not online*/
mem = packet_to_string(pk);
sprintf(str, "This packet was dropped: %s", mem);
if (mem != NULL) free(mem);
log_errors( blk->tid,
STD_ERRORS,
DONT_EXIT,
NO_WRITE,
LOGS_WARNING,
str,
pk,
(void(*)(void*))destroy_packet );
sprintf(str, "|%d|-1|-1|-1|ACK|", ACK_PACKET);
send_msg_dontwait(blk, str);
}
}
if (blk->userid == pk->sender_id && pk->sender_id != pk->receiver_id) { /*if i am the sender, then I am asking for someone location*/
pthread_mutex_lock( &addrTable_mutex );
node = getAddrFromaddrTable(pk->receiver_id);
pthread_mutex_unlock( &addrTable_mutex );
if ( node != NULL ) { //if receiver is online receive geolocation info
pthread_mutex_lock( &(node->in_queue->queue_lock) ); //lock receiver msg
enqueue(node->in_queue, pk); //append packet
pthread_mutex_unlock( &(node->in_queue->queue_lock) );
server_debug("Enqueue packet: |%d|%d|%d|%d|%s|", pk->ptype, pk->sender_id, pk->receiver_id, pk->tmsg, (char*)pk->msg);
} else {
destroy_packet(pk);
sprintf(str, "|%d|-1|-1|-1|ACK|", ACK_PACKET);
send_msg_dontwait(blk, str);
}
}
break;
case SET_GEO_PACKET:
NOT_YET_AUTHENTICATED_EXIT(blk, pk);
latti_word = strtok_r(pk->msg, ":", &saveptr);
longi_word = strtok_r(NULL, ":", &saveptr);
server_debug("Setting geolocation for user %d", blk->userid);
double lattitude = strtof((const char*)latti_word, NULL);
double longitude = strtof((const char*)longi_word, NULL);
set_geolocation_info(blk, longitude, lattitude);
destroy_packet(pk);
break;
case GEO_PACKET: //read from the queue
NOT_YET_AUTHENTICATED_EXIT(blk, pk);
snprintf( str, MAX_PACKET_SIZE,
"|%d|%d|%d|%d|%s|", /*format of /30/3/1/10/125541:624561/*/
GEO_PACKET,
pk->sender_id, pk->receiver_id,
COORD_DATA, (char*)pk->msg);
send_msg_dontwait(blk, str);
break;
default:
NOT_YET_AUTHENTICATED_EXIT(blk, pk);
}
}
static void destroy_handlers_block(void *arg)
{
struct thread_block *node = (struct thread_block*)arg;
if ( node != NULL ) {
pthread_mutex_lock( &addrTable_mutex );
(void*)removeFromAddrTable(node->userid);
pthread_mutex_unlock( &addrTable_mutex );
destroy_thread_node(node);
}
}
void *connection_handler(void *data)
{
struct thread_block *thread_node = (struct thread_block*)data;
struct packet *pk = NULL;
ssize_t nread = 0;
char buff[MAX_PACKET_SIZE+5];
int test_cond = -1; /*check for whether pk is set for [while queue not empty]*/
memset(buff, '\0', MAX_PACKET_SIZE); //init buff
/*For unmanageble thread cancelling. So the thread_info_block
* must be search and destroy it manually*/
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
pthread_cleanup_push(destroy_handlers_block, thread_node); //clean up function
init_connection_handler(thread_node);
while (1) {
if ( thread_node->user_auth == USER_TO_EXIT ) break;
//while socket is ready read it
nread = recv(thread_node->socket, buff, MAX_PACKET_SIZE, MSG_DONTWAIT);
if (nread > 0) {
pk = string_to_packet(buff); //packetize string packet
server_debug("Read from socket:|%d|%d|%d|%d|%s|", pk->ptype, pk->sender_id, pk->receiver_id, pk->tmsg, (char*)pk->msg);
if ( pk == NULL ) {
strcat(buff, "::packet dropped.");
log_errors( thread_node->tid,
STD_ERRORS,
DONT_EXIT,
NO_WRITE,
LOGS_WARNING,
buff,
NULL,
error_ignore );
}
interpret_packets(thread_node, pk);
}
//check whether queue is empty but dont wait if you will block
pthread_mutex_lock( &(thread_node->in_queue->queue_lock) );
if ( !empty( thread_node->in_queue ) ) {
pk = dequeue(thread_node->in_queue);
test_cond = 0;
server_debug("Dequeued packet:|%d|%d|%d|%d|%s|", pk->ptype, pk->sender_id, pk->receiver_id, pk->tmsg, (char*)pk->msg);
}
pthread_mutex_unlock( &(thread_node->in_queue->queue_lock) );
if (test_cond == 0 && pk != NULL) {
interpret_packets(thread_node, pk);
server_debug("About to destroy packet:|%d|%d|%d|%d|%s|", pk->ptype, pk->sender_id, pk->receiver_id, pk->tmsg, (char*)pk->msg);
destroy_packet(pk); //destroy the packet now
}
test_cond = -1;
pthread_testcancel(); /*cancellation point*/
memset(buff, '\0', MAX_PACKET_SIZE); //init buff
sleep(THREAD_WAIT_TIME); /*cancellation point*/
}
pthread_cleanup_pop(1);
mysql_thread_end();
pthread_exit(NULL);
}