summaryrefslogtreecommitdiffstats
path: root/private/nw/nwscript/attach.c
blob: fa6cacc75aff000af37314c97a0b532be84a2d53 (plain) (blame)
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
/*************************************************************************
*
*  ATTACH.C
*
*  NT Attach routines
*
*  Copyright (c) 1995 Microsoft Corporation
*
*  $Log:   N:\NT\PRIVATE\NW4\NWSCRIPT\VCS\ATTACH.C  $
*  
*     Rev 1.2   10 Apr 1996 14:21:30   terryt
*  Hotfix for 21181hq
*  
*     Rev 1.2   12 Mar 1996 19:52:08   terryt
*  Relative NDS names and merge
*  
*     Rev 1.1   22 Dec 1995 14:23:32   terryt
*  Add Microsoft headers
*  
*     Rev 1.0   15 Nov 1995 18:06:26   terryt
*  Initial revision.
*  
*     Rev 1.1   23 May 1995 19:36:30   terryt
*  Spruce up source
*  
*     Rev 1.0   15 May 1995 19:10:10   terryt
*  Initial revision.
*  
*************************************************************************/

#include <stdio.h>
#include <direct.h>
#include <time.h>
#include <stdlib.h>

#include <nt.h>
#include <ntrtl.h>
#include <nturtl.h>
#include <windows.h>

#include <nwapi32.h>
#include <ntddnwfs.h>
#include <nwapi.h>
#include <npapi.h>

#include "inc/common.h"
#include "ntnw.h"

/********************************************************************

        GetDefaultConnectionID

Routine Description:

        Return the default connection ID ( the "preferred server" )

Arguments:

        phNewConn - pointer to connection number

Return Value:
        0 = success
        else NetWare error number

 *******************************************************************/
unsigned int
GetDefaultConnectionID(
    unsigned int   *phNewConn
    )
{
    VERSION_INFO VerInfo;
    unsigned int Result;

    if ( fNDS ) 
    {
        Result = NTAttachToFileServer( NDSTREE, phNewConn );
    }
    else 
    {
        //
        //  "*" is the name for the preferred server
        //
        Result = NTAttachToFileServer( "*", phNewConn );
        if ( Result )
            return Result;

        Result = NWGetFileServerVersionInfo( (NWCONN_HANDLE)*phNewConn,
                                         &VerInfo );
        if ( Result )
            return Result;

        NWDetachFromFileServer( (NWCONN_HANDLE)*phNewConn );

        Result = NTAttachToFileServer( VerInfo.szName, phNewConn );
    }
    return Result;

}

/********************************************************************

        NTAttachToFileServer

Routine Description:

        Given a server name, return a connection handle.
        We need our own because NWAPI32 does it's own mapping
        of errors.

Arguments:

        pszServerName - Ascii server name
        phNewConn     - pointer to connection handle

Return Value:
        0 = success
        else NetWare error number

 *******************************************************************/
unsigned int
NTAttachToFileServer(
    unsigned char  *pszServerName,
    unsigned int   *phNewConn
    )
{
    return ( NWAttachToFileServer( pszServerName, 0,
                                   (NWCONN_HANDLE *)phNewConn ) );
}


/********************************************************************

        NTIsConnected

Routine Description:

        Given a server name, is there already a connection to it?

Arguments:

        pszServerName - ascii server name

Return Value:
        TRUE  - a connection to the server exists
        FALSE - a connection to the server does not exist

 *******************************************************************/
unsigned int
NTIsConnected( unsigned char * pszServerName )
{
    LPBYTE       Buffer ; 
    DWORD        dwErr ;
    HANDLE       EnumHandle ;
    DWORD        Count ;
    LPWSTR       pszServerNameW;
    INT          nSize;
    DWORD        BufferSize = 4096;

    nSize = (strlen( pszServerName ) + 1 + 2) * sizeof( WCHAR );
    
    //
    // allocate memory and open the enumeration
    //
    if (!(pszServerNameW = LocalAlloc( LPTR, nSize ))) {
        DisplayMessage(IDR_NOT_ENOUGH_MEMORY);
        return FALSE;
    }
    wcscpy( pszServerNameW, L"\\\\" );
    szToWide( pszServerNameW + 2, pszServerName, nSize );
 
    //
    // allocate memory and open the enumeration
    //
    if (!(Buffer = LocalAlloc( LPTR, BufferSize ))) {
        (void) LocalFree((HLOCAL) pszServerNameW) ;
        DisplayMessage(IDR_NOT_ENOUGH_MEMORY);
        return FALSE;
    }
    memset( Buffer, 0, BufferSize );

    dwErr = NPOpenEnum(RESOURCE_CONNECTED, 0, 0, NULL, &EnumHandle) ;
    if (dwErr != WN_SUCCESS) {
        (void) LocalFree((HLOCAL) pszServerNameW) ;
        (void) LocalFree((HLOCAL) Buffer) ;
        return FALSE;
    }

    do {

        Count = 0xFFFFFFFF ;
        BufferSize = 4096;
        dwErr = NwEnumConnections(EnumHandle, &Count, Buffer, &BufferSize, TRUE) ;

        if ((dwErr == WN_SUCCESS || dwErr == WN_NO_MORE_ENTRIES)
            && ( Count != 0xFFFFFFFF) )
        {
            LPNETRESOURCE lpNetResource ;
            DWORD i ;
            DWORD ServerLen;

            ServerLen = wcslen( pszServerNameW );
            lpNetResource = (LPNETRESOURCE) Buffer ;
            //
            // search for our server
            //
            for ( i = 0; i < Count; lpNetResource++, i++ )
            {
              if ( lpNetResource->lpProvider )
                  if ( _wcsicmp( lpNetResource->lpProvider, NW_PROVIDER ) ) {
                      continue;
                  }
               if ( lpNetResource->lpRemoteName ) {
                   if ( wcslen(lpNetResource->lpRemoteName) > ServerLen ) {
                       if ( lpNetResource->lpRemoteName[ServerLen] == L'\\' ) 
                           lpNetResource->lpRemoteName[ServerLen] = L'\0';
                   }
                   if ( !_wcsicmp(lpNetResource->lpRemoteName, pszServerNameW )) {
                       (void) WNetCloseEnum(EnumHandle) ; 
                       (void) LocalFree((HLOCAL) pszServerNameW) ;
                       (void) LocalFree((HLOCAL) Buffer) ;
                       return TRUE;
                   }
               }
            }

        }

    } while (dwErr == WN_SUCCESS) ;

    (void ) WNetCloseEnum(EnumHandle) ;
    (void) LocalFree((HLOCAL) pszServerNameW) ;
    (void) LocalFree((HLOCAL) Buffer) ;

    return FALSE;
}