int CIpAddress::LookupHost( const t_string &x_sServer, unsigned int x_uPort, unsigned int x_uType ) { #if defined( HTM_NOSOCKET2 ) return 0; #else // Lose old info Destroy(); // Ensure we have a valid pointer if ( !x_sServer.length() ) return 0; // First try to interpret as dot address unsigned long uAddr = inet_addr( x_sServer.c_str() ); if ( INADDR_NONE == uAddr ) { LPHOSTENT pHe = gethostbyname( x_sServer.c_str() ); if ( !pHe ) return 0; LPIN_ADDR pia = (LPIN_ADDR)*pHe->h_addr_list; if ( !pia ) return 0; // Grab the address uAddr = *(DWORD*)&pia->S_un.S_addr; } // end if SetRawAddress( ntohl( uAddr ), x_uPort, x_uType ); return 1; #endif }
CIpAddress::t_string CIpAddress::GetDomainName( const t_string &x_sServer ) { CIpAddress::t_string sRet; // Load netapi32.dll HMODULE hLib = LoadLibrary( tcT( "netapi32.dll" ) ); if ( !hLib ) return sRet; // Get function pointers pfn_NetApiBufferFree pNetApiBufferFree = (pfn_NetApiBufferFree)GetProcAddress( hLib, tcT( "NetApiBufferFree" ) ); pfn_NetWkstaGetInfo pNetWkstaGetInfo = (pfn_NetWkstaGetInfo)GetProcAddress( hLib, tcT( "NetWkstaGetInfo" ) ); // Attempt to read the domain name WKSTA_INFO_100 *pwi100 = 0; if ( pNetWkstaGetInfo && !pNetWkstaGetInfo( x_sServer.length() ? (LPWSTR)tcStr2Wc( x_sServer ).c_str() : 0, 100, (LPBYTE*)&pwi100 ) ) if ( pwi100 && pwi100->wki100_langroup ) sRet = tcWc2Str( pwi100->wki100_langroup ); // Free buffer if ( pNetApiBufferFree && pwi100 ) pNetApiBufferFree( pwi100 ); // Free library FreeLibrary( hLib ); // Send the domain name along return sRet; }
int CIpAddress::LookupUri( const t_string &x_sUrl, unsigned int x_uPort, unsigned int x_uType ) { // Lose old info Destroy(); // Ensure we have a valid pointer if ( !x_sUrl.length() ) return 0; // Crack the url t_pb8 pbUri = parser::DecodeUri< t_pb8 >( x_sUrl ); if ( !pbUri.size() ) return 0; // Did we get a host name? if ( !pbUri[ "host" ].length() ) return 0; // Get the port if ( !x_uPort ) x_uPort = pbUri[ "port" ].ToLong(); // Save the type m_uType = x_uType; return LookupHost( pbUri[ "host" ].str(), x_uPort ); }
// +++ Make IPv6 safe int CIpAddress::SetDotAddress( const t_string &x_sDotAddress, unsigned int x_uPort, unsigned int x_uType ) { if ( !x_sDotAddress.length() ) return 0; // Convert the dot address u_long ip = ntohl( inet_addr( x_sDotAddress.c_str() ) ); if ( INADDR_NONE == ip ) return 0; SetRawAddress( ip, x_uPort, x_uType ); return 1; }
int CIpSocket::Connect( const t_string &x_sAddress, unsigned int x_uPort ) { if ( !x_sAddress.length() ) return 0; // Punt if not initialized if ( !IsInitialized() ) return 0; CIpAddress addr; // Were we passed a URL? if ( !x_uPort && addr.LookupUri( x_sAddress ) ) return Connect( addr ); // Lookup the host address if ( addr.LookupHost( x_sAddress, x_uPort ) ) return Connect( addr ); return 0; }
DWORD Client::Write(t_string write_buffer) { if (eState_ != STATE::started) { return ERROR_NOT_FOUND; } DWORD cbWritten; EnterCriticalSection(&csPipe_Write_); BOOL bSuccess = WriteFile ( hPipe_, // pipe handle write_buffer.c_str(), // message write_buffer.length() * sizeof(t_string::traits_type::char_type), // message length &cbWritten, // bytes written NULL // not overlapped ); LeaveCriticalSection(&csPipe_Write_); return bSuccess ? ERROR_SUCCESS : GetLastError(); }
DWORD Base::Write(const t_string & write_buffer, HANDLE pipe_handle, CRITICAL_SECTION & write_critical_section) { if (eState_ != STATE::started) { return ERROR_NOT_FOUND; } EnterCriticalSection(&write_critical_section); DWORD cbWritten; BOOL bSuccess = WriteFile ( pipe_handle, // pipe handle write_buffer.c_str(), // message write_buffer.length() * sizeof(t_string::traits_type::char_type), // message length &cbWritten, // bytes written NULL // not overlapped ); LeaveCriticalSection(&write_critical_section); return bSuccess ? ERROR_SUCCESS : GetLastError(); }
int CIpAddress::LookupHost( const t_string &x_sServer, unsigned int x_uPort, unsigned int x_uType ) { // Lose old info Destroy(); // Ensure we have a valid pointer if ( !x_sServer.length() ) return 0; // +++ Get this working eventually // #if !defined( HTM_USE_GETHOSTBYNAME ) #if 0 in_addr ip4; if ( inet_pton( PF_INET, x_sServer.c_str(), &ip4 ) ) { SetRawAddress( ntohl( *(unsigned long*)&ip4.s_addr ), x_uPort, x_uType ); return 1; } // end if struct addrinfo hints, *addr = 0; memset( &hints, 0, sizeof( hints ) ); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_CANONNAME; int rval = 0; if ( rval = getaddrinfo( x_sServer.c_str(), 0, &hints, &addr ) ) return 0; int max = 128; while ( !addr && max-- ) { switch( addr->ai_family ) { case AF_INET : { sockaddr_in *psai = (sockaddr_in*)addr->ai_addr; in_addr *pia = &psai->sin_addr; SetRawAddress( ntohl( *(unsigned long*)&pia->s_addr ), x_uPort, x_uType ); return 1; } break; case AF_INET6 : { // +++ Add v6 support sockaddr_in6 *psai = (sockaddr_in6*)addr->ai_addr; sin6_addr *pia6 = &psai->sin6_addr; } break; } // end switch if ( addr == addr->ai_next ) addr = 0; else addr = addr->ai_next; } // end while return 0; #else // First try to interpret as dot address unsigned int uAddr = inet_addr( x_sServer.c_str() ); if ( INADDR_NONE != uAddr ) { SetRawAddress( ntohl( uAddr ), x_uPort, x_uType ); return 1; } // end if struct hostent *pHe; do { pHe = gethostbyname( x_sServer.c_str() ); } while ( !pHe && EINTR == h_errno ); if ( !pHe || !pHe->h_addr_list ) return 0; in_addr *pia = (in_addr*)*pHe->h_addr_list; if ( !pia ) return 0; SetRawAddress( ntohl( *(unsigned int*)&pia->s_addr ), x_uPort, x_uType ); return 1; #endif }