xref: /aoo4110/main/vos/source/socket.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <stdio.h>
25 
26 #include <rtl/memory.h>
27 #include <osl/time.h>
28 #include <vos/socket.hxx>
29 #include <vos/diagnose.hxx>
30 //#include <osl/tools.h>
31 
32 using namespace vos;
33 
34 
35 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OSocketAddr, vos),
36 						VOS_NAMESPACE(OSocketAddr, vos),
37 						VOS_NAMESPACE(OObject, vos), 0);
38 
39 
40 /*****************************************************************************/
41 // OSocketAddr()
42 /*****************************************************************************/
OSocketAddr()43 OSocketAddr::OSocketAddr()
44 {
45 	m_SockAddr= 0;
46 }
47 
48 /*****************************************************************************/
49 // OSocketAddr()
50 /*****************************************************************************/
OSocketAddr(oslSocketAddr Addr)51 OSocketAddr::OSocketAddr(oslSocketAddr Addr)
52 {
53 	m_SockAddr= Addr;
54 }
55 
56 /*****************************************************************************/
57 // OSocketAddr()
58 /*****************************************************************************/
OSocketAddr(const OSocketAddr & Addr)59 OSocketAddr::OSocketAddr(const OSocketAddr& Addr) :
60 ISocketAddr(), OObject()
61 {
62 	m_SockAddr= osl_copySocketAddr((oslSocketAddr)Addr);
63 }
64 
65 /*****************************************************************************/
66 // ~OSocketAddr()
67 /*****************************************************************************/
~OSocketAddr()68 OSocketAddr::~OSocketAddr()
69 {
70 	osl_destroySocketAddr(m_SockAddr);
71 }
72 
73 
74 /*****************************************************************************/
75 // getFamily()
76 /*****************************************************************************/
getFamily() const77 OSocketAddr::TAddrFamily OSocketAddr::getFamily() const
78 {
79 	return (TAddrFamily)osl_getFamilyOfSocketAddr(m_SockAddr);
80 }
81 
82 /*****************************************************************************/
83 // operator oslSocketAddr ()
84 /*****************************************************************************/
operator oslSocketAddr() const85 OSocketAddr::operator oslSocketAddr() const
86 {
87 	return m_SockAddr;
88 }
89 
90 /*****************************************************************************/
91 // getHostname()
92 /*****************************************************************************/
getHostname(rtl::OUString & rBuffer) const93 OSocketAddr::TResult OSocketAddr::getHostname(rtl::OUString& rBuffer ) const
94 {
95 	return (TResult)osl_getHostnameOfSocketAddr(m_SockAddr, &rBuffer.pData );
96 }
97 
98 /*****************************************************************************/
99 // getLocalHostname()
100 /*****************************************************************************/
getLocalHostname(rtl::OUString & pBuffer)101 OSocketAddr::TResult OSocketAddr::getLocalHostname( rtl::OUString& pBuffer )
102 {
103 	return (TResult)osl_getLocalHostname( &pBuffer.pData );
104 }
105 
106 /*****************************************************************************/
107 // resolveHostname()
108 /*****************************************************************************/
resolveHostname(const rtl::OUString & ustrHostname)109 oslSocketAddr OSocketAddr::resolveHostname(const rtl::OUString& ustrHostname)
110 {
111 	return osl_resolveHostname( ustrHostname.pData );
112 }
113 
114 /*****************************************************************************/
115 // operator= (oslSocketAddr Addr)
116 /*****************************************************************************/
operator =(oslSocketAddr Addr)117 void OSocketAddr::operator= (oslSocketAddr Addr)
118 {
119 	if(m_SockAddr) {
120 		osl_destroySocketAddr(m_SockAddr);
121 	}
122 
123 	m_SockAddr= Addr;
124 }
125 
126 /*****************************************************************************/
127 // operator== (oslSocketAddr Addr)
128 /*****************************************************************************/
operator ==(oslSocketAddr Addr)129 sal_Bool OSocketAddr::operator== (oslSocketAddr Addr)
130 {
131 	return (osl_isEqualSocketAddr(m_SockAddr, Addr));
132 }
133 
134 /*****************************************************************************/
135 // operator=(const OSocketAddr& Addr)
136 /*****************************************************************************/
operator =(const OSocketAddr & Addr)137 OSocketAddr& OSocketAddr::operator=(const OSocketAddr& Addr)
138 {
139 	if(m_SockAddr) {
140 		osl_destroySocketAddr(m_SockAddr);
141 	}
142 
143 	m_SockAddr= osl_copySocketAddr((oslSocketAddr)Addr);
144 
145 	return *this;
146 }
147 
148 
149 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OInetSocketAddr, vos),
150 						VOS_NAMESPACE(OInetSocketAddr, vos),
151 						VOS_NAMESPACE(OSocketAddr, vos), 0);
152 
153 
154 
155 /*****************************************************************************/
156 // OInetSocketAddr
157 // creates arbitrary inet-address (INADDR_ANY)
158 /*****************************************************************************/
OInetSocketAddr()159 OInetSocketAddr::OInetSocketAddr()
160 {
161 	m_SockAddr= osl_createEmptySocketAddr(osl_Socket_FamilyInet);
162 }
163 
164 /*****************************************************************************/
165 // OInetSocketAddr
166 // uses the given oslSocketAddr as its own
167 /*****************************************************************************/
OInetSocketAddr(oslSocketAddr Addr)168 OInetSocketAddr::OInetSocketAddr(oslSocketAddr Addr) :
169 	OSocketAddr(Addr)
170 {
171 	VOS_ASSERT(osl_getFamilyOfSocketAddr(Addr) == osl_Socket_FamilyInet);
172 }
173 
174 /*****************************************************************************/
175 // OInetSocketAddr
176 // Create a socket address either from a dotted decimal address
177 //(e.g. 141.99.128.50) or a hostname (e.g. www.stardiv.de).
178 /*****************************************************************************/
OInetSocketAddr(const rtl::OUString & ustrAddrOrHostname,sal_Int32 Port)179 OInetSocketAddr::OInetSocketAddr(const rtl::OUString& ustrAddrOrHostname, sal_Int32 Port)
180 {
181 	// first try as dotted address.
182 	m_SockAddr= osl_createInetSocketAddr(ustrAddrOrHostname.pData, Port);
183 
184 	// create failed, maybe it's an hostname
185 	if(m_SockAddr == 0)
186 	{
187 		m_SockAddr= osl_resolveHostname(ustrAddrOrHostname.pData);
188 
189 		// host found?
190 		if(m_SockAddr)
191 		{
192 			// set port will fail if addrtype is not osl_Socket_FamilyInet
193 			VOS_VERIFY(osl_setInetPortOfSocketAddr(m_SockAddr, Port));
194 		}
195 	}
196 }
197 
198 /*****************************************************************************/
199 // OInetSocketAddr(const OInetSocketAddr&)
200 /*****************************************************************************/
OInetSocketAddr(const OInetSocketAddr & sa)201 OInetSocketAddr::OInetSocketAddr(const OInetSocketAddr& sa) :
202 	OSocketAddr(sa)
203 {
204 	VOS_ASSERT(getFamily() == TFamily_Inet);
205 }
206 
207 /*****************************************************************************/
208 // OInetSocketAddr(const OSocketAddr&)
209 /*****************************************************************************/
OInetSocketAddr(const OSocketAddr & sa)210 OInetSocketAddr::OInetSocketAddr(const OSocketAddr& sa) :
211 	OSocketAddr(sa)
212 {
213 	VOS_ASSERT(sa.getFamily() == TFamily_Inet);
214 }
215 
216 /*****************************************************************************/
217 // ~OInetSocketAddr
218 /*****************************************************************************/
~OInetSocketAddr()219 OInetSocketAddr::~OInetSocketAddr()
220 {
221 }
222 
223 /*****************************************************************************/
224 // operator= (oslSocketAddr Addr)
225 /*****************************************************************************/
operator =(oslSocketAddr Addr)226 void OInetSocketAddr::operator= (oslSocketAddr Addr)
227 {
228 	VOS_PRECOND(osl_getFamilyOfSocketAddr(Addr) == osl_Socket_FamilyInet,
229 				"oslSocketAddr not of type osl_Socket_FamilyInet!");
230 
231 	OSocketAddr::operator=(Addr);
232 }
233 
234 /*****************************************************************************/
235 // operator== (oslSocketAddr Addr)
236 /*****************************************************************************/
operator ==(oslSocketAddr Addr)237 sal_Bool OInetSocketAddr::operator== (oslSocketAddr Addr)
238 {
239 	return (osl_isEqualSocketAddr(m_SockAddr, Addr));
240 }
241 
242 /*****************************************************************************/
243 // operator=(const OInetSocketAddr& Addr)
244 /*****************************************************************************/
operator =(const OInetSocketAddr & Addr)245 OInetSocketAddr& OInetSocketAddr::operator=(const OInetSocketAddr& Addr)
246 {
247 	VOS_ASSERT(Addr.getFamily() == TFamily_Inet);
248 
249 	OSocketAddr::operator=(Addr);
250 
251 	return *this;
252 }
253 
254 /*****************************************************************************/
255 // operator=(const OSocketAddr& Addr)
256 /*****************************************************************************/
operator =(const OSocketAddr & Addr)257 OInetSocketAddr& OInetSocketAddr::operator=(const OSocketAddr& Addr)
258 {
259 	VOS_ASSERT(Addr.getFamily() == TFamily_Inet);
260 
261 	OSocketAddr::operator=(Addr);
262 
263 	return *this;
264 }
265 
266 /*****************************************************************************/
267 // getServicePort()
268 /*****************************************************************************/
getServicePort(const rtl::OUString & ustrServiceName,const rtl::OUString & ustrProtocolName)269 sal_Int32 OInetSocketAddr::getServicePort(const rtl::OUString& ustrServiceName,
270 									const rtl::OUString& ustrProtocolName)
271 {
272 	return osl_getServicePort(ustrServiceName.pData, ustrProtocolName.pData);
273 }
274 
275 
276 /*****************************************************************************/
277 // getPort()
278 /*****************************************************************************/
getPort() const279 sal_Int32 OInetSocketAddr::getPort () const
280 {
281 	return osl_getInetPortOfSocketAddr(m_SockAddr);
282 }
283 
284 
285 /*****************************************************************************/
286 // setPort()
287 /*****************************************************************************/
setPort(sal_Int32 Port)288 sal_Bool OInetSocketAddr::setPort (sal_Int32 Port)
289 {
290 	return osl_setInetPortOfSocketAddr(m_SockAddr, Port);
291 }
292 
293 
294 /*****************************************************************************/
295 // getDottedAddr()
296 /*****************************************************************************/
getDottedAddr(rtl::OUString & pBuffer) const297 OSocketAddr::TResult OInetSocketAddr::getDottedAddr( rtl::OUString& pBuffer ) const
298 {
299 	return (TResult)osl_getDottedInetAddrOfSocketAddr(m_SockAddr, &pBuffer.pData );
300 }
301 
302 /*****************************************************************************/
303 // setAddr()
304 /*****************************************************************************/
setAddr(const rtl::OUString & ustrAddrOrHostname)305 sal_Bool OInetSocketAddr::setAddr(const rtl::OUString& ustrAddrOrHostname)
306 {
307 	sal_Int32 Port = 0;
308 
309 	if(m_SockAddr) {
310 
311 		// retrieve old port
312 		Port= getPort();
313 
314 		// free old address
315 		osl_destroySocketAddr(m_SockAddr);
316 		m_SockAddr= 0;
317 	}
318 
319 	// first try as dotted address.
320 	m_SockAddr= osl_createInetSocketAddr(ustrAddrOrHostname.pData, Port);
321 
322 	// create failed, maybe it's an hostname
323 	if(m_SockAddr == 0)
324 	{
325 
326 		m_SockAddr= osl_resolveHostname( ustrAddrOrHostname.pData );
327 
328 		// host found?
329 		if(m_SockAddr==0)
330 		{
331 			return sal_False;
332 		}
333 
334 		// set port will fail if addrtype is not osl_Socket_FamilyInet
335 		VOS_VERIFY(osl_setInetPortOfSocketAddr(m_SockAddr, Port));
336 
337 	}
338 
339 	return sal_True;
340 }
341 
342 ///////////////////////////////////////////////////////////////////////////////
343 // OIpxSocketAddr
344 
345 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OIpxSocketAddr, vos),
346 						VOS_NAMESPACE(OIpxSocketAddr, vos),
347 						VOS_NAMESPACE(OSocketAddr, vos), 0);
348 
349 
350 /*****************************************************************************/
351 // OIpxSocketAddr()
352 /*****************************************************************************/
OIpxSocketAddr()353 OIpxSocketAddr::OIpxSocketAddr()
354 {
355 	m_SockAddr= osl_createEmptySocketAddr(osl_Socket_FamilyIpx);
356 }
357 
358 /*****************************************************************************/
359 // OIpxSocketAddr(oslSocketAddr)
360 /*****************************************************************************/
OIpxSocketAddr(oslSocketAddr Addr)361 OIpxSocketAddr::OIpxSocketAddr(oslSocketAddr Addr) :
362 	OSocketAddr(Addr)
363 {
364 	VOS_ASSERT(osl_getFamilyOfSocketAddr(Addr) == osl_Socket_FamilyIpx);
365 }
366 
367 /*****************************************************************************/
368 // OIpxSocketAddr()
369 /*****************************************************************************/
OIpxSocketAddr(const rtl::OUString &,const rtl::OUString &,sal_uInt32)370 OIpxSocketAddr::OIpxSocketAddr(const rtl::OUString&,
371 							   const rtl::OUString&,
372 							   sal_uInt32 )
373 {
374 	// jbu : functionality removed from vos
375 }
376 
377 /*****************************************************************************/
378 // OIpxSocketAddr(OIpxSocketAddr&)
379 /*****************************************************************************/
OIpxSocketAddr(const OIpxSocketAddr & sa)380 OIpxSocketAddr::OIpxSocketAddr(const OIpxSocketAddr& sa) :
381 	OSocketAddr(sa)
382 {
383 	VOS_ASSERT(sa.getFamily() == TFamily_Ipx);
384 }
385 
386 
387 /*****************************************************************************/
388 // OIpxSocketAddr(OSocketAddr&)
389 /*****************************************************************************/
OIpxSocketAddr(const OSocketAddr & sa)390 OIpxSocketAddr::OIpxSocketAddr(const OSocketAddr& sa) :
391 	OSocketAddr(sa)
392 {
393 	VOS_ASSERT(sa.getFamily() == TFamily_Ipx);
394 }
395 
396 /*****************************************************************************/
397 // ~OIpxSocketAddr()
398 /*****************************************************************************/
~OIpxSocketAddr()399 OIpxSocketAddr::~OIpxSocketAddr()
400 {
401 }
402 
403 
404 /*****************************************************************************/
405 // operator=()
406 /*****************************************************************************/
operator =(oslSocketAddr Addr)407 void OIpxSocketAddr::operator= (oslSocketAddr Addr)
408 {
409 	VOS_PRECOND(osl_getFamilyOfSocketAddr(Addr) == osl_Socket_FamilyIpx,
410 			"oslSocketAddr not of type osl_Socket_FamilyIpx!");
411 
412 	OSocketAddr::operator=(Addr);
413 }
414 
415 /*****************************************************************************/
416 // operator== (oslSocketAddr Addr)
417 /*****************************************************************************/
operator ==(oslSocketAddr Addr)418 sal_Bool OIpxSocketAddr::operator== (oslSocketAddr Addr)
419 {
420 	return (osl_isEqualSocketAddr(m_SockAddr, Addr));
421 }
422 
423 /*****************************************************************************/
424 // operator=(const OIpxSocketAddr& Addr)
425 /*****************************************************************************/
operator =(const OIpxSocketAddr & Addr)426 OIpxSocketAddr& OIpxSocketAddr::operator=(const OIpxSocketAddr& Addr)
427 {
428 	VOS_ASSERT(Addr.getFamily() == TFamily_Ipx);
429 
430 	OSocketAddr::operator=(Addr);
431 
432 	return *this;
433 }
434 
435 /*****************************************************************************/
436 // operator=(const OSocketAddr& Addr)
437 /*****************************************************************************/
operator =(const OSocketAddr & Addr)438 OIpxSocketAddr& OIpxSocketAddr::operator=(const OSocketAddr& Addr)
439 {
440 	VOS_ASSERT(Addr.getFamily() == TFamily_Ipx);
441 
442 	OSocketAddr::operator=(Addr);
443 
444 	return *this;
445 }
446 
447 /*****************************************************************************/
448 // getNetNumber()
449 /*****************************************************************************/
getNetNumber(TIpxNetNumber &) const450 OSocketAddr::TResult OIpxSocketAddr::getNetNumber(TIpxNetNumber&) const
451 {
452 	// jbu : functionality removed from vos
453 	return (TResult)0;
454 }
455 
456 /*****************************************************************************/
457 // getNodeNumber()
458 /*****************************************************************************/
getNodeNumber(TIpxNodeNumber &) const459 OSocketAddr::TResult OIpxSocketAddr::getNodeNumber(TIpxNodeNumber& ) const
460 {
461 	// jbu : functionality removed from vos
462 	return (TResult)0;
463 }
464 
465 /*****************************************************************************/
466 // getSocketNumber()
467 /*****************************************************************************/
getSocketNumber() const468 sal_uInt32 OIpxSocketAddr::getSocketNumber() const
469 {
470 //  	return osl_getIpxSocketNumber(m_SockAddr);
471 	return (TResult)0;
472 }
473 
474 
475 /*****************************************************************************/
476 // getAddressString()
477 /*****************************************************************************/
478 //void OIpxSocketAddr::getAddressString(sal_Char* Buffer, sal_uInt32 Len) const
getAddressString(rtl::OUString &) const479 void OIpxSocketAddr::getAddressString( rtl::OUString& ) const
480 {
481 	// jbu : functionality removed from vos
482 }
483 
484 
485 ///////////////////////////////////////////////////////////////////////////////
486 // Socket
487 
488 
489 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OSocket, vos),
490 						VOS_NAMESPACE(OSocket, vos),
491 						VOS_NAMESPACE(OObject, vos), 0);
492 
493 /*****************************************************************************/
494 // OSocket()
495 /*****************************************************************************/
OSocket()496 OSocket::OSocket()
497 {
498 	m_pRecvTimeout = 0;
499 	m_pSendTimeout = 0;
500 
501 	m_pSockRef= 0;
502 }
503 
504 
505 /*****************************************************************************/
506 // OSocket()
507 /*****************************************************************************/
OSocket(TSocketType Type,TAddrFamily Family,TProtocol Protocol)508 OSocket::OSocket(TSocketType Type,
509 				 TAddrFamily Family,
510 				 TProtocol   Protocol)
511 {
512 	m_pRecvTimeout = 0;
513 	m_pSendTimeout = 0;
514 
515 	m_pSockRef=
516 		new SockRef(osl_createSocket((oslAddrFamily)Family,
517 									 (oslSocketType)Type,
518 									 (oslProtocol)Protocol));
519 
520 	VOS_POSTCOND(m_pSockRef != 0, "OSocket(): new failed.\n");
521 	VOS_POSTCOND((*m_pSockRef)(), "OSocket(): creation of socket failed!\n");
522 }
523 
524 /*****************************************************************************/
525 // OSocket()
526 /*****************************************************************************/
OSocket(const OSocket & sock)527 OSocket::OSocket(const OSocket& sock) :
528 ISocketTypes(), OReference(), OObject()
529 {
530 	m_pRecvTimeout = 0;
531 	m_pSendTimeout = 0;
532 	m_pSockRef=0;
533 
534 	VOS_ASSERT(sock.m_pSockRef != 0);
535 
536     if ( sock.m_pSockRef !=0 )
537     {
538         m_pSockRef= sock.m_pSockRef;
539 
540         setRecvTimeout(sock.m_pRecvTimeout);
541         setSendTimeout(sock.m_pSendTimeout);
542 
543         m_pSockRef->acquire();
544     }
545 }
546 
547 /*****************************************************************************/
548 // OSocket()
549 /*****************************************************************************/
OSocket(oslSocket Socket)550 OSocket::OSocket(oslSocket Socket)
551 {
552 	m_pRecvTimeout = 0;
553 	m_pSendTimeout = 0;
554 
555 	m_pSockRef = new SockRef(Socket);
556 }
557 
558 
559 /*****************************************************************************/
560 // ~OSocket()
561 /*****************************************************************************/
~OSocket()562 OSocket::~OSocket()
563 {
564 	close();
565 
566 	delete m_pRecvTimeout;
567 	delete m_pSendTimeout;
568 }
569 
570 
571 /*****************************************************************************/
572 // create
573 /*****************************************************************************/
create(TSocketType Type,TAddrFamily Family,TProtocol Protocol)574 sal_Bool OSocket::create(TSocketType Type,
575 						TAddrFamily Family,
576 						TProtocol   Protocol)
577 {
578 	// if this was a valid socket, decrease reference
579 	if ((m_pSockRef) && (m_pSockRef->release() == 0))
580 	{
581 		osl_releaseSocket((*m_pSockRef)());
582 		delete m_pSockRef;
583 		m_pSockRef= 0;
584 	}
585 
586 	m_pSockRef=
587 		new SockRef(osl_createSocket((oslAddrFamily)Family,
588 									 (oslSocketType)Type,
589 									 (oslProtocol)Protocol));
590 
591 	VOS_POSTCOND(m_pSockRef != 0, "OSocket(): new failed.\n");
592 
593 	return (*m_pSockRef)() != 0;
594 }
595 
596 /*****************************************************************************/
597 // operator=
598 /*****************************************************************************/
operator =(const OSocket & sock)599 OSocket& OSocket::operator= (const OSocket& sock)
600 {
601 	VOS_PRECOND(sock.m_pSockRef != 0, "OSocket::operator=: tried to assign an empty/invalid socket\n");
602 
603 	if (m_pSockRef == sock.m_pSockRef)
604 		return *this;
605 
606 	// if this was a valid socket, decrease reference
607 	if ((m_pSockRef) && (m_pSockRef->release() == 0))
608 	{
609 		osl_releaseSocket((*m_pSockRef)());
610 		delete m_pSockRef;
611 		m_pSockRef= 0;
612 	}
613 
614 	m_pSockRef= sock.m_pSockRef;
615 
616 	setRecvTimeout(sock.m_pRecvTimeout);
617 	setSendTimeout(sock.m_pSendTimeout);
618 
619 	m_pSockRef->acquire();
620 
621 	return *this;
622 }
623 
624 /*****************************************************************************/
625 // operator oslSocket()
626 /*****************************************************************************/
operator oslSocket() const627 OSocket::operator oslSocket() const
628 {
629 	VOS_ASSERT(m_pSockRef);
630 	return (*m_pSockRef)();
631 }
632 
633 /*****************************************************************************/
634 // isValid()
635 /*****************************************************************************/
isValid() const636 sal_Bool OSocket::isValid() const
637 {
638 	return m_pSockRef != 0 && (*m_pSockRef)() != 0;
639 }
640 
641 
642 /*****************************************************************************/
643 // close
644 /*****************************************************************************/
close()645 void OSocket::close()
646 {
647 	if (m_pSockRef && (*m_pSockRef)() && (m_pSockRef->release() == 0))
648 	{
649 		osl_releaseSocket((*m_pSockRef)());
650 		delete m_pSockRef;
651 	}
652 
653 	m_pSockRef= 0;
654 }
655 
656 /*****************************************************************************/
657 // getLocalAddr
658 /*****************************************************************************/
getLocalAddr(OSocketAddr & sa) const659 void OSocket::getLocalAddr(OSocketAddr& sa) const
660 {
661 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
662 
663     if ( m_pSockRef && (*m_pSockRef)() )
664     {
665         sa= osl_getLocalAddrOfSocket((*m_pSockRef)());
666     }
667 }
668 
669 /*****************************************************************************/
670 // getLocalPort
671 /*****************************************************************************/
getLocalPort() const672 sal_Int32 OSocket::getLocalPort() const
673 {
674 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
675 
676 	sal_Int32 Port= OSL_INVALID_PORT;
677 
678     if ( m_pSockRef && (*m_pSockRef)() )
679     {
680         oslSocketAddr Addr= osl_getLocalAddrOfSocket((*m_pSockRef)());
681 
682         if(Addr)
683         {
684             Port= osl_getInetPortOfSocketAddr(Addr);
685             osl_destroySocketAddr(Addr);
686         }
687     }
688 
689 	return Port;
690 }
691 
692 /*****************************************************************************/
693 // getLocalHost
694 /*****************************************************************************/
getLocalHost(rtl::OUString & pBuffer) const695 OSocket::TResult OSocket::getLocalHost( rtl::OUString& pBuffer) const
696 {
697 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
698 
699     if ( m_pSockRef && (*m_pSockRef)() )
700     {
701         oslSocketAddr Addr= osl_getLocalAddrOfSocket((*m_pSockRef)());
702 
703         if(Addr)
704         {
705 //	    	TResult Result= (TResult)osl_getHostnameOfSocketAddr(Addr,
706 //														pBuffer, BufferSize);
707             TResult Result= (TResult)osl_getHostnameOfSocketAddr(Addr,
708 															 &pBuffer.pData );
709 
710             osl_destroySocketAddr(Addr);
711 
712             return Result;
713         }
714     }
715 
716 	return TResult_Error;
717 }
718 
719 /*****************************************************************************/
720 // getPeerAddr
721 /*****************************************************************************/
getPeerAddr(OSocketAddr & sa) const722 void OSocket::getPeerAddr(OSocketAddr& sa) const
723 {
724 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
725 
726     if ( m_pSockRef && (*m_pSockRef)() )
727     {
728         sa= osl_getPeerAddrOfSocket((*m_pSockRef)());
729     }
730 }
731 
732 /*****************************************************************************/
733 // getPeerPort
734 /*****************************************************************************/
getPeerPort() const735 sal_Int32 OSocket::getPeerPort() const
736 {
737 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
738 
739     sal_Int32 Port= OSL_INVALID_PORT;
740 
741     if ( m_pSockRef && (*m_pSockRef)() )
742     {
743         oslSocketAddr Addr= osl_getPeerAddrOfSocket((*m_pSockRef)());
744 
745         if(Addr)
746         {
747             Port= osl_getInetPortOfSocketAddr(Addr);
748             osl_destroySocketAddr(Addr);
749         }
750     }
751 
752 	return Port;
753 }
754 
755 /*****************************************************************************/
756 // getPeerHost
757 /*****************************************************************************/
getPeerHost(rtl::OUString & pBuffer) const758 OSocket::TResult OSocket::getPeerHost( rtl::OUString& pBuffer ) const
759 {
760 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
761 
762     if ( m_pSockRef && (*m_pSockRef)() )
763     {
764         oslSocketAddr Addr= osl_getPeerAddrOfSocket((*m_pSockRef)());
765 
766         if(Addr)
767         {
768 //		    TResult Result= (TResult)osl_getHostnameOfSocketAddr(Addr,
769 //														pBuffer, BufferSize);
770             TResult Result= (TResult)osl_getHostnameOfSocketAddr(Addr,
771 															 &pBuffer.pData );
772 
773             osl_destroySocketAddr(Addr);
774 
775             return Result;
776         }
777     }
778 
779 	return TResult_Error;
780 }
781 
782 /*****************************************************************************/
783 // bind
784 /*****************************************************************************/
bind(const OSocketAddr & Addr)785 sal_Bool OSocket::bind(const OSocketAddr& Addr)
786 {
787 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
788 
789     if ( m_pSockRef && (*m_pSockRef)() )
790     {
791         return osl_bindAddrToSocket((*m_pSockRef)(), (oslSocketAddr)Addr);
792     }
793 
794 	return sal_False;
795 }
796 
797 
798 /*****************************************************************************/
799 // setSendTimeout
800 /*****************************************************************************/
setSendTimeout(const TimeValue * pTimeout)801 void OSocket::setSendTimeout(const TimeValue* pTimeout)
802 {
803 	delete m_pSendTimeout;
804 
805 	if (pTimeout)
806 		m_pSendTimeout = new TimeValue(*pTimeout);
807 	else
808 		m_pSendTimeout = 0;
809 }
810 
811 /*****************************************************************************/
812 // setRecvTimeout
813 /*****************************************************************************/
setRecvTimeout(const TimeValue * pTimeout)814 void OSocket::setRecvTimeout(const TimeValue* pTimeout)
815 {
816 	delete m_pRecvTimeout;
817 
818 	if (pTimeout)
819 		m_pRecvTimeout = new TimeValue(*pTimeout);
820 	else
821 		m_pRecvTimeout = 0;
822 }
823 
824 /*****************************************************************************/
825 // isRecvReady
826 /*****************************************************************************/
isRecvReady(const TimeValue * pTimeout) const827 sal_Bool OSocket::isRecvReady(const TimeValue* pTimeout) const
828 {
829 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
830 
831     if ( m_pSockRef && (*m_pSockRef)() )
832     {
833         return osl_isReceiveReady((*m_pSockRef)(), pTimeout);
834     }
835 
836     return sal_False;
837 }
838 
839 /*****************************************************************************/
840 // isSendReady
841 /*****************************************************************************/
isSendReady(const TimeValue * pTimeout) const842 sal_Bool OSocket::isSendReady(const TimeValue* pTimeout) const
843 {
844 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
845 
846     if ( m_pSockRef && (*m_pSockRef)() )
847     {
848         return osl_isSendReady((*m_pSockRef)(), pTimeout);
849     }
850 
851     return sal_False;
852 }
853 
854 /*****************************************************************************/
855 // isExceptionPending
856 /*****************************************************************************/
isExceptionPending(const TimeValue * pTimeout) const857 sal_Bool OSocket::isExceptionPending(const TimeValue* pTimeout) const
858 {
859 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
860 
861     if ( m_pSockRef && (*m_pSockRef)() )
862     {
863         return osl_isExceptionPending((*m_pSockRef)(), pTimeout);
864     }
865 
866     return sal_False;
867 }
868 
869 
870 /*****************************************************************************/
871 // getOption
872 /*****************************************************************************/
getOption(TSocketOption Option,void * pBuffer,sal_uInt32 BufferLen,TSocketOptionLevel Level) const873 sal_Int32 OSocket::getOption(TSocketOption Option,
874 					   	   void* pBuffer,
875 					       sal_uInt32 BufferLen,
876 					       TSocketOptionLevel Level) const
877 {
878 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
879 
880     if ( m_pSockRef && (*m_pSockRef)() )
881     {
882         return osl_getSocketOption((*m_pSockRef)(),
883                                    (oslSocketOptionLevel)Level,
884                                    (oslSocketOption)Option,
885                                    pBuffer,
886                                    BufferLen);
887     }
888 
889     return sal_False;
890 }
891 
892 /*****************************************************************************/
893 // setOption
894 /*****************************************************************************/
setOption(TSocketOption Option,void * pBuffer,sal_uInt32 BufferLen,TSocketOptionLevel Level) const895 sal_Bool OSocket::setOption(TSocketOption Option,
896 						   void* pBuffer,
897 						   sal_uInt32 BufferLen,
898 						   TSocketOptionLevel Level) const
899 {
900 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
901 
902     if ( m_pSockRef && (*m_pSockRef)() )
903     {
904         return osl_setSocketOption((*m_pSockRef)(),
905                                    (oslSocketOptionLevel)Level,
906                                    (oslSocketOption)Option,
907                                    pBuffer,
908                                    BufferLen);
909     }
910 
911     return sal_False;
912 }
913 
914 
915 /*****************************************************************************/
916 // enableNonBlockingMode
917 /*****************************************************************************/
enableNonBlockingMode(sal_Bool On)918 sal_Bool OSocket::enableNonBlockingMode(sal_Bool On)
919 {
920 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
921 
922     if ( m_pSockRef && (*m_pSockRef)() )
923     {
924         return osl_enableNonBlockingMode((*m_pSockRef)(), On);
925     }
926 
927     return sal_False;
928 }
929 
930 /*****************************************************************************/
931 // isNonBlockingMode
932 /*****************************************************************************/
isNonBlockingMode() const933 sal_Bool OSocket::isNonBlockingMode() const
934 {
935 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
936 
937     if ( m_pSockRef && (*m_pSockRef)() )
938     {
939         return osl_isNonBlockingMode((*m_pSockRef)());
940     }
941 
942     return sal_False;
943 }
944 
945 /*****************************************************************************/
946 // getType
947 /*****************************************************************************/
getType() const948 OSocket::TSocketType OSocket::getType() const
949 {
950 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
951 
952     if ( m_pSockRef && (*m_pSockRef)() )
953     {
954         return (TSocketType)osl_getSocketType((*m_pSockRef)());
955     }
956 
957     return TType_Invalid;
958 }
959 
960 /*****************************************************************************/
961 // clearError
962 /*****************************************************************************/
clearError() const963 sal_Int32 OSocket::clearError() const
964 {
965 	sal_Int32 err = 0;
966 
967 	getOption(TOption_Error, &err, sizeof(err));
968 
969 	return err;
970 }
971 
972 /*****************************************************************************/
973 // setDebug
974 /*****************************************************************************/
setDebug(sal_Int32 opt) const975 sal_Int32 OSocket::setDebug(sal_Int32 opt) const
976 {
977 	sal_Int32 old= 0;
978 
979 	getOption(TOption_Debug, &old, sizeof(old));
980 
981 	if (opt != -1)
982 		setOption(TOption_Debug, &opt, sizeof(opt));
983 
984 	return old;
985 }
986 
987 /*****************************************************************************/
988 // setReuseAddr
989 /*****************************************************************************/
setReuseAddr(sal_Int32 opt) const990 sal_Int32 OSocket::setReuseAddr(sal_Int32 opt) const
991 {
992 	sal_Int32 old = 0;
993 
994 	getOption(TOption_ReuseAddr, &old, sizeof(old));
995 
996 	if (opt != -1)
997 		setOption(TOption_ReuseAddr, &opt, sizeof(opt));
998 
999 	return (old);
1000 }
1001 
1002 /*****************************************************************************/
1003 // setKeepAlive
1004 /*****************************************************************************/
setKeepAlive(sal_Int32 opt) const1005 sal_Int32 OSocket::setKeepAlive(sal_Int32 opt) const
1006 {
1007 	sal_Int32 old = 0;
1008 
1009 	getOption(TOption_KeepAlive, &old, sizeof(old));
1010 
1011 	if (opt != -1)
1012 		setOption(TOption_KeepAlive, &opt, sizeof(opt));
1013 
1014 	return (old);
1015 }
1016 
1017 /*****************************************************************************/
1018 // setDontRoute
1019 /*****************************************************************************/
setDontRoute(sal_Int32 opt) const1020 sal_Int32 OSocket::setDontRoute(sal_Int32 opt) const
1021 {
1022 	sal_Int32 old = 0;
1023 
1024 	getOption(TOption_DontRoute, &old, sizeof(old));
1025 
1026 	if (opt != -1)
1027 		setOption(TOption_DontRoute, &opt, sizeof(opt));
1028 
1029 	return (old);
1030 }
1031 
1032 /*****************************************************************************/
1033 // setBroadcast
1034 /*****************************************************************************/
setBroadcast(sal_Int32 opt) const1035 sal_Int32 OSocket::setBroadcast(sal_Int32 opt) const
1036 {
1037 	sal_Int32 old = 0;
1038 
1039 	getOption(TOption_Broadcast, &old, sizeof(old));
1040 
1041 	if (opt != -1)
1042 		setOption(TOption_Broadcast, &opt, sizeof(opt));
1043 
1044 	return (old);
1045 }
1046 
1047 /*****************************************************************************/
1048 // setOobinline
1049 /*****************************************************************************/
setOobinline(sal_Int32 opt) const1050 sal_Int32 OSocket::setOobinline(sal_Int32 opt) const
1051 {
1052 	sal_Int32 old = 0;
1053 
1054 	getOption(TOption_OOBinLine, &old, sizeof(old));
1055 
1056 	if (opt != -1)
1057 		setOption(TOption_OOBinLine, &opt, sizeof(opt));
1058 
1059 	return (old);
1060 }
1061 
1062 /*****************************************************************************/
1063 // setLinger
1064 /*****************************************************************************/
setLinger(sal_Int32 time) const1065 sal_Int32 OSocket::setLinger(sal_Int32 time) const
1066 {
1067 	/* local decl. of linger-struct */
1068 	struct SockLinger
1069 	{
1070 		sal_Int32 m_onoff;    // option on/off
1071 		sal_Int32 m_linger;   // linger time
1072 	};
1073 
1074 
1075 	SockLinger  old = { 0, 0 };
1076 
1077 	getOption(TOption_Linger, &old, sizeof(old));
1078 
1079 	if (time > 0) // enable linger with wait-times > 0
1080 	{
1081 		SockLinger nw = { 1, time };
1082 		setOption(TOption_Linger, &nw, sizeof(nw));
1083 	}
1084 	else if (time == 0) // disable linger with wait-time == 0
1085 	{
1086 		SockLinger nw = { 0, old.m_linger };
1087 		setOption(TOption_Linger, &nw, sizeof(nw));
1088 	}
1089 
1090 	// returns 0 if linger was off, else the linger-time
1091 	return (old.m_onoff ? old.m_linger : 0);
1092 }
1093 
1094 /*****************************************************************************/
1095 // setSendBufSize
1096 /*****************************************************************************/
setSendBufSize(sal_Int32 sz) const1097 sal_Int32 OSocket::setSendBufSize(sal_Int32 sz) const
1098 {
1099 	sal_Int32 old = 0;
1100 
1101 	getOption(TOption_SndBuf, &old, sizeof(old));
1102 
1103 	if (sz >= 0)
1104 		setOption(TOption_SndBuf, &sz, sizeof(sz));
1105 
1106 	return (old);
1107 }
1108 
1109 /*****************************************************************************/
1110 // setRecvBufSize
1111 /*****************************************************************************/
setRecvBufSize(sal_Int32 sz) const1112 sal_Int32 OSocket::setRecvBufSize(sal_Int32 sz) const
1113 {
1114 	sal_Int32 old = 0;
1115 
1116 	getOption(TOption_RcvBuf, &old, sizeof(old));
1117 
1118 	if (sz >= 0)
1119 		setOption(TOption_RcvBuf, &sz, sizeof(sz));
1120 
1121 	return (old);
1122 }
1123 
1124 /*****************************************************************************/
1125 // setTcpNoDelay
1126 /*****************************************************************************/
setTcpNoDelay(sal_Int32 sz) const1127 sal_Int32 OSocket::setTcpNoDelay(sal_Int32 sz) const
1128 {
1129 	sal_Int32 old = 0;
1130 
1131 	getOption(TOption_TcpNoDelay, &old, sizeof(old), TLevel_Tcp);
1132 
1133 	if (sz >= 0)
1134 		setOption(TOption_TcpNoDelay, &sz, sizeof(sz), TLevel_Tcp);
1135 
1136 	return (old);
1137 }
1138 
1139 /*****************************************************************************/
1140 // getError
1141 /*****************************************************************************/
1142 //void OSocket::getError(sal_Char* pBuffer, sal_uInt32 nSize) const
getError(rtl::OUString & pBuffer) const1143 void OSocket::getError( rtl::OUString& pBuffer ) const
1144 {
1145 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1146 
1147     if (m_pSockRef && (*m_pSockRef)())
1148 		osl_getLastSocketErrorDescription((*m_pSockRef)(), &pBuffer.pData );
1149 	else
1150 		osl_getLastSocketErrorDescription(NULL, &pBuffer.pData );
1151 }
1152 
1153 /*****************************************************************************/
1154 // getError
1155 /*****************************************************************************/
getError() const1156 OSocket::TSocketError OSocket::getError() const
1157 {
1158 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1159 
1160     if (m_pSockRef && (*m_pSockRef)())
1161 		return (TSocketError)osl_getLastSocketError((*m_pSockRef)());
1162 	else
1163 		return (TSocketError)osl_getLastSocketError(NULL);
1164 }
1165 
1166 
1167 
1168 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OAcceptorSocket, vos),
1169 						VOS_NAMESPACE(OAcceptorSocket, vos),
1170 						VOS_NAMESPACE(OSocket, vos), 0);
1171 
1172 
1173 /*****************************************************************************/
1174 // OAcceptorSocket
1175 /*****************************************************************************/
OAcceptorSocket(TAddrFamily Family,TProtocol Protocol,TSocketType Type)1176 OAcceptorSocket::OAcceptorSocket(TAddrFamily Family,
1177 								 TProtocol   Protocol,
1178 								 TSocketType Type) :
1179 	OSocket(Type, Family, Protocol)
1180 {
1181 }
1182 
1183 /*****************************************************************************/
1184 // OAcceptorSocket
1185 /*****************************************************************************/
OAcceptorSocket(const OAcceptorSocket & sock)1186 OAcceptorSocket::OAcceptorSocket(const OAcceptorSocket& sock) :
1187 	OSocket(sock)
1188 {
1189 }
1190 
1191 /*****************************************************************************/
1192 // ~OAcceptorSocket
1193 /*****************************************************************************/
~OAcceptorSocket()1194 OAcceptorSocket::~OAcceptorSocket()
1195 {
1196 	if (m_pSockRef && (*m_pSockRef)() && (m_pSockRef->release() == 0))
1197 	{
1198         /* mfe: prepare for forthcoming api change */
1199 		osl_closeSocket((*m_pSockRef)());
1200 		osl_releaseSocket((*m_pSockRef)());
1201 		delete m_pSockRef;
1202 		m_pSockRef = 0;
1203 	}
1204 }
1205 
1206 /*****************************************************************************/
1207 // close
1208 /*****************************************************************************/
close()1209 void OAcceptorSocket::close()
1210 {
1211 	if (m_pSockRef && (*m_pSockRef)() && (m_pSockRef->release() == 0))
1212 	{
1213 		osl_closeSocket((*m_pSockRef)());
1214 	}
1215 
1216 	m_pSockRef= 0;
1217 }
1218 
1219 /*****************************************************************************/
1220 // listen
1221 /*****************************************************************************/
listen(sal_Int32 MaxPendingConnections)1222 sal_Bool OAcceptorSocket::listen(sal_Int32 MaxPendingConnections)
1223 {
1224 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1225 
1226     if ( m_pSockRef && (*m_pSockRef)() )
1227     {
1228         return osl_listenOnSocket((*m_pSockRef)(), MaxPendingConnections);
1229     }
1230 
1231     return sal_False;
1232 }
1233 
1234 
1235 /*****************************************************************************/
1236 // acceptConnection
1237 /*****************************************************************************/
acceptConnection(OStreamSocket & connection)1238 OSocket::TResult OAcceptorSocket::acceptConnection(OStreamSocket& connection)
1239 {
1240 	if (m_pRecvTimeout && ! isRecvReady(m_pRecvTimeout))
1241 		return TResult_TimedOut;
1242 
1243 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1244     OStreamSocket aSocket;
1245 
1246     if ( m_pSockRef && (*m_pSockRef)() )
1247     {
1248         aSocket = osl_acceptConnectionOnSocket((*m_pSockRef)(), 0);
1249     }
1250 
1251 	if( aSocket.isValid() )
1252     {
1253         connection = aSocket;
1254 		return TResult_Ok;
1255     }
1256 	else
1257     {
1258 		return TResult_Error;
1259     }
1260 
1261 }
1262 
1263 /*****************************************************************************/
1264 // acceptConnection
1265 /*****************************************************************************/
acceptConnection(OStreamSocket & connection,OSocketAddr & sa)1266 OSocket::TResult OAcceptorSocket::acceptConnection(OStreamSocket& connection,
1267 												   OSocketAddr& sa)
1268 {
1269 	oslSocketAddr PeerAddr = 0;
1270 	oslSocket     Socket = 0;
1271 
1272 	if (m_pRecvTimeout && ! isRecvReady(m_pRecvTimeout))
1273 		return TResult_TimedOut;
1274 
1275 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1276 
1277     if ( m_pSockRef && (*m_pSockRef)() )
1278     {
1279         Socket= osl_acceptConnectionOnSocket((*m_pSockRef)(), &PeerAddr);
1280     }
1281 
1282 	if (Socket)
1283 	{
1284 		sa= PeerAddr;
1285 		connection= Socket;
1286 		return TResult_Ok;
1287 	}
1288 	else
1289 	{
1290 		return TResult_Error;
1291 	}
1292 }
1293 
1294 
1295 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OStreamSocket, vos),
1296 						VOS_NAMESPACE(OStreamSocket, vos),
1297 						VOS_NAMESPACE(OSocket, vos), 0);
1298 
1299 
1300 
1301 /*****************************************************************************/
1302 // OStreamSocket
1303 /*****************************************************************************/
OStreamSocket()1304 OStreamSocket::OStreamSocket()
1305 {
1306 }
1307 
1308 /*****************************************************************************/
1309 // OStreamSocket
1310 /*****************************************************************************/
OStreamSocket(TAddrFamily Family,TProtocol Protocol,TSocketType Type)1311 OStreamSocket::OStreamSocket(TAddrFamily Family,
1312 							 TProtocol   Protocol,
1313 							 TSocketType Type) :
1314 	OSocket(Type, Family, Protocol)
1315 {
1316 }
1317 
1318 
1319 /*****************************************************************************/
1320 // OStreamSocket
1321 /*****************************************************************************/
OStreamSocket(oslSocket Socket)1322 OStreamSocket::OStreamSocket(oslSocket Socket) :
1323 	OSocket(Socket)
1324 {
1325 }
1326 
1327 /*****************************************************************************/
1328 // OStreamSocket
1329 // copy constructor
1330 /*****************************************************************************/
OStreamSocket(const OStreamSocket & sock)1331 OStreamSocket::OStreamSocket(const OStreamSocket& sock) :
1332 	OSocket(sock), IStream()
1333 {
1334 }
1335 
1336 /*****************************************************************************/
1337 // OStreamSocket
1338 // copy constructor
1339 /*****************************************************************************/
OStreamSocket(const OSocket & sock)1340 OStreamSocket::OStreamSocket(const OSocket& sock) :
1341 	OSocket(sock)
1342 {
1343 }
1344 
1345 /*****************************************************************************/
1346 // ~OStreamSocket
1347 /*****************************************************************************/
~OStreamSocket()1348 OStreamSocket::~OStreamSocket()
1349 {
1350 }
1351 
1352 /*****************************************************************************/
1353 // close
1354 /*****************************************************************************/
close()1355 void OStreamSocket::close()
1356 {
1357 	if (m_pSockRef && (*m_pSockRef)() && (m_pSockRef->release() == 0))
1358 	{
1359 		shutdown();
1360 		osl_releaseSocket((*m_pSockRef)());
1361 		delete m_pSockRef;
1362 	}
1363 
1364 	m_pSockRef= 0;
1365 }
1366 
1367 
1368 /*****************************************************************************/
1369 // operator=(oslSocket)
1370 /*****************************************************************************/
operator =(oslSocket Socket)1371 OStreamSocket& OStreamSocket::operator=(oslSocket Socket)
1372 {
1373 	OSocket::operator=(Socket);
1374 
1375 	return *this;
1376 }
1377 
1378 /*****************************************************************************/
1379 // operator=
1380 /*****************************************************************************/
operator =(const OSocket & sock)1381 OStreamSocket& OStreamSocket::operator= (const OSocket& sock)
1382 {
1383 	OSocket::operator=(sock);
1384 
1385 	return *this;
1386 }
1387 
1388 /*****************************************************************************/
1389 // operator=
1390 /*****************************************************************************/
operator =(const OStreamSocket & sock)1391 OStreamSocket& OStreamSocket::operator= (const OStreamSocket& sock)
1392 {
1393 	OSocket::operator=(sock);
1394 
1395 	return *this;
1396 }
1397 
1398 /*****************************************************************************/
1399 // read
1400 /*****************************************************************************/
read(void * pBuffer,sal_uInt32 n) const1401 sal_Int32 OStreamSocket::read(void* pBuffer, sal_uInt32 n) const
1402 {
1403 	sal_uInt8 *Ptr = (sal_uInt8  *)pBuffer;
1404 
1405 	if (m_pRecvTimeout && ! isRecvReady(m_pRecvTimeout))
1406 		return 0;
1407 
1408 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1409 
1410     if ( ! ( m_pSockRef && (*m_pSockRef)() ) )
1411     {
1412         return -1;
1413     }
1414 
1415 	/* loop until all desired bytes were read or an error occured */
1416 	sal_uInt32 BytesRead= 0;
1417 	sal_uInt32 BytesToRead= n;
1418 	while (BytesToRead > 0)
1419 	{
1420 		sal_Int32 RetVal;
1421 		RetVal= osl_receiveSocket((*m_pSockRef)(),
1422 								   Ptr,
1423 								   BytesToRead,
1424 								   osl_Socket_MsgNormal);
1425 
1426 		/* error occured? */
1427 		if(RetVal <= 0)
1428 		{
1429 			break;
1430 		}
1431 
1432 		BytesToRead -= RetVal;
1433 		BytesRead += RetVal;
1434 		Ptr += RetVal;
1435 
1436 		/* wait for next available data or timeout */
1437 		if ( BytesToRead > 0 && m_pRecvTimeout && ! isRecvReady(m_pRecvTimeout))
1438 			break;
1439 
1440 	}
1441 
1442 	return BytesRead;
1443 }
1444 
1445 /*****************************************************************************/
1446 // write
1447 /*****************************************************************************/
write(const void * pBuffer,sal_uInt32 n)1448 sal_Int32 OStreamSocket::write(const void* pBuffer, sal_uInt32 n)
1449 {
1450 	sal_uInt8 *Ptr = (sal_uInt8 *)pBuffer;
1451 
1452 	if (m_pSendTimeout && ! isSendReady(m_pSendTimeout))
1453 		return 0;
1454 
1455 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1456 
1457     if ( ! ( m_pSockRef && (*m_pSockRef)() ) )
1458     {
1459         return -1;
1460     }
1461 
1462 	/* loop until all desired bytes were send or an error occured */
1463 	sal_uInt32 BytesSend= 0;
1464 	sal_uInt32 BytesToSend= n;
1465 	while (BytesToSend > 0)
1466 	{
1467 		sal_Int32 RetVal;
1468 
1469 		RetVal= osl_sendSocket((*m_pSockRef)(),
1470 								Ptr,
1471 								BytesToSend,
1472 								osl_Socket_MsgNormal);
1473 
1474 		/* error occured? */
1475 		if(RetVal <= 0)
1476 		{
1477 			break;
1478 		}
1479 
1480 		BytesToSend -= RetVal;
1481 		BytesSend += RetVal;
1482 		Ptr += RetVal;
1483 
1484 		/* wait till new data is available or timeout occures */
1485 		if ( BytesToSend > 0 && m_pSendTimeout && ! isSendReady(m_pSendTimeout))
1486 			break;
1487 	}
1488 
1489 	return BytesSend;
1490 }
1491 
isEof() const1492 sal_Bool OStreamSocket::isEof() const
1493 {
1494 	return isValid();
1495 	// BHO not enough
1496 }
1497 
1498 /*****************************************************************************/
1499 // recv
1500 /*****************************************************************************/
recv(void * pBuffer,sal_uInt32 BytesToRead,TSocketMsgFlag Flag)1501 sal_Int32 OStreamSocket::recv(void* pBuffer,
1502 						    sal_uInt32 BytesToRead,
1503 						    TSocketMsgFlag Flag)
1504 {
1505 	if (m_pRecvTimeout && ! isRecvReady(m_pRecvTimeout))
1506 		return 0;
1507 
1508 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1509 
1510     if ( ! ( m_pSockRef && (*m_pSockRef)() ) )
1511     {
1512         return -1;
1513     }
1514 
1515 	return osl_receiveSocket((*m_pSockRef)(),
1516 							 pBuffer,
1517 							 BytesToRead,
1518 							 (oslSocketMsgFlag)Flag);
1519 }
1520 
1521 /*****************************************************************************/
1522 // send
1523 /*****************************************************************************/
send(const void * pBuffer,sal_uInt32 BytesToSend,TSocketMsgFlag Flag)1524 sal_Int32 OStreamSocket::send(const void* pBuffer,
1525 						    sal_uInt32 BytesToSend,
1526 						    TSocketMsgFlag Flag)
1527 {
1528 	if (m_pSendTimeout && ! isSendReady(m_pSendTimeout))
1529 		return 0;
1530 
1531 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1532 
1533     if ( ! ( m_pSockRef && (*m_pSockRef)() ) )
1534     {
1535         return -1;
1536     }
1537 
1538 	return osl_sendSocket((*m_pSockRef)(),
1539 						  pBuffer,
1540 						  BytesToSend,
1541 						  (oslSocketMsgFlag)Flag);
1542 }
1543 
1544 /*****************************************************************************/
1545 // shutdown
1546 /*****************************************************************************/
shutdown(TSocketDirection Direction)1547 sal_Bool OStreamSocket::shutdown(TSocketDirection Direction)
1548 {
1549 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1550 
1551     if ( m_pSockRef && (*m_pSockRef)() )
1552     {
1553         return osl_shutdownSocket((*m_pSockRef)(), (oslSocketDirection)Direction);
1554     }
1555 
1556     return sal_False;
1557 }
1558 
1559 
1560 
1561 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OConnectorSocket, vos),
1562 						VOS_NAMESPACE(OConnectorSocket, vos),
1563 						VOS_NAMESPACE(OStreamSocket, vos), 0);
1564 
1565 
1566 
1567 /*****************************************************************************/
1568 // OConnectorSocket
1569 /*****************************************************************************/
OConnectorSocket(TAddrFamily Family,TProtocol Protocol,TSocketType Type)1570 OConnectorSocket::OConnectorSocket(TAddrFamily Family,
1571 								   TProtocol   Protocol,
1572 								   TSocketType Type) :
1573 	OStreamSocket(Family, Protocol, Type)
1574 {
1575 }
1576 
1577 /*****************************************************************************/
1578 // OConnectorSocket
1579 /*****************************************************************************/
OConnectorSocket(const OConnectorSocket & sock)1580 OConnectorSocket::OConnectorSocket(const OConnectorSocket& sock) :
1581 	OStreamSocket(sock)
1582 {
1583 }
1584 
1585 /*****************************************************************************/
1586 // ~OConnectorSocket
1587 /*****************************************************************************/
~OConnectorSocket()1588 OConnectorSocket::~OConnectorSocket()
1589 {
1590 }
1591 
1592 /*****************************************************************************/
1593 // connect
1594 /*****************************************************************************/
connect(const OSocketAddr & Addr,const TimeValue * pTimeout)1595 OSocket::TResult OConnectorSocket::connect(const OSocketAddr& Addr,
1596 										   const TimeValue* pTimeout)
1597 {
1598 
1599 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1600 
1601     if ( m_pSockRef && (*m_pSockRef)() )
1602     {
1603         return (TResult)osl_connectSocketTo((*m_pSockRef)(),
1604                                             (oslSocketAddr)Addr,
1605                                             pTimeout);
1606     }
1607 
1608     return TResult_Error;
1609 }
1610 
1611 
1612 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(ODatagramSocket, vos),
1613 						VOS_NAMESPACE(ODatagramSocket, vos),
1614 						VOS_NAMESPACE(OSocket, vos), 0);
1615 
1616 
1617 /*****************************************************************************/
1618 // ODatagramSocket
1619 /*****************************************************************************/
ODatagramSocket(TAddrFamily Family,TProtocol Protocol,TSocketType Type)1620 ODatagramSocket::ODatagramSocket(TAddrFamily Family,
1621 								 TProtocol   Protocol,
1622 								 TSocketType Type) :
1623 	OSocket(Type, Family, Protocol)
1624 {
1625 }
1626 
1627 /*****************************************************************************/
1628 // ODatagramSocket
1629 /*****************************************************************************/
ODatagramSocket(const ODatagramSocket & sock)1630 ODatagramSocket::ODatagramSocket(const ODatagramSocket& sock) :
1631 	OSocket(sock)
1632 {
1633 }
1634 
1635 /*****************************************************************************/
1636 // ~ODatagramSocket
1637 /*****************************************************************************/
~ODatagramSocket()1638 ODatagramSocket::~ODatagramSocket()
1639 {
1640 }
1641 
1642 
1643 /*****************************************************************************/
1644 // recvFrom
1645 /*****************************************************************************/
recvFrom(void * pBuffer,sal_uInt32 BufferSize,OSocketAddr * pSenderAddr,TSocketMsgFlag Flag)1646 sal_Int32 ODatagramSocket::recvFrom(void* pBuffer,
1647 					              sal_uInt32 BufferSize,
1648 					  			  OSocketAddr* pSenderAddr,
1649 					  			  TSocketMsgFlag Flag)
1650 {
1651 
1652 	if (m_pRecvTimeout && ! isRecvReady(m_pRecvTimeout))
1653 		return 0;
1654 
1655 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1656 
1657     if ( ! ( m_pSockRef && (*m_pSockRef)() ) )
1658     {
1659         return -1;
1660     }
1661 
1662 	sal_Int32 BytesRead;
1663 
1664 	if(pSenderAddr)
1665 	{
1666 		// we are interested in the senders address
1667 		oslSocketAddr SenderAddr= osl_createEmptySocketAddr(osl_Socket_FamilyInet);
1668 
1669 		BytesRead=
1670 			osl_receiveFromSocket((*m_pSockRef)(),
1671 								   SenderAddr,
1672 								   pBuffer,
1673 								   BufferSize,
1674 								   (oslSocketMsgFlag)Flag);
1675 
1676 		*pSenderAddr= SenderAddr;
1677 	}
1678 	else
1679 	{
1680 		// we don't want to know the senders address
1681 
1682 		BytesRead=
1683 			osl_receiveFromSocket((*m_pSockRef)(),
1684 								   0,
1685 								   pBuffer,
1686 								   BufferSize,
1687 								   (oslSocketMsgFlag)Flag);
1688 	}
1689 
1690 	return BytesRead;
1691 }
1692 
1693 
1694 /*****************************************************************************/
1695 // sendTo
1696 /*****************************************************************************/
sendTo(const OSocketAddr & ReceiverAddr,const void * pBuffer,sal_uInt32 BufferSize,TSocketMsgFlag Flag)1697 sal_Int32 ODatagramSocket::sendTo(const OSocketAddr& ReceiverAddr,
1698 							    const void* pBuffer,
1699 							    sal_uInt32 BufferSize,
1700 							    TSocketMsgFlag Flag)
1701 {
1702 	if (m_pSendTimeout && ! isSendReady(m_pSendTimeout))
1703 		return 0;
1704 
1705 	VOS_ASSERT(m_pSockRef && (*m_pSockRef)());
1706 
1707     if ( ( m_pSockRef && (*m_pSockRef)() ) )
1708     {
1709 
1710         return osl_sendToSocket((*m_pSockRef)(),
1711                                 (oslSocketAddr)ReceiverAddr,
1712                                 pBuffer,
1713                                 BufferSize,
1714                                 (oslSocketMsgFlag)Flag);
1715     }
1716 
1717     return -1;
1718 }
1719 
1720