Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / sal / qa / osl / socket / sockethelper.cxx
blob217cc89afabc49feff94df233d603814aa9e9b19
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/types.h>
21 #include "sockethelper.hxx"
22 #include <cppunit/TestFixture.h>
23 #include <cppunit/extensions/HelperMacros.h>
24 #include <cppunit/plugin/TestPlugIn.h>
26 #if OSL_DEBUG_LEVEL > 0
27 # define SILENT_TEST 0
28 #else
29 # define SILENT_TEST 1
30 #endif
32 #if SILENT_TEST
33 # define t_print(...) { }
34 #else
35 # define t_print printf
36 #endif
38 //------------------------------------------------------------------------
39 // Ip version definition
40 //------------------------------------------------------------------------
41 #define IP_VER 4 /// currently only IPv4 is considered.
43 //------------------------------------------------------------------------
44 // helper functions
45 //------------------------------------------------------------------------
47 /** compare two OUString.
49 sal_Bool compareUString( const ::rtl::OUString & ustr1, const ::rtl::OUString & ustr2 )
51 sal_Bool bOk = ustr1.equalsIgnoreAsciiCase( ustr2 );
53 return bOk;
56 /** compare a OUString and an ASCII string.
58 sal_Bool compareUString( const ::rtl::OUString & ustr, const sal_Char *astr )
60 ::rtl::OUString ustr2 = rtl::OUString::createFromAscii( astr );
61 sal_Bool bOk = ustr.equalsIgnoreAsciiCase( ustr2 );
63 return bOk;
66 /** compare two socket address.
68 sal_Bool compareSocketAddr( const ::osl::SocketAddr & addr1 , const ::osl::SocketAddr & addr2 )
70 return ( ( sal_True == compareUString( addr1.getHostname( 0 ), addr2.getHostname( 0 ) ) ) && ( addr2.getPort( ) == addr2.getPort( ) ) );
73 /** print a UNI_CODE String. And also print some comments of the string.
75 void printUString( const ::rtl::OUString & str, const char* msg)
77 #if SILENT_TEST
78 (void)str;
79 (void)msg;
80 #else
81 t_print("#%s #printUString_u# ", msg );
82 rtl::OString aString;
83 aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
84 t_print("%s\n", aString.getStr( ) );
85 #endif
88 /** get the local host name.
89 mindy: gethostbyname( "localhost" ), on Linux, it returns the hostname in /etc/hosts + domain name,
90 if no entry in /etc/hosts, it returns "localhost" + domain name
92 ::rtl::OUString getHost( void )
94 struct hostent *hptr;
96 hptr = gethostbyname( "localhost" );
97 OSL_ENSURE( hptr != NULL, "#In getHostname function, error on gethostbyname()" );
98 ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) hptr->h_name );
100 return aUString;
103 /** get the full host name of the current processor, such as "aegean.prc.sun.com" --mindyliu
105 ::rtl::OUString getThisHostname( void )
107 ::rtl::OUString aUString;
108 #ifdef WNT
109 struct hostent *hptr;
110 hptr = gethostbyname( "localhost" );
111 OSL_ENSURE( hptr != NULL, "#In getHostname function, error on gethostbyname()" );
112 rtl::OString sHostname(hptr->h_name);
113 aUString = ::rtl::OStringToOUString(sHostname, RTL_TEXTENCODING_ASCII_US);
114 #else
115 char hostname[255];
116 if (gethostname(hostname, 255) != 0) {
117 OSL_FAIL( "#Error: gethostname failed." );
120 struct hostent *hptr;
121 //first search /ets/hosts, then search from dns
122 hptr = gethostbyname( hostname);
123 if ( hptr != NULL )
125 strcpy( hostname, hptr->h_name );
128 t_print("hostname is %s \n", hostname );
129 rtl::OString sHostname( hostname );
130 aUString = ::rtl::OStringToOUString( sHostname, RTL_TEXTENCODING_ASCII_US );
131 aUString.getLength();
132 #endif
133 return aUString;
136 /** get IP by name, search /etc/hosts first, then search from dns, fail return OUString("")
138 ::rtl::OUString getIPbyName( rtl::OString const& str_name )
140 ::rtl::OUString aUString;
141 struct hostent *hptr;
142 //first search /ets/hosts, then search from dns
143 hptr = gethostbyname( str_name.getStr());
144 if ( hptr != NULL )
146 struct in_addr ** addrptr;
147 addrptr = (struct in_addr **) hptr->h_addr_list ;
148 //if there are more than one IPs on the same machine, we select one
149 for (; *addrptr; addrptr++)
151 t_print("#Local IP Address: %s\n", inet_ntoa(**addrptr));
152 aUString = ::rtl::OUString::createFromAscii( (sal_Char *) (inet_ntoa(**addrptr)) );
155 return aUString;
158 /** get local ethernet IP
160 ::rtl::OUString getLocalIP( )
162 char hostname[255];
163 gethostname(hostname, 255);
165 return getIPbyName( hostname );
168 /** construct error message
170 ::rtl::OUString outputError( const ::rtl::OUString & returnVal, const ::rtl::OUString & rightVal, const sal_Char * msg )
172 ::rtl::OUString aUString;
173 if ( returnVal.equals( rightVal ) )
174 return aUString;
175 aUString += ::rtl::OUString::createFromAscii(msg);
176 aUString += ::rtl::OUString(": the returned value is '");
177 aUString += returnVal;
178 aUString += ::rtl::OUString("', but the value should be '");
179 aUString += rightVal;
180 aUString += ::rtl::OUString("'.");
181 return aUString;
184 /** wait _nSec seconds.
186 void thread_sleep( sal_Int32 _nSec )
188 /// print statement in thread process must use fflush() to force display.
189 // printf("wait %d seconds. ", _nSec );
190 // fflush(stdout);
192 #ifdef WNT //Windows
193 Sleep( _nSec * 100 );
194 #endif
195 #if ( defined UNX ) //Unix
196 usleep(_nSec * 100000);
197 #endif
198 // t_print("# done\n" );
201 /** print Boolean value.
203 void printBool( sal_Bool bOk )
205 #if SILENT_TEST
206 (void)bOk;
207 #else
208 t_print("#printBool# " );
209 t_print ("%s", (sal_True == bOk) ? "YES!\n" : "NO!\n");
210 #endif
213 /** print content of a ByteSequence.
215 void printByteSequence_IP( const ::rtl::ByteSequence & bsByteSeq, sal_Int32 nLen )
217 #if SILENT_TEST
218 (void)bsByteSeq;
219 (void)nLen;
220 #else
221 t_print("#ByteSequence is: " );
222 for ( int i = 0; i < nLen; i++ ){
223 if ( bsByteSeq[i] < 0 )
224 t_print("%d ", 256 + bsByteSeq[i] );
225 else
226 t_print("%d ", bsByteSeq[i] );
228 t_print(" .\n" );
229 #endif
232 /** convert an IP which is stored as a UString format to a ByteSequence array for later use.
234 ::rtl::ByteSequence UStringIPToByteSequence( ::rtl::OUString aUStr )
237 rtl::OString aString = ::rtl::OUStringToOString( aUStr, RTL_TEXTENCODING_ASCII_US );
238 const sal_Char *pChar = aString.getStr( ) ;
239 sal_Char tmpBuffer[4];
240 sal_Int32 nCharCounter = 0;
241 ::rtl::ByteSequence bsByteSequence( IP_VER );
242 sal_Int32 nByteSeqCounter = 0;
244 for ( int i = 0; i < aString.getLength( ) + 1 ; i++ )
246 if ( ( *pChar != '.' ) && ( i !=aString.getLength( ) ) )
247 tmpBuffer[nCharCounter++] = *pChar;
248 else
250 tmpBuffer[nCharCounter] = '\0';
251 nCharCounter = 0;
252 bsByteSequence[nByteSeqCounter++] = static_cast<sal_Int8>(atoi( tmpBuffer ));
254 pChar++;
256 return bsByteSequence;
259 /** print a socket result name.
261 void printSocketResult( oslSocketResult eResult )
263 t_print("printSocketResult: " );
264 if (!eResult)
265 switch (eResult)
267 case osl_Socket_Ok:
268 t_print("client connected\n");
269 break;
270 case osl_Socket_Error:
271 t_print("got an error ... exiting\r\n\r\n" );
272 break;
273 case osl_Socket_TimedOut:
274 t_print("timeout\n");
275 break;
276 case osl_Socket_Interrupted:
277 t_print("interrupted\n");
278 break;
279 case osl_Socket_InProgress:
280 t_print("in progress\n");
281 break;
282 default:
283 t_print("unknown result\n");
284 break;
288 /** if 4 parts of an IP addr are equal to specified values
290 sal_Bool ifIpv4is( const ::rtl::ByteSequence Ipaddr, sal_Int8 seq1, sal_Int8 seq2, sal_Int8 seq3, sal_Int8 seq4 )
292 if ( ( Ipaddr[0] == seq1 ) && ( Ipaddr[1] == seq2 ) && ( Ipaddr[2] == seq3 ) && ( Ipaddr[3] == seq4 ) )
293 return sal_True;
294 return sal_False;
297 sal_Bool ifAvailable( rtl::OUString const& strAddrOrHostName )
299 ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
300 ::osl::SocketAddr aSocketAddr( strAddrOrHostName, 7 );
302 if (! aSocketAddr.is())
304 aSocket.close();
305 return sal_False;
308 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
310 TimeValue *pTimeout;
311 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
312 pTimeout->Seconds = 3;
313 pTimeout->Nanosec = 0;
315 oslSocketResult aResult = aSocket.connect( aSocketAddr, pTimeout );
316 free( pTimeout );
317 aSocket.close();
318 if ( aResult != osl_Socket_Ok )
320 t_print("Error: ");
321 printSocketResult(aResult);
322 t_print("\n");
324 return sal_False;
326 return sal_True;
329 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */