1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 /*
21  * This file is part of LibreOffice published API.
22  */
23 
24 #ifndef INCLUDED_OSL_SOCKET_HXX
25 #define INCLUDED_OSL_SOCKET_HXX
26 
27 #include "osl/socket_decl.hxx"
28 
29 namespace osl
30 {
31 
SocketAddr()32     inline SocketAddr::SocketAddr()
33         : m_handle( osl_createEmptySocketAddr( osl_Socket_FamilyInet ) )
34     {}
35 
36 
SocketAddr(const SocketAddr & Addr)37     inline SocketAddr::SocketAddr(const SocketAddr& Addr)
38         : m_handle( osl_copySocketAddr( Addr.m_handle ) )
39     {
40     }
41 
42 #if defined LIBO_INTERNAL_ONLY
SocketAddr(SocketAddr && other)43     SocketAddr::SocketAddr(SocketAddr && other) noexcept : m_handle(other.m_handle) {
44         other.m_handle = nullptr;
45     }
46 #endif
47 
SocketAddr(oslSocketAddr Addr)48     inline SocketAddr::SocketAddr(oslSocketAddr Addr)
49         : m_handle( osl_copySocketAddr( Addr ) )
50     {
51     }
52 
53 
SocketAddr(oslSocketAddr Addr,__osl_socket_NoCopy)54     inline SocketAddr::SocketAddr(oslSocketAddr Addr, __osl_socket_NoCopy )
55         : m_handle( Addr )
56     {
57     }
58 
59 
SocketAddr(const::rtl::OUString & strAddrOrHostName,sal_Int32 nPort)60     inline SocketAddr::SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort)
61         : m_handle( osl_createInetSocketAddr( strAddrOrHostName.pData, nPort ) )
62     {
63         if(! m_handle )
64         {
65             m_handle = osl_resolveHostname(strAddrOrHostName.pData);
66 
67             // host found?
68             if(m_handle)
69             {
70                 osl_setInetPortOfSocketAddr(m_handle, nPort);
71             }
72             else
73             {
74                 osl_destroySocketAddr( m_handle );
75                 m_handle = NULL;
76             }
77         }
78     }
79 
80 
~SocketAddr()81     inline SocketAddr::~SocketAddr()
82     {
83         if( m_handle )
84             osl_destroySocketAddr( m_handle );
85     }
86 
87 
getHostname(oslSocketResult * pResult) const88     inline ::rtl::OUString SocketAddr::getHostname( oslSocketResult *pResult ) const
89     {
90         ::rtl::OUString hostname;
91         oslSocketResult result = osl_getHostnameOfSocketAddr( m_handle, &(hostname.pData) );
92         if( pResult )
93             *pResult = result;
94         return hostname;
95     }
96 
97 
getPort() const98     inline sal_Int32 SAL_CALL SocketAddr::getPort() const
99     {
100         return osl_getInetPortOfSocketAddr(m_handle);
101     }
102 
103 
setPort(sal_Int32 nPort)104     inline bool SAL_CALL SocketAddr::setPort( sal_Int32 nPort )
105     {
106         return osl_setInetPortOfSocketAddr(m_handle, nPort );
107     }
108 
setHostname(const::rtl::OUString & sDottedIpOrHostname)109     inline bool SAL_CALL SocketAddr::setHostname( const ::rtl::OUString &sDottedIpOrHostname )
110     {
111         *this = SocketAddr( sDottedIpOrHostname , getPort() );
112         return is();
113     }
114 
115 
setAddr(const::rtl::ByteSequence & address)116     inline bool SAL_CALL SocketAddr::setAddr( const ::rtl::ByteSequence & address )
117     {
118         return osl_setAddrOfSocketAddr( m_handle, address.getHandle() )
119             == osl_Socket_Ok;
120     }
121 
getAddr(oslSocketResult * pResult) const122     inline ::rtl::ByteSequence SAL_CALL SocketAddr::getAddr( oslSocketResult *pResult ) const
123     {
124         ::rtl::ByteSequence sequence;
125         oslSocketResult result = osl_getAddrOfSocketAddr( m_handle, reinterpret_cast<sal_Sequence **>(&sequence) );
126         if( pResult )
127             *pResult = result;
128         return sequence;
129     }
130 
131 
operator =(oslSocketAddr Addr)132     inline SocketAddr & SAL_CALL SocketAddr::operator= (oslSocketAddr Addr)
133     {
134         oslSocketAddr pNewAddr = osl_copySocketAddr( Addr );
135         if( m_handle )
136             osl_destroySocketAddr( m_handle );
137         m_handle = pNewAddr;
138         return *this;
139     }
140 
141 
operator =(const SocketAddr & Addr)142     inline SocketAddr & SAL_CALL SocketAddr::operator= (const SocketAddr& Addr)
143     {
144         *this = Addr.getHandle();
145         return *this;
146     }
147 
148 #if defined LIBO_INTERNAL_ONLY
operator =(SocketAddr && other)149     SocketAddr & SocketAddr::operator =(SocketAddr && other) noexcept {
150         if (m_handle != nullptr) {
151             osl_destroySocketAddr(m_handle);
152         }
153         m_handle = other.m_handle;
154         other.m_handle = nullptr;
155         return *this;
156     }
157 #endif
158 
assign(oslSocketAddr Addr,__osl_socket_NoCopy)159     inline SocketAddr & SAL_CALL SocketAddr::assign( oslSocketAddr Addr, __osl_socket_NoCopy )
160     {
161         if( m_handle )
162             osl_destroySocketAddr( m_handle );
163         m_handle = Addr;
164         return *this;
165     }
166 
167 
operator ==(oslSocketAddr Addr) const168     inline bool SAL_CALL SocketAddr::operator== (oslSocketAddr Addr) const
169     {
170         return osl_isEqualSocketAddr( m_handle, Addr );
171     }
172 
getHandle() const173     inline oslSocketAddr SocketAddr::getHandle() const
174     {
175         return m_handle;
176     }
177 
178 
is() const179     inline bool SocketAddr::is() const
180     {
181         return m_handle != NULL;
182     }
183 
getLocalHostname(oslSocketResult * pResult)184     inline ::rtl::OUString SAL_CALL SocketAddr::getLocalHostname( oslSocketResult *pResult )
185     {
186         ::rtl::OUString hostname;
187         oslSocketResult result = osl_getLocalHostname( &(hostname.pData) );
188         if(pResult )
189             *pResult = result;
190         return hostname;
191     }
192 
resolveHostname(const::rtl::OUString & strHostName,SocketAddr & Addr)193     inline void SAL_CALL SocketAddr::resolveHostname(
194         const ::rtl::OUString & strHostName, SocketAddr &Addr)
195     {
196         Addr = SocketAddr( osl_resolveHostname( strHostName.pData ) , SAL_NO_COPY );
197     }
198 
getServicePort(const::rtl::OUString & strServiceName,const::rtl::OUString & strProtocolName)199     inline sal_Int32 SAL_CALL SocketAddr::getServicePort(
200             const ::rtl::OUString& strServiceName,
201             const ::rtl::OUString & strProtocolName )
202     {
203         return osl_getServicePort( strServiceName.pData, strProtocolName.pData );
204     }
205 
206 
Socket(oslSocketType Type,oslAddrFamily Family,oslProtocol Protocol)207     inline Socket::Socket(oslSocketType Type,
208                           oslAddrFamily Family,
209                           oslProtocol   Protocol)
210         : m_handle( osl_createSocket(Family, Type, Protocol) )
211     {}
212 
213 
Socket(oslSocket socketHandle,__sal_NoAcquire)214     inline Socket::Socket( oslSocket socketHandle, __sal_NoAcquire )
215         : m_handle( socketHandle )
216     {}
217 
218 
Socket(oslSocket socketHandle)219     inline Socket::Socket( oslSocket socketHandle )
220         : m_handle( socketHandle )
221     {
222         osl_acquireSocket( m_handle );
223     }
224 
225 
Socket(const Socket & socket)226     inline Socket::Socket( const Socket & socket )
227         : m_handle( socket.getHandle() )
228     {
229         osl_acquireSocket( m_handle );
230     }
231 
232 
~Socket()233     inline Socket::~Socket()
234     {
235         osl_releaseSocket( m_handle );
236     }
237 
238 
operator =(oslSocket socketHandle)239     inline Socket& Socket::operator= ( oslSocket socketHandle)
240     {
241         osl_acquireSocket( socketHandle );
242         osl_releaseSocket( m_handle );
243         m_handle = socketHandle;
244         return *this;
245     }
246 
247 
operator =(const Socket & sock)248     inline Socket&  Socket::operator= (const Socket& sock)
249     {
250         *this = sock.getHandle();
251         return *this;
252     }
253 
254 
operator ==(const Socket & rSocket) const255     inline bool Socket::operator==( const Socket& rSocket ) const
256     {
257         return m_handle == rSocket.getHandle();
258     }
259 
260 
operator ==(const oslSocket socketHandle) const261     inline bool Socket::operator==( const oslSocket socketHandle ) const
262     {
263         return m_handle == socketHandle;
264     }
265 
266 
shutdown(oslSocketDirection Direction)267     inline void Socket::shutdown( oslSocketDirection Direction )
268     {
269         osl_shutdownSocket( m_handle , Direction );
270     }
271 
272 
close()273     inline void Socket::close()
274     {
275         osl_closeSocket( m_handle );
276     }
277 
278 
getLocalAddr(SocketAddr & addr) const279     inline void Socket::getLocalAddr( SocketAddr & addr) const
280     {
281         addr.assign( osl_getLocalAddrOfSocket( m_handle ) , SAL_NO_COPY );
282     }
283 
284 
getLocalPort() const285     inline sal_Int32 Socket::getLocalPort() const
286     {
287         SocketAddr addr( NULL );
288         getLocalAddr( addr );
289         return addr.getPort();
290     }
291 
292 
getLocalHost() const293     inline ::rtl::OUString Socket::getLocalHost() const
294     {
295         SocketAddr addr( NULL );
296         getLocalAddr( addr );
297         return addr.getHostname();
298     }
299 
300 
getPeerAddr(SocketAddr & addr) const301     inline void Socket::getPeerAddr( SocketAddr &addr ) const
302     {
303         addr.assign( osl_getPeerAddrOfSocket( m_handle ), SAL_NO_COPY );
304     }
305 
306 
getPeerPort() const307     inline sal_Int32 Socket::getPeerPort() const
308     {
309         SocketAddr addr( NULL );
310         getPeerAddr( addr );
311         return addr.getPort();
312     }
313 
314 
getPeerHost() const315     inline ::rtl::OUString Socket::getPeerHost() const
316     {
317         SocketAddr addr( NULL );
318         getPeerAddr( addr );
319         return addr.getHostname();
320     }
321 
322 
bind(const SocketAddr & LocalInterface)323     inline bool Socket::bind(const SocketAddr& LocalInterface)
324     {
325         return osl_bindAddrToSocket( m_handle , LocalInterface.getHandle() );
326     }
327 
328 
isRecvReady(const TimeValue * pTimeout) const329     inline bool Socket::isRecvReady(const TimeValue *pTimeout ) const
330     {
331         return osl_isReceiveReady( m_handle , pTimeout );
332     }
333 
334 
isSendReady(const TimeValue * pTimeout) const335     inline bool Socket::isSendReady(const TimeValue *pTimeout ) const
336     {
337         return osl_isSendReady( m_handle, pTimeout );
338     }
339 
340 
isExceptionPending(const TimeValue * pTimeout) const341     inline bool Socket::isExceptionPending(const TimeValue *pTimeout ) const
342     {
343         return osl_isExceptionPending( m_handle, pTimeout );
344     }
345 
346 
getType() const347     inline oslSocketType Socket::getType() const
348     {
349         return osl_getSocketType( m_handle );
350     }
351 
352 
getOption(oslSocketOption Option,void * pBuffer,sal_uInt32 BufferLen,oslSocketOptionLevel Level) const353     inline sal_Int32  Socket::getOption(
354         oslSocketOption Option,
355         void* pBuffer,
356         sal_uInt32 BufferLen,
357         oslSocketOptionLevel Level) const
358     {
359         return osl_getSocketOption( m_handle, Level, Option, pBuffer , BufferLen );
360     }
361 
362 
setOption(oslSocketOption Option,void * pBuffer,sal_uInt32 BufferLen,oslSocketOptionLevel Level) const363     inline bool Socket::setOption(  oslSocketOption Option,
364                                         void* pBuffer,
365                                         sal_uInt32 BufferLen,
366                                         oslSocketOptionLevel Level ) const
367     {
368         return osl_setSocketOption( m_handle, Level, Option , pBuffer, BufferLen );
369     }
370 
371 
setOption(oslSocketOption option,sal_Int32 nValue)372     inline bool Socket::setOption( oslSocketOption option, sal_Int32 nValue  )
373     {
374         return setOption( option, &nValue, sizeof( nValue ) );
375     }
376 
377 
getOption(oslSocketOption option) const378     inline sal_Int32 Socket::getOption( oslSocketOption option ) const
379     {
380         sal_Int32 n;
381         getOption( option, &n, sizeof( n ) );
382         return n;
383     }
384 
385 
enableNonBlockingMode(bool bNonBlockingMode)386     inline bool Socket::enableNonBlockingMode( bool bNonBlockingMode)
387     {
388         return osl_enableNonBlockingMode( m_handle , bNonBlockingMode );
389     }
390 
391 
isNonBlockingMode() const392     inline bool Socket::isNonBlockingMode() const
393     {
394         return osl_isNonBlockingMode( m_handle );
395     }
396 
397 
clearError() const398     inline void SAL_CALL Socket::clearError() const
399     {
400         sal_Int32 err = 0;
401         getOption(osl_Socket_OptionError, &err, sizeof(err));
402     }
403 
404 
getError() const405     inline oslSocketError Socket::getError() const
406     {
407         return osl_getLastSocketError( m_handle );
408     }
409 
410 
getErrorAsString() const411     inline ::rtl::OUString Socket::getErrorAsString( ) const
412     {
413         ::rtl::OUString error;
414         osl_getLastSocketErrorDescription( m_handle, &(error.pData) );
415         return error;
416     }
417 
418 
getHandle() const419     inline oslSocket Socket::getHandle() const
420     {
421         return m_handle;
422     }
423 
424 
StreamSocket(oslAddrFamily Family,oslProtocol Protocol,oslSocketType Type)425     inline StreamSocket::StreamSocket(oslAddrFamily Family,
426                                       oslProtocol Protocol,
427                                       oslSocketType Type )
428         : Socket( Type, Family, Protocol )
429     {}
430 
431 
StreamSocket(oslSocket socketHandle,__sal_NoAcquire noacquire)432     inline StreamSocket::StreamSocket( oslSocket socketHandle, __sal_NoAcquire noacquire )
433         : Socket( socketHandle, noacquire )
434     {}
435 
436 
StreamSocket(oslSocket socketHandle)437     inline StreamSocket::StreamSocket( oslSocket socketHandle )
438         : Socket( socketHandle )
439     {}
440 
441 
read(void * pBuffer,sal_uInt32 n)442     inline sal_Int32 StreamSocket::read(void* pBuffer, sal_uInt32 n)
443     {
444         return osl_readSocket( m_handle, pBuffer, n );
445     }
446 
447 
write(const void * pBuffer,sal_uInt32 n)448     inline sal_Int32 StreamSocket::write(const void* pBuffer, sal_uInt32 n)
449     {
450         return osl_writeSocket( m_handle, pBuffer, n );
451     }
452 
453 
recv(void * pBuffer,sal_uInt32 BytesToRead,oslSocketMsgFlag Flag)454     inline sal_Int32 StreamSocket::recv(void* pBuffer,
455                                         sal_uInt32 BytesToRead,
456                                         oslSocketMsgFlag Flag)
457     {
458         return osl_receiveSocket( m_handle, pBuffer,BytesToRead, Flag );
459     }
460 
461 
send(const void * pBuffer,sal_uInt32 BytesToSend,oslSocketMsgFlag Flag)462     inline sal_Int32 StreamSocket::send(const void* pBuffer,
463                                         sal_uInt32 BytesToSend,
464                                         oslSocketMsgFlag Flag)
465     {
466         return osl_sendSocket( m_handle, pBuffer, BytesToSend, Flag );
467     }
468 
469 
ConnectorSocket(oslAddrFamily Family,oslProtocol Protocol,oslSocketType Type)470       inline ConnectorSocket::ConnectorSocket(oslAddrFamily Family,
471                                             oslProtocol Protocol,
472                                             oslSocketType   Type)
473         : StreamSocket( Family, Protocol ,Type )
474     {}
475 
476 
connect(const SocketAddr & TargetHost,const TimeValue * pTimeout)477     inline oslSocketResult ConnectorSocket::connect( const SocketAddr& TargetHost,
478                                                      const TimeValue* pTimeout )
479     {
480         return osl_connectSocketTo( m_handle , TargetHost.getHandle(), pTimeout );
481     }
482 
483 
AcceptorSocket(oslAddrFamily Family,oslProtocol Protocol,oslSocketType Type)484     inline AcceptorSocket::AcceptorSocket(oslAddrFamily Family ,
485                                           oslProtocol   Protocol ,
486                                           oslSocketType Type )
487         : Socket( Type, Family, Protocol )
488     {}
489 
490 
listen(sal_Int32 MaxPendingConnections)491     inline bool AcceptorSocket::listen(sal_Int32 MaxPendingConnections)
492     {
493         return osl_listenOnSocket( m_handle, MaxPendingConnections );
494     }
495 
496 
acceptConnection(StreamSocket & Connection)497     inline oslSocketResult AcceptorSocket::acceptConnection( StreamSocket& Connection)
498     {
499         oslSocket o = osl_acceptConnectionOnSocket( m_handle, NULL );
500         oslSocketResult status = osl_Socket_Ok;
501         if( o )
502         {
503             Connection = StreamSocket( o , SAL_NO_ACQUIRE );
504         }
505         else
506         {
507             Connection = StreamSocket();
508             status = osl_Socket_Error;
509         }
510         return status;
511     }
512 
513 
acceptConnection(StreamSocket & Connection,SocketAddr & PeerAddr)514     inline oslSocketResult AcceptorSocket::acceptConnection(
515         StreamSocket&   Connection, SocketAddr & PeerAddr)
516     {
517         // TODO change in/OUT parameter
518         oslSocket o = osl_acceptConnectionOnSocket(
519             m_handle, reinterpret_cast<oslSocketAddr *>(&PeerAddr));
520         oslSocketResult status = osl_Socket_Ok;
521         if( o )
522         {
523             Connection = StreamSocket( o , SAL_NO_ACQUIRE );
524         }
525         else
526         {
527             Connection = StreamSocket();
528             status = osl_Socket_Error;
529         }
530         return status;
531     }
532 
533 
DatagramSocket(oslAddrFamily Family,oslProtocol Protocol,oslSocketType Type)534     inline DatagramSocket::DatagramSocket(oslAddrFamily Family,
535                                           oslProtocol   Protocol,
536                                           oslSocketType Type)
537         : Socket( Type, Family, Protocol )
538     {}
539 
540 
recvFrom(void * pBuffer,sal_uInt32 BufferSize,SocketAddr * pSenderAddr,oslSocketMsgFlag Flag)541     inline sal_Int32 DatagramSocket::recvFrom(void*  pBuffer,
542                                               sal_uInt32 BufferSize,
543                                               SocketAddr* pSenderAddr,
544                                               oslSocketMsgFlag Flag )
545     {
546         sal_Int32 nByteRead;
547         if( pSenderAddr )
548         {
549             // TODO : correct the out-parameter pSenderAddr outparameter
550             nByteRead = osl_receiveFromSocket( m_handle, pSenderAddr->getHandle() , pBuffer,
551                                                  BufferSize, Flag);
552         }
553         else
554         {
555             nByteRead = osl_receiveFromSocket( m_handle, NULL , pBuffer , BufferSize ,  Flag );
556         }
557         return nByteRead;
558     }
559 
560 
sendTo(const SocketAddr & ReceiverAddr,const void * pBuffer,sal_uInt32 BufferSize,oslSocketMsgFlag Flag)561     inline sal_Int32  DatagramSocket::sendTo( const SocketAddr& ReceiverAddr,
562                                               const void* pBuffer,
563                                               sal_uInt32 BufferSize,
564                                               oslSocketMsgFlag Flag )
565     {
566         return osl_sendToSocket( m_handle, ReceiverAddr.getHandle(), pBuffer, BufferSize, Flag );
567     }
568 }
569 #endif
570 
571 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
572