Get the style color and number just once
[LibreOffice.git] / sal / osl / w32 / socket.cxx
blobbc5a50dfd99b12b40666fb8b7c31bafd3859056f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <utility>
24 #include "system.h"
26 #include <osl/socket.h>
27 #include <osl/thread.h>
28 #include <osl/diagnose.h>
29 #include <rtl/alloc.h>
30 #include <rtl/byteseq.h>
31 #include <sal/log.hxx>
32 #include <o3tl/char16_t2wchar_t.hxx>
33 #include <o3tl/safeint.hxx>
34 #include <comphelper/windowserrorstring.hxx>
36 #include "sockimpl.hxx"
39 oslSocketAddr is a pointer to a Berkeley struct sockaddr.
40 I refrained from using sockaddr_in because of possible further
41 extensions of this socket-interface (IP-NG?).
42 The intention was to hide all Berkeley data-structures from
43 direct access past the osl-interface.
45 The current implementation is internet (IP) centered. All
46 the constructor-functions (osl_create...) take parameters
47 that will probably make sense only in the IP-environment
48 (e.g. because of using the dotted-Addr-format).
50 If the interface will be extended to host other protocol-
51 families, I expect no externally visible changes in the
52 existing functions. You'll probably need only new
53 constructor-functions who take the different Addr
54 formats into consideration (maybe a long dotted Addr
55 or whatever).
59 _Note_ that I rely on the fact that oslSocketAddr and struct sockaddr
60 are the same! I don't like it very much but see no other easy way to
61 conceal the struct sockaddr from the eyes of the user.
64 #define OSL_INVALID_SOCKET INVALID_SOCKET /* WIN32 */
65 #define OSL_SOCKET_ERROR SOCKET_ERROR /* WIN32 */
67 static DWORD FamilyMap[]= {
68 AF_INET, /* osl_Socket_FamilyInet */
69 AF_IPX, /* osl_Socket_FamilyIpx */
70 0 /* osl_Socket_FamilyInvalid */
73 static oslAddrFamily osl_AddrFamilyFromNative(DWORD nativeType)
75 oslAddrFamily i= oslAddrFamily(0);
76 while(i != osl_Socket_FamilyInvalid)
78 if(FamilyMap[i] == nativeType)
79 return i;
80 i = static_cast<oslAddrFamily>( static_cast<int>(i) + 1);
82 return i;
85 #define FAMILY_FROM_NATIVE(y) osl_AddrFamilyFromNative(y)
86 #define FAMILY_TO_NATIVE(x) static_cast<short>(FamilyMap[x])
88 static DWORD ProtocolMap[]= {
89 0, /* osl_Socket_FamilyInet */
90 NSPROTO_IPX, /* osl_Socket_FamilyIpx */
91 NSPROTO_SPX, /* osl_Socket_ProtocolSpx */
92 NSPROTO_SPXII, /* osl_Socket_ProtocolSpx_ii */
93 0 /* osl_Socket_ProtocolInvalid */
96 #define PROTOCOL_TO_NATIVE(x) ProtocolMap[x]
98 static DWORD TypeMap[]= {
99 SOCK_STREAM, /* osl_Socket_TypeStream */
100 SOCK_DGRAM, /* osl_Socket_TypeDgram */
101 SOCK_RAW, /* osl_Socket_TypeRaw */
102 SOCK_RDM, /* osl_Socket_TypeRdm */
103 SOCK_SEQPACKET, /* osl_Socket_TypeSeqPacket */
104 0 /* osl_Socket_TypeInvalid */
107 static oslSocketType osl_SocketTypeFromNative(DWORD nativeType)
109 oslSocketType i= oslSocketType(0);
110 while(i != osl_Socket_TypeInvalid)
112 if(TypeMap[i] == nativeType)
113 return i;
114 i = static_cast<oslSocketType>(static_cast<int>(i)+1);
116 return i;
119 #define TYPE_TO_NATIVE(x) TypeMap[x]
120 #define TYPE_FROM_NATIVE(y) osl_SocketTypeFromNative(y)
122 static DWORD OptionMap[]= {
123 SO_DEBUG, /* osl_Socket_OptionDebug */
124 SO_ACCEPTCONN, /* osl_Socket_OptionAcceptConn */
125 SO_REUSEADDR, /* osl_Socket_OptionReuseAddr */
126 SO_KEEPALIVE, /* osl_Socket_OptionKeepAlive */
127 SO_DONTROUTE, /* osl_Socket_OptionDontRoute */
128 SO_BROADCAST, /* osl_Socket_OptionBroadcast */
129 SO_USELOOPBACK, /* osl_Socket_OptionUseLoopback */
130 SO_LINGER, /* osl_Socket_OptionLinger */
131 SO_OOBINLINE, /* osl_Socket_OptionOOBinLine */
132 SO_SNDBUF, /* osl_Socket_OptionSndBuf */
133 SO_RCVBUF, /* osl_Socket_OptionRcvBuf */
134 SO_SNDLOWAT, /* osl_Socket_OptionSndLowat */
135 SO_RCVLOWAT, /* osl_Socket_OptionRcvLowat */
136 SO_SNDTIMEO, /* osl_Socket_OptionSndTimeo */
137 SO_RCVTIMEO, /* osl_Socket_OptionRcvTimeo */
138 SO_ERROR, /* osl_Socket_OptionError */
139 SO_TYPE, /* osl_Socket_OptionType */
140 TCP_NODELAY, /* osl_Socket_OptionTcpNoDelay */
141 0 /* osl_Socket_OptionInvalid */
144 #define OPTION_TO_NATIVE(x) OptionMap[x]
146 static DWORD OptionLevelMap[]= {
147 SOL_SOCKET, /* osl_Socket_LevelSocket */
148 IPPROTO_TCP, /* osl_Socket_LevelTcp */
149 0 /* osl_invalid_SocketLevel */
152 #define OPTION_LEVEL_TO_NATIVE(x) OptionLevelMap[x]
154 static DWORD SocketMsgFlagMap[]= {
155 0, /* osl_Socket_MsgNormal */
156 MSG_OOB, /* osl_Socket_MsgOOB */
157 MSG_PEEK, /* osl_Socket_MsgPeek */
158 MSG_DONTROUTE, /* osl_Socket_MsgDontRoute */
159 MSG_MAXIOVLEN /* osl_Socket_MsgMaxIOVLen */
162 #define MSG_FLAG_TO_NATIVE(x) SocketMsgFlagMap[x]
164 static DWORD SocketDirection[]= {
165 SD_RECEIVE, /* osl_Socket_DirRead */
166 SD_SEND, /* osl_Socket_DirWrite */
167 SD_BOTH /* osl_Socket_DirReadwrite */
170 #define DIRECTION_TO_NATIVE(x) SocketDirection[x]
172 static int SocketError[]= {
173 0, /* no error */
174 WSAENOTSOCK, /* Socket operation on non-socket */
175 WSAEDESTADDRREQ, /* Destination address required */
176 WSAEMSGSIZE, /* Message too long */
177 WSAEPROTOTYPE, /* Protocol wrong type for socket */
178 WSAENOPROTOOPT, /* Protocol not available */
179 WSAEPROTONOSUPPORT, /* Protocol not supported */
180 WSAESOCKTNOSUPPORT, /* Socket type not supported */
181 WSAEOPNOTSUPP, /* Operation not supported on socket */
182 WSAEPFNOSUPPORT, /* Protocol family not supported */
183 WSAEAFNOSUPPORT, /* Address family not supported by protocol family */
184 WSAEADDRINUSE, /* Address already in use */
185 WSAEADDRNOTAVAIL, /* Can't assign requested address */
186 WSAENETDOWN, /* Network is down */
187 WSAENETUNREACH, /* Network is unreachable */
188 WSAENETRESET, /* Network dropped connection because of reset */
189 WSAECONNABORTED, /* Software caused connection abort */
190 WSAECONNRESET, /* Connection reset by peer */
191 WSAENOBUFS, /* No buffer space available */
192 WSAEISCONN, /* Socket is already connected */
193 WSAENOTCONN, /* Socket is not connected */
194 WSAESHUTDOWN, /* Can't send after socket shutdown */
195 WSAETOOMANYREFS, /* Too many references: can't splice */
196 WSAETIMEDOUT, /* Connection timed out */
197 WSAECONNREFUSED, /* Connection refused */
198 WSAEHOSTDOWN, /* Host is down */
199 WSAEHOSTUNREACH, /* No route to host */
200 WSAEWOULDBLOCK, /* call would block on non-blocking socket */
201 WSAEALREADY, /* operation already in progress */
202 WSAEINPROGRESS /* operation now in progress */
205 static oslSocketError osl_SocketErrorFromNative(int nativeType)
207 oslSocketError i= oslSocketError(0);
209 while(i != osl_Socket_E_InvalidError)
211 if(SocketError[i] == nativeType)
212 return i;
214 i = static_cast<oslSocketError>( static_cast<int>(i) + 1);
216 return i;
219 #define ERROR_FROM_NATIVE(y) osl_SocketErrorFromNative(y)
221 #if OSL_DEBUG_LEVEL > 0
222 static sal_uInt32 g_nSocketAddr = 0;
224 namespace {
226 struct LeakWarning
228 ~LeakWarning()
230 SAL_WARN_IF( g_nSocketAddr, "sal.osl", "sal_socket: " << g_nSocketAddr << " socket address instances leak" );
236 static LeakWarning socketWarning;
237 #endif
239 static oslSocket createSocketImpl(SOCKET Socket)
241 oslSocket pSockImpl = static_cast<oslSocket>(rtl_allocateZeroMemory( sizeof(struct oslSocketImpl)));
242 pSockImpl->m_Socket = Socket;
243 pSockImpl->m_nRefCount = 1;
244 return pSockImpl;
247 static void destroySocketImpl(oslSocketImpl *pImpl)
249 if (pImpl)
251 free (pImpl);
255 static oslSocketAddr createSocketAddr( )
257 oslSocketAddr pAddr = static_cast<oslSocketAddr>(rtl_allocateZeroMemory( sizeof( struct oslSocketAddrImpl )));
258 pAddr->m_nRefCount = 1;
259 #if OSL_DEBUG_LEVEL > 0
260 g_nSocketAddr ++;
261 #endif
262 return pAddr;
265 static oslSocketAddr createSocketAddrWithFamily(
266 oslAddrFamily family, sal_Int32 port, sal_uInt32 nAddr )
268 OSL_ASSERT( family == osl_Socket_FamilyInet );
270 oslSocketAddr pAddr = createSocketAddr();
271 switch( family )
273 case osl_Socket_FamilyInet:
275 struct sockaddr_in* pInetAddr= reinterpret_cast<struct sockaddr_in*>(&pAddr->m_sockaddr);
277 pInetAddr->sin_family = FAMILY_TO_NATIVE(osl_Socket_FamilyInet);
278 pInetAddr->sin_addr.s_addr = nAddr;
279 pInetAddr->sin_port = static_cast<sal_uInt16>(port&0xffff);
280 break;
282 default:
283 pAddr->m_sockaddr.sa_family = FAMILY_TO_NATIVE(family);
285 return pAddr;
288 static oslSocketAddr createSocketAddFromSystem( struct sockaddr *pSystemSockAddr )
290 oslSocketAddr pAddr = createSocketAddr();
291 memcpy( &(pAddr->m_sockaddr), pSystemSockAddr, sizeof( sockaddr ) );
292 return pAddr;
295 static void destroySocketAddr( oslSocketAddr addr )
297 #if OSL_DEBUG_LEVEL > 0
298 g_nSocketAddr --;
299 #endif
300 free( addr );
303 oslSocketAddr SAL_CALL osl_createEmptySocketAddr(oslAddrFamily Family)
305 oslSocketAddr pAddr = nullptr;
307 /* is it an internet-Addr? */
308 if (Family == osl_Socket_FamilyInet)
309 pAddr = createSocketAddrWithFamily(Family, 0 , htonl(INADDR_ANY) );
310 else
311 pAddr = createSocketAddrWithFamily( Family , 0 , 0 );
313 return pAddr;
316 /** @deprecated, to be removed */
317 oslSocketAddr SAL_CALL osl_copySocketAddr(oslSocketAddr Addr)
319 oslSocketAddr pCopy = nullptr;
320 if (Addr)
322 pCopy = createSocketAddr();
324 if (pCopy)
325 memcpy(&(pCopy->m_sockaddr),&(Addr->m_sockaddr), sizeof(struct sockaddr));
327 return pCopy;
330 sal_Bool SAL_CALL osl_isEqualSocketAddr(oslSocketAddr Addr1, oslSocketAddr Addr2)
332 assert(Addr1);
333 assert(Addr2);
334 struct sockaddr* pAddr1= &(Addr1->m_sockaddr);
335 struct sockaddr* pAddr2= &(Addr2->m_sockaddr);
337 assert(pAddr1);
338 assert(pAddr2);
340 if (pAddr1->sa_family == pAddr2->sa_family)
342 switch (pAddr1->sa_family)
344 case AF_INET:
346 struct sockaddr_in* pInetAddr1= reinterpret_cast<struct sockaddr_in*>(pAddr1);
347 struct sockaddr_in* pInetAddr2= reinterpret_cast<struct sockaddr_in*>(pAddr2);
349 if ((pInetAddr1->sin_family == pInetAddr2->sin_family) &&
350 (pInetAddr1->sin_addr.s_addr == pInetAddr2->sin_addr.s_addr) &&
351 (pInetAddr1->sin_port == pInetAddr2->sin_port))
352 return true;
353 [[fallthrough]];
356 default:
358 return (memcmp(pAddr1, pAddr2, sizeof(struct sockaddr)) == 0);
363 return false;
366 oslSocketAddr SAL_CALL osl_createInetBroadcastAddr (
367 rtl_uString *strDottedAddr,
368 sal_Int32 Port)
370 sal_uInt32 nAddr = OSL_INADDR_NONE;
372 if (strDottedAddr && strDottedAddr->length)
374 IN_ADDR addr;
375 INT ret = InetPtonW(AF_INET, o3tl::toW(strDottedAddr->buffer), & addr);
376 if (1 == ret)
378 nAddr = addr.S_un.S_addr;
382 if (nAddr != OSL_INADDR_NONE)
384 /* Limited broadcast */
385 nAddr = ntohl(nAddr);
386 if (IN_CLASSA(nAddr))
388 nAddr &= IN_CLASSA_NET;
389 nAddr |= IN_CLASSA_HOST;
391 else if (IN_CLASSB(nAddr))
393 nAddr &= IN_CLASSB_NET;
394 nAddr |= IN_CLASSB_HOST;
396 else if (IN_CLASSC(nAddr))
398 nAddr &= IN_CLASSC_NET;
399 nAddr |= IN_CLASSC_HOST;
401 else
403 /* No broadcast in class D */
404 return nullptr;
406 nAddr = htonl(nAddr);
409 oslSocketAddr pAddr =
410 createSocketAddrWithFamily( osl_Socket_FamilyInet, htons( static_cast<sal_uInt16>(Port)), nAddr );
411 return pAddr;
414 oslSocketAddr SAL_CALL osl_createInetSocketAddr (
415 rtl_uString *strDottedAddr,
416 sal_Int32 Port)
418 sal_uInt32 Addr;
420 IN_ADDR addr;
421 INT ret = InetPtonW(AF_INET, o3tl::toW(strDottedAddr->buffer), & addr);
422 Addr = ret == 1 ? addr.S_un.S_addr : OSL_INADDR_NONE;
424 oslSocketAddr pAddr = nullptr;
425 if(Addr != OSL_INADDR_NONE)
427 pAddr = createSocketAddrWithFamily( osl_Socket_FamilyInet, htons( static_cast<sal_uInt16>(Port)), Addr );
429 return pAddr;
432 oslSocketResult SAL_CALL osl_setAddrOfSocketAddr( oslSocketAddr pAddr, sal_Sequence *pByteSeq )
434 OSL_ASSERT( pAddr );
435 OSL_ASSERT( pByteSeq );
437 oslSocketResult res = osl_Socket_Error;
438 if( pAddr && pByteSeq )
440 OSL_ASSERT( pAddr->m_sockaddr.sa_family == FAMILY_TO_NATIVE( osl_Socket_FamilyInet ) );
441 OSL_ASSERT( pByteSeq->nElements == 4 );
442 struct sockaddr_in * pSystemInetAddr = reinterpret_cast<struct sockaddr_in *>(&pAddr->m_sockaddr);
443 memcpy( &(pSystemInetAddr->sin_addr) , pByteSeq->elements , 4 );
444 res = osl_Socket_Ok;
446 return res;
449 /** Returns the addr field in the struct sockaddr. ppByteSeq is in network byteorder. *ppByteSeq may
450 either be 0 or contain a constructed sal_Sequence.
452 oslSocketResult SAL_CALL osl_getAddrOfSocketAddr( oslSocketAddr pAddr, sal_Sequence **ppByteSeq )
454 OSL_ASSERT( pAddr );
455 OSL_ASSERT( ppByteSeq );
457 oslSocketResult res = osl_Socket_Error;
458 if( pAddr && ppByteSeq )
460 struct sockaddr_in * pSystemInetAddr = reinterpret_cast<struct sockaddr_in *>(&pAddr->m_sockaddr);
461 rtl_byte_sequence_constructFromArray( ppByteSeq , reinterpret_cast<sal_Int8 *>(&pSystemInetAddr->sin_addr),4);
462 res = osl_Socket_Ok;
464 return res;
467 struct oslHostAddrImpl {
468 rtl_uString *pHostName;
469 oslSocketAddr pSockAddr;
472 oslHostAddr SAL_CALL osl_createHostAddr (
473 rtl_uString *strHostname,
474 const oslSocketAddr pSocketAddr)
476 oslHostAddr pAddr;
477 rtl_uString *cn= nullptr;
479 if ((strHostname == nullptr) || (strHostname->length == 0) || (pSocketAddr == nullptr))
480 return nullptr;
482 rtl_uString_newFromString( &cn, strHostname);
484 pAddr= static_cast<oslHostAddr>(malloc (sizeof (struct oslHostAddrImpl)));
486 if (pAddr == nullptr)
488 rtl_uString_release(cn);
489 return nullptr;
492 pAddr->pHostName= cn;
493 pAddr->pSockAddr= osl_copySocketAddr( pSocketAddr );
495 return pAddr;
498 oslHostAddr SAL_CALL osl_createHostAddrByName(rtl_uString *strHostname)
500 if ((strHostname == nullptr) || (strHostname->length == 0))
501 return nullptr;
503 PADDRINFOW pAddrInfo = nullptr;
504 int ret = GetAddrInfoW(
505 o3tl::toW(strHostname->buffer), nullptr, nullptr, & pAddrInfo);
506 if (0 != ret)
508 SAL_INFO("sal.osl", "GetAddrInfoW failed: " << WSAGetLastError());
509 return nullptr;
512 oslHostAddr pRet = nullptr;
513 for (PADDRINFOW pIter = pAddrInfo; pIter; pIter = pIter->ai_next)
515 if (AF_INET == pIter->ai_family)
517 pRet = static_cast<oslHostAddr>(
518 rtl_allocateZeroMemory(sizeof(struct oslHostAddrImpl)));
519 if (pIter->ai_canonname == nullptr) {
520 rtl_uString_new(&pRet->pHostName);
521 } else {
522 rtl_uString_newFromStr(&pRet->pHostName, o3tl::toU(pIter->ai_canonname));
524 pRet->pSockAddr = createSocketAddr();
525 memcpy(& pRet->pSockAddr->m_sockaddr,
526 pIter->ai_addr, pIter->ai_addrlen);
527 break; // ignore other results
530 FreeAddrInfoW(pAddrInfo);
531 return pRet;
534 oslHostAddr SAL_CALL osl_createHostAddrByAddr(const oslSocketAddr pAddr)
536 if (pAddr == nullptr)
537 return nullptr;
539 if (pAddr->m_sockaddr.sa_family != FAMILY_TO_NATIVE(osl_Socket_FamilyInet))
540 return nullptr;
542 const struct sockaddr_in *sin= reinterpret_cast<const struct sockaddr_in *>(&pAddr->m_sockaddr);
544 if (sin->sin_addr.s_addr == htonl(INADDR_ANY))
545 return nullptr;
547 WCHAR buf[NI_MAXHOST];
548 int ret = GetNameInfoW(
549 & pAddr->m_sockaddr, sizeof(struct sockaddr),
550 buf, NI_MAXHOST,
551 nullptr, 0, 0);
552 if (0 != ret)
554 SAL_INFO("sal.osl", "GetNameInfoW failed: " << WSAGetLastError());
555 return nullptr;
558 oslHostAddr pRet = static_cast<oslHostAddr>(
559 rtl_allocateZeroMemory(sizeof(struct oslHostAddrImpl)));
560 rtl_uString_newFromStr(&pRet->pHostName, o3tl::toU(buf));
561 pRet->pSockAddr = createSocketAddr();
562 memcpy(& pRet->pSockAddr->m_sockaddr,
563 & pAddr->m_sockaddr, sizeof(struct sockaddr));
564 return pRet;
567 oslHostAddr SAL_CALL osl_copyHostAddr(const oslHostAddr Addr)
569 oslHostAddr pAddr = Addr;
571 if (pAddr)
572 return osl_createHostAddr (pAddr->pHostName, pAddr->pSockAddr);
573 else
574 return nullptr;
577 void SAL_CALL osl_getHostnameOfHostAddr(
578 const oslHostAddr pAddr, rtl_uString **strHostname)
580 if (pAddr)
581 rtl_uString_assign (strHostname, pAddr->pHostName);
582 else
583 rtl_uString_new (strHostname);
586 oslSocketAddr SAL_CALL osl_getSocketAddrOfHostAddr(const oslHostAddr pAddr)
588 if (pAddr)
589 return pAddr->pSockAddr;
590 else
591 return nullptr;
594 void SAL_CALL osl_destroyHostAddr(oslHostAddr pAddr)
596 if (pAddr)
598 if (pAddr->pHostName)
599 rtl_uString_release (pAddr->pHostName);
600 if (pAddr->pSockAddr)
601 osl_destroySocketAddr( pAddr->pSockAddr );
603 free (pAddr);
607 oslSocketResult SAL_CALL osl_getLocalHostname (rtl_uString **strLocalHostname)
609 static auto const init = []() -> std::pair<oslSocketResult, OUString> {
610 sal_Unicode LocalHostname[256] = {0};
612 char Host[256]= "";
613 if (gethostname(Host, sizeof(Host)) == 0)
615 OUString u;
616 if (rtl_convertStringToUString(
617 &u.pData, Host, strlen(Host), osl_getThreadTextEncoding(),
618 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR
619 | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR
620 | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR))
621 && o3tl::make_unsigned(u.getLength()) < SAL_N_ELEMENTS(LocalHostname))
623 memcpy(LocalHostname, u.getStr(), (u.getLength() + 1) * sizeof (sal_Unicode));
627 if (rtl_ustr_getLength(LocalHostname) > 0)
629 return {osl_Socket_Ok, OUString(LocalHostname)};
632 return {osl_Socket_Error, OUString()};
633 }();
635 rtl_uString_assign (strLocalHostname, init.second.pData);
637 return init.first;
640 oslSocketAddr SAL_CALL osl_resolveHostname(rtl_uString* strHostname)
642 oslHostAddr pAddr = osl_createHostAddrByName (strHostname);
643 if (pAddr)
645 oslSocketAddr SockAddr = osl_copySocketAddr( pAddr->pSockAddr );
646 osl_destroyHostAddr(pAddr);
647 return SockAddr;
649 return nullptr;
652 sal_Int32 SAL_CALL osl_getServicePort (
653 rtl_uString* strServicename,
654 rtl_uString* strProtocol)
656 struct servent* ps;
658 rtl_String *str_Servicename=nullptr;
659 rtl_String *str_Protocol=nullptr;
661 rtl_uString2String(
662 &str_Servicename,
663 rtl_uString_getStr(strServicename),
664 rtl_uString_getLength(strServicename),
665 RTL_TEXTENCODING_UTF8, OUSTRING_TO_OSTRING_CVTFLAGS);
666 rtl_uString2String(
667 &str_Protocol,
668 rtl_uString_getStr(strProtocol),
669 rtl_uString_getLength(strProtocol),
670 RTL_TEXTENCODING_UTF8, OUSTRING_TO_OSTRING_CVTFLAGS);
672 ps= getservbyname(
673 rtl_string_getStr(str_Servicename),
674 rtl_string_getStr(str_Protocol));
676 rtl_string_release( str_Servicename );
677 rtl_string_release( str_Protocol );
679 if (ps != nullptr)
680 return ntohs(ps->s_port);
682 return OSL_INVALID_PORT;
685 void SAL_CALL osl_destroySocketAddr(oslSocketAddr pAddr)
687 destroySocketAddr( pAddr );
690 oslAddrFamily SAL_CALL osl_getFamilyOfSocketAddr(oslSocketAddr pAddr)
692 if (pAddr)
693 return FAMILY_FROM_NATIVE(pAddr->m_sockaddr.sa_family);
694 else
695 return osl_Socket_FamilyInvalid;
698 sal_Int32 SAL_CALL osl_getInetPortOfSocketAddr(oslSocketAddr pAddr)
700 if( pAddr )
702 struct sockaddr_in* pSystemInetAddr= reinterpret_cast<struct sockaddr_in*>(&pAddr->m_sockaddr);
704 if (pSystemInetAddr->sin_family == FAMILY_TO_NATIVE(osl_Socket_FamilyInet))
705 return ntohs(pSystemInetAddr->sin_port);
707 return OSL_INVALID_PORT;
710 sal_Bool SAL_CALL osl_setInetPortOfSocketAddr (
711 oslSocketAddr pAddr,
712 sal_Int32 Port)
714 if (pAddr == nullptr)
715 return false;
717 struct sockaddr_in* pSystemInetAddr= reinterpret_cast<struct sockaddr_in*>(&pAddr->m_sockaddr);
719 if (pSystemInetAddr->sin_family != FAMILY_TO_NATIVE(osl_Socket_FamilyInet))
720 return false;
722 pSystemInetAddr->sin_port= htons(static_cast<short>(Port));
723 return true;
726 oslSocketResult SAL_CALL osl_getHostnameOfSocketAddr (
727 oslSocketAddr Addr,
728 rtl_uString **strHostName)
730 oslHostAddr pAddr= osl_createHostAddrByAddr (Addr);
732 if (pAddr)
734 rtl_uString_newFromString(strHostName, pAddr->pHostName);
736 osl_destroyHostAddr(pAddr);
738 return osl_Socket_Ok;
741 return osl_Socket_Error;
744 oslSocketResult SAL_CALL osl_getDottedInetAddrOfSocketAddr (
745 oslSocketAddr pAddr,
746 rtl_uString **strDottedInetAddr)
748 if (pAddr == nullptr)
749 return osl_Socket_Error;
751 struct sockaddr_in *pSystemInetAddr = reinterpret_cast<struct sockaddr_in*>(&pAddr->m_sockaddr);
752 if (pSystemInetAddr->sin_family != FAMILY_TO_NATIVE(osl_Socket_FamilyInet))
753 return osl_Socket_Error;
755 *strDottedInetAddr = nullptr;
756 WCHAR buf[16]; // 16 for IPV4, 46 for IPV6
757 PCWSTR ret = InetNtopW(
758 AF_INET, & pSystemInetAddr->sin_addr,
759 buf, SAL_N_ELEMENTS(buf));
760 if (nullptr == ret)
762 SAL_INFO("sal.osl", "InetNtopW failed: " << WSAGetLastError());
763 return osl_Socket_Error;
765 rtl_uString_newFromStr(strDottedInetAddr, o3tl::toU(ret));
766 OSL_ASSERT(*strDottedInetAddr != nullptr);
768 return osl_Socket_Ok;
771 oslSocket SAL_CALL osl_createSocket(
772 oslAddrFamily Family,
773 oslSocketType Type,
774 oslProtocol Protocol)
776 /* alloc memory */
777 oslSocket pSocket = createSocketImpl(0);
779 if (pSocket == nullptr)
780 return nullptr;
782 /* create socket */
783 pSocket->m_Socket = socket(FAMILY_TO_NATIVE(Family),
784 TYPE_TO_NATIVE(Type),
785 PROTOCOL_TO_NATIVE(Protocol));
787 /* creation failed => free memory */
788 if(pSocket->m_Socket == OSL_INVALID_SOCKET)
790 int nErrno = WSAGetLastError();
791 SAL_WARN("sal.osl", "socket creation failed: (" << nErrno << "): " << WindowsErrorString(nErrno));
793 destroySocketImpl(pSocket);
794 pSocket = nullptr;
796 else
798 pSocket->m_Flags = 0;
801 return pSocket;
804 void SAL_CALL osl_acquireSocket(oslSocket pSocket)
806 osl_atomic_increment(&(pSocket->m_nRefCount));
809 void SAL_CALL osl_releaseSocket(oslSocket pSocket)
811 if (pSocket && osl_atomic_decrement(&(pSocket->m_nRefCount)) == 0)
813 osl_closeSocket(pSocket);
814 destroySocketImpl(pSocket);
818 void SAL_CALL osl_closeSocket(oslSocket pSocket)
820 /* socket already invalid */
821 if (!pSocket)
822 return;
824 /* close */
825 closesocket(pSocket->m_Socket);
827 pSocket->m_Socket = OSL_INVALID_SOCKET;
831 Note that I rely on the fact that oslSocketAddr and struct sockaddr
832 are the same! I don't like it very much but see no other easy way
833 to conceal the struct sockaddr from the eyes of the user.
835 oslSocketAddr SAL_CALL osl_getLocalAddrOfSocket(oslSocket pSocket)
837 struct sockaddr Addr;
838 int AddrLen;
840 if (pSocket == nullptr) /* ENOTSOCK */
841 return nullptr;
843 AddrLen= sizeof(struct sockaddr);
845 if (getsockname(pSocket->m_Socket, &Addr, &AddrLen) == OSL_SOCKET_ERROR)
846 return nullptr;
848 oslSocketAddr pAddr = createSocketAddFromSystem( &Addr );
849 return pAddr;
852 oslSocketAddr SAL_CALL osl_getPeerAddrOfSocket(oslSocket pSocket)
854 struct sockaddr Addr;
855 int AddrLen;
857 if (pSocket == nullptr) /* ENOTSOCK */
858 return nullptr;
860 AddrLen= sizeof(struct sockaddr);
862 if (getpeername(pSocket->m_Socket, &Addr, &AddrLen) == OSL_SOCKET_ERROR)
863 return nullptr;
865 oslSocketAddr pAddr = createSocketAddFromSystem( &Addr );
866 return pAddr;
869 sal_Bool SAL_CALL osl_bindAddrToSocket ( oslSocket pSocket, oslSocketAddr pAddr)
871 assert(pAddr);
873 if (pSocket == nullptr) /* ENOTSOCK */
874 return false;
876 return (bind(pSocket->m_Socket,
877 &(pAddr->m_sockaddr),
878 sizeof(struct sockaddr)) != OSL_SOCKET_ERROR);
881 oslSocketResult SAL_CALL osl_connectSocketTo (
882 oslSocket pSocket,
883 oslSocketAddr pAddr,
884 const TimeValue* pTimeout)
887 if (pSocket == nullptr) /* ENOTSOCK */
888 return osl_Socket_Error;
890 if (pAddr == nullptr) /* EDESTADDRREQ */
891 return osl_Socket_Error;
893 if (pTimeout == nullptr)
895 if(connect(pSocket->m_Socket,
896 &(pAddr->m_sockaddr),
897 sizeof(struct sockaddr)) == OSL_SOCKET_ERROR)
898 return osl_Socket_Error;
899 else
900 return osl_Socket_Ok;
902 else
904 fd_set fds;
905 int error;
906 struct timeval tv;
907 unsigned long Param;
908 oslSocketResult Result= osl_Socket_Ok;
910 if (pSocket->m_Flags & OSL_SOCKET_FLAGS_NONBLOCKING)
912 if (connect(pSocket->m_Socket,
913 &(pAddr->m_sockaddr),
914 sizeof(struct sockaddr)) == OSL_SOCKET_ERROR)
916 switch (WSAGetLastError())
918 case WSAEWOULDBLOCK:
919 case WSAEINPROGRESS:
920 return osl_Socket_InProgress;
922 default:
923 return osl_Socket_Error;
926 else
927 return osl_Socket_Ok;
930 /* set socket temporarily to non-blocking */
931 Param= 1;
932 OSL_VERIFY(ioctlsocket(
933 pSocket->m_Socket, FIONBIO, &Param) != OSL_SOCKET_ERROR);
935 /* initiate connect */
936 if (connect(pSocket->m_Socket,
937 &(pAddr->m_sockaddr),
938 sizeof(struct sockaddr)) != OSL_SOCKET_ERROR)
940 /* immediate connection */
942 Param= 0;
943 ioctlsocket(pSocket->m_Socket, FIONBIO, &Param);
945 return osl_Socket_Ok;
947 else
949 error = WSAGetLastError();
951 /* really an error or just delayed? */
952 if (error != WSAEWOULDBLOCK && error != WSAEINPROGRESS)
954 Param= 0;
955 ioctlsocket(pSocket->m_Socket, FIONBIO, &Param);
957 return osl_Socket_Error;
961 /* prepare select set for socket */
962 FD_ZERO(&fds);
963 FD_SET(pSocket->m_Socket, &fds);
965 /* divide milliseconds into seconds and microseconds */
966 tv.tv_sec= pTimeout->Seconds;
967 tv.tv_usec= pTimeout->Nanosec / 1000L;
969 /* select */
970 error= select(pSocket->m_Socket+1,
971 nullptr,
972 &fds,
973 nullptr,
974 &tv);
976 if (error > 0) /* connected */
978 SAL_WARN_IF(
979 !FD_ISSET(pSocket->m_Socket, &fds),
980 "sal.osl",
981 "osl_connectSocketTo(): select returned but socket not set");
983 Result= osl_Socket_Ok;
986 else if(error < 0) /* error */
988 /* errno == EBADF: most probably interrupted by close() */
989 if(WSAGetLastError() == WSAEBADF)
991 /* do not access pSockImpl because it is about to be or */
992 /* already destroyed */
993 return osl_Socket_Interrupted;
995 else
996 Result= osl_Socket_Error;
999 else /* timeout */
1000 Result= osl_Socket_TimedOut;
1002 /* clean up */
1003 Param= 0;
1004 ioctlsocket(pSocket->m_Socket, FIONBIO, &Param);
1006 return Result;
1010 sal_Bool SAL_CALL osl_listenOnSocket (
1011 oslSocket pSocket,
1012 sal_Int32 MaxPendingConnections)
1014 if (pSocket == nullptr) /* ENOTSOCK */
1015 return false;
1017 return (listen(pSocket->m_Socket,
1018 MaxPendingConnections == -1 ?
1019 SOMAXCONN :
1020 MaxPendingConnections) != OSL_SOCKET_ERROR);
1023 oslSocket SAL_CALL osl_acceptConnectionOnSocket (
1024 oslSocket pSocket,
1025 oslSocketAddr* ppAddr)
1027 if (pSocket == nullptr) /* ENOTSOCK */
1028 return nullptr;
1030 SOCKET Connection;
1031 if(ppAddr)
1033 if( *ppAddr )
1035 osl_destroySocketAddr( *ppAddr );
1036 *ppAddr = nullptr;
1038 int AddrLen= sizeof(struct sockaddr);
1040 /* user wants to know peer Addr */
1041 struct sockaddr Addr;
1043 Connection= accept(pSocket->m_Socket, &Addr, &AddrLen);
1044 OSL_ASSERT(AddrLen == sizeof(struct sockaddr));
1046 if(Connection != static_cast<SOCKET>(OSL_SOCKET_ERROR))
1047 *ppAddr= createSocketAddFromSystem(&Addr);
1048 else
1049 *ppAddr = nullptr;
1051 else
1053 /* user is not interested in peer-addr */
1054 Connection= accept(pSocket->m_Socket, nullptr, nullptr);
1057 /* accept failed? */
1058 if(Connection == static_cast<SOCKET>(OSL_SOCKET_ERROR))
1059 return nullptr;
1061 /* alloc memory */
1062 oslSocket pConnectionSocket;
1063 pConnectionSocket= createSocketImpl(Connection);
1065 pConnectionSocket->m_Flags = 0;
1067 return pConnectionSocket;
1070 sal_Int32 SAL_CALL osl_receiveSocket (
1071 oslSocket pSocket,
1072 void* pBuffer,
1073 sal_uInt32 BytesToRead,
1074 oslSocketMsgFlag Flag)
1076 if (pSocket == nullptr) /* ENOTSOCK */
1077 return osl_Socket_Error;
1079 return recv(pSocket->m_Socket,
1080 static_cast<char*>(pBuffer),
1081 BytesToRead,
1082 MSG_FLAG_TO_NATIVE(Flag));
1085 sal_Int32 SAL_CALL osl_receiveFromSocket (
1086 oslSocket pSocket,
1087 oslSocketAddr SenderAddr,
1088 void* pBuffer,
1089 sal_uInt32 BufferSize,
1090 oslSocketMsgFlag Flag)
1092 struct sockaddr *pSystemSockAddr = nullptr;
1093 int AddrLen = 0;
1094 if( SenderAddr )
1096 AddrLen = sizeof( struct sockaddr );
1097 pSystemSockAddr = &(SenderAddr->m_sockaddr);
1100 if (pSocket == nullptr) /* ENOTSOCK */
1101 return osl_Socket_Error;
1103 return recvfrom(pSocket->m_Socket,
1104 static_cast<char*>(pBuffer),
1105 BufferSize,
1106 MSG_FLAG_TO_NATIVE(Flag),
1107 pSystemSockAddr,
1108 &AddrLen);
1111 sal_Int32 SAL_CALL osl_sendSocket (
1112 oslSocket pSocket,
1113 const void* pBuffer,
1114 sal_uInt32 BytesToSend,
1115 oslSocketMsgFlag Flag)
1117 if (pSocket == nullptr) /* ENOTSOCK */
1118 return osl_Socket_Error;
1120 return send(pSocket->m_Socket,
1121 static_cast<char const *>(pBuffer),
1122 BytesToSend,
1123 MSG_FLAG_TO_NATIVE(Flag));
1126 sal_Int32 SAL_CALL osl_sendToSocket (
1127 oslSocket pSocket,
1128 oslSocketAddr ReceiverAddr,
1129 const void* pBuffer,
1130 sal_uInt32 BytesToSend,
1131 oslSocketMsgFlag Flag)
1133 if (pSocket == nullptr) /* ENOTSOCK */
1134 return osl_Socket_Error;
1136 /* ReceiverAddr might be 0 when used on a connected socket. */
1137 /* Then sendto should behave like send. */
1139 struct sockaddr *pSystemSockAddr = nullptr;
1140 if( ReceiverAddr )
1141 pSystemSockAddr = &(ReceiverAddr->m_sockaddr);
1143 return sendto(pSocket->m_Socket,
1144 static_cast<char const *>(pBuffer),
1145 BytesToSend,
1146 MSG_FLAG_TO_NATIVE(Flag),
1147 pSystemSockAddr,
1148 pSystemSockAddr == nullptr ? 0 : sizeof(struct sockaddr));
1151 sal_Int32 SAL_CALL osl_readSocket( oslSocket pSocket, void *pBuffer, sal_Int32 n )
1153 sal_uInt8 * Ptr = static_cast<sal_uInt8 *>(pBuffer);
1155 OSL_ASSERT( pSocket);
1157 /* loop until all desired bytes were read or an error occurred */
1158 sal_uInt32 BytesRead= 0;
1159 sal_uInt32 BytesToRead= n;
1160 while (BytesToRead > 0)
1162 sal_Int32 RetVal;
1163 RetVal= osl_receiveSocket(pSocket,
1164 Ptr,
1165 BytesToRead,
1166 osl_Socket_MsgNormal);
1168 /* error occurred? */
1169 if(RetVal <= 0)
1171 break;
1174 BytesToRead -= RetVal;
1175 BytesRead += RetVal;
1176 Ptr += RetVal;
1179 return BytesRead;
1182 sal_Int32 SAL_CALL osl_writeSocket( oslSocket pSocket, const void *pBuffer, sal_Int32 n )
1184 OSL_ASSERT( pSocket );
1186 /* loop until all desired bytes were send or an error occurred */
1187 sal_uInt32 BytesSend= 0;
1188 sal_uInt32 BytesToSend= n;
1189 sal_uInt8 const *Ptr = static_cast<sal_uInt8 const *>(pBuffer);
1190 while (BytesToSend > 0)
1192 sal_Int32 RetVal;
1194 RetVal= osl_sendSocket( pSocket,Ptr,BytesToSend,osl_Socket_MsgNormal);
1196 /* error occurred? */
1197 if(RetVal <= 0)
1199 break;
1202 BytesToSend -= RetVal;
1203 BytesSend += RetVal;
1204 Ptr += RetVal;
1207 return BytesSend;
1210 sal_Bool SAL_CALL osl_isReceiveReady (
1211 oslSocket pSocket,
1212 const TimeValue* pTimeout)
1214 fd_set fds;
1215 struct timeval tv;
1217 if (pSocket == nullptr) /* ENOTSOCK */
1218 return false;
1220 FD_ZERO(&fds);
1221 FD_SET(pSocket->m_Socket, &fds);
1223 if (pTimeout)
1225 tv.tv_sec = pTimeout->Seconds;
1226 tv.tv_usec = pTimeout->Nanosec / 1000L;
1229 return (select(pSocket->m_Socket + 1, /* no of sockets to monitor */
1230 &fds, /* check read operations */
1231 nullptr, /* check write ops */
1232 nullptr, /* check for OOB */
1233 pTimeout ? &tv : nullptr)==1); /* use timeout? */
1236 /*****************************************************************************/
1237 /* osl_isSendReady */
1238 /*****************************************************************************/
1239 sal_Bool SAL_CALL osl_isSendReady (
1240 oslSocket pSocket,
1241 const TimeValue* pTimeout)
1243 fd_set fds;
1244 struct timeval tv;
1246 if (pSocket == nullptr) /* ENOTSOCK */
1247 return false;
1249 FD_ZERO(&fds);
1250 FD_SET(pSocket->m_Socket, &fds);
1252 if (pTimeout)
1254 tv.tv_sec = pTimeout->Seconds;
1255 tv.tv_usec = pTimeout->Nanosec / 1000L;
1258 return (select(pSocket->m_Socket + 1, /* no of sockets to monitor */
1259 nullptr, /* check read operations */
1260 &fds, /* check write ops */
1261 nullptr, /* check for OOB */
1262 pTimeout ? &tv : nullptr)==1); /* use timeout? */
1265 sal_Bool SAL_CALL osl_isExceptionPending (
1266 oslSocket pSocket,
1267 const TimeValue* pTimeout)
1269 fd_set fds;
1270 struct timeval tv;
1272 if (pSocket == nullptr) /* ENOTSOCK */
1273 return false;
1275 FD_ZERO(&fds);
1276 FD_SET(pSocket->m_Socket, &fds);
1278 if (pTimeout)
1280 tv.tv_sec = pTimeout->Seconds;
1281 tv.tv_usec = pTimeout->Nanosec / 1000L;
1284 return (select(pSocket->m_Socket + 1, /* no of sockets to monitor */
1285 nullptr, /* check read operations */
1286 nullptr, /* check write ops */
1287 &fds, /* check for OOB */
1288 pTimeout ? &tv : nullptr)==1); /* use timeout? */
1291 sal_Bool SAL_CALL osl_shutdownSocket (
1292 oslSocket pSocket,
1293 oslSocketDirection Direction)
1295 if (pSocket == nullptr) /* ENOTSOCK */
1296 return false;
1298 return (shutdown(pSocket->m_Socket, DIRECTION_TO_NATIVE(Direction))==0);
1301 sal_Int32 SAL_CALL osl_getSocketOption (
1302 oslSocket pSocket,
1303 oslSocketOptionLevel Level,
1304 oslSocketOption Option,
1305 void* pBuffer,
1306 sal_uInt32 BufferLen)
1308 if (pSocket == nullptr) /* ENOTSOCK */
1309 return osl_Socket_Error;
1311 int len = BufferLen;
1312 if (getsockopt(pSocket->m_Socket,
1313 OPTION_LEVEL_TO_NATIVE(Level),
1314 OPTION_TO_NATIVE(Option),
1315 static_cast<char *>(pBuffer),
1316 &len) == -1)
1318 return -1;
1321 return len;
1324 sal_Bool SAL_CALL osl_setSocketOption (
1325 oslSocket pSocket,
1326 oslSocketOptionLevel Level,
1327 oslSocketOption Option,
1328 void* pBuffer,
1329 sal_uInt32 BufferLen)
1331 if (pSocket == nullptr) /* ENOTSOCK */
1332 return false;
1334 return(setsockopt(pSocket->m_Socket,
1335 OPTION_LEVEL_TO_NATIVE(Level),
1336 OPTION_TO_NATIVE(Option),
1337 static_cast<char*>(pBuffer),
1338 BufferLen) == 0);
1341 sal_Bool SAL_CALL osl_enableNonBlockingMode ( oslSocket pSocket, sal_Bool On)
1343 unsigned long Param= On ? 1 : 0;
1345 if (pSocket == nullptr) /* ENOTSOCK */
1346 return false;
1348 pSocket->m_Flags = Param ?
1349 (pSocket->m_Flags | OSL_SOCKET_FLAGS_NONBLOCKING) :
1350 (pSocket->m_Flags & ~OSL_SOCKET_FLAGS_NONBLOCKING) ;
1352 return (
1353 ioctlsocket(pSocket->m_Socket, FIONBIO, &Param) != OSL_SOCKET_ERROR);
1356 sal_Bool SAL_CALL osl_isNonBlockingMode(oslSocket pSocket)
1358 if (pSocket == nullptr) /* ENOTSOCK */
1359 return false;
1361 return (pSocket->m_Flags & OSL_SOCKET_FLAGS_NONBLOCKING) != 0;
1364 oslSocketType SAL_CALL osl_getSocketType(oslSocket pSocket)
1366 int Type=0;
1367 int TypeSize= sizeof(Type);
1369 if (pSocket == nullptr) /* ENOTSOCK */
1370 return osl_Socket_TypeInvalid;
1372 if(getsockopt(pSocket->m_Socket,
1373 OPTION_LEVEL_TO_NATIVE(osl_Socket_LevelSocket),
1374 OPTION_TO_NATIVE(osl_Socket_OptionType),
1375 reinterpret_cast<char *>(&Type),
1376 &TypeSize) == -1)
1378 /* error */
1379 return osl_Socket_TypeInvalid;
1382 return TYPE_FROM_NATIVE(Type);
1385 void SAL_CALL osl_getLastSocketErrorDescription (
1386 oslSocket /*Socket*/,
1387 rtl_uString **strError)
1389 int error;
1391 switch(error = WSAGetLastError())
1393 case WSAENOTSOCK:
1394 rtl_uString_newFromAscii (strError, "WSAENOTSOCK, Socket operation on non-socket. A socket created in one process is used by another process.");
1395 break;
1397 case WSAEDESTADDRREQ:
1398 rtl_uString_newFromAscii (strError, "WSAEDESTADDRREQ, Destination Addr required");
1399 break;
1401 case WSAEMSGSIZE:
1402 rtl_uString_newFromAscii (strError, "WSAEMSGSIZE, Message too long");
1403 break;
1405 case WSAEPROTOTYPE:
1406 rtl_uString_newFromAscii (strError, "WSAEPROTOTYPE, Protocol wrong type for socket");
1407 break;
1409 case WSAENOPROTOOPT:
1410 rtl_uString_newFromAscii (strError, "WSAENOPROTOOPT, Protocol not available");
1411 break;
1413 case WSAEPROTONOSUPPORT:
1414 rtl_uString_newFromAscii (strError, "WSAEPROTONOSUPPORT, Protocol not supported");
1415 break;
1417 case WSAESOCKTNOSUPPORT:
1418 rtl_uString_newFromAscii (strError, "WSAESOCKTNOSUPPORT, Socket type not supported");
1419 break;
1421 case WSAEOPNOTSUPP:
1422 rtl_uString_newFromAscii (strError, "WSAEOPNOTSUPP, Operation not supported on socket");
1423 break;
1425 case WSAEPFNOSUPPORT:
1426 rtl_uString_newFromAscii (strError, "WSAEPFNOSUPPORT, Protocol family not supported");
1427 break;
1429 case WSAEAFNOSUPPORT:
1430 rtl_uString_newFromAscii (strError, "WSEAFNOSUPPORT, Addr family not supported by protocol family");
1431 break;
1433 case WSAEADDRINUSE:
1434 rtl_uString_newFromAscii (strError, "WSAEADDRINUSE, Triggered by bind() because a process went down without closing a socket.");
1435 break;
1437 case WSAEADDRNOTAVAIL:
1438 rtl_uString_newFromAscii (strError, "WSAEADDRNOTAVAIL, Can't assign requested Addr");
1439 break;
1441 case WSAENETDOWN:
1442 rtl_uString_newFromAscii (strError, "WSAENETDOWN, Network is down");
1443 break;
1445 case WSAENETUNREACH:
1446 rtl_uString_newFromAscii (strError, "WSAENETUNREACH, Network is unreachable");
1447 break;
1449 case WSAENETRESET:
1450 rtl_uString_newFromAscii (strError, "WSAENETRESET, Network dropped connection or reset");
1451 break;
1453 case WSAECONNABORTED:
1454 rtl_uString_newFromAscii (strError, "WSAECONNABORTED, Software caused connection abort");
1455 break;
1457 case WSAECONNRESET:
1458 rtl_uString_newFromAscii (strError, "WSAECONNRESET, Connection reset by peer");
1459 break;
1461 case WSAENOBUFS:
1462 rtl_uString_newFromAscii (strError, "WSAENOBUFS, No buffer space available.");
1463 break;
1465 case WSAEISCONN:
1466 rtl_uString_newFromAscii (strError, "WSAEISCONN, Socket is already connected");
1467 break;
1469 case WSAENOTCONN:
1470 rtl_uString_newFromAscii (strError, "WSAENOTCONN, Socket is not connected");
1471 break;
1473 case WSAESHUTDOWN:
1474 rtl_uString_newFromAscii (strError, "WSAESHUTDOWN, Can't send after socket shutdown");
1475 break;
1477 case WSAETIMEDOUT:
1478 rtl_uString_newFromAscii (strError, "WSAETIMEDOUT, Connection timed out");
1479 break;
1481 case WSAECONNREFUSED:
1482 rtl_uString_newFromAscii (strError, "WSAECONNREFUSED, Connection refused");
1483 break;
1485 case WSAEHOSTDOWN:
1486 rtl_uString_newFromAscii (strError, "WSAEHOSTDOWN, Networking subsystem not started");
1487 break;
1489 case WSAEHOSTUNREACH:
1490 rtl_uString_newFromAscii (strError, "WSAEHOSTUNREACH, No route to host");
1491 break;
1493 case WSAEWOULDBLOCK:
1494 rtl_uString_newFromAscii (strError, "WSAEWOULDBLOCK, Operation would block");
1495 break;
1497 case WSAEINPROGRESS:
1498 rtl_uString_newFromAscii (strError, "WSAEINPROGRESS, Operation now in progress");
1499 break;
1501 case WSAEALREADY:
1502 rtl_uString_newFromAscii (strError, "WSAEALREADY, Operation already in progress");
1503 break;
1505 case WSAEINTR:
1506 rtl_uString_newFromAscii (strError, "WSAEALREADY, Operation was interrupted");
1507 break;
1509 case WSAEBADF:
1510 rtl_uString_newFromAscii (strError, "WSAEBADF, Bad file number");
1511 break;
1513 case WSAEACCES:
1514 rtl_uString_newFromAscii (strError, "WSAEACCES, Access is denied");
1515 break;
1517 case WSAEFAULT:
1518 rtl_uString_newFromAscii (strError, "WSAEFAULT, Bad memory Addr");
1519 break;
1521 case WSAEINVAL:
1522 rtl_uString_newFromAscii (strError, "WSAEINVAL, The socket has not been bound with bind() or is already connected");
1523 break;
1525 case WSAEMFILE:
1526 rtl_uString_newFromAscii (strError, "WSAEMFILE, No more file descriptors are available");
1527 break;
1529 case WSAETOOMANYREFS:
1530 rtl_uString_newFromAscii (strError, "WSAETOOMANYREFS, Undocumented WinSock error");
1531 break;
1533 case WSAENAMETOOLONG:
1534 rtl_uString_newFromAscii (strError, "WSAENAMETOOLONG, Undocumented WinSock error");
1535 break;
1537 case WSAENOTEMPTY:
1538 rtl_uString_newFromAscii (strError, "WSAENOTEMPTY, Undocumented WinSock error");
1539 break;
1541 case WSAEPROCLIM:
1542 rtl_uString_newFromAscii (strError, "WSAEPROCLIM, Undocumented WinSock error");
1543 break;
1545 case WSAEUSERS:
1546 rtl_uString_newFromAscii (strError, "WSAEUSERS, Undocumented WinSock error");
1547 break;
1549 case WSAEDQUOT:
1550 rtl_uString_newFromAscii (strError, "WSAEDQUOT, Undocumented WinSock error");
1551 break;
1553 case WSAESTALE:
1554 rtl_uString_newFromAscii (strError, "WSAESTALE, Undocumented WinSock error");
1555 break;
1557 case WSAEREMOTE:
1558 rtl_uString_newFromAscii (strError, "WSAEREMOTE, Undocumented WinSock error");
1559 break;
1561 case WSAEDISCON:
1562 rtl_uString_newFromAscii (strError, "WSAEDISCON, Circuit was gracefully terminated");
1563 break;
1565 case WSASYSNOTREADY:
1566 rtl_uString_newFromAscii (strError, "WSASYSNOTREADY, The underlying network subsystem is not ready for network communication");
1567 break;
1569 case WSAVERNOTSUPPORTED:
1570 rtl_uString_newFromAscii (strError, "WSAVERNOTSUPPORTED, The version of Windows Sockets API support requested is not provided by this particular Windows Sockets implementation");
1571 break;
1573 case WSANOTINITIALISED:
1574 rtl_uString_newFromAscii (strError, "WSANOTINITIALISED, WSAStartup() has not been called");
1575 break;
1577 case WSAHOST_NOT_FOUND:
1578 rtl_uString_newFromAscii (strError, "WSAHOST_NOT_FOUND, Authoritative answer host not found");
1579 break;
1581 case WSATRY_AGAIN:
1582 rtl_uString_newFromAscii (strError, "WSATRY_AGAIN, Non-authoritative answer host not found or SERVERFAIL");
1583 break;
1585 case WSANO_RECOVERY:
1586 rtl_uString_newFromAscii (strError, "WSANO_RECOVERY, Non recoverable errors, FORMERR, REFUSED, NOTIMP");
1587 break;
1589 case WSANO_DATA:
1590 rtl_uString_newFromAscii (strError, "WSANO_DATA or WSANO_ADDRESS, Valid name, no data record of requested type");
1591 break;
1593 default:
1595 sal_Unicode message[128];
1597 wsprintfW(o3tl::toW(message), L"Unknown WinSock Error Number %d", error);
1598 rtl_uString_newFromStr (strError, message);
1601 return;
1606 oslSocketError SAL_CALL osl_getLastSocketError(oslSocket /*Socket*/)
1608 return ERROR_FROM_NATIVE(WSAGetLastError());
1611 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */