Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

ConnectionPacket.cpp

Go to the documentation of this file.
00001 #include "headers.h"
00002 
00015 
00016 ConnectionPacket::ConnectionPacket() : NpPacket() {
00017     m_controlType = NpPacket::Connect;
00018         m_headerLength = CP_HEADER_LENGTH;
00019     m_avoidCensoredNodes = false;
00020     m_doFastRouting = false;
00021     m_hasSpecifiedDestination = false;
00022 } // ctor
00023 
00024 
00025 ConnectionPacket::ConnectionPacket(unsigned char* rawData) : NpPacket(rawData) {
00026     // Decode specified destination bit
00027     m_hasSpecifiedDestination = (rawData[OptionsOffset] & SpecifiedDestinationBit) != 0;
00028 
00029     // decode "avoid censored nodes" bit
00030     m_avoidCensoredNodes = (rawData[OptionsOffset] & AvoidCensoredNodesBit) != 0;
00031 
00032     // decode "do fast routing" bit
00033     m_doFastRouting = (rawData[OptionsOffset] & FastRoutingBit) != 0;
00034 
00035     if (m_hasSpecifiedDestination) {
00036         // Decode the port
00037         memcpy(&m_port, &rawData[PortOffset], S_USHORT);
00038         m_port = ntohs(m_port);
00039 
00040         // Set IP version
00041         if ((rawData[OptionsOffset] & IpVersionBit) == 0) {
00042             m_ip.setVersion(IpAddress::IPv4);
00043             
00044             // Decode the IP address
00045             unsigned long tmpIp;
00046             memcpy(&tmpIp, &rawData[IpOffset], S_UINT); 
00047             m_ip.setIpAddress(ntohl(tmpIp));
00048         }
00049         else {
00050             m_ip.setVersion(IpAddress::IPv6);
00051             debug(DEBUG_ERR, "ERROR: Functionality not defined!!!!!!!!!!");
00052         }
00053     }
00054 
00055         m_headerLength = CP_HEADER_LENGTH;
00056 } // ctor
00057 
00058 
00062 unsigned char* 
00063 ConnectionPacket::getRawData() {
00064         unsigned int tmpUint;
00065         unsigned short tmpUshort;
00066 
00067         unsigned char* buf = NpPacket::getRawData();
00068     
00069 
00070     // Encode the options
00071     buf[OptionsOffset] = 0;
00072     buf[OptionsOffset] = buf[OptionsOffset] | (m_hasSpecifiedDestination ? SpecifiedDestinationBit : 0);
00073     buf[OptionsOffset] = buf[OptionsOffset] | (m_avoidCensoredNodes ? AvoidCensoredNodesBit : 0);
00074     buf[OptionsOffset] = buf[OptionsOffset] | (m_doFastRouting ? FastRoutingBit : 0);
00075     buf[OptionsOffset] = buf[OptionsOffset] | ((m_ip.getVersion() == IpAddress::IPv6) ? IpVersionBit : 0);
00076 
00077     if (m_hasSpecifiedDestination) {
00078         // Encode the port
00079         tmpUshort = htons(m_port);
00080         memcpy(&buf[PortOffset], &tmpUshort, S_USHORT);
00081         
00082         // Encode the IP address
00083         if (m_ip.getVersion() == IpAddress::IPv4) {
00084             tmpUint = htonl(m_ip.getUnsignedLongForm());
00085             memcpy(&buf[IpOffset], &tmpUint, S_UINT);
00086         }
00087         else {
00088             debug(DEBUG_ERR, "FUNCTIONALITY NOT DEFINED!!!");
00089         }
00090     }
00091     else {
00092         memset(&buf[PortOffset], 0, 18);
00093     }
00094 
00095     return buf;
00096 } // fn getRawData
00097 
00098 
00099 bool 
00100 ConnectionPacket::hasSpecifiedDestination() {
00101     return m_hasSpecifiedDestination;
00102 } // fn hasSpecifiedDestination
00103 
00104 
00105 void 
00106 ConnectionPacket::setHasSpecifiedDestination(bool value) {
00107     m_hasSpecifiedDestination = value;
00108 } // fn setHasSpecifiedDestination
00109 
00110 
00111 bool 
00112 ConnectionPacket::avoidCensoredNodes() {
00113     return m_avoidCensoredNodes;
00114 } // fn avoidCensoredNodes
00115 
00116 
00117 void 
00118 ConnectionPacket::setAvoidCensoredNodes(bool value) {
00119     m_avoidCensoredNodes = value;
00120 } // fn setAvoidCensoredNodes
00121 
00122 
00123 bool 
00124 ConnectionPacket::doFastRouting() {
00125     return m_doFastRouting;
00126 } // fn doFastRouting
00127 
00128 
00129 void 
00130 ConnectionPacket::setDoFastRouting(bool value) {
00131     m_doFastRouting = value;
00132 } // fn setDoFastRouting
00133 
00134 
00135 SocketAddress
00136 ConnectionPacket::getSocketAddress() {
00137     SocketAddress s;
00138     s.setIpAddress(m_ip);
00139     s.setPort(m_port);
00140     return s;
00141 }
00142 
00143 
00144 void
00145 ConnectionPacket::setSocketAddress(SocketAddress* value) {
00146     m_ip = value->getIpAddress();
00147     m_port = value->getPort();
00148 }
00149 
00150 
00151 void
00152 ConnectionPacket::toStream(std::ostream& out) {
00153     SocketAddress s;
00154 
00155     NpPacket::toStream(out);
00156     out << "Has Specified Destination ?= " << hasSpecifiedDestination() << "\n";
00157     out << "Avoid Censored Nodes      ?= " << avoidCensoredNodes() << "\n";
00158     out << "Do Fast Routing           ?= " << doFastRouting() << "\n";
00159     if (hasSpecifiedDestination()) {
00160         s = getSocketAddress(); 
00161         out << "Socket address             = " << s << "\n";
00162     }
00163         out << "\n";
00164 } // fn toStream
00165 
00166 

Generated at Thu Jul 11 13:31:50 2002 for Peekabooty by doxygen1.2.9 written by Dimitri van Heesch, © 1997-2001