Panda3D

datagramUDPHeader.cxx
Go to the documentation of this file.
1 /**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file datagramUDPHeader.cxx
10 * @author drose
11 * @date 2000-02-08
12 */
13
14 #include "datagramUDPHeader.h"
15 #include "netDatagram.h"
16 #include "datagramIterator.h"
17 #include "config_net.h"
18
19 #include "pnotify.h"
20
21 /**
22 * This constructor creates a header based on an already-constructed
23 * NetDatagram.
24 */
25 DatagramUDPHeader::
26 DatagramUDPHeader(const NetDatagram &datagram) {
27 const unsigned char *begin = (const unsigned char *)datagram.get_data();
28 const unsigned char *end = begin + datagram.get_length();
29 uint16_t checksum = 0;
30 for (const unsigned char *p = begin; p != end; ++p) {
31 checksum += (uint16_t)(uint8_t)*p;
32 }
33
34 // Now pack the header.
35 _header.add_uint16(checksum);
36 nassertv((int)_header.get_length() == datagram_udp_header_size);
37 }
38
39 /**
40 * This constructor decodes a header from a block of data of length
41 * datagram_udp_header_size, presumably just read from a socket.
42 */
43 DatagramUDPHeader::
44 DatagramUDPHeader(const void *data) : _header(data, datagram_udp_header_size) {
45 }
46
47 /**
48 * Verifies that the indicated datagram has the appropriate length and
49 * checksum. Returns true if it matches, false otherwise.
50 */
51 bool DatagramUDPHeader::
52 verify_datagram(const NetDatagram &datagram) const {
53 const unsigned char *begin = (const unsigned char *)datagram.get_data();
54 const unsigned char *end = begin + datagram.get_length();
55 uint16_t checksum = 0;
56 for (const unsigned char *p = begin; p != end; ++p) {
57 checksum += (uint16_t)(uint8_t)*p;
58 }
59
60 if (checksum == get_datagram_checksum()) {
61 return true;
62 }
63
64 if (net_cat.is_debug()) {
65 net_cat.debug()
66 << "Invalid datagram!\n";
67 if (checksum != get_datagram_checksum()) {
68 net_cat.debug()
69 << " checksum is " << checksum << ", header reports "
70 << get_datagram_checksum() << "\n";
71 }
72
73 // We write the hex dump into a ostringstream first, to guarantee an
74 // atomic write to the output stream in case we're threaded.
75
76 std::ostringstream hex;
77 datagram.dump_hex(hex);
78 hex << "\n";
79 net_cat.debug(false) << hex.str();
80 }
81
82 return false;
83 }
DatagramUDPHeader::DatagramUDPHeader
DatagramUDPHeader(const NetDatagram &datagram)
This constructor creates a header based on an already-constructed NetDatagram.
Definition: datagramUDPHeader.cxx:26
DatagramUDPHeader::get_datagram_checksum
int get_datagram_checksum() const
Returns the checksum appropriate for the indicated datagram.
Definition: datagramUDPHeader.I:18
DatagramUDPHeader::verify_datagram
bool verify_datagram(const NetDatagram &datagram) const
Verifies that the indicated datagram has the appropriate length and checksum.
Definition: datagramUDPHeader.cxx:52
Datagram::get_length
size_t get_length() const
Returns the number of bytes in the datagram.
Definition: datagram.I:335
Datagram::dump_hex
void dump_hex(std::ostream &out, unsigned int indent=0) const
Writes a representation of the entire datagram contents, as a sequence of hex (and ASCII) values.
Definition: datagram.cxx:44
Datagram::add_uint16
void add_uint16(uint16_t value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:85
Datagram::get_data
const void * get_data() const
Returns a pointer to the beginning of the datagram's data.
Definition: datagram.I:327
NetDatagram
A specific kind of Datagram, especially for sending across or receiving from a network.
Definition: netDatagram.h:40
config_net.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
datagramIterator.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
datagramUDPHeader.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
netDatagram.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pnotify.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.