Panda3D
Loading...
Searching...
No Matches
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 */
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 */
31NetAddress(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 */
41set_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 */
49set_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 */
57set_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 */
66set_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 */
74clear() {
75 _addr.clear();
76}
77
78/**
79 * Returns the port number to which this address refers.
80 */
82get_port() const {
83 return _addr.get_port();
84}
85
86/**
87 * Resets the port number without otherwise changing the address.
88 */
90set_port(int port) {
91 _addr.set_port(port);
92}
93
94/**
95 * Returns true if the IP address has only zeroes.
96 */
98is_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 */
105std::string NetAddress::
106get_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 */
116get_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 */
126get_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 */
138get_addr() const {
139 return _addr;
140}
141
142/**
143 *
144 */
145void NetAddress::
146output(std::ostream &out) const {
147 out << _addr.get_ip_port();
148}
149
150/**
151 *
152 */
153size_t NetAddress::
154get_hash() const {
155 return (size_t)(((int)get_ip()) ^ ((int)get_port() << 16));
156}
157
158/**
159 *
160 */
161bool NetAddress::
162operator == (const NetAddress &other) const {
163 return _addr == other._addr;
164}
165
166/**
167 *
168 */
169bool NetAddress::
170operator != (const NetAddress &other) const {
171 return _addr != other._addr;
172}
Represents a network address to which UDP packets may be sent or to which a TCP socket may be bound.
Definition netAddress.h:25
void clear()
Resets the NetAddress to its initial state.
bool set_host(const std::string &hostname, int port)
Sets the address up to refer to a particular port on a particular host.
bool set_localhost(int port)
Sets the address up to refer to a particular port, on this host.
std::string get_ip_string() const
Returns the IP address to which this address refers, formatted as a string.
int get_port() const
Returns the port number to which this address refers.
bool set_any(int port)
Sets the address up to refer to a particular port, but not to any particular IP.
const Socket_Address & get_addr() const
Returns the Socket_Address for this address.
bool is_any() const
Returns true if the IP address has only zeroes.
uint8_t get_ip_component(int n) const
Returns the nth 8-bit component of the IP address.
uint32_t get_ip() const
Returns the IP address to which this address refers, as a 32-bit integer, in host byte order.
bool set_broadcast(int port)
Sets the address to the broadcast address.
NetAddress()
Constructs an unspecified address.
void set_port(int port)
Resets the port number without otherwise changing the address.
A simple place to store and manipulate tcp and port address for communication layer.
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...
bool set_broadcast(unsigned short port)
Set to the broadcast address and a specified port.
void clear()
Set the internal values to a suitable known value.
std::string get_ip() const
Return the IP address portion in dot notation string.
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_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.
unsigned short get_port() const
Get the port portion as an integer.
bool set_port(unsigned short port)
Set to a specified port.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.