Panda3D
 All Classes Functions Variables Enumerations
socket_address.I
1 // Filename: socket_address.I
2 // Created by: rdb (19Oct14)
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 
16 //////////////////////////////////////////////////////////////
17 // Function: Socket_Address::GetIPAdddressRaw
18 // Access: Public
19 // Description: Return a RAW sockaddr_in
20 //////////////////////////////////////////////////////////////
21 INLINE unsigned long Socket_Address::
22 GetIPAddressRaw() const {
23  return _addr.sin_addr.s_addr;
24 }
25 
26 ///////////////////////////////////////////////////////////////////
27 // Function: Socket_Address
28 // Access: Published
29 // Description: Constructor that lets us set a port value
30 ////////////////////////////////////////////////////////////////////
31 INLINE Socket_Address::
32 Socket_Address(unsigned short port) {
33  _addr.sin_family = AF_INET;
34  _addr.sin_addr.s_addr = INADDR_ANY;
35  _addr.sin_port = htons(port);
36 }
37 
38 ////////////////////////////////////////////////////////////////////
39 // Function: Socket_Address::Copy constructor
40 // Access: Published
41 // Description:
42 ////////////////////////////////////////////////////////////////////
43 INLINE Socket_Address::
44 Socket_Address(const Socket_Address &inaddr) {
45  _addr.sin_family = inaddr._addr.sin_family;
46  _addr.sin_addr.s_addr = inaddr._addr.sin_addr.s_addr;
47  _addr.sin_port = inaddr._addr.sin_port;
48 }
49 
50 ////////////////////////////////////////////////////////////////////
51 // Function: Socket_Address::Constructor
52 // Access: Public
53 // Description:
54 ////////////////////////////////////////////////////////////////////
55 INLINE Socket_Address::
56 Socket_Address(const AddressType &inaddr) {
57  _addr.sin_family = inaddr.sin_family;
58  _addr.sin_addr.s_addr = inaddr.sin_addr.s_addr;
59  _addr.sin_port = inaddr.sin_port;
60 }
61 
62 ////////////////////////////////////////////////////////////////////
63 // Function: Socket_Address::~Destructor
64 // Access: Published
65 // Description: Normal Destructor
66 ////////////////////////////////////////////////////////////////////
67 INLINE Socket_Address::
69 }
70 
71 //////////////////////////////////////////////////////////////
72 // Function: Socket_Address::operator ==
73 // Access: Published
74 // Description:
75 //////////////////////////////////////////////////////////////
76 INLINE bool Socket_Address::
77 operator == (const Socket_Address &in) const {
78  return ((_addr.sin_family == in._addr.sin_family) &&
79  (_addr.sin_addr.s_addr == in._addr.sin_addr.s_addr) &&
80  (_addr.sin_port == in._addr.sin_port));
81 }
82 
83 //////////////////////////////////////////////////////////////
84 // Function: Socket_Address::operator !=
85 // Access: Published
86 // Description:
87 //////////////////////////////////////////////////////////////
88 INLINE bool Socket_Address::
89 operator != (const Socket_Address &in) const {
90  return ((_addr.sin_family != in._addr.sin_family) ||
91  (_addr.sin_addr.s_addr != in._addr.sin_addr.s_addr) ||
92  (_addr.sin_port != in._addr.sin_port));
93 }
94 
95 ////////////////////////////////////////////////////////////////////
96 // Function: Socket_Address::set_broadcast
97 // Access: Published
98 // Description: Set to the broadcast address and a specified port
99 ////////////////////////////////////////////////////////////////////
100 INLINE bool Socket_Address::
101 set_broadcast(int port) {
102  _addr.sin_family = AF_INET;
103  _addr.sin_addr.s_addr = 0xffffffff;
104  _addr.sin_port = htons(port);
105  return true;
106 }
107 
108 ////////////////////////////////////////////////////////////////////
109 // Function: Socket_Address::set_any_IP
110 // Access: Published
111 // Description: Set to any address and a specified port
112 ////////////////////////////////////////////////////////////////////
113 INLINE bool Socket_Address::
114 set_any_IP(int port) {
115  _addr.sin_family = AF_INET;
116  _addr.sin_addr.s_addr = INADDR_ANY;
117  _addr.sin_port = htons(port);
118  return true;
119 }
120 
121 ////////////////////////////////////////////////////////////////////
122 // Function: Socket_Address::set_port
123 // Access: Published
124 // Description: Set to a specified port
125 ////////////////////////////////////////////////////////////////////
126 INLINE bool Socket_Address::
127 set_port(int port) {
128  _addr.sin_port = htons(port);
129  return true;
130 }
131 
132 ////////////////////////////////////////////////////////////////////
133 // Function: Socket_Address::clear
134 // Access: Published
135 // Description: Set the internal values to a suitable known value
136 ////////////////////////////////////////////////////////////////////
137 INLINE void Socket_Address::
138 clear() {
139  _addr.sin_family = AF_INET;
140  _addr.sin_addr.s_addr = INADDR_ANY;
141  _addr.sin_port = htons(0);
142 }
143 
144 ////////////////////////////////////////////////////////////////////
145 // Function: Socket_Address::get_port
146 // Access: Published
147 // Description: Get the port portion as an integer
148 ////////////////////////////////////////////////////////////////////
149 INLINE unsigned short Socket_Address::
150 get_port() const {
151  return ntohs(_addr.sin_port);
152 }
153 
154 ////////////////////////////////////////////////////////////////////
155 // Function: Socket_Address::get_ip
156 // Access: Published
157 // Description: Return the IP address portion in dot notation string
158 ////////////////////////////////////////////////////////////////////
159 INLINE std::string Socket_Address::
160 get_ip() const {
161  return std::string(inet_ntoa(_addr.sin_addr));
162 }
163 
164 ////////////////////////////////////////////////////////////////////
165 // Function: Socket_Address::get_ip_port
166 // Access: Published
167 // Description: Return the ip address/port in dot notation string
168 ////////////////////////////////////////////////////////////////////
169 INLINE std::string Socket_Address::
170 get_ip_port() const {
171  char buf1[100]; // 100 is more than enough for any ip address:port combo..
172  sprintf(buf1, "%s:%d", inet_ntoa(_addr.sin_addr), get_port());
173  return std::string(buf1);
174 }
175 
176 ////////////////////////////////////////////////////////////////////
177 // Function: Socket_Address::set_host
178 // Access: Published
179 // Description: This function will take a port and string-based
180 // TCP address and initialize the address with this
181 // information. Returns true on success; on failure,
182 // it returns false and the address may be undefined.
183 ////////////////////////////////////////////////////////////////////
184 INLINE bool Socket_Address::
185 set_host(const std::string &hostname, int port) {
186  struct hostent *hp = NULL;
187 
188  //
189  // hmm inet_addr does not resolve 255.255.255.255 on ME/98 ??
190  //
191  // * HACK * ??
192  if (hostname == "255.255.255.255") {
193  return set_broadcast(port);
194  }
195  //
196  //
197 
198  PN_uint32 addr = (long)inet_addr(hostname.c_str());
199  if (addr == INADDR_NONE) {
200  hp = gethostbyname(hostname.c_str());
201  if (hp == NULL) {
202  return false;
203  } else {
204  memcpy(&_addr.sin_addr, hp->h_addr_list[0], (unsigned int) hp->h_length);
205  }
206  } else {
207  memcpy(&_addr.sin_addr, &addr, sizeof(addr));
208  }
209 
210  _addr.sin_port = htons(port);
211  _addr.sin_family = AF_INET;
212  return true;
213 }
214 
215 //////////////////////////////////////////////////////////////
216 // Function: Socket_Address::set_host
217 // Access: Published
218 // Description:
219 //////////////////////////////////////////////////////////////
220 INLINE bool Socket_Address::
221 set_host(const std::string &hostname) {
222  std::string::size_type pos = hostname.find(':');
223  if (pos == std::string::npos)
224  return false;
225 
226  std::string host = hostname.substr(0, pos);
227  std::string port = hostname.substr(pos + 1, 100);;
228 
229  int port_dig = atoi(port.c_str());
230  return set_host(host, port_dig);
231 }
232 
233 //////////////////////////////////////////////////////////////
234 // Function: Socket_Address::set_host
235 // Access: Published
236 // Description:
237 //////////////////////////////////////////////////////////////
238 INLINE bool Socket_Address::
239 set_host(PN_uint32 in_hostname, int port) {
240  memcpy(&_addr.sin_addr, &in_hostname, sizeof(in_hostname));
241  _addr.sin_port = htons(port);
242  _addr.sin_family = AF_INET;
243  return true;
244 }
245 
246 //////////////////////////////////////////////////////////////
247 // Function: <
248 // Access: Published
249 // Description:
250 //////////////////////////////////////////////////////////////
251 INLINE bool Socket_Address::
252 operator < (const Socket_Address &in) const {
253  if (_addr.sin_port < in._addr.sin_port)
254  return true;
255 
256  if (_addr.sin_port > in._addr.sin_port)
257  return false;
258 
259  if (_addr.sin_addr.s_addr < in._addr.sin_addr.s_addr)
260  return true;
261 
262  if (_addr.sin_addr.s_addr > in._addr.sin_addr.s_addr)
263  return false;
264 
265  return (_addr.sin_family < in._addr.sin_family);
266 }
267 
268 //////////////////////////////////////////////////////////////
269 // Function: is_mcast_range
270 // Access: Published
271 // Description: True if the address is in the multicast range.
272 //////////////////////////////////////////////////////////////
273 INLINE bool Socket_Address::
274 is_mcast_range(void) const {
275  PN_uint32 address = ntohl(_addr.sin_addr.s_addr);
276  //224.0.0.0-239.255.255.255 .. e0,ef
277  return (address >= 0xe0000000 && address < 0xefffffff);
278 }
virtual ~Socket_Address()
Normal Destructor.
bool set_any_IP(int port)
Set to any address and a specified port.
unsigned short get_port() const
Get the port portion as an integer.
std::string get_ip_port() const
Return the ip address/port in dot notation string.
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.
unsigned long GetIPAddressRaw() const
Return a RAW sockaddr_in.
bool is_mcast_range() const
True if the address is in the multicast range.
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.
bool set_broadcast(int port)
Set to the broadcast address and a specified port.
bool set_port(int port)
Set to a specified port.