1*87d2adbcSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*87d2adbcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*87d2adbcSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*87d2adbcSAndrew Rist * distributed with this work for additional information 6*87d2adbcSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*87d2adbcSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*87d2adbcSAndrew Rist * "License"); you may not use this file except in compliance 9*87d2adbcSAndrew Rist * with the License. You may obtain a copy of the License at 10*87d2adbcSAndrew Rist * 11*87d2adbcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*87d2adbcSAndrew Rist * 13*87d2adbcSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*87d2adbcSAndrew Rist * software distributed under the License is distributed on an 15*87d2adbcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*87d2adbcSAndrew Rist * KIND, either express or implied. See the License for the 17*87d2adbcSAndrew Rist * specific language governing permissions and limitations 18*87d2adbcSAndrew Rist * under the License. 19*87d2adbcSAndrew Rist * 20*87d2adbcSAndrew Rist *************************************************************/ 21*87d2adbcSAndrew Rist 22*87d2adbcSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir 25cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 26cdf0e10cSrcweir #include "precompiled_sal.hxx" 27cdf0e10cSrcweir #include "sockethelper.hxx" 28cdf0e10cSrcweir #include <testshl/simpleheader.hxx> 29cdf0e10cSrcweir 30cdf0e10cSrcweir //------------------------------------------------------------------------ 31cdf0e10cSrcweir // Ip version definition 32cdf0e10cSrcweir //------------------------------------------------------------------------ 33cdf0e10cSrcweir #define IP_VER 4 /// currently only IPv4 is considered. 34cdf0e10cSrcweir 35cdf0e10cSrcweir //------------------------------------------------------------------------ 36cdf0e10cSrcweir // helper functions 37cdf0e10cSrcweir //------------------------------------------------------------------------ 38cdf0e10cSrcweir 39cdf0e10cSrcweir /** compare two OUString. 40cdf0e10cSrcweir */ 41cdf0e10cSrcweir sal_Bool compareUString( const ::rtl::OUString & ustr1, const ::rtl::OUString & ustr2 ) 42cdf0e10cSrcweir { 43cdf0e10cSrcweir sal_Bool bOk = ustr1.equalsIgnoreAsciiCase( ustr2 ); 44cdf0e10cSrcweir 45cdf0e10cSrcweir return bOk; 46cdf0e10cSrcweir } 47cdf0e10cSrcweir 48cdf0e10cSrcweir /** compare a OUString and an ASCII string. 49cdf0e10cSrcweir */ 50cdf0e10cSrcweir sal_Bool compareUString( const ::rtl::OUString & ustr, const sal_Char *astr ) 51cdf0e10cSrcweir { 52cdf0e10cSrcweir ::rtl::OUString ustr2 = rtl::OUString::createFromAscii( astr ); 53cdf0e10cSrcweir sal_Bool bOk = ustr.equalsIgnoreAsciiCase( ustr2 ); 54cdf0e10cSrcweir 55cdf0e10cSrcweir return bOk; 56cdf0e10cSrcweir } 57cdf0e10cSrcweir 58cdf0e10cSrcweir /** compare two socket address. 59cdf0e10cSrcweir */ 60cdf0e10cSrcweir sal_Bool compareSocketAddr( const ::osl::SocketAddr & addr1 , const ::osl::SocketAddr & addr2 ) 61cdf0e10cSrcweir { 62cdf0e10cSrcweir return ( ( sal_True == compareUString( addr1.getHostname( 0 ), addr2.getHostname( 0 ) ) ) && ( addr2.getPort( ) == addr2.getPort( ) ) ); 63cdf0e10cSrcweir } 64cdf0e10cSrcweir 65cdf0e10cSrcweir /*char * oustring2char( const ::rtl::OUString & str ) 66cdf0e10cSrcweir { 67cdf0e10cSrcweir rtl::OString aString; 68cdf0e10cSrcweir aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US ); 69cdf0e10cSrcweir t_print("oustring2char %s\n", aString.getStr( ) ); 70cdf0e10cSrcweir sal_Char * sStr = aString.getStr( ); 71cdf0e10cSrcweir return (char *)sStr; 72cdf0e10cSrcweir }*/ 73cdf0e10cSrcweir 74cdf0e10cSrcweir /** print a UNI_CODE String. And also print some comments of the string. 75cdf0e10cSrcweir */ 76cdf0e10cSrcweir void printUString( const ::rtl::OUString & str, const char* msg) 77cdf0e10cSrcweir { 78cdf0e10cSrcweir t_print("#%s #printUString_u# ", msg ); 79cdf0e10cSrcweir rtl::OString aString; 80cdf0e10cSrcweir aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US ); 81cdf0e10cSrcweir //char * sStr = aString.getStr( ); 82cdf0e10cSrcweir t_print("%s\n", aString.getStr( ) ); 83cdf0e10cSrcweir } 84cdf0e10cSrcweir 85cdf0e10cSrcweir /** get the local host name. 86cdf0e10cSrcweir mindy: gethostbyname( "localhost" ), on Linux, it returns the hostname in /etc/hosts + domain name, 87cdf0e10cSrcweir if no entry in /etc/hosts, it returns "localhost" + domain name 88cdf0e10cSrcweir */ 89cdf0e10cSrcweir ::rtl::OUString getHost( void ) 90cdf0e10cSrcweir { 91cdf0e10cSrcweir struct hostent *hptr; 92cdf0e10cSrcweir 93cdf0e10cSrcweir hptr = gethostbyname( "localhost" ); 94cdf0e10cSrcweir OSL_ENSURE( hptr != NULL, "#In getHostname function, error on gethostbyname()" ); 95cdf0e10cSrcweir ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) hptr->h_name ); 96cdf0e10cSrcweir 97cdf0e10cSrcweir return aUString; 98cdf0e10cSrcweir } 99cdf0e10cSrcweir 100cdf0e10cSrcweir /** get the full host name of the current processor, such as "aegean.prc.sun.com" --mindyliu 101cdf0e10cSrcweir */ 102cdf0e10cSrcweir ::rtl::OUString getThisHostname( void ) 103cdf0e10cSrcweir { 104cdf0e10cSrcweir ::rtl::OUString aUString; 105cdf0e10cSrcweir #ifdef WNT 106cdf0e10cSrcweir struct hostent *hptr; 107cdf0e10cSrcweir hptr = gethostbyname( "localhost" ); 108cdf0e10cSrcweir OSL_ENSURE( hptr != NULL, "#In getHostname function, error on gethostbyname()" ); 109cdf0e10cSrcweir rtl::OString sHostname(hptr->h_name); 110cdf0e10cSrcweir aUString = ::rtl::OStringToOUString(sHostname, RTL_TEXTENCODING_ASCII_US); 111cdf0e10cSrcweir #else 112cdf0e10cSrcweir char hostname[255]; 113cdf0e10cSrcweir if (gethostname(hostname, 255) != 0) { 114cdf0e10cSrcweir OSL_ENSURE( false, "#Error: gethostname failed." ); 115cdf0e10cSrcweir } 116cdf0e10cSrcweir 117cdf0e10cSrcweir struct hostent *hptr; 118cdf0e10cSrcweir //first search /ets/hosts, then search from dns 119cdf0e10cSrcweir hptr = gethostbyname( hostname); 120cdf0e10cSrcweir if ( hptr != NULL ) 121cdf0e10cSrcweir { 122cdf0e10cSrcweir strcpy( hostname, hptr->h_name ); 123cdf0e10cSrcweir } 124cdf0e10cSrcweir 125cdf0e10cSrcweir t_print("hostname is %s \n", hostname ); 126cdf0e10cSrcweir rtl::OString sHostname( hostname ); 127cdf0e10cSrcweir aUString = ::rtl::OStringToOUString( sHostname, RTL_TEXTENCODING_ASCII_US ); 128cdf0e10cSrcweir aUString.getLength(); 129cdf0e10cSrcweir #endif 130cdf0e10cSrcweir return aUString; 131cdf0e10cSrcweir } 132cdf0e10cSrcweir 133cdf0e10cSrcweir /** get IP by name, search /etc/hosts first, then search from dns, fail return OUString("") 134cdf0e10cSrcweir */ 135cdf0e10cSrcweir ::rtl::OUString getIPbyName( rtl::OString const& str_name ) 136cdf0e10cSrcweir { 137cdf0e10cSrcweir ::rtl::OUString aUString; 138cdf0e10cSrcweir struct hostent *hptr; 139cdf0e10cSrcweir //first search /ets/hosts, then search from dns 140cdf0e10cSrcweir hptr = gethostbyname( str_name.getStr()); 141cdf0e10cSrcweir if ( hptr != NULL ) 142cdf0e10cSrcweir { 143cdf0e10cSrcweir struct in_addr ** addrptr; 144cdf0e10cSrcweir addrptr = (struct in_addr **) hptr->h_addr_list ; 145cdf0e10cSrcweir //if there are more than one IPs on the same machine, we select one 146cdf0e10cSrcweir for (; *addrptr; addrptr++) 147cdf0e10cSrcweir { 148cdf0e10cSrcweir t_print("#Local IP Address: %s\n", inet_ntoa(**addrptr)); 149cdf0e10cSrcweir aUString = ::rtl::OUString::createFromAscii( (sal_Char *) (inet_ntoa(**addrptr)) ); 150cdf0e10cSrcweir } 151cdf0e10cSrcweir } 152cdf0e10cSrcweir return aUString; 153cdf0e10cSrcweir } 154cdf0e10cSrcweir 155cdf0e10cSrcweir /** get local ethernet IP 156cdf0e10cSrcweir */ 157cdf0e10cSrcweir ::rtl::OUString getLocalIP( ) 158cdf0e10cSrcweir { 159cdf0e10cSrcweir char hostname[255]; 160cdf0e10cSrcweir gethostname(hostname, 255); 161cdf0e10cSrcweir 162cdf0e10cSrcweir return getIPbyName( hostname ); 163cdf0e10cSrcweir } 164cdf0e10cSrcweir 165cdf0e10cSrcweir /** construct error message 166cdf0e10cSrcweir */ 167cdf0e10cSrcweir ::rtl::OUString outputError( const ::rtl::OUString & returnVal, const ::rtl::OUString & rightVal, const sal_Char * msg ) 168cdf0e10cSrcweir { 169cdf0e10cSrcweir ::rtl::OUString aUString; 170cdf0e10cSrcweir if ( returnVal.equals( rightVal ) ) 171cdf0e10cSrcweir return aUString; 172cdf0e10cSrcweir aUString += ::rtl::OUString::createFromAscii(msg); 173cdf0e10cSrcweir aUString += ::rtl::OUString::createFromAscii(": the returned value is '"); 174cdf0e10cSrcweir aUString += returnVal; 175cdf0e10cSrcweir aUString += ::rtl::OUString::createFromAscii("', but the value should be '"); 176cdf0e10cSrcweir aUString += rightVal; 177cdf0e10cSrcweir aUString += ::rtl::OUString::createFromAscii("'."); 178cdf0e10cSrcweir return aUString; 179cdf0e10cSrcweir } 180cdf0e10cSrcweir 181cdf0e10cSrcweir /** wait _nSec seconds. 182cdf0e10cSrcweir */ 183cdf0e10cSrcweir void thread_sleep( sal_Int32 _nSec ) 184cdf0e10cSrcweir { 185cdf0e10cSrcweir /// print statement in thread process must use fflush() to force display. 186cdf0e10cSrcweir // printf("wait %d seconds. ", _nSec ); 187cdf0e10cSrcweir // fflush(stdout); 188cdf0e10cSrcweir 189cdf0e10cSrcweir #ifdef WNT //Windows 190cdf0e10cSrcweir Sleep( _nSec * 100 ); 191cdf0e10cSrcweir #endif 192cdf0e10cSrcweir #if ( defined UNX ) || ( defined OS2 ) //Unix 193cdf0e10cSrcweir usleep(_nSec * 100000); 194cdf0e10cSrcweir #endif 195cdf0e10cSrcweir // t_print("# done\n" ); 196cdf0e10cSrcweir } 197cdf0e10cSrcweir 198cdf0e10cSrcweir /** print Boolean value. 199cdf0e10cSrcweir */ 200cdf0e10cSrcweir void printBool( sal_Bool bOk ) 201cdf0e10cSrcweir { 202cdf0e10cSrcweir t_print("printBool " ); 203cdf0e10cSrcweir ( sal_True == bOk ) ? t_print("YES!" ): t_print("NO!"); 204cdf0e10cSrcweir t_print("\n"); 205cdf0e10cSrcweir } 206cdf0e10cSrcweir 207cdf0e10cSrcweir /** print content of a ByteSequence. 208cdf0e10cSrcweir */ 209cdf0e10cSrcweir void printByteSequence_IP( const ::rtl::ByteSequence & bsByteSeq, sal_Int32 nLen ) 210cdf0e10cSrcweir { 211cdf0e10cSrcweir t_print("ByteSequence is: " ); 212cdf0e10cSrcweir for ( int i = 0; i < nLen; i++ ){ 213cdf0e10cSrcweir if ( bsByteSeq[i] < 0 ) 214cdf0e10cSrcweir t_print("%d ", 256 + bsByteSeq[i] ); 215cdf0e10cSrcweir else 216cdf0e10cSrcweir t_print("%d ", bsByteSeq[i] ); 217cdf0e10cSrcweir } 218cdf0e10cSrcweir t_print(" .\n" ); 219cdf0e10cSrcweir } 220cdf0e10cSrcweir 221cdf0e10cSrcweir /** convert an IP which is stored as a UString format to a ByteSequence array for later use. 222cdf0e10cSrcweir */ 223cdf0e10cSrcweir ::rtl::ByteSequence UStringIPToByteSequence( ::rtl::OUString aUStr ) 224cdf0e10cSrcweir { 225cdf0e10cSrcweir 226cdf0e10cSrcweir rtl::OString aString = ::rtl::OUStringToOString( aUStr, RTL_TEXTENCODING_ASCII_US ); 227cdf0e10cSrcweir const sal_Char *pChar = aString.getStr( ) ; 228cdf0e10cSrcweir sal_Char tmpBuffer[4]; 229cdf0e10cSrcweir sal_Int32 nCharCounter = 0; 230cdf0e10cSrcweir ::rtl::ByteSequence bsByteSequence( IP_VER ); 231cdf0e10cSrcweir sal_Int32 nByteSeqCounter = 0; 232cdf0e10cSrcweir 233cdf0e10cSrcweir for ( int i = 0; i < aString.getLength( ) + 1 ; i++ ) 234cdf0e10cSrcweir { 235cdf0e10cSrcweir if ( ( *pChar != '.' ) && ( i !=aString.getLength( ) ) ) 236cdf0e10cSrcweir tmpBuffer[nCharCounter++] = *pChar; 237cdf0e10cSrcweir else 238cdf0e10cSrcweir { 239cdf0e10cSrcweir tmpBuffer[nCharCounter] = '\0'; 240cdf0e10cSrcweir nCharCounter = 0; 241cdf0e10cSrcweir bsByteSequence[nByteSeqCounter++] = static_cast<sal_Int8>(atoi( tmpBuffer )); 242cdf0e10cSrcweir } 243cdf0e10cSrcweir pChar++; 244cdf0e10cSrcweir } 245cdf0e10cSrcweir return bsByteSequence; 246cdf0e10cSrcweir } 247cdf0e10cSrcweir 248cdf0e10cSrcweir /** print a socket result name. 249cdf0e10cSrcweir */ 250cdf0e10cSrcweir void printSocketResult( oslSocketResult eResult ) 251cdf0e10cSrcweir { 252cdf0e10cSrcweir t_print("printSocketResult: " ); 253cdf0e10cSrcweir if (!eResult) 254cdf0e10cSrcweir switch (eResult) 255cdf0e10cSrcweir { 256cdf0e10cSrcweir case osl_Socket_Ok: 257cdf0e10cSrcweir t_print("client connected\n"); 258cdf0e10cSrcweir break; 259cdf0e10cSrcweir case osl_Socket_Error: 260cdf0e10cSrcweir t_print("got an error ... exiting\r\n\r\n" ); 261cdf0e10cSrcweir break; 262cdf0e10cSrcweir case osl_Socket_TimedOut: 263cdf0e10cSrcweir t_print("timeout\n"); 264cdf0e10cSrcweir break; 265cdf0e10cSrcweir case osl_Socket_Interrupted: 266cdf0e10cSrcweir t_print("interrupted\n"); 267cdf0e10cSrcweir break; 268cdf0e10cSrcweir case osl_Socket_InProgress: 269cdf0e10cSrcweir t_print("in progress\n"); 270cdf0e10cSrcweir break; 271cdf0e10cSrcweir default: 272cdf0e10cSrcweir t_print("unknown result\n"); 273cdf0e10cSrcweir break; 274cdf0e10cSrcweir } 275cdf0e10cSrcweir } 276cdf0e10cSrcweir 277cdf0e10cSrcweir /** if 4 parts of an IP addr are equal to specified values 278cdf0e10cSrcweir */ 279cdf0e10cSrcweir sal_Bool ifIpv4is( const ::rtl::ByteSequence Ipaddr, sal_Int8 seq1, sal_Int8 seq2, sal_Int8 seq3, sal_Int8 seq4 ) 280cdf0e10cSrcweir { 281cdf0e10cSrcweir if ( ( Ipaddr[0] == seq1 ) && ( Ipaddr[1] == seq2 ) && ( Ipaddr[2] == seq3 ) && ( Ipaddr[3] == seq4 ) ) 282cdf0e10cSrcweir return sal_True; 283cdf0e10cSrcweir return sal_False; 284cdf0e10cSrcweir } 285cdf0e10cSrcweir 286cdf0e10cSrcweir /** if the IP or hostname is availble( alive ) 287cdf0e10cSrcweir */ 288cdf0e10cSrcweir /*sal_Bool ifAvailable( const char * stringAddrOrHostName ) 289cdf0e10cSrcweir { 290cdf0e10cSrcweir sal_Bool result; 291cdf0e10cSrcweir int p[2]; 292cdf0e10cSrcweir sal_Char buffer[2000]; 293cdf0e10cSrcweir char stringhost[20]; 294cdf0e10cSrcweir strcpy(stringhost, stringAddrOrHostName ); 295cdf0e10cSrcweir 296cdf0e10cSrcweir result = sal_False; 297cdf0e10cSrcweir if (pipe (p) == 0) 298cdf0e10cSrcweir { 299cdf0e10cSrcweir pid_t pid; 300cdf0e10cSrcweir int nStatus; 301cdf0e10cSrcweir pid = fork(); 302cdf0e10cSrcweir if (pid == 0) 303cdf0e10cSrcweir { 304cdf0e10cSrcweir #if ( defined LINUX ) 305cdf0e10cSrcweir char *argv[] = 306cdf0e10cSrcweir { 307cdf0e10cSrcweir "/bin/ping", 308cdf0e10cSrcweir "-c", "3", 309cdf0e10cSrcweir "-W", "3", 310cdf0e10cSrcweir stringhost, 311cdf0e10cSrcweir NULL 312cdf0e10cSrcweir }; 313cdf0e10cSrcweir #endif 314cdf0e10cSrcweir #if ( defined SOLARIS ) 315cdf0e10cSrcweir char *argv[] = 316cdf0e10cSrcweir { 317cdf0e10cSrcweir "/usr/sbin/ping", 318cdf0e10cSrcweir stringhost, 319cdf0e10cSrcweir "3", 320cdf0e10cSrcweir NULL 321cdf0e10cSrcweir }; 322cdf0e10cSrcweir #endif 323cdf0e10cSrcweir close (p[0]); 324cdf0e10cSrcweir dup2 (p[1], 1); 325cdf0e10cSrcweir close (p[1]); 326cdf0e10cSrcweir #if ( defined LINUX ) 327cdf0e10cSrcweir execv ("/bin/ping", argv); 328cdf0e10cSrcweir #endif 329cdf0e10cSrcweir #if ( defined SOLARIS ) 330cdf0e10cSrcweir execv ("/usr/sbin/ping", argv); 331cdf0e10cSrcweir #endif 332cdf0e10cSrcweir // arriving here means exec failed 333cdf0e10cSrcweir _exit(-1); 334cdf0e10cSrcweir } 335cdf0e10cSrcweir else if (pid > 0) 336cdf0e10cSrcweir { 337cdf0e10cSrcweir sal_Int32 k = 0, n = 2000; 338cdf0e10cSrcweir close (p[1]); 339cdf0e10cSrcweir if ((k = read (p[0], buffer, n - 1)) > 0) 340cdf0e10cSrcweir { 341cdf0e10cSrcweir buffer[k] = 0; 342cdf0e10cSrcweir if (buffer[k - 1] == '\n') 343cdf0e10cSrcweir buffer[k - 1] = 0; 344cdf0e10cSrcweir #if ( defined LINUX ) 345cdf0e10cSrcweir char strOK[] = "bytes from"; 346cdf0e10cSrcweir #endif 347cdf0e10cSrcweir #if ( defined SOLARIS ) 348cdf0e10cSrcweir char strOK[] = "is alive"; 349cdf0e10cSrcweir #endif 350cdf0e10cSrcweir if (strstr( buffer, strOK ) != NULL ) 351cdf0e10cSrcweir result = sal_True; 352cdf0e10cSrcweir t_print("buffer is %s\n", buffer ); 353cdf0e10cSrcweir } 354cdf0e10cSrcweir close (p[0]); 355cdf0e10cSrcweir waitpid (pid, &nStatus, 0); 356cdf0e10cSrcweir } 357cdf0e10cSrcweir else 358cdf0e10cSrcweir { 359cdf0e10cSrcweir close (p[0]); 360cdf0e10cSrcweir close (p[1]); 361cdf0e10cSrcweir } 362cdf0e10cSrcweir 363cdf0e10cSrcweir } 364cdf0e10cSrcweir return result; 365cdf0e10cSrcweir }*/ 366cdf0e10cSrcweir 367cdf0e10cSrcweir sal_Bool ifAvailable( rtl::OUString const& strAddrOrHostName ) 368cdf0e10cSrcweir { 369cdf0e10cSrcweir ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream ); 370cdf0e10cSrcweir ::osl::SocketAddr aSocketAddr( strAddrOrHostName, 7 ); 371cdf0e10cSrcweir 372cdf0e10cSrcweir if (! aSocketAddr.is()) 373cdf0e10cSrcweir { 374cdf0e10cSrcweir aSocket.close(); 375cdf0e10cSrcweir return sal_False; 376cdf0e10cSrcweir } 377cdf0e10cSrcweir 378cdf0e10cSrcweir aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True; 379cdf0e10cSrcweir 380cdf0e10cSrcweir TimeValue *pTimeout; 381cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) ); 382cdf0e10cSrcweir pTimeout->Seconds = 3; 383cdf0e10cSrcweir pTimeout->Nanosec = 0; 384cdf0e10cSrcweir 385cdf0e10cSrcweir oslSocketResult aResult = aSocket.connect( aSocketAddr, pTimeout ); 386cdf0e10cSrcweir free( pTimeout ); 387cdf0e10cSrcweir aSocket.close(); 388cdf0e10cSrcweir if ( aResult != osl_Socket_Ok ) 389cdf0e10cSrcweir { 390cdf0e10cSrcweir t_print("Error: "); 391cdf0e10cSrcweir printSocketResult(aResult); 392cdf0e10cSrcweir t_print("\n"); 393cdf0e10cSrcweir 394cdf0e10cSrcweir return sal_False; 395cdf0e10cSrcweir } 396cdf0e10cSrcweir return sal_True; 397cdf0e10cSrcweir } 398