Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

cabal_os_wsa.cpp

00001 /*
00002    CABAL PROTOCOL
00003    CAched BAlanced LAN
00004 
00005    OS specific cabal extensions (WIN*OWS WSA)
00006 
00007    $Id: cabal_os_wsa.cpp,v 1.5 2004/03/26 22:45:11 jonnymind Exp $
00008 ---------------------------------------------
00009    Begin      :
00010    Author     : Giancarlo Niccolai
00011 
00012    Last modified because:
00013 
00014 */
00015 
00016 
00017 #include <cabal_os.h>
00018 
00019 namespace Cabal
00020 {
00021 
00022 bool Net_WSA::init() throw(InitError)
00023 {
00024    WORD wVersion;
00025    WSADATA wsaData;
00026 
00027 
00028    wVersion = MAKEWORD( 2, 0 );
00029    if ( WSAStartup( wVersion, &wsaData ) == 0 ) {
00030       return true;
00031    }
00032 
00033    throw InitError( "Cannot initialize Windows Socket API", GetLastError() );
00034 }
00035 
00036 bool Net_WSA::exit() throw(DataIncompleteError)
00037 {
00038    if ( WSACleanup() == 0 ) {
00039       return true;
00040    }
00041    throw DataIncompleteError( "Cannot shut down Windows Socket API", GetLastError() );
00042 }
00043      
00044 bool Net_WSA::getHostList(
00045    const std::string &name,
00046    std::string &real_name,
00047    char *real_ip,
00048    NetAddr *other_addrs, int na_count )
00049 {
00050    int i;
00051    struct hostent *result;
00052 
00053    if (isalpha(name.c_str()[0])) 
00054    {   /* host address is a name */
00055      result = gethostbyname(name.c_str());
00056    }
00057    else  
00058    { 
00059      unsigned int addr = inet_addr(name.c_str());
00060      result = gethostbyaddr((char *)&addr, 4, AF_INET);
00061    }
00062 
00063    if ( result )
00064    {
00065       char **ret_ips = result->h_addr_list;
00066 
00067       i = 0;
00068       while ( *ret_ips && i < na_count )
00069       {
00070          memcpy( other_addrs+i, *ret_ips, sizeof( NetAddr ) );
00071          // WSA bug: hosts are returned in host order bytes.
00072          *(other_addrs + i) = hostToNetAddr( *(other_addrs + i) );
00073          ret_ips++;
00074          i++;
00075       }
00076 
00077       SockAddrInet addr;
00078       Net::initSockAddr( addr,  *other_addrs, 0 );
00079       addrToString( addr , real_ip );
00080       
00081       if( i < na_count ) 
00082          other_addrs[i] = CABAL_INVALID_ADDR;
00083          
00084       real_name = result->h_name;
00085       return true;
00086    }
00087 
00088    return false;
00089 }
00090 
00091 
00092 bool Net_WSA::addrToString( const SockAddrInet &addr, char* buf )
00093 {
00094    // TODO: Lock here
00095    char *ret =  inet_ntoa( addr.sin_addr );
00096    if ( ret == 0 )
00097       return false;
00098    strncpy( buf, ret, CABAL_MAX_IP_SIZE );
00099    buf[CABAL_MAX_IP_SIZE-1] = '\0';
00100    return true;
00101 }
00102 
00103 
00104 std::string Net_WSA::errorDescription( const int osCode )
00105 {
00106    char buf[256];
00107    DWORD dwRet;
00108    
00109    if ( osCode < 0 ) return "";
00110    
00111    dwRet = FormatMessage( 
00112       FORMAT_MESSAGE_FROM_SYSTEM,
00113       0,
00114       osCode,
00115       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00116       buf,
00117       256,
00118       0);
00119    
00120    if ( dwRet != 0 ) return std::string(buf);
00121    
00122    return "Unknown Error";
00123 }
00124 
00125 MSECS Net_WSA::timeOfDay()
00126 {
00127    SYSTEMTIME sysTime;
00128 
00129    GetSystemTime( &sysTime );
00130 
00131    return (static_cast<MSECS>(sysTime.wHour) * 3600000L) + 
00132             sysTime.wMinute * 60000L + 
00133             sysTime.wSecond * 1000 + 
00134             sysTime.wMilliseconds;
00135 }
00136 
00137 
00138 }
00139 
00140 /* End of cabal_os_wsa.cpp */

Generated on Sat Apr 10 17:41:48 2004 for Cabal by doxygen 1.3.5