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.

1189 lines
29 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows NT **/
  3. /** Copyright(c) Microsoft Corp., 1993 **/
  4. /**********************************************************************/
  5. /*
  6. sockutil.cxx
  7. This module contains utility routines for managing & manipulating
  8. sockets.
  9. Functions exported by this module:
  10. CreateDataSocket
  11. CreateFtpdSocket
  12. CloseSocket
  13. ResetSocket
  14. AcceptSocket
  15. SockSend
  16. SockPrintf2
  17. ReplyToUser()
  18. SockReadLine
  19. SockMultilineMessage2
  20. FILE HISTORY:
  21. KeithMo 07-Mar-1993 Created.
  22. MuraliK April-1995 Misc modifications (removed usage of various
  23. socket functions/modified them)
  24. */
  25. #include "ftpdp.hxx"
  26. //
  27. // Private constants.
  28. //
  29. #define DEFAULT_BUFFER_SIZE 4096 // bytes
  30. //
  31. // Private globals.
  32. //
  33. //
  34. // Private prototypes.
  35. //
  36. SOCKERR
  37. vSockPrintf(
  38. LPUSER_DATA pUserData,
  39. SOCKET sock,
  40. LPCSTR pszFormat,
  41. va_list args
  42. );
  43. SOCKERR
  44. vSockReply(
  45. LPUSER_DATA pUserData,
  46. SOCKET sock,
  47. UINT ReplyCode,
  48. CHAR chSeparator,
  49. LPCSTR pszFormat,
  50. va_list args
  51. );
  52. BOOL
  53. vTelnetEscapeIAC(
  54. PCHAR pszBuffer,
  55. PINT pcchBufChars,
  56. INT ccbMaxLen
  57. );
  58. //
  59. // Public functions.
  60. //
  61. /*******************************************************************
  62. NAME: CreateDataSocket
  63. SYNOPSIS: Creates a data socket for the specified address & port.
  64. ENTRY: psock - Will receive the new socket ID if successful.
  65. addrLocal - The local Internet address for the socket
  66. in network byte order.
  67. portLocal - The local port for the socket in network
  68. byte order.
  69. addrRemote - The remote Internet address for the socket
  70. in network byte order.
  71. portRemote - The remote port for the socket in network
  72. byte order.
  73. RETURNS: SOCKERR - 0 if successful, !0 if not.
  74. HISTORY:
  75. KeithMo 10-Mar-1993 Created.
  76. KeithMo 07-Sep-1993 Enable SO_REUSEADDR.
  77. ********************************************************************/
  78. SOCKERR
  79. CreateDataSocket(
  80. SOCKET * psock,
  81. ULONG addrLocal,
  82. PORT portLocal,
  83. ULONG addrRemote,
  84. PORT portRemote
  85. )
  86. {
  87. SOCKET sNew = INVALID_SOCKET;
  88. SOCKERR serr = 0;
  89. SOCKADDR_IN sockAddr;
  90. //
  91. // Just to be paranoid...
  92. //
  93. DBG_ASSERT( psock != NULL );
  94. *psock = INVALID_SOCKET;
  95. //
  96. // Create the socket.
  97. //
  98. sNew = WSASocketW(AF_INET,
  99. SOCK_STREAM,
  100. IPPROTO_TCP,
  101. NULL,
  102. 0,
  103. WSA_FLAG_OVERLAPPED);
  104. serr = ( sNew == INVALID_SOCKET ) ? WSAGetLastError() : 0;
  105. if( serr == 0 )
  106. {
  107. BOOL fReuseAddr = TRUE;
  108. //
  109. // Since we always bind to the same local port,
  110. // allow the reuse of address/port pairs.
  111. //
  112. if( setsockopt( sNew,
  113. SOL_SOCKET,
  114. SO_REUSEADDR,
  115. (CHAR *)&fReuseAddr,
  116. sizeof(fReuseAddr) ) != 0 )
  117. {
  118. serr = WSAGetLastError();
  119. }
  120. }
  121. if( serr == 0 )
  122. {
  123. //
  124. // Bind the local internet address & port to the socket.
  125. //
  126. sockAddr.sin_family = AF_INET;
  127. sockAddr.sin_addr.s_addr = addrLocal;
  128. sockAddr.sin_port = portLocal;
  129. if( bind( sNew, (SOCKADDR *)&sockAddr, sizeof(sockAddr) ) != 0 )
  130. {
  131. serr = WSAGetLastError();
  132. }
  133. }
  134. if( serr == 0 )
  135. {
  136. //
  137. // Connect to the remote internet address & port.
  138. //
  139. sockAddr.sin_family = AF_INET;
  140. sockAddr.sin_addr.s_addr = addrRemote;
  141. sockAddr.sin_port = portRemote;
  142. if( connect( sNew, (SOCKADDR *)&sockAddr, sizeof(sockAddr) ) != 0 )
  143. {
  144. serr = WSAGetLastError();
  145. }
  146. }
  147. if( serr == 0 )
  148. {
  149. //
  150. // Success! Return the socket to the caller.
  151. //
  152. DBG_ASSERT( sNew != INVALID_SOCKET );
  153. *psock = sNew;
  154. IF_DEBUG( SOCKETS )
  155. {
  156. DBGPRINTF(( DBG_CONTEXT,
  157. "data socket %d connected from (%08lX,%04X) to (%08lX,%04X)\n",
  158. sNew,
  159. ntohl( addrLocal ),
  160. ntohs( portLocal ),
  161. ntohl( addrRemote ),
  162. ntohs( portRemote ) ));
  163. }
  164. }
  165. else
  166. {
  167. //
  168. // Something fatal happened. Close the socket if
  169. // managed to actually open it.
  170. //
  171. IF_DEBUG( SOCKETS )
  172. {
  173. DBGPRINTF(( DBG_CONTEXT,
  174. "no data socket from (%08lX,%04X) to (%08lX, %04X), error %d\n",
  175. ntohl( addrLocal ),
  176. ntohs( portLocal ),
  177. ntohl( addrRemote ),
  178. ntohs( portRemote ),
  179. serr ));
  180. }
  181. if( sNew != INVALID_SOCKET )
  182. {
  183. ResetSocket( sNew );
  184. }
  185. }
  186. return serr;
  187. } // CreateDataSocket
  188. /*******************************************************************
  189. NAME: CreateFtpdSocket
  190. SYNOPSIS: Creates a new socket at the FTPD port.
  191. This will be used by the passive data transfer.
  192. ENTRY: psock - Will receive the new socket ID if successful.
  193. addrLocal - The lcoal Internet address for the socket
  194. in network byte order.
  195. RETURNS: SOCKERR - 0 if successful, !0 if not.
  196. HISTORY:
  197. KeithMo 08-Mar-1993 Created.
  198. ********************************************************************/
  199. SOCKERR
  200. CreateFtpdSocket(
  201. SOCKET * psock,
  202. ULONG addrLocal,
  203. FTP_SERVER_INSTANCE *pInstance
  204. )
  205. {
  206. SOCKET sNew = INVALID_SOCKET;
  207. SOCKERR serr = 0;
  208. PORT DbgBindPort = 0;
  209. //
  210. // Just to be paranoid...
  211. //
  212. DBG_ASSERT( psock != NULL );
  213. *psock = INVALID_SOCKET;
  214. //
  215. // Create the connection socket.
  216. //
  217. sNew = WSASocketW(AF_INET,
  218. SOCK_STREAM,
  219. IPPROTO_TCP,
  220. NULL,
  221. 0,
  222. WSA_FLAG_OVERLAPPED);
  223. serr = ( sNew == INVALID_SOCKET ) ? WSAGetLastError() : 0;
  224. if( serr == 0 )
  225. {
  226. BOOL fReuseAddr = FALSE;
  227. //
  228. // Muck around with the socket options a bit.
  229. // Berkeley FTPD does this.
  230. //
  231. if( setsockopt( sNew,
  232. SOL_SOCKET,
  233. SO_REUSEADDR,
  234. (CHAR *)&fReuseAddr,
  235. sizeof(fReuseAddr) ) != 0 )
  236. {
  237. serr = WSAGetLastError();
  238. }
  239. }
  240. if( serr == 0 )
  241. {
  242. FTP_PASV_PORT PasvPort;
  243. SOCKADDR_IN sockAddr;
  244. //
  245. // Bind an address to the socket.
  246. //
  247. sockAddr.sin_family = AF_INET;
  248. sockAddr.sin_addr.s_addr = addrLocal;
  249. sockAddr.sin_port = htons(PasvPort.GetPort());
  250. //
  251. // either we bind to a port in the dynamic range (1024-5000), or itterate
  252. // through the range of configured ports. If we are using the default
  253. // system managed range, then GetPort() will always return 0, and we only
  254. // run through the loop once. otherwise, GetPort() will return different ports
  255. // scanning the port range.
  256. //
  257. do {
  258. if (bind( sNew, (SOCKADDR *)&sockAddr, sizeof(sockAddr) ) == 0) {
  259. serr = 0;
  260. DbgBindPort = sockAddr.sin_port;
  261. break;
  262. }
  263. } while( ((serr = WSAGetLastError()) == WSAEADDRINUSE) &&
  264. (sockAddr.sin_port = htons(PasvPort.GetPort())) != 0);
  265. }
  266. if( serr == 0 )
  267. {
  268. //
  269. // Put the socket into listen mode.
  270. //
  271. if( listen( sNew, (INT)pInstance->NumListenBacklog()) != 0 )
  272. {
  273. serr = WSAGetLastError();
  274. }
  275. }
  276. if( serr == 0 )
  277. {
  278. //
  279. // Success! Return the socket to the caller.
  280. //
  281. DBG_ASSERT( sNew != INVALID_SOCKET );
  282. *psock = sNew;
  283. IF_DEBUG( SOCKETS )
  284. {
  285. DBGPRINTF(( DBG_CONTEXT,
  286. "connection socket %d created at (%08lX,%04X)\n",
  287. sNew,
  288. ntohl( addrLocal ),
  289. ntohs( DbgBindPort ) ));
  290. }
  291. }
  292. else
  293. {
  294. //
  295. // Something fatal happened. Close the socket if
  296. // managed to actually open it.
  297. //
  298. IF_DEBUG( SOCKETS )
  299. {
  300. DBGPRINTF(( DBG_CONTEXT,
  301. "no connection socket at (%08lX, %04X), error %d\n",
  302. ntohl( addrLocal ),
  303. ntohs( DbgBindPort ),
  304. serr ));
  305. }
  306. if( sNew != INVALID_SOCKET )
  307. {
  308. ResetSocket( sNew );
  309. }
  310. }
  311. return serr;
  312. } // CreateFtpdSocket
  313. /*******************************************************************
  314. NAME: CloseSocket
  315. SYNOPSIS: Closes the specified socket. This is just a thin
  316. wrapper around the "real" closesocket() API.
  317. ENTRY: sock - The socket to close.
  318. RETURNS: SOCKERR - 0 if successful, !0 if not.
  319. HISTORY:
  320. KeithMo 26-Apr-1993 Created.
  321. ********************************************************************/
  322. SOCKERR
  323. CloseSocket(
  324. SOCKET sock
  325. )
  326. {
  327. SOCKERR serr = 0;
  328. //
  329. // Close the socket.
  330. //
  331. if( closesocket( sock ) != 0 )
  332. {
  333. serr = WSAGetLastError();
  334. }
  335. IF_DEBUG( SOCKETS )
  336. {
  337. if( serr == 0 )
  338. {
  339. DBGPRINTF(( DBG_CONTEXT,
  340. "closed socket %d\n",
  341. sock ));
  342. }
  343. else
  344. {
  345. DBGPRINTF(( DBG_CONTEXT,
  346. "cannot close socket %d, error %d\n",
  347. sock,
  348. serr ));
  349. }
  350. }
  351. return serr;
  352. } // CloseSocket
  353. /*******************************************************************
  354. NAME: ResetSocket
  355. SYNOPSIS: Performs a "hard" close on the given socket.
  356. ENTRY: sock - The socket to close.
  357. RETURNS: SOCKERR - 0 if successful, !0 if not.
  358. HISTORY:
  359. KeithMo 08-Mar-1993 Created.
  360. ********************************************************************/
  361. SOCKERR
  362. ResetSocket(
  363. SOCKET sock
  364. )
  365. {
  366. SOCKERR serr = 0;
  367. LINGER linger;
  368. //
  369. // Enable linger with a timeout of zero. This will
  370. // force the hard close when we call closesocket().
  371. //
  372. // We ignore the error return from setsockopt. If it
  373. // fails, we'll just try to close the socket anyway.
  374. //
  375. linger.l_onoff = TRUE;
  376. linger.l_linger = 0;
  377. setsockopt( sock,
  378. SOL_SOCKET,
  379. SO_LINGER,
  380. (CHAR *)&linger,
  381. sizeof(linger) );
  382. //
  383. // Close the socket.
  384. //
  385. if( closesocket( sock ) != 0 )
  386. {
  387. serr = WSAGetLastError();
  388. }
  389. IF_DEBUG( SOCKETS )
  390. {
  391. if( serr == 0 )
  392. {
  393. DBGPRINTF(( DBG_CONTEXT,
  394. "reset socket %d\n",
  395. sock ));
  396. }
  397. else
  398. {
  399. DBGPRINTF(( DBG_CONTEXT,
  400. "cannot reset socket %d, error %d\n",
  401. sock,
  402. serr ));
  403. }
  404. }
  405. return serr;
  406. } // ResetSocket
  407. /*******************************************************************
  408. NAME: AcceptSocket
  409. SYNOPSIS: Waits for a connection to the specified socket.
  410. The socket is assumed to be "listening".
  411. ENTRY: sockListen - The socket to accept on.
  412. psockNew - Will receive the newly "accepted" socket
  413. if successful.
  414. paddr - Will receive the client's network address.
  415. fEnforceTimeout - If TRUE, this routine will enforce
  416. the idle-client timeout. If FALSE, no timeouts
  417. are enforced (and this routine may block
  418. indefinitely).
  419. RETURNS: SOCKERR - 0 if successful, !0 if not.
  420. HISTORY:
  421. KeithMo 27-Apr-1993 Created.
  422. ********************************************************************/
  423. SOCKERR
  424. AcceptSocket(
  425. SOCKET sockListen,
  426. SOCKET * psockNew,
  427. LPSOCKADDR_IN paddr,
  428. BOOL fEnforceTimeout,
  429. FTP_SERVER_INSTANCE *pInstance
  430. )
  431. {
  432. SOCKERR serr = 0;
  433. SOCKET sockNew = INVALID_SOCKET;
  434. BOOL fRead = FALSE;
  435. DBG_ASSERT( psockNew != NULL );
  436. DBG_ASSERT( paddr != NULL );
  437. if( fEnforceTimeout ) {
  438. //
  439. // Timeouts are to be enforced, so wait for a connection
  440. // to the socket.
  441. //
  442. serr = WaitForSocketWorker(
  443. sockListen,
  444. INVALID_SOCKET,
  445. &fRead,
  446. NULL,
  447. pInstance->QueryConnectionTimeout()
  448. );
  449. }
  450. if( serr == 0 )
  451. {
  452. INT cbAddr = sizeof(SOCKADDR_IN);
  453. //
  454. // Wait for the actual connection.
  455. //
  456. sockNew = accept( sockListen, (SOCKADDR *)paddr, &cbAddr );
  457. if( sockNew == INVALID_SOCKET )
  458. {
  459. serr = WSAGetLastError();
  460. }
  461. }
  462. //
  463. // Return the (potentially invalid) socket to the caller.
  464. //
  465. *psockNew = sockNew;
  466. return serr;
  467. } // AcceptSocket
  468. /*******************************************************************
  469. NAME: SockSend
  470. SYNOPSIS: Sends a block of bytes to a specified socket.
  471. ENTRY: pUserData - The user initiating the request.
  472. sock - The target socket.
  473. pBuffer - Contains the data to send.
  474. cbBuffer - The size (in bytes) of the buffer.
  475. RETURNS: SOCKERR - 0 if successful, !0 if not.
  476. HISTORY:
  477. KeithMo 13-Mar-1993 Created.
  478. ********************************************************************/
  479. SOCKERR
  480. SockSend(
  481. LPUSER_DATA pUserData,
  482. SOCKET sock,
  483. LPVOID pBuffer,
  484. DWORD cbBuffer
  485. )
  486. {
  487. SOCKERR serr = 0;
  488. INT cbSent;
  489. DWORD dwBytesSent = 0;
  490. DBG_ASSERT( pBuffer != NULL );
  491. //
  492. // Loop until there's no more data to send.
  493. //
  494. while( cbBuffer > 0 ) {
  495. //
  496. // Wait for the socket to become writeable.
  497. //
  498. BOOL fWrite = FALSE;
  499. serr = WaitForSocketWorker(
  500. INVALID_SOCKET,
  501. sock,
  502. NULL,
  503. &fWrite,
  504. (pUserData != NULL) ?
  505. pUserData->QueryInstance()->QueryConnectionTimeout():
  506. FTP_DEF_SEND_TIMEOUT
  507. );
  508. if( serr == 0 )
  509. {
  510. //
  511. // Write a block to the socket.
  512. //
  513. cbSent = send( sock, (CHAR *)pBuffer, (INT)cbBuffer, 0 );
  514. if( cbSent < 0 )
  515. {
  516. //
  517. // Socket error.
  518. //
  519. serr = WSAGetLastError();
  520. }
  521. else
  522. {
  523. dwBytesSent += (DWORD)cbSent;
  524. IF_DEBUG( SEND )
  525. {
  526. if( pUserData && TEST_UF( pUserData, TRANSFER ) )
  527. {
  528. DBGPRINTF(( DBG_CONTEXT,
  529. "send %d bytes @%p to socket %d\n",
  530. cbSent,
  531. pBuffer,
  532. sock ));
  533. }
  534. }
  535. }
  536. }
  537. // added a check for special case when we are sending and thinking that we are sending
  538. // synchronoulsy on socket which was set to non blocking mode. In that case when buffer space
  539. // in winsock becomes exhausted send return WSAEWOULDBLOCK. So then we just retry.
  540. if ( serr != WSAEWOULDBLOCK )
  541. {
  542. if( serr != 0 )
  543. {
  544. break;
  545. }
  546. pBuffer = (LPVOID)( (LPBYTE)pBuffer + cbSent );
  547. cbBuffer -= (DWORD)cbSent;
  548. }
  549. }
  550. if( serr != 0 )
  551. {
  552. IF_DEBUG( SEND )
  553. {
  554. DBGPRINTF(( DBG_CONTEXT,
  555. "socket error %d during send on socket %d.\n",
  556. serr,
  557. sock));
  558. }
  559. }
  560. if( pUserData != NULL ) {
  561. pUserData->QueryInstance()->QueryStatsObj()->UpdateTotalBytesSent(
  562. dwBytesSent );
  563. }
  564. return serr;
  565. } // SockSend
  566. /*******************************************************************
  567. NAME: SockPrintf2
  568. SYNOPSIS: Send a formatted string to a specific socket.
  569. ENTRY: pUserData - The user initiating the request.
  570. sock - The target socket.
  571. pszFormat - A printf-style format string.
  572. ... - Any other parameters needed by the format string.
  573. RETURNS: SOCKERR - 0 if successful, !0 if not.
  574. HISTORY:
  575. KeithMo 10-Mar-1993 Created.
  576. ********************************************************************/
  577. SOCKERR
  578. __cdecl
  579. SockPrintf2(
  580. LPUSER_DATA pUserData,
  581. SOCKET sock,
  582. LPCSTR pszFormat,
  583. ...
  584. )
  585. {
  586. va_list ArgPtr;
  587. SOCKERR serr;
  588. //
  589. // Let the worker do the dirty work.
  590. //
  591. va_start( ArgPtr, pszFormat );
  592. serr = vSockPrintf( pUserData,
  593. sock,
  594. pszFormat,
  595. ArgPtr );
  596. va_end( ArgPtr );
  597. return serr;
  598. } // SockPrintf2
  599. SOCKERR
  600. __cdecl
  601. ReplyToUser(
  602. IN LPUSER_DATA pUserData,
  603. IN UINT ReplyCode,
  604. IN LPCSTR pszFormat,
  605. ...
  606. )
  607. /*++
  608. This function sends an FTP reply to the user data object. The reply
  609. is usually sent over the control socket.
  610. Arguments:
  611. pUserData pointer to UserData object initiating the reply
  612. ReplyCode One of the REPLY_* manifests.
  613. pszFormat pointer to null-terminated string containing the format
  614. ... additional paramters if any required.
  615. Returns:
  616. SOCKET error code. 0 on success and !0 on failure.
  617. History:
  618. MuraliK
  619. --*/
  620. {
  621. va_list ArgPtr;
  622. SOCKERR serr;
  623. DBG_ASSERT( pUserData != NULL);
  624. pUserData->SetLastReplyCode( ReplyCode );
  625. if ( pUserData->QueryControlSocket() != INVALID_SOCKET) {
  626. //
  627. // Let the worker do the dirty work.
  628. //
  629. va_start( ArgPtr, pszFormat );
  630. serr = vSockReply( pUserData,
  631. pUserData->QueryControlSocket(),
  632. ReplyCode,
  633. ' ',
  634. pszFormat,
  635. ArgPtr );
  636. va_end( ArgPtr );
  637. } else {
  638. serr = WSAECONNABORTED;
  639. }
  640. return serr;
  641. } // ReplyToUser()
  642. // Private functions
  643. /*******************************************************************
  644. NAME: vSockPrintf
  645. SYNOPSIS: Worker function for printf-to-socket functions.
  646. ENTRY: pUserData - The user initiating the request.
  647. sock - The target socket.
  648. pszFormat - The format string.
  649. args - Variable number of arguments.
  650. RETURNS: SOCKERR - 0 if successful, !0 if not.
  651. HISTORY:
  652. KeithMo 17-Mar-1993 Created.
  653. ********************************************************************/
  654. SOCKERR
  655. vSockPrintf(
  656. LPUSER_DATA pUserData,
  657. SOCKET sock,
  658. LPCSTR pszFormat,
  659. va_list args
  660. )
  661. {
  662. INT cchBuffer = 0;
  663. INT buffMaxLen;
  664. SOCKERR serr = 0;
  665. CHAR szBuffer[MAX_REPLY_LENGTH];
  666. DBG_ASSERT( pszFormat != NULL );
  667. //
  668. // Render the format into our local buffer.
  669. //
  670. DBG_ASSERT( MAX_REPLY_LENGTH > 3);
  671. buffMaxLen = sizeof(szBuffer) - 3;
  672. cchBuffer = _vsnprintf( szBuffer,
  673. buffMaxLen,
  674. pszFormat, args );
  675. //
  676. // The string length is long, we get back -1.
  677. // so we get the string length for partial data.
  678. //
  679. if ( (cchBuffer < 0) || (cchBuffer >= buffMaxLen) ) {
  680. //
  681. // terminate the string properly,
  682. // since _vsnprintf() does not terminate properly on failure.
  683. //
  684. cchBuffer = buffMaxLen;
  685. szBuffer[ buffMaxLen] = '\0';
  686. }
  687. IF_DEBUG( SOCKETS ) {
  688. DBGPRINTF(( DBG_CONTEXT, "sending '%s'\n", szBuffer ));
  689. }
  690. //
  691. // Escape all telnet IAC bytes with a second IAC
  692. //
  693. vTelnetEscapeIAC( szBuffer, &cchBuffer, MAX_REPLY_LENGTH - 3 );
  694. strcpy( szBuffer + cchBuffer, "\r\n" );
  695. cchBuffer += 2;
  696. //
  697. // Blast it out to the client.
  698. //
  699. serr = SockSend( pUserData, sock, szBuffer, cchBuffer );
  700. return serr;
  701. } // vSockPrintf
  702. /*******************************************************************
  703. NAME: vSockReply
  704. SYNOPSIS: Worker function for reply functions.
  705. ENTRY: pUserData - The user initiating the request.
  706. sock - The target socket.
  707. ReplyCode - A three digit reply code from RFC 959.
  708. chSeparator - Should be either ' ' (normal reply) or
  709. '-' (first line of multi-line reply).
  710. pszFormat - The format string.
  711. args - Variable number of arguments.
  712. RETURNS: SOCKERR - 0 if successful, !0 if not.
  713. HISTORY:
  714. KeithMo 17-Mar-1993 Created.
  715. ********************************************************************/
  716. SOCKERR
  717. vSockReply(
  718. LPUSER_DATA pUserData,
  719. SOCKET sock,
  720. UINT ReplyCode,
  721. CHAR chSeparator,
  722. LPCSTR pszFormat,
  723. va_list args
  724. )
  725. {
  726. INT cchBuffer;
  727. INT cchBuffer1;
  728. INT buffMaxLen;
  729. SOCKERR serr = 0;
  730. CHAR szBuffer[MAX_REPLY_LENGTH];
  731. DBG_ASSERT( ( ReplyCode >= 100 ) && ( ReplyCode < 600 ) );
  732. //
  733. // Render the format into our local buffer.
  734. //
  735. cchBuffer = _snprintf( szBuffer, sizeof( szBuffer ),
  736. "%u%c",
  737. ReplyCode,
  738. chSeparator );
  739. DBG_ASSERT( cchBuffer > 0);
  740. DBG_ASSERT( MAX_REPLY_LENGTH > cchBuffer + 3);
  741. buffMaxLen = MAX_REPLY_LENGTH - cchBuffer - 3;
  742. cchBuffer1 = _vsnprintf( szBuffer + cchBuffer,
  743. buffMaxLen,
  744. pszFormat, args );
  745. //
  746. // The string length is long, we get back -1.
  747. // so we get the string length for partial data.
  748. //
  749. if ( (cchBuffer1 < 0) || (cchBuffer1 > buffMaxLen) ) {
  750. //
  751. // terminate the string properly,
  752. // since _vsnprintf() does not terminate properly on failure.
  753. //
  754. cchBuffer = buffMaxLen;
  755. szBuffer[ buffMaxLen] = '\0';
  756. } else {
  757. cchBuffer += cchBuffer1;
  758. }
  759. IF_DEBUG( SOCKETS ) {
  760. DBGPRINTF(( DBG_CONTEXT, "sending '%s'\n",szBuffer ));
  761. }
  762. //
  763. // Escape all telnet IAC bytes with a second IAC
  764. //
  765. vTelnetEscapeIAC( szBuffer, &cchBuffer, MAX_REPLY_LENGTH - 3 );
  766. strcpy( szBuffer + cchBuffer, "\r\n" );
  767. cchBuffer += 2;
  768. //
  769. // Blast it out to the client.
  770. //
  771. serr = SockSend( pUserData, sock, szBuffer, cchBuffer );
  772. return serr;
  773. } // vSockReply
  774. DWORD
  775. FtpFormatResponseMessage( IN UINT uiReplyCode,
  776. IN LPCTSTR pszReplyMsg,
  777. OUT LPTSTR pszReplyBuffer,
  778. IN INT cchReplyBuffer)
  779. /*++
  780. This function formats the message to be sent to the client,
  781. given the reply code and the message to be sent.
  782. The formatting function takes care of the reply buffer length
  783. and ensures the safe write of data. If the reply buffer is
  784. not sufficient to hold the entire message, the reply msg is trunctaed.
  785. Arguments:
  786. uiReplyCode reply code to be used.
  787. pszReplyMsg pointer to string containing the reply message
  788. pszReplyBuffer pointer to character buffer where the reply message
  789. can be sent.
  790. cchReplyBuffer length of reply buffer.
  791. Returns:
  792. length of the data written to the reply buffer.
  793. --*/
  794. {
  795. DBG_ASSERT( pszReplyMsg != NULL && pszReplyBuffer != NULL);
  796. DBG_ASSERT( cchReplyBuffer > sizeof("NNN \r\n"));
  797. INT len, pos, maxlen;
  798. pos = _snprintf( pszReplyBuffer, cchReplyBuffer,
  799. "%u ",
  800. uiReplyCode);
  801. DBG_ASSERT( pos >= 4);
  802. len = lstrlen( pszReplyMsg);
  803. maxlen = cchReplyBuffer - pos - sizeof("\r\n");
  804. if (len > maxlen) {
  805. len = maxlen;
  806. }
  807. strncpy(pszReplyBuffer + pos, pszReplyMsg, len);
  808. strcpy(pszReplyBuffer + pos + len, "\r\n");
  809. len += pos + 2;
  810. DBG_ASSERT( len == lstrlen(pszReplyBuffer) );
  811. DBG_ASSERT( len <= cchReplyBuffer );
  812. return (len);
  813. } // FtpFormatResponseMessage()
  814. /*******************************************************************
  815. NAME: vTelnetEscapeIAC
  816. SYNOPSIS: replace (in place) all 0xFF bytes in a buffer with 0xFF 0xFF.
  817. This is the TELNET escape sequence for an IAC value data byte.
  818. ENTRY: pszBuffer - data buffer.
  819. pcchBufChars - on entry, current number of chars in buffer.
  820. on return, number of chars in buffer.
  821. cchMaxLen - maximum characters in the output buffer
  822. RETURNS: TRUE - success, FALSE - overflow.
  823. HISTORY:
  824. RobSol 25-April-2001 Created.
  825. ********************************************************************/
  826. BOOL
  827. vTelnetEscapeIAC( IN OUT PCHAR pszBuffer,
  828. IN OUT PINT pcchBufChars,
  829. IN INT cchMaxLen)
  830. {
  831. # define CHAR_IAC ((CHAR)(-1))
  832. PCHAR pszFirstIAC;
  833. PCHAR pSrc, pDst;
  834. BOOL fReturn = TRUE;
  835. CHAR szBuf[MAX_REPLY_LENGTH];
  836. INT cCharsInDst;
  837. DBG_ASSERT( pszBuffer );
  838. DBG_ASSERT( pcchBufChars );
  839. DBG_ASSERT( cchMaxLen <= MAX_REPLY_LENGTH );
  840. if ((pszFirstIAC = strchr( pszBuffer, CHAR_IAC)) == NULL) {
  841. //
  842. // no IAC - return.
  843. //
  844. return TRUE;
  845. }
  846. //
  847. // we'll expand the string into a temp buffer, then copy back.
  848. //
  849. pSrc = pszFirstIAC;
  850. pDst = szBuf;
  851. cCharsInDst = DIFF(pszFirstIAC - pszBuffer);
  852. do {
  853. if (*pSrc == CHAR_IAC) {
  854. //
  855. // this is a char to escape
  856. //
  857. if ((cCharsInDst + 1) < cchMaxLen) {
  858. //
  859. // we have space to escape the char, so do it.
  860. //
  861. cCharsInDst++;
  862. *pDst++ = CHAR_IAC;
  863. } else {
  864. //
  865. // overflow.
  866. //
  867. fReturn = FALSE;
  868. break;
  869. }
  870. }
  871. *pDst++ = *pSrc++;
  872. } while ((++cCharsInDst < cchMaxLen) && (*pSrc != '\0'));
  873. //
  874. // copy the expanded data back into the input buffer and terminate the string
  875. //
  876. memcpy( pszFirstIAC, szBuf, pDst - szBuf);
  877. pszBuffer[ cCharsInDst ] = '\0';
  878. *pcchBufChars = cCharsInDst;
  879. return fReturn;
  880. }
  881. /************************ End of File ******************************/