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