source: trunk/src/wnetap32/oslibnet.cpp@ 8052

Last change on this file since 8052 was 8052, checked in by sandervl, 23 years ago

PH: Netbios updates (currently disabled)

File size: 17.3 KB
Line 
1/* $Id: oslibnet.cpp,v 1.6 2002-03-08 11:37:10 sandervl Exp $
2 *
3 * Wrappers for OS/2 Netbios/Network/LAN API
4 *
5 * Copyright 2000 Patrick Haller (patrick.haller@innotek.de)
6 *
7 * Project Odin Software License can be found in LICENSE.TXT
8 *
9 */
10#define INCL_BASE
11#define INCL_DOSEXCEPTIONS
12#define INCL_DOSMEMMGR
13#define INCL_DOSPROCESS
14#define INCL_DOSFILEMGR
15#define INCL_DOSERRORS
16#define INCL_NPIPES
17#include <os2wrap.h> //Odin32 OS/2 api wrappers
18#include <stdlib.h>
19#include <stdio.h>
20#include <string.h>
21#include <ctype.h>
22#include <win32api.h>
23#include <winconst.h>
24#include <misc.h>
25
26#define PURE_32
27#include <netcons.h>
28#include <wksta.h>
29#include <neterr.h>
30#include <netstats.h>
31#include <server.h>
32#include <ncb.h>
33#include <netbios.h>
34// #include <netb_1_c.h>
35// #include <netb_2_c.h>
36// #include <netb_4_c.h>
37
38#include "wnetap32.h"
39#include "oslibnet.h"
40
41
42//******************************************************************************
43// translate OS/2 error codes to Windows codes
44// NOTE: add all codes you need, list is not complete!
45//******************************************************************************
46static DWORD error2WinError(APIRET rc)
47{
48 switch (rc)
49 {
50 // NT/LAN Manager specific error codes
51 case NERR_NetNotStarted: return NERR_NetNotStarted;
52 case NERR_UnknownServer: return NERR_UnknownServer;
53 case NERR_ShareMem: return NERR_ShareMem;
54 case NERR_NoNetworkResource: return NERR_NoNetworkResource;
55 case NERR_RemoteOnly: return NERR_RemoteOnly;
56 case NERR_DevNotRedirected: return NERR_DevNotRedirected;
57 case NERR_ServerNotStarted: return NERR_ServerNotStarted;
58 case NERR_ItemNotFound: return NERR_ItemNotFound;
59 case NERR_UnknownDevDir: return NERR_UnknownDevDir;
60 case NERR_RedirectedPath: return NERR_RedirectedPath;
61 case NERR_DuplicateShare: return NERR_DuplicateShare;
62 case NERR_NoRoom: return NERR_NoRoom;
63 case NERR_TooManyItems: return NERR_TooManyItems;
64 case NERR_InvalidMaxUsers: return NERR_InvalidMaxUsers;
65 case NERR_BufTooSmall: return NERR_BufTooSmall;
66 case NERR_RemoteErr: return NERR_RemoteErr;
67 case NERR_LanmanIniError: return NERR_LanmanIniError;
68 // case NERR_OS2IoctlError
69 case NERR_NetworkError: return NERR_NetworkError;
70 case NERR_WkstaNotStarted: return NERR_WkstaNotStarted;
71 case NERR_BASE + 39: return NERR_BASE + 39; // NERR_BrowserNotStarted
72 case NERR_InternalError: return NERR_InternalError;
73 case NERR_BadTransactConfig: return NERR_BadTransactConfig;
74 case NERR_InvalidAPI: return NERR_InvalidAPI;
75 case NERR_BadEventName: return NERR_BadEventName;
76 case NERR_BASE + 44: return NERR_BASE + 44; // NERR_DupNameReboot
77
78 // ...
79
80 case NO_ERROR: //0
81 return ERROR_SUCCESS_W;
82
83 case ERROR_INVALID_FUNCTION: //1
84 return ERROR_INVALID_FUNCTION_W;
85
86 case ERROR_FILE_NOT_FOUND: //2
87 return ERROR_FILE_NOT_FOUND_W;
88
89 case ERROR_PATH_NOT_FOUND: //3
90 return ERROR_PATH_NOT_FOUND_W;
91
92 case ERROR_TOO_MANY_OPEN_FILES: //4
93 return ERROR_TOO_MANY_OPEN_FILES_W;
94
95 case ERROR_ACCESS_DENIED: //5
96 return ERROR_ACCESS_DENIED_W;
97
98 case ERROR_INVALID_HANDLE: //6
99 return ERROR_INVALID_HANDLE_W;
100
101 case ERROR_NOT_ENOUGH_MEMORY: //8
102 return ERROR_NOT_ENOUGH_MEMORY_W;
103
104 case ERROR_BAD_FORMAT: //11
105 return ERROR_BAD_FORMAT_W;
106
107 case ERROR_INVALID_ACCESS: //12
108 return ERROR_INVALID_ACCESS_W;
109
110 case ERROR_NO_MORE_FILES: //18
111 return ERROR_NO_MORE_FILES_W;
112
113 case ERROR_WRITE_PROTECT: //19
114 return ERROR_WRITE_PROTECT_W;
115
116 case ERROR_NOT_DOS_DISK: //26
117 return ERROR_NOT_DOS_DISK_W;
118
119 case ERROR_WRITE_FAULT: //29
120 return ERROR_WRITE_FAULT_W;
121
122 case ERROR_SHARING_VIOLATION: //32
123 return ERROR_SHARING_VIOLATION_W;
124
125 case ERROR_LOCK_VIOLATION: //32
126 return ERROR_LOCK_VIOLATION_W;
127
128 case ERROR_SHARING_BUFFER_EXCEEDED: //36
129 return ERROR_SHARING_BUFFER_EXCEEDED_W;
130
131 case ERROR_CANNOT_MAKE: //82
132 return ERROR_CANNOT_MAKE_W;
133
134 case ERROR_OUT_OF_STRUCTURES: //84
135 return ERROR_OUT_OF_STRUCTURES_W;
136
137 case ERROR_INVALID_PARAMETER: //87
138 return ERROR_INVALID_PARAMETER_W;
139
140 case ERROR_INTERRUPT: //95
141 return ERROR_INVALID_AT_INTERRUPT_TIME_W; //CB: right???
142
143 case ERROR_DEVICE_IN_USE: //99
144 return ERROR_DEVICE_IN_USE_W;
145
146 case ERROR_DRIVE_LOCKED: //108
147 return ERROR_DRIVE_LOCKED_W;
148
149 case ERROR_BROKEN_PIPE: //109
150 return ERROR_BROKEN_PIPE_W;
151
152 case ERROR_OPEN_FAILED: //110
153 return ERROR_OPEN_FAILED_W;
154
155 case ERROR_BUFFER_OVERFLOW: //111
156 return ERROR_BUFFER_OVERFLOW_W;
157
158 case ERROR_DISK_FULL: //112
159 return ERROR_DISK_FULL_W;
160
161 case ERROR_NO_MORE_SEARCH_HANDLES: //113
162 return ERROR_NO_MORE_SEARCH_HANDLES_W;
163
164 case ERROR_SEM_TIMEOUT: //121
165 return ERROR_SEM_TIMEOUT_W;
166
167 case ERROR_DIRECT_ACCESS_HANDLE: //130
168 return ERROR_DIRECT_ACCESS_HANDLE_W;
169
170 case ERROR_NEGATIVE_SEEK: //131
171 return ERROR_NEGATIVE_SEEK;
172
173 case ERROR_SEEK_ON_DEVICE: //132
174 return ERROR_SEEK_ON_DEVICE_W;
175
176 case ERROR_DISCARDED: //157
177 return ERROR_DISCARDED_W;
178
179 case ERROR_FILENAME_EXCED_RANGE: //206
180 return ERROR_FILENAME_EXCED_RANGE_W;
181
182 case ERROR_META_EXPANSION_TOO_LONG: //208
183 return ERROR_META_EXPANSION_TOO_LONG_W;
184
185 case ERROR_BAD_PIPE: //230
186 return ERROR_BAD_PIPE_W;
187
188 case ERROR_PIPE_BUSY: //231
189 return ERROR_PIPE_BUSY_W;
190
191 case ERROR_NO_DATA: //232
192 return ERROR_NO_DATA_W;
193
194 case ERROR_PIPE_NOT_CONNECTED: //233
195 return ERROR_PIPE_NOT_CONNECTED_W;
196
197 case ERROR_MORE_DATA: //234
198 return ERROR_MORE_DATA_W;
199
200 case ERROR_INVALID_EA_NAME: //254
201 return ERROR_INVALID_EA_NAME_W;
202
203 case ERROR_EA_LIST_INCONSISTENT: //255
204 return ERROR_EA_LIST_INCONSISTENT_W;
205
206 case ERROR_EAS_DIDNT_FIT: //275
207 return ERROR_EAS_DIDNT_FIT;
208
209 default:
210 dprintf(("WARNING: error2WinError: error %d not included!!!!", rc));
211 return rc;
212 }
213}
214
215
216/*****************************************************************************
217 * Name : NET_API_STATUS OSLibNetWkstaGetInfo
218 * Purpose :
219 * Parameters:
220 * Variables :
221 * Result :
222 * Remark :
223 * Status : UNTESTED STUB
224 *
225 * Author : Patrick Haller 2000/01/10 01:42
226 *****************************************************************************/
227
228DWORD OSLibNetWkstaGetInfo (const unsigned char * pszServer,
229 unsigned long ulLevel,
230 unsigned char * pbBuffer,
231 unsigned long ulBuffer,
232 unsigned long * pulTotalAvail)
233{
234 USHORT sel = RestoreOS2FS();
235
236 APIRET rc = error2WinError(Net32WkstaGetInfo(pszServer, ulLevel, pbBuffer, ulBuffer, pulTotalAvail));
237 SetFS(sel);
238 return rc;
239}
240
241
242/*****************************************************************************
243 * Name : NET_API_STATUS OSLibNetStatisticsGet
244 * Purpose :
245 * Parameters:
246 * Variables :
247 * Result :
248 * Remark :
249 * Status : UNTESTED STUB
250 *
251 * Author : Patrick Haller 2000/01/10 01:42
252 *****************************************************************************/
253
254DWORD OSLibNetStatisticsGet(const unsigned char * pszServer,
255 const unsigned char * pszService,
256 unsigned long ulReserved,
257 unsigned long ulLevel,
258 unsigned long flOptions,
259 unsigned char * pbBuffer,
260 unsigned long ulBuffer,
261 unsigned long * pulTotalAvail)
262{
263 USHORT sel = RestoreOS2FS();
264 APIRET rc = error2WinError(Net32StatisticsGet2(pszServer,
265 pszService,
266 ulReserved,
267 ulLevel,
268 flOptions,
269 pbBuffer,
270 ulBuffer,
271 pulTotalAvail));
272 SetFS(sel);
273 return rc;
274}
275
276
277/*****************************************************************************
278 * Name : NET_API_STATUS OSLibNetStatisticsGet
279 * Purpose :
280 * Parameters:
281 * Variables :
282 * Result :
283 * Remark :
284 * Status : UNTESTED STUB
285 *
286 * Author : Patrick Haller 2000/01/10 01:42
287 *****************************************************************************/
288
289DWORD OSLibNetServerEnum(const unsigned char * pszServer,
290 unsigned long ulLevel,
291 unsigned char * pbBuffer,
292 unsigned long ulBufferLength,
293 unsigned long * pulEntriesReturned,
294 unsigned long * pulEntriesAvail,
295 unsigned long ulServerType,
296 unsigned char * pszDomain)
297{
298 USHORT sel = RestoreOS2FS();
299
300 APIRET rc = error2WinError(Net32ServerEnum2(pszServer,
301 ulLevel,
302 pbBuffer,
303 ulBufferLength,
304 pulEntriesReturned,
305 pulEntriesAvail,
306 ulServerType,
307 pszDomain));
308 SetFS(sel);
309 return rc;
310}
311
312
313/*****************************************************************************
314 * Name : OSLibNetBIOS
315 * Purpose :
316 * Parameters: PNCB pncb (Win32-style of the structure!!!)
317 * Variables :
318 * Result :
319 * Remark :
320 * Status : TESTED
321 *
322 * Author : Patrick Haller 2002/02/26 01:42
323 *****************************************************************************/
324
325UCHAR OSLibNetBIOS_Passthru(PNCB_w pncb)
326{
327 // IBM NETBIOS 3.0 as defined in ncb.h
328 // UCHAR rc = NetBios( (struct ncb LSFAR *)pncb );
329
330 // Note:
331 // asynchronous operation probably not really supported here,
332 // this has to be done with a wrapper thread!
333
334 // @@@PH
335 // open / close the right adapter based upon
336 // the LANA number specified inside the NCB
337
338 USHORT sel = RestoreOS2FS();
339 UCHAR rc = NetBios32Submit(0, // send automatically to first adapter
340 // open & close are implicit
341 0, // single NCB w/o error retry!
342 (struct ncb LSFAR *)pncb );
343 SetFS(sel);
344 return rc;
345}
346
347
348/*****************************************************************************
349 * Name : OSLibNetBIOSEnum
350 * Purpose :
351 * Parameters: PNCB pncb (Win32-style of the structure!!!)
352 * Variables :
353 * Result :
354 * Remark :
355 * Status : TESTED
356 *
357 * Author : Patrick Haller 2002/02/26 01:42
358 *****************************************************************************/
359
360UCHAR OSLibNetBIOS_Enum(PNCB_w pncb)
361{
362 static BOOL flagEnumAvailable = FALSE;
363 static UCHAR arrLANA[ MAX_LANA_w ];
364 static int iLANALength = 0;
365 USHORT sel = RestoreOS2FS();
366
367 if (FALSE == flagEnumAvailable)
368 {
369 ULONG ulEntriesReturned = 0;
370 ULONG ulEntriesAvailable = 0;
371 ULONG rc;
372 PVOID pBuffer;
373 ULONG ulBufferLength;
374 struct netbios_info_1* pNBI1;
375
376 // reset the array first
377 memset( arrLANA, 0, sizeof( arrLANA ) );
378
379
380 // determine number of available entries
381 rc = NetBios32Enum(NULL,
382 0,
383 NULL,
384 0,
385 &ulEntriesReturned,
386 &ulEntriesAvailable);
387
388 // if network adapters are available ...
389 iLANALength = ulEntriesAvailable;
390 if (0 != ulEntriesAvailable)
391 {
392 // allocate buffer of sufficient size
393 ulBufferLength = ulEntriesAvailable * sizeof( struct netbios_info_1 );
394 pBuffer = malloc( ulBufferLength );
395 if (NULL == pBuffer)
396 {
397 SetFS(sel);
398 pncb->ncb_retcode = NRC_SYSTEM_w;
399 return NRC_SYSTEM_w;
400 }
401
402 // enumerate all network drivers (net1, net2, ...)
403 // and build the array of LANA numbers
404 rc = NetBios32Enum(NULL, // local machine
405 1,
406 (PUCHAR)pBuffer,
407 ulBufferLength,
408 &ulEntriesReturned,
409 &ulEntriesAvailable);
410 pNBI1 = (struct netbios_info_1 *)pBuffer;
411 for (ULONG ulCount = 0;
412 ulCount < ulEntriesReturned;
413 ulCount++)
414 {
415 arrLANA[ ulCount ] = pNBI1->nb1_lana_num;
416 pNBI1++;
417 }
418
419 free( pBuffer );
420 }
421 // else no network adapters are available
422
423 flagEnumAvailable = TRUE;
424 }
425
426 // copy the result
427 PLANA_ENUM_w pLE = (PLANA_ENUM_w)pncb->ncb_buffer;
428 pLE->length = iLANALength;
429
430 // compensate UCHAR size for the length field
431 memcpy( pLE->lana,
432 arrLANA,
433 pncb->ncb_length - sizeof(UCHAR) );
434 pncb->ncb_retcode = NRC_GOODRET_w;
435
436 SetFS(sel);
437 return pncb->ncb_retcode;
438}
439
440
441
442
443/*****************************************************************************
444 * Name : OSLibNetBIOS
445 * Purpose :
446 * Parameters: PNCB pncb (Win32-style of the structure!!!)
447 * Variables :
448 * Result :
449 * Remark : this is for synchronous operation only!
450 * Status : TESTED
451 *
452 * Author : Patrick Haller 2002/02/26 01:42
453 *****************************************************************************/
454
455UCHAR OSLibNetBIOS(PNCB_w pncb)
456{
457 dprintf(("NETBIOS: Netbios NCB: command=%02xh, retcode=%d, lsn=%d, num=%d, buffer=%08xh, "
458 "length=%d callname='%s' name='%s' rto=%d sto=%d pfnCallback=%08xh, lana=%d, "
459 "cmd_cplt=%d event=%08xh\n",
460 pncb->ncb_command,
461 pncb->ncb_retcode,
462 pncb->ncb_lsn,
463 pncb->ncb_num,
464 pncb->ncb_buffer,
465 pncb->ncb_length,
466 pncb->ncb_callname,
467 pncb->ncb_name,
468 pncb->ncb_rto,
469 pncb->ncb_sto,
470 pncb->ncb_post,
471 pncb->ncb_lana_num,
472 pncb->ncb_cmd_cplt,
473 pncb->ncb_event));
474
475 // Request Router
476 // The request router is responsible for mapping the incoming commands
477 // and NCBs to their OS/2 NetBIOS pendant plus support of synchronous /
478 // asynchronous handling of NCB processing.
479
480 // Note:
481 // for a first attempt at NetBIOS support, we just try to pass through
482 // all supported commands and assume the structures are essentially
483 // compatible.
484 UCHAR rc;
485
486 switch (pncb->ncb_command & ~ASYNCH_w)
487 {
488 case NCBENUM_w:
489 {
490 // enumerate all network drivers (net1, net2, ...)
491 // and build the array of LANA numbers
492 rc = OSLibNetBIOS_Enum( pncb );
493 break;
494 }
495
496 // Note: NCBLANSTALERT_w
497 // seems to be supported in asynchronous manner only,
498 // we just try to pass it through!
499
500 case NCBACTION_w:
501 dprintf(("NCBACTION not yet implemented"));
502 break;
503
504 case NCBTRACE_w:
505 dprintf(("NCBTRACE not yet implemented"));
506 break;
507
508 case NCBRESET_w:
509 // This seems to cause the requester to go wild
510 // (netbios session limit exceeded)
511 // rc = OSLibNetBIOS_Passthru( pncb );
512
513 // Win32 and OS/2 do have different behaviour upon RESET calls.
514 // Returning OK here is experimental.
515 pncb->ncb_retcode = NRC_GOODRET_w;
516 pncb->ncb_cmd_cplt = NRC_GOODRET_w;
517 rc = NRC_GOODRET_w;
518 break;
519
520
521 default:
522 rc = OSLibNetBIOS_Passthru( pncb );
523 break;
524 }
525
526 dprintf(("NETBIOS: Netbios NCB: command=%02xh --> rc=%d\n",
527 pncb->ncb_command,
528 rc));
529 return rc;
530}
531
532
533/*****************************************************************************
534 * Name :
535 * Purpose :
536 * Parameters: PNCB pncb (Win32-style of the structure!!!)
537 * Variables :
538 * Result :
539 * Remark :
540 * Status : TESTED
541 *
542 * Author : Patrick Haller 2002/02/26 01:42
543 *****************************************************************************/
544
545DWORD WIN32API OSLibNetbiosHlpHandler(LPVOID lpParam)
546{
547 PNCB_w pncb = (PNCB_w)lpParam;
548 UCHAR ncb_cmd_original = pncb->ncb_command;
549 void (* CALLBACK ncb_post_original)( struct _NCB_w * ) = pncb->ncb_post;
550
551 // rewrite ncb for synchronous operation
552 pncb->ncb_command &= ~ASYNCH_w;
553 pncb->ncb_post = 0;
554 // ncb_cmd_cplt is expected to be NRC_PENDING_w
555 // when we come here (set by the main Netbios() function)
556
557 // synchronous operation
558 // return code is expected to be stored inside pncb
559 dprintf(("NETBIOS: async NETBIOS for command %02xh\n",
560 pncb->ncb_command));
561
562 OSLibNetBIOS( pncb );
563
564 dprintf(("NETBIOS: async NETBIOS for command %02xh returned %d\n",
565 pncb->ncb_command,
566 pncb->ncb_retcode));
567
568 // restore original command
569 pncb->ncb_command = ncb_cmd_original;
570 pncb->ncb_post = ncb_post_original;
571
572 // propagate the command complete value as netbios is expected
573 // to do for asynchronous operation
574 pncb->ncb_cmd_cplt= pncb->ncb_retcode;
575
576 // perform post-operation notification!
577 if (pncb->ncb_event)
578 {
579 // Netbios seems to use PulseEvent instead of SetEvent.
580
581 // SetEvent( pncb->ncb_event );
582 PulseEvent( pncb->ncb_event );
583 }
584
585 if (pncb->ncb_post != NULL)
586 {
587 // callback into post function
588 (pncb->ncb_post)( pncb );
589 }
590
591 return pncb->ncb_retcode;
592}
Note: See TracBrowser for help on using the repository browser.