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.

586 lines
16 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. main.c
  5. Abstract:
  6. <TODO: fill in abstract>
  7. Author:
  8. TODO: <full name> (<alias>) <date>
  9. Revision History:
  10. <full name> (<alias>) <date> <comments>
  11. --*/
  12. #include "pch.h"
  13. #include "wininet.h"
  14. #include <lm.h>
  15. HANDLE g_hHeap;
  16. HINSTANCE g_hInst;
  17. BOOL WINAPI MigUtil_Entry (HINSTANCE, DWORD, PVOID);
  18. BOOL g_Source = FALSE;
  19. BOOL
  20. pCallEntryPoints (
  21. DWORD Reason
  22. )
  23. {
  24. switch (Reason) {
  25. case DLL_PROCESS_ATTACH:
  26. UtInitialize (NULL);
  27. break;
  28. case DLL_PROCESS_DETACH:
  29. UtTerminate ();
  30. break;
  31. }
  32. return TRUE;
  33. }
  34. BOOL
  35. Init (
  36. VOID
  37. )
  38. {
  39. g_hHeap = GetProcessHeap();
  40. g_hInst = GetModuleHandle (NULL);
  41. return pCallEntryPoints (DLL_PROCESS_ATTACH);
  42. }
  43. VOID
  44. Terminate (
  45. VOID
  46. )
  47. {
  48. pCallEntryPoints (DLL_PROCESS_DETACH);
  49. }
  50. VOID
  51. HelpAndExit (
  52. VOID
  53. )
  54. {
  55. //
  56. // This routine is called whenever command line args are wrong
  57. //
  58. fprintf (
  59. stderr,
  60. "Command Line Syntax:\n\n"
  61. //
  62. // TODO: Describe command line syntax(es), indent 2 spaces
  63. //
  64. " utiltool [/F:file]\n"
  65. "\nDescription:\n\n"
  66. //
  67. // TODO: Describe tool, indent 2 spaces
  68. //
  69. " <Not Specified>\n"
  70. "\nArguments:\n\n"
  71. //
  72. // TODO: Describe args, indent 2 spaces, say optional if necessary
  73. //
  74. " /F Specifies optional file name\n"
  75. );
  76. exit (1);
  77. }
  78. HANDLE
  79. pOpenAndSetPort (
  80. IN PCTSTR ComPort
  81. )
  82. {
  83. HANDLE result = INVALID_HANDLE_VALUE;
  84. COMMTIMEOUTS commTimeouts;
  85. DCB dcb;
  86. // let's open the port. If we can't we just exit with error;
  87. result = CreateFile (ComPort, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  88. if (result == INVALID_HANDLE_VALUE) {
  89. return result;
  90. }
  91. // we want 10 sec timeout for both read and write
  92. commTimeouts.ReadIntervalTimeout = 0;
  93. commTimeouts.ReadTotalTimeoutMultiplier = 0;
  94. commTimeouts.ReadTotalTimeoutConstant = 10000;
  95. commTimeouts.WriteTotalTimeoutMultiplier = 0;
  96. commTimeouts.WriteTotalTimeoutConstant = 10000;
  97. SetCommTimeouts (result, &commTimeouts);
  98. // let's set some comm state data
  99. if (GetCommState (result, &dcb)) {
  100. dcb.fBinary = 1;
  101. dcb.fParity = 1;
  102. dcb.ByteSize = 8;
  103. if (g_Source) {
  104. dcb.BaudRate = CBR_115200;
  105. } else {
  106. dcb.BaudRate = CBR_57600;
  107. }
  108. if (!SetCommState (result, &dcb)) {
  109. CloseHandle (result);
  110. result = INVALID_HANDLE_VALUE;
  111. return result;
  112. }
  113. } else {
  114. CloseHandle (result);
  115. result = INVALID_HANDLE_VALUE;
  116. return result;
  117. }
  118. return result;
  119. }
  120. #define ACK 0x16
  121. #define NAK 0x15
  122. #define SOH 0x01
  123. #define EOT 0x04
  124. BOOL
  125. pSendFileToHandle (
  126. IN HANDLE DeviceHandle,
  127. IN PCTSTR FileName
  128. )
  129. {
  130. HANDLE fileHandle = NULL;
  131. BOOL result = TRUE;
  132. BYTE buffer [132];
  133. BYTE signal;
  134. BYTE currBlock = 0;
  135. DWORD numRead;
  136. DWORD numWritten;
  137. BOOL repeat = FALSE;
  138. UINT index;
  139. fileHandle = BfOpenReadFile (FileName);
  140. if (!fileHandle) {
  141. return FALSE;
  142. }
  143. // finally let's start the protocol
  144. // We are going to listen for the NAK(15h) signal.
  145. // As soon as we get it we are going to send a 132 bytes block having:
  146. // 1 byte - SOH (01H)
  147. // 1 byte - block number
  148. // 1 byte - FF - block number
  149. // 128 bytes of data
  150. // 1 byte - checksum - sum of all 128 bytes of data
  151. // After the block is sent, we are going to wait for ACK(16h). If we don't get
  152. // it after timeout or if we get something else we are going to send the block again.
  153. // wait for NAK
  154. while (!ReadFile (DeviceHandle, &signal, sizeof (signal), &numRead, NULL) ||
  155. (numRead != 1) ||
  156. (signal != NAK)
  157. );
  158. repeat = FALSE;
  159. while (TRUE) {
  160. if (!repeat) {
  161. // prepare the next block
  162. currBlock ++;
  163. if (currBlock == 0) {
  164. result = TRUE;
  165. }
  166. buffer [0] = SOH;
  167. buffer [1] = currBlock;
  168. buffer [2] = 0xFF - currBlock;
  169. if (!ReadFile (fileHandle, buffer + 3, 128, &numRead, NULL) ||
  170. (numRead == 0)
  171. ) {
  172. // we are done with data, send the EOT signal
  173. signal = EOT;
  174. WriteFile (DeviceHandle, &signal, sizeof (signal), &numWritten, NULL);
  175. break;
  176. }
  177. // compute the checksum
  178. buffer [sizeof (buffer) - 1] = 0;
  179. signal = 0;
  180. for (index = 0; index < sizeof (buffer) - 1; index ++) {
  181. signal += buffer [index];
  182. }
  183. buffer [sizeof (buffer) - 1] = signal;
  184. }
  185. // now send the block to the other side
  186. if (!WriteFile (DeviceHandle, buffer, sizeof (buffer), &numWritten, NULL) ||
  187. (numWritten != sizeof (buffer))
  188. ) {
  189. repeat = TRUE;
  190. } else {
  191. repeat = FALSE;
  192. }
  193. if (repeat) {
  194. // we could not send the data last time
  195. // let's just wait for a NAK for 10 sec and then send it again
  196. ReadFile (DeviceHandle, &signal, sizeof (signal), &numRead, NULL);
  197. } else {
  198. // we sent it OK. We need to wait for an ACK to come. If we timeout
  199. // or we get something else, we will repeat the block.
  200. if (!ReadFile (DeviceHandle, &signal, sizeof (signal), &numRead, NULL) ||
  201. (numRead != sizeof (signal)) ||
  202. (signal != ACK)
  203. ) {
  204. repeat = TRUE;
  205. }
  206. }
  207. }
  208. // we are done here. However, let's listen one more timeout for a
  209. // potential NAK. If we get it, we'll repeat the EOT signal
  210. while (ReadFile (DeviceHandle, &signal, sizeof (signal), &numRead, NULL) &&
  211. (numRead == 1)
  212. ) {
  213. if (signal == NAK) {
  214. signal = EOT;
  215. WriteFile (DeviceHandle, &signal, sizeof (signal), &numWritten, NULL);
  216. }
  217. }
  218. CloseHandle (fileHandle);
  219. return result;
  220. }
  221. BOOL
  222. pSendFile (
  223. IN PCTSTR ComPort,
  224. IN PCTSTR FileName
  225. )
  226. {
  227. HANDLE deviceHandle = INVALID_HANDLE_VALUE;
  228. BOOL result = FALSE;
  229. deviceHandle = pOpenAndSetPort (ComPort);
  230. if ((!deviceHandle) || (deviceHandle == INVALID_HANDLE_VALUE)) {
  231. return result;
  232. }
  233. result = pSendFileToHandle (deviceHandle, FileName);
  234. CloseHandle (deviceHandle);
  235. return result;
  236. }
  237. BOOL
  238. pReceiveFileFromHandle (
  239. IN HANDLE DeviceHandle,
  240. IN PCTSTR FileName
  241. )
  242. {
  243. HANDLE fileHandle = NULL;
  244. BOOL result = TRUE;
  245. BYTE buffer [132];
  246. BYTE signal;
  247. BYTE currBlock = 1;
  248. DWORD numRead;
  249. DWORD numWritten;
  250. BOOL repeat = TRUE;
  251. UINT index;
  252. fileHandle = BfCreateFile (FileName);
  253. if (!fileHandle) {
  254. return FALSE;
  255. }
  256. // finally let's start the protocol
  257. // We are going to send an NAK(15h) signal.
  258. // After that we are going to listen for a block.
  259. // If we don't get the block in time, or the block is wrong size
  260. // or it has a wrong checksum we are going to send a NAK signal,
  261. // otherwise we are going to send an ACK signal
  262. // One exception. If the block size is 1 and the block is actually the
  263. // EOT signal it means we are done.
  264. while (TRUE) {
  265. if (repeat) {
  266. // send the NAK
  267. signal = NAK;
  268. WriteFile (DeviceHandle, &signal, sizeof (signal), &numWritten, NULL);
  269. } else {
  270. // send the ACK
  271. signal = ACK;
  272. WriteFile (DeviceHandle, &signal, sizeof (signal), &numWritten, NULL);
  273. }
  274. repeat = TRUE;
  275. // let's read the data block
  276. if (ReadFile (DeviceHandle, buffer, sizeof (buffer), &numRead, NULL)) {
  277. if ((numRead == 1) &&
  278. (buffer [0] == EOT)
  279. ) {
  280. break;
  281. }
  282. if (numRead == sizeof (buffer)) {
  283. // compute the checksum
  284. signal = 0;
  285. for (index = 0; index < sizeof (buffer) - 1; index ++) {
  286. signal += buffer [index];
  287. }
  288. if (buffer [sizeof (buffer) - 1] == signal) {
  289. repeat = FALSE;
  290. // checksum is correct, let's see if this is the right block
  291. if (currBlock < buffer [1]) {
  292. // this is a major error, the sender is ahead of us,
  293. // we have to fail
  294. result = FALSE;
  295. break;
  296. }
  297. if (currBlock == buffer [1]) {
  298. WriteFile (fileHandle, buffer + 3, 128, &numWritten, NULL);
  299. currBlock ++;
  300. }
  301. }
  302. }
  303. }
  304. }
  305. CloseHandle (fileHandle);
  306. return result;
  307. }
  308. BOOL
  309. pReceiveFile (
  310. IN PCTSTR ComPort,
  311. IN PCTSTR FileName
  312. )
  313. {
  314. HANDLE deviceHandle = INVALID_HANDLE_VALUE;
  315. BOOL result = FALSE;
  316. deviceHandle = pOpenAndSetPort (ComPort);
  317. if ((!deviceHandle) || (deviceHandle == INVALID_HANDLE_VALUE)) {
  318. return result;
  319. }
  320. result = pReceiveFileFromHandle (deviceHandle, FileName);
  321. CloseHandle (deviceHandle);
  322. return result;
  323. }
  324. BOOL
  325. pPrintStuff (
  326. PCTSTR ComPort
  327. )
  328. {
  329. HANDLE comPortHandle = INVALID_HANDLE_VALUE;
  330. COMMTIMEOUTS commTimeouts;
  331. DCB dcb;
  332. COMMPROP commProp;
  333. printf ("Processing %s...\n\n", ComPort);
  334. comPortHandle = CreateFile (ComPort, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  335. if (comPortHandle == INVALID_HANDLE_VALUE) {
  336. printf ("Cannot open comport. Error: %d\n", GetLastError ());
  337. return FALSE;
  338. }
  339. if (GetCommTimeouts (comPortHandle, &commTimeouts)) {
  340. printf ("Timeouts:\n");
  341. printf ("ReadIntervalTimeout %d\n", commTimeouts.ReadIntervalTimeout);
  342. printf ("ReadTotalTimeoutMultiplier %d\n", commTimeouts.ReadTotalTimeoutMultiplier);
  343. printf ("ReadTotalTimeoutConstant %d\n", commTimeouts.ReadTotalTimeoutConstant);
  344. printf ("WriteTotalTimeoutMultiplier %d\n", commTimeouts.WriteTotalTimeoutMultiplier);
  345. printf ("WriteTotalTimeoutConstant %d\n", commTimeouts.WriteTotalTimeoutConstant);
  346. printf ("\n");
  347. } else {
  348. printf ("Cannot get CommTimeouts. Error: %d\n\n", GetLastError ());
  349. }
  350. if (GetCommState (comPortHandle, &dcb)) {
  351. printf ("CommState:\n");
  352. printf ("DCBlength %d\n", dcb.DCBlength);
  353. printf ("BaudRate %d\n", dcb.BaudRate);
  354. printf ("fBinary %d\n", dcb.fBinary);
  355. printf ("fParity %d\n", dcb.fParity);
  356. printf ("fOutxCtsFlow %d\n", dcb.fOutxCtsFlow);
  357. printf ("fOutxDsrFlow %d\n", dcb.fOutxDsrFlow);
  358. printf ("fDtrControl %d\n", dcb.fDtrControl);
  359. printf ("fDsrSensitivity %d\n", dcb.fDsrSensitivity);
  360. printf ("fTXContinueOnXoff %d\n", dcb.fTXContinueOnXoff);
  361. printf ("fOutX %d\n", dcb.fOutX);
  362. printf ("fInX %d\n", dcb.fInX);
  363. printf ("fErrorChar %d\n", dcb.fErrorChar);
  364. printf ("fNull %d\n", dcb.fNull);
  365. printf ("fRtsControl %d\n", dcb.fRtsControl);
  366. printf ("fAbortOnError %d\n", dcb.fAbortOnError);
  367. printf ("fDummy2 %d\n", dcb.fDummy2);
  368. printf ("wReserved %d\n", dcb.wReserved);
  369. printf ("XonLim %d\n", dcb.XonLim);
  370. printf ("XoffLim %d\n", dcb.XoffLim);
  371. printf ("ByteSize %d\n", dcb.ByteSize);
  372. printf ("Parity %d\n", dcb.Parity);
  373. printf ("StopBits %d\n", dcb.StopBits);
  374. printf ("XonChar %d\n", dcb.XonChar);
  375. printf ("XoffChar %d\n", dcb.XoffChar);
  376. printf ("ErrorChar %d\n", dcb.ErrorChar);
  377. printf ("EofChar %d\n", dcb.EofChar);
  378. printf ("EvtChar %d\n", dcb.EvtChar);
  379. printf ("wReserved1 %d\n", dcb.wReserved1);
  380. printf ("\n");
  381. } else {
  382. printf ("Cannot get CommState. Error: %d\n\n", GetLastError ());
  383. }
  384. if (GetCommProperties (comPortHandle, &commProp)) {
  385. printf ("CommProperties:\n");
  386. printf ("wPacketLength %d\n", commProp.wPacketLength);
  387. printf ("wPacketVersion %d\n", commProp.wPacketVersion);
  388. printf ("dwServiceMask %d\n", commProp.dwServiceMask);
  389. printf ("dwReserved1 %d\n", commProp.dwReserved1);
  390. printf ("dwMaxTxQueue %d\n", commProp.dwMaxTxQueue);
  391. printf ("dwMaxRxQueue %d\n", commProp.dwMaxRxQueue);
  392. printf ("dwMaxBaud %d\n", commProp.dwMaxBaud);
  393. printf ("dwProvSubType %d\n", commProp.dwProvSubType);
  394. printf ("dwProvCapabilities %d\n", commProp.dwProvCapabilities);
  395. printf ("dwSettableParams %d\n", commProp.dwSettableParams);
  396. printf ("dwSettableBaud %d\n", commProp.dwSettableBaud);
  397. printf ("wSettableData %d\n", commProp.wSettableData);
  398. printf ("wSettableStopParity %d\n", commProp.wSettableStopParity);
  399. printf ("dwCurrentTxQueue %d\n", commProp.dwCurrentTxQueue);
  400. printf ("dwCurrentRxQueue %d\n", commProp.dwCurrentRxQueue);
  401. printf ("dwProvSpec1 %d\n", commProp.dwProvSpec1);
  402. printf ("dwProvSpec2 %d\n", commProp.dwProvSpec2);
  403. printf ("wcProvChar %S\n", commProp.wcProvChar);
  404. printf ("\n");
  405. } else {
  406. printf ("Cannot get CommProperties. Error: %d\n\n", GetLastError ());
  407. }
  408. return TRUE;
  409. }
  410. INT
  411. __cdecl
  412. _tmain (
  413. INT argc,
  414. PCTSTR argv[]
  415. )
  416. {
  417. INT i;
  418. PCTSTR FileArg = NULL;
  419. PCTSTR comPort = NULL;
  420. BOOL sender = FALSE;
  421. //
  422. // TODO: Parse command line here
  423. //
  424. for (i = 1 ; i < argc ; i++) {
  425. if (argv[i][0] == TEXT('/') || argv[i][0] == TEXT('-')) {
  426. switch (_totlower (_tcsnextc (&argv[i][1]))) {
  427. case TEXT('f'):
  428. //
  429. // Sample option - /f:file
  430. //
  431. if (argv[i][2] == TEXT(':')) {
  432. FileArg = &argv[i][3];
  433. } else if (i + 1 < argc) {
  434. FileArg = argv[++i];
  435. } else {
  436. HelpAndExit();
  437. }
  438. break;
  439. case TEXT('s'):
  440. sender = TRUE;
  441. g_Source = TRUE;
  442. break;
  443. case TEXT('c'):
  444. //
  445. // Sample option - /f:file
  446. //
  447. if (argv[i][2] == TEXT(':')) {
  448. comPort = &argv[i][3];
  449. } else if (i + 1 < argc) {
  450. comPort = argv[++i];
  451. } else {
  452. HelpAndExit();
  453. }
  454. break;
  455. default:
  456. HelpAndExit();
  457. }
  458. } else {
  459. //
  460. // Parse other args that don't require / or -
  461. //
  462. // None
  463. HelpAndExit();
  464. }
  465. }
  466. //
  467. // Begin processing
  468. //
  469. if (!Init()) {
  470. return 0;
  471. }
  472. //
  473. // TODO: Do work here
  474. //
  475. {
  476. pPrintStuff (comPort);
  477. /*
  478. if (sender) {
  479. pSendFile (comPort, FileArg);
  480. } else {
  481. pReceiveFile (comPort, FileArg);
  482. }
  483. */
  484. }
  485. //
  486. // End of processing
  487. //
  488. Terminate();
  489. return 0;
  490. }