Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/network/ip.cc @ 9416

Last change on this file since 9416 was 9406, checked in by bensch, 18 years ago

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

File size: 5.9 KB
RevLine 
[9344]1/**
2 * @file ip.cc
3 * @brief A IP class, that handles IP names and resolution.
4 *
5 * code taken from audiere.
6 *
7 *
8   orxonox - the future of 3D-vertical-scrollers
9
10   Copyright (C) 2004 orx
11
12   This program is free software; you can redistribute it and/or modify
13   it under the terms of the GNU General Public License as published by
14   the Free Software Foundation; either version 2, or (at your option)
15   any later version.
16
17### File Specific:
18   main-programmer: Benjamin Grauer
19 * co-programmer: Patrick Boenzli
20 */
21
22#include "ip.h"
23
24#include <iostream>
25
26#include "multi_type.h"
27#include "substring.h"
28
29/**
30 * @brief default constructor
31 */
32IP::IP()
33{
[9385]34  this->_host = 0;
[9344]35  this->_port = IP::_defaultPort;
36}
37
38
39/**
40 * @brief constructor from ip and port
41 * @param ip the IP
42 * @param port the Port
43 * @return self
44 */
45IP::IP(int ip, int port)
46{
[9385]47  this->_host = ip;
[9344]48  if (port != -1)
49    this->_port = port;
50  else
51    this->_port = IP::_defaultPort;
52}
53
54
55/**
56 * @brief constructor from a String
57 * @param ip the IP as a String.
58 * @param resolve if true, the IP is resolved via DNS,
59 * @return self
60 */
61IP::IP(const std::string& ip, bool resolve)
62{
63  *this = IP::stringToIP(ip, IP::_defaultPort, resolve);
64}
65
66/**
67 * @brief constructor from a String
68 * @param ip the IP as a String.
69 * @param port The port to be resolved.
70 * @param resolve if true, the IP is resolved via DNS,
71 * @return self
72 */
73IP::IP(const std::string& ip, int port, bool resolve)
74{
75  *this = IP::stringToIP(ip, port, resolve);
76}
77
78
79/**
80 * @brief constructor from an SDL net structure IPaddress
81 * @param ip the ip.
82 * @return self
83 */
84IP::IP(const IPaddress& ip)
85{
[9385]86  this->_host = ip.host;
[9344]87  this->_port = ip.port;
88}
89
90
91/**
92 * @brief copy constructor.
93 * @param ip the IP to copy.
94 * @return self
95 */
96IP::IP(const IP& ip)
97{
98  *this = ip;
99}
100
[9385]101
[9344]102/**
[9385]103 * @brief constructs an IP out of the four pairs and a Port.
104 * @param first The first octal.
105 * @param second The second octal.
106 * @param third The third octal.
107 * @param fourth The fourth octal.
108 * @param port The Port.
109 * @return self.
110 */
111IP::IP(unsigned int first, unsigned int second, unsigned int third, unsigned int fourth, int port)
112{
113  this->_host = (first << 24) +
114                (second << 16) +
115                (third <<  8) +
116                fourth;
117  this->_port = port;
118}
119
120
121/**
[9344]122 * @brief copy operator
123 * @param ip the IP to copy.
124 * @return self.
125 */
126const IP& IP::operator=(const IP& ip)
127{
[9385]128  this->_host = ip.host();
[9344]129  this->_port = ip.port();
130  return *this;
131}
132
133
134/**
135 * @brief copy operator
136 * @param ip the IP to copy.
137 * @return self.
138 */
139const IP& IP::operator=(const IPaddress& ip)
140{
[9385]141  this->_host = ip.host;
[9344]142  this->_port = ip.port;
143  return *this;
144}
145
146
147/**
148 * @brief comparison operator
149 * @param ip the IP to compare
150 * @return true if ip _and_ port match.
151 */
[9383]152bool IP::operator==(const IP& ip) const
[9344]153{
[9385]154  return (this->_host == ip.host() &&
[9344]155          this->_port == ip.port());
156}
157
158
159/**
160 * @brief comparison operator
161 * @param ip the IP to compare
[9383]162 * @return true if ip _or_ port do not match.
[9344]163 */
[9383]164bool IP::operator!=(const IP& ip) const
[9344]165{
[9385]166  return (this->_host != ip.host() ||
167          this->_port != ip.port());
[9344]168}
169
170
171/**
172 * @brief converts a String into an IP Object.
173 * @param ip The string holding an IP.
174 * @param port The port to be resolved.
175 * @param resolve if true, the IP is resolved via DNS,
176 * otherwise (resolve == false) the IP is being transformed
177 * from a String (xxx.xxx.xxx.xxx) to an integer.
178 *
179 * @return A newly constructed IP.
180 */
181IP IP::stringToIP(const std::string& ip, int port, bool resolve)
182{
183  if (resolve)
184  {
185    IPaddress ipaddr;
186
187    SDLNet_ResolveHost(&ipaddr, NULL, 2000);
188
189    return IP(ipaddr.host, port);
190  }
191  else
192  {
193    SubString ipaddr(ip, '.');
194    if(ip.size() != 4 )
195      return IP();
196
197    MultiType part0(ipaddr[0]);
198    MultiType part1(ipaddr[1]);
199    MultiType part2(ipaddr[2]);
200    MultiType part3(ipaddr[3]);
201
202    int addr = (part0.getInt() << 24) +
203               (part1.getInt() << 16) +
204               (part2.getInt() <<  8) +
205               part3.getInt();
206    return IP(addr, port);
207  }
208}
209
210
211/**
212 * @brief if you want to have a specific part of an IP
213 * @param part the n'th part of the IP addr (splitted by '.').
214 * @return the amount held in the designated part.
215 */
216int IP::ipPart(unsigned int part) const
217{
218  switch (part)
219  {
220    case 0:
[9385]221      return  (_host & 0xFF000000) >> 24;
[9344]222    case 1:
[9385]223      return  (_host & 0x00FF0000) >> 16;
[9344]224    case 2:
[9385]225      return  (_host & 0x0000FF00) >> 8;
[9344]226    case 3:
[9385]227      return  (_host & 0x000000FF);
[9344]228    default:
229      return -1;
230  }
231}
232
233
234/**
235 * @return the Ip as a string.
236 */
237std::string IP::ipString() const
238{
[9385]239  return IP::ipToString(this->_host, this->_port);
[9344]240}
241
242
243/**
244 * @brief converts an IPaddress struct into a String.
245 * @param ipaddr the IP address as a SDL_net struct.
246 * @return the string retrieved from the IP.
247 */
248std::string IP::ipToString(const IPaddress& ipaddr)
249{
250  int ip = SDLNet_Read32 (ipaddr.host);
251  return IP::ipToString(ip, ipaddr.port);
252}
253
254
255/**
256 * @brief converts a IP into a String (without port).
257 * @param ip the IP to put into the string.
258 * @param port -1 if not wanted
259 * @return the string of the ip.
260 */
261std::string IP::ipToString(int ip, int port)
262{
263  MultiType part0((int) (ip & 0xFF000000) >> 24);
264  MultiType part1((int) (ip & 0x00FF0000) >> 16);
265  MultiType part2((int) (ip & 0x0000FF00) >>  8);
266  MultiType part3((int) (ip & 0x000000FF) );
267
268  std::string addr = part3.getString() + "." + part2.getString() + "." +
269                     part1.getString() + "." + part0.getString();
270
271  if (port != -1)
272    addr += ":" + MultiType(port).getString();
273  return addr;
274}
275
276
277/// default port definition.
[9360]278int IP::_defaultPort = 9999;
[9344]279
280/**
281 * @brief if no IP is supplied this port is used, so that IP can be resolved usefully.
282 * @param defaultPort The default port.
283 */
[9360]284void IP::setDefaultPort(int defaultPort)
[9344]285{
286  IP::_defaultPort = defaultPort;
287}
288
289
290/**
291 * @brief print out the IP in a nice fashion
292 */
293void IP::debug() const
294{
295  std::cout << "IP is " << this->ipString() << std::endl;
296}
Note: See TracBrowser for help on using the repository browser.