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 if (auto accept4 = QT_WEAK_SYMBOL(::accept4)) {
79 // use accept4
80 int sockflags = SOCK_CLOEXEC;
81 if (flags & O_NONBLOCK)
82 sockflags |= SOCK_NONBLOCK;
83 fd = accept4(s, addr, static_cast<QT_SOCKLEN_T *>(addrlen), sockflags);
84 return fd;
85 }
86#endif
87
88 fd = ::accept(s, addr, static_cast<QT_SOCKLEN_T *>(addrlen));
89 if (fd == -1)
90 return -1;
91
92 ::fcntl(fd, F_SETFD, FD_CLOEXEC);
93
94 // set non-block too?
95 if (flags & O_NONBLOCK)
96 ::fcntl(fd, F_SETFL, ::fcntl(fd, F_GETFL) | O_NONBLOCK);
97
98 return fd;
99}
100
101static inline int qt_safe_listen(int s, int backlog)
102{
103 return ::listen(s, backlog);
104}
105
106static inline int qt_safe_connect(int sockfd, const struct sockaddr *addr, QT_SOCKLEN_T addrlen)
107{
108 int ret;
109#ifdef Q_OS_WASM
110 errno = 0;
111#endif
112 // Solaris e.g. expects a non-const 2nd parameter
113 QT_EINTR_LOOP(ret, QT_SOCKET_CONNECT(sockfd, const_cast<struct sockaddr *>(addr), addrlen));
114#ifdef Q_OS_WASM
115// ::connect on wasm returns 0 when it shouldn't so use errno instead
116 if (errno != 0)
117 ret = -1;
118#endif
119 return ret;
120}
121#undef QT_SOCKET_CONNECT
122#define QT_SOCKET_CONNECT qt_safe_connect
123
124#if defined(socket)
125# undef socket
126#endif
127#if defined(accept)
128# undef accept
129#endif
130#if defined(listen)
131# undef listen
132#endif
133
134template <typename T>
135static inline int qt_safe_ioctl(int sockfd, unsigned long request, T arg)
136{
137 return ::ioctl(sockfd, request, arg);
138}
139
140static inline int qt_safe_sendmsg(int sockfd, const struct msghdr *msg, int flags)
141{
142#ifdef MSG_NOSIGNAL
143 flags |= MSG_NOSIGNAL;
144#else
145 qt_ignore_sigpipe();
146#endif
147
148 int ret;
149 QT_EINTR_LOOP(ret, ::sendmsg(sockfd, msg, flags));
150 return ret;
151}
152
153static inline int qt_safe_recvmsg(int sockfd, struct msghdr *msg, int flags)
154{
155 int ret;
156
157 QT_EINTR_LOOP(ret, ::recvmsg(sockfd, msg, flags));
158 return ret;
159}
160
161QT_END_NAMESPACE
162
163#endif // QNET_UNIX_P_H
Combined button and popup list for selecting options.
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)