0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the QtNetwork module of the Qt Toolkit.
|
|
8 |
**
|
|
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
|
|
14 |
** this package.
|
|
15 |
**
|
|
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.
|
|
23 |
**
|
|
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.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#ifndef QNET_UNIX_P_H
|
|
43 |
#define QNET_UNIX_P_H
|
|
44 |
|
|
45 |
//
|
|
46 |
// W A R N I N G
|
|
47 |
// -------------
|
|
48 |
//
|
|
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.
|
|
52 |
//
|
|
53 |
// We mean it.
|
|
54 |
//
|
|
55 |
|
|
56 |
#include "private/qcore_unix_p.h"
|
|
57 |
|
|
58 |
#include <sys/types.h>
|
|
59 |
#include <sys/socket.h>
|
|
60 |
#include <netinet/in.h>
|
|
61 |
|
|
62 |
#if defined(Q_OS_VXWORKS)
|
|
63 |
# include <sockLib.h>
|
|
64 |
#endif
|
|
65 |
|
|
66 |
// for inet_addr
|
|
67 |
#include <netdb.h>
|
|
68 |
#include <arpa/inet.h>
|
|
69 |
#if defined(Q_OS_VXWORKS)
|
|
70 |
# include <hostLib.h>
|
|
71 |
#else
|
|
72 |
# include <resolv.h>
|
|
73 |
#endif
|
|
74 |
|
|
75 |
QT_BEGIN_NAMESPACE
|
|
76 |
|
|
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
|
|
80 |
#endif
|
|
81 |
|
|
82 |
// UnixWare 7 redefines socket -> _socket
|
|
83 |
static inline int qt_safe_socket(int domain, int type, int protocol, int flags = 0)
|
|
84 |
{
|
|
85 |
Q_ASSERT((flags & ~O_NONBLOCK) == 0);
|
|
86 |
|
|
87 |
register int fd;
|
|
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)
|
|
94 |
return fd;
|
|
95 |
#endif
|
|
96 |
|
|
97 |
fd = ::socket(domain, type, protocol);
|
|
98 |
if (fd == -1)
|
|
99 |
return -1;
|
|
100 |
|
|
101 |
::fcntl(fd, F_SETFD, FD_CLOEXEC);
|
|
102 |
|
|
103 |
// set non-block too?
|
|
104 |
if (flags & O_NONBLOCK)
|
|
105 |
::fcntl(fd, F_SETFL, ::fcntl(fd, F_GETFL) | O_NONBLOCK);
|
|
106 |
|
|
107 |
return fd;
|
|
108 |
}
|
|
109 |
|
|
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)
|
|
112 |
{
|
|
113 |
Q_ASSERT((flags & ~O_NONBLOCK) == 0);
|
|
114 |
|
|
115 |
register int fd;
|
|
116 |
#if QT_UNIX_SUPPORTS_THREADSAFE_CLOEXEC && defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK)
|
|
117 |
// use accept4
|
|
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))
|
|
123 |
return fd;
|
|
124 |
#endif
|
|
125 |
|
|
126 |
fd = ::accept(s, addr, static_cast<QT_SOCKLEN_T *>(addrlen));
|
|
127 |
if (fd == -1)
|
|
128 |
return -1;
|
|
129 |
|
|
130 |
::fcntl(fd, F_SETFD, FD_CLOEXEC);
|
|
131 |
|
|
132 |
// set non-block too?
|
|
133 |
if (flags & O_NONBLOCK)
|
|
134 |
::fcntl(fd, F_SETFL, ::fcntl(fd, F_GETFL) | O_NONBLOCK);
|
|
135 |
|
|
136 |
return fd;
|
|
137 |
}
|
|
138 |
|
|
139 |
// UnixWare 7 redefines listen -> _listen
|
|
140 |
static inline int qt_safe_listen(int s, int backlog)
|
|
141 |
{
|
|
142 |
return ::listen(s, backlog);
|
|
143 |
}
|
|
144 |
|
|
145 |
static inline int qt_safe_connect(int sockfd, const struct sockaddr *addr, QT_SOCKLEN_T addrlen)
|
|
146 |
{
|
|
147 |
register int ret;
|
|
148 |
// Solaris e.g. expects a non-const 2nd parameter
|
|
149 |
EINTR_LOOP(ret, QT_SOCKET_CONNECT(sockfd, const_cast<struct sockaddr *>(addr), addrlen));
|
|
150 |
return ret;
|
|
151 |
}
|
|
152 |
#undef QT_SOCKET_CONNECT
|
|
153 |
#define QT_SOCKET_CONNECT qt_safe_connect
|
|
154 |
|
|
155 |
#if defined(socket)
|
|
156 |
# undef socket
|
|
157 |
#endif
|
|
158 |
#if defined(accept)
|
|
159 |
# undef accept
|
|
160 |
#endif
|
|
161 |
#if defined(listen)
|
|
162 |
# undef listen
|
|
163 |
#endif
|
|
164 |
|
|
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)
|
|
168 |
{
|
|
169 |
#ifdef Q_OS_VXWORKS
|
|
170 |
return ::ioctl(sockfd, request, (int) arg);
|
|
171 |
#else
|
|
172 |
return ::ioctl(sockfd, request, arg);
|
|
173 |
#endif
|
|
174 |
}
|
|
175 |
|
|
176 |
// VxWorks' headers do not specify any const modifiers
|
|
177 |
static inline in_addr_t qt_safe_inet_addr(const char *cp)
|
|
178 |
{
|
|
179 |
#ifdef Q_OS_VXWORKS
|
|
180 |
return ::inet_addr((char *) cp);
|
|
181 |
#else
|
|
182 |
return ::inet_addr(cp);
|
|
183 |
#endif
|
|
184 |
}
|
|
185 |
|
|
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)
|
|
188 |
{
|
|
189 |
#ifdef MSG_NOSIGNAL
|
|
190 |
flags |= MSG_NOSIGNAL;
|
|
191 |
#endif
|
|
192 |
|
|
193 |
register int ret;
|
|
194 |
#ifdef Q_OS_VXWORKS
|
|
195 |
EINTR_LOOP(ret, ::sendto(sockfd, (char *) buf, len, flags, (struct sockaddr *) to, tolen));
|
|
196 |
#else
|
|
197 |
EINTR_LOOP(ret, ::sendto(sockfd, buf, len, flags, to, tolen));
|
|
198 |
#endif
|
|
199 |
return ret;
|
|
200 |
}
|
|
201 |
|
|
202 |
QT_END_NAMESPACE
|
|
203 |
|
|
204 |
#endif // QNET_UNIX_P_H
|