Qt 4.8
qnet_unix_p.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
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 #else
193 #endif
194 
195  register int ret;
196 #ifdef Q_OS_VXWORKS
197  EINTR_LOOP(ret, ::sendto(sockfd, (char *) buf, len, flags, (struct sockaddr *) to, tolen));
198 #else
199  EINTR_LOOP(ret, ::sendto(sockfd, buf, len, flags, to, tolen));
200 #endif
201  return ret;
202 }
203 
205 
206 #endif // QNET_UNIX_P_H
int type
Definition: qmetatype.cpp:239
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define EINTR_LOOP(var, cmd)
Definition: qcore_unix_p.h:96
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
static int qt_safe_ioctl(int sockfd, int request, T arg)
Definition: qnet_unix_p.h:167
#define QT_SOCKLEN_T
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void qt_ignore_sigpipe()
Definition: qcore_unix_p.h:148
static int qt_safe_connect(int sockfd, const struct sockaddr *addr, QT_SOCKLEN_T addrlen)
Definition: qnet_unix_p.h:145
#define QT_SOCKET_CONNECT
Definition: qnet_unix_p.h:153
int fcntl(int, int,...)
static int qt_safe_socket(int domain, int type, int protocol, int flags=0)
Definition: qnet_unix_p.h:83
static int qt_safe_sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *to, QT_SOCKLEN_T tolen)
Definition: qnet_unix_p.h:187
static int qt_safe_accept(int s, struct sockaddr *addr, QT_SOCKLEN_T *addrlen, int flags=0)
Definition: qnet_unix_p.h:111
static in_addr_t qt_safe_inet_addr(const char *cp)
Definition: qnet_unix_p.h:177
static int qt_safe_listen(int s, int backlog)
Definition: qnet_unix_p.h:140
int errno