Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/lib/network/ip.cc @ 9326

Last change on this file since 9326 was 9321, checked in by bensch, 18 years ago

more elaborate IP

File size: 4.8 KB
Line 
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#include "ip.h"
9
10#include <iostream>
11
12#include "multi_type.h"
13#include "substring.h"
14
15/**
16 * @brief default constructor
17 */
18IP::IP()
19{
20  this->_ip = 0;
21  this->_port = IP::_defaultPort;
22}
23
24
25/**
26 * @brief constructor from ip and port
27 * @param ip the IP
28 * @param port the Port
29 * @return self
30 */
31IP::IP(int ip, int port)
32{
33  this->_ip = ip;
34  if (port != -1)
35    this->_port = port;
36  else
37    this->_port = IP::_defaultPort;
38}
39
40
41/**
42 * @brief constructor from a String
43 * @param ip the IP as a String.
44 * @param resolve if true, the IP is resolved via DNS,
45 * @return self
46 */
47IP::IP(const std::string& ip, bool resolve)
48{
49  *this = IP::stringToIP(ip, IP::_defaultPort, resolve);
50}
51
52/**
53 * @brief constructor from a String
54 * @param ip the IP as a String.
55 * @param port The port to be resolved.
56 * @param resolve if true, the IP is resolved via DNS,
57 * @return self
58 */
59IP::IP(const std::string& ip, int port, bool resolve)
60{
61  *this = IP::stringToIP(ip, port, resolve);
62}
63
64
65/**
66 * @brief constructor from an SDL net structure IPaddress
67 * @param ip the ip.
68 * @return self
69 */
70IP::IP(const IPaddress& ip)
71{
72  this->_ip = ip.host;
73  this->_port = ip.port;
74}
75
76
77/**
78 * @brief copy constructor.
79 * @param ip the IP to copy.
80 * @return self
81 */
82IP::IP(const IP& ip)
83{
84  *this = ip;
85}
86
87/**
88 * @brief copy operator
89 * @param ip the IP to copy.
90 * @return self.
91 */
92const IP& IP::operator=(const IP& ip)
93{
94  this->_ip = ip.ip();
95  this->_port = ip.port();
96  return *this;
97}
98
99/**
100 * @brief comparison operator
101 * @param ip the IP to compare
102 * @return true if ip _and_ port match.
103 */
104bool IP::operator==(const IP& ip)
105{
106  return (this->_ip == ip.ip() &&
107          this->_port == ip.port());
108}
109
110
111/**
112 * @brief converts a String into an IP Object.
113 * @param ip The string holding an IP.
114 * @param port The port to be resolved.
115 * @param resolve if true, the IP is resolved via DNS,
116 * otherwise (resolve == false) the IP is being transformed
117 * from a String (xxx.xxx.xxx.xxx) to an integer.
118 *
119 * @return A newly constructed IP.
120 */
121IP IP::stringToIP(const std::string& ip, int port, bool resolve)
122{
123  if (resolve)
124  {
125    IPaddress ipaddr;
126
127    SDLNet_ResolveHost(&ipaddr, NULL, 2000);
128
129    return IP(ipaddr.host, port);
130  }
131  else
132  {
133    SubString ipaddr(ip, '.');
134    if(ip.size() != 4 )
135      return IP();
136
137    MultiType part0(ipaddr[0]);
138    MultiType part1(ipaddr[1]);
139    MultiType part2(ipaddr[2]);
140    MultiType part3(ipaddr[3]);
141
142    int addr = (part0.getInt() << 24) +
143               (part1.getInt() << 16) +
144               (part2.getInt() <<  8) +
145               part3.getInt();
146    return IP(addr, port);
147  }
148}
149
150
151/**
152 * @brief if you want to have a specific part of an IP
153 * @param part the n'th part of the IP addr (splitted by '.').
154 * @return the amount held in the designated part.
155 */
156int IP::ipPart(unsigned int part) const
157{
158  switch (part)
159  {
160    case 0:
161      return  (_ip & 0xFF000000) >> 24;
162    case 1:
163      return  (_ip & 0x00FF0000) >> 16;
164    case 2:
165      return  (_ip & 0x0000FF00) >> 8;
166    case 3:
167      return  (_ip & 0x000000FF);
168    default:
169      return -1;
170  }
171}
172
173
174/**
175 * @return the Ip as a string.
176 */
177std::string IP::ipString() const
178{
179  return IP::ipToString(this->_ip, this->_port);
180}
181
182
183/**
184 * @brief converts an IPaddress struct into a String.
185 * @param ipaddr the IP address as a SDL_net struct.
186 * @return the string retrieved from the IP.
187 */
188std::string IP::ipToString(const IPaddress& ipaddr)
189{
190  int ip = SDLNet_Read32 (ipaddr.host);
191  return IP::ipToString(ip, ipaddr.port);
192}
193
194
195/**
196 * @brief converts a IP into a String (without port).
197 * @param ip the IP to put into the string.
198 * @param port -1 if not wanted
199 * @return the string of the ip.
200 */
201std::string IP::ipToString(int ip, int port)
202{
203  MultiType part0((int) (ip & 0xFF000000) >> 24);
204  MultiType part1((int) (ip & 0x00FF0000) >> 16);
205  MultiType part2((int) (ip & 0x0000FF00) >>  8);
206  MultiType part3((int) (ip & 0x000000FF) );
207
208  std::string addr = part0.getString() + "." + part1.getString() + "." +
209                     part2.getString() + "." + part3.getString();
210
211  if (port != -1)
212    addr += ":" + MultiType(port).getString();
213  return addr;
214}
215
216
217/// default port definition.
218short IP::_defaultPort = 80;
219
220/**
221 * @brief if no IP is supplied this port is used, so that IP can be resolved usefully.
222 * @param defaultPort The default port.
223 */
224void IP::setDefaultPort(short defaultPort)
225{
226  IP::_defaultPort = defaultPort;
227}
228
229
230/**
231 * @brief print out the IP in a nice fashion
232 */
233void IP::debug() const
234{
235  std::cout << "IP is " << this->ipString() << std::endl;
236}
Note: See TracBrowser for help on using the repository browser.