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
RevLine 
[7899]1/**
[9304]2 * @file ip.cc
[9321]3 * @brief A IP class, that handles IP names and resolution.
[7899]4 *
5 * code taken from audiere.
6 */
7
[9304]8#include "ip.h"
[9321]9
10#include <iostream>
11
[9305]12#include "multi_type.h"
[9306]13#include "substring.h"
[7899]14
[9307]15/**
16 * @brief default constructor
17 */
[9304]18IP::IP()
[9306]19{
20  this->_ip = 0;
[9321]21  this->_port = IP::_defaultPort;
[9306]22}
[7899]23
24
[9307]25/**
26 * @brief constructor from ip and port
27 * @param ip the IP
28 * @param port the Port
29 * @return self
30 */
[9306]31IP::IP(int ip, int port)
[9304]32{
33  this->_ip = ip;
[9321]34  if (port != -1)
35    this->_port = port;
36  else
37    this->_port = IP::_defaultPort;
[7909]38}
39
[9306]40
[9307]41/**
42 * @brief constructor from a String
43 * @param ip the IP as a String.
[9321]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.
[9310]55 * @param port The port to be resolved.
56 * @param resolve if true, the IP is resolved via DNS,
[9307]57 * @return self
58 */
[9310]59IP::IP(const std::string& ip, int port, bool resolve)
[9304]60{
[9310]61  *this = IP::stringToIP(ip, port, resolve);
[9304]62}
[7899]63
[7909]64
[9307]65/**
66 * @brief constructor from an SDL net structure IPaddress
67 * @param ip the ip.
68 * @return self
69 */
[9306]70IP::IP(const IPaddress& ip)
71{
72  this->_ip = ip.host;
73  this->_port = ip.port;
74}
[9305]75
76
[9307]77/**
78 * @brief copy constructor.
79 * @param ip the IP to copy.
80 * @return self
81 */
[9306]82IP::IP(const IP& ip)
[9304]83{
[9321]84  *this = ip;
[9306]85}
[7899]86
[9307]87/**
88 * @brief copy operator
89 * @param ip the IP to copy.
90 * @return self.
91 */
[9306]92const IP& IP::operator=(const IP& ip)
93{
94  this->_ip = ip.ip();
95  this->_port = ip.port();
96  return *this;
[9305]97}
[9304]98
[9307]99/**
100 * @brief comparison operator
101 * @param ip the IP to compare
102 * @return true if ip _and_ port match.
103 */
[9306]104bool IP::operator==(const IP& ip)
105{
[9321]106  return (this->_ip == ip.ip() &&
107          this->_port == ip.port());
[9306]108}
109
110
111/**
[9307]112 * @brief converts a String into an IP Object.
113 * @param ip The string holding an IP.
[9310]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.
[9306]120 */
[9310]121IP IP::stringToIP(const std::string& ip, int port, bool resolve)
[9306]122{
[9310]123  if (resolve)
124  {
125    IPaddress ipaddr;
[9306]126
[9310]127    SDLNet_ResolveHost(&ipaddr, NULL, 2000);
[9306]128
[9310]129    return IP(ipaddr.host, port);
130  }
131  else
132  {
133    SubString ipaddr(ip, '.');
134    if(ip.size() != 4 )
135      return IP();
[9306]136
[9310]137    MultiType part0(ipaddr[0]);
138    MultiType part1(ipaddr[1]);
139    MultiType part2(ipaddr[2]);
140    MultiType part3(ipaddr[3]);
[9306]141
[9310]142    int addr = (part0.getInt() << 24) +
143               (part1.getInt() << 16) +
144               (part2.getInt() <<  8) +
145               part3.getInt();
146    return IP(addr, port);
147  }
[9306]148}
149
[9307]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 */
[9306]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
[9307]174/**
175 * @return the Ip as a string.
176 */
[9306]177std::string IP::ipString() const
178{
[9321]179  return IP::ipToString(this->_ip, this->_port);
[9306]180}
181
182
[9307]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 */
[9305]188std::string IP::ipToString(const IPaddress& ipaddr)
189{
190  int ip = SDLNet_Read32 (ipaddr.host);
[9321]191  return IP::ipToString(ip, ipaddr.port);
[7899]192}
[9304]193
[9305]194
[9307]195/**
[9310]196 * @brief converts a IP into a String (without port).
[9307]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)
[9304]202{
[9306]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) );
[9304]207
[9307]208  std::string addr = part0.getString() + "." + part1.getString() + "." +
209                     part2.getString() + "." + part3.getString();
[9305]210
[9307]211  if (port != -1)
[9321]212    addr += ":" + MultiType(port).getString();
[9307]213  return addr;
[9304]214}
215
[9321]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.