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 // Ip version definition
40 #define IP_VER 4 /// currently only IPv4 is considered.
44 /** compare two OUString.
46 sal_Bool
compareUString( const ::rtl::OUString
& ustr1
, const ::rtl::OUString
& ustr2
)
48 sal_Bool bOk
= ustr1
.equalsIgnoreAsciiCase( ustr2
);
53 /** compare a OUString and an ASCII string.
55 sal_Bool
compareUString( const ::rtl::OUString
& ustr
, const sal_Char
*astr
)
57 ::rtl::OUString ustr2
= rtl::OUString::createFromAscii( astr
);
58 sal_Bool bOk
= ustr
.equalsIgnoreAsciiCase( ustr2
);
63 /** compare two socket address.
65 sal_Bool
compareSocketAddr( const ::osl::SocketAddr
& addr1
, const ::osl::SocketAddr
& addr2
)
67 return ( ( sal_True
== compareUString( addr1
.getHostname( 0 ), addr2
.getHostname( 0 ) ) ) && ( addr2
.getPort( ) == addr2
.getPort( ) ) );
70 /** print a UNI_CODE String. And also print some comments of the string.
72 void printUString( const ::rtl::OUString
& str
, const char* msg
)
78 t_print("#%s #printUString_u# ", msg
);
80 aString
= ::rtl::OUStringToOString( str
, RTL_TEXTENCODING_ASCII_US
);
81 t_print("%s\n", aString
.getStr( ) );
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
89 ::rtl::OUString
getHost()
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
);
100 /** get the full host name of the current processor, such as "aegean.prc.sun.com" --mindyliu
102 ::rtl::OUString
getThisHostname()
104 ::rtl::OUString aUString
;
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
);
113 if (gethostname(hostname
, 255) != 0) {
114 OSL_FAIL( "#Error: gethostname failed." );
117 struct hostent
*hptr
;
118 //first search /ets/hosts, then search from dns
119 hptr
= gethostbyname( hostname
);
122 strcpy( hostname
, hptr
->h_name
);
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();
133 /** get IP by name, search /etc/hosts first, then search from dns, fail return OUString("")
135 ::rtl::OUString
getIPbyName( rtl::OString
const& str_name
)
137 ::rtl::OUString aUString
;
138 struct hostent
*hptr
;
139 //first search /ets/hosts, then search from dns
140 hptr
= gethostbyname( str_name
.getStr());
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
++)
148 t_print("#Local IP Address: %s\n", inet_ntoa(**addrptr
));
149 aUString
= ::rtl::OUString::createFromAscii( (sal_Char
*) (inet_ntoa(**addrptr
)) );
155 /** get local ethernet IP
157 ::rtl::OUString
getLocalIP( )
160 gethostname(hostname
, 255);
162 return getIPbyName( hostname
);
165 /** construct error message
167 ::rtl::OUString
outputError( const ::rtl::OUString
& returnVal
, const ::rtl::OUString
& rightVal
, const sal_Char
* msg
)
169 ::rtl::OUString aUString
;
170 if ( returnVal
.equals( rightVal
) )
172 aUString
+= ::rtl::OUString::createFromAscii(msg
);
173 aUString
+= ::rtl::OUString(": the returned value is '");
174 aUString
+= returnVal
;
175 aUString
+= ::rtl::OUString("', but the value should be '");
176 aUString
+= rightVal
;
177 aUString
+= ::rtl::OUString("'.");
181 /** wait _nSec seconds.
183 void thread_sleep( sal_Int32 _nSec
)
185 /// print statement in thread process must use fflush() to force display.
186 // printf("wait %d seconds. ", _nSec );
190 Sleep( _nSec
* 100 );
192 #if ( defined UNX ) //Unix
193 usleep(_nSec
* 100000);
195 // t_print("# done\n" );
198 /** print Boolean value.
200 void printBool( sal_Bool bOk
)
205 t_print("#printBool# " );
206 t_print ("%s", (sal_True
== bOk
) ? "YES!\n" : "NO!\n");
210 /** print content of a ByteSequence.
212 void printByteSequence_IP( const ::rtl::ByteSequence
& bsByteSeq
, sal_Int32 nLen
)
218 t_print("#ByteSequence is: " );
219 for ( int i
= 0; i
< nLen
; i
++ ){
220 if ( bsByteSeq
[i
] < 0 )
221 t_print("%d ", 256 + bsByteSeq
[i
] );
223 t_print("%d ", bsByteSeq
[i
] );
229 /** convert an IP which is stored as a UString format to a ByteSequence array for later use.
231 ::rtl::ByteSequence
UStringIPToByteSequence( ::rtl::OUString aUStr
)
234 rtl::OString aString
= ::rtl::OUStringToOString( aUStr
, RTL_TEXTENCODING_ASCII_US
);
235 const sal_Char
*pChar
= aString
.getStr( ) ;
236 sal_Char tmpBuffer
[4];
237 sal_Int32 nCharCounter
= 0;
238 ::rtl::ByteSequence
bsByteSequence( IP_VER
);
239 sal_Int32 nByteSeqCounter
= 0;
241 for ( int i
= 0; i
< aString
.getLength( ) + 1 ; i
++ )
243 if ( ( *pChar
!= '.' ) && ( i
!=aString
.getLength( ) ) )
244 tmpBuffer
[nCharCounter
++] = *pChar
;
247 tmpBuffer
[nCharCounter
] = '\0';
249 bsByteSequence
[nByteSeqCounter
++] = static_cast<sal_Int8
>(atoi( tmpBuffer
));
253 return bsByteSequence
;
256 /** print a socket result name.
258 void printSocketResult( oslSocketResult eResult
)
260 t_print("printSocketResult: " );
265 t_print("client connected\n");
267 case osl_Socket_Error
:
268 t_print("got an error ... exiting\r\n\r\n" );
270 case osl_Socket_TimedOut
:
271 t_print("timeout\n");
273 case osl_Socket_Interrupted
:
274 t_print("interrupted\n");
276 case osl_Socket_InProgress
:
277 t_print("in progress\n");
280 t_print("unknown result\n");
285 /** if 4 parts of an IP addr are equal to specified values
287 sal_Bool
ifIpv4is( const ::rtl::ByteSequence Ipaddr
, sal_Int8 seq1
, sal_Int8 seq2
, sal_Int8 seq3
, sal_Int8 seq4
)
289 if ( ( Ipaddr
[0] == seq1
) && ( Ipaddr
[1] == seq2
) && ( Ipaddr
[2] == seq3
) && ( Ipaddr
[3] == seq4
) )
294 sal_Bool
ifAvailable( rtl::OUString
const& strAddrOrHostName
)
296 ::osl::ConnectorSocket
aSocket( osl_Socket_FamilyInet
, osl_Socket_ProtocolIp
, osl_Socket_TypeStream
);
297 ::osl::SocketAddr
aSocketAddr( strAddrOrHostName
, 7 );
299 if (! aSocketAddr
.is())
305 aSocket
.setOption( osl_Socket_OptionReuseAddr
, 1 ); //sal_True;
308 pTimeout
= ( TimeValue
* )malloc( sizeof( TimeValue
) );
309 pTimeout
->Seconds
= 3;
310 pTimeout
->Nanosec
= 0;
312 oslSocketResult aResult
= aSocket
.connect( aSocketAddr
, pTimeout
);
315 if ( aResult
!= osl_Socket_Ok
)
318 printSocketResult(aResult
);
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */