1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
29 # define SILENT_TEST 1
33 # define t_print(...) { }
35 # define t_print printf
38 //------------------------------------------------------------------------
39 // Ip version definition
40 //------------------------------------------------------------------------
41 #define IP_VER 4 /// currently only IPv4 is considered.
43 //------------------------------------------------------------------------
45 //------------------------------------------------------------------------
47 /** compare two OUString.
49 sal_Bool
compareUString( const ::rtl::OUString
& ustr1
, const ::rtl::OUString
& ustr2
)
51 sal_Bool bOk
= ustr1
.equalsIgnoreAsciiCase( ustr2
);
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
);
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
)
81 t_print("#%s #printUString_u# ", msg
);
83 aString
= ::rtl::OUStringToOString( str
, RTL_TEXTENCODING_ASCII_US
);
84 t_print("%s\n", aString
.getStr( ) );
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 )
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
);
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
;
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
);
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
);
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();
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());
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
)) );
158 /** get local ethernet IP
160 ::rtl::OUString
getLocalIP( )
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
) )
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("'.");
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 );
193 Sleep( _nSec
* 100 );
195 #if ( defined UNX ) //Unix
196 usleep(_nSec
* 100000);
198 // t_print("# done\n" );
201 /** print Boolean value.
203 void printBool( sal_Bool bOk
)
208 t_print("#printBool# " );
209 t_print ("%s", (sal_True
== bOk
) ? "YES!\n" : "NO!\n");
213 /** print content of a ByteSequence.
215 void printByteSequence_IP( const ::rtl::ByteSequence
& bsByteSeq
, sal_Int32 nLen
)
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
] );
226 t_print("%d ", bsByteSeq
[i
] );
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
;
250 tmpBuffer
[nCharCounter
] = '\0';
252 bsByteSequence
[nByteSeqCounter
++] = static_cast<sal_Int8
>(atoi( tmpBuffer
));
256 return bsByteSequence
;
259 /** print a socket result name.
261 void printSocketResult( oslSocketResult eResult
)
263 t_print("printSocketResult: " );
268 t_print("client connected\n");
270 case osl_Socket_Error
:
271 t_print("got an error ... exiting\r\n\r\n" );
273 case osl_Socket_TimedOut
:
274 t_print("timeout\n");
276 case osl_Socket_Interrupted
:
277 t_print("interrupted\n");
279 case osl_Socket_InProgress
:
280 t_print("in progress\n");
283 t_print("unknown result\n");
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
) )
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())
308 aSocket
.setOption( osl_Socket_OptionReuseAddr
, 1 ); //sal_True;
311 pTimeout
= ( TimeValue
* )malloc( sizeof( TimeValue
) );
312 pTimeout
->Seconds
= 3;
313 pTimeout
->Nanosec
= 0;
315 oslSocketResult aResult
= aSocket
.connect( aSocketAddr
, pTimeout
);
318 if ( aResult
!= osl_Socket_Ok
)
321 printSocketResult(aResult
);
329 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */