update dev300-m58
[ooovba.git] / vos / source / socket.cxx
blob02ab022a0ead84498fffc0a7b66cf79acfe57c1b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: socket.cxx,v $
10 * $Revision: 1.18 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <stdio.h>
33 #include <rtl/memory.h>
34 #include <osl/time.h>
35 #include <vos/socket.hxx>
36 #include <vos/diagnose.hxx>
37 //#include <osl/tools.h>
39 using namespace vos;
42 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OSocketAddr, vos),
43 VOS_NAMESPACE(OSocketAddr, vos),
44 VOS_NAMESPACE(OObject, vos), 0);
47 /*****************************************************************************/
48 // OSocketAddr()
49 /*****************************************************************************/
50 OSocketAddr::OSocketAddr()
52 m_SockAddr= 0;
55 /*****************************************************************************/
56 // OSocketAddr()
57 /*****************************************************************************/
58 OSocketAddr::OSocketAddr(oslSocketAddr Addr)
60 m_SockAddr= Addr;
63 /*****************************************************************************/
64 // OSocketAddr()
65 /*****************************************************************************/
66 OSocketAddr::OSocketAddr(const OSocketAddr& Addr) :
67 ISocketAddr(), OObject()
69 m_SockAddr= osl_copySocketAddr((oslSocketAddr)Addr);
72 /*****************************************************************************/
73 // ~OSocketAddr()
74 /*****************************************************************************/
75 OSocketAddr::~OSocketAddr()
77 osl_destroySocketAddr(m_SockAddr);
81 /*****************************************************************************/
82 // getFamily()
83 /*****************************************************************************/
84 OSocketAddr::TAddrFamily OSocketAddr::getFamily() const
86 return (TAddrFamily)osl_getFamilyOfSocketAddr(m_SockAddr);
89 /*****************************************************************************/
90 // operator oslSocketAddr ()
91 /*****************************************************************************/
92 OSocketAddr::operator oslSocketAddr() const
94 return m_SockAddr;
97 /*****************************************************************************/
98 // getHostname()
99 /*****************************************************************************/
100 OSocketAddr::TResult OSocketAddr::getHostname(rtl::OUString& rBuffer ) const
102 return (TResult)osl_getHostnameOfSocketAddr(m_SockAddr, &rBuffer.pData );
105 /*****************************************************************************/
106 // getLocalHostname()
107 /*****************************************************************************/
108 OSocketAddr::TResult OSocketAddr::getLocalHostname( rtl::OUString& pBuffer )
110 return (TResult)osl_getLocalHostname( &pBuffer.pData );
113 /*****************************************************************************/
114 // resolveHostname()
115 /*****************************************************************************/
116 oslSocketAddr OSocketAddr::resolveHostname(const rtl::OUString& ustrHostname)
118 return osl_resolveHostname( ustrHostname.pData );
121 /*****************************************************************************/
122 // operator= (oslSocketAddr Addr)
123 /*****************************************************************************/
124 void OSocketAddr::operator= (oslSocketAddr Addr)
126 if(m_SockAddr) {
127 osl_destroySocketAddr(m_SockAddr);
130 m_SockAddr= Addr;
133 /*****************************************************************************/
134 // operator== (oslSocketAddr Addr)
135 /*****************************************************************************/
136 sal_Bool OSocketAddr::operator== (oslSocketAddr Addr)
138 return (osl_isEqualSocketAddr(m_SockAddr, Addr));
141 /*****************************************************************************/
142 // operator=(const OSocketAddr& Addr)
143 /*****************************************************************************/
144 OSocketAddr& OSocketAddr::operator=(const OSocketAddr& Addr)
146 if(m_SockAddr) {
147 osl_destroySocketAddr(m_SockAddr);
150 m_SockAddr= osl_copySocketAddr((oslSocketAddr)Addr);
152 return *this;
156 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OInetSocketAddr, vos),
157 VOS_NAMESPACE(OInetSocketAddr, vos),
158 VOS_NAMESPACE(OSocketAddr, vos), 0);
162 /*****************************************************************************/
163 // OInetSocketAddr
164 // creates arbitrary inet-address (INADDR_ANY)
165 /*****************************************************************************/
166 OInetSocketAddr::OInetSocketAddr()
168 m_SockAddr= osl_createEmptySocketAddr(osl_Socket_FamilyInet);
171 /*****************************************************************************/
172 // OInetSocketAddr
173 // uses the given oslSocketAddr as its own
174 /*****************************************************************************/
175 OInetSocketAddr::OInetSocketAddr(oslSocketAddr Addr) :
176 OSocketAddr(Addr)
178 VOS_ASSERT(osl_getFamilyOfSocketAddr(Addr) == osl_Socket_FamilyInet);
181 /*****************************************************************************/
182 // OInetSocketAddr
183 // Create a socket address either from a dotted decimal address
184 //(e.g. 141.99.128.50) or a hostname (e.g. www.stardiv.de).
185 /*****************************************************************************/
186 OInetSocketAddr::OInetSocketAddr(const rtl::OUString& ustrAddrOrHostname, sal_Int32 Port)
188 // first try as dotted address.
189 m_SockAddr= osl_createInetSocketAddr(ustrAddrOrHostname.pData, Port);
191 // create failed, maybe it's an hostname
192 if(m_SockAddr == 0)
194 m_SockAddr= osl_resolveHostname(ustrAddrOrHostname.pData);
196 // host found?
197 if(m_SockAddr)
199 // set port will fail if addrtype is not osl_Socket_FamilyInet
200 VOS_VERIFY(osl_setInetPortOfSocketAddr(m_SockAddr, Port));
205 /*****************************************************************************/
206 // OInetSocketAddr(const OInetSocketAddr&)
207 /*****************************************************************************/
208 OInetSocketAddr::OInetSocketAddr(const OInetSocketAddr& sa) :
209 OSocketAddr(sa)
211 VOS_ASSERT(getFamily() == TFamily_Inet);
214 /*****************************************************************************/
215 // OInetSocketAddr(const OSocketAddr&)
216 /*****************************************************************************/
217 OInetSocketAddr::OInetSocketAddr(const OSocketAddr& sa) :
218 OSocketAddr(sa)
220 VOS_ASSERT(sa.getFamily() == TFamily_Inet);
223 /*****************************************************************************/
224 // ~OInetSocketAddr
225 /*****************************************************************************/
226 OInetSocketAddr::~OInetSocketAddr()
230 /*****************************************************************************/
231 // operator= (oslSocketAddr Addr)
232 /*****************************************************************************/
233 void OInetSocketAddr::operator= (oslSocketAddr Addr)
235 VOS_PRECOND(osl_getFamilyOfSocketAddr(Addr) == osl_Socket_FamilyInet,
236 "oslSocketAddr not of type osl_Socket_FamilyInet!");
238 OSocketAddr::operator=(Addr);
241 /*****************************************************************************/
242 // operator== (oslSocketAddr Addr)
243 /*****************************************************************************/
244 sal_Bool OInetSocketAddr::operator== (oslSocketAddr Addr)
246 return (osl_isEqualSocketAddr(m_SockAddr, Addr));
249 /*****************************************************************************/
250 // operator=(const OInetSocketAddr& Addr)
251 /*****************************************************************************/
252 OInetSocketAddr& OInetSocketAddr::operator=(const OInetSocketAddr& Addr)
254 VOS_ASSERT(Addr.getFamily() == TFamily_Inet);
256 OSocketAddr::operator=(Addr);
258 return *this;
261 /*****************************************************************************/
262 // operator=(const OSocketAddr& Addr)
263 /*****************************************************************************/
264 OInetSocketAddr& OInetSocketAddr::operator=(const OSocketAddr& Addr)
266 VOS_ASSERT(Addr.getFamily() == TFamily_Inet);
268 OSocketAddr::operator=(Addr);
270 return *this;
273 /*****************************************************************************/
274 // getServicePort()
275 /*****************************************************************************/
276 sal_Int32 OInetSocketAddr::getServicePort(const rtl::OUString& ustrServiceName,
277 const rtl::OUString& ustrProtocolName)
279 return osl_getServicePort(ustrServiceName.pData, ustrProtocolName.pData);
283 /*****************************************************************************/
284 // getPort()
285 /*****************************************************************************/
286 sal_Int32 OInetSocketAddr::getPort () const
288 return osl_getInetPortOfSocketAddr(m_SockAddr);
292 /*****************************************************************************/
293 // setPort()
294 /*****************************************************************************/
295 sal_Bool OInetSocketAddr::setPort (sal_Int32 Port)
297 return osl_setInetPortOfSocketAddr(m_SockAddr, Port);
301 /*****************************************************************************/
302 // getDottedAddr()
303 /*****************************************************************************/
304 OSocketAddr::TResult OInetSocketAddr::getDottedAddr( rtl::OUString& pBuffer ) const
306 return (TResult)osl_getDottedInetAddrOfSocketAddr(m_SockAddr, &pBuffer.pData );
309 /*****************************************************************************/
310 // setAddr()
311 /*****************************************************************************/
312 sal_Bool OInetSocketAddr::setAddr(const rtl::OUString& ustrAddrOrHostname)
314 sal_Int32 Port = 0;
316 if(m_SockAddr) {
318 // retrieve old port
319 Port= getPort();
321 // free old address
322 osl_destroySocketAddr(m_SockAddr);
323 m_SockAddr= 0;
326 // first try as dotted address.
327 m_SockAddr= osl_createInetSocketAddr(ustrAddrOrHostname.pData, Port);
329 // create failed, maybe it's an hostname
330 if(m_SockAddr == 0)
333 m_SockAddr= osl_resolveHostname( ustrAddrOrHostname.pData );
335 // host found?
336 if(m_SockAddr==0)
338 return sal_False;
341 // set port will fail if addrtype is not osl_Socket_FamilyInet
342 VOS_VERIFY(osl_setInetPortOfSocketAddr(m_SockAddr, Port));
346 return sal_True;
349 ///////////////////////////////////////////////////////////////////////////////
350 // OIpxSocketAddr
352 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OIpxSocketAddr, vos),
353 VOS_NAMESPACE(OIpxSocketAddr, vos),
354 VOS_NAMESPACE(OSocketAddr, vos), 0);
357 /*****************************************************************************/
358 // OIpxSocketAddr()
359 /*****************************************************************************/
360 OIpxSocketAddr::OIpxSocketAddr()
362 m_SockAddr= osl_createEmptySocketAddr(osl_Socket_FamilyIpx);
365 /*****************************************************************************/
366 // OIpxSocketAddr(oslSocketAddr)
367 /*****************************************************************************/
368 OIpxSocketAddr::OIpxSocketAddr(oslSocketAddr Addr) :
369 OSocketAddr(Addr)
371 VOS_ASSERT(osl_getFamilyOfSocketAddr(Addr) == osl_Socket_FamilyIpx);
374 /*****************************************************************************/
375 // OIpxSocketAddr()
376 /*****************************************************************************/
377 OIpxSocketAddr::OIpxSocketAddr(const rtl::OUString&,
378 const rtl::OUString&,
379 sal_uInt32 )
381 // jbu : functionality removed from vos
384 /*****************************************************************************/
385 // OIpxSocketAddr(OIpxSocketAddr&)
386 /*****************************************************************************/
387 OIpxSocketAddr::OIpxSocketAddr(const OIpxSocketAddr& sa) :
388 OSocketAddr(sa)
390 VOS_ASSERT(sa.getFamily() == TFamily_Ipx);
394 /*****************************************************************************/
395 // OIpxSocketAddr(OSocketAddr&)
396 /*****************************************************************************/
397 OIpxSocketAddr::OIpxSocketAddr(const OSocketAddr& sa) :
398 OSocketAddr(sa)
400 VOS_ASSERT(sa.getFamily() == TFamily_Ipx);
403 /*****************************************************************************/
404 // ~OIpxSocketAddr()
405 /*****************************************************************************/
406 OIpxSocketAddr::~OIpxSocketAddr()
411 /*****************************************************************************/
412 // operator=()
413 /*****************************************************************************/
414 void OIpxSocketAddr::operator= (oslSocketAddr Addr)
416 VOS_PRECOND(osl_getFamilyOfSocketAddr(Addr) == osl_Socket_FamilyIpx,
417 "oslSocketAddr not of type osl_Socket_FamilyIpx!");
419 OSocketAddr::operator=(Addr);
422 /*****************************************************************************/
423 // operator== (oslSocketAddr Addr)
424 /*****************************************************************************/
425 sal_Bool OIpxSocketAddr::operator== (oslSocketAddr Addr)
427 return (osl_isEqualSocketAddr(m_SockAddr, Addr));
430 /*****************************************************************************/
431 // operator=(const OIpxSocketAddr& Addr)
432 /*****************************************************************************/
433 OIpxSocketAddr& OIpxSocketAddr::operator=(const OIpxSocketAddr& Addr)
435 VOS_ASSERT(Addr.getFamily() == TFamily_Ipx);
437 OSocketAddr::operator=(Addr);
439 return *this;
442 /*****************************************************************************/
443 // operator=(const OSocketAddr& Addr)
444 /*****************************************************************************/
445 OIpxSocketAddr& OIpxSocketAddr::operator=(const OSocketAddr& Addr)
447 VOS_ASSERT(Addr.getFamily() == TFamily_Ipx);
449 OSocketAddr::operator=(Addr);
451 return *this;
454 /*****************************************************************************/
455 // getNetNumber()
456 /*****************************************************************************/
457 OSocketAddr::TResult OIpxSocketAddr::getNetNumber(TIpxNetNumber&) const
459 // jbu : functionality removed from vos
460 return (TResult)0;
463 /*****************************************************************************/
464 // getNodeNumber()
465 /*****************************************************************************/
466 OSocketAddr::TResult OIpxSocketAddr::getNodeNumber(TIpxNodeNumber& ) const
468 // jbu : functionality removed from vos
469 return (TResult)0;
472 /*****************************************************************************/
473 // getSocketNumber()
474 /*****************************************************************************/
475 sal_uInt32 OIpxSocketAddr::getSocketNumber() const
477 // return osl_getIpxSocketNumber(m_SockAddr);
478 return (TResult)0;
482 /*****************************************************************************/
483 // getAddressString()
484 /*****************************************************************************/
485 //void OIpxSocketAddr::getAddressString(sal_Char* Buffer, sal_uInt32 Len) const
486 void OIpxSocketAddr::getAddressString( rtl::OUString& ) const
488 // jbu : functionality removed from vos
492 ///////////////////////////////////////////////////////////////////////////////
493 // Socket
496 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OSocket, vos),
497 VOS_NAMESPACE(OSocket, vos),
498 VOS_NAMESPACE(OObject, vos), 0);
500 /*****************************************************************************/
501 // OSocket()
502 /*****************************************************************************/
503 OSocket::OSocket()
505 m_pRecvTimeout = 0;
506 m_pSendTimeout = 0;
508 m_pSockRef= 0;
512 /*****************************************************************************/
513 // OSocket()
514 /*****************************************************************************/
515 OSocket::OSocket(TSocketType Type,
516 TAddrFamily Family,
517 TProtocol Protocol)
519 m_pRecvTimeout = 0;
520 m_pSendTimeout = 0;
522 m_pSockRef=
523 new SockRef(osl_createSocket((oslAddrFamily)Family,
524 (oslSocketType)Type,
525 (oslProtocol)Protocol));
527 VOS_POSTCOND(m_pSockRef != 0, "OSocket(): new failed.\n");
528 VOS_POSTCOND((*m_pSockRef)(), "OSocket(): creation of socket failed!\n");
531 /*****************************************************************************/
532 // OSocket()
533 /*****************************************************************************/
534 OSocket::OSocket(const OSocket& sock) :
535 ISocketTypes(), OReference(), OObject()
537 m_pRecvTimeout = 0;
538 m_pSendTimeout = 0;
539 m_pSockRef=0;
541 VOS_ASSERT(sock.m_pSockRef != 0);
543 if ( sock.m_pSockRef !=0 )
545 m_pSockRef= sock.m_pSockRef;
547 setRecvTimeout(sock.m_pRecvTimeout);
548 setSendTimeout(sock.m_pSendTimeout);
550 m_pSockRef->acquire();
554 /*****************************************************************************/
555 // OSocket()
556 /*****************************************************************************/
557 OSocket::OSocket(oslSocket Socket)
559 m_pRecvTimeout = 0;
560 m_pSendTimeout = 0;
562 m_pSockRef = new SockRef(Socket);
566 /*****************************************************************************/
567 // ~OSocket()
568 /*****************************************************************************/
569 OSocket::~OSocket()
571 close();
573 delete m_pRecvTimeout;
574 delete m_pSendTimeout;
578 /*****************************************************************************/
579 // create
580 /*****************************************************************************/
581 sal_Bool OSocket::create(TSocketType Type,
582 TAddrFamily Family,
583 TProtocol Protocol)
585 // if this was a valid socket, decrease reference
586 if ((m_pSockRef) && (m_pSockRef->release() == 0))
588 osl_releaseSocket((*m_pSockRef)());
589 delete m_pSockRef;
590 m_pSockRef= 0;
593 m_pSockRef=
594 new SockRef(osl_createSocket((oslAddrFamily)Family,
595 (oslSocketType)Type,
596 (oslProtocol)Protocol));
598 VOS_POSTCOND(m_pSockRef != 0, "OSocket(): new failed.\n");
600 return (*m_pSockRef)() != 0;
603 /*****************************************************************************/
604 // operator=
605 /*****************************************************************************/
606 OSocket& OSocket::operator= (const OSocket& sock)
608 VOS_PRECOND(sock.m_pSockRef != 0, "OSocket::operator=: tried to assign an empty/invalid socket\n");
610 if (m_pSockRef == sock.m_pSockRef)
611 return *this;
613 // if this was a valid socket, decrease reference
614 if ((m_pSockRef) && (m_pSockRef->release() == 0))
616 osl_releaseSocket((*m_pSockRef)());
617 delete m_pSockRef;
618 m_pSockRef= 0;
621 m_pSockRef= sock.m_pSockRef;
623 setRecvTimeout(sock.m_pRecvTimeout);
624 setSendTimeout(sock.m_pSendTimeout);
626 m_pSockRef->acquire();
628 return *this;
631 /*****************************************************************************/
632 // operator oslSocket()
633 /*****************************************************************************/
634 OSocket::operator oslSocket() const
636 VOS_ASSERT(m_pSockRef);
637 return (*m_pSockRef)();
640 /*****************************************************************************/
641 // isValid()
642 /*****************************************************************************/
643 sal_Bool OSocket::isValid() const
645 return m_pSockRef != 0 && (*m_pSockRef)() != 0;
649 /*****************************************************************************/
650 // close
651 /*****************************************************************************/
652 void OSocket::close()
654 if (m_pSockRef && (*m_pSockRef)() && (m_pSockRef->release() == 0))
656 osl_releaseSocket((*m_pSockRef)());
657 delete m_pSockRef;
660 m_pSockRef= 0;
663 /*****************************************************************************/
664 // getLocalAddr
665 /*****************************************************************************/
666 void OSocket::getLocalAddr(OSocketAddr& sa) const
668 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
670 if ( m_pSockRef && (*m_pSockRef)() )
672 sa= osl_getLocalAddrOfSocket((*m_pSockRef)());
676 /*****************************************************************************/
677 // getLocalPort
678 /*****************************************************************************/
679 sal_Int32 OSocket::getLocalPort() const
681 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
683 sal_Int32 Port= OSL_INVALID_PORT;
685 if ( m_pSockRef && (*m_pSockRef)() )
687 oslSocketAddr Addr= osl_getLocalAddrOfSocket((*m_pSockRef)());
689 if(Addr)
691 Port= osl_getInetPortOfSocketAddr(Addr);
692 osl_destroySocketAddr(Addr);
696 return Port;
699 /*****************************************************************************/
700 // getLocalHost
701 /*****************************************************************************/
702 OSocket::TResult OSocket::getLocalHost( rtl::OUString& pBuffer) const
704 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
706 if ( m_pSockRef && (*m_pSockRef)() )
708 oslSocketAddr Addr= osl_getLocalAddrOfSocket((*m_pSockRef)());
710 if(Addr)
712 // TResult Result= (TResult)osl_getHostnameOfSocketAddr(Addr,
713 // pBuffer, BufferSize);
714 TResult Result= (TResult)osl_getHostnameOfSocketAddr(Addr,
715 &pBuffer.pData );
717 osl_destroySocketAddr(Addr);
719 return Result;
723 return TResult_Error;
726 /*****************************************************************************/
727 // getPeerAddr
728 /*****************************************************************************/
729 void OSocket::getPeerAddr(OSocketAddr& sa) const
731 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
733 if ( m_pSockRef && (*m_pSockRef)() )
735 sa= osl_getPeerAddrOfSocket((*m_pSockRef)());
739 /*****************************************************************************/
740 // getPeerPort
741 /*****************************************************************************/
742 sal_Int32 OSocket::getPeerPort() const
744 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
746 sal_Int32 Port= OSL_INVALID_PORT;
748 if ( m_pSockRef && (*m_pSockRef)() )
750 oslSocketAddr Addr= osl_getPeerAddrOfSocket((*m_pSockRef)());
752 if(Addr)
754 Port= osl_getInetPortOfSocketAddr(Addr);
755 osl_destroySocketAddr(Addr);
759 return Port;
762 /*****************************************************************************/
763 // getPeerHost
764 /*****************************************************************************/
765 OSocket::TResult OSocket::getPeerHost( rtl::OUString& pBuffer ) const
767 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
769 if ( m_pSockRef && (*m_pSockRef)() )
771 oslSocketAddr Addr= osl_getPeerAddrOfSocket((*m_pSockRef)());
773 if(Addr)
775 // TResult Result= (TResult)osl_getHostnameOfSocketAddr(Addr,
776 // pBuffer, BufferSize);
777 TResult Result= (TResult)osl_getHostnameOfSocketAddr(Addr,
778 &pBuffer.pData );
780 osl_destroySocketAddr(Addr);
782 return Result;
786 return TResult_Error;
789 /*****************************************************************************/
790 // bind
791 /*****************************************************************************/
792 sal_Bool OSocket::bind(const OSocketAddr& Addr)
794 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
796 if ( m_pSockRef && (*m_pSockRef)() )
798 return osl_bindAddrToSocket((*m_pSockRef)(), (oslSocketAddr)Addr);
801 return sal_False;
805 /*****************************************************************************/
806 // setSendTimeout
807 /*****************************************************************************/
808 void OSocket::setSendTimeout(const TimeValue* pTimeout)
810 delete m_pSendTimeout;
812 if (pTimeout)
813 m_pSendTimeout = new TimeValue(*pTimeout);
814 else
815 m_pSendTimeout = 0;
818 /*****************************************************************************/
819 // setRecvTimeout
820 /*****************************************************************************/
821 void OSocket::setRecvTimeout(const TimeValue* pTimeout)
823 delete m_pRecvTimeout;
825 if (pTimeout)
826 m_pRecvTimeout = new TimeValue(*pTimeout);
827 else
828 m_pRecvTimeout = 0;
831 /*****************************************************************************/
832 // isRecvReady
833 /*****************************************************************************/
834 sal_Bool OSocket::isRecvReady(const TimeValue* pTimeout) const
836 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
838 if ( m_pSockRef && (*m_pSockRef)() )
840 return osl_isReceiveReady((*m_pSockRef)(), pTimeout);
843 return sal_False;
846 /*****************************************************************************/
847 // isSendReady
848 /*****************************************************************************/
849 sal_Bool OSocket::isSendReady(const TimeValue* pTimeout) const
851 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
853 if ( m_pSockRef && (*m_pSockRef)() )
855 return osl_isSendReady((*m_pSockRef)(), pTimeout);
858 return sal_False;
861 /*****************************************************************************/
862 // isExceptionPending
863 /*****************************************************************************/
864 sal_Bool OSocket::isExceptionPending(const TimeValue* pTimeout) const
866 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
868 if ( m_pSockRef && (*m_pSockRef)() )
870 return osl_isExceptionPending((*m_pSockRef)(), pTimeout);
873 return sal_False;
877 /*****************************************************************************/
878 // getOption
879 /*****************************************************************************/
880 sal_Int32 OSocket::getOption(TSocketOption Option,
881 void* pBuffer,
882 sal_uInt32 BufferLen,
883 TSocketOptionLevel Level) const
885 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
887 if ( m_pSockRef && (*m_pSockRef)() )
889 return osl_getSocketOption((*m_pSockRef)(),
890 (oslSocketOptionLevel)Level,
891 (oslSocketOption)Option,
892 pBuffer,
893 BufferLen);
896 return sal_False;
899 /*****************************************************************************/
900 // setOption
901 /*****************************************************************************/
902 sal_Bool OSocket::setOption(TSocketOption Option,
903 void* pBuffer,
904 sal_uInt32 BufferLen,
905 TSocketOptionLevel Level) const
907 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
909 if ( m_pSockRef && (*m_pSockRef)() )
911 return osl_setSocketOption((*m_pSockRef)(),
912 (oslSocketOptionLevel)Level,
913 (oslSocketOption)Option,
914 pBuffer,
915 BufferLen);
918 return sal_False;
922 /*****************************************************************************/
923 // enableNonBlockingMode
924 /*****************************************************************************/
925 sal_Bool OSocket::enableNonBlockingMode(sal_Bool On)
927 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
929 if ( m_pSockRef && (*m_pSockRef)() )
931 return osl_enableNonBlockingMode((*m_pSockRef)(), On);
934 return sal_False;
937 /*****************************************************************************/
938 // isNonBlockingMode
939 /*****************************************************************************/
940 sal_Bool OSocket::isNonBlockingMode() const
942 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
944 if ( m_pSockRef && (*m_pSockRef)() )
946 return osl_isNonBlockingMode((*m_pSockRef)());
949 return sal_False;
952 /*****************************************************************************/
953 // getType
954 /*****************************************************************************/
955 OSocket::TSocketType OSocket::getType() const
957 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
959 if ( m_pSockRef && (*m_pSockRef)() )
961 return (TSocketType)osl_getSocketType((*m_pSockRef)());
964 return TType_Invalid;
967 /*****************************************************************************/
968 // clearError
969 /*****************************************************************************/
970 sal_Int32 OSocket::clearError() const
972 sal_Int32 err = 0;
974 getOption(TOption_Error, &err, sizeof(err));
976 return err;
979 /*****************************************************************************/
980 // setDebug
981 /*****************************************************************************/
982 sal_Int32 OSocket::setDebug(sal_Int32 opt) const
984 sal_Int32 old= 0;
986 getOption(TOption_Debug, &old, sizeof(old));
988 if (opt != -1)
989 setOption(TOption_Debug, &opt, sizeof(opt));
991 return old;
994 /*****************************************************************************/
995 // setReuseAddr
996 /*****************************************************************************/
997 sal_Int32 OSocket::setReuseAddr(sal_Int32 opt) const
999 sal_Int32 old = 0;
1001 getOption(TOption_ReuseAddr, &old, sizeof(old));
1003 if (opt != -1)
1004 setOption(TOption_ReuseAddr, &opt, sizeof(opt));
1006 return (old);
1009 /*****************************************************************************/
1010 // setKeepAlive
1011 /*****************************************************************************/
1012 sal_Int32 OSocket::setKeepAlive(sal_Int32 opt) const
1014 sal_Int32 old = 0;
1016 getOption(TOption_KeepAlive, &old, sizeof(old));
1018 if (opt != -1)
1019 setOption(TOption_KeepAlive, &opt, sizeof(opt));
1021 return (old);
1024 /*****************************************************************************/
1025 // setDontRoute
1026 /*****************************************************************************/
1027 sal_Int32 OSocket::setDontRoute(sal_Int32 opt) const
1029 sal_Int32 old = 0;
1031 getOption(TOption_DontRoute, &old, sizeof(old));
1033 if (opt != -1)
1034 setOption(TOption_DontRoute, &opt, sizeof(opt));
1036 return (old);
1039 /*****************************************************************************/
1040 // setBroadcast
1041 /*****************************************************************************/
1042 sal_Int32 OSocket::setBroadcast(sal_Int32 opt) const
1044 sal_Int32 old = 0;
1046 getOption(TOption_Broadcast, &old, sizeof(old));
1048 if (opt != -1)
1049 setOption(TOption_Broadcast, &opt, sizeof(opt));
1051 return (old);
1054 /*****************************************************************************/
1055 // setOobinline
1056 /*****************************************************************************/
1057 sal_Int32 OSocket::setOobinline(sal_Int32 opt) const
1059 sal_Int32 old = 0;
1061 getOption(TOption_OOBinLine, &old, sizeof(old));
1063 if (opt != -1)
1064 setOption(TOption_OOBinLine, &opt, sizeof(opt));
1066 return (old);
1069 /*****************************************************************************/
1070 // setLinger
1071 /*****************************************************************************/
1072 sal_Int32 OSocket::setLinger(sal_Int32 time) const
1074 /* local decl. of linger-struct */
1075 struct SockLinger
1077 sal_Int32 m_onoff; // option on/off
1078 sal_Int32 m_linger; // linger time
1082 SockLinger old = { 0, 0 };
1084 getOption(TOption_Linger, &old, sizeof(old));
1086 if (time > 0) // enable linger with wait-times > 0
1088 SockLinger nw = { 1, time };
1089 setOption(TOption_Linger, &nw, sizeof(nw));
1091 else if (time == 0) // disable linger with wait-time == 0
1093 SockLinger nw = { 0, old.m_linger };
1094 setOption(TOption_Linger, &nw, sizeof(nw));
1097 // returns 0 if linger was off, else the linger-time
1098 return (old.m_onoff ? old.m_linger : 0);
1101 /*****************************************************************************/
1102 // setSendBufSize
1103 /*****************************************************************************/
1104 sal_Int32 OSocket::setSendBufSize(sal_Int32 sz) const
1106 sal_Int32 old = 0;
1108 getOption(TOption_SndBuf, &old, sizeof(old));
1110 if (sz >= 0)
1111 setOption(TOption_SndBuf, &sz, sizeof(sz));
1113 return (old);
1116 /*****************************************************************************/
1117 // setRecvBufSize
1118 /*****************************************************************************/
1119 sal_Int32 OSocket::setRecvBufSize(sal_Int32 sz) const
1121 sal_Int32 old = 0;
1123 getOption(TOption_RcvBuf, &old, sizeof(old));
1125 if (sz >= 0)
1126 setOption(TOption_RcvBuf, &sz, sizeof(sz));
1128 return (old);
1131 /*****************************************************************************/
1132 // setTcpNoDelay
1133 /*****************************************************************************/
1134 sal_Int32 OSocket::setTcpNoDelay(sal_Int32 sz) const
1136 sal_Int32 old = 0;
1138 getOption(TOption_TcpNoDelay, &old, sizeof(old), TLevel_Tcp);
1140 if (sz >= 0)
1141 setOption(TOption_TcpNoDelay, &sz, sizeof(sz), TLevel_Tcp);
1143 return (old);
1146 /*****************************************************************************/
1147 // getError
1148 /*****************************************************************************/
1149 //void OSocket::getError(sal_Char* pBuffer, sal_uInt32 nSize) const
1150 void OSocket::getError( rtl::OUString& pBuffer ) const
1152 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1154 if (m_pSockRef && (*m_pSockRef)())
1155 osl_getLastSocketErrorDescription((*m_pSockRef)(), &pBuffer.pData );
1156 else
1157 osl_getLastSocketErrorDescription(NULL, &pBuffer.pData );
1160 /*****************************************************************************/
1161 // getError
1162 /*****************************************************************************/
1163 OSocket::TSocketError OSocket::getError() const
1165 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1167 if (m_pSockRef && (*m_pSockRef)())
1168 return (TSocketError)osl_getLastSocketError((*m_pSockRef)());
1169 else
1170 return (TSocketError)osl_getLastSocketError(NULL);
1175 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OAcceptorSocket, vos),
1176 VOS_NAMESPACE(OAcceptorSocket, vos),
1177 VOS_NAMESPACE(OSocket, vos), 0);
1180 /*****************************************************************************/
1181 // OAcceptorSocket
1182 /*****************************************************************************/
1183 OAcceptorSocket::OAcceptorSocket(TAddrFamily Family,
1184 TProtocol Protocol,
1185 TSocketType Type) :
1186 OSocket(Type, Family, Protocol)
1190 /*****************************************************************************/
1191 // OAcceptorSocket
1192 /*****************************************************************************/
1193 OAcceptorSocket::OAcceptorSocket(const OAcceptorSocket& sock) :
1194 OSocket(sock)
1198 /*****************************************************************************/
1199 // ~OAcceptorSocket
1200 /*****************************************************************************/
1201 OAcceptorSocket::~OAcceptorSocket()
1203 if (m_pSockRef && (*m_pSockRef)() && (m_pSockRef->release() == 0))
1205 /* mfe: prepare for forthcoming api change */
1206 osl_closeSocket((*m_pSockRef)());
1207 osl_releaseSocket((*m_pSockRef)());
1208 delete m_pSockRef;
1209 m_pSockRef = 0;
1213 /*****************************************************************************/
1214 // close
1215 /*****************************************************************************/
1216 void OAcceptorSocket::close()
1218 if (m_pSockRef && (*m_pSockRef)() && (m_pSockRef->release() == 0))
1220 osl_closeSocket((*m_pSockRef)());
1223 m_pSockRef= 0;
1226 /*****************************************************************************/
1227 // listen
1228 /*****************************************************************************/
1229 sal_Bool OAcceptorSocket::listen(sal_Int32 MaxPendingConnections)
1231 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1233 if ( m_pSockRef && (*m_pSockRef)() )
1235 return osl_listenOnSocket((*m_pSockRef)(), MaxPendingConnections);
1238 return sal_False;
1242 /*****************************************************************************/
1243 // acceptConnection
1244 /*****************************************************************************/
1245 OSocket::TResult OAcceptorSocket::acceptConnection(OStreamSocket& connection)
1247 if (m_pRecvTimeout && ! isRecvReady(m_pRecvTimeout))
1248 return TResult_TimedOut;
1250 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1251 OStreamSocket aSocket;
1253 if ( m_pSockRef && (*m_pSockRef)() )
1255 aSocket = osl_acceptConnectionOnSocket((*m_pSockRef)(), 0);
1258 if( aSocket.isValid() )
1260 connection = aSocket;
1261 return TResult_Ok;
1263 else
1265 return TResult_Error;
1270 /*****************************************************************************/
1271 // acceptConnection
1272 /*****************************************************************************/
1273 OSocket::TResult OAcceptorSocket::acceptConnection(OStreamSocket& connection,
1274 OSocketAddr& sa)
1276 oslSocketAddr PeerAddr = 0;
1277 oslSocket Socket = 0;
1279 if (m_pRecvTimeout && ! isRecvReady(m_pRecvTimeout))
1280 return TResult_TimedOut;
1282 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1284 if ( m_pSockRef && (*m_pSockRef)() )
1286 Socket= osl_acceptConnectionOnSocket((*m_pSockRef)(), &PeerAddr);
1289 if (Socket)
1291 sa= PeerAddr;
1292 connection= Socket;
1293 return TResult_Ok;
1295 else
1297 return TResult_Error;
1302 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OStreamSocket, vos),
1303 VOS_NAMESPACE(OStreamSocket, vos),
1304 VOS_NAMESPACE(OSocket, vos), 0);
1308 /*****************************************************************************/
1309 // OStreamSocket
1310 /*****************************************************************************/
1311 OStreamSocket::OStreamSocket()
1315 /*****************************************************************************/
1316 // OStreamSocket
1317 /*****************************************************************************/
1318 OStreamSocket::OStreamSocket(TAddrFamily Family,
1319 TProtocol Protocol,
1320 TSocketType Type) :
1321 OSocket(Type, Family, Protocol)
1326 /*****************************************************************************/
1327 // OStreamSocket
1328 /*****************************************************************************/
1329 OStreamSocket::OStreamSocket(oslSocket Socket) :
1330 OSocket(Socket)
1334 /*****************************************************************************/
1335 // OStreamSocket
1336 // copy constructor
1337 /*****************************************************************************/
1338 OStreamSocket::OStreamSocket(const OStreamSocket& sock) :
1339 OSocket(sock), IStream()
1343 /*****************************************************************************/
1344 // OStreamSocket
1345 // copy constructor
1346 /*****************************************************************************/
1347 OStreamSocket::OStreamSocket(const OSocket& sock) :
1348 OSocket(sock)
1352 /*****************************************************************************/
1353 // ~OStreamSocket
1354 /*****************************************************************************/
1355 OStreamSocket::~OStreamSocket()
1359 /*****************************************************************************/
1360 // close
1361 /*****************************************************************************/
1362 void OStreamSocket::close()
1364 if (m_pSockRef && (*m_pSockRef)() && (m_pSockRef->release() == 0))
1366 shutdown();
1367 osl_releaseSocket((*m_pSockRef)());
1368 delete m_pSockRef;
1371 m_pSockRef= 0;
1375 /*****************************************************************************/
1376 // operator=(oslSocket)
1377 /*****************************************************************************/
1378 OStreamSocket& OStreamSocket::operator=(oslSocket Socket)
1380 OSocket::operator=(Socket);
1382 return *this;
1385 /*****************************************************************************/
1386 // operator=
1387 /*****************************************************************************/
1388 OStreamSocket& OStreamSocket::operator= (const OSocket& sock)
1390 OSocket::operator=(sock);
1392 return *this;
1395 /*****************************************************************************/
1396 // operator=
1397 /*****************************************************************************/
1398 OStreamSocket& OStreamSocket::operator= (const OStreamSocket& sock)
1400 OSocket::operator=(sock);
1402 return *this;
1405 /*****************************************************************************/
1406 // read
1407 /*****************************************************************************/
1408 sal_Int32 OStreamSocket::read(void* pBuffer, sal_uInt32 n) const
1410 sal_uInt8 *Ptr = (sal_uInt8 *)pBuffer;
1412 if (m_pRecvTimeout && ! isRecvReady(m_pRecvTimeout))
1413 return 0;
1415 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1417 if ( ! ( m_pSockRef && (*m_pSockRef)() ) )
1419 return -1;
1422 /* loop until all desired bytes were read or an error occured */
1423 sal_uInt32 BytesRead= 0;
1424 sal_uInt32 BytesToRead= n;
1425 while (BytesToRead > 0)
1427 sal_Int32 RetVal;
1428 RetVal= osl_receiveSocket((*m_pSockRef)(),
1429 Ptr,
1430 BytesToRead,
1431 osl_Socket_MsgNormal);
1433 /* error occured? */
1434 if(RetVal <= 0)
1436 break;
1439 BytesToRead -= RetVal;
1440 BytesRead += RetVal;
1441 Ptr += RetVal;
1443 /* wait for next available data or timeout */
1444 if ( BytesToRead > 0 && m_pRecvTimeout && ! isRecvReady(m_pRecvTimeout))
1445 break;
1449 return BytesRead;
1452 /*****************************************************************************/
1453 // write
1454 /*****************************************************************************/
1455 sal_Int32 OStreamSocket::write(const void* pBuffer, sal_uInt32 n)
1457 sal_uInt8 *Ptr = (sal_uInt8 *)pBuffer;
1459 if (m_pSendTimeout && ! isSendReady(m_pSendTimeout))
1460 return 0;
1462 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1464 if ( ! ( m_pSockRef && (*m_pSockRef)() ) )
1466 return -1;
1469 /* loop until all desired bytes were send or an error occured */
1470 sal_uInt32 BytesSend= 0;
1471 sal_uInt32 BytesToSend= n;
1472 while (BytesToSend > 0)
1474 sal_Int32 RetVal;
1476 RetVal= osl_sendSocket((*m_pSockRef)(),
1477 Ptr,
1478 BytesToSend,
1479 osl_Socket_MsgNormal);
1481 /* error occured? */
1482 if(RetVal <= 0)
1484 break;
1487 BytesToSend -= RetVal;
1488 BytesSend += RetVal;
1489 Ptr += RetVal;
1491 /* wait till new data is available or timeout occures */
1492 if ( BytesToSend > 0 && m_pSendTimeout && ! isSendReady(m_pSendTimeout))
1493 break;
1496 return BytesSend;
1499 sal_Bool OStreamSocket::isEof() const
1501 return isValid();
1502 // BHO not enough
1505 /*****************************************************************************/
1506 // recv
1507 /*****************************************************************************/
1508 sal_Int32 OStreamSocket::recv(void* pBuffer,
1509 sal_uInt32 BytesToRead,
1510 TSocketMsgFlag Flag)
1512 if (m_pRecvTimeout && ! isRecvReady(m_pRecvTimeout))
1513 return 0;
1515 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1517 if ( ! ( m_pSockRef && (*m_pSockRef)() ) )
1519 return -1;
1522 return osl_receiveSocket((*m_pSockRef)(),
1523 pBuffer,
1524 BytesToRead,
1525 (oslSocketMsgFlag)Flag);
1528 /*****************************************************************************/
1529 // send
1530 /*****************************************************************************/
1531 sal_Int32 OStreamSocket::send(const void* pBuffer,
1532 sal_uInt32 BytesToSend,
1533 TSocketMsgFlag Flag)
1535 if (m_pSendTimeout && ! isSendReady(m_pSendTimeout))
1536 return 0;
1538 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1540 if ( ! ( m_pSockRef && (*m_pSockRef)() ) )
1542 return -1;
1545 return osl_sendSocket((*m_pSockRef)(),
1546 pBuffer,
1547 BytesToSend,
1548 (oslSocketMsgFlag)Flag);
1551 /*****************************************************************************/
1552 // shutdown
1553 /*****************************************************************************/
1554 sal_Bool OStreamSocket::shutdown(TSocketDirection Direction)
1556 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1558 if ( m_pSockRef && (*m_pSockRef)() )
1560 return osl_shutdownSocket((*m_pSockRef)(), (oslSocketDirection)Direction);
1563 return sal_False;
1568 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OConnectorSocket, vos),
1569 VOS_NAMESPACE(OConnectorSocket, vos),
1570 VOS_NAMESPACE(OStreamSocket, vos), 0);
1574 /*****************************************************************************/
1575 // OConnectorSocket
1576 /*****************************************************************************/
1577 OConnectorSocket::OConnectorSocket(TAddrFamily Family,
1578 TProtocol Protocol,
1579 TSocketType Type) :
1580 OStreamSocket(Family, Protocol, Type)
1584 /*****************************************************************************/
1585 // OConnectorSocket
1586 /*****************************************************************************/
1587 OConnectorSocket::OConnectorSocket(const OConnectorSocket& sock) :
1588 OStreamSocket(sock)
1592 /*****************************************************************************/
1593 // ~OConnectorSocket
1594 /*****************************************************************************/
1595 OConnectorSocket::~OConnectorSocket()
1599 /*****************************************************************************/
1600 // connect
1601 /*****************************************************************************/
1602 OSocket::TResult OConnectorSocket::connect(const OSocketAddr& Addr,
1603 const TimeValue* pTimeout)
1606 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1608 if ( m_pSockRef && (*m_pSockRef)() )
1610 return (TResult)osl_connectSocketTo((*m_pSockRef)(),
1611 (oslSocketAddr)Addr,
1612 pTimeout);
1615 return TResult_Error;
1619 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(ODatagramSocket, vos),
1620 VOS_NAMESPACE(ODatagramSocket, vos),
1621 VOS_NAMESPACE(OSocket, vos), 0);
1624 /*****************************************************************************/
1625 // ODatagramSocket
1626 /*****************************************************************************/
1627 ODatagramSocket::ODatagramSocket(TAddrFamily Family,
1628 TProtocol Protocol,
1629 TSocketType Type) :
1630 OSocket(Type, Family, Protocol)
1634 /*****************************************************************************/
1635 // ODatagramSocket
1636 /*****************************************************************************/
1637 ODatagramSocket::ODatagramSocket(const ODatagramSocket& sock) :
1638 OSocket(sock)
1642 /*****************************************************************************/
1643 // ~ODatagramSocket
1644 /*****************************************************************************/
1645 ODatagramSocket::~ODatagramSocket()
1650 /*****************************************************************************/
1651 // recvFrom
1652 /*****************************************************************************/
1653 sal_Int32 ODatagramSocket::recvFrom(void* pBuffer,
1654 sal_uInt32 BufferSize,
1655 OSocketAddr* pSenderAddr,
1656 TSocketMsgFlag Flag)
1659 if (m_pRecvTimeout && ! isRecvReady(m_pRecvTimeout))
1660 return 0;
1662 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1664 if ( ! ( m_pSockRef && (*m_pSockRef)() ) )
1666 return -1;
1669 sal_Int32 BytesRead;
1671 if(pSenderAddr)
1673 // we are interested in the senders address
1674 oslSocketAddr SenderAddr= osl_createEmptySocketAddr(osl_Socket_FamilyInet);
1676 BytesRead=
1677 osl_receiveFromSocket((*m_pSockRef)(),
1678 SenderAddr,
1679 pBuffer,
1680 BufferSize,
1681 (oslSocketMsgFlag)Flag);
1683 *pSenderAddr= SenderAddr;
1685 else
1687 // we don't want to know the senders address
1689 BytesRead=
1690 osl_receiveFromSocket((*m_pSockRef)(),
1692 pBuffer,
1693 BufferSize,
1694 (oslSocketMsgFlag)Flag);
1697 return BytesRead;
1701 /*****************************************************************************/
1702 // sendTo
1703 /*****************************************************************************/
1704 sal_Int32 ODatagramSocket::sendTo(const OSocketAddr& ReceiverAddr,
1705 const void* pBuffer,
1706 sal_uInt32 BufferSize,
1707 TSocketMsgFlag Flag)
1709 if (m_pSendTimeout && ! isSendReady(m_pSendTimeout))
1710 return 0;
1712 VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1714 if ( ( m_pSockRef && (*m_pSockRef)() ) )
1717 return osl_sendToSocket((*m_pSockRef)(),
1718 (oslSocketAddr)ReceiverAddr,
1719 pBuffer,
1720 BufferSize,
1721 (oslSocketMsgFlag)Flag);
1724 return -1;