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
qnetworkdatagram.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 Intel Corporation.
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
7
8#ifndef QT_NO_UDPSOCKET
9
11
12QT_IMPL_METATYPE_EXTERN(QNetworkDatagram)
13
14/*!
15 \class QNetworkDatagram
16 \brief The QNetworkDatagram class provides the data and metadata of a UDP datagram.
17 \since 5.8
18 \ingroup network
19 \inmodule QtNetwork
20 \reentrant
21
22 QNetworkDatagram can be used with the \l QUdpSocket class to represent the full
23 information contained in a UDP (User Datagram Protocol) datagram.
24 QNetworkDatagram encapsulates the following information of a datagram:
25 \list
26 \li the payload data;
27 \li the sender address and port number;
28 \li the destination address and port number;
29 \li the remaining hop count limit (on IPv4, this field is usually called "time to live" - TTL);
30 \li the network interface index the datagram was received on or to be sent on.
31 \endlist
32
33 QUdpSocket will try to match a common behavior as much as possible on all
34 operating systems, but not all of the metadata above can be obtained in
35 some operating systems. Metadata that cannot be set on the datagram when
36 sending with QUdpSocket::writeDatagram() will be silently discarded.
37
38 Upon reception, the senderAddress() and senderPort() properties contain the
39 address and port of the peer that sent the datagram, while
40 destinationAddress() and destinationPort() contain the target that was
41 contained in the datagram. That is usually an address local to the current
42 machine, but it can also be an IPv4 broadcast address (such as
43 "255.255.255.255") or an IPv4 or IPv6 multicast address. Applications may
44 find it useful to determine if the datagram was sent specifically to this
45 machine via unicast addressing or whether it was sent to multiple destinations.
46
47 When sending, the senderAddress() and senderPort() should contain the local
48 address to be used when sending. The sender address must be an address that
49 is assigned to this machine, which can be obtained using
50 \l{QNetworkInterface}, and the port number must be the port number that the
51 socket is bound to. Either field can be left unset and will be filled in by
52 the operating system with default values. The destinationAddress() and
53 destinationPort() fields may be set to a target address different from the
54 one the UDP socket is currently associated with.
55
56 Usually, when sending a datagram in reply to a datagram previously
57 received, one will set the destinationAddress() to be the senderAddress()
58 of the incoming datagram and similarly for the port numbers. To facilitate
59 this common process, QNetworkDatagram provides the function makeReply().
60
61 The hopCount() function contains, for a received datagram, the remaining
62 hop count limit for the packet. When sending, it contains the hop count
63 limit to be set. Most protocols will leave this value set to the default
64 and let the operating system decide on the best value to be used.
65 Multicasting over IPv4 often uses this field to indicate the scope of the
66 multicast group (link-local, local to an organization or global).
67
68 The interfaceIndex() function contains the index of the operating system's
69 interface that received the packet. This value is the same one that can be
70 set on a QHostAddress::scopeId() property and matches the
71 QNetworkInterface::index() property. When sending packets to global
72 addresses, it is not necessary to set the interface index as the operating
73 system will choose the correct one using the system routing table. This
74 property is important when sending datagrams to link-local destinations,
75 whether unicast or multicast.
76
77 \section1 Feature support
78
79 Some features of QNetworkDatagram are not supported in all operating systems.
80 Only the address and ports of the remote host (sender in received packets
81 and destination for outgoing packets) are supported in all systems. On most
82 operating systems, the other features are supported only for IPv6. Software
83 should check at runtime whether the rest could be determined for IPv4
84 addresses.
85
86 The current feature support is as follows:
87
88 \table
89 \header \li Operating system \li Local address \li Hop count \li Interface index
90 \row \li FreeBSD \li Supported \li Supported \li Only for IPv6
91 \row \li Linux \li Supported \li Supported \li Supported
92 \row \li OS X \li Supported \li Supported \li Only for IPv6
93 \row \li Other Unix supporting RFC 3542 \li Only for IPv6 \li Only for IPv6 \li Only for IPv6
94 \row \li Windows (desktop) \li Supported \li Supported \li Supported
95 \row \li Windows RT \li Not supported \li Not supported \li Not supported
96 \endtable
97
98 \sa QUdpSocket, QNetworkInterface
99 */
100
101/*!
102 Creates a QNetworkDatagram object with no payload data and undefined destination address.
103
104 The payload can be modified by using setData() and the destination address
105 can be set with setDestination().
106
107 If the destination address is left undefined, QUdpSocket::writeDatagram()
108 will attempt to send the datagram to the address last associated with, by
109 using QUdpSocket::connectToHost().
110 */
111QNetworkDatagram::QNetworkDatagram()
112 : d(new QNetworkDatagramPrivate)
113{
114}
115
116/*!
117 Creates a QNetworkDatagram object and sets \a data as the payload data, along with
118 \a destinationAddress and \a port as the destination address of the datagram.
119 */
120QNetworkDatagram::QNetworkDatagram(const QByteArray &data, const QHostAddress &destinationAddress, quint16 port)
121 : d(new QNetworkDatagramPrivate(data, destinationAddress, port))
122{
123}
124
125/*!
126 Creates a copy of the \a other datagram, including the payload and metadata.
127
128 To create a datagram suitable for sending in a reply, use QNetworkDatagram::makeReply();
129 */
130QNetworkDatagram::QNetworkDatagram(const QNetworkDatagram &other)
131 : d(new QNetworkDatagramPrivate(*other.d))
132{
133}
134
135/*! \internal */
136QNetworkDatagram::QNetworkDatagram(QNetworkDatagramPrivate &dd)
137 : d(&dd)
138{
139}
140
141/*!
142 Copies the \a other datagram, including the payload and metadata.
143
144 To create a datagram suitable for sending in a reply, use QNetworkDatagram::makeReply();
145 */
146QNetworkDatagram &QNetworkDatagram::operator=(const QNetworkDatagram &other)
147{
148 *d = *other.d;
149 return *this;
150}
151
152/*!
153 Clears the payload data and metadata in this QNetworkDatagram object, resetting
154 them to their default values.
155 */
156void QNetworkDatagram::clear()
157{
158 d->data.clear();
159 d->header.senderAddress.clear();
160 d->header.destinationAddress.clear();
161 d->header.hopLimit = -1;
162 d->header.ifindex = 0;
163}
164
165/*!
166 \fn QNetworkDatagram::isNull() const
167 Returns true if this QNetworkDatagram object is null. This function is the
168 opposite of isValid().
169 */
170
171/*!
172 Returns true if this QNetworkDatagram object is valid. A valid QNetworkDatagram
173 object contains at least one sender or receiver address. Valid datagrams
174 can contain empty payloads.
175 */
176bool QNetworkDatagram::isValid() const
177{
178 return d->header.senderAddress.protocol() != QAbstractSocket::UnknownNetworkLayerProtocol ||
179 d->header.destinationAddress.protocol() != QAbstractSocket::UnknownNetworkLayerProtocol;
180}
181
182/*!
183 Returns the sender address associated with this datagram. For a datagram
184 received from the network, it is the address of the peer node that sent the
185 datagram. For an outgoing datagrams, it is the local address to be used
186 when sending.
187
188 If no sender address was set on this datagram, the returned object will
189 report true to QHostAddress::isNull().
190
191 \sa destinationAddress(), senderPort(), setSender()
192*/
193QHostAddress QNetworkDatagram::senderAddress() const
194{
195 return d->header.senderAddress;
196}
197
198/*!
199 Returns the destination address associated with this datagram. For a
200 datagram received from the network, it is the address the peer node sent
201 the datagram to, which can either be a local address of this machine or a
202 multicast or broadcast address. For an outgoing datagrams, it is the
203 address the datagram should be sent to.
204
205 If no destination address was set on this datagram, the returned object
206 will report true to QHostAddress::isNull().
207
208 \sa senderAddress(), destinationPort(), setDestination()
209*/
210QHostAddress QNetworkDatagram::destinationAddress() const
211{
212 return d->header.destinationAddress;
213}
214
215/*!
216 Returns the port number of the sender associated with this datagram. For a
217 datagram received from the network, it is the port number that the peer
218 node sent the datagram from. For an outgoing datagram, it is the local port
219 the datagram should be sent from.
220
221 If no sender address was associated with this datagram, this function
222 returns -1.
223
224 \sa senderAddress(), destinationPort(), setSender()
225*/
226int QNetworkDatagram::senderPort() const
227{
228 return d->header.senderAddress.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol
229 ? -1 : d->header.senderPort;
230}
231
232/*!
233 Returns the port number of the destination associated with this datagram.
234 For a datagram received from the network, it is the local port number that
235 the peer node sent the datagram to. For an outgoing datagram, it is the
236 peer port the datagram should be sent to.
237
238 If no destination address was associated with this datagram, this function
239 returns -1.
240
241 \sa destinationAddress(), senderPort(), setDestination()
242*/
243int QNetworkDatagram::destinationPort() const
244{
245 return d->header.destinationAddress.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol
246 ? -1 : d->header.destinationPort;
247}
248
249/*!
250 Sets the sender address associated with this datagram to be the address \a
251 address and port number \a port. The sender address and port numbers are
252 usually set by \l QUdpSocket upon reception, so there's no need to call
253 this function on a received datagram.
254
255 For outgoing datagrams, this function can be used to set the address the
256 datagram should carry. The address \a address must usually be one of the
257 local addresses assigned to this machine, which can be obtained using \l
258 QNetworkInterface. If left unset, the operating system will choose the most
259 appropriate address to use given the destination in question.
260
261 The port number \a port must be the port number associated with the socket,
262 if there is one. The value of 0 can be used to indicate that the operating
263 system should choose the port number.
264
265 \sa QUdpSocket::writeDatagram(), senderAddress(), senderPort(), setDestination()
266 */
267void QNetworkDatagram::setSender(const QHostAddress &address, quint16 port)
268{
269 d->header.senderAddress = address;
270 d->header.senderPort = port;
271}
272
273/*!
274 Sets the destination address associated with this datagram to be the
275 address \a address and port number \a port. The destination address and
276 port numbers are usually set by \l QUdpSocket upon reception, so there's no
277 need to call this function on a received datagram.
278
279 For outgoing datagrams, this function can be used to set the address the
280 datagram should be sent to. It can be the unicast address used to
281 communicate with the peer or a broadcast or multicast address to send to a
282 group of devices.
283
284 \sa QUdpSocket::writeDatagram(), destinationAddress(), destinationPort(), setSender()
285 */
286void QNetworkDatagram::setDestination(const QHostAddress &address, quint16 port)
287{
288 d->header.destinationAddress = address;
289 d->header.destinationPort = port;
290}
291
292/*!
293 Returns the hop count limit associated with this datagram. The hop count
294 limit is the number of nodes that are allowed to forward the IP packet
295 before it expires and an error is sent back to the sender of the datagram.
296 In IPv4, this value is usually known as "time to live" (TTL).
297
298 If this datagram was received from the network, this is the remaining hop
299 count of the datagram after reception and was decremented by 1 by each node
300 that forwarded the packet. A value of -1 indicates that the hop limit count
301 not be obtained.
302
303 If this is an outgoing datagram, this is the value to be set in the IP header
304 upon sending. A value of -1 indicates the operating system should choose
305 the value.
306
307 \sa setHopLimit()
308 */
309int QNetworkDatagram::hopLimit() const
310{
311 return d->header.hopLimit;
312}
313
314/*!
315 Sets the hop count limit associated with this datagram to \a count. The hop
316 count limit is the number of nodes that are allowed to forward the IP
317 packet before it expires and an error is sent back to the sender of the
318 datagram. In IPv4, this value is usually known as "time to live" (TTL).
319
320 It is usually not necessary to call this function on datagrams received
321 from the network.
322
323 If this is an outgoing packet, this is the value to be set in the IP header
324 upon sending. The valid range for the value is 1 to 255. This function also
325 accepts a value of -1 to indicate that the operating system should choose
326 the value.
327
328 \sa hopLimit()
329 */
330void QNetworkDatagram::setHopLimit(int count)
331{
332 d->header.hopLimit = count;
333}
334
335/*!
336 Returns the interface index this datagram is associated with. The interface
337 index is a positive number that uniquely identifies the network interface
338 in the operating system. This number matches the value returned by
339 QNetworkInterface::index() for the interface.
340
341 If this datagram was received from the network, this is the index of the
342 interface that the packet was received from. If this is an outgoing
343 datagram, this is the index of the interface that the datagram should be
344 sent on.
345
346 A value of 0 indicates that the interface index is unknown.
347
348 \sa setInterfaceIndex()
349 */
350uint QNetworkDatagram::interfaceIndex() const
351{
352 return d->header.ifindex;
353}
354
355/*!
356 Sets the interface index this datagram is associated with to \a index. The
357 interface index is a positive number that uniquely identifies the network
358 interface in the operating system. This number matches the value returned
359 by QNetworkInterface::index() for the interface.
360
361 It is usually not necessary to call this function on datagrams received
362 from the network.
363
364 If this is an outgoing packet, this is the index of the interface the
365 datagram should be sent on. A value of 0 indicates that the operating
366 system should choose the interface based on other factors.
367
368 Note that the interface index can also be set with
369 QHostAddress::setScopeId() for IPv6 destination addresses and then with
370 setDestination(). If the scope ID set in the destination address and \a
371 index are different and neither is zero, it is undefined which interface
372 the operating system will send the datagram on.
373
374 \sa interfaceIndex()
375 */
376void QNetworkDatagram::setInterfaceIndex(uint index)
377{
378 d->header.ifindex = index;
379}
380
381/*!
382 Returns the data payload of this datagram. For a datagram received from the
383 network, it contains the payload of the datagram. For an outgoing datagram,
384 it is the datagram to be sent.
385
386 Note that datagrams can be transmitted with no data, so the returned
387 QByteArray may be empty.
388
389 \sa setData()
390 */
391QByteArray QNetworkDatagram::data() const
392{
393 return d->data;
394}
395
396/*!
397 Sets the data payload of this datagram to \a data. It is usually not
398 necessary to call this function on received datagrams. For outgoing
399 datagrams, this function sets the data to be sent on the network.
400
401 Since datagrams can empty, an empty QByteArray is a valid value for \a
402 data.
403
404 \sa data()
405 */
406void QNetworkDatagram::setData(const QByteArray &data)
407{
408 d->data = data;
409}
410
411/*!
412 \fn QNetworkDatagram QNetworkDatagram::makeReply(const QByteArray &payload) const &
413 \fn QNetworkDatagram QNetworkDatagram::makeReply(const QByteArray &payload) &&
414
415 Creates a new QNetworkDatagram representing a reply to this incoming datagram
416 and sets the payload data to \a payload. This function is a very convenient
417 way of responding to a datagram back to the original sender.
418
419 Example:
420 \snippet code/src_network_kernel_qnetworkdatagram.cpp 0
421
422 This function is especially convenient since it will automatically copy
423 parameters from this datagram to the new datagram as appropriate:
424
425 \list
426 \li this datagram's sender address and port are copied to the new
427 datagram's destination address and port;
428 \li this datagram's interface index, if any, is copied to the new
429 datagram's interface index;
430 \li this datagram's destination address and port are copied to the new
431 datagram's sender address and port only if the address is IPv6
432 global (non-multicast) address;
433 \li the hop count limit on the new datagram is reset to the default (-1);
434 \endlist
435
436 If QNetworkDatagram is modified in a future version of Qt to carry further
437 metadata, this function will copy that metadata as appropriate.
438
439 This datagram's destination address is not copied if it is an IPv4 address
440 because it is not possible to tell an IPv4 broadcast address apart from a
441 regular IPv4 address without an exhaustive search of all addresses assigned
442 to this machine. Attempting to send a datagram with the sender address
443 equal to the broadcast address is likely to fail. However, this should not
444 affect the communication as network interfaces with multiple IPv4 addresses
445 are uncommon, so the address the operating system will select will likely
446 be one the peer will understand.
447
448 \note This function comes with both rvalue- and lvalue-reference qualifier
449 overloads, so it is a good idea to make sure this object is rvalue, if
450 possible, before calling makeReply, so as to make better use of move
451 semantics. To achieve that, the example above would use:
452 \snippet code/src_network_kernel_qnetworkdatagram.cpp 1
453 */
454
455
456static bool isNonMulticast(const QHostAddress &addr)
457{
458 // is it a multicast address?
459 return !addr.isMulticast();
460}
461
462QNetworkDatagram QNetworkDatagram::makeReply_helper(const QByteArray &data) const
463{
464 QNetworkDatagramPrivate *x = new QNetworkDatagramPrivate(data, d->header.senderAddress, d->header.senderPort);
465 x->header.ifindex = d->header.ifindex;
466 if (isNonMulticast(d->header.destinationAddress)) {
467 x->header.senderAddress = d->header.destinationAddress;
468 x->header.senderPort = d->header.destinationPort;
469 }
470 return QNetworkDatagram(*x);
471}
472
473void QNetworkDatagram::makeReply_helper_inplace(const QByteArray &data)
474{
475 d->data = data;
476 d->header.hopLimit = -1;
477 qSwap(d->header.destinationPort, d->header.senderPort);
478 qSwap(d->header.destinationAddress, d->header.senderAddress);
479 if (!isNonMulticast(d->header.senderAddress))
480 d->header.senderAddress.clear();
481}
482
483void QNetworkDatagram::destroy(QNetworkDatagramPrivate *d)
484{
485 Q_ASSERT(d);
486 delete d;
487}
488
489/*! \fn void QNetworkDatagram::swap(QNetworkDatagram &other)
490 \memberswap{datagram}
491*/
492
493
494QT_END_NAMESPACE
495
496#endif // QT_NO_UDPSOCKET
static bool isNonMulticast(const QHostAddress &addr)