Source code of Windows XP (NT5)
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.

839 lines
18 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1992 - 1999
  3. Module Name:
  4. Client.c
  5. Abstract:
  6. The Client component of Remote. Connects to the remote
  7. server using named pipes. It sends its stdin to
  8. the server and output everything from server to
  9. its stdout.
  10. Author:
  11. Rajivendra Nath (rajnath) 2-Jan-1992
  12. Environment:
  13. Console App. User mode.
  14. Revision History:
  15. --*/
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <io.h>
  19. #include <string.h>
  20. #include "Remote.h"
  21. HANDLE*
  22. EstablishSession(
  23. TCHAR *server,
  24. TCHAR *pipe
  25. );
  26. SOCKET*
  27. SockEstablishSession(
  28. TCHAR *server,
  29. TCHAR *pipe
  30. );
  31. DWORD
  32. GetServerOut(
  33. PVOID *Noarg
  34. );
  35. DWORD
  36. SockGetServerOut(
  37. PVOID *Noarg
  38. );
  39. DWORD
  40. SendServerInp(
  41. PVOID *Noarg
  42. );
  43. DWORD
  44. SockSendServerInp(
  45. PVOID *Noarg
  46. );
  47. BOOL
  48. FilterClientInp(
  49. TCHAR *buff,
  50. int count
  51. );
  52. BOOL
  53. SockFilterClientInp(
  54. TCHAR *buff,
  55. int count
  56. );
  57. BOOL
  58. Mych(
  59. DWORD ctrlT
  60. );
  61. BOOL
  62. SockMych(
  63. DWORD ctrlT
  64. );
  65. VOID
  66. SendMyInfo(
  67. PHANDLE Pipes
  68. );
  69. VOID
  70. SockSendMyInfo(
  71. SOCKET MySocket
  72. );
  73. HANDLE iothreads[2];
  74. HANDLE MyStdInp;
  75. HANDLE MyStdOut;
  76. HANDLE ReadPipe;
  77. HANDLE WritePipe;
  78. SOCKET RWSocket;
  79. CONSOLE_SCREEN_BUFFER_INFO csbi;
  80. TCHAR MyEchoStr[30];
  81. BOOL CmdSent;
  82. DWORD LinesToSend=LINESTOSEND;
  83. VOID
  84. Client(
  85. TCHAR* Server,
  86. TCHAR* Pipe
  87. )
  88. {
  89. HANDLE *Connection;
  90. DWORD tid;
  91. MyStdInp=GetStdHandle(STD_INPUT_HANDLE);
  92. MyStdOut=GetStdHandle(STD_OUTPUT_HANDLE);
  93. _tprintf(TEXT("****************************************\n"));
  94. _tprintf(TEXT("*********** WSREMOTE ************\n"));
  95. _tprintf(TEXT("*********** CLIENT ************\n"));
  96. _tprintf(TEXT("****************************************\n"));
  97. if ((Connection=EstablishSession(Server,Pipe))==NULL)
  98. return;
  99. ReadPipe=Connection[0];
  100. WritePipe=Connection[1];
  101. SetConsoleCtrlHandler((PHANDLER_ROUTINE)Mych,TRUE);
  102. // Start Thread For Server --> Client Flow
  103. if ((iothreads[0]=CreateThread((LPSECURITY_ATTRIBUTES)NULL, // No security attributes.
  104. (DWORD)0, // Use same stack size.
  105. (LPTHREAD_START_ROUTINE)GetServerOut, // Thread procedure.
  106. (LPVOID)NULL, // Parameter to pass.
  107. (DWORD)0, // Run immediately.
  108. (LPDWORD)&tid))==NULL) // Thread identifier.
  109. {
  110. Errormsg(TEXT("Could Not Create rwSrv2Cl Thread"));
  111. return;
  112. }
  113. //
  114. // Start Thread for Client --> Server Flow
  115. //
  116. if ((iothreads[1]=CreateThread((LPSECURITY_ATTRIBUTES)NULL, // No security attributes.
  117. (DWORD)0, // Use same stack size.
  118. (LPTHREAD_START_ROUTINE)SendServerInp, // Thread procedure.
  119. (LPVOID)NULL, // Parameter to pass.
  120. (DWORD)0, // Run immediately.
  121. (LPDWORD)&tid))==NULL) // Thread identifier.
  122. {
  123. Errormsg(TEXT("Could Not Create rwSrv2Cl Thread"));
  124. return;
  125. }
  126. WaitForMultipleObjects(2,iothreads,FALSE,INFINITE);
  127. TerminateThread(iothreads[0],1);
  128. TerminateThread(iothreads[1],1);
  129. _tprintf(TEXT("*** SESSION OVER ***\n"));
  130. }
  131. VOID
  132. SockClient(
  133. TCHAR* Server,
  134. TCHAR* Pipe
  135. )
  136. {
  137. SOCKET *Connection;
  138. DWORD tid;
  139. int nRet;
  140. MyStdInp=GetStdHandle(STD_INPUT_HANDLE);
  141. MyStdOut=GetStdHandle(STD_OUTPUT_HANDLE);
  142. _tprintf(TEXT("**************************************\n"));
  143. _tprintf(TEXT("*********** WSREMOTE ************\n"));
  144. _tprintf(TEXT("*********** CLIENT(IP) ************\n"));
  145. _tprintf(TEXT("**************************************\n"));
  146. if ((Connection=SockEstablishSession(Server,Pipe))==NULL)
  147. return;
  148. RWSocket = *Connection;
  149. SetConsoleCtrlHandler((PHANDLER_ROUTINE)SockMych,TRUE);
  150. // Start Thread For Server --> Client Flow
  151. if ((iothreads[0]=CreateThread((LPSECURITY_ATTRIBUTES)NULL, // No security attributes.
  152. (DWORD)0, // Use same stack size.
  153. (LPTHREAD_START_ROUTINE)SockGetServerOut, // Thread procedure.
  154. (LPVOID)NULL, // Parameter to pass.
  155. (DWORD)0, // Run immediately.
  156. (LPDWORD)&tid))==NULL) // Thread identifier.
  157. {
  158. Errormsg(TEXT("Could Not Create rwSrv2Cl Thread"));
  159. return;
  160. }
  161. //
  162. // Start Thread for Client --> Server Flow
  163. //
  164. if ((iothreads[1]=CreateThread((LPSECURITY_ATTRIBUTES)NULL, // No security attributes.
  165. (DWORD)0, // Use same stack size.
  166. (LPTHREAD_START_ROUTINE)SockSendServerInp, // Thread procedure.
  167. (LPVOID)NULL, // Parameter to pass.
  168. (DWORD)0, // Run immediately.
  169. (LPDWORD)&tid))==NULL) // Thread identifier.
  170. {
  171. Errormsg(TEXT("Could Not Create rwSrv2Cl Thread"));
  172. return;
  173. }
  174. WaitForMultipleObjects(2,iothreads,FALSE,INFINITE);
  175. TerminateThread(iothreads[0],1);
  176. TerminateThread(iothreads[1],1);
  177. // _tprintf(TEXT("Calling WSACleanup()....\n"));
  178. nRet = WSACleanup();
  179. _tprintf(TEXT("*** SESSION OVER ***\n"));
  180. }
  181. DWORD
  182. GetServerOut(
  183. PVOID *Noarg
  184. )
  185. {
  186. TCHAR buffin[200];
  187. DWORD dread=0,tmp;
  188. while(ReadFile(ReadPipe,buffin,200,&dread,NULL))
  189. {
  190. if (dread!=0)
  191. {
  192. if (!WriteFile(MyStdOut,buffin,dread,&tmp,NULL))
  193. break;
  194. }
  195. }
  196. return(1);
  197. }
  198. DWORD
  199. SockGetServerOut(
  200. PVOID *Noarg
  201. )
  202. {
  203. TCHAR buffin[200];
  204. DWORD dread=0,tmp;
  205. while(ReadSocket(RWSocket,buffin,200,&dread))
  206. {
  207. if (dread!=0)
  208. {
  209. if (!WriteFile(MyStdOut,buffin,dread,&tmp,NULL))
  210. break;
  211. }
  212. }
  213. return(1);
  214. }
  215. DWORD
  216. SendServerInp(
  217. PVOID *Noarg
  218. )
  219. {
  220. TCHAR buff[200];
  221. DWORD dread,dwrote;
  222. SetLastError(0);
  223. while(ReadFile(MyStdInp,buff,200,&dread,NULL))
  224. {
  225. if (FilterClientInp(buff,dread))
  226. continue;
  227. if (!WriteFile(WritePipe,buff,dread,&dwrote,NULL))
  228. break;
  229. }
  230. return(0);
  231. }
  232. DWORD
  233. SockSendServerInp(
  234. PVOID *Noarg
  235. )
  236. {
  237. TCHAR buff[200];
  238. DWORD dread,dwrote;
  239. SetLastError(0);
  240. while(ReadFile(MyStdInp,buff,200,&dread,NULL))
  241. {
  242. if (SockFilterClientInp(buff,dread))
  243. continue;
  244. if (!WriteSocket(RWSocket,buff,dread,&dwrote))
  245. break;
  246. memset(buff, 0, sizeof(buff));
  247. }
  248. return(0);
  249. }
  250. BOOL
  251. SockSendAuth(
  252. SOCKET s
  253. )
  254. {
  255. TCHAR EncodeBuffer[1024];
  256. TCHAR * pEncodeBuffer;
  257. TCHAR UserBuffer[1024];
  258. // TCHAR * String = UserBuffer;
  259. DWORD dwrote;
  260. int len;
  261. BOOL bRet;
  262. SetLastError(0);
  263. memset(EncodeBuffer, 0, sizeof(EncodeBuffer));
  264. _stprintf( UserBuffer,
  265. TEXT("%s:%s"),
  266. Username,
  267. Password);
  268. pEncodeBuffer = EncodeBuffer + _tcslen(EncodeBuffer);
  269. len = _tcslen(UserBuffer);
  270. Base64Encode(UserBuffer, _tcslen(UserBuffer), pEncodeBuffer);
  271. len = _tcslen(pEncodeBuffer);
  272. bRet = WriteSocket(s,pEncodeBuffer,len,&dwrote);
  273. return TRUE;
  274. }
  275. BOOL
  276. FilterClientInp(
  277. TCHAR *buff,
  278. int count
  279. )
  280. {
  281. if (count==0)
  282. return(TRUE);
  283. if (buff[0]==2) //Adhoc screening of ^B so that i386kd/mipskd
  284. return(TRUE);//do not terminate.
  285. if (buff[0]==COMMANDCHAR)
  286. {
  287. switch (buff[1])
  288. {
  289. case 'k':
  290. case 'K':
  291. case 'q':
  292. case 'Q':
  293. CloseHandle(WritePipe);
  294. return(FALSE);
  295. case 'h':
  296. case 'H':
  297. _tprintf(TEXT("%cM : Send Message\n"),COMMANDCHAR);
  298. _tprintf(TEXT("%cP : Show Popup on Server\n"),COMMANDCHAR);
  299. _tprintf(TEXT("%cS : Status of Server\n"),COMMANDCHAR);
  300. _tprintf(TEXT("%cQ : Quit client\n"),COMMANDCHAR);
  301. _tprintf(TEXT("%cH : This Help\n"),COMMANDCHAR);
  302. return(TRUE);
  303. default:
  304. return(FALSE);
  305. }
  306. }
  307. return(FALSE);
  308. }
  309. BOOL
  310. SockFilterClientInp(
  311. TCHAR *buff,
  312. int count
  313. )
  314. {
  315. int nRet;
  316. if (count==0)
  317. return(TRUE);
  318. if (buff[0]==2) //Adhoc screening of ^B so that i386kd/mipskd
  319. return(TRUE);//do not terminate.
  320. if (buff[0]==COMMANDCHAR)
  321. {
  322. switch (buff[1])
  323. {
  324. case 'k':
  325. case 'K':
  326. case 'q':
  327. case 'Q':
  328. nRet = shutdown(RWSocket, SD_BOTH);
  329. if (nRet == SOCKET_ERROR)
  330. _tprintf(TEXT("** shutdown()..error %d"), WSAGetLastError());
  331. closesocket(RWSocket);
  332. return(FALSE);
  333. case 'h':
  334. case 'H':
  335. _tprintf(TEXT("%cM : Send Message\n"),COMMANDCHAR);
  336. _tprintf(TEXT("%cP : Show Popup on Server\n"),COMMANDCHAR);
  337. _tprintf(TEXT("%cS : Status of Server\n"),COMMANDCHAR);
  338. _tprintf(TEXT("%cQ : Quit client\n"),COMMANDCHAR);
  339. _tprintf(TEXT("%cH : This Help\n"),COMMANDCHAR);
  340. return(TRUE);
  341. default:
  342. return(FALSE);
  343. }
  344. }
  345. return(FALSE);
  346. }
  347. BOOL
  348. Mych(
  349. DWORD ctrlT
  350. )
  351. {
  352. TCHAR c[2];
  353. DWORD tmp;
  354. DWORD send=1;
  355. c[0]=CTRLC;
  356. if (ctrlT==CTRL_C_EVENT)
  357. {
  358. if (!WriteFile(WritePipe,c,send,&tmp,NULL))
  359. {
  360. Errormsg(TEXT("Error Sending ^c\n"));
  361. return(FALSE);
  362. }
  363. return(TRUE);
  364. }
  365. if ((ctrlT==CTRL_BREAK_EVENT)||
  366. (ctrlT==CTRL_CLOSE_EVENT)||
  367. (ctrlT==CTRL_LOGOFF_EVENT)||
  368. (ctrlT==CTRL_SHUTDOWN_EVENT)
  369. )
  370. {
  371. CloseHandle(WritePipe); //Will Shutdown naturally
  372. }
  373. return(FALSE);
  374. }
  375. BOOL
  376. SockMych(
  377. DWORD ctrlT
  378. )
  379. {
  380. TCHAR c[2];
  381. DWORD tmp;
  382. DWORD send=1;
  383. c[0]=CTRLC;
  384. if (ctrlT==CTRL_C_EVENT)
  385. {
  386. if (!WriteSocket(RWSocket,c,send,&tmp))
  387. {
  388. Errormsg(TEXT("Error Sending ^c\n"));
  389. return(FALSE);
  390. }
  391. return(TRUE);
  392. }
  393. if ((ctrlT==CTRL_BREAK_EVENT)||
  394. (ctrlT==CTRL_CLOSE_EVENT)||
  395. (ctrlT==CTRL_LOGOFF_EVENT)||
  396. (ctrlT==CTRL_SHUTDOWN_EVENT)
  397. )
  398. {
  399. CloseHandle(WritePipe); //Will Shutdown naturally
  400. }
  401. return(FALSE);
  402. }
  403. HANDLE*
  404. EstablishSession(
  405. TCHAR *server,
  406. TCHAR *srvpipename
  407. )
  408. {
  409. static HANDLE PipeH[2];
  410. TCHAR pipenameSrvIn[200];
  411. TCHAR pipenameSrvOut[200];
  412. _stprintf(pipenameSrvIn ,SERVER_READ_PIPE ,server,srvpipename);
  413. _stprintf(pipenameSrvOut,SERVER_WRITE_PIPE,server,srvpipename);
  414. if ((INVALID_HANDLE_VALUE==(PipeH[0]=CreateFile(pipenameSrvOut,
  415. GENERIC_READ ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL))) ||
  416. (INVALID_HANDLE_VALUE==(PipeH[1]=CreateFile(pipenameSrvIn ,
  417. GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL)))) {
  418. DWORD Err=GetLastError();
  419. TCHAR msg[128];
  420. Errormsg(TEXT("*** Unable to Connect ***"));
  421. //
  422. // Print a helpful message
  423. //
  424. switch(Err)
  425. {
  426. case 2: _stprintf(msg,TEXT("Invalid PipeName %s"),srvpipename);break;
  427. case 53:_stprintf(msg,TEXT("Server %s not found"),server);break;
  428. default:
  429. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|
  430. FORMAT_MESSAGE_IGNORE_INSERTS,
  431. NULL, Err, 0, msg, 128, NULL);
  432. break;
  433. }
  434. _tprintf(TEXT("Diagnosis:%s\n"),msg);
  435. return(NULL);
  436. }
  437. _tprintf(TEXT("Connected..\n\n"));
  438. SendMyInfo(PipeH);
  439. return(PipeH);
  440. }
  441. SOCKET*
  442. SockEstablishSession(
  443. TCHAR *server,
  444. TCHAR *srvpipename
  445. )
  446. {
  447. static SOCKET Socket;
  448. int nRet;
  449. LPHOSTENT lpHostEntry = NULL;
  450. SOCKADDR_IN sa;
  451. WORD wVersionRequested = MAKEWORD(1,1);
  452. WSADATA wsaData;
  453. unsigned short usPort;
  454. #ifdef UNICODE
  455. int nStrLen;
  456. #endif
  457. //
  458. // Initialize WinSock
  459. //
  460. nRet = WSAStartup(wVersionRequested, &wsaData);
  461. if (nRet)
  462. {
  463. _tprintf(TEXT("Initialize WinSock Failed"));
  464. return NULL;
  465. }
  466. // Check version
  467. if (wsaData.wVersion != wVersionRequested)
  468. {
  469. _tprintf(TEXT("Wrong WinSock Version"));
  470. return NULL;
  471. }
  472. //
  473. // Lookup host
  474. //
  475. #ifdef UNICODE
  476. nStrLen = lstrlen( server );
  477. if (nStrLen)
  478. {
  479. char * pszAnsiStr = (char *)malloc( nStrLen + 1 );
  480. if (pszAnsiStr)
  481. {
  482. int nErr = WideCharToMultiByte( CP_THREAD_ACP,
  483. WC_COMPOSITECHECK,
  484. server,
  485. -1,
  486. pszAnsiStr,
  487. nStrLen,
  488. NULL,
  489. NULL );
  490. if (!nErr)
  491. {
  492. DWORD dwErr = GetLastError();
  493. switch( dwErr )
  494. {
  495. case ERROR_INSUFFICIENT_BUFFER:
  496. _tprintf(TEXT("error: gethostbyname-- WideCharToMultiByte Error: ERROR_INSUFFICIENT_BUFFER"));
  497. break;
  498. case ERROR_INVALID_FLAGS:
  499. _tprintf(TEXT("error: gethostbyname-- WideCharToMultiByte Error: ERROR_INVALID_FLAGS"));
  500. break;
  501. case ERROR_INVALID_PARAMETER:
  502. _tprintf(TEXT("error: gethostbyname-- WideCharToMultiByte Error: ERROR_INVALID_PARAMETER"));
  503. break;
  504. }
  505. free( pszAnsiStr );
  506. return NULL;
  507. }
  508. lpHostEntry = gethostbyname( pszAnsiStr );
  509. free( pszAnsiStr );
  510. }
  511. }
  512. #else
  513. lpHostEntry = gethostbyname( server );
  514. #endif
  515. if (lpHostEntry == NULL)
  516. {
  517. _tprintf(TEXT("wsremote: gethostbyname() error "));
  518. return NULL;
  519. }
  520. //
  521. // Fill in the server address structure
  522. //
  523. sa.sin_family = AF_INET;
  524. sa.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
  525. usPort = (unsigned short)_ttoi( srvpipename );
  526. sa.sin_port = htons(usPort);
  527. //
  528. // Create a TCP/IP stream socket
  529. //
  530. Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  531. if (Socket == INVALID_SOCKET)
  532. {
  533. _tprintf(TEXT("socket()"));
  534. return NULL;
  535. }
  536. //
  537. // Request a connection
  538. //
  539. nRet = connect(Socket,
  540. (LPSOCKADDR)&sa,
  541. sizeof(SOCKADDR_IN));
  542. if (nRet == SOCKET_ERROR)
  543. {
  544. int iWSAErr;
  545. iWSAErr = WSAGetLastError();
  546. _tprintf( TEXT("connect(), Error: %d"), iWSAErr );
  547. return NULL;
  548. }
  549. SockSendMyInfo(Socket);
  550. return(&Socket);
  551. }
  552. VOID
  553. SendMyInfo(
  554. PHANDLE pipeH
  555. )
  556. {
  557. HANDLE rPipe=pipeH[0];
  558. HANDLE wPipe=pipeH[1];
  559. DWORD hostlen=HOSTNAMELEN-1;
  560. WORD BytesToSend=sizeof(SESSION_STARTUPINFO);
  561. DWORD tmp;
  562. SESSION_STARTUPINFO ssi;
  563. SESSION_STARTREPLY ssr;
  564. DWORD BytesToRead;
  565. TCHAR *buff;
  566. ssi.Size=BytesToSend;
  567. ssi.Version=VERSION;
  568. GetComputerName((TCHAR *)ssi.ClientName,&hostlen);
  569. ssi.LinesToSend=LinesToSend;
  570. ssi.Flag=ClientToServerFlag;
  571. {
  572. DWORD NewCode=MAGICNUMBER;
  573. TCHAR Name[15];
  574. _tcscpy(Name,(TCHAR *)ssi.ClientName);
  575. memcpy(&Name[11],(TCHAR *)&NewCode,sizeof(NewCode));
  576. WriteFile(wPipe,(TCHAR *)Name,HOSTNAMELEN-1,&tmp,NULL);
  577. ReadFile(rPipe ,(TCHAR *)&ssr.MagicNumber,sizeof(ssr.MagicNumber),&tmp,NULL);
  578. if (ssr.MagicNumber!=MAGICNUMBER)
  579. {
  580. _tprintf(TEXT("WSREMOTE FAILED TO CONNECT TO SERVER..\n"));
  581. WriteFile(MyStdOut,(TCHAR *)&ssr.MagicNumber,sizeof(ssr.MagicNumber),&tmp,NULL);
  582. return;
  583. }
  584. //Get Rest of the info-its not the old server
  585. ReadFixBytes(rPipe,(TCHAR *)&ssr.Size,sizeof(ssr.Size),0);
  586. ReadFixBytes(rPipe,(TCHAR *)&ssr.FileSize,sizeof(ssr)-sizeof(ssr.FileSize)-sizeof(ssr.MagicNumber),0);
  587. }
  588. if (!WriteFile(wPipe,(TCHAR *)&ssi,BytesToSend,&tmp,NULL))
  589. {
  590. Errormsg(TEXT("INFO Send Error"));
  591. return;
  592. }
  593. BytesToRead=MINIMUM(ssr.FileSize,ssi.LinesToSend*CHARS_PER_LINE);
  594. buff=calloc(BytesToRead+1,1);
  595. if (buff!=NULL)
  596. {
  597. DWORD bytesread=0;
  598. ReadFile(rPipe,buff,BytesToRead,&bytesread,NULL);
  599. WriteFile(MyStdOut,buff,bytesread,&tmp,NULL);
  600. free(buff);
  601. }
  602. }
  603. VOID
  604. SockSendMyInfo(
  605. SOCKET MySocket
  606. )
  607. {
  608. BOOL bRet;
  609. #ifdef UNICODE
  610. char szAnsiName[HOSTNAMELEN];
  611. #endif
  612. DWORD hostlen = HOSTNAMELEN-1;
  613. DWORD BytesToRead;
  614. DWORD tmp;
  615. DWORD NewCode = MAGICNUMBER;
  616. SESSION_STARTUPINFO ssi;
  617. SESSION_STARTREPLY ssr;
  618. int nRet;
  619. TCHAR Name[HOSTNAMELEN];
  620. TCHAR * buff;
  621. WORD BytesToSend = sizeof(SESSION_STARTUPINFO);
  622. ssi.Size=BytesToSend;
  623. ssi.Version=VERSION;
  624. GetComputerName((TCHAR *)ssi.ClientName,&hostlen);
  625. ssi.LinesToSend=LinesToSend;
  626. ssi.Flag=ClientToServerFlag;
  627. bRet = SockSendAuth(MySocket);
  628. // append on magic number
  629. _tcscpy(Name, ssi.ClientName);
  630. #ifdef UNICODE
  631. GetAnsiStr( (TCHAR *)&Name, (char *)&szAnsiName, HOSTNAMELEN );
  632. memcpy(&szAnsiName[11], &NewCode, sizeof(DWORD) );
  633. WriteSocketA( MySocket,(char *)&szAnsiName,HOSTNAMELEN-1,&tmp);
  634. #else
  635. memcpy(&Name[11], &NewCode, sizeof(DWORD) );
  636. WriteSocket( MySocket,(TCHAR *)Name,HOSTNAMELEN-1,&tmp);
  637. #endif
  638. ReadSocket(MySocket ,(TCHAR *)&ssr.MagicNumber,sizeof(ssr.MagicNumber),&tmp);
  639. if (ssr.MagicNumber!=MAGICNUMBER)
  640. {
  641. _tprintf(TEXT("WSREMOTE FAILED TO CONNECT TO SERVER..\n"));
  642. nRet = shutdown(MySocket, SD_BOTH);
  643. if (nRet == SOCKET_ERROR)
  644. _tprintf(TEXT("** shutdown()..error %d"), WSAGetLastError());
  645. closesocket(MySocket);
  646. return;
  647. }
  648. //Get Rest of the info-its not the old server
  649. SockReadFixBytes(MySocket,(TCHAR *)&ssr.Size,sizeof(ssr.Size),0);
  650. SockReadFixBytes(MySocket,(TCHAR *)&ssr.FileSize,sizeof(ssr)-sizeof(ssr.FileSize)-sizeof(ssr.MagicNumber),0);
  651. if (!WriteSocket(MySocket,(TCHAR *)&ssi,BytesToSend,&tmp))
  652. {
  653. _tprintf(TEXT("INFO Send Error"));
  654. return;
  655. }
  656. BytesToRead=MINIMUM(ssr.FileSize,ssi.LinesToSend*CHARS_PER_LINE);
  657. buff=calloc(BytesToRead+1,1);
  658. if (buff!=NULL)
  659. {
  660. DWORD bytesread=0;
  661. ReadSocket(MySocket,buff,BytesToRead,&bytesread);
  662. WriteFile(MyStdOut,buff,bytesread,&tmp,NULL);
  663. free(buff);
  664. }
  665. }