Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

380 lines
9.9 KiB

  1. /****************************** Module Header ******************************\
  2. * Module Name: net.c
  3. *
  4. * PURPOSE: Contains routines network support
  5. *
  6. * Created: Feb 1991
  7. *
  8. * Copyright (c) 1991 Microsoft Corporation
  9. *
  10. * History:
  11. * Srinik 02\12\1190 Orginal
  12. * curts created portable version for WIN16/32
  13. *
  14. \***************************************************************************/
  15. #include <windows.h>
  16. #ifdef WIN16
  17. #include <winnet.h>
  18. #endif
  19. #ifdef WIN32
  20. #include <winnetwk.h>
  21. #endif
  22. #include "dll.h"
  23. #define MAX_DRIVE 26
  24. char szNULL[] = "";
  25. char szNetName[]= "NetName";
  26. BOOL FAR PASCAL GetTaskVisibleWindow (HWND, LPARAM);
  27. void INTERNAL RemoveNetName (LPOBJECT_LE);
  28. // Gets the drive letter from topic (if one exists) and then gets the remote
  29. // name for that drive and then saves it in the object.
  30. OLESTATUS FARINTERNAL SetNetName (
  31. LPOBJECT_LE lpobj
  32. ){
  33. char buf[MAX_STR];
  34. WORD2DWORD cbBuf = sizeof(buf);
  35. WORD2DWORD driveType;
  36. char szDrive[3];
  37. if (lpobj->head.ctype == CT_EMBEDDED)
  38. return OLE_OK;
  39. if (!GlobalGetAtomName (lpobj->topic, buf, cbBuf))
  40. return OLE_ERROR_BLANK;
  41. if (buf[1] != ':') {
  42. RemoveNetName (lpobj);
  43. return OLE_OK;
  44. }
  45. szDrive[2] = '\0';
  46. szDrive[1] = ':';
  47. szDrive[0] = buf[0];
  48. AnsiUpperBuff ((LPSTR) szDrive, 1);
  49. if (!(driveType = GetDriveType (MAPVALUE(szDrive[0] - 'A',szDrive)) )) {
  50. // drive is non existent
  51. return OLE_ERROR_DRIVE;
  52. }
  53. if (driveType == DRIVE_REMOTE) {
  54. if (WNetGetConnection (szDrive, buf, (MAPTYPE(LPWORD,LPDWORD)) &cbBuf)
  55. != WN_SUCCESS)
  56. return OLE_ERROR_DRIVE;
  57. lpobj->cDrive = szDrive[0];
  58. if (lpobj->aNetName)
  59. GlobalDeleteAtom (lpobj->aNetName);
  60. lpobj->aNetName = GlobalAddAtom(buf);
  61. #ifdef WIN16
  62. lpobj->dwNetInfo = MAKELONG((WNetGetCaps (WNNC_NET_TYPE)),
  63. (WNetGetCaps (WNNC_DRIVER_VERSION)));
  64. #endif
  65. }
  66. else {
  67. RemoveNetName (lpobj);
  68. }
  69. return OLE_OK;
  70. }
  71. // If netname exists for the given object, then it makes sure that drive
  72. // in topic corresponds to the netname. If it's not the drive letter will
  73. // be fixed by calling FixNet()
  74. OLESTATUS FARINTERNAL CheckNetDrive (
  75. LPOBJECT_LE lpobj,
  76. BOOL fNetDlg
  77. ){
  78. char buf[MAX_NET_NAME];
  79. char netName[MAX_NET_NAME];
  80. WORD2DWORD cbBuf = sizeof(buf);
  81. char szDrive[3];
  82. if (lpobj->head.ctype == CT_EMBEDDED)
  83. return OLE_OK;
  84. if (!lpobj->aNetName)
  85. return OLE_OK;
  86. if (!GlobalGetAtomName (lpobj->aNetName, netName, sizeof(netName)))
  87. return OLE_ERROR_MEMORY;
  88. szDrive[2] = '\0';
  89. szDrive[1] = ':';
  90. if (!(szDrive[0] = lpobj->cDrive)) {
  91. if (GlobalGetAtomName (lpobj->topic, buf, sizeof(buf)))
  92. szDrive[0] = lpobj->cDrive = buf[0];
  93. }
  94. if ((WNetGetConnection (szDrive, buf, (MAPTYPE(LPWORD,LPDWORD)) &cbBuf)
  95. == WN_SUCCESS) && (!lstrcmp(netName, buf)))
  96. return OLE_OK;
  97. return FixNet (lpobj, netName, fNetDlg);
  98. }
  99. // Find if there is a drive connected to the given server. If so, get the
  100. // drive letter and set it in topic. If not try to make connection, and if
  101. // that attempt is successful the set the drive letter in topic.
  102. OLESTATUS INTERNAL FixNet (
  103. LPOBJECT_LE lpobj,
  104. LPSTR lpNetName,
  105. BOOL fNetDlg
  106. ){
  107. int nDrive = 2; // drive 'C'
  108. OLESTATUS retVal;
  109. if (SetNextNetDrive(lpobj, &nDrive, lpNetName))
  110. return OLE_OK;
  111. if (fNetDlg != POPUP_NETDLG)
  112. return OLE_ERROR_NETWORK;
  113. if ((retVal = ConnectNet (lpobj, lpNetName)) == OLE_OK) {
  114. if (!ChangeTopic (lpobj))
  115. return OLE_ERROR_BLANK;
  116. }
  117. return retVal;
  118. }
  119. BOOL FARINTERNAL SetNextNetDrive (
  120. LPOBJECT_LE lpobj,
  121. int FAR * lpnDrive,
  122. LPSTR lpNetName
  123. ){
  124. char buf[MAX_STR];
  125. WORD2DWORD cbBuf = sizeof(buf);
  126. char szDrive[3];
  127. if (!lpNetName[0]) {
  128. if (!GlobalGetAtomName(lpobj->aNetName, lpNetName, MAX_STR))
  129. return FALSE;
  130. }
  131. szDrive[2] = '\0';
  132. szDrive[1] = ':';
  133. while (*lpnDrive < MAX_DRIVE) {
  134. szDrive[0] = (char) ('A' + (++*lpnDrive));
  135. if (GetDriveType (szDrive) == DRIVE_REMOTE) {
  136. #ifdef WIN16
  137. if (GetDriveType (++*lpnDrive) == DRIVE_REMOTE) {
  138. #endif
  139. cbBuf = sizeof(buf);
  140. if ((WNetGetConnection (szDrive, buf, (MAPTYPE(LPWORD,LPDWORD)) &cbBuf)
  141. == WN_SUCCESS) && (!lstrcmp(lpNetName, buf))) {
  142. lpobj->cDrive = szDrive[0];
  143. return ChangeTopic (lpobj);
  144. }
  145. }
  146. }
  147. return FALSE;
  148. }
  149. BOOL FARINTERNAL ChangeTopic (
  150. LPOBJECT_LE lpobj
  151. ){
  152. char buf[MAX_STR];
  153. if (!GlobalGetAtomName(lpobj->topic, buf, sizeof(buf)))
  154. return FALSE;
  155. if (lpobj->topic)
  156. GlobalDeleteAtom(lpobj->topic);
  157. buf[0] = lpobj->cDrive;
  158. lpobj->topic = GlobalAddAtom (buf);
  159. if (lpobj->hLink) {
  160. GlobalFree (lpobj->hLink);
  161. lpobj->hLink = NULL;
  162. }
  163. return TRUE;
  164. }
  165. OLESTATUS INTERNAL ConnectNet (
  166. LPOBJECT_LE lpobj,
  167. LPSTR lpNetName
  168. ){
  169. HWND hCurTask;
  170. HWND hwndParent = NULL;
  171. hCurTask = (HWND)ULongToPtr(MGetCurrentTask());
  172. ASSERT (hCurTask, "Current task handle in NULL");
  173. // Get the container task's main window, and use that as parent for
  174. // the dlg box.
  175. EnumTaskWindows (hCurTask, (WNDENUMPROC)GetTaskVisibleWindow,
  176. (DWORD_PTR) ((WORD FAR *) &hwndParent));
  177. if (lpobj->cDrive = (char) DialogBoxParam (hInstDLL, "CONNECTDLG",
  178. hwndParent, ConnectDlgProc,
  179. (LONG_PTR) lpNetName))
  180. return OLE_OK;
  181. else
  182. return OLE_ERROR_NETWORK;
  183. }
  184. INT_PTR FAR PASCAL ConnectDlgProc(
  185. HWND hDlg,
  186. UINT wMsg,
  187. WPARAM wParam,
  188. LPARAM lParam
  189. ){
  190. char szPassword[32];
  191. char szTitle[64];
  192. switch (wMsg) {
  193. case WM_INITDIALOG:
  194. SetProp (hDlg, szNetName, (HANDLE)lParam);
  195. FillDrives (hDlg);
  196. SetDlgItemText (hDlg, IDD_PATH, (LPSTR) lParam);
  197. break;
  198. case WM_COMMAND:
  199. switch (GET_WM_COMMAND_ID(wParam,lParam)) {
  200. case IDOK:
  201. {
  202. int cch = 128;
  203. char szMessage[128];
  204. char szDrive[3];
  205. LPSTR lpNetName;
  206. GetDlgItemText(hDlg, IDD_DRIVE, szDrive, sizeof(szDrive));
  207. GetDlgItemText(hDlg, IDD_PASSWORD, szPassword,
  208. sizeof(szPassword));
  209. lpNetName = (LPSTR) GetProp (hDlg, szNetName);
  210. wParam = WNetAddConnection (lpNetName,
  211. (LPSTR) szPassword, szDrive);
  212. if (wParam == WN_SUCCESS) {
  213. RemoveProp (hDlg, szNetName);
  214. EndDialog (hDlg, szDrive[0]);
  215. return TRUE;
  216. }
  217. LoadString (hInstDLL, IDS_NETERR, szTitle,
  218. sizeof(szTitle));
  219. #ifdef WIN16
  220. if (WNetGetErrorText ((UINT)wParam, szMessage, &cch)
  221. != WN_SUCCESS)
  222. #endif
  223. LoadString (hInstDLL, IDS_NETCONERRMSG,
  224. szMessage, sizeof(szMessage));
  225. if (MessageBox (hDlg, szMessage, szTitle,
  226. MB_RETRYCANCEL) == IDCANCEL)
  227. goto error;
  228. if (wParam == WN_ALREADY_CONNECTED)
  229. FillDrives (hDlg);
  230. SetDlgItemText (hDlg, IDD_PASSWORD, szNULL);
  231. break;
  232. }
  233. case IDCANCEL:
  234. error:
  235. RemoveProp (hDlg, szNetName);
  236. EndDialog(hDlg, 0);
  237. return TRUE;
  238. case IDD_DRIVE:
  239. break;
  240. case IDD_PATH:
  241. if (GET_WM_COMMAND_CMD(wParam,lParam) == EN_KILLFOCUS) {
  242. LPSTR lpNetName;
  243. lpNetName = (LPSTR) GetProp (hDlg, szNetName);
  244. SendDlgItemMessage (hDlg, IDD_PATH, WM_SETTEXT, 0,
  245. (DWORD_PTR) lpNetName);
  246. }
  247. break;
  248. default:
  249. break;
  250. }
  251. break;
  252. default:
  253. break;
  254. }
  255. return FALSE;
  256. }
  257. VOID INTERNAL FillDrives (
  258. HWND hDlg
  259. ){
  260. HWND hwndCB;
  261. int nDrive = 3;
  262. char szDrive[3];
  263. DWORD dwDriveType;
  264. hwndCB = GetDlgItem(hDlg, IDD_DRIVE);
  265. SendMessage(hwndCB, CB_RESETCONTENT, 0, 0L);
  266. szDrive[2] = '\0';
  267. szDrive[1] = ':';
  268. while (nDrive < MAX_DRIVE) {
  269. szDrive[0] = (char) ('A' + nDrive);
  270. #ifdef WIN32
  271. if ((dwDriveType = GetDriveType (szDrive)) == 1)
  272. #endif
  273. #ifdef WIN16
  274. if (!GetDriveType (nDrive))
  275. #endif
  276. SendMessage(hwndCB, CB_ADDSTRING, 0, (DWORD_PTR)(LPSTR)szDrive);
  277. nDrive++;
  278. }
  279. SendMessage(hwndCB, CB_SETCURSEL, 0, 0L);
  280. }
  281. BOOL FAR PASCAL GetTaskVisibleWindow (
  282. HWND hWnd,
  283. LPARAM lpTaskVisWnd
  284. ){
  285. if (IsWindowVisible (hWnd)) {
  286. *(HWND FAR *)lpTaskVisWnd = hWnd;
  287. return FALSE;
  288. }
  289. return TRUE;
  290. }
  291. void INTERNAL RemoveNetName (LPOBJECT_LE lpobj)
  292. {
  293. if (lpobj->aNetName) {
  294. GlobalDeleteAtom (lpobj->aNetName);
  295. lpobj->aNetName = (ATOM)0;
  296. }
  297. lpobj->cDrive = '\0';
  298. }