00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
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
00024
00025
00026
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
00037 _header.add_uint16(checksum);
00038 nassertv((int)_header.get_length() == datagram_udp_header_size);
00039 }
00040
00041
00042
00043
00044
00045
00046
00047
00048 DatagramUDPHeader::
00049 DatagramUDPHeader(const void *data) : _header(data, datagram_udp_header_size) {
00050 }
00051
00052
00053
00054
00055
00056
00057
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
00082
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 }