home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / wfc007.000 / src / cras.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-22  |  15.6 KB  |  722 lines

  1. #include <wfc.h>
  2. #pragma hdrstop
  3.  
  4. /*
  5. ** Author: Samuel R. Blackburn
  6. ** CI$: 76300,326
  7. ** Internet: sammy@sed.csc.com
  8. **
  9. ** You can use it any way you like.
  10. */
  11.  
  12. #if defined( _DEBUG )
  13. #undef THIS_FILE
  14. static char BASED_CODE THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. /*
  18. ** CRASAuthenticationMessageBlock
  19. */
  20.  
  21. CRASAuthenticationMessageBlock::CRASAuthenticationMessageBlock()
  22. {
  23.    m_Initialize();
  24. }
  25.  
  26. CRASAuthenticationMessageBlock::CRASAuthenticationMessageBlock( const RASAMB *source )
  27. {
  28.    m_Initialize();
  29.    Copy( source );
  30. }
  31.  
  32. CRASAuthenticationMessageBlock::CRASAuthenticationMessageBlock( const CRASAuthenticationMessageBlock& source )
  33. {
  34.    m_Initialize();
  35.    Copy( source );
  36. }
  37.  
  38. CRASAuthenticationMessageBlock::~CRASAuthenticationMessageBlock()
  39. {
  40.    m_Initialize();
  41. }
  42.  
  43. void CRASAuthenticationMessageBlock::m_Initialize( void )
  44. {
  45.    ::ZeroMemory( (RASAMB *) this, sizeof( RASAMB ) );
  46.    dwSize = sizeof( RASAMB );
  47. }
  48.  
  49. void CRASAuthenticationMessageBlock::Copy( const RASAMB *source )
  50. {
  51.    ASSERT( source != NULL );
  52.  
  53.    if ( source == NULL )
  54.    {
  55.       m_Initialize();
  56.       return;
  57.    }
  58.  
  59.    dwError = source->dwError;
  60.    bLana   = source->bLana;
  61.    ::strcpy( szNetBiosError, source->szNetBiosError );
  62. }
  63.  
  64. void CRASAuthenticationMessageBlock::Copy( const CRASAuthenticationMessageBlock& source )
  65. {
  66.    dwError = source.dwError;
  67.    bLana   = source.bLana;
  68.    ::strcpy( szNetBiosError, source.szNetBiosError );
  69. }
  70.  
  71. BYTE CRASAuthenticationMessageBlock::GetLana( void ) const
  72. {
  73.    return( bLana );
  74. }
  75.  
  76. DWORD CRASAuthenticationMessageBlock::GetErrorCode( void ) const
  77. {
  78.    return( dwError );
  79. }
  80.  
  81. void CRASAuthenticationMessageBlock::GetErrorString( CString &return_string ) const
  82. {
  83.    return_string = szNetBiosError;
  84. }
  85.  
  86. /*
  87. ** CRASNetBEUIFramer
  88. */
  89.  
  90. CRASNetBEUIFramer::CRASNetBEUIFramer()
  91. {
  92.    m_Initialize();
  93. }
  94.  
  95. CRASNetBEUIFramer::CRASNetBEUIFramer( const RASPPPNBF *source )
  96. {
  97.    m_Initialize();
  98.    Copy( source );
  99. }
  100.  
  101. CRASNetBEUIFramer::CRASNetBEUIFramer( const CRASNetBEUIFramer& source )
  102. {
  103.    m_Initialize();
  104.    Copy( source );
  105. }
  106.  
  107. CRASNetBEUIFramer::~CRASNetBEUIFramer()
  108. {
  109.    m_Initialize();
  110. }
  111.  
  112. void CRASNetBEUIFramer::m_Initialize( void )
  113. {
  114.    ::ZeroMemory( (RASPPPNBF *) this, sizeof( RASPPPNBF ) );
  115.    dwSize = sizeof( RASPPPNBF );
  116. }
  117.  
  118. void CRASNetBEUIFramer::Copy( const RASPPPNBF *source )
  119. {
  120.    ASSERT( source != NULL );
  121.  
  122.    if ( source == NULL )
  123.    {
  124.       m_Initialize();
  125.       return;
  126.    }
  127.  
  128.    dwError        = source->dwError;
  129.    dwNetBiosError = source->dwNetBiosError;
  130.    bLana          = source->bLana;
  131.    ::strcpy( szNetBiosError,    source->szNetBiosError    );
  132.    ::strcpy( szWorkstationName, source->szWorkstationName );
  133. }
  134.  
  135. void CRASNetBEUIFramer::Copy( const CRASNetBEUIFramer& source )
  136. {
  137.    dwError        = source.dwError;
  138.    dwNetBiosError = source.dwNetBiosError;
  139.    bLana          = source.bLana;                      
  140.    ::strcpy( szNetBiosError,    source.szNetBiosError    );
  141.    ::strcpy( szWorkstationName, source.szWorkstationName );
  142. }
  143.  
  144. BYTE CRASNetBEUIFramer::GetLana( void ) const
  145. {
  146.    return( bLana );
  147. }
  148.  
  149. DWORD CRASNetBEUIFramer::GetErrorCode( void ) const
  150. {
  151.    return( dwError );
  152. }
  153.  
  154. DWORD CRASNetBEUIFramer::GetNetBiosErrorCode( void ) const
  155. {
  156.    return( dwNetBiosError );
  157. }
  158.  
  159. void CRASNetBEUIFramer::GetErrorString( CString& return_string ) const
  160. {
  161.    return_string = szNetBiosError;
  162. }
  163.  
  164. void CRASNetBEUIFramer::GetWorkstationName( CString& return_string ) const
  165. {
  166.    return_string = szWorkstationName;
  167. }
  168.  
  169. /*
  170. ** CRASInternetworkPacketExchange
  171. */
  172.  
  173. CRASInternetworkPacketExchange::CRASInternetworkPacketExchange()
  174. {
  175.    m_Initialize();
  176. }
  177.  
  178. CRASInternetworkPacketExchange::CRASInternetworkPacketExchange( const RASPPPIPX * source )
  179. {
  180.    m_Initialize();
  181.    Copy( source );
  182. }
  183.  
  184. CRASInternetworkPacketExchange::CRASInternetworkPacketExchange( const CRASInternetworkPacketExchange& source )
  185. {
  186.    m_Initialize();
  187.    Copy( source );
  188. }
  189.  
  190. CRASInternetworkPacketExchange::~CRASInternetworkPacketExchange()
  191. {
  192.    m_Initialize();
  193. }
  194.  
  195. void CRASInternetworkPacketExchange::m_Initialize( void )
  196. {
  197.    ::ZeroMemory( (RASPPPIPX *) this, sizeof( RASPPPIPX ) );
  198.    dwSize = sizeof( RASPPPIPX );
  199. }
  200.  
  201. void CRASInternetworkPacketExchange::Copy( const RASPPPIPX * source )
  202. {
  203.    ASSERT( source != NULL );
  204.  
  205.    if ( source == NULL )
  206.    {
  207.       m_Initialize();
  208.       return;
  209.    }
  210.  
  211.    dwError = source->dwError;
  212.    ::strcpy( szIpxAddress, source->szIpxAddress );
  213. }
  214.  
  215. void CRASInternetworkPacketExchange::Copy( const CRASInternetworkPacketExchange& source )
  216. {
  217.    dwError = source.dwError;
  218.    ::strcpy( szIpxAddress, source.szIpxAddress );
  219. }
  220.  
  221. DWORD CRASInternetworkPacketExchange::GetErrorCode( void ) const
  222. {
  223.    return( dwError );
  224. }
  225.  
  226. void CRASInternetworkPacketExchange::GetIPXAddress( CString& return_string ) const
  227. {
  228.    return_string =szIpxAddress;
  229. }
  230.  
  231. /*
  232. ** CRASInternetProtocol
  233. */
  234.  
  235. CRASInternetProtocol::CRASInternetProtocol()
  236. {
  237.    m_Initialize();
  238. }
  239.  
  240. CRASInternetProtocol::CRASInternetProtocol( const RASPPPIP * source )
  241. {
  242.    m_Initialize();
  243.    Copy( source );
  244. }
  245.  
  246. CRASInternetProtocol::CRASInternetProtocol( const CRASInternetProtocol& source )
  247. {
  248.    m_Initialize();
  249.    Copy( source );
  250. }
  251.  
  252. CRASInternetProtocol::~CRASInternetProtocol()
  253. {
  254.    m_Initialize();
  255. }
  256.  
  257. void CRASInternetProtocol::m_Initialize( void )
  258. {
  259.    ::ZeroMemory( (RASPPPIP *) this, sizeof( RASPPPIP ) );
  260.    dwSize = sizeof( RASPPPIP );
  261. }
  262.  
  263. void CRASInternetProtocol::Copy( const RASPPPIP * source )
  264. {
  265.    ASSERT( source != NULL );
  266.  
  267.    if ( source == NULL )
  268.    {
  269.       m_Initialize();
  270.       return;
  271.    }
  272.  
  273.    dwError = source->dwError;
  274.    ::strcpy( szIpAddress, source->szIpAddress );
  275. }
  276.  
  277. void CRASInternetProtocol::Copy( const CRASInternetProtocol& source )
  278. {
  279.    dwError = source.dwError;
  280.    ::strcpy( szIpAddress, source.szIpAddress );
  281. }
  282.  
  283. DWORD CRASInternetProtocol::GetErrorCode( void ) const
  284. {
  285.    return( dwError );
  286. }
  287.  
  288. void CRASInternetProtocol::GetIPAddress( CString& return_string ) const
  289. {
  290.    return_string = szIpAddress;
  291. }
  292.  
  293. /*
  294. ** CRAS
  295. */
  296.  
  297. CRAS::CRAS()
  298. {
  299.    m_Initialize();
  300. }
  301.  
  302. CRAS::~CRAS()
  303. {
  304.    m_Initialize();
  305. }
  306.  
  307. void CRAS::m_Initialize( void )
  308. {
  309.    ASSERT_VALID( this );
  310.  
  311.    ::ZeroMemory( &m_RasConnectionStatus, sizeof( m_RasConnectionStatus ) );
  312.    m_RasConnectionStatus.dwSize = sizeof( m_RasConnectionStatus );
  313.  
  314.    m_ErrorCode           = 0;
  315.    m_RasConnectionHandle = NULL;
  316. }
  317.  
  318. BOOL CRAS::Close( LPCTSTR name_of_connection )
  319. {
  320.    ASSERT_VALID( this );
  321.    return( HangUp( name_of_connection ) );
  322. }
  323.  
  324. BOOL CRAS::Dial( LPCTSTR who_to_dial )
  325. {
  326.    ASSERT_VALID( this );
  327.    ASSERT( who_to_dial != NULL );
  328.  
  329.    if ( who_to_dial == NULL )
  330.    {
  331.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  332.       return( FALSE );
  333.    }
  334.  
  335.    RASDIALPARAMS dialing_parameters;
  336.  
  337.    ::ZeroMemory( &dialing_parameters, sizeof( dialing_parameters ) );
  338.  
  339.    dialing_parameters.dwSize = sizeof( dialing_parameters );
  340.    strcpy( dialing_parameters.szEntryName, who_to_dial );
  341.  
  342.    m_RasConnectionHandle = NULL;
  343.  
  344.    m_ErrorCode = ::RasDial( NULL, NULL, &dialing_parameters, 0, NULL, &m_RasConnectionHandle );
  345.  
  346.    if ( m_ErrorCode == 0 )
  347.    {
  348.       return( TRUE );
  349.    }
  350.    else
  351.    {
  352.       return( FALSE );
  353.    }
  354. }
  355.  
  356. void CRAS::m_GetConnectionsIntoMemory( LPRASCONN& connections, DWORD& number_of_connections )
  357. {
  358.    ASSERT_VALID( this );
  359.  
  360.    DWORD size_of_buffer         = 64 * sizeof( RASCONN );
  361.    DWORD number_of_bytes_needed = size_of_buffer;
  362.    DWORD number_of_entries      = 0;
  363.  
  364.    LPRASCONN memory_buffer = (LPRASCONN) new BYTE[ size_of_buffer ]; // Get room for 64 entries (to begin with)
  365.  
  366.    if ( memory_buffer == NULL )
  367.    {
  368.       m_ErrorCode = ERROR_NOT_ENOUGH_MEMORY;
  369.       return;
  370.    }
  371.  
  372.    ::ZeroMemory( memory_buffer, size_of_buffer );
  373.  
  374.    memory_buffer->dwSize = sizeof( RASCONN );
  375.  
  376.    m_ErrorCode = ::RasEnumConnections( memory_buffer, &number_of_bytes_needed, &number_of_entries );
  377.  
  378.    if ( m_ErrorCode != 0 )
  379.    {
  380.       if ( m_ErrorCode == ERROR_BUFFER_TOO_SMALL || m_ErrorCode == ERROR_NOT_ENOUGH_MEMORY )
  381.       {
  382.          delete [] memory_buffer;
  383.  
  384.          memory_buffer = (LPRASCONN) new BYTE[ number_of_bytes_needed ];
  385.  
  386.          if ( memory_buffer == NULL )
  387.          {
  388.             m_ErrorCode = ERROR_NOT_ENOUGH_MEMORY;
  389.             return;
  390.          }
  391.  
  392.          ::ZeroMemory( memory_buffer, number_of_bytes_needed );
  393.  
  394.          memory_buffer->dwSize = sizeof( RASCONN );
  395.          m_ErrorCode = ::RasEnumConnections( memory_buffer, &number_of_bytes_needed, &number_of_entries );
  396.       }
  397.    }
  398.  
  399.    connections           = memory_buffer;
  400.    number_of_connections = number_of_entries;
  401. }
  402.  
  403. HRASCONN CRAS::GetConnection( LPCTSTR name_of_connection )
  404. {
  405.    ASSERT_VALID( this );
  406.  
  407.    if ( name_of_connection == NULL )
  408.    {
  409.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  410.       return( NULL );
  411.    }
  412.  
  413.    LPRASCONN connections       = NULL;
  414.    DWORD     number_of_entries = 0;
  415.  
  416.    m_GetConnectionsIntoMemory( connections, number_of_entries );
  417.  
  418.    HRASCONN return_value = NULL;
  419.  
  420.    if ( m_ErrorCode == 0 )
  421.    {
  422.       DWORD index = 0;
  423.  
  424.       while( index < number_of_entries )
  425.       {
  426.          if ( ::strcmpi( name_of_connection, connections[ index ].szEntryName ) == 0 )
  427.          {
  428.             return_value = connections[ index ].hrasconn;
  429.             index = number_of_entries;
  430.          }
  431.  
  432.          index++;
  433.       }
  434.    }
  435.  
  436.    delete [] connections;
  437.  
  438.    return( return_value );
  439. }
  440.  
  441. BOOL CRAS::GetConnections( CStringArray& connection_names )
  442. {
  443.    ASSERT_VALID( this );
  444.  
  445.    connection_names.RemoveAll();
  446.  
  447.    LPRASCONN connections       = NULL;
  448.    DWORD     number_of_entries = 0;
  449.  
  450.    m_GetConnectionsIntoMemory( connections, number_of_entries );
  451.  
  452.    BOOL return_value = FALSE;
  453.  
  454.    if ( m_ErrorCode == 0 )
  455.    {
  456.       return_value = TRUE;
  457.  
  458.       DWORD index = 0;
  459.  
  460.       while( index < number_of_entries )
  461.       {
  462.          connection_names.Add( connections[ index ].szEntryName );
  463.          index++;
  464.       }
  465.    }
  466.  
  467.    delete [] connections;
  468.  
  469.    return( return_value );
  470. }
  471.  
  472. BOOL CRAS::GetConnectionStatus( void )
  473. {
  474.    ASSERT_VALID( this );
  475.  
  476.    m_ErrorCode = ::RasGetConnectStatus( m_RasConnectionHandle, &m_RasConnectionStatus );
  477.  
  478.    if ( m_ErrorCode == 0 )
  479.    {
  480.       return( TRUE );
  481.    }
  482.    else
  483.    {
  484.       return( FALSE );
  485.    }
  486. }
  487.  
  488. BOOL CRAS::GetErrorCode( void ) const
  489. {
  490.    ASSERT_VALID( this );
  491.    return( m_ErrorCode );
  492. }
  493.  
  494. BOOL CRAS::GetErrorString( CString& return_string )
  495. {
  496.    ASSERT_VALID( this );
  497.  
  498.    TCHAR string[ 4096 ];
  499.  
  500.    ::ZeroMemory( string, sizeof( string ) );
  501.  
  502.    if ( ::RasGetErrorString( m_ErrorCode, string, sizeof( string ) ) == 0 )
  503.    {
  504.       return_string = string;
  505.       return( TRUE );
  506.    }
  507.    else
  508.    {
  509.       return_string.Empty();
  510.       return( FALSE );
  511.    }
  512. }
  513.  
  514. BOOL CRAS::GetPhoneBookEntries( CStringArray& phone_book_entries )
  515. {
  516.    ASSERT_VALID( this );
  517.  
  518.    phone_book_entries.RemoveAll();
  519.  
  520.    DWORD size_of_buffer         = 64 * sizeof( RASENTRYNAME );
  521.    DWORD number_of_bytes_needed = size_of_buffer;
  522.    DWORD number_of_entries      = 0;
  523.  
  524.    LPRASENTRYNAME memory_buffer = (LPRASENTRYNAME) new BYTE[ size_of_buffer ]; // Get room for 64 entries (to begin with)
  525.  
  526.    if ( memory_buffer == NULL )
  527.    {
  528.       m_ErrorCode = ERROR_NOT_ENOUGH_MEMORY;
  529.       return( FALSE );
  530.    }
  531.  
  532.    ::ZeroMemory( memory_buffer, size_of_buffer );
  533.  
  534.    memory_buffer->dwSize = sizeof( RASENTRYNAME );
  535.  
  536.    m_ErrorCode = ::RasEnumEntries( NULL, NULL, memory_buffer, &number_of_bytes_needed, &number_of_entries );
  537.  
  538.    if ( m_ErrorCode != 0 )
  539.    {
  540.       if ( m_ErrorCode == ERROR_BUFFER_TOO_SMALL || m_ErrorCode == ERROR_NOT_ENOUGH_MEMORY )
  541.       {
  542.          delete [] memory_buffer;
  543.  
  544.          memory_buffer = (LPRASENTRYNAME) new BYTE[ number_of_bytes_needed ];
  545.  
  546.          if ( memory_buffer == NULL )
  547.          {
  548.             m_ErrorCode = ERROR_NOT_ENOUGH_MEMORY;
  549.             return( FALSE );
  550.          }
  551.  
  552.          ::ZeroMemory( memory_buffer, number_of_bytes_needed );
  553.  
  554.          memory_buffer->dwSize = sizeof( RASENTRYNAME );
  555.          m_ErrorCode = ::RasEnumEntries( NULL, NULL, memory_buffer, &number_of_bytes_needed, &number_of_entries );
  556.       }
  557.    }
  558.  
  559.    BOOL return_value = FALSE;
  560.  
  561.    if ( m_ErrorCode == 0 )
  562.    {
  563.       return_value = TRUE;
  564.  
  565.       DWORD index = 0;
  566.  
  567.       while( index < number_of_entries )
  568.       {
  569.          phone_book_entries.Add( memory_buffer[ index ].szEntryName );
  570.          index++;
  571.       }
  572.    }
  573.  
  574.    delete [] memory_buffer;
  575.  
  576.    return( return_value );
  577. }
  578.  
  579. BOOL CRAS::GetProtocolInformation( CRASAuthenticationMessageBlock& data_to_get )
  580. {
  581.    ASSERT_VALID( this );
  582.  
  583.    RASAMB return_data;
  584.  
  585.    DWORD size_of_return_data = sizeof( return_data );
  586.  
  587.    ::ZeroMemory( &return_data, size_of_return_data );
  588.  
  589.    return_data.dwSize = size_of_return_data;
  590.  
  591.    if ( ::RasGetProjectionInfo( m_RasConnectionHandle, RASP_Amb, (LPVOID) &return_data, &size_of_return_data ) == 0 )
  592.    {
  593.       data_to_get.Copy( CRASAuthenticationMessageBlock( &return_data ) );
  594.  
  595.       return( TRUE );
  596.    }
  597.    else
  598.    {
  599.       return( FALSE );
  600.    }
  601. }
  602.  
  603. BOOL CRAS::GetProtocolInformation( CRASNetBEUIFramer& data_to_get )
  604. {
  605.    ASSERT_VALID( this );
  606.  
  607.    RASPPPNBF return_data;
  608.  
  609.    DWORD size_of_return_data = sizeof( return_data );
  610.  
  611.    ::ZeroMemory( &return_data, size_of_return_data );
  612.  
  613.    return_data.dwSize = size_of_return_data;
  614.  
  615.    if ( ::RasGetProjectionInfo( m_RasConnectionHandle, RASP_PppNbf, (LPVOID) &return_data, &size_of_return_data ) == 0 )
  616.    {
  617.       data_to_get.Copy( CRASNetBEUIFramer( &return_data ) );
  618.  
  619.       return( TRUE );
  620.    }
  621.    else
  622.    {
  623.       return( FALSE );
  624.    }
  625. }
  626.  
  627. BOOL CRAS::GetProtocolInformation( CRASInternetworkPacketExchange& data_to_get )
  628. {
  629.    ASSERT_VALID( this );
  630.  
  631.    RASPPPIPX return_data;
  632.  
  633.    DWORD size_of_return_data = sizeof( return_data );
  634.  
  635.    ::ZeroMemory( &return_data, size_of_return_data );
  636.  
  637.    return_data.dwSize = size_of_return_data;
  638.  
  639.    if ( ::RasGetProjectionInfo( m_RasConnectionHandle, RASP_PppIpx, (LPVOID) &return_data, &size_of_return_data ) == 0 )
  640.    {
  641.       data_to_get.Copy( CRASInternetworkPacketExchange( &return_data ) );
  642.  
  643.       return( TRUE );
  644.    }
  645.    else
  646.    {
  647.       return( FALSE );
  648.    }
  649. }
  650.  
  651. BOOL CRAS::GetProtocolInformation( CRASInternetProtocol& data_to_get )
  652. {
  653.    ASSERT_VALID( this );
  654.  
  655.    RASPPPIP return_data;
  656.  
  657.    DWORD size_of_return_data = sizeof( return_data );
  658.  
  659.    ::ZeroMemory( &return_data, size_of_return_data );
  660.  
  661.    return_data.dwSize = size_of_return_data;
  662.  
  663.    if ( ::RasGetProjectionInfo( m_RasConnectionHandle, RASP_PppIp, (LPVOID) &return_data, &size_of_return_data ) == 0 )
  664.    {
  665.       data_to_get.Copy( CRASInternetProtocol( &return_data ) );
  666.  
  667.       return( TRUE );
  668.    }
  669.    else
  670.    {
  671.       return( FALSE );
  672.    }
  673. }
  674.  
  675. BOOL CRAS::HangUp( LPCTSTR name_of_connection )
  676. {
  677.    ASSERT_VALID( this );
  678.  
  679.    if ( name_of_connection == NULL )
  680.    {
  681.       m_ErrorCode = ::RasHangUp( m_RasConnectionHandle );
  682.       m_RasConnectionHandle = NULL;
  683.    }
  684.    else
  685.    {
  686.       HRASCONN temp_handle = GetConnection( name_of_connection );
  687.       m_ErrorCode = ::RasHangUp( temp_handle );
  688.    }
  689.  
  690.    if ( m_ErrorCode == 0 )
  691.    {
  692.       return( TRUE );
  693.    }
  694.    else
  695.    {
  696.       return( FALSE );
  697.    }
  698. }
  699.  
  700. BOOL CRAS::IsConnected( void )
  701. {
  702.    ASSERT_VALID( this );
  703.  
  704.    GetConnectionStatus();
  705.  
  706.    if ( m_RasConnectionStatus.rasconnstate == RASCS_Connected )
  707.    {
  708.       return( TRUE );
  709.    }
  710.    else
  711.    {
  712.       return( FALSE );
  713.    }
  714. }
  715.                                                               
  716. BOOL CRAS::Open( LPCTSTR what_to_open )
  717. {
  718.    ASSERT_VALID( this );
  719.    return( Dial( what_to_open ) );
  720. }
  721.  
  722.