Bug Summary

File:sal/osl/unx/socket.c
Location:line 611, column 30
Description:Access to field 'sa_family' results in a dereference of a null pointer (loaded from variable 'pAddr2')

Annotated Source Code

1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*************************************************************************
3 *
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 *
8 * OpenOffice.org - a multi-platform office productivity suite
9 *
10 * This file is part of OpenOffice.org.
11 *
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
15 *
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
21 *
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
26 *
27 ************************************************************************/
28
29#include "system.h"
30
31#include <osl/socket.h>
32#include <osl/diagnose.h>
33#include <osl/mutex.h>
34#include <osl/signal.h>
35
36#include <rtl/alloc.h>
37
38#include <ctype.h>
39#include <sal/types.h>
40
41#include "sockimpl.h"
42
43/* defines for poll */
44#ifdef HAVE_POLL_H
45#undef HAVE_POLL_H
46#endif
47
48#if defined(LINUX1) || defined(NETBSD) || defined ( FREEBSD ) || \
49 defined (MACOSX) || defined (OPENBSD) || defined(DRAGONFLY)
50#include <sys/poll.h>
51#define HAVE_POLL_H
52#endif /* HAVE_POLL_H */
53
54#if defined(SOLARIS)
55#include <poll.h>
56#define HAVE_POLL_H
57#endif /* SOLARIS */
58
59#ifndef HAVE_POLL_H
60#define POLLIN0x001 0x0001
61#define POLLOUT0x004 0x0002
62#define POLLPRI0x002 0x0004
63#endif /* HAVE_POLL_H */
64
65
66/* defines for shutdown */
67#define SD_RECEIVE0 0
68#define SD_SEND1 1
69#define SD_BOTH2 2
70
71
72/*
73 oslSocketAddr is a pointer to a Berkeley struct sockaddr.
74 I refrained from using sockaddr_in because of possible further
75 extensions of this socket-interface (IP-NG?).
76 The intention was to hide all Berkeley data-structures from
77 direct access past the osl-interface.
78
79 The current implementation is internet (IP) centered. All
80 the constructor-functions (osl_create...) take parameters
81 that will probably make sense only in the IP-environment
82 (e.g. because of using the dotted-address-format).
83
84 If the interface will be extended to host other protocol-
85 families, I expect no externally visible changes in the
86 existing functions. You'll probably need only new
87 constructor-functions who take the different address
88 formats into consideration (maybe a long dotted address
89 or whatever).
90*/
91
92/* _Note_ that I rely on the fact that oslSocketAddr and struct sockaddr */
93/* are the same! I don't like it very much but see no other easy way to */
94/* conceal the struct sockaddr from the eyes of the user. */
95
96
97#define OSL_INVALID_SOCKET-1 -1
98#define OSL_SOCKET_ERROR-1 -1
99
100
101/* Buffer size for gethostbyname */
102#define MAX_HOSTBUFFER_SIZE2048 2048
103
104/*****************************************************************************/
105/* enum oslAddrFamily */
106/*****************************************************************************/
107
108/* map */
109static unsigned long FamilyMap[]= {
110 AF_INET2, /* osl_Socket_FamilyInet */
111 AF_IPX4, /* osl_Socket_FamilyIpx */
112 0 /* osl_Socket_FamilyInvalid */
113};
114
115/* reverse map */
116static oslAddrFamily osl_AddrFamilyFromNative(sal_uInt32 nativeType)
117{
118 oslAddrFamily i= (oslAddrFamily)0;
119
120 while(i != osl_Socket_FamilyInvalid)
121 {
122 if(FamilyMap[i] == nativeType)
123 return i;
124 i = (oslAddrFamily) ( i + 1 );
125 }
126
127 return i;
128}
129
130/* macros */
131#define FAMILY_FROM_NATIVE(y)osl_AddrFamilyFromNative(y) osl_AddrFamilyFromNative(y)
132#define FAMILY_TO_NATIVE(x)(short)FamilyMap[x] (short)FamilyMap[x]
133
134/*****************************************************************************/
135/* enum oslProtocol */
136/*****************************************************************************/
137
138/* map */
139static sal_uInt32 ProtocolMap[]= {
140 0, /* osl_Socket_ProtocolIp */
141 NSPROTO_IPX1000, /* osl_Socket_ProtocolIpx */
142 NSPROTO_SPX1256, /* osl_Socket_ProtocolSpx */
143 NSPROTO_SPXII1257, /* osl_Socket_ProtocolSpxII */
144 0 /* osl_Socket_ProtocolInvalid */
145};
146
147/* reverse map */
148/* mfe: NOT USED
149static oslProtocol osl_ProtocolFromNative(sal_uInt32 nativeType)
150{
151 oslProtocol i= (oslProtocol)0;
152
153 while(i != osl_Socket_ProtocolInvalid)
154 {
155 if(ProtocolMap[i] == nativeType)
156 return i;
157 i = (oslProtocol) ( i + 1);
158 }
159
160 return i;
161}
162*/
163
164/* macros */
165#define PROTOCOL_FROM_NATIVE(y)osl_ProtocolFromNative(y) osl_ProtocolFromNative(y)
166#define PROTOCOL_TO_NATIVE(x)ProtocolMap[x] ProtocolMap[x]
167
168
169/*****************************************************************************/
170/* enum oslSocketType */
171/*****************************************************************************/
172
173/* map */
174static sal_uInt32 TypeMap[]= {
175 SOCK_STREAMSOCK_STREAM, /* osl_Socket_TypeStream */
176 SOCK_DGRAMSOCK_DGRAM, /* osl_Socket_TypeDgram */
177 SOCK_RAWSOCK_RAW, /* osl_Socket_TypeRaw */
178 SOCK_RDMSOCK_RDM, /* osl_Socket_TypeRdm */
179 SOCK_SEQPACKETSOCK_SEQPACKET, /* osl_Socket_TypeSeqPacket */
180 0 /* osl_Socket_TypeInvalid */
181};
182
183/* reverse map */
184static oslSocketType osl_SocketTypeFromNative(sal_uInt32 nativeType)
185{
186 oslSocketType i= (oslSocketType)0;
187
188 while(i != osl_Socket_TypeInvalid)
189 {
190 if(TypeMap[i] == nativeType)
191 return i;
192 i = (oslSocketType)(i + 1);
193 }
194
195 return i;
196}
197
198/* macros */
199#define TYPE_TO_NATIVE(x)TypeMap[x] TypeMap[x]
200#define TYPE_FROM_NATIVE(y)osl_SocketTypeFromNative(y) osl_SocketTypeFromNative(y)
201
202
203/*****************************************************************************/
204/* enum oslSocketOption */
205/*****************************************************************************/
206
207/* map */
208static sal_uInt32 OptionMap[]= {
209 SO_DEBUG1, /* osl_Socket_OptionDebug */
210 SO_ACCEPTCONN30, /* osl_Socket_OptionAcceptConn */
211 SO_REUSEADDR2, /* osl_Socket_OptionReuseAddr */
212 SO_KEEPALIVE9, /* osl_Socket_OptionKeepAlive */
213 SO_DONTROUTE5, /* osl_Socket_OptionDontRoute */
214 SO_BROADCAST6, /* osl_Socket_OptionBroadcast */
215 SO_USELOOPBACK0, /* osl_Socket_OptionUseLoopback */
216 SO_LINGER13, /* osl_Socket_OptionLinger */
217 SO_OOBINLINE10, /* osl_Socket_OptionOOBinLine */
218 SO_SNDBUF7, /* osl_Socket_OptionSndBuf */
219 SO_RCVBUF8, /* osl_Socket_OptionRcvBuf */
220 SO_SNDLOWAT19, /* osl_Socket_OptionSndLowat */
221 SO_RCVLOWAT18, /* osl_Socket_OptionRcvLowat */
222 SO_SNDTIMEO21, /* osl_Socket_OptionSndTimeo */
223 SO_RCVTIMEO20, /* osl_Socket_OptionRcvTimeo */
224 SO_ERROR4, /* osl_Socket_OptionError */
225 SO_TYPE3, /* osl_Socket_OptionType */
226 TCP_NODELAY1, /* osl_Socket_OptionTcpNoDelay */
227 0 /* osl_Socket_OptionInvalid */
228};
229
230/* reverse map */
231/* mfe: NOT USED
232static oslSocketOption osl_SocketOptionFromNative(sal_uInt32 nativeType)
233{
234 oslSocketOption i= (oslSocketOption)0;
235
236 while(i != osl_Socket_OptionInvalid)
237 {
238 if(OptionMap[i] == nativeType)
239 return i;
240 i = (oslSocketOption) ( i + 1 );
241 }
242
243 return i;
244}
245*/
246/* macros */
247#define OPTION_TO_NATIVE(x)OptionMap[x] OptionMap[x]
248#define OPTION_FROM_NATIVE(y)osl_SocketOptionFromNative(y) osl_SocketOptionFromNative(y)
249
250
251/*****************************************************************************/
252/* enum oslSocketOptionLevel */
253/*****************************************************************************/
254
255static sal_uInt32 OptionLevelMap[]= {
256 SOL_SOCKET1, /* osl_Socket_LevelSocket */
257 IPPROTO_TCPIPPROTO_TCP, /* osl_Socket_LevelTcp */
258 0 /* osl_Socket_LevelInvalid */
259};
260
261/* reverse map */
262/* mfe: NOT USED
263static oslSocketOptionLevel osl_SocketOptionLevelFromNative(sal_uInt32 nativeType)
264{
265 oslSocketOptionLevel i= (oslSocketOptionLevel)0;
266
267 while(i != osl_Socket_LevelInvalid)
268 {
269 if(OptionLevelMap[i] == nativeType)
270 return i;
271 i = (oslSocketOptionLevel) ( i + 1 );
272 }
273
274 return i;
275}
276*/
277/* macros */
278#define OPTION_LEVEL_TO_NATIVE(x)OptionLevelMap[x] OptionLevelMap[x]
279#define OPTION_LEVEL_FROM_NATIVE(y)osl_SocketOptionLevelFromNative(y) osl_SocketOptionLevelFromNative(y)
280
281/*****************************************************************************/
282/* enum oslSocketMsgFlag */
283/*****************************************************************************/
284
285static sal_uInt32 SocketMsgFlagMap[]= {
286 0, /* osl_Socket_MsgNormal */
287 MSG_OOBMSG_OOB, /* osl_Socket_MsgOOB */
288 MSG_PEEKMSG_PEEK, /* osl_Socket_MsgPeek */
289 MSG_DONTROUTEMSG_DONTROUTE, /* osl_Socket_MsgDontRoute */
290 MSG_MAXIOVLEN0, /* osl_Socket_MsgMaxIOVLen */
291 0 /* osl_Socket_MsgInvalid */
292};
293
294/* reverse map */
295/* mfe: NOT USED
296static oslSocketMsgFlag osl_SocketMsgFlagFromNative(sal_uInt32 nativeType)
297{
298 oslSocketMsgFlag i= (oslSocketMsgFlag)0;
299
300 while(i != osl_Socket_MsgInvalid)
301 {
302 if(SocketMsgFlagMap[i] == nativeType)
303 return i;
304 i = (oslSocketMsgFlag) ( i + 1 );
305 }
306
307 return i;
308}
309*/
310
311/* macros */
312#define MSG_FLAG_TO_NATIVE(x)SocketMsgFlagMap[x] SocketMsgFlagMap[x]
313#define MSG_FLAG_FROM_NATIVE(y)osl_SocketMsgFlagFromNative(y) osl_SocketMsgFlagFromNative(y)
314
315
316/*****************************************************************************/
317/* enum oslSocketDirection */
318/*****************************************************************************/
319
320static sal_uInt32 SocketDirection[]= {
321 SD_RECEIVE0, /* osl_Socket_DirRead */
322 SD_SEND1, /* osl_Socket_DirWrite */
323 SD_BOTH2, /* osl_Socket_DirReadWrite */
324 0 /* osl_Socket_DirInvalid */
325};
326
327/* reverse map */
328/* mfe: NOT USED
329static oslSocketDirection osl_SocketDirectionFromNative(sal_uInt32 nativeType)
330{
331 oslSocketDirection i= (oslSocketDirection)0;
332
333 while(i != osl_Socket_DirInvalid)
334 {
335 if(SocketDirection[i] == nativeType)
336 return i;
337 i = (oslSocketDirection) ( i + 1 );
338 }
339
340 return i;
341}
342*/
343
344/* macros */
345#define DIRECTION_TO_NATIVE(x)SocketDirection[x] SocketDirection[x]
346#define DIRECTION_FROM_NATIVE(y)osl_SocketDirectionFromNative(y) osl_SocketDirectionFromNative(y)
347
348/*****************************************************************************/
349/* enum oslSocketError */
350/*****************************************************************************/
351
352static struct
353{
354 int errcode;
355 oslSocketError error;
356} SocketError[]= {
357 { 0, osl_Socket_E_None }, /* no error */
358 { ENOTSOCK88, osl_Socket_E_NotSocket }, /* Socket operation on non-socket */
359 { EDESTADDRREQ89, osl_Socket_E_DestAddrReq }, /* Destination address required */
360 { EMSGSIZE90, osl_Socket_E_MsgSize }, /* Message too long */
361 { EPROTOTYPE91, osl_Socket_E_Prototype }, /* Protocol wrong type for socket */
362 { ENOPROTOOPT92, osl_Socket_E_NoProtocol }, /* Protocol not available */
363 { EPROTONOSUPPORT93, osl_Socket_E_ProtocolNoSupport }, /* Protocol not supported */
364 { ESOCKTNOSUPPORT94, osl_Socket_E_TypeNoSupport }, /* Socket type not supported */
365 { EOPNOTSUPP95, osl_Socket_E_OpNotSupport }, /* Operation not supported on socket */
366 { EPFNOSUPPORT96, osl_Socket_E_PfNoSupport }, /* Protocol family not supported */
367 { EAFNOSUPPORT97, osl_Socket_E_AfNoSupport }, /* Address family not supported by */
368 /* protocol family */
369 { EADDRINUSE98, osl_Socket_E_AddrInUse }, /* Address already in use */
370 { EADDRNOTAVAIL99, osl_Socket_E_AddrNotAvail }, /* Can't assign requested address */
371 { ENETDOWN100, osl_Socket_E_NetDown }, /* Network is down */
372 { ENETUNREACH101, osl_Socket_E_NetUnreachable }, /* Network is unreachable */
373 { ENETRESET102, osl_Socket_E_NetReset }, /* Network dropped connection because */
374 /* of reset */
375 { ECONNABORTED103, osl_Socket_E_ConnAborted }, /* Software caused connection abort */
376 { ECONNRESET104, osl_Socket_E_ConnReset }, /* Connection reset by peer */
377 { ENOBUFS105, osl_Socket_E_NoBufferSpace }, /* No buffer space available */
378 { EISCONN106, osl_Socket_E_IsConnected }, /* Socket is already connected */
379 { ENOTCONN107, osl_Socket_E_NotConnected }, /* Socket is not connected */
380 { ESHUTDOWN108, osl_Socket_E_Shutdown }, /* Can't send after socket shutdown */
381 { ETOOMANYREFS109, osl_Socket_E_TooManyRefs }, /* Too many references: can't splice */
382 { ETIMEDOUT110, osl_Socket_E_TimedOut }, /* Connection timed out */
383 { ECONNREFUSED111, osl_Socket_E_ConnRefused }, /* Connection refused */
384 { EHOSTDOWN112, osl_Socket_E_HostDown }, /* Host is down */
385 { EHOSTUNREACH113, osl_Socket_E_HostUnreachable }, /* No route to host */
386 { EWOULDBLOCK11, osl_Socket_E_WouldBlock }, /* call would block on non-blocking socket */
387 { EALREADY114, osl_Socket_E_Already }, /* operation already in progress */
388 { EINPROGRESS115, osl_Socket_E_InProgress }, /* operation now in progress */
389 { EAGAIN11, osl_Socket_E_WouldBlock }, /* same as EWOULDBLOCK */
390 { -1, osl_Socket_E_InvalidError }
391};
392
393/* map */
394/* mfe: NOT USED
395static int osl_NativeFromSocketError(oslSocketError errorCode)
396{
397 int i = 0;
398
399 while ((SocketError[i].error != osl_Socket_E_InvalidError) &&
400 (SocketError[i].error != errorCode)) i++;
401
402 return SocketError[i].errcode;
403}
404*/
405
406/* reverse map */
407static oslSocketError osl_SocketErrorFromNative(int nativeType)
408{
409 int i = 0;
410
411 while ((SocketError[i].error != osl_Socket_E_InvalidError) &&
412 (SocketError[i].errcode != nativeType)) i++;
413
414 return SocketError[i].error;
415}
416
417/* macros */
418#define ERROR_TO_NATIVE(x)osl_NativeFromSocketError(x) osl_NativeFromSocketError(x)
419#define ERROR_FROM_NATIVE(y)osl_SocketErrorFromNative(y) osl_SocketErrorFromNative(y)
420
421/*****************************************************************************/
422/* local function prototypes */
423/*****************************************************************************/
424
425oslSocketAddr SAL_CALL osl_psz_createInetSocketAddr (
426 const sal_Char* pszDottedAddr, sal_Int32 Port);
427
428oslSocketAddr SAL_CALL osl_psz_createIpxSocketAddr (
429 const sal_Char NetNumber[4],
430 const sal_Char NodeNumber[6],
431 sal_uInt32 SocketNumber);
432
433oslHostAddr SAL_CALL osl_psz_createHostAddr (
434 const sal_Char *pszHostname, const oslSocketAddr Addr);
435
436oslHostAddr SAL_CALL osl_psz_createHostAddrByName (
437 const sal_Char *pszHostname);
438
439const sal_Char* SAL_CALL osl_psz_getHostnameOfHostAddr (
440 const oslHostAddr Addr);
441
442oslSocketResult SAL_CALL osl_psz_getLocalHostname (
443 sal_Char *pBuffer, sal_uInt32 nBufLen);
444
445oslSocketAddr SAL_CALL osl_psz_resolveHostname (
446 const sal_Char* pszHostname);
447
448sal_Int32 SAL_CALL osl_psz_getServicePort (
449 const sal_Char* pszServicename, const sal_Char* pszProtocol);
450
451oslSocketResult SAL_CALL osl_psz_getHostnameOfSocketAddr (
452 oslSocketAddr Addr, sal_Char *pBuffer, sal_uInt32 BufferSize);
453
454oslSocketResult SAL_CALL osl_psz_getDottedInetAddrOfSocketAddr (
455 oslSocketAddr Addr, sal_Char *pBuffer, sal_uInt32 BufferSize);
456
457void SAL_CALL osl_psz_getLastSocketErrorDescription (
458 oslSocket Socket, sal_Char* pBuffer, sal_uInt32 BufferSize);
459
460/*****************************************************************************/
461/* osl_create/destroy-SocketImpl */
462/*****************************************************************************/
463
464#if OSL_DEBUG_LEVEL1 > 1
465static sal_uInt32 g_nSocketImpl = 0;
466static sal_uInt32 g_nSocketAddr = 0;
467
468/* sorry, must be implemented otherwise */
469
470#endif /* OSL_DEBUG_LEVEL */
471
472
473oslSocket __osl_createSocketImpl(int Socket)
474{
475 oslSocket pSocket;
476
477 pSocket = (oslSocket)calloc(1, sizeof(struct oslSocketImpl));
478
479 pSocket->m_Socket = Socket;
480 pSocket->m_nLastError = 0;
481 pSocket->m_CloseCallback = 0;
482 pSocket->m_CallbackArg = 0;
483 pSocket->m_nRefCount = 1;
484
485#if defined(LINUX1)
486 pSocket->m_bIsAccepting = sal_False((sal_Bool)0);
487#endif
488
489#if OSL_DEBUG_LEVEL1 > 1
490 g_nSocketImpl ++;
491#endif
492 return pSocket;
493}
494
495void __osl_destroySocketImpl(oslSocket Socket)
496{
497 if ( Socket != NULL((void*)0))
498 free((struct oslSocketImpl *) Socket);
499#if OSL_DEBUG_LEVEL1 > 1
500 g_nSocketImpl --;
501#endif
502}
503
504static oslSocketAddr __osl_createSocketAddr( )
505{
506 oslSocketAddr pAddr = (oslSocketAddr) rtl_allocateZeroMemory( sizeof( struct oslSocketAddrImpl ));
507#if OSL_DEBUG_LEVEL1 > 1
508 g_nSocketAddr ++;
509#endif
510 return pAddr;
511}
512
513static oslSocketAddr __osl_createSocketAddrWithFamily(
514 oslAddrFamily family, sal_Int32 port, sal_uInt32 nAddr )
515{
516 oslSocketAddr pAddr;
517
518 OSL_ASSERT( family == osl_Socket_FamilyInet )do { if (((sal_Bool)1) && (!(family == osl_Socket_FamilyInet
))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"
), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c" ":" "518"
": "), "OSL_ASSERT: %s", "family == osl_Socket_FamilyInet");
} } while (((sal_Bool)0))
;
519
520 pAddr = __osl_createSocketAddr();
521 switch( family )
522 {
523 case osl_Socket_FamilyInet:
524 {
525 struct sockaddr_in* pInetAddr= (struct sockaddr_in*)&(pAddr->m_sockaddr);
526
527 pInetAddr->sin_family = FAMILY_TO_NATIVE(osl_Socket_FamilyInet)(short)FamilyMap[osl_Socket_FamilyInet];
528 pInetAddr->sin_addr.s_addr = nAddr;
529 pInetAddr->sin_port = (sal_uInt16)(port&0xffff);
530 break;
531 }
532 default:
533 pAddr->m_sockaddr.sa_family = FAMILY_TO_NATIVE(family)(short)FamilyMap[family];
534 }
535 return pAddr;
536}
537
538static oslSocketAddr __osl_createSocketAddrFromSystem( struct sockaddr *pSystemSockAddr )
539{
540 oslSocketAddr pAddr = __osl_createSocketAddr();
541 memcpy( &(pAddr->m_sockaddr), pSystemSockAddr, sizeof( struct sockaddr ) );
542 return pAddr;
543}
544
545static void __osl_destroySocketAddr( oslSocketAddr addr )
546{
547#if OSL_DEBUG_LEVEL1 > 1
548 g_nSocketAddr --;
549#endif
550 rtl_freeMemory( addr );
551}
552
553/*****************************************************************************/
554/* osl_createEmptySocketAddr */
555/*****************************************************************************/
556oslSocketAddr SAL_CALL osl_createEmptySocketAddr(oslAddrFamily Family)
557{
558 oslSocketAddr pAddr = 0;
559
560 /* is it an internet-Addr? */
561 if (Family == osl_Socket_FamilyInet)
562 {
563 pAddr = __osl_createSocketAddrWithFamily(Family, 0 , htonl(INADDR_ANY((in_addr_t) 0x00000000)) );
564 }
565 else
566 {
567 pAddr = __osl_createSocketAddrWithFamily( Family , 0 , 0 );
568 }
569
570 return pAddr;
571}
572
573/*****************************************************************************/
574/* osl_copySocketAddr */
575/*****************************************************************************/
576oslSocketAddr SAL_CALL osl_copySocketAddr(oslSocketAddr Addr)
577{
578 oslSocketAddr pCopy = 0;
579 if (Addr)
580 {
581 pCopy = __osl_createSocketAddr();
582
583 if (pCopy)
584 memcpy(&(pCopy->m_sockaddr),&(Addr->m_sockaddr), sizeof(struct sockaddr));
585 }
586 return pCopy;
587}
588
589/*****************************************************************************/
590/* osl_isEqualSocketAddr */
591/*****************************************************************************/
592sal_Bool SAL_CALL osl_isEqualSocketAddr (
593 oslSocketAddr Addr1,
594 oslSocketAddr Addr2)
595{
596 struct sockaddr* pAddr1 = NULL((void*)0);
597 struct sockaddr* pAddr2 = NULL((void*)0);
598
599 OSL_ASSERT(Addr1)do { if (((sal_Bool)1) && (!(Addr1))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "599" ": "), "OSL_ASSERT: %s", "Addr1"); } } while (((sal_Bool
)0))
;
600 OSL_ASSERT(Addr2)do { if (((sal_Bool)1) && (!(Addr2))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "600" ": "), "OSL_ASSERT: %s", "Addr2"); } } while (((sal_Bool
)0))
;
601 pAddr1 = &(Addr1->m_sockaddr);
602 pAddr2 = &(Addr2->m_sockaddr);
603
604 OSL_ASSERT(pAddr1)do { if (((sal_Bool)1) && (!(pAddr1))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "604" ": "), "OSL_ASSERT: %s", "pAddr1"); } } while (((sal_Bool
)0))
;
605 OSL_ASSERT(pAddr2)do { if (((sal_Bool)1) && (!(pAddr2))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "605" ": "), "OSL_ASSERT: %s", "pAddr2"); } } while (((sal_Bool
)0))
;
606 if (pAddr1 == pAddr2)
1
Taking false branch
607 {
608 return (sal_True((sal_Bool)1));
609 }
610
611 if (pAddr1->sa_family == pAddr2->sa_family)
2
Access to field 'sa_family' results in a dereference of a null pointer (loaded from variable 'pAddr2')
612 {
613 switch (pAddr1->sa_family)
614 {
615 case AF_INET2:
616 {
617 struct sockaddr_in* pInetAddr1= (struct sockaddr_in*)pAddr1;
618 struct sockaddr_in* pInetAddr2= (struct sockaddr_in*)pAddr2;
619
620 if ((pInetAddr1->sin_family == pInetAddr2->sin_family) &&
621 (pInetAddr1->sin_addr.s_addr == pInetAddr2->sin_addr.s_addr) &&
622 (pInetAddr1->sin_port == pInetAddr2->sin_port))
623 return (sal_True((sal_Bool)1));
624 }
625
626 default:
627 {
628 return (memcmp(pAddr1, pAddr2, sizeof(struct sockaddr)) == 0);
629 }
630 }
631 }
632
633 return (sal_False((sal_Bool)0));
634}
635
636/*****************************************************************************/
637/* osl_createInetBroadcastAddr */
638/*****************************************************************************/
639oslSocketAddr SAL_CALL osl_createInetBroadcastAddr (
640 rtl_uString *strDottedAddr,
641 sal_Int32 Port)
642{
643 sal_uInt32 nAddr = OSL_INADDR_NONE0xffffffff;
644 oslSocketAddr pAddr;
645
646 if (strDottedAddr && strDottedAddr->length)
647 {
648 /* Dotted host address for limited broadcast */
649 rtl_String *pDottedAddr = NULL((void*)0);
650
651 rtl_uString2String (
652 &pDottedAddr, strDottedAddr->buffer, strDottedAddr->length,
653 RTL_TEXTENCODING_UTF8(((rtl_TextEncoding) 76)), OUSTRING_TO_OSTRING_CVTFLAGS(((sal_uInt32)0x0006) | ((sal_uInt32)0x0060) | ((sal_uInt32)0x0100
) | ((sal_uInt32)0x0400) | ((sal_uInt32)0x4000))
);
654
655 nAddr = inet_addr (pDottedAddr->buffer);
656 rtl_string_release (pDottedAddr);
657 }
658
659 if (nAddr != OSL_INADDR_NONE0xffffffff)
660 {
661 /* Limited broadcast */
662 nAddr = ntohl(nAddr);
663 if (IN_CLASSA(nAddr)((((in_addr_t)(nAddr)) & 0x80000000) == 0))
664 {
665 nAddr &= IN_CLASSA_NET0xff000000;
666 nAddr |= IN_CLASSA_HOST(0xffffffff & ~0xff000000);
667 }
668 else if (IN_CLASSB(nAddr)((((in_addr_t)(nAddr)) & 0xc0000000) == 0x80000000))
669 {
670 nAddr &= IN_CLASSB_NET0xffff0000;
671 nAddr |= IN_CLASSB_HOST(0xffffffff & ~0xffff0000);
672 }
673 else if (IN_CLASSC(nAddr)((((in_addr_t)(nAddr)) & 0xe0000000) == 0xc0000000))
674 {
675 nAddr &= IN_CLASSC_NET0xffffff00;
676 nAddr |= IN_CLASSC_HOST(0xffffffff & ~0xffffff00);
677 }
678 else
679 {
680 /* No broadcast in class D */
681 return ((oslSocketAddr)NULL((void*)0));
682 }
683 nAddr = htonl(nAddr);
684 }
685
686 pAddr = __osl_createSocketAddrWithFamily( osl_Socket_FamilyInet, htons(Port), nAddr );
687 return pAddr;
688}
689
690/*****************************************************************************/
691/* osl_createInetSocketAddr */
692/*****************************************************************************/
693oslSocketAddr SAL_CALL osl_createInetSocketAddr (
694 rtl_uString *ustrDottedAddr,
695 sal_Int32 Port)
696{
697 rtl_String* strDottedAddr=0;
698 oslSocketAddr Addr;
699 sal_Char* pszDottedAddr=0;
700
701 if ( ustrDottedAddr != 0 )
702 {
703 rtl_uString2String( &strDottedAddr,
704 rtl_uString_getStr(ustrDottedAddr),
705 rtl_uString_getLength(ustrDottedAddr),
706 RTL_TEXTENCODING_UTF8(((rtl_TextEncoding) 76)),
707 OUSTRING_TO_OSTRING_CVTFLAGS(((sal_uInt32)0x0006) | ((sal_uInt32)0x0060) | ((sal_uInt32)0x0100
) | ((sal_uInt32)0x0400) | ((sal_uInt32)0x4000))
);
708 pszDottedAddr = rtl_string_getStr(strDottedAddr);
709 }
710
711
712 Addr = osl_psz_createInetSocketAddr(pszDottedAddr, Port);
713
714 if ( strDottedAddr != 0 )
715 {
716 rtl_string_release(strDottedAddr);
717 }
718
719 return Addr;
720}
721
722oslSocketAddr SAL_CALL osl_psz_createInetSocketAddr (
723 const sal_Char* pszDottedAddr,
724 sal_Int32 Port)
725{
726 oslSocketAddr pAddr = 0;
727 sal_Int32 Addr = inet_addr(pszDottedAddr);
728 if(Addr != -1)
729 {
730 /* valid dotted addr */
731 pAddr = __osl_createSocketAddrWithFamily( osl_Socket_FamilyInet, htons(Port) , Addr );
732 }
733 return pAddr;
734}
735
736/*****************************************************************************/
737/* osl_setAddrOfSocketAddr */
738/*****************************************************************************/
739oslSocketResult SAL_CALL osl_setAddrOfSocketAddr( oslSocketAddr pAddr, sal_Sequence *pByteSeq )
740{
741 oslSocketResult res = osl_Socket_Error;
742
743 OSL_ASSERT( pAddr )do { if (((sal_Bool)1) && (!(pAddr))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "743" ": "), "OSL_ASSERT: %s", "pAddr"); } } while (((sal_Bool
)0))
;
744 OSL_ASSERT( pByteSeq )do { if (((sal_Bool)1) && (!(pByteSeq))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "744" ": "), "OSL_ASSERT: %s", "pByteSeq"); } } while ((
(sal_Bool)0))
;
745
746 if( pAddr && pByteSeq )
747 {
748 struct sockaddr_in * pSystemInetAddr;
749
750 OSL_ASSERT( pAddr->m_sockaddr.sa_family == FAMILY_TO_NATIVE( osl_Socket_FamilyInet ) )do { if (((sal_Bool)1) && (!(pAddr->m_sockaddr.sa_family
== (short)FamilyMap[osl_Socket_FamilyInet]))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "750" ": "), "OSL_ASSERT: %s", "pAddr->m_sockaddr.sa_family == FAMILY_TO_NATIVE( osl_Socket_FamilyInet )"
); } } while (((sal_Bool)0))
;
751 OSL_ASSERT( pByteSeq->nElements == 4 )do { if (((sal_Bool)1) && (!(pByteSeq->nElements ==
4))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"
), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c" ":" "751"
": "), "OSL_ASSERT: %s", "pByteSeq->nElements == 4"); } }
while (((sal_Bool)0))
;
752
753 pSystemInetAddr = (struct sockaddr_in * ) &(pAddr->m_sockaddr);
754 memcpy( &(pSystemInetAddr->sin_addr) , pByteSeq->elements , 4 );
755 res = osl_Socket_Ok;
756 }
757 return res;
758}
759
760/*****************************************************************************/
761/* osl_getAddrOfSocketAddr */
762/*****************************************************************************/
763oslSocketResult SAL_CALL osl_getAddrOfSocketAddr( oslSocketAddr pAddr, sal_Sequence **ppByteSeq )
764{
765 oslSocketResult res = osl_Socket_Error;
766
767 OSL_ASSERT( pAddr )do { if (((sal_Bool)1) && (!(pAddr))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "767" ": "), "OSL_ASSERT: %s", "pAddr"); } } while (((sal_Bool
)0))
;
768 OSL_ASSERT( ppByteSeq )do { if (((sal_Bool)1) && (!(ppByteSeq))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "768" ": "), "OSL_ASSERT: %s", "ppByteSeq"); } } while (
((sal_Bool)0))
;
769
770 if( pAddr && ppByteSeq )
771 {
772 struct sockaddr_in * pSystemInetAddr = (struct sockaddr_in * ) &(pAddr->m_sockaddr);
773 rtl_byte_sequence_constructFromArray( ppByteSeq , (sal_Int8 *) &(pSystemInetAddr->sin_addr),4);
774 res = osl_Socket_Ok;
775 }
776 return res;
777}
778
779
780/*****************************************************************************/
781/* _osl_getFullQualifiedDomainName */
782/*****************************************************************************/
783
784/** try to figure out a full-qualified hostname, by adding the current domain
785 as given by the domainname program to the given hostname.
786 This function MUST NOT call gethostbyname since pHostName allready points
787 to data returned by gethostname and would be garbled: use gethostname_r
788 instead!
789 */
790
791/* wrap around different interfaces to reentrant gethostbyname */
792static struct hostent* _osl_gethostbyname_r (
793 const char *name, struct hostent *result,
794 char *buffer, int buflen, int *h_errnop)
795{
796#if defined(LINUX1) || defined(ANDROID) || (defined(FREEBSD) && (__FreeBSD_version >= 601103)) || defined(DRAGONFLY)
797 struct hostent *__result; /* will be the same as result */
798 int __error;
799 __error = gethostbyname_r (name, result, buffer, buflen,
800 &__result, h_errnop);
801 return __error ? NULL((void*)0) : __result ;
802#elif defined(AIX)
803 *h_errnop = gethostbyname_r (name, result, (struct hostent_data *)buffer);
804 (void)buflen;
805 return *h_errnop ? NULL((void*)0) : result ;
806#else
807 return gethostbyname_r( name, result, buffer, buflen, h_errnop);
808#endif
809}
810
811static sal_Char* _osl_getFullQualifiedDomainName (const sal_Char *pHostName)
812{
813# define DOMAINNAME_LENGTH512 512
814 struct hostent aHostByName;
815 struct hostent *pHostByName;
816 sal_Char pQualifiedHostBuffer[ MAX_HOSTBUFFER_SIZE2048 ];
817 sal_Char *pFullQualifiedName = NULL((void*)0);
818 int nErrorNo;
819
820 pHostByName = _osl_gethostbyname_r (
821 pHostName,
822 &aHostByName, pQualifiedHostBuffer,
823 sizeof(pQualifiedHostBuffer), &nErrorNo );
824 if (pHostByName != NULL((void*)0))
825 {
826 pFullQualifiedName = strdup(pHostByName->h_name);
827 }
828 return pFullQualifiedName;
829}
830/*****************************************************************************/
831/* _osl_isFullQualifiedDomainName */
832/*****************************************************************************/
833static sal_Bool _osl_isFullQualifiedDomainName (const sal_Char *pHostName)
834{
835 /* a FQDN (aka 'hostname.domain.top_level_domain' )
836 * is a name which contains a dot '.' in it ( would
837 * match as well for 'hostname.' but is good enough
838 * for now )*/
839 return (sal_Bool)( strchr( pHostName, (int)'.' ) != NULL((void*)0) );
840}
841
842/*****************************************************************************/
843/* oslHostAddr */
844/*****************************************************************************/
845struct oslHostAddrImpl
846{
847 sal_Char *pHostName;
848 oslSocketAddr pSockAddr;
849};
850
851static oslHostAddr _osl_hostentToHostAddr (const struct hostent *he)
852{
853 oslHostAddr pAddr= NULL((void*)0);
854 oslSocketAddr pSockAddr = 0;
855
856 sal_Char *cn;
857
858 if ((he == NULL((void*)0)) || (he->h_name == NULL((void*)0)) || (he->h_addr_list[0] == NULL((void*)0)))
859 return ((oslHostAddr)NULL((void*)0));
860
861 if (_osl_isFullQualifiedDomainName(he->h_name))
862 {
863 cn= (sal_Char *)malloc(strlen (he->h_name) + 1);
864 OSL_ASSERT(cn)do { if (((sal_Bool)1) && (!(cn))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "864" ": "), "OSL_ASSERT: %s", "cn"); } } while (((sal_Bool
)0))
;
865 if (cn == NULL((void*)0))
866 return ((oslHostAddr)NULL((void*)0));
867
868 strcpy(cn, he->h_name);
869 }
870 else
871 {
872 cn =_osl_getFullQualifiedDomainName (he->h_name);
873 OSL_ASSERT(cn)do { if (((sal_Bool)1) && (!(cn))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "873" ": "), "OSL_ASSERT: %s", "cn"); } } while (((sal_Bool
)0))
;
874 if (cn == NULL((void*)0))
875 return ((oslHostAddr)NULL((void*)0));
876 }
877
878 pSockAddr = __osl_createSocketAddr();
879 OSL_ASSERT(pSockAddr)do { if (((sal_Bool)1) && (!(pSockAddr))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "879" ": "), "OSL_ASSERT: %s", "pSockAddr"); } } while (
((sal_Bool)0))
;
880 if (pSockAddr == NULL((void*)0))
881 {
882 free(cn);
883 return ((oslHostAddr)NULL((void*)0));
884 }
885
886 pSockAddr->m_sockaddr.sa_family= he->h_addrtype;
887 if (pSockAddr->m_sockaddr.sa_family == FAMILY_TO_NATIVE(osl_Socket_FamilyInet)(short)FamilyMap[osl_Socket_FamilyInet])
888 {
889 struct sockaddr_in *sin= (struct sockaddr_in *)&(pSockAddr->m_sockaddr);
890 memcpy (
891 &(sin->sin_addr.s_addr),
892 he->h_addr_list[0],
893 he->h_length);
894 }
895 else
896 {
897 /* unknown address family */
898 /* future extensions for new families might be implemented here */
899
900 OSL_TRACE("_osl_hostentToHostAddr: unknown address family.")do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "900" ": "), "_osl_hostentToHostAddr: unknown address family."
); } } while (((sal_Bool)0))
;
901 OSL_ASSERT(sal_False)do { if (((sal_Bool)1) && (!(((sal_Bool)0)))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "901" ": "), "OSL_ASSERT: %s", "sal_False"); } } while (
((sal_Bool)0))
;
902
903 __osl_destroySocketAddr( pSockAddr );
904 free (cn);
905 return ((oslHostAddr)NULL((void*)0));
906 }
907
908 pAddr= (oslHostAddr) malloc(sizeof(struct oslHostAddrImpl));
909 OSL_ASSERT(pAddr)do { if (((sal_Bool)1) && (!(pAddr))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "909" ": "), "OSL_ASSERT: %s", "pAddr"); } } while (((sal_Bool
)0))
;
910 if (pAddr == NULL((void*)0))
911 {
912 __osl_destroySocketAddr( pSockAddr );
913 free (cn);
914 return ((oslHostAddr)NULL((void*)0));
915 }
916
917 pAddr->pHostName= cn;
918 pAddr->pSockAddr= pSockAddr;
919
920 return pAddr;
921}
922
923/*****************************************************************************/
924/* osl_createHostAddr */
925/*****************************************************************************/
926oslHostAddr SAL_CALL osl_createHostAddr (
927 rtl_uString *ustrHostname,
928 const oslSocketAddr Addr)
929{
930 oslHostAddr HostAddr;
931 rtl_String* strHostname=0;
932 sal_Char* pszHostName=0;
933
934 if ( ustrHostname != 0 )
935 {
936 rtl_uString2String( &strHostname,
937 rtl_uString_getStr(ustrHostname),
938 rtl_uString_getLength(ustrHostname),
939 RTL_TEXTENCODING_UTF8(((rtl_TextEncoding) 76)),
940 OUSTRING_TO_OSTRING_CVTFLAGS(((sal_uInt32)0x0006) | ((sal_uInt32)0x0060) | ((sal_uInt32)0x0100
) | ((sal_uInt32)0x0400) | ((sal_uInt32)0x4000))
);
941 pszHostName = rtl_string_getStr(strHostname);
942 }
943
944 HostAddr = osl_psz_createHostAddr(pszHostName,Addr);
945
946 if ( strHostname != 0 )
947 {
948 rtl_string_release(strHostname);
949 }
950
951 return HostAddr;
952}
953
954oslHostAddr SAL_CALL osl_psz_createHostAddr (
955 const sal_Char *pszHostname,
956 const oslSocketAddr pAddr)
957{
958 oslHostAddr pHostAddr;
959 sal_Char *cn;
960
961 OSL_ASSERT(pszHostname && pAddr)do { if (((sal_Bool)1) && (!(pszHostname && pAddr
))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"
), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c" ":" "961"
": "), "OSL_ASSERT: %s", "pszHostname && pAddr"); } }
while (((sal_Bool)0))
;
962 if ((pszHostname == NULL((void*)0)) || (pAddr == NULL((void*)0)))
963 return ((oslHostAddr)NULL((void*)0));
964
965 cn = (sal_Char *)malloc(strlen (pszHostname) + 1);
966 OSL_ASSERT(cn)do { if (((sal_Bool)1) && (!(cn))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "966" ": "), "OSL_ASSERT: %s", "cn"); } } while (((sal_Bool
)0))
;
967 if (cn == NULL((void*)0))
968 return ((oslHostAddr)NULL((void*)0));
969
970 strcpy (cn, pszHostname);
971
972 pHostAddr= (oslHostAddr) malloc(sizeof(struct oslHostAddrImpl));
973 OSL_ASSERT(pHostAddr)do { if (((sal_Bool)1) && (!(pHostAddr))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "973" ": "), "OSL_ASSERT: %s", "pHostAddr"); } } while (
((sal_Bool)0))
;
974 if (pHostAddr == NULL((void*)0))
975 {
976 free (cn);
977 return ((oslHostAddr)NULL((void*)0));
978 }
979
980 pHostAddr->pHostName= cn;
981 pHostAddr->pSockAddr= osl_copySocketAddr( pAddr );
982
983 return pHostAddr;
984}
985
986/*****************************************************************************/
987/* osl_createHostAddrByName */
988/*****************************************************************************/
989oslHostAddr SAL_CALL osl_createHostAddrByName(rtl_uString *ustrHostname)
990{
991 oslHostAddr HostAddr;
992 rtl_String* strHostname=0;
993 sal_Char* pszHostName=0;
994
995 if ( ustrHostname != 0 )
996 {
997 rtl_uString2String( &strHostname,
998 rtl_uString_getStr(ustrHostname),
999 rtl_uString_getLength(ustrHostname),
1000 RTL_TEXTENCODING_UTF8(((rtl_TextEncoding) 76)),
1001 OUSTRING_TO_OSTRING_CVTFLAGS(((sal_uInt32)0x0006) | ((sal_uInt32)0x0060) | ((sal_uInt32)0x0100
) | ((sal_uInt32)0x0400) | ((sal_uInt32)0x4000))
);
1002 pszHostName=rtl_string_getStr(strHostname);
1003 }
1004
1005 HostAddr = osl_psz_createHostAddrByName(pszHostName);
1006
1007 if ( strHostname != 0 )
1008 {
1009 rtl_string_release(strHostname);
1010 }
1011
1012 return HostAddr;
1013}
1014
1015oslHostAddr SAL_CALL osl_psz_createHostAddrByName (const sal_Char *pszHostname)
1016{
1017 struct hostent aHe;
1018 struct hostent *pHe;
1019 sal_Char heBuffer[ MAX_HOSTBUFFER_SIZE2048 ];
1020 int nErrorNo;
1021
1022 pHe = _osl_gethostbyname_r (
1023 pszHostname,
1024 &aHe, heBuffer,
1025 sizeof(heBuffer), &nErrorNo );
1026
1027 return _osl_hostentToHostAddr (pHe);
1028}
1029
1030/*****************************************************************************/
1031/* osl_createHostAddrByAddr */
1032/*****************************************************************************/
1033oslHostAddr SAL_CALL osl_createHostAddrByAddr (const oslSocketAddr pAddr)
1034{
1035 OSL_ASSERT(pAddr)do { if (((sal_Bool)1) && (!(pAddr))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1035" ": "), "OSL_ASSERT: %s", "pAddr"); } } while (((sal_Bool
)0))
;
1036
1037 if (pAddr == NULL((void*)0))
1038 return ((oslHostAddr)NULL((void*)0));
1039
1040 if (pAddr->m_sockaddr.sa_family == FAMILY_TO_NATIVE(osl_Socket_FamilyInet)(short)FamilyMap[osl_Socket_FamilyInet])
1041 {
1042 const struct sockaddr_in *sin= (const struct sockaddr_in *)&(pAddr->m_sockaddr);
1043 struct hostent *he;
1044
1045 if (sin->sin_addr.s_addr == htonl(INADDR_ANY((in_addr_t) 0x00000000)))
1046 return ((oslHostAddr)NULL((void*)0));
1047
1048 he= gethostbyaddr((sal_Char *)&(sin->sin_addr),
1049 sizeof (sin->sin_addr),
1050 sin->sin_family);
1051 return _osl_hostentToHostAddr (he);
1052 }
1053
1054 return ((oslHostAddr)NULL((void*)0));
1055}
1056
1057/*****************************************************************************/
1058/* osl_copyHostAddr */
1059/*****************************************************************************/
1060oslHostAddr SAL_CALL osl_copyHostAddr (const oslHostAddr pAddr)
1061{
1062 OSL_ASSERT(pAddr)do { if (((sal_Bool)1) && (!(pAddr))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1062" ": "), "OSL_ASSERT: %s", "pAddr"); } } while (((sal_Bool
)0))
;
1063
1064 if (pAddr)
1065 return osl_psz_createHostAddr (pAddr->pHostName, pAddr->pSockAddr);
1066 else
1067 return ((oslHostAddr)NULL((void*)0));
1068}
1069
1070/*****************************************************************************/
1071/* osl_getHostnameOfHostAddr */
1072/*****************************************************************************/
1073void SAL_CALL osl_getHostnameOfHostAddr (
1074 const oslHostAddr Addr,
1075 rtl_uString **ustrHostname)
1076{
1077 const sal_Char* pHostname=0;
1078
1079 pHostname = osl_psz_getHostnameOfHostAddr(Addr);
1080
1081 rtl_uString_newFromAscii (ustrHostname, pHostname);
1082
1083 return;
1084}
1085
1086const sal_Char* SAL_CALL osl_psz_getHostnameOfHostAddr (const oslHostAddr pAddr)
1087{
1088 if (pAddr)
1089 return pAddr->pHostName;
1090 else
1091 return NULL((void*)0);
1092}
1093
1094/*****************************************************************************/
1095/* osl_getSocketAddrOfHostAddr */
1096/*****************************************************************************/
1097oslSocketAddr SAL_CALL osl_getSocketAddrOfHostAddr (const oslHostAddr pAddr)
1098{
1099 OSL_ASSERT(pAddr)do { if (((sal_Bool)1) && (!(pAddr))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1099" ": "), "OSL_ASSERT: %s", "pAddr"); } } while (((sal_Bool
)0))
;
1100
1101 if (pAddr)
1102 return ((oslSocketAddr)(pAddr->pSockAddr));
1103 else
1104 return NULL((void*)0);
1105}
1106
1107/*****************************************************************************/
1108/* osl_destroyHostAddr */
1109/*****************************************************************************/
1110void SAL_CALL osl_destroyHostAddr (oslHostAddr pAddr)
1111{
1112 if (pAddr)
1113 {
1114 if (pAddr->pHostName)
1115 free (pAddr->pHostName);
1116 if (pAddr->pSockAddr)
1117 osl_destroySocketAddr (pAddr->pSockAddr);
1118 free (pAddr);
1119 }
1120}
1121
1122/*****************************************************************************/
1123/* osl_getLocalHostname */
1124/*****************************************************************************/
1125oslSocketResult SAL_CALL osl_getLocalHostname(rtl_uString **ustrLocalHostname)
1126{
1127 oslSocketResult Result;
1128 sal_Char pszHostname[1024];
1129
1130 pszHostname[0] = '\0';
1131
1132 Result = osl_psz_getLocalHostname(pszHostname,sizeof(pszHostname));
1133
1134 rtl_uString_newFromAscii(ustrLocalHostname,pszHostname);
1135
1136 return Result;
1137}
1138
1139oslSocketResult SAL_CALL osl_psz_getLocalHostname (
1140 sal_Char *pBuffer, sal_uInt32 nBufLen)
1141{
1142 static sal_Char LocalHostname[256] = "";
1143
1144 if (strlen(LocalHostname) == 0)
1145 {
1146 const sal_Char *pStr;
1147
1148#ifdef SYSV
1149 struct utsname uts;
1150
1151 if (uname(&uts) < 0)
1152 return osl_Socket_Error;
1153
1154 if ((strlen(uts.nodename) + 1) > nBufLen)
1155 return osl_Socket_Error;
1156
1157 strncpy(LocalHostname, uts.nodename, sizeof( LocalHostname ));
1158#else /* BSD compatible */
1159
1160 if (gethostname(LocalHostname, sizeof(LocalHostname)-1) != 0)
1161 return osl_Socket_Error;
1162 LocalHostname[sizeof(LocalHostname)-1] = 0;
1163#endif /* SYSV */
1164
1165 /* check if we have an FQDN */
1166 if (strchr(LocalHostname, '.') == NULL((void*)0))
1167 {
1168 oslHostAddr Addr;
1169
1170 /* no, determine it via dns */
1171 Addr = osl_psz_createHostAddrByName(LocalHostname);
1172
1173 if ((pStr = osl_psz_getHostnameOfHostAddr(Addr)) != NULL((void*)0))
1174 {
1175 strcpy(LocalHostname, pStr);
1176 }
1177 osl_destroyHostAddr(Addr);
1178 }
1179 }
1180
1181 if (strlen(LocalHostname) > 0)
1182 {
1183 strncpy(pBuffer, LocalHostname, nBufLen);
1184 pBuffer[nBufLen - 1] = '\0';
1185
1186 return osl_Socket_Ok;
1187 }
1188
1189 return osl_Socket_Error;
1190}
1191
1192/*****************************************************************************/
1193/* osl_resolveHostname */
1194/*****************************************************************************/
1195oslSocketAddr SAL_CALL osl_resolveHostname(rtl_uString *ustrHostname)
1196{
1197 oslSocketAddr Addr;
1198 rtl_String* strHostname=0;
1199 sal_Char* pszHostName=0;
1200
1201 if ( ustrHostname != 0 )
1202 {
1203 rtl_uString2String( &strHostname,
1204 rtl_uString_getStr(ustrHostname),
1205 rtl_uString_getLength(ustrHostname),
1206 RTL_TEXTENCODING_UTF8(((rtl_TextEncoding) 76)),
1207 OUSTRING_TO_OSTRING_CVTFLAGS(((sal_uInt32)0x0006) | ((sal_uInt32)0x0060) | ((sal_uInt32)0x0100
) | ((sal_uInt32)0x0400) | ((sal_uInt32)0x4000))
);
1208 pszHostName = rtl_string_getStr(strHostname);
1209 }
1210
1211
1212 Addr = osl_psz_resolveHostname(pszHostName);
1213
1214 if ( strHostname != 0 )
1215 {
1216 rtl_string_release(strHostname);
1217 }
1218
1219
1220 return Addr;
1221}
1222
1223
1224oslSocketAddr SAL_CALL osl_psz_resolveHostname(const sal_Char* pszHostname)
1225{
1226 struct oslHostAddrImpl *pAddr = (oslHostAddr)osl_psz_createHostAddrByName(pszHostname);
1227
1228 if (pAddr)
1229 {
1230 oslSocketAddr SockAddr = osl_copySocketAddr(pAddr->pSockAddr);
1231
1232 osl_destroyHostAddr(pAddr);
1233
1234 return (SockAddr);
1235 }
1236
1237 return ((oslSocketAddr)NULL((void*)0));
1238}
1239
1240/*****************************************************************************/
1241/* osl_getServicePort */
1242/*****************************************************************************/
1243sal_Int32 SAL_CALL osl_getServicePort(rtl_uString *ustrServicename, rtl_uString *ustrProtocol)
1244{
1245 sal_Int32 nPort;
1246 rtl_String* strServicename=0;
1247 rtl_String* strProtocol=0;
1248 sal_Char* pszServiceName=0;
1249 sal_Char* pszProtocol=0;
1250
1251 if ( ustrServicename != 0 )
1252 {
1253 rtl_uString2String( &strServicename,
1254 rtl_uString_getStr(ustrServicename),
1255 rtl_uString_getLength(ustrServicename),
1256 RTL_TEXTENCODING_UTF8(((rtl_TextEncoding) 76)),
1257 OUSTRING_TO_OSTRING_CVTFLAGS(((sal_uInt32)0x0006) | ((sal_uInt32)0x0060) | ((sal_uInt32)0x0100
) | ((sal_uInt32)0x0400) | ((sal_uInt32)0x4000))
);
1258 pszServiceName = rtl_string_getStr(strServicename);
1259 }
1260
1261 if ( ustrProtocol != 0 )
1262 {
1263 rtl_uString2String( &strProtocol,
1264 rtl_uString_getStr(ustrProtocol),
1265 rtl_uString_getLength(ustrProtocol),
1266 RTL_TEXTENCODING_UTF8(((rtl_TextEncoding) 76)),
1267 OUSTRING_TO_OSTRING_CVTFLAGS(((sal_uInt32)0x0006) | ((sal_uInt32)0x0060) | ((sal_uInt32)0x0100
) | ((sal_uInt32)0x0400) | ((sal_uInt32)0x4000))
);
1268 pszProtocol = rtl_string_getStr(strProtocol);
1269 }
1270
1271 nPort = osl_psz_getServicePort(pszServiceName,pszProtocol);
1272
1273 if ( strServicename != 0 )
1274 {
1275 rtl_string_release(strServicename);
1276 }
1277
1278 if ( strProtocol != 0 )
1279 {
1280 rtl_string_release(strProtocol);
1281 }
1282
1283
1284 return nPort;
1285}
1286
1287
1288sal_Int32 SAL_CALL osl_psz_getServicePort(const sal_Char* pszServicename,
1289 const sal_Char* pszProtocol)
1290{
1291 struct servent* ps;
1292
1293 ps= getservbyname(pszServicename, pszProtocol);
1294
1295 if (ps != 0)
1296 return ntohs(ps->s_port);
1297
1298 return OSL_INVALID_PORT(-1);
1299}
1300
1301/*****************************************************************************/
1302/* osl_destroySocketAddr */
1303/*****************************************************************************/
1304void SAL_CALL osl_destroySocketAddr(oslSocketAddr pAddr)
1305{
1306 __osl_destroySocketAddr( pAddr );
1307}
1308
1309/*****************************************************************************/
1310/* osl_getFamilyOfSocketAddr */
1311/*****************************************************************************/
1312oslAddrFamily SAL_CALL osl_getFamilyOfSocketAddr(oslSocketAddr pAddr)
1313{
1314 OSL_ASSERT(pAddr)do { if (((sal_Bool)1) && (!(pAddr))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1314" ": "), "OSL_ASSERT: %s", "pAddr"); } } while (((sal_Bool
)0))
;
1315
1316 if (pAddr)
1317 return FAMILY_FROM_NATIVE(pAddr->m_sockaddr.sa_family)osl_AddrFamilyFromNative(pAddr->m_sockaddr.sa_family);
1318 else
1319 return osl_Socket_FamilyInvalid;
1320}
1321
1322/*****************************************************************************/
1323/* osl_getInetPortOfSocketAddr */
1324/*****************************************************************************/
1325sal_Int32 SAL_CALL osl_getInetPortOfSocketAddr(oslSocketAddr pAddr)
1326{
1327 OSL_ASSERT(pAddr)do { if (((sal_Bool)1) && (!(pAddr))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1327" ": "), "OSL_ASSERT: %s", "pAddr"); } } while (((sal_Bool
)0))
;
1328 if( pAddr )
1329 {
1330 struct sockaddr_in* pSystemInetAddr= (struct sockaddr_in*)&(pAddr->m_sockaddr);
1331
1332 if ( pSystemInetAddr->sin_family == FAMILY_TO_NATIVE(osl_Socket_FamilyInet)(short)FamilyMap[osl_Socket_FamilyInet])
1333 return ntohs(pSystemInetAddr->sin_port);
1334 }
1335 return OSL_INVALID_PORT(-1);
1336}
1337
1338/*****************************************************************************/
1339/* osl_setInetPortOfSocketAddr */
1340/*****************************************************************************/
1341sal_Bool SAL_CALL osl_setInetPortOfSocketAddr(oslSocketAddr pAddr, sal_Int32 Port)
1342{
1343 OSL_ASSERT(pAddr)do { if (((sal_Bool)1) && (!(pAddr))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1343" ": "), "OSL_ASSERT: %s", "pAddr"); } } while (((sal_Bool
)0))
;
1344 if( pAddr )
1345 {
1346 struct sockaddr_in* pSystemInetAddr= (struct sockaddr_in*)&(pAddr->m_sockaddr);
1347 if ( pSystemInetAddr->sin_family == FAMILY_TO_NATIVE(osl_Socket_FamilyInet)(short)FamilyMap[osl_Socket_FamilyInet])
1348 {
1349 pSystemInetAddr->sin_port= htons((short)Port);
1350 return sal_True((sal_Bool)1);
1351 }
1352 }
1353
1354 /* this is not a inet-addr => can't set port */
1355 return sal_False((sal_Bool)0);
1356}
1357
1358/*****************************************************************************/
1359/* osl_getHostnameOfSocketAddr */
1360/*****************************************************************************/
1361oslSocketResult SAL_CALL osl_getHostnameOfSocketAddr(oslSocketAddr Addr, rtl_uString **ustrHostname)
1362{
1363 oslSocketResult Result;
1364 sal_Char pszHostname[1024];
1365
1366 pszHostname[0] = '\0';
1367
1368 Result = osl_psz_getHostnameOfSocketAddr(Addr,pszHostname,sizeof(pszHostname));
1369
1370 rtl_uString_newFromAscii(ustrHostname,pszHostname);
1371
1372 return Result;
1373}
1374
1375
1376oslSocketResult SAL_CALL osl_psz_getHostnameOfSocketAddr(oslSocketAddr pAddr,
1377 sal_Char *pBuffer, sal_uInt32 BufferSize)
1378{
1379 oslHostAddr pHostAddr= (oslHostAddr )osl_createHostAddrByAddr(pAddr);
1380
1381 if (pHostAddr)
1382 {
1383 strncpy(pBuffer, pHostAddr->pHostName, BufferSize);
1384
1385 pBuffer[BufferSize - 1] = '\0';
1386
1387 osl_destroyHostAddr(pHostAddr);
1388
1389 return osl_Socket_Ok;
1390 }
1391
1392 return osl_Socket_Error;
1393}
1394
1395/*****************************************************************************/
1396/* osl_getDottedInetAddrOfSocketAddr */
1397/*****************************************************************************/
1398oslSocketResult SAL_CALL osl_getDottedInetAddrOfSocketAddr(oslSocketAddr Addr, rtl_uString **ustrDottedInetAddr)
1399{
1400 oslSocketResult Result;
1401 sal_Char pszDottedInetAddr[1024];
1402
1403 pszDottedInetAddr[0] = '\0';
1404
1405 Result = osl_psz_getDottedInetAddrOfSocketAddr(Addr,pszDottedInetAddr,sizeof(pszDottedInetAddr));
1406
1407 rtl_uString_newFromAscii(ustrDottedInetAddr,pszDottedInetAddr);
1408
1409 return Result;
1410
1411}
1412
1413oslSocketResult SAL_CALL osl_psz_getDottedInetAddrOfSocketAddr(oslSocketAddr pAddr,
1414 sal_Char *pBuffer, sal_uInt32 BufferSize)
1415{
1416 OSL_ASSERT(pAddr)do { if (((sal_Bool)1) && (!(pAddr))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1416" ": "), "OSL_ASSERT: %s", "pAddr"); } } while (((sal_Bool
)0))
;
1417
1418 if( pAddr )
1419 {
1420 struct sockaddr_in* pSystemInetAddr = ( struct sockaddr_in * ) &(pAddr->m_sockaddr);
1421
1422 if (pSystemInetAddr->sin_family == FAMILY_TO_NATIVE(osl_Socket_FamilyInet)(short)FamilyMap[osl_Socket_FamilyInet])
1423 {
1424 strncpy(pBuffer, inet_ntoa(pSystemInetAddr->sin_addr), BufferSize);
1425 pBuffer[BufferSize - 1] = '\0';
1426
1427 return osl_Socket_Ok;
1428 }
1429 }
1430
1431 return osl_Socket_Error;
1432}
1433
1434/*****************************************************************************/
1435/* osl_createSocket */
1436/*****************************************************************************/
1437oslSocket SAL_CALL osl_createSocket(oslAddrFamily Family,
1438 oslSocketType Type,
1439 oslProtocol Protocol)
1440{
1441 int Flags;
1442 oslSocket pSocket;
1443
1444 /* alloc memory */
1445 pSocket= __osl_createSocketImpl(OSL_INVALID_SOCKET-1);
1446
1447 /* create socket */
1448 pSocket->m_Socket= socket(FAMILY_TO_NATIVE(Family)(short)FamilyMap[Family],
1449 TYPE_TO_NATIVE(Type)TypeMap[Type],
1450 PROTOCOL_TO_NATIVE(Protocol)ProtocolMap[Protocol]);
1451
1452 /* creation failed => free memory */
1453 if(pSocket->m_Socket == OSL_INVALID_SOCKET-1)
1454 {
1455 OSL_TRACE("osl_createSocket failed. Errno: %d; %s\n",do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1457" ": "), "osl_createSocket failed. Errno: %d; %s\n"
, (*__errno_location ()), strerror((*__errno_location ()))); }
} while (((sal_Bool)0))
1456 errno,do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1457" ": "), "osl_createSocket failed. Errno: %d; %s\n"
, (*__errno_location ()), strerror((*__errno_location ()))); }
} while (((sal_Bool)0))
1457 strerror(errno))do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1457" ": "), "osl_createSocket failed. Errno: %d; %s\n"
, (*__errno_location ()), strerror((*__errno_location ()))); }
} while (((sal_Bool)0))
;
1458
1459 __osl_destroySocketImpl((pSocket));
1460 pSocket= 0;
1461 }
1462 else
1463 {
1464 /* set close-on-exec flag */
1465 if ((Flags = fcntl(pSocket->m_Socket, F_GETFD1, 0)) != -1)
1466 {
1467 Flags |= FD_CLOEXEC1;
1468 if (fcntl(pSocket->m_Socket, F_SETFD2, Flags) == -1)
1469 {
1470 pSocket->m_nLastError=errno(*__errno_location ());
1471 OSL_TRACE("osl_createSocket failed changing socket flags. Errno: %d; %s\n",do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1473" ": "), "osl_createSocket failed changing socket flags. Errno: %d; %s\n"
, (*__errno_location ()), strerror((*__errno_location ()))); }
} while (((sal_Bool)0))
1472 errno,do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1473" ": "), "osl_createSocket failed changing socket flags. Errno: %d; %s\n"
, (*__errno_location ()), strerror((*__errno_location ()))); }
} while (((sal_Bool)0))
1473 strerror(errno))do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1473" ": "), "osl_createSocket failed changing socket flags. Errno: %d; %s\n"
, (*__errno_location ()), strerror((*__errno_location ()))); }
} while (((sal_Bool)0))
;
1474 }
1475 }
1476 else
1477 {
1478 pSocket->m_nLastError=errno(*__errno_location ());
1479 }
1480
1481
1482 pSocket->m_CloseCallback = NULL((void*)0);
1483 pSocket->m_CallbackArg = NULL((void*)0);
1484 }
1485
1486 return pSocket;
1487}
1488
1489void SAL_CALL osl_acquireSocket(oslSocket pSocket)
1490{
1491 osl_incrementInterlockedCount( &(pSocket->m_nRefCount ) );
1492}
1493
1494void SAL_CALL osl_releaseSocket( oslSocket pSocket )
1495{
1496 if( pSocket && 0 == osl_decrementInterlockedCount( &(pSocket->m_nRefCount) ) )
1497 {
1498#if defined(LINUX1)
1499 if ( pSocket->m_bIsAccepting == sal_True((sal_Bool)1) )
1500 {
1501 OSL_FAIL("osl_destroySocket : attempt to destroy socket while accepting\n")do { if (((sal_Bool)1) && (((sal_Bool)1))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1501" ": "), "%s", "osl_destroySocket : attempt to destroy socket while accepting\n"
); } } while (((sal_Bool)0))
;
1502 return;
1503 }
1504#endif /* LINUX */
1505 osl_closeSocket( pSocket );
1506 __osl_destroySocketImpl( pSocket );
1507 }
1508}
1509
1510
1511
1512/*****************************************************************************/
1513/* osl_closeSocket */
1514/*****************************************************************************/
1515void SAL_CALL osl_closeSocket(oslSocket pSocket)
1516{
1517 int nRet;
1518 int nFD;
1519
1520 /* socket already invalid */
1521 if(pSocket==0)
1522 return;
1523
1524 pSocket->m_nLastError=0;
1525 nFD = pSocket->m_Socket;
1526
1527 if (nFD == OSL_INVALID_SOCKET-1)
1528 return;
1529
1530 pSocket->m_Socket = OSL_INVALID_SOCKET-1;
1531
1532#if defined(LINUX1)
1533 pSocket->m_bIsInShutdown = sal_True((sal_Bool)1);
1534
1535 if ( pSocket->m_bIsAccepting == sal_True((sal_Bool)1) )
1536 {
1537 int nConnFD;
1538 union {
1539 struct sockaddr aSockAddr;
1540 struct sockaddr_in aSockAddrIn;
1541 } s;
1542 socklen_t nSockLen = sizeof(s.aSockAddr);
1543
1544 nRet = getsockname(nFD, &s.aSockAddr, &nSockLen);
1545 if ( nRet < 0 )
1546 {
1547 OSL_TRACE("getsockname call failed with error: %s", strerror(errno))do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1547" ": "), "getsockname call failed with error: %s", strerror
((*__errno_location ()))); } } while (((sal_Bool)0))
;
1548 }
1549
1550 if ( s.aSockAddr.sa_family == AF_INET2 )
1551 {
1552 if ( s.aSockAddrIn.sin_addr.s_addr == htonl(INADDR_ANY((in_addr_t) 0x00000000)) )
1553 {
1554 s.aSockAddrIn.sin_addr.s_addr = htonl(INADDR_LOOPBACK((in_addr_t) 0x7f000001));
1555 }
1556
1557 nConnFD = socket(AF_INET2, SOCK_STREAMSOCK_STREAM, 0);
1558 if ( nConnFD < 0 )
1559 {
1560 OSL_TRACE("socket call failed with error: %s", strerror(errno))do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1560" ": "), "socket call failed with error: %s", strerror
((*__errno_location ()))); } } while (((sal_Bool)0))
;
1561 }
1562
1563 nRet = connect(nConnFD, &s.aSockAddr, sizeof(s.aSockAddr));
1564 if ( nRet < 0 )
1565 {
1566 OSL_TRACE("connect call failed with error: %s", strerror(errno))do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1566" ": "), "connect call failed with error: %s", strerror
((*__errno_location ()))); } } while (((sal_Bool)0))
;
1567 }
1568 close(nConnFD);
1569 }
1570 pSocket->m_bIsAccepting = sal_False((sal_Bool)0);
1571 }
1572#endif /* LINUX */
1573
1574 /* registrierten Callback ausfuehren */
1575 if (pSocket->m_CloseCallback != NULL((void*)0))
1576 {
1577 pSocket->m_CloseCallback(pSocket->m_CallbackArg);
1578 }
1579
1580 nRet=close(nFD);
1581 if ( nRet != 0 )
1582 {
1583 pSocket->m_nLastError=errno(*__errno_location ());
1584 OSL_TRACE("closeSocket close error '%s'",strerror(errno))do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1584" ": "), "closeSocket close error '%s'",strerror((*
__errno_location ()))); } } while (((sal_Bool)0))
;
1585 }
1586
1587 pSocket->m_Socket = OSL_INVALID_SOCKET-1;
1588}
1589
1590/*****************************************************************************/
1591/* osl_getLocalAddrOfSocket */
1592/* Note that I rely on the fact that oslSocketAddr and struct sockaddr */
1593/* are the same! I don't like it very much but see no other easy way to conceal */
1594/* the struct sockaddr from the eyes of the user. */
1595/*****************************************************************************/
1596oslSocketAddr SAL_CALL osl_getLocalAddrOfSocket(oslSocket pSocket)
1597{
1598 socklen_t AddrLen;
1599 struct sockaddr Addr;
1600 oslSocketAddr pAddr;
1601
1602 if (pSocket == NULL((void*)0)) /* ENOTSOCK */
1603 return ((oslSocketAddr)NULL((void*)0));
1604
1605 AddrLen= sizeof(struct sockaddr);
1606
1607 if (getsockname(pSocket->m_Socket, &Addr, &AddrLen) == OSL_SOCKET_ERROR-1)
1608 return ((oslSocketAddr)NULL((void*)0));
1609
1610 pAddr = __osl_createSocketAddrFromSystem( &Addr );
1611 return pAddr;
1612}
1613
1614/*****************************************************************************/
1615/* osl_getPeerAddrOfSocket */
1616/*****************************************************************************/
1617oslSocketAddr SAL_CALL osl_getPeerAddrOfSocket(oslSocket pSocket)
1618{
1619 socklen_t AddrLen;
1620 struct sockaddr Addr;
1621
1622 OSL_ASSERT(pSocket)do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1622" ": "), "OSL_ASSERT: %s", "pSocket"); } } while ((
(sal_Bool)0))
;
1623 if ( pSocket == 0 )
1624 {
1625 return 0;
1626 }
1627
1628 pSocket->m_nLastError=0;
1629 AddrLen= sizeof(struct sockaddr);
1630
1631 if(getpeername(pSocket->m_Socket, &Addr, &AddrLen) == OSL_SOCKET_ERROR-1)
1632 {
1633 pSocket->m_nLastError=errno(*__errno_location ());
1634 return 0;
1635 }
1636 return __osl_createSocketAddrFromSystem( &Addr );
1637}
1638
1639/*****************************************************************************/
1640/* osl_bindAddrToSocket */
1641/*****************************************************************************/
1642sal_Bool SAL_CALL osl_bindAddrToSocket(oslSocket pSocket,
1643 oslSocketAddr pAddr)
1644{
1645 int nRet;
1646
1647 OSL_ASSERT(pSocket && pAddr )do { if (((sal_Bool)1) && (!(pSocket && pAddr
))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"
), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c" ":" "1647"
": "), "OSL_ASSERT: %s", "pSocket && pAddr"); } } while
(((sal_Bool)0))
;
1648 if ( pSocket == 0 || pAddr == 0 )
1649 {
1650 return sal_False((sal_Bool)0);
1651 }
1652
1653 pSocket->m_nLastError=0;
1654
1655 nRet = bind(pSocket->m_Socket, &(pAddr->m_sockaddr), sizeof(struct sockaddr));
1656
1657 if ( nRet == OSL_SOCKET_ERROR-1)
1658 {
1659 pSocket->m_nLastError=errno(*__errno_location ());
1660 return sal_False((sal_Bool)0);
1661 }
1662
1663 return sal_True((sal_Bool)1);
1664}
1665
1666
1667/*****************************************************************************/
1668/* osl_listenOnSocket */
1669/*****************************************************************************/
1670sal_Bool SAL_CALL osl_listenOnSocket(oslSocket pSocket,
1671 sal_Int32 MaxPendingConnections)
1672{
1673 int nRet;
1674
1675 OSL_ASSERT(pSocket)do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1675" ": "), "OSL_ASSERT: %s", "pSocket"); } } while ((
(sal_Bool)0))
;
1676 if ( pSocket == 0 )
1677 {
1678 return sal_False((sal_Bool)0);
1679 }
1680
1681 pSocket->m_nLastError=0;
1682
1683 nRet = listen(pSocket->m_Socket,
1684 MaxPendingConnections == -1 ?
1685 SOMAXCONN128 :
1686 MaxPendingConnections);
1687 if ( nRet == OSL_SOCKET_ERROR-1)
1688 {
1689 pSocket->m_nLastError=errno(*__errno_location ());
1690 return sal_False((sal_Bool)0);
1691 }
1692
1693 return sal_True((sal_Bool)1);
1694}
1695
1696
1697/*****************************************************************************/
1698/* osl_connectSocketTo */
1699/*****************************************************************************/
1700oslSocketResult SAL_CALL osl_connectSocketTo(oslSocket pSocket,
1701 oslSocketAddr pAddr,
1702 const TimeValue* pTimeout)
1703{
1704 fd_set WriteSet;
1705 fd_set ExcptSet;
1706 int ReadyHandles;
1707 struct timeval tv;
1708 oslSocketResult Result= osl_Socket_Ok;
1709
1710 OSL_PRECOND(pSocket, "osl_connectSocketTo(): need a valid socket!\n")do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1710" ": "), "%s", "osl_connectSocketTo(): need a valid socket!\n"
); } } while (((sal_Bool)0))
;
1711
1712 if ( pSocket == 0 )
1713 {
1714 return osl_Socket_Error;
1715 }
1716
1717 pSocket->m_nLastError=0;
1718
1719 if (osl_isNonBlockingMode(pSocket))
1720 {
1721 if (connect(pSocket->m_Socket,
1722 &(pAddr->m_sockaddr),
1723 sizeof(struct sockaddr)) != OSL_SOCKET_ERROR-1)
1724 return osl_Socket_Ok;
1725 else
1726 if (errno(*__errno_location ()) == EWOULDBLOCK11 || errno(*__errno_location ()) == EINPROGRESS115)
1727 {
1728 pSocket->m_nLastError=EINPROGRESS115;
1729 return osl_Socket_InProgress;
1730 }
1731
1732
1733 pSocket->m_nLastError=errno(*__errno_location ());
1734 OSL_TRACE("can't connect : '%s'",strerror(errno))do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1734" ": "), "can't connect : '%s'",strerror((*__errno_location
()))); } } while (((sal_Bool)0))
;
1735 return osl_Socket_Error;
1736 }
1737
1738 /* set socket temporarily to non-blocking */
1739 OSL_VERIFY(osl_enableNonBlockingMode(pSocket, sal_True))do { if (!(osl_enableNonBlockingMode(pSocket, ((sal_Bool)1)))
) do { if (((sal_Bool)1) && (!(0))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1739" ": "), "OSL_ASSERT: %s", "0"); } } while (((sal_Bool
)0)); } while (0)
;
1740
1741 /* initiate connect */
1742 if(connect(pSocket->m_Socket,
1743 &(pAddr->m_sockaddr),
1744 sizeof(struct sockaddr)) != OSL_SOCKET_ERROR-1)
1745 {
1746 /* immediate connection */
1747 osl_enableNonBlockingMode(pSocket, sal_False((sal_Bool)0));
1748
1749 return osl_Socket_Ok;
1750 }
1751 else
1752 {
1753 /* really an error or just delayed? */
1754 if (errno(*__errno_location ()) != EINPROGRESS115)
1755 {
1756 pSocket->m_nLastError=errno(*__errno_location ());
1757 OSL_TRACE(do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1759" ": "), "osl_connectSocketTo(): connect failed: errno: %d (%s)\n"
, (*__errno_location ()), strerror((*__errno_location ()))); }
} while (((sal_Bool)0))
1758 "osl_connectSocketTo(): connect failed: errno: %d (%s)\n",do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1759" ": "), "osl_connectSocketTo(): connect failed: errno: %d (%s)\n"
, (*__errno_location ()), strerror((*__errno_location ()))); }
} while (((sal_Bool)0))
1759 errno, strerror(errno))do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1759" ": "), "osl_connectSocketTo(): connect failed: errno: %d (%s)\n"
, (*__errno_location ()), strerror((*__errno_location ()))); }
} while (((sal_Bool)0))
;
1760
1761 osl_enableNonBlockingMode(pSocket, sal_False((sal_Bool)0));
1762 return osl_Socket_Error;
1763 }
1764 }
1765
1766
1767 /* prepare select set for socket */
1768 FD_ZERO(&WriteSet)do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; stosl" :
"=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof
(__fd_mask)), "1" (&((&WriteSet)->__fds_bits)[0])
: "memory"); } while (0)
;
1769 FD_ZERO(&ExcptSet)do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; stosl" :
"=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof
(__fd_mask)), "1" (&((&ExcptSet)->__fds_bits)[0])
: "memory"); } while (0)
;
1770 FD_SET(pSocket->m_Socket, &WriteSet)((void) (((&WriteSet)->__fds_bits)[((pSocket->m_Socket
) / (8 * sizeof(unsigned long)))] |= ((__fd_mask) 1 << (
(pSocket->m_Socket) % (8 * sizeof(unsigned long))))))
;
1771 FD_SET(pSocket->m_Socket, &ExcptSet)((void) (((&ExcptSet)->__fds_bits)[((pSocket->m_Socket
) / (8 * sizeof(unsigned long)))] |= ((__fd_mask) 1 << (
(pSocket->m_Socket) % (8 * sizeof(unsigned long))))))
;
1772
1773 /* prepare timeout */
1774 if (pTimeout)
1775 {
1776 /* divide milliseconds into seconds and microseconds */
1777 tv.tv_sec= pTimeout->Seconds;
1778 tv.tv_usec= pTimeout->Nanosec / 1000L;
1779 }
1780
1781 /* select */
1782 ReadyHandles= select(pSocket->m_Socket+1,
1783 0,
1784 PTR_FD_SET(WriteSet)(&(WriteSet)),
1785 PTR_FD_SET(ExcptSet)(&(ExcptSet)),
1786 (pTimeout) ? &tv : 0);
1787
1788 if (ReadyHandles > 0) /* connected */
1789 {
1790 if ( FD_ISSET(pSocket->m_Socket, &WriteSet )((((&WriteSet)->__fds_bits)[((pSocket->m_Socket) / (
8 * sizeof(unsigned long)))] & ((__fd_mask) 1 << ((
pSocket->m_Socket) % (8 * sizeof(unsigned long))))) != 0)
)
1791 {
1792 int nErrorCode = 0;
1793 socklen_t nErrorSize = sizeof( nErrorCode );
1794
1795 int nSockOpt;
1796
1797 nSockOpt = getsockopt ( pSocket->m_Socket, SOL_SOCKET1, SO_ERROR4,
1798 &nErrorCode, &nErrorSize );
1799 if ( (nSockOpt == 0) && (nErrorCode == 0))
1800 Result = osl_Socket_Ok;
1801 else
1802 Result = osl_Socket_Error;
1803 }
1804 else
1805 {
1806 Result= osl_Socket_Error;
1807 }
1808 }
1809 else if (ReadyHandles < 0) /* error */
1810 {
1811 if (errno(*__errno_location ()) == EBADF9) /* most probably interrupted by close() */
1812 {
1813 /* do not access pSockImpl because it is about to be or */
1814 /* already destroyed */
1815 return osl_Socket_Interrupted;
1816 }
1817 else
1818 {
1819 pSocket->m_nLastError=errno(*__errno_location ());
1820 Result= osl_Socket_Error;
1821 }
1822 }
1823 else /* timeout */
1824 {
1825 pSocket->m_nLastError=errno(*__errno_location ());
1826 Result= osl_Socket_TimedOut;
1827 }
1828
1829 osl_enableNonBlockingMode(pSocket, sal_False((sal_Bool)0));
1830
1831 return Result;
1832}
1833
1834
1835/*****************************************************************************/
1836/* osl_acceptConnectionOnSocket */
1837/*****************************************************************************/
1838oslSocket SAL_CALL osl_acceptConnectionOnSocket(oslSocket pSocket,
1839 oslSocketAddr* ppAddr)
1840{
1841 struct sockaddr Addr;
1842 int Connection, Flags;
1843 oslSocket pConnectionSockImpl;
1844
1845 socklen_t AddrLen = sizeof(struct sockaddr);
1846 OSL_ASSERT(pSocket)do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1846" ": "), "OSL_ASSERT: %s", "pSocket"); } } while ((
(sal_Bool)0))
;
1847 if ( pSocket == 0 )
1848 {
1849 return 0;
1850 }
1851
1852 pSocket->m_nLastError=0;
1853#if defined(LINUX1)
1854 pSocket->m_bIsAccepting = sal_True((sal_Bool)1);
1855#endif /* LINUX */
1856
1857 if( ppAddr && *ppAddr )
1858 {
1859 osl_destroySocketAddr( *ppAddr );
1860 *ppAddr = 0;
1861 }
1862
1863 /* prevent Linux EINTR behaviour */
1864 do
1865 {
1866 Connection = accept(pSocket->m_Socket, &Addr, &AddrLen);
1867 } while (Connection == -1 && errno(*__errno_location ()) == EINTR4);
1868
1869
1870 /* accept failed? */
1871 if( Connection == OSL_SOCKET_ERROR-1 )
1872 {
1873 pSocket->m_nLastError=errno(*__errno_location ());
1874 OSL_TRACE("osl_acceptConnectionOnSocket : accept error '%s'",strerror(errno))do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1874" ": "), "osl_acceptConnectionOnSocket : accept error '%s'"
,strerror((*__errno_location ()))); } } while (((sal_Bool)0))
;
1875
1876#if defined(LINUX1)
1877 pSocket->m_bIsAccepting = sal_False((sal_Bool)0);
1878#endif /* LINUX */
1879 return 0;
1880 }
1881
1882 OSL_ASSERT(AddrLen == sizeof(struct sockaddr))do { if (((sal_Bool)1) && (!(AddrLen == sizeof(struct
sockaddr)))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN
), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1882" ": "), "OSL_ASSERT: %s", "AddrLen == sizeof(struct sockaddr)"
); } } while (((sal_Bool)0))
;
1883
1884
1885#if defined(LINUX1)
1886 if ( pSocket->m_bIsInShutdown == sal_True((sal_Bool)1) )
1887 {
1888 close(Connection);
1889 OSL_TRACE("osl_acceptConnectionOnSocket : close while accept")do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1889" ": "), "osl_acceptConnectionOnSocket : close while accept"
); } } while (((sal_Bool)0))
;
1890 return 0;
1891 }
1892#endif /* LINUX */
1893
1894
1895 if(ppAddr)
1896 {
1897 *ppAddr= __osl_createSocketAddrFromSystem(&Addr);
1898 }
1899
1900 /* alloc memory */
1901 pConnectionSockImpl= __osl_createSocketImpl(OSL_INVALID_SOCKET-1);
1902
1903 /* set close-on-exec flag */
1904 if ((Flags = fcntl(Connection, F_GETFD1, 0)) != -1)
1905 {
1906 Flags |= FD_CLOEXEC1;
1907 if (fcntl(Connection, F_SETFD2, Flags) == -1)
1908 {
1909 pSocket->m_nLastError=errno(*__errno_location ());
1910 OSL_TRACE("osl_acceptConnectionOnSocket failed changing socket flags. Errno: %d (%s)\n",do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1912" ": "), "osl_acceptConnectionOnSocket failed changing socket flags. Errno: %d (%s)\n"
, (*__errno_location ()), strerror((*__errno_location ()))); }
} while (((sal_Bool)0))
1911 errno,do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1912" ": "), "osl_acceptConnectionOnSocket failed changing socket flags. Errno: %d (%s)\n"
, (*__errno_location ()), strerror((*__errno_location ()))); }
} while (((sal_Bool)0))
1912 strerror(errno))do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1912" ": "), "osl_acceptConnectionOnSocket failed changing socket flags. Errno: %d (%s)\n"
, (*__errno_location ()), strerror((*__errno_location ()))); }
} while (((sal_Bool)0))
;
1913 }
1914
1915 }
1916
1917 pConnectionSockImpl->m_Socket = Connection;
1918 pConnectionSockImpl->m_nLastError = 0;
1919 pConnectionSockImpl->m_CloseCallback = NULL((void*)0);
1920 pConnectionSockImpl->m_CallbackArg = NULL((void*)0);
1921#if defined(LINUX1)
1922 pConnectionSockImpl->m_bIsAccepting = sal_False((sal_Bool)0);
1923
1924 pSocket->m_bIsAccepting = sal_False((sal_Bool)0);
1925#endif /* LINUX */
1926 return pConnectionSockImpl;
1927}
1928
1929/*****************************************************************************/
1930/* osl_receiveSocket */
1931/*****************************************************************************/
1932sal_Int32 SAL_CALL osl_receiveSocket(oslSocket pSocket,
1933 void* pBuffer,
1934 sal_uInt32 BytesToRead,
1935 oslSocketMsgFlag Flag)
1936{
1937 int nRead;
1938
1939 OSL_ASSERT(pSocket)do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1939" ": "), "OSL_ASSERT: %s", "pSocket"); } } while ((
(sal_Bool)0))
;
1940 if ( pSocket == 0 )
1941 {
1942 OSL_TRACE("osl_receiveSocket : Invalid socket")do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1942" ": "), "osl_receiveSocket : Invalid socket"); } }
while (((sal_Bool)0))
;
1943 return -1;
1944 }
1945
1946 pSocket->m_nLastError=0;
1947
1948 do
1949 {
1950 nRead = recv(pSocket->m_Socket,
1951 (sal_Char*)pBuffer,
1952 BytesToRead,
1953 MSG_FLAG_TO_NATIVE(Flag)SocketMsgFlagMap[Flag]);
1954 } while ( nRead < 0 && errno(*__errno_location ()) == EINTR4 );
1955
1956 if ( nRead < 0 )
1957 {
1958 pSocket->m_nLastError=errno(*__errno_location ());
1959 OSL_TRACE("osl_receiveSocket failed : %i '%s'",nRead,strerror(errno))do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1959" ": "), "osl_receiveSocket failed : %i '%s'",nRead
,strerror((*__errno_location ()))); } } while (((sal_Bool)0))
;
1960 }
1961 else if ( nRead == 0 )
1962 {
1963 OSL_TRACE("osl_receiveSocket failed : %i '%s'",nRead,"EOL")do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1963" ": "), "osl_receiveSocket failed : %i '%s'",nRead
,"EOL"); } } while (((sal_Bool)0))
;
1964 }
1965
1966 return nRead;
1967}
1968
1969
1970/*****************************************************************************/
1971/* osl_receiveFromSocket */
1972/*****************************************************************************/
1973sal_Int32 SAL_CALL osl_receiveFromSocket(oslSocket pSocket,
1974 oslSocketAddr pSenderAddr,
1975 void* pBuffer,
1976 sal_uInt32 BufferSize,
1977 oslSocketMsgFlag Flag)
1978{
1979 int nRead;
1980 struct sockaddr *pSystemSockAddr = 0;
1981 socklen_t AddrLen = 0;
1982 if( pSenderAddr )
1983 {
1984 AddrLen = sizeof( struct sockaddr );
1985 pSystemSockAddr = &(pSenderAddr->m_sockaddr);
1986 }
1987
1988 OSL_ASSERT(pSocket)do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1988" ": "), "OSL_ASSERT: %s", "pSocket"); } } while ((
(sal_Bool)0))
;
1989 if ( pSocket == 0 )
1990 {
1991 OSL_TRACE("osl_receiveFromSocket : Invalid socket")do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "1991" ": "), "osl_receiveFromSocket : Invalid socket");
} } while (((sal_Bool)0))
;
1992 return -1;
1993 }
1994
1995 pSocket->m_nLastError=0;
1996
1997 nRead = recvfrom(pSocket->m_Socket,
1998 (sal_Char*)pBuffer,
1999 BufferSize,
2000 MSG_FLAG_TO_NATIVE(Flag)SocketMsgFlagMap[Flag],
2001 pSystemSockAddr,
2002 &AddrLen);
2003
2004 if ( nRead < 0 )
2005 {
2006 pSocket->m_nLastError=errno(*__errno_location ());
2007 OSL_TRACE("osl_receiveFromSocket failed : %i '%s'",nRead,strerror(errno))do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2007" ": "), "osl_receiveFromSocket failed : %i '%s'",nRead
,strerror((*__errno_location ()))); } } while (((sal_Bool)0))
;
2008 }
2009 else if ( nRead == 0 )
2010 {
2011 OSL_TRACE("osl_receiveSocket failed : %i '%s'",nRead,"EOL")do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2011" ": "), "osl_receiveSocket failed : %i '%s'",nRead
,"EOL"); } } while (((sal_Bool)0))
;
2012 }
2013
2014 return nRead;
2015}
2016
2017
2018/*****************************************************************************/
2019/* osl_sendSocket */
2020/*****************************************************************************/
2021sal_Int32 SAL_CALL osl_sendSocket(oslSocket pSocket,
2022 const void* pBuffer,
2023 sal_uInt32 BytesToSend,
2024 oslSocketMsgFlag Flag)
2025{
2026 int nWritten;
2027
2028 OSL_ASSERT(pSocket)do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2028" ": "), "OSL_ASSERT: %s", "pSocket"); } } while ((
(sal_Bool)0))
;
2029 if ( pSocket == 0 )
2030 {
2031 OSL_TRACE("osl_sendSocket : Invalid socket")do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2031" ": "), "osl_sendSocket : Invalid socket"); } } while
(((sal_Bool)0))
;
2032 return -1;
2033 }
2034
2035 pSocket->m_nLastError=0;
2036
2037 do
2038 {
2039 nWritten = send(pSocket->m_Socket,
2040 (sal_Char*)pBuffer,
2041 BytesToSend,
2042 MSG_FLAG_TO_NATIVE(Flag)SocketMsgFlagMap[Flag]);
2043 } while ( nWritten < 0 && errno(*__errno_location ()) == EINTR4 );
2044
2045
2046 if ( nWritten < 0 )
2047 {
2048 pSocket->m_nLastError=errno(*__errno_location ());
2049 OSL_TRACE("osl_sendSocket failed : %i '%s'",nWritten,strerror(errno))do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2049" ": "), "osl_sendSocket failed : %i '%s'",nWritten
,strerror((*__errno_location ()))); } } while (((sal_Bool)0))
;
2050 }
2051 else if ( nWritten == 0 )
2052 {
2053 OSL_TRACE("osl_sendSocket failed : %i '%s'",nWritten,"EOL")do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2053" ": "), "osl_sendSocket failed : %i '%s'",nWritten
,"EOL"); } } while (((sal_Bool)0))
;
2054 }
2055
2056 return nWritten;
2057}
2058
2059/*****************************************************************************/
2060/* osl_sendToSocket */
2061/*****************************************************************************/
2062sal_Int32 SAL_CALL osl_sendToSocket(oslSocket pSocket,
2063 oslSocketAddr ReceiverAddr,
2064 const void* pBuffer,
2065 sal_uInt32 BytesToSend,
2066 oslSocketMsgFlag Flag)
2067{
2068 int nWritten;
2069
2070 struct sockaddr *pSystemSockAddr = 0;
2071 int AddrLen = 0;
2072 if( ReceiverAddr )
2073 {
2074 pSystemSockAddr = &(ReceiverAddr->m_sockaddr);
2075 AddrLen = sizeof( struct sockaddr );
2076 }
2077
2078 OSL_ASSERT(pSocket)do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2078" ": "), "OSL_ASSERT: %s", "pSocket"); } } while ((
(sal_Bool)0))
;
2079 if ( pSocket == 0 )
2080 {
2081 OSL_TRACE("osl_sendToSocket : Invalid socket")do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2081" ": "), "osl_sendToSocket : Invalid socket"); } } while
(((sal_Bool)0))
;
2082 return -1;
2083 }
2084
2085 pSocket->m_nLastError=0;
2086
2087 /* ReceiverAddr might be 0 when used on a connected socket. */
2088 /* Then sendto should behave like send. */
2089
2090 nWritten = sendto(pSocket->m_Socket,
2091 (sal_Char*)pBuffer,
2092 BytesToSend,
2093 MSG_FLAG_TO_NATIVE(Flag)SocketMsgFlagMap[Flag],
2094 pSystemSockAddr,
2095 AddrLen);
2096
2097 if ( nWritten < 0 )
2098 {
2099 pSocket->m_nLastError=errno(*__errno_location ());
2100 OSL_TRACE("osl_sendToSocket failed : %i '%s'",nWritten,strerror(errno))do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2100" ": "), "osl_sendToSocket failed : %i '%s'",nWritten
,strerror((*__errno_location ()))); } } while (((sal_Bool)0))
;
2101 }
2102 else if ( nWritten == 0 )
2103 {
2104 OSL_TRACE("osl_sendToSocket failed : %i '%s'",nWritten,"EOL")do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2104" ": "), "osl_sendToSocket failed : %i '%s'",nWritten
,"EOL"); } } while (((sal_Bool)0))
;
2105 }
2106
2107 return nWritten;
2108}
2109
2110/*****************************************************************************/
2111/* osl_readSocket */
2112/*****************************************************************************/
2113sal_Int32 SAL_CALL osl_readSocket (
2114 oslSocket pSocket, void *pBuffer, sal_Int32 n )
2115{
2116 sal_uInt8 * Ptr = (sal_uInt8 *)pBuffer;
2117 sal_uInt32 BytesRead= 0;
2118 sal_uInt32 BytesToRead= n;
2119
2120 OSL_ASSERT( pSocket)do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2120" ": "), "OSL_ASSERT: %s", "pSocket"); } } while ((
(sal_Bool)0))
;
2121
2122 /* loop until all desired bytes were read or an error occurred */
2123 while (BytesToRead > 0)
2124 {
2125 sal_Int32 RetVal;
2126 RetVal= osl_receiveSocket(pSocket,
2127 Ptr,
2128 BytesToRead,
2129 osl_Socket_MsgNormal);
2130
2131 /* error occurred? */
2132 if(RetVal <= 0)
2133 {
2134 break;
2135 }
2136
2137 BytesToRead -= RetVal;
2138 BytesRead += RetVal;
2139 Ptr += RetVal;
2140 }
2141
2142 return BytesRead;
2143}
2144
2145/*****************************************************************************/
2146/* osl_writeSocket */
2147/*****************************************************************************/
2148sal_Int32 SAL_CALL osl_writeSocket(
2149 oslSocket pSocket, const void *pBuffer, sal_Int32 n )
2150{
2151 /* loop until all desired bytes were send or an error occurred */
2152 sal_uInt32 BytesSend= 0;
2153 sal_uInt32 BytesToSend= n;
2154 sal_uInt8 *Ptr = ( sal_uInt8 * )pBuffer;
2155
2156 OSL_ASSERT( pSocket )do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2156" ": "), "OSL_ASSERT: %s", "pSocket"); } } while ((
(sal_Bool)0))
;
2157
2158 while (BytesToSend > 0)
2159 {
2160 sal_Int32 RetVal;
2161
2162 RetVal= osl_sendSocket( pSocket,Ptr,BytesToSend,osl_Socket_MsgNormal);
2163
2164 /* error occurred? */
2165 if(RetVal <= 0)
2166 {
2167 break;
2168 }
2169
2170 BytesToSend -= RetVal;
2171 BytesSend += RetVal;
2172 Ptr += RetVal;
2173
2174 }
2175 return BytesSend;
2176}
2177
2178/*****************************************************************************/
2179/* __osl_socket_poll */
2180/*****************************************************************************/
2181
2182#ifdef HAVE_POLL_H /* poll() */
2183
2184sal_Bool __osl_socket_poll (
2185 oslSocket pSocket,
2186 const TimeValue* pTimeout,
2187 short nEvent)
2188{
2189 struct pollfd fds;
2190 int timeout;
2191 int result;
2192
2193 OSL_ASSERT(0 != pSocket)do { if (((sal_Bool)1) && (!(0 != pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2193" ": "), "OSL_ASSERT: %s", "0 != pSocket"); } } while
(((sal_Bool)0))
;
2194 if (0 == pSocket)
2195 return sal_False((sal_Bool)0); /* EINVAL */
2196
2197 pSocket->m_nLastError = 0;
2198
2199 fds.fd = pSocket->m_Socket;
2200 fds.events = nEvent;
2201 fds.revents = 0;
2202
2203 timeout = -1;
2204 if (pTimeout)
2205 {
2206 /* Convert to [ms] */
2207 timeout = pTimeout->Seconds * 1000;
2208 timeout += pTimeout->Nanosec / (1000 * 1000);
2209 }
2210
2211 result = poll (&fds, 1, timeout);
2212 if (result < 0)
2213 {
2214 pSocket->m_nLastError = errno(*__errno_location ());
2215 OSL_TRACE("__osl_socket_poll(): poll error: %d (%s)",do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2216" ": "), "__osl_socket_poll(): poll error: %d (%s)"
, (*__errno_location ()), strerror((*__errno_location ()))); }
} while (((sal_Bool)0))
2216 errno, strerror(errno))do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2216" ": "), "__osl_socket_poll(): poll error: %d (%s)"
, (*__errno_location ()), strerror((*__errno_location ()))); }
} while (((sal_Bool)0))
;
2217 return sal_False((sal_Bool)0);
2218 }
2219 if (result == 0)
2220 {
2221 /* Timeout */
2222 return sal_False((sal_Bool)0);
2223 }
2224
2225 return ((fds.revents & nEvent) == nEvent);
2226}
2227
2228#else /* select() */
2229
2230sal_Bool __osl_socket_poll (
2231 oslSocket pSocket,
2232 const TimeValue* pTimeout,
2233 short nEvent)
2234{
2235 fd_set fds;
2236 struct timeval tv;
2237 int result;
2238
2239 OSL_ASSERT(0 != pSocket)do { if (((sal_Bool)1) && (!(0 != pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2239" ": "), "OSL_ASSERT: %s", "0 != pSocket"); } } while
(((sal_Bool)0))
;
2240 if (0 == pSocket)
2241 return sal_False((sal_Bool)0); /* EINVAL */
2242
2243 pSocket->m_nLastError = 0;
2244
2245 FD_ZERO(&fds)do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; stosl" :
"=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof
(__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"
); } while (0)
;
2246 FD_SET(pSocket->m_Socket, &fds)((void) (((&fds)->__fds_bits)[((pSocket->m_Socket) /
(8 * sizeof(unsigned long)))] |= ((__fd_mask) 1 << ((pSocket
->m_Socket) % (8 * sizeof(unsigned long))))))
;
2247
2248 if (pTimeout)
2249 {
2250 /* Convert to 'timeval' */
2251 tv.tv_sec = pTimeout->Seconds;
2252 tv.tv_usec = pTimeout->Nanosec / 1000;
2253 }
2254
2255 result = select (
2256 pSocket->m_Socket + 1,
2257 (nEvent == POLLIN0x001 ) ? PTR_FD_SET(fds)(&(fds)) : NULL((void*)0),
2258 (nEvent == POLLOUT0x004) ? PTR_FD_SET(fds)(&(fds)) : NULL((void*)0),
2259 (nEvent == POLLPRI0x002) ? PTR_FD_SET(fds)(&(fds)) : NULL((void*)0),
2260 (pTimeout) ? &tv : NULL((void*)0));
2261
2262 if (result < 0)
2263 {
2264 pSocket->m_nLastError = errno(*__errno_location ());
2265 OSL_TRACE("__osl_socket_poll(): select error: %d (%s)",do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2266" ": "), "__osl_socket_poll(): select error: %d (%s)"
, (*__errno_location ()), strerror((*__errno_location ()))); }
} while (((sal_Bool)0))
2266 errno, strerror(errno))do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2266" ": "), "__osl_socket_poll(): select error: %d (%s)"
, (*__errno_location ()), strerror((*__errno_location ()))); }
} while (((sal_Bool)0))
;
2267 return sal_False((sal_Bool)0);
2268 }
2269 if (result == 0)
2270 {
2271 /* Timeout */
2272 return sal_False((sal_Bool)0);
2273 }
2274
2275 return (FD_ISSET(pSocket->m_Socket, &fds)((((&fds)->__fds_bits)[((pSocket->m_Socket) / (8 * sizeof
(unsigned long)))] & ((__fd_mask) 1 << ((pSocket->
m_Socket) % (8 * sizeof(unsigned long))))) != 0)
? sal_True((sal_Bool)1) : sal_False((sal_Bool)0));
2276}
2277
2278#endif /* HAVE_POLL_H */
2279
2280/*****************************************************************************/
2281/* osl_isReceiveReady */
2282/*****************************************************************************/
2283sal_Bool SAL_CALL osl_isReceiveReady (
2284 oslSocket pSocket, const TimeValue* pTimeout)
2285{
2286 OSL_ASSERT(pSocket)do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2286" ": "), "OSL_ASSERT: %s", "pSocket"); } } while ((
(sal_Bool)0))
;
2287 if (pSocket == NULL((void*)0))
2288 {
2289 /* ENOTSOCK */
2290 return sal_False((sal_Bool)0);
2291 }
2292
2293 return __osl_socket_poll (pSocket, pTimeout, POLLIN0x001);
2294}
2295
2296/*****************************************************************************/
2297/* osl_isSendReady */
2298/*****************************************************************************/
2299sal_Bool SAL_CALL osl_isSendReady (
2300 oslSocket pSocket, const TimeValue* pTimeout)
2301{
2302 OSL_ASSERT(pSocket)do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2302" ": "), "OSL_ASSERT: %s", "pSocket"); } } while ((
(sal_Bool)0))
;
2303 if (pSocket == NULL((void*)0))
2304 {
2305 /* ENOTSOCK */
2306 return sal_False((sal_Bool)0);
2307 }
2308
2309 return __osl_socket_poll (pSocket, pTimeout, POLLOUT0x004);
2310}
2311
2312/*****************************************************************************/
2313/* osl_isExceptionPending */
2314/*****************************************************************************/
2315sal_Bool SAL_CALL osl_isExceptionPending (
2316 oslSocket pSocket, const TimeValue* pTimeout)
2317{
2318 OSL_ASSERT(pSocket)do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2318" ": "), "OSL_ASSERT: %s", "pSocket"); } } while ((
(sal_Bool)0))
;
2319 if (pSocket == NULL((void*)0))
2320 {
2321 /* ENOTSOCK */
2322 return sal_False((sal_Bool)0);
2323 }
2324
2325 return __osl_socket_poll (pSocket, pTimeout, POLLPRI0x002);
2326}
2327
2328/*****************************************************************************/
2329/* osl_shutdownSocket */
2330/*****************************************************************************/
2331sal_Bool SAL_CALL osl_shutdownSocket(oslSocket pSocket,
2332 oslSocketDirection Direction)
2333{
2334 int nRet;
2335
2336 OSL_ASSERT(pSocket)do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2336" ": "), "OSL_ASSERT: %s", "pSocket"); } } while ((
(sal_Bool)0))
;
2337 if ( pSocket == 0 )
2338 {
2339 return sal_False((sal_Bool)0);
2340 }
2341
2342 pSocket->m_nLastError=0;
2343
2344 nRet=shutdown(pSocket->m_Socket, DIRECTION_TO_NATIVE(Direction)SocketDirection[Direction]);
2345 if (nRet != 0 )
2346 {
2347 pSocket->m_nLastError=errno(*__errno_location ());
2348 OSL_TRACE("shutdown error '%s'",strerror(errno))do { if (((sal_Bool)1) && (1 > 0)) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_INFO), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2348" ": "), "shutdown error '%s'",strerror((*__errno_location
()))); } } while (((sal_Bool)0))
;
2349 }
2350 return (nRet==0);
2351}
2352
2353
2354/*****************************************************************************/
2355/* osl_getSocketOption */
2356/*****************************************************************************/
2357sal_Int32 SAL_CALL osl_getSocketOption(oslSocket pSocket,
2358 oslSocketOptionLevel Level,
2359 oslSocketOption Option,
2360 void* pBuffer,
2361 sal_uInt32 BufferLen)
2362{
2363 socklen_t nOptLen = (socklen_t) BufferLen;
2364
2365 OSL_ASSERT(pSocket)do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2365" ": "), "OSL_ASSERT: %s", "pSocket"); } } while ((
(sal_Bool)0))
;
2366 if ( pSocket == 0 )
2367 {
2368 return -1;
2369 }
2370
2371 pSocket->m_nLastError=0;
2372
2373 if(getsockopt(pSocket->m_Socket,
2374 OPTION_LEVEL_TO_NATIVE(Level)OptionLevelMap[Level],
2375 OPTION_TO_NATIVE(Option)OptionMap[Option],
2376 (sal_Char*)pBuffer,
2377 &nOptLen) == -1)
2378 {
2379 pSocket->m_nLastError=errno(*__errno_location ());
2380 return -1;
2381 }
2382
2383 return BufferLen;
2384}
2385
2386/*****************************************************************************/
2387/* osl_setSocketOption */
2388/*****************************************************************************/
2389sal_Bool SAL_CALL osl_setSocketOption(oslSocket pSocket,
2390 oslSocketOptionLevel Level,
2391 oslSocketOption Option,
2392 void* pBuffer,
2393 sal_uInt32 BufferLen)
2394{
2395 int nRet;
2396
2397 OSL_ASSERT(pSocket)do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2397" ": "), "OSL_ASSERT: %s", "pSocket"); } } while ((
(sal_Bool)0))
;
2398 if ( pSocket == 0 )
2399 {
2400 return sal_False((sal_Bool)0);
2401 }
2402
2403 pSocket->m_nLastError=0;
2404
2405 nRet = setsockopt(pSocket->m_Socket,
2406 OPTION_LEVEL_TO_NATIVE(Level)OptionLevelMap[Level],
2407 OPTION_TO_NATIVE(Option)OptionMap[Option],
2408 (sal_Char*)pBuffer,
2409 BufferLen);
2410
2411 if ( nRet < 0 )
2412 {
2413 pSocket->m_nLastError=errno(*__errno_location ());
2414 return sal_False((sal_Bool)0);
2415 }
2416
2417 return sal_True((sal_Bool)1);
2418}
2419
2420/*****************************************************************************/
2421/* osl_enableNonBlockingMode */
2422/*****************************************************************************/
2423sal_Bool SAL_CALL osl_enableNonBlockingMode(oslSocket pSocket,
2424 sal_Bool On)
2425{
2426 int flags;
2427 int nRet;
2428
2429 OSL_ASSERT(pSocket)do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2429" ": "), "OSL_ASSERT: %s", "pSocket"); } } while ((
(sal_Bool)0))
;
2430 if ( pSocket == 0 )
2431 {
2432 return sal_False((sal_Bool)0);
2433 }
2434
2435 pSocket->m_nLastError=0;
2436
2437 flags = fcntl(pSocket->m_Socket, F_GETFL3, 0);
2438
2439 if (On)
2440 flags |= O_NONBLOCK04000;
2441 else
2442 flags &= ~(O_NONBLOCK04000);
2443
2444 nRet = fcntl(pSocket->m_Socket, F_SETFL4, flags);
2445
2446 if ( nRet < 0 )
2447 {
2448 pSocket->m_nLastError=errno(*__errno_location ());
2449 return sal_False((sal_Bool)0);
2450 }
2451
2452 return sal_True((sal_Bool)1);
2453}
2454
2455/*****************************************************************************/
2456/* osl_isNonBlockingMode */
2457/*****************************************************************************/
2458sal_Bool SAL_CALL osl_isNonBlockingMode(oslSocket pSocket)
2459{
2460 int flags;
2461
2462 OSL_ASSERT(pSocket)do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2462" ": "), "OSL_ASSERT: %s", "pSocket"); } } while ((
(sal_Bool)0))
;
2463 if ( pSocket == 0 )
2464 {
2465 return sal_False((sal_Bool)0);
2466 }
2467
2468 pSocket->m_nLastError=0;
2469
2470 flags = fcntl(pSocket->m_Socket, F_GETFL3, 0);
2471
2472 if (flags == -1 || !(flags & O_NONBLOCK04000))
2473 return sal_False((sal_Bool)0);
2474 else
2475 return sal_True((sal_Bool)1);
2476}
2477
2478/*****************************************************************************/
2479/* osl_getSocketType */
2480/*****************************************************************************/
2481oslSocketType SAL_CALL osl_getSocketType(oslSocket pSocket)
2482{
2483 int Type=0;
2484 socklen_t TypeSize= sizeof(Type);
2485
2486 OSL_ASSERT(pSocket)do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2486" ": "), "OSL_ASSERT: %s", "pSocket"); } } while ((
(sal_Bool)0))
;
2487 if ( pSocket == 0 )
2488 {
2489 return osl_Socket_TypeInvalid;
2490 }
2491
2492 pSocket->m_nLastError=0;
2493
2494 if(getsockopt(pSocket->m_Socket,
2495 OPTION_LEVEL_TO_NATIVE(osl_Socket_LevelSocket)OptionLevelMap[osl_Socket_LevelSocket],
2496 OPTION_TO_NATIVE(osl_Socket_OptionType)OptionMap[osl_Socket_OptionType],
2497 (sal_Char*)&Type,
2498 &TypeSize) == -1)
2499 {
2500 /* error */
2501 pSocket->m_nLastError=errno(*__errno_location ());
2502 return osl_Socket_TypeInvalid;
2503 }
2504
2505 return TYPE_FROM_NATIVE(Type)osl_SocketTypeFromNative(Type);
2506
2507}
2508
2509/*****************************************************************************/
2510/* osl_getLastSocketErrorDescription */
2511/*****************************************************************************/
2512void SAL_CALL osl_getLastSocketErrorDescription(oslSocket Socket, rtl_uString **ustrError)
2513{
2514 sal_Char pszError[1024];
2515
2516 pszError[0] = '\0';
2517
2518 osl_psz_getLastSocketErrorDescription(Socket,pszError,sizeof(pszError));
2519
2520 rtl_uString_newFromAscii(ustrError,pszError);
2521
2522 return;
2523}
2524
2525
2526void SAL_CALL osl_psz_getLastSocketErrorDescription(oslSocket pSocket, sal_Char* pBuffer, sal_uInt32 BufferSize)
2527{
2528 /* make shure pBuffer will be a zero-terminated string even when strncpy has to cut */
2529 pBuffer[BufferSize-1]= '\0';
2530
2531 if ( pSocket == 0 )
2532 {
2533 strncpy(pBuffer, strerror(EINVAL22), BufferSize-1);
2534 return;
2535 }
2536
2537 strncpy(pBuffer, strerror(pSocket->m_nLastError), BufferSize-1);
2538 return;
2539}
2540
2541/*****************************************************************************/
2542/* osl_getLastSocketError */
2543/*****************************************************************************/
2544oslSocketError SAL_CALL osl_getLastSocketError(oslSocket pSocket)
2545{
2546 if ( pSocket == 0 )
2547 {
2548 return ERROR_FROM_NATIVE(EINVAL)osl_SocketErrorFromNative(22);
2549 }
2550
2551 return ERROR_FROM_NATIVE(pSocket->m_nLastError)osl_SocketErrorFromNative(pSocket->m_nLastError);
2552}
2553
2554/*****************************************************************************/
2555/* SocketSet */
2556/*****************************************************************************/
2557typedef struct _TSocketSetImpl
2558{
2559 int m_MaxHandle; /* for select(), the largest descriptor in the set */
2560 fd_set m_Set; /* the set of descriptors */
2561
2562} TSocketSetImpl;
2563
2564/*****************************************************************************/
2565/* osl_createSocketSet */
2566/*****************************************************************************/
2567oslSocketSet SAL_CALL osl_createSocketSet()
2568{
2569 TSocketSetImpl* pSet;
2570
2571 pSet= (TSocketSetImpl*)malloc(sizeof(TSocketSetImpl));
2572
2573 OSL_ASSERT(pSet)do { if (((sal_Bool)1) && (!(pSet))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2573" ": "), "OSL_ASSERT: %s", "pSet"); } } while (((sal_Bool
)0))
;
2574
2575 if(pSet)
2576 {
2577 pSet->m_MaxHandle= 0;
2578 FD_ZERO(&pSet->m_Set)do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; stosl" :
"=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof
(__fd_mask)), "1" (&((&pSet->m_Set)->__fds_bits
)[0]) : "memory"); } while (0)
;
2579 }
2580
2581 return (oslSocketSet)pSet;
2582}
2583
2584/*****************************************************************************/
2585/* osl_destroySocketSet */
2586/*****************************************************************************/
2587void SAL_CALL osl_destroySocketSet(oslSocketSet Set)
2588{
2589 if(Set)
2590 free(Set);
2591}
2592
2593/*****************************************************************************/
2594/* osl_clearSocketSet */
2595/*****************************************************************************/
2596void SAL_CALL osl_clearSocketSet(oslSocketSet Set)
2597{
2598 TSocketSetImpl* pSet;
2599 OSL_ASSERT(Set)do { if (((sal_Bool)1) && (!(Set))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2599" ": "), "OSL_ASSERT: %s", "Set"); } } while (((sal_Bool
)0))
;
2600 if ( Set == 0 )
2601 {
2602 return;
2603 }
2604
2605 pSet= (TSocketSetImpl*)Set;
2606 pSet->m_MaxHandle= 0;
2607
2608 FD_ZERO(&pSet->m_Set)do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; stosl" :
"=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof
(__fd_mask)), "1" (&((&pSet->m_Set)->__fds_bits
)[0]) : "memory"); } while (0)
;
2609}
2610
2611/*****************************************************************************/
2612/* osl_addToSocketSet */
2613/*****************************************************************************/
2614void SAL_CALL osl_addToSocketSet(oslSocketSet Set, oslSocket pSocket)
2615{
2616 TSocketSetImpl* pSet;
2617
2618 OSL_ASSERT(Set)do { if (((sal_Bool)1) && (!(Set))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2618" ": "), "OSL_ASSERT: %s", "Set"); } } while (((sal_Bool
)0))
;
2619 OSL_ASSERT(pSocket)do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2619" ": "), "OSL_ASSERT: %s", "pSocket"); } } while ((
(sal_Bool)0))
;
2620
2621 if ( Set == 0 || pSocket == 0)
2622 {
2623 return;
2624 }
2625
2626 pSet= (TSocketSetImpl*)Set;
2627
2628 /* correct max handle */
2629 if(pSocket->m_Socket > pSet->m_MaxHandle)
2630 pSet->m_MaxHandle= pSocket->m_Socket;
2631 FD_SET(pSocket->m_Socket, &pSet->m_Set)((void) (((&pSet->m_Set)->__fds_bits)[((pSocket->
m_Socket) / (8 * sizeof(unsigned long)))] |= ((__fd_mask) 1 <<
((pSocket->m_Socket) % (8 * sizeof(unsigned long))))))
;
2632
2633}
2634
2635/*****************************************************************************/
2636/* osl_removeFromSocketSet */
2637/*****************************************************************************/
2638void SAL_CALL osl_removeFromSocketSet(oslSocketSet Set, oslSocket pSocket)
2639{
2640 TSocketSetImpl* pSet;
2641
2642 OSL_ASSERT(Set)do { if (((sal_Bool)1) && (!(Set))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2642" ": "), "OSL_ASSERT: %s", "Set"); } } while (((sal_Bool
)0))
;
2643 OSL_ASSERT(pSocket)do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2643" ": "), "OSL_ASSERT: %s", "pSocket"); } } while ((
(sal_Bool)0))
;
2644
2645 if ( Set == 0 || pSocket == 0)
2646 {
2647 return;
2648 }
2649
2650 pSet= (TSocketSetImpl*)Set;
2651
2652 /* correct max handle */
2653 if(pSocket->m_Socket == pSet->m_MaxHandle)
2654 {
2655 /* not optimal, since the next used descriptor might be */
2656 /* much smaller than m_Socket-1, but it will do */
2657 pSet->m_MaxHandle--;
2658 if(pSet->m_MaxHandle < 0)
2659 {
2660 pSet->m_MaxHandle= 0; /* avoid underflow */
2661 }
2662 }
2663
2664 FD_CLR(pSocket->m_Socket, &pSet->m_Set)((void) (((&pSet->m_Set)->__fds_bits)[((pSocket->
m_Socket) / (8 * sizeof(unsigned long)))] &= ~((__fd_mask
) 1 << ((pSocket->m_Socket) % (8 * sizeof(unsigned long
))))))
;
2665}
2666
2667/*****************************************************************************/
2668/* osl_isInSocketSet */
2669/*****************************************************************************/
2670sal_Bool SAL_CALL osl_isInSocketSet(oslSocketSet Set, oslSocket pSocket)
2671{
2672 TSocketSetImpl* pSet;
2673
2674 OSL_ASSERT(Set)do { if (((sal_Bool)1) && (!(Set))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2674" ": "), "OSL_ASSERT: %s", "Set"); } } while (((sal_Bool
)0))
;
2675 OSL_ASSERT(pSocket)do { if (((sal_Bool)1) && (!(pSocket))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/sal/osl/unx/socket.c"
":" "2675" ": "), "OSL_ASSERT: %s", "pSocket"); } } while ((
(sal_Bool)0))
;
2676 if ( Set == 0 || pSocket == 0 )
2677 {
2678 return sal_False((sal_Bool)0);
2679 }
2680
2681 pSet= (TSocketSetImpl*)Set;
2682
2683 return (FD_ISSET(pSocket->m_Socket, &pSet->m_Set)((((&pSet->m_Set)->__fds_bits)[((pSocket->m_Socket
) / (8 * sizeof(unsigned long)))] & ((__fd_mask) 1 <<
((pSocket->m_Socket) % (8 * sizeof(unsigned long))))) != 0
)
!= 0);
2684}
2685
2686/*****************************************************************************/
2687/* osl_demultiplexSocketEvents */
2688/*****************************************************************************/
2689sal_Int32 SAL_CALL osl_demultiplexSocketEvents(oslSocketSet IncomingSet,
2690 oslSocketSet OutgoingSet,
2691 oslSocketSet OutOfBandSet,
2692 const TimeValue* pTimeout)
2693{
2694 int MaxHandle= 0;
2695 struct timeval tv;
2696 TSocketSetImpl* pInSet;
2697 TSocketSetImpl* pOutSet;
2698 TSocketSetImpl* pOOBSet;
2699
2700 if (pTimeout)
2701 {
2702 /* non-blocking call */
2703 tv.tv_sec = pTimeout->Seconds;
2704 tv.tv_usec = pTimeout->Nanosec / 1000L;
2705 }
2706
2707 /* map opaque data to impl-types */
2708 pInSet= (TSocketSetImpl*)IncomingSet;
2709 pOutSet= (TSocketSetImpl*)OutgoingSet;
2710 pOOBSet= (TSocketSetImpl*)OutOfBandSet;
2711
2712 /* get max handle from all sets */
2713 if (pInSet)
2714 MaxHandle= pInSet->m_MaxHandle;
2715
2716 if (pOutSet && (pOutSet->m_MaxHandle > MaxHandle))
2717 MaxHandle= pOutSet->m_MaxHandle;
2718
2719 if (pOOBSet && (pOOBSet->m_MaxHandle > MaxHandle))
2720 MaxHandle= pOOBSet->m_MaxHandle;
2721
2722 return select(MaxHandle+1,
2723 pInSet ? PTR_FD_SET(pInSet->m_Set)(&(pInSet->m_Set)) : 0,
2724 pOutSet ? PTR_FD_SET(pOutSet->m_Set)(&(pOutSet->m_Set)) : 0,
2725 pOOBSet ? PTR_FD_SET(pOOBSet->m_Set)(&(pOOBSet->m_Set)) : 0,
2726 pTimeout ? &tv : 0);
2727}
2728
2729/* vim:set shiftwidth=4 softtabstop=4 expandtab: */