Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qnet_unix_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QNET_UNIX_P_H
6#define QNET_UNIX_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists for the convenience
13// of Qt code on Unix. This header file may change from version to
14// version to version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtNetwork/private/qtnetworkglobal_p.h>
20#include "private/qcore_unix_p.h"
21
22#include <sys/types.h>
23#include <sys/socket.h>
24#include <netinet/in.h>
25
26#if defined(Q_OS_VXWORKS)
27# include <sockLib.h>
28#endif
29
30// for inet_addr
31#include <netdb.h>
32#include <arpa/inet.h>
33#if defined(Q_OS_VXWORKS)
34# include <hostLib.h>
35#else
36# include <resolv.h>
37#endif
38
40
41// Almost always the same. If not, specify in qplatformdefs.h.
42#if !defined(QT_SOCKOPTLEN_T)
43# define QT_SOCKOPTLEN_T QT_SOCKLEN_T
44#endif
45
46static inline int qt_safe_socket(int domain, int type, int protocol, int flags = 0)
47{
48 Q_ASSERT((flags & ~O_NONBLOCK) == 0);
49
50 int fd;
51#ifdef QT_THREADSAFE_CLOEXEC
52 int newtype = type | SOCK_CLOEXEC;
53 if (flags & O_NONBLOCK)
54 newtype |= SOCK_NONBLOCK;
55 fd = ::socket(domain, newtype, protocol);
56 return fd;
57#else
58 fd = ::socket(domain, type, protocol);
59 if (fd == -1)
60 return -1;
61
62 ::fcntl(fd, F_SETFD, FD_CLOEXEC);
63
64 // set non-block too?
65 if (flags & O_NONBLOCK)
66 ::fcntl(fd, F_SETFL, ::fcntl(fd, F_GETFL) | O_NONBLOCK);
67
68 return fd;
69#endif
70}
71
72static inline int qt_safe_accept(int s, struct sockaddr *addr, QT_SOCKLEN_T *addrlen, int flags = 0)
73{
74 Q_ASSERT((flags & ~O_NONBLOCK) == 0);
75
76 int fd;
77#if QT_CONFIG(accept4)
78 // use accept4
79 int sockflags = SOCK_CLOEXEC;
80 if (flags & O_NONBLOCK)
81 sockflags |= SOCK_NONBLOCK;
82# if defined(Q_OS_NETBSD)
83 fd = ::paccept(s, addr, static_cast<QT_SOCKLEN_T *>(addrlen), NULL, sockflags);
84# else
85 fd = ::accept4(s, addr, static_cast<QT_SOCKLEN_T *>(addrlen), sockflags);
86# endif
87 return fd;
88#else
89 fd = ::accept(s, addr, static_cast<QT_SOCKLEN_T *>(addrlen));
90 if (fd == -1)
91 return -1;
92
93 ::fcntl(fd, F_SETFD, FD_CLOEXEC);
94
95 // set non-block too?
96 if (flags & O_NONBLOCK)
97 ::fcntl(fd, F_SETFL, ::fcntl(fd, F_GETFL) | O_NONBLOCK);
98
99 return fd;
100#endif
101}
102
103static inline int qt_safe_listen(int s, int backlog)
104{
105 return ::listen(s, backlog);
106}
107
108static inline int qt_safe_connect(int sockfd, const struct sockaddr *addr, QT_SOCKLEN_T addrlen)
109{
110 int ret;
111 // Solaris e.g. expects a non-const 2nd parameter
112 QT_EINTR_LOOP(ret, QT_SOCKET_CONNECT(sockfd, const_cast<struct sockaddr *>(addr), addrlen));
113#ifdef Q_OS_WASM
114// ::connect on wasm returns 0 when it shouldn't so use errno instead
115 if (errno != 0)
116 ret = -1;
117#endif
118 return ret;
119}
120#undef QT_SOCKET_CONNECT
121#define QT_SOCKET_CONNECT qt_safe_connect
122
123#if defined(socket)
124# undef socket
125#endif
126#if defined(accept)
127# undef accept
128#endif
129#if defined(listen)
130# undef listen
131#endif
132
133template <typename T>
134static inline int qt_safe_ioctl(int sockfd, unsigned long request, T arg)
135{
136 return ::ioctl(sockfd, request, arg);
137}
138
139static inline int qt_safe_sendmsg(int sockfd, const struct msghdr *msg, int flags)
140{
141#ifdef MSG_NOSIGNAL
142 flags |= MSG_NOSIGNAL;
143#else
144 qt_ignore_sigpipe();
145#endif
146
147 int ret;
148 QT_EINTR_LOOP(ret, ::sendmsg(sockfd, msg, flags));
149 return ret;
150}
151
152static inline int qt_safe_recvmsg(int sockfd, struct msghdr *msg, int flags)
153{
154 int ret;
155
156 QT_EINTR_LOOP(ret, ::recvmsg(sockfd, msg, flags));
157 return ret;
158}
159
160QT_END_NAMESPACE
161
162#endif // QNET_UNIX_P_H
static int qt_safe_ioctl(int sockfd, unsigned long request, T arg)
static int qt_safe_listen(int s, int backlog)
static int qt_safe_sendmsg(int sockfd, const struct msghdr *msg, int flags)
static int qt_safe_socket(int domain, int type, int protocol, int flags=0)
Definition qnet_unix_p.h:46
static int qt_safe_accept(int s, struct sockaddr *addr, QT_SOCKLEN_T *addrlen, int flags=0)
Definition qnet_unix_p.h:72
static int qt_safe_recvmsg(int sockfd, struct msghdr *msg, int flags)
static int qt_safe_connect(int sockfd, const struct sockaddr *addr, QT_SOCKLEN_T addrlen)