-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal.cpp
5954 lines (5268 loc) · 233 KB
/
signal.cpp
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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// signal.h (c)2018 Justin Jack
// deedlecore project - libsignal.a / libsignal.lib
/*
This file handles the underlying connection to the signalling server.
It will implement TCP transport for reliability.
It will expose functions for sending and receiving signalling traffic.
It is responsible for assuring that the connection is alive and stable. It
will monitor the response time for SIP OPTIONS, and monitor TCP keep-alives.
It will also handle registrations to the signalling server so we can get that task off
of the application.
v1.2
*/
#include "jstring.h"
#include "signal.h"
#ifdef __APPLE__
#include <pthread.h>
#endif
//#include "memcheck.h"
const char *SIGNAL::sip_request_list[] = { "INVITE", "ACK", "CANCEL", "OPTIONS", "BYE", "REFER", "SUBSCRIBE", "NOTIFY", "INFO", "PUBLISH", "MESSAGE", "REGISTER" };
const char *SIP::sip_request_list[] = { "INVITE", "ACK", "CANCEL", "OPTIONS", "BYE", "REFER", "SUBSCRIBE", "NOTIFY", "INFO", "PUBLISH", "MESSAGE", "REGISTER" };
const char *SIGNAL::empty = "";
const char SIGNAL::OPTIONS[] = "\
OPTIONS sip:Registrar@%s;transport=tcp SIP/2.0\r\n\
Via: SIP/2.0/TCP %s;branch=%s;rport\r\n\
Max-Forwards: 70\r\n\
From: <sip:%d@%s;transport=tcp>;tag=%s\r\n\
To: \"Registrar\" <sip:Registrar@%s>\r\n\
Contact: <sip:%d@%s;transport=tcp>\r\n\
Call-ID: %s\r\n\
CSeq: 102 OPTIONS\r\n\
User-Agent: deedlebox/%s\r\n\
Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS\r\n\
Accept: application/sdp, application/simple-message-summary, message/sipfrag;version=2.0, text/plain\r\n\
Supported: replaces, timer\r\n\
Allow-Events: presence, message-summary, refer\r\n\
Content-Length: 0\r\n\r\n";
/*
const char SIGNAL::REGISTER[]...
Old Contact line: Contact: <sip:%d@%s;transport=tcp;ob>;expires=%d\r\n\
*/
const char SIGNAL::REGISTER[] = "\
REGISTER sip:%s SIP/2.0\r\n\
Via: SIP/2.0/TCP %s;rport;branch=%s\r\n\
Max-Forwards: 70\r\n\
From: \"%s\" <sip:%d@%s>;tag=%s\r\n\
To: <sip:%d@%s>\r\n\
Call-ID: %s\r\n\
CSeq: %d REGISTER\r\n\
User-Agent: deedlebox/%s\r\n\
Contact: <sip:%d@%s;transport=tcp>;expires=%d\r\n\
Expires: %d\r\n\
Allow: PRACK,INVITE,ACK,BYE,CANCEL,UPDATE,SUBSCRIBE,NOTIFY,REFER,MESSAGE,OPTIONS\r\n\
Content-Length: 0\r\n\r\n";
/*
const char SIGNAL::AUTHREGISTER[]
Contact: <sip:%d@%s;transport=tcp;ob>;expires=%d\r\n\
*/
const char SIGNAL::AUTHREGISTER[] = "\
REGISTER sip:%s SIP/2.0\r\n\
Via: SIP/2.0/TCP %s;rport;branch=%s\r\n\
Max-Forwards: 70\r\n\
From: \"%s\" <sip:%d@%s>;tag=%s\r\n\
To: <sip:%d@%s>\r\n\
Call-ID: %s\r\n\
CSeq: %d REGISTER\r\n\
User-Agent: deedlebox/%s\r\n\
Contact: <sip:%d@%s;transport=tcp>;expires=%d\r\n\
Expires: %d\r\n\
Allow: PRACK,INVITE,ACK,BYE,CANCEL,UPDATE,SUBSCRIBE,NOTIFY,REFER,MESSAGE,OPTIONS\r\n\
Authorization: Digest %s\r\n\
Content-Length: 0\r\n\r\n";
/*
unsigned long long timestamp(void) {
#ifdef _WIN32
return (unsigned long long)GetTickCount();
#else
unsigned long long retval = 0;
struct timeval ts;
gettimeofday(&ts, NULL);
retval = (((unsigned long long)ts.tv_sec * (unsigned long long)1000000) + (unsigned long long)ts.tv_usec) / 1000;
return retval;
#endif
}
*/
/*
This function DID get the IP address of the computer on which it's running, but that caused problems for Macs
with Wine. It's not all that necessary with TCP, so I did away with it and put in a dummy IP address for
inital presentation...
*/
char *SIGNAL::setlocalipaddress() {
int i = 0;
if (this->validateipaddress(this->user_info.ipaddress)) return this->user_info.ipaddress;
for (i = 0; i < 10; i++) {
if (this->iplist[i] > 0) {
if (db_inet_ntop(AF_INET, &this->iplist[i], this->user_info.ipaddress, 50)) {
break;
}
}
}
if (i > 9 ) {
sprintf(this->user_info.ipaddress, "192.168.1.1");
}
return this->user_info.ipaddress;
}
int SIGNAL::IPandport_deedle(char *buff) {
if (!this->validateipaddress(this->user_info.ipaddress)) {
/*
OutputDebugString("\n\n***************************************\n");
OutputDebugString("* Calling this->setlocalipaddress() *\n");
OutputDebugString("***************************************\n\n");
*/
this->setlocalipaddress();
}
if (this->user_info.rport == 0) this->user_info.rport = 5060;
sprintf(buff, "%s:%u", this->user_info.ipaddress, this->user_info.rport);
return 1;
}
int SIGNAL::IPandport_server(char *buff) {
if (!this->validateipaddress(this->voip_server_ip)) return 0;
if (this->signal_port == 0) this->signal_port = 5060;
sprintf(buff, "%s:%u", this->voip_server_ip, this->signal_port);
return 1;
}
void SIGNAL::newbranch(char *buff) {
static int ctr = 0;
sprintf(buff, "z9hG4bKdb%x%x%x%x%x",
rand(),
rand(),
rand(),
rand(), ctr++);
}
void SIGNAL::newcallid(char *buff) {
static int ctr = 0;
sprintf(buff, "%x%x%x%x%x%x%x",
rand(),
rand(),
rand(),
rand(),
rand(),
rand(), ctr++);
}
void SIGNAL::newtag(char *buff) {
this->newcallid(buff);
}
void SIGNAL::options(void) {
char opts[3000];
char my_address[50], server_address[50];
char newtag[255], newbranch[255], callid[255];
this->newcallid(callid);
this->newtag(newtag);
this->newbranch(newbranch);
if (!this->IPandport_deedle(my_address)) return;
if (!this->IPandport_server(server_address)) return;
sprintf(opts, SIGNAL::OPTIONS,
server_address,
my_address,
newbranch,
this->user_info.extension,
my_address,
newtag,
server_address,
this->user_info.extension,
my_address,
callid, DEEDLEBOX_VERSION);
showsip(opts);
db_send(this->psocket->s, opts, strlen(opts), 0);
return;
}
int SIGNAL::setlocalip(char *ip) {
if (this->validateipaddress(ip)) {
memset(this->user_info.ipaddress, 0, 50);
strcpy(this->user_info.ipaddress, ip);
return 1;
}
return 0;
}
int SIGNAL::setlocalip(char *ip, size_t len) {
if (this->validateipaddress(ip)) {
memset(this->user_info.ipaddress, 0, 50);
memcpy(this->user_info.ipaddress, ip, len);
return 1;
}
return 0;
}
int SIGNAL::isregistered(void) {
if (!this->registration) return 0;
if (time(0) < this->current_registration_expires) return 1;
return 0;
}
void SIGNAL::ready(void) {
this->lock();
this->application_ready = 1;
this->unlock();
}
void SIGNAL::unready(void) {
this->lock();
this->application_ready = 0;
this->unlock();
}
void SIGNAL::set_rtp(RTPENGINE *rtp_to_use) {
this->rtp = rtp_to_use;
}
void SIGNAL::initialize() {
int ip4count = 0, json_ip_size = 0, i = 0, j = 0;
char temp_ip_buffer[100];
#ifdef _WIN32
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
#endif
this->shut_down = 0;
this->calls_talking = 0;
/* Default, set do not disturb off */
this->do_not_disturb = 0;
/*
Set our initial call counter to zero. This is only used
when we're shutting down to make sure all calls are disposed
of before exiting...
*/
this->total_call_count = 0;
/* Set up dialog stuff */
db_create_mutex(&this->dialog_mutex);
memset(this->dialog_list, 0, sizeof(DIALOG) * MAX_DIALOG_COUNT);
this->call_status_change_callback = 0;
this->ringing_status_callback = 0;
this->voicemail_callback = 0;
this->registered_callback = 0;
/* Get local IP Address list. Mainly for exposing to the code controlling the SIGNAL class. */
this->json_ip_list = 0;
ip4count = get_ip4_list(this->iplist);
if (ip4count > 0) {
json_ip_size = ip4count * 100;
//this->json_ip_list = (char *)MEMORY::calloc_(json_ip_size, 1, (char *)"signalling.cpp > initialize(): this->json_ip_list");
this->json_ip_list = (char *)calloc(json_ip_size, 1);
this->json_ip_list[0] = '[';
for (i = 0; i < ip4count; i++) {
if (db_inet_ntop(AF_INET, &this->iplist[i], temp_ip_buffer, 100)) {
if (j++ > 0) strcat(this->json_ip_list, ",");
strcat(this->json_ip_list, "\"");
strcat(this->json_ip_list, temp_ip_buffer);
strcat(this->json_ip_list, "\"");
}
}
strcat(this->json_ip_list, "]");
}
if (!this->json_ip_list) {
printf("* No IP Addresses found on this local system!\n");
}
this->rtp = 0;
/* Set up variables to keep up with registration failures */
this->registration_info.auth_sent = 0;
this->registration_info.failed_attempts = 0;
this->registration_info.last_registration_attempt = 0;
/* Initialize other variables */
this->connected = 0;
this->application_ready = 0;
this->cseq_registration = 100;
this->current_registration_expires = 0;
this->registration = 0;
this->shuttingdown = 0;
this->last_message = 0;
this->signal_available_message_count = 0;
this->last_options = 0;
this->signal_last_options = 0;
this->db_signal_thread = 0;
this->signal_thread_running = 1;
this->signal_last_successfull_send = 0;
memset(&this->signal_serveraddr, 0, sizeof(this->signal_serveraddr));
memset(&this->signal_registration, 0, sizeof(this->signal_registration));
memset(this->_socket, 0, sizeof(struct __socket) * 10);
this->psocket = 0;
this->signal_thread_id = 0;
this->voip_server_ip[0] = 0;
this->send_queue = 0;
this->send_queue_count = 0;
this->message_stack_size = 0;
this->user_info.rport = this->signal_port;
this->user_info.ipconfirmed = 0;
this->user_info.extension = 0;
memset(this->user_info.password, 0, 100);
memset(this->user_info.callerid, 0, 100);
sprintf(this->user_info.callername, "Caller");
this->user_info.ipaddress[0] = 0;
this->user_info.name[0] = 0;
db_create_mutex(&this->signal_mutex);
// printf("Done with SIGNAL::Initialzize()\n");
return;
}
int SIGNAL::setvoipserverip(char *ip) {
if (!ip) return 0;
if (strlen(ip) > 19) return 0;
if (this->validateipaddress(ip)) {
memset(this->voip_server_ip, 0, 20);
strcpy(this->voip_server_ip, ip);
return 1;
}
return 0;
}
SIGNAL::SIGNAL(int extension, char *ipadress, unsigned short port) {
this->initialize();
if (this->validateipaddress(ipadress)) strcpy(this->voip_server_ip, ipadress);
if (port > 0) this->signal_port = port;
if (extension > 0) this->user_info.extension = extension;
if (strlen(ipadress) > 19) {
strncpy(this->voip_server_ip, ipadress, 19);
} else {
sprintf(this->voip_server_ip, "%s", ipadress);
}
return;
}
SIGNAL::SIGNAL(int extension, char *pwd, char *ipadress, unsigned short port) {
this->initialize();
if (this->validateipaddress(ipadress)) strcpy(this->voip_server_ip, ipadress);
if (port > 0) this->signal_port = port;
if (extension > 0) this->user_info.extension = extension;
if (strlen(ipadress) > 19) {
strncpy(this->voip_server_ip, ipadress, 19);
} else {
sprintf(this->voip_server_ip, "%s", ipadress);
}
if (pwd) {
strcpy(this->user_info.password, pwd);
}
return;
}
SIGNAL::SIGNAL(char *username, int extension, char *ipadress, unsigned short port) {
this->initialize();
if (this->validateipaddress(ipadress)) strcpy(this->voip_server_ip, ipadress);
if (port > 0) this->signal_port = port;
if (extension > 0) this->user_info.extension = extension;
if (strlen(ipadress) > 19) {
strncpy(this->voip_server_ip, ipadress, 19);
} else {
sprintf(this->voip_server_ip, "%s", ipadress);
}
if (strlen(username) > 99) {
strncpy(this->user_info.name, username, 99);
} else {
sprintf(this->user_info.name, "%s", username);
}
return;
}
SIGNAL::SIGNAL(char *username, int extension, char *pwd, char *ipadress, unsigned short port) {
this->initialize();
if (this->validateipaddress(ipadress)) strcpy(this->voip_server_ip, ipadress);
if (port > 0) this->signal_port = port;
if (extension > 0) this->user_info.extension = extension;
if (strlen(ipadress) > 19) {
strncpy(this->voip_server_ip, ipadress, 19);
} else {
sprintf(this->voip_server_ip, "%s", ipadress);
}
if (strlen(username) > 99) {
strncpy(this->user_info.name, username, 99);
} else {
sprintf(this->user_info.name, "%s", username);
}
if (pwd) {
strcpy(this->user_info.password, pwd);
}
return;
}
SIGNAL::SIGNAL(const char *username, int extension, char *ipadress, unsigned short port) {
this->initialize();
if (this->validateipaddress(ipadress)) strcpy(this->voip_server_ip, ipadress);
if (port > 0) this->signal_port = port;
if (extension > 0) this->user_info.extension = extension;
if (strlen(username) > 99) {
strncpy(this->user_info.name, username, 99);
} else {
sprintf(this->user_info.name, "%s", username);
}
return;
}
SIGNAL::SIGNAL(const char *username, int extension, char *pwd, char *ipadress, unsigned short port) {
this->initialize();
if (this->validateipaddress(ipadress)) strcpy(this->voip_server_ip, ipadress);
if (port > 0) this->signal_port = port;
if (extension > 0) this->user_info.extension = extension;
if (strlen(username) > 99) {
strncpy(this->user_info.name, username, 99);
} else {
sprintf(this->user_info.name, "%s", username);
}
if (pwd) {
strcpy(this->user_info.password, pwd);
}
return;
}
// new constructor signal candidate new signal
SIGNAL::SIGNAL(SIGNAL_SETTINGS *s) {
int server_ip_okay = 0, extension_okay = 0, password_okay = 0;
this->initialize();
this->do_not_disturb = s->do_not_disturb;
if (s->auth.password[0] != 0) {
strncpy(this->user_info.password, s->auth.password, 99);
password_okay = 1;
}
if (s->auth.username[0] != 0) {
strncpy(this->user_info.name, s->auth.username, 99);
}
if (s->callbacks.call_status_change_callback) {
this->call_status_change_callback = s->callbacks.call_status_change_callback;
}
if (s->callbacks.registered_callback) {
this->registered_callback =
s->callbacks.registered_callback;
}
if (s->callbacks.ringing_status_callback) {
this->ringing_status_callback = s->callbacks.ringing_status_callback;
}
if (s->callbacks.voicemail_callback) {
this->voicemail_callback =
s->callbacks.voicemail_callback;
}
if (s->extension) {
this->user_info.extension =
s->extension;
extension_okay = 1;
}
if (s->rtp) {
this->rtp =
s->rtp;
}
if (s->SIP_server_IP[0] != 0) {
if (this->validateipaddress(s->SIP_server_IP)) {
strncpy(this->voip_server_ip, s->SIP_server_IP, 20);
server_ip_okay = 1;
} else {
unsigned long ipaddr = db_get_host_ip_address(s->SIP_server_IP);
if (ipaddr != 0) {
if (db_inet_ntop(AF_INET, &ipaddr, this->voip_server_ip, 20)) {
// printf("Server's IP Address is: %s\n", this->voip_server_ip);
sprintf(s->SIP_server_IP, "%s", this->voip_server_ip);
server_ip_okay = 1;
}
}
}
}
if ( s->SIP_server_port > 0) {
this->signal_port = s->SIP_server_port;
} else {
this->signal_port = 5060;
}
if (s->userinfo.callerid[0] != 0) {
strncpy(this->user_info.callerid, s->userinfo.callerid, 99);
}
if (s->userinfo.username[0] != 0) {
strncpy(this->user_info.callername, s->userinfo.username, 99);
}
if (extension_okay && server_ip_okay && password_okay) {
// printf("Settings OKAY, launching signal_thread()\n\n");
#ifdef __APPLE__
pthread_create(&this->db_signal_thread, 0, &signal_thread, (void *)this);
#elif _WIN32
db_create_thread(&signal_thread, this, &this->db_signal_thread);
#endif
} else {
if (!extension_okay) {
printf("* Invalid Telephone Extension: Please make sure you've specified a valid extension in SIGNAL_SETTINGS::extension.\n\n");
}
if (!password_okay) {
printf("* Password not set: Please make sure you've entered a valid password in SIGNAL_SETTINGS::auth::password.\n\n");
}
if (!server_ip_okay) {
printf("* IP Address for \"%s\" could not be found/verified.\n\n", s->SIP_server_IP);
}
}
return;
}
SIGNAL::SIGNAL(char *ipadress, unsigned short port) {
this->initialize();
if (this->validateipaddress(ipadress)) strcpy(this->voip_server_ip, ipadress);
if (port > 0) this->signal_port = port;
return;
}
SIGNAL::SIGNAL(unsigned short port) {
this->initialize();
this->signal_port = port;
return;
};
SIGNAL::SIGNAL() {
this->initialize();
this->signal_port = 5060;
return;
};
SIGNAL::~SIGNAL() {
/* Destructor */
time_t timeout = time(0) + 3;
int i = 0;
DIALOG *d = 0;
char calls_to_hang_up[500][MAX_DIALOG_COUNT];
int icalls_to_hang_up = 0;
this->shut_down = 1;
/* Set DO NOT DISTURB so we don't get too far with any new calls */
this->do_not_disturb = 1;
/* Un register so we don't GET any more calls */
if (this->isregistered()) {
this->sipshutdown();
while (timeout > time(0)) {
if (!this->isregistered()) {
break;
}
}
}
/* Hang up or reject all calls we are currently on */
db_lock_mutex(&this->dialog_mutex);
for (i = 0; i < MAX_DIALOG_COUNT; i++) {
d = &this->dialog_list[i];
if (!d->inuse) continue;
switch (d->callinfo.status) {
case RINGING_IN:
case RINGING_OUT:
case ANSWERING:
case TALKING:
case ONHOLD:
case TRANSFERRING:
case CONFIRMING:
sprintf(calls_to_hang_up[icalls_to_hang_up++], "%s", d->dialog_info.callid);
break;
case CANCELLING_CALL:
case SUBSCRIPTION:
case UNKNOWN:
case HUNGUP:
case HANGING_UP:
default:
break;
}
}
db_unlock_mutex(&this->dialog_mutex);
for (i = 0; i < icalls_to_hang_up; i++) {
this->hangup(calls_to_hang_up[i]);
}
timeout = time(0) + 2;
while (this->total_call_count > 0) {
Sleep(10);
if (time(0) > timeout) {
printf("* Timeout waiting for all queued calls to be terminated!\n");
break;
}
}
if (this->json_ip_list) {
free(this->json_ip_list);
//MEMORY::free_(this->json_ip_list);
}
this->json_ip_list = 0;
WSACleanup();
db_destroy_mutex(&this->dialog_mutex);
db_destroy_mutex(&this->signal_mutex);
this->signal_thread_running = 0;
db_thread_join(&db_signal_thread);
return;
};
void SIGNAL::reconnect(void) {
return;
}
int SIGNAL::nextmessage(char *buff, int len) {
int bytesreturned = 0, i = 1;
struct signal_message *msg = 0;
if (!this->connected) return 0;
this->lock();
msg = &this->message_stack[0];
if (this->message_stack_size == 0) {
this->unlock();
return 0;
}
if (len < msg->messagesize) SIGNAL_FAIL(SIGNAL_BUFF_TOO_SMALL);
bytesreturned = msg->messagesize;
memmove(buff, msg->message, msg->messagesize);
buff[bytesreturned] = 0;
//MEMORY::free_(msg->message);
free(msg->message);
msg->message = 0;
for (; i < this->message_stack_size; i++) {
memmove(&this->message_stack[i - 1], &this->message_stack[i], sizeof(struct signal_message));
}
this->message_stack_size--;
this->unlock();
return bytesreturned;
}
/* Changes in_addr to IPv4 text */
char *SIGNAL::ip2string(struct in_addr *addr) {
static char setup = 0;
size_t arraycount = 0, currentindex = 0;
static struct _ipaddress {
char address[50];
} *addresslist;
char *activepointer = 0;
if (!setup) {
/* Allocate 50 spots of 50 characters */
arraycount = 50;
//addresslist = (struct _ipaddress *)MEMORY::calloc_(arraycount, sizeof(struct _ipaddress), (char *)"signalling.cpp > SIGNAL::ip2string");
addresslist = (struct _ipaddress *)calloc(arraycount, sizeof(struct _ipaddress));
setup = 1;
} else {
if (++currentindex == 50) currentindex = 0;
}
activepointer = addresslist[currentindex].address;
memset(activepointer, 0, 50);
db_inet_ntop(AF_INET, addr, activepointer, 50);
return activepointer;
}
/* Changes a text-based IP address to an in_addr structure */
unsigned long SIGNAL::string2ip(char *ipaddress) {
struct in_addr output;
db_inet_pton(AF_INET, ipaddress, &output);
return *((unsigned long *)&output);
}
unsigned long SIGNAL::string2ip(const char *ipaddress) {
return SIGNAL::string2ip((char *)ipaddress);
}
int SIGNAL::nextmessage(char *buff, int len, sockaddr_in* sa) {
/* Copy server info into *sa here!!! */
memmove(sa, &this->signal_serveraddr, sizeof(struct sockaddr_in));
return this->nextmessage(buff, len);
}
void SIGNAL::_close(void) {
int i = 0;
for (; i < 10; i++) {
if (this->_socket[i].s == 0) continue;
//#ifdef _WIN32
shutdown(this->_socket[i].s, 2);
closesocket(this->_socket[i].s);
//#else
// closesocket(this->_socket[i].s);
//#endif
}
this->psocket = 0;
// OutputDebugString("SIGNAL::close(): socket closed\n");
}
/*
Basically, just checks for three dots in the string, I don't want
to make a big deal of this.
*/
int SIGNAL::validateipaddress(char *ip) {
int i = 0, pcount = 0;
unsigned int addrchar = 0;
JSTRING::jsstring **split = 0;
char *endptr = 0;
if (JSTRING::haschar(ip, ':')) {
/* There is a port */
split = JSTRING::split(ip, '.', ':');
} else {
/* No port */
split = JSTRING::split(ip, '.', '\0');
}
if (!split) return 0;
pcount = 0;
while (split[i] != 0) {
if (split[i]->length > 0) {
endptr = &split[i]->ptr[split[i]->length];
addrchar = (unsigned int)strtoull(split[i]->ptr, &endptr, 10);
if (i == 0) {
if (addrchar > 0 && addrchar <= 255) {
pcount++;
}
} else {
if (addrchar <= 255) {
pcount++;
}
}
}
i++;
}
JSTRING::freesplit(split);
return ((pcount == 4) ? 1 : 0);
}
int SIGNAL::voipconnect(void) {
int i = 0;
struct SIGNAL::__socket *poldsocket = 0;
SIP *oldregistration = 0;
char debug[200];
memset(&this->signal_serveraddr, 0, sizeof(struct sockaddr));
if (this->registration) {
OutputDebugString("\n\nClearing out old registration...\n\n");
oldregistration = this->registration;
this->registration = 0;
this->current_registration_expires = 0;
//MEMORY::deleted(oldregistration);
delete oldregistration;
}
// sprintf(debug, "Ext: %d\tAttempting to connect to: %s:%u\n", this->user_info.extension, this->voip_server_ip, this->signal_port);
// OutputDebugString(debug);
//sprintf(debug, "Connected is: %d\n", signal->connected);
if (!this->validateipaddress(this->voip_server_ip)) {
OutputDebugString("signalling.cpp > SIGNAL::voipconnect(): Failing with invalid IP address.\n");
SIGNAL_FAIL(SIGNAL_CONNECT_INVALID_IP);
}
if (!this->signal_port) {
OutputDebugString("signalling.cpp > SIGNAL::voipconnect(): Failing with invalid port.\n");
SIGNAL_FAIL(SIGNAL_CONNECT_NO_PORT);
}
/* Unused */
//this->signal_serveraddr;
/**********************************/
if (this->psocket == 0) {
// OutputDebugString("signalling.cpp > voipconnect(): Starting new connection on socket 0.\n");
this->psocket = &this->_socket[0];
} else {
poldsocket = this->psocket;
for (i = 0; i < 10; i++) {
if (&this->_socket[i] == psocket) {
if (i == 9) {
// OutputDebugString("signalling.cpp > voipconnect(): Starting new connection with socket 0 and scheduling destruction of socket 9.\n");
this->psocket = &this->_socket[0];
} else {
// sprintf(debug, "signalling.cpp > voipconnect(): Starting new connection with socket %i and scheduling destruction of socket %d.\n", (i + 1), i);
OutputDebugString(debug);
this->psocket = &this->_socket[i + 1];
}
break;
}
}
// Check if the new psocket is NOT empty, if it is NOT EMPTY, destroy it.
if (this->psocket->s != 0) {
OutputDebugString("\tsignalling.cpp > voipconnect(): For some reason, the new socket was not destroyed. We're going ahead and destroying it to make room for the new connection...\n");
shutdown(this->psocket->s, 2);
closesocket(this->psocket->s);
memset(this->psocket, 0, sizeof(struct SIGNAL::__socket));
}
// Set poldsocket for destruction two minutes out
poldsocket->destroy_at_time = time(0) + 120;
}
this->psocket->s = tcpconnect(this->voip_server_ip, this->signal_port, 1);
if (!this->psocket->s) {
printf("SIGNAL::voipconnect(): tcpconnect() failed...\n");
sprintf(this->string_error_condition, "FAILED to connect to %s:%u", this->voip_server_ip, this->signal_port);
memset(this->psocket, 0, sizeof(struct SIGNAL::__socket));
this->psocket = 0;
this->connected = 0;
SIGNAL_FAIL(-1);
}
this->connected = 1;
this->string_error_condition[0] = 0;
this->options();
this->psocket->last_response = time(0); // Give 10 seconds before signal_thread() loop marks this connection as dead.
return this->connected;
}
void SIGNAL::sipregister(struct SIGNAL::__socket *psocketstruct, int expires) {
char opts[1000];
char my_address[50], server_address[50];
char newtag[255], newbranch[255], callid[255];
SIP *oldregistration = 0;
if (!psocketstruct) return;
if (!this->registration) {
/* This is a new registration */
this->newcallid(callid);
this->newtag(newtag);
} else {
oldregistration = this->registration;
sprintf(callid, "%.*s", (int)this->registration->message.callid_header->value.length,
this->registration->message.callid_header->value.string);
sprintf(newtag, "%s", this->registration->message.from_header->sip_headerinfo.uri->tag());
}
this->newbranch(newbranch);
if (!this->IPandport_deedle(my_address)) return;
if (!this->IPandport_server(server_address)) return;
sprintf(opts, SIGNAL::REGISTER,
server_address, // REGISTER
my_address, newbranch, // Via
((strlen(this->user_info.name) > 0) ? this->user_info.name : ""), this->user_info.extension, server_address, newtag, // From:
this->user_info.extension, server_address, // To:
callid, // Call-ID
++this->cseq_registration, // CSeq
DEEDLEBOX_VERSION, // User-Agent
this->user_info.extension, my_address, expires, // Contact: (with ";expires=" parameter)
expires); // Expires:
this->registration = new SIP(opts, strlen(opts));
//MEMORY::add_pointer_to_track(this->registration, (char *)"signalling.cpp > sipregister(): Allocated new REGISTER-ation");
psocketstruct->last_connection_check = time(0);
showsip(opts);
db_send(psocketstruct->s, opts, strlen(opts), 0);
if (oldregistration) {
//MEMORY::deleted(oldregistration);
delete oldregistration;
}
return;
}
void SIGNAL::sipregister(struct SIGNAL::__socket *psocketstruct) {
SIGNAL::sipregister(psocketstruct, REGISTRATION_EXPIRATION);
}
void SIGNAL::sipunregister(struct SIGNAL::__socket *psocketstruct) {
if (this->registration) SIGNAL::sipregister(psocketstruct, 0);
}
void SIGNAL::sipunregister(void) {
if (this->registration) SIGNAL::sipregister(this->psocket, 0);
}
void SIGNAL::sipshutdown(void) {
this->shuttingdown = 1;
this->sipunregister();
}
void SIGNAL::set_caller_id(const char *callerid) {
if (!callerid) return;
sprintf(this->user_info.callerid, "%.*s", ((strlen(callerid) < 100) ? (int)strlen(callerid) : 99), callerid);
}
void SIGNAL::set_caller_name(const char *callername) {
if (!callername) return;
sprintf(this->user_info.callername, "%.*s", ((strlen(callername) < 100) ? (int)strlen(callername) : 99), callername);
}
char *SIGNAL::password(void) {
return this->user_info.password;
}
void SIGNAL::password(char *pwd) {
memset(this->user_info.password, 0, 100);
if (pwd) {
strcpy(this->user_info.password, pwd);
}
return;
}
void SIGNAL::auth(DIALOG *dialog, SIP *message) {
char thispacket[5000];
char authheader[1000];
char ha1[33], ha2[33], response[33], first_value[500], second_value[500], third_value[500];
char my_address[50], server_address[50];
char auth_extension[100];
JSTRING *js = 0;
size_t i = 0, linelength = 0;
char *line = 0;
memset(thispacket, 0, 5000);
if (message->message.from_header->sip_headerinfo.uri->_extension.length > 0) {
sprintf(auth_extension, "%.*s",
(int)message->message.from_header->sip_headerinfo.uri->_extension.length,
message->message.from_header->sip_headerinfo.uri->_extension.string);
} else {
sprintf(auth_extension, "%d", this->user_info.extension);
}
sprintf(first_value, "%s:%.*s:%s",
((strlen(this->user_info.name) > 0) ? this->user_info.name : message->message.from_header->sip_headerinfo.uri->extension()) /*message->message.from_header->sip_headerinfo.uri->extension()*/,
(int)message->message.authinfo.realm.length,
message->message.authinfo.realm.string,
this->password());
md5(first_value, ha1);
sprintf(second_value, "%.*s:sip:%s",
(int)message->message.request.length,
message->message.request.string,
message->message.to_header->sip_headerinfo.uri->ipaddress());
md5(second_value, ha2);
sprintf(third_value, "%s:%.*s:%s",
ha1,
(int)message->message.authinfo.nonce.length,
message->message.authinfo.nonce.string,
ha2);
md5(third_value, response);
if (message->message.authinfo.opaque.length > 0) {
sprintf(authheader, "Authorization: Digest username=\"%s\",realm=\"%.*s\",nonce=\"%.*s\",opaque=\"%.*s\",uri=\"sip:%s\",response=\"%s\",algorithm=MD5\r\n",
((strlen(this->user_info.name) > 0) ? this->user_info.name : message->message.from_header->sip_headerinfo.uri->extension()) /*message->message.from_header->sip_headerinfo.uri->extension()*/,
(int)message->message.authinfo.realm.length,
message->message.authinfo.realm.string,
(int)message->message.authinfo.nonce.length,
message->message.authinfo.nonce.string,
(int)message->message.authinfo.opaque.length,
message->message.authinfo.opaque.string,
message->message.to_header->sip_headerinfo.uri->ipaddress(),
response);
} else {
sprintf(authheader, "Authorization: Digest username=\"%s\",realm=\"%.*s\",nonce=\"%.*s\",uri=\"sip:%s\",response=\"%s\",algorithm=MD5\r\n",
((strlen(this->user_info.name) > 0) ? this->user_info.name : message->message.from_header->sip_headerinfo.uri->extension()) /*message->message.from_header->sip_headerinfo.uri->extension()*/,
/*message->message.from_header->sip_headerinfo.uri->extension(),*/
(int)message->message.authinfo.realm.length,
message->message.authinfo.realm.string,
(int)message->message.authinfo.nonce.length,
message->message.authinfo.nonce.string,
message->message.to_header->sip_headerinfo.uri->ipaddress(),
response);
}
dialog->dialog_info.mine.cseq++;
dialog->dialog_errors++;
dialog->dialog_info.theirs.tag[0] = 0;
if (!this->IPandport_deedle(my_address)) return;
if (!this->IPandport_server(server_address)) return;
js = new JSTRING(dialog->primary->pmessage);