ReactOS  0.4.12-dev-102-g4b7f1e0
netbios.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Service Host
3  * LICENSE: BSD - See COPYING.ARM in the top level directory
4  * FILE: base/services/svchost/netbios.c
5  * PURPOSE: NetBIOS Service Support
6  * PROGRAMMERS: ReactOS Portable Systems Group
7  */
8 
9 /* INCLUDES ******************************************************************/
10 
11 #include "svchost.h"
12 
13 #include <lmerr.h>
14 #include <nb30.h>
15 
16 /* GLOBALS *******************************************************************/
17 
21 
22 /* FUNCTIONS *****************************************************************/
23 
24 DWORD
25 WINAPI
27  _In_ DWORD NetBiosError
28  )
29 {
30  /* Convert from one status to another */
31  switch (NetBiosError)
32  {
33  case NRC_GOODRET:
34  return NERR_Success;
35  case NRC_INUSE:
36  case NRC_NAMCONF:
37  return NERR_DuplicateName;
38  case NRC_NOWILD:
39  case NRC_NAMERR:
41  case NRC_NOCALL:
42  return NERR_NameNotFound;
43  case NRC_NORES:
45  case NRC_DUPNAME:
46  return NERR_AlreadyExists;
47  case NRC_NAMTFUL:
48  return NERR_TooManyNames;
49  case NRC_ACTSES:
50  return NERR_DeleteLater;
51  case NRC_REMTFUL:
52  return ERROR_REM_NOT_LIST;
53  default:
54  return NERR_NetworkError;
55  }
56 }
57 
58 BOOL
59 WINAPI
61  _In_ UCHAR Lana
62  )
63 {
64  DWORD i = Lana / 32;
65 
66  /* Clear the flag for this LANA */
67  return (i <= 7) ? LanaFlags[i] & (1 << (Lana - 32 * i)) : FALSE;
68 }
69 
70 VOID
71 WINAPI
73  _In_ UCHAR Lana
74  )
75 {
76  DWORD i = Lana / 32;
77 
78  /* Set the flag for this LANA */
79  if (i <= 7) LanaFlags[i] |= 1 << (Lana - 32 * i);
80 }
81 
82 VOID
83 WINAPI
85  VOID
86  )
87 {
88  /* Initialize NetBIOS-related structures and variables */
89  InitializeCriticalSection(&SvcNetBiosCritSec);
91  ZeroMemory(LanaFlags, sizeof(LanaFlags));
92 }
93 
94 VOID
95 WINAPI
97  VOID
98  )
99 {
100  /* While holding the lock, drop a reference*/
101  EnterCriticalSection(&SvcNetBiosCritSec);
102  if ((GlobalNetBiosUseCount != 0) && (--GlobalNetBiosUseCount == 0))
103  {
104  /* All references are gone, clear all LANA's */
105  ZeroMemory(LanaFlags, sizeof(LanaFlags));
106  }
107  LeaveCriticalSection(&SvcNetBiosCritSec);
108 }
109 
110 VOID
111 WINAPI
113  VOID
114  )
115 {
116  /* Increment the reference counter while holding the lock */
117  EnterCriticalSection(&SvcNetBiosCritSec);
119  LeaveCriticalSection(&SvcNetBiosCritSec);
120 }
121 
122 DWORD
123 WINAPI
125  _In_ UCHAR LanaNum
126  )
127 {
128  DWORD dwError = ERROR_SUCCESS;
129  UCHAR nbtError;
130  NCB ncb;
131 
132  /* Block all other NetBIOS operations */
133  EnterCriticalSection(&SvcNetBiosCritSec);
134 
135  /* Is this LANA enabled? */
136  if (!LanaFlagIsSet(LanaNum))
137  {
138  /* Yep, build a reset packet */
139  ZeroMemory(&ncb, sizeof(ncb));
140  ncb.ncb_lsn = 0;
141  ncb.ncb_command = NCBRESET;
142  ncb.ncb_callname[0] = 0xFE; // Max Sessions
143  ncb.ncb_callname[1] = 0;
144  ncb.ncb_callname[2] = 0xFD; // Max Names
145  ncb.ncb_callname[3] = 0;
146  ncb.ncb_lana_num = LanaNum;
147 
148  /* Send it */
149  nbtError = Netbios(&ncb);
150 
151  /* Convert the status to Win32 format */
152  dwError = SvcNetBiosStatusToApiStatus(nbtError);
153 
154  /* Enable the LANA if the reset worked */
155  if (dwError == ERROR_SUCCESS) SetLanaFlag(LanaNum);
156  }
157 
158  /* Drop the lock and return */
159  LeaveCriticalSection(&SvcNetBiosCritSec);
160  return dwError;
161 }
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
#define NRC_GOODRET
Definition: nb30.h:52
#define ERROR_SUCCESS
Definition: deptool.c:10
UCHAR WINAPI Netbios(PNCB ncb)
Definition: netbios.c:785
#define NRC_NAMTFUL
Definition: nb30.h:63
UCHAR ncb_callname[NCBNAMSZ]
Definition: nb30.h:155
DWORD LanaFlags[8]
Definition: netbios.c:19
Definition: nb30.h:148
#define NERR_NameNotFound
Definition: lmerr.h:123
#define ZeroMemory
Definition: winbase.h:1635
#define ERROR_REM_NOT_LIST
Definition: winerror.h:143
#define NERR_DuplicateName
Definition: lmerr.h:142
CRITICAL_SECTION SvcNetBiosCritSec
Definition: netbios.c:20
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
VOID WINAPI SetLanaFlag(_In_ UCHAR Lana)
Definition: netbios.c:72
#define NERR_Success
Definition: lmerr.h:5
GLenum GLclampf GLint i
Definition: glfuncs.h:14
long LONG
Definition: pedump.c:60
VOID WINAPI InitializeCriticalSection(OUT LPCRITICAL_SECTION lpCriticalSection)
Definition: synch.c:697
#define NRC_NAMCONF
Definition: nb30.h:73
VOID WINAPI SvcNetBiosInit(VOID)
Definition: netbios.c:84
#define NERR_AlreadyExists
Definition: lmerr.h:126
#define NCBRESET
Definition: nb30.h:38
VOID WINAPI SvcNetBiosClose(VOID)
Definition: netbios.c:96
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
#define NRC_REMTFUL
Definition: nb30.h:66
unsigned char UCHAR
Definition: xmlstorage.h:181
BOOL WINAPI LanaFlagIsSet(_In_ UCHAR Lana)
Definition: netbios.c:60
#define NRC_NAMERR
Definition: nb30.h:71
#define NRC_NOWILD
Definition: nb30.h:69
UCHAR ncb_lana_num
Definition: nb30.h:160
#define NRC_DUPNAME
Definition: nb30.h:62
#define NERR_DeleteLater
Definition: lmerr.h:143
#define NRC_INUSE
Definition: nb30.h:70
#define WINAPI
Definition: msvc.h:20
DWORD WINAPI SvcNetBiosStatusToApiStatus(_In_ DWORD NetBiosError)
Definition: netbios.c:26
#define NRC_ACTSES
Definition: nb30.h:64
#define _In_
Definition: no_sal2.h:204
#define NERR_TooManyNames
Definition: lmerr.h:127
LONG GlobalNetBiosUseCount
Definition: netbios.c:18
#define NERR_NoNetworkResource
Definition: lmerr.h:10
#define NRC_NORES
Definition: nb30.h:59
#define NRC_NOCALL
Definition: nb30.h:68
UCHAR ncb_lsn
Definition: nb30.h:151
DWORD WINAPI SvcNetBiosReset(_In_ UCHAR LanaNum)
Definition: netbios.c:124
UCHAR ncb_command
Definition: nb30.h:149
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
VOID WINAPI SvcNetBiosOpen(VOID)
Definition: netbios.c:112
#define NERR_NetworkError
Definition: lmerr.h:24