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