1
/****************************************************************************
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
7
** This file is part of the QtNetwork module of the Qt Toolkit.
9
** $QT_BEGIN_LICENSE:LGPL$
10
** No Commercial Usage
11
** This file contains pre-release code and may not be distributed.
12
** You may use this file in accordance with the terms and conditions
13
** contained in the Technology Preview License Agreement accompanying
16
** GNU Lesser General Public License Usage
17
** Alternatively, this file may be used under the terms of the GNU Lesser
18
** General Public License version 2.1 as published by the Free Software
19
** Foundation and appearing in the file LICENSE.LGPL included in the
20
** packaging of this file. Please review the following information to
21
** ensure the GNU Lesser General Public License version 2.1 requirements
22
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
** In addition, as a special exception, Nokia gives you certain additional
25
** rights. These rights are described in the Nokia Qt LGPL Exception
26
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
40
****************************************************************************/
49
// This file is not part of the Qt API. It exists for the convenience
50
// of Qt code on Unix. This header file may change from version to
51
// version to version without notice, or even be removed.
56
#include "private/qcore_unix_p.h"
58
#include <sys/types.h>
59
#include <sys/socket.h>
60
#include <netinet/in.h>
62
#if defined(Q_OS_VXWORKS)
68
#include <arpa/inet.h>
69
#if defined(Q_OS_VXWORKS)
77
// Almost always the same. If not, specify in qplatformdefs.h.
78
#if !defined(QT_SOCKOPTLEN_T)
79
# define QT_SOCKOPTLEN_T QT_SOCKLEN_T
82
// UnixWare 7 redefines socket -> _socket
83
static inline int qt_safe_socket(int domain, int type, int protocol, int flags = 0)
85
Q_ASSERT((flags & ~O_NONBLOCK) == 0);
88
#if defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK)
89
int newtype = type | SOCK_CLOEXEC;
90
if (flags & O_NONBLOCK)
91
newtype |= SOCK_NONBLOCK;
92
fd = ::socket(domain, newtype, protocol);
93
if (fd != -1 || errno != EINVAL)
97
fd = ::socket(domain, type, protocol);
101
::fcntl(fd, F_SETFD, FD_CLOEXEC);
103
// set non-block too?
104
if (flags & O_NONBLOCK)
105
::fcntl(fd, F_SETFL, ::fcntl(fd, F_GETFL) | O_NONBLOCK);
110
// Tru64 redefines accept -> _accept with _XOPEN_SOURCE_EXTENDED
111
static inline int qt_safe_accept(int s, struct sockaddr *addr, QT_SOCKLEN_T *addrlen, int flags = 0)
113
Q_ASSERT((flags & ~O_NONBLOCK) == 0);
116
#if QT_UNIX_SUPPORTS_THREADSAFE_CLOEXEC && defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK)
118
int sockflags = SOCK_CLOEXEC;
119
if (flags & O_NONBLOCK)
120
sockflags |= SOCK_NONBLOCK;
121
fd = ::accept4(s, addr, static_cast<QT_SOCKLEN_T *>(addrlen), sockflags);
122
if (fd != -1 || !(errno == ENOSYS || errno == EINVAL))
126
fd = ::accept(s, addr, static_cast<QT_SOCKLEN_T *>(addrlen));
130
::fcntl(fd, F_SETFD, FD_CLOEXEC);
132
// set non-block too?
133
if (flags & O_NONBLOCK)
134
::fcntl(fd, F_SETFL, ::fcntl(fd, F_GETFL) | O_NONBLOCK);
139
// UnixWare 7 redefines listen -> _listen
140
static inline int qt_safe_listen(int s, int backlog)
142
return ::listen(s, backlog);
145
static inline int qt_safe_connect(int sockfd, const struct sockaddr *addr, QT_SOCKLEN_T addrlen)
148
// Solaris e.g. expects a non-const 2nd parameter
149
EINTR_LOOP(ret, QT_SOCKET_CONNECT(sockfd, const_cast<struct sockaddr *>(addr), addrlen));
152
#undef QT_SOCKET_CONNECT
153
#define QT_SOCKET_CONNECT qt_safe_connect
165
// VxWorks' headers specify 'int' instead of '...' for the 3rd ioctl() parameter.
166
template <typename T>
167
static inline int qt_safe_ioctl(int sockfd, int request, T arg)
170
return ::ioctl(sockfd, request, (int) arg);
172
return ::ioctl(sockfd, request, arg);
176
// VxWorks' headers do not specify any const modifiers
177
static inline in_addr_t qt_safe_inet_addr(const char *cp)
180
return ::inet_addr((char *) cp);
182
return ::inet_addr(cp);
186
// VxWorks' headers do not specify any const modifiers
187
static inline int qt_safe_sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *to, QT_SOCKLEN_T tolen)
190
flags |= MSG_NOSIGNAL;
195
EINTR_LOOP(ret, ::sendto(sockfd, (char *) buf, len, flags, (struct sockaddr *) to, tolen));
197
EINTR_LOOP(ret, ::sendto(sockfd, buf, len, flags, to, tolen));
204
#endif // QNET_UNIX_P_H