Parses a response string and extracts the ip address and port information. @param[in] strResponse The response string of a FTP server which holds the ip address and port information. @param[out] ulIpAddress Buffer for the ip address. @param[out] ushPort Buffer for the port information. @retval true Everything went ok. @retval false An error occurred (invalid format).
| 1347 | /// @retval true Everything went ok. |
| 1348 | /// @retval false An error occurred (invalid format). |
| 1349 | bool CFTPClient::GetIpAddressFromResponse(const tstring& strResponse, ULONG& ulIpAddress, USHORT& ushPort) const |
| 1350 | { |
| 1351 | // parsing of ip-address and port implemented with a finite state machine |
| 1352 | // ...(192,168,1,1,3,44)... |
| 1353 | enum T_enState { state0, state1, state2, state3, state4 } enState = state0; |
| 1354 | |
| 1355 | tstring strIpAddress, strPort; |
| 1356 | USHORT ushTempPort = 0; |
| 1357 | ULONG ulTempIpAddress = 0; |
| 1358 | int iCommaCnt = 4; |
| 1359 | for( tstring::const_iterator it=strResponse.begin(); it!=strResponse.end(); ++it ) |
| 1360 | { |
| 1361 | switch( enState ) |
| 1362 | { |
| 1363 | case state0: |
| 1364 | if( *it == _T('(') ) |
| 1365 | enState = state1; |
| 1366 | break; |
| 1367 | case state1: |
| 1368 | if( *it == _T(',') ) |
| 1369 | { |
| 1370 | if( --iCommaCnt == 0 ) |
| 1371 | { |
| 1372 | enState = state2; |
| 1373 | ulTempIpAddress += CCnv::TStringToLong(strIpAddress.c_str()); |
| 1374 | } |
| 1375 | else |
| 1376 | { |
| 1377 | ulTempIpAddress += CCnv::TStringToLong(strIpAddress.c_str())<<8*iCommaCnt; |
| 1378 | strIpAddress.clear(); |
| 1379 | } |
| 1380 | } |
| 1381 | else |
| 1382 | { |
| 1383 | if( !tisdigit(*it) ) |
| 1384 | return false; |
| 1385 | strIpAddress += *it; |
| 1386 | } |
| 1387 | break; |
| 1388 | case state2: |
| 1389 | if( *it == _T(',') ) |
| 1390 | { |
| 1391 | ushTempPort = static_cast<USHORT>(CCnv::TStringToLong(strPort.c_str())<<8); |
| 1392 | strPort.clear(); |
| 1393 | enState = state3; |
| 1394 | } |
| 1395 | else |
| 1396 | { |
| 1397 | if( !tisdigit(*it) ) |
| 1398 | return false; |
| 1399 | strPort += *it; |
| 1400 | } |
| 1401 | break; |
| 1402 | case state3: |
| 1403 | if( *it == _T(')') ) |
| 1404 | { |
| 1405 | // compiler warning if using +=operator |
| 1406 | ushTempPort = ushTempPort + static_cast<USHORT>(CCnv::TStringToLong(strPort.c_str())); |