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