Panda3D
netAddress.cxx
1 // Filename: netAddress.cxx
2 // Created by: drose (08Feb00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "netAddress.h"
16 #include "config_net.h"
17 
18 
19 ////////////////////////////////////////////////////////////////////
20 // Function: NetAddress::Constructor
21 // Access: Published
22 // Description: Constructs an unspecified address.
23 ////////////////////////////////////////////////////////////////////
26 }
27 
28 ////////////////////////////////////////////////////////////////////
29 // Function: NetAddress::Constructor
30 // Access: Published
31 // Description: Constructs an address from a given Socket_Address.
32 // Normally, this constructor should not be used by user
33 // code; instead, create a default NetAddress and use
34 // one of the set_*() functions to set up an address.
35 ////////////////////////////////////////////////////////////////////
37 NetAddress(const Socket_Address &addr) : _addr(addr) {
38 }
39 
40 
41 ////////////////////////////////////////////////////////////////////
42 // Function: NetAddress::set_any
43 // Access: Published
44 // Description: Sets the address up to refer to a particular port,
45 // but not to any particular IP. Returns true if
46 // successful, false otherwise (currently, this only
47 // returns true).
48 ////////////////////////////////////////////////////////////////////
49 bool NetAddress::
50 set_any(int port) {
51  return _addr.set_any_IP(port);
52 }
53 
54 ////////////////////////////////////////////////////////////////////
55 // Function: NetAddress::set_localhost
56 // Access: Published
57 // Description: Sets the address up to refer to a particular port,
58 // on this host.
59 ////////////////////////////////////////////////////////////////////
60 bool NetAddress::
61 set_localhost(int port) {
62  return _addr.set_host("127.0.0.1", port);
63 }
64 
65 ////////////////////////////////////////////////////////////////////
66 // Function: NetAddress::set_broadcast
67 // Access: Published
68 // Description: Sets the address to the broadcast address.
69 ////////////////////////////////////////////////////////////////////
70 bool NetAddress::
71 set_broadcast(int port) {
72  return _addr.set_broadcast(port);
73 }
74 
75 ////////////////////////////////////////////////////////////////////
76 // Function: NetAddress::set_host
77 // Access: Published
78 // Description: Sets the address up to refer to a particular port
79 // on a particular host. Returns true if the hostname
80 // is known, false otherwise.
81 ////////////////////////////////////////////////////////////////////
82 bool NetAddress::
83 set_host(const string &hostname, int port) {
84  return _addr.set_host(hostname, port);
85 }
86 
87 ////////////////////////////////////////////////////////////////////
88 // Function: NetAddress::clear
89 // Access: Published
90 // Description: Resets the NetAddress to its initial state.
91 ////////////////////////////////////////////////////////////////////
92 void NetAddress::
93 clear() {
94  _addr.clear();
95 }
96 
97 ////////////////////////////////////////////////////////////////////
98 // Function: NetAddress::get_port
99 // Access: Published
100 // Description: Returns the port number to which this address refers.
101 ////////////////////////////////////////////////////////////////////
102 int NetAddress::
103 get_port() const {
104  return _addr.get_port();
105 }
106 
107 ////////////////////////////////////////////////////////////////////
108 // Function: NetAddress::set_port
109 // Access: Published
110 // Description: Resets the port number without otherwise changing the
111 // address.
112 ////////////////////////////////////////////////////////////////////
113 void NetAddress::
114 set_port(int port) {
115  _addr.set_port(port);
116 }
117 
118 ////////////////////////////////////////////////////////////////////
119 // Function: NetAddress::get_ip_string
120 // Access: Published
121 // Description: Returns the IP address to which this address refers,
122 // formatted as a string.
123 ////////////////////////////////////////////////////////////////////
124 string NetAddress::
125 get_ip_string() const {
126  return _addr.get_ip();
127 }
128 
129 ////////////////////////////////////////////////////////////////////
130 // Function: NetAddress::get_ip
131 // Access: Published
132 // Description: Returns the IP address to which this address refers,
133 // as a 32-bit integer, in host byte order.
134 ////////////////////////////////////////////////////////////////////
135 PN_uint32 NetAddress::
136 get_ip() const {
137  return _addr.GetIPAddressRaw();
138 }
139 
140 ////////////////////////////////////////////////////////////////////
141 // Function: NetAddress::get_ip_component
142 // Access: Published
143 // Description: Returns the nth 8-bit component of the IP address.
144 // An IP address has four components; component 0 is the
145 // first (leftmost), and component 3 is the last
146 // (rightmost) in the dotted number convention.
147 ////////////////////////////////////////////////////////////////////
148 PN_uint8 NetAddress::
149 get_ip_component(int n) const {
150  nassertr(n >= 0 && n < 4, 0);
151  PN_uint32 ip_long = _addr.GetIPAddressRaw();
152  const PN_uint8 *ip = (const PN_uint8 *)&ip_long;
153  return ip[n];
154 }
155 
156 
157 ////////////////////////////////////////////////////////////////////
158 // Function: NetAddress::get_addr
159 // Access: Published
160 // Description: Returns the Socket_Address for this address.
161 ////////////////////////////////////////////////////////////////////
163 get_addr() const {
164  return _addr;
165 }
166 
167 ////////////////////////////////////////////////////////////////////
168 // Function: NetAddress::output
169 // Access: Published
170 // Description:
171 ////////////////////////////////////////////////////////////////////
172 void NetAddress::
173 output(ostream &out) const {
174  out << get_ip_string();
175 }
176 
177 //////////////////////////////////////////////////////////////
178 // Function: NetAddress::get_hash
179 // Access: Published
180 // Description:
181 //////////////////////////////////////////////////////////////
182 size_t NetAddress::
183 get_hash() const {
184  return (size_t)(((int)get_ip()) ^ ((int)get_port() << 16));
185 }
186 
187 //////////////////////////////////////////////////////////////
188 // Function: NetAddress::operator ==
189 // Access: Published
190 // Description:
191 //////////////////////////////////////////////////////////////
192 bool NetAddress::
193 operator == (const NetAddress &other) const {
194  return _addr == other._addr;
195 }
196 
197 //////////////////////////////////////////////////////////////
198 // Function: NetAddress::operator !=
199 // Access: Published
200 // Description:
201 //////////////////////////////////////////////////////////////
202 bool NetAddress::
203 operator != (const NetAddress &other) const {
204  return _addr != other._addr;
205 }
unsigned long GetIPAddressRaw() const
Return a RAW sockaddr_in.
bool set_any(int port)
Sets the address up to refer to a particular port, but not to any particular IP.
Definition: netAddress.cxx:50
bool set_any_IP(int port)
Set to any address and a specified port.
bool set_broadcast(int port)
Sets the address to the broadcast address.
Definition: netAddress.cxx:71
string get_ip_string() const
Returns the IP address to which this address refers, formatted as a string.
Definition: netAddress.cxx:125
void set_port(int port)
Resets the port number without otherwise changing the address.
Definition: netAddress.cxx:114
int get_port() const
Returns the port number to which this address refers.
Definition: netAddress.cxx:103
void clear()
Resets the NetAddress to its initial state.
Definition: netAddress.cxx:93
bool set_host(const string &hostname, int port)
Sets the address up to refer to a particular port on a particular host.
Definition: netAddress.cxx:83
NetAddress()
Constructs an unspecified address.
Definition: netAddress.cxx:25
unsigned short get_port() const
Get the port portion as an integer.
PN_uint32 get_ip() const
Returns the IP address to which this address refers, as a 32-bit integer, in host byte order...
Definition: netAddress.cxx:136
bool set_localhost(int port)
Sets the address up to refer to a particular port, on this host.
Definition: netAddress.cxx:61
void clear()
Set the internal values to a suitable known value.
A simple place to store and munipulate tcp and port address for communication layer.
bool set_host(const std::string &hostname, int port)
This function will take a port and string-based TCP address and initialize the address with this info...
std::string get_ip() const
Return the IP address portion in dot notation string.
const Socket_Address & get_addr() const
Returns the Socket_Address for this address.
Definition: netAddress.cxx:163
PN_uint8 get_ip_component(int n) const
Returns the nth 8-bit component of the IP address.
Definition: netAddress.cxx:149
bool set_broadcast(int port)
Set to the broadcast address and a specified port.
Represents a network address to which UDP packets may be sent or to which a TCP socket may be bound...
Definition: netAddress.h:27
bool set_port(int port)
Set to a specified port.