TestPlugin.cpp
16.4 KB
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
/////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002 Ultr@VNC. All Rights Reserved.
//
// This program is 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 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope 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, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
//
// If the source code for the program is not available from the place from
// which you received this file, check
// http://ultravnc.sourceforge.net/
//
////////////////////////////////////////////////////////////////////////////
//
// IF YOU USE THIS GPL SOURCE CODE TO MAKE YOUR DSM PLUGIN, PLEASE ADD YOUR
// COPYRIGHT TO THE TOP OF THIS FILE AND SHORTLY DESCRIBE/EXPLAIN THE
// MODIFICATIONS YOU'VE MADE. THANKS.
//
// IF YOU DON'T USE THIS CODE AS A BASE FOR YOUR PLUGIN, THE HEADER ABOVE AND
// ULTR@VNC COPYRIGHT SHOULDN'T BE FOUND IN YOUR PLUGIN SOURCE CODE.
//
////////////////////////////////////////////////////////////////////////////
//
// Testplugin is a sample that shows the rules that a DSM Plugin for UltraVNC
// must follow (Fonctions to export, their signatures, design constraints...)
//
// WARNING: As it is still beta, some rules might change in the near future
//
//
// WARNING : In order to avoid multithreading issues with your DSM Plugin, it is recommended
// (not to say mandatory...) to use 2 distincts contexts (buffers, structs, objects, keys on so on)
// for "Tranformation" and "Restoration" operations.
// Actually, 2 distinct mutexes are used at DSMPlugin.cpp level (one in the TransformBuffer
// function, and the other in the RestoreBuffer function) so Transform and Restore events
// may occur at the same time.
//
//////////////////////////////////////////////////////////////////////////////
#include "TestPlugin.h"
HINSTANCE hInstance;
PLUGINSTRUCT* pPlugin = NULL; // struct (or class instance) that handles all Plugin params.
// Given as an example.
// Internal Plugin vars, depending on what it does
char szExternalKey[255]; // To store the password/key transmitted via SetParams() by UltraVNC apps
char szLoaderType[32]; // To store the type of application that has loaded the plugin
BYTE* pLocalTransBuffer = NULL; // Local Transformation buffer (freed on VNC demand)
BYTE* pLocalRestBuffer = NULL; // Local Restoration buffer (freed on VNC demand)
int nLocalTransBufferSize = 0;
int nLocalRestBufferSize = 0;
//
// Plugin Description
// Please use the following format (with ',' (comma) as separator)
// Name-Description,Author,Date,Version,FileName-Comment
// For the version, we recommend the following format: x.y.z
// The other fields (Name-Description, Author, Date, FileName-Comment) are format free (don't use ',' in them, of course)
#define PLUGIN_DESCRIPTION "TestPlugin,Sam,Nov 21 2002,1.0.0,TestPlugin.dsm"
// ----------------------------------------------------------------------
//
// A VNC DSM Plugin MUST export (extern "C" (__cdecl)) all the following
// functions (same names, same signatures)
//
// For return values, the rule is:
// < 0, 0, and NULL pointer mean Error
// > 0 and pointer != NULL mean Ok
//
// ----------------------------------------------------------------------
//
// Returns the ID string of the Plugin
//
TESTPLUGIN_API char* Description(void)
{
return PLUGIN_DESCRIPTION;
}
//
// Initialize the plugin and all its internals
// Return -1 if error
//
TESTPLUGIN_API int Startup(void)
{
// Init everything
memset(szExternalKey, 0, sizeof(szExternalKey));
// Create threads if any
return 1;
}
//
// Stop and Clean up the plugin
// Return -1 if error
//
TESTPLUGIN_API int Shutdown(void)
{
// Terminate Threads if any
// Cleanup everything
return 1;
}
//
// Stop and Clean up the plugin
// Return -1 if error
//
TESTPLUGIN_API int Reset(void)
{
// Reset the plugin (buffers, keys, whatever that
// requires to be reset between 2 connections
return 1;
}
//
// Set the plugin params (Key or password )
// If several params are needed, they can be transmitted separated with ',' (comma)
// then translated if necessary. They also can be taken from the internal Plugin config
//
// WARNING: The plugin is responsible for implementing necessary GUI or File/Registry reading
// to acquire additionnal parameters and to ensure their persistence if necessary.
// Same thing for events/errors logging.
//
// This function can be called 2 times, both from vncviewer and WinVNC:
//
// 1.If the user clicks on the Plugin's "config" button in vncviewer and WinVNC dialog boxes
// In this case this function is called with hVNC != 0 (CASE 1)
//
// -> szParams is a string formatted as follow: "Part1,Part2"
// Part1 = "NoPassword"
// Part2 = type of application that has loaded the plugin
// "viewer" : for vncviewer
// "server-svc" : for WinVNC run as a service
// "server-app" : for WINVNC run as an application
//
// -> The Plugin Config dialog box is displayed if any.
//
// 2.When then plugin is Inited from VNC viewer or Server, right after Startup() call (CASE 2);
// In this case, this function is called with hVNC = 0 and
// szParams is a string formatted as follows: "part1,Part2"
// Part1 = The VNC password, if required by the GetParams() function return value
// Part2 = type of application that has loaded the plugin
// "viewer" : for vncviewer
// "server-svc" : for WinVNC run as a service
// "server-app" : for WINVNC run as an application
// (this info can be used for application/environnement dependent
// operations (config saving...))
//
TESTPLUGIN_API int SetParams(HWND hVNC, char* szParams)
{
// CASE 1
// Get the environnement (szLoaderType) value that is always sent from
// VNC viewer or server
MyStrToken(szLoaderType, szParams, 2, ',');
// If hVNC != 0, display for instance the Plugin Config Dialog box
if (hVNC)
{
// Display the Plugin Config dialog box
DoDialog();
}
// CASE 2:
// Use szParams to setup the Plugin.
// In this example Plugin, the externalkey is not used but we store it anyway.for demo.
// (it corresponds to the VNC password as we require it in the GetParams() function below)
MyStrToken(szExternalKey, szParams, 1, ',');
return 1;
}
//
// Return the current plugin params
// As the plugin is basically a blackbox, VNC doesn't need to know
// the Plugin parameters.
// But we use this method to know if the plugin needs the VNC password
// as a parameter to do its job correctly (for login step).
// Thus this function is called once before the SetParams() function is called
// - Return "VNCPasswordNeeded" if VNC password must be transmitted by the UltraVNC app
// - Return any other Plugin parameters value otherwise (not used in WinVNC & vncviewer for now)
TESTPLUGIN_API char* GetParams(void)
{
if (strlen(szExternalKey) > 0)
return szExternalKey; // Return the already stored externalkey params
else
return "VNCPasswordNeeded";
// return "IveGotAllINeedThanks";
}
//
// TransformBuffer function
//
// Transform the data given in pDataBuffer then return the pointer on the allocated
// buffer containing the resulting data.
// The length of the resulting data is given by pnTransformedDataLen
//
TESTPLUGIN_API BYTE* TransformBuffer(BYTE* pDataBuffer, int nDataLen, int* pnTransformedDataLen)
{
BYTE* pTransBuffer = CheckLocalTransBufferSize(GiveTransDataLen(nDataLen));
if (pTransBuffer == NULL)
{
*pnTransformedDataLen = -1;
return NULL;
}
// Do the actual data padding/transformation/utilization or whatever here
// In this Sample plugin we do nothing.
// We just copy the incoming data into the destination buffer, without modifiying it.
int i;
for (i = 0; i < nDataLen; i++)
{
pTransBuffer[i] = pDataBuffer[i];
}
// return the transformed data length
*pnTransformedDataLen = GiveTransDataLen(nDataLen);
return pTransBuffer; // Actually, pTransBuffer = pLocalTransBuffer
}
//
// RestoreBuffer function
//
// This function has a 2 mandatory behaviors:
//
// 1. If pRestoredDataBuffer is NULL, the function must return the pointer to current
// LocalRestBuffer that is going to receive the Transformed data to restore
// from VNC viewer/server's socket.
// This buffer must be of the size of transformed data, calculated from nDataLen
// and this size must be given back in pnRestoredDataLen.
//
// 2. If pRestoredDataBuffer != NULL, it is the destination buffer that is going to receive
// the restored data. So the function must restore the data that is currently in the
// local pLocalRestBuffer (nDataLen long) and put the result in pRestoredDataBuffer.
// The length of the resulting data is given back in pnTransformedDataLen
//
// Explanation: When VNC viewer/server wants to restore some data, it does the following:
// - Calls RestoreBuffer with NULL to get the buffer (and its length) to store incoming transformed data
// - Reads incoming transformed data from socket directly into the buffer given (and of given length)
// - Calls RestoreBuffer again to actually restore data into the given destination buffer.
// This way the copies of data buffers are reduced to the minimum.
//
TESTPLUGIN_API BYTE* RestoreBuffer(BYTE* pRestoredDataBuffer, int nDataLen, int* pnRestoredDataLen)
{
// If given buffer is NULL, allocate necessary space here and return the pointer.
// Additionaly, calculate the resulting length based on nDataLen and return it at the same time.
if (pRestoredDataBuffer == NULL)
{
// Give the size of the transformed data buffer, based on the original data length
*pnRestoredDataLen = GiveTransDataLen(nDataLen);
// Ensure the pLocalRestBuffer that receive transformed data is big enought
BYTE* pBuffer = CheckLocalRestBufferSize(*pnRestoredDataLen);
return pBuffer; // Actually pBuffer = pLocalRestBuffer
}
// If we reach this point, pLocalTransBuffer must contain the transformed data to restore
// Do the actual data padding/transformation/utilization or whatever here
// In this Sample plugin we do nothing.
// We just copy data into the destination buffer, without modifiying it.
// WARNING: don't use the *pnRestoredDataLen value here, as its value can be
// modified between the 2 calls to this function.
// Instead use the GiveTransDataLen() and GiveRestDataLen() functions.
int i;
for (i = 0; i < nDataLen; i++)
{
pRestoredDataBuffer[i] = pLocalRestBuffer[i];
}
// return the resulting data length
*pnRestoredDataLen = GiveRestDataLen(nDataLen);
return pLocalRestBuffer;
}
//
// Free the DataBuffer and TransBuffer than have been allocated
// in TransformBuffer and RestoreBuffer, using the method adapted
// to the used allocation method.
//
TESTPLUGIN_API void FreeBuffer(BYTE* pBuffer)
{
if (pBuffer != NULL)
free(pBuffer);
return;
}
// -----------------------------------------------------------------
// End of functions that must be exported
// -----------------------------------------------------------------
// -----------------------------------------------------------------
// Plugin internal Config Dialog Box Sample
// -----------------------------------------------------------------
// Move the given window to the centre of the screen
// and bring it to the top.
void CentreWindow(HWND hwnd)
{
RECT winrect, workrect;
// Find how large the desktop work area is
SystemParametersInfo(SPI_GETWORKAREA, 0, &workrect, 0);
int workwidth = workrect.right - workrect.left;
int workheight = workrect.bottom - workrect.top;
// And how big the window is
GetWindowRect(hwnd, &winrect);
int winwidth = winrect.right - winrect.left;
int winheight = winrect.bottom - winrect.top;
// Make sure it's not bigger than the work area
winwidth = min(winwidth, workwidth);
winheight = min(winheight, workheight);
// Now centre it
SetWindowPos(hwnd,
HWND_TOP,
workrect.left + (workwidth-winwidth) / 2,
workrect.top + (workheight-winheight) / 2,
winwidth, winheight,
SWP_SHOWWINDOW);
SetForegroundWindow(hwnd);
}
//
// Display the Plugin Config Dialog box
//
int DoDialog(void)
{
return DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_CONFIG_DIALOG),
NULL, (DLGPROC) ConfigDlgProc, (LONG) pPlugin);
}
//
// Config Dialog box callback
//
BOOL CALLBACK ConfigDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
PLUGINSTRUCT* _this = (PLUGINSTRUCT*) GetWindowLong(hwnd, GWL_USERDATA);
switch (uMsg)
{
case WM_INITDIALOG:
{
// Init the various fields with the saved values if they exist.
CentreWindow(hwnd);
return TRUE;
}
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
// Save the parameters in an ini file or registry for instance/
// szLoaderType (environnement value) param can be used if necessary.
return TRUE;
case IDCANCEL:
EndDialog(hwnd, FALSE);
return TRUE;
}
break;
case WM_DESTROY:
EndDialog(hwnd, FALSE);
return TRUE;
}
return 0;
}
// -----------------------------------------------------------------
// Others internal functions, some depending on what the Plugin does
// -----------------------------------------------------------------
//
//
//
BOOL MyStrToken(LPSTR szToken, LPSTR lpString, int nTokenNum, char cSep)
{
int i = 1;
while (i < nTokenNum)
{
while ( *lpString && (*lpString != cSep) &&(*lpString != '\0'))
{
lpString++;
}
i++;
lpString++;
}
while ((*lpString != cSep) && (*lpString != '\0'))
{
*szToken = *lpString;
szToken++;
lpString++;
}
*szToken = '\0' ;
if (( ! *lpString ) || (! *szToken)) return NULL;
return FALSE;
}
//
// Calculate the len of the data after Transformation and return it.
//
// MANDATORY: The calculation must be possible by
// ONLY knowing the source data length ! (=> forget compression algos...)
//
int GiveTransDataLen(int nDataLen)
{
int nTransDataLen = nDataLen; // In this example, the datalen remains unchanged
return nTransDataLen;
}
//
// Calculate the len of the data after Restauration and return it.
//
// MANDATORY: The calculation must be possible by
// ONLY knowing the source data length ! (=> forget compression algos...)
//
int GiveRestDataLen(int nDataLen)
{
int nRestDataLen = nDataLen; // In this example, the datalen remains unchanged
return nRestDataLen;
}
//
// Allocate more space for the local transformation buffer if necessary
// and returns the pointer to this buffer
//
BYTE* CheckLocalTransBufferSize(int nBufferSize)
{
if (nLocalTransBufferSize >= nBufferSize) return pLocalTransBuffer;
BYTE *pNewBuffer = (BYTE *) malloc (nBufferSize + 256);
if (pNewBuffer == NULL)
{
return NULL;
}
if (pLocalTransBuffer != NULL)
free(pLocalTransBuffer);
pLocalTransBuffer = pNewBuffer;
nLocalTransBufferSize = nBufferSize + 256;
memset(pLocalTransBuffer, 0, nLocalTransBufferSize);
return pLocalTransBuffer;
}
//
// Allocate more space for the local restoration buffer if necessary
// and returns the pointer to this buffer
//
BYTE* CheckLocalRestBufferSize(int nBufferSize)
{
if (nLocalRestBufferSize >= nBufferSize) return pLocalRestBuffer;
BYTE *pNewBuffer = (BYTE *) malloc (nBufferSize + 256);
if (pNewBuffer == NULL)
{
return NULL;
}
if (pLocalRestBuffer != NULL)
free(pLocalRestBuffer);
pLocalRestBuffer = pNewBuffer;
nLocalRestBufferSize = nBufferSize + 256;
memset(pLocalRestBuffer, 0, nLocalRestBufferSize);
return pLocalRestBuffer;
}
//
// DLL Main Entry point
//
BOOL WINAPI DllMain( HINSTANCE hInst,
DWORD dwReason,
LPVOID lpReserved
)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
hInstance = hInst;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}