00001 // Filename: netAddress.cxx00002 // Created by: drose (08Feb00)00003 //00004 ////////////////////////////////////////////////////////////////////00005 //00006 // PANDA 3D SOFTWARE00007 // Copyright (c) Carnegie Mellon University. All rights reserved.00008 //00009 // All use of this software is subject to the terms of the revised BSD00010 // license. You should have received a copy of this license along00011 // with this source code in a file named "LICENSE."00012 //00013 ////////////////////////////////////////////////////////////////////00014
00015 #include "netAddress.h"00016 #include "config_net.h"00017
00018
00019 ////////////////////////////////////////////////////////////////////00020 // Function: NetAddress::Constructor00021 // Access: Public00022 // Description: Constructs an unspecified address.00023 ////////////////////////////////////////////////////////////////////00024 NetAddress::00025NetAddress() {
00026 }
00027
00028 ////////////////////////////////////////////////////////////////////00029 // Function: NetAddress::Constructor00030 // Access: Public00031 // Description: Constructs an address from a given Socket_Address.00032 // Normally, this constructor should not be used by user00033 // code; instead, create a default NetAddress and use00034 // one of the set_*() functions to set up an address.00035 ////////////////////////////////////////////////////////////////////00036 NetAddress::00037NetAddress(constSocket_Address &addr) : _addr(addr) {
00038 }
00039
00040
00041 ////////////////////////////////////////////////////////////////////00042 // Function: NetAddress::set_any00043 // Access: Public00044 // Description: Sets the address up to refer to a particular port,00045 // but not to any particular IP. Returns true if00046 // successful, false otherwise (currently, this only00047 // returns true).00048 ////////////////////////////////////////////////////////////////////00049 boolNetAddress::00050set_any(int port) {
00051 return _addr.set_any_IP(port);
00052 }
00053
00054 ////////////////////////////////////////////////////////////////////00055 // Function: NetAddress::set_localhost00056 // Access: Public00057 // Description: Sets the address up to refer to a particular port,00058 // on this host.00059 ////////////////////////////////////////////////////////////////////00060 boolNetAddress::00061set_localhost(int port) {
00062 return _addr.set_host("127.0.0.1", port);
00063 }
00064
00065 ////////////////////////////////////////////////////////////////////00066 // Function: NetAddress::set_host00067 // Access: Public00068 // Description: Sets the address up to refer to a particular port00069 // on a particular host. Returns true if the hostname00070 // is known, false otherwise.00071 ////////////////////////////////////////////////////////////////////00072 boolNetAddress::00073set_host(conststring &hostname, int port) {
00074 return _addr.set_host(hostname, port);
00075 }
00076
00077 ////////////////////////////////////////////////////////////////////00078 // Function: NetAddress::clear00079 // Access: Public00080 // Description: Resets the NetAddress to its initial state.00081 ////////////////////////////////////////////////////////////////////00082 voidNetAddress::00083clear() {
00084 _addr.clear();
00085 }
00086
00087 ////////////////////////////////////////////////////////////////////00088 // Function: NetAddress::get_port00089 // Access: Public00090 // Description: Returns the port number to which this address refers.00091 ////////////////////////////////////////////////////////////////////00092 intNetAddress::00093get_port() const {
00094 return _addr.get_port();
00095 }
00096
00097 ////////////////////////////////////////////////////////////////////00098 // Function: NetAddress::set_port00099 // Access: Public00100 // Description: Resets the port number without otherwise changing the00101 // address.00102 ////////////////////////////////////////////////////////////////////00103 voidNetAddress::00104set_port(int port) {
00105 _addr.set_port(port);
00106 }
00107
00108 ////////////////////////////////////////////////////////////////////00109 // Function: NetAddress::get_ip_string00110 // Access: Public00111 // Description: Returns the IP address to which this address refers,00112 // formatted as a string.00113 ////////////////////////////////////////////////////////////////////00114 stringNetAddress::00115get_ip_string() const {
00116 return _addr.get_ip();
00117 }
00118
00119 ////////////////////////////////////////////////////////////////////00120 // Function: NetAddress::get_ip00121 // Access: Public00122 // Description: Returns the IP address to which this address refers,00123 // as a 32-bit integer, in host byte order.00124 ////////////////////////////////////////////////////////////////////00125 PN_uint32 NetAddress::00126get_ip() const {
00127 return _addr.GetIPAddressRaw();
00128 }
00129
00130 ////////////////////////////////////////////////////////////////////00131 // Function: NetAddress::get_ip_component00132 // Access: Public00133 // Description: Returns the nth 8-bit component of the IP address.00134 // An IP address has four components; component 0 is the00135 // first (leftmost), and component 3 is the last00136 // (rightmost) in the dotted number convention.00137 ////////////////////////////////////////////////////////////////////00138 PN_uint8 NetAddress::00139get_ip_component(int n) const {
00140 nassertr(n >= 0 && n < 4, 0);
00141 PN_uint32 ip_long = _addr.GetIPAddressRaw();
00142 const PN_uint8 *ip = (const PN_uint8 *)&ip_long;
00143 return ip[n];
00144 }
00145
00146
00147 ////////////////////////////////////////////////////////////////////00148 // Function: NetAddress::get_addr00149 // Access: Public00150 // Description: Returns the Socket_Address for this address.00151 ////////////////////////////////////////////////////////////////////00152 constSocket_Address &NetAddress::00153get_addr() const {
00154 return _addr;
00155 }
00156
00157 ////////////////////////////////////////////////////////////////////00158 // Function: NetAddress::output00159 // Access: Public00160 // Description:00161 ////////////////////////////////////////////////////////////////////00162 void NetAddress::
00163 output(ostream &out) const {
00164 out << get_ip_string();
00165 }