-
Notifications
You must be signed in to change notification settings - Fork 22
/
cLSPInstall.cpp
3559 lines (3062 loc) · 109 KB
/
cLSPInstall.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
/*
*
* Copyright (C) 2013 Anwar Mohamed <anwarelmakrahy[at]gmail.com>
*
* 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 Anwar Mohamed
* anwarelmakrahy[at]gmail.com
*
*/
#include "Packetyzer.h"
using namespace Packetyzer::Capture;
GUID gProviderGuid;
cLSPInstall::cLSPInstall(CHAR* DLLPath)
{
fnWscUpdateProvider = NULL,
fnWscUpdateProvider32 = NULL;
gModule = NULL;
LSPError = LSP_ERROR_NONE;
ProtocolsInfo = NULL;
nProtocols = 0;
ReadyInstall = FALSE;
LSPGuid = new GUID;
this->DLLPath = DLLPath;
// Load Winsock
rc = WSAStartup( MAKEWORD(2,2), &wsd );
if ( 0 != rc )
{
fprintf( stderr, "Unable to load Winsock: %d\n", rc );
LSPError = LSP_ERROR_WINSOCK;
return;
}
// Initialize data structures
LspCreateHeap( &rc );
__try
{
InitializeCriticalSection( &gDebugCritSec );
}
__except( EXCEPTION_EXECUTE_HANDLER )
{
Cleanup();
}
ReadyInstall = TRUE;
}
void cLSPInstall::Cleanup()
{
if ( NULL != pdwCatalogIdArray )
LspFree( pdwCatalogIdArray );
if ( NULL != pProtocolInfo)
FreeProviders( pProtocolInfo );
if ( NULL != pLspMap )
FreeLspMap( pLspMap, iLspCount );
if ( NULL != gModule )
FreeLibrary( gModule );
LspDestroyHeap( );
DeleteCriticalSection( &gDebugCritSec );
WSACleanup();
}
cLSPInstall::~cLSPInstall()
{
Cleanup();
}
BOOL cLSPInstall::Install(UINT CatalogIDs[], CHAR* LSPName, BOOL IFSProvider, BOOL InstallOverAll, WINSOCK_CATALOG Catalog)
{
this->eCatalog = Catalog;
dwCatalogIdArrayCount = sizeof(CatalogIDs)/sizeof(UINT);
// Allocate space for the array of catalog IDs
if ( 0 < dwCatalogIdArrayCount )
{
pdwCatalogIdArray = (DWORD *) LspAlloc(
sizeof( DWORD ) * dwCatalogIdArrayCount,
&rc
);
if ( NULL == pdwCatalogIdArray )
{
return FALSE;
}
}
// Set back to zero so we can use it as the index into our array
this->bInstallOverAll = InstallOverAll;
lpszLspPathAndFile = DLLPath;
//bRemoveAllLayeredEntries = TRUE;
// bInstall = FALSE;
bInstall = TRUE;
bIFSProvider = IFSProvider;
lpszLspName = LSPName;
for (i=0; i<(INT)dwCatalogIdArrayCount; i++)
pdwCatalogIdArray[i] = CatalogIDs[i];
/*case 'r': // remove an LSP
bInstall = FALSE;
if (i+1 >= argc)
goto cleanup;
dwRemoveCatalogId = atol(argv[++i]);
break;*/
#ifndef _WIN64
if ( LspCatalog64Only == eCatalog )
{
fprintf(stderr, "\n\nUnable to manipulate 64-bit Winsock catalog from 32-bit process!\n\n");
return FALSE;
}
#endif
gModule = LoadUpdateProviderFunction();
if ( NULL == lpszLspPathAndFile )
{
fprintf( stderr, "\n\nError! Please specify path and filename of LSP!\n\n");
return FALSE;
}
if ( TRUE == bInstallOverAll )
{
// Make sure user didn't specify '-a' and '-o' flags
if ( 0 != dwCatalogIdArrayCount )
return FALSE;
// Enumerate the appropriate catalog we will be working on
pProtocolInfo = EnumerateProviders( eCatalog, &iTotalProtocols );
if ( NULL == pProtocolInfo )
{
fprintf( stderr, "EnumerateProviders: Unable to enumerate Winsock catalog\n" );
return FALSE;
}
// Count how many non layered protocol entries there are
for(i=0; i < iTotalProtocols ;i++)
{
if ( LAYERED_PROTOCOL != pProtocolInfo[ i ].ProtocolChain.ChainLen )
dwCatalogIdArrayCount++;
}
// Allocate space for all the entries
pdwCatalogIdArray = (DWORD *) LspAlloc(
sizeof( DWORD ) * dwCatalogIdArrayCount,
&rc
);
if ( NULL == pdwCatalogIdArray )
{
fprintf( stderr, " LspAlloc failed: %d\n", rc );
return FALSE;
}
// Get the catalog IDs for all existing providers
dwCatalogIdArrayCount = 0 ;
for(i=0; i < iTotalProtocols ;i++)
{
if ( LAYERED_PROTOCOL != pProtocolInfo[ i ].ProtocolChain.ChainLen )
{
pdwCatalogIdArray[ dwCatalogIdArrayCount++ ] = pProtocolInfo[ i ].dwCatalogEntryId;
}
}
//FreeProviders( pProtocolInfo );
//pProtocolInfo = NULL;
}
// Install the LSP with the supplied parameters
rc = InstallLsp(
eCatalog,
lpszLspName,
lpszLspPathAndFile,
dwCatalogIdArrayCount,
pdwCatalogIdArray,
bIFSProvider,
bInstallOverAll
);
return TRUE;
}
BOOL cLSPInstall::UninstallAll()
{
//if ( ( LspCatalogBoth == this->eCatalog ) || ( LspCatalog32Only == eCatalog ) )
RemoveAllLayeredEntries( LspCatalog32Only );
//if ( ( LspCatalogBoth == eCatalog ) || ( LspCatalog64Only == eCatalog ) )
RemoveAllLayeredEntries( LspCatalog64Only );
return TRUE;
}
BOOL cLSPInstall::UninstallMe()
{
DWORD MyCatalogID = GetCatalogIdForProviderGuid(&gProviderGuid, pProtocolInfo, iTotalProtocols);
if ( ( LspCatalogBoth == eCatalog ) || ( LspCatalog32Only == eCatalog ) )
RemoveProvider( LspCatalog32Only, MyCatalogID );
if ( ( LspCatalogBoth == eCatalog ) || ( LspCatalog64Only == eCatalog ) )
RemoveProvider( LspCatalog64Only, MyCatalogID );
return TRUE;
}
BOOL cLSPInstall::UninstallOne(DWORD dwRemoveCatalogId)
{
if ( ( LspCatalogBoth == eCatalog ) || ( LspCatalog32Only == eCatalog ) )
RemoveProvider( LspCatalog32Only, dwRemoveCatalogId );
if ( ( LspCatalogBoth == eCatalog ) || ( LspCatalog64Only == eCatalog ) )
RemoveProvider( LspCatalog64Only, dwRemoveCatalogId );
return TRUE;
}
/*
lspAdd
*/
INT cLSPInstall::InstallLsp(
WINSOCK_CATALOG eCatalog, // Which catalog to install LSP into
__in_z char *lpszLspName, // String name of LSP
__in_z char *lpszLspPathAndFile, // Location of LSP dll and dll name
DWORD dwCatalogIdArrayCount, // Number of entries in pdwCatalogIdArray
DWORD *pdwCatalogIdArray, // Array of IDs to install over
BOOL IfsProvider,
BOOL InstallOverAll
)
{
OSVERSIONINFOEX osv = {0};
WSAPROTOCOL_INFOW //*pProtocolInfo = NULL,
*pDummyEntry = NULL,
*pLayeredEntries = NULL;
WCHAR wszLspName[ WSAPROTOCOL_LEN ],
wszFullProviderPath[ MAX_PATH+1 ];
GUID ProviderBaseGuid;
INT rc = SOCKET_ERROR;
if ( NULL == lpszLspName ) lpszLspName = LSP_DEFAULT_NAME;
// Convert the LSP name to UNICODE since the Winsock catalog is all UNICODE
rc = MultiByteToWideChar(CP_ACP, 0, lpszLspName, (int) strlen( lpszLspName ) + 1,wszLspName, WSAPROTOCOL_LEN );
if (rc == 0)
{
fprintf(stderr, "InstallLsp: MultiByteToWideChar failed to convert '%s'; Error = %d\n",
lpszLspName, GetLastError());
goto cleanup;
}
rc = MultiByteToWideChar(
CP_ACP,
0,
lpszLspPathAndFile,
(int) strlen( lpszLspPathAndFile ) + 1,
wszFullProviderPath,
MAX_PATH
);
if ( 0 == rc )
{
fprintf( stderr, "InstallLsp: MultiByteToWidechar failed to convert '%s': Error = %d\n",
lpszLspPathAndFile, GetLastError() );
goto cleanup;
}
// Verify there's at least one entry to layer over
if ( 0 == dwCatalogIdArrayCount )
{
fprintf(stderr, "InstallLsp: Error! Must specify at least one provider to layer over!\n\n");
goto cleanup;
}
printf("LSP name is '%S'\n", wszLspName);
// Retrieve the GUID under which the LSP is to be installed
RetrieveLspGuid( lpszLspPathAndFile, &ProviderBaseGuid );
osv.dwOSVersionInfoSize = sizeof(osv);
GetVersionEx( (LPOSVERSIONINFO) &osv );
if ( osv.dwMajorVersion >= 6 )
{
// On Windows Vista, use the new LSP install API
rc = InstallProviderVista(
eCatalog,
wszLspName,
wszFullProviderPath,
&ProviderBaseGuid,
dwCatalogIdArrayCount,
pdwCatalogIdArray,
IfsProvider,
InstallOverAll
);
if ( SOCKET_ERROR == rc )
{
goto cleanup;
}
}
else
{
//
// This isn't Vista so install the LSP the old way
//
// Create the 'dummy' protocol entry
pDummyEntry = CreateDummyEntry( eCatalog, pdwCatalogIdArray[ 0 ], wszLspName, IfsProvider );
if (pDummyEntry == NULL)
{
fprintf(stderr, "InstallLsp: CreateDummyEntry failed!\n");
goto cleanup;
}
// Install the 'dummy' protocol entry for the LSP
rc = InstallProvider(
eCatalog,
&ProviderBaseGuid,
wszFullProviderPath,
pDummyEntry,
1
);
if ( NO_ERROR != rc )
{
fprintf(stderr, "InstallLsp: Unable to install the dummy LSP entry!\n");
goto cleanup;
}
// Don't need this struture any more
LspFree( pDummyEntry );
pDummyEntry = NULL;
if ( FALSE == IfsProvider )
{
rc = InstallNonIfsLspProtocolChains( eCatalog, &ProviderBaseGuid, wszLspName,
wszFullProviderPath, pdwCatalogIdArray, dwCatalogIdArrayCount );
}
else
{
rc = InstallIfsLspProtocolChains( eCatalog, &ProviderBaseGuid, wszLspName,
wszFullProviderPath, pdwCatalogIdArray, dwCatalogIdArrayCount );
}
if ( SOCKET_ERROR == rc )
{
// An error occured installing the chains so remove the dummy entry
DeinstallProvider( eCatalog, &ProviderBaseGuid );
}
}
cleanup:
//if ( NULL != pProtocolInfo )
// FreeProviders( pProtocolInfo );
if ( NULL != pDummyEntry )
LspFree( pDummyEntry );
if ( NULL != pLayeredEntries )
LspFree( pLayeredEntries );
return rc;
}
int
cLSPInstall::InstallProvider(
WINSOCK_CATALOG Catalog, // Which catalog are we operating on
GUID *Guid, // GUID under which provider will be installed
WCHAR *lpwszLspPath, // Path to LSP's DLL
WSAPROTOCOL_INFOW *pProvider, // Array of provider structures to install
INT iProviderCount // Number of providers in array
)
{
WSAPROTOCOL_INFOW *pEnumProviders = NULL,
*pEntry = NULL;
INT iEnumProviderCount,
ErrorCode,
rc = SOCKET_ERROR;
#ifdef _WIN64
if ( LspCatalog32Only == Catalog )
{
// Can't install only in 32-bit catalog from 64-bit
fprintf( stderr, "InstallProvider: Error! It is not possible to install only "
"in 32-bit catalog from 64-bit process!\n\n"
);
goto cleanup;
}
else if ( LspCatalog64Only == Catalog )
{
// Just need to call WSCInstallProvider
rc = WSCInstallProvider(
Guid,
lpwszLspPath,
pProvider,
iProviderCount,
&ErrorCode
);
}
else
{
// To install in both we must call WSCInstallProviderPath64_32
rc = WSCInstallProvider64_32(
Guid,
lpwszLspPath,
pProvider,
iProviderCount,
&ErrorCode
);
}
#else
if ( LspCatalog32Only == Catalog )
{
// From a 32-bit process we can only install into 32-bit catalog
rc = WSCInstallProvider(
Guid,
lpwszLspPath,
pProvider,
iProviderCount,
&ErrorCode
);
}
else
{
// From a 32-bit process, we can't touch the 64-bit catalog at all
fprintf( stderr, "InstallProvider: Error! It is not possible to install into "
"the 64-bit catalog from a 32-bit process!\n\n"
);
goto cleanup;
}
#endif
if ( SOCKET_ERROR == rc )
{
fprintf( stderr, "InstallProvider: WSCInstallProvider* failed: %d\n", ErrorCode );
goto cleanup;
}
// Go back and enumerate what we just installed
pEnumProviders = EnumerateProviders( Catalog, &iEnumProviderCount );
if ( NULL == pEnumProviders )
{
fprintf( stderr, "InstallProvider: EnumerateProviders failed!\n" );
goto cleanup;
}
// Make sure our entry is in the catalog
pEntry = FindProviderByGuid( Guid, pEnumProviders, iEnumProviderCount );
if ( pEntry )
{
printf( "Installed: [%4d] %S\n",
pEntry->dwCatalogEntryId,
pEntry->szProtocol
);
}
cleanup:
if ( NULL != pEnumProviders )
FreeProviders( pEnumProviders );
return rc;
}
WSAPROTOCOL_INFOW *
cLSPInstall::CreateDummyEntry(
WINSOCK_CATALOG Catalog,
INT CatalogId,
WCHAR *lpwszLspName,
BOOL IfsProvider
)
{
WSAPROTOCOL_INFOW *pProtocolInfo = NULL,
*pDummyEntry = NULL,
*pEntry = NULL;
INT iProtocolCount = 0;
int err;
// Enumerate the catalog
pProtocolInfo = EnumerateProviders( Catalog, &iProtocolCount );
if ( NULL == pProtocolInfo )
{
fprintf(stderr, "CreateDummyEntry: EnumerateProviders failed!\n");
goto cleanup;
}
// Find one of the providers we are layering over
pEntry = FindProviderById( CatalogId, pProtocolInfo, iProtocolCount );
if ( pEntry )
{
// Allocate space and copy the provider structure
pDummyEntry = (WSAPROTOCOL_INFOW *) LspAlloc(
sizeof( WSAPROTOCOL_INFOW ),
&err
);
if ( NULL == pDummyEntry )
{
fprintf( stderr, "CreateDummyEntry: LspAlloc failed: %d\n", err );
goto cleanup;
}
// Copy the entry as a basis for the dummy entry
memcpy( pDummyEntry, pEntry, sizeof( WSAPROTOCOL_INFOW ) );
}
else
{
fprintf(stderr, "CreateDummyEntry: Error! Unable to find provider with ID of %d\n\n",
CatalogId
);
goto cleanup;
}
// Remove the IFS provider flag if the LSP doesn't support it
if ( FALSE == IfsProvider )
pDummyEntry->dwServiceFlags1 &= (~XP1_IFS_HANDLES);
// Set the flags indicating this is a hidden ("dummy") entry
pDummyEntry->iSocketType = 0;
pDummyEntry->iProtocol = 0;
pDummyEntry->dwProviderFlags |= PFL_HIDDEN;
pDummyEntry->dwProviderFlags &= (~PFL_MATCHES_PROTOCOL_ZERO);
pDummyEntry->ProtocolChain.ChainLen = LAYERED_PROTOCOL;
// Copy the LSP name
//wcsncpy( pDummyEntry->szProtocol, lpwszLspName, WSAPROTOCOL_LEN );
wcsncpy_s( pDummyEntry->szProtocol, lpwszLspName, WSAPROTOCOL_LEN );
cleanup:
if ( NULL != pProtocolInfo )
FreeProviders( pProtocolInfo );
return pDummyEntry;
}
int
cLSPInstall::InstallIfsLspProtocolChains(
WINSOCK_CATALOG eCatalog,
GUID *Guid,
WCHAR *lpszLspName,
WCHAR *lpszLspFullPathAndFile,
DWORD *pdwCatalogIdArray,
DWORD dwCatalogIdArrayCount
)
{
WSAPROTOCOL_INFOW *pProvider = NULL,
*pProviderNew = NULL,
*pLayeredEntries = NULL,
*pEntry = NULL,
TempEntry = {0};
DWORD *pProviderOrder = NULL,
dwDummyLspId;
WCHAR wszLspDll[ MAX_PATH ];
BOOL bLayeredOverNonIfs = FALSE,
bContainsNonIfs = FALSE;
HRESULT hr;
int ProviderPathLen = MAX_PATH-1,
iProviderCount,
iProviderCountNew,
LayerIdx,
retval = SOCKET_ERROR,
err,
idx,
rc,
i, j, k;
// Enumerate the catalog
pProvider = EnumerateProviders( eCatalog, &iProviderCount );
if ( NULL == pProvider )
{
fprintf( stderr, "InstallIfsLspProtocolChains: Unable to enumerate catalog\n" );
goto cleanup;
}
// Find the dummy, hidden entry of our new LSP
dwDummyLspId = GetCatalogIdForProviderGuid( Guid, pProvider, iProviderCount );
ASSERT( dwDummyLspId != 0 );
// Allocate space for the protocol chains of the new LSP
pLayeredEntries = (WSAPROTOCOL_INFOW *) LspAlloc( sizeof(WSAPROTOCOL_INFOW) *
dwCatalogIdArrayCount, &err );
if ( NULL == pLayeredEntries )
{
fprintf( stderr, "InstallIfsLspProtocolChains: LspAlloc failed: %d\n", err );
goto cleanup;
}
LayerIdx = 0;
// Build the layered protocol entries as well as a list of those providers which
// require modification. Whenever an LSP is installed, a number of protocol entries
// are installed where the first entry in the chain array is the LSP's dummy entry.
// Addtionally, if we're installing an IFS LSP over an provider whose protocol chain
// includes non-IFS LSPs, the IFS LSP must be placed in the chain such that no
// non-IFS LSPs are positioned after it in the chain.
// Loop through each ID we're layering over
for(i=0; i < (int)dwCatalogIdArrayCount ;i++)
{
for(j=0; j < iProviderCount ;j++)
{
printf("Matching selected ID %d to catalog %d\n",
pdwCatalogIdArray[ i ], pProvider[ j ].dwCatalogEntryId );
if ( pdwCatalogIdArray[ i ] == pProvider[ j ].dwCatalogEntryId )
{
// Verify the entry has room enough to be layered over
if ( pProvider[ j ].ProtocolChain.ChainLen >= ( MAX_PROTOCOL_CHAIN - 1 ) )
{
fprintf( stderr, "InstallIfsLspProtocolChain: Too many LSPs installed!\n");
goto cleanup;
}
// Save off the entry which we're layering over
memcpy( &pLayeredEntries[ LayerIdx ], &pProvider[ j ],
sizeof( pLayeredEntries[ 0 ] ) );
memcpy( &TempEntry, &pProvider[ j ], sizeof( TempEntry ) );
// Fill in the new LSP entry's name
hr = StringCchPrintfW( pLayeredEntries[ LayerIdx ].szProtocol, WSAPROTOCOL_LEN,
L"%s over [%s]",
lpszLspName,
pProvider[ j ].szProtocol
);
if ( FAILED( hr ) )
{
fprintf( stderr, "InstallIfsLspProtocolChains: StringCchPrintfW failed: 0x%x\n", hr );
goto cleanup;
}
// Check whether the selected entry contains non IFS LSPs in its chain
if ( pProvider[ j ].ProtocolChain.ChainLen >= 2 )
{
for(k=pProvider[ j ].ProtocolChain.ChainLen-2 ; k >= 0 ;k--)
{
bContainsNonIfs = IsNonIfsProvider( pProvider, iProviderCount,
pProvider[ j ].ProtocolChain.ChainEntries[ k ] );
if ( TRUE == bContainsNonIfs )
{
// Need to modify the pProvider entry to reference the
// added LSP entry within its chain
// In the 'modified' array make a space at location after 'k'
InsertIdIntoProtocolChain( &pProvider[ j ], k+1, UPDATE_LSP_ENTRY );
// Save the index to the layer which corresponds to this entry
pProvider[ j ].dwProviderReserved = LayerIdx + 1;
// Need to fix the 'pLayeredEntry' as well
BuildSubsetLspChain( &pLayeredEntries[ LayerIdx ], k+1, dwDummyLspId );
pLayeredEntries[ LayerIdx ].dwServiceFlags1 |= XP1_IFS_HANDLES;
bLayeredOverNonIfs = TRUE;
// Need to insert the IFS provider in all LSPs that are layered
// above the location where the IFS provider was just inserted
InsertIfsLspIntoAllChains( &TempEntry, pProvider, iProviderCount,
LayerIdx + 1, k );
break;
}
}
}
// Need to setup the protocol chain in the pLayeredEntry if we haven't
// already done so above
if ( TRUE != bContainsNonIfs )
{
InsertIdIntoProtocolChain( &pLayeredEntries[ LayerIdx ], 0, dwDummyLspId );
// The second entry is always the ID of the current pProvider[i]
// In case of multiple LSPs then if we didn't do this the [1] index
// would contain the ID of that LSP's dummy entry and not the entry
// itself.
pLayeredEntries[ LayerIdx ].ProtocolChain.ChainEntries[ 1 ] =
TempEntry.dwCatalogEntryId;
pLayeredEntries[ LayerIdx ].dwServiceFlags1 |= XP1_IFS_HANDLES;
}
LayerIdx++;
}
}
}
ASSERT( LayerIdx == (int)dwCatalogIdArrayCount );
// Create a unique GUID for each provider to install and install it
for(i=0;i < (int)dwCatalogIdArrayCount ;i++)
{
if ( RPC_S_OK != UuidCreate( &pLayeredEntries[ i ].ProviderId ) )
{
fprintf(stderr, "InstallIfsLspProtocolChains: UuidCreate failed: %d\n", GetLastError());
goto cleanup;
}
rc = InstallProvider( eCatalog, &pLayeredEntries[ i ].ProviderId,
lpszLspFullPathAndFile, &pLayeredEntries[ i ], 1 );
if ( NO_ERROR != rc )
{
fprintf(stderr, "InstallIfsLspProtocolChains: Unable to install the dummy LSP entry!\n");
goto cleanup;
}
}
if ( TRUE == bLayeredOverNonIfs )
{
// Enumerate the catalog again so we can find the catalog IDs
pProviderNew = EnumerateProviders( eCatalog, &iProviderCountNew );
if ( NULL == pProviderNew )
{
fprintf( stderr, "InstallIfsLspProtocolChains: Unable to enumerate catalog\n" );
goto cleanup;
}
for(i=0; i < (int)dwCatalogIdArrayCount ;i++)
{
pLayeredEntries[ i ].dwCatalogEntryId = GetCatalogIdForProviderGuid(
&pLayeredEntries[ i ].ProviderId,
pProviderNew,
iProviderCountNew
);
ASSERT( pLayeredEntries[ i ].dwCatalogEntryId != 0 );
}
// Update the protocol chains of the modified entries to point to the just
// installed providers
for(i=0; i < iProviderCount ;i++)
{
if ( pProvider[ i ].dwProviderReserved == 0 )
continue;
for(j=0; j < pProvider[ i ].ProtocolChain.ChainLen ;j++)
{
if ( UPDATE_LSP_ENTRY == pProvider[ i ].ProtocolChain.ChainEntries[ j ] )
{
pProvider[ i ].ProtocolChain.ChainEntries[ j ] =
pLayeredEntries[ pProvider[ i ].dwProviderReserved - 1 ].dwCatalogEntryId;
pProvider[ i ].dwProviderReserved = 0;
}
}
// Get the DLL path
ProviderPathLen = MAX_PATH-1;
rc = WSCGetProviderPath(
&pProvider[ i ].ProviderId,
wszLspDll,
&ProviderPathLen,
&err
);
if ( SOCKET_ERROR == rc )
{
fprintf( stderr, "InstallIfsLspProtocolChains: WSCGetProviderPath failed: %d\n", err );
goto cleanup;
}
// Update the providers which were modified
rc = UpdateProvider( eCatalog, &pProvider[ i ].ProviderId,
wszLspDll, &pProvider[ i ], 1, &err );
if ( SOCKET_ERROR == rc )
{
fprintf( stderr, "InstallIfsLspProtocolChains: UpdateProvider failed: %d\n", err );
goto cleanup;
}
printf("Updated entry ID: %d: %S (chain len = %d)\n",
pProvider[ i ].dwCatalogEntryId,
pProvider[ i ].szProtocol,
pProvider[ i ].ProtocolChain.ChainLen
);
}
FreeProviders( pProvider );
pProvider = NULL;
FreeProviders( pProviderNew );
pProviderNew = NULL;
//WSCUpdateProvider doesn't update the process' copy of the winsock catalog.
//By calling cleanup and startup again, it forces a refresh. Otherwise,
//the rest of the installer code can't see the changes that were just made.
{
WSADATA wsd;
WSACleanup();
WSAStartup( MAKEWORD(2,2), &wsd );
}
pProvider = EnumerateProviders( eCatalog, &iProviderCount );
if ( NULL == pProvider )
{
fprintf( stderr, "InstallIfsLspProtocolChains: Unable to enumerate catalog\n" );
goto cleanup;
}
// Allocate an array of DWORDs to contain the new catalog ordering
pProviderOrder = (DWORD *)LspAlloc( iProviderCount * sizeof(DWORD), &err );
if ( NULL == pProviderOrder )
{
fprintf( stderr, "InstallIfsLspProtocolChains: Unable to enumerate catalog\n" );
goto cleanup;
}
// First add the entries we layered over first
idx = 0;
for(i=0; i < (int)dwCatalogIdArrayCount ;i++)
{
pEntry = FindProviderById( pdwCatalogIdArray[ i ], pProvider, iProviderCount );
if ( NULL == pEntry )
{
fprintf(stderr, "InstallIfsLspProtocolChain: Unable to find entry to reorder catalog!\n");
goto cleanup;
}
pEntry->dwProviderReserved = 1;
pProviderOrder[ idx++ ] = pEntry->dwCatalogEntryId;
}
// Now go through the protocol chain of the entries we layered over and put those
// LSP entries next in the new order
for(i=0; i < (int)dwCatalogIdArrayCount ;i++)
{
pEntry = FindProviderById( pdwCatalogIdArray[ i ], pProvider, iProviderCount );
if ( NULL == pEntry )
{
fprintf(stderr, "InstallIfsLspProtocolChain: Unable to find entry to reorder catalog!\n");
goto cleanup;
}
printf("Looping through: %d: %S (chain len = %d)\n",
pEntry->dwCatalogEntryId,
pEntry->szProtocol,
pEntry->ProtocolChain.ChainLen );
for(j=1; j < pEntry->ProtocolChain.ChainLen-1 ;j++)
{
dwDummyLspId = FindDummyIdFromProtocolChainId(
pEntry->ProtocolChain.ChainEntries[ j ],
pProvider,
iProviderCount
);
printf(" Finding dummy ID for chain entry: %d is %d\n",
pEntry->ProtocolChain.ChainEntries[ j ],
dwDummyLspId
);
for(k=0; k < iProviderCount ;k++)
{
if ( ( pProvider[ k ].ProtocolChain.ChainLen >= 2 ) &&
( pProvider[ k ].ProtocolChain.ChainEntries[ 0 ] == dwDummyLspId ) &&
( pProvider[ k ].dwProviderReserved == 0 )
)
{
pProviderOrder[ idx++ ] = pProvider[ k ].dwCatalogEntryId;
pProvider[ k ].dwProviderReserved = 1;
printf(" Adding: %d\n", pProvider[ k ].dwCatalogEntryId );
}
}
}
}
// Now any catalog entry that wasn't already copied, copy it
for(i=0; i < iProviderCount ;i++)
{
if ( pProvider[ i ].dwProviderReserved == 0 )
pProviderOrder[ idx++ ] = pProvider[ i ].dwCatalogEntryId;
}
ASSERT( idx == iProviderCount );
// Write the new catalog order
rc = WriteProviderOrder( eCatalog, pProviderOrder, iProviderCount, &err );
if ( NO_ERROR != rc )
{
fprintf( stderr, "InstallIfsLspProtocolChains: WriteProviderOrder failed: %d\n",
err );
goto cleanup;
}
}
else
{
//
// Reorder the winsock catalog so the layered chain entries appear first.
// Since we didn't have to modify any existing entries, all we need to do is
// move the added entries to the head of the catalog
//
rc = ReorderCatalog( eCatalog, dwDummyLspId );
if ( NO_ERROR != rc )
{
fprintf(stderr, "InstallIfsLspProtocolChains: Unable to reorder Winsock catalog!\n");
goto cleanup;
}
}
retval = NO_ERROR;
cleanup:
if ( NULL != pProvider )
{
FreeProviders( pProvider );
pProvider = NULL;
}
if ( NULL != pProviderNew )
{
FreeProviders( pProviderNew );
pProviderNew = NULL;
}
if ( NULL != pProviderOrder )
{
LspFree( pProviderOrder );
pProviderOrder = NULL;
}
return retval;
}
int
cLSPInstall::InstallNonIfsLspProtocolChains(
WINSOCK_CATALOG eCatalog,
GUID *Guid,
WCHAR *lpszLspName,
WCHAR *lpszLspFullPathAndFile,
DWORD *pdwCatalogIdArray,
DWORD dwCatalogIdArrayCount
)
{
WSAPROTOCOL_INFOW *pProvider = NULL,
*pLayeredEntries = NULL;
DWORD dwDummyLspId = 0;
INT iProviderCount = 0,
retval = SOCKET_ERROR,
idx,
err,
rc,
i, j;
HRESULT hr;
// Enumerate the catalog
pProvider = EnumerateProviders( eCatalog, &iProviderCount );
if ( NULL == pProvider )
{
fprintf( stderr, "InstallNonIfsLspProtocolChain: Unable to enumerate catalog\n" );
goto cleanup;
}
pLayeredEntries = (WSAPROTOCOL_INFOW *) LspAlloc( sizeof(WSAPROTOCOL_INFOW) *
dwCatalogIdArrayCount, &err );
if ( NULL == pLayeredEntries )
{
fprintf( stderr, "InstallNonIfsLspProtocolChain: LspAlloc failed: %d\n", err );
goto cleanup;
}
// Find the dummy entry so we can extract its catalog ID
dwDummyLspId = GetCatalogIdForProviderGuid( Guid, pProvider, iProviderCount );
ASSERT( dwDummyLspId != 0 );
// Go through the catalog and build the layered entries