forked from nic-at/siptapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.cpp
348 lines (300 loc) · 8.57 KB
/
utilities.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
/*
Name: Utilities.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 ***** */
#define STRICT
#include <winsock2.h>
#include <windows.h>
#include <windowsx.h>
#include <tapi.h>
#include <tspi.h>
#include <strsafe.h>
#include "wavetsp.h"
#include <string>
#include <eXosip2/eXosip.h>
#include "utilities.h"
//Utilities for the windows registry, so we can save out and read in the settings we require
//const char RegKey[] = "Software\\IPCom\\SIPTAPI";
//const char RegKey[] = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Telephony\\SIPTAPI";
const char SubKeyName[] = "SIPTAPI";
const char SubKeyRoot[] = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Telephony";
const char SubKeyComplete[] = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Telephony\\SIPTAPI";
const HKEY WHICHKEY = HKEY_LOCAL_MACHINE;
Mutex outputMutex;
//Called once to make sure everything is in place.
bool initConfigStore(void)
{
HKEY ourKey;
DWORD result;
TspTrace("initConfigStore: creating Registry Path HKEY_LOCAL_MACHINE\\%s ...", SubKeyComplete);
if ( ERROR_SUCCESS == RegOpenKeyEx(WHICHKEY, SubKeyRoot, 0, KEY_ALL_ACCESS , &ourKey) )
{
HKEY newKey;
if ( ERROR_SUCCESS != RegCreateKeyEx(
ourKey,
SubKeyName,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&newKey,
&result
)) {
TspTrace("initConfigStore: Failed to create key HKEY_LOCAL_MACHINE\\%s",SubKeyComplete);
}
RegCloseKey(newKey);
RegCloseKey(ourKey);
return true;
}
return false;
}
bool storeConfigString(std::string item, std::string str)
{
HKEY ourKey;
if ( ERROR_SUCCESS == RegOpenKeyEx(WHICHKEY,
SubKeyComplete,
0,
KEY_ALL_ACCESS /* for just reading we would use KEY_READ*/,
&ourKey) )
{
//at this point we should have our key opened.
if ( ERROR_SUCCESS != RegSetValueEx(ourKey,
item.c_str(),
0,
REG_SZ,
(const BYTE *)str.c_str(),
str.size()) )
{
//MessageBox(0, "Failed set value", "Windows Registry error", MB_SETFOREGROUND);
TSPTRACE("Failed to set registry value");
return false;
}
RegCloseKey(ourKey);
}
else
{
std::string msg = "Failed to open key: ";
msg += SubKeyComplete;
//MessageBox(0, msg.c_str() , "Windows Registry error", MB_SETFOREGROUND);
TSPTRACE(msg.c_str());
return false;
}
return true;
}
bool readConfigString(std::string item, std::string &str)
{
HKEY ourKey;
char tempStr[100];
DWORD type;
DWORD length = sizeof(tempStr);
str = "";
if ( ERROR_SUCCESS == RegOpenKeyEx(WHICHKEY, SubKeyComplete, 0,KEY_READ, &ourKey) )
{
//at this point we should have our key opened.
if ( ERROR_SUCCESS != RegQueryValueEx(ourKey,item.c_str(), 0, &type,(BYTE*)&tempStr[0],&length ) )
{
//MessageBox(0, "Failed to read value", "Windows Registry error", MB_SETFOREGROUND);
TSPTRACE("Failed to read value");
return false;
}
else if ( REG_SZ != type )
{
//MessageBox(0, "unexpected data type", "Windows Registry error", MB_SETFOREGROUND);
TSPTRACE("Unexpected data type");
return false;
}
RegCloseKey(ourKey);
}
else
{
std::string msg = "Failed to open key: ";
msg += SubKeyComplete;
//MessageBox(0, msg.c_str() , "Windows Registry error", MB_SETFOREGROUND);
TSPTRACE(msg.c_str());
return false;
}
//ensure it is terminated
tempStr[length] = 0;
str = tempStr;
return true;
}
bool storeConfigInt(std::string item, DWORD value)
{
HKEY ourKey;
if ( ERROR_SUCCESS == RegOpenKeyEx(WHICHKEY, SubKeyComplete, 0,KEY_ALL_ACCESS /* for just reading we would use KEY_READ*/, &ourKey) )
{
//at this point we should have our key opened.
if ( ERROR_SUCCESS != RegSetValueEx(ourKey,item.c_str(), 0, REG_DWORD,(const BYTE *)&value,sizeof(DWORD)) )
{
//MessageBox(0, "Failed set value", "Windows Registry error", MB_SETFOREGROUND);
TSPTRACE("Failed to set value");
return false;
}
RegCloseKey(ourKey);
}
else
{
std::string msg = "Failed to open key: ";
msg += SubKeyComplete;
//MessageBox(0, msg.c_str() , "Windows Registry error", MB_SETFOREGROUND);
TSPTRACE("Failed to open key");
return false;
}
return true;
}
bool readConfigInt(std::string item, DWORD &value)
{
HKEY ourKey;
char tempStr[100];
DWORD type;
DWORD length = sizeof(tempStr);
value = 0;
if ( ERROR_SUCCESS == RegOpenKeyEx(WHICHKEY, SubKeyComplete, 0,KEY_READ, &ourKey) )
{
//at this point we should have our key opened.
if ( ERROR_SUCCESS != RegQueryValueEx(ourKey,item.c_str(), 0, &type,(BYTE*)&value,&length ) )
{
//MessageBox(0, "Failed to read value", "Windows Registry error", MB_SETFOREGROUND);
TSPTRACE("Failed to read value");
return false;
}
else if ( REG_DWORD != type )
{
MessageBox(0, "Unexpected data type", "Windows Registry error", MB_SETFOREGROUND);
TSPTRACE("Unexpected data type");
return false;
}
RegCloseKey(ourKey);
}
else
{
std::string msg = "Failed to open key: ";
msg += SubKeyComplete;
//MessageBox(0, msg.c_str() , "Windows Registry error", MB_SETFOREGROUND);
TSPTRACE(msg.c_str());
return false;
}
return true;
}
#if defined(_DEBUG) && defined(DEBUGTSP)
static
void DumpParams(FUNC_INFO* pInfo, ParamDirection dir)
{
TSPTRACE(" %s parameters:\n", (dir == dirIn ? "in" : "out"));
FUNC_PARAM* begin = &pInfo->rgParams[0];
FUNC_PARAM* end = &pInfo->rgParams[pInfo->dwNumParams];
for( FUNC_PARAM* pParam = begin; pParam != end; ++pParam ) {
if( pParam->dir == dir ) {
switch( pParam->pt ) {
case ptString: {
//char sz[MAX_PATH+1] = "<NULL>";
//if( pParam->dwVal ) {
// /* NOTE TODO KLAUS This crashes on Unicode strings, thus we currently
// * do not log strings
// */
// wcstombs(sz, (const wchar_t*)pParam->dwVal, MAX_PATH);
// sz[MAX_PATH] = 0;
//}
//TSPTRACE(" %s= 0x%lx '%s'\n", pParam->pszVal, pParam->dwVal, sz);
TSPTRACE(" %s= (logging disabled due to problems)\n", pParam->pszVal);
break;
}
case ptDword:
default:
if( dir == dirIn ) {
TSPTRACE(" %s= 0x%lx\n", pParam->pszVal, pParam->dwVal);
} else {
TSPTRACE(" %s= 0x%lx\n", pParam->pszVal, pParam->dwVal);
// TSPTRACE(" %s= 0x%lx '0x%lx'\n", pParam->pszVal, pParam->dwVal, (pParam->dwVal ? *(DWORD*)pParam->dwVal : 0));
}
break;
}
}
}
}
void Prolog(FUNC_INFO* pInfo)
{
char sz[MAX_PATH+1];
GetModuleFileName(0, sz, MAX_PATH);
TSPTRACE("%s() from %s\n", pInfo->pszFuncName, sz);
DumpParams(pInfo, dirIn);
}
LONG Epilog(FUNC_INFO* pInfo, LONG tr)
{
DumpParams(pInfo, dirOut);
TSPTRACE("%s() returning 0x%x\n\n", pInfo->pszFuncName, tr);
return tr;
}
void TspTrace(LPCSTR pszFormat, ...)
{
#define DEBUGBUFLEN 4096
char buf[DEBUGBUFLEN] = "SIPTAPI_0.3: ";
va_list ap;
outputMutex.Lock();
va_start(ap, pszFormat);
// wvsprintf(buf, pszFormat, ap);
StringCbVPrintf(buf + strlen(buf), DEBUGBUFLEN-strlen(buf), pszFormat, ap);
OutputDebugString(buf);
#ifdef DEBUGTSPTOFILE
HANDLE hFile = CreateFile("c:\\siptapi.log", GENERIC_WRITE,
0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile != INVALID_HANDLE_VALUE) {
SetFilePointer(hFile, 0, 0, FILE_END);
//DWORD err = GetLastError();
DWORD nBytes;
WriteFile(hFile, buf, strlen(buf), &nBytes, 0);
CloseHandle(hFile);
}
#endif
va_end(ap);
outputMutex.Unlock();
}
void TspSipTrace(LPCSTR pszFormat, osip_message_t *msg)
{
char *dest=NULL;
size_t length=0;
int i;
i = osip_message_to_str(msg, &dest, &length);
if (i!=0) {
TspTrace("TspSipTrace: cannot get printable message\n");
} else {
TspTrace("TspSipTrace: %s: \n", pszFormat);
TspTrace("%s\n", dest);
osip_free(dest);
}
}
//void TspSipTrace(LPCSTR pszFormat, osip_message_t *msg)
//{
// char *dest=NULL;
// size_t length=0;
// int i;
//
// i = osip_message_to_str(msg, &dest, &length);
// if (i!=0) {
// TspTrace("TspSipTrace: cannot get printable message\n");
// } else {
// TspTrace("TspSipTrace: %s: %s\n", pszFormat, dest);
// osip_free(dest);
// }
//}
#endif // _DEBUG