Panda3D
|
00001 // Filename: datagramUDPHeader.cxx 00002 // Created by: drose (08Feb00) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "datagramUDPHeader.h" 00016 #include "netDatagram.h" 00017 #include "datagramIterator.h" 00018 #include "config_net.h" 00019 00020 #include "pnotify.h" 00021 00022 //////////////////////////////////////////////////////////////////// 00023 // Function: DatagramUDPHeader::Constructor 00024 // Access: Public 00025 // Description: This constructor creates a header based on an 00026 // already-constructed NetDatagram. 00027 //////////////////////////////////////////////////////////////////// 00028 DatagramUDPHeader:: 00029 DatagramUDPHeader(const NetDatagram &datagram) { 00030 const string &str = datagram.get_message(); 00031 PN_uint16 checksum = 0; 00032 for (size_t p = 0; p < str.size(); p++) { 00033 checksum += (PN_uint16)(PN_uint8)str[p]; 00034 } 00035 00036 // Now pack the header. 00037 _header.add_uint16(checksum); 00038 nassertv((int)_header.get_length() == datagram_udp_header_size); 00039 } 00040 00041 //////////////////////////////////////////////////////////////////// 00042 // Function: DatagramUDPHeader::Constructor 00043 // Access: Public 00044 // Description: This constructor decodes a header from a block of 00045 // data of length datagram_udp_header_size, presumably 00046 // just read from a socket. 00047 //////////////////////////////////////////////////////////////////// 00048 DatagramUDPHeader:: 00049 DatagramUDPHeader(const void *data) : _header(data, datagram_udp_header_size) { 00050 } 00051 00052 //////////////////////////////////////////////////////////////////// 00053 // Function: DatagramUDPHeader::verify_datagram 00054 // Access: Public 00055 // Description: Verifies that the indicated datagram has the 00056 // appropriate length and checksum. Returns true if it 00057 // matches, false otherwise. 00058 //////////////////////////////////////////////////////////////////// 00059 bool DatagramUDPHeader:: 00060 verify_datagram(const NetDatagram &datagram) const { 00061 const string &str = datagram.get_message(); 00062 00063 PN_uint16 checksum = 0; 00064 for (size_t p = 0; p < str.size(); p++) { 00065 checksum += (PN_uint16)(PN_uint8)str[p]; 00066 } 00067 00068 if (checksum == get_datagram_checksum()) { 00069 return true; 00070 } 00071 00072 if (net_cat.is_debug()) { 00073 net_cat.debug() 00074 << "Invalid datagram!\n"; 00075 if (checksum != get_datagram_checksum()) { 00076 net_cat.debug() 00077 << " checksum is " << checksum << ", header reports " 00078 << get_datagram_checksum() << "\n"; 00079 } 00080 00081 // We write the hex dump into a ostringstream first, to guarantee 00082 // an atomic write to the output stream in case we're threaded. 00083 00084 ostringstream hex; 00085 datagram.dump_hex(hex); 00086 hex << "\n"; 00087 net_cat.debug(false) << hex.str(); 00088 } 00089 00090 return false; 00091 }