2 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 Change History (most recent first):
28 Revision 1.1 2004/01/30 03:02:58 bradley
29 NameSpace Provider Tool for installing, removing, list, etc. NameSpace Providers.
36 #include "CommonServices.h"
37 #include "DebugServices.h"
42 //===========================================================================================================================
44 //===========================================================================================================================
46 int main( int argc
, char *argv
[] );
47 DEBUG_LOCAL
void Usage( void );
48 DEBUG_LOCAL
int ProcessArgs( int argc
, char *argv
[] );
49 DEBUG_LOCAL OSStatus
InstallNSP( const char *inName
, const char *inGUID
, const char *inPath
);
50 DEBUG_LOCAL OSStatus
RemoveNSP( const char *inGUID
);
51 DEBUG_LOCAL OSStatus
EnableNSP( const char *inGUID
, BOOL inEnable
);
52 DEBUG_LOCAL OSStatus
ListNameSpaces( void );
53 DEBUG_LOCAL OSStatus
ReorderNameSpaces( void );
55 DEBUG_LOCAL WCHAR
* CharToWCharString( const char *inCharString
, WCHAR
*outWCharString
);
56 DEBUG_LOCAL
char * GUIDtoString( const GUID
*inGUID
, char *outString
);
57 DEBUG_LOCAL OSStatus
StringToGUID( const char *inCharString
, GUID
*outGUID
);
59 //===========================================================================================================================
61 //===========================================================================================================================
63 int main( int argc
, char *argv
[] )
67 debug_initialize( kDebugOutputTypeMetaConsole
);
68 debug_set_property( kDebugPropertyTagPrintLevel
, kDebugLevelVerbose
);
70 err
= ProcessArgs( argc
, argv
);
74 //===========================================================================================================================
76 //===========================================================================================================================
78 DEBUG_LOCAL
void Usage( void )
80 fprintf( stderr
, "\n" );
81 fprintf( stderr
, "NSP Tool 1.0d1\n" );
82 fprintf( stderr
, " Name Space Provider Tool\n" );
83 fprintf( stderr
, "\n" );
85 fprintf( stderr
, " -install <name> <guid> <path> - Installs a Name Space Provider\n" );
86 fprintf( stderr
, "\n" );
87 fprintf( stderr
, " <name> Name of the NSP\n" );
88 fprintf( stderr
, " <guid> GUID of the NSP\n" );
89 fprintf( stderr
, " <path> Path to the NSP file\n" );
90 fprintf( stderr
, "\n" );
92 fprintf( stderr
, " -remove <guid> - Removes a Name Space Provider\n" );
93 fprintf( stderr
, "\n" );
94 fprintf( stderr
, " <guid> GUID of the NSP\n" );
95 fprintf( stderr
, "\n" );
97 fprintf( stderr
, " -enable/-disable <guid> - Enables or Disables a Name Space Provider\n" );
98 fprintf( stderr
, "\n" );
99 fprintf( stderr
, " <guid> GUID of the NSP\n" );
100 fprintf( stderr
, "\n" );
102 fprintf( stderr
, " -list - Lists Name Space Providers\n" );
103 fprintf( stderr
, " -reorder - Reorders Name Space Providers\n" );
104 fprintf( stderr
, " -h[elp] - Help\n" );
105 fprintf( stderr
, "\n" );
108 //===========================================================================================================================
110 //===========================================================================================================================
112 DEBUG_LOCAL
int ProcessArgs( int argc
, char* argv
[] )
126 for( i
= 1; i
< argc
; ++i
)
128 if( strcmp( argv
[ i
], "-install" ) == 0 )
132 if( argc
<= ( i
+ 3 ) )
134 fprintf( stderr
, "\n### ERROR: missing arguments for %s\n\n", argv
[ i
] );
145 name
= "DotLocalNSP";
149 guid
= "B600E6E9-553B-4a19-8696-335E5C896153";
152 err
= InstallNSP( name
, guid
, path
);
153 require_noerr( err
, exit
);
155 else if( strcmp( argv
[ i
], "-remove" ) == 0 )
159 if( argc
<= ( i
+ 1 ) )
161 fprintf( stderr
, "\n### ERROR: missing arguments for %s\n\n", argv
[ i
] );
169 guid
= "B600E6E9-553B-4a19-8696-335E5C896153";
172 err
= RemoveNSP( guid
);
173 require_noerr( err
, exit
);
175 else if( ( strcmp( argv
[ i
], "-enable" ) == 0 ) ||
176 ( strcmp( argv
[ i
], "-disable" ) == 0 ) )
182 enable
= ( strcmp( argv
[ i
], "-enable" ) == 0 );
183 if( argc
<= ( i
+ 1 ) )
185 fprintf( stderr
, "\n### ERROR: missing arguments for %s\n\n", argv
[ i
] );
192 err
= EnableNSP( guid
, enable
);
193 require_noerr( err
, exit
);
195 else if( strcmp( argv
[ i
], "-list" ) == 0 )
199 err
= ListNameSpaces();
200 require_noerr( err
, exit
);
202 else if( strcmp( argv
[ i
], "-reorder" ) == 0 )
206 err
= ReorderNameSpaces();
207 require_noerr( err
, exit
);
209 else if( ( strcmp( argv
[ i
], "-help" ) == 0 ) ||
210 ( strcmp( argv
[ i
], "-h" ) == 0 ) )
220 fprintf( stderr
, "\n### ERROR: unknown argment: \"%s\"\n\n", argv
[ i
] );
236 //===========================================================================================================================
238 //===========================================================================================================================
240 OSStatus
InstallNSP( const char *inName
, const char *inGUID
, const char *inPath
)
247 WCHAR path
[ MAX_PATH
];
249 require_action( inName
&& ( *inName
!= '\0' ), exit
, err
= kParamErr
);
250 require_action( inGUID
&& ( *inGUID
!= '\0' ), exit
, err
= kParamErr
);
251 require_action( inPath
&& ( *inPath
!= '\0' ), exit
, err
= kParamErr
);
253 size
= strlen( inName
);
254 require_action( size
< sizeof_array( name
), exit
, err
= kSizeErr
);
255 CharToWCharString( inName
, name
);
257 err
= StringToGUID( inGUID
, &guid
);
258 require_noerr( err
, exit
);
260 size
= strlen( inPath
);
261 require_action( size
< sizeof_array( path
), exit
, err
= kSizeErr
);
262 CharToWCharString( inPath
, path
);
264 err
= WSAStartup( MAKEWORD( 2, 2 ), &wsd
);
265 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
266 require_noerr( err
, exit
);
268 err
= WSCInstallNameSpace( name
, path
, NS_DNS
, 1, &guid
);
269 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
271 require_noerr( err
, exit
);
273 fprintf( stderr
, "Installed NSP \"%s\" (%s) at %s\n", inName
, inGUID
, inPath
);
278 fprintf( stderr
, "### FAILED (%d) to install \"%s\" (%s) Name Space Provider at %s\n", err
, inName
, inGUID
, inPath
);
283 //===========================================================================================================================
285 //===========================================================================================================================
287 DEBUG_LOCAL OSStatus
RemoveNSP( const char *inGUID
)
293 require_action( inGUID
&& ( *inGUID
!= '\0' ), exit
, err
= kParamErr
);
295 err
= StringToGUID( inGUID
, &guid
);
296 require_noerr( err
, exit
);
298 err
= WSAStartup( MAKEWORD( 2, 2 ), &wsd
);
299 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
300 require_noerr( err
, exit
);
302 err
= WSCUnInstallNameSpace( &guid
);
303 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
305 require_noerr( err
, exit
);
307 fprintf( stderr
, "Removed NSP %s\n", inGUID
);
312 fprintf( stderr
, "### FAILED (%d) to remove %s Name Space Provider\n", err
, inGUID
);
317 //===========================================================================================================================
319 //===========================================================================================================================
321 DEBUG_LOCAL OSStatus
EnableNSP( const char *inGUID
, BOOL inEnable
)
327 require_action( inGUID
&& ( *inGUID
!= '\0' ), exit
, err
= kParamErr
);
329 err
= StringToGUID( inGUID
, &guid
);
330 require_noerr( err
, exit
);
332 err
= WSAStartup( MAKEWORD( 2, 2 ), &wsd
);
333 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
334 require_noerr( err
, exit
);
336 err
= WSCEnableNSProvider( &guid
, inEnable
);
337 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
339 require_noerr( err
, exit
);
341 fprintf( stderr
, "Removed NSP %s\n", inGUID
);
346 fprintf( stderr
, "### FAILED (%d) to remove %s Name Space Provider\n", err
, inGUID
);
351 //===========================================================================================================================
353 //===========================================================================================================================
355 DEBUG_LOCAL OSStatus
ListNameSpaces( void )
363 WSANAMESPACE_INFO
* array
;
369 err
= WSAStartup( MAKEWORD( 2, 2 ), &wsd
);
370 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
371 require_noerr( err
, exit
);
374 // Build an array of all the NSPs. Call it first with NULL to get the size, allocate a buffer, then get them into it.
377 n
= WSAEnumNameSpaceProviders( &size
, NULL
);
378 err
= translate_errno( n
!= SOCKET_ERROR
, (OSStatus
) GetLastError(), kUnknownErr
);
379 require_action( err
== WSAEFAULT
, exit
, err
= kUnknownErr
);
381 array
= (WSANAMESPACE_INFO
*) malloc( size
);
382 require_action( array
, exit
, err
= kNoMemoryErr
);
384 n
= WSAEnumNameSpaceProviders( &size
, array
);
385 err
= translate_errno( n
!= SOCKET_ERROR
, (OSStatus
) GetLastError(), kUnknownErr
);
386 require_noerr( err
, exit
);
388 fprintf( stdout
, "\n" );
389 for( i
= 0; i
< n
; ++i
)
391 fprintf( stdout
, "Name Space %d\n", i
+ 1 );
392 fprintf( stdout
, " NSProviderId: %s\n", GUIDtoString( &array
[ i
].NSProviderId
, s
) );
393 fprintf( stdout
, " dwNameSpace: %d\n", array
[ i
].dwNameSpace
);
394 fprintf( stdout
, " fActive: %s\n", array
[ i
].fActive
? "YES" : "NO" );
395 fprintf( stdout
, " dwVersion: %d\n", array
[ i
].dwVersion
);
396 fprintf( stdout
, " lpszIdentifier: \"%s\"\n", array
[ i
].lpszIdentifier
);
397 fprintf( stdout
, "\n" );
412 fprintf( stderr
, "### FAILED (%d) to list Name Space Providers\n", err
);
417 //===========================================================================================================================
419 //===========================================================================================================================
421 DEBUG_LOCAL OSStatus
ReorderNameSpaces( void )
429 WSANAMESPACE_INFO
* array
;
431 WCHAR path
[ MAX_PATH
];
436 err
= WSAStartup( MAKEWORD( 2, 2 ), &wsd
);
437 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
438 require_noerr( err
, exit
);
441 // Build an array of all the NSPs. Call it first with NULL to get the size, allocate a buffer, then get them into it.
444 n
= WSAEnumNameSpaceProviders( &size
, NULL
);
445 err
= translate_errno( n
!= SOCKET_ERROR
, (OSStatus
) GetLastError(), kUnknownErr
);
446 require_action( err
== WSAEFAULT
, exit
, err
= kUnknownErr
);
448 array
= (WSANAMESPACE_INFO
*) malloc( size
);
449 require_action( array
, exit
, err
= kNoMemoryErr
);
451 n
= WSAEnumNameSpaceProviders( &size
, array
);
452 err
= translate_errno( n
!= SOCKET_ERROR
, (OSStatus
) GetLastError(), kUnknownErr
);
453 require_noerr( err
, exit
);
455 // Find the "Tcpip" NSP.
457 for( i
= 0; i
< n
; ++i
)
459 if( strcmp( array
[ i
].lpszIdentifier
, "Tcpip" ) == 0 )
464 require_action( i
< n
, exit
, err
= kNotFoundErr
);
466 // Uninstall it then re-install it to move it to the end.
468 size
= strlen( array
[ i
].lpszIdentifier
);
469 require_action( size
< sizeof_array( name
), exit
, err
= kSizeErr
);
470 CharToWCharString( array
[ i
].lpszIdentifier
, name
);
472 size
= strlen( "%SystemRoot%\\System32\\mswsock.dll" );
473 require_action( size
< sizeof_array( path
), exit
, err
= kSizeErr
);
474 CharToWCharString( "%SystemRoot%\\System32\\mswsock.dll", path
);
476 err
= WSCUnInstallNameSpace( &array
[ i
].NSProviderId
);
477 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
478 require_noerr( err
, exit
);
480 err
= WSCInstallNameSpace( name
, path
, NS_DNS
, array
[ i
].dwVersion
, &array
[ i
].NSProviderId
);
481 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
482 require_noerr( err
, exit
);
486 fprintf( stderr
, "Reordered \"Tcpip\" NSP to to the bottom of the NSP chain\n" );
500 fprintf( stderr
, "### FAILED (%d) to reorder Name Space Providers\n", err
);
509 //===========================================================================================================================
511 //===========================================================================================================================
513 DEBUG_LOCAL WCHAR
* CharToWCharString( const char *inCharString
, WCHAR
*outWCharString
)
519 check( inCharString
);
520 check( outWCharString
);
523 dst
= outWCharString
;
529 } while( c
!= '\0' );
531 return( outWCharString
);
534 //===========================================================================================================================
536 //===========================================================================================================================
538 DEBUG_LOCAL
char * GUIDtoString( const GUID
*inGUID
, char *outString
)
543 sprintf( outString
, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
544 inGUID
->Data1
, inGUID
->Data2
, inGUID
->Data3
,
545 inGUID
->Data4
[ 0 ], inGUID
->Data4
[ 1 ], inGUID
->Data4
[ 2 ], inGUID
->Data4
[ 3 ],
546 inGUID
->Data4
[ 4 ], inGUID
->Data4
[ 5 ], inGUID
->Data4
[ 6 ], inGUID
->Data4
[ 7 ] );
550 //===========================================================================================================================
552 //===========================================================================================================================
554 DEBUG_LOCAL OSStatus
StringToGUID( const char *inCharString
, GUID
*outGUID
)
560 check( inCharString
);
563 n
= sscanf( inCharString
, "%lX-%hX-%hX-%02X%02X-%02X%02X%02X%02X%02X%02X",
564 &outGUID
->Data1
, &outGUID
->Data2
, &outGUID
->Data3
,
565 &v
[ 0 ], &v
[ 1 ], &v
[ 2 ], &v
[ 3 ], &v
[ 4 ], &v
[ 5 ], &v
[ 6 ], &v
[ 7 ] );
566 require_action( n
== 11, exit
, err
= kFormatErr
);
568 outGUID
->Data4
[ 0 ] = (unsigned char) v
[ 0 ];
569 outGUID
->Data4
[ 1 ] = (unsigned char) v
[ 1 ];
570 outGUID
->Data4
[ 2 ] = (unsigned char) v
[ 2 ];
571 outGUID
->Data4
[ 3 ] = (unsigned char) v
[ 3 ];
572 outGUID
->Data4
[ 4 ] = (unsigned char) v
[ 4 ];
573 outGUID
->Data4
[ 5 ] = (unsigned char) v
[ 5 ];
574 outGUID
->Data4
[ 6 ] = (unsigned char) v
[ 6 ];
575 outGUID
->Data4
[ 7 ] = (unsigned char) v
[ 7 ];