1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
21 #include "CommonServices.h"
22 #include "DebugServices.h"
27 //===========================================================================================================================
29 //===========================================================================================================================
31 int main( int argc
, char *argv
[] );
32 DEBUG_LOCAL
void Usage( void );
33 DEBUG_LOCAL
int ProcessArgs( int argc
, char *argv
[] );
34 DEBUG_LOCAL OSStatus
InstallNSP( const char *inName
, const char *inGUID
, const char *inPath
);
35 DEBUG_LOCAL OSStatus
RemoveNSP( const char *inGUID
);
36 DEBUG_LOCAL OSStatus
EnableNSP( const char *inGUID
, BOOL inEnable
);
37 DEBUG_LOCAL OSStatus
ListNameSpaces( void );
38 DEBUG_LOCAL OSStatus
ReorderNameSpaces( void );
40 DEBUG_LOCAL WCHAR
* CharToWCharString( const char *inCharString
, WCHAR
*outWCharString
);
41 DEBUG_LOCAL
char * GUIDtoString( const GUID
*inGUID
, size_t inLen
, char *outString
);
42 DEBUG_LOCAL OSStatus
StringToGUID( const char *inCharString
, GUID
*outGUID
);
44 DEBUG_LOCAL BOOL gToolQuietMode
= FALSE
;
46 //===========================================================================================================================
48 //===========================================================================================================================
50 int main( int argc
, char *argv
[] )
54 debug_initialize( kDebugOutputTypeMetaConsole
);
55 debug_set_property( kDebugPropertyTagPrintLevel
, kDebugLevelVerbose
);
57 err
= ProcessArgs( argc
, argv
);
61 //===========================================================================================================================
63 //===========================================================================================================================
65 DEBUG_LOCAL
void Usage( void )
67 fprintf( stderr
, "\n" );
68 fprintf( stderr
, "NSP Tool 1.0d1\n" );
69 fprintf( stderr
, " Name Space Provider Tool\n" );
70 fprintf( stderr
, "\n" );
72 fprintf( stderr
, " -install <name> <guid> <path> - Installs a Name Space Provider\n" );
73 fprintf( stderr
, "\n" );
74 fprintf( stderr
, " <name> Name of the NSP\n" );
75 fprintf( stderr
, " <guid> GUID of the NSP\n" );
76 fprintf( stderr
, " <path> Path to the NSP file\n" );
77 fprintf( stderr
, "\n" );
79 fprintf( stderr
, " -remove <guid> - Removes a Name Space Provider\n" );
80 fprintf( stderr
, "\n" );
81 fprintf( stderr
, " <guid> GUID of the NSP\n" );
82 fprintf( stderr
, "\n" );
84 fprintf( stderr
, " -enable/-disable <guid> - Enables or Disables a Name Space Provider\n" );
85 fprintf( stderr
, "\n" );
86 fprintf( stderr
, " <guid> GUID of the NSP\n" );
87 fprintf( stderr
, "\n" );
89 fprintf( stderr
, " -list - Lists Name Space Providers\n" );
90 fprintf( stderr
, " -reorder - Reorders Name Space Providers\n" );
91 fprintf( stderr
, " -q - Enable quiet mode\n" );
92 fprintf( stderr
, " -h[elp] - Help\n" );
93 fprintf( stderr
, "\n" );
96 //===========================================================================================================================
98 //===========================================================================================================================
100 DEBUG_LOCAL
int ProcessArgs( int argc
, char* argv
[] )
114 for( i
= 1; i
< argc
; ++i
)
116 if( strcmp( argv
[ i
], "-install" ) == 0 )
120 if( argc
<= ( i
+ 3 ) )
122 fprintf( stderr
, "\n### ERROR: missing arguments for %s\n\n", argv
[ i
] );
133 name
= "DotLocalNSP";
137 guid
= "B600E6E9-553B-4a19-8696-335E5C896153";
140 err
= InstallNSP( name
, guid
, path
);
141 require_noerr( err
, exit
);
143 else if( strcmp( argv
[ i
], "-remove" ) == 0 )
147 if( argc
<= ( i
+ 1 ) )
149 fprintf( stderr
, "\n### ERROR: missing arguments for %s\n\n", argv
[ i
] );
157 guid
= "B600E6E9-553B-4a19-8696-335E5C896153";
160 err
= RemoveNSP( guid
);
161 require_noerr( err
, exit
);
163 else if( ( strcmp( argv
[ i
], "-enable" ) == 0 ) ||
164 ( strcmp( argv
[ i
], "-disable" ) == 0 ) )
170 enable
= ( strcmp( argv
[ i
], "-enable" ) == 0 );
171 if( argc
<= ( i
+ 1 ) )
173 fprintf( stderr
, "\n### ERROR: missing arguments for %s\n\n", argv
[ i
] );
180 err
= EnableNSP( guid
, enable
);
181 require_noerr( err
, exit
);
183 else if( strcmp( argv
[ i
], "-list" ) == 0 )
187 err
= ListNameSpaces();
188 require_noerr( err
, exit
);
190 else if( strcmp( argv
[ i
], "-reorder" ) == 0 )
194 err
= ReorderNameSpaces();
195 require_noerr( err
, exit
);
197 else if( strcmp( argv
[ i
], "-q" ) == 0 )
199 gToolQuietMode
= TRUE
;
201 else if( ( strcmp( argv
[ i
], "-help" ) == 0 ) ||
202 ( strcmp( argv
[ i
], "-h" ) == 0 ) )
212 fprintf( stderr
, "\n### ERROR: unknown argment: \"%s\"\n\n", argv
[ i
] );
228 //===========================================================================================================================
230 //===========================================================================================================================
232 OSStatus
InstallNSP( const char *inName
, const char *inGUID
, const char *inPath
)
239 WCHAR path
[ MAX_PATH
];
241 require_action( inName
&& ( *inName
!= '\0' ), exit
, err
= kParamErr
);
242 require_action( inGUID
&& ( *inGUID
!= '\0' ), exit
, err
= kParamErr
);
243 require_action( inPath
&& ( *inPath
!= '\0' ), exit
, err
= kParamErr
);
245 size
= strlen( inName
);
246 require_action( size
< sizeof_array( name
), exit
, err
= kSizeErr
);
247 CharToWCharString( inName
, name
);
249 err
= StringToGUID( inGUID
, &guid
);
250 require_noerr( err
, exit
);
252 size
= strlen( inPath
);
253 require_action( size
< sizeof_array( path
), exit
, err
= kSizeErr
);
254 CharToWCharString( inPath
, path
);
256 err
= WSAStartup( MAKEWORD( 2, 2 ), &wsd
);
257 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
258 require_noerr( err
, exit
);
260 err
= WSCInstallNameSpace( name
, path
, NS_DNS
, 1, &guid
);
261 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
263 require_noerr( err
, exit
);
267 fprintf( stderr
, "Installed NSP \"%s\" (%s) at %s\n", inName
, inGUID
, inPath
);
273 fprintf( stderr
, "### FAILED (%d) to install \"%s\" (%s) Name Space Provider at %s\n", err
, inName
, inGUID
, inPath
);
278 //===========================================================================================================================
280 //===========================================================================================================================
282 DEBUG_LOCAL OSStatus
RemoveNSP( const char *inGUID
)
288 require_action( inGUID
&& ( *inGUID
!= '\0' ), exit
, err
= kParamErr
);
290 err
= StringToGUID( inGUID
, &guid
);
291 require_noerr( err
, exit
);
293 err
= WSAStartup( MAKEWORD( 2, 2 ), &wsd
);
294 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
295 require_noerr( err
, exit
);
297 err
= WSCUnInstallNameSpace( &guid
);
298 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
300 require_noerr( err
, exit
);
304 fprintf( stderr
, "Removed NSP %s\n", inGUID
);
310 fprintf( stderr
, "### FAILED (%d) to remove %s Name Space Provider\n", err
, inGUID
);
315 //===========================================================================================================================
317 //===========================================================================================================================
319 DEBUG_LOCAL OSStatus
EnableNSP( const char *inGUID
, BOOL inEnable
)
325 require_action( inGUID
&& ( *inGUID
!= '\0' ), exit
, err
= kParamErr
);
327 err
= StringToGUID( inGUID
, &guid
);
328 require_noerr( err
, exit
);
330 err
= WSAStartup( MAKEWORD( 2, 2 ), &wsd
);
331 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
332 require_noerr( err
, exit
);
334 err
= WSCEnableNSProvider( &guid
, inEnable
);
335 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
337 require_noerr( err
, exit
);
341 fprintf( stderr
, "Removed NSP %s\n", inGUID
);
347 fprintf( stderr
, "### FAILED (%d) to remove %s Name Space Provider\n", err
, inGUID
);
352 //===========================================================================================================================
354 //===========================================================================================================================
356 DEBUG_LOCAL OSStatus
ListNameSpaces( void )
364 WSANAMESPACE_INFO
* array
;
370 err
= WSAStartup( MAKEWORD( 2, 2 ), &wsd
);
371 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
372 require_noerr( err
, exit
);
375 // Build an array of all the NSPs. Call it first with NULL to get the size, allocate a buffer, then get them into it.
378 n
= WSAEnumNameSpaceProviders( &size
, NULL
);
379 err
= translate_errno( n
!= SOCKET_ERROR
, (OSStatus
) GetLastError(), kUnknownErr
);
380 require_action( err
== WSAEFAULT
, exit
, err
= kUnknownErr
);
382 array
= (WSANAMESPACE_INFO
*) malloc( size
);
383 require_action( array
, exit
, err
= kNoMemoryErr
);
385 n
= WSAEnumNameSpaceProviders( &size
, array
);
386 err
= translate_errno( n
!= SOCKET_ERROR
, (OSStatus
) GetLastError(), kUnknownErr
);
387 require_noerr( err
, exit
);
389 fprintf( stdout
, "\n" );
390 for( i
= 0; i
< n
; ++i
)
392 fprintf( stdout
, "Name Space %d\n", i
+ 1 );
393 fprintf( stdout
, " NSProviderId: %s\n", GUIDtoString( &array
[ i
].NSProviderId
, sizeof( s
), s
) );
394 fprintf( stdout
, " dwNameSpace: %d\n", array
[ i
].dwNameSpace
);
395 fprintf( stdout
, " fActive: %s\n", array
[ i
].fActive
? "YES" : "NO" );
396 fprintf( stdout
, " dwVersion: %d\n", array
[ i
].dwVersion
);
397 fprintf( stdout
, " lpszIdentifier: \"%s\"\n", array
[ i
].lpszIdentifier
);
398 fprintf( stdout
, "\n" );
413 fprintf( stderr
, "### FAILED (%d) to list Name Space Providers\n", err
);
418 //===========================================================================================================================
420 //===========================================================================================================================
422 DEBUG_LOCAL OSStatus
ReorderNameSpaces( void )
430 WSANAMESPACE_INFO
* array
;
432 WCHAR path
[ MAX_PATH
];
437 err
= WSAStartup( MAKEWORD( 2, 2 ), &wsd
);
438 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
439 require_noerr( err
, exit
);
442 // Build an array of all the NSPs. Call it first with NULL to get the size, allocate a buffer, then get them into it.
445 n
= WSAEnumNameSpaceProviders( &size
, NULL
);
446 err
= translate_errno( n
!= SOCKET_ERROR
, (OSStatus
) GetLastError(), kUnknownErr
);
447 require_action( err
== WSAEFAULT
, exit
, err
= kUnknownErr
);
449 array
= (WSANAMESPACE_INFO
*) malloc( size
);
450 require_action( array
, exit
, err
= kNoMemoryErr
);
452 n
= WSAEnumNameSpaceProviders( &size
, array
);
453 err
= translate_errno( n
!= SOCKET_ERROR
, (OSStatus
) GetLastError(), kUnknownErr
);
454 require_noerr( err
, exit
);
456 // Find the "Tcpip" NSP.
458 for( i
= 0; i
< n
; ++i
)
460 if( strcmp( array
[ i
].lpszIdentifier
, "Tcpip" ) == 0 )
465 require_action( i
< n
, exit
, err
= kNotFoundErr
);
467 // Uninstall it then re-install it to move it to the end.
469 size
= (DWORD
) strlen( array
[ i
].lpszIdentifier
);
470 require_action( size
< sizeof_array( name
), exit
, err
= kSizeErr
);
471 CharToWCharString( array
[ i
].lpszIdentifier
, name
);
473 size
= (DWORD
) strlen( "%SystemRoot%\\System32\\mswsock.dll" );
474 require_action( size
< sizeof_array( path
), exit
, err
= kSizeErr
);
475 CharToWCharString( "%SystemRoot%\\System32\\mswsock.dll", path
);
477 err
= WSCUnInstallNameSpace( &array
[ i
].NSProviderId
);
478 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
479 require_noerr( err
, exit
);
481 err
= WSCInstallNameSpace( name
, path
, NS_DNS
, array
[ i
].dwVersion
, &array
[ i
].NSProviderId
);
482 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
483 require_noerr( err
, exit
);
487 fprintf( stderr
, "Reordered \"Tcpip\" NSP to to the bottom of the NSP chain\n" );
501 fprintf( stderr
, "### FAILED (%d) to reorder Name Space Providers\n", err
);
510 //===========================================================================================================================
512 //===========================================================================================================================
514 DEBUG_LOCAL WCHAR
* CharToWCharString( const char *inCharString
, WCHAR
*outWCharString
)
520 check( inCharString
);
521 check( outWCharString
);
524 dst
= outWCharString
;
530 } while( c
!= '\0' );
532 return( outWCharString
);
535 //===========================================================================================================================
537 //===========================================================================================================================
539 DEBUG_LOCAL
char * GUIDtoString( const GUID
*inGUID
, size_t inLen
, char *outString
)
544 _snprintf( outString
, inLen
, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
545 inGUID
->Data1
, inGUID
->Data2
, inGUID
->Data3
,
546 inGUID
->Data4
[ 0 ], inGUID
->Data4
[ 1 ], inGUID
->Data4
[ 2 ], inGUID
->Data4
[ 3 ],
547 inGUID
->Data4
[ 4 ], inGUID
->Data4
[ 5 ], inGUID
->Data4
[ 6 ], inGUID
->Data4
[ 7 ] );
551 //===========================================================================================================================
553 //===========================================================================================================================
555 DEBUG_LOCAL OSStatus
StringToGUID( const char *inCharString
, GUID
*outGUID
)
561 check( inCharString
);
564 n
= sscanf( inCharString
, "%lX-%hX-%hX-%02X%02X-%02X%02X%02X%02X%02X%02X",
565 &outGUID
->Data1
, &outGUID
->Data2
, &outGUID
->Data3
,
566 &v
[ 0 ], &v
[ 1 ], &v
[ 2 ], &v
[ 3 ], &v
[ 4 ], &v
[ 5 ], &v
[ 6 ], &v
[ 7 ] );
567 require_action( n
== 11, exit
, err
= kFormatErr
);
569 outGUID
->Data4
[ 0 ] = (unsigned char) v
[ 0 ];
570 outGUID
->Data4
[ 1 ] = (unsigned char) v
[ 1 ];
571 outGUID
->Data4
[ 2 ] = (unsigned char) v
[ 2 ];
572 outGUID
->Data4
[ 3 ] = (unsigned char) v
[ 3 ];
573 outGUID
->Data4
[ 4 ] = (unsigned char) v
[ 4 ];
574 outGUID
->Data4
[ 5 ] = (unsigned char) v
[ 5 ];
575 outGUID
->Data4
[ 6 ] = (unsigned char) v
[ 6 ];
576 outGUID
->Data4
[ 7 ] = (unsigned char) v
[ 7 ];