xref: /aoo42x/main/sal/inc/osl/socket.hxx (revision 565d668c)
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 #ifndef _OSL_SOCKET_HXX_
24 #define _OSL_SOCKET_HXX_
25 
26 #include <osl/socket_decl.hxx>
27 
28 namespace osl
29 {
30     //______________________________________________________________________________
SocketAddr()31 	inline SocketAddr::SocketAddr()
32 		: m_handle( osl_createEmptySocketAddr( osl_Socket_FamilyInet ) )
33 	{}
34 
35 	//______________________________________________________________________________
SocketAddr(const SocketAddr & Addr)36 	inline SocketAddr::SocketAddr(const SocketAddr& Addr)
37 		: m_handle( osl_copySocketAddr( Addr.m_handle ) )
38 	{
39 	}
40 
41 	//______________________________________________________________________________
SocketAddr(oslSocketAddr Addr)42 	inline SocketAddr::SocketAddr(oslSocketAddr Addr)
43 		: m_handle( osl_copySocketAddr( Addr ) )
44 	{
45 	}
46 
47 	//______________________________________________________________________________
SocketAddr(oslSocketAddr Addr,__osl_socket_NoCopy)48 	inline SocketAddr::SocketAddr(oslSocketAddr Addr, __osl_socket_NoCopy )
49 		: m_handle( Addr )
50 	{
51 	}
52 
53 	//______________________________________________________________________________
SocketAddr(const::rtl::OUString & strAddrOrHostName,sal_Int32 nPort)54 	inline SocketAddr::SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort)
55 		: m_handle( osl_createInetSocketAddr( strAddrOrHostName.pData, nPort ) )
56 	{
57 		if(! m_handle )
58 		{
59 			m_handle = osl_resolveHostname(strAddrOrHostName.pData);
60 
61 			// host found?
62 			if(m_handle)
63 			{
64 				osl_setInetPortOfSocketAddr(m_handle, nPort);
65 			}
66 			else
67 			{
68 				osl_destroySocketAddr( m_handle );
69 				m_handle = 0;
70 			}
71 		}
72 	}
73 
74 	//______________________________________________________________________________
~SocketAddr()75 	inline SocketAddr::~SocketAddr()
76 	{
77 		if( m_handle )
78 			osl_destroySocketAddr( m_handle );
79 	}
80 
81 	//______________________________________________________________________________
getHostname(oslSocketResult * pResult) const82 	inline ::rtl::OUString SocketAddr::getHostname( oslSocketResult *pResult ) const
83 	{
84 		::rtl::OUString hostname;
85 		oslSocketResult result = osl_getHostnameOfSocketAddr( m_handle, &(hostname.pData) );
86 		if( pResult )
87 			*pResult = result;
88 		return hostname;
89 	}
90 
91 	//______________________________________________________________________________
getPort() const92 	inline sal_Int32 SAL_CALL SocketAddr::getPort() const
93 	{
94 		return osl_getInetPortOfSocketAddr(m_handle);
95 	}
96 
97 	//______________________________________________________________________________
setPort(sal_Int32 nPort)98 	inline sal_Bool SAL_CALL SocketAddr::setPort( sal_Int32 nPort )
99 	{
100 		return osl_setInetPortOfSocketAddr(m_handle, nPort );
101 	}
102 
setHostname(const::rtl::OUString & sDottedIpOrHostname)103 	inline sal_Bool SAL_CALL SocketAddr::setHostname( const ::rtl::OUString &sDottedIpOrHostname )
104 	{
105 		*this = SocketAddr( sDottedIpOrHostname , getPort() );
106 		return is();
107 	}
108 
109 	//______________________________________________________________________________
setAddr(const::rtl::ByteSequence & address)110 	inline sal_Bool SAL_CALL SocketAddr::setAddr( const ::rtl::ByteSequence & address )
111 	{
112 		return osl_setAddrOfSocketAddr( m_handle, address.getHandle() )
113             == osl_Socket_Ok;
114 	}
115 
getAddr(oslSocketResult * pResult) const116 	inline ::rtl::ByteSequence SAL_CALL SocketAddr::getAddr( oslSocketResult *pResult ) const
117 	{
118 		::rtl::ByteSequence sequence;
119 		oslSocketResult result = osl_getAddrOfSocketAddr( m_handle,(sal_Sequence **) &sequence );
120 		if( pResult )
121 			*pResult = result;
122 		return sequence;
123 	}
124 
125 	//______________________________________________________________________________
operator =(oslSocketAddr Addr)126 	inline SocketAddr & SAL_CALL SocketAddr::operator= (oslSocketAddr Addr)
127 	{
128 		oslSocketAddr pNewAddr = osl_copySocketAddr( Addr );
129 		if( m_handle )
130 			osl_destroySocketAddr( m_handle );
131 		m_handle = pNewAddr;
132 		return *this;
133 	}
134 
135 	//______________________________________________________________________________
operator =(const SocketAddr & Addr)136 	inline SocketAddr & SAL_CALL SocketAddr::operator= (const SocketAddr& Addr)
137 	{
138 		*this = (Addr.getHandle());
139 		return *this;
140 	}
141 
assign(oslSocketAddr Addr,__osl_socket_NoCopy)142 	inline SocketAddr & SAL_CALL SocketAddr::assign( oslSocketAddr Addr, __osl_socket_NoCopy )
143 	{
144 		if( m_handle )
145 			osl_destroySocketAddr( m_handle );
146 		m_handle = Addr;
147 		return *this;
148 	}
149 
150 	//______________________________________________________________________________
operator ==(oslSocketAddr Addr) const151 	inline sal_Bool SAL_CALL SocketAddr::operator== (oslSocketAddr Addr) const
152 	{
153 		return osl_isEqualSocketAddr( m_handle, Addr );
154 	}
155 
getHandle() const156 	inline oslSocketAddr SocketAddr::getHandle() const
157 	{
158 		return m_handle;
159 	}
160 
161 	//______________________________________________________________________________
is() const162 	inline sal_Bool SocketAddr::is() const
163 	{
164 		return m_handle != 0;
165 	}
166 
167 	// (static method)______________________________________________________________
getLocalHostname(oslSocketResult * pResult)168 	inline ::rtl::OUString SAL_CALL SocketAddr::getLocalHostname( oslSocketResult *pResult )
169 	{
170 		::rtl::OUString hostname;
171 		oslSocketResult result = osl_getLocalHostname( &(hostname.pData) );
172 		if(pResult )
173 			*pResult = result;
174 		return hostname;
175 	}
176 
177 	// (static method)______________________________________________________________
resolveHostname(const::rtl::OUString & strHostName,SocketAddr & Addr)178 	inline void SAL_CALL SocketAddr::resolveHostname(
179 		const ::rtl::OUString & strHostName, SocketAddr &Addr)
180 	{
181 		Addr = SocketAddr( osl_resolveHostname( strHostName.pData ) , SAL_NO_COPY );
182 	}
183 
184 	// (static method)______________________________________________________________
getServicePort(const::rtl::OUString & strServiceName,const::rtl::OUString & strProtocolName)185 	inline sal_Int32 SAL_CALL SocketAddr::getServicePort(
186 			const ::rtl::OUString& strServiceName,
187 			const ::rtl::OUString & strProtocolName )
188 	{
189 		return osl_getServicePort( strServiceName.pData, strProtocolName.pData );
190 	}
191 
192 	//______________________________________________________________________________
Socket(oslSocketType Type,oslAddrFamily Family,oslProtocol Protocol)193 	inline Socket::Socket(oslSocketType Type,
194 						  oslAddrFamily Family,
195 						  oslProtocol   Protocol)
196 		: m_handle( osl_createSocket(Family, Type, Protocol) )
197 	{}
198 
199 	//______________________________________________________________________________
Socket(oslSocket socketHandle,__sal_NoAcquire)200 	inline Socket::Socket( oslSocket socketHandle, __sal_NoAcquire )
201 		: m_handle( socketHandle )
202 	{}
203 
204 	//______________________________________________________________________________
Socket(oslSocket socketHandle)205 	inline Socket::Socket( oslSocket socketHandle )
206 		: m_handle( socketHandle )
207 	{
208 		osl_acquireSocket( m_handle );
209 	}
210 
211 	//______________________________________________________________________________
Socket(const Socket & socket)212 	inline Socket::Socket( const Socket & socket )
213 		: m_handle( socket.getHandle() )
214 	{
215 		osl_acquireSocket( m_handle );
216 	}
217 
218 	//______________________________________________________________________________
~Socket()219 	inline Socket::~Socket()
220 	{
221 		osl_releaseSocket( m_handle );
222 	}
223 
224 	//______________________________________________________________________________
operator =(oslSocket socketHandle)225 	inline Socket& Socket::operator= ( oslSocket socketHandle)
226 	{
227 		osl_acquireSocket( socketHandle );
228 		osl_releaseSocket( m_handle );
229 		m_handle = socketHandle;
230 		return *this;
231 	}
232 
233 	//______________________________________________________________________________
operator =(const Socket & sock)234 	inline Socket&  Socket::operator= (const Socket& sock)
235 	{
236 		return (*this) = sock.getHandle();
237 	}
238 
239 	//______________________________________________________________________________
operator ==(const Socket & rSocket) const240 	inline sal_Bool Socket::operator==( const Socket& rSocket ) const
241 	{
242 		return m_handle == rSocket.getHandle();
243 	}
244 
245 	//______________________________________________________________________________
operator ==(const oslSocket socketHandle) const246 	inline sal_Bool Socket::operator==( const oslSocket socketHandle ) const
247 	{
248 		return m_handle == socketHandle;
249 	}
250 
251 	//______________________________________________________________________________
shutdown(oslSocketDirection Direction)252 	inline void Socket::shutdown( oslSocketDirection Direction )
253 	{
254 		osl_shutdownSocket( m_handle , Direction );
255 	}
256 
257 	//______________________________________________________________________________
close()258 	inline void Socket::close()
259 	{
260 		osl_closeSocket( m_handle );
261 	}
262 
263 	//______________________________________________________________________________
getLocalAddr(SocketAddr & addr) const264 	inline void Socket::getLocalAddr( SocketAddr & addr) const
265 	{
266 		addr.assign( osl_getLocalAddrOfSocket( m_handle ) , SAL_NO_COPY );
267 	}
268 
269 	//______________________________________________________________________________
getLocalPort() const270 	inline sal_Int32 Socket::getLocalPort() const
271 	{
272 		SocketAddr addr( 0 );
273 		getLocalAddr( addr );
274 		return addr.getPort();
275 	}
276 
277 	//______________________________________________________________________________
getLocalHost() const278 	inline ::rtl::OUString Socket::getLocalHost() const
279 	{
280 		SocketAddr addr( 0 );
281 		getLocalAddr( addr );
282 		return addr.getHostname();
283 	}
284 
285 	//______________________________________________________________________________
getPeerAddr(SocketAddr & addr) const286 	inline void Socket::getPeerAddr( SocketAddr &addr ) const
287 	{
288 		addr.assign( osl_getPeerAddrOfSocket( m_handle ), SAL_NO_COPY );
289 	}
290 
291 	//______________________________________________________________________________
getPeerPort() const292 	inline sal_Int32 Socket::getPeerPort() const
293 	{
294 		SocketAddr addr( 0 );
295 		getPeerAddr( addr );
296 		return addr.getPort();
297 	}
298 
299 	//______________________________________________________________________________
getPeerHost() const300 	inline ::rtl::OUString Socket::getPeerHost() const
301 	{
302 		SocketAddr addr( 0 );
303 		getPeerAddr( addr );
304 		return addr.getHostname();
305 	}
306 
307 	//______________________________________________________________________________
bind(const SocketAddr & LocalInterface)308 	inline sal_Bool Socket::bind(const SocketAddr& LocalInterface)
309 	{
310 		return osl_bindAddrToSocket( m_handle , LocalInterface.getHandle() );
311 	}
312 
313 	//______________________________________________________________________________
isRecvReady(const TimeValue * pTimeout) const314 	inline sal_Bool	Socket::isRecvReady(const TimeValue *pTimeout ) const
315 	{
316 		return osl_isReceiveReady( m_handle , pTimeout );
317 	}
318 
319 	//______________________________________________________________________________
isSendReady(const TimeValue * pTimeout) const320 	inline sal_Bool	Socket::isSendReady(const TimeValue *pTimeout ) const
321 	{
322 		return osl_isSendReady( m_handle, pTimeout );
323 	}
324 
325 	//______________________________________________________________________________
isExceptionPending(const TimeValue * pTimeout) const326 	inline sal_Bool	Socket::isExceptionPending(const TimeValue *pTimeout ) const
327 	{
328 		return osl_isExceptionPending( m_handle, pTimeout );
329 	}
330 
331 	//______________________________________________________________________________
getType() const332 	inline oslSocketType Socket::getType() const
333 	{
334 		return osl_getSocketType( m_handle );
335 	}
336 
337 	//______________________________________________________________________________
getOption(oslSocketOption Option,void * pBuffer,sal_uInt32 BufferLen,oslSocketOptionLevel Level) const338 	inline sal_Int32  Socket::getOption(
339 		oslSocketOption Option,
340 		void* pBuffer,
341 		sal_uInt32 BufferLen,
342 		oslSocketOptionLevel Level) const
343 	{
344 		return osl_getSocketOption( m_handle, Level, Option, pBuffer , BufferLen );
345 	}
346 
347 	//______________________________________________________________________________
setOption(oslSocketOption Option,void * pBuffer,sal_uInt32 BufferLen,oslSocketOptionLevel Level) const348 	inline sal_Bool Socket::setOption(	oslSocketOption Option,
349 										void* pBuffer,
350 										sal_uInt32 BufferLen,
351 										oslSocketOptionLevel Level ) const
352 	{
353 		return osl_setSocketOption( m_handle, Level, Option , pBuffer, BufferLen );
354 	}
355 
356 	//______________________________________________________________________________
setOption(oslSocketOption option,sal_Int32 nValue)357 	inline sal_Bool Socket::setOption( oslSocketOption option, sal_Int32 nValue  )
358 	{
359 		return setOption( option, &nValue, sizeof( nValue ) );
360 	}
361 
362 	//______________________________________________________________________________
getOption(oslSocketOption option) const363 	inline sal_Int32 Socket::getOption( oslSocketOption option ) const
364 	{
365 		sal_Int32 n;
366 		getOption( option, &n, sizeof( n ) );
367 		return n;
368 	}
369 
370 	//______________________________________________________________________________
enableNonBlockingMode(sal_Bool bNonBlockingMode)371 	inline sal_Bool Socket::enableNonBlockingMode( sal_Bool bNonBlockingMode)
372 	{
373 		return osl_enableNonBlockingMode( m_handle , bNonBlockingMode );
374 	}
375 
376 	//______________________________________________________________________________
isNonBlockingMode() const377 	inline sal_Bool Socket::isNonBlockingMode() const
378 	{
379 		return osl_isNonBlockingMode( m_handle );
380 	}
381 
382 	//______________________________________________________________________________
clearError() const383 	inline void	SAL_CALL Socket::clearError() const
384 	{
385 		sal_Int32 err = 0;
386 		getOption(osl_Socket_OptionError, &err, sizeof(err));
387 	}
388 
389 	//______________________________________________________________________________
getError() const390 	inline oslSocketError Socket::getError() const
391 	{
392 		return osl_getLastSocketError( m_handle );
393 	}
394 
395 	//______________________________________________________________________________
getErrorAsString() const396 	inline ::rtl::OUString Socket::getErrorAsString( ) const
397 	{
398 		::rtl::OUString error;
399 		osl_getLastSocketErrorDescription( m_handle, &(error.pData) );
400 		return error;
401 	}
402 
403 	//______________________________________________________________________________
getHandle() const404 	inline oslSocket Socket::getHandle() const
405 	{
406 		return m_handle;
407 	}
408 
409 	//______________________________________________________________________________
StreamSocket(oslAddrFamily Family,oslProtocol Protocol,oslSocketType Type)410 	inline StreamSocket::StreamSocket(oslAddrFamily Family,
411 									  oslProtocol Protocol,
412 									  oslSocketType	Type )
413 		: Socket( Type, Family, Protocol )
414 	{}
415 
416 	//______________________________________________________________________________
StreamSocket(oslSocket socketHandle,__sal_NoAcquire noacquire)417 	inline StreamSocket::StreamSocket( oslSocket socketHandle, __sal_NoAcquire noacquire )
418 		: Socket( socketHandle, noacquire )
419 	{}
420 
421 	//______________________________________________________________________________
StreamSocket(oslSocket socketHandle)422 	inline StreamSocket::StreamSocket( oslSocket socketHandle )
423 		: Socket( socketHandle )
424 	{}
425 
426 	//______________________________________________________________________________
StreamSocket(const StreamSocket & socket)427 	inline StreamSocket::StreamSocket( const StreamSocket & socket )
428 		: Socket( socket )
429 	{}
430 
431     //______________________________________________________________________________
read(void * pBuffer,sal_uInt32 n)432 	inline sal_Int32 StreamSocket::read(void* pBuffer, sal_uInt32 n)
433 	{
434 		return osl_readSocket( m_handle, pBuffer, n );
435 	}
436 
437 	//______________________________________________________________________________
write(const void * pBuffer,sal_uInt32 n)438 	inline sal_Int32 StreamSocket::write(const void* pBuffer, sal_uInt32 n)
439 	{
440 		return osl_writeSocket( m_handle, pBuffer, n );
441 	}
442 
443 
444 	//______________________________________________________________________________
recv(void * pBuffer,sal_uInt32 BytesToRead,oslSocketMsgFlag Flag)445 	inline sal_Int32 StreamSocket::recv(void* pBuffer,
446 										sal_uInt32 BytesToRead,
447 										oslSocketMsgFlag Flag)
448 	{
449 		return osl_receiveSocket( m_handle, pBuffer,BytesToRead, Flag );
450 	}
451 
452 	//______________________________________________________________________________
send(const void * pBuffer,sal_uInt32 BytesToSend,oslSocketMsgFlag Flag)453 	inline sal_Int32 StreamSocket::send(const void* pBuffer,
454 										sal_uInt32 BytesToSend,
455 										oslSocketMsgFlag Flag)
456 	{
457 		return osl_sendSocket( m_handle, pBuffer, BytesToSend, Flag );
458 	}
459 
460 	//______________________________________________________________________________
ConnectorSocket(oslAddrFamily Family,oslProtocol Protocol,oslSocketType Type)461   	inline ConnectorSocket::ConnectorSocket(oslAddrFamily Family,
462 											oslProtocol	Protocol,
463 											oslSocketType	Type)
464 		: StreamSocket( Family, Protocol ,Type )
465 	{}
466 
467 	//______________________________________________________________________________
connect(const SocketAddr & TargetHost,const TimeValue * pTimeout)468 	inline oslSocketResult ConnectorSocket::connect( const SocketAddr& TargetHost,
469 													 const TimeValue* pTimeout )
470 	{
471 		return osl_connectSocketTo( m_handle , TargetHost.getHandle(), pTimeout );
472 	}
473 
474 	//______________________________________________________________________________
AcceptorSocket(oslAddrFamily Family,oslProtocol Protocol,oslSocketType Type)475 	inline AcceptorSocket::AcceptorSocket(oslAddrFamily Family ,
476 										  oslProtocol	Protocol ,
477 										  oslSocketType	Type )
478 		: Socket( Type, Family, Protocol )
479 	{}
480 
481 	//______________________________________________________________________________
listen(sal_Int32 MaxPendingConnections)482 	inline sal_Bool AcceptorSocket::listen(sal_Int32 MaxPendingConnections)
483 	{
484 		return osl_listenOnSocket( m_handle, MaxPendingConnections );
485 	}
486 
487 	//______________________________________________________________________________
acceptConnection(StreamSocket & Connection)488 	inline oslSocketResult AcceptorSocket::acceptConnection( StreamSocket& Connection)
489 	{
490 		oslSocket o = osl_acceptConnectionOnSocket( m_handle, 0 );
491 		oslSocketResult status = osl_Socket_Ok;
492 		if( o )
493 		{
494 			Connection = StreamSocket( o , SAL_NO_ACQUIRE );
495 		}
496 		else
497 		{
498 			Connection = StreamSocket();
499 			status = osl_Socket_Error;
500 		}
501 		return status;
502 	}
503 
504 	//______________________________________________________________________________
acceptConnection(StreamSocket & Connection,SocketAddr & PeerAddr)505 	inline oslSocketResult AcceptorSocket::acceptConnection(
506 		StreamSocket&	Connection, SocketAddr & PeerAddr)
507 	{
508 		// TODO change in/OUT parameter
509 		oslSocket o = osl_acceptConnectionOnSocket( m_handle, (oslSocketAddr *)&PeerAddr );
510 		oslSocketResult status = osl_Socket_Ok;
511 		if( o )
512 		{
513 			Connection = StreamSocket( o , SAL_NO_ACQUIRE );
514 		}
515 		else
516 		{
517 			Connection = StreamSocket();
518 			status = osl_Socket_Error;
519 		}
520 		return status;
521 	}
522 
523 	//______________________________________________________________________________
DatagramSocket(oslAddrFamily Family,oslProtocol Protocol,oslSocketType Type)524 	inline DatagramSocket::DatagramSocket(oslAddrFamily Family,
525 										  oslProtocol	Protocol,
526 										  oslSocketType	Type)
527 		: Socket( Type, Family, Protocol )
528 	{}
529 
530 	//______________________________________________________________________________
recvFrom(void * pBuffer,sal_uInt32 BufferSize,SocketAddr * pSenderAddr,oslSocketMsgFlag Flag)531 	inline sal_Int32 DatagramSocket::recvFrom(void*  pBuffer,
532 											  sal_uInt32 BufferSize,
533 											  SocketAddr* pSenderAddr,
534 											  oslSocketMsgFlag Flag )
535 	{
536 		sal_Int32 nByteRead;
537 		if( pSenderAddr )
538 		{
539 			// TODO : correct the out-parameter pSenderAddr outparameter
540   			nByteRead = osl_receiveFromSocket( m_handle, pSenderAddr->getHandle() , pBuffer,
541   											   BufferSize, Flag);
542 //  			nByteRead = osl_receiveFromSocket( m_handle, *(oslSocketAddr**) &pSenderAddr , pBuffer,
543 //  											   BufferSize, Flag);
544 		}
545 		else
546 		{
547 			nByteRead = osl_receiveFromSocket( m_handle, 0 , pBuffer , BufferSize ,  Flag );
548 		}
549 		return nByteRead;
550 	}
551 
552 	//______________________________________________________________________________
sendTo(const SocketAddr & ReceiverAddr,const void * pBuffer,sal_uInt32 BufferSize,oslSocketMsgFlag Flag)553 	inline sal_Int32  DatagramSocket::sendTo( const SocketAddr& ReceiverAddr,
554 											  const void* pBuffer,
555 											  sal_uInt32 BufferSize,
556 											  oslSocketMsgFlag Flag )
557 	{
558 		return osl_sendToSocket( m_handle, ReceiverAddr.getHandle(), pBuffer, BufferSize, Flag );
559 	}
560 }
561 #endif
562