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