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