Panda3D

netAddress.cxx

00001 // Filename: netAddress.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 "netAddress.h"
00016 #include "config_net.h"
00017 
00018 
00019 ////////////////////////////////////////////////////////////////////
00020 //     Function: NetAddress::Constructor
00021 //       Access: Public
00022 //  Description: Constructs an unspecified address.
00023 ////////////////////////////////////////////////////////////////////
00024 NetAddress::
00025 NetAddress() {
00026 }
00027 
00028 ////////////////////////////////////////////////////////////////////
00029 //     Function: NetAddress::Constructor
00030 //       Access: Public
00031 //  Description: Constructs an address from a given Socket_Address.
00032 //               Normally, this constructor should not be used by user
00033 //               code; instead, create a default NetAddress and use
00034 //               one of the set_*() functions to set up an address.
00035 ////////////////////////////////////////////////////////////////////
00036 NetAddress::
00037 NetAddress(const Socket_Address &addr) : _addr(addr) {
00038 }
00039 
00040 
00041 ////////////////////////////////////////////////////////////////////
00042 //     Function: NetAddress::set_any
00043 //       Access: Public
00044 //  Description: Sets the address up to refer to a particular port,
00045 //               but not to any particular IP.  Returns true if
00046 //               successful, false otherwise (currently, this only
00047 //               returns true).
00048 ////////////////////////////////////////////////////////////////////
00049 bool NetAddress::
00050 set_any(int port) {
00051   return _addr.set_any_IP(port);
00052 }
00053 
00054 ////////////////////////////////////////////////////////////////////
00055 //     Function: NetAddress::set_localhost
00056 //       Access: Public
00057 //  Description: Sets the address up to refer to a particular port,
00058 //               on this host.
00059 ////////////////////////////////////////////////////////////////////
00060 bool NetAddress::
00061 set_localhost(int port) {
00062   return _addr.set_host("127.0.0.1", port);
00063 }
00064 
00065 ////////////////////////////////////////////////////////////////////
00066 //     Function: NetAddress::set_host
00067 //       Access: Public
00068 //  Description: Sets the address up to refer to a particular port
00069 //               on a particular host.  Returns true if the hostname
00070 //               is known, false otherwise.
00071 ////////////////////////////////////////////////////////////////////
00072 bool NetAddress::
00073 set_host(const string &hostname, int port) {
00074   return _addr.set_host(hostname, port);
00075 }
00076 
00077 ////////////////////////////////////////////////////////////////////
00078 //     Function: NetAddress::clear
00079 //       Access: Public
00080 //  Description: Resets the NetAddress to its initial state.
00081 ////////////////////////////////////////////////////////////////////
00082 void NetAddress::
00083 clear() {
00084   _addr.clear();
00085 }
00086 
00087 ////////////////////////////////////////////////////////////////////
00088 //     Function: NetAddress::get_port
00089 //       Access: Public
00090 //  Description: Returns the port number to which this address refers.
00091 ////////////////////////////////////////////////////////////////////
00092 int NetAddress::
00093 get_port() const {
00094   return _addr.get_port();
00095 }
00096 
00097 ////////////////////////////////////////////////////////////////////
00098 //     Function: NetAddress::set_port
00099 //       Access: Public
00100 //  Description: Resets the port number without otherwise changing the
00101 //               address.
00102 ////////////////////////////////////////////////////////////////////
00103 void NetAddress::
00104 set_port(int port) {
00105   _addr.set_port(port);
00106 }
00107 
00108 ////////////////////////////////////////////////////////////////////
00109 //     Function: NetAddress::get_ip_string
00110 //       Access: Public
00111 //  Description: Returns the IP address to which this address refers,
00112 //               formatted as a string.
00113 ////////////////////////////////////////////////////////////////////
00114 string NetAddress::
00115 get_ip_string() const {
00116   return _addr.get_ip();
00117 }
00118 
00119 ////////////////////////////////////////////////////////////////////
00120 //     Function: NetAddress::get_ip
00121 //       Access: Public
00122 //  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::
00126 get_ip() const {
00127   return _addr.GetIPAddressRaw();
00128 }
00129 
00130 ////////////////////////////////////////////////////////////////////
00131 //     Function: NetAddress::get_ip_component
00132 //       Access: Public
00133 //  Description: Returns the nth 8-bit component of the IP address.
00134 //               An IP address has four components; component 0 is the
00135 //               first (leftmost), and component 3 is the last
00136 //               (rightmost) in the dotted number convention.
00137 ////////////////////////////////////////////////////////////////////
00138 PN_uint8 NetAddress::
00139 get_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_addr
00149 //       Access: Public
00150 //  Description: Returns the Socket_Address for this address.
00151 ////////////////////////////////////////////////////////////////////
00152 const Socket_Address &NetAddress::
00153 get_addr() const {
00154   return _addr;
00155 }
00156 
00157 ////////////////////////////////////////////////////////////////////
00158 //     Function: NetAddress::output
00159 //       Access: Public
00160 //  Description:
00161 ////////////////////////////////////////////////////////////////////
00162 void NetAddress::
00163 output(ostream &out) const {
00164   out << get_ip_string();
00165 }
 All Classes Functions Variables Enumerations