-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsiptapi.cpp
5127 lines (4536 loc) · 173 KB
/
siptapi.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
/*
Name: asttapi.cpp
License: Under the GNU General Public License Version 2 or later (the "GPL")
(c): Nick Knight
(c): Klaus Darilion (IPCom GmbH, www.ipcom.at)
Description:
*/
/* ***** BEGIN LICENSE BLOCK *****
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Asttapi.
*
* The Initial Developer of the Original Code is
* Nick Knight.
* Portions created by the Initial Developer are Copyright (C) 2005
* the Initial Developer. All Rights Reserved.
*
* Contributor(s): Klaus Darilion (IPCom GmbH, www.ipcom.at)
*
* ***** END LICENSE BLOCK ***** */
// The debugger can't handle symbols more than 255 characters long.
// STL often creates symbols longer than that.
// When symbols are longer than 255 characters, the warning is issued.
#pragma warning(disable:4786)
// Helge
//#define TRIALPROXY "85.200.227.170"
//#define TRIALOUTBOUNDPROXY "85.200.227.170"
//#define TRIALUSERNAME "2607"
#if defined(TRIALPROXY) || defined(TRIALOUTBOUNDPROXY) || defined(TRIALUSERNAME)
#undef MULTILINE
#endif
//Validity date
//#define EXPIRATIONDATE "2017-12-31"
//Domain prefix, must contain a leading .
//#define DOMAINSUFFIX ".allocloud.com"
// setting the version to 3.0 causes problem with dialer.exe
//#define TAPI_CURRENT_VERSION 0x00030000
#include "dll.h"
#include <windows.h>
#include <stdio.h>
#include <wchar.h>
#include <atlconv.h>
//For debug and definitions
#include "WaveTsp.h"
//our resources
#include "resource.h"
//misc functions
#include "utilities.h"
//kd
#include "TapiLine.h"
#include "SipStack.h"
#include <map>
////////////////////////////////////////////////////////////////////////////////
//
// Globals
//
// Keep track of all the globals we require
//
////////////////////////////////////////////////////////////////////////////////
DWORD g_dwPermanentProviderID = 0;
DWORD g_dwLineDeviceIDBase = 0;
HPROVIDER g_hProvider = 0;
//For our windows...
HINSTANCE g_hinst = 0;
//kd
SipStack *g_sipStack = NULL;
// { dwDialPause, dwDialSpeed, dwDigitDuration, dwWaitForDialtone }
LINEDIALPARAMS g_dpMin = { 100, 50, 100, 100 };
LINEDIALPARAMS g_dpDef = { 250, 50, 250, 500 };
LINEDIALPARAMS g_dpMax = { 1000, 50, 1000, 1000 };
////////////////////////////////////////////////////////////////////////////////
// Function DllMain
//
// Dll entry
//
////////////////////////////////////////////////////////////////////////////////
BOOL WINAPI DllMain(
HINSTANCE hinst,
DWORD dwReason,
void* /*pReserved*/)
{
if( dwReason == DLL_PROCESS_ATTACH )
{
g_hinst = hinst;
}
return TRUE;
}
LONG TSPIAPI TSPI_providerInit(
DWORD dwTSPIVersion,
DWORD dwPermanentProviderID,
DWORD dwLineDeviceIDBase,
DWORD dwPhoneDeviceIDBase,
DWORD_PTR dwNumLines,
DWORD_PTR dwNumPhones,
ASYNC_COMPLETION lpfnCompletionProc,
LPDWORD lpdwTSPIOptions // TSPI v2.0
)
{
BEGIN_PARAM_TABLE("TSPI_providerInit")
DWORD_IN_ENTRY(dwTSPIVersion)
DWORD_IN_ENTRY(dwPermanentProviderID)
DWORD_IN_ENTRY(dwLineDeviceIDBase)
DWORD_IN_ENTRY(dwPhoneDeviceIDBase)
DWORD_OUT_ENTRY(dwNumLines)
DWORD_OUT_ENTRY(dwNumPhones)
END_PARAM_TABLE()
TSPTRACE("TSPI_providerInit: ...\n");
//Record all of the globals we need
g_pfnCompletionProc = lpfnCompletionProc;
//other params we need to track
g_dwPermanentProviderID = dwPermanentProviderID;
g_dwLineDeviceIDBase = dwLineDeviceIDBase;
TSPTRACE("TSPI_providerInit: dwTSPIVersion=0x%X, dwPermanentProviderID=0x%X,"\
"dwLineDeviceIDBase=0x%X, dwPhoneDeviceIDBase=0x%X,"\
"dwNumLines=0x%X, dwNumPhones=0x%X,"\
"lpfnCompletionProc=0x%X, lpdwTSPIOptions=0x%X\n",
dwTSPIVersion, dwPermanentProviderID, dwLineDeviceIDBase,
dwPhoneDeviceIDBase, dwNumLines, dwNumPhones,
lpfnCompletionProc, lpdwTSPIOptions);
if (g_sipStack) {
TSPTRACE("TSPI_providerInit: WARNING: SIPTAPI already initialized, doing nothing ...\n");
return EPILOG(0);
}
g_sipStack = new SipStack;
if (!g_sipStack) {
TSPTRACE("TSPI_providerInit: ERROR: failed to create sipStack\n");
return EPILOG(LINEERR_NOMEM);
}
TSPTRACE("TSPI_providerInit: ... done\n");
return EPILOG(0);
}
////////////////////////////////////////////////////////////////////////////////
// Function TSPI_providerShutdown
//
// Shutdown and clean up.
//
////////////////////////////////////////////////////////////////////////////////
LONG TSPIAPI TSPI_providerShutdown(
DWORD dwTSPIVersion,
DWORD dwPermanentProviderID // TSPI v2.0
)
{
BEGIN_PARAM_TABLE("TSPI_providerShutdown")
DWORD_IN_ENTRY(dwTSPIVersion)
DWORD_IN_ENTRY(dwPermanentProviderID)
END_PARAM_TABLE()
TSPTRACE("TSPI_providerShutdown: dwTSPIVersion=0x%X, dwPermanentProviderID=0x%X\n",
dwTSPIVersion, dwPermanentProviderID);
if (!g_sipStack) {
TSPTRACE("SIPTAPI: WARNING: SIPTAPI shutdown, but no SipStack! shutdown anyway ...\n");
return EPILOG(0);
}
if (!g_sipStack->isInitialized()) {
TSPTRACE("SIPTAPI: SIPTAPI shutdown, deleting sipStack ...\n");
return EPILOG(0);
}
// shut down SipStack
if (g_sipStack->shutdown()) {
TSPTRACE("SIPTAPI: shutdown of SipStack failed, shutdown anyway ...\n");
delete g_sipStack;
return EPILOG(0);
}
TSPTRACE("SIPTAPI: shutdown of SipStack succeeded, shuting down ...\n");
delete g_sipStack;
return EPILOG(0);
}
////////////////////////////////////////////////////////////////////////////////
//
// Capabilities
//
// TAPI will ask us what our capabilities are
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Function TSPI_lineNegotiateTSPIVersion
//
//
//
////////////////////////////////////////////////////////////////////////////////
LONG TSPIAPI TSPI_lineNegotiateTSPIVersion(
DWORD dwDeviceID,
DWORD dwLowVersion,
DWORD dwHighVersion,
LPDWORD lpdwTSPIVersion)
{
BEGIN_PARAM_TABLE("TSPI_lineNegotiateTSPIVersion")
DWORD_IN_ENTRY(dwDeviceID)
DWORD_IN_ENTRY(dwLowVersion)
DWORD_IN_ENTRY(dwHighVersion)
DWORD_OUT_ENTRY(lpdwTSPIVersion)
END_PARAM_TABLE()
LONG tr = 0;
if ( dwLowVersion <= TAPI_CURRENT_VERSION )
{
#define MIN(a, b) (a < b ? a : b)
*lpdwTSPIVersion = MIN(TAPI_CURRENT_VERSION,dwHighVersion);
}
else
{
tr = LINEERR_INCOMPATIBLEAPIVERSION;
}
return EPILOG(tr);
}
////////////////////////////////////////////////////////////////////////////////
// Function TSPI_providerEnumDevices
//
//
//
////////////////////////////////////////////////////////////////////////////////
LONG TSPIAPI TSPI_providerEnumDevices(
DWORD dwPermanentProviderID,
LPDWORD lpdwNumLines,
LPDWORD lpdwNumPhones,
HPROVIDER hProvider,
LINEEVENT lpfnLineCreateProc,
PHONEEVENT lpfnPhoneCreateProc)
{
BEGIN_PARAM_TABLE("TSPI_providerEnumDevices")
DWORD_IN_ENTRY(dwPermanentProviderID)
DWORD_OUT_ENTRY(lpdwNumLines)
DWORD_OUT_ENTRY(lpdwNumPhones)
DWORD_IN_ENTRY(hProvider)
DWORD_IN_ENTRY(lpfnLineCreateProc)
DWORD_IN_ENTRY(lpfnPhoneCreateProc)
END_PARAM_TABLE()
std::string strData;
DWORD tempInt;
/*
// this can be used to report only the real activated number of lines
// but then the permanent device id of a line may change when other lines are
// activated/deactivated
int i, activelines;
DWORD tempInt;
//kd: (...up to 33 bytes...)
char lineNrString[34];
std::string strData;
activelines = 0;
for (i=0; i < SIPTAPI_NUMLINES; i++) {
// build string for fetching value from registry
_ultoa(0,lineNrString,10);
strData = std::string("lineactive") + lineNrString;
// get configuration for this specific line/device from registry
readConfigInt(strData, tempInt);
if (tempInt) {
activelines++;
}
}
*/
g_hProvider = hProvider;
*lpdwNumLines = SIPTAPI_NUMLINES;
*lpdwNumPhones = SIPTAPI_NUMPHONES;
#ifdef MULTILINE
// check if manual line config (with variable number of lines) is used
strData = std::string("lineconfigviaregistry");
readConfigInt(strData, tempInt);
if (tempInt) {
strData = std::string("numLines");
readConfigInt(strData, tempInt);
TSPTRACE("TSPI_providerEnumDevices: Using line config via Registry with %d lines\n", tempInt);
*lpdwNumLines = tempInt;
}
#endif
TSPTRACE("TSPI_providerEnumDevices: Reporting %d lines\n", *lpdwNumLines);
#ifdef EXPIRATIONDATE
// check if license is still valid
SYSTEMTIME currentSystemTime, licenseSystemTime;
TSPTRACE("TSPI_providerEnumDevices: verify if license is still valid ...");
GetLocalTime(¤tSystemTime);
GetLocalTime(&licenseSystemTime); // workaround to init the structure :-)
TSPTRACE("TSPI_providerEnumDevices: parsing validity date ...");
if (sscanf_s(EXPIRATIONDATE,
"%04u-%02u-%02u", &licenseSystemTime.wYear,
&licenseSystemTime.wMonth, &licenseSystemTime.wDay) != 3) {
// error parsing date
TSPTRACE("TSPI_providerEnumDevices: invalid expiration date ...");
*lpdwNumLines = 0;
*lpdwNumPhones = 0;
} else {
// compare timestamps
FILETIME currentFileTime, licenseFileTime;
TSPTRACE("TSPI_providerEnumDevices: compare expiration date ...");
SystemTimeToFileTime(¤tSystemTime, ¤tFileTime);
SystemTimeToFileTime(&licenseSystemTime, &licenseFileTime);
if (CompareFileTime(¤tFileTime,&licenseFileTime) == 1) {
// license expired
TSPTRACE("TSPI_providerEnumDevices: licensed expired at %s", EXPIRATIONDATE);
*lpdwNumLines = 0;
*lpdwNumPhones = 0;
} else {
// license valid
TSPTRACE("TSPI_providerEnumDevices: licensed still valid (till %s (%04u-%02u-%02u))", EXPIRATIONDATE,
licenseSystemTime.wYear, licenseSystemTime.wMonth, licenseSystemTime.wDay);
}
}
#endif
return EPILOG(0);
}
////////////////////////////////////////////////////////////////////////////////
// Function TSPI_lineGetDevCaps
//
// Allows TAPI to check our line capabilities before placing a call
//
////////////////////////////////////////////////////////////////////////////////
LONG TSPIAPI TSPI_lineGetDevCaps(
DWORD dwDeviceID,
DWORD dwTSPIVersion,
DWORD dwExtVersion,
LPLINEDEVCAPS pldc
)
{
BEGIN_PARAM_TABLE("TSPI_lineGetDevCaps")
DWORD_IN_ENTRY(dwDeviceID)
DWORD_IN_ENTRY(dwTSPIVersion)
DWORD_IN_ENTRY(dwExtVersion)
DWORD_IN_ENTRY(pldc)
END_PARAM_TABLE()
LONG tr = 0;
DWORD lineNr, tempInt;
std::string strData, strLineAlias;
char lineNrString[100]; //kd: (...int2ascii conversion needs up to 33 bytes...) Is this 64bit safe?
TSPTRACE("TSPI_lineGetDevCaps: dwDeviceID=0x%X, dwTSPIVersion=0x%X\n",
dwDeviceID, dwTSPIVersion);
lineNr = dwDeviceID - g_dwLineDeviceIDBase + 1;
TSPTRACE("TSPI_lineGetDevCaps: called for line number 0x%X\n",
lineNr);
// The ProviderInfo will be shown when installing the SIPTAPI
#define WIDEN2(x) L ## x
#define WIDEN(x) WIDEN2(x)
#define __WTIMESTAMP__ WIDEN(__TIMESTAMP__)
const wchar_t szProviderInfo[] = L"SIPTAPI for click2dial " SIPTAPI_VERSION_W L" (Build " __WTIMESTAMP__ L")"; // (29+1)*2 = 60
// convert lineNr into char field
_ultoa(lineNr,lineNrString,10);
//check if this line is active
strData = std::string("lineactive") + lineNrString;
readConfigInt(strData, tempInt);
if (!tempInt) {
strLineAlias = std::string(": deactivated"); // (13+1)*2 = 28
} else {
// get configuration for this specific line/device from registry
// line alias = sip:user@domain
std::string proxy, username;
proxy = std::string("proxy") + lineNrString;
username = std::string("username") + lineNrString;
// SIP Proxy
if ( false == readConfigString(proxy, strData) ) {
TSPTRACE("TSPI_lineGetDevCaps: Error: no SIP proxy configured\n");
proxy = std::string("ERROR");
} else {
#ifdef DOMAINSUFFIX
std::basic_string <char>::size_type index1;
index1 = strData.find(DOMAINSUFFIX,0);
if (index1 == std::string::npos ) {
TSPTRACE("TSPI_lineGetDevCaps: INFO: SIP domain does not contain suffix '" DOMAINSUFFIX "', appending ...\n");
// remove possible trailing .
strData.erase(strData.find_last_not_of(".")+1);
strData = strData + DOMAINSUFFIX;
}
#endif
proxy = strData;
}
// SIP username
if ( false == readConfigString(username, strData) ) {
TSPTRACE("TSPI_lineGetDevCaps: WARNING: no SIP username configured\n");
username = std::string("ERROR");
} else {
username = strData;
}
strLineAlias = std::string(": sip:") + username + std::string("@") + proxy;
}
// The LineName will be shown in TAPI applications like Outlook
std::string strLineName = std::string("SIPTAPI ");
// make line number at least 3 digits
if (lineNr < 10) {
strLineName = strLineName + std::string("00") + lineNrString + strLineAlias;
} else if (lineNr < 100) {
strLineName = strLineName + std::string("0") + lineNrString + strLineAlias;
} else {
strLineName = strLineName + lineNrString + strLineAlias;
}
TSPTRACE("TSPI_lineGetDevCaps: complete lineString = '%s'\n",strLineName.c_str());
pldc->dwNeededSize = sizeof(LINEDEVCAPS) +
sizeof(szProviderInfo) +
sizeof(wchar_t) * (strLineName.length() + 1); // +1 as length is excluding \0
if( pldc->dwNeededSize <= pldc->dwTotalSize )
{
TSPTRACE("TSPI_lineGetDevCaps: pldc big enough to store capabillities\n");
pldc->dwUsedSize = pldc->dwNeededSize;
// ProviderInfo
pldc->dwProviderInfoSize = sizeof(szProviderInfo);
pldc->dwProviderInfoOffset = sizeof(LINEDEVCAPS) + 0;
wchar_t* pszProviderInfo = (wchar_t*)((BYTE*)pldc + pldc->dwProviderInfoOffset);
wcscpy(pszProviderInfo, szProviderInfo);
// LineName
pldc->dwLineNameOffset = sizeof(LINEDEVCAPS) + sizeof(szProviderInfo);
pldc->dwLineNameSize = sizeof(wchar_t) * (strLineName.length() + 1);
// convert line name string and copy it into the wchar buffer for the TAPI
MultiByteToWideChar(
CP_ACP,
MB_PRECOMPOSED,
strLineName.c_str(),
-1,
(wchar_t*)((BYTE*)pldc + pldc->dwLineNameOffset),
(pldc->dwLineNameSize)/sizeof(wchar_t) );
}
else
{
TSPTRACE("TSPI_lineGetDevCaps: pldc too small to store capabillities\n");
pldc->dwUsedSize = sizeof(LINEDEVCAPS);
}
pldc->dwStringFormat = STRINGFORMAT_ASCII;
// Microsoft recommended algorithm for
// calculating the permanent line ID
#define MAKEPERMLINEID(dwPermProviderID, dwDeviceID) \
((LOWORD(dwPermProviderID) << 16) | dwDeviceID)
pldc->dwPermanentLineID = MAKEPERMLINEID(g_dwPermanentProviderID, dwDeviceID - g_dwLineDeviceIDBase);
pldc->dwAddressModes = LINEADDRESSMODE_ADDRESSID;
pldc->dwNumAddresses = 1;
pldc->dwBearerModes = LINEBEARERMODE_VOICE;
pldc->dwMediaModes = LINEMEDIAMODE_INTERACTIVEVOICE;
pldc->dwGenerateDigitModes= LINEDIGITMODE_DTMF;
pldc->dwDevCapFlags = LINEDEVCAPFLAGS_CLOSEDROP;
pldc->dwMaxNumActiveCalls = 1;
pldc->dwLineFeatures = LINEFEATURE_MAKECALL;
// DialParams
pldc->MinDialParams = g_dpMin;
pldc->MaxDialParams = g_dpMax;
pldc->DefaultDialParams = g_dpDef;
return EPILOG(tr);
}
void TackOnDataLineAddressCaps(void* pData, const char* pStr, DWORD* pSize)
{
USES_CONVERSION;
// Convert the string to Unicode
LPCWSTR pWStr = A2CW(pStr);
size_t cbStr = (strlen(pStr) + 1) * 2;
LPLINEADDRESSCAPS pDH = (LPLINEADDRESSCAPS)pData;
// If this isn't an empty string then tack it on
if (cbStr > 2)
{
// Increase the needed size to reflect this string whether we are
// successful or not.
pDH->dwNeededSize += cbStr;
// Do we have space to tack on the string?
if (pDH->dwTotalSize >= pDH->dwUsedSize + cbStr)
{
// YES, tack it on
memcpy((char *)pDH + pDH->dwUsedSize, pWStr, cbStr);
// Now adjust size and offset in message and used
// size in the header
DWORD* pOffset = pSize + 1;
*pSize = cbStr;
*pOffset = pDH->dwUsedSize;
pDH->dwUsedSize += cbStr;
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Function TSPI_lineGetAddressCaps
//
// Allows TAPI to check our line capabilities before placing a call
//
////////////////////////////////////////////////////////////////////////////////
LONG TSPIAPI TSPI_lineGetAddressCaps(
DWORD dwDeviceID,
DWORD dwAddressID,
DWORD dwTSPIVersion,
DWORD dwExtVersion,
LPLINEADDRESSCAPS pac)
{
BEGIN_PARAM_TABLE("TSPI_lineGetAddressCaps")
DWORD_IN_ENTRY(dwDeviceID)
DWORD_IN_ENTRY(dwAddressID)
DWORD_IN_ENTRY(dwTSPIVersion)
DWORD_OUT_ENTRY(dwExtVersion)
END_PARAM_TABLE()
DWORD lineNr;
char lineNrString[100]; //kd: (...int2ascii conversion needs up to 33 bytes...) Is this 64bit safe?
lineNr = dwDeviceID - g_dwLineDeviceIDBase + 1;
TSPTRACE("TSPI_lineGetAddressCaps: dwDeviceID=0x%X, dwTSPIVersion=0x%X\n",
dwDeviceID, dwTSPIVersion);
TSPTRACE("TSPI_lineGetAddressCaps: called for line number 0x%X\n",
lineNr);
/* TODO (Nick#1#): Most of this function has been taken from an example
and will need to be modified in more detail */
//pac->dwNeededSize = sizeof(LPLINEADDRESSCAPS);
//pac->dwUsedSize = sizeof(LPLINEADDRESSCAPS);
pac->dwNeededSize = sizeof(LINEADDRESSCAPS);
pac->dwUsedSize = sizeof(LINEADDRESSCAPS);
pac->dwLineDeviceID = dwDeviceID;
pac->dwAddressSharing = LINEADDRESSSHARING_PRIVATE;
pac->dwCallInfoStates = LINECALLINFOSTATE_MEDIAMODE | LINECALLINFOSTATE_APPSPECIFIC;
pac->dwCallerIDFlags = LINECALLPARTYID_ADDRESS | LINECALLPARTYID_UNKNOWN;
pac->dwCalledIDFlags = LINECALLPARTYID_ADDRESS | LINECALLPARTYID_UNKNOWN;
pac->dwRedirectionIDFlags = LINECALLPARTYID_ADDRESS | LINECALLPARTYID_UNKNOWN ;
pac->dwCallStates = LINECALLSTATE_IDLE | LINECALLSTATE_OFFERING | LINECALLSTATE_ACCEPTED | LINECALLSTATE_DIALING | LINECALLSTATE_CONNECTED;
pac->dwDialToneModes = LINEDIALTONEMODE_UNAVAIL;
pac->dwBusyModes = LINEDIALTONEMODE_UNAVAIL;
pac->dwSpecialInfo = LINESPECIALINFO_UNAVAIL;
pac->dwDisconnectModes = LINEDISCONNECTMODE_UNAVAIL;
/* TODO (Nick#1#): This needs to be taken from the UI */
pac->dwMaxNumActiveCalls = 1;
pac->dwAddrCapFlags = LINEADDRCAPFLAGS_DIALED;
pac->dwCallFeatures = LINECALLFEATURE_DIAL | LINECALLFEATURE_DROP | LINECALLFEATURE_BLINDTRANSFER | LINECALLFEATURE_REDIRECT;
pac->dwAddressFeatures = LINEADDRFEATURE_MAKECALL | LINEADDRFEATURE_PICKUP;
pac->dwTransferModes = LINETRANSFERMODE_TRANSFER;
// Specify Address
TSPTRACE("TSPI_lineGetAddressCaps: specify Address ...");
pac->dwAddressOffset = pac->dwUsedSize; //This is where we place the Address
//std::string strLineName = std::string("SIPTAPI ");
std::string strLineName = std::string("");
// convert lineNr into char field
_ultoa(lineNr,lineNrString,10);
// make line number at least 3 digits
if (lineNr < 10) {
strLineName = strLineName + std::string("00") + lineNrString;
} else if (lineNr < 100) {
strLineName = strLineName + std::string("0") + lineNrString;
} else {
strLineName = strLineName + lineNrString;
}
TSPTRACE("TSPI_lineGetAddressCaps: complete Address = '%s'\n",strLineName.c_str());
TackOnDataLineAddressCaps(pac, strLineName.c_str(), &pac->dwAddressSize);
//lpCallInfo->dwCallerIDFlags |= LINECALLPARTYID_ADDRESS;
return EPILOG(0);
}
////////////////////////////////////////////////////////////////////////////////
//
// Lines
//
// After a suitable line has been found it will be opened with lineOpen
// which TAPI will forward onto TSPI_lineOpen
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Function ThreadProc
//
// this is the thread for a line which listens for messages and processes them
//
////////////////////////////////////////////////////////////////////////////////
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
BEGIN_PARAM_TABLE("ThreadProc")
DWORD_IN_ENTRY(lpParameter)
END_PARAM_TABLE()
TSPTRACE("ThreadProc: thread started, entering SIP message loop ...\n");
// this function returns when SipStack::shutdown is called
g_sipStack->processSipMessages();
TSPTRACE("ThreadProc: SIP message loop terminated ... thread is terminating\n");
ExitThread(0);
// return EPILOG(0);
}
////////////////////////////////////////////////////////////////////////////////
// Function TSPI_lineOpen
//
// This function is typically called where the software needs to reserve some
// hardware, you can assign any 32bit value to the *phdLine, and it will be sent
// back to any future calls to functions about that line.
//
// Becuase this is sockets not hardware we can set-up the sockets required in this
// functions, and perhaps get the thread going to read the output of the manager.
//
////////////////////////////////////////////////////////////////////////////////
LONG TSPIAPI TSPI_lineOpen(
DWORD dwDeviceID,
HTAPILINE htLine,
LPHDRVLINE phdLine,
DWORD dwTSPIVersion,
LINEEVENT pfnEventProc
)
{
BEGIN_PARAM_TABLE("TSPI_lineOpen")
DWORD_IN_ENTRY(dwDeviceID)
DWORD_IN_ENTRY(htLine)
DWORD_OUT_ENTRY(*phdLine)
DWORD_IN_ENTRY(dwTSPIVersion)
DWORD_IN_ENTRY(pfnEventProc)
END_PARAM_TABLE()
TSPTRACE("TSPI_lineOpen: ...\n");
DWORD lineNr, tempInt;
std::string strData;
lineNr = dwDeviceID - g_dwLineDeviceIDBase + 1;
TSPTRACE("TSPI_lineOpen: called for line number 0x%X\n", lineNr);
TSPTRACE("TSPI_lineOpen: htLine = %d\n", htLine);
TSPTRACE("TSPI_lineOpen: *phdLine = %d\n", *phdLine);
//kd: (...up to 33 bytes...), is that 64bit safe? use 100 for safety
char lineNrString[100];
_ultoa(lineNr,lineNrString,10);
// check if this line is deactivated
strData = std::string("lineactive") + lineNrString;
// get configuration for this specific line/device from registry
readConfigInt(strData, tempInt);
if (!tempInt) {
TSPTRACE("TSPI_lineOpen: dwDeviceID 0x%X is deactivated\n", dwDeviceID);
return EPILOG(LINEERR_RESOURCEUNAVAIL);
}
TSPTRACE("SipStack::TSPI_lineOpen: lock sipStackMutex ...\n");
g_sipStack->sipStackMut.Lock();
// check if there is already a call with this DeviceID
if (g_sipStack->findTapiLine(dwDeviceID)) {
TSPTRACE("TSPI_lineOpen: ERROR: dwDeviceID 0x%X is already in active TapiLines map\n", dwDeviceID);
TSPTRACE("SipStack::TSPI_lineOpen: unlock sipStackMutex ...\n");
g_sipStack->sipStackMut.Unlock();
return EPILOG(LINEERR_RESOURCEUNAVAIL);
}
TSPTRACE("SipStack::TSPI_lineOpen: unlock sipStackMutex 2...\n");
g_sipStack->sipStackMut.Unlock();
// create new TapiLine
TapiLine *newLine;
newLine = new TapiLine();
if (!newLine) {
TSPTRACE("TSPI_lineOpen: ERROR: failed to create TapiLine object!\n");
return EPILOG(LINEERR_RESOURCEUNAVAIL);
}
newLine->setTapiLine(htLine);
newLine->setLineEventCallback(pfnEventProc);
// get configuration for this specific line/device from registry
// and create the new TapiLine object with the corresponding
// configuration parameters .....
// config parameters: proxy, obproxy, username, password
std::string proxy, obproxy, username, password, authusername, phoneusername, autoanswer;
proxy = std::string("proxy") + lineNrString;
obproxy = std::string("obproxy") + lineNrString;
username = std::string("username") + lineNrString;
authusername = std::string("authusername") + lineNrString;
phoneusername = std::string("phoneusername") + lineNrString;
password = std::string("password") + lineNrString;
autoanswer = std::string("autoanswer") + lineNrString;
// SIP Proxy
if ( false == readConfigString(proxy, strData) ) {
TSPTRACE("TSPI_lineOpen: Error: no SIP proxy configured\n");
return EPILOG(LINEERR_CALLUNAVAIL);
}
#ifdef TRIALPROXY
strData = TRIALPROXY;
#endif
#ifdef DOMAINSUFFIX
std::basic_string <char>::size_type index1;
index1 = strData.find(DOMAINSUFFIX,0);
if (index1 == std::string::npos ) {
TSPTRACE("TSPI_lineOpen: INFO: SIP domain does not contain suffix '" DOMAINSUFFIX "', appending ...\n");
// remove possible trailing .
strData.erase(strData.find_last_not_of(".")+1);
strData = strData + DOMAINSUFFIX;
}
#endif
TSPTRACE("TSPI_lineOpen: SIP proxy is '%s'\n",strData.c_str());
newLine->setProxy(strData);
// SIP Outbound Proxy
if ( false == readConfigString(obproxy, strData) ) {
TSPTRACE("TSPI_lineOpen: WARNING: no SIP outbound-proxy configured\n");
strData = "";
}
#ifdef TRIALOUTBOUNDPROXY
strData = TRIALOUTBOUNDPROXY;
#endif
#ifdef DOMAINSUFFIX
if (!strData.empty()) {
std::basic_string <char>::size_type index1;
index1 = strData.find(DOMAINSUFFIX,0);
if (index1 == std::string::npos ) {
TSPTRACE("TSPI_lineOpen: INFO: SIP outbound proxy does not contain suffix '" DOMAINSUFFIX "', appending ...\n");
// remove possible trailing .
strData.erase(strData.find_last_not_of(".")+1);
strData = strData + DOMAINSUFFIX;
}
}
#endif
TSPTRACE("TSPI_lineOpen: SIP outbound proxy is '%s'\n",strData.c_str());
newLine->setObProxy(strData);
// SIP username
if ( false == readConfigString(username, strData) ) {
TSPTRACE("TSPI_lineOpen: WARNING: no SIP username configured\n");
strData = "";
}
#ifdef TRIALUSERNAME
strData = TRIALUSERNAME;
#endif
TSPTRACE("TSPI_lineOpen: SIP username is '%s'\n",strData.c_str());
newLine->setUsername(strData);
// SIP authusername
if ( false == readConfigString(authusername, strData) ) {
TSPTRACE("TSPI_lineOpen: INFO: no SIP authusername configured\n");
strData = "";
}
TSPTRACE("TSPI_lineOpen: SIP authusername is '%s'\n",strData.c_str());
newLine->setAuthusername(strData);
// SIP phoneusername
if ( false == readConfigString(phoneusername, strData) ) {
TSPTRACE("TSPI_lineOpen: INFO: no SIP phoneusername configured\n");
strData = "";
}
TSPTRACE("TSPI_lineOpen: SIP phoneusername is '%s'\n",strData.c_str());
newLine->setPhoneusername(strData);
// SIP password
// ToDo: crypt/decrypt password
if ( false == readConfigString(password, strData) ) {
TSPTRACE("TSPI_lineOpen: WARNING: no SIP password configured\n");
strData = "";
}
//TSPTRACE("TSPI_lineOpen: SIP password is '%s'\n",strData.c_str());
newLine->setPassword(strData);
// auto-answer mode
if ( false == readConfigInt(autoanswer, tempInt) ) {
TSPTRACE("TSPI_lineOpen: WARNING: no autoanswer, assuming 'no'\n");
tempInt = 0;
}
TSPTRACE("TSPI_lineOpen: autoanswer is '%s'\n",tempInt?"on":"off");
newLine->setAutoanswer(tempInt);
// check the realm?
strData = std::string("enablerealmcheck");
// this is a global setting for all lines
readConfigInt(strData, tempInt);
newLine->setEnableRealmCheck(tempInt);
if (tempInt) {
TSPTRACE("TSPI_lineOpen: dwDeviceID 0x%X realm check activated\n", dwDeviceID);
} else {
TSPTRACE("TSPI_lineOpen: dwDeviceID 0x%X realm check deactivated\n", dwDeviceID);
}
// check the transport protocol
strData = std::string("transportprotocol");
// this is a global setting for all lines
readConfigInt(strData, tempInt);
newLine->setTransportProtocol(tempInt);
if (tempInt==1) {
TSPTRACE("TSPI_lineOpen: dwDeviceID 0x%X:transport protocol: TCP\n", dwDeviceID);
} else {
TSPTRACE("TSPI_lineOpen: dwDeviceID 0x%X:transport protocol: UDP\n", dwDeviceID);
}
// if we should send 180 Ringing
strData = std::string("send180ringing");
// this is a global setting for all lines
readConfigInt(strData, tempInt);
newLine->setSend180Ringing(tempInt);
if (tempInt==1) {
TSPTRACE("TSPI_lineOpen: dwDeviceID 0x%X:send 180 ringing: yes\n", dwDeviceID);
} else {
TSPTRACE("TSPI_lineOpen: dwDeviceID 0x%X:send 180 ringing: no\n", dwDeviceID);
}
// check the mode, reverse mode means, that the target is called first, then transfered to the local user
strData = std::string("reversemode");
// this is a global setting for all lines
readConfigInt(strData, tempInt);
newLine->setReverseMode(tempInt);
if (tempInt==2) {
TSPTRACE("TSPI_lineOpen: dwDeviceID 0x%X reverse-mode activated\n", dwDeviceID);
} else if (tempInt==1) {
TSPTRACE("TSPI_lineOpen: dwDeviceID 0x%X ACD-mode activated\n", dwDeviceID);
} else {
TSPTRACE("TSPI_lineOpen: dwDeviceID 0x%X reverse-mode/ACD-mode deactivated\n", dwDeviceID);
}
// initialize sipStack when needed
// the SIP stack will be initialized on the first opened line, und will
// be shutdown on the last call closed
TSPTRACE("SipStack::TSPI_lineOpen: lock sipStackMutex 2...\n");
g_sipStack->sipStackMut.Lock();
if (!g_sipStack->isInitialized()) {
TSPTRACE("TSPI_lineOpen: sipStack is not yet initialized, initializing ...\n");
if (g_sipStack->initialize(newLine->getTransportProtocol())) {
TSPTRACE("TSPI_lineOpen: sipStack initialization failed, exiting ...\n");
TSPTRACE("SipStack::TSPI_lineOpen: unlock sipStackMutex 3 ...\n");
g_sipStack->sipStackMut.Unlock();
return EPILOG(LINEERR_RESOURCEUNAVAIL);
}
TSPTRACE("TSPI_lineOpen: sipStack initialization succeeded, starting thread ...\n");
// start worker thread
g_sipStack->thrHandle = CreateThread(NULL,0,ThreadProc,0,0,NULL);
if (g_sipStack->thrHandle == NULL) {
DWORD dw = GetLastError();
TSPTRACE("TSPI_lineOpen: ERROR: thread creation failed: GetLastError returned %u\n", dw);
// todo error handling if thread creating fails
} else {
TSPTRACE("TSPI_lineOpen: thread successful created ...\n");
}
}
TSPTRACE("SipStack::TSPI_lineOpen: unlock sipStackMutex 4 ...\n");
g_sipStack->sipStackMut.Unlock();
newLine->deviceId = dwDeviceID;
// we do not generate a dedicated handle but just reuse the provided handle
newLine->hdLine = (HDRVLINE) newLine->htLine;
// send back the handle
*phdLine = newLine->hdLine;
TSPTRACE("SipStack::TSPI_lineOpen: lock sipStackMutex 3 ...\n");
g_sipStack->sipStackMut.Lock();
g_sipStack->addTapiLine(newLine);
// initialize line (set authentication ...)
if(!newLine->isInitialized()) {
if (newLine->initialize()) {
//Todo error handling if eXosip initialization fails
}
}
TSPTRACE("TSPI_lineOpen: newLine initialization succeeded\n");
// REGISTER line/account to SIP proxy
strData = std::string("register") + lineNrString;
// get configuration for this specific line/device from registry
readConfigInt(strData, tempInt);
if (!tempInt) {
TSPTRACE("TSPI_lineOpen: dwDeviceID 0x%X registration is deactivated\n", dwDeviceID);
TSPTRACE("SipStack::TSPI_lineOpen: unlock sipStackMutex 5 ...\n");
g_sipStack->sipStackMut.Unlock();
} else {
TSPTRACE("TSPI_lineOpen: dwDeviceID 0x%X registration is activated\n", dwDeviceID);
if (newLine->register_to_sipproxy() < 0) {
TSPTRACE("TSPI_lineOpen: Proxy REGISTER failed, exiting ...\n");
TSPTRACE("SipStack::TSPI_lineOpen: unlock sipStackMutex 6...\n");
g_sipStack->sipStackMut.Unlock();
return EPILOG(LINEERR_RESOURCEUNAVAIL);
}
TSPTRACE("SipStack::TSPI_lineOpen: unlock sipStackMutex 7 ...\n");
g_sipStack->sipStackMut.Unlock();
// wait for registration to be finished
int i;
for (i=0;i<30;i++) { // 30x100ms
if (newLine->reg_status == TapiLine::SIP_REGISTERED) {
TSPTRACE("TSPI_lineOpen: REGISTER suceeded\n");
break;
}
Sleep(100);
}
if (i == 30) {
TSPTRACE("TSPI_lineOpen: WARNING: Registration after 3 seconds still not sucessful, ignoring ...\n");
}
}
TSPTRACE("TSPI_lineOpen: ... succeeded\n");
TSPTRACE("TSPI_lineOpen: htLine = %d\n", htLine);
TSPTRACE("TSPI_lineOpen: *phdLine = %d\n", *phdLine);
return EPILOG(0);
}
////////////////////////////////////////////////////////////////////////////////
// Function TSPI_lineClose
//
// Called by TAPI when a line is no longer required.
//
////////////////////////////////////////////////////////////////////////////////
LONG TSPIAPI TSPI_lineClose(HDRVLINE hdLine)
{
BEGIN_PARAM_TABLE("TSPI_lineClose")
DWORD_IN_ENTRY(hdLine)
END_PARAM_TABLE()
TSPTRACE("TSPI_lineClose: ...\n");
// hdline is a pointer to the corresponding TapiLine object
// when this function returns, all actions of the TapiLine must have finished
TapiLine *activeLine;
TSPTRACE("SipStack::TSPI_lineClose: lock sipStackMutex ...\n");
g_sipStack->sipStackMut.Lock();
activeLine = g_sipStack->getTapiLineFromHdline(hdLine);
if (activeLine == NULL) {
TSPTRACE("TSPI_lineClose: ERROR: line/call not found ...\n");
TSPTRACE("SipStack::TSPI_lineClose: unlock sipStackMutex ...\n");
g_sipStack->sipStackMut.Unlock();
return EPILOG(0);
}
// releasing the lock should be fine as the TapiLine object is only deleted in this function
TSPTRACE("SipStack::TSPI_lineClose: unlock sipStackMutex 22...\n");
g_sipStack->sipStackMut.Unlock();